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