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>
19 #include <sys/types.h>
27 #include <sys/audit.h>
31 #define AUDIT_FAIL_AUTH 2
32 #define AUDIT_FAIL_ACCESS 3
33 #define AUDIT_FAIL_PRIV 4
34 #endif /* AFS_AIX32_ENV */
37 #include "afs/afsint.h"
43 #include <sys/audit.h>
45 #include <afs/afsutil.h>
47 /* C99 requires va_copy. Older versions of GCC provide __va_copy. Per t
48 Autoconf manual, memcpy is a generally portable fallback. */
51 # define va_copy(d, s) __va_copy((d), (s))
53 # define va_copy(d, s) memcpy(&(d), &(s), sizeof(va_list))
59 int osi_audit_all = (-1); /* Not determined yet */
60 int osi_echo_trail = (-1);
62 FILE *auditout = NULL;
64 int osi_audit_check();
67 audmakebuf(char *audEvent, va_list vaList)
79 vaEntry = va_arg(vaList, int);
80 while (vaEntry != AUD_END) {
82 case AUD_STR: /* String */
83 case AUD_NAME: /* Name */
84 case AUD_ACL: /* ACL */
85 vaStr = (char *)va_arg(vaList, char *);
87 strcpy(bufferPtr, vaStr);
88 bufferPtr += strlen(vaStr) + 1;
90 strcpy(bufferPtr, "");
94 case AUD_INT: /* Integer */
95 case AUD_ID: /* ViceId */
96 vaInt = va_arg(vaList, int);
97 *(int *)bufferPtr = vaInt;
98 bufferPtr += sizeof(vaInt);
100 case AUD_DATE: /* Date */
101 case AUD_HOST: /* Host ID */
102 case AUD_LONG: /* long */
103 vaLong = va_arg(vaList, afs_int32);
104 *(afs_int32 *) bufferPtr = vaLong;
105 bufferPtr += sizeof(vaLong);
107 case AUD_FID: /* AFSFid - contains 3 entries */
108 vaFid = (struct AFSFid *)va_arg(vaList, struct AFSFid *);
110 memcpy(bufferPtr, vaFid, sizeof(struct AFSFid));
112 memset(bufferPtr, 0, sizeof(struct AFSFid));
114 bufferPtr += sizeof(struct AFSFid);
117 /* Whole array of fids-- don't know how to handle variable length audit
118 * data with AIX audit package, so for now we just store the first fid.
119 * Better one than none. */
122 struct AFSCBFids *Fids;
124 Fids = (struct AFSCBFids *)va_arg(vaList, struct AFSCBFids *);
125 if (Fids && Fids->AFSCBFids_len) {
126 *((u_int *) bufferPtr) = Fids->AFSCBFids_len;
127 bufferPtr += sizeof(u_int);
128 memcpy(bufferPtr, Fids->AFSCBFids_val,
129 sizeof(struct AFSFid));
131 *((u_int *) bufferPtr) = 0;
132 bufferPtr += sizeof(u_int);
133 memset(bufferPtr, 0, sizeof(struct AFSFid));
135 bufferPtr += sizeof(struct AFSFid);
141 auditlog("AFS_Aud_EINVAL", (-1), audEvent,
142 (strlen(audEvent) + 1));
148 vaEntry = va_arg(vaList, int);
153 printbuf(FILE *out, int rec, char *audEvent, char *afsName, afs_int32 hostId,
154 afs_int32 errCode, va_list vaList)
161 struct AFSFid *vaFid;
162 struct AFSCBFids *vaFids;
163 int num = LogThreadNum();
164 struct in_addr hostAddr;
169 /* Don't print the timestamp or thread id if we recursed */
171 currenttime = time(0);
172 timeStamp = afs_ctime(¤ttime, tbuffer,
174 timeStamp[24] = ' '; /* ts[24] is the newline, 25 is the null */
175 fprintf(out, timeStamp);
178 fprintf(out, "[%d] ", num);
181 fprintf(out, "EVENT %s CODE %d ", audEvent, errCode);
184 hostAddr.s_addr = hostId;
185 fprintf(out, "NAME %s HOST %s ", afsName, inet_ntoa(hostAddr));
188 vaEntry = va_arg(vaList, int);
189 while (vaEntry != AUD_END) {
191 case AUD_STR: /* String */
192 vaStr = (char *)va_arg(vaList, char *);
194 fprintf(out, "STR %s ", vaStr);
196 fprintf(out, "STR <null>");
198 case AUD_NAME: /* Name */
199 vaStr = (char *)va_arg(vaList, char *);
201 fprintf(out, "NAME %s ", vaStr);
203 fprintf(out, "NAME <null>");
205 case AUD_ACL: /* ACL */
206 vaStr = (char *)va_arg(vaList, char *);
208 fprintf(out, "ACL %s ", vaStr);
210 fprintf(out, "ACL <null>");
212 case AUD_INT: /* Integer */
213 vaInt = va_arg(vaList, int);
214 fprintf(out, "INT %d ", vaInt);
216 case AUD_ID: /* ViceId */
217 vaInt = va_arg(vaList, int);
218 fprintf(out, "ID %d ", vaInt);
220 case AUD_DATE: /* Date */
221 vaLong = va_arg(vaList, afs_int32);
222 fprintf(out, "DATE %u ", vaLong);
224 case AUD_HOST: /* Host ID */
225 vaLong = va_arg(vaList, afs_int32);
226 hostAddr.s_addr = vaLong;
227 fprintf(out, "HOST %s ", inet_ntoa(hostAddr));
229 case AUD_LONG: /* afs_int32 */
230 vaLong = va_arg(vaList, afs_int32);
231 fprintf(out, "LONG %d ", vaLong);
233 case AUD_FID: /* AFSFid - contains 3 entries */
234 vaFid = va_arg(vaList, struct AFSFid *);
236 fprintf(out, "FID %u:%u:%u ", vaFid->Volume, vaFid->Vnode,
239 fprintf(out, "FID %u:%u:%u ", 0, 0, 0);
241 case AUD_FIDS: /* array of Fids */
242 vaFids = va_arg(vaList, struct AFSCBFids *);
247 vaFid = vaFids->AFSCBFids_val;
250 fprintf(out, "FIDS %u FID %u:%u:%u ", vaFids->AFSCBFids_len, vaFid->Volume,
251 vaFid->Vnode, vaFid->Unique);
252 for ( i = 1; i < vaFids->AFSCBFids_len; i++, vaFid++ )
253 fprintf(out, "FID %u:%u:%u ", vaFid->Volume,
254 vaFid->Vnode, vaFid->Unique);
256 fprintf(out, "FIDS 0 FID 0:0:0 ");
261 fprintf(out, "--badval-- ");
264 vaEntry = va_arg(vaList, int);
270 #ifdef AFS_PTHREAD_ENV
271 static pthread_mutex_t audit_lock;
272 static volatile afs_int32 audit_lock_initialized = 0;
273 static pthread_once_t audit_lock_once = PTHREAD_ONCE_INIT;
276 osi_audit_init_lock(void)
278 pthread_mutex_init(&audit_lock, NULL);
279 audit_lock_initialized = 1;
286 #ifdef AFS_PTHREAD_ENV
287 if (!audit_lock_initialized) {
288 pthread_once(&audit_lock_once, osi_audit_init_lock);
290 #endif /* AFS_PTHREAD_ENV */
293 /* ************************************************************************** */
294 /* The routine that acually does the audit call.
295 * ************************************************************************** */
297 osi_audit_internal(char *audEvent, /* Event name (15 chars or less) */
298 afs_int32 errCode, /* The error code */
306 static char BUFFER[32768];
311 #ifdef AFS_PTHREAD_ENV
312 /* i'm pretty sure all the server apps now call osi_audit_init(),
313 * but to be extra careful we'll leave this assert in here for a
314 * while to make sure */
315 assert(audit_lock_initialized);
316 #endif /* AFS_PTHREAD_ENV */
318 if ((osi_audit_all < 0) || (osi_echo_trail < 0))
320 if (!osi_audit_all && !auditout)
323 va_copy(vaCopy, vaList);
329 case KANOAUTH: /* kautils.h */
330 case RXKADNOAUTH: /* rxkad.h */
331 result = AUDIT_FAIL_AUTH;
333 case EPERM: /* errno.h */
334 case EACCES: /* errno.h */
335 case PRPERM: /* pterror.h */
336 result = AUDIT_FAIL_ACCESS;
338 case VL_PERM: /* vlserver.h */
339 case BUDB_NOTPERMITTED: /* budb_errs.h */
340 case BZACCESS: /* bnode.h */
341 case VOLSERBAD_ACCESS: /* volser.h */
342 result = AUDIT_FAIL_PRIV;
349 #ifdef AFS_PTHREAD_ENV
350 pthread_mutex_lock(&audit_lock);
355 /* Put the error code into the buffer list */
356 *(int *)bufferPtr = errCode;
357 bufferPtr += sizeof(errCode);
359 audmakebuf(audEvent, vaList);
362 if (osi_echo_trail) {
363 printbuf(stdout, 0, audEvent, afsName, hostId, errCode, vaList);
368 bufferLen = (int)((afs_int32) bufferPtr - (afs_int32) & BUFFER[0]);
369 code = auditlog(audEvent, result, BUFFER, bufferLen);
372 printbuf(auditout, 0, audEvent, afsName, hostId, errCode, vaList);
376 #ifdef AFS_PTHREAD_ENV
377 pthread_mutex_unlock(&audit_lock);
383 osi_audit(char *audEvent, /* Event name (15 chars or less) */
384 afs_int32 errCode, /* The error code */
389 if ((osi_audit_all < 0) || (osi_echo_trail < 0))
391 if (!osi_audit_all && !auditout)
394 va_start(vaList, errCode);
395 osi_audit_internal(audEvent, errCode, NULL, 0, vaList);
401 /* ************************************************************************** */
402 /* Given a RPC call structure, this routine extracts the name and host id from the
403 * call and includes it within the audit information.
404 * ************************************************************************** */
406 osi_auditU(struct rx_call *call, char *audEvent, int errCode, ...)
408 struct rx_connection *conn;
409 struct rx_peer *peer;
412 char afsName[MAXKTCNAMELEN];
416 if (osi_audit_all < 0)
418 if (!osi_audit_all && !auditout)
421 strcpy(afsName, "--Unknown--");
425 conn = rx_ConnectionOf(call); /* call -> conn) */
427 secClass = rx_SecurityClassOf(conn); /* conn -> securityIndex */
428 if (secClass == 0) { /* unauthenticated */
429 osi_audit("AFS_Aud_Unauth", (-1), AUD_STR, audEvent, AUD_END);
430 strcpy(afsName, "--UnAuth--");
431 } else if (secClass == 2) { /* authenticated */
432 char tcell[MAXKTCREALMLEN];
433 char name[MAXKTCNAMELEN];
434 char inst[MAXKTCNAMELEN];
439 rxkad_GetServerInfo(conn, NULL, NULL, name, inst, tcell,
442 osi_audit("AFS_Aud_NoAFSId", (-1), AUD_STR, audEvent, AUD_END);
443 strcpy(afsName, "--NoName--");
445 strncpy(vname, name, sizeof(vname));
446 if ((ilen = strlen(inst))) {
447 if (strlen(vname) + 1 + ilen >= sizeof(vname))
452 if ((clen = strlen(tcell))) {
453 #if defined(AFS_ATHENA_STDENV) || defined(AFS_KERBREALM_ENV)
454 static char local_realms[AFS_NUM_LREALMS][AFS_REALM_SZ];
455 static int num_lrealms = -1;
458 if (num_lrealms == -1) {
459 for (i=0; i<AFS_NUM_LREALMS; i++) {
460 if (afs_krb_get_lrealm(local_realms[i], i) != 0 /*KSUCCESS*/)
465 strncpy(local_realms[0], "UNKNOWN.LOCAL.REALM", AFS_REALM_SZ);
469 /* Check to see if the ticket cell matches one of the local realms */
471 for ( i=0;i<num_lrealms;i++ ) {
472 if (!strcasecmp(local_realms[i], tcell)) {
477 /* If yes, then make sure that the name is not present in
478 * an exclusion list */
482 snprintf(uname,sizeof(uname),"%s.%s@%s",name,inst,tcell);
484 snprintf(uname,sizeof(uname),"%s@%s",name,tcell);
486 if (afs_krb_exclusion(uname))
491 if (strlen(vname) + 1 + clen >= sizeof(vname))
494 strcat(vname, tcell);
498 strcpy(afsName, vname);
500 } else { /* Unauthenticated & unknown */
501 osi_audit("AFS_Aud_UnknSec", (-1), AUD_STR, audEvent, AUD_END);
502 strcpy(afsName, "--Unknown--");
505 peer = rx_PeerOf(conn); /* conn -> peer */
507 hostId = rx_HostOf(peer); /* peer -> host */
509 osi_audit("AFS_Aud_NoHost", (-1), AUD_STR, audEvent, AUD_END);
510 } else { /* null conn */
511 osi_audit("AFS_Aud_NoConn", (-1), AUD_STR, audEvent, AUD_END);
513 } else { /* null call */
514 osi_audit("AFS_Aud_NoCall", (-1), AUD_STR, audEvent, AUD_END);
516 va_start(vaList, errCode);
517 osi_audit_internal(audEvent, errCode, afsName, hostId, vaList);
522 /* ************************************************************************** */
523 /* Determines whether auditing is on or off by looking at the Audit file.
524 * If the string AFS_AUDIT_AllEvents is defined in the file, then auditing will be
526 * ************************************************************************** */
535 osi_audit_all = 1; /* say we made check (>= 0) */
536 /* and assume audit all events (for now) */
537 onoff = 0; /* assume we will turn auditing off */
538 osi_echo_trail = 0; /* assume no echoing */
540 fds = fopen(AFSDIR_SERVER_AUDIT_FILEPATH, "r");
542 while (fscanf(fds, "%256s", event) > 0) {
543 if (strcmp(event, "AFS_AUDIT_AllEvents") == 0)
546 if (strcmp(event, "Echo_Trail") == 0)
552 /* Audit this event all of the time */
554 osi_audit("AFS_Aud_On", 0, AUD_END);
556 osi_audit("AFS_Aud_Off", 0, AUD_END);
558 /* Now set whether we audit all events from here on out */
559 osi_audit_all = onoff;
565 osi_audit_file(char *fileName)
568 char oldName[MAXPATHLEN];
573 if ((lstat(fileName, &statbuf) == 0)
574 && (S_ISFIFO(statbuf.st_mode))) {
575 flags = O_WRONLY | O_NONBLOCK;
579 strcpy(oldName, fileName);
580 strcat(oldName, ".old");
581 renamefile(fileName, oldName);
582 flags = O_WRONLY | O_TRUNC | O_CREAT;
584 tempfd = open(fileName, flags, 0666);
586 auditout = fdopen(tempfd, "a");
588 printf("Warning: auditlog %s not writable, ignored.\n", fileName);
592 printf("Warning: auditlog %s not writable, ignored.\n", fileName);