Coverage Report

Created: 2026-08-28 06:51

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/net-snmp/snmplib/read_config.c
Line
Count
Source
1
/*
2
 * read_config.c
3
 */
4
/* Portions of this file are subject to the following copyright(s).  See
5
 * the Net-SNMP's COPYING file for more details and other copyrights
6
 * that may apply:
7
 */
8
/*
9
 * Portions of this file are copyrighted by:
10
 * Copyright © 2003 Sun Microsystems, Inc. All rights reserved.
11
 * Use is subject to license terms specified in the COPYING file
12
 * distributed with the Net-SNMP package.
13
 */
14
15
/** @defgroup read_config parsing various configuration files at run time
16
 *  @ingroup library
17
 *
18
 * The read_config related functions are a fairly extensible  system  of
19
 * parsing various configuration files at the run time.
20
 *
21
 * The idea is that the calling application is able to register
22
 * handlers for certain tokens specified in certain types
23
 * of files.  The read_configs function can then be  called
24
 * to  look  for all the files that it has registrations for,
25
 * find the first word on each line, and pass  the  remainder
26
 * to the appropriately registered handler.
27
 *
28
 * For persistent configuration storage you will need to use the
29
 * read_config_read_data, read_config_store, and read_config_store_data
30
 * APIs in conjunction with first registering a
31
 * callback so when the agent shuts down for whatever reason data is written
32
 * to your configuration files.  The following explains in more detail the
33
 * sequence to make this happen.
34
 *
35
 * This is the callback registration API, you need to call this API with
36
 * the appropriate parameters in order to configure persistent storage needs.
37
 *
38
 *        int snmp_register_callback(int major, int minor,
39
 *                                   SNMPCallback *new_callback,
40
 *                                   void *arg);
41
 *
42
 * You will need to set major to SNMP_CALLBACK_LIBRARY, minor to
43
 * SNMP_CALLBACK_STORE_DATA. arg is whatever you want.
44
 *
45
 * Your callback function's prototype is:
46
 * int     (SNMPCallback) (int majorID, int minorID, void *serverarg,
47
 *                        void *clientarg);
48
 *
49
 * The majorID, minorID and clientarg are what you passed in the callback
50
 * registration above.  When the callback is called you have to essentially
51
 * transfer all your state from memory to disk. You do this by generating
52
 * configuration lines into a buffer.  The lines are of the form token
53
 * followed by token parameters.
54
 * 
55
 * Finally storing is done using read_config_store(type, buffer);
56
 * type is the application name this can be obtained from:
57
 *
58
 * netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_APPTYPE);
59
 *
60
 * Now, reading back the data: This is done by registering a config handler
61
 * for your token using the register_config_handler function. Your
62
 * handler will be invoked and you can parse in the data using the
63
 * read_config_read APIs.
64
 *
65
 *  @{
66
 */
67
#include <net-snmp/net-snmp-config.h>
68
#include <net-snmp/net-snmp-features.h>
69
70
#include <stdio.h>
71
#include <ctype.h>
72
#ifdef HAVE_STDLIB_H
73
#include <stdlib.h>
74
#endif
75
#ifdef HAVE_STRING_H
76
#include <string.h>
77
#else
78
#include <strings.h>
79
#endif
80
#ifdef HAVE_UNISTD_H
81
#include <unistd.h>
82
#endif
83
#include <sys/types.h>
84
#ifdef HAVE_SYS_PARAM_H
85
#include <sys/param.h>
86
#endif
87
#ifdef TIME_WITH_SYS_TIME
88
# include <sys/time.h>
89
# include <time.h>
90
#else
91
# ifdef HAVE_SYS_TIME_H
92
#  include <sys/time.h>
93
# else
94
#  include <time.h>
95
# endif
96
#endif
97
#ifdef HAVE_SYS_STAT_H
98
#include <sys/stat.h>
99
#endif
100
#ifdef HAVE_NETINET_IN_H
101
#include <netinet/in.h>
102
#endif
103
#ifdef HAVE_ARPA_INET_H
104
#include <arpa/inet.h>
105
#endif
106
#ifdef HAVE_SYS_SELECT_H
107
#include <sys/select.h>
108
#endif
109
#ifdef HAVE_SYS_SOCKET_H
110
#include <sys/socket.h>
111
#endif
112
#ifdef HAVE_NETDB_H
113
#include <netdb.h>
114
#endif
115
#include <errno.h>
116
#ifdef HAVE_IO_H
117
#include <io.h>
118
#endif
119
120
#ifdef HAVE_DIRENT_H
121
#include <dirent.h>
122
#endif
123
124
#include <net-snmp/types.h>
125
#include <net-snmp/output_api.h>
126
#include <net-snmp/config_api.h>
127
#include <net-snmp/library/read_config.h>       /* for "internal" definitions */
128
#include <net-snmp/utilities.h>
129
130
#include <net-snmp/library/mib.h>
131
#include <net-snmp/library/parse.h>
132
#include <net-snmp/library/snmp_api.h>
133
#include <net-snmp/library/callback.h>
134
135
netsnmp_feature_child_of(read_config_all, libnetsnmp);
136
137
netsnmp_feature_child_of(unregister_app_config_handler, read_config_all);
138
netsnmp_feature_child_of(read_config_register_app_prenetsnmp_mib_handler, netsnmp_unused);
139
140
static int      config_errors;
141
static int      config_warnings;
142
143
struct config_files *config_files = NULL;
144
145
146
static struct config_line *
147
internal_register_config_handler(const char *type_param,
148
         const char *token,
149
         void (*parser1) (const char *, char *),
150
         void (*parser2) (const char *, const char *),
151
         void (*releaser) (void), const char *help,
152
         int when)
153
29.6k
{
154
29.6k
    struct config_files **ctmp = &config_files;
155
29.6k
    struct config_line  **ltmp;
156
29.6k
    const char           *type = type_param;
157
158
29.6k
    if (type == NULL || *type == '\0') {
159
0
        type = netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID, 
160
0
             NETSNMP_DS_LIB_APPTYPE);
161
0
    }
162
163
    /*
164
     * Handle multiple types (recursively)
165
     */
166
29.6k
    if (strchr(type, ':')) {
167
0
        struct config_line *ltmp2 = NULL;
168
0
        char                buf[STRINGMAX];
169
0
        char               *cptr = buf;
170
171
0
        strlcpy(buf, type, STRINGMAX);
172
0
        while (cptr) {
173
0
            char* c = cptr;
174
0
            cptr = strchr(cptr, ':');
175
0
            if(cptr) {
176
0
                *cptr = '\0';
177
0
                ++cptr;
178
0
            }
179
0
            ltmp2 = internal_register_config_handler(c, token, parser1, parser2,
180
0
                                                     releaser, help, when);
181
0
        }
182
0
        return ltmp2;
183
0
    }
184
    
185
    /*
186
     * Find type in current list  -OR-  create a new file type.
187
     */
188
29.6k
    while (*ctmp != NULL && strcmp((*ctmp)->fileHeader, type)) {
189
0
        ctmp = &((*ctmp)->next);
190
0
    }
191
192
29.6k
    if (*ctmp == NULL) {
193
1
        *ctmp = (struct config_files *)
194
1
            calloc(1, sizeof(struct config_files));
195
1
        if (!*ctmp) {
196
0
            return NULL;
197
0
        }
198
199
1
        (*ctmp)->fileHeader = strdup(type);
200
1
        if (!(*ctmp)->fileHeader) {
201
0
            free(*ctmp);
202
0
            *ctmp = NULL;
203
0
            return NULL;
204
0
        }
205
1
        DEBUGMSGTL(("9:read_config:type", "new type %s\n", type));
206
1
    }
207
208
29.6k
    DEBUGMSGTL(("9:read_config:register_handler", "registering %s %s\n",
209
29.6k
                type, token));
210
    /*
211
     * Find parser type in current list  -OR-  create a new
212
     * line parser entry.
213
     */
214
29.6k
    ltmp = &((*ctmp)->start);
215
216
341k
    while (*ltmp != NULL && strcmp((*ltmp)->config_token, token)) {
217
311k
        ltmp = &((*ltmp)->next);
218
311k
    }
219
220
29.6k
    if (*ltmp == NULL) {
221
22
        *ltmp = (struct config_line *)
222
22
            calloc(1, sizeof(struct config_line));
223
22
        if (!*ltmp) {
224
0
            return NULL;
225
0
        }
226
227
22
        (*ltmp)->config_time = when;
228
22
        (*ltmp)->config_token = strdup(token);
229
22
        if (!(*ltmp)->config_token) {
230
0
            free(*ltmp);
231
0
            *ltmp = NULL;
232
0
            return NULL;
233
0
        }
234
235
22
        if (help != NULL)
236
22
            (*ltmp)->help = strdup(help);
237
22
    }
238
239
    /*
240
     * Add/Replace the parse/free functions for the given line type
241
     * in the given file type.
242
     */
243
29.6k
    (*ltmp)->parse_line1 = parser1;
244
29.6k
    (*ltmp)->parse_line2 = parser2;
245
29.6k
    (*ltmp)->free_func = releaser;
246
247
29.6k
    return (*ltmp);
248
249
29.6k
}                               /* end register_config_handler() */
250
251
struct config_line *
252
register_prenetsnmp_mib_handler(const char *type,
253
                                const char *token,
254
                                void (*parser) (const char *, char *),
255
                                void (*releaser) (void), const char *help)
256
28.3k
{
257
28.3k
    return internal_register_config_handler(type, token, parser, NULL, releaser,
258
28.3k
              help, PREMIB_CONFIG);
259
28.3k
}
260
261
#ifndef NETSNMP_FEATURE_REMOVE_READ_CONFIG_REGISTER_APP_PRENETSNMP_MIB_HANDLER
262
struct config_line *
263
register_app_prenetsnmp_mib_handler(const char *token,
264
                                    void (*parser) (const char *, char *),
265
                                    void (*releaser) (void),
266
                                    const char *help)
267
0
{
268
0
    return (register_prenetsnmp_mib_handler
269
0
            (NULL, token, parser, releaser, help));
270
0
}
271
#endif /* NETSNMP_FEATURE_REMOVE_READ_CONFIG_REGISTER_APP_PRENETSNMP_MIB_HANDLER */
272
273
/**
274
 * register_config_handler registers handlers for certain tokens specified in
275
 * certain types of files.
276
 *
277
 * Allows a module writer use/register multiple configuration files based off
278
 * of the type parameter.  A module writer may want to set up multiple
279
 * configuration files to separate out related tasks/variables or just for
280
 * management of where to put tokens as the module or modules get more complex
281
 * in regard to handling token registrations.
282
 *
283
 * @param type     the configuration file used, e.g., if snmp.conf is the
284
 *                 file where the token is located use "snmp" here.
285
 *                 Multiple colon separated tokens might be used.
286
 *                 If NULL or "" then the configuration file used will be
287
 *                 \<application\>.conf.
288
 *
289
 * @param token    the token being parsed from the file.  Must be non-NULL.
290
 *
291
 * @param parser   the handler function pointer that use  the specified
292
 *                 token and the rest of the line to do whatever is required
293
 *                 Should be non-NULL in order to make use of this API.
294
 *
295
 * @param releaser if non-NULL, the function specified is called when
296
 *                 unregistering config handler or when configuration
297
 *                 files are re-read.
298
 *                 This function should free any resources allocated by
299
 *                 the token handler function.
300
 *
301
 * @param help     if non-NULL, used to display help information on the
302
 *                 expected arguments after the token.
303
 *
304
 * @return Pointer to a new config line entry or NULL on error.
305
 */
306
struct config_line *
307
register_config_handler(const char *type,
308
      const char *token,
309
      void (*parser) (const char *, char *),
310
      void (*releaser) (void), const char *help)
311
1.34k
{
312
1.34k
    return internal_register_config_handler(type, token, parser, NULL, releaser,
313
1.34k
              help, NORMAL_CONFIG);
314
1.34k
}
315
316
struct config_line *
317
register_const_config_handler(const char *type,
318
                              const char *token,
319
                              void (*parser) (const char *, const char *),
320
                              void (*releaser) (void), const char *help)
321
0
{
322
0
    return internal_register_config_handler(type, token, NULL, parser, releaser,
323
0
              help, NORMAL_CONFIG);
324
0
}
325
326
struct config_line *
327
register_app_config_handler(const char *token,
328
                            void (*parser) (const char *, char *),
329
                            void (*releaser) (void), const char *help)
