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