Coverage Report

Created: 2025-10-10 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/net-snmp/snmplib/snmp_logging.c
Line
Count
Source
1
/*
2
 * logging.c - generic logging for snmp-agent
3
 * * Contributed by Ragnar Kjørstad, ucd@ragnark.vestdata.no 1999-06-26 
4
 */
5
/* Portions of this file are subject to the following copyright(s).  See
6
 * the Net-SNMP's COPYING file for more details and other copyrights
7
 * that may apply:
8
 */
9
/*
10
 * Portions of this file are copyrighted by:
11
 * Copyright © 2003 Sun Microsystems, Inc. All rights reserved.
12
 * Use is subject to license terms specified in the COPYING file
13
 * distributed with the Net-SNMP package.
14
 *
15
 * Portions of this file are copyrighted by:
16
 * Copyright (c) 2016 VMware, Inc. All rights reserved.
17
 * Use is subject to license terms specified in the COPYING file
18
 * distributed with the Net-SNMP package.
19
 */
20
/** @defgroup snmp_logging generic logging for net-snmp 
21
 *  @ingroup library
22
 * 
23
 *  @{
24
 */
25
#include <net-snmp/net-snmp-config.h>
26
#include <net-snmp/net-snmp-features.h>
27
#include <stdio.h>
28
#ifdef HAVE_MALLOC_H
29
#include <malloc.h>
30
#endif
31
#ifdef HAVE_STRING_H
32
#include <string.h>
33
#else
34
#include <strings.h>
35
#endif
36
#include <ctype.h>
37
#ifdef HAVE_STDLIB_H
38
#include <stdlib.h>
39
#endif
40
#include <sys/types.h>
41
#include <sys/stat.h>
42
#include <fcntl.h>
43
#include <errno.h>
44
#ifdef HAVE_SYSLOG_H
45
#include <syslog.h>
46
#ifndef LOG_CONS                /* Interesting Ultrix feature */
47
#include <sys/syslog.h>
48
#endif
49
#endif
50
#ifdef TIME_WITH_SYS_TIME
51
# include <sys/time.h>
52
# include <time.h>
53
#else
54
# ifdef HAVE_SYS_TIME_H
55
#  include <sys/time.h>
56
# else
57
#  include <time.h>
58
# endif
59
#endif
60
#ifdef HAVE_NETINET_IN_H
61
#include <netinet/in.h>
62
#endif
63
64
#include <stdarg.h>
65
66
#ifdef HAVE_UNISTD_H
67
#include <unistd.h>
68
#endif
69
70
#include <net-snmp/types.h>
71
#include <net-snmp/output_api.h>
72
#include <net-snmp/library/snmp_logging.h>      /* For this file's "internal" definitions */
73
#include <net-snmp/config_api.h>
74
#include <net-snmp/utilities.h>
75
76
#include <net-snmp/library/callback.h>
77
78
#include "snmp_syslog.h"
79
80
#ifdef va_copy
81
#define NEED_VA_END_AFTER_VA_COPY
82
#else
83
#ifdef __vacopy
84
#define vacopy __vacopy
85
#define NEED_VA_END_AFTER_VA_COPY
86
#else
87
#define va_copy(dest, src) memcpy (&dest, &src, sizeof (va_list))
88
#endif
89
#endif
90
#ifndef HAVE_VSNPRINTF
91
#include "snprintf.h"
92
#endif
93
94
netsnmp_feature_child_of(logging_all, libnetsnmp);
95
96
netsnmp_feature_child_of(logging_outputs, logging_all);
97
netsnmp_feature_child_of(logging_file, logging_outputs);
98
netsnmp_feature_child_of(logging_stdio, logging_outputs);
99
netsnmp_feature_child_of(logging_syslog, logging_outputs);
100
netsnmp_feature_child_of(logging_external, logging_all);
101
102
netsnmp_feature_child_of(enable_stderrlog, logging_all);
103
104
netsnmp_feature_child_of(logging_enable_calllog, netsnmp_unused);
105
netsnmp_feature_child_of(logging_enable_loghandler, netsnmp_unused);
106
107
/* default to the file/stdio/syslog set */
108
netsnmp_feature_want(logging_outputs);
109
110
/*
111
 * logh_head:  A list of all log handlers, in increasing order of priority
112
 * logh_priorities:  'Indexes' into this list, by priority
113
 */
114
static netsnmp_log_handler *logh_head;
115
static netsnmp_log_handler *logh_priorities[LOG_DEBUG+1];
116
static int logh_enabled;
117
118
#ifndef NETSNMP_FEATURE_REMOVE_LOGGING_SYSLOG
119
static char syslogname[64] = DEFAULT_LOG_ID;
120
#endif /* NETSNMP_FEATURE_REMOVE_LOGGING_SYSLOG */
121
122
#ifndef NETSNMP_FEATURE_REMOVE_LOGGING_STDIO
123
netsnmp_log_handler *
124
netsnmp_register_stdio_loghandler(int is_stdout, int priority, int priority_max,
125
                                  const char *tok);
126
#endif
127
#ifndef NETSNMP_FEATURE_REMOVE_LOGGING_FILE
128
netsnmp_log_handler *
129
netsnmp_register_filelog_handler(const char* logfilename, int priority,
130
                                 int priority_max, int dont_zero_log);
131
#endif
132
133
void
134
netsnmp_disable_this_loghandler(netsnmp_log_handler *logh)
135
0
{
136
0
    if (!logh || (0 == logh->enabled))
137
0
        return;
138
0
    logh->enabled = 0;
139
0
    --logh_enabled;
140
0
    netsnmp_assert(logh_enabled >= 0);
141
0
}
142
143
void
144
netsnmp_enable_this_loghandler(netsnmp_log_handler *logh)
145
0
{
146
0
    if (!logh || (0 != logh->enabled))
147
0
        return;
148
0
    logh->enabled = 1;
149
0
    ++logh_enabled;
150
0
}
151
152
#ifndef NETSNMP_FEATURE_REMOVE_LOGGING_FILE
153
void
154
netsnmp_enable_filelog(netsnmp_log_handler *logh, int dont_zero_log);
155
#endif /* NETSNMP_FEATURE_REMOVE_LOGGING_FILE */
156
157
void
158
parse_config_logOption(const char *token, char *cptr)
159
0
{
160
0
  int my_argc = 0 ;
161
0
  char **my_argv = NULL;
162
163
0
  snmp_log_options( cptr, my_argc, my_argv );
164
0
}
165
166
void
167
init_snmp_logging(void)
168
0
{
169
0
    netsnmp_ds_register_premib(ASN_BOOLEAN, "snmp", "logTimestamp", 
170
0
       NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_LOG_TIMESTAMP);
171
0
    register_prenetsnmp_mib_handler("snmp", "logOption",
172
0
                                    parse_config_logOption, NULL, "string");
173
174
0
}
175
176
void
177
shutdown_snmp_logging(void)
178
0
{
179
0
   snmp_disable_log();
180
0
   while(NULL != logh_head)
181
0
      netsnmp_remove_loghandler( logh_head );
182
0
}
183
184
/* Set line buffering mode for a stream. */
185
void
186
netsnmp_set_line_buffering(FILE *stream)
187
1
{
188
#if defined(WIN32)
189
    /*
190
     * According to MSDN, the Microsoft Visual Studio C runtime library does
191
     * not support line buffering, so turn off buffering completely.
192
     * See also http://msdn.microsoft.com/en-us/library/86cebhfs(VS.71).aspx.
193
     */
194
    setvbuf(stream, NULL, _IONBF, BUFSIZ);
195
#elif defined(HAVE_SETLINEBUF)
196
    /* setlinefunction() is a function from the BSD Unix API. */
197
    setlinebuf(stream);
198
#else
199
    /* See also the C89 or C99 standard for more information about setvbuf(). */
200
1
    setvbuf(stream, NULL, _IOLBF, BUFSIZ);
201
1
#endif
202
1
}
203
204
/*
205
 * Decodes log priority.
206
 * @param optarg - IN - priority to decode, "0" or "0-7"
207
 *                 OUT - points to last character after the decoded priority
208
 * @param pri_max - OUT - maximum priority (i.e. 0x7 from "0-7")
209
 */