330
0
{
331
0
    return register_config_handler(NULL, token, parser, releaser, help);
332
0
}
333
334
struct config_line *
335
register_const_app_config_handler(const char *token,
336
                                  void (*parser) (const char *, const char *),
337
                                  void (*releaser) (void), const char *help)
338
0
{
339
0
    return register_const_config_handler(NULL, token, parser, releaser, help);
340
0
}
341
342
343
/**
344
 * uregister_config_handler un-registers handlers given a specific type_param
345
 * and token.
346
 *
347
 * @param type_param the configuration file used where the token is located.
348
 *                   Used to lookup the config file entry
349
 * 
350
 * @param token      the token that is being unregistered
351
 *
352
 * @return void
353
 */
354
void
355
unregister_config_handler(const char *type_param, const char *token)
356
0
{
357
0
    struct config_files **ctmp = &config_files;
358
0
    struct config_line  **ltmp;
359
0
    const char           *type = type_param;
360
361
0
    if (type == NULL || *type == '\0') {
362
0
        type = netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID, 
363
0
             NETSNMP_DS_LIB_APPTYPE);
364
0
    }
365
366
    /*
367
     * Handle multiple types (recursively)
368
     */
369
0
    if (strchr(type, ':')) {
370
0
        char                buf[STRINGMAX];
371
0
        char               *cptr = buf;
372
373
0
        strlcpy(buf, type, STRINGMAX);
374
0
        while (cptr) {
375
0
            char* c = cptr;
376
0
            cptr = strchr(cptr, ':');
377
0
            if(cptr) {
378
0
                *cptr = '\0';
379
0
                ++cptr;
380
0
            }
381
0
            unregister_config_handler(c, token);
382
0
        }
383
0
        return;
384
0
    }
385
    
386
    /*
387
     * find type in current list 
388
     */
389
0
    while (*ctmp != NULL && strcmp((*ctmp)->fileHeader, type)) {
390
0
        ctmp = &((*ctmp)->next);
391
0
    }
392
393
0
    if (*ctmp == NULL) {
394
        /*
395
         * Not found, return. 
396
         */
397
0
        return;
398
0
    }
399
400
0
    ltmp = &((*ctmp)->start);
401
0
    if (*ltmp == NULL) {
402
        /*
403
         * Not found, return. 
404
         */
405
0
        return;
406
0
    }
407
0
    if (strcmp((*ltmp)->config_token, token) == 0) {
408
        /*
409
         * found it at the top of the list 
410
         */
411
0
        struct config_line *ltmp2 = (*ltmp)->next;
412
0
        if ((*ltmp)->free_func)
413
0
            (*ltmp)->free_func();
414
0
        SNMP_FREE((*ltmp)->config_token);
415
0
        SNMP_FREE((*ltmp)->help);
416
0
        SNMP_FREE(*ltmp);
417
0
        (*ctmp)->start = ltmp2;
418
0
        return;
419
0
    }
420
0
    while ((*ltmp)->next != NULL
421
0
           && strcmp((*ltmp)->next->config_token, token)) {
422
0
        ltmp = &((*ltmp)->next);
423
0
    }
424
0
    if ((*ltmp)->next != NULL) {
425
0
        struct config_line *ltmp2 = (*ltmp)->next->next;
426
0
        if ((*ltmp)->next->free_func)
427
0
            (*ltmp)->next->free_func();
428
0
        SNMP_FREE((*ltmp)->next->config_token);
429
0
        SNMP_FREE((*ltmp)->next->help);
430
0
        SNMP_FREE((*ltmp)->next);
431
0
        (*ltmp)->next = ltmp2;
432
0
    }
433
0
}
434
435
#ifndef NETSNMP_FEATURE_REMOVE_UNREGISTER_APP_CONFIG_HANDLER
436
void
437
unregister_app_config_handler(const char *token)
438
0
{
439
0
    unregister_config_handler(NULL, token);
440
0
}
441
#endif /* NETSNMP_FEATURE_REMOVE_UNREGISTER_APP_CONFIG_HANDLER */
442
443
void
444
unregister_all_config_handlers(void)
445
0
{
446
0
    struct config_files *ctmp, *save;
447
0
    struct config_line *ltmp;
448
449
    /*
450
     * Keep using config_files until there are no more! 
451
     */
452
0
    for (ctmp = config_files; ctmp;) {
453
0
        for (ltmp = ctmp->start; ltmp; ltmp = ctmp->start) {
454
0
            unregister_config_handler(ctmp->fileHeader,
455
0
                                      ltmp->config_token);
456
0
        }
457
0
        SNMP_FREE(ctmp->fileHeader);
458
0
        save = ctmp->next;
459
0
        SNMP_FREE(ctmp);
460
0
        ctmp = save;
461
0
        config_files = save;
462
0
    }
463
0
}
464
465
#ifdef TESTING
466
void
467
print_config_handlers(void)
468
{
469
    struct config_files *ctmp = config_files;
470
    struct config_line *ltmp;
471
472
    for (; ctmp != NULL; ctmp = ctmp->next) {
473
        DEBUGMSGTL(("read_config", "read_conf: %s\n", ctmp->fileHeader));
474
        for (ltmp = ctmp->start; ltmp != NULL; ltmp = ltmp->next)
475
            DEBUGMSGTL(("read_config", "                   %s\n",
476
                        ltmp->config_token));
477
    }
478
}
479
#endif
480
481
static unsigned int  linecount;
482
static const char   *curfilename;
483
484
struct config_line *
485
read_config_get_handlers(const char *type)
486
2.09k
{
487
2.09k
    struct config_files *ctmp = config_files;
488
2.44k
    for (; ctmp != NULL && strcmp(ctmp->fileHeader, type);
489
2.09k
         ctmp = ctmp->next);
490
2.09k
    if (ctmp)
491
1.74k
        return ctmp->start;
492
350
    return NULL;
493
2.09k
}
494
495
int
496
read_config_with_type_when(const char *filename, const char *type, int when)
497
0
{
498
0
    struct config_line *ctmp = read_config_get_handlers(type);
499
0
    if (ctmp)
500
0
        return read_config(filename, ctmp, when);
501
0
    else
502
0
        DEBUGMSGTL(("read_config",
503
0
                    "read_config: I have no registrations for type:%s,file:%s\n",
504
0
                    type, filename));
505
0
    return SNMPERR_GENERR;     /* No config files read */
506
0
}
507
508
int
509
read_config_with_type(const char *filename, const char *type)
510
0
{
511
0
    return read_config_with_type_when(filename, type, EITHER_CONFIG);
512
0
}
513
514
515
struct config_line *
516
read_config_find_handler(struct config_line *line_handlers,
517
                         const char *token)
518
9.16k
{
519
9.16k
    struct config_line *lptr;
520
521
9.16k
    netsnmp_assert(token);
522
523
171k
    for (lptr = line_handlers; lptr != NULL; lptr = lptr->next) {
524
164k
        if (!strcasecmp(token, lptr->config_token)) {
525
1.95k
            return lptr;
526
1.95k
        }
527
164k
    }
528
7.20k
    return NULL;
529
9.16k
}
530
531
532
/*
533
 * searches a config_line linked list for a match 
534
 */
535
static int
536
run_config_handler(struct config_line *lptr,
537
                   const char *token, char *cptr, int when)
538
9.16k
{
539
9.16k
    char           *cp;
540
541
9.16k
    netsnmp_assert(token);
542
543
9.16k
    lptr = read_config_find_handler(lptr, token);
544
9.16k
    if (lptr != NULL) {
545
1.95k
        if (when == EITHER_CONFIG || lptr->config_time == when) {
546
1.68k
            char tmpbuf[1];
547
1.68k
            DEBUGMSGTL(("read_config:parser",
548
1.68k
                        "Found a parser.  Calling it: %s / %s\n", token,
549
1.68k
                        cptr));
550
            /*
551
             * Make sure cptr is non-null
552
             */
553
1.68k
            if (!cptr) {
554
0
                tmpbuf[0] = '\0';
555
0
                cptr = tmpbuf;
556
0
            }
557
558
            /*
559
             * Stomp on any trailing whitespace
560
             */
561
1.68k
            cp = cptr[0] ? &(cptr[strlen(cptr)-1]) : cptr;
562
1.89k
            while ((cp > cptr) && isspace((unsigned char)(*cp))) {
563
204
                *(cp--) = '\0';
564
204
            }
565
1.68k
            if (lptr->parse_line1)
566
1.68k
                lptr->parse_line1(token, cptr);
567
0
            else
568
0
                lptr->parse_line2(token, cptr);
569
1.68k
        }
570
265
        else
571
265
            DEBUGMSGTL(("9:read_config:parser",
572
1.95k
                        "%s handler not registered for this time\n", token));
573
7.20k
    } else if (when != PREMIB_CONFIG && 
574
7.20k
         !netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, 
575
7.20k
               NETSNMP_DS_LIB_NO_TOKEN_WARNINGS)) {
576
7.20k
  netsnmp_config_warn("Unknown token: %s.", token);
577
7.20k
        return SNMPERR_GENERR;
578
7.20k
    }
579
1.95k
    return SNMPERR_SUCCESS;
580
9.16k
}
581
582
/*
583
 * takes an arbitrary string and tries to interprets it based on the
584
 * known configuration handlers for all registered types.  May produce
585
 * inconsistent results when multiple tokens of the same name are
586
 * registered under different file types. 
587
 */
588
589
/*
590
 * we allow = delimiters here 
591
 */
592
0
#define SNMP_CONFIG_DELIMETERS " \t="
593
594
static int
595
snmp_config_when(char *line, int when)
596
0
{
597
0
    char           *cptr, buf[STRINGMAX];
598
0
    struct config_line *lptr = NULL;
599
0
    struct config_files *ctmp = config_files;
600
0
    char           *st, *start_from, *end;
601
602
0
    if (line == NULL) {
603
0
        config_perror("snmp_config() called with a null string.");
604
0
        return SNMPERR_GENERR;
605
0
    }
606
607
0
    strlcpy(buf, line, STRINGMAX);
608
0
    cptr = strtok_r(buf, SNMP_CONFIG_DELIMETERS, &st);
609
0
    if (!cptr) {
610
0
        netsnmp_config_warn("Wrong format: %s", line);
611
0
        return SNMPERR_GENERR;
612
0
    }
613
0
    if (cptr[0] == '[') {
614
0
        if (cptr[strlen(cptr) - 1] != ']') {
615
0
      netsnmp_config_error("no matching ']' for type %s.", cptr + 1);
616
0
            return SNMPERR_GENERR;
617
0
        }
618
0
        cptr[strlen(cptr) - 1] = '\0';
619
0
        lptr = read_config_get_handlers(cptr + 1);
620
0
        if (lptr == NULL) {
621
0
      netsnmp_config_error("No handlers registered for type %s.",
622
0
         cptr + 1);
623
0
            return SNMPERR_GENERR;
624
0
        }
625
0
        cptr = strtok_r(NULL, SNMP_CONFIG_DELIMETERS, &st);
626
0
        if (!cptr) {
627
0
            netsnmp_config_error("Invalid configuration line %s", line);
628
0
            return SNMPERR_GENERR;
629
0
        }
630
0
        lptr = read_config_find_handler(lptr, cptr);
631
0
    } else {
632
        /*
633
         * we have to find a token 
634
         */
635
0
        for (; ctmp != NULL && lptr == NULL; ctmp = ctmp->next)
636
0
            lptr = read_config_find_handler(ctmp->start, cptr);
637
0
    }
638
0
    if (lptr == NULL && netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, 
639
0
            NETSNMP_DS_LIB_NO_TOKEN_WARNINGS)) {
640
0
  netsnmp_config_warn("Unknown token: %s.", cptr);
641
0
        return SNMPERR_GENERR;
642
0
    }
643
644
    /*
645
     * use the original string instead since strtok_r messed up the original 
646
     */
647
0
    end = line + strlen(line);
648
0
    start_from = line + (cptr - buf) + strlen(cptr) + 1;
649
0
    if (start_from > end)
650
0
        start_from = end;
651
0
    line = skip_white(start_from);
652
653
0
    return (run_config_handler(lptr, cptr, line, when));
