Change __signed to signed.
[dragonfly.git] / crypto / kerberosIV / lib / krb / logging.c
1 /*
2  * Copyright (c) 1995, 1996, 1997 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  * 
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 
17  * 3. Neither the name of the Institute nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  * 
21  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 #include "krb_locl.h"
35 #include <klog.h>
36
37 RCSID("$Id: logging.c,v 1.18.2.1 2000/10/13 15:57:34 assar Exp $");
38
39 struct krb_log_facility {
40     char filename[MaxPathLen]; 
41     FILE *file; 
42     krb_log_func_t func;
43 };
44
45 int
46 krb_vlogger(struct krb_log_facility *f, const char *format, va_list args)
47 {
48     FILE *file = NULL;
49     int ret;
50
51     if (f->file != NULL)
52         file = f->file;
53     else if (f->filename && f->filename[0])
54         file = fopen(f->filename, "a");
55
56     if (file == NULL)
57       return KFAILURE;
58
59     ret = f->func(file, format, args);
60
61     if (file != f->file)
62         fclose(file);
63     return ret;
64 }
65
66 int
67 krb_logger(struct krb_log_facility *f, const char *format, ...)
68 {
69     va_list args;
70     int ret;
71     va_start(args, format);
72     ret = krb_vlogger(f, format, args);
73     va_end(args);
74     return ret;
75 }
76
77 /*
78  * If FILE * is given log to it, otherwise, log to filename. When
79  * given a file name the file is opened and closed for each log
80  * record.
81  */
82 int
83 krb_openlog(struct krb_log_facility *f,
84             char *filename,
85             FILE *file,
86             krb_log_func_t func)
87 {
88     strlcpy(f->filename, filename, MaxPathLen);
89     f->file = file;
90     f->func = func;
91     return KSUCCESS;
92 }
93
94 /* ------------------------------------------------------------
95    Compatibility functions from warning.c
96    ------------------------------------------------------------ */
97
98 static int
99 log_tty(FILE *f, const char *format,  va_list args)
100 {
101     if (f != NULL && isatty(fileno(f)))
102         vfprintf(f, format, args);
103     return KSUCCESS;
104 }
105
106 /* stderr */
107 static struct krb_log_facility std_log = { "/dev/tty", NULL, log_tty };
108
109 static void
110 init_std_log (void)
111 {
112   static int done = 0;
113
114   if (!done) {
115     std_log.file = stderr;
116     done = 1;
117   }
118 }
119
120 /*
121  *
122  */
123 void
124 krb_set_warnfn (krb_warnfn_t newfunc)
125 {
126     init_std_log ();
127     std_log.func =  newfunc;
128 }
129
130 /*
131  *
132  */
133 krb_warnfn_t
134 krb_get_warnfn (void)
135 {
136     init_std_log ();
137     return std_log.func;
138 }
139
140 /*
141  * Log warnings to stderr if it's a tty.
142  */
143 void
144 krb_warning (const char *format, ...)
145 {
146     va_list args;
147     
148     init_std_log ();
149     va_start(args, format);
150     krb_vlogger(&std_log, format, args);
151     va_end(args);
152 }
153
154 /* ------------------------------------------------------------
155    Compatibility functions from klog.c and log.c
156    ------------------------------------------------------------ */
157
158 /*
159  * Used by kerberos and kadmind daemons and in libkrb (rd_req.c).
160  *
161  * By default they log to the kerberos server log-file (KRBLOG) to be
162  * backwards compatible.
163  */
164
165 static int
166 log_with_timestamp_and_nl(FILE *file, const char *format, va_list args)
167 {
168     time_t now;
169     if(file == NULL)
170         return KFAILURE;
171     time(&now);
172     fputs(krb_stime(&now), file);
173     fputs(": ", file);
174     vfprintf(file, format, args);
175     fputs("\n", file);
176     fflush(file);
177     return KSUCCESS;
178 }
179
180 static struct krb_log_facility
181 file_log = { KRBLOG, NULL, log_with_timestamp_and_nl };
182
183 /*
184  * kset_logfile() changes the name of the file to which
185  * messages are logged.  If kset_logfile() is not called,
186  * the logfile defaults to KRBLOG, defined in "krb.h".
187  */
188
189 void
190 kset_logfile(char *filename)
191 {
192     krb_openlog(&file_log, filename, NULL, log_with_timestamp_and_nl);
193 }
194
195 /*
196  * krb_log() and klog() is used to add entries to the logfile.
197  *
198  * The log entry consists of a timestamp and the given arguments
199  * printed according to the given "format" string.
200  *
201  * The log file is opened and closed for each log entry.
202  *
203  * If the given log type "type" is unknown, or if the log file
204  * cannot be opened, no entry is made to the log file.
205  *
206  * CHANGE: the type is always ignored
207  *
208  * The return value of klog() is always a pointer to the formatted log
209  * text string "logtxt".
210  */
211
212 /* Used in kerberos.c only. */
213 char *
214 klog(int type, const char *format, ...)
215 {
216     static char logtxt[1024];
217
218     va_list ap;
219
220     va_start(ap, format);
221     vsnprintf(logtxt, sizeof(logtxt), format, ap);
222     va_end(ap);
223     
224     krb_logger(&file_log, "%s", logtxt);
225     
226     return logtxt;
227 }
228
229 /* Used in kadmind and rd_req.c */
230 void
231 krb_log(const char *format, ...)
232 {
233     va_list args;
234
235     va_start(args, format);
236     krb_vlogger(&file_log, format, args);
237     va_end(args);
238 }