210
static int
211
decode_priority( char **optarg, int *pri_max )
212
0
{
213
0
    int pri_low = LOG_DEBUG;
214
215
0
    if (*optarg == NULL)
216
0
        return -1;
217
218
0
    switch (**optarg) {
219
0
        case '0': 
220
0
        case '!': 
221
0
            pri_low = LOG_EMERG;
222
0
            break;
223
0
        case '1': 
224
0
        case 'a': 
225
0
        case 'A': 
226
0
            pri_low = LOG_ALERT;
227
0
            break;
228
0
        case '2': 
229
0
        case 'c': 
230
0
        case 'C': 
231
0
            pri_low = LOG_CRIT;
232
0
            break;
233
0
        case '3': 
234
0
        case 'e': 
235
0
        case 'E': 
236
0
            pri_low = LOG_ERR;
237
0
            break;
238
0
        case '4': 
239
0
        case 'w': 
240
0
        case 'W': 
241
0
            pri_low = LOG_WARNING;
242
0
            break;
243
0
        case '5': 
244
0
        case 'n': 
245
0
        case 'N': 
246
0
            pri_low = LOG_NOTICE;
247
0
            break;
248
0
        case '6': 
249
0
        case 'i': 
250
0
        case 'I': 
251
0
            pri_low = LOG_INFO;
252
0
            break;
253
0
        case '7': 
254
0
        case 'd': 
255
0
        case 'D': 
256
0
            pri_low = LOG_DEBUG;
257
0
            break;
258
0
        default: 
259
0
            fprintf(stderr, "invalid priority: %c\n",**optarg);
260
0
            return -1;
261
0
    }
262
0
    *optarg = *optarg+1;
263
264
0
    if (pri_max && **optarg=='-') {
265
0
        *optarg = *optarg + 1; /* skip '-' */
266
0
        *pri_max = decode_priority( optarg, NULL );
267
0
        if (*pri_max == -1) return -1;
268
0
        if (pri_low < *pri_max) { 
269
0
            int tmp = pri_low; 
270
0
            pri_low = *pri_max; 
271
0
            *pri_max = tmp; 
272
0
        }
273
274
0
    }
275
0
    return pri_low;
276
0
}
277
278
#ifndef NETSNMP_FEATURE_REMOVE_LOGGING_SYSLOG
279
static int
280
decode_facility( char *optarg )
281
0
{
282
0
    if (optarg == NULL)
283
0
        return -1;
284
285
0
    switch (*optarg) {
286
0
    case 'd':
287
0
    case 'D':
288
0
        return LOG_DAEMON;
289
0
    case 'u':
290
0
    case 'U':
291
0
        return LOG_USER;
292
0
    case '0':
293
0
        return LOG_LOCAL0;
294
0
    case '1':
295
0
        return LOG_LOCAL1;
296
0
    case '2':
297
0
        return LOG_LOCAL2;
298
0
    case '3':
299
0
        return LOG_LOCAL3;
300
0
    case '4':
301
0
        return LOG_LOCAL4;
302
0
    case '5':
303
0
        return LOG_LOCAL5;
304
0
    case '6':
305
0
        return LOG_LOCAL6;
306
0
    case '7':
307
0
        return LOG_LOCAL7;
308
0
    default:
309
0
        fprintf(stderr, "invalid syslog facility: %c\n",*optarg);
310
0
        return -1;
311
0
    }
312
0
}
313
#endif /* NETSNMP_FEATURE_REMOVE_LOGGING_SYSLOG */
314
315
int
316
snmp_log_options(char *optarg, int argc, char *const *argv)
317
0
{
318
0
    char           *cp = optarg;
319
        /*
320
   * Hmmm... this doesn't seem to work.
321
   * The main agent 'getopt' handling assumes
322
   *   that the -L option takes an argument,
323
   *   and objects if this is missing.
324
   * Trying to differentiate between
325
   *   new-style "-Lx", and old-style "-L xx"
326
   *   is likely to be a major headache.
327
   */
328
0
    char            missing_opt = 'e';  /* old -L is new -Le */
329
0
    int             priority = LOG_DEBUG;
330
0
    int             pri_max  = LOG_DEBUG;
331
0
    int             inc_optind = 0;
332
0
    netsnmp_log_handler *logh;
333
334
0
    DEBUGMSGT(("logging:options", "optarg: '%s', argc %d, argv '%s'\n",
335
0
               optarg, argc, argv ? argv[0] : "NULL"));
336
0
    if (!*cp)
337
0
        cp = &missing_opt;
338
0
    else
339
0
        optarg++;
340
341
    /*
342
     * Support '... -Lx=value ....' syntax
343
     */
344
0
    if (*optarg == '=') {
345
0
        optarg++;
346
0
    }
347
    /*
348
     * and '.... "-Lx value" ....'  (*with* the quotes)
349
     */
350
0
    while (*optarg && isspace((unsigned char)(*optarg))) {
351
0
        optarg++;
352
0
    }
353
    /*
354
     * Finally, handle ".... -Lx value ...." syntax
355
     *   (*without* surrounding quotes)
356
     */
357
0
    if (!*optarg && argv && optind < argc) {
358
        /*
359
         * We've run off the end of the argument
360
         *  so move on to the next.
361
         * But we might not actually need it, so don't
362
   *  increment optind just yet!
363
         */
364
0
        optarg = argv[optind];
365
0
        inc_optind = 1;
366
0
    }
367
368
0
    DEBUGMSGT(("logging:options", "*cp: '%c'\n", *cp));
369
0
    switch (*cp) {
370
371
0
#ifndef NETSNMP_FEATURE_REMOVE_LOGGING_STDIO
372
    /*
373
     * Log to Standard Error
374
     */
375
0
    case 'E':
376
0
        priority = decode_priority( &optarg, &pri_max );
377
0
        if (priority == -1)  return -1;
378
0
        if (inc_optind)
379
0
            optind++;
380
0
        NETSNMP_FALLTHROUGH;
381
0
    case 'e':
382
0
        logh = netsnmp_register_stdio_loghandler(0, priority, pri_max, "stderr");
383
0
        break;
384
385
    /*
386
     * Log to Standard Output
387
     */
388
0
    case 'O':
389
0
        priority = decode_priority( &optarg, &pri_max );
390
0
        if (priority == -1)  return -1;
391
0
        if (inc_optind)
392
0
            optind++;
393
0
        NETSNMP_FALLTHROUGH;
394
0
    case 'o':
395
0
        logh = netsnmp_register_stdio_loghandler( 1, priority, pri_max, "stdout" );
396
0
        break;
397
0
#endif /* NETSNMP_FEATURE_REMOVE_LOGGING_STDIO */
398
399
    /*
400
     * Log to a named file
401
     */
402
0
#ifndef NETSNMP_FEATURE_REMOVE_LOGGING_FILE
403
0
    case 'F':
404
0
        priority = decode_priority( &optarg, &pri_max );
405
0
        if (priority == -1) return -1;
406
0
        while (*optarg == ' ') optarg++;
407
0
        if (!*optarg && !argv)
408
0
            return -1;
409
0
        else if (!*optarg)
410
0
            optarg = optind + 1 < argc ? argv[++optind] : NULL;
411
0
        NETSNMP_FALLTHROUGH;
412
0
    case 'f':
413
0
        if (inc_optind)
414
0
            optind++;
415
0
        if (!optarg) {
416
0
            fprintf(stderr, "Missing log file\n");
417
0
            return -1;
418
0
        }
419
0
        DEBUGMSGTL(("logging:options", "%d-%d: '%s'\n", priority, pri_max, optarg));
420
0
        logh = netsnmp_register_filelog_handler(optarg, priority, pri_max,
421
0
                                                   -1);
422
0
        break;
423
0
#endif /* NETSNMP_FEATURE_REMOVE_LOGGING_FILE */
424
425
0
#ifndef NETSNMP_FEATURE_REMOVE_LOGGING_SYSLOG
426
    /*
427
     * Log to syslog
428
     */
429
0
    case 'S':
430
0
        priority = decode_priority( &optarg, &pri_max );
431
0
        if (priority == -1 || !argv)  return -1;
432
0
        if (!optarg[0]) {
433
            /* The command line argument with priority does not contain log
434
             * facility. The facility must be in next argument then. */
435
0
            optind++;
436
0
            if (optind < argc)
437
0
                optarg = argv[optind];
438
0
        }
439
0
        NETSNMP_FALLTHROUGH;
440
0
    case 's':
441
0
        if (inc_optind)
442
0
            optind++;
443
0
        if (!optarg) {
444
0
            fprintf(stderr, "Missing syslog facility\n");
445
0
            return -1;
446
0
        }
447
0
        logh = netsnmp_register_loghandler(NETSNMP_LOGHANDLER_SYSLOG, priority);
448
0
        if (logh) {
449
0
            int facility = decode_facility(optarg);
450
0
            if (facility == -1) {
451
0
                netsnmp_remove_loghandler(logh);
452
0
                return -1;
453
0
            }
454
0
            logh->pri_max = pri_max;
455
0
            logh->token   = strdup(snmp_log_syslogname(NULL));
456
0
            logh->magic   = (void *)(intptr_t)facility;
457
0
      snmp_enable_syslog_ident(snmp_log_syslogname(NULL), facility);
458
0
  }
459
0
        break;
460
0
#endif /* NETSNMP_FEATURE_REMOVE_LOGGING_SYSLOG */
461
462
    /*
463
     * Don't log 
464
     */
465
0
    case 'N':
466
0
        priority = decode_priority( &optarg, &pri_max );
467
0
        if (priority == -1)  return -1;
468
0
        if (inc_optind)
469
0
            optind++;
470
0
        NETSNMP_FALLTHROUGH;
471
0
    case 'n':
472
        /*
473
         * disable all logs to clean them up (close files, etc),
474
         * remove all log handlers, then register a null handler.
475
         */
476
0
        snmp_disable_log();
477
0
        while(NULL != logh_head)
478
0
            netsnmp_remove_loghandler( logh_head );
479
0
        logh = netsnmp_register_loghandler(NETSNMP_LOGHANDLER_NONE, priority);
480
0
        if (logh) {
481
0
            logh->pri_max = pri_max;
482
0
  }
483
0
        break;
484
485
0
    default:
486
0
        fprintf(stderr, "Unknown logging option passed to -L: %c.\n", *cp);
487
0
        return -1;
488
0
    }
489
0
    return 0;
490
0
}
491
492
#ifndef NETSNMP_FEATURE_REMOVE_LOGGING_SYSLOG
493
char *
494
snmp_log_syslogname(const char *pstr)
495
0
{
496
0
  if (pstr)
497
0
    strlcpy (syslogname, pstr, sizeof(syslogname));
498
499
0
  return syslogname;
500
0
}
501
#endif /* NETSNMP_FEATURE_REMOVE_LOGGING_SYSLOG */
502
503
void
504
snmp_log_options_usage(const char *lead, FILE * outf)
505
0
{
506
0
    const char *pri1_msg = " for level 'pri' and above";
507
0
    const char *pri2_msg = " for levels 'p1' to 'p2'";
508
0
#ifndef NETSNMP_FEATURE_REMOVE_LOGGING_STDIO
509
0
    fprintf(outf, "%se:           log to standard error\n", lead);
510
0
    fprintf(outf, "%so:           log to standard output\n", lead);
511
0
#endif /* NETSNMP_FEATURE_REMOVE_LOGGING_STDIO */
512
0
    fprintf(outf, "%sn:           don't log at all\n", lead);
513
0
#ifndef NETSNMP_FEATURE_REMOVE_LOGGING_FILE
514
0
    fprintf(outf, "%sf file:      log to the specified file\n", lead);
515
0
#endif /* NETSNMP_FEATURE_REMOVE_LOGGING_FILE */
516
0
#ifndef NETSNMP_FEATURE_REMOVE_LOGGING_SYSLOG
517
0
    fprintf(outf, "%ss facility:  log to syslog (via the specified facility)\n", lead);
518
0
#endif /* NETSNMP_FEATURE_REMOVE_LOGGING_SYSLOG */
519
0
    fprintf(outf, "\n%s(variants)\n", lead);
520
0
#ifndef NETSNMP_FEATURE_REMOVE_LOGGING_FILE
521
0
    fprintf(outf, "%s[EON] pri:   log to standard error, output or /dev/null%s\n", lead, pri1_msg);
522
0
    fprintf(outf, "%s[EON] p1-p2: log to standard error, output or /dev/null%s\n", lead, pri2_msg);
523
0
#endif /* NETSNMP_FEATURE_REMOVE_LOGGING_FILE */
524
0
    fprintf(outf, "%s[FS] pri token:    log to file/syslog%s\n", lead, pri1_msg);
525
0
    fprintf(outf, "%s[FS] p1-p2 token:  log to file/syslog%s\n", lead, pri2_msg);
526
0
}
527
528
/**
529
 * Is logging done?
530
 *
531
 * @return Returns 0 if logging is off, 1 when it is done.
532
 *
533
 */
