e9d67c3ba60daac82a030638be01cc96a32fe167
[openafs.git] / src / WINNT / afsadmsvr / TaAfsAdmSvrMain.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 "TaAfsAdmSvrInternal.h"
16
17 /*
18  * ROUTINES ___________________________________________________________________
19  *
20  */
21
22 #ifdef DEBUG
23 LPARAM CALLBACK AfsAdmSvr_Debug_ThreadProc (PVOID lp)
24 {
25    ShowMemoryManager();
26
27    MSG msg;
28    while (GetMessage (&msg, 0, 0, 0))
29       {
30       if (!IsMemoryManagerMessage (&msg))
31          {
32          TranslateMessage (&msg);
33          DispatchMessage (&msg);
34          }
35       }
36
37    return 0;
38 }
39 #endif
40
41
42 int cdecl main (int argc, char **argv)
43 {
44    BOOL fSuccess = FALSE;
45
46    Print (TEXT("Initializing..."));
47
48    WSADATA Data;
49    WSAStartup (0x0101, &Data);
50
51    // Parse the command-line
52    //
53    DWORD dwAutoScope = AFSADMSVR_SCOPE_VOLUMES | AFSADMSVR_SCOPE_USERS;
54
55    for (--argc,++argv; argc; --argc,++argv)
56       {
57       if (!lstrcmpi (*argv, AFSADMSVR_KEYWORD_TIMED))
58          AfsAdmSvr_EnableAutoShutdown (TRUE);
59       else if (!lstrcmpi (*argv, AFSADMSVR_KEYWORD_MANUAL))
60          dwAutoScope = 0;
61       else if (!lstrcmpi (*argv, AFSADMSVR_KEYWORD_SCOPE_USERS))
62          dwAutoScope &= ~AFSADMSVR_SCOPE_VOLUMES;
63       else if (!lstrcmpi (*argv, AFSADMSVR_KEYWORD_SCOPE_VOLUMES))
64          dwAutoScope &= ~AFSADMSVR_SCOPE_USERS;
65 #ifdef DEBUG
66       else if (!lstrcmpi (*argv, AFSADMSVR_KEYWORD_DEBUG))
67          CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE)AfsAdmSvr_Debug_ThreadProc, 0, 0, 0);
68 #endif
69       }
70
71    // Prepare to listen for RPCs
72    //
73    unsigned char *pszPROTOCOL = (unsigned char *)"ncacn_ip_tcp";
74    unsigned char *pszENTRYNAME = (unsigned char *)AFSADMSVR_ENTRYNAME_DEFAULT;
75    unsigned char *pszANNOTATION = (unsigned char *)"Transarc AFS Administrative Server";
76    unsigned char szEndpoint[ 32 ];
77    wsprintf ((LPTSTR)szEndpoint, "%lu", AFSADMSVR_ENDPOINT_DEFAULT);
78    int cMAX_CALLS = 50;
79
80    // Clean up any broken interface registration
81    //
82    RpcServerUnregisterIf (ITaAfsAdminSvr_v1_0_s_ifspec, 0, FALSE);
83    RpcNsBindingUnexport (RPC_C_NS_SYNTAX_DEFAULT, pszENTRYNAME, ITaAfsAdminSvr_v1_0_s_ifspec, NULL);
84
85    // Register our interface
86    //
87    RPC_STATUS status;
88    if ((status = RpcServerUseProtseq (pszPROTOCOL, cMAX_CALLS, NULL)) != 0)
89       {
90       Print (dlERROR, TEXT("RpcServerUseProtseq failed; error 0x%08lX"), status);
91       }
92    else if ((status = RpcServerRegisterIf (ITaAfsAdminSvr_v1_0_s_ifspec, 0, 0)) != 0)
93       {
94       Print (dlERROR, TEXT("RpcServerRegisterIf failed; error 0x%08lX"), status);
95       }
96    else
97       {
98       // Always try to register on port 1025; that's the easiest thing for
99       // some clients to find. We'll only fail if we (a) can't use 1025, and
100       // (b) can't export our bindings.
101       //
102       BOOL fGotPort = FALSE;
103       if (RpcServerUseProtseqEp (pszPROTOCOL, cMAX_CALLS, szEndpoint, NULL) == 0)
104          fGotPort = TRUE;
105       else
106          Print (dlWARNING, TEXT("RpcServerUseProtseqEp failed (benign); error 0x%08lX"), status);
107
108       RPC_BINDING_VECTOR *pBindingVector;
109       if ((status = RpcServerInqBindings (&pBindingVector)) != 0)
110          {
111          Print (dlERROR, TEXT("RpcServerRegisterIf failed; error 0x%08lX"), status);
112          }
113       else if ((status = RpcEpRegister (ITaAfsAdminSvr_v1_0_s_ifspec, pBindingVector, NULL, pszANNOTATION)) != 0)
114          {
115          Print (dlERROR, TEXT("RpcEpRegister failed; error 0x%08lX"), status);
116          }
117       else
118          {
119          BOOL fExportedBinding = FALSE;
120
121          if ((status = RpcNsBindingExport (RPC_C_NS_SYNTAX_DEFAULT, pszENTRYNAME, ITaAfsAdminSvr_v1_0_s_ifspec, pBindingVector, NULL)) == 0)
122             fExportedBinding = TRUE;
123          else
124             Print (dlWARNING, TEXT("RpcNsBindingExport failed (benign); error 0x%08lX"), status);
125
126          if (!fExportedBinding && !fGotPort)
127             {
128             Print (dlERROR, TEXT("RpcNsBindingExport failed; error 0x%08lX"), status);
129             Print (dlERROR, TEXT("Could not bind to port %s or export bindings; terminating"), szEndpoint);
130             }
131          else
132             {
133             AfsAdmSvr_Startup();
134
135             Print (TEXT("Ready.\n"));
136
137             // If not asked to open cells manually, fork a thread to start opening
138             // the default local cell
139             //
140             if (dwAutoScope)
141                {
142                DWORD dwThreadID;
143                CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE)AfsAdmSvr_AutoOpen_ThreadProc, (PVOID)dwAutoScope, 0, &dwThreadID);
144                }
145
146             // Listen for requests until someone calls StopListen
147             //
148             if ((status = RpcServerListen (1, cMAX_CALLS, FALSE)) != 0)
149                {
150                Print (dlERROR, TEXT("RpcServerListen failed; error 0x%08lX"), status);
151                }
152             else
153                {
154                fSuccess = TRUE;
155                }
156
157             AfsAdmSvr_Shutdown();
158             }
159
160          if (fExportedBinding)
161             {
162             if ((status = RpcNsBindingUnexport (RPC_C_NS_SYNTAX_DEFAULT, pszENTRYNAME, ITaAfsAdminSvr_v1_0_s_ifspec, NULL)) != 0)
163                {
164                Print (dlWARNING, TEXT("RpcNsBindingExport failed; error 0x%08lX"), status);
165                }
166             }
167
168          if ((status = RpcEpUnregister (ITaAfsAdminSvr_v1_0_s_ifspec, pBindingVector, NULL)) != 0)
169             {
170             Print (dlWARNING, TEXT("RpcEpUnregister failed; error 0x%08lX"), status);
171             }
172          }
173       }
174
175    Print (TEXT("Shutting down...\n"));
176
177    if ((status = RpcServerUnregisterIf (0, 0, FALSE)) != 0)
178       {
179       Print (dlWARNING, TEXT("RpcServerUnregisterIf failed; error 0x%08lX"), status);
180       exit (-1);
181       }
182
183    return (fSuccess) ? (0) : (-1);
184 }
185
186
187 extern "C" void __RPC_FAR * __RPC_USER MIDL_user_allocate (size_t cbAllocate)
188 {
189    return (void __RPC_FAR *)Allocate (cbAllocate);
190 }
191
192
193 extern "C" void __RPC_USER MIDL_user_free (void __RPC_FAR *pData)
194 {
195    Free (pData);
196 }
197