misc-build-cleanup-20010917
[openafs.git] / src / ptserver / ptserver.c
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 <afsconfig.h>
11 #include <afs/param.h>
12
13 RCSID("$Header$");
14
15 #include <afs/stds.h>
16 #ifdef  AFS_AIX32_ENV
17 #include <signal.h>
18 #endif
19 #include <sys/types.h>
20 #include <stdio.h>
21 #ifdef AFS_NT40_ENV 
22 #include <winsock2.h>
23 #include <WINNT/afsevent.h>
24 #else
25 #include <netdb.h>
26 #include <netinet/in.h>
27 #endif
28 #include <rx/xdr.h>
29 #include <rx/rx.h>
30 #include <rx/rx_globals.h>
31 #include <lock.h>
32 #include <ubik.h>
33 #include <afs/cellconfig.h>
34 #include <afs/auth.h>
35 #include <afs/keys.h>
36 #include "ptserver.h"
37 #include "error_macros.h"
38 #include "afs/audit.h"
39 #include <afs/afsutil.h>
40
41
42 /* make all of these into a structure if you want */
43 struct prheader cheader;
44 struct ubik_dbase *dbase;
45 struct afsconf_dir *prdir;
46
47 extern afs_int32 ubik_lastYesTime;
48 extern afs_int32 ubik_nBuffers;
49
50 extern int afsconf_ServerAuth();
51 extern int afsconf_CheckAuth();
52
53 int   pr_realmNameLen;
54 char *pr_realmName;
55
56 #include "AFS_component_version_number.c"
57
58 /* check whether caller is authorized to manage RX statistics */
59 int pr_rxstat_userok(call)
60     struct rx_call *call;
61 {
62     return afsconf_SuperUser(prdir, call, (char *)0);
63 }
64
65 void main (argc, argv)
66   int argc;
67   char **argv;
68 {
69     register afs_int32 code;
70     afs_int32 myHost;
71     register struct hostent *th;
72     char hostname[64];
73     struct rx_service *tservice;
74     struct rx_securityClass *sc[3];
75     extern struct rx_securityClass *rxnull_NewServerSecurityObject();
76     extern struct rx_securityClass *rxkad_NewServerSecurityObject();
77     extern int RXSTATS_ExecuteRequest();
78     extern int PR_ExecuteRequest();
79 #if 0
80     struct ktc_encryptionKey tkey;
81 #endif
82     struct afsconf_cell info;
83     int kerberosKeys;                   /* set if found some keys */
84     int lwps = 3;
85     char clones[MAXHOSTSPERCELL];
86
87     const char *pr_dbaseName;
88     char *whoami = "ptserver";
89
90     int   a;
91     char  arg[100];
92     
93 #ifdef  AFS_AIX32_ENV
94     /*
95      * The following signal action for AIX is necessary so that in case of a 
96      * crash (i.e. core is generated) we can include the user's data section 
97      * in the core dump. Unfortunately, by default, only a partial core is
98      * generated which, in many cases, isn't too useful.
99      */
100     struct sigaction nsa;
101     
102     sigemptyset(&nsa.sa_mask);
103     nsa.sa_handler = SIG_DFL;
104     nsa.sa_flags = SA_FULLDUMP;
105     sigaction(SIGABRT, &nsa, NULL);
106     sigaction(SIGSEGV, &nsa, NULL);
107 #endif
108     osi_audit (PTS_StartEvent, 0, AUD_END);
109
110     /* Initialize dirpaths */
111     if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
112 #ifdef AFS_NT40_ENV
113         ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0],0);
114 #endif
115         fprintf(stderr,"%s: Unable to obtain AFS server directory.\n", argv[0]);
116         exit(2);
117     }
118
119     pr_dbaseName = AFSDIR_SERVER_PRDB_FILEPATH;
120
121     for (a=1; a<argc; a++) {
122         int alen;
123         lcstring (arg, argv[a], sizeof(arg));
124         alen = strlen (arg);
125         if ((strncmp (arg, "-database", alen) == 0) ||
126             (strncmp (arg, "-db", alen) == 0)) {
127             pr_dbaseName = argv[++a];   /* specify a database */
128         }
129         else if (strncmp(arg, "-p", alen) == 0) {
130            lwps = atoi(argv[++a]);
131            if (lwps > 16) {       /* maximum of 16 */
132               printf("Warning: '-p %d' is too big; using %d instead\n",
133                      lwps, 16);
134               lwps = 16;
135            } else if (lwps < 3) { /* minimum of 3 */
136               printf("Warning: '-p %d' is too small; using %d instead\n",
137                      lwps, 3);
138               lwps = 3;
139            }
140         }
141         else if (strncmp (arg, "-rebuild", alen) == 0) /* rebuildDB++ */ ;
142         else if (strncmp (arg, "-enable_peer_stats", alen) == 0) {
143             rx_enablePeerRPCStats();
144         }
145         else if (strncmp (arg, "-enable_process_stats", alen) == 0) {
146             rx_enableProcessRPCStats();
147         }
148 #ifndef AFS_NT40_ENV
149         else if (strncmp(arg, "-syslog", alen)==0) {
150             /* set syslog logging flag */
151             serverLogSyslog = 1;
152         } 
153         else if (strncmp(arg, "-syslog=", MIN(8,alen))==0) {
154             serverLogSyslog = 1;
155             serverLogSyslogFacility = atoi(arg+8);
156         }
157 #endif
158         else if (*arg == '-') {
159                 /* hack in help flag support */
160
161 #ifndef AFS_NT40_ENV
162                 printf ("Usage: ptserver [-database <db path>] "
163                         "[-syslog[=FACILITY]] "
164                         "[-p <number of processes>] [-rebuild] "
165                         "[-enable_peer_stats] [-enable_process_stats] "
166                         "[-help]\n");
167 #else
168                 printf ("Usage: ptserver [-database <db path>] "
169                         "[-p <number of processes>] [-rebuild] "
170                         "[-help]\n");
171 #endif
172                 fflush(stdout);
173
174             PT_EXIT(1);
175         }
176     }
177
178     OpenLog(AFSDIR_SERVER_PTLOG_FILEPATH);     /* set up logging */
179     SetupLogSignals();
180  
181     prdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
182     if (!prdir) {
183         fprintf (stderr, "ptserver: can't open configuration directory.\n");
184         PT_EXIT(1);
185     }
186     if (afsconf_GetNoAuthFlag(prdir))
187         printf ("ptserver: running unauthenticated\n");
188
189 #ifdef AFS_NT40_ENV 
190     /* initialize winsock */
191     if (afs_winsockInit()<0) {
192       ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0,
193                           argv[0],0);
194       
195       fprintf(stderr, "ptserver: couldn't initialize winsock. \n");
196       PT_EXIT(1);
197     }
198 #endif
199     /* get this host */
200     gethostname(hostname,sizeof(hostname));
201     th = gethostbyname(hostname);
202     if (!th) {
203         fprintf (stderr, "ptserver: couldn't get address of this host.\n");
204         PT_EXIT(1);
205     }
206     memcpy(&myHost, th->h_addr, sizeof(afs_int32));
207         
208     /* get list of servers */
209     code = afsconf_GetExtendedCellInfo(prdir,(char *)0,"afsprot",
210                        &info, &clones);
211     if (code) {
212         com_err (whoami, code, "Couldn't get server list");
213         PT_EXIT(2);
214     }
215     pr_realmName = info.name;
216     pr_realmNameLen = strlen (pr_realmName);
217
218 #if 0
219     /* get keys */
220     code = afsconf_GetKey(prdir,999,&tkey);
221     if (code) {
222         com_err (whoami, code, "couldn't get bcrypt keys from key file, ignoring.");
223     }
224 #endif
225     {   afs_int32 kvno;                 /* see if there is a KeyFile here */
226         struct ktc_encryptionKey key;
227         code = afsconf_GetLatestKey (prdir, &kvno, &key);
228         kerberosKeys = (code == 0);
229         if (!kerberosKeys)
230             printf ("ptserver: can't find any Kerberos keys, code = %d, ignoring\n", code);
231     }
232     if (kerberosKeys) {
233         /* initialize ubik */
234         ubik_CRXSecurityProc = afsconf_ClientAuth;
235         ubik_CRXSecurityRock = (char *)prdir;
236         ubik_SRXSecurityProc = afsconf_ServerAuth;
237         ubik_SRXSecurityRock = (char *)prdir;
238         ubik_CheckRXSecurityProc = afsconf_CheckAuth;
239         ubik_CheckRXSecurityRock = (char *)prdir;
240     }
241     /* The max needed is when deleting an entry.  A full CoEntry deletion
242      * required removal from 39 entries.  Each of which may refers to the entry
243      * being deleted in one of its CoEntries.  If a CoEntry is freed its
244      * predecessor CoEntry will be modified as well.  Any freed blocks also
245      * modifies the database header.  Counting the entry being deleted and its
246      * CoEntry this adds up to as much as 1+1+39*3 = 119.  If all these entries
247      * and the header are in separate Ubik buffers then 120 buffers may be
248      * required. */
249     ubik_nBuffers = 120 + /*fudge*/40;
250     code = ubik_ServerInitByInfo(myHost, htons(AFSCONF_PROTPORT), &info,
251                            &clones, pr_dbaseName, &dbase);
252     if (code) {
253         com_err (whoami, code, "Ubik init failed");
254         PT_EXIT(2);
255     }
256     sc[0] = rxnull_NewServerSecurityObject();
257     sc[1] = 0;
258     if (kerberosKeys) {
259         sc[2] = rxkad_NewServerSecurityObject
260             (0, prdir, afsconf_GetKey, (char *)0);
261     }
262     else sc[2] = sc[0];
263
264     /* These two lines disallow jumbograms */
265     rx_maxReceiveSize = OLD_MAX_PACKET_SIZE;
266     rxi_nSendFrags = rxi_nRecvFrags = 1;
267
268     tservice = rx_NewService(0,PRSRV,"Protection Server",sc,3,PR_ExecuteRequest);
269     if (tservice == (struct rx_service *)0) {
270         fprintf (stderr, "ptserver: Could not create new rx service.\n");
271         PT_EXIT(3);
272     }
273     rx_SetMinProcs(tservice,2);
274     rx_SetMaxProcs(tservice,lwps);
275
276     tservice = rx_NewService(0,RX_STATS_SERVICE_ID,"rpcstats",sc,3,RXSTATS_ExecuteRequest);
277     if (tservice == (struct rx_service *)0) {
278         fprintf (stderr, "ptserver: Could not create new rx service.\n");
279         PT_EXIT(3);
280     }
281     rx_SetMinProcs(tservice,2);
282     rx_SetMaxProcs(tservice,4);
283
284     /* allow super users to manage RX statistics */
285     rx_SetRxStatUserOk(pr_rxstat_userok);
286
287     rx_StartServer(1);
288     osi_audit (PTS_FinishEvent, -1, AUD_END);
289 }