534
int
535
snmp_get_do_logging(void)
536
0
{
537
0
    return (logh_enabled > 0);
538
0
}
539
540
541
static char    *
542
sprintf_stamp(time_t * now, char *sbuf)
543
128
{
544
128
    time_t          Now;
545
128
    struct tm      *tm;
546
547
128
    if (now == NULL) {
548
128
        now = &Now;
549
128
        time(now);
550
128
    }
551
128
    tm = localtime(now);
552
128
    if (tm)
553
128
        sprintf(sbuf, "%.4d-%.2d-%.2d %.2d:%.2d:%.2d ",
554
128
                tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
555
128
                tm->tm_hour, tm->tm_min, tm->tm_sec);
556
0
    else
557
0
        sprintf(sbuf, "(unknown)");
558
128
    return sbuf;
559
128
}
560
561
#ifndef NETSNMP_FEATURE_REMOVE_LOGGING_SYSLOG
562
void
563
snmp_disable_syslog_entry(netsnmp_log_handler *logh)
564
0
{
565
0
    if (!logh || !logh->enabled || logh->type != NETSNMP_LOGHANDLER_SYSLOG)
566
0
        return;
567
568
#ifdef WIN32
569
    if (logh->magic) {
570
        HANDLE eventlog_h = (HANDLE)logh->magic;
571
        CloseEventLog(eventlog_h);
572
        logh->magic = NULL;
573
    }
574
#else
575
0
    closelog();
576
0
    logh->imagic  = 0;
577
0
#endif
578
579
0
    netsnmp_disable_this_loghandler(logh);
580
0
}
581
582
void
583
snmp_disable_syslog(void)
584
0
{
585
0
    netsnmp_log_handler *logh;
586
587
0
    for (logh = logh_head; logh; logh = logh->next)
588
0
        if (logh->enabled && logh->type == NETSNMP_LOGHANDLER_SYSLOG)
589
0
            snmp_disable_syslog_entry(logh);
590
0
}
591
#endif /* NETSNMP_FEATURE_REMOVE_LOGGING_SYSLOG */
592
593
#ifndef NETSNMP_FEATURE_REMOVE_LOGGING_FILE
594
void
595
snmp_disable_filelog_entry(netsnmp_log_handler *logh)
596
0
{
597
0
    if (!logh /* || !logh->enabled */ || logh->type != NETSNMP_LOGHANDLER_FILE)
598
0
        return;
599
600
0
    if (logh->magic) {
601
0
        fputs("\n", (FILE*)logh->magic);  /* XXX - why? */
602
0
        fclose((FILE*)logh->magic);
603
0
        logh->magic   = NULL;
604
0
    }
605
0
    netsnmp_disable_this_loghandler(logh);
606
0
}
607
608
void
609
snmp_disable_filelog(void)
610
0
{
611
0
    netsnmp_log_handler *logh;
612
613
0
    for (logh = logh_head; logh; logh = logh->next)
614
0
        if (logh->enabled && logh->type == NETSNMP_LOGHANDLER_FILE)
615
0
            snmp_disable_filelog_entry(logh);
616
0
}
617
#endif /* NETSNMP_FEATURE_REMOVE_LOGGING_FILE */
618
619
#ifndef NETSNMP_FEATURE_REMOVE_LOGGING_STDIO
620
621
netsnmp_log_handler *
622
netsnmp_register_stdio_loghandler(int is_stdout, int priority, int priority_max,
623
                                  const char *tok)