654
0
}
655
656
int
657
netsnmp_config(char *line)
658
0
{
659
0
    int             ret = SNMP_ERR_NOERROR;
660
0
    DEBUGMSGTL(("snmp_config", "remembering line \"%s\"\n", line));
661
0
    netsnmp_config_remember(line);      /* always remember it so it's read
662
                                         * processed after a free_config()
663
                                         * call */
664
0
    if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, 
665
0
             NETSNMP_DS_LIB_HAVE_READ_CONFIG)) {
666
0
        DEBUGMSGTL(("snmp_config", "  ... processing it now\n"));
667
0
        ret = snmp_config_when(line, NORMAL_CONFIG);
668
0
    }
669
0
    return ret;
670
0
}
671
672
void
673
netsnmp_config_remember_in_list(char *line,
674
                                struct read_config_memory **mem)
675
0
{
676
0
    if (mem == NULL)
677
0
        return;
678
679
0
    while (*mem != NULL)
680
0
        mem = &((*mem)->next);
681
682
0
    *mem = SNMP_MALLOC_STRUCT(read_config_memory);
683
0
    if (*mem != NULL) {
684
0
        if (line)
685
0
            (*mem)->line = strdup(line);
686
0
    }
687
0
}
688
689
void
690
netsnmp_config_remember_free_list(struct read_config_memory **mem)
691
0
{
692
0
    struct read_config_memory *tmpmem;
693
0
    while (*mem) {
694
0
        SNMP_FREE((*mem)->line);
695
0
        tmpmem = (*mem)->next;
696
0
        SNMP_FREE(*mem);
697
0
        *mem = tmpmem;
698
0
    }
699
0
}
700
701
void
702
netsnmp_config_process_memory_list(struct read_config_memory **memp,
703
                                   int when, int clear)
704
0
{
705
706
0
    struct read_config_memory *mem;
707
708
0
    if (!memp)
709
0
        return;
710
711
0
    mem = *memp;
712
713
0
    while (mem) {
714
0
        DEBUGMSGTL(("read_config:mem", "processing memory: %s\n", mem->line));
715
0
        snmp_config_when(mem->line, when);
716
0
        mem = mem->next;
717
0
    }
718
719
0
    if (clear)
720
0
        netsnmp_config_remember_free_list(memp);
721
0
}
722
723
/*
724
 * default storage location implementation 
725
 */
726
static struct read_config_memory *memorylist = NULL;
727
728
void
729
netsnmp_config_remember(char *line)
730
0
{
731
0
    netsnmp_config_remember_in_list(line, &memorylist);
732
0
}
733
734
void
735
netsnmp_config_process_memories(void)
736
0
{
737
0
    netsnmp_config_process_memory_list(&memorylist, EITHER_CONFIG, 1);
738
0
}
739
740
void
741
netsnmp_config_process_memories_when(int when, int clear)
742
0
{
743
0
    netsnmp_config_process_memory_list(&memorylist, when, clear);
744
0
}
745
746
/*******************************************************************-o-******
747
 * read_config
748
 *
749
 * Parameters:
750
 *  *filename
751
 *  *line_handler
752
 *   when
753
 *
754
 * Read <filename> and process each line in accordance with the list of
755
 * <line_handler> functions.
756
 *
757
 *
758
 * For each line in <filename>, search the list of <line_handler>'s 
759
 * for an entry that matches the first token on the line.  This comparison is
760
 * case insensitive.
761
 *
762
 * For each match, check that <when> is the designated time for the
763
 * <line_handler> function to be executed before processing the line.
764
 *
765
 * Returns SNMPERR_SUCCESS if the file is processed successfully.
766
 * Returns SNMPERR_GENERR  if it cannot.
767
 *    Note that individual config token errors do not trigger SNMPERR_GENERR
768
 *    It's only if the whole file cannot be processed for some reason.
769
 */
770
int
771
read_config(const char *filename,
772
            struct config_line *line_handler, int when)
773
92.6k
{
774
92.6k
    static int      depth = 0;
775
92.6k
    static int      files = 0;
776
777
92.6k
    const char * const prev_filename = curfilename;
778
92.6k
    const unsigned int prev_linecount = linecount;
779
780
92.6k
    FILE           *ifile;
781
92.6k
    char           *line = NULL;  /* current line buffer */
782
92.6k
    size_t          linesize = 0; /* allocated size of line */
783
784
92.6k
    netsnmp_assert(line_handler);
785
92.6k
    netsnmp_assert(line_handler->config_token);
786
787
    /* reset file counter when recursion depth is 0 */
788
92.6k
    if (depth == 0) {
789
1.34k
        files = 0;
790
1.34k
        config_errors = 0;
791
1.34k
        config_warnings = 0;
792
1.34k
    }
793
794
92.6k
    if ((ifile = fopen(filename, "r")) == NULL) {
795
6.06k
#ifdef ENOENT
796
6.06k
        if (errno == ENOENT) {
797
5.95k
            DEBUGMSGTL(("read_config", "%s: %s\n", filename,
798
5.95k
                        strerror(errno)));
799
5.95k
        } else
800
110
#endif                          /* ENOENT */
801
110
#ifdef EACCES
802
110
        if (errno == EACCES) {
803
0
            DEBUGMSGTL(("read_config", "%s: %s\n", filename,
804
0
                        strerror(errno)));
805
0
        } else
806
110
#endif                          /* EACCES */
807
110
        {
808
110
            snmp_log_perror(filename);
809
110
        }
810
6.06k
        return SNMPERR_GENERR;
811
6.06k
    }
812
813
87.0k
#define CONFIG_MAX_FILES 4096
814
86.5k
    if (files > CONFIG_MAX_FILES) {
815
449
        netsnmp_config_error("maximum conf file count (%d) exceeded\n",
816
449
                             CONFIG_MAX_FILES);
817
449
  fclose(ifile);
818
449
        return SNMPERR_GENERR;
819
449
    }
820
86.1k
#define CONFIG_MAX_RECURSE_DEPTH 16
821
86.1k
    if (depth > CONFIG_MAX_RECURSE_DEPTH) {
822
0
        netsnmp_config_error("nested include depth > %d\n",
823
0
                             CONFIG_MAX_RECURSE_DEPTH);
824
0
  fclose(ifile);
825
0
        return SNMPERR_GENERR;
826
0
    }
827
828
86.1k
    linecount = 0;
829
86.1k
    curfilename = filename;
830
831
86.1k
    ++files;
832
86.1k
    ++depth;
833
834
86.1k
    DEBUGMSGTL(("read_config:file", "Reading configuration %s (%d)\n",
835
86.1k
                filename, when));
836
837
296k
    while (ifile) {
838
209k
        size_t              linelen = 0; /* strlen of the current line */
839
209k
        char               *cptr;
840
209k
        struct config_line *lptr = line_handler;
841
842
209k
        if (config_errors + config_warnings > 500) {
843
21
            snmp_log(LOG_ERR, "Too many config errors/warnings. Aborting parsing of %s.\n", filename);
844
21
            fclose(ifile);
845
21
            ifile = NULL;
846
21
            break;
847
21
        }
848
849
276k
        for (;;) {
850
276k
            if (linesize <= linelen + 1) {
851
135k
                char *tmp = realloc(line, linesize + 256);
852
135k
                if (tmp) {
853
135k
                    line = tmp;
854
135k
                    linesize += 256;
855
135k
                } else {
856
0
                    netsnmp_config_error("Failed to allocate memory\n");
857
0
                    free(line);
858
0
                    fclose(ifile);
859
0
                    return SNMPERR_GENERR;
860
0
                }
861
135k
            }
862
276k
            if (fgets(line + linelen, linesize - linelen, ifile) == NULL) {
863
86.0k
                line[linelen] = '\0';
864
86.0k
                fclose (ifile);
865
86.0k
                ifile = NULL;
866
86.0k
                break;
867
86.0k
            }
868
869
190k
            linelen += strlen(line + linelen);
870
871
190k
            if (linelen && line[linelen - 1] == '\n') {
872
123k
              line[linelen - 1] = '\0';
873
123k
              break;
874
123k
            }
875
190k
        }
876
877
209k
        ++linecount;
878
209k
        DEBUGMSGTL(("9:read_config:line", "%s:%d examining: %s\n",
879
209k
                    filename, linecount, line));
880
        /*
881
         * check blank line or # comment 
882
         */
883
209k
        if ((cptr = skip_white(line))) {
884
110k
            char token[STRINGMAX];
885
886
110k
            cptr = copy_nword(cptr, token, sizeof(token));
887
110k
            if (token[0] == '[') {
888
862
                if (token[strlen(token) - 1] != ']') {
889
119
        netsnmp_config_error("no matching ']' for type %s.",
890
119
           &token[1]);
891
119
                    continue;
892
119
                }
893
743
                token[strlen(token) - 1] = '\0';
894
743
                lptr = read_config_get_handlers(&token[1]);
895
743
                if (lptr == NULL) {
896
350
        netsnmp_config_error("No handlers registered for type %s.",
897
350
           &token[1]);
898
350
                    continue;
899
350
                }
900
393
                DEBUGMSGTL(("read_config:context",
901
393
                            "Switching to new context: %s%s\n",
902
393
                            ((cptr) ? "(this line only) " : ""),
903
393
                            &token[1]));
904
393
                if (cptr == NULL) {
905
                    /*
906
                     * change context permanently 
907
                     */
908
194
                    line_handler = lptr;
909
194
                    continue;
910
199
                } else {
911
                    /*
912
                     * the rest of this line only applies. 
913
                     */
914
199
                    cptr = copy_nword(cptr, token, sizeof(token));
915
199
                }
916
109k
            } else if ((token[0] == 'i') && (strncasecmp(token,"include", 7 )==0)) {
917
90.2k
                if ( strcasecmp( token, "include" )==0) {
918
246
                    if (when != PREMIB_CONFIG && 
919
246
                  !netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, 
920
246
                        NETSNMP_DS_LIB_NO_TOKEN_WARNINGS)) {
921
246
                  netsnmp_config_warn("Ambiguous token '%s' - use 'includeSearch' (or 'includeFile') instead.", token);
922
246
                    }
923
246
                    continue;
924
89.9k
                } else if ( strcasecmp( token, "includedir" )==0) {
925
718
                    DIR *d;
926
718
                    struct dirent *entry;
927
718
                    char  fname[SNMP_MAXPATH];
928
718
                    int   len;
929
930
718
                    if (cptr == NULL) {
931
211
                        if (when != PREMIB_CONFIG)
932
211
                netsnmp_config_error("Blank line following %s token.", token);
933
211
                        continue;
934
211
                    }
935
507
                    if ((d=opendir(cptr)) == NULL ) {
936
199
                        if (when != PREMIB_CONFIG)
937
199
                            netsnmp_config_error("Can't open include dir '%s'.", cptr);
938
199
                        continue;
939
199
                    }
940
18.7k
                    while ((entry = readdir( d )) != NULL ) {
941
18.4k
                        if ( entry->d_name[0] != '.') {
942
17.5k
                            len = strlen(entry->d_name);
943
17.5k
                            if ((len > 5) && (strcmp(&(entry->d_name[len-5]),".conf") == 0)) {
944
780
                                snprintf(fname, SNMP_MAXPATH, "%s/%s",
945
780
                                         cptr, entry->d_name);
946
780
                                (void)read_config(fname, line_handler, when);
947
780
                            }
948
17.5k
                        }
949
18.4k
                    }
950
308
                    closedir(d);
951
308
                    continue;
952
89.2k
                } else if ( strcasecmp( token, "includefile" )==0) {
953
86.1k
                    char  fname[SNMP_MAXPATH], *cp;
954
955
86.1k
                    if (cptr == NULL) {
956
198
                        if (when != PREMIB_CONFIG)
957
198
                netsnmp_config_error("Blank line following %s token.", token);
958
198
                        continue;
959
198
                    }
960
85.9k
                    if (strlen(cptr) + 1 >= SNMP_MAXPATH) {
961
216
                        netsnmp_config_error("File name '%s' is too long",
962
216
                                             cptr);
963
216
                        continue;
964
216
                    }
965
85.7k
                    if ( cptr[0] == '/' ) {
966
84.6k
                        strlcpy(fname, cptr, SNMP_MAXPATH);
967
84.6k
                    } else {
968
1.05k
                        strlcpy(fname, filename, SNMP_MAXPATH);
969
1.05k
                        cp = strrchr(fname, '/');
970
1.05k
                        if (!cp)
971
0
                            fname[0] = '\0';
972
1.05k
                        else
973
1.05k
                            *(++cp) = '\0';
974
1.05k
                        strlcat(fname, cptr, SNMP_MAXPATH);
975
1.05k
                    }
976
85.7k
                    if (read_config(fname, line_handler, when) !=
977
85.7k
                        SNMPERR_SUCCESS && when != PREMIB_CONFIG)
978
1.52k
                        netsnmp_config_error("Included file '%s' not found.",
979
1.52k
                                             fname);
980
85.7k
                    continue;
981
85.9k
                } else if ( strcasecmp( token, "includesearch" )==0) {
982
2.58k
                    struct config_files ctmp;
983
2.58k
                    int len, ret;
984
985
2.58k
                    if (cptr == NULL) {
986
196
                        if (when != PREMIB_CONFIG)
987
196
                netsnmp_config_error("Blank line following %s token.", token);
988
196
                        continue;
989
196
                    }
990
2.38k
                    len = strlen(cptr);
991
2.38k
                    ctmp.fileHeader = cptr;
992
2.38k
                    ctmp.start = line_handler;
993
2.38k
                    ctmp.next = NULL;
994
2.38k
                    if ((len > 5) && (strcmp(&cptr[len-5],".conf") == 0))
995
199
                       cptr[len-5] = 0; /* chop off .conf */
996
2.38k
                    ret = read_config_files_of_type(when,&ctmp);
997
2.38k
                    if ((len > 5) && (cptr[len-5] == 0))
998
199
                       cptr[len-5] = '.'; /* restore .conf */
999
2.38k
                    if (( ret != SNMPERR_SUCCESS ) && (when != PREMIB_CONFIG))
1000
2.38k
            netsnmp_config_error("Included config '%s' not found.", cptr);
1001
2.38k
                    continue;
1002
2.58k
                } else {
1003
514
                    lptr = line_handler;
1004
514
                }
1005
90.2k
            } else {
1006
19.2k
                lptr = line_handler;
1007
19.2k
            }
1008
20.0k
            if (cptr == NULL) {
1009
10.8k
    netsnmp_config_error("Blank line following %s token.", token);
1010
10.8k
            } else {
1011
9.16k
                DEBUGMSGTL(("read_config:line", "%s:%d examining: %s\n",
1012
9.16k
                            filename, linecount, line));
1013
9.16k
                run_config_handler(lptr, token, cptr, when);
1014
9.16k
            }
1015
20.0k
        }
1016
209k
    }
