⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 read_config.c

📁 snmp up 2
💻 C
📖 第 1 页 / 共 4 页
字号:
read_config_files(int when){    int             i, j;    char            configfile[300];    char           *envconfpath;    const char     *confpath, *perspath;    char           *cptr1, *cptr2;    char            defaultPath[SPRINT_MAX_LEN];    struct config_files *ctmp = config_files;    struct config_line *ltmp;    struct stat     statbuf;    config_errors = 0;    if (when == PREMIB_CONFIG)        free_config();    confpath = get_configuration_directory();    perspath = get_persistent_directory();    /*     * read all config file types      */    for (; ctmp != NULL; ctmp = ctmp->next) {        ltmp = ctmp->start;        /*         * read the config files          */        if ((envconfpath = getenv("SNMPCONFPATH")) == NULL) {            snprintf(defaultPath, sizeof(defaultPath), "%s%s%s",                    ((confpath == NULL) ? "" : confpath),                    ((perspath == NULL) ? "" : ENV_SEPARATOR),                    ((perspath == NULL) ? "" : perspath));            defaultPath[ sizeof(defaultPath)-1 ] = 0;            envconfpath = defaultPath;        }        envconfpath = strdup(envconfpath);      /* prevent actually writing in env */        DEBUGMSGTL(("read_config", "config path used:%s\n", envconfpath));        cptr1 = cptr2 = envconfpath;        i = 1;        while (i && *cptr2 != 0) {            while (*cptr1 != 0 && *cptr1 != ENV_SEPARATOR_CHAR)                cptr1++;            if (*cptr1 == 0)                i = 0;            else                *cptr1 = 0;            /*             * for proper persistent storage retrival, we need to read old backup             * copies of the previous storage files.  If the application in             * question has died without the proper call to snmp_clean_persistent,             * then we read all the configuration files we can, starting with             * the oldest first.             */            if (strncmp(cptr2, perspath,                        strlen(perspath)) == 0 ||                (getenv("SNMP_PERSISTENT_FILE") != NULL &&                 strncmp(cptr2, getenv("SNMP_PERSISTENT_FILE"),                         strlen(getenv("SNMP_PERSISTENT_FILE"))) == 0)) {                /*                 * limit this to the known storage directory only                  */                for (j = 0; j <= MAX_PERSISTENT_BACKUPS; j++) {                    snprintf(configfile, sizeof(configfile),                           "%s/%s.%d.conf", cptr2,                            ctmp->fileHeader, j);                    configfile[ sizeof(configfile)-1 ] = 0;                    if (stat(configfile, &statbuf) != 0) {                        /*                         * file not there, continue                          */                        break;                    } else {                        /*                         * backup exists, read it                          */                        DEBUGMSGTL(("read_config_files",                                    "old config file found: %s, parsing\n",                                    configfile));                        read_config(configfile, ltmp, when);
                    }                }            }/*            snprintf(configfile, sizeof(configfile),
                     "%s/%s.conf", cptr2, ctmp->fileHeader);
  */
			snprintf(configfile, sizeof(configfile),
                     "%s%s.conf", cptr2, ctmp->fileHeader);
            configfile[ sizeof(configfile)-1 ] = 0;            read_config(configfile, ltmp, when);/*            snprintf(configfile, sizeof(configfile),
                     "%s/%s.local.conf", cptr2, ctmp->fileHeader);
 */
			snprintf(configfile, sizeof(configfile),
                     "%s%s.local.conf", cptr2, ctmp->fileHeader);
            configfile[ sizeof(configfile)-1 ] = 0;            read_config(configfile, ltmp, when);            cptr2 = ++cptr1;        }        free(envconfpath);    }    if (config_errors) {        snmp_log(LOG_ERR, "net-snmp: %d error(s) in config file(s)\n",                 config_errors);        /*         * exit(1);          */    }}voidread_config_print_usage(const char *lead){    struct config_files *ctmp = config_files;    struct config_line *ltmp;    if (lead == NULL)        lead = "";    for (ctmp = config_files; ctmp != NULL; ctmp = ctmp->next) {        snmp_log(LOG_INFO, "%sIn %s.conf and %s.local.conf:\n", lead,                 ctmp->fileHeader, ctmp->fileHeader);        for (ltmp = ctmp->start; ltmp != NULL; ltmp = ltmp->next) {            DEBUGIF("read_config_usage") {                if (ltmp->config_time == PREMIB_CONFIG)                    DEBUGMSG(("read_config_usage", "*"));                else                    DEBUGMSG(("read_config_usage", " "));            }            if (ltmp->help) {                snmp_log(LOG_INFO, "%s%s%-24s %s\n", lead, lead,                         ltmp->config_token, ltmp->help);            } else {                DEBUGIF("read_config_usage") {                    snmp_log(LOG_INFO, "%s%s%-24s [NO HELP]\n", lead, lead,                             ltmp->config_token);                }            }        }    }}/*******************************************************************-o-****** * read_config_store * * Parameters: *	*type *	*line *       *  * Append line to a file named either ENV(SNMP_PERSISTENT_FILE) or *   "<PERSISTENT_DIRECTORY>/<type>.conf". * Add a trailing newline to the stored file if necessary. * * Intended for use by applications to store permenant configuration  * information generated by sets or persistent counters. * */voidread_config_store(const char *type, const char *line){#ifdef PERSISTENT_DIRECTORY    char            file[512], *filep;    FILE           *fout;#ifdef PERSISTENT_MASK    mode_t          oldmask;#endif    /*     * store configuration directives in the following order of preference:     * 1. ENV variable SNMP_PERSISTENT_FILE     * 2. configured <PERSISTENT_DIRECTORY>/<type>.conf     */    if ((filep = getenv("SNMP_PERSISTENT_FILE")) == NULL) {        snprintf(file, sizeof(file),                 "%s/%s.conf", get_persistent_directory(), type);        file[ sizeof(file)-1 ] = 0;        filep = file;    }#ifdef PERSISTENT_MASK    oldmask = umask(PERSISTENT_MASK);#endif    if (mkdirhier(filep, AGENT_DIRECTORY_MODE, 1)) {        snmp_log(LOG_ERR,                 "Failed to create the persistent directory for %s\n",                 file);    }    if ((fout = fopen(filep, "a")) != NULL) {        fprintf(fout, "%s", line);        if (line[strlen(line)] != '\n')            fprintf(fout, "\n");        DEBUGMSGTL(("read_config", "storing: %s\n", line));        fclose(fout);    } else {        snmp_log(LOG_ERR, "read_config_store open failure on %s\n", filep);    }#ifdef PERSISTENT_MASK    umask(oldmask);#endif#endif}                               /* end read_config_store() */voidread_app_config_store(const char *line){    read_config_store(netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID, 					    NETSNMP_DS_LIB_APPTYPE), line);}/*******************************************************************-o-****** * snmp_save_persistent * * Parameters: *	*type *       * * Save the file "<PERSISTENT_DIRECTORY>/<type>.conf" into a backup copy * called "<PERSISTENT_DIRECTORY>/<type>.%d.conf", which %d is an * incrementing number on each call, but less than MAX_PERSISTENT_BACKUPS. * * Should be called just before all persistent information is supposed to be * written to move aside the existing persistent cache. * snmp_clean_persistent should then be called afterward all data has been * saved to remove these backup files. * * Note: on an rename error, the files are removed rather than saved. * */voidsnmp_save_persistent(const char *type){    char            file[512], fileold[SPRINT_MAX_LEN];    struct stat     statbuf;    int             j;    DEBUGMSGTL(("snmp_save_persistent", "saving %s files...\n", type));    snprintf(file, sizeof(file),             "%s/%s.conf", get_persistent_directory(), type);    file[ sizeof(file)-1 ] = 0;    if (stat(file, &statbuf) == 0) {        for (j = 0; j <= MAX_PERSISTENT_BACKUPS; j++) {            snprintf(fileold, sizeof(fileold),                     "%s/%s.%d.conf", get_persistent_directory(), type, j);            fileold[ sizeof(fileold)-1 ] = 0;            if (stat(fileold, &statbuf) != 0) {                DEBUGMSGTL(("snmp_save_persistent",                            " saving old config file: %s -> %s.\n", file,                            fileold));                if (rename(file, fileold)) {                    snmp_log(LOG_ERR, "Cannot rename %s to %s\n", file, fileold);                     /* moving it failed, try nuking it, as leaving                      * it around is very bad. */                    if (unlink(file) == -1)                        snmp_log(LOG_ERR, "Cannot unlink %s\n", file);                }                break;            }        }    }    /*     * save a warning header to the top of the new file      */    snprintf(fileold, sizeof(fileold),            "#\n# net-snmp (or ucd-snmp) persistent data file.\n#\n# DO NOT STORE CONFIGURATION ENTRIES HERE.\n# Please save normal configuration tokens for %s in SNMPCONFPATH/%s.conf.\n# Only \"createUser\" tokens should be placed here by %s administrators.\n#\n",            type, type, type);    fileold[ sizeof(fileold)-1 ] = 0;    read_config_store(type, fileold);}/*******************************************************************-o-****** * snmp_clean_persistent * * Parameters: *	*type *       * * Unlink all backup files called "<PERSISTENT_DIRECTORY>/<type>.%d.conf". * * Should be called just after we successfull dumped the last of the * persistent data, to remove the backup copies of previous storage dumps. * * XXX  Worth overwriting with random bytes first?  This would *	ensure that the data is destroyed, even a buffer containing the *	data persists in memory or swap.  Only important if secrets *	will be stored here. */voidsnmp_clean_persistent(const char *type){    char            file[512];    struct stat     statbuf;    int             j;    DEBUGMSGTL(("snmp_clean_persistent", "cleaning %s files...\n", type));    snprintf(file, sizeof(file),             "%s/%s.conf", get_persistent_directory(), type);    file[ sizeof(file)-1 ] = 0;    if (stat(file, &statbuf) == 0) {        for (j = 0; j <= MAX_PERSISTENT_BACKUPS; j++) {            snprintf(file, sizeof(file),                     "%s/%s.%d.conf", get_persistent_directory(), type, j);            file[ sizeof(file)-1 ] = 0;            if (stat(file, &statbuf) == 0) {                DEBUGMSGTL(("snmp_clean_persistent",                            " removing old config file: %s\n", file));                if (unlink(file) == -1)                    snmp_log(LOG_ERR, "Cannot unlink %s\n", file);            }        }    }}/* * config_perror: prints a warning string associated with a file and * line number of a .conf file and increments the error count.  */voidconfig_perror(const char *string){    snmp_log(LOG_ERR, "%s: line %d: Error: %s\n", curfilename, linecount,             string);    config_errors++;}voidconfig_pwarn(const char *string){    snmp_log(LOG_WARNING, "%s: line %d: Warning: %s\n", curfilename,             linecount, string);}/* * skip all white spaces and return 1 if found something either end of * line or a comment character  */char           *skip_white(char *ptr){    if (ptr == NULL)        return (NULL);    while (*ptr != 0 && isspace(*ptr))        ptr++;    if (*ptr == 0 || *ptr == '#')        return (NULL);    return (ptr);}char           *skip_not_white(char *ptr){    if (ptr == NULL)        return (NULL);    while (*ptr != 0 && !isspace(*ptr))        ptr++;    if (*ptr == 0 || *ptr == '#')        return (NULL);    return (ptr);}char           *skip_token(char *ptr){    ptr = skip_white(ptr);    ptr = skip_not_white(ptr);    ptr = skip_white(ptr);    return (ptr);}/* * copy_word * copies the next 'token' from 'from' into 'to', maximum len-1 characters. * currently a token is anything seperate by white space * or within quotes (double or single) (i.e. "the red rose"  * is one token, \"the red rose\" is three tokens) * a '\' character will allow a quote character to be treated * as a regular character  * It returns a pointer to first non-white space after the end of the token * being copied or to 0 if we reach the end. * Note: Partially copied words (greater than len) still returns a !NULL ptr * Note: partially copied words are, however, null terminated. */char           *copy_nword(char *from, char *to, int len){    char            quote;    if ((*from == '\"') || (*from == '\'')) {        quote = *(from++);        while ((*from != quote) && (*from != 0)) {            if ((*from == '\\') && (*(from + 1) != 0)) {                if (len > 0) {  /* don't copy beyond len bytes */                    *to++ = *(from + 1);                    if (--len == 0)                        *(to - 1) = '\0';       /* null protect the last spot */                }                from = from + 2;            } else {                if (len > 0) {  /* don't copy beyond len bytes */                    *to++ = *from++;                    if (--len == 0)                        *(to - 1) = '\0';       /* null protect the last spot */                } else                    from++;            }        }        if (*from == 0) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -