windows-talocale-20060829
[openafs.git] / src / WINNT / afsusrmgr / creds.cpp
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 extern "C" {
11 #include <afs/param.h>
12 #include <afs/stds.h>
13 }
14
15 #include "TaAfsUsrMgr.h"
16 #include "creds.h"
17
18
19 /*
20  * OPENCELL DIALOG ____________________________________________________________
21  *
22  */
23
24 void OpenCell_Hook_Enable (HWND hDlg, BOOL fEnable)
25 {
26    EnableWindow (GetDlgItem (hDlg, IDOK), fEnable);
27    EnableWindow (GetDlgItem (hDlg, IDCANCEL), fEnable);
28    EnableWindow (GetDlgItem (hDlg, IDHELP), fEnable);
29    EnableWindow (GetDlgItem (hDlg, IDC_OPENCELL_CELL), fEnable);
30    EnableWindow (GetDlgItem (hDlg, IDC_OPENCELL_ID), fEnable);
31    EnableWindow (GetDlgItem (hDlg, IDC_OPENCELL_PASSWORD), fEnable);
32 }
33
34
35 void OpenCell_Hook_OnEndTask_OpenCell (HWND hDlg, LPTASKPACKET ptp)
36 {
37    if (ptp->rc)
38       {
39       EndDialog (hDlg, IDOK);
40       }
41    else
42       {
43       OpenCell_Hook_Enable (hDlg, TRUE);
44
45       TCHAR szCell[ cchNAME ];
46       GetDlgItemText (hDlg, IDC_OPENCELL_CELL, szCell, cchNAME);
47       ErrorDialog (ptp->status, IDS_ERROR_CANT_OPEN_CELL, TEXT("%s"), szCell);
48       }
49 }
50
51
52 void OpenCell_Hook_OnOK (HWND hDlg, LPOPENCELLDLG_PARAMS lpp)
53 {
54    BOOL rc = FALSE;
55    OpenCell_Hook_Enable (hDlg, FALSE);
56    StartHourGlass ();
57
58    // Remember what cell the user chose to edit
59    //
60    GetDlgItemText (hDlg, IDC_OPENCELL_CELL, lpp->szCell, cchNAME);
61
62    // Try to obtain the credentials specified by the user.
63    //
64    TCHAR szCell[ cchNAME ];
65    GetDlgItemText (hDlg, IDC_OPENCELL_CELL, szCell, cchNAME);
66
67    TCHAR szUser[ cchRESOURCE ];
68    GetDlgItemText (hDlg, IDC_OPENCELL_ID, szUser, cchNAME);
69
70    TCHAR szPassword[ cchRESOURCE ];
71    GetDlgItemText (hDlg, IDC_OPENCELL_PASSWORD, szPassword, cchNAME);
72
73    ULONG status;
74    if ((lpp->hCreds = AfsAppLib_SetCredentials (lpp->szCell, szUser, szPassword, &status)) == NULL)
75       {
76       ErrorDialog (status, IDS_ERROR_BAD_CREDENTIALS);
77       }
78    else
79       {
80       // See if those credentials are sufficient
81       //
82       CHECKCREDS_PARAMS pp;
83       memset (&pp, 0x00, sizeof(pp));
84       memcpy (&pp.bcdp, &lpp->bcdp, sizeof(BADCREDSDLG_PARAMS));
85       pp.bcdp.hParent = hDlg;
86       pp.hCreds = lpp->hCreds;
87       pp.fShowWarning = TRUE;
88
89       if ((rc = AfsAppLib_CheckCredentials (&pp)) == FALSE)
90          {
91          SetDlgItemText (hDlg, IDC_OPENCELL_ID, TEXT("admin"));
92          PostMessage (hDlg, WM_NEXTDLGCTL, (WPARAM)GetDlgItem(hDlg,IDC_OPENCELL_PASSWORD), TRUE);
93          }
94       }
95
96    if (rc)
97       {
98       g.hCreds = lpp->hCreds;
99
100       // Instead of closing the dialog, start an taskOPENCELL task;
101       // we'll wait for that to complete successfully before we'll
102       // close the dialog.
103       //
104       LPOPENCELL_PARAMS lpocp = New (OPENCELL_PARAMS);
105       memset (lpocp, 0x00, sizeof(OPENCELL_PARAMS));
106       lstrcpy (lpocp->szCell, lpp->szCell);
107       lpocp->fCloseAppOnFail = FALSE;
108       lpocp->hCreds = lpp->hCreds;
109       StartTask (taskOPENCELL, hDlg, lpocp);
110       }
111
112    if (!rc)
113       OpenCell_Hook_Enable (hDlg, TRUE);
114    StopHourGlass ();
115 }
116
117
118 BOOL CALLBACK OpenCell_Hook (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp)
119 {
120    switch (msg)
121       {
122       case WM_ENDTASK:
123          LPTASKPACKET ptp;
124          if ((ptp = (LPTASKPACKET)lp) != NULL)
125             {
126             if (ptp->idTask == taskOPENCELL)
127                OpenCell_Hook_OnEndTask_OpenCell (hDlg, ptp);
128             FreeTaskPacket (ptp);
129             }
130          break;
131
132       case WM_COMMAND:
133          switch (LOWORD(wp))
134             {
135             case IDOK:
136                OpenCell_Hook_OnOK (hDlg, (LPOPENCELLDLG_PARAMS)GetWindowLongPtr (hDlg, DWLP_USER));
137                return TRUE;
138             }
139          break;
140       }
141
142    return FALSE;
143 }
144
145
146 /*
147  * CREDENTIALS ________________________________________________________________
148  *
149  */
150
151 void GetBadCredsDlgParams (LPBADCREDSDLG_PARAMS lpp)
152 {
153    memset (lpp, 0x00, sizeof(BADCREDSDLG_PARAMS));
154    lpp->pfShowWarningEver = &gr.fWarnBadCreds;
155    lpp->idsDesc = IDS_BADCREDS_DESC;
156 }
157
158
159 void GetCredentialsDlgParams (LPCREDENTIALSDLG_PARAMS lpp)
160 {
161    memset (lpp, 0x00, sizeof(CREDENTIALSDLG_PARAMS));
162    lpp->hParent = g.hMain;
163    lpp->hCreds = g.hCreds;
164    if (g.idCell)
165       asc_CellNameGet_Fast (g.idClient, g.idCell, lpp->szCell);
166    else
167       AfsAppLib_GetLocalCell (lpp->szCell);
168    GetBadCredsDlgParams (&lpp->bcdp);
169 }
170
171
172 /*
173  * OPERATIONS _________________________________________________________________
174  *
175  */
176
177 BOOL OpenCellDialog (void)
178 {
179    OPENCELLDLG_PARAMS pp;
180    memset (&pp, 0x00, sizeof(pp));
181    pp.hookproc = (DLGPROC)OpenCell_Hook;
182    pp.hParent = (IsWindowVisible (g.hMain)) ? g.hMain : NULL;
183    pp.idsDesc = 0;
184    pp.hCreds = g.hCreds;
185    pp.lpcl = AfsAppLib_GetCellList (HKCU, REGSTR_SETTINGS_CELLS);
186    GetBadCredsDlgParams (&pp.bcdp);
187
188    BOOL rc = AfsAppLib_ShowOpenCellDialog (&pp);
189
190    AfsAppLib_FreeCellList (pp.lpcl);
191    return rc;
192 }
193
194
195 BOOL NewCredsDialog (void)
196 {
197    CREDENTIALSDLG_PARAMS pp;
198    GetCredentialsDlgParams (&pp);
199    return AfsAppLib_ShowCredentialsDialog (&pp);
200 }
201
202
203 void CheckForExpiredCredentials (void)
204 {
205    CREDENTIALSDLG_PARAMS pp;
206    GetCredentialsDlgParams (&pp);
207    AfsAppLib_CheckForExpiredCredentials (&pp);
208 }
209
210
211 BOOL CheckCredentials (BOOL fComplain)
212 {
213    CHECKCREDS_PARAMS pp;
214    memset (&pp, 0x00, sizeof(pp));
215    pp.fShowWarning = fComplain;
216    pp.hCreds = g.hCreds;
217    GetBadCredsDlgParams (&pp.bcdp);
218
219    return AfsAppLib_CheckCredentials (&pp);
220 }
221
222
223 void ShowCurrentCredentials (void)
224 {
225    int idsCreds = IDS_CRED_NONE;
226    TCHAR szCell[ cchNAME ];
227    TCHAR szUser[ cchNAME ];
228    SYSTEMTIME stExpire;
229
230    if (g.hCreds)
231       {
232       if (AfsAppLib_CrackCredentials (g.hCreds, szCell, szUser, &stExpire))
233          {
234          if (AfsAppLib_IsTimeInFuture (&stExpire))
235             idsCreds = IDS_CRED_OK;
236          else
237             idsCreds = IDS_CRED_EXP;
238          }
239       }
240
241    LPTSTR pszCreds = FormatString (idsCreds, TEXT("%s%t"), szUser, &stExpire);
242    SetDlgItemText (g.hMain, IDC_CREDS, pszCreds);
243    FreeString (pszCreds);
244 }
245