624
0
{
625
0
    netsnmp_log_handler *logh =
626
0
        netsnmp_register_loghandler(NETSNMP_LOGHANDLER_STDERR, priority);
627
0
    if (NULL == logh) {
628
0
        return NULL;
629
0
    }
630
0
    if (is_stdout) {
631
0
        netsnmp_set_line_buffering(stdout);
632
0
        logh->imagic = 1; /* stdout, not stderr */
633
0
    } else
634
0
        netsnmp_set_line_buffering(stderr);
635
636
0
    logh->pri_max = priority_max;
637
0
    if (tok)
638
0
        logh->token   = strdup(tok);
639
0
    return logh;
640
0
}
641
642
/*
643
 * returns that status of stderr logging
644
 *
645
 * @retval 0 : stderr logging disabled
646
 * @retval 1 : stderr logging enabled
647
 */
648
int
649
snmp_stderrlog_status(void)
650
0
{
651
0
    netsnmp_log_handler *logh;
652
653
0
    for (logh = logh_head; logh; logh = logh->next)
654
0
        if (logh->enabled && (logh->type == NETSNMP_LOGHANDLER_STDOUT ||
655
0
                              logh->type == NETSNMP_LOGHANDLER_STDERR)) {
656
0
            return 1;
657
0
       }
658
659
0
    return 0;
660
0
}
661
662
void
663
snmp_disable_stderrlog(void)
664
0
{
665
0
    netsnmp_log_handler *logh;
666
667
0
    for (logh = logh_head; logh; logh = logh->next)
668
0
        if (logh->enabled && (logh->type == NETSNMP_LOGHANDLER_STDOUT ||
669
0
                              logh->type == NETSNMP_LOGHANDLER_STDERR)) {
670
0
            netsnmp_disable_this_loghandler(logh);
671
0
  }
672
0
}
673
#endif /* NETSNMP_FEATURE_REMOVE_LOGGING_STDIO */
674
675
#ifndef NETSNMP_FEATURE_REMOVE_LOGGING_ENABLE_CALLLOG
676
void
677
snmp_disable_calllog(void)
678
0
{
679
0
    netsnmp_log_handler *logh;
680
681
0
    for (logh = logh_head; logh; logh = logh->next)
682
0
        if (logh->enabled && logh->type == NETSNMP_LOGHANDLER_CALLBACK) {
683
0
            netsnmp_disable_this_loghandler(logh);
684
0
  }
685
0
}
686
#endif /* NETSNMP_FEATURE_REMOVE_LOGGING_ENABLE_CALLLOG */
687
688
void
689
snmp_disable_log(void)
690
0
{
691
0
    netsnmp_log_handler *logh;
692
693
0
    for (logh = logh_head; logh; logh = logh->next) {
694
0
#ifndef NETSNMP_FEATURE_REMOVE_LOGGING_SYSLOG
695
0
        if (logh->type == NETSNMP_LOGHANDLER_SYSLOG)
696
0
            snmp_disable_syslog_entry(logh);
697
0
#endif /* NETSNMP_FEATURE_REMOVE_LOGGING_SYSLOG */
698
0
#ifndef NETSNMP_FEATURE_REMOVE_LOGGING_FILE
699
0
        if (logh->type == NETSNMP_LOGHANDLER_FILE)
700
0
            snmp_disable_filelog_entry(logh);
701
0
#endif /* NETSNMP_FEATURE_REMOVE_LOGGING_FILE */
702
0
        netsnmp_disable_this_loghandler(logh);
703
0
    }
704
0
}
705
706
/*
707
 * close and reopen all file based logs, to allow logfile
708
 * rotation.
709
 */
