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 */
26 #include "afs/afsint.h"
30 #include "audit-api.h"
33 #include <afs/afsutil.h>
35 extern struct osi_audit_ops audit_file_ops;
37 extern struct osi_audit_ops audit_sysvmq_ops;
42 int (*islocal)(void *rock, char *name, char *inst, char *cell);
43 } audit_user_check = { NULL, NULL };
47 const struct osi_audit_ops *ops;
48 } audit_interfaces[] = {
50 { "file", &audit_file_ops },
52 { "sysvmq", &audit_sysvmq_ops },
56 #define N_INTERFACES (sizeof(audit_interfaces) / sizeof(audit_interfaces[0]))
58 /* default to `file' audit interface */
59 static const struct osi_audit_ops *audit_ops = &audit_file_ops;
61 static int osi_audit_all = (-1); /* Not determined yet */
62 static int osi_echo_trail = (-1);
64 static int auditout_open = 0;
66 static int osi_audit_check(void);
69 static char *bufferPtr;
73 audmakebuf(char *audEvent, va_list vaList)
82 vaEntry = va_arg(vaList, int);
83 while (vaEntry != AUD_END) {
85 case AUD_STR: /* String */
86 case AUD_NAME: /* Name */
87 case AUD_ACL: /* ACL */
88 vaStr = (char *)va_arg(vaList, char *);
90 strcpy(bufferPtr, vaStr);
91 bufferPtr += strlen(vaStr) + 1;
93 strcpy(bufferPtr, "");
97 case AUD_INT: /* Integer */
98 case AUD_ID: /* ViceId */
99 vaInt = va_arg(vaList, int);
100 *(int *)bufferPtr = vaInt;
101 bufferPtr += sizeof(vaInt);
103 case AUD_DATE: /* Date */
104 case AUD_HOST: /* Host ID */
105 case AUD_LONG: /* long */
106 vaLong = va_arg(vaList, afs_int32);
107 *(afs_int32 *) bufferPtr = vaLong;
108 bufferPtr += sizeof(vaLong);
110 case AUD_FID: /* AFSFid - contains 3 entries */
111 vaFid = (struct AFSFid *)va_arg(vaList, struct AFSFid *);
113 memcpy(bufferPtr, vaFid, sizeof(struct AFSFid));
115 memset(bufferPtr, 0, sizeof(struct AFSFid));
117 bufferPtr += sizeof(struct AFSFid);
120 /* Whole array of fids-- don't know how to handle variable length audit
121 * data with AIX audit package, so for now we just store the first fid.
122 * Better one than none. */
125 struct AFSCBFids *Fids;
127 Fids = (struct AFSCBFids *)va_arg(vaList, struct AFSCBFids *);
128 if (Fids && Fids->AFSCBFids_len) {
129 *((u_int *) bufferPtr) = Fids->AFSCBFids_len;
130 bufferPtr += sizeof(u_int);
131 memcpy(bufferPtr, Fids->AFSCBFids_val,
132 sizeof(struct AFSFid));
134 *((u_int *) bufferPtr) = 0;
135 bufferPtr += sizeof(u_int);
136 memset(bufferPtr, 0, sizeof(struct AFSFid));
138 bufferPtr += sizeof(struct AFSFid);
144 auditlog("AFS_Aud_EINVAL", (-1), audEvent,
145 (strlen(audEvent) + 1));
151 vaEntry = va_arg(vaList, int);
157 printbuf(int rec, char *audEvent, char *afsName, afs_int32 hostId,
158 afs_int32 errCode, va_list vaList)
164 struct AFSFid *vaFid;
165 struct AFSCBFids *vaFids;
166 int num = LogThreadNum();
167 struct in_addr hostAddr;
172 /* Don't print the timestamp or thread id if we recursed */
174 currenttime = time(0);
175 if (strftime(tbuffer, sizeof(tbuffer), "%a %b %d %H:%M:%S %Y ",
176 localtime_r(¤ttime, &tm)) !=0)
177 audit_ops->append_msg(tbuffer);
180 audit_ops->append_msg("[%d] ", num);
183 audit_ops->append_msg("EVENT %s CODE %d ", audEvent, errCode);
186 hostAddr.s_addr = hostId;
187 audit_ops->append_msg("NAME %s HOST %s ", afsName, inet_ntoa(hostAddr));
190 vaEntry = va_arg(vaList, int);
191 while (vaEntry != AUD_END) {
193 case AUD_STR: /* String */
194 vaStr = (char *)va_arg(vaList, char *);
196 audit_ops->append_msg("STR %s ", vaStr);
198 audit_ops->append_msg("STR <null>");
200 case AUD_NAME: /* Name */
201 vaStr = (char *)va_arg(vaList, char *);
203 audit_ops->append_msg("NAME %s ", vaStr);
205 audit_ops->append_msg("NAME <null>");
207 case AUD_ACL: /* ACL */
208 vaStr = (char *)va_arg(vaList, char *);
210 audit_ops->append_msg("ACL %s ", vaStr);
212 audit_ops->append_msg("ACL <null>");
214 case AUD_INT: /* Integer */
215 vaInt = va_arg(vaList, int);
216 audit_ops->append_msg("INT %d ", vaInt);
218 case AUD_ID: /* ViceId */
219 vaInt = va_arg(vaList, int);
220 audit_ops->append_msg("ID %d ", vaInt);
222 case AUD_DATE: /* Date */
223 vaLong = va_arg(vaList, afs_int32);
224 audit_ops->append_msg("DATE %u ", vaLong);
226 case AUD_HOST: /* Host ID */
227 vaLong = va_arg(vaList, afs_int32);
228 hostAddr.s_addr = vaLong;
229 audit_ops->append_msg("HOST %s ", inet_ntoa(hostAddr));
231 case AUD_LONG: /* afs_int32 */
232 vaLong = va_arg(vaList, afs_int32);
233 audit_ops->append_msg("LONG %d ", vaLong);
235 case AUD_FID: /* AFSFid - contains 3 entries */
236 vaFid = va_arg(vaList, struct AFSFid *);
238 audit_ops->append_msg("FID %u:%u:%u ", vaFid->Volume, vaFid->Vnode,
241 audit_ops->append_msg("FID %u:%u:%u ", 0, 0, 0);
243 case AUD_FIDS: /* array of Fids */
244 vaFids = va_arg(vaList, struct AFSCBFids *);
249 vaFid = vaFids->AFSCBFids_val;
252 audit_ops->append_msg("FIDS %u FID %u:%u:%u ", vaFids->AFSCBFids_len, vaFid->Volume,
253 vaFid->Vnode, vaFid->Unique);
254 for ( i = 1; i < vaFids->AFSCBFids_len; i++, vaFid++ )
255 audit_ops->append_msg("FID %u:%u:%u ", vaFid->Volume,
256 vaFid->Vnode, vaFid->Unique);
258 audit_ops->append_msg("FIDS 0 FID 0:0:0 ");
263 audit_ops->append_msg("--badval-- ");
266 vaEntry = va_arg(vaList, int);
269 audit_ops->send_msg();
272 #ifdef AFS_PTHREAD_ENV
273 static pthread_mutex_t audit_lock;
274 static volatile afs_int32 audit_lock_initialized = 0;
275 static pthread_once_t audit_lock_once = PTHREAD_ONCE_INIT;
278 osi_audit_init_lock(void)
280 MUTEX_INIT(&audit_lock, "audit", MUTEX_DEFAULT, 0);
281 audit_lock_initialized = 1;
288 #ifdef AFS_PTHREAD_ENV
289 if (!audit_lock_initialized) {
290 pthread_once(&audit_lock_once, osi_audit_init_lock);
292 #endif /* AFS_PTHREAD_ENV */
295 /* ************************************************************************** */
296 /* The routine that acually does the audit call.
297 * ************************************************************************** */
299 osi_audit_internal(char *audEvent, /* Event name (15 chars or less) */
300 afs_int32 errCode, /* The error code */
308 static char BUFFER[32768];
312 #ifdef AFS_PTHREAD_ENV
313 /* i'm pretty sure all the server apps now call osi_audit_init(),
314 * but to be extra careful we'll leave this assert in here for a
315 * while to make sure */
316 opr_Assert(audit_lock_initialized);
317 #endif /* AFS_PTHREAD_ENV */
319 if ((osi_audit_all < 0) || (osi_echo_trail < 0))
321 if (!osi_audit_all && !auditout_open)
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;
350 MUTEX_ENTER(&audit_lock);
354 /* Put the error code into the buffer list */
355 *(int *)bufferPtr = errCode;
356 bufferPtr += sizeof(errCode);
358 audmakebuf(audEvent, vaList);
362 bufferLen = (int)((afs_int32) bufferPtr - (afs_int32) & BUFFER[0]);
363 code = auditlog(audEvent, result, BUFFER, bufferLen);
366 printbuf(0, audEvent, afsName, hostId, errCode, vaList);
369 MUTEX_EXIT(&audit_lock);
374 osi_audit(char *audEvent, /* Event name (15 chars or less) */
375 afs_int32 errCode, /* The error code */
380 if ((osi_audit_all < 0) || (osi_echo_trail < 0))
382 if (!osi_audit_all && !auditout_open)
385 va_start(vaList, errCode);
386 osi_audit_internal(audEvent, errCode, NULL, 0, vaList);
392 /* ************************************************************************** */
393 /* Given a RPC call structure, this routine extracts the name and host id from the
394 * call and includes it within the audit information.
395 * ************************************************************************** */
397 osi_auditU(struct rx_call *call, char *audEvent, int errCode, ...)
399 struct rx_connection *conn;
400 struct rx_peer *peer;
403 char afsName[MAXKTCNAMELEN + MAXKTCNAMELEN + MAXKTCREALMLEN + 3];
407 if (osi_audit_all < 0)
409 if (!osi_audit_all && !auditout_open)
412 strcpy(afsName, "--Unknown--");
416 conn = rx_ConnectionOf(call); /* call -> conn) */
418 secClass = rx_SecurityClassOf(conn); /* conn -> securityIndex */
419 if (secClass == RX_SECIDX_NULL) { /* unauthenticated */
420 osi_audit("AFS_Aud_Unauth", (-1), AUD_STR, audEvent, AUD_END);
421 strcpy(afsName, "--UnAuth--");
422 } else if (secClass == RX_SECIDX_KAD) { /* authenticated */
423 char tcell[MAXKTCREALMLEN];
424 char name[MAXKTCNAMELEN];
425 char inst[MAXKTCNAMELEN];
428 rxkad_GetServerInfo(conn, NULL, NULL, name, inst, tcell,
431 osi_audit("AFS_Aud_NoAFSId", (-1), AUD_STR, audEvent, AUD_END);
432 strcpy(afsName, "--NoName--");
434 afs_int32 islocal = 0;
435 if (audit_user_check.islocal) {
437 audit_user_check.islocal(audit_user_check.rock,
440 strlcpy(afsName, name, sizeof(afsName));
442 strlcat(afsName, ".", sizeof(afsName));
443 strlcat(afsName, inst, sizeof(afsName));
445 if (tcell[0] && !islocal) {
446 strlcat(afsName, "@", sizeof(afsName));
447 strlcat(afsName, tcell, sizeof(afsName));
450 } else { /* Unauthenticated & unknown */
451 osi_audit("AFS_Aud_UnknSec", (-1), AUD_STR, audEvent, AUD_END);
452 strcpy(afsName, "--Unknown--");
454 peer = rx_PeerOf(conn); /* conn -> peer */
456 hostId = rx_HostOf(peer); /* peer -> host */
458 osi_audit("AFS_Aud_NoHost", (-1), AUD_STR, audEvent, AUD_END);
459 } else { /* null conn */
460 osi_audit("AFS_Aud_NoConn", (-1), AUD_STR, audEvent, AUD_END);
462 } else { /* null call */
463 osi_audit("AFS_Aud_NoCall", (-1), AUD_STR, audEvent, AUD_END);
465 va_start(vaList, errCode);
466 osi_audit_internal(audEvent, errCode, afsName, hostId, vaList);
471 /* ************************************************************************** */
472 /* Determines whether auditing is on or off by looking at the Audit file.
473 * If the string AFS_AUDIT_AllEvents is defined in the file, then auditing will be
475 * ************************************************************************** */
478 osi_audit_check(void)
484 osi_audit_all = 1; /* say we made check (>= 0) */
485 /* and assume audit all events (for now) */
486 onoff = 0; /* assume we will turn auditing off */
487 osi_echo_trail = 0; /* assume no echoing */
489 fds = fopen(AFSDIR_SERVER_AUDIT_FILEPATH, "r");
491 while (fscanf(fds, "%256s", event) > 0) {
492 if (strcmp(event, "AFS_AUDIT_AllEvents") == 0)
495 if (strcmp(event, "Echo_Trail") == 0)
501 /* Audit this event all of the time */
503 osi_audit("AFS_Aud_On", 0, AUD_END);
505 osi_audit("AFS_Aud_Off", 0, AUD_END);
507 /* Now set whether we audit all events from here on out */
508 osi_audit_all = onoff;
514 osi_audit_file(const char *fileName)
516 if(!audit_ops->open_file(fileName)) {
524 osi_audit_interface(const char *interface)
527 for (i = 0; i < N_INTERFACES; ++i) {
528 if (strcmp(interface, audit_interfaces[i].name) == 0) {
529 audit_ops = audit_interfaces[i].ops;
538 osi_audit_set_user_check(void *rock,
539 int (*islocal) (void *rock, char *name, char *inst,
542 audit_user_check.rock = rock;
543 audit_user_check.islocal = islocal;
547 audit_PrintStats(FILE *out)
549 audit_ops->print_interface_stats(out);