1017
86.1k
    free(line);
1018
86.1k
    linecount = prev_linecount;
1019
86.1k
    curfilename = prev_filename;
1020
86.1k
    --depth;
1021
86.1k
    return SNMPERR_SUCCESS;
1022
1023
86.1k
}                               /* end read_config() */
1024
1025
1026
1027
void
1028
free_config(void)
1029
0
{
1030
0
    struct config_files *ctmp = config_files;
1031
0
    struct config_line *ltmp;
1032
1033
0
    for (; ctmp != NULL; ctmp = ctmp->next)
1034
0
        for (ltmp = ctmp->start; ltmp != NULL; ltmp = ltmp->next)
1035
0
            if (ltmp->free_func)
1036
0
                (*(ltmp->free_func)) ();
1037
0
}
1038
1039
/*
1040
 * Return SNMPERR_SUCCESS if any config files are processed
1041
 * Return SNMPERR_GENERR if _no_ config files are processed
1042
 *    Whether this is actually an error is left to the application
1043
 */
1044
int
1045
read_configs_optional(const char *optional_config, int when)
1046
0
{
1047
0
    char *newp, *cp, *st = NULL;
1048
0
    int              ret = SNMPERR_GENERR;
1049
0
    char *type = netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID, 
1050
0
               NETSNMP_DS_LIB_APPTYPE);
1051
1052
0
    if ((NULL == optional_config) || (NULL == type))
1053
0
        return ret;
1054
1055
0
    DEBUGMSGTL(("read_configs_optional",
1056
0
                "reading optional configuration tokens for %s\n", type));
1057
    
1058
0
    newp = strdup(optional_config);      /* strtok_r messes it up */
1059
0
    if (!newp)
1060
0
        return ret;
1061
0
    cp = strtok_r(newp, ",", &st);
1062
0
    while (cp) {
1063
0
        struct stat     statbuf;
1064
0
        if (stat(cp, &statbuf)) {
1065
0
            DEBUGMSGTL(("read_config",
1066
0
                        "Optional File \"%s\" does not exist.\n", cp));
1067
0
            snmp_log_perror(cp);
1068
0
        } else {
1069
0
            DEBUGMSGTL(("read_config:opt",
1070
0
                        "Reading optional config file: \"%s\"\n", cp));
1071
0
            if ( read_config_with_type_when(cp, type, when) == SNMPERR_SUCCESS )
1072
0
                ret = SNMPERR_SUCCESS;
1073
0
        }
1074
0
        cp = strtok_r(NULL, ",", &st);
1075
0
    }
1076
0
    free(newp);
1077
0
    return ret;
1078
0
}
1079
1080
void
1081
read_configs(void)
1082
0
{
1083
0
    char *optional_config = netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID, 
1084
0
                 NETSNMP_DS_LIB_OPTIONALCONFIG);
1085
1086
0
    snmp_call_callbacks(SNMP_CALLBACK_LIBRARY,
1087
0
                        SNMP_CALLBACK_PRE_READ_CONFIG, NULL);
1088
1089
0
    DEBUGMSGTL(("read_config", "reading normal configuration tokens\n"));
1090
1091
0
    if ((NULL != optional_config) && (*optional_config == '-')) {
1092
0
        (void)read_configs_optional(++optional_config, NORMAL_CONFIG);
1093
0
        optional_config = NULL; /* clear, so we don't read them twice */
1094
0
    }
1095
1096
0
    (void)read_config_files(NORMAL_CONFIG);
1097
1098
    /*
1099
     * do this even when the normal above wasn't done 
1100
     */
1101
0
    if (NULL != optional_config)
1102
0
        (void)read_configs_optional(optional_config, NORMAL_CONFIG);
1103
1104
0
    netsnmp_config_process_memories_when(NORMAL_CONFIG, 1);
1105
1106
0
    netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, 
1107
0
         NETSNMP_DS_LIB_HAVE_READ_CONFIG, 1);
1108
0
    snmp_call_callbacks(SNMP_CALLBACK_LIBRARY,
1109
0
                        SNMP_CALLBACK_POST_READ_CONFIG, NULL);
1110
0
}
1111
1112
void
1113
read_premib_configs(void)
1114
0
{
1115
0
    char *optional_config = netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID, 
1116
0
                 NETSNMP_DS_LIB_OPTIONALCONFIG);
1117
1118
0
    snmp_call_callbacks(SNMP_CALLBACK_LIBRARY,
1119
0
                        SNMP_CALLBACK_PRE_PREMIB_READ_CONFIG, NULL);
1120
1121
0
    DEBUGMSGTL(("read_config", "reading premib configuration tokens\n"));
1122
1123
0
    if ((NULL != optional_config) && (*optional_config == '-')) {
1124
0
        (void)read_configs_optional(++optional_config, PREMIB_CONFIG);
1125
0
        optional_config = NULL; /* clear, so we don't read them twice */
1126
0
    }
1127
1128
0
    (void)read_config_files(PREMIB_CONFIG);
1129
1130
0
    if (NULL != optional_config)
1131
0
        (void)read_configs_optional(optional_config, PREMIB_CONFIG);
1132
1133
0
    netsnmp_config_process_memories_when(PREMIB_CONFIG, 0);
1134
1135
0
    netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, 
1136
0
         NETSNMP_DS_LIB_HAVE_READ_PREMIB_CONFIG, 1);
1137
0
    snmp_call_callbacks(SNMP_CALLBACK_LIBRARY,
1138
0
                        SNMP_CALLBACK_POST_PREMIB_READ_CONFIG, NULL);
1139
0
}
1140
1141
/*******************************************************************-o-******
1142
 * set_configuration_directory
1143
 *
1144
 * Parameters:
1145
 *      char *dir - value of the directory
1146
 * Sets the configuration directory. Multiple directories can be
1147
 * specified, but need to be separated by 'ENV_SEPARATOR_CHAR'.
1148
 */
1149
void
1150
set_configuration_directory(const char *dir)
1151
1
{
1152
1
    netsnmp_ds_set_string(NETSNMP_DS_LIBRARY_ID, 
1153
1
        NETSNMP_DS_LIB_CONFIGURATION_DIR, dir);
1154
1
}
1155
1156
/*******************************************************************-o-******
1157
 * get_configuration_directory
1158
 *
1159
 * Parameters: -
1160
 * Retrieve the configuration directory or directories.
1161
 * (For backwards compatibility that is:
1162
 *       SNMPCONFPATH, SNMPSHAREPATH, SNMPLIBPATH, HOME/.snmp
1163
 * First check whether the value is set.
1164
 * If not set give it the default value.
1165
 * Return the value.
1166
 * We always retrieve it new, since we have to do it anyway if it is just set.
1167
 */
1168
const char     *
1169
get_configuration_directory(void)
1170
2.38k
{
1171
2.38k
    char            defaultPath[SPRINT_MAX_LEN];
1172
2.38k
    const char     *homepath;
1173
1174
2.38k
    if (NULL == netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID, 
1175
2.38k
              NETSNMP_DS_LIB_CONFIGURATION_DIR)) {
1176
1
        homepath = netsnmp_gethomedir();
1177
1
        snprintf(defaultPath, sizeof(defaultPath), "%s%c%s%c%s%s%s%s",
1178
1
                SNMPCONFPATH, ENV_SEPARATOR_CHAR,
1179
1
                SNMPSHAREPATH, ENV_SEPARATOR_CHAR, SNMPLIBPATH,
1180
1
                ((homepath == NULL) ? "" : ENV_SEPARATOR),
1181
1
                ((homepath == NULL) ? "" : homepath),
1182
1
                ((homepath == NULL) ? "" : "/.snmp"));
1183
1
        defaultPath[ sizeof(defaultPath)-1 ] = 0;
1184
1
        set_configuration_directory(defaultPath);
1185
1
    }
1186
2.38k
    return (netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID, 
1187
2.38k
          NETSNMP_DS_LIB_CONFIGURATION_DIR));
1188
2.38k
}
1189
1190
/*******************************************************************-o-******
1191
 * set_persistent_directory
1192
 *
1193
 * Parameters:
1194
 *      char *dir - value of the directory
1195
 * Sets the configuration directory. 
1196
 * No multiple directories may be specified.
1197
 * (However, this is not checked)
1198
 */
1199
void
1200
set_persistent_directory(const char *dir)
1201
1
{
1202
1
    netsnmp_ds_set_string(NETSNMP_DS_LIBRARY_ID, 
1203
1
        NETSNMP_DS_LIB_PERSISTENT_DIR, dir);
1204
1
}
1205
1206
/*******************************************************************-o-******
1207
 * get_persistent_directory
1208
 *
1209
 * Parameters: -
1210
 * Function will retrieve the persistent directory value.
1211
 * First check whether the value is set.
1212
 * If not set give it the default value.
1213
 * Return the value. 
1214
 * We always retrieve it new, since we have to do it anyway if it is just set.
1215
 */
1216
const char     *
1217
get_persistent_directory(void)
1218
4.77k
{
1219
4.77k
    if (NULL == netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID, 
1220
4.77k
              NETSNMP_DS_LIB_PERSISTENT_DIR)) {
1221
1
        const char *persdir = netsnmp_getenv("SNMP_PERSISTENT_DIR");
1222
1
        if (NULL == persdir)
1223
1
            persdir = NETSNMP_PERSISTENT_DIRECTORY;
1224
1
        set_persistent_directory(persdir);
1225
1
    }
1226
4.77k
    return (netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID, 
1227
4.77k
          NETSNMP_DS_LIB_PERSISTENT_DIR));
1228
4.77k
}
1229
1230
/*******************************************************************-o-******
1231
 * set_temp_file_pattern
1232
 *
1233
 * Parameters:
1234
 *      char *pattern - value of the file pattern
1235
 * Sets the temp file pattern. 
1236
 * Multiple patterns may not be specified.
1237
 * (However, this is not checked)
1238
 */
1239
void
1240
set_temp_file_pattern(const char *pattern)
1241
0
{
1242
0
    netsnmp_ds_set_string(NETSNMP_DS_LIBRARY_ID, 
1243
0
        NETSNMP_DS_LIB_TEMP_FILE_PATTERN, pattern);
1244
0
}
1245
1246
/*******************************************************************-o-******
1247
 * get_temp_file_pattern
1248
 *
1249
 * Parameters: -
1250
 * Function will retrieve the temp file pattern value.
1251
 * First check whether the value is set.
1252
 * If not set give it the default value.
1253
 * Return the value. 
1254
 * We always retrieve it new, since we have to do it anyway if it is just set.
1255
 */