710
void
711
netsnmp_logging_restart(void)
712
0
{
713
0
    netsnmp_log_handler *logh;
714
0
#ifndef NETSNMP_FEATURE_REMOVE_LOGGING_SYSLOG
715
0
    int doneone = 0;
716
0
#endif /* NETSNMP_FEATURE_REMOVE_LOGGING_SYSLOG */
717
718
0
    for (logh = logh_head; logh; logh = logh->next) {
719
0
        if (0 == logh->enabled)
720
0
            continue;
721
0
#ifndef NETSNMP_FEATURE_REMOVE_LOGGING_SYSLOG
722
0
        if (logh->type == NETSNMP_LOGHANDLER_SYSLOG) {
723
0
            snmp_disable_syslog_entry(logh);
724
0
            snmp_enable_syslog_ident(logh->token,(int)(intptr_t)logh->magic);
725
0
            doneone = 1;
726
0
        }
727
0
#endif /* NETSNMP_FEATURE_REMOVE_LOGGING_SYSLOG */
728
0
#ifndef NETSNMP_FEATURE_REMOVE_LOGGING_FILE
729
0
        if (logh->type == NETSNMP_LOGHANDLER_FILE && !doneone) {
730
0
            snmp_disable_filelog_entry(logh);
731
            /** hmm, don't zero status isn't saved.. i think it's
732
             * safer not to overwrite, in case a hup is just to
733
             * re-read config files...
734
             */
735
0
            netsnmp_enable_filelog(logh, 1);
736
0
        }
737
0
#endif /* NETSNMP_FEATURE_REMOVE_LOGGING_FILE */
738
0
    }
739
0
}
740
741
/* ================================================== */
742
743
#ifndef NETSNMP_FEATURE_REMOVE_LOGGING_SYSLOG
744
void
745
snmp_enable_syslog(void)
746
0
{
747
0
    snmp_enable_syslog_ident(snmp_log_syslogname(NULL), LOG_DAEMON);
748
0
}
749
750
void
751
snmp_enable_syslog_ident(const char *ident, const int facility)
752
0
{
753
0
    netsnmp_log_handler *logh;
754
0
    int                  found = 0;
755
0
    int                  enable = 1;
756
#ifdef WIN32
757
    HANDLE               eventlog_h;
758
#else
759
0
    void                *eventlog_h = NULL;
760
0
#endif
761
762
0
    snmp_disable_syslog();     /* only one syslog at a time */
763
#ifdef WIN32
764
    eventlog_h = OpenEventLog(NULL, ident);
765
    if (eventlog_h == NULL) {
766
      /*
767
       * Hmmm.....
768
       * Maybe disable this handler, and log the error ?
769
       */
770
        fprintf(stderr, "Could not open event log for %s. Last error: %u\n",
771
                ident, (unsigned int)GetLastError());
772
        enable = 0;
773
    }
774
#else
775
0
    openlog(snmp_log_syslogname(ident), LOG_CONS | LOG_PID, facility);
776
0
#endif
777
778
0
    for (logh = logh_head; logh; logh = logh->next)
779
0
        if (logh->type == NETSNMP_LOGHANDLER_SYSLOG) {
780
0
            logh->magic   = (void*)eventlog_h;
781
0
            logh->imagic  = enable; /* syslog open */
782
0
            if (logh->enabled && (0 == enable))
783
0
                netsnmp_disable_this_loghandler(logh);
784
0
            else if ((0 == logh->enabled) && enable)
785
0
                netsnmp_enable_this_loghandler(logh);
786
0
            found         = 1;
787
0
  }
788
789
0
    if (!found) {
790
0
        logh = netsnmp_register_loghandler(NETSNMP_LOGHANDLER_SYSLOG,
791
0
                                           LOG_DEBUG );
792
0
        if (logh) {
793
0
            logh->magic    = (void*)eventlog_h;
794
0
            logh->token    = strdup(ident);
795
0
            logh->imagic   = enable;  /* syslog open */
796
0
            if (logh->enabled && (0 == enable))
797
0
                netsnmp_disable_this_loghandler(logh);
798
0
            else if ((0 == logh->enabled) && enable)
799
0
                netsnmp_enable_this_loghandler(logh);
800
0
        }
801
0
    }
802
0
}
803
#endif /* NETSNMP_FEATURE_REMOVE_LOGGING_SYSLOG */
804
805
#ifndef NETSNMP_FEATURE_REMOVE_LOGGING_FILE
806
void
807
netsnmp_enable_filelog(netsnmp_log_handler *logh, int dont_zero_log)
808
0
{
809
0
    FILE *logfile;
810
811
0
    if (!logh)
812
0
        return;
813
814
0
    if (!logh->magic) {
815
0
        logfile = fopen(logh->token, dont_zero_log ? "a" : "w");
816
0
        if (!logfile) {
817
0
      snmp_log_perror(logh->token);
818
0
            return;
819
0
  }
820
0
        logh->magic = (void*)logfile;
821
0
        netsnmp_set_line_buffering(logfile);
822
0
    }
823
0
    netsnmp_enable_this_loghandler(logh);
824
0
}
825
826
netsnmp_log_handler *
827
netsnmp_register_filelog_handler(const char* logfilename, int priority,
828
                                 int priority_max, int dont_zero_log)
829
0
{
830
0
    netsnmp_log_handler *logh =
831
0
        netsnmp_register_loghandler(NETSNMP_LOGHANDLER_FILE,
832
0
                                    priority );
833
0
    if (NULL == logh)
834
0
        return NULL;
835
0
    logh->pri_max = priority_max;
836
0
    logh->token = strdup(logfilename);
837
0
    if (-1 == dont_zero_log)
838
0
        dont_zero_log = netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID,
839
0
                                               NETSNMP_DS_LIB_APPEND_LOGFILES);
840
0
    netsnmp_enable_filelog(logh, dont_zero_log);
841
0
    return logh;
