/* * setescape - add chars to the set we escape on transmission. */ static int setescape( char **argv) { int n, ret; char *p, *endp; p = *argv; ret = 1; while (*p) { n = strtol(p, &endp, 16); if (p == endp) { option_error("escape parameter contains invalid hex number '%s'", p); return 0; } p = endp; if (n < 0 || n == 0x5E || n > 0xFF) { option_error("can't escape character 0x%x", n); ret = 0; } else xmit_accm[0][n >> 5] |= 1 << (n & 0x1F); while (*p == ',' || *p == ' ') ++p; } return ret; }
/* * Option parsing. */ static int setbsdcomp( char **argv) { int rbits, abits; char *str, *endp; str = *argv; abits = rbits = strtol(str, &endp, 0); if (endp != str && *endp == ',') { str = endp + 1; abits = strtol(str, &endp, 0); } if (*endp != 0 || endp == str) { option_error("invalid parameter '%s' for bsdcomp option", *argv); return 0; } if ((rbits != 0 && (rbits < BSD_MIN_BITS || rbits > BSD_MAX_BITS)) || (abits != 0 && (abits < BSD_MIN_BITS || abits > BSD_MAX_BITS))) { option_error("bsdcomp option values must be 0 or %d .. %d", BSD_MIN_BITS, BSD_MAX_BITS); return 0; } if (rbits > 0) { ccp_wantoptions[0].bsd_compress = 1; ccp_wantoptions[0].bsd_bits = rbits; } else ccp_wantoptions[0].bsd_compress = 0; if (abits > 0) { ccp_allowoptions[0].bsd_compress = 1; ccp_allowoptions[0].bsd_bits = abits; } else ccp_allowoptions[0].bsd_compress = 0; return 1; }
/* * setwinsaddr - set the wins address(es) * This is primrarly used with the Samba package under UNIX or for pointing * the caller to the existing WINS server on a Windows NT platform. */ static int setwinsaddr( char **argv) { uint32_t wins; struct hostent *hp; wins = inet_addr(*argv); if (wins == (uint32_t) -1) { if ((hp = gethostbyname(*argv)) == NULL) { option_error("invalid address parameter '%s' for ms-wins option", *argv); return 0; } wins = *(uint32_t *)hp->h_addr; } /* if there is no primary then update it. */ if (ipcp_allowoptions[0].winsaddr[0] == 0) ipcp_allowoptions[0].winsaddr[0] = wins; /* always set the secondary address value to the same value. */ ipcp_allowoptions[0].winsaddr[1] = wins; return (1); }
int main(int argc, char **argv) { s_dir *d; static s_arg *a; s_err *e; e = malloc(sizeof(s_err)); a = malloc(sizeof(s_arg)); d = malloc(sizeof(s_dir)); a = setzero(a); e = seterrzero(e); e = all_check(d, a, e, argv); if (argc > 2) argv = sort_arg(argv); //ft_putnbr(count_arg(argv)); //ft_putnbr(ft_strcmp("b.out","a")); option_error(e); //DEBUG //a = argcheck(a, argv); //remettre quand jaurais fait le tri des arguments if(!argv[a->arc]) argv[a->arc] = "."; ft_dirread(d, a, argv); //ft_check(d, argv); return(0); }
const ConfigOption ConfigParser::operator[] (const std::string&& option) const { try { return _config->at(option); } catch (std::out_of_range) { throw option_error(ERR_NO_OPTION + ": " + option); } }
/* ----------------------------------------------------------------------------- ----------------------------------------------------------------------------- */ void serial_process_extra_options() { char str[MAXPATHLEN]; struct stat statbuf; if (device && !ptycommand) { // first, transform device name str[0] = 0; if (device[0] != '/') { strlcat(str, DIR_TTYS, sizeof(str)); if ((device[0] != 't') || (device[1] != 't') || (device[2] != 'y') || (device[3] != 'd')) strlcat(str, "cu.", sizeof(str)); } strlcat(str, device, sizeof(str)); strlcpy(devnam, str, sizeof(devnam)); default_device = 0; // then check if device is there if (stat(devnam, &statbuf) < 0) { if (errno == ENOENT) { option_error("Device '%s' does not exist", devnam); die(EXIT_DEVICE_ERROR); } else fatal("Couldn't stat device %s: %m", devnam); } } if (old_process_extra_options) (*old_process_extra_options)(); }
int main (int argc, char **argv, char *env[]) { t_goptions goptions; process_command_line_arguments (&goptions, argc, argv); /* sets input file or stdin */ if (goptions.in_filename == NULL ) { goptions.in_file = stdin; } else { if ((goptions.in_file = fopen (goptions.in_filename, "r")) == NULL) option_error (10, "Unable to open input file.\n"); } /* sets output file or stdout */ if (goptions.out_filename == NULL ) { goptions.out_file = stdout; } else { if ((goptions.out_file = fopen (goptions.out_filename, "w")) == NULL) { if (goptions.in_filename != NULL) fclose (goptions.in_file); option_error (11, "Unable to open output file.\n"); } } switch (goptions.logtool_mode) { case GLOBAL: stats_general (&goptions); break; case PER_HOST: stats_max_hosts (&goptions); break; case FILTER: stats_filter_mode (&goptions); break; case NOT_DEFINED: // it will never reach this point (only to avoid warnings from compiler) break; } if (goptions.in_filename != NULL) fclose (goptions.in_file); if (goptions.out_filename != NULL) fclose (goptions.out_file); exit (0); }
/* * sys_check_options - check the options that the user specified */ int sys_check_options() { if (demand) { option_error("Sorry - demand-dialling is not supported under Ultrix\n"); return 0; } return 1; }
/* ** Get the field named name as an integer. ** The table MUST be at position 2. */ static long longtabparam (lua_State *L, const char *name, int def) { strgettable (L, name); if (lua_isnil (L, -1)) return def; else if (lua_isnumber (L, -1)) return (long)lua_tonumber (L, -1); else return option_error (L, name, "number"); }
/* ** Get the field named name as a double. ** The table MUST be at position 2. */ static double numbertabparam (lua_State *L, const char *name, double def) { strgettable (L, name); if (lua_isnil (L, -1)) return def; else if (lua_isnumber (L, -1)) return lua_tonumber (L, -1); else return option_error (L, name, "number"); }
/* ** Get the field named name as a boolean. ** The table MUST be at position 2. */ static int booltabparam (lua_State *L, const char *name, int def) { strgettable (L, name); if (lua_isnil (L, -1)) return def; else if (lua_isboolean (L, -1)) return lua_toboolean (L, -1); else return option_error (L, name, "boolean"); }
static int radius_get_server(char **argv) { char *p, *endp; struct servent *servp; struct hostent *hostp; struct in_addr addr; /* Determine the port */ p = strchr(*argv, ':'); if (p != NULL) { radius_auth_port = strtoul(p+1, &endp, 10); if (*endp) { option_error("invalid RADIUS server port '%s'", p+1); return 0; } } if (radius_auth_port == 0) { servp = getservbyname("radacct", "udp"); if (servp != NULL) { radius_auth_port = ntohs(servp->s_port); } else { radius_auth_port = PW_AUTH_UDP_PORT; } } /* Remove port if present */ if (p != NULL) *p = 0; /* Determine the server IP address */ if (inet_aton(*argv, &addr) == 0) { hostp = gethostbyname(*argv); if (hostp == NULL) { option_error("invalid RADIUS server '%s'", *argv); return 0; } memcpy((char*)&addr, hostp->h_addr, sizeof(addr)); } if (p != NULL) *p = ':'; radius_server = ntohl(addr.s_addr); return 1; }
ConfigOption::operator channeling::ChannelDirection() const { if (_value == "input") return channeling::ChannelDirection::Input; else if (_value == "output") return channeling::ChannelDirection::Output; else if (_value == "inout") return channeling::ChannelDirection::Bidirectional; throw option_error(ERR_WRONG_DIR + ": " + _value); }
ConfigOption::operator int() const { try { return std::stoi(_value); } catch (std::invalid_argument) { if ((strutil::cistrcmp(_value, "true")) || (strutil::cistrcmp(_value, "false"))) return (strutil::cistrcmp(_value, "true")); throw option_error(ERR_MALFORMED_VAL + ": " + _value); } }
/* ** Get the field named name as a string. ** The table MUST be at position 2. */ static const char *strtabparam (lua_State *L, const char *name, char *def) { strgettable (L, name); if (lua_isnil (L, -1)) return def; else if (lua_isstring (L, -1)) return lua_tostring (L, -1); else { option_error (L, name, "string"); return NULL; } }
inline void remote_cert_ku(const OptionList& opt, std::vector<unsigned int>& ku) { const Option* o = opt.get_ptr("remote-cert-ku"); if (o) { if (o->empty()) throw option_error("remote-cert-ku: no hex values specified"); else if (o->size() >= 64) throw option_error("remote-cert-ku: too many parameters"); else { try { for (size_t i = 1; i < o->size(); ++i) ku.push_back(parse_hex_number<unsigned int>(o->get(i, 16).c_str())); } catch (parse_hex_error&) { throw option_error("remote-cert-ku: error parsing hex value list"); } } } }
static int radius_nas_ip_address(char **argv) { struct in_addr addr; if (inet_aton(*argv, &addr) == 0) { option_error("invalid RADIUS NAS IP address '%s'", *argv); return 0; } nas_ip_address = ntohl(addr.s_addr); return 1; }
/* * auth_check_options - called to check authentication options. */ void auth_check_options(void) { lcp_options *wo = &lcp_wantoptions[0]; int can_auth; ipcp_options *ipwo = &ipcp_wantoptions[0]; u_int32_t remote; /* Default our_name to hostname, and user to our_name */ if (our_name[0] == 0 || usehostname) strcpy(our_name, hostname); if (user[0] == 0) strcpy(user, our_name); /* If authentication is required, ask peer for CHAP or PAP. */ if (auth_required && !wo->neg_chap && !wo->neg_upap) { wo->neg_chap = 1; wo->neg_upap = 1; } /* * Check whether we have appropriate secrets to use * to authenticate the peer. */ can_auth = wo->neg_upap && (uselogin || have_pap_secret()); if (!can_auth && wo->neg_chap) { remote = ipwo->accept_remote? 0: ipwo->hisaddr; can_auth = have_chap_secret(remote_name, our_name, remote); } if (auth_required && !can_auth) { option_error("peer authentication required but no suitable secret(s) found\n"); if (remote_name[0] == 0) option_error("for authenticating any peer to us (%s)\n", our_name); else option_error("for authenticating peer %s to us (%s)\n", remote_name, our_name); exit(1); } /* * Check whether the user tried to override certain values * set by root. */ if (!auth_required && auth_req_info.priv > 0) { if (!default_device && devnam_info.priv == 0) { option_error("can't override device name when noauth option used"); exit(1); } if ((connector != NULL && connector_info.priv == 0) || (disconnector != NULL && disconnector_info.priv == 0) || (welcomer != NULL && welcomer_info.priv == 0)) { option_error("can't override connect, disconnect or welcome"); option_error("option values when noauth option used"); exit(1); } } }
inline Type ns_cert_type(const OptionList& opt) { const Option* o = opt.get_ptr("ns-cert-type"); if (o) { const std::string& ct = o->get_optional(1, 16); if (ct == "server") return SERVER; else if (ct == "client") return CLIENT; else throw option_error("ns-cert-type must be 'client' or 'server'"); } return NONE; }
static int setdeflate( char **argv) { int rbits, abits; char *str, *endp; str = *argv; abits = rbits = strtol(str, &endp, 0); if (endp != str && *endp == ',') { str = endp + 1; abits = strtol(str, &endp, 0); } if (*endp != 0 || endp == str) { option_error("invalid parameter '%s' for deflate option", *argv); return 0; } if ((rbits != 0 && (rbits < DEFLATE_MIN_SIZE || rbits > DEFLATE_MAX_SIZE)) || (abits != 0 && (abits < DEFLATE_MIN_SIZE || abits > DEFLATE_MAX_SIZE))) { option_error("deflate option values must be 0 or %d .. %d", DEFLATE_MIN_SIZE, DEFLATE_MAX_SIZE); return 0; } if (rbits > 0) { ccp_wantoptions[0].deflate = 1; ccp_wantoptions[0].deflate_size = rbits; } else ccp_wantoptions[0].deflate = 0; if (abits > 0) { ccp_allowoptions[0].deflate = 1; ccp_allowoptions[0].deflate_size = abits; } else ccp_allowoptions[0].deflate = 0; return 1; }
/* * setupapfile - specifies UPAP info for authenticating with peer. */ static int setupapfile(char **argv) { FILE * ufile; int l; lcp_allowoptions[0].neg_upap = 1; /* open user info file */ seteuid(getuid()); ufile = fopen(*argv, "r"); seteuid(0); if (ufile == NULL) { option_error("unable to open user login data file %s", *argv); return 0; } check_access(ufile, *argv); /* get username */ if (fgets(user, MAXNAMELEN - 1, ufile) == NULL || fgets(passwd, MAXSECRETLEN - 1, ufile) == NULL){ option_error("unable to read user login data file %s", *argv); return 0; } fclose(ufile); /* get rid of newlines */ l = strlen(user); if (l > 0 && user[l-1] == '\n') user[l-1] = 0; l = strlen(passwd); if (l > 0 && passwd[l-1] == '\n') passwd[l-1] = 0; return (1); }
/* * setvjslots - set maximum number of connection slots for VJ compression */ static int setvjslots( char **argv) { int value; if (!int_option(*argv, &value)) return 0; if (value < 2 || value > 16) { option_error("vj-max-slots value must be between 2 and 16"); return 0; } ipcp_wantoptions [0].maxslotindex = ipcp_allowoptions[0].maxslotindex = value - 1; return 1; }
void pppoe_check_options(void) { unsigned int mac[ETH_ALEN]; int i; if (pppoe_reqd_mac != NULL) { if (sscanf(pppoe_reqd_mac, "%x:%x:%x:%x:%x:%x", &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]) != ETH_ALEN) { option_error("cannot parse pppoe-mac option value"); exit(EXIT_OPTION_ERROR); } for (i = 0; i < 6; ++i) conn->req_peer_mac[i] = mac[i]; conn->req_peer = 1; } lcp_allowoptions[0].neg_accompression = 0; lcp_wantoptions[0].neg_accompression = 0; lcp_allowoptions[0].neg_asyncmap = 0; lcp_wantoptions[0].neg_asyncmap = 0; lcp_allowoptions[0].neg_pcompression = 0; lcp_wantoptions[0].neg_pcompression = 0; if (lcp_allowoptions[0].mru > MAX_PPPOE_MTU) { lcp_allowoptions[0].mru = MAX_PPPOE_MTU; } if (lcp_wantoptions[0].mru > MAX_PPPOE_MTU) { lcp_wantoptions[0].mru = MAX_PPPOE_MTU; } /* Save configuration */ conn->mtu = lcp_allowoptions[0].mru; conn->mru = lcp_wantoptions[0].mru; ccp_allowoptions[0].deflate = 0; ccp_wantoptions[0].deflate = 0; ipcp_allowoptions[0].neg_vj = 0; ipcp_wantoptions[0].neg_vj = 0; ccp_allowoptions[0].bsd_compress = 0; ccp_wantoptions[0].bsd_compress = 0; }
static int tacacs_get_server(char **argv) { struct in_addr addr; struct hostent *h; if (inet_aton(*argv, &addr) == 0) { h = gethostbyname(*argv); if (h == NULL) { option_error("invalid TACACS+ server '%s'", *argv); return 0; } memcpy((char*)&addr, h->h_addr, sizeof(addr)); } tac_server = addr.s_addr; return 1; }
/* * privgroup - allow members of the group to have privileged access. */ static int privgroup(char **argv) { struct group *g; int i; g = getgrnam(*argv); if (g == 0) { option_error("group %s is unknown", *argv); return 0; } for (i = 0; i < ngroups; ++i) { if (groups[i] == g->gr_gid) { privileged = 1; break; } } return 1; }
inline void remote_cert_tls(const OptionList& opt, std::vector<unsigned int>& ku, std::string& eku) { const Option* o = opt.get_ptr("remote-cert-tls"); if (o) { const std::string& ct = o->get_optional(1, 16); if (ct == "server") { ku.push_back(0xa0); ku.push_back(0x88); eku = "TLS Web Server Authentication"; } else if (ct == "client") { ku.push_back(0x80); ku.push_back(0x08); ku.push_back(0x88); eku = "TLS Web Client Authentication"; } else throw option_error("remote-cert-tls must be 'client' or 'server'"); } }
void parse_comand_line(int argc, char* argv[], char*& input_filename, char*& input_sites, char*& mesh_format, char*& tetgen_params) { int i = 1; if (argc == 1) { printf("Usage: GenerateMesh [OPTION] FILE\n"); exit(0); } // Parse command line while (i < argc) { // Parse --help if (strcmp (argv[i], "--help") == 0) { long_usage(); exit(0); } // Parse --format option if (strcmp (argv[i], "--format") == 0) { i++; if (i == argc) option_error(); mesh_format = argv[i]; if (check_format(mesh_format)) option_error(); i++; if (i == argc) option_error(); continue; } // Parse --format option if (strcmp (argv[i], "--tetgen-params") == 0) { i++; if (i == argc) option_error(); sprintf(tetgen_params, "%s%s", tetgen_default_params, argv[i]); i++; if (i == argc) option_error(); continue; } // Parse --active-sites option if (strcmp (argv[i], "--active-sites") == 0) { i++; if (i == argc) option_error(); input_sites = argv[i]; i++; if (i == argc) option_error(); continue; } // Parse filename input_filename = argv[i]; i++; if (i != argc) option_error(); } }
int main(int argc, char *argv[]) { int i, n, fdflags; struct sigaction sa; FILE *iffile; char *p; struct passwd *pw; struct timeval timo; sigset_t mask; struct protent *protp; struct stat statbuf; int connect_attempts = 0; char numbuf[16]; phase = PHASE_INITIALIZE; p = ttyname(0); if (p) strcpy(devnam, p); strcpy(default_devnam, devnam); script_env = NULL; /* Initialize syslog facilities */ #ifdef ULTRIX openlog("pppd", LOG_PID); #else openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP); setlogmask(LOG_UPTO(LOG_INFO)); #endif if (gethostname(hostname, MAXNAMELEN) < 0 ) { option_error("Couldn't get hostname: %m"); die(1); } hostname[MAXNAMELEN-1] = 0; uid = getuid(); privileged = uid == 0; sprintf(numbuf, "%d", uid); script_setenv("UID", numbuf); /* * Initialize to the standard option set, then parse, in order, * the system options file, the user's options file, * the tty's options file, and the command line arguments. */ for (i = 0; (protp = protocols[i]) != NULL; ++i) (*protp->init)(0); progname = *argv; if (!options_from_file(_PATH_SYSOPTIONS, !privileged, 0, 1) || !options_from_user()) exit(1); scan_args(argc-1, argv+1); /* look for tty name on command line */ if (!options_for_tty() || !parse_args(argc-1, argv+1)) exit(1); /* * Check that we are running as root. */ if (geteuid() != 0) { option_error("must be root to run %s, since it is not setuid-root", argv[0]); die(1); } if (!ppp_available()) { option_error(no_ppp_msg); exit(1); } /* * Check that the options given are valid and consistent. */ sys_check_options(); auth_check_options(); for (i = 0; (protp = protocols[i]) != NULL; ++i) if (protp->check_options != NULL) (*protp->check_options)(); if (demand && connector == 0) { option_error("connect script required for demand-dialling\n"); exit(1); } script_setenv("DEVICE", devnam); sprintf(numbuf, "%d", baud_rate); script_setenv("SPEED", numbuf); /* * If the user has specified the default device name explicitly, * pretend they hadn't. */ if (!default_device && strcmp(devnam, default_devnam) == 0) default_device = 1; if (default_device) nodetach = 1; /* * Initialize system-dependent stuff and magic number package. */ sys_init(); magic_init(); if (debug) setlogmask(LOG_UPTO(LOG_DEBUG)); /* * Detach ourselves from the terminal, if required, * and identify who is running us. */ if (nodetach == 0) detach(); pid = getpid(); p = getlogin(); stime = time(NULL); if (p == NULL) { pw = getpwuid(uid); if (pw != NULL && pw->pw_name != NULL) p = pw->pw_name; else p = "(unknown)"; } syslog(LOG_NOTICE, "pppd %s.%d%s started by %s, uid %d", VERSION, PATCHLEVEL, IMPLEMENTATION, p, uid); /* * Compute mask of all interesting signals and install signal handlers * for each. Only one signal handler may be active at a time. Therefore, * all other signals should be masked when any handler is executing. */ sigemptyset(&mask); sigaddset(&mask, SIGHUP); sigaddset(&mask, SIGINT); sigaddset(&mask, SIGTERM); sigaddset(&mask, SIGCHLD); #define SIGNAL(s, handler) { \ sa.sa_handler = handler; \ if (sigaction(s, &sa, NULL) < 0) { \ syslog(LOG_ERR, "Couldn't establish signal handler (%d): %m", s); \ die(1); \ } \ } sa.sa_mask = mask; sa.sa_flags = 0; SIGNAL(SIGHUP, hup); /* Hangup */ SIGNAL(SIGINT, term); /* Interrupt */ SIGNAL(SIGTERM, term); /* Terminate */ SIGNAL(SIGCHLD, chld); SIGNAL(SIGUSR1, toggle_debug); /* Toggle debug flag */ SIGNAL(SIGUSR2, open_ccp); /* Reopen CCP */ /* * Install a handler for other signals which would otherwise * cause pppd to exit without cleaning up. */ SIGNAL(SIGABRT, bad_signal); SIGNAL(SIGALRM, bad_signal); SIGNAL(SIGFPE, bad_signal); SIGNAL(SIGILL, bad_signal); SIGNAL(SIGPIPE, bad_signal); SIGNAL(SIGQUIT, bad_signal); SIGNAL(SIGSEGV, bad_signal); #ifdef SIGBUS SIGNAL(SIGBUS, bad_signal); #endif #ifdef SIGEMT SIGNAL(SIGEMT, bad_signal); #endif #ifdef SIGPOLL SIGNAL(SIGPOLL, bad_signal); #endif #ifdef SIGPROF SIGNAL(SIGPROF, bad_signal); #endif #ifdef SIGSYS SIGNAL(SIGSYS, bad_signal); #endif #ifdef SIGTRAP SIGNAL(SIGTRAP, bad_signal); #endif #ifdef SIGVTALRM SIGNAL(SIGVTALRM, bad_signal); #endif #ifdef SIGXCPU SIGNAL(SIGXCPU, bad_signal); #endif #ifdef SIGXFSZ SIGNAL(SIGXFSZ, bad_signal); #endif /* * Apparently we can get a SIGPIPE when we call syslog, if * syslogd has died and been restarted. Ignoring it seems * be sufficient. */ signal(SIGPIPE, SIG_IGN); /* * If we're doing dial-on-demand, set up the interface now. */ if (demand) { /* * Open the loopback channel and set it up to be the ppp interface. */ open_ppp_loopback(); syslog(LOG_INFO, "Using interface ppp%d", ifunit); sprintf(ifname, "ppp%d", ifunit); script_setenv("IFNAME", ifname); create_pidfile(); /* write pid to file */ /* * Configure the interface and mark it up, etc. */ demand_conf(); } for (;;) { need_holdoff = 1; if (demand) { /* * Don't do anything until we see some activity. */ phase = PHASE_DORMANT; kill_link = 0; demand_unblock(); for (;;) { wait_loop_output(timeleft(&timo)); calltimeout(); if (kill_link) { if (!persist) die(0); kill_link = 0; } if (get_loop_output()) break; reap_kids(); } /* * Now we want to bring up the link. */ demand_block(); syslog(LOG_INFO, "Starting link"); } /* * Lock the device if we've been asked to. */ if (lockflag && !default_device) { if (lock(devnam) < 0) goto fail; locked = 1; } /* * Open the serial device and set it up to be the ppp interface. * First we open it in non-blocking mode so we can set the * various termios flags appropriately. If we aren't dialling * out and we want to use the modem lines, we reopen it later * in order to wait for the carrier detect signal from the modem. */ while ((ttyfd = open(devnam, O_NONBLOCK | O_RDWR, 0)) < 0) { if (errno != EINTR) syslog(LOG_ERR, "Failed to open %s: %m", devnam); if (!persist || errno != EINTR) goto fail; } if ((fdflags = fcntl(ttyfd, F_GETFL)) == -1 || fcntl(ttyfd, F_SETFL, fdflags & ~O_NONBLOCK) < 0) syslog(LOG_WARNING, "Couldn't reset non-blocking mode on device: %m"); hungup = 0; kill_link = 0; /* * Do the equivalent of `mesg n' to stop broadcast messages. */ if (fstat(ttyfd, &statbuf) < 0 || fchmod(ttyfd, statbuf.st_mode & ~(S_IWGRP | S_IWOTH)) < 0) { syslog(LOG_WARNING, "Couldn't restrict write permissions to %s: %m", devnam); } else tty_mode = statbuf.st_mode; /* run connection script */ if (connector && connector[0]) { MAINDEBUG((LOG_INFO, "Connecting with <%s>", connector)); /* * Set line speed, flow control, etc. * On most systems we set CLOCAL for now so that we can talk * to the modem before carrier comes up. But this has the * side effect that we might miss it if CD drops before we * get to clear CLOCAL below. On systems where we can talk * successfully to the modem with CLOCAL clear and CD down, * we can clear CLOCAL at this point. */ set_up_tty(ttyfd, 1); /* drop dtr to hang up in case modem is off hook */ if (!default_device && modem) { setdtr(ttyfd, FALSE); sleep(1); setdtr(ttyfd, TRUE); } if (device_script(connector, ttyfd, ttyfd) < 0) { syslog(LOG_ERR, "Connect script failed"); setdtr(ttyfd, FALSE); connect_attempts++; goto fail; } syslog(LOG_INFO, "Serial connection established."); sleep(1); /* give it time to set up its terminal */ } connect_attempts = 0; /* we made it through ok */ /* set line speed, flow control, etc.; clear CLOCAL if modem option */ set_up_tty(ttyfd, 0); /* reopen tty if necessary to wait for carrier */ if (connector == NULL && modem) { while ((i = open(devnam, O_RDWR)) < 0) { if (errno != EINTR) syslog(LOG_ERR, "Failed to reopen %s: %m", devnam); if (!persist || errno != EINTR || hungup || kill_link) goto fail; } close(i); } /* run welcome script, if any */ if (welcomer && welcomer[0]) { if (device_script(welcomer, ttyfd, ttyfd) < 0) syslog(LOG_WARNING, "Welcome script failed"); } /* set up the serial device as a ppp interface */ establish_ppp(ttyfd); if (!demand) { syslog(LOG_INFO, "Using interface ppp%d", ifunit); sprintf(ifname, "ppp%d", ifunit); create_pidfile(); /* write pid to file */ /* write interface unit number to file */ for (n = strlen(devnam); n > 0 ; n--) if (devnam[n] == '/') { n++; break; } sprintf(iffilename, "%s%s.if", _PATH_VARRUN, &devnam[n]); if ((iffile = fopen(iffilename, "w")) != NULL) { fprintf(iffile, "ppp%d\n", ifunit); fclose(iffile); } else { syslog(LOG_ERR, "Failed to create if file %s: %m", iffilename); iffilename[0] = 0; } script_setenv("IFNAME", ifname); } /* * Start opening the connection and wait for * incoming events (reply, timeout, etc.). */ syslog(LOG_NOTICE, "Connect: %s <--> %s", ifname, devnam); stime = time(NULL); lcp_lowerup(0); lcp_open(0); /* Start protocol */ for (phase = PHASE_ESTABLISH; phase != PHASE_DEAD; ) { wait_input(timeleft(&timo)); calltimeout(); get_input(); if (kill_link) { lcp_close(0, "User request"); kill_link = 0; } if (open_ccp_flag) { if (phase == PHASE_NETWORK) { ccp_fsm[0].flags = OPT_RESTART; /* clears OPT_SILENT */ (*ccp_protent.open)(0); } open_ccp_flag = 0; } reap_kids(); /* Don't leave dead kids lying around */ } /* * If we may want to bring the link up again, transfer * the ppp unit back to the loopback. Set the * real serial device back to its normal mode of operation. */ clean_check(); if (demand) restore_loop(); disestablish_ppp(ttyfd); /* * Run disconnector script, if requested. * XXX we may not be able to do this if the line has hung up! */ if (disconnector && !hungup) { set_up_tty(ttyfd, 1); if (device_script(disconnector, ttyfd, ttyfd) < 0) { syslog(LOG_WARNING, "disconnect script failed"); } else { syslog(LOG_INFO, "Serial link disconnected."); } } fail: if (ttyfd >= 0) close_tty(); if (locked) { unlock(); locked = 0; } if (!demand) { if (pidfilename[0] != 0 && unlink(pidfilename) < 0 && errno != ENOENT) syslog(LOG_WARNING, "unable to delete pid file: %m"); pidfilename[0] = 0; if (iffile) if (unlink(iffilename) < 0 && errno != ENOENT) syslog(LOG_WARNING, "unable to delete if file: %m"); iffilename[0] = 0; } /* limit to retries? */ if (max_con_attempts) if (connect_attempts >= max_con_attempts) break; if (!persist) die(1); if (demand) demand_discard(); if (holdoff > 0 && need_holdoff) { phase = PHASE_HOLDOFF; TIMEOUT(holdoff_end, NULL, holdoff); do { wait_time(timeleft(&timo)); calltimeout(); if (kill_link) { if (!persist) die(0); kill_link = 0; phase = PHASE_DORMANT; /* allow signal to end holdoff */ } reap_kids(); } while (phase == PHASE_HOLDOFF); } } die(0); return 0; }
static void lookup_realm(char const *user, SERVER **authserver, SERVER **acctserver) { char *realm; FILE *fd; SERVER *accts, *auths, *s; char buffer[512], *p; int line = 0; auths = (SERVER *) malloc(sizeof(SERVER)); auths->max = 0; accts = (SERVER *) malloc(sizeof(SERVER)); accts->max = 0; realm = strrchr(user, '@'); if (realm) { info("Looking up servers for realm '%s'", realm); } else { info("Looking up servers for DEFAULT realm"); } if (realm) { if (*(++realm) == '\0') { realm = NULL; } } if ((fd = fopen(radrealms_config, "r")) == NULL) { option_error("cannot open %s", radrealms_config); free(auths); free(accts); return; } info("Reading %s", radrealms_config); while ((fgets(buffer, sizeof(buffer), fd) != NULL)) { line++; if ((*buffer == '\n') || (*buffer == '#') || (*buffer == '\0')) continue; buffer[strlen(buffer)-1] = '\0'; p = strtok(buffer, "\t "); if (p == NULL || (strcmp(p, "authserver") !=0 && strcmp(p, "acctserver"))) { fclose(fd); option_error("%s: invalid line %d: %s", radrealms_config, line, buffer); free(auths); free(accts); return; } info("Parsing '%s' entry:", p); s = auths; if (p[1] == 'c') { s = accts; } if (s->max >= SERVER_MAX) continue; if ((p = strtok(NULL, "\t ")) == NULL) { fclose(fd); option_error("%s: realm name missing on line %d: %s", radrealms_config, line, buffer); free(auths); free(accts); return; } if ((realm != NULL && strcmp(p, realm) == 0) || (realm == NULL && strcmp(p, "DEFAULT") == 0) ) { info(" - Matched realm %s", p); if ((p = strtok(NULL, ":")) == NULL) { fclose(fd); option_error("%s: server address missing on line %d: %s", radrealms_config, line, buffer); free(auths); free(accts); return; } s->name[s->max] = strdup(p); info(" - Address is '%s'",p); if ((p = strtok(NULL, "\t ")) == NULL) { fclose(fd); option_error("%s: server port missing on line %d: %s", radrealms_config, line, buffer); free(auths); free(accts); return; } s->port[s->max] = atoi(p); info(" - Port is '%d'", s->port[s->max]); s->max++; } else info(" - Skipping realm '%s'", p); } fclose(fd); if (accts->max) *acctserver = accts; if (auths->max) *authserver = auths; return; }
/* This function forks a child which calls child_main(). First, * however, it has to establish communication paths to and from the * newborn child. It creates two socket pairs -- one for writing to * the child (from the parent) and one for reading from the child * (writing to the parent). Since that's four socket ends, each * process has to close the two ends it doesn't need. The remaining * two socket ends are retained for reading and writing. In the * child, the STDIN and STDOUT file descriptors refer to these * sockets. In the parent, the function arguments f_in and f_out are * set to refer to these sockets. */ pid_t local_child(int argc, char **argv, int *f_in, int *f_out, int (*child_main)(int, char*[])) { pid_t pid; int to_child_pipe[2]; int from_child_pipe[2]; /* The parent process is always the sender for a local rsync. */ assert(am_sender); if (fd_pair(to_child_pipe) < 0 || fd_pair(from_child_pipe) < 0) { rsyserr(FERROR, errno, "pipe"); exit_cleanup(RERR_IPC); } pid = do_fork(); if (pid == -1) { rsyserr(FERROR, errno, "fork"); exit_cleanup(RERR_IPC); } if (pid == 0) { am_sender = 0; am_server = 1; filesfrom_fd = -1; munge_symlinks = 0; /* Each side needs its own option. */ chmod_modes = NULL; /* Let the sending side handle this. */ /* Let the client side handle this. */ if (logfile_name) { logfile_name = NULL; logfile_close(); } if (remote_option_cnt) { int rc = remote_option_cnt + 1; const char **rv = remote_options; if (!parse_arguments(&rc, &rv)) { option_error(); exit_cleanup(RERR_SYNTAX); } } if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 || close(to_child_pipe[1]) < 0 || close(from_child_pipe[0]) < 0 || dup2(from_child_pipe[1], STDOUT_FILENO) < 0) { rsyserr(FERROR, errno, "Failed to dup/close"); exit_cleanup(RERR_IPC); } if (to_child_pipe[0] != STDIN_FILENO) close(to_child_pipe[0]); if (from_child_pipe[1] != STDOUT_FILENO) close(from_child_pipe[1]); #ifdef ICONV_CONST setup_iconv(); #endif child_main(argc, argv); } if (close(from_child_pipe[1]) < 0 || close(to_child_pipe[0]) < 0) { rsyserr(FERROR, errno, "Failed to close"); exit_cleanup(RERR_IPC); } *f_in = from_child_pipe[0]; *f_out = to_child_pipe[1]; return pid; }