1256
const char     *
1257
get_temp_file_pattern(void)
1258
0
{
1259
0
    if (NULL == netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID, 
1260
0
              NETSNMP_DS_LIB_TEMP_FILE_PATTERN)) {
1261
0
        set_temp_file_pattern(NETSNMP_TEMP_FILE_PATTERN);
1262
0
    }
1263
0
    return (netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID, 
1264
0
          NETSNMP_DS_LIB_TEMP_FILE_PATTERN));
1265
0
}
1266
1267
/**
1268
 * utility routine for read_config_files
1269
 *
1270
 * Return SNMPERR_SUCCESS if any config files are processed
1271
 * Return SNMPERR_GENERR if _no_ config files are processed
1272
 *    Whether this is actually an error is left to the application
1273
 */
1274
static int
1275
read_config_files_in_path(const char *path, struct config_files *ctmp,
1276
                          int when, const char *perspath, const char *persfile)
1277
4.77k
{
1278
4.77k
    int             done, j;
1279
4.77k
    char            configfile[300];
1280
4.77k
    char           *cptr1, *cptr2, *envconfpath;
1281
4.77k
    struct stat     statbuf;
1282
4.77k
    int             ret = SNMPERR_GENERR;
1283
1284
4.77k
    if ((NULL == path) || (NULL == ctmp))
1285
0
        return SNMPERR_GENERR;
1286
1287
4.77k
    envconfpath = strdup(path);
1288
4.77k
    if (NULL == envconfpath) {
1289
0
        return SNMPERR_GENERR;
1290
0
    }
1291
1292
4.77k
    DEBUGMSGTL(("read_config:path", " config path used for %s:%s (persistent path:%s)\n",
1293
4.77k
                ctmp->fileHeader, envconfpath, perspath));
1294
4.77k
    cptr1 = cptr2 = envconfpath;
1295
4.77k
    done = 0;
1296
14.3k
    while ((!done) && (*cptr2 != 0)) {
1297
324k
        while (*cptr1 != 0 && *cptr1 != ENV_SEPARATOR_CHAR)
1298
312k
            cptr1++;
1299
11.9k
        if (*cptr1 == 0)
1300
4.77k
            done = 1;
1301
7.15k
        else
1302
7.15k
            *cptr1 = 0;
1303
1304
11.9k
        DEBUGMSGTL(("read_config:dir", " config dir: %s\n", cptr2 ));
1305
11.9k
        if (stat(cptr2, &statbuf) != 0) {
1306
            /*
1307
             * Directory not there, continue 
1308
             */
1309
9.54k
            DEBUGMSGTL(("read_config:dir", " Directory not present: %s\n", cptr2 ));
1310
9.54k
            cptr2 = ++cptr1;
1311
9.54k
            continue;
1312
9.54k
        }
1313
2.38k
#ifdef S_ISDIR
1314
2.38k
        if (!S_ISDIR(statbuf.st_mode)) {
1315
            /*
1316
             * Not a directory, continue 
1317
             */
1318
0
            DEBUGMSGTL(("read_config:dir", " Not a directory: %s\n", cptr2 ));
1319
0
            cptr2 = ++cptr1;
1320
0
            continue;
1321
0
        }
1322
2.38k
#endif
1323
1324
        /*
1325
         * for proper persistent storage retrieval, we need to read old backup
1326
         * copies of the previous storage files.  If the application in
1327
         * question has died without the proper call to snmp_clean_persistent,
1328
         * then we read all the configuration files we can, starting with
1329
         * the oldest first.
1330
         */
1331
2.38k
        if (strncmp(cptr2, perspath, strlen(perspath)) == 0 ||
1332
0
            (persfile != NULL &&
1333
2.38k
             strncmp(cptr2, persfile, strlen(persfile)) == 0)) {
1334
2.38k
            DEBUGMSGTL(("read_config:persist", " persist dir: %s\n", cptr2 ));
1335
            /*
1336
             * limit this to the known storage directory only 
1337
             */
1338
2.38k
            for (j = 0; j <= NETSNMP_MAX_PERSISTENT_BACKUPS; j++) {
1339
2.38k
                snprintf(configfile, sizeof(configfile),
1340
2.38k
                         "%s/%s.%d.conf", cptr2,
1341
2.38k
                         ctmp->fileHeader, j);
1342
2.38k
                if (stat(configfile, &statbuf) != 0) {
1343
                    /*
1344
                     * file not there, continue 
1345
                     */
1346
2.38k
                    break;
1347
2.38k
                } else {
1348
                    /*
1349
                     * backup exists, read it 
1350
                     */
1351
0
                    DEBUGMSGTL(("read_config_files",
1352
0
                                "old config file found: %s, parsing\n",
1353
0
                                configfile));
1354
0
                    if (read_config(configfile, ctmp->start, when) == SNMPERR_SUCCESS)
1355
0
                        ret = SNMPERR_SUCCESS;
1356
0
                }
1357
2.38k
            }
1358
2.38k
        }
1359
2.38k
        snprintf(configfile, sizeof(configfile),
1360
2.38k
                 "%s/%s.conf", cptr2, ctmp->fileHeader);
1361
2.38k
        if (read_config(configfile, ctmp->start, when) == SNMPERR_SUCCESS)
1362
0
            ret = SNMPERR_SUCCESS;
1363
2.38k
        snprintf(configfile, sizeof(configfile),
1364
2.38k
                 "%s/%s.local.conf", cptr2, ctmp->fileHeader);
1365
2.38k
        if (read_config(configfile, ctmp->start, when) == SNMPERR_SUCCESS)
1366
0
            ret = SNMPERR_SUCCESS;
1367
1368
2.38k
        if(done)
1369
2.38k
            break;
1370
1371
0
        cptr2 = ++cptr1;
1372
0
    }
1373
4.77k
    SNMP_FREE(envconfpath);
1374
4.77k
    return ret;
1375
4.77k
}
1376
1377
/*******************************************************************-o-******
1378
 * read_config_files
1379
 *
1380
 * Parameters:
1381
 *  when  == PREMIB_CONFIG, NORMAL_CONFIG  -or-  EITHER_CONFIG
1382
 *
1383
 *
1384
 * Traverse the list of config file types, performing the following actions
1385
 * for each --
1386
 *
1387
 * First, build a search path for config files.  If the contents of 
1388
 * environment variable SNMPCONFPATH are NULL, then use the following
1389
 * path list (where the last entry exists only if HOME is non-null):
1390
 *
1391
 *  SNMPSHAREPATH:SNMPLIBPATH:${HOME}/.snmp
1392
 *
1393
 * Then, In each of these directories, read config files by the name of:
1394
 *
1395
 *  <dir>/<fileHeader>.conf   -AND-
1396
 *  <dir>/<fileHeader>.local.conf
1397
 *
1398
 * where <fileHeader> is taken from the config file type structure.
1399
 *
1400
 *
1401
 * PREMIB_CONFIG causes free_config() to be invoked prior to any other action.
1402
 *
1403
 *
1404
 * EXITs if any 'config_errors' are logged while parsing config file lines.
1405
 *
1406
 * Return SNMPERR_SUCCESS if any config files are processed
1407
 * Return SNMPERR_GENERR if _no_ config files are processed
1408
 *    Whether this is actually an error is left to the application
1409
 */
1410
int
1411
read_config_files_of_type(int when, struct config_files *ctmp)
1412
2.38k
{
1413
2.38k
    const char     *confpath, *persfile, *envconfpath;
1414
2.38k
    char           *perspath;
1415
2.38k
    int             ret = SNMPERR_GENERR;
1416
1417
2.38k
    if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID,
1418
2.38k
                               NETSNMP_DS_LIB_DONT_PERSIST_STATE)
1419
2.38k
        || netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID,
1420
2.38k
                                  NETSNMP_DS_LIB_DISABLE_CONFIG_LOAD)
1421
2.38k
        || (NULL == ctmp)) return ret;
1422
1423
    /*
1424
     * these shouldn't change
1425
     */
1426
2.38k
    confpath = get_configuration_directory();
1427
2.38k
    persfile = netsnmp_getenv("SNMP_PERSISTENT_FILE");
1428
2.38k
    envconfpath = netsnmp_getenv("SNMPCONFPATH");
1429
1430
1431
        /*
1432
         * read the config files. strdup() the result of
1433
         * get_persistent_directory() to avoid that parsing the "persistentDir"
1434
         * keyword transforms the perspath pointer into a dangling pointer.
1435
         */
1436
2.38k
        perspath = strdup(get_persistent_directory());
1437
2.38k
        if (perspath == NULL) {
1438
0
            return SNMPERR_GENERR;
1439
0
        }
1440
2.38k
        if (envconfpath == NULL) {
1441
            /*
1442
             * read just the config files (no persistent stuff), since
1443
             * persistent path can change via conf file. Then get the
1444
             * current persistent directory, and read files there.
1445
             */
1446
2.38k
            if ( read_config_files_in_path(confpath, ctmp, when, perspath,
1447
2.38k
                                      persfile) == SNMPERR_SUCCESS )
1448
0
                ret = SNMPERR_SUCCESS;
1449
2.38k
            free(perspath);
1450
2.38k
            perspath = strdup(get_persistent_directory());
1451
2.38k
            if (perspath == NULL) {
1452
0
                return SNMPERR_GENERR;
1453
0
            }
1454
2.38k
            if ( read_config_files_in_path(perspath, ctmp, when, perspath,
1455
2.38k
                                      persfile) == SNMPERR_SUCCESS )
1456
0
                ret = SNMPERR_SUCCESS;
1457
2.38k
        }
1458
0
        else {
1459
            /*
1460
             * only read path specified by user
1461
             */
1462
0
            if ( read_config_files_in_path(envconfpath, ctmp, when, perspath,
1463
0
                                      persfile) == SNMPERR_SUCCESS )
1464
0
                ret = SNMPERR_SUCCESS;
1465
0
        }
1466
2.38k
        free(perspath);
1467
2.38k
        return ret;
1468
2.38k
}
1469
1470
/*
1471
 * Return SNMPERR_SUCCESS if any config files are processed
1472
 * Return SNMPERR_GENERR if _no_ config files are processed
1473
 *    Whether this is actually an error is left to the application
1474
 */
1475
int
1476
0
read_config_files(int when) {
1477
1478
0
    struct config_files *ctmp = config_files;
1479
0
    int                  ret  = SNMPERR_GENERR;
1480
1481
0
    config_errors = 0;
1482
1483
0
    if (when == PREMIB_CONFIG)
1484
0
        free_config();
1485
1486
    /*
1487
     * read all config file types 
1488
     */
1489
0
    for (; ctmp != NULL; ctmp = ctmp->next) {
1490
0
        if ( read_config_files_of_type(when, ctmp) == SNMPERR_SUCCESS )
1491
0
            ret = SNMPERR_SUCCESS;
1492
0
    }
1493
1494
0
    if (config_errors) {
1495
0
        snmp_log(LOG_ERR, "net-snmp: %d error(s) in config file(s)\n",
1496
0
                 config_errors);
1497
0
    }
1498
0
    return ret;
1499
0
}
1500
1501
void
1502
read_config_print_usage(const char *lead)
1503
0
{
1504
0
    struct config_files *ctmp;
1505
0
    struct config_line *ltmp;
1506
1507
0
    if (lead == NULL)
1508
0
        lead = "";
1509
1510
0
    for (ctmp = config_files; ctmp != NULL; ctmp = ctmp->next) {
1511
0
        snmp_log(LOG_INFO, "%sIn %s.conf and %s.local.conf:\n", lead,
1512
0
                 ctmp->fileHeader, ctmp->fileHeader);
1513
0
        for (ltmp = ctmp->start; ltmp != NULL; ltmp = ltmp->next) {
1514
0
            DEBUGIF("read_config_usage") {
1515
0
                if (ltmp->config_time == PREMIB_CONFIG)
1516
0
                    DEBUGMSG(("read_config_usage", "*"));
1517
0
                else
1518
0
                    DEBUGMSG(("read_config_usage", " "));
1519
0
            }
1520
0
            if (ltmp->help) {
1521
0
                snmp_log(LOG_INFO, "%s%s%-24s %s\n", lead, lead,
1522
0
                         ltmp->config_token, ltmp->help);
1523
0
            } else {
1524
0
                DEBUGIF("read_config_usage") {
1525
0
                    snmp_log(LOG_INFO, "%s%s%-24s [NO HELP]\n", lead, lead,
1526
0
                             ltmp->config_token);
1527
0
                }
1528
0
            }
1529
0
        }
1530
0
    }
1531
0
}
1532
1533
/**
1534
 * read_config_store intended for use by applications to store permanent
1535
 * configuration information generated by sets or persistent counters.
1536
 * Appends line to a file named either ENV(SNMP_PERSISTENT_FILE) or
1537
 *   "<NETSNMP_PERSISTENT_DIRECTORY>/<type>.conf".
1538
 * Adds a trailing newline to the stored file if necessary.
1539
 *
1540
 * @param type is the application name
1541
 * @param line is the configuration line written to the application name's
1542
 * configuration file
1543
 *      
1544
 * @return void
1545
  */