842
0
}
843
844
void
845
snmp_enable_filelog(const char *logfilename, int dont_zero_log)
846
0
{
847
0
    netsnmp_log_handler *logh;
848
849
    /*
850
     * don't disable ALL filelogs whenever a new one is enabled.
851
     * this prevents '-Lf file' from working in snmpd, as the
852
     * call to set up /var/log/snmpd.log will disable the previous
853
     * log setup.
854
     * snmp_disable_filelog();
855
     */
856
857
0
    if (logfilename) {
858
0
        logh = netsnmp_find_loghandler( logfilename );
859
0
        if (!logh)
860
0
            logh = netsnmp_register_filelog_handler( logfilename, LOG_DEBUG,
861
0
                                                     0, dont_zero_log );
862
0
        else
863
0
            netsnmp_enable_filelog(logh, dont_zero_log);
864
0
    } else {
865
0
        for (logh = logh_head; logh; logh = logh->next)
866
0
            if (logh->type == NETSNMP_LOGHANDLER_FILE)
867
0
                netsnmp_enable_filelog(logh, dont_zero_log);
868
0
    }
869
0
}
870
#endif /* NETSNMP_FEATURE_REMOVE_LOGGING_FILE */
871
872
873
#ifndef NETSNMP_FEATURE_REMOVE_ENABLE_STDERRLOG
874
/* used in the perl modules and ip-mib/ipv4InterfaceTable/ipv4InterfaceTable_subagent.c */
875
void
876
snmp_enable_stderrlog(void)
877
0
{
878
0
    netsnmp_log_handler *logh;
879
0
    int                  found = 0;
880
881
0
    for (logh = logh_head; logh; logh = logh->next)
882
0
        if (logh->type == NETSNMP_LOGHANDLER_STDOUT ||
883
0
            logh->type == NETSNMP_LOGHANDLER_STDERR) {
884
0
            netsnmp_enable_this_loghandler(logh);
885
0
            found         = 1;
886
0
        }
887
888
0
    if (!found) {
889
0
        logh = netsnmp_register_loghandler(NETSNMP_LOGHANDLER_STDERR,
890
0
                                           LOG_DEBUG );
891
0
        if (logh)
892
0
            logh->token    = strdup("stderr");
893
0
    }
894
0
}
895
#endif /* NETSNMP_FEATURE_REMOVE_ENABLE_STDERRLOG */
896
897
#ifndef NETSNMP_FEATURE_REMOVE_LOGGING_ENABLE_CALLLOG
898
void
899
snmp_enable_calllog(void) /* XXX - or take a callback routine ??? */
900
0
{
901
0
    netsnmp_log_handler *logh;
902
0
    int                  found = 0;
903
904
0
    for (logh = logh_head; logh; logh = logh->next)
905
0
        if (logh->type == NETSNMP_LOGHANDLER_CALLBACK) {
906
0
            netsnmp_enable_this_loghandler(logh);
907
0
            found         = 1;
908
0
  }
909
910
0
    if (!found) {
911
0
        logh = netsnmp_register_loghandler(NETSNMP_LOGHANDLER_CALLBACK,
912
0
                                           LOG_DEBUG );
913
0
        if (logh)
914
0
            logh->token    = strdup("callback");
915
0
    }
916
0
}
917
#endif /* NETSNMP_FEATURE_REMOVE_LOGGING_ENABLE_CALLLOG */
918
919
920
/* ==================================================== */
921
922
923
netsnmp_log_handler *
924
netsnmp_find_loghandler( const char *token )
925
0
{
926
0
    netsnmp_log_handler *logh;
927
0
    if (!token)
928
0
        return NULL;
929
930
0
    for (logh = logh_head; logh; logh = logh->next)
931
0
        if (logh->token && !strcmp( token, logh->token ))
932
0
            break;
933
934
0
    return logh;
935
0
}
936
937
int
938
netsnmp_add_loghandler( netsnmp_log_handler *logh )
939
0
{
940
0
    int i;
941
0
    netsnmp_log_handler *logh2;
942
943
0
    if (!logh)
944
0
        return 0;
945
946
    /*
947
     * Find the appropriate point for the new entry...
948
     *   (logh2 will point to the entry immediately following)
949
     */
950
0
    for (logh2 = logh_head; logh2; logh2 = logh2->next)
951
0
        if ( logh2->priority >= logh->priority )
952
0
            break;
953
954
    /*
955
     * ... and link it into the main list.
956
     */
957
0
    if (logh2) {
958
0
        if (logh2->prev)
959
0
            logh2->prev->next = logh;
960
0
        else
961
0
            logh_head = logh;
962
0
        logh->prev = logh2->prev;
963
0
        logh->next  = logh2;
964
0
        logh2->prev = logh;
965
0
    } else if (logh_head ) {
966
        /*
967
         * If logh2 is NULL, we're tagging on to the end
968
         */
969
0
        for (logh2 = logh_head; logh2->next; logh2 = logh2->next)
970
0
            ;
971
0
        logh2->next = logh;
972
0
        logh->prev = logh2;
973
0
    } else {
974
0
        logh_head = logh;
975
0
    }
976
977
    /*
978
     * Also tweak the relevant priority-'index' array.
979
     */
980
0
    for (i=LOG_EMERG; i<=logh->priority; i++)
981
0
        if (!logh_priorities[i] ||
982
0
             logh_priorities[i]->priority >= logh->priority)
983
0
             logh_priorities[i] = logh;
984
985
0
    return 1;
986
0
}
987
988
netsnmp_log_handler *
989
netsnmp_register_loghandler( int type, int priority )
990
0
{
991
0
    netsnmp_log_handler *logh;
992
993
0
    logh = SNMP_MALLOC_TYPEDEF(netsnmp_log_handler);
994
0
    if (!logh)
995
0
        return NULL;
996
997
0
    DEBUGMSGT(("logging:register", "registering log type %d with pri %d\n",
998
0
               type, priority));
999
0
    if (priority > LOG_DEBUG) {
1000
0
        DEBUGMSGT(("logging:register", "  limiting pri %d to %d\n", priority,
1001
0
                   LOG_DEBUG));
1002
0
        priority = LOG_DEBUG;
1003
0
    }
1004
1005
0
    logh->type     = type;
1006
0
    switch ( type ) {
1007
0
#ifndef NETSNMP_FEATURE_REMOVE_LOGGING_STDIO
1008
0
    case NETSNMP_LOGHANDLER_STDOUT:
1009
0
        logh->imagic  = 1;
1010
0
        logh->handler = log_handler_stdouterr;
1011
0
        break;
1012
0
    case NETSNMP_LOGHANDLER_STDERR:
1013
0
        logh->handler = log_handler_stdouterr;
1014
0
        break;
1015
0
#endif /* NETSNMP_FEATURE_REMOVE_LOGGING_STDIO */
1016
1017
0
#ifndef NETSNMP_FEATURE_REMOVE_LOGGING_FILE
1018
0
    case NETSNMP_LOGHANDLER_FILE:
1019
0
        logh->handler = log_handler_file;
1020
0
        logh->imagic  = 1;
1021
0
        break;
1022
0
#endif /* NETSNMP_FEATURE_REMOVE_LOGGING_FILE */
1023
0
#ifndef NETSNMP_FEATURE_REMOVE_LOGGING_SYSLOG
1024
0
    case NETSNMP_LOGHANDLER_SYSLOG:
1025
0
        logh->handler = log_handler_syslog;
1026
0
        break;
1027
0
#endif /* NETSNMP_FEATURE_REMOVE_LOGGING_SYSLOG */
1028
0
    case NETSNMP_LOGHANDLER_CALLBACK:
1029
0
        logh->handler = log_handler_callback;
1030
0
        break;
1031
0
    case NETSNMP_LOGHANDLER_NONE:
1032
0
        logh->handler = log_handler_null;
1033
0
        break;
1034
0
    default:
1035
0
        free(logh);
1036
0
        return NULL;
1037
0
    }
1038
0
    logh->priority = priority;
1039
0
    netsnmp_enable_this_loghandler(logh);
1040
0
    netsnmp_add_loghandler( logh );
1041
0
    return logh;
1042
0
}
1043
1044
1045
#ifndef NETSNMP_FEATURE_REMOVE_LOGGING_ENABLE_LOGHANDLER
1046
int
1047
netsnmp_enable_loghandler( const char *token )
1048
0
{
1049
0
    netsnmp_log_handler *logh;
1050
1051
0
    logh = netsnmp_find_loghandler( token );
1052
0
    if (!logh)
1053
0
        return 0;
1054
0
    netsnmp_enable_this_loghandler(logh);
1055
0
    return 1;
1056
0
}
1057
1058
1059
int
1060
netsnmp_disable_loghandler( const char *token )
1061
0
{
1062
0
    netsnmp_log_handler *logh;
1063
1064
0
    logh = netsnmp_find_loghandler( token );
1065
0
    if (!logh)
1066
0
        return 0;
1067
0
    netsnmp_disable_this_loghandler(logh);
1068
0
    return 1;
1069
0
}
1070
#endif /* NETSNMP_FEATURE_REMOVE_LOGGING_ENABLE_LOGHANDLER */
1071
1072
int
1073
netsnmp_remove_loghandler( netsnmp_log_handler *logh )
1074
0
{
1075
0
    int i;
1076
0
    if (!logh)
1077
0
        return 0;
1078
1079
0
    if (logh->prev)
1080
0
        logh->prev->next = logh->next;
1081
0
    else
1082
0
        logh_head = logh->next;
1083
1084
0
    if (logh->next)
1085
0
        logh->next->prev = logh->prev;
1086
1087
0
    for (i=LOG_EMERG; i<=logh->priority; i++)
1088
0
        if (logh == logh_priorities[i])
1089
0
            logh_priorities[i] = logh->next;
1090
0
    free(NETSNMP_REMOVE_CONST(char*, logh->token));
1091
0
    SNMP_FREE(logh);
1092
1093
0
    return 1;
1094
0
}
1095
1096
/* ==================================================== */
1097
1098
#ifndef NETSNMP_FEATURE_REMOVE_LOGGING_STDIO
1099
int
1100
log_handler_stdouterr(  netsnmp_log_handler* logh, int pri, const char *str)
1101
141
{
1102
141
    static int      newline = 1;   /* MTCRITICAL_RESOURCE */
1103
141
    const char     *newline_ptr;
1104
141
    char            sbuf[40];
1105
1106
141
    if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, 
1107
141
                               NETSNMP_DS_LIB_LOG_TIMESTAMP) && newline) {
1108
128
        sprintf_stamp(NULL, sbuf);
1109
128
    } else {
1110
13
        strcpy(sbuf, "");
1111
13
    }
