add-missing-return-values-20031207
[openafs.git] / src / budb / server.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
14     ("$Header$");
15
16 #ifdef AFS_NT40_ENV
17 #include <winsock2.h>
18 #include <fcntl.h>
19 #include <WINNT/afsevent.h>
20 #else
21 #include <netinet/in.h>
22 #include <sys/file.h>
23 #include <sys/time.h>
24 #include <netdb.h>
25 #endif
26
27 #ifdef HAVE_STRING_H
28 #include <string.h>
29 #else
30 #ifdef HAVE_STRINGS_H
31 #include <strings.h>
32 #endif
33 #endif
34
35 #include <afs/stds.h>
36 #include <sys/types.h>
37 #include <time.h>
38 #include <stdio.h>
39 #include <afs/cmd.h>
40 #include <lwp.h>
41 #include <ubik.h>
42 #include <rx/xdr.h>
43 #include <rx/rx.h>
44 #include <rx/rxkad.h>
45 #include <rx/rx_globals.h>
46 #include <afs/cellconfig.h>
47 #include <afs/auth.h>
48 #include <afs/bubasics.h>
49 #include <afs/afsutil.h>
50 #include <afs/com_err.h>
51 #include <errno.h>
52 #ifdef  AFS_AIX32_ENV
53 #include <signal.h>
54 #endif
55 #include "budb_errs.h"
56 #include "database.h"
57 #include "error_macros.h"
58 #include "globals.h"
59 #include "afs/audit.h"
60
61 struct ubik_dbase *BU_dbase;
62 struct afsconf_dir *BU_conf;    /* for getting cell info */
63
64 char lcell[MAXKTCREALMLEN];
65 afs_int32 myHost = 0;
66 int helpOption;
67
68 /* server's global configuration information. This is exported to other
69  * files/routines
70  */
71
72 buServerConfT globalConf;
73 buServerConfP globalConfPtr = &globalConf;
74 char dbDir[AFSDIR_PATH_MAX], cellConfDir[AFSDIR_PATH_MAX];
75 /* debugging control */
76 int debugging = 0;
77
78 /* check whether caller is authorized to manage RX statistics */
79 int
80 BU_rxstat_userok(call)
81      struct rx_call *call;
82 {
83     return afsconf_SuperUser(BU_conf, call, NULL);
84 }
85
86 int
87 convert_cell_to_ubik(cellinfo, myHost, serverList)
88      struct afsconf_cell *cellinfo;
89      afs_int32 *myHost;
90      afs_int32 *serverList;
91 {
92     int i;
93     char hostname[64];
94     struct hostent *th;
95
96     /* get this host */
97     gethostname(hostname, sizeof(hostname));
98     th = gethostbyname(hostname);
99     if (!th) {
100         printf("prserver: couldn't get address of this host.\n");
101         BUDB_EXIT(1);
102     }
103     memcpy(myHost, th->h_addr, sizeof(afs_int32));
104
105     for (i = 0; i < cellinfo->numServers; i++)
106         /* omit my host from serverList */
107         if (cellinfo->hostAddr[i].sin_addr.s_addr != *myHost)
108             *serverList++ = cellinfo->hostAddr[i].sin_addr.s_addr;
109
110     *serverList = 0;            /* terminate list */
111     return 0;
112 }
113
114 /* MyBeforeProc
115  *      The whole purpose of MyBeforeProc is to detect
116  *      if the -help option was not within the command line.
117  *      If it were, this routine would never have been called.
118  */
119 static int
120 MyBeforeProc(as)
121      register struct cmd_syndesc *as;
122 {
123     helpOption = 0;
124     return 0;
125 }
126
127 /* initializeCommands
128  *      initialize all the supported commands and their arguments
129  */
130
131 initializeArgHandler()
132 {
133     struct cmd_syndesc *cptr;
134
135     int argHandler();
136
137     cmd_SetBeforeProc(MyBeforeProc, NULL);
138
139     cptr = cmd_CreateSyntax(NULL, argHandler, NULL, "Backup database server");
140
141     cmd_AddParm(cptr, "-database", CMD_SINGLE, CMD_OPTIONAL,
142                 "database directory");
143
144     cmd_AddParm(cptr, "-cellservdb", CMD_SINGLE, CMD_OPTIONAL,
145                 "cell configuration directory");
146
147     cmd_AddParm(cptr, "-resetdb", CMD_FLAG, CMD_OPTIONAL,
148                 "truncate the database");
149
150     cmd_AddParm(cptr, "-noauth", CMD_FLAG, CMD_OPTIONAL,
151                 "run without authentication");
152
153     cmd_AddParm(cptr, "-smallht", CMD_FLAG, CMD_OPTIONAL,
154                 "use small hash tables");
155
156     cmd_AddParm(cptr, "-servers", CMD_LIST, CMD_OPTIONAL,
157                 "list of ubik database servers");
158
159     cmd_AddParm(cptr, "-ubikbuffers", CMD_SINGLE, CMD_OPTIONAL,
160                 "the number of ubik buffers");
161
162 }
163
164 int
165 argHandler(as, arock)
166      struct cmd_syndesc *as;
167      char *arock;
168 {
169
170     /* globalConfPtr provides the handle for the configuration information */
171
172     /* database directory */
173     if (as->parms[0].items != 0) {
174         globalConfPtr->databaseDirectory =
175             (char *)malloc(strlen(as->parms[0].items->data) + 1);
176         if (globalConfPtr->databaseDirectory == 0)
177             BUDB_EXIT(-1);
178         strcpy(globalConfPtr->databaseDirectory, as->parms[0].items->data);
179     }
180
181     /* -cellservdb, cell configuration directory */
182     if (as->parms[1].items != 0) {
183         globalConfPtr->cellConfigdir =
184             (char *)malloc(strlen(as->parms[1].items->data) + 1);
185         if (globalConfPtr->cellConfigdir == 0)
186             BUDB_EXIT(-1);
187
188         strcpy(globalConfPtr->cellConfigdir, as->parms[1].items->data);
189
190         globalConfPtr->debugFlags |= DF_RECHECKNOAUTH;
191     }
192
193     /* truncate the database */
194     if (as->parms[2].items != 0)
195         truncateDatabase();
196
197     /* run without authentication */
198     if (as->parms[3].items != 0)
199         globalConfPtr->debugFlags |= DF_NOAUTH;
200
201     /* use small hash tables */
202     if (as->parms[4].items != 0)
203         globalConfPtr->debugFlags |= DF_SMALLHT;
204
205     /* user provided list of ubik database servers */
206     if (as->parms[5].items != 0)
207         parseServerList(as->parms[5].items);
208
209     /* user provided the number of ubik buffers    */
210     if (as->parms[6].items != 0)
211         ubik_nBuffers = atoi(as->parms[6].items->data);
212     else
213         ubik_nBuffers = 0;
214
215     return 0;
216 }
217
218 /* --- */
219
220 parseServerList(itemPtr)
221      struct cmd_item *itemPtr;
222 {
223     struct cmd_item *save;
224     char **serverArgs;
225     char **ptr;
226     afs_int32 nservers = 0;
227     afs_int32 code = 0;
228
229     save = itemPtr;
230
231     /* compute number of servers in the list */
232     while (itemPtr) {
233         nservers++;
234         itemPtr = itemPtr->next;
235     }
236
237     LogDebug(3, "%d servers\n", nservers);
238
239     /* now can allocate the space for the server arguments */
240     serverArgs = (char **)malloc((nservers + 2) * sizeof(char *));
241     if (serverArgs == 0)
242         ERROR(-1);
243
244     ptr = serverArgs;
245     *ptr++ = "";
246     *ptr++ = "-servers";
247
248     /* now go through and construct the list of servers */
249     itemPtr = save;
250     while (itemPtr) {
251         *ptr++ = itemPtr->data;
252         itemPtr = itemPtr->next;
253     }
254
255     code =
256         ubik_ParseServerList(nservers + 2, serverArgs, &globalConfPtr->myHost,
257                              globalConfPtr->serverList);
258     if (code)
259         ERROR(code);
260
261     /* free space for the server args */
262     free((char *)serverArgs);
263
264   error_exit:
265     return (code);
266 }
267
268 /* truncateDatabase
269  *      truncates just the database file.
270  */
271
272 truncateDatabase()
273 {
274     char *path;
275     afs_int32 code = 0;
276     int fd;
277
278     path =
279         (char *)malloc(strlen(globalConfPtr->databaseDirectory) +
280                        strlen(globalConfPtr->databaseName) +
281                        strlen(globalConfPtr->databaseExtension) + 1);
282     if (path == 0)
283         ERROR(-1);
284
285     /* construct the database name */
286     strcpy(path, globalConfPtr->databaseDirectory);
287     strcat(path, globalConfPtr->databaseName);
288     strcat(path, globalConfPtr->databaseExtension);
289
290     fd = open(path, O_RDWR, 0755);
291     if (!fd) {
292         code = errno;
293     } else {
294         if (ftruncate(fd, 0) != 0) {
295             code = errno;
296         } else
297             close(fd);
298     }
299
300   error_exit:
301     return (code);
302 }
303
304
305 /* --- */
306
307 #include "AFS_component_version_number.c"
308
309 main(argc, argv)
310      int argc;
311      char *argv[];
312 {
313     char *whoami = argv[0];
314     char *dbNamePtr = 0;
315     struct afsconf_cell cellinfo;
316     time_t currentTime;
317     afs_int32 code = 0;
318
319     struct rx_service *tservice;
320     struct rx_securityClass *sca[3];
321
322     extern int afsconf_ServerAuth();
323     extern int afsconf_CheckAuth();
324
325     extern int rx_stackSize;
326     extern int BUDB_ExecuteRequest();
327
328 #ifdef AFS_NT40_ENV
329     /* initialize winsock */
330     if (afs_winsockInit() < 0) {
331         ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0, argv[0], 0);
332         fprintf(stderr, "%s: Couldn't initialize winsock.\n", whoami);
333         exit(1);
334     }
335 #endif
336
337 #ifdef  AFS_AIX32_ENV
338     /*
339      * The following signal action for AIX is necessary so that in case of a 
340      * crash (i.e. core is generated) we can include the user's data section 
341      * in the core dump. Unfortunately, by default, only a partial core is
342      * generated which, in many cases, isn't too useful.
343      */
344     struct sigaction nsa;
345
346     sigemptyset(&nsa.sa_mask);
347     nsa.sa_handler = SIG_DFL;
348     nsa.sa_flags = SA_FULLDUMP;
349     sigaction(SIGSEGV, &nsa, NULL);
350     sigaction(SIGABRT, &nsa, NULL);
351 #endif
352     osi_audit(BUDB_StartEvent, 0, AUD_END);
353
354     initialize_BUDB_error_table();
355     initializeArgHandler();
356
357     /* Initialize dirpaths */
358     if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
359 #ifdef AFS_NT40_ENV
360         ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
361 #endif
362         com_err(whoami, errno, "; Unable to obtain AFS server directory.");
363         exit(2);
364     }
365
366     memset(globalConfPtr, 0, sizeof(*globalConfPtr));
367
368     /* set default configuration values */
369     strcpy(dbDir, AFSDIR_SERVER_DB_DIRPATH);
370     strcat(dbDir, "/");
371     globalConfPtr->databaseDirectory = dbDir;
372     globalConfPtr->databaseName = DEFAULT_DBPREFIX;
373     strcpy(cellConfDir, AFSDIR_SERVER_ETC_DIRPATH);
374     globalConfPtr->cellConfigdir = cellConfDir;
375
376     /* open the log file */
377 /*
378     globalConfPtr->log = fopen(DEFAULT_LOGNAME,"a");
379     if ( globalConfPtr->log == NULL )
380     {   
381         printf("Can't open log file %s - aborting\n", DEFAULT_LOGNAME);
382         BUDB_EXIT(-1);
383     }
384 */
385
386     srandom(1);
387
388     /* process the user supplied args */
389     helpOption = 1;
390     code = cmd_Dispatch(argc, argv);
391     if (code)
392         ERROR(code);
393
394     /* exit if there was a help option */
395     if (helpOption)
396         BUDB_EXIT(0);
397
398     /* open the log file */
399     globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
400     if (globalConfPtr->log == NULL) {
401         printf("Can't open log file %s - aborting\n",
402                AFSDIR_SERVER_BUDBLOG_FILEPATH);
403         BUDB_EXIT(-1);
404     }
405
406     /* keep log closed so can remove it */
407
408     fclose(globalConfPtr->log);
409
410     /* open the cell's configuration directory */
411     LogDebug(4, "opening %s\n", globalConfPtr->cellConfigdir);
412
413     BU_conf = afsconf_Open(globalConfPtr->cellConfigdir);
414     if (BU_conf == 0) {
415         LogError(code, "Failed getting cell info\n");
416         com_err(whoami, code, "Failed getting cell info");
417         ERROR(BUDB_NOCELLS);
418     }
419
420     code = afsconf_GetLocalCell(BU_conf, lcell, sizeof(lcell));
421     if (code) {
422         LogError(0, "** Can't determine local cell name!\n");
423         ERROR(code);
424     }
425
426     if (globalConfPtr->myHost == 0) {
427         /* if user hasn't supplied a list of servers, extract server
428          * list from the cell's database
429          */
430
431         LogDebug(1, "Using server list from %s cell database.\n", lcell);
432
433         code = afsconf_GetCellInfo(BU_conf, lcell, 0, &cellinfo);
434         code =
435             convert_cell_to_ubik(&cellinfo, &globalConfPtr->myHost,
436                                  globalConfPtr->serverList);
437         if (code)
438             ERROR(code);
439     }
440
441     /* initialize ubik */
442     ubik_CRXSecurityProc = afsconf_ClientAuth;
443     ubik_CRXSecurityRock = (char *)BU_conf;
444
445     ubik_SRXSecurityProc = afsconf_ServerAuth;
446     ubik_SRXSecurityRock = (char *)BU_conf;
447
448     ubik_CheckRXSecurityProc = afsconf_CheckAuth;
449     ubik_CheckRXSecurityRock = (char *)BU_conf;
450
451     if (ubik_nBuffers == 0)
452         ubik_nBuffers = 400;
453
454     LogError(0, "Will allocate %d ubik buffers\n", ubik_nBuffers);
455
456     dbNamePtr =
457         (char *)malloc(strlen(globalConfPtr->databaseDirectory) +
458                        strlen(globalConfPtr->databaseName) + 1);
459     if (dbNamePtr == 0)
460         ERROR(-1);
461
462     /* construct the database name */
463     strcpy(dbNamePtr, globalConfPtr->databaseDirectory);
464     strcat(dbNamePtr, globalConfPtr->databaseName);     /* name prefix */
465
466     rx_SetRxDeadTime(60);       /* 60 seconds inactive before timeout */
467
468     code = ubik_ServerInit(globalConfPtr->myHost, htons(AFSCONF_BUDBPORT), globalConfPtr->serverList, dbNamePtr,        /* name prefix */
469                            &BU_dbase);
470     if (code) {
471         LogError(code, "Ubik init failed\n");
472         com_err(whoami, code, "Ubik init failed");
473         ERROR(code);
474     }
475
476     sca[RX_SCINDEX_NULL] = rxnull_NewServerSecurityObject();
477     sca[RX_SCINDEX_VAB] = 0;
478     sca[RX_SCINDEX_KAD] =
479         rxkad_NewServerSecurityObject(rxkad_clear, BU_conf, afsconf_GetKey,
480                                       NULL);
481
482     /* Disable jumbograms */
483     rx_SetNoJumbo();
484
485     tservice =
486         rx_NewService(0, BUDB_SERVICE, "BackupDatabase", sca, 3,
487                       BUDB_ExecuteRequest);
488     if (tservice == (struct rx_service *)0) {
489         LogError(0, "Could not create backup database rx service\n");
490         printf("Could not create backup database rx service\n");
491         BUDB_EXIT(3);
492     }
493     rx_SetMinProcs(tservice, 1);
494     rx_SetMaxProcs(tservice, 3);
495     rx_SetStackSize(tservice, 10000);
496
497     /* allow super users to manage RX statistics */
498     rx_SetRxStatUserOk(BU_rxstat_userok);
499
500     /* misc. initialization */
501
502     /* database dump synchronization */
503     memset(dumpSyncPtr, 0, sizeof(*dumpSyncPtr));
504     Lock_Init(&dumpSyncPtr->ds_lock);
505
506     rx_StartServer(0);          /* start handling requests */
507
508     code = InitProcs();
509     if (code)
510         ERROR(code);
511
512
513     currentTime = time(0);
514     LogError(0, "Ready to process requests at %s\n", ctime(&currentTime));
515
516     rx_ServerProc();            /* donate this LWP */
517
518   error_exit:
519     osi_audit(BUDB_FinishEvent, code, AUD_END);
520     return (code);
521 }
522
523
524 consistencyCheckDb()
525 {
526     /* do consistency checks on structure sizes */
527     if ((sizeof(struct htBlock) > BLOCKSIZE)
528         || (sizeof(struct vfBlock) > BLOCKSIZE)
529         || (sizeof(struct viBlock) > BLOCKSIZE)
530         || (sizeof(struct dBlock) > BLOCKSIZE)
531         || (sizeof(struct tBlock) > BLOCKSIZE)
532         ) {
533         fprintf(stderr, "Block layout error!\n");
534         BUDB_EXIT(99);
535     }
536 }
537
538  /*VARARGS*/
539 LogDebug(level, a, b, c, d, e, f, g, h, i)
540      int level;
541      char *a, *b, *c, *d, *e, *f, *g, *h, *i;
542 {
543
544     if (debugging >= level) {
545         /* log normally closed so can remove it */
546         globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
547         if (globalConfPtr->log != NULL) {
548             fprintf(globalConfPtr->log, a, b, c, d, e, f, g, h, i);
549             fflush(globalConfPtr->log);
550             fclose(globalConfPtr->log);
551         }
552     }
553     return 0;
554 }
555
556 static char *
557 TimeStamp(time_t t)
558 {
559     struct tm *lt;
560     static char timestamp[20];
561
562     lt = localtime(&t);
563     strftime(timestamp, 20, "%m/%d/%Y %T", lt);
564     return timestamp;
565 }
566
567  /*VARARGS*/
568 Log(a, b, c, d, e, f, g, h, i)
569      char *a, *b, *c, *d, *e, *f, *g, *h, *i;
570 {
571     time_t now;
572
573     globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
574     if (globalConfPtr->log != NULL) {
575         now = time(0);
576         fprintf(globalConfPtr->log, "%s ", TimeStamp(now));
577
578         fprintf(globalConfPtr->log, a, b, c, d, e, f, g, h, i);
579         fflush(globalConfPtr->log);
580         fclose(globalConfPtr->log);
581     }
582     return 0;
583 }
584
585  /*VARARGS*/
586 LogError(code, a, b, c, d, e, f, g, h, i)
587      long code;
588      char *a, *b, *c, *d, *e, *f, *g, *h, *i;
589 {
590     time_t now;
591
592     globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
593
594     if (globalConfPtr->log != NULL) {
595         now = time(0);
596         fprintf(globalConfPtr->log, "%s ", TimeStamp(now));
597
598         if (code)
599             fprintf(globalConfPtr->log, "%s: %s\n", error_table_name(code),
600                     error_message(code));
601         fprintf(globalConfPtr->log, a, b, c, d, e, f, g, h, i);
602         fflush(globalConfPtr->log);
603         fclose(globalConfPtr->log);
604     }
605     return 0;
606 }
607
608
609 /*  ----------------
610  * debug
611  * ----------------
612  */
613
614
615 LogNetDump(dumpPtr)
616      struct dump *dumpPtr;
617 {
618     struct dump hostDump;
619     extern buServerConfP globalConfPtr;
620
621     dump_ntoh(dumpPtr, &hostDump);
622
623     globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
624     if (globalConfPtr->log != NULL) {
625         printDump(globalConfPtr->log, &hostDump);
626         fclose(globalConfPtr->log);
627     }
628     return 0;
629 }