1546
void
1547
read_config_store(const char *type, const char *line)
1548
0
{
1549
0
#ifdef NETSNMP_PERSISTENT_DIRECTORY
1550
0
    char            file[512], *filep;
1551
0
    FILE           *fout;
1552
0
#ifdef NETSNMP_PERSISTENT_MASK
1553
0
    mode_t          oldmask;
1554
0
#endif
1555
1556
0
    if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID,
1557
0
                               NETSNMP_DS_LIB_DONT_PERSIST_STATE)
1558
0
     || netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID,
1559
0
                               NETSNMP_DS_LIB_DISABLE_PERSISTENT_LOAD)) return;
1560
1561
    /*
1562
     * store configuration directives in the following order of preference:
1563
     * 1. ENV variable SNMP_PERSISTENT_FILE
1564
     * 2. configured <NETSNMP_PERSISTENT_DIRECTORY>/<type>.conf
1565
     */
1566
0
    if ((filep = netsnmp_getenv("SNMP_PERSISTENT_FILE")) == NULL) {
1567
0
        snprintf(file, sizeof(file),
1568
0
                 "%s/%s.conf", get_persistent_directory(), type);
1569
0
        filep = file;
1570
0
    }
1571
0
#ifdef NETSNMP_PERSISTENT_MASK
1572
0
    oldmask = umask(NETSNMP_PERSISTENT_MASK);
1573
0
#endif
1574
0
    if (mkdirhier(filep, NETSNMP_AGENT_DIRECTORY_MODE, 1)) {
1575
0
        snmp_log(LOG_ERR,
1576
0
                 "Failed to create the persistent directory for %s\n",
1577
0
                 file);
1578
0
    }
1579
0
    if ((fout = fopen(filep, "a")) != NULL) {
1580
0
        fprintf(fout, "%s", line);
1581
0
        if (line[strlen(line)] != '\n')
1582
0
            fprintf(fout, "\n");
1583
0
        DEBUGMSGTL(("read_config:store", "storing: %s\n", line));
1584
0
        fflush(fout);
1585
0
#if defined(HAVE_FSYNC)
1586
0
        fsync(fileno(fout));
1587
#elif defined(HAVE__GET_OSFHANDLE)
1588
        {
1589
            int fd;
1590
            HANDLE h;
1591
1592
            fd = fileno(fout);
1593
            netsnmp_assert(fd != -1);
1594
            /*
1595
             * Use size_t instead of uintptr_t because not all supported
1596
             * Windows compilers support uintptr_t.
1597
             */
1598
            h = (HANDLE)(size_t)_get_osfhandle(fd);
1599
            netsnmp_assert(h != INVALID_HANDLE_VALUE);
1600
            FlushFileBuffers(h);
1601
        }
1602
#endif
1603
0
        fclose(fout);
1604
0
    } else {
1605
0
        if (strcmp(NETSNMP_APPLICATION_CONFIG_TYPE, type) != 0) {
1606
            /*
1607
             * Ignore this error in client utilities, they can run with random
1608
             * UID/GID and typically cannot write to /var. Error message just
1609
             * confuses people.
1610
             */
1611
0
            snmp_log(LOG_ERR, "read_config_store open failure on %s\n", filep);
1612
0
        }
1613
0
    }
1614
0
#ifdef NETSNMP_PERSISTENT_MASK
1615
0
    umask(oldmask);
1616
0
#endif
1617
1618
0
#endif
1619
0
}                               /* end read_config_store() */
1620
1621
void
1622
read_app_config_store(const char *line)
1623
0
{
1624
0
    read_config_store(netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID, 
1625
0
              NETSNMP_DS_LIB_APPTYPE), line);
1626
0
}
1627
1628
1629
1630
1631
/*******************************************************************-o-******
1632
 * snmp_save_persistent
1633
 *
1634
 * Parameters:
1635
 *  *type
1636
 *      
1637
 *
1638
 * Save the file "<NETSNMP_PERSISTENT_DIRECTORY>/<type>.conf" into a backup copy
1639
 * called "<NETSNMP_PERSISTENT_DIRECTORY>/<type>.%d.conf", which %d is an
1640
 * incrementing number on each call, but less than NETSNMP_MAX_PERSISTENT_BACKUPS.
1641
 *
1642
 * Should be called just before all persistent information is supposed to be
1643
 * written to move aside the existing persistent cache.
1644
 * snmp_clean_persistent should then be called afterward all data has been
1645
 * saved to remove these backup files.
1646
 *
1647
 * Note: on an rename error, the files are removed rather than saved.
1648
 *
1649
 */
1650
void
1651
snmp_save_persistent(const char *type)
1652
0
{
1653
0
    char            file[512], fileold[SPRINT_MAX_LEN];
1654
0
    struct stat     statbuf;
1655
0
    int             j;
1656
1657
0
    if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID,
1658
0
                               NETSNMP_DS_LIB_DONT_PERSIST_STATE)
1659
0
     || netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID,
1660
0
                               NETSNMP_DS_LIB_DISABLE_PERSISTENT_SAVE)) return;
1661
1662
0
    DEBUGMSGTL(("snmp_save_persistent", "saving %s files...\n", type));
1663
0
    snprintf(file, sizeof(file),
1664
0
             "%s/%s.conf", get_persistent_directory(), type);
1665
0
    if (stat(file, &statbuf) == 0) {
1666
0
        for (j = 0; j <= NETSNMP_MAX_PERSISTENT_BACKUPS; j++) {
1667
0
            snprintf(fileold, sizeof(fileold),
1668
0
                     "%s/%s.%d.conf", get_persistent_directory(), type, j);
1669
0
            if (stat(fileold, &statbuf) != 0) {
1670
0
                DEBUGMSGTL(("snmp_save_persistent",
1671
0
                            " saving old config file: %s -> %s.\n", file,
1672
0
                            fileold));
1673
0
                if (rename(file, fileold)) {
1674
0
                    snmp_log(LOG_ERR, "Cannot rename %s to %s\n", file, fileold);
1675
                     /* moving it failed, try nuking it, as leaving
1676
                      * it around is very bad. */
1677
0
                    if (unlink(file) == -1)
1678
0
                        snmp_log(LOG_ERR, "Cannot unlink %s\n", file);
1679
0
                }
1680
0
                break;
1681
0
            }
1682
0
        }
1683
0
    }
1684
    /*
1685
     * save a warning header to the top of the new file 
1686
     */
1687
0
    snprintf(fileold, sizeof(fileold),
1688
0
            "%s%s# Please save normal configuration tokens for %s in SNMPCONFPATH/%s.conf.\n# Only \"createUser\" tokens should be placed here by %s administrators.\n%s",
1689
0
            "#\n# net-snmp (or ucd-snmp) persistent data file.\n#\n############################################################################\n# STOP STOP STOP STOP STOP STOP STOP STOP STOP \n",
1690
0
            "#\n#          **** DO NOT EDIT THIS FILE ****\n#\n# STOP STOP STOP STOP STOP STOP STOP STOP STOP \n############################################################################\n#\n# DO NOT STORE CONFIGURATION ENTRIES HERE.\n",
1691
0
            type, type, type,
1692
0
      "# (Did I mention: do not edit this file?)\n#\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
1693
0
    read_config_store(type, fileold);
1694
0
}
1695
1696
1697
/*******************************************************************-o-******
1698
 * snmp_clean_persistent
1699
 *
1700
 * Parameters:
1701
 *  *type
1702
 *      
1703
 *
1704
 * Unlink all backup files called "<NETSNMP_PERSISTENT_DIRECTORY>/<type>.%d.conf".
1705
 *
1706
 * Should be called just after we successfull dumped the last of the
1707
 * persistent data, to remove the backup copies of previous storage dumps.
1708
 *
1709
 * XXX  Worth overwriting with random bytes first?  This would
1710
 *  ensure that the data is destroyed, even a buffer containing the
1711
 *  data persists in memory or swap.  Only important if secrets
1712
 *  will be stored here.
1713
 */
1714
void
1715
snmp_clean_persistent(const char *type)
1716
0
{
1717
0
    char            file[512];
1718
0
    struct stat     statbuf;
1719
0
    int             j;
1720
1721
0
    if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID,
1722
0
                               NETSNMP_DS_LIB_DONT_PERSIST_STATE)
1723
0
     || netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID,
1724
0
                               NETSNMP_DS_LIB_DISABLE_PERSISTENT_SAVE)) return;
1725
1726
0
    DEBUGMSGTL(("snmp_clean_persistent", "cleaning %s files...\n", type));
1727
0
    snprintf(file, sizeof(file),
1728
0
             "%s/%s.conf", get_persistent_directory(), type);
1729
0
    if (stat(file, &statbuf) == 0) {
1730
0
        for (j = 0; j <= NETSNMP_MAX_PERSISTENT_BACKUPS; j++) {
1731
0
            snprintf(file, sizeof(file),
1732
0
                     "%s/%s.%d.conf", get_persistent_directory(), type, j);
1733
0
            if (stat(file, &statbuf) == 0) {
1734
0
                DEBUGMSGTL(("snmp_clean_persistent",
1735
0
                            " removing old config file: %s\n", file));
1736
0
                if (unlink(file) == -1)
1737
0
                    snmp_log(LOG_ERR, "Cannot unlink %s\n", file);
1738
0
            }
1739
0
        }
1740
0
    }
1741
0
}
1742
1743
1744
1745
1746
/*
1747
 * config_perror: prints a warning string associated with a file and
1748
 * line number of a .conf file and increments the error count. 
1749
 */
1750
static void
1751
config_vlog(int level, const char *levelmsg, const char *str, va_list args)
1752
24.1k
{
1753
24.1k
    char *buf;
1754
1755
24.1k
    if (asprintf(&buf, "%s: line %d: %s: %s\n",
1756
24.1k
                 curfilename, linecount, levelmsg, str) < 0)
1757
0
        return;
1758
24.1k
    snmp_vlog(level, buf, args);
1759
24.1k
    free(buf);
1760
24.1k
}
1761
1762
void
1763
netsnmp_config_error(const char *str, ...)
1764
16.6k
{
1765
16.6k
    va_list args;
1766
16.6k
    va_start(args, str);
1767
16.6k
    config_vlog(LOG_ERR, "Error", str, args);
1768
16.6k
    va_end(args);
1769
16.6k
    config_errors++;
1770
16.6k
}
1771
1772
void
1773
netsnmp_config_warn(const char *str, ...)
1774
7.45k
{
1775
7.45k
    va_list args;
1776
7.45k
    va_start(args, str);
1777
7.45k
    config_vlog(LOG_WARNING, "Warning", str, args);
1778
7.45k
    va_end(args);
1779
7.45k
    config_warnings++;
1780
7.45k
}
1781
1782
void
1783
config_perror(const char *str)
1784
0
{
1785
0
    netsnmp_config_error("%s", str);
1786
0
}
1787
1788
void
1789
config_pwarn(const char *str)
1790
0
{
1791
0
    netsnmp_config_warn("%s", str);
1792
0
}
1793
1794
/*
1795
 * skip all white spaces and return 1 if found something either end of
1796
 * line or a comment character 
1797
 */