1112
    /*
1113
     * Remember whether or not the current line ends with a newline for the
1114
     * next call of log_handler_stdouterr().
1115
     */
1116
141
    newline_ptr = strrchr(str, '\n');
1117
141
    newline = newline_ptr && newline_ptr[1] == 0;
1118
1119
141
    if (logh->imagic)
1120
0
       printf(         "%s%s", sbuf, str);
1121
141
    else
1122
141
       fprintf(stderr, "%s%s", sbuf, str);
1123
1124
141
    return 1;
1125
141
}
1126
#endif /* NETSNMP_FEATURE_REMOVE_LOGGING_STDIO */
1127
1128
1129
#ifndef NETSNMP_FEATURE_REMOVE_LOGGING_SYSLOG
1130
#ifdef WIN32
1131
int
1132
log_handler_syslog(  netsnmp_log_handler* logh, int pri, const char *str)
1133
{
1134
    WORD            etype;
1135
    LPCTSTR         event_msg[2];
1136
    HANDLE          eventlog_h = logh->magic;
1137
1138
        /*
1139
         **  EVENT TYPES:
1140
         **
1141
         **  Information (EVENTLOG_INFORMATION_TYPE)
1142
         **      Information events indicate infrequent but significant
1143
         **      successful operations.
1144
         **  Warning (EVENTLOG_WARNING_TYPE)
1145
         **      Warning events indicate problems that are not immediately
1146
         **      significant, but that may indicate conditions that could
1147
         **      cause future problems. Resource consumption is a good
1148
         **      candidate for a warning event.
1149
         **  Error (EVENTLOG_ERROR_TYPE)
1150
         **      Error events indicate significant problems that the user
1151
         **      should know about. Error events usually indicate a loss of
1152
         **      functionality or data.
1153
         */
1154
    switch (pri) {
1155
        case LOG_EMERG:
1156
        case LOG_ALERT:
1157
        case LOG_CRIT:
1158
        case LOG_ERR:
1159
            etype = EVENTLOG_ERROR_TYPE;
1160
            break;
1161
        case LOG_WARNING:
1162
            etype = EVENTLOG_WARNING_TYPE;
1163
            break;
1164
        case LOG_NOTICE:
1165
        case LOG_INFO:
1166
        case LOG_DEBUG:
1167
            etype = EVENTLOG_INFORMATION_TYPE;
1168
            break;
1169
        default:
1170
            etype = EVENTLOG_INFORMATION_TYPE;
1171
            break;
1172
    }
1173
    event_msg[0] = str;
1174
    event_msg[1] = NULL;
1175
    /* NOTE: 4th parameter must match winservice.mc:MessageId value */
1176
    if (!ReportEvent(eventlog_h, etype, 0, 100, NULL, 1, 0, event_msg, NULL)) {
1177
      /*
1178
       * Hmmm.....
1179
       * Maybe disable this handler, and log the error ?
1180
       */
1181
        fprintf(stderr, "Could not report event.  Last error: %u\n",
1182
                (unsigned int)GetLastError());
1183
        return 0;
1184
    }
1185
    return 1;
1186
}
1187
#else
1188
int
1189
log_handler_syslog(  netsnmp_log_handler* logh, int pri, const char *str)
1190
0
{
1191
  /*
1192
   * XXX
1193
   * We've got three items of information to work with:
1194
   *     Is the syslog currently open?
1195
   *     What ident string to use?
1196
   *     What facility to log to?
1197
   *
1198
   * We've got two "magic" locations (imagic & magic) plus the token
1199
   */
1200
0
    if (!(logh->imagic)) {
1201
0
        const char *ident    = logh->token;
1202
0
        int   facility = (int)(intptr_t)logh->magic;
1203
0
        if (!ident)
1204
0
            ident = netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID,
1205
0
                                          NETSNMP_DS_LIB_APPTYPE);
1206
0
        openlog(ident, LOG_CONS | LOG_PID, facility);
1207
0
        logh->imagic = 1;
1208
0
    }
1209
0
    syslog( pri, "%s", str );
1210
0
    return 1;
1211
0
}
1212
#endif /* !WIN32 */
1213
#endif /* NETSNMP_FEATURE_REMOVE_LOGGING_SYSLOG */
1214
1215
1216
#ifndef NETSNMP_FEATURE_REMOVE_LOGGING_FILE
1217
int
1218
log_handler_file(    netsnmp_log_handler* logh, int pri, const char *str)
1219
0
{
1220
0
    FILE           *fhandle;
1221
0
    char            sbuf[40];
1222
0
    int             len = strlen( str );
1223
1224
    /*
1225
     * We use imagic to save information about whether the next output
1226
     * will start a new line, and thus might need a timestamp
1227
     */
1228
0
    if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, 
1229
0
                               NETSNMP_DS_LIB_LOG_TIMESTAMP) && logh->imagic) {
1230
0
        sprintf_stamp(NULL, sbuf);
1231
0
    } else {
1232
0
        strcpy(sbuf, "");
1233
0
    }
1234
1235
    /*
1236
     * If we haven't already opened the file, then do so.
1237
     * Save the filehandle pointer for next time.
1238
     *
1239
     * Note that this should still work, even if the file
1240
     * is closed in the meantime (e.g. a regular "cleanup" sweep)
1241
     */
1242
0
    fhandle = (FILE*)logh->magic;
1243
0
    if (!logh->magic) {
1244
0
        fhandle = fopen(logh->token, "a+");
1245
0
        if (!fhandle)
1246
0
            return 0;
1247
0
        logh->magic = (void*)fhandle;
1248
0
    }
1249
0
    fprintf(fhandle, "%s%s", sbuf, str);
1250
0
    fflush(fhandle);
1251
0
    if (len > 0) {
1252
0
        logh->imagic = str[len - 1] == '\n';
1253
0
    } else {
1254
0
        logh->imagic = 0;
1255
0
    }
1256
0
    return 1;
