convert-from-bsd-to-posix-string-and-memory-functions-20010807
[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     afs_int32 i,j;
85     int lwps = 3;
86     char clones[MAXHOSTSPERCELL];
87
88     const char *pr_dbaseName;
89     char *whoami = "ptserver";
90
91     int   a;
92     char  arg[100];
93     
94 #ifdef  AFS_AIX32_ENV
95     /*
96      * The following signal action for AIX is necessary so that in case of a 
97      * crash (i.e. core is generated) we can include the user's data section 
98      * in the core dump. Unfortunately, by default, only a partial core is
99      * generated which, in many cases, isn't too useful.
100      */
101     struct sigaction nsa;
102     
103     sigemptyset(&nsa.sa_mask);
104     nsa.sa_handler = SIG_DFL;
105     nsa.sa_flags = SA_FULLDUMP;
106     sigaction(SIGABRT, &nsa, NULL);
107     sigaction(SIGSEGV, &nsa, NULL);
108 #endif
109     osi_audit (PTS_StartEvent, 0, AUD_END);
110
111     /* Initialize dirpaths */
112     if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
113 #ifdef AFS_NT40_ENV
114         ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0],0);
115 #endif
116         fprintf(stderr,"%s: Unable to obtain AFS server directory.\n", argv[0]);
117         exit(2);
118     }
119
120     pr_dbaseName = AFSDIR_SERVER_PRDB_FILEPATH;
121
122     for (a=1; a<argc; a++) {
123         int alen;
124         lcstring (arg, argv[a], sizeof(arg));
125         alen = strlen (arg);
126         if ((strncmp (arg, "-database", alen) == 0) ||
127             (strncmp (arg, "-db", alen) == 0)) {
128             pr_dbaseName = argv[++a];   /* specify a database */
129         }
130         else if (strncmp(arg, "-p", alen) == 0) {
131            lwps = atoi(argv[++a]);
132            if (lwps > 16) {       /* maximum of 16 */
133               printf("Warning: '-p %d' is too big; using %d instead\n",
134                      lwps, 16);
135               lwps = 16;
136            } else if (lwps < 3) { /* minimum of 3 */
137               printf("Warning: '-p %d' is too small; using %d instead\n",
138                      lwps, 3);
139               lwps = 3;
140            }
141         }
142         else if (strncmp (arg, "-rebuild", alen) == 0) /* rebuildDB++ */ ;
143         else if (strncmp (arg, "-enable_peer_stats", alen) == 0) {
144             rx_enablePeerRPCStats();
145         }
146         else if (strncmp (arg, "-enable_process_stats", alen) == 0) {
147             rx_enableProcessRPCStats();
148         }
149 #ifndef AFS_NT40_ENV
150         else if (strncmp(arg, "-syslog", alen)==0) {
151             /* set syslog logging flag */
152             serverLogSyslog = 1;
153         } 
154         else if (strncmp(arg, "-syslog=", MIN(8,alen))==0) {
155             serverLogSyslog = 1;
156             serverLogSyslogFacility = atoi(arg+8);
157         }
158 #endif
159         else if (*arg == '-') {
160           usage:
161
162                 /* hack in help flag support */
163
164 #ifndef AFS_NT40_ENV
165                 printf ("Usage: ptserver [-database <db path>] "
166                         "[-syslog[=FACILITY]] "
167                         "[-p <number of processes>] [-rebuild] "
168                         "[-enable_peer_stats] [-enable_process_stats] "
169                         "[-help]\n");
170 #else
171                 printf ("Usage: ptserver [-database <db path>] "
172                         "[-p <number of processes>] [-rebuild] "
173                         "[-help]\n");
174 #endif
175                 fflush(stdout);
176
177             PT_EXIT(1);
178         }
179     }
180
181     OpenLog(AFSDIR_SERVER_PTLOG_FILEPATH);     /* set up logging */
182     SetupLogSignals();
183  
184     prdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
185     if (!prdir) {
186         fprintf (stderr, "ptserver: can't open configuration directory.\n");
187         PT_EXIT(1);
188     }
189     if (afsconf_GetNoAuthFlag(prdir))
190         printf ("ptserver: running unauthenticated\n");
191
192 #ifdef AFS_NT40_ENV 
193     /* initialize winsock */
194     if (afs_winsockInit()<0) {
195       ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0,
196                           argv[0],0);
197       
198       fprintf(stderr, "ptserver: couldn't initialize winsock. \n");
199       PT_EXIT(1);
200     }
201 #endif
202     /* get this host */
203     gethostname(hostname,sizeof(hostname));
204     th = gethostbyname(hostname);
205     if (!th) {
206         fprintf (stderr, "ptserver: couldn't get address of this host.\n");
207         PT_EXIT(1);
208     }
209     memcpy(&myHost, th->h_addr, sizeof(afs_int32));
210         
211     /* get list of servers */
212     code = afsconf_GetExtendedCellInfo(prdir,(char *)0,"afsprot",
213                        &info, &clones);
214     if (code) {
215         com_err (whoami, code, "Couldn't get server list");
216         PT_EXIT(2);
217     }
218     pr_realmName = info.name;
219     pr_realmNameLen = strlen (pr_realmName);
220
221 #if 0
222     /* get keys */
223     code = afsconf_GetKey(prdir,999,&tkey);
224     if (code) {
225         com_err (whoami, code, "couldn't get bcrypt keys from key file, ignoring.");
226     }
227 #endif
228     {   afs_int32 kvno;                 /* see if there is a KeyFile here */
229         struct ktc_encryptionKey key;
230         code = afsconf_GetLatestKey (prdir, &kvno, &key);
231         kerberosKeys = (code == 0);
232         if (!kerberosKeys)
233             printf ("ptserver: can't find any Kerberos keys, code = %d, ignoring\n", code);
234     }
235     if (kerberosKeys) {
236         /* initialize ubik */
237         ubik_CRXSecurityProc = afsconf_ClientAuth;
238         ubik_CRXSecurityRock = (char *)prdir;
239         ubik_SRXSecurityProc = afsconf_ServerAuth;
240         ubik_SRXSecurityRock = (char *)prdir;
241         ubik_CheckRXSecurityProc = afsconf_CheckAuth;
242         ubik_CheckRXSecurityRock = (char *)prdir;
243     }
244     /* The max needed is when deleting an entry.  A full CoEntry deletion
245      * required removal from 39 entries.  Each of which may refers to the entry
246      * being deleted in one of its CoEntries.  If a CoEntry is freed its
247      * predecessor CoEntry will be modified as well.  Any freed blocks also
248      * modifies the database header.  Counting the entry being deleted and its
249      * CoEntry this adds up to as much as 1+1+39*3 = 119.  If all these entries
250      * and the header are in separate Ubik buffers then 120 buffers may be
251      * required. */
252     ubik_nBuffers = 120 + /*fudge*/40;
253     code = ubik_ServerInitByInfo(myHost, htons(AFSCONF_PROTPORT), &info,
254                            &clones, pr_dbaseName, &dbase);
255     if (code) {
256         com_err (whoami, code, "Ubik init failed");
257         PT_EXIT(2);
258     }
259     sc[0] = rxnull_NewServerSecurityObject();
260     sc[1] = 0;
261     if (kerberosKeys) {
262         sc[2] = rxkad_NewServerSecurityObject
263             (0, prdir, afsconf_GetKey, (char *)0);
264     }
265     else sc[2] = sc[0];
266
267     /* These two lines disallow jumbograms */
268     rx_maxReceiveSize = OLD_MAX_PACKET_SIZE;
269     rxi_nSendFrags = rxi_nRecvFrags = 1;
270
271     tservice = rx_NewService(0,PRSRV,"Protection Server",sc,3,PR_ExecuteRequest);
272     if (tservice == (struct rx_service *)0) {
273         fprintf (stderr, "ptserver: Could not create new rx service.\n");
274         PT_EXIT(3);
275     }
276     rx_SetMinProcs(tservice,2);
277     rx_SetMaxProcs(tservice,lwps);
278
279     tservice = rx_NewService(0,RX_STATS_SERVICE_ID,"rpcstats",sc,3,RXSTATS_ExecuteRequest);
280     if (tservice == (struct rx_service *)0) {
281         fprintf (stderr, "ptserver: Could not create new rx service.\n");
282         PT_EXIT(3);
283     }
284     rx_SetMinProcs(tservice,2);
285     rx_SetMaxProcs(tservice,4);
286
287     /* allow super users to manage RX statistics */
288     rx_SetRxStatUserOk(pr_rxstat_userok);
289
290     rx_StartServer(1);
291     osi_audit (PTS_FinishEvent, -1, AUD_END);
292 }