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>
42 int osi_audit_all = (-1); /* Not determined yet */
43 int osi_echo_trail = (-1);
45 FILE *auditout = NULL;
47 int osi_audit_check();
50 audmakebuf(char *audEvent, va_list vaList)
62 vaEntry = va_arg(vaList, int);
63 while (vaEntry != AUD_END) {
65 case AUD_STR: /* String */
66 case AUD_NAME: /* Name */
67 case AUD_ACL: /* ACL */
68 vaStr = (char *)va_arg(vaList, char *);
70 strcpy(bufferPtr, vaStr);
71 bufferPtr += strlen(vaStr) + 1;
73 strcpy(bufferPtr, "");
77 case AUD_INT: /* Integer */
78 case AUD_ID: /* ViceId */
79 vaInt = va_arg(vaList, int);
80 *(int *)bufferPtr = vaInt;
81 bufferPtr += sizeof(vaInt);
83 case AUD_DATE: /* Date */
84 case AUD_HOST: /* Host ID */
85 case AUD_LONG: /* long */
86 vaLong = va_arg(vaList, afs_int32);
87 *(afs_int32 *) bufferPtr = vaLong;
88 bufferPtr += sizeof(vaLong);
90 case AUD_LST: /* Ptr to another list */
91 vaLst = va_arg(vaList, va_list);
92 audmakebuf(audEvent, vaLst);
94 case AUD_FID: /* AFSFid - contains 3 entries */
95 vaFid = (struct AFSFid *)va_arg(vaList, struct AFSFid *);
97 memcpy(bufferPtr, vaFid, sizeof(struct AFSFid));
99 memset(bufferPtr, 0, sizeof(struct AFSFid));
101 bufferPtr += sizeof(struct AFSFid);
104 /* Whole array of fids-- don't know how to handle variable length audit
105 * data with AIX audit package, so for now we just store the first fid.
106 * Better one than none. */
109 struct AFSCBFids *Fids;
111 Fids = (struct AFSCBFids *)va_arg(vaList, struct AFSCBFids *);
112 if (Fids && Fids->AFSCBFids_len) {
113 *((u_int *) bufferPtr) = Fids->AFSCBFids_len;
114 bufferPtr += sizeof(u_int);
115 memcpy(bufferPtr, Fids->AFSCBFids_val,
116 sizeof(struct AFSFid));
118 *((u_int *) bufferPtr) = 0;
119 bufferPtr += sizeof(u_int);
120 memset(bufferPtr, 0, sizeof(struct AFSFid));
122 bufferPtr += sizeof(struct AFSFid);
128 auditlog("AFS_Aud_EINVAL", (-1), audEvent,
129 (strlen(audEvent) + 1));
135 vaEntry = va_arg(vaList, int);
140 printbuf(FILE *out, int rec, char *audEvent, afs_int32 errCode, va_list vaList)
147 struct AFSFid *vaFid;
148 struct AFSCBFids *vaFids;
149 int num = LogThreadNum();
150 struct in_addr hostAddr;
155 /* Don't print the timestamp or thread id if we recursed */
157 currenttime = time(0);
158 timeStamp = afs_ctime(¤ttime, tbuffer,
160 timeStamp[24] = ' '; /* ts[24] is the newline, 25 is the null */
161 fprintf(out, timeStamp);
164 fprintf(out, "[%d] ", num);
167 if (strcmp(audEvent, "VALST") != 0)
168 fprintf(out, "EVENT %s CODE %d ", audEvent, errCode);
170 vaEntry = va_arg(vaList, int);
171 while (vaEntry != AUD_END) {
173 case AUD_STR: /* String */
174 vaStr = (char *)va_arg(vaList, char *);
176 fprintf(out, "STR %s ", vaStr);
178 fprintf(out, "STR <null>");
180 case AUD_NAME: /* Name */
181 vaStr = (char *)va_arg(vaList, char *);
183 fprintf(out, "NAME %s ", vaStr);
185 fprintf(out, "NAME <null>");
187 case AUD_ACL: /* ACL */
188 vaStr = (char *)va_arg(vaList, char *);
190 fprintf(out, "ACL %s ", vaStr);
192 fprintf(out, "ACL <null>");
194 case AUD_INT: /* Integer */
195 vaInt = va_arg(vaList, int);
196 fprintf(out, "INT %d ", vaInt);
198 case AUD_ID: /* ViceId */
199 vaInt = va_arg(vaList, int);
200 fprintf(out, "ID %d ", vaInt);
202 case AUD_DATE: /* Date */
203 vaLong = va_arg(vaList, afs_int32);
204 fprintf(out, "DATE %u ", vaLong);
206 case AUD_HOST: /* Host ID */
207 vaLong = va_arg(vaList, afs_int32);
208 hostAddr.s_addr = vaLong;
209 fprintf(out, "HOST %s ", inet_ntoa(hostAddr));
211 case AUD_LONG: /* afs_int32 */
212 vaLong = va_arg(vaList, afs_int32);
213 fprintf(out, "LONG %d ", vaLong);
215 case AUD_LST: /* Ptr to another list */
216 vaLst = va_arg(vaList, va_list);
217 printbuf(out, 1, "VALST", 0, vaLst);
219 case AUD_FID: /* AFSFid - contains 3 entries */
220 vaFid = va_arg(vaList, struct AFSFid *);
222 fprintf(out, "FID %u:%u:%u ", vaFid->Volume, vaFid->Vnode,
225 fprintf(out, "FID %u:%u:%u ", 0, 0, 0);
227 case AUD_FIDS: /* array of Fids */
228 vaFids = va_arg(vaList, struct AFSCBFids *);
234 fprintf(out, "FIDS %u FID %u:%u:%u ", vaFids->AFSCBFids_len, vaFid->Volume,
235 vaFid->Vnode, vaFid->Unique);
237 fprintf(out, "FIDS 0 FID 0:0:0 ");
239 for ( i = 1; i < vaFids->AFSCBFids_len; i++ ) {
240 vaFid = vaFids->AFSCBFids_val;
242 fprintf(out, "FID %u:%u:%u ", vaFid->Volume,
243 vaFid->Vnode, vaFid->Unique);
245 fprintf(out, "FID 0:0:0 ");
250 fprintf(out, "--badval-- ");
253 vaEntry = va_arg(vaList, int);
256 if (strcmp(audEvent, "VALST") != 0)
260 #ifdef AFS_PTHREAD_ENV
261 static pthread_mutex_t audit_lock;
262 static volatile afs_int32 audit_lock_initialized = 0;
263 static pthread_once_t audit_lock_once = PTHREAD_ONCE_INIT;
266 osi_audit_init_lock(void)
268 pthread_mutex_init(&audit_lock, NULL);
269 audit_lock_initialized = 1;
276 #ifdef AFS_PTHREAD_ENV
277 if (!audit_lock_initialized) {
278 pthread_once(&audit_lock_once, osi_audit_init_lock);
280 #endif /* AFS_PTHREAD_ENV */
283 /* ************************************************************************** */
284 /* The routine that acually does the audit call.
285 * ************************************************************************** */
287 osi_audit(char *audEvent, /* Event name (15 chars or less) */
288 afs_int32 errCode, /* The error code */
294 static char BUFFER[32768];
299 #ifdef AFS_PTHREAD_ENV
300 /* i'm pretty sure all the server apps now call osi_audit_init(),
301 * but to be extra careful we'll leave this assert in here for a
302 * while to make sure */
303 assert(audit_lock_initialized);
304 #endif /* AFS_PTHREAD_ENV */
306 if ((osi_audit_all < 0) || (osi_echo_trail < 0))
308 if (!osi_audit_all && !auditout)
315 case KANOAUTH: /* kautils.h */
316 case RXKADNOAUTH: /* rxkad.h */
317 result = AUDIT_FAIL_AUTH;
319 case EPERM: /* errno.h */
320 case EACCES: /* errno.h */
321 case PRPERM: /* pterror.h */
322 result = AUDIT_FAIL_ACCESS;
324 case VL_PERM: /* vlserver.h */
325 case BUDB_NOTPERMITTED: /* budb_errs.h */
326 case BZACCESS: /* bnode.h */
327 case VOLSERBAD_ACCESS: /* volser.h */
328 result = AUDIT_FAIL_PRIV;
335 #ifdef AFS_PTHREAD_ENV
336 pthread_mutex_lock(&audit_lock);
341 /* Put the error code into the buffer list */
342 *(int *)bufferPtr = errCode;
343 bufferPtr += sizeof(errCode);
345 va_start(vaList, errCode);
346 audmakebuf(audEvent, vaList);
349 if (osi_echo_trail) {
350 va_start(vaList, errCode);
351 printbuf(stdout, 0, audEvent, errCode, vaList);
355 bufferLen = (int)((afs_int32) bufferPtr - (afs_int32) & BUFFER[0]);
356 code = auditlog(audEvent, result, BUFFER, bufferLen);
360 code = auditlog("AFS_Aud_Fail", result, &err, sizeof(err));
362 printf("Error while writing audit entry: %d.\n", errno);
367 va_start(vaList, errCode);
368 printbuf(auditout, 0, audEvent, errCode, vaList);
372 #ifdef AFS_PTHREAD_ENV
373 pthread_mutex_unlock(&audit_lock);
379 /* ************************************************************************** */
380 /* Given a RPC call structure, this routine extracts the name and host id from the
381 * call and includes it within the audit information.
382 * ************************************************************************** */
384 osi_auditU(struct rx_call *call, char *audEvent, int errCode, ...)
386 struct rx_connection *conn;
387 struct rx_peer *peer;
390 char afsName[MAXKTCNAMELEN];
394 if (osi_audit_all < 0)
396 if (!osi_audit_all && !auditout)
399 strcpy(afsName, "--Unknown--");
403 conn = rx_ConnectionOf(call); /* call -> conn) */
405 secClass = rx_SecurityClassOf(conn); /* conn -> securityIndex */
406 if (secClass == 0) { /* unauthenticated */
407 osi_audit("AFS_Aud_Unauth", (-1), AUD_STR, audEvent, AUD_END);
408 strcpy(afsName, "--UnAuth--");
409 } else if (secClass == 2) { /* authenticated */
410 char tcell[MAXKTCREALMLEN];
411 char name[MAXKTCNAMELEN];
412 char inst[MAXKTCNAMELEN];
417 rxkad_GetServerInfo(conn, NULL, NULL, name, inst, tcell,
420 osi_audit("AFS_Aud_NoAFSId", (-1), AUD_STR, audEvent, AUD_END);
421 strcpy(afsName, "--NoName--");
423 strncpy(vname, name, sizeof(vname));
424 if ((ilen = strlen(inst))) {
425 if (strlen(vname) + 1 + ilen >= sizeof(vname))
430 if ((clen = strlen(tcell))) {
431 #if defined(AFS_ATHENA_STDENV) || defined(AFS_KERBREALM_ENV)
432 static char local_realms[AFS_NUM_LREALMS][AFS_REALM_SZ];
433 static int num_lrealms = -1;
436 if (num_lrealms == -1) {
437 for (i=0; i<AFS_NUM_LREALMS; i++) {
438 if (afs_krb_get_lrealm(local_realms[i], i) != 0 /*KSUCCESS*/)
443 strncpy(local_realms[0], "UNKNOWN.LOCAL.REALM", AFS_REALM_SZ);
447 /* Check to see if the ticket cell matches one of the local realms */
449 for ( i=0;i<num_lrealms;i++ ) {
450 if (!strcasecmp(local_realms[i], tcell)) {
455 /* If yes, then make sure that the name is not present in
456 * an exclusion list */
460 snprintf(uname,sizeof(uname),"%s.%s@%s",name,inst,tcell);
462 snprintf(uname,sizeof(uname),"%s@%s",name,tcell);
464 if (afs_krb_exclusion(uname))
469 if (strlen(vname) + 1 + clen >= sizeof(vname))
472 strcat(vname, tcell);
476 strcpy(afsName, vname);
478 } else { /* Unauthenticated & unknown */
479 osi_audit("AFS_Aud_UnknSec", (-1), AUD_STR, audEvent, AUD_END);
480 strcpy(afsName, "--Unknown--");
483 peer = rx_PeerOf(conn); /* conn -> peer */
485 hostId = rx_HostOf(peer); /* peer -> host */
487 osi_audit("AFS_Aud_NoHost", (-1), AUD_STR, audEvent, AUD_END);
488 } else { /* null conn */
489 osi_audit("AFS_Aud_NoConn", (-1), AUD_STR, audEvent, AUD_END);
491 } else { /* null call */
492 osi_audit("AFS_Aud_NoCall", (-1), AUD_STR, audEvent, AUD_END);
494 va_start(vaList, errCode);
495 osi_audit(audEvent, errCode, AUD_NAME, afsName, AUD_HOST, hostId,
496 AUD_LST, vaList, AUD_END);
501 /* ************************************************************************** */
502 /* Determines whether auditing is on or off by looking at the Audit file.
503 * If the string AFS_AUDIT_AllEvents is defined in the file, then auditing will be
505 * ************************************************************************** */
514 osi_audit_all = 1; /* say we made check (>= 0) */
515 /* and assume audit all events (for now) */
516 onoff = 0; /* assume we will turn auditing off */
517 osi_echo_trail = 0; /* assume no echoing */
519 fds = fopen(AFSDIR_SERVER_AUDIT_FILEPATH, "r");
521 while (fscanf(fds, "%256s", event) > 0) {
522 if (strcmp(event, "AFS_AUDIT_AllEvents") == 0)
525 if (strcmp(event, "Echo_Trail") == 0)
531 /* Audit this event all of the time */
533 osi_audit("AFS_Aud_On", 0, AUD_END);
535 osi_audit("AFS_Aud_Off", 0, AUD_END);
537 /* Now set whether we audit all events from here on out */
538 osi_audit_all = onoff;
544 osi_audit_file(FILE *out)