1257
0
}
1258
#endif /* NETSNMP_FEATURE_REMOVE_LOGGING_FILE */
1259
1260
int
1261
log_handler_callback(netsnmp_log_handler* logh, int pri, const char *str)
1262
0
{
1263
  /*
1264
   * XXX - perhaps replace 'snmp_call_callbacks' processing
1265
   *       with individual callback log_handlers ??
1266
   */
1267
0
    struct snmp_log_message slm;
1268
0
    int             dodebug = snmp_get_do_debugging();
1269
1270
0
    slm.priority = pri;
1271
0
    slm.msg = str;
1272
0
    if (dodebug)            /* turn off debugging inside the callbacks else will loop */
1273
0
        snmp_set_do_debugging(0);
1274
0
    snmp_call_callbacks(SNMP_CALLBACK_LIBRARY, SNMP_CALLBACK_LOGGING, &slm);
1275
0
    if (dodebug)
1276
0
        snmp_set_do_debugging(dodebug);
1277
0
    return 1;
1278
0
}
1279
1280
int
1281
log_handler_null(    netsnmp_log_handler* logh, int pri, const char *str)
1282
0
{
1283
    /*
1284
     * Dummy log handler - just throw away the error completely
1285
     * You probably don't really want to do this!
1286
     */
1287
0
    return 1;
1288
0
}
1289
1290
void
1291
snmp_log_string(int priority, const char *str)
1292
141
{
1293
141
#ifndef NETSNMP_FEATURE_REMOVE_LOGGING_STDIO
1294
141
    static int stderr_enabled = 0;
1295
141
    static netsnmp_log_handler lh = { 1, 0, 0, 0, "stderr",
1296
141
                                      log_handler_stdouterr, 0, NULL,  NULL,
1297
141
                                      NULL };
1298
141
#endif /* NETSNMP_FEATURE_REMOVE_LOGGING_STDIO */
1299
141
    netsnmp_log_handler *logh;
1300
1301
    /*
1302
     * We've got to be able to log messages *somewhere*!
1303
     * If you don't want stderr logging, then enable something else.
1304
     */
1305
141
    if (0 == logh_enabled) {
1306
141
#ifndef NETSNMP_FEATURE_REMOVE_LOGGING_STDIO
1307
141
        if (!stderr_enabled) {
1308
1
            ++stderr_enabled;
1309
1
            netsnmp_set_line_buffering(stderr);
1310
1
        }
1311
141
        log_handler_stdouterr( &lh, priority, str );
1312
141
#endif /* NETSNMP_FEATURE_REMOVE_LOGGING_STDIO */
1313
1314
141
        return;
1315
141
    }
1316
0
#ifndef NETSNMP_FEATURE_REMOVE_LOGGING_STDIO
1317
0
    else if (stderr_enabled) {
1318
0
        stderr_enabled = 0;
1319
0
        log_handler_stdouterr( &lh, LOG_INFO,
1320
0
                               "Log handling defined - disabling stderr\n" );
1321
0
    }
1322
0
#endif /* NETSNMP_FEATURE_REMOVE_LOGGING_STDIO */
1323
        
1324
1325
    /*
1326
     * Start at the given priority, and work "upwards"....
1327
     */
1328
0
    if (priority > LOG_DEBUG)
1329
0
        priority = LOG_DEBUG;
1330
0
    logh = logh_priorities[priority];
1331
0
    for ( ; logh; logh = logh->next ) {
1332
        /*
1333
         * ... but skipping any handlers with a "maximum priority"
1334
         *     that we have already exceeded. And don't forget to
1335
         *     ensure this logging is turned on (see snmp_disable_stderrlog
1336
         *     and its cohorts).
1337
         */
1338
0
        if (logh->enabled && priority <= logh->pri_max)
1339
0
            logh->handler( logh, priority, str );
1340
0
    }
1341
0
}
1342
1343
/* ==================================================== */
1344
1345
1346
/**
1347
 * This snmp logging function allows variable argument list given the
1348
 * specified priority, format and a populated va_list structure.
1349
 * The default logfile this function writes to is /var/log/snmpd.log.
1350
 *
1351
 * @param priority is an integer representing the type of message to be written
1352
 *  to the snmp log file.  The types are errors, warning, and information.
1353
 *      - The error types are:
1354
 *        - LOG_EMERG       system is unusable 
1355
 *        - LOG_ALERT       action must be taken immediately 
1356
 *        - LOG_CRIT        critical conditions 
1357
 *        - LOG_ERR         error conditions
1358
 *      - The warning type is:
1359
 *        - LOG_WARNING     warning conditions 
1360
 *      - The information types are:
1361
 *        - LOG_NOTICE      normal but significant condition
1362
 *        - LOG_INFO        informational
1363
 *        - LOG_DEBUG       debug-level messages
1364
 *
1365
 * @param format is a pointer to a char representing the variable argument list
1366
 *  format used.
1367
 *
1368
 * @param ap is a va_list type used to traverse the list of arguments.
1369
 *
1370
 * @return Returns 0 on success, -1 when the code could not format the log-
1371
 *         string, -2 when dynamic memory could not be allocated if the length
1372
 *         of the log buffer is greater then 1024 bytes.  For each of these
1373
 *         errors a LOG_ERR message is written to the logfile.
1374
 *
1375
 * @see snmp_log
1376
 */
1377
int
1378
snmp_vlog(int priority, const char *format, va_list ap)
1379
141
{
1380
141
    char           *buffer = NULL;
1381
141
    int             length;
1382
1383
141
    length = vasprintf(&buffer, format, ap);
1384
141
    if (length < 0) {
1385
0
        snmp_log_string(LOG_ERR, "Could not format log-string\n");
1386
0
        return -1;
1387
0
    }
1388
1389
141
    snmp_log_string(priority, buffer);
1390
141
    free(buffer);
1391
141
    return 0;
1392
141
}
1393
1394
/**
1395
 * This snmp logging function allows variable argument list given the
1396
 * specified format and priority.  Calls the snmp_vlog function.
1397
 * The default logfile this function writes to is /var/log/snmpd.log.
1398
 *
1399
 * @see snmp_vlog
1400
 */
1401
int
1402
snmp_log(int priority, const char *format, ...)
1403
141
{
1404
141
    va_list         ap;
1405
141
    int             ret;
1406
141
    va_start(ap, format);
1407
141
    ret = snmp_vlog(priority, format, ap);
1408
141
    va_end(ap);
1409
141
    return (ret);
1410
141
}
1411
1412
/*
1413
 * log a critical error.
1414
 */
1415
void
1416
snmp_log_perror(const char *s)
1417
0
{
1418
0
    char           *error = strerror(errno);
1419
0
    if (s) {
1420
0
        if (error)
1421
0
            snmp_log(LOG_ERR, "%s: %s\n", s, error);
1422
0
        else
1423
0
            snmp_log(LOG_ERR, "%s: Error %d out-of-range\n", s, errno);
1424
0
    } else {
1425
0
        if (error)
1426
0
            snmp_log(LOG_ERR, "%s\n", error);
1427
0
        else
1428
0
            snmp_log(LOG_ERR, "Error %d out-of-range\n", errno);
1429
0
    }
1430
0
}
1431
1432
#ifndef NETSNMP_FEATURE_REMOVE_LOGGING_EXTERNAL
1433
/* external access to logh_head variable */
1434
netsnmp_log_handler  *
1435
get_logh_head(void)
1436
0
{
1437
0
  return logh_head;
1438
0
}
1439
#endif /* NETSNMP_FEATURE_REMOVE_LOGGING_EXTERNAL */
1440
1441
/**  @} */