2 * Copyright 2000, International Business Machines Corporation and others.
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
10 #include <afsconfig.h>
11 #include <afs/param.h>
17 #include <sys/types.h>
25 #include <sys/audit.h>
29 #define AUDIT_FAIL_AUTH 2
30 #define AUDIT_FAIL_ACCESS 3
31 #define AUDIT_FAIL_PRIV 4
32 #endif /* AFS_AIX32_ENV */
35 #include "afs/afsint.h"
41 #include <sys/audit.h>
43 #include <afs/afsutil.h>
45 /* C99 requires va_copy. Older versions of GCC provide __va_copy. Per t
46 Autoconf manual, memcpy is a generally portable fallback. */
49 # define va_copy(d, s) __va_copy((d), (s))
51 # define va_copy(d, s) memcpy(&(d), &(s), sizeof(va_list))
57 int osi_audit_all = (-1); /* Not determined yet */
58 int osi_echo_trail = (-1);
60 FILE *auditout = NULL;
62 int osi_audit_check(void);
66 audmakebuf(char *audEvent, va_list vaList)
77 vaEntry = va_arg(vaList, int);
78 while (vaEntry != AUD_END) {
80 case AUD_STR: /* String */
81 case AUD_NAME: /* Name */
82 case AUD_ACL: /* ACL */
83 vaStr = (char *)va_arg(vaList, char *);
85 strcpy(bufferPtr, vaStr);
86 bufferPtr += strlen(vaStr) + 1;
88 strcpy(bufferPtr, "");
92 case AUD_INT: /* Integer */
93 case AUD_ID: /* ViceId */
94 vaInt = va_arg(vaList, int);
95 *(int *)bufferPtr = vaInt;
96 bufferPtr += sizeof(vaInt);
98 case AUD_DATE: /* Date */
99 case AUD_HOST: /* Host ID */
100 case AUD_LONG: /* long */
101 vaLong = va_arg(vaList, afs_int32);
102 *(afs_int32 *) bufferPtr = vaLong;
103 bufferPtr += sizeof(vaLong);
105 case AUD_FID: /* AFSFid - contains 3 entries */
106 vaFid = (struct AFSFid *)va_arg(vaList, struct AFSFid *);
108 memcpy(bufferPtr, vaFid, sizeof(struct AFSFid));
110 memset(bufferPtr, 0, sizeof(struct AFSFid));
112 bufferPtr += sizeof(struct AFSFid);
115 /* Whole array of fids-- don't know how to handle variable length audit
116 * data with AIX audit package, so for now we just store the first fid.
117 * Better one than none. */
120 struct AFSCBFids *Fids;
122 Fids = (struct AFSCBFids *)va_arg(vaList, struct AFSCBFids *);
123 if (Fids && Fids->AFSCBFids_len) {
124 *((u_int *) bufferPtr) = Fids->AFSCBFids_len;
125 bufferPtr += sizeof(u_int);
126 memcpy(bufferPtr, Fids->AFSCBFids_val,
127 sizeof(struct AFSFid));
129 *((u_int *) bufferPtr) = 0;
130 bufferPtr += sizeof(u_int);
131 memset(bufferPtr, 0, sizeof(struct AFSFid));
133 bufferPtr += sizeof(struct AFSFid);
139 auditlog("AFS_Aud_EINVAL", (-1), audEvent,
140 (strlen(audEvent) + 1));
146 vaEntry = va_arg(vaList, int);
152 printbuf(FILE *out, int rec, char *audEvent, char *afsName, afs_int32 hostId,
153 afs_int32 errCode, va_list vaList)
159 struct AFSFid *vaFid;
160 struct AFSCBFids *vaFids;
161 int num = LogThreadNum();
162 struct in_addr hostAddr;
167 /* Don't print the timestamp or thread id if we recursed */
169 currenttime = time(0);
170 timeStamp = afs_ctime(¤ttime, tbuffer,
172 timeStamp[24] = ' '; /* ts[24] is the newline, 25 is the null */
173 fprintf(out, timeStamp);
176 fprintf(out, "[%d] ", num);
179 fprintf(out, "EVENT %s CODE %d ", audEvent, errCode);
182 hostAddr.s_addr = hostId;
183 fprintf(out, "NAME %s HOST %s ", afsName, inet_ntoa(hostAddr));
186 vaEntry = va_arg(vaList, int);
187 while (vaEntry != AUD_END) {
189 case AUD_STR: /* String */
190 vaStr = (char *)va_arg(vaList, char *);
192 fprintf(out, "STR %s ", vaStr);
194 fprintf(out, "STR <null>");
196 case AUD_NAME: /* Name */
197 vaStr = (char *)va_arg(vaList, char *);
199 fprintf(out, "NAME %s ", vaStr);
201 fprintf(out, "NAME <null>");
203 case AUD_ACL: /* ACL */
204 vaStr = (char *)va_arg(vaList, char *);
206 fprintf(out, "ACL %s ", vaStr);
208 fprintf(out, "ACL <null>");
210 case AUD_INT: /* Integer */
211 vaInt = va_arg(vaList, int);
212 fprintf(out, "INT %d ", vaInt);
214 case AUD_ID: /* ViceId */
215 vaInt = va_arg(vaList, int);
216 fprintf(out, "ID %d ", vaInt);
218 case AUD_DATE: /* Date */
219 vaLong = va_arg(vaList, afs_int32);
220 fprintf(out, "DATE %u ", vaLong);
222 case AUD_HOST: /* Host ID */
223 vaLong = va_arg(vaList, afs_int32);
224 hostAddr.s_addr = vaLong;
225 fprintf(out, "HOST %s ", inet_ntoa(hostAddr));
227 case AUD_LONG: /* afs_int32 */
228 vaLong = va_arg(vaList, afs_int32);
229 fprintf(out, "LONG %d ", vaLong);
231 case AUD_FID: /* AFSFid - contains 3 entries */
232 vaFid = va_arg(vaList, struct AFSFid *);
234 fprintf(out, "FID %u:%u:%u ", vaFid->Volume, vaFid->Vnode,
237 fprintf(out, "FID %u:%u:%u ", 0, 0, 0);
239 case AUD_FIDS: /* array of Fids */
240 vaFids = va_arg(vaList, struct AFSCBFids *);
245 vaFid = vaFids->AFSCBFids_val;
248 fprintf(out, "FIDS %u FID %u:%u:%u ", vaFids->AFSCBFids_len, vaFid->Volume,
249 vaFid->Vnode, vaFid->Unique);
250 for ( i = 1; i < vaFids->AFSCBFids_len; i++, vaFid++ )
251 fprintf(out, "FID %u:%u:%u ", vaFid->Volume,
252 vaFid->Vnode, vaFid->Unique);
254 fprintf(out, "FIDS 0 FID 0:0:0 ");
259 fprintf(out, "--badval-- ");
262 vaEntry = va_arg(vaList, int);
268 #ifdef AFS_PTHREAD_ENV
269 static pthread_mutex_t audit_lock;
270 static volatile afs_int32 audit_lock_initialized = 0;
271 static pthread_once_t audit_lock_once = PTHREAD_ONCE_INIT;
274 osi_audit_init_lock(void)
276 pthread_mutex_init(&audit_lock, NULL);
277 audit_lock_initialized = 1;
284 #ifdef AFS_PTHREAD_ENV
285 if (!audit_lock_initialized) {
286 pthread_once(&audit_lock_once, osi_audit_init_lock);
288 #endif /* AFS_PTHREAD_ENV */
291 /* ************************************************************************** */
292 /* The routine that acually does the audit call.
293 * ************************************************************************** */
295 osi_audit_internal(char *audEvent, /* Event name (15 chars or less) */
296 afs_int32 errCode, /* The error code */
304 static char BUFFER[32768];
309 #ifdef AFS_PTHREAD_ENV
310 /* i'm pretty sure all the server apps now call osi_audit_init(),
311 * but to be extra careful we'll leave this assert in here for a
312 * while to make sure */
313 assert(audit_lock_initialized);
314 #endif /* AFS_PTHREAD_ENV */
316 if ((osi_audit_all < 0) || (osi_echo_trail < 0))
318 if (!osi_audit_all && !auditout)
321 va_copy(vaCopy, vaList);
327 case KANOAUTH: /* kautils.h */
328 case RXKADNOAUTH: /* rxkad.h */
329 result = AUDIT_FAIL_AUTH;
331 case EPERM: /* errno.h */
332 case EACCES: /* errno.h */
333 case PRPERM: /* pterror.h */
334 result = AUDIT_FAIL_ACCESS;
336 case VL_PERM: /* vlserver.h */
337 case BUDB_NOTPERMITTED: /* budb_errs.h */
338 case BZACCESS: /* bnode.h */
339 case VOLSERBAD_ACCESS: /* volser.h */
340 result = AUDIT_FAIL_PRIV;
347 #ifdef AFS_PTHREAD_ENV
348 pthread_mutex_lock(&audit_lock);
353 /* Put the error code into the buffer list */
354 *(int *)bufferPtr = errCode;
355 bufferPtr += sizeof(errCode);
357 audmakebuf(audEvent, vaList);
360 if (osi_echo_trail) {
361 printbuf(stdout, 0, audEvent, afsName, hostId, errCode, vaList);
366 bufferLen = (int)((afs_int32) bufferPtr - (afs_int32) & BUFFER[0]);
367 code = auditlog(audEvent, result, BUFFER, bufferLen);
370 printbuf(auditout, 0, audEvent, afsName, hostId, errCode, vaList);
374 #ifdef AFS_PTHREAD_ENV
375 pthread_mutex_unlock(&audit_lock);
381 osi_audit(char *audEvent, /* Event name (15 chars or less) */
382 afs_int32 errCode, /* The error code */
387 if ((osi_audit_all < 0) || (osi_echo_trail < 0))
389 if (!osi_audit_all && !auditout)
392 va_start(vaList, errCode);
393 osi_audit_internal(audEvent, errCode, NULL, 0, vaList);
399 /* ************************************************************************** */
400 /* Given a RPC call structure, this routine extracts the name and host id from the
401 * call and includes it within the audit information.
402 * ************************************************************************** */
404 osi_auditU(struct rx_call *call, char *audEvent, int errCode, ...)
406 struct rx_connection *conn;
407 struct rx_peer *peer;
410 char afsName[MAXKTCNAMELEN];
414 if (osi_audit_all < 0)
416 if (!osi_audit_all && !auditout)
419 strcpy(afsName, "--Unknown--");
423 conn = rx_ConnectionOf(call); /* call -> conn) */
425 secClass = rx_SecurityClassOf(conn); /* conn -> securityIndex */
426 if (secClass == 0) { /* unauthenticated */
427 osi_audit("AFS_Aud_Unauth", (-1), AUD_STR, audEvent, AUD_END);
428 strcpy(afsName, "--UnAuth--");
429 } else if (secClass == 2) { /* authenticated */
430 char tcell[MAXKTCREALMLEN];
431 char name[MAXKTCNAMELEN];
432 char inst[MAXKTCNAMELEN];
437 rxkad_GetServerInfo(conn, NULL, NULL, name, inst, tcell,
440 osi_audit("AFS_Aud_NoAFSId", (-1), AUD_STR, audEvent, AUD_END);
441 strcpy(afsName, "--NoName--");
443 strncpy(vname, name, sizeof(vname));
444 if ((ilen = strlen(inst))) {
445 if (strlen(vname) + 1 + ilen >= sizeof(vname))
450 if ((clen = strlen(tcell))) {
451 #if defined(AFS_ATHENA_STDENV) || defined(AFS_KERBREALM_ENV)
452 static char local_realms[AFS_NUM_LREALMS][AFS_REALM_SZ];
453 static int num_lrealms = -1;
456 if (num_lrealms == -1) {
457 for (i=0; i<AFS_NUM_LREALMS; i++) {
458 if (afs_krb_get_lrealm(local_realms[i], i) != 0 /*KSUCCESS*/)
463 strncpy(local_realms[0], "UNKNOWN.LOCAL.REALM", AFS_REALM_SZ);
467 /* Check to see if the ticket cell matches one of the local realms */
469 for ( i=0;i<num_lrealms;i++ ) {
470 if (!strcasecmp(local_realms[i], tcell)) {
475 /* If yes, then make sure that the name is not present in
476 * an exclusion list */
480 snprintf(uname,sizeof(uname),"%s.%s@%s",name,inst,tcell);
482 snprintf(uname,sizeof(uname),"%s@%s",name,tcell);
484 if (afs_krb_exclusion(uname))
489 if (strlen(vname) + 1 + clen >= sizeof(vname))
492 strcat(vname, tcell);
496 strcpy(afsName, vname);
498 } else { /* Unauthenticated & unknown */
499 osi_audit("AFS_Aud_UnknSec", (-1), AUD_STR, audEvent, AUD_END);
500 strcpy(afsName, "--Unknown--");
503 peer = rx_PeerOf(conn); /* conn -> peer */
505 hostId = rx_HostOf(peer); /* peer -> host */
507 osi_audit("AFS_Aud_NoHost", (-1), AUD_STR, audEvent, AUD_END);
508 } else { /* null conn */
509 osi_audit("AFS_Aud_NoConn", (-1), AUD_STR, audEvent, AUD_END);
511 } else { /* null call */
512 osi_audit("AFS_Aud_NoCall", (-1), AUD_STR, audEvent, AUD_END);
514 va_start(vaList, errCode);
515 osi_audit_internal(audEvent, errCode, afsName, hostId, vaList);
520 /* ************************************************************************** */
521 /* Determines whether auditing is on or off by looking at the Audit file.
522 * If the string AFS_AUDIT_AllEvents is defined in the file, then auditing will be
524 * ************************************************************************** */
527 osi_audit_check(void)
533 osi_audit_all = 1; /* say we made check (>= 0) */
534 /* and assume audit all events (for now) */
535 onoff = 0; /* assume we will turn auditing off */
536 osi_echo_trail = 0; /* assume no echoing */
538 fds = fopen(AFSDIR_SERVER_AUDIT_FILEPATH, "r");
540 while (fscanf(fds, "%256s", event) > 0) {
541 if (strcmp(event, "AFS_AUDIT_AllEvents") == 0)
544 if (strcmp(event, "Echo_Trail") == 0)
550 /* Audit this event all of the time */
552 osi_audit("AFS_Aud_On", 0, AUD_END);
554 osi_audit("AFS_Aud_Off", 0, AUD_END);
556 /* Now set whether we audit all events from here on out */
557 osi_audit_all = onoff;
563 osi_audit_file(char *fileName)
566 char oldName[MAXPATHLEN];
571 if ((lstat(fileName, &statbuf) == 0)
572 && (S_ISFIFO(statbuf.st_mode))) {
573 flags = O_WRONLY | O_NONBLOCK;
577 strcpy(oldName, fileName);
578 strcat(oldName, ".old");
579 renamefile(fileName, oldName);
580 flags = O_WRONLY | O_TRUNC | O_CREAT;
582 tempfd = open(fileName, flags, 0666);
584 auditout = fdopen(tempfd, "a");
586 printf("Warning: auditlog %s not writable, ignored.\n", fileName);
590 printf("Warning: auditlog %s not writable, ignored.\n", fileName);