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