read_config.c
来自「eCos操作系统源码」· C语言 代码 · 共 1,133 行 · 第 1/3 页
C
1,133 行
for(j=0; j <= MAX_PERSISTENT_BACKUPS; j++) { sprintf(fileold,"%s/%s.%d.conf", PERSISTENT_DIRECTORY, type, j); if (stat(fileold, &statbuf) != 0) { DEBUGMSGTL(("snmp_save_persistent"," saving old config file: %s -> %s.\n", file, fileold)); if (rename(file, fileold)) { unlink(file);/* moving it failed, try nuking it, as leaving it around is very bad. */ } break; } } }#endif}/*******************************************************************-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){#ifndef ECOSFIXME_NEEDFILESYSTEM char file[512]; struct stat statbuf; int j; DEBUGMSGTL(("snmp_clean_persistent","cleaning %s files...\n", type)); sprintf(file,"%s/%s.conf",PERSISTENT_DIRECTORY,type); if (stat(file, &statbuf) == 0) { for(j=0; j <= MAX_PERSISTENT_BACKUPS; j++) { sprintf(file,"%s/%s.%d.conf", PERSISTENT_DIRECTORY, type, j); if (stat(file, &statbuf) == 0) { DEBUGMSGTL(("snmp_clean_persistent"," removing old config file: %s\n", file)); unlink(file); } } }#endif} /* config_perror: prints a warning string associated with a file and line number of a .conf file and increments the error count. */void config_perror(const char *string){ config_pwarn(string); config_errors++;}void config_pwarn(const char *string){ snmp_log(LOG_WARNING, "%s: line %d: %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'. 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.*/char *copy_word(char *from, char *to){ char quote; if ( (*from == '\"') || (*from =='\'') ){ quote = *(from++); while ( (*from != quote) && (*from != 0) ) { if ((*from == '\\') && (*(from+1) != 0)) { *to++ = *(from+1); from = from +2; } else *to++ = *from++; } if (*from == 0) { DEBUGMSGTL(("read_config_copy_word", "no end quote found in config string\n")); } else from++; } else { while (*from != 0 && !isspace(*from)) { if ((*from == '\\') && (*(from+1) != 0)) { *to++ = *(from+1); from = from +2; } else *to++ = *from++; } } *to = 0; from = skip_white(from); return(from);} /* copy_word *//* read_config_save_octet_string(): saves an octet string as a length followed by a string of hex */char *read_config_save_octet_string(char *saveto, u_char *str, size_t len) { int i; if (str != NULL) { sprintf(saveto, "0x"); saveto += 2; for(i = 0; i < (int)len; i++) { sprintf(saveto,"%02x", str[i]); saveto = saveto + 2; } return saveto; } else { sprintf(saveto,"\"\""); saveto += 2; } return saveto;}/* read_config_read_octet_string(): reads an octet string that was saved by the read_config_save_octet_string() function */char *read_config_read_octet_string(char *readfrom, u_char **str, size_t *len) { u_char *cptr=NULL; char *cptr1; u_int tmp; int i; if (readfrom == NULL || str == NULL) return NULL; if (strncasecmp(readfrom,"0x",2) == 0) { /* A hex string submitted. How long? */ readfrom += 2; cptr1 = skip_not_white(readfrom); if (cptr1) *len = (cptr1 - readfrom); else *len = strlen(readfrom); if (*len % 2) { DEBUGMSGTL(("read_config_read_octet_string","invalid hex string: wrong length")); return NULL; } *len = *len / 2; /* malloc data space if needed */ if (*str == NULL) { if (*len == 0) { /* null length string found */ cptr = NULL; } else if (*len > 0 && (str == NULL || (cptr = (u_char *)malloc(*len)) == NULL)) { return NULL; } *str = cptr; } else { cptr = *str; } /* copy data */ for(i = 0; i < (int)*len; i++) { sscanf(readfrom,"%2x",&tmp); *cptr++ = (u_char) tmp; readfrom += 2; } readfrom = skip_white(readfrom); } else { /* Normal string */ /* malloc data space if needed */ if (*str == NULL) { char buf[SNMP_MAXBUF]; readfrom = copy_word(readfrom, buf); *len = strlen(buf); /* malloc an extra space to add a null */ if (*len > 0 && (str == NULL || (cptr = (u_char *) malloc(*len + 1)) == NULL)) return NULL; *str = cptr; if (cptr) memcpy(cptr, buf, (*len+1)); } else { readfrom = copy_word(readfrom, (char *)*str); } } return readfrom;}/* read_config_save_objid(): saves an objid as a numerical string */char *read_config_save_objid(char *saveto, oid *objid, size_t len) { int i; if (len == 0) { strcat(saveto, "NULL"); saveto += strlen(saveto); return saveto; } /* in case len=0, this makes it easier to read it back in */ for(i=0; i < (int)len; i++) { sprintf(saveto,".%ld", objid[i]); saveto += strlen(saveto); } return saveto;}/* read_config_read_objid(): reads an objid from a format saved by the above */char *read_config_read_objid(char *readfrom, oid **objid, size_t *len) { if (objid == NULL || readfrom == NULL) return NULL; if (*objid != NULL) { char buf[SPRINT_MAX_LEN]; if (strncmp(readfrom,"NULL",4) == 0) { /* null length oid */ *len = 0; } else { /* read_objid is touchy with trailing stuff */ copy_word(readfrom, buf); /* read the oid into the buffer passed to us */ if (!read_objid(buf, *objid, len)) { DEBUGMSGTL(("read_config_read_objid","Invalid OID")); return NULL; } } readfrom = skip_token(readfrom); } else { if (strncmp(readfrom,"NULL",4) == 0) { /* null length oid */ *len = 0; readfrom = skip_token(readfrom); } else { /* space needs to be malloced. Call ourself recursively to figure out how long the oid actually is */ oid obuf[MAX_OID_LEN]; size_t obuflen = MAX_OID_LEN; oid *oidp = obuf; oid **oidpp = &oidp; /* done this way for odd, untrue, gcc warnings */ readfrom = read_config_read_objid(readfrom, oidpp, &obuflen); /* Then malloc and copy the results */ *len = obuflen; if (*len > 0 && (*objid = (oid*)malloc(*len * sizeof(oid))) == NULL) return NULL; if (obuflen > 0) memcpy(*objid, obuf, obuflen*sizeof(oid)); } } return readfrom;}/* read_config_read_data(): reads data of a given type from a token(s) on a configuration line. Returns: character pointer to the next token in the configuration line. NULL if none left. NULL if an unknown type.*/char *read_config_read_data(int type, char *readfrom, void *dataptr, size_t *len) { int *intp; char **charpp; oid **oidpp; if (dataptr == NULL || readfrom == NULL) return NULL; switch(type) { case ASN_INTEGER: intp = (int *) dataptr; *intp = atoi(readfrom); readfrom = skip_token(readfrom); return readfrom; case ASN_OCTET_STR: charpp = (char **) dataptr; return read_config_read_octet_string(readfrom, (u_char **) charpp, len); case ASN_OBJECT_ID: oidpp = (oid **) dataptr; return read_config_read_objid(readfrom, oidpp, len); default: DEBUGMSGTL(("read_config_read_data","Fail: Unknown type: %d", type)); return NULL; } return NULL;}/* read_config_read_data(): reads data of a given type from a token(s) on a configuration line. Returns: character pointer to the next token in the configuration line. NULL if none left. NULL if an unknown type.*/char *read_config_store_data(int type, char *storeto, void *dataptr, size_t *len) { int *intp; u_char **charpp; oid **oidpp; if (dataptr == NULL || storeto == NULL) return NULL; switch(type) { case ASN_INTEGER: intp = (int *) dataptr; sprintf(storeto," %d", *intp); return (storeto + strlen(storeto)); case ASN_OCTET_STR: charpp = (u_char **) dataptr; return read_config_save_octet_string(storeto, *charpp, *len); case ASN_OBJECT_ID: oidpp = (oid **) dataptr; return read_config_save_objid(storeto, *oidpp, *len); default: DEBUGMSGTL(("read_config_store_data","Fail: Unknown type: %d", type)); return NULL; } return NULL;}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?