windows-updates-20010108
[openafs.git] / src / WINNT / client_creds / main.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 "afscreds.h"
16 #include "..\afsreg\afsreg.h" // So we can see if the server's installed
17 #include "drivemap.h"
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <osilog.h>
21 #include "rxkad.h"
22
23 /*
24  * DEFINITIONS ________________________________________________________________
25  *
26  */
27
28 const TCHAR cszCLASSNAME[] = TEXT("AfsCreds");
29
30
31 /*
32  * VARIABLES __________________________________________________________________
33  *
34  */
35
36 GLOBALS g;
37
38
39 /*
40  * PROTOTYPES _________________________________________________________________
41  *
42  */
43
44 BOOL InitApp (LPSTR pszCmdLineA);
45 void ExitApp (void);
46 void Quit (void);
47 void PumpMessage (MSG *pmsg);
48 BOOL IsServerInstalled (void);
49
50
51 /*
52  * ROUTINES ___________________________________________________________________
53  *
54  */
55
56 int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrev, LPSTR pCmdLine, int nCmdShow)
57 {
58    Shortcut_Init();
59    TaLocale_LoadCorrespondingModule (hInst);
60
61    if (InitApp (pCmdLine))
62       {
63       MSG msg;
64       while (GetMessage (&msg, NULL, 0, 0) > 0)
65          {
66          PumpMessage (&msg);
67          }
68
69       ExitApp();
70       }
71
72    Shortcut_Exit();
73    return 0;
74 }
75
76 #define ISHIGHSECURITY(v) ( ((v) & LOGON_OPTION_HIGHSECURITY)==LOGON_OPTION_HIGHSECURITY)
77 #define REG_CLIENT_PROVIDER_KEY "SYSTEM\\CurrentControlSet\\Services\\TransarcAFSDaemon\\NetworkProvider"
78
79 BOOL InitApp (LPSTR pszCmdLineA)
80 {
81    BOOL fShow = FALSE;
82    BOOL fQuiet = FALSE;
83    BOOL fExit = FALSE;
84    BOOL fInstall = FALSE;
85    BOOL fUninstall = FALSE;
86
87    // Parse the command-line
88    //
89    while (pszCmdLineA && *pszCmdLineA)
90       {
91       if ((*pszCmdLineA != '-') && (*pszCmdLineA != '/'))
92          break;
93
94       switch (*(++pszCmdLineA))
95          {
96          case 's':
97          case 'S':
98             fShow = TRUE;
99             break;
100
101          case 'q':
102          case 'Q':
103             fQuiet = TRUE;
104             break;
105
106          case 'e':
107          case 'E':
108             fExit = TRUE;
109             break;
110
111          case 'i':
112          case 'I':
113             fInstall = TRUE;
114             break;
115
116          case 'u':
117          case 'U':
118             fUninstall = TRUE;
119             break;
120                  case ':':
121                          MapShareName(pszCmdLineA);
122                          break;
123          case 'x':
124          case 'X':
125              DWORD LogonOption;
126              DWORD LSPtype, LSPsize;
127              HKEY NPKey;
128              LSPsize=sizeof(LogonOption);
129              (void) RegOpenKeyEx(HKEY_LOCAL_MACHINE, REG_CLIENT_PROVIDER_KEY,
130                                  0, KEY_QUERY_VALUE, &NPKey);
131              RegQueryValueEx(NPKey, "LogonOptions", NULL,
132                              &LSPtype, (LPBYTE)&LogonOption, &LSPsize);
133              RegCloseKey (NPKey);
134              if (ISHIGHSECURITY(LogonOption))
135                  DoMapShare();
136              GlobalMountDrive();
137              return 0;
138          }
139
140       while (*pszCmdLineA && (*pszCmdLineA != ' '))
141          ++pszCmdLineA;
142           if (*pszCmdLineA==' ') ++pszCmdLineA;
143       }
144
145    if (fInstall)
146       Shortcut_FixStartup (cszSHORTCUT_NAME, g.fStartup = TRUE);
147    else if (fUninstall)
148       Shortcut_FixStartup (cszSHORTCUT_NAME, g.fStartup = FALSE);
149
150    if (fInstall)
151       {
152       HKEY hk;
153       if (RegCreateKey (HKEY_LOCAL_MACHINE, TEXT("System\\CurrentControlSet\\Services\\TransarcAFSDaemon\\Parameters"), &hk) == 0)
154          {
155          DWORD dwSize = sizeof(g.fStartup);
156          DWORD dwType = REG_DWORD;
157          RegSetValueEx (hk, TEXT("ShowTrayIcon"), NULL, dwType, (PBYTE)&g.fStartup, dwSize);
158          RegCloseKey (hk);
159          }
160       }
161
162    // Only show up if there's not another version of this app around already.
163    //
164    for (HWND hSearch = GetWindow (GetDesktopWindow(), GW_CHILD);
165         hSearch && IsWindow(hSearch);
166         hSearch = GetWindow (hSearch, GW_HWNDNEXT))
167       {
168       TCHAR szClassName[ cchRESOURCE ];
169       if (GetClassName (hSearch, szClassName, cchRESOURCE))
170          {
171          if (!lstrcmpi (szClassName, cszCLASSNAME))
172             {
173             if (fShow)
174                PostMessage (hSearch, WM_COMMAND, M_ACTIVATE, 0);
175             else if (fExit)
176                PostMessage (hSearch, WM_COMMAND, M_TERMINATE_NOW, 0);
177             else if (fUninstall)
178                PostMessage (hSearch, WM_COMMAND, M_TERMINATE_NOW, 0);
179             return FALSE;
180             }
181          }
182       }
183
184    if (fExit || fUninstall || fInstall)
185       return FALSE;
186
187    // Initialize our global variables and window classes
188    //
189    memset (&g, 0x00, sizeof(g));
190    g.fStartup = TRUE;
191
192    HKEY hk;
193    if (RegOpenKey (HKEY_LOCAL_MACHINE, TEXT("System\\CurrentControlSet\\Services\\TransarcAFSDaemon\\Parameters"), &hk) == 0)
194       {
195       DWORD dwSize = sizeof(g.fStartup);
196       DWORD dwType = REG_DWORD;
197       RegQueryValueEx (hk, TEXT("ShowTrayIcon"), NULL, &dwType, (PBYTE)&g.fStartup, &dwSize);
198       RegCloseKey (hk);
199       }
200
201    Shortcut_FixStartup (cszSHORTCUT_NAME, g.fStartup);
202
203    // Is this Windows NT?
204    //
205    OSVERSIONINFO Version;
206    memset (&Version, 0x00, sizeof(Version));
207    Version.dwOSVersionInfoSize = sizeof(Version);
208    if (GetVersionEx (&Version))
209       g.fIsWinNT = (Version.dwPlatformId == VER_PLATFORM_WIN32_NT) ? TRUE : FALSE;
210
211    if (!g.fIsWinNT)
212       lstrcpy (g.szHelpFile, TEXT("afs-light.hlp"));
213    else
214       lstrcpy (g.szHelpFile, TEXT("afs-nt.hlp"));
215
216    // Initialize winsock etc
217    //
218    WSADATA Data;
219    WSAStartup (0x0101, &Data);
220
221    InitCommonControls();
222    RegisterCheckListClass();
223
224    // Create a main window. All further initialization will be done during
225    // processing of WM_INITDIALOG.
226    //
227    WNDCLASS wc;
228    if (!GetClassInfo (NULL, WC_DIALOG, &wc))   // Get dialog class info
229       return FALSE;
230    wc.hInstance = THIS_HINST;
231    wc.hIcon = TaLocale_LoadIcon (IDI_MAIN);
232    wc.lpszClassName = cszCLASSNAME;
233    wc.style |= CS_GLOBALCLASS;
234    RegisterClass(&wc);
235
236    g.hMain = ModelessDialog (IDD_MAIN, NULL, (DLGPROC)Main_DlgProc);
237    if (g.hMain == NULL)
238       return FALSE;
239
240    // If the service isn't started yet, show our startup wizard.
241    //
242    if (!IsServiceRunning() && !fQuiet)
243       {
244       if (!g.fIsWinNT)
245          Message (MB_ICONHAND, IDS_UNCONFIG_TITLE_95, IDS_UNCONFIG_DESC_95);
246       else if (IsServiceConfigured())
247          ShowStartupWizard();
248       else if (!IsServerInstalled())
249          Message (MB_ICONHAND, IDS_UNCONFIG_TITLE, IDS_UNCONFIG_DESC);
250       }
251    if (IsServiceRunning() && fShow)
252       {
253       Main_Show (TRUE);
254       }
255
256    return TRUE;
257 }
258
259
260 void ExitApp (void)
261 {
262    g.hMain = NULL;
263 }
264
265
266 void PumpMessage (MSG *pmsg)
267 {
268    if (!IsMemoryManagerMessage (pmsg))
269       {
270       if (!IsDialogMessage (g.hMain, pmsg))
271          {
272          TranslateMessage (pmsg);
273          DispatchMessage (pmsg);
274          }
275       }
276 }
277
278
279 void Quit (void)
280 {
281    if (IsWindow (g.hMain))
282       {
283       ChangeTrayIcon (NIM_DELETE);
284       DestroyWindow (g.hMain);
285       }
286    PostQuitMessage (0);
287 }
288
289
290 BOOL IsServerInstalled (void)
291 {
292    BOOL fInstalled = FALSE;
293
294    TCHAR szKey[] = AFSREG_SVR_SVC_KEY;
295    LPCTSTR pch = lstrchr (szKey, TEXT('\\'));
296
297    HKEY hk;
298    if (RegOpenKey (HKEY_LOCAL_MACHINE, &pch[1], &hk) == 0)
299       {
300       fInstalled = TRUE;
301       RegCloseKey (hk);
302       }
303
304    return fInstalled;
305 }
306