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>
20 #include <sys/audit.h>
24 #define AUDIT_FAIL_AUTH 2
25 #define AUDIT_FAIL_ACCESS 3
26 #define AUDIT_FAIL_PRIV 4
27 #endif /* AFS_AIX32_ENV */
30 #include "afs/afsint.h"
36 #include <sys/audit.h>
38 #include <afs/afsutil.h>
40 /* C99 requires va_copy. Older versions of GCC provide __va_copy. Per t
41 Autoconf manual, memcpy is a generally portable fallback. */
44 # define va_copy(d, s) __va_copy((d), (s))
46 # define va_copy(d, s) memcpy(&(d), &(s), sizeof(va_list))
52 int osi_audit_all = (-1); /* Not determined yet */
53 int osi_echo_trail = (-1);
55 FILE *auditout = NULL;
57 int osi_audit_check();
60 audmakebuf(char *audEvent, va_list vaList)
72 vaEntry = va_arg(vaList, int);
73 while (vaEntry != AUD_END) {
75 case AUD_STR: /* String */
76 case AUD_NAME: /* Name */
77 case AUD_ACL: /* ACL */
78 vaStr = (char *)va_arg(vaList, char *);
80 strcpy(bufferPtr, vaStr);
81 bufferPtr += strlen(vaStr) + 1;
83 strcpy(bufferPtr, "");
87 case AUD_INT: /* Integer */
88 case AUD_ID: /* ViceId */
89 vaInt = va_arg(vaList, int);
90 *(int *)bufferPtr = vaInt;
91 bufferPtr += sizeof(vaInt);
93 case AUD_DATE: /* Date */
94 case AUD_HOST: /* Host ID */
95 case AUD_LONG: /* long */
96 vaLong = va_arg(vaList, afs_int32);
97 *(afs_int32 *) bufferPtr = vaLong;
98 bufferPtr += sizeof(vaLong);
100 case AUD_FID: /* AFSFid - contains 3 entries */
101 vaFid = (struct AFSFid *)va_arg(vaList, struct AFSFid *);
103 memcpy(bufferPtr, vaFid, sizeof(struct AFSFid));
105 memset(bufferPtr, 0, sizeof(struct AFSFid));
107 bufferPtr += sizeof(struct AFSFid);
110 /* Whole array of fids-- don't know how to handle variable length audit
111 * data with AIX audit package, so for now we just store the first fid.
112 * Better one than none. */
115 struct AFSCBFids *Fids;
117 Fids = (struct AFSCBFids *)va_arg(vaList, struct AFSCBFids *);
118 if (Fids && Fids->AFSCBFids_len) {
119 *((u_int *) bufferPtr) = Fids->AFSCBFids_len;
120 bufferPtr += sizeof(u_int);
121 memcpy(bufferPtr, Fids->AFSCBFids_val,
122 sizeof(struct AFSFid));
124 *((u_int *) bufferPtr) = 0;
125 bufferPtr += sizeof(u_int);
126 memset(bufferPtr, 0, sizeof(struct AFSFid));
128 bufferPtr += sizeof(struct AFSFid);
134 auditlog("AFS_Aud_EINVAL", (-1), audEvent,
135 (strlen(audEvent) + 1));
141 vaEntry = va_arg(vaList, int);
146 printbuf(FILE *out, int rec, char *audEvent, char *afsName, afs_int32 hostId,
147 afs_int32 errCode, va_list vaList)
154 struct AFSFid *vaFid;
155 struct AFSCBFids *vaFids;
156 int num = LogThreadNum();
157 struct in_addr hostAddr;
162 /* Don't print the timestamp or thread id if we recursed */
164 currenttime = time(0);
165 timeStamp = afs_ctime(¤ttime, tbuffer,
167 timeStamp[24] = ' '; /* ts[24] is the newline, 25 is the null */
168 fprintf(out, timeStamp);
171 fprintf(out, "[%d] ", num);
174 fprintf(out, "EVENT %s CODE %d ", audEvent, errCode);
177 hostAddr.s_addr = hostId;
178 fprintf(out, "NAME %s HOST %s ", afsName, inet_ntoa(hostAddr));
181 vaEntry = va_arg(vaList, int);
182 while (vaEntry != AUD_END) {
184 case AUD_STR: /* String */
185 vaStr = (char *)va_arg(vaList, char *);
187 fprintf(out, "STR %s ", vaStr);
189 fprintf(out, "STR <null>");
191 case AUD_NAME: /* Name */
192 vaStr = (char *)va_arg(vaList, char *);
194 fprintf(out, "NAME %s ", vaStr);
196 fprintf(out, "NAME <null>");
198 case AUD_ACL: /* ACL */
199 vaStr = (char *)va_arg(vaList, char *);
201 fprintf(out, "ACL %s ", vaStr);
203 fprintf(out, "ACL <null>");
205 case AUD_INT: /* Integer */
206 vaInt = va_arg(vaList, int);
207 fprintf(out, "INT %d ", vaInt);
209 case AUD_ID: /* ViceId */
210 vaInt = va_arg(vaList, int);
211 fprintf(out, "ID %d ", vaInt);
213 case AUD_DATE: /* Date */
214 vaLong = va_arg(vaList, afs_int32);
215 fprintf(out, "DATE %u ", vaLong);
217 case AUD_HOST: /* Host ID */
218 vaLong = va_arg(vaList, afs_int32);
219 hostAddr.s_addr = vaLong;
220 fprintf(out, "HOST %s ", inet_ntoa(hostAddr));
222 case AUD_LONG: /* afs_int32 */
223 vaLong = va_arg(vaList, afs_int32);
224 fprintf(out, "LONG %d ", vaLong);
226 case AUD_FID: /* AFSFid - contains 3 entries */
227 vaFid = va_arg(vaList, struct AFSFid *);
229 fprintf(out, "FID %u:%u:%u ", vaFid->Volume, vaFid->Vnode,
232 fprintf(out, "FID %u:%u:%u ", 0, 0, 0);
234 case AUD_FIDS: /* array of Fids */
235 vaFids = va_arg(vaList, struct AFSCBFids *);
240 vaFid = vaFids->AFSCBFids_val;
243 fprintf(out, "FIDS %u FID %u:%u:%u ", vaFids->AFSCBFids_len, vaFid->Volume,
244 vaFid->Vnode, vaFid->Unique);
245 for ( i = 1; i < vaFids->AFSCBFids_len; i++, vaFid++ )
246 fprintf(out, "FID %u:%u:%u ", vaFid->Volume,
247 vaFid->Vnode, vaFid->Unique);
249 fprintf(out, "FIDS 0 FID 0:0:0 ");
254 fprintf(out, "--badval-- ");
257 vaEntry = va_arg(vaList, int);
263 #ifdef AFS_PTHREAD_ENV
264 static pthread_mutex_t audit_lock;
265 static volatile afs_int32 audit_lock_initialized = 0;
266 static pthread_once_t audit_lock_once = PTHREAD_ONCE_INIT;
269 osi_audit_init_lock(void)
271 pthread_mutex_init(&audit_lock, NULL);
272 audit_lock_initialized = 1;
279 #ifdef AFS_PTHREAD_ENV
280 if (!audit_lock_initialized) {
281 pthread_once(&audit_lock_once, osi_audit_init_lock);
283 #endif /* AFS_PTHREAD_ENV */
286 /* ************************************************************************** */
287 /* The routine that acually does the audit call.
288 * ************************************************************************** */
290 osi_audit_internal(char *audEvent, /* Event name (15 chars or less) */
291 afs_int32 errCode, /* The error code */
299 static char BUFFER[32768];
304 #ifdef AFS_PTHREAD_ENV
305 /* i'm pretty sure all the server apps now call osi_audit_init(),
306 * but to be extra careful we'll leave this assert in here for a
307 * while to make sure */
308 assert(audit_lock_initialized);
309 #endif /* AFS_PTHREAD_ENV */
311 if ((osi_audit_all < 0) || (osi_echo_trail < 0))
313 if (!osi_audit_all && !auditout)
316 va_copy(vaCopy, vaList);
322 case KANOAUTH: /* kautils.h */
323 case RXKADNOAUTH: /* rxkad.h */
324 result = AUDIT_FAIL_AUTH;
326 case EPERM: /* errno.h */
327 case EACCES: /* errno.h */
328 case PRPERM: /* pterror.h */
329 result = AUDIT_FAIL_ACCESS;
331 case VL_PERM: /* vlserver.h */
332 case BUDB_NOTPERMITTED: /* budb_errs.h */
333 case BZACCESS: /* bnode.h */
334 case VOLSERBAD_ACCESS: /* volser.h */
335 result = AUDIT_FAIL_PRIV;
342 #ifdef AFS_PTHREAD_ENV
343 pthread_mutex_lock(&audit_lock);
348 /* Put the error code into the buffer list */
349 *(int *)bufferPtr = errCode;
350 bufferPtr += sizeof(errCode);
352 audmakebuf(audEvent, vaList);
355 if (osi_echo_trail) {
356 printbuf(stdout, 0, audEvent, afsName, hostId, errCode, vaList);
361 bufferLen = (int)((afs_int32) bufferPtr - (afs_int32) & BUFFER[0]);
362 code = auditlog(audEvent, result, BUFFER, bufferLen);
365 printbuf(auditout, 0, audEvent, afsName, hostId, errCode, vaList);
369 #ifdef AFS_PTHREAD_ENV
370 pthread_mutex_unlock(&audit_lock);
376 osi_audit(char *audEvent, /* Event name (15 chars or less) */
377 afs_int32 errCode, /* The error code */
382 if ((osi_audit_all < 0) || (osi_echo_trail < 0))
384 if (!osi_audit_all && !auditout)
387 va_start(vaList, errCode);
388 osi_audit_internal(audEvent, errCode, NULL, 0, vaList);
394 /* ************************************************************************** */
395 /* Given a RPC call structure, this routine extracts the name and host id from the
396 * call and includes it within the audit information.
397 * ************************************************************************** */
399 osi_auditU(struct rx_call *call, char *audEvent, int errCode, ...)
401 struct rx_connection *conn;
402 struct rx_peer *peer;
405 char afsName[MAXKTCNAMELEN];
409 if (osi_audit_all < 0)
411 if (!osi_audit_all && !auditout)
414 strcpy(afsName, "--Unknown--");
418 conn = rx_ConnectionOf(call); /* call -> conn) */
420 secClass = rx_SecurityClassOf(conn); /* conn -> securityIndex */
421 if (secClass == 0) { /* unauthenticated */
422 osi_audit("AFS_Aud_Unauth", (-1), AUD_STR, audEvent, AUD_END);
423 strcpy(afsName, "--UnAuth--");
424 } else if (secClass == 2) { /* authenticated */
425 char tcell[MAXKTCREALMLEN];
426 char name[MAXKTCNAMELEN];
427 char inst[MAXKTCNAMELEN];
432 rxkad_GetServerInfo(conn, NULL, NULL, name, inst, tcell,
435 osi_audit("AFS_Aud_NoAFSId", (-1), AUD_STR, audEvent, AUD_END);
436 strcpy(afsName, "--NoName--");
438 strncpy(vname, name, sizeof(vname));
439 if ((ilen = strlen(inst))) {
440 if (strlen(vname) + 1 + ilen >= sizeof(vname))
445 if ((clen = strlen(tcell))) {
446 #if defined(AFS_ATHENA_STDENV) || defined(AFS_KERBREALM_ENV)
447 static char local_realms[AFS_NUM_LREALMS][AFS_REALM_SZ];
448 static int num_lrealms = -1;
451 if (num_lrealms == -1) {
452 for (i=0; i<AFS_NUM_LREALMS; i++) {
453 if (afs_krb_get_lrealm(local_realms[i], i) != 0 /*KSUCCESS*/)
458 strncpy(local_realms[0], "UNKNOWN.LOCAL.REALM", AFS_REALM_SZ);
462 /* Check to see if the ticket cell matches one of the local realms */
464 for ( i=0;i<num_lrealms;i++ ) {
465 if (!strcasecmp(local_realms[i], tcell)) {
470 /* If yes, then make sure that the name is not present in
471 * an exclusion list */
475 snprintf(uname,sizeof(uname),"%s.%s@%s",name,inst,tcell);
477 snprintf(uname,sizeof(uname),"%s@%s",name,tcell);
479 if (afs_krb_exclusion(uname))
484 if (strlen(vname) + 1 + clen >= sizeof(vname))
487 strcat(vname, tcell);
491 strcpy(afsName, vname);
493 } else { /* Unauthenticated & unknown */
494 osi_audit("AFS_Aud_UnknSec", (-1), AUD_STR, audEvent, AUD_END);
495 strcpy(afsName, "--Unknown--");
498 peer = rx_PeerOf(conn); /* conn -> peer */
500 hostId = rx_HostOf(peer); /* peer -> host */
502 osi_audit("AFS_Aud_NoHost", (-1), AUD_STR, audEvent, AUD_END);
503 } else { /* null conn */
504 osi_audit("AFS_Aud_NoConn", (-1), AUD_STR, audEvent, AUD_END);
506 } else { /* null call */
507 osi_audit("AFS_Aud_NoCall", (-1), AUD_STR, audEvent, AUD_END);
509 va_start(vaList, errCode);
510 osi_audit_internal(audEvent, errCode, afsName, hostId, vaList);
515 /* ************************************************************************** */
516 /* Determines whether auditing is on or off by looking at the Audit file.
517 * If the string AFS_AUDIT_AllEvents is defined in the file, then auditing will be
519 * ************************************************************************** */
528 osi_audit_all = 1; /* say we made check (>= 0) */
529 /* and assume audit all events (for now) */
530 onoff = 0; /* assume we will turn auditing off */
531 osi_echo_trail = 0; /* assume no echoing */
533 fds = fopen(AFSDIR_SERVER_AUDIT_FILEPATH, "r");
535 while (fscanf(fds, "%256s", event) > 0) {
536 if (strcmp(event, "AFS_AUDIT_AllEvents") == 0)
539 if (strcmp(event, "Echo_Trail") == 0)
545 /* Audit this event all of the time */
547 osi_audit("AFS_Aud_On", 0, AUD_END);
549 osi_audit("AFS_Aud_Off", 0, AUD_END);
551 /* Now set whether we audit all events from here on out */
552 osi_audit_all = onoff;
558 osi_audit_file(FILE *out)