static void tcpeek_init_setuid(void) { struct passwd *passwd; gid_t groups[128]; int ngroups; if(strisempty(g.option.user)) { return; } passwd = strisdigit(g.option.user) ? getpwuid((uid_t)strtol(g.option.user, NULL, 10)) : getpwnam(g.option.user); if(!passwd) { error_abort("%s", strerror(errno)); } ngroups = sizeof(groups); if(getgrouplist(g.option.user, passwd->pw_gid, groups, &ngroups) == -1) { error_abort("getgrouplist: %s", strerror(errno)); } if(setgroups(ngroups, groups) == -1) { error_abort("setgroups: %s", strerror(errno)); } if(setgid(passwd->pw_gid) == -1) { error_abort("setgid: %s", strerror(errno)); } if(setuid(passwd->pw_uid) == -1) { error_abort("setuid: %s", strerror(errno)); } }
/* Gets a possible int on the stack. * * KLUDGE: I don't think I should sanitize here. Perhaps I should sanitize * somewhere prior to this call. */ int get_stack_int(bool *success) { if (success == NULL) { eprintf("programmer error: null success attribute."); abort(); } if (num_stack_items() < 1) { eprintf("stack underflow error."); *success = false; return INT_MIN; } char *arg = stack_pop(); if (!strisdigit(arg)) { feprintf("type error with given argument: %s.", arg); *success = false; return INT_MIN; } int arg_converted; sscanf(arg, "%d", &arg_converted); *success = true; return arg_converted; }
/* * This function takes in the list of headers that were in the server's * response, it walks through the headers looking for a Retry-After response * header. If one is found, the value is parsed and saved away in the EST * context. This value can be in one of two formats, both are represented as * an ASCII string. The first format can be a count of the number of seconds * the client should wait before retrying the request. The second format is a * time/date stamp of the point in time at which the client should retry the * request. The result of this function is the setting of the retry_after * values in the context. If no retry-after header was received, or was * received and could not be parsed, the values will be zero, otherwise, they * are set to the value received. * * NOTE: The EST client currently does not support the time/date format * response and will not process a response in this format. */ static EST_ERROR est_io_parse_http_retry_after_resp (EST_CTX *ctx, HTTP_HEADER *hdrs, int hdr_cnt) { EST_ERROR rv = EST_ERR_INVALID_RETRY_VALUE; int i; int cmp_result; int rc; long long int temp_ll; int found = 0; /* * Initialize assuming there was no retry-after header. */ ctx->retry_after_delay = 0; ctx->retry_after_date = 0; for (i = 0; i < hdr_cnt; i++) { cmp_result = strncasecmp(hdrs[i].name, EST_HTTP_HDR_RETRY_AFTER, sizeof(EST_HTTP_HDR_RETRY_AFTER)); if (!cmp_result) { EST_LOG_INFO("Retry-After value = %s", hdrs[i].value); found = 1; /* * Determine whether or not the value is a date/time string * or is an integer representing the number of seconds * that the client must wait. */ if (isalpha((int)*(hdrs[i].value))) { #ifdef RETRY_AFTER_DELAY_TIME_SUPPORT int rc; /* * Convert the date/time string into a time_t */ rc = parsedate(hdrs[i].value, &ctx->retry_after_date); if (rc != PARSEDATE_OK) { EST_LOG_ERR("Retry-After value could not be parsed"); } #else /* * This format is not currently supported. */ EST_LOG_ERR("Retry-After value not in the correct format"); #endif } else { /* * make sure it's all digits, make sure it's no larger than a * four byte integer, and cache away the value returned for * the retry delay. */ rc = strisdigit(hdrs[i].value, 10); // max of 10 decimal places if (rc) { temp_ll = atoll(hdrs[i].value); if (temp_ll <= INT_MAX) { ctx->retry_after_delay = (int) temp_ll; rv = EST_ERR_CA_ENROLL_RETRY; } else { EST_LOG_ERR("Retry-After value too large"); } } else { EST_LOG_ERR("Retry-After value could not be parsed"); } } } } if (found == 0) { EST_LOG_ERR("Retry-After header missing"); } return rv; }
static void tcpeek_init_option(int argc, char *argv[]) { int opt; static struct option long_options[] = { {"user", 1, NULL, 'u'}, {"interface", 1, NULL, 'i'}, {"checksum", 1, NULL, 'c'}, {"socket", 1, NULL, 'U'}, {"timeout", 1, NULL, 't'}, {"buffer", 1, NULL, 'B'}, {"loglevel", 1, NULL, 'l'}, {"quiet", 0, NULL, 'q'}, {"promisc", 0, NULL, 500}, {"icmp", 0, NULL, 501}, {"help", 0, NULL, 'h'}, {"version", 0, NULL, 'v'}, { NULL, 0, NULL, 0 } }; while((opt = getopt_long_only(argc, argv, "u:i:c:U:t:B:l:qhv", long_options, NULL)) != -1) { switch(opt) { case 'u': strncpy(g.option.user, optarg, sizeof(g.option.user) - 1); break; case 'i': strncpy(g.option.ifname, optarg, sizeof(g.option.ifname) - 1); break; case 'c': if(!strisequal(optarg, "0") && !strisequal(optarg, "1") && !strisequal(optarg, "2")) { usage(); tcpeek_terminate(0); } g.option.checksum = strtol(optarg, NULL, 10); break; case 'U': strncpy(g.option.socket, optarg, sizeof(g.option.socket) - 1); break; case 'B': if(!strisdigit(optarg)) { usage(); tcpeek_terminate(0); } g.option.buffer = strtol(optarg, NULL, 10); break; case 't': if(!strisdigit(optarg)) { usage(); tcpeek_terminate(0); } g.option.timeout = strtol(optarg, NULL, 10); break; case 'q': g.option.quiet = 1; break; case 500: g.option.promisc = 1; break; case 501: g.option.icmp = 1; break; case 'h': usage(); tcpeek_terminate(0); case 'v': version(); tcpeek_terminate(0); default: usage(); tcpeek_terminate(1); } } while(optind < argc) { lnklist_add_tail(g.option.expression, strdup(argv[optind++])); } }
int main(int argc, char* argv[]) { int res = 0, index = 0; FILE* fp = NULL; /* dict file */ char* path = NULL, /* dict file path */ * quote = NULL; polyaness_t* pt = NULL; /* flag and args */ flags_t flags = { 0, 0, 0, 0 ,NULL, }; struct option opts[] = { {"file", required_argument, NULL, 'f'}, {"number", required_argument, NULL, 'n'}, {"list", no_argument, NULL, 'l'}, {"help", no_argument, NULL, 'h'}, {"version", no_argument, NULL, 'v'}, {0, 0, 0, 0} }; /* processing of arguments */ while ((res = getopt_long(argc, argv, "f:n:lvh", opts, &index)) != -1) { switch (res) { case 'f': flags.farg = optarg; flags.fflag = 1; break; case 'n': if (strisdigit(optarg) < 0) return -1; flags.narg = atoi(optarg); flags.nflag = 1; break; case 'l': flags.lflag = 1; break; case 'v': print_version(); case 'h': print_usage(); case '?': return -1; } } /* concat file path */ if ((path = concat_file_path(&flags)) == NULL) return 1; /* open flags-quotes */ if (open_dict_file(path, &fp) < 0) { release(NULL, path, NULL); return 2; } /* read dict file */ if (read_dict_file(fp, &pt) < 0) { release(NULL, path, NULL); return 3; } /* do parse polyaness */ if (parse_dict_file(fp, &pt) < 0) { release(fp, path, pt); return 4; } /* * print all quotes list and exit */ if (flags.lflag == 1) { print_all_quotes(pt); release(fp, path, pt); return 0; } /* * generate output quote */ if (flags.nflag == 0) { quote = get_polyaness("quote", create_rand(pt->recs - 1), &pt); } else { if (flags.narg < pt->recs) quote = get_polyaness("quote", flags.narg, &pt); else quote = get_polyaness("quote", 0, &pt); } /* print of string */ fprintf(stdout, "%s\n", quote); /* release memory */ release(fp, path, pt); return 0; }