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>
16 #include <sys/audit.h>
20 #define AUDIT_FAIL_AUTH 2
21 #define AUDIT_FAIL_ACCESS 3
22 #define AUDIT_FAIL_PRIV 4
23 #endif /* AFS_AIX32_ENV */
25 #include "afs/afsint.h"
29 #include "audit-api.h"
32 #include <afs/afsutil.h>
34 /* C99 requires va_copy. Older versions of GCC provide __va_copy. Per t
35 Autoconf manual, memcpy is a generally portable fallback. */
38 # define va_copy(d, s) __va_copy((d), (s))
40 # define va_copy(d, s) memcpy(&(d), &(s), sizeof(va_list))
44 extern struct osi_audit_ops audit_file_ops;
46 extern struct osi_audit_ops audit_sysvmq_ops;
51 const struct osi_audit_ops *ops;
52 } audit_interfaces[] = {
54 { "file", &audit_file_ops },
56 { "sysvmq", &audit_sysvmq_ops },
60 #define N_INTERFACES (sizeof(audit_interfaces) / sizeof(audit_interfaces[0]))
62 /* default to `file' audit interface */
63 static const struct osi_audit_ops *audit_ops = &audit_file_ops;
65 static int osi_audit_all = (-1); /* Not determined yet */
66 static int osi_echo_trail = (-1);
68 static int auditout_open = 0;
70 static int osi_audit_check(void);
73 static char *bufferPtr;
77 audmakebuf(char *audEvent, va_list vaList)
86 vaEntry = va_arg(vaList, int);
87 while (vaEntry != AUD_END) {
89 case AUD_STR: /* String */
90 case AUD_NAME: /* Name */
91 case AUD_ACL: /* ACL */
92 vaStr = (char *)va_arg(vaList, char *);
94 strcpy(bufferPtr, vaStr);
95 bufferPtr += strlen(vaStr) + 1;
97 strcpy(bufferPtr, "");
101 case AUD_INT: /* Integer */
102 case AUD_ID: /* ViceId */
103 vaInt = va_arg(vaList, int);
104 *(int *)bufferPtr = vaInt;
105 bufferPtr += sizeof(vaInt);
107 case AUD_DATE: /* Date */
108 case AUD_HOST: /* Host ID */
109 case AUD_LONG: /* long */
110 vaLong = va_arg(vaList, afs_int32);
111 *(afs_int32 *) bufferPtr = vaLong;
112 bufferPtr += sizeof(vaLong);
114 case AUD_FID: /* AFSFid - contains 3 entries */
115 vaFid = (struct AFSFid *)va_arg(vaList, struct AFSFid *);
117 memcpy(bufferPtr, vaFid, sizeof(struct AFSFid));
119 memset(bufferPtr, 0, sizeof(struct AFSFid));
121 bufferPtr += sizeof(struct AFSFid);
124 /* Whole array of fids-- don't know how to handle variable length audit
125 * data with AIX audit package, so for now we just store the first fid.
126 * Better one than none. */
129 struct AFSCBFids *Fids;
131 Fids = (struct AFSCBFids *)va_arg(vaList, struct AFSCBFids *);
132 if (Fids && Fids->AFSCBFids_len) {
133 *((u_int *) bufferPtr) = Fids->AFSCBFids_len;
134 bufferPtr += sizeof(u_int);
135 memcpy(bufferPtr, Fids->AFSCBFids_val,
136 sizeof(struct AFSFid));
138 *((u_int *) bufferPtr) = 0;
139 bufferPtr += sizeof(u_int);
140 memset(bufferPtr, 0, sizeof(struct AFSFid));
142 bufferPtr += sizeof(struct AFSFid);
148 auditlog("AFS_Aud_EINVAL", (-1), audEvent,
149 (strlen(audEvent) + 1));
155 vaEntry = va_arg(vaList, int);
161 printbuf(int rec, char *audEvent, char *afsName, afs_int32 hostId,
162 afs_int32 errCode, va_list vaList)
168 struct AFSFid *vaFid;
169 struct AFSCBFids *vaFids;
170 int num = LogThreadNum();
171 struct in_addr hostAddr;
176 /* Don't print the timestamp or thread id if we recursed */
178 currenttime = time(0);
179 if (strftime(tbuffer, sizeof(tbuffer), "%a %b %d %T %Y ",
180 localtime_r(¤ttime, &tm)) !=0)
181 audit_ops->append_msg(tbuffer);
184 audit_ops->append_msg("[%d] ", num);
187 audit_ops->append_msg("EVENT %s CODE %d ", audEvent, errCode);
190 hostAddr.s_addr = hostId;
191 audit_ops->append_msg("NAME %s HOST %s ", afsName, inet_ntoa(hostAddr));
194 vaEntry = va_arg(vaList, int);
195 while (vaEntry != AUD_END) {
197 case AUD_STR: /* String */
198 vaStr = (char *)va_arg(vaList, char *);
200 audit_ops->append_msg("STR %s ", vaStr);
202 audit_ops->append_msg("STR <null>");
204 case AUD_NAME: /* Name */
205 vaStr = (char *)va_arg(vaList, char *);
207 audit_ops->append_msg("NAME %s ", vaStr);
209 audit_ops->append_msg("NAME <null>");
211 case AUD_ACL: /* ACL */
212 vaStr = (char *)va_arg(vaList, char *);
214 audit_ops->append_msg("ACL %s ", vaStr);
216 audit_ops->append_msg("ACL <null>");
218 case AUD_INT: /* Integer */
219 vaInt = va_arg(vaList, int);
220 audit_ops->append_msg("INT %d ", vaInt);
222 case AUD_ID: /* ViceId */
223 vaInt = va_arg(vaList, int);
224 audit_ops->append_msg("ID %d ", vaInt);
226 case AUD_DATE: /* Date */
227 vaLong = va_arg(vaList, afs_int32);
228 audit_ops->append_msg("DATE %u ", vaLong);
230 case AUD_HOST: /* Host ID */
231 vaLong = va_arg(vaList, afs_int32);
232 hostAddr.s_addr = vaLong;
233 audit_ops->append_msg("HOST %s ", inet_ntoa(hostAddr));
235 case AUD_LONG: /* afs_int32 */
236 vaLong = va_arg(vaList, afs_int32);
237 audit_ops->append_msg("LONG %d ", vaLong);
239 case AUD_FID: /* AFSFid - contains 3 entries */
240 vaFid = va_arg(vaList, struct AFSFid *);
242 audit_ops->append_msg("FID %u:%u:%u ", vaFid->Volume, vaFid->Vnode,
245 audit_ops->append_msg("FID %u:%u:%u ", 0, 0, 0);
247 case AUD_FIDS: /* array of Fids */
248 vaFids = va_arg(vaList, struct AFSCBFids *);
253 vaFid = vaFids->AFSCBFids_val;
256 audit_ops->append_msg("FIDS %u FID %u:%u:%u ", vaFids->AFSCBFids_len, vaFid->Volume,
257 vaFid->Vnode, vaFid->Unique);
258 for ( i = 1; i < vaFids->AFSCBFids_len; i++, vaFid++ )
259 audit_ops->append_msg("FID %u:%u:%u ", vaFid->Volume,
260 vaFid->Vnode, vaFid->Unique);
262 audit_ops->append_msg("FIDS 0 FID 0:0:0 ");
267 audit_ops->append_msg("--badval-- ");
270 vaEntry = va_arg(vaList, int);
273 audit_ops->send_msg();
276 #ifdef AFS_PTHREAD_ENV
277 static pthread_mutex_t audit_lock;
278 static volatile afs_int32 audit_lock_initialized = 0;
279 static pthread_once_t audit_lock_once = PTHREAD_ONCE_INIT;
282 osi_audit_init_lock(void)
284 MUTEX_INIT(&audit_lock, "audit", MUTEX_DEFAULT, 0);
285 audit_lock_initialized = 1;
292 #ifdef AFS_PTHREAD_ENV
293 if (!audit_lock_initialized) {
294 pthread_once(&audit_lock_once, osi_audit_init_lock);
296 #endif /* AFS_PTHREAD_ENV */
299 /* ************************************************************************** */
300 /* The routine that acually does the audit call.
301 * ************************************************************************** */
303 osi_audit_internal(char *audEvent, /* Event name (15 chars or less) */
304 afs_int32 errCode, /* The error code */
312 static char BUFFER[32768];
316 #ifdef AFS_PTHREAD_ENV
317 /* i'm pretty sure all the server apps now call osi_audit_init(),
318 * but to be extra careful we'll leave this assert in here for a
319 * while to make sure */
320 osi_Assert(audit_lock_initialized);
321 #endif /* AFS_PTHREAD_ENV */
323 if ((osi_audit_all < 0) || (osi_echo_trail < 0))
325 if (!osi_audit_all && !auditout_open)
333 case KANOAUTH: /* kautils.h */
334 case RXKADNOAUTH: /* rxkad.h */
335 result = AUDIT_FAIL_AUTH;
337 case EPERM: /* errno.h */
338 case EACCES: /* errno.h */
339 case PRPERM: /* pterror.h */
340 result = AUDIT_FAIL_ACCESS;
342 case VL_PERM: /* vlserver.h */
343 case BUDB_NOTPERMITTED: /* budb_errs.h */
344 case BZACCESS: /* bnode.h */
345 case VOLSERBAD_ACCESS: /* volser.h */
346 result = AUDIT_FAIL_PRIV;
354 MUTEX_ENTER(&audit_lock);
358 /* Put the error code into the buffer list */
359 *(int *)bufferPtr = errCode;
360 bufferPtr += sizeof(errCode);
362 audmakebuf(audEvent, vaList);
366 bufferLen = (int)((afs_int32) bufferPtr - (afs_int32) & BUFFER[0]);
367 code = auditlog(audEvent, result, BUFFER, bufferLen);
370 printbuf(0, audEvent, afsName, hostId, errCode, vaList);
373 MUTEX_EXIT(&audit_lock);
378 osi_audit(char *audEvent, /* Event name (15 chars or less) */
379 afs_int32 errCode, /* The error code */
384 if ((osi_audit_all < 0) || (osi_echo_trail < 0))
386 if (!osi_audit_all && !auditout_open)
389 va_start(vaList, errCode);
390 osi_audit_internal(audEvent, errCode, NULL, 0, vaList);
396 /* ************************************************************************** */
397 /* Given a RPC call structure, this routine extracts the name and host id from the
398 * call and includes it within the audit information.
399 * ************************************************************************** */
401 osi_auditU(struct rx_call *call, char *audEvent, int errCode, ...)
403 struct rx_connection *conn;
404 struct rx_peer *peer;
407 char afsName[MAXKTCNAMELEN];
411 if (osi_audit_all < 0)
413 if (!osi_audit_all && !auditout_open)
416 strcpy(afsName, "--Unknown--");
420 conn = rx_ConnectionOf(call); /* call -> conn) */
422 secClass = rx_SecurityClassOf(conn); /* conn -> securityIndex */
423 if (secClass == 0) { /* unauthenticated */
424 osi_audit("AFS_Aud_Unauth", (-1), AUD_STR, audEvent, AUD_END);
425 strcpy(afsName, "--UnAuth--");
426 } else if (secClass == 2) { /* authenticated */
427 char tcell[MAXKTCREALMLEN];
428 char name[MAXKTCNAMELEN];
429 char inst[MAXKTCNAMELEN];
434 rxkad_GetServerInfo(conn, NULL, NULL, name, inst, tcell,
437 osi_audit("AFS_Aud_NoAFSId", (-1), AUD_STR, audEvent, AUD_END);
438 strcpy(afsName, "--NoName--");
440 strncpy(vname, name, sizeof(vname));
441 if ((ilen = strlen(inst))) {
442 if (strlen(vname) + 1 + ilen >= sizeof(vname))
447 if ((clen = strlen(tcell))) {
448 #if defined(AFS_ATHENA_STDENV) || defined(AFS_KERBREALM_ENV)
449 static char local_realms[AFS_NUM_LREALMS][AFS_REALM_SZ];
450 static int num_lrealms = -1;
453 if (num_lrealms == -1) {
454 for (i = 0; i < AFS_NUM_LREALMS; i++) {
455 if (afs_krb_get_lrealm(local_realms[i], i) != 0 /*KSUCCESS*/)
460 strncpy(local_realms[0], "UNKNOWN.LOCAL.REALM", AFS_REALM_SZ);
464 /* Check to see if the ticket cell matches one of the local realms */
466 for (i = 0; i < num_lrealms ; i++ ) {
467 if (!strcasecmp(local_realms[i], tcell)) {
472 /* If yes, then make sure that the name is not present in
473 * an exclusion list */
477 snprintf(uname,sizeof(uname),"%s.%s@%s",name,inst,tcell);
479 snprintf(uname,sizeof(uname),"%s@%s",name,tcell);
481 if (afs_krb_exclusion(uname))
486 if (strlen(vname) + 1 + clen >= sizeof(vname))
489 strcat(vname, tcell);
493 strcpy(afsName, vname);
495 } else { /* Unauthenticated & unknown */
496 osi_audit("AFS_Aud_UnknSec", (-1), AUD_STR, audEvent, AUD_END);
497 strcpy(afsName, "--Unknown--");
500 peer = rx_PeerOf(conn); /* conn -> peer */
502 hostId = rx_HostOf(peer); /* peer -> host */
504 osi_audit("AFS_Aud_NoHost", (-1), AUD_STR, audEvent, AUD_END);
505 } else { /* null conn */
506 osi_audit("AFS_Aud_NoConn", (-1), AUD_STR, audEvent, AUD_END);
508 } else { /* null call */
509 osi_audit("AFS_Aud_NoCall", (-1), AUD_STR, audEvent, AUD_END);
511 va_start(vaList, errCode);
512 osi_audit_internal(audEvent, errCode, afsName, hostId, vaList);
517 /* ************************************************************************** */
518 /* Determines whether auditing is on or off by looking at the Audit file.
519 * If the string AFS_AUDIT_AllEvents is defined in the file, then auditing will be
521 * ************************************************************************** */
524 osi_audit_check(void)
530 osi_audit_all = 1; /* say we made check (>= 0) */
531 /* and assume audit all events (for now) */
532 onoff = 0; /* assume we will turn auditing off */
533 osi_echo_trail = 0; /* assume no echoing */
535 fds = fopen(AFSDIR_SERVER_AUDIT_FILEPATH, "r");
537 while (fscanf(fds, "%256s", event) > 0) {
538 if (strcmp(event, "AFS_AUDIT_AllEvents") == 0)
541 if (strcmp(event, "Echo_Trail") == 0)
547 /* Audit this event all of the time */
549 osi_audit("AFS_Aud_On", 0, AUD_END);
551 osi_audit("AFS_Aud_Off", 0, AUD_END);
553 /* Now set whether we audit all events from here on out */
554 osi_audit_all = onoff;
560 osi_audit_file(const char *fileName)
562 if(!audit_ops->open_file(fileName)) {
570 osi_audit_interface(const char *interface)
573 for (i = 0; i < N_INTERFACES; ++i) {
574 if (strcmp(interface, audit_interfaces[i].name) == 0) {
575 audit_ops = audit_interfaces[i].ops;
584 audit_PrintStats(FILE *out)
586 audit_ops->print_interface_stats(out);