]>
Commit | Line | Data |
---|---|---|
8efc0c15 | 1 | /* |
6ae2364d | 2 | * |
5260325f | 3 | * log-server.c |
6ae2364d | 4 | * |
5260325f | 5 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
6ae2364d | 6 | * |
5260325f | 7 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
8 | * All rights reserved | |
6ae2364d | 9 | * |
5260325f | 10 | * Created: Mon Mar 20 21:19:30 1995 ylo |
6ae2364d | 11 | * |
5260325f | 12 | * Server-side versions of debug(), log(), etc. These normally send the output |
13 | * to the system log. | |
6ae2364d | 14 | * |
5260325f | 15 | */ |
8efc0c15 | 16 | |
17 | #include "includes.h" | |
18 | RCSID("$Id$"); | |
19 | ||
20 | #include <syslog.h> | |
21 | #include "packet.h" | |
22 | #include "xmalloc.h" | |
23 | #include "ssh.h" | |
24 | ||
f601d847 | 25 | #ifdef HAVE___PROGNAME |
26 | extern char *__progname; | |
27 | #else /* HAVE___PROGNAME */ | |
d3083fbd | 28 | static const char *__progname = "sshd"; |
f601d847 | 29 | #endif /* HAVE___PROGNAME */ |
30 | ||
6a17f9c2 | 31 | static LogLevel log_level = SYSLOG_LEVEL_INFO; |
8efc0c15 | 32 | static int log_on_stderr = 0; |
e7c0f9d5 | 33 | static int log_facility = LOG_AUTH; |
8efc0c15 | 34 | |
35 | /* Initialize the log. | |
5260325f | 36 | * av0 program name (should be argv[0]) |
37 | * on_stderr print also on stderr | |
38 | * level logging level | |
39 | */ | |
8efc0c15 | 40 | |
6ae2364d | 41 | void |
5260325f | 42 | log_init(char *av0, LogLevel level, SyslogFacility facility, int on_stderr) |
8efc0c15 | 43 | { |
5260325f | 44 | switch (level) { |
45 | case SYSLOG_LEVEL_QUIET: | |
46 | case SYSLOG_LEVEL_ERROR: | |
47 | case SYSLOG_LEVEL_FATAL: | |
48 | case SYSLOG_LEVEL_INFO: | |
49 | case SYSLOG_LEVEL_VERBOSE: | |
50 | case SYSLOG_LEVEL_DEBUG: | |
51 | log_level = level; | |
52 | break; | |
53 | default: | |
54 | fprintf(stderr, "Unrecognized internal syslog level code %d\n", | |
55 | (int) level); | |
56 | exit(1); | |
57 | } | |
58 | switch (facility) { | |
59 | case SYSLOG_FACILITY_DAEMON: | |
60 | log_facility = LOG_DAEMON; | |
61 | break; | |
62 | case SYSLOG_FACILITY_USER: | |
63 | log_facility = LOG_USER; | |
64 | break; | |
65 | case SYSLOG_FACILITY_AUTH: | |
66 | log_facility = LOG_AUTH; | |
67 | break; | |
68 | case SYSLOG_FACILITY_LOCAL0: | |
69 | log_facility = LOG_LOCAL0; | |
70 | break; | |
71 | case SYSLOG_FACILITY_LOCAL1: | |
72 | log_facility = LOG_LOCAL1; | |
73 | break; | |
74 | case SYSLOG_FACILITY_LOCAL2: | |
75 | log_facility = LOG_LOCAL2; | |
76 | break; | |
77 | case SYSLOG_FACILITY_LOCAL3: | |
78 | log_facility = LOG_LOCAL3; | |
79 | break; | |
80 | case SYSLOG_FACILITY_LOCAL4: | |
81 | log_facility = LOG_LOCAL4; | |
82 | break; | |
83 | case SYSLOG_FACILITY_LOCAL5: | |
84 | log_facility = LOG_LOCAL5; | |
85 | break; | |
86 | case SYSLOG_FACILITY_LOCAL6: | |
87 | log_facility = LOG_LOCAL6; | |
88 | break; | |
89 | case SYSLOG_FACILITY_LOCAL7: | |
90 | log_facility = LOG_LOCAL7; | |
91 | break; | |
92 | default: | |
93 | fprintf(stderr, "Unrecognized internal syslog facility code %d\n", | |
94 | (int) facility); | |
95 | exit(1); | |
96 | } | |
97 | log_on_stderr = on_stderr; | |
8efc0c15 | 98 | } |
99 | ||
c8d54615 | 100 | #define MSGBUFSIZ 1024 |
8efc0c15 | 101 | |
6a17f9c2 | 102 | void |
103 | do_log(LogLevel level, const char *fmt, va_list args) | |
8efc0c15 | 104 | { |
c8d54615 | 105 | char msgbuf[MSGBUFSIZ]; |
106 | char fmtbuf[MSGBUFSIZ]; | |
5260325f | 107 | char *txt = NULL; |
108 | int pri = LOG_INFO; | |
109 | ||
110 | if (level > log_level) | |
111 | return; | |
112 | switch (level) { | |
113 | case SYSLOG_LEVEL_ERROR: | |
114 | txt = "error"; | |
115 | pri = LOG_ERR; | |
116 | break; | |
117 | case SYSLOG_LEVEL_FATAL: | |
118 | txt = "fatal"; | |
119 | pri = LOG_ERR; | |
120 | break; | |
121 | case SYSLOG_LEVEL_INFO: | |
122 | case SYSLOG_LEVEL_VERBOSE: | |
123 | pri = LOG_INFO; | |
124 | break; | |
125 | case SYSLOG_LEVEL_DEBUG: | |
126 | txt = "debug"; | |
127 | pri = LOG_DEBUG; | |
128 | break; | |
129 | default: | |
130 | txt = "internal error"; | |
131 | pri = LOG_ERR; | |
132 | break; | |
133 | } | |
134 | if (txt != NULL) { | |
135 | snprintf(fmtbuf, sizeof(fmtbuf), "%s: %s", txt, fmt); | |
136 | vsnprintf(msgbuf, sizeof(msgbuf), fmtbuf, args); | |
137 | } else { | |
138 | vsnprintf(msgbuf, sizeof(msgbuf), fmt, args); | |
139 | } | |
7368a6c8 | 140 | if (log_on_stderr) { |
5260325f | 141 | fprintf(stderr, "%s\n", msgbuf); |
7368a6c8 | 142 | } else { |
143 | openlog(__progname, LOG_PID, log_facility); | |
144 | syslog(pri, "%.500s", msgbuf); | |
145 | closelog(); | |
146 | } | |
8efc0c15 | 147 | } |