1798
char           *
1799
skip_white(char *ptr)
1800
209k
{
1801
209k
    return NETSNMP_REMOVE_CONST(char *, skip_white_const(ptr));
1802
209k
}
1803
1804
const char     *
1805
skip_white_const(const char *ptr)
1806
320k
{
1807
320k
    if (ptr == NULL)
1808
0
        return (NULL);
1809
433k
    while (*ptr != 0 && isspace((unsigned char)*ptr))
1810
113k
        ptr++;
1811
320k
    if (*ptr == 0 || *ptr == '#')
1812
111k
        return (NULL);
1813
208k
    return (ptr);
1814
320k
}
1815
1816
char           *
1817
skip_not_white(char *ptr)
1818
0
{
1819
0
    return NETSNMP_REMOVE_CONST(char *, skip_not_white_const(ptr));
1820
0
}
1821
1822
const char     *
1823
skip_not_white_const(const char *ptr)
1824
0
{
1825
0
    if (ptr == NULL)
1826
0
        return (NULL);
1827
0
    while (*ptr != 0 && !isspace((unsigned char)*ptr))
1828
0
        ptr++;
1829
0
    if (*ptr == 0 || *ptr == '#')
1830
0
        return (NULL);
1831
0
    return (ptr);
1832
0
}
1833
1834
char           *
1835
skip_token(char *ptr)
1836
0
{
1837
0
    return NETSNMP_REMOVE_CONST(char *, skip_token_const(ptr));
1838
0
}
1839
1840
const char     *
1841
skip_token_const(const char *ptr)
1842
0
{
1843
0
    ptr = skip_white_const(ptr);
1844
0
    ptr = skip_not_white_const(ptr);
1845
0
    ptr = skip_white_const(ptr);
1846
0
    return (ptr);
1847
0
}
1848
1849
/*
1850
 * copy_word
1851
 * copies the next 'token' from 'from' into 'to', maximum len-1 characters.
1852
 * currently a token is anything separate by white space
1853
 * or within quotes (double or single) (i.e. "the red rose" 
1854
 * is one token, \"the red rose\" is three tokens)
1855
 * a '\' character will allow a quote character to be treated
1856
 * as a regular character 
1857
 * It returns a pointer to first non-white space after the end of the token
1858
 * being copied or to 0 if we reach the end.
1859
 * Note: Partially copied words (greater than len) still returns a !NULL ptr
1860
 * Note: partially copied words are, however, null terminated.
1861
 */
1862
1863
char           *
1864
copy_nword(char *from, char *to, int len)
1865
110k
{
1866
110k
    return NETSNMP_REMOVE_CONST(char *, copy_nword_const(from, to, len));
1867
110k
}
1868
1869
const char           *
1870
copy_nword_const(const char *from, char *to, int len)
1871
110k
{
1872
110k
    char            quote;
1873
110k
    if (!from || !to)
1874
0
        return NULL;
1875
110k
    if ((*from == '\"') || (*from == '\'')) {
1876
1.41k
        quote = *(from++);
1877
439k
        while ((*from != quote) && (*from != 0)) {
1878
438k
            if ((*from == '\\') && (*(from + 1) != 0)) {
1879
761
                if (len > 0) {  /* don't copy beyond len bytes */
1880
560
                    *to++ = *(from + 1);
1881
560
                    if (--len == 0)
1882
194
                        *(to - 1) = '\0';       /* null protect the last spot */
1883
560
                }
1884
761
                from = from + 2;
1885
437k
            } else {
1886
437k
                if (len > 0) {  /* don't copy beyond len bytes */
1887
430k
                    *to++ = *from++;
1888
430k
                    if (--len == 0)
1889
212
                        *(to - 1) = '\0';       /* null protect the last spot */
1890
430k
                } else
1891
7.24k
                    from++;
1892
437k
            }
1893
438k
        }
1894
1.41k
        if (*from == 0) {
1895
1.19k
            DEBUGMSGTL(("read_config_copy_word",
1896
1.19k
                        "no end quote found in config string\n"));
1897
1.19k
        } else
1898
219
            from++;
1899
109k
    } else {
1900
3.87M
        while (*from != 0 && !isspace((unsigned char)(*from))) {
1901
3.76M
            if ((*from == '\\') && (*(from + 1) != 0)) {
1902
17.4k
                if (len > 0) {  /* don't copy beyond len bytes */
1903
14.1k
                    *to++ = *(from + 1);
1904
14.1k
                    if (--len == 0)
1905
196
                        *(to - 1) = '\0';       /* null protect the last spot */
1906
14.1k
                }
1907
17.4k
                from = from + 2;
1908
3.74M
            } else {
1909
3.74M
                if (len > 0) {  /* don't copy beyond len bytes */
1910
1.80M
                    *to++ = *from++;
1911
1.80M
                    if (--len == 0)
1912
234
                        *(to - 1) = '\0';       /* null protect the last spot */
1913
1.80M
                } else
1914
1.94M
                    from++;
1915
3.74M
            }
1916
3.76M
        }
1917
109k
    }
1918
110k
    if (len > 0)
1919
109k
        *to = 0;
1920
110k
    from = skip_white_const(from);
1921
110k
    return (from);
1922
110k
}                               /* copy_nword */
1923
1924
/*
1925
 * copy_word
1926
 * copies the next 'token' from 'from' into 'to'.
1927
 * currently a token is anything separate by white space
1928
 * or within quotes (double or single) (i.e. "the red rose" 
1929
 * is one token, \"the red rose\" is three tokens)
1930
 * a '\' character will allow a quote character to be treated
1931
 * as a regular character 
1932
 * It returns a pointer to first non-white space after the end of the token
1933
 * being copied or to 0 if we reach the end.
1934
 */
1935
1936
static int      have_warned = 0;
1937
char           *
1938
copy_word(char *from, char *to)
1939
0
{
1940
0
    if (!have_warned) {
1941
0
        snmp_log(LOG_INFO,
1942
0
                 "copy_word() called.  Use copy_nword() instead.\n");
1943
0
        have_warned = 1;
1944
0
    }
1945
0
    return copy_nword(from, to, SPRINT_MAX_LEN);
1946
0
}                               /* copy_word */
1947
1948
/**
1949
 * Stores an quoted version of the first len bytes from str into saveto.
1950
 *
1951
 * If all octets in str are in the set [[:alnum:] ] then the quotation
1952
 * is to enclose the string in quotation marks ("str") otherwise the
1953
 * quotation is to prepend the string 0x and then add the hex representation
1954
 * of all characters from str (0x737472)
1955
 *
1956
 * @param[in] saveto pointer to output stream, is assumed to be big enough.
1957
 * @param[in] str pointer of the data that is to be stored.
1958
 * @param[in] len length of the data that is to be stored.
1959
 * @return A pointer to saveto after str is added to it.
1960
 */
1961
char           *
1962
read_config_save_octet_string(char *saveto, const u_char * str, size_t len)
1963
0
{
1964
0
    size_t          i;
1965
0
    const u_char   *cp;
1966
1967
    /*
1968
     * is everything easily printable
1969
     */
1970
0
    for (i = 0, cp = str; i < len && cp &&
1971
0
         (isalpha(*cp) || isdigit(*cp) || *cp == ' '); cp++, i++);
1972
1973
0
    if (len != 0 && i == len) {
1974
0
        *saveto++ = '"';
1975
0
        memcpy(saveto, str, len);
1976
0
        saveto += len;
1977
0
        *saveto++ = '"';
1978
0
        *saveto = '\0';
1979
0
    } else {
1980
0
        if (str != NULL) {
1981
0
            sprintf(saveto, "0x");
1982
0
            saveto += 2;
1983
0
            for (i = 0; i < len; i++) {
1984
0
                sprintf(saveto, "%02x", str[i]);
1985
0
                saveto = saveto + 2;
1986
0
            }
1987
0
        } else {
1988
0
            sprintf(saveto, "\"\"");
1989
0
            saveto += 2;
1990
0
        }
1991
0
    }
1992
0
    return saveto;
1993
0
}
1994
1995
/**
1996
 * Reads an octet string that was saved by the
1997
 * read_config_save_octet_string() function.
1998
 *
1999
 * @param[in]     readfrom Pointer to the input data to be parsed.
2000
 * @param[in,out] str      Pointer to the output buffer pointer. The data
2001
 *   written to the output buffer will be '\0'-terminated. If *str == NULL,
2002
 *   an output buffer will be allocated that is one byte larger than the
2003
 *   data stored.
2004
 * @param[in,out] len      If str != NULL, *len is the size of the buffer *str
2005
 *   points at. If str == NULL, the value passed via *len is ignored.
2006
 *   Before this function returns the number of bytes read will be stored
2007
 *   in *len. If a buffer overflow occurs, *len will be set to 0.
2008
 *
2009
 * @return A pointer to the next character in the input to be parsed if
2010
 *   parsing succeeded; NULL when the end of the input string has been reached
2011
 *   or if an error occurred.
2012
 */
2013
char           *
2014
read_config_read_octet_string(const char *readfrom, u_char ** str,
2015
                              size_t * len)
2016
0
{
2017
0
    return NETSNMP_REMOVE_CONST(char *,
2018
0
               read_config_read_octet_string_const(readfrom, str, len));
2019
0
}
2020
2021
const char     *
2022
read_config_read_octet_string_const(const char *readfrom, u_char ** str,
2023
                                    size_t * len)
2024
0
{
2025
0
    u_char         *cptr;
2026
0
    const char     *cptr1;
2027
0
    u_int           tmp;
2028
0
    size_t          i, ilen;
2029
2030
0
    if (readfrom == NULL || str == NULL || len == NULL)
2031
0
        return NULL;
2032
2033
0
    if (strncasecmp(readfrom, "0x", 2) == 0) {
2034
        /*
2035
         * A hex string submitted. How long? 
2036
         */
2037
0
        readfrom += 2;
2038
0
        cptr1 = skip_not_white_const(readfrom);
2039
0
        if (cptr1)
2040
0
            ilen = (cptr1 - readfrom);
2041
0
        else
2042
0
            ilen = strlen(readfrom);
2043
2044
0
        if (ilen % 2) {
2045
0
            snmp_log(LOG_WARNING,"invalid hex string: wrong length\n");
2046
0
            DEBUGMSGTL(("read_config_read_octet_string",
2047
0
                        "invalid hex string: wrong length\n"));
2048
0
            return NULL;
2049
0
        }
2050
0
        ilen = ilen / 2;
2051
2052
        /*
2053
         * malloc data space if needed (+1 for good measure) 
2054
         */
2055
0
        if (*str == NULL) {
2056
0
            *str = (u_char *) malloc(ilen + 1);
2057
0
            if (!*str)
2058
0
                return NULL;
2059
0
        } else {
2060
            /*
2061
             * require caller to have +1, and bail if not enough space.
2062
             */
2063
0
            if (ilen >= *len) {
2064
0
                snmp_log(LOG_WARNING,"buffer too small to read octet string (%lu < %lu)\n",
2065
0
                         (unsigned long)*len, (unsigned long)ilen);
2066
0
                DEBUGMSGTL(("read_config_read_octet_string",
2067
0
                            "buffer too small (%lu < %lu)\n", (unsigned long)*len, (unsigned long)ilen));
2068
0
                *len = 0;
2069
0
                cptr1 = skip_not_white_const(readfrom);
2070
0
                return skip_white_const(cptr1);
2071
0
            }
2072
0
        }
2073
2074
        /*
2075
         * copy validated data 
2076
         */
2077
0
        *len = ilen;
2078
0
        cptr = *str;
2079
0
        for (i = 0; i < ilen; i++) {
2080
0
            if (1 == sscanf(readfrom, "%2x", &tmp))
2081
0
                *cptr++ = (u_char) tmp;
2082
0
            else {
2083
                /*
2084
                 * we may lose memory, but don't know caller's buffer XX free(cptr); 
2085
                 */
2086
0
                return (NULL);
2087
0
            }
2088
0
            readfrom += 2;
2089
0
        }
2090
        /*
2091
         * Terminate the output buffer.
2092
         */
2093
0
        *cptr++ = '\0';
2094
0
        readfrom = skip_white_const(readfrom);
2095
0
    } else {
2096
        /*
2097
         * Normal string 
2098
         */
2099
2100
        /*
2101
         * malloc string space if needed (including NULL terminator) 
2102
         */
2103
0
        if (*str == NULL) {
2104
0
            char            buf[SNMP_MAXBUF];
2105
0
            readfrom = copy_nword_const(readfrom, buf, sizeof(buf));
2106
2107
0
            *len = strlen(buf);
2108
0
            *str = (u_char *) malloc(*len + 1);
2109
0
            if (*str == NULL)
2110
0
                return NULL;
2111
0
            memcpy(*str, buf, *len + 1);
2112
0
        } else {
2113
0
            readfrom = copy_nword_const(readfrom, (char *) *str, *len);
2114
0
            if (*len)
2115
0
                *len = strlen((char *) *str);
2116
0
        }
2117
0
    }
2118
2119
0
    return readfrom;
2120
0
}
2121
2122
/*
2123
 * read_config_save_objid(): saves an objid as a numerical string 
2124
 */
