static void read_one(ddns_alias_t *alias, int nonslookup) { FILE *fp; char path[256]; alias->last_update = 0; memset(alias->address, 0, sizeof(alias->address)); cache_file(alias->name, path, sizeof(path)); fp = fopen(path, "r"); if (!fp) { if (nonslookup) return; /* Try a DNS lookup of our last known IP#. */ nslookup(alias); } else { struct stat st; char address[MAX_ADDRESS_LEN]; if (fgets(address, sizeof(address), fp)) { logit(LOG_INFO, "Cached IP# %s from previous invocation.", address); strncpy(alias->address, address, sizeof(alias->address)); } /* Initialize time since last update from modification time of cache file. */ if (!fstat(fileno(fp), &st)) { alias->last_update = st.st_mtime; logit(LOG_INFO, "Last update of %s on %s", alias->name, ctime(&st.st_mtime)); } fclose(fp); } }
void loadServerFile(struct config* config) { FILE* file = fopen(config->server_file, "r"); if(file==NULL) { printf("File %s does not exist!\n", config->server_file); exit(1); } char lineBuffer[1024]; int i=0; while (fgets(lineBuffer, sizeof(lineBuffer), file)) { char* ipaddress = nslookup(strtok(lineBuffer, " ,\n")); config->server_port[i]=atoi(strtok(NULL, " ,\n")); config->server_ip_address[i]=calloc(strlen(ipaddress)+1, sizeof(char)); strcpy(config->server_ip_address[i], ipaddress); i++; } fclose(file); config->n_servers=i; }
/** * * @param argc * @param argv * @return */ int main(int argc, char** argv) { if (0 != getuid()) { printf("%s", INSUFF_PRIV); exit(EXIT_FAILURE); } start_time = time(NULL); char *host = NULL; int opt = 0; int longIndex = 0; memset(globals.routes, 0, ROUTES_MAX_COUNT); // curl_global_init(CURL_GLOBAL_ALL); // watchdog_stop_flag = false; sheduler_stop_flag = false; pbuffer_stop_flag = false; rotate_stop_flag = false; listen_stop_flag = false; /* Initialize arguments before we get to work. */ char * argument = NULL; argument = "/var/run/arewik.pid"; arguments.pidfile = xmalloc(sizeof (char) * strlen(argument) + 1); strcpy(arguments.pidfile, argument); argument = "/var/log/arewik"; arguments.logdir = xmalloc(sizeof (char) * strlen(argument) + 1); strcpy(arguments.logdir, argument); argument = "/var/arewik"; arguments.storagedir = xmalloc(sizeof (char) * strlen(argument) + 1); strcpy(arguments.storagedir, argument); argument = "/var/arewik/buffers"; arguments.bufferdir = xmalloc(sizeof (char) * strlen(argument) + 1); strcpy(arguments.bufferdir, argument); argument = "/etc/arewik/arewik.conf"; arguments.configfile = xmalloc(sizeof (char) * strlen(argument) + 1); strcpy(arguments.configfile, argument); argument = "arewik"; arguments.user = xmalloc(sizeof (char) * strlen(argument) + 1); strcpy(arguments.user, argument); argument = "arewik"; arguments.group = xmalloc(sizeof (char) * strlen(argument) + 1); strcpy(arguments.group, argument); argument = "/tmp"; arguments.wdir = xmalloc(sizeof (char) * strlen(argument) + 1); strcpy(arguments.wdir, argument); argument = DEFAULT_LISTEN_IP; arguments.listen_host = xmalloc(sizeof (char) * strlen(argument) + 1); strcpy(arguments.listen_host, argument); arguments.listen_port = DEFAULT_LISTEN_PORT; arguments.verbosity = 0; arguments.foreground = 0; arguments.debuginfo = 0; argument = "custom.log"; globals.custom_logfile_name = xmalloc(sizeof (char) * strlen(argument) + 1); strcpy(globals.custom_logfile_name, argument); argument = "debug.log"; globals.debug_logfile_name = xmalloc(sizeof (char) * strlen(argument) + 1); strcpy(globals.debug_logfile_name, argument); argument = "connections.log"; globals.connections_logfile_name = xmalloc(sizeof (char) * strlen(argument) + 1); strcpy(globals.connections_logfile_name, argument); argument = "access.log"; globals.access_logfile_name = xmalloc(sizeof (char) * strlen(argument) + 1); strcpy(globals.access_logfile_name, argument); globals.maxcon = 4096; // routes max * 64 globals.proxymode = false; //todo globals.routes_cnt = 1; globals.watchdog_interval = WATCHDOG_T; globals.ping_interval = 30; globals.autoreconfigure = false; globals.reconfigure_interval = 60; globals.use_resolver = 0; globals.use_syslog = 0; globals.socktimeout = 10000; globals.epolltimeout = EPOLL_RUN_TIMEOUT; globals.workers = POSSIBLE_FHNDL; sprintf(globals.identline, "%s", ident); // globals.modules[0].id = 0; globals.modules[0].enabled = true; globals.modules[0].name = "plain"; globals.modules[0].process_function = &process_plain; globals.modules[0].init_function = NULL; globals.modules[1].id = 1; globals.modules[1].enabled = false; globals.modules[1].name = "riak"; globals.modules[1].process_function = &process_riak; globals.modules[1].init_function = &init_riak_module; globals.modules[1].close_function = &close_riak; globals.modules[2].id = 2; globals.modules[2].enabled = false; globals.modules[2].name = "esearch"; globals.modules[2].process_function = &process_esearch; globals.modules[2].init_function = &init_esearch_module; globals.modules[2].close_function = &close_esearch; globals.modules[3].id = 3; globals.modules[3].enabled = false; globals.modules[3].name = "webhdfs"; globals.modules[3].process_function = &process_webhdfs; globals.modules[3].init_function = &init_webhdfs_module; globals.modules[3].close_function = &close_webhdfs; CPRESS_FUNCT[0].type = SNAPPY; CPRESS_FUNCT[0].compress_function = &compress_snappy; CPRESS_FUNCT[1].type = GZIP; CPRESS_FUNCT[1].compress_function = &compress_gzip; set_sig_handler(); static const char *optString = "s:D:B:u:g:p:l:c:H:P:vfhd?"; opt = getopt_long(argc, argv, optString, longOpts, &longIndex); while (opt != -1) { switch (opt) { case 'p': if (!is_valid_opt(optarg)) { print_usage(); } arguments.pidfile = xrealloc(arguments.pidfile, (sizeof (char) * strlen(optarg) + 1)); strcpy(arguments.pidfile, optarg); break; case 'l': if (!is_valid_opt(optarg)) { print_usage(); } arguments.logdir = xrealloc(arguments.pidfile, (sizeof (char) * strlen(optarg) + 1)); strcpy(arguments.logdir, optarg); break; case 'c': if (!is_valid_opt(optarg)) { print_usage(); } arguments.configfile = xrealloc(arguments.configfile, (sizeof (char) * strlen(optarg) + 1)); strcpy(arguments.configfile, optarg); break; case 'H': if (!isValidIP(optarg)) { print_usage(); } if (NULL != (host = nslookup(optarg))) { arguments.listen_host = xrealloc(arguments.listen_host, (sizeof (char) * strlen(optarg) + 1)); strcpy(arguments.listen_host, host); } else { printf("%s\n", hstrerror(h_errno)); exit(EXIT_FAILURE); } break; case 'u': if (!is_valid_opt(optarg)) { print_usage(); } arguments.user = xrealloc(arguments.user, (sizeof (char) * strlen(optarg) + 1)); strcpy(arguments.user, optarg); break; case 'g': arguments.group = xrealloc(arguments.group, (sizeof (char) * strlen(optarg) + 1)); strcpy(arguments.group, optarg); break; case 'D': if (!is_valid_opt(optarg)) { print_usage(); } arguments.wdir = xrealloc(arguments.wdir, (sizeof (char) * strlen(optarg) + 1)); strcpy(arguments.wdir, optarg); break; case 's': if (!is_valid_opt(optarg)) { print_usage(); } arguments.storagedir = xrealloc(arguments.storagedir, (sizeof (char) * strlen(optarg) + 1)); strcpy(arguments.storagedir, optarg); break; case 'B': if (!is_valid_opt(optarg)) { print_usage(); } arguments.bufferdir = xrealloc(arguments.bufferdir, (sizeof (char) * strlen(optarg) + 1)); strcpy(arguments.bufferdir, optarg); break; case 'P': arguments.listen_port = atoi(optarg); break; case 'v': arguments.verbosity++; break; case 'f': arguments.foreground++; break; case 'h': print_usage(); break; case 'd': arguments.debuginfo++; arguments.verbosity++; break; default: if (!is_valid_opt(optarg)) { print_usage(); } break; } opt = getopt_long(argc, argv, optString, longOpts, &longIndex); } if (false == DirectoryExists(arguments.storagedir)) { printf("Specified storage directory '%s' does not exists\n", arguments.storagedir); printf("Creating new one\n"); if (0 > mkdir(arguments.storagedir, 0755)) { printf("Can not create storage directory - %s\n", strerror(errno)); exit(EXIT_FAILURE); } else { setRightOwner(arguments.storagedir); } } setRightOwner(arguments.storagedir); if (false == DirectoryExists(arguments.bufferdir)) { printf("Specified buffer directory '%s' does not exists\n", arguments.bufferdir); printf("Creating new one\n"); if (0 > mkdir(arguments.bufferdir, 0755)) { printf("Can not create buffer directory - %s\n", strerror(errno)); exit(EXIT_FAILURE); } else { setRightOwner(arguments.bufferdir); } } setRightOwner(arguments.bufferdir); if (true == DirectoryExists(arguments.pidfile)) { printf("Specified pid file '%s' is a directory\n", arguments.pidfile); exit(EXIT_FAILURE); } if (false == DirectoryExists(arguments.wdir)) { printf("Specified working directory '%s' does not exists or not a directory\n", arguments.wdir); exit(EXIT_FAILURE); } if (arguments.foreground == 1) { printf("Running in foreground mode...\n"); setbuf(stdout, 0); setbuf(stdin, 0); setbuf(stderr, 0); } if (arguments.verbosity == 1) { printf("Verbosity enabled\n"); } if (false == FileExists(arguments.configfile)) { printf("Specified file '%s' does not exists\n", arguments.configfile); exit(EXIT_FAILURE); } else if (true == DirectoryExists(arguments.configfile)) { printf("Specified file '%s' is a directory\n", arguments.configfile); exit(EXIT_FAILURE); } else { setRightOwner(arguments.configfile); if (true != hasRightOwner(arguments.configfile)) { printf("The file '%s' has invalid owner/group\nMust be %s:%s\n", arguments.configfile, arguments.user, arguments.group); exit(EXIT_FAILURE); } } if (false == DirectoryExists(arguments.logdir)) { printf("Specified log directory '%s' does not exists\n", arguments.logdir); printf("Creating new one\n"); if (0 > mkdir(arguments.logdir, 0755)) { printf("Can not create log directory - %s\n", strerror(errno)); exit(EXIT_FAILURE); } else { setRightOwner(arguments.logdir); } } setRightOwner(arguments.logdir); if (true != hasRightOwner(arguments.logdir)) { printf("The file '%s' has invalid owner/group\nMust be %s:%s\n", arguments.logdir, arguments.user, arguments.group); exit(EXIT_FAILURE); } glob = readConfig(arguments.configfile); checkPid(); openCustomLog(); openDebugLog(); openConLog(); openAccessLog(); if (!(pwd = getpwnam(arguments.user))) { snprintf(TMP_MSG, MAXLINE, "No such user: %s. Quitting!", arguments.user); writeToCustomLog(TMP_MSG); exit(EXIT_FAILURE); } else my_uid = pwd->pw_uid; if (!(grp = getgrnam(arguments.group))) { snprintf(TMP_MSG, MAXLINE, "No such group: %s. Quitting!", arguments.group); writeToCustomLog(TMP_MSG); exit(EXIT_FAILURE); } else my_gid = grp->gr_gid; if (arguments.foreground == 0) { pid = fork(); if (pid < 0) { snprintf(TMP_MSG, MAXLINE, "%s", "Can not fork! [Invalid PID]"); writeToCustomLog(TMP_MSG); removePid(); exit(EXIT_FAILURE); } /* If we got a good PID, then we can exit the parent process. */ if (pid > 0) { exit(EXIT_SUCCESS); } // snprintf(TMP_MSG, MAXLINE, "%s", "Forked successfully"); writeToCustomLog(TMP_MSG); /* Change the file mode mask */ umask(027); /* Create a new SID for the child process */ sid = setsid(); if (sid < 0) { snprintf(TMP_MSG, MAXLINE, "%s", "Can not create child process"); writeToCustomLog(TMP_MSG); /* Log the failure */ removePid(); exit(EXIT_FAILURE); } snprintf(TMP_MSG, MAXLINE, "%s", "Daemonizing"); writeToCustomLog(TMP_MSG); /* Change the current working directory */ if ((chdir(arguments.wdir)) < 0) { snprintf(TMP_MSG, MAXLINE, "%s", "Can not change current directory"); writeToCustomLog(TMP_MSG); /* Log the failure */ exit(EXIT_FAILURE); } snprintf(TMP_MSG, MAXLINE, "%s", "Working directory changed"); writeToCustomLog(TMP_MSG); } savePid(); if ((setgid(my_gid)) < 0) { snprintf(TMP_MSG, MAXLINE, "ERROR: setgid(%d) failed: %s", my_gid, strerror(errno)); writeToCustomLog(TMP_MSG); halt(); } snprintf(TMP_MSG, MAXLINE, "Group ID changed to %d", my_gid); writeToCustomLog(TMP_MSG); if ((setuid(my_uid)) < 0) { snprintf(TMP_MSG, MAXLINE, "ERROR: setuid(%d) failed: %s", my_uid, strerror(errno)); writeToCustomLog(TMP_MSG); halt(); } snprintf(TMP_MSG, MAXLINE, "User ID changed to %d", my_gid); writeToCustomLog(TMP_MSG); if (arguments.foreground == 0) { snprintf(TMP_MSG, MAXLINE, "%s", "Closing descriptors & disabling verbosity.\nEnsure you have set right permissions for your log file.\nWatch your log file for further information\n"); VERBOSE(TMP_MSG); arguments.verbosity = 0; /* Close out the standard file descriptors */ close(STDIN_FILENO); close(STDOUT_FILENO); close(STDERR_FILENO); } //////////////////////////////////////////////////////////////////////////////// // init_arrays(); init_webhdfs_arrays(); init_active_c_table(); init_descriptors(); scan_connections(); // //////////////////////////////////////////////////////////////////////////////// spawn_threads(); do_listen(); //////////////////////////////////////////////////////////////////////////////// /* This never happens */ return 0; }
int main(int argc, char *argv[]) { // make sure there are inputted values for the needed variables if (argc != 4) { printf("usage: ./findDCs numOfSubs numOfDomains numOfProcesses\n"); return 0; } // validate the inputted values int numOfSubs = atoi(argv[1]); int numOfDomains = atoi(argv[2]); int numOfProcesses = atoi(argv[3]); if (numOfSubs < 1 || numOfDomains < 1 || numOfProcesses > numOfDomains) { printf("numOfSubs, numOfDomains, and numOfProcesses must be greater than 0; numOfProcesses cannot be greater than numOfDomains\n"); return 0; } char *subs[numOfSubs]; printf("\n"); // open the subs file FILE *fp = fopen("subs.txt", "r"); // malloc space for the subs int i, j, k; for (i = 0; i < numOfSubs; i++) { subs[i] = malloc(100); } // read in the subs i = 0; while (fgets(subs[i++], 99, fp) != NULL && i < numOfSubs); // close the file fclose(fp); // remove all new lines for (i = 0; i < numOfSubs; i++) { char *p = strstr(subs[i], "\n"); *p = '\0'; } // divide the domains if (mkdir("domains", 0700) == 0) { char command[100]; sprintf(command, "./divideDomains %d %d", numOfDomains, numOfProcesses); system(command); } // create the processes and start making DNS queries pid_t pids[numOfProcesses]; for (i = 0; i < numOfProcesses; i++) { pid_t pid = fork(); if (pid == 0) { int start = i * (numOfDomains / numOfProcesses); int end = start + (numOfDomains / numOfProcesses); if (i == (numOfProcesses - 1)) { end += (numOfDomains % numOfProcesses); } char filename[100]; sprintf(filename, "domains/domains%d.txt", i); fp = fopen(filename, "r"); // malloc space for the domains char *domains[end - start]; for (j = 0; j < end - start; j++) { domains[j] = malloc(200); } // read in the domains j = 0; while (fgets(domains[j++], 199, fp) != NULL); // close the file fclose(fp); // loop through all domains for (j = 0; j < end - start; j++) { char *p = strstr(domains[j], "\n"); *p = '\0'; nslookup(domains[j]); } // loop through all subdomains for (j = 0; j < numOfSubs; j++) { for (k = 0; k < end - start; k++) { char subdomain[300]; sprintf(subdomain, "%s.%s", subs[j], domains[k]); //printf("%s\n", subdomain); if (gethostbyname(subdomain)) { nslookup(subdomain); } } } // free the subs for (i = 0; i < numOfSubs; i++) { free(subs[i]); } // free the domains for (i = 0; i < end - start; i++) { free(domains[i]); } return 0; } else { pids[i] = pid; } } // wait for the processes to finish for (i = 0; i < numOfProcesses; i++) { waitpid(pids[i], NULL, 0); } // free the subs for (i = 0; i < numOfSubs; i++) { free(subs[i]); } // delete the divided domains folder system("rm -rf domains"); return 0; }
int main(int argc, char* argv[]) { int i = 0, res = 0, index = 0, datalen = 0; char** data = NULL; nc_t nyan = { 1, 0, 0, NULL, }; struct option opts[] = { {"send", no_argument, NULL, 's'}, {"port", required_argument, NULL, 'p'}, {"help", no_argument, NULL, 'h'}, {"version", no_argument, NULL, 'v'}, {0, 0, 0, 0}, }; while ((res = getopt_long(argc, argv, "sp:hv", opts, &index)) != -1) { switch (res) { case 's': nyan.sflag = 1; break; case 'p': for (i = 0; i < strlen(optarg); i++) { if (!isdigit(*(optarg + i))) { fprintf(stderr, "%s: %s: invalid port number\n", PROGNAME, optarg); return -1; } } nyan.net_port = atoi(optarg); nyan.pflag = 1; break; case 'h': print_usage(); case 'v': print_version(); case '?': return -1; } } /* check required argument */ if (optind != (argc - 1)) { fprintf(stderr, "%s: invalid values\n", PROGNAME); return 1; } /* set default port */ if (nyan.pflag == 0) nyan.net_port = PORT; /* set hostname */ if (is_ipaddr(argv[optind]) == 0) { nyan.net_ipv4_addr = nslookup(argv[optind]); } else { if ((nyan.net_ipv4_addr = (char*)malloc(sizeof(char) * (strlen(argv[optind])) + 1)) == NULL) fprintf(stderr, "%s: malloc() failure\n", PROGNAME); else strcpy(nyan.net_ipv4_addr, argv[optind]); } /* reading stdin */ if (nyan.sflag == 1) { if ((data = p_read_file_char(512, 512, stdin)) == NULL) { fprintf(stderr, "%s: p_read_file_char() failure\n", PROGNAME); return 3; } datalen = p_count_file_lines(data); if (sender(&nyan, data, datalen) != 0) { fprintf(stderr, "%s: sender failure\n", PROGNAME); release(&nyan); return 4; } } release(&nyan); return 0; }