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