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