89d3ae814d03714e6e7b0231e4c4bd992a49c9ef
[openafs.git] / src / kauth / kalog.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
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
8  */
9
10 /*
11  * ALL RIGHTS RESERVED
12  */
13
14 /*
15  * Routines to log kaserver activity
16  *
17  */
18
19 #include <afs/param.h>
20 #include <afsconfig.h>
21
22 RCSID("$Header$");
23
24 #include <stdio.h>
25 #include <afs/afsutil.h>
26 #ifndef AFS_NT40_ENV
27 #include <strings.h>
28 #endif
29 #include <fcntl.h>
30 #include <sys/types.h>
31 #include <time.h>
32 #include <signal.h>
33 #include <assert.h>
34 #include <afs/afsutil.h>
35 #include "kauth.h"
36 #include "kalog.h"
37
38 extern afs_int32 verbose_track; 
39
40 #ifdef AUTH_DBM_LOG
41
42 DBM *kalog_db;
43
44 kalog_Init()
45 {
46     OpenLog(AFSDIR_SERVER_KALOGDB_FILEPATH);      /* set up logging */
47     SetupLogSignals();
48     kalog_db = dbm_open(AFSDIR_SERVER_KALOG_FILEPATH, O_WRONLY | O_CREAT, KALOG_DB_MODE);
49     if (!kalog_db)
50         ViceLog(0, ("Cannot open dbm database - no DB logging possible\n"));
51 }
52
53 /* log a ticket usage */
54 kalog_log(principal, instance, sprincipal, sinstance, realm, hostaddr, type)
55     char *principal, *instance, *sprincipal, *sinstance, *realm;
56     int hostaddr, type;
57 {
58     char keybuf[512]; /* not random! 63 . 63 , 63 . 63 max key */
59     datum key, data;
60     kalog_elt rdata;
61
62     if (!kalog_db) return;
63     if (*principal)
64          strcpy(keybuf, principal);
65     if (realm) {
66         strcat(keybuf, "@");
67         strcat(keybuf, realm);
68     }
69     if (*instance) {
70         strcat(keybuf, ".");
71         strcat(keybuf, instance);
72     }
73
74     /* unlike the name/instance, the services can come down as NULL */
75     if (sprincipal && *sprincipal) {
76         strcat(keybuf, ",");
77         strcat(keybuf, sprincipal);
78         if (sinstance && *sinstance) {
79             strcat(keybuf, ".");
80             strcat(keybuf, sinstance);
81         }
82     } 
83     switch(type) {
84       case LOG_CRUSER:
85         strcat(keybuf, ":cruser");
86         break;
87       case LOG_CHPASSWD:
88         strcat(keybuf, ":chp");
89         break;
90       case LOG_AUTHENTICATE:
91         strcat(keybuf, ":auth");
92         break;
93       case LOG_AUTHFAILED:
94         strcat(keybuf, ":authnot");
95         break;
96       case LOG_SETFIELDS:
97         strcat(keybuf, ":setf");
98         break;
99       case LOG_DELUSER:
100         strcat(keybuf, ":delu");
101         break;
102       case LOG_UNLOCK:
103         strcat(keybuf, ":unlok");
104         break;
105       case LOG_GETTICKET:
106         strcat(keybuf, ":gtck");
107         break;
108       default:
109         break;
110     }
111     
112     key.dptr = keybuf;
113     key.dsize = strlen(keybuf) + 1; /* store the key in a string w/ null */
114     rdata.last_use = time((time_t *) 0);
115     rdata.host = hostaddr;
116     data.dptr = (char *) &rdata;
117     data.dsize = sizeof(kalog_elt);
118
119     dbm_store(kalog_db, key, data, DBM_REPLACE);
120
121     ViceLog(verbose_track, ("%s from %x\n", keybuf, hostaddr));
122 }
123
124
125 #endif /* AUTH_DBM_LOG */
126
127
128 /* log a ticket usage to the text log */
129 void ka_log(char *principal, char *instance, char *sprincipal, char *sinstance, 
130        char *realm, int hostaddr, int type)
131 {
132     char logbuf[512]; /* not random! 63 . 63 , 63 . 63 max key */
133
134     if (*principal)
135          strcpy(logbuf, principal);
136     if (realm) {
137         strcat(logbuf, "@");
138         strcat(logbuf, realm);
139     }
140     if (*instance) {
141         strcat(logbuf, ".");
142         strcat(logbuf, instance);
143     }
144
145     /* unlike the name/instance, the services can come down as NULL */
146     if (sprincipal && *sprincipal) {
147         strcat(logbuf, ",");
148         strcat(logbuf, sprincipal);
149         if (sinstance && *sinstance) {
150             strcat(logbuf, ".");
151             strcat(logbuf, sinstance);
152         }
153     } 
154     switch(type) {
155       case LOG_CRUSER:
156         strcat(logbuf, ":cruser");
157         break;
158       case LOG_CHPASSWD:
159         strcat(logbuf, ":chp");
160         break;
161       case LOG_AUTHENTICATE:
162         strcat(logbuf, ":auth");
163         break;
164       case LOG_AUTHFAILED:
165         strcat(logbuf, ":authnot");
166         break;
167       case LOG_SETFIELDS:
168         strcat(logbuf, ":setf");
169         break;
170       case LOG_DELUSER:
171         strcat(logbuf, ":delu");
172         break;
173       case LOG_UNLOCK:
174         strcat(logbuf, ":unlok");
175         break;
176       case LOG_GETTICKET:
177         strcat(logbuf, ":gtck");
178         break;
179       default:
180         break;
181     }
182     
183     ViceLog(verbose_track, ("%s from %x\n", logbuf, hostaddr));
184 }