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