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