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