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, int);
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 = (char *)va_arg(vaList, int);
92 audmakebuf(audEvent, vaLst);
94 case AUD_FID: /* AFSFid - contains 3 entries */
95 vaFid = (struct AFSFid *)va_arg(vaList, int);
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, int);
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;
152 /* Don't print the thread id if we recursed */
153 if ((num > -1) && (rec == 0))
154 fprintf(out, "[%d]:", num);
156 if (strcmp(audEvent, "VALST") != 0)
157 fprintf(out, "EVENT %s CODE %d ", audEvent, errCode);
159 vaEntry = va_arg(vaList, int);
160 while (vaEntry != AUD_END) {
162 case AUD_STR: /* String */
163 vaStr = (char *)va_arg(vaList, int);
165 fprintf(out, "STR %s ", vaStr);
167 fprintf(out, "STR <null>");
169 case AUD_NAME: /* Name */
170 vaStr = (char *)va_arg(vaList, int);
172 fprintf(out, "NAME %s ", vaStr);
174 fprintf(out, "NAME <null>");
176 case AUD_ACL: /* ACL */
177 vaStr = (char *)va_arg(vaList, int);
179 fprintf(out, "ACL %s ", vaStr);
181 fprintf(out, "ACL <null>");
183 case AUD_INT: /* Integer */
184 vaInt = va_arg(vaList, int);
185 fprintf(out, "INT %d ", vaInt);
187 case AUD_ID: /* ViceId */
188 vaInt = va_arg(vaList, int);
189 fprintf(out, "ID %d ", vaInt);
191 case AUD_DATE: /* Date */
192 vaLong = va_arg(vaList, afs_int32);
193 fprintf(out, "DATE %u ", vaLong);
195 case AUD_HOST: /* Host ID */
196 vaLong = va_arg(vaList, afs_int32);
197 hostAddr.s_addr = vaLong;
198 fprintf(out, "HOST %s ", inet_ntoa(hostAddr));
200 case AUD_LONG: /* afs_int32 */
201 vaLong = va_arg(vaList, afs_int32);
202 fprintf(out, "LONG %d ", vaLong);
204 case AUD_LST: /* Ptr to another list */
205 vaLst = (char *)va_arg(vaList, int);
206 printbuf(out, 1, "VALST", 0, vaLst);
208 case AUD_FID: /* AFSFid - contains 3 entries */
209 vaFid = (struct AFSFid *)va_arg(vaList, int);
211 fprintf(out, "FID %u:%u:%u ", vaFid->Volume, vaFid->Vnode,
214 fprintf(out, "FID %u:%u:%u ", 0, 0, 0);
216 case AUD_FIDS: /* array of Fids */
217 vaFids = (struct AFSCBFids *)va_arg(vaList, int);
223 fprintf(out, "FIDS %u FID %u:%u:%u ", vaFids->AFSCBFids_len, vaFid->Volume,
224 vaFid->Vnode, vaFid->Unique);
226 fprintf(out, "FIDS 0 FID 0:0:0 ");
228 for ( i = 1; i < vaFids->AFSCBFids_len; i++ ) {
229 vaFid = vaFids->AFSCBFids_val;
231 fprintf(out, "FID %u:%u:%u ", vaFid->Volume,
232 vaFid->Vnode, vaFid->Unique);
234 fprintf(out, "FID 0:0:0 ");
239 fprintf(out, "--badval-- ");
242 vaEntry = va_arg(vaList, int);
245 if (strcmp(audEvent, "VALST") != 0)
249 /* ************************************************************************** */
250 /* The routine that acually does the audit call.
251 * ************************************************************************** */
253 osi_audit(char *audEvent, /* Event name (15 chars or less) */
254 afs_int32 errCode, /* The error code */
265 static struct Lock audbuflock = { 0, 0, 0, 0,
266 #ifdef AFS_PTHREAD_ENV
267 PTHREAD_MUTEX_INITIALIZER,
268 PTHREAD_COND_INITIALIZER,
269 PTHREAD_COND_INITIALIZER
270 #endif /* AFS_PTHREAD_ENV */
272 static char BUFFER[32768];
275 if ((osi_audit_all < 0) || (osi_echo_trail < 0))
277 if (!osi_audit_all && !auditout)
284 case KANOAUTH: /* kautils.h */
285 case RXKADNOAUTH: /* rxkad.h */
286 result = AUDIT_FAIL_AUTH;
288 case EPERM: /* errno.h */
289 case EACCES: /* errno.h */
290 case PRPERM: /* pterror.h */
291 result = AUDIT_FAIL_ACCESS;
293 case VL_PERM: /* vlserver.h */
294 case BUDB_NOTPERMITTED: /* budb_errs.h */
295 case BZACCESS: /* bnode.h */
296 case VOLSERBAD_ACCESS: /* volser.h */
297 result = AUDIT_FAIL_PRIV;
305 ObtainWriteLock(&audbuflock);
308 /* Put the error code into the buffer list */
309 *(int *)bufferPtr = errCode;
310 bufferPtr += sizeof(errCode);
312 va_start(vaList, errCode);
313 audmakebuf(audEvent, vaList);
316 if (osi_echo_trail) {
317 va_start(vaList, errCode);
318 printbuf(stdout, 0, audEvent, errCode, vaList);
322 bufferLen = (int)((afs_int32) bufferPtr - (afs_int32) & BUFFER[0]);
323 code = auditlog(audEvent, result, BUFFER, bufferLen);
327 code = auditlog("AFS_Aud_Fail", result, &err, sizeof(err));
329 printf("Error while writing audit entry: %d.\n", errno);
332 ReleaseWriteLock(&audbuflock);
335 va_start(vaList, errCode);
336 printbuf(auditout, 0, audEvent, errCode, vaList);
344 /* ************************************************************************** */
345 /* Given a RPC call structure, this routine extracts the name and host id from the
346 * call and includes it within the audit information.
347 * ************************************************************************** */
349 osi_auditU(struct rx_call *call, char *audEvent, int errCode, ...)
351 struct rx_connection *conn;
352 struct rx_peer *peer;
355 char afsName[MAXKTCNAMELEN];
359 if (osi_audit_all < 0)
361 if (!osi_audit_all && !auditout)
364 strcpy(afsName, "--Unknown--");
368 conn = rx_ConnectionOf(call); /* call -> conn) */
370 secClass = rx_SecurityClassOf(conn); /* conn -> securityIndex */
371 if (secClass == 0) { /* unauthenticated */
372 osi_audit("AFS_Aud_Unauth", (-1), AUD_STR, audEvent, AUD_END);
373 strcpy(afsName, "--UnAuth--");
374 } else if (secClass == 2) { /* authenticated */
375 char tcell[MAXKTCREALMLEN];
376 char name[MAXKTCNAMELEN];
377 char inst[MAXKTCNAMELEN];
382 rxkad_GetServerInfo(conn, NULL, NULL, name, inst, tcell,
385 osi_audit("AFS_Aud_NoAFSId", (-1), AUD_STR, audEvent, AUD_END);
386 strcpy(afsName, "--NoName--");
388 strncpy(vname, name, sizeof(vname));
389 if ((ilen = strlen(inst))) {
390 if (strlen(vname) + 1 + ilen >= sizeof(vname))
395 if ((clen = strlen(tcell))) {
396 #if defined(AFS_ATHENA_STDENV) || defined(AFS_KERBREALM_ENV)
397 static char local_realm[AFS_REALM_SZ] = "";
398 if (!local_realm[0]) {
399 if (afs_krb_get_lrealm(local_realm, 0) != 0 /*KSUCCESS*/)
400 strncpy(local_realm, "UNKNOWN.LOCAL.REALM", AFS_REALM_SZ);
402 if (strcasecmp(local_realm, tcell)) {
403 if (strlen(vname) + 1 + clen >= sizeof(vname))
406 strcat(vname, tcell);
410 strcpy(afsName, vname);
412 } else { /* Unauthenticated & unknown */
413 osi_audit("AFS_Aud_UnknSec", (-1), AUD_STR, audEvent, AUD_END);
414 strcpy(afsName, "--Unknown--");
417 peer = rx_PeerOf(conn); /* conn -> peer */
419 hostId = rx_HostOf(peer); /* peer -> host */
421 osi_audit("AFS_Aud_NoHost", (-1), AUD_STR, audEvent, AUD_END);
422 } else { /* null conn */
423 osi_audit("AFS_Aud_NoConn", (-1), AUD_STR, audEvent, AUD_END);
425 } else { /* null call */
426 osi_audit("AFS_Aud_NoCall", (-1), AUD_STR, audEvent, AUD_END);
428 va_start(vaList, errCode);
429 osi_audit(audEvent, errCode, AUD_NAME, afsName, AUD_HOST, hostId,
430 AUD_LST, vaList, AUD_END);
435 /* ************************************************************************** */
436 /* Determines whether auditing is on or off by looking at the Audit file.
437 * If the string AFS_AUDIT_AllEvents is defined in the file, then auditing will be
439 * ************************************************************************** */
448 osi_audit_all = 1; /* say we made check (>= 0) */
449 /* and assume audit all events (for now) */
450 onoff = 0; /* assume we will turn auditing off */
451 osi_echo_trail = 0; /* assume no echoing */
453 fds = fopen(AFSDIR_SERVER_AUDIT_FILEPATH, "r");
455 while (fscanf(fds, "%256s", event) > 0) {
456 if (strcmp(event, "AFS_AUDIT_AllEvents") == 0)
459 if (strcmp(event, "Echo_Trail") == 0)
465 /* Audit this event all of the time */
467 osi_audit("AFS_Aud_On", 0, AUD_END);
469 osi_audit("AFS_Aud_Off", 0, AUD_END);
471 /* Now set whether we audit all events from here on out */
472 osi_audit_all = onoff;
478 osi_audit_file(FILE *out)