void csort(char *src, char *dest) { int i, j = 0, c = 0; char *words[256]; int count[256]; for (i = 0; i < 256; count[i] = 0, words[i++] = 0); c = wcount(src, words); for (j = 0; j < c - 1; j++) for (i = j + 1; i < c; i++) if (word_len(words[i]) < word_len(words[j])) count[j]++; else count[i]++; char *p = dest; for (i = 0; i < c; i++) for (j = 0; j < c; j++) if (count[j] == i) { copy_word(words[j], p); p += word_len(words[j]); *p++ = ' '; } *p = '\0'; }
void disk_parse_config(char *token, char *cptr) { #if HAVE_GETMNTENT #if HAVE_SYS_MNTTAB_H struct mnttab mnttab; #else struct mntent *mntent; #endif FILE *mntfp; #else #if HAVE_FSTAB_H struct fstab *fstab; struct stat stat1, stat2; #endif #endif char tmpbuf[1024]; #if defined(HAVE_GETMNTENT) && !defined(HAVE_SETMNTENT) int i; #endif #if HAVE_FSTAB_H || HAVE_GETMNTENT if (numdisks == MAXDISKS) { config_perror("Too many disks specified."); sprintf(tmpbuf,"\tignoring: %s",cptr); config_perror(tmpbuf); } else { /* read disk path (eg, /1 or /usr) */ copy_word(cptr,disks[numdisks].path); cptr = skip_not_white(cptr); cptr = skip_white(cptr); /* read optional minimum disk usage spec */ if (cptr != NULL) { if (strchr(cptr, '%') == 0) { disks[numdisks].minimumspace = atoi(cptr); disks[numdisks].minpercent = -1; } else { disks[numdisks].minimumspace = -1; disks[numdisks].minpercent = atoi(cptr); } } else { disks[numdisks].minimumspace = DEFDISKMINIMUMSPACE; disks[numdisks].minpercent = -1; } /* find the device associated with the directory */ #if HAVE_GETMNTENT #if HAVE_SETMNTENT mntfp = setmntent(ETC_MNTTAB, "r"); disks[numdisks].device[0] = 0; while ((mntent = getmntent (mntfp))) if (strcmp (disks[numdisks].path, mntent->mnt_dir) == 0) { copy_word (mntent->mnt_fsname, disks[numdisks].device); DEBUGMSGTL(("ucd-snmp/disk", "Disk: %s\n",mntent->mnt_fsname)); break; } else { DEBUGMSGTL(("ucd-snmp/disk", " %s != %s\n", disks[numdisks].path, mntent->mnt_dir)); } endmntent(mntfp); if (disks[numdisks].device[0] != 0) { /* dummy clause for else below */ numdisks += 1; /* but inc numdisks here after test */ } #else /* getmentent but not setmntent */ mntfp = fopen (ETC_MNTTAB, "r"); while ((i = getmntent (mntfp, &mnttab)) == 0) if (strcmp (disks[numdisks].path, mnttab.mnt_mountp) == 0) break; else { DEBUGMSGTL(("ucd-snmp/disk", " %s != %s\n", disks[numdisks].path, mnttab.mnt_mountp)); } fclose (mntfp); if (i == 0) { copy_word (mnttab.mnt_special, disks[numdisks].device); numdisks += 1; } #endif /* HAVE_SETMNTENT */ #else #if HAVE_FSTAB_H stat(disks[numdisks].path,&stat1); setfsent(); if ((fstab = getfsfile(disks[numdisks].path))) { copy_word(fstab->fs_spec,disks[numdisks].device); numdisks += 1; } #endif #endif else { sprintf(tmpbuf, "Couldn't find device for disk %s", disks[numdisks].path); config_pwarn(tmpbuf); disks[numdisks].minimumspace = -1; disks[numdisks].minpercent = -1; disks[numdisks].path[0] = 0; } #if HAVE_FSTAB_H endfsent(); #endif } #else config_perror("'disk' checks not supported on this architecture."); #endif }
Node::Node(const Node & node) { //TODO Do I need an initialization list? copy_word(word, node.word); }
void usm_parse_create_usmUser(const char *token, char *line) { char *cp; char buf[SNMP_MAXBUF_MEDIUM]; struct usmUser *newuser; u_char userKey[SNMP_MAXBUF_SMALL]; size_t userKeyLen = SNMP_MAXBUF_SMALL; int ret; newuser = usm_create_user(); /* READ: Security Name */ cp = copy_word(line, buf); newuser->secName = strdup(buf); newuser->name = strdup(buf); newuser->engineID = snmpv3_generate_engineID(&ret); if ( ret < 0 ) { usm_free_user(newuser); return; } newuser->engineIDLen = ret; if (!cp) goto add; /* no authentication or privacy type */ /* READ: Authentication Type */ if (strncmp(cp, "MD5", 3) == 0) { memcpy(newuser->authProtocol, usmHMACMD5AuthProtocol, sizeof(usmHMACMD5AuthProtocol)); } else if (strncmp(cp, "SHA", 3) == 0) { memcpy(newuser->authProtocol, usmHMACSHA1AuthProtocol, sizeof(usmHMACSHA1AuthProtocol)); } else { config_perror("Unknown authentication protocol"); usm_free_user(newuser); return; } cp = skip_token(cp); /* READ: Authentication Pass Phrase */ if (!cp) { config_perror("no authentication pass phrase"); usm_free_user(newuser); return; } cp = copy_word(cp, buf); /* And turn it into a localized key */ ret = generate_Ku(newuser->authProtocol, newuser->authProtocolLen, (u_char *)buf, strlen(buf), userKey, &userKeyLen ); if (ret != SNMPERR_SUCCESS) { config_perror("Error generating auth key from pass phrase."); usm_free_user(newuser); return; } newuser->authKeyLen = sc_get_properlength(newuser->authProtocol, newuser->authProtocolLen); newuser->authKey = (u_char *) malloc(newuser->authKeyLen); ret = generate_kul(newuser->authProtocol, newuser->authProtocolLen, newuser->engineID, newuser->engineIDLen, userKey, userKeyLen, newuser->authKey, &newuser->authKeyLen ); if (ret != SNMPERR_SUCCESS) { config_perror("Error generating localized auth key (Kul) from Ku."); usm_free_user(newuser); return; } if (!cp) goto add; /* no privacy type (which is legal) */ /* READ: Privacy Type */ if (strncmp(cp, "DES", 3) == 0) { memcpy(newuser->privProtocol, usmDESPrivProtocol, sizeof(usmDESPrivProtocol)); } else { config_perror("Unknown privacy protocol"); usm_free_user(newuser); return; } cp = skip_token(cp); /* READ: Authentication Pass Phrase */ if (!cp) { /* assume the same as the authentication key */ memdup(&newuser->privKey, newuser->authKey, newuser->authKeyLen); } else { cp = copy_word(cp, buf); /* And turn it into a localized key */ ret = generate_Ku(newuser->authProtocol, newuser->authProtocolLen, (u_char *)buf, strlen(buf), userKey, &userKeyLen ); if (ret != SNMPERR_SUCCESS) { config_perror("Error generating priv key from pass phrase."); usm_free_user(newuser); return; } ret = sc_get_properlength(newuser->authProtocol, newuser->authProtocolLen); if (ret < 0) { config_perror("Error getting proper key length for priv algorithm."); usm_free_user(newuser); return; } newuser->privKeyLen = ret; newuser->privKey = (u_char *) malloc(newuser->privKeyLen); ret = generate_kul(newuser->authProtocol, newuser->authProtocolLen, newuser->engineID, newuser->engineIDLen, userKey, userKeyLen, newuser->privKey, &newuser->privKeyLen ); if (ret != SNMPERR_SUCCESS) { config_perror("Error generating localized priv key (Kul) from Ku."); usm_free_user(newuser); return; } } add: usm_add_user(newuser); DEBUGMSGTL(("usmUser","created a new user %s\n", newuser->secName)); }
void extensible_parse_config(char *token, char* cptr) { struct extensible **pptmp; struct extensible **pprelocs = &relocs; struct extensible **ppexten = &extens; char *tcptr; if (*cptr == '.') cptr++; if (isdigit(*cptr)) { /* its a relocatable extensible mib */ while(*pprelocs != NULL) pprelocs = &((*pprelocs)->next); numrelocs++; (*pprelocs) = (struct extensible *) malloc(sizeof(struct extensible)); pptmp = pprelocs; } else { /* it goes in with the general extensible table */ while(*ppexten != NULL) ppexten = &((*ppexten)->next); numextens++; (*ppexten) = (struct extensible *) malloc(sizeof(struct extensible)); pptmp = ppexten; } /* the rest is pretty much handled the same */ if (!strncasecmp(token,"sh",2)) (*pptmp)->type = SHPROC; else (*pptmp)->type = EXECPROC; if (isdigit(*cptr)) { (*pptmp)->miblen = parse_miboid(cptr,(*pptmp)->miboid); while (isdigit(*cptr) || *cptr == '.') cptr++; } else { (*pptmp)->miboid[0] = 0; (*pptmp)->miblen = 0; } /* name */ cptr = skip_white(cptr); copy_word(cptr,(*pptmp)->name); cptr = skip_not_white(cptr); cptr = skip_white(cptr); /* command */ if (cptr == NULL) { config_perror("No command specified on line"); (*pptmp)->command[0] = 0; } else { for(tcptr=cptr; *tcptr != 0 && *tcptr != '#' && *tcptr != ';'; tcptr++); strncpy((*pptmp)->command,cptr,tcptr-cptr); (*pptmp)->command[tcptr-cptr] = 0; (*pptmp)->next = NULL; } #ifdef PROCFIXCMD sprintf((*pptmp)->fixcmd, EXECFIXCMD, (*pptmp)->name); #endif if ((*pptmp)->miblen > 0) { register_mib(token, (struct variable *) extensible_relocatable_variables, sizeof(struct variable2), 6, (*pptmp)->miboid, (*pptmp)->miblen); } }
const char *dotconf_handle_command(configfile_t * configfile, char *buffer) { signed char *cp1; signed char *cp2; /* generic char pointer */ signed char *eob; /* end of buffer; end of string */ const char *error; /* error message we'll return */ const char *context_error; /* error message returned by contextchecker */ command_t command; /* command structure */ int mod = 0; int next_opt_idx = 0; memset(&command, 0, sizeof(command_t)); name[0] = 0; error = 0; context_error = 0; cp1 = buffer; eob = cp1 + strlen(cp1); skip_whitespace(&cp1, eob - cp1, 0); /* ignore comments and empty lines */ if (!cp1 || !*cp1 || *cp1 == '#' || *cp1 == '\n' || *cp1 == EOF) return NULL; /* skip line if it only contains whitespace */ if (cp1 == eob) return NULL; /* get first token: read the name of a possible option */ cp2 = name; copy_word(&cp2, &cp1, MIN(eob - cp1, CFG_MAX_OPTION), 0); while (1) { const configoption_t *option; int done = 0; int opt_idx = 0; for (option = 0; configfile->config_options[mod] && !done; mod++) { for (opt_idx = next_opt_idx; configfile->config_options[mod][opt_idx].name[0]; opt_idx++) { if (!configfile-> cmp_func(name, configfile-> config_options[mod][opt_idx].name, CFG_MAX_OPTION)) { /* TODO: this could be flagged: option overwriting by modules */ option = (configoption_t *) & configfile-> config_options[mod][opt_idx]; done = 1; break; /* found one; break out */ } } } if (!option) option = get_argname_fallback(configfile->config_options[1]); if (!option || !option->callback) { if (error) return error; dotconf_warning(configfile, DCLOG_INFO, ERR_UNKNOWN_OPTION, "Unknown Config-Option: '%s'", name); return NULL; } /* set up the command structure (contextchecker wants this) */ dotconf_set_command(configfile, option, cp1, &command); if (configfile->contextchecker) context_error = configfile->contextchecker(&command, command.option->context); if (!context_error) error = dotconf_invoke_command(configfile, &command); else { if (!error) { /* avoid returning another error then the first. This makes it easier to reproduce problems. */ error = context_error; } } dotconf_free_command(&command); if (!context_error || !(configfile->flags & DUPLICATE_OPTION_NAMES)) { /* don't try more, just quit now. */ break; } } return error; }
static void split_file_into_words (SIDE *side) { struct stat stat_buffer; /* for checking if file is directory */ if (stat (tmp_path, &stat_buffer) != 0) { fprintf(stderr, "Could not open path %s", tmp_path); errexit (EXIT_FAILURE, errno, NULL); } if ((stat_buffer.st_mode & S_IFMT) != S_IFDIR) { errexit (EXIT_FAILURE, 0, "Temporary path must be a directory.\n"); } /* Open files. */ size_t tmplen; if (side->filename == NULL) { /* Select a file name, use it for opening a temporary file and unlink it right away. Then, copy the whole standard input on this temporary local file. Once done, prepare it for reading. We do not need the file name itself anymore. */ /* check if we have room for <tmp_path + template + null byte> */ tmplen = strlen(tmp_path) + strlen("/htmldiff1XXXXXX") + 1; if (tmplen > TMP_PATH_MAX_LEN) { fprintf(stderr, "Maximum length of temporary path is %d; tried to allocate %zu bytes\n", TMP_PATH_MAX_LEN, tmplen); errexit(EXIT_FAILURE, 0, NULL); } sprintf(side->temp_name, "%s/htmldiff1XXXXXX", tmp_path); side->file = fdopen(mkstemp(side->temp_name), "w+"); if (side->file == NULL) errexit (EXIT_FAILURE, errno, "%s", side->temp_name); if (unlink (side->temp_name) != 0) errexit (EXIT_FAILURE, errno, "%s", side->temp_name); while (side->character = getchar (), side->character != EOF) putc (side->character, side->file); rewind (side->file); } else { /* Check and diagnose if the file name is a directory. Or else, prepare the file for reading. */ if (access(side->filename, F_OK | R_OK) == -1) { fprintf(stderr, "Input file %s does not exist or we dont have read permissions", side->filename); errexit(EXIT_FAILURE, errno, NULL); } if (stat (side->filename, &stat_buffer) != 0) { errexit (EXIT_FAILURE, errno, "%s", side->filename); } if ((stat_buffer.st_mode & S_IFMT) == S_IFDIR) { errexit (EXIT_FAILURE, 0, _("Directories not supported")); } side->file = fopen (side->filename, "r"); if (side->file == NULL) { errexit (EXIT_FAILURE, errno, "%s", side->filename); } } side->character = getc (side->file); side->position = 0; /* check if we have room for <tmp_path + template + null byte> */ tmplen = strlen(tmp_path) + strlen("/htmldiff2XXXXXX") + 1; if (tmplen > TMP_PATH_MAX_LEN) { fprintf(stderr, "Maximum length of temporary path is %d; tried to allocate %zu bytes\n", TMP_PATH_MAX_LEN, tmplen); errexit(EXIT_FAILURE, 0, NULL); } sprintf(side->temp_name, "%s/htmldiff2XXXXXX", tmp_path); int tmpfilefd = mkstemp(side->temp_name); if (tmpfilefd < 0) { errexit(EXIT_FAILURE, 0, _("Error while creating temporary file")); } side->temp_file = fdopen(tmpfilefd, "w"); if (side->temp_file == NULL) { errexit (EXIT_FAILURE, errno, "%s", side->temp_name); } /* Complete splitting input file into words on output. */ while (side->character != EOF) { if (interrupted) { longjmp (signal_label, 1); } skip_whitespace (side); if (side->character == EOF) { break; } copy_word (side, side->temp_file); putc ('\n', side->temp_file); } fclose (side->temp_file); }
/* 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; }
/*******************************************************************-o-****** * read_config * * Parameters: * *filename * *line_handler * when * * Read <filename> and process each line in accordance with the list of * <line_handler> functions. * * * For each line in <filename>, search the list of <line_handler>'s * for an entry that matches the first token on the line. This comparison is * case insensitive. * * For each match, check that <when> is the designated time for the * <line_handler> function to be executed before processing the line. */ void read_config(const char *filename, struct config_line *line_handler, int when) { #ifdef CYGPKG_SNMPLIB_FILESYSTEM_SUPPORT FILE *ifile; char line[STRINGMAX], token[STRINGMAX], tmpbuf[STRINGMAX]; char *cptr; int i, done; struct config_line *lptr; linecount = 0; curfilename = filename; if ((ifile = fopen(filename, "r")) == NULL) { #ifdef ENOENT if (errno == ENOENT) { DEBUGMSGTL(("read_config", "%s: %s\n", filename, strerror(errno))); } else #endif /* ENOENT */ #ifdef EACCES if (errno == EACCES) { DEBUGMSGTL(("read_config", "%s: %s\n", filename, strerror(errno))); } else #endif /* EACCES */ #if defined(ENOENT) || defined(EACCES) { snmp_log_perror(filename); } #else /* defined(ENOENT) || defined(EACCES) */ snmp_log_perror(filename); #endif /* ENOENT */ return; } else { DEBUGMSGTL(("read_config", "Reading configuration %s\n", filename)); } while (fgets(line, sizeof(line), ifile) != NULL) { lptr = line_handler; linecount++; cptr = line; i = strlen(line)-1; if (line[i] == '\n') line[i] = 0; /* check blank line or # comment */ if ((cptr = skip_white(cptr))) { cptr = copy_word(cptr,token); if (cptr == NULL) { sprintf(tmpbuf,"Blank line following %s token.", token); config_perror(tmpbuf); } else { for(lptr = line_handler, done=0; lptr != NULL && !done; lptr = lptr->next) { if (!strcasecmp(token,lptr->config_token)) { if (when == EITHER_CONFIG || lptr->config_time == when) { DEBUGMSGTL(("read_config", "%s:%d Parsing: %s\n", filename, linecount, line)); (*(lptr->parse_line))(token,cptr); } done = 1; } } if (!done && when != PREMIB_CONFIG && !ds_get_boolean(DS_LIBRARY_ID, DS_LIB_NO_TOKEN_WARNINGS)) { sprintf(tmpbuf,"Unknown token: %s.", token); config_pwarn(tmpbuf); } } } } fclose(ifile); #endif return; } /* end read_config() */