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