auditlogs-for-everyone-20050702
[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(BUDB_StartEvent, 0, AUD_END);
399
400     initialize_BUDB_error_table();
401     initializeArgHandler();
402
403     /* Initialize dirpaths */
404     if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
405 #ifdef AFS_NT40_ENV
406         ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
407 #endif
408         com_err(whoami, errno, "; Unable to obtain AFS server directory.");
409         exit(2);
410     }
411
412     memset(globalConfPtr, 0, sizeof(*globalConfPtr));
413
414     /* set default configuration values */
415     strcpy(dbDir, AFSDIR_SERVER_DB_DIRPATH);
416     strcat(dbDir, "/");
417     globalConfPtr->databaseDirectory = dbDir;
418     globalConfPtr->databaseName = DEFAULT_DBPREFIX;
419     strcpy(cellConfDir, AFSDIR_SERVER_ETC_DIRPATH);
420     globalConfPtr->cellConfigdir = cellConfDir;
421
422     /* open the log file */
423 /*
424     globalConfPtr->log = fopen(DEFAULT_LOGNAME,"a");
425     if ( globalConfPtr->log == NULL )
426     {   
427         printf("Can't open log file %s - aborting\n", DEFAULT_LOGNAME);
428         BUDB_EXIT(-1);
429     }
430 */
431
432     srandom(1);
433
434 #ifdef AFS_PTHREAD_ENV
435     SetLogThreadNumProgram( threadNum );
436 #endif
437
438     /* process the user supplied args */
439     helpOption = 1;
440     code = cmd_Dispatch(argc, argv);
441     if (code)
442         ERROR(code);
443
444     /* exit if there was a help option */
445     if (helpOption)
446         BUDB_EXIT(0);
447
448     /* open the log file */
449     globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
450     if (globalConfPtr->log == NULL) {
451         printf("Can't open log file %s - aborting\n",
452                AFSDIR_SERVER_BUDBLOG_FILEPATH);
453         BUDB_EXIT(-1);
454     }
455
456     /* keep log closed so can remove it */
457
458     fclose(globalConfPtr->log);
459
460     /* open the cell's configuration directory */
461     LogDebug(4, "opening %s\n", globalConfPtr->cellConfigdir);
462
463     BU_conf = afsconf_Open(globalConfPtr->cellConfigdir);
464     if (BU_conf == 0) {
465         LogError(code, "Failed getting cell info\n");
466         com_err(whoami, code, "Failed getting cell info");
467         ERROR(BUDB_NOCELLS);
468     }
469
470     code = afsconf_GetLocalCell(BU_conf, lcell, sizeof(lcell));
471     if (code) {
472         LogError(0, "** Can't determine local cell name!\n");
473         ERROR(code);
474     }
475
476     if (globalConfPtr->myHost == 0) {
477         /* if user hasn't supplied a list of servers, extract server
478          * list from the cell's database
479          */
480
481         LogDebug(1, "Using server list from %s cell database.\n", lcell);
482
483         code = afsconf_GetExtendedCellInfo (BU_conf, lcell, 0, &cellinfo, 
484                                             &clones); 
485         code =
486             convert_cell_to_ubik(&cellinfo, &globalConfPtr->myHost,
487                                  globalConfPtr->serverList);
488         if (code)
489             ERROR(code);
490     }
491
492     /* initialize ubik */
493     ubik_CRXSecurityProc = afsconf_ClientAuth;
494     ubik_CRXSecurityRock = (char *)BU_conf;
495
496     ubik_SRXSecurityProc = afsconf_ServerAuth;
497     ubik_SRXSecurityRock = (char *)BU_conf;
498
499     ubik_CheckRXSecurityProc = afsconf_CheckAuth;
500     ubik_CheckRXSecurityRock = (char *)BU_conf;
501
502     if (ubik_nBuffers == 0)
503         ubik_nBuffers = 400;
504
505     LogError(0, "Will allocate %d ubik buffers\n", ubik_nBuffers);
506
507     dbNamePtr =
508         (char *)malloc(strlen(globalConfPtr->databaseDirectory) +
509                        strlen(globalConfPtr->databaseName) + 1);
510     if (dbNamePtr == 0)
511         ERROR(-1);
512
513     /* construct the database name */
514     strcpy(dbNamePtr, globalConfPtr->databaseDirectory);
515     strcat(dbNamePtr, globalConfPtr->databaseName);     /* name prefix */
516
517     rx_SetRxDeadTime(60);       /* 60 seconds inactive before timeout */
518
519     code = ubik_ServerInitByInfo (globalConfPtr->myHost,
520                                   htons(AFSCONF_BUDBPORT), 
521                                   &cellinfo,
522                                   &clones,              
523                                   dbNamePtr,           /* name prefix */
524                                   &BU_dbase);
525
526     if (code) {
527         LogError(code, "Ubik init failed\n");
528         com_err(whoami, code, "Ubik init failed");
529         ERROR(code);
530     }
531
532     sca[RX_SCINDEX_NULL] = rxnull_NewServerSecurityObject();
533     sca[RX_SCINDEX_VAB] = 0;
534     sca[RX_SCINDEX_KAD] =
535         rxkad_NewServerSecurityObject(rxkad_clear, BU_conf, afsconf_GetKey,
536                                       NULL);
537
538     /* Disable jumbograms */
539     rx_SetNoJumbo();
540
541     tservice =
542         rx_NewService(0, BUDB_SERVICE, "BackupDatabase", sca, 3,
543                       BUDB_ExecuteRequest);
544     if (tservice == (struct rx_service *)0) {
545         LogError(0, "Could not create backup database rx service\n");
546         printf("Could not create backup database rx service\n");
547         BUDB_EXIT(3);
548     }
549     rx_SetMinProcs(tservice, 1);
550     rx_SetMaxProcs(tservice, 3);
551     rx_SetStackSize(tservice, 10000);
552
553     /* allow super users to manage RX statistics */
554     rx_SetRxStatUserOk(BU_rxstat_userok);
555
556     /* misc. initialization */
557
558     /* database dump synchronization */
559     memset(dumpSyncPtr, 0, sizeof(*dumpSyncPtr));
560     Lock_Init(&dumpSyncPtr->ds_lock);
561
562     rx_StartServer(0);          /* start handling requests */
563
564     code = InitProcs();
565     if (code)
566         ERROR(code);
567
568
569     currentTime = time(0);
570     LogError(0, "Ready to process requests at %s\n", ctime(&currentTime));
571
572     rx_ServerProc();            /* donate this LWP */
573
574   error_exit:
575     osi_audit(BUDB_FinishEvent, code, AUD_END);
576     return (code);
577 }
578
579
580 consistencyCheckDb()
581 {
582     /* do consistency checks on structure sizes */
583     if ((sizeof(struct htBlock) > BLOCKSIZE)
584         || (sizeof(struct vfBlock) > BLOCKSIZE)
585         || (sizeof(struct viBlock) > BLOCKSIZE)
586         || (sizeof(struct dBlock) > BLOCKSIZE)
587         || (sizeof(struct tBlock) > BLOCKSIZE)
588         ) {
589         fprintf(stderr, "Block layout error!\n");
590         BUDB_EXIT(99);
591     }
592 }
593
594  /*VARARGS*/
595 LogDebug(level, a, b, c, d, e, f, g, h, i)
596      int level;
597      char *a, *b, *c, *d, *e, *f, *g, *h, *i;
598 {
599
600     if (debugging >= level) {
601         /* log normally closed so can remove it */
602         globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
603         if (globalConfPtr->log != NULL) {
604             fprintf(globalConfPtr->log, a, b, c, d, e, f, g, h, i);
605             fflush(globalConfPtr->log);
606             fclose(globalConfPtr->log);
607         }
608     }
609     return 0;
610 }
611
612 static char *
613 TimeStamp(time_t t)
614 {
615     struct tm *lt;
616     static char timestamp[20];
617
618     lt = localtime(&t);
619     strftime(timestamp, 20, "%m/%d/%Y %T", lt);
620     return timestamp;
621 }
622
623  /*VARARGS*/
624 Log(a, b, c, d, e, f, g, h, i)
625      char *a, *b, *c, *d, *e, *f, *g, *h, *i;
626 {
627     time_t now;
628
629     globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
630     if (globalConfPtr->log != NULL) {
631         now = time(0);
632         fprintf(globalConfPtr->log, "%s ", TimeStamp(now));
633
634         fprintf(globalConfPtr->log, a, b, c, d, e, f, g, h, i);
635         fflush(globalConfPtr->log);
636         fclose(globalConfPtr->log);
637     }
638     return 0;
639 }
640
641  /*VARARGS*/
642 LogError(code, a, b, c, d, e, f, g, h, i)
643      long code;
644      char *a, *b, *c, *d, *e, *f, *g, *h, *i;
645 {
646     time_t now;
647
648     globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
649
650     if (globalConfPtr->log != NULL) {
651         now = time(0);
652         fprintf(globalConfPtr->log, "%s ", TimeStamp(now));
653
654         if (code)
655             fprintf(globalConfPtr->log, "%s: %s\n", error_table_name(code),
656                     error_message(code));
657         fprintf(globalConfPtr->log, a, b, c, d, e, f, g, h, i);
658         fflush(globalConfPtr->log);
659         fclose(globalConfPtr->log);
660     }
661     return 0;
662 }
663
664
665 /*  ----------------
666  * debug
667  * ----------------
668  */
669
670
671 LogNetDump(dumpPtr)
672      struct dump *dumpPtr;
673 {
674     struct dump hostDump;
675     extern buServerConfP globalConfPtr;
676
677     dump_ntoh(dumpPtr, &hostDump);
678
679     globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
680     if (globalConfPtr->log != NULL) {
681         printDump(globalConfPtr->log, &hostDump);
682         fclose(globalConfPtr->log);
683     }
684     return 0;
685 }