[331] | 1 | /*
|
---|
| 2 | * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
|
---|
| 3 | *
|
---|
| 4 | * Licensed under the OpenSSL license (the "License"). You may not use
|
---|
| 5 | * this file except in compliance with the License. You can obtain a copy
|
---|
| 6 | * in the file LICENSE in the source distribution or at
|
---|
| 7 | * https://www.openssl.org/source/license.html
|
---|
| 8 | */
|
---|
| 9 |
|
---|
| 10 | /*
|
---|
| 11 | * Why BIO_s_log?
|
---|
| 12 | *
|
---|
| 13 | * BIO_s_log is useful for system daemons (or services under NT). It is
|
---|
| 14 | * one-way BIO, it sends all stuff to syslogd (on system that commonly use
|
---|
| 15 | * that), or event log (on NT), or OPCOM (on OpenVMS).
|
---|
| 16 | *
|
---|
| 17 | */
|
---|
| 18 |
|
---|
| 19 | #include <stdio.h>
|
---|
| 20 | #include <errno.h>
|
---|
| 21 |
|
---|
| 22 | #include "bio_lcl.h"
|
---|
| 23 | #include "internal/cryptlib.h"
|
---|
| 24 |
|
---|
| 25 | #if defined(OPENSSL_SYS_WINCE)
|
---|
| 26 | #elif defined(OPENSSL_SYS_WIN32)
|
---|
| 27 | #elif defined(OPENSSL_SYS_VMS)
|
---|
| 28 | # include <opcdef.h>
|
---|
| 29 | # include <descrip.h>
|
---|
| 30 | # include <lib$routines.h>
|
---|
| 31 | # include <starlet.h>
|
---|
| 32 | /* Some compiler options may mask the declaration of "_malloc32". */
|
---|
| 33 | # if __INITIAL_POINTER_SIZE && defined _ANSI_C_SOURCE
|
---|
| 34 | # if __INITIAL_POINTER_SIZE == 64
|
---|
| 35 | # pragma pointer_size save
|
---|
| 36 | # pragma pointer_size 32
|
---|
| 37 | void *_malloc32(__size_t);
|
---|
| 38 | # pragma pointer_size restore
|
---|
| 39 | # endif /* __INITIAL_POINTER_SIZE == 64 */
|
---|
| 40 | # endif /* __INITIAL_POINTER_SIZE && defined
|
---|
| 41 | * _ANSI_C_SOURCE */
|
---|
| 42 | #elif defined(OPENSSL_SYS_NETWARE)
|
---|
| 43 | # define NO_SYSLOG
|
---|
| 44 | #elif (!defined(MSDOS) || defined(WATT32)) && !defined(OPENSSL_SYS_VXWORKS) && !defined(NO_SYSLOG)
|
---|
| 45 | # include <syslog.h>
|
---|
| 46 | #endif
|
---|
| 47 |
|
---|
| 48 | #include <openssl/buffer.h>
|
---|
| 49 | #include <openssl/err.h>
|
---|
| 50 |
|
---|
| 51 | #ifndef NO_SYSLOG
|
---|
| 52 |
|
---|
| 53 | # if defined(OPENSSL_SYS_WIN32)
|
---|
| 54 | # define LOG_EMERG 0
|
---|
| 55 | # define LOG_ALERT 1
|
---|
| 56 | # define LOG_CRIT 2
|
---|
| 57 | # define LOG_ERR 3
|
---|
| 58 | # define LOG_WARNING 4
|
---|
| 59 | # define LOG_NOTICE 5
|
---|
| 60 | # define LOG_INFO 6
|
---|
| 61 | # define LOG_DEBUG 7
|
---|
| 62 |
|
---|
| 63 | # define LOG_DAEMON (3<<3)
|
---|
| 64 | # elif defined(OPENSSL_SYS_VMS)
|
---|
| 65 | /* On VMS, we don't really care about these, but we need them to compile */
|
---|
| 66 | # define LOG_EMERG 0
|
---|
| 67 | # define LOG_ALERT 1
|
---|
| 68 | # define LOG_CRIT 2
|
---|
| 69 | # define LOG_ERR 3
|
---|
| 70 | # define LOG_WARNING 4
|
---|
| 71 | # define LOG_NOTICE 5
|
---|
| 72 | # define LOG_INFO 6
|
---|
| 73 | # define LOG_DEBUG 7
|
---|
| 74 |
|
---|
| 75 | # define LOG_DAEMON OPC$M_NM_NTWORK
|
---|
| 76 | # endif
|
---|
| 77 |
|
---|
| 78 | static int slg_write(BIO *h, const char *buf, int num);
|
---|
| 79 | static int slg_puts(BIO *h, const char *str);
|
---|
| 80 | static long slg_ctrl(BIO *h, int cmd, long arg1, void *arg2);
|
---|
| 81 | static int slg_new(BIO *h);
|
---|
| 82 | static int slg_free(BIO *data);
|
---|
| 83 | static void xopenlog(BIO *bp, char *name, int level);
|
---|
| 84 | static void xsyslog(BIO *bp, int priority, const char *string);
|
---|
| 85 | static void xcloselog(BIO *bp);
|
---|
| 86 |
|
---|
| 87 | static const BIO_METHOD methods_slg = {
|
---|
| 88 | BIO_TYPE_MEM, "syslog",
|
---|
| 89 | slg_write,
|
---|
| 90 | NULL,
|
---|
| 91 | slg_puts,
|
---|
| 92 | NULL,
|
---|
| 93 | slg_ctrl,
|
---|
| 94 | slg_new,
|
---|
| 95 | slg_free,
|
---|
| 96 | NULL,
|
---|
| 97 | };
|
---|
| 98 |
|
---|
| 99 | const BIO_METHOD *BIO_s_log(void)
|
---|
| 100 | {
|
---|
| 101 | return (&methods_slg);
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | static int slg_new(BIO *bi)
|
---|
| 105 | {
|
---|
| 106 | bi->init = 1;
|
---|
| 107 | bi->num = 0;
|
---|
| 108 | bi->ptr = NULL;
|
---|
| 109 | xopenlog(bi, "application", LOG_DAEMON);
|
---|
| 110 | return (1);
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | static int slg_free(BIO *a)
|
---|
| 114 | {
|
---|
| 115 | if (a == NULL)
|
---|
| 116 | return (0);
|
---|
| 117 | xcloselog(a);
|
---|
| 118 | return (1);
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | static int slg_write(BIO *b, const char *in, int inl)
|
---|
| 122 | {
|
---|
| 123 | int ret = inl;
|
---|
| 124 | char *buf;
|
---|
| 125 | char *pp;
|
---|
| 126 | int priority, i;
|
---|
| 127 | static const struct {
|
---|
| 128 | int strl;
|
---|
| 129 | char str[10];
|
---|
| 130 | int log_level;
|
---|
| 131 | } mapping[] = {
|
---|
| 132 | {
|
---|
| 133 | 6, "PANIC ", LOG_EMERG
|
---|
| 134 | },
|
---|
| 135 | {
|
---|
| 136 | 6, "EMERG ", LOG_EMERG
|
---|
| 137 | },
|
---|
| 138 | {
|
---|
| 139 | 4, "EMR ", LOG_EMERG
|
---|
| 140 | },
|
---|
| 141 | {
|
---|
| 142 | 6, "ALERT ", LOG_ALERT
|
---|
| 143 | },
|
---|
| 144 | {
|
---|
| 145 | 4, "ALR ", LOG_ALERT
|
---|
| 146 | },
|
---|
| 147 | {
|
---|
| 148 | 5, "CRIT ", LOG_CRIT
|
---|
| 149 | },
|
---|
| 150 | {
|
---|
| 151 | 4, "CRI ", LOG_CRIT
|
---|
| 152 | },
|
---|
| 153 | {
|
---|
| 154 | 6, "ERROR ", LOG_ERR
|
---|
| 155 | },
|
---|
| 156 | {
|
---|
| 157 | 4, "ERR ", LOG_ERR
|
---|
| 158 | },
|
---|
| 159 | {
|
---|
| 160 | 8, "WARNING ", LOG_WARNING
|
---|
| 161 | },
|
---|
| 162 | {
|
---|
| 163 | 5, "WARN ", LOG_WARNING
|
---|
| 164 | },
|
---|
| 165 | {
|
---|
| 166 | 4, "WAR ", LOG_WARNING
|
---|
| 167 | },
|
---|
| 168 | {
|
---|
| 169 | 7, "NOTICE ", LOG_NOTICE
|
---|
| 170 | },
|
---|
| 171 | {
|
---|
| 172 | 5, "NOTE ", LOG_NOTICE
|
---|
| 173 | },
|
---|
| 174 | {
|
---|
| 175 | 4, "NOT ", LOG_NOTICE
|
---|
| 176 | },
|
---|
| 177 | {
|
---|
| 178 | 5, "INFO ", LOG_INFO
|
---|
| 179 | },
|
---|
| 180 | {
|
---|
| 181 | 4, "INF ", LOG_INFO
|
---|
| 182 | },
|
---|
| 183 | {
|
---|
| 184 | 6, "DEBUG ", LOG_DEBUG
|
---|
| 185 | },
|
---|
| 186 | {
|
---|
| 187 | 4, "DBG ", LOG_DEBUG
|
---|
| 188 | },
|
---|
| 189 | {
|
---|
| 190 | 0, "", LOG_ERR
|
---|
| 191 | }
|
---|
| 192 | /* The default */
|
---|
| 193 | };
|
---|
| 194 |
|
---|
| 195 | if ((buf = OPENSSL_malloc(inl + 1)) == NULL) {
|
---|
| 196 | return (0);
|
---|
| 197 | }
|
---|
| 198 | strncpy(buf, in, inl);
|
---|
| 199 | buf[inl] = '\0';
|
---|
| 200 |
|
---|
| 201 | i = 0;
|
---|
| 202 | while (strncmp(buf, mapping[i].str, mapping[i].strl) != 0)
|
---|
| 203 | i++;
|
---|
| 204 | priority = mapping[i].log_level;
|
---|
| 205 | pp = buf + mapping[i].strl;
|
---|
| 206 |
|
---|
| 207 | xsyslog(b, priority, pp);
|
---|
| 208 |
|
---|
| 209 | OPENSSL_free(buf);
|
---|
| 210 | return (ret);
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 | static long slg_ctrl(BIO *b, int cmd, long num, void *ptr)
|
---|
| 214 | {
|
---|
| 215 | switch (cmd) {
|
---|
| 216 | case BIO_CTRL_SET:
|
---|
| 217 | xcloselog(b);
|
---|
| 218 | xopenlog(b, ptr, num);
|
---|
| 219 | break;
|
---|
| 220 | default:
|
---|
| 221 | break;
|
---|
| 222 | }
|
---|
| 223 | return (0);
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 | static int slg_puts(BIO *bp, const char *str)
|
---|
| 227 | {
|
---|
| 228 | int n, ret;
|
---|
| 229 |
|
---|
| 230 | n = strlen(str);
|
---|
| 231 | ret = slg_write(bp, str, n);
|
---|
| 232 | return (ret);
|
---|
| 233 | }
|
---|
| 234 |
|
---|
| 235 | # if defined(OPENSSL_SYS_WIN32)
|
---|
| 236 |
|
---|
| 237 | static void xopenlog(BIO *bp, char *name, int level)
|
---|
| 238 | {
|
---|
| 239 | if (check_winnt())
|
---|
| 240 | bp->ptr = RegisterEventSourceA(NULL, name);
|
---|
| 241 | else
|
---|
| 242 | bp->ptr = NULL;
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | static void xsyslog(BIO *bp, int priority, const char *string)
|
---|
| 246 | {
|
---|
| 247 | LPCSTR lpszStrings[2];
|
---|
| 248 | WORD evtype = EVENTLOG_ERROR_TYPE;
|
---|
| 249 | char pidbuf[DECIMAL_SIZE(DWORD) + 4];
|
---|
| 250 |
|
---|
| 251 | if (bp->ptr == NULL)
|
---|
| 252 | return;
|
---|
| 253 |
|
---|
| 254 | switch (priority) {
|
---|
| 255 | case LOG_EMERG:
|
---|
| 256 | case LOG_ALERT:
|
---|
| 257 | case LOG_CRIT:
|
---|
| 258 | case LOG_ERR:
|
---|
| 259 | evtype = EVENTLOG_ERROR_TYPE;
|
---|
| 260 | break;
|
---|
| 261 | case LOG_WARNING:
|
---|
| 262 | evtype = EVENTLOG_WARNING_TYPE;
|
---|
| 263 | break;
|
---|
| 264 | case LOG_NOTICE:
|
---|
| 265 | case LOG_INFO:
|
---|
| 266 | case LOG_DEBUG:
|
---|
| 267 | evtype = EVENTLOG_INFORMATION_TYPE;
|
---|
| 268 | break;
|
---|
| 269 | default:
|
---|
| 270 | /*
|
---|
| 271 | * Should never happen, but set it
|
---|
| 272 | * as error anyway.
|
---|
| 273 | */
|
---|
| 274 | evtype = EVENTLOG_ERROR_TYPE;
|
---|
| 275 | break;
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 | sprintf(pidbuf, "[%lu] ", GetCurrentProcessId());
|
---|
| 279 | lpszStrings[0] = pidbuf;
|
---|
| 280 | lpszStrings[1] = string;
|
---|
| 281 |
|
---|
| 282 | ReportEventA(bp->ptr, evtype, 0, 1024, NULL, 2, 0, lpszStrings, NULL);
|
---|
| 283 | }
|
---|
| 284 |
|
---|
| 285 | static void xcloselog(BIO *bp)
|
---|
| 286 | {
|
---|
| 287 | if (bp->ptr)
|
---|
| 288 | DeregisterEventSource((HANDLE) (bp->ptr));
|
---|
| 289 | bp->ptr = NULL;
|
---|
| 290 | }
|
---|
| 291 |
|
---|
| 292 | # elif defined(OPENSSL_SYS_VMS)
|
---|
| 293 |
|
---|
| 294 | static int VMS_OPC_target = LOG_DAEMON;
|
---|
| 295 |
|
---|
| 296 | static void xopenlog(BIO *bp, char *name, int level)
|
---|
| 297 | {
|
---|
| 298 | VMS_OPC_target = level;
|
---|
| 299 | }
|
---|
| 300 |
|
---|
| 301 | static void xsyslog(BIO *bp, int priority, const char *string)
|
---|
| 302 | {
|
---|
| 303 | struct dsc$descriptor_s opc_dsc;
|
---|
| 304 |
|
---|
| 305 | /* Arrange 32-bit pointer to opcdef buffer and malloc(), if needed. */
|
---|
| 306 | # if __INITIAL_POINTER_SIZE == 64
|
---|
| 307 | # pragma pointer_size save
|
---|
| 308 | # pragma pointer_size 32
|
---|
| 309 | # define OPCDEF_TYPE __char_ptr32
|
---|
| 310 | # define OPCDEF_MALLOC _malloc32
|
---|
| 311 | # else /* __INITIAL_POINTER_SIZE == 64 */
|
---|
| 312 | # define OPCDEF_TYPE char *
|
---|
| 313 | # define OPCDEF_MALLOC OPENSSL_malloc
|
---|
| 314 | # endif /* __INITIAL_POINTER_SIZE == 64 [else] */
|
---|
| 315 |
|
---|
| 316 | struct opcdef *opcdef_p;
|
---|
| 317 |
|
---|
| 318 | # if __INITIAL_POINTER_SIZE == 64
|
---|
| 319 | # pragma pointer_size restore
|
---|
| 320 | # endif /* __INITIAL_POINTER_SIZE == 64 */
|
---|
| 321 |
|
---|
| 322 | char buf[10240];
|
---|
| 323 | unsigned int len;
|
---|
| 324 | struct dsc$descriptor_s buf_dsc;
|
---|
| 325 | $DESCRIPTOR(fao_cmd, "!AZ: !AZ");
|
---|
| 326 | char *priority_tag;
|
---|
| 327 |
|
---|
| 328 | switch (priority) {
|
---|
| 329 | case LOG_EMERG:
|
---|
| 330 | priority_tag = "Emergency";
|
---|
| 331 | break;
|
---|
| 332 | case LOG_ALERT:
|
---|
| 333 | priority_tag = "Alert";
|
---|
| 334 | break;
|
---|
| 335 | case LOG_CRIT:
|
---|
| 336 | priority_tag = "Critical";
|
---|
| 337 | break;
|
---|
| 338 | case LOG_ERR:
|
---|
| 339 | priority_tag = "Error";
|
---|
| 340 | break;
|
---|
| 341 | case LOG_WARNING:
|
---|
| 342 | priority_tag = "Warning";
|
---|
| 343 | break;
|
---|
| 344 | case LOG_NOTICE:
|
---|
| 345 | priority_tag = "Notice";
|
---|
| 346 | break;
|
---|
| 347 | case LOG_INFO:
|
---|
| 348 | priority_tag = "Info";
|
---|
| 349 | break;
|
---|
| 350 | case LOG_DEBUG:
|
---|
| 351 | priority_tag = "DEBUG";
|
---|
| 352 | break;
|
---|
| 353 | }
|
---|
| 354 |
|
---|
| 355 | buf_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
|
---|
| 356 | buf_dsc.dsc$b_class = DSC$K_CLASS_S;
|
---|
| 357 | buf_dsc.dsc$a_pointer = buf;
|
---|
| 358 | buf_dsc.dsc$w_length = sizeof(buf) - 1;
|
---|
| 359 |
|
---|
| 360 | lib$sys_fao(&fao_cmd, &len, &buf_dsc, priority_tag, string);
|
---|
| 361 |
|
---|
| 362 | /* We know there's an 8-byte header. That's documented. */
|
---|
| 363 | opcdef_p = OPCDEF_MALLOC(8 + len);
|
---|
| 364 | opcdef_p->opc$b_ms_type = OPC$_RQ_RQST;
|
---|
| 365 | memcpy(opcdef_p->opc$z_ms_target_classes, &VMS_OPC_target, 3);
|
---|
| 366 | opcdef_p->opc$l_ms_rqstid = 0;
|
---|
| 367 | memcpy(&opcdef_p->opc$l_ms_text, buf, len);
|
---|
| 368 |
|
---|
| 369 | opc_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
|
---|
| 370 | opc_dsc.dsc$b_class = DSC$K_CLASS_S;
|
---|
| 371 | opc_dsc.dsc$a_pointer = (OPCDEF_TYPE) opcdef_p;
|
---|
| 372 | opc_dsc.dsc$w_length = len + 8;
|
---|
| 373 |
|
---|
| 374 | sys$sndopr(opc_dsc, 0);
|
---|
| 375 |
|
---|
| 376 | OPENSSL_free(opcdef_p);
|
---|
| 377 | }
|
---|
| 378 |
|
---|
| 379 | static void xcloselog(BIO *bp)
|
---|
| 380 | {
|
---|
| 381 | }
|
---|
| 382 |
|
---|
| 383 | # else /* Unix/Watt32 */
|
---|
| 384 |
|
---|
| 385 | static void xopenlog(BIO *bp, char *name, int level)
|
---|
| 386 | {
|
---|
| 387 | # ifdef WATT32 /* djgpp/DOS */
|
---|
| 388 | openlog(name, LOG_PID | LOG_CONS | LOG_NDELAY, level);
|
---|
| 389 | # else
|
---|
| 390 | openlog(name, LOG_PID | LOG_CONS, level);
|
---|
| 391 | # endif
|
---|
| 392 | }
|
---|
| 393 |
|
---|
| 394 | static void xsyslog(BIO *bp, int priority, const char *string)
|
---|
| 395 | {
|
---|
| 396 | syslog(priority, "%s", string);
|
---|
| 397 | }
|
---|
| 398 |
|
---|
| 399 | static void xcloselog(BIO *bp)
|
---|
| 400 | {
|
---|
| 401 | closelog();
|
---|
| 402 | }
|
---|
| 403 |
|
---|
| 404 | # endif /* Unix */
|
---|
| 405 |
|
---|
| 406 | #endif /* NO_SYSLOG */
|
---|