2125
char           *
2126
read_config_save_objid(char *saveto, const oid *objid, size_t len)
2127
0
{
2128
0
    int             i;
2129
2130
0
    if (len == 0) {
2131
0
        strcat(saveto, "NULL");
2132
0
        saveto += strlen(saveto);
2133
0
        return saveto;
2134
0
    }
2135
2136
    /*
2137
     * in case len=0, this makes it easier to read it back in 
2138
     */
2139
0
    for (i = 0; i < len; i++)
2140
0
        saveto += sprintf(saveto, ".%" NETSNMP_PRIo "d", objid[i]);
2141
2142
0
    return saveto;
2143
0
}
2144
2145
/*
2146
 * read_config_read_objid(): reads an objid from a format saved by the above 
2147
 */
2148
char           *
2149
read_config_read_objid(char *readfrom, oid ** objid, size_t * len)
2150
0
{
2151
0
    return NETSNMP_REMOVE_CONST(char *,
2152
0
             read_config_read_objid_const(readfrom, objid, len));
2153
0
}
2154
2155
const char     *
2156
read_config_read_objid_const(const char *readfrom, oid ** objid, size_t * len)
2157
0
{
2158
2159
0
    if (objid == NULL || readfrom == NULL || len == NULL)
2160
0
        return NULL;
2161
2162
0
    if (*objid == NULL) {
2163
0
        *len = 0;
2164
0
        if ((*objid = (oid *) malloc(MAX_OID_LEN * sizeof(oid))) == NULL)
2165
0
            return NULL;
2166
0
        *len = MAX_OID_LEN;
2167
0
    }
2168
2169
0
    if (strncmp(readfrom, "NULL", 4) == 0) {
2170
        /*
2171
         * null length oid 
2172
         */
2173
0
        *len = 0;
2174
0
    } else {
2175
        /*
2176
         * qualify the string for read_objid 
2177
         */
2178
0
        char            buf[SPRINT_MAX_LEN];
2179
0
        copy_nword_const(readfrom, buf, sizeof(buf));
2180
2181
0
        if (!read_objid(buf, *objid, len)) {
2182
0
            DEBUGMSGTL(("read_config_read_objid", "Invalid OID\n"));
2183
0
            *len = 0;
2184
0
            return NULL;
2185
0
        }
2186
0
    }
2187
2188
0
    readfrom = skip_token_const(readfrom);
2189
0
    return readfrom;
2190
0
}
2191
2192
/**
2193
 * read_config_read_data reads data of a given type from a token(s) on a
2194
 * configuration line.  The supported types are:
2195
 *
2196
 *    - ASN_INTEGER
2197
 *    - ASN_TIMETICKS
2198
 *    - ASN_UNSIGNED
2199
 *    - ASN_OCTET_STR
2200
 *    - ASN_BIT_STR
2201
 *    - ASN_OBJECT_ID
2202
 *
2203
 * @param type the asn data type to be read in.
2204
 *
2205
 * @param readfrom the configuration line data to be read.
2206
 *
2207
 * @param dataptr an allocated pointer expected to match the type being read
2208
 *        (int *, u_int *, char **, oid **)
2209
 *
2210
 * @param len is the length of an asn oid or octet/bit string, not required
2211
 *            for the asn integer, unsigned integer, and timeticks types
2212
 *
2213
 * @return the next token in the configuration line.  NULL if none left or
2214
 * if an unknown type.
2215
 * 
2216
 */
2217
char           *
2218
read_config_read_data(int type, char *readfrom, void *dataptr,
2219
                      size_t * len)
2220
0
{
2221
0
    int            *intp;
2222
0
    char          **charpp;
2223
0
    oid           **oidpp;
2224
0
    unsigned int   *uintp;
2225
2226
0
    if (dataptr && readfrom)
2227
0
        switch (type) {
2228
0
        case ASN_INTEGER:
2229
0
            intp = (int *) dataptr;
2230
0
            *intp = atoi(readfrom);
2231
0
            readfrom = skip_token(readfrom);
2232
0
            return readfrom;
2233
2234
0
        case ASN_TIMETICKS:
2235
0
        case ASN_UNSIGNED:
2236
0
            uintp = (unsigned int *) dataptr;
2237
0
            *uintp = strtoul(readfrom, NULL, 0);
2238
0
            readfrom = skip_token(readfrom);
2239
0
            return readfrom;
2240
2241
0
        case ASN_IPADDRESS:
2242
0
            intp = (int *) dataptr;
2243
0
            *intp = inet_addr(readfrom);
2244
0
            if ((*intp == -1) &&
2245
0
                (strncmp(readfrom, "255.255.255.255", 15) != 0))
2246
0
                return NULL;
2247
0
            readfrom = skip_token(readfrom);
2248
0
            return readfrom;
2249
2250
0
        case ASN_OCTET_STR:
2251
0
        case ASN_BIT_STR:
2252
0
            charpp = (char **) dataptr;
2253
0
            return read_config_read_octet_string(readfrom,
2254
0
                                                 (u_char **) charpp, len);
2255
2256
0
        case ASN_OBJECT_ID:
2257
0
            oidpp = (oid **) dataptr;
2258
0
            return read_config_read_objid(readfrom, oidpp, len);
2259
2260
0
        default:
2261
0
            DEBUGMSGTL(("read_config_read_data", "Fail: Unknown type: %d\n",
2262
0
                        type));
2263
0
            return NULL;
2264
0
        }
2265
0
    return NULL;
2266
0
}
2267
2268
/*
2269
 * read_config_read_memory():
2270
 * 
2271
 * similar to read_config_read_data, but expects a generic memory
2272
 * pointer rather than a specific type of pointer.  Len is expected to
2273
 * be the amount of available memory.
2274
 */
2275
char           *
2276
read_config_read_memory(int type, char *readfrom,
2277
                        char *dataptr, size_t * len)
2278
0
{
2279
0
    int            *intp;
2280
0
    unsigned int   *uintp;
2281
0
    char            buf[SPRINT_MAX_LEN];
2282
2283
0
    if (!dataptr || !readfrom)
2284
0
        return NULL;
2285
2286
0
    switch (type) {
2287
0
    case ASN_INTEGER:
2288
0
        if (*len < sizeof(int))
2289
0
            return NULL;
2290
0
        intp = (int *) dataptr;
2291
0
        readfrom = copy_nword(readfrom, buf, sizeof(buf));
2292
0
        *intp = atoi(buf);
2293
0
        *len = sizeof(int);
2294
0
        return readfrom;
2295
2296
0
    case ASN_COUNTER:
2297
0
    case ASN_TIMETICKS:
2298
0
    case ASN_UNSIGNED:
2299
0
        if (*len < sizeof(unsigned int))
2300
0
            return NULL;
2301
0
        uintp = (unsigned int *) dataptr;
2302
0
        readfrom = copy_nword(readfrom, buf, sizeof(buf));
2303
0
        *uintp = strtoul(buf, NULL, 0);
2304
0
        *len = sizeof(unsigned int);
2305
0
        return readfrom;
2306
2307
0
    case ASN_IPADDRESS:
2308
0
        if (*len < sizeof(int))
2309
0
            return NULL;
2310
0
        intp = (int *) dataptr;
2311
0
        readfrom = copy_nword(readfrom, buf, sizeof(buf));
2312
0
        *intp = inet_addr(buf);
2313
0
        if ((*intp == -1) && (strcmp(buf, "255.255.255.255") != 0))
2314
0
            return NULL;
2315
0
        *len = sizeof(int);
2316
0
        return readfrom;
2317
2318
0
    case ASN_OCTET_STR:
2319
0
    case ASN_BIT_STR:
2320
0
    case ASN_PRIV_IMPLIED_OCTET_STR:
2321
0
        return read_config_read_octet_string(readfrom,
2322
0
                                             (u_char **) & dataptr, len);
2323
2324
0
    case ASN_PRIV_IMPLIED_OBJECT_ID:
2325
0
    case ASN_OBJECT_ID:
2326
0
        readfrom =
2327
0
            read_config_read_objid(readfrom, (oid **) & dataptr, len);
2328
0
        *len *= sizeof(oid);
2329
0
        return readfrom;
2330
2331
0
    case ASN_COUNTER64:
2332
0
        if (*len < sizeof(struct counter64))
2333
0
            return NULL;
2334
0
        *len = sizeof(struct counter64);
2335
0
        read64((struct counter64 *) dataptr, readfrom);
2336
0
        readfrom = skip_token(readfrom);
2337
0
        return readfrom;
2338
0
    }
2339
2340
0
    DEBUGMSGTL(("read_config_read_memory", "Fail: Unknown type: %d\n", type));
2341
0
    return NULL;
2342
0
}
2343
2344
/**
2345
 * read_config_store_data stores data of a given type to a configuration line
2346
 * into the storeto buffer.
2347
 * Calls read_config_store_data_prefix with the prefix parameter set to a char
2348
 * space.  The supported types are:
2349
 *
2350
 *    - ASN_INTEGER
2351
 *    - ASN_TIMETICKS
2352
 *    - ASN_UNSIGNED
2353
 *    - ASN_OCTET_STR
2354
 *    - ASN_BIT_STR
2355
 *    - ASN_OBJECT_ID
2356
 *
2357
 * @param type    the asn data type to be stored
2358
 *
2359
 * @param storeto a pre-allocated char buffer which will contain the data
2360
 *                to be stored
2361
 *
2362
 * @param dataptr contains the value to be stored, the supported pointers:
2363
 *                (int *, u_int *, char **, oid **)
2364
 *
2365
 * @param len     is the length of the value to be stored
2366
 *                (not required for the asn integer, unsigned integer,
2367
 *                 and timeticks types)
2368
 *
2369
 * @return character pointer to the end of the line. NULL if an unknown type.
2370
 */
2371
char           *
2372
read_config_store_data(int type, char *storeto, void *dataptr, size_t * len)
2373
0
{
2374
0
    return read_config_store_data_prefix(' ', type, storeto, dataptr,
2375
0
                                                         (len ? *len : 0));
2376
0
}
2377
2378
char           *
2379
read_config_store_data_prefix(char prefix, int type, char *storeto,
2380
                              void *dataptr, size_t len)
2381
0
{
2382
0
    int            *intp;
2383
0
    u_char        **charpp;
2384
0
    unsigned int   *uintp;
2385
0
    struct in_addr  in;
2386
0
    oid           **oidpp;
2387
2388
0
    if (dataptr && storeto)
2389
0
        switch (type) {
2390
0
        case ASN_INTEGER:
2391
0
            intp = (int *) dataptr;
2392
0
            sprintf(storeto, "%c%d", prefix, *intp);
2393
0
            return (storeto + strlen(storeto));
2394
2395
0
        case ASN_TIMETICKS:
2396
0
        case ASN_UNSIGNED:
2397
0
            uintp = (unsigned int *) dataptr;
2398
0
            sprintf(storeto, "%c%u", prefix, *uintp);
2399
0
            return (storeto + strlen(storeto));
2400
2401
0
        case ASN_IPADDRESS:
2402
0
            in.s_addr = *(unsigned int *) dataptr; 
2403
0
            sprintf(storeto, "%c%s", prefix, inet_ntoa(in));
2404
0
            return (storeto + strlen(storeto));
2405
2406
0
        case ASN_OCTET_STR:
2407
0
        case ASN_BIT_STR:
2408
0
            *storeto++ = prefix;
2409
0
            charpp = (u_char **) dataptr;
2410
0
            return read_config_save_octet_string(storeto, *charpp, len);
2411
2412
0
        case ASN_OBJECT_ID:
2413
0
            *storeto++ = prefix;
2414
0
            oidpp = (oid **) dataptr;
2415
0
            return read_config_save_objid(storeto, *oidpp, len);
2416
2417
0
        default:
2418
0
            DEBUGMSGTL(("read_config_store_data_prefix",
2419
0
                        "Fail: Unknown type: %d\n", type));
2420
0
            return NULL;
2421
0
        }
2422
0
    return NULL;
2423
0
}
2424
2425
/** @} */