const char * system_IsValid(const char *name, struct prompt *prompt, int mode) { /* * Note: The ReadSystem() calls only result in calls to the Allow* * functions. arg->bundle will be set to NULL for these commands ! */ int def, how, rs; int defuserok; def = !strcmp(name, "default"); how = ID0realuid() == 0 ? SYSTEM_EXISTS : SYSTEM_VALIDATE; userok = -1; modeok = 1; modereq = mode; rs = ReadSystem(NULL, "default", CONFFILE, prompt, NULL, how); defuserok = userok; userok = -1; if (!def) { if (rs == -1) rs = 0; /* we don't care that ``default'' doesn't exist */ if (rs == 0) rs = ReadSystem(NULL, name, CONFFILE, prompt, NULL, how); if (rs == -1) return "Configuration label not found"; if (rs == -2) return PPP_CONFDIR "/" CONFFILE " : File not found"; } if (userok == -1) userok = defuserok; if (how == SYSTEM_EXISTS) userok = modeok = 1; if (!userok) return "User access denied"; if (!modeok) return "Mode denied for this label"; return NULL; }
int AllowUsers(struct cmdargs const *arg) { /* arg->bundle may be NULL (see system_IsValid()) ! */ int f; struct passwd *pwd; if (userok == -1) userok = 0; pwd = getpwuid(ID0realuid()); if (pwd != NULL) for (f = arg->argn; f < arg->argc; f++) if (!strcmp("*", arg->argv[f]) || !strcmp(pwd->pw_name, arg->argv[f])) { userok = 1; break; } endpwent(); return 0; }
static void chap_StartChild(struct chap *chap, char *prog, const char *name) { char *argv[MAXARGS], *nargv[MAXARGS]; int argc, fd; int in[2], out[2]; pid_t pid; if (chap->child.fd != -1) { log_Printf(LogWARN, "Chap: %s: Program already running\n", prog); return; } if (pipe(in) == -1) { log_Printf(LogERROR, "Chap: pipe: %s\n", strerror(errno)); return; } if (pipe(out) == -1) { log_Printf(LogERROR, "Chap: pipe: %s\n", strerror(errno)); close(in[0]); close(in[1]); return; } pid = getpid(); switch ((chap->child.pid = fork())) { case -1: log_Printf(LogERROR, "Chap: fork: %s\n", strerror(errno)); close(in[0]); close(in[1]); close(out[0]); close(out[1]); chap->child.pid = 0; return; case 0: timer_TermService(); if ((argc = command_Interpret(prog, strlen(prog), argv)) <= 0) { if (argc < 0) { log_Printf(LogWARN, "CHAP: Invalid command syntax\n"); _exit(255); } _exit(0); } close(in[1]); close(out[0]); if (out[1] == STDIN_FILENO) out[1] = dup(out[1]); dup2(in[0], STDIN_FILENO); dup2(out[1], STDOUT_FILENO); close(STDERR_FILENO); if (open(_PATH_DEVNULL, O_RDWR) != STDERR_FILENO) { log_Printf(LogALERT, "Chap: Failed to open %s: %s\n", _PATH_DEVNULL, strerror(errno)); exit(1); } for (fd = getdtablesize(); fd > STDERR_FILENO; fd--) fcntl(fd, F_SETFD, 1); #ifndef NOSUID setuid(ID0realuid()); #endif command_Expand(nargv, argc, (char const *const *)argv, chap->auth.physical->dl->bundle, 0, pid); execvp(nargv[0], nargv); printf("exec() of %s failed: %s\n", nargv[0], strerror(errno)); _exit(255); default: close(in[0]); close(out[1]); chap->child.fd = out[0]; chap->child.buf.len = 0; write(in[1], chap->auth.in.name, strlen(chap->auth.in.name)); write(in[1], "\n", 1); write(in[1], chap->challenge.peer + 1, *chap->challenge.peer); write(in[1], "\n", 1); write(in[1], name, strlen(name)); write(in[1], "\n", 1); close(in[1]); break; } }
int main(int argc, char **argv) { char *name; const char *lastlabel; int arg, f, holdfd[3], label; struct bundle *bundle; struct prompt *prompt; struct switches sw; probe_Init(); /* * We open 3 descriptors to ensure that STDIN_FILENO, STDOUT_FILENO and * STDERR_FILENO are always open. These are closed before DoLoop(), * but *after* we've avoided the possibility of erroneously closing * an important descriptor with close(STD{IN,OUT,ERR}_FILENO). */ if ((holdfd[0] = open(_PATH_DEVNULL, O_RDWR)) == -1) { fprintf(stderr, "Cannot open %s !\n", _PATH_DEVNULL); return 2; } for (f = 1; f < sizeof holdfd / sizeof *holdfd; f++) holdfd[f] = dup(holdfd[0]); name = strrchr(argv[0], '/'); log_Open(name ? name + 1 : argv[0]); #ifndef NONAT PacketAliasInit(); #endif label = ProcessArgs(argc, argv, &sw); /* * A FreeBSD & OpenBSD hack to dodge a bug in the tty driver that drops * output occasionally.... I must find the real reason some time. To * display the dodgy behaviour, comment out this bit, make yourself a large * routing table and then run ppp in interactive mode. The `show route' * command will drop chunks of data !!! */ if (sw.mode == PHYS_INTERACTIVE) { close(STDIN_FILENO); if (open(_PATH_TTY, O_RDONLY) != STDIN_FILENO) { fprintf(stderr, "Cannot open %s for input !\n", _PATH_TTY); return 2; } } /* Allow output for the moment (except in direct mode) */ if (sw.mode == PHYS_DIRECT) prompt = NULL; else SignalPrompt = prompt = prompt_Create(NULL, NULL, PROMPT_STD); ID0init(); if (ID0realuid() != 0) { char conf[200], *ptr; snprintf(conf, sizeof conf, "%s/%s", PPP_CONFDIR, CONFFILE); do { struct stat sb; if (stat(conf, &sb) == 0 && sb.st_mode & S_IWOTH) { log_Printf(LogALERT, "ppp: Access violation: Please protect %s\n", conf); return -1; } ptr = conf + strlen(conf)-2; while (ptr > conf && *ptr != '/') *ptr-- = '\0'; } while (ptr >= conf); } if (label < argc) for (arg = label; arg < argc; arg++) CheckLabel(argv[arg], prompt, sw.mode); else CheckLabel("default", prompt, sw.mode); if (!sw.quiet) prompt_Printf(prompt, "Working in %s mode\n", mode2Nam(sw.mode)); if ((bundle = bundle_Create(TUN_PREFIX, sw.mode, sw.unit)) == NULL) return EX_START; /* NOTE: We may now have changed argv[1] via a ``set proctitle'' */ if (prompt) { prompt->bundle = bundle; /* couldn't do it earlier */ if (!sw.quiet) prompt_Printf(prompt, "Using interface: %s\n", bundle->iface->name); } SignalBundle = bundle; bundle->NatEnabled = sw.nat; if (sw.nat) opt_enable(bundle, OPT_IFACEALIAS); if (system_Select(bundle, "default", CONFFILE, prompt, NULL) < 0) prompt_Printf(prompt, "Warning: No default entry found in config file.\n"); sig_signal(SIGHUP, CloseSession); sig_signal(SIGTERM, CloseSession); sig_signal(SIGINT, CloseConnection); sig_signal(SIGQUIT, CloseSession); sig_signal(SIGALRM, SIG_IGN); signal(SIGPIPE, SIG_IGN); if (sw.mode == PHYS_INTERACTIVE) sig_signal(SIGTSTP, TerminalStop); sig_signal(SIGUSR1, RestartServer); sig_signal(SIGUSR2, BringDownServer); lastlabel = argv[argc - 1]; for (arg = label; arg < argc; arg++) { /* In case we use LABEL or ``set enddisc label'' */ bundle_SetLabel(bundle, lastlabel); system_Select(bundle, argv[arg], CONFFILE, prompt, NULL); } if (label < argc) /* In case the last label did a ``load'' */ bundle_SetLabel(bundle, lastlabel); if (sw.mode == PHYS_AUTO && ncprange_family(&bundle->ncp.ipcp.cfg.peer_range) == AF_UNSPEC) { prompt_Printf(prompt, "You must ``set ifaddr'' with a peer address " "in auto mode.\n"); AbortProgram(EX_START); } if (sw.mode != PHYS_INTERACTIVE) { if (sw.mode != PHYS_DIRECT) { if (!sw.fg) { int bgpipe[2]; pid_t bgpid; if (sw.mode == PHYS_BACKGROUND && pipe(bgpipe)) { log_Printf(LogERROR, "pipe: %s\n", strerror(errno)); AbortProgram(EX_SOCK); } bgpid = fork(); if (bgpid == -1) { log_Printf(LogERROR, "fork: %s\n", strerror(errno)); AbortProgram(EX_SOCK); } if (bgpid) { char c = EX_NORMAL; int ret; if (sw.mode == PHYS_BACKGROUND) { close(bgpipe[1]); BGPid = bgpid; /* If we get a signal, kill the child */ signal(SIGHUP, KillChild); signal(SIGTERM, KillChild); signal(SIGINT, KillChild); signal(SIGQUIT, KillChild); /* Wait for our child to close its pipe before we exit */ while ((ret = read(bgpipe[0], &c, 1)) == 1) { switch (c) { case EX_NORMAL: if (!sw.quiet) { prompt_Printf(prompt, "PPP enabled\n"); log_Printf(LogPHASE, "Parent: PPP enabled\n"); } break; case EX_REDIAL: if (!sw.quiet) prompt_Printf(prompt, "Attempting redial\n"); continue; case EX_RECONNECT: if (!sw.quiet) prompt_Printf(prompt, "Attempting reconnect\n"); continue; default: prompt_Printf(prompt, "Child failed (%s)\n", ex_desc((int)c)); log_Printf(LogPHASE, "Parent: Child failed (%s)\n", ex_desc((int) c)); } break; } if (ret != 1) { prompt_Printf(prompt, "Child exit, no status.\n"); log_Printf(LogPHASE, "Parent: Child exit, no status.\n"); } close(bgpipe[0]); } return c; } else if (sw.mode == PHYS_BACKGROUND) { close(bgpipe[0]); bundle->notify.fd = bgpipe[1]; } bundle_ChangedPID(bundle); bundle_LockTun(bundle); /* we have a new pid */ } /* -auto, -dedicated, -ddial, -foreground & -background */ prompt_Destroy(prompt, 0); close(STDOUT_FILENO); close(STDERR_FILENO); close(STDIN_FILENO); if (!sw.fg) setsid(); } else { /* -direct - STDIN_FILENO gets used by physical_Open */ prompt_TtyInit(NULL); close(STDOUT_FILENO); close(STDERR_FILENO); } } else { /* -interactive */ close(STDERR_FILENO); prompt_TtyInit(prompt); prompt_TtyCommandMode(prompt); prompt_Required(prompt); } /* We can get rid of these now */ for (f = 0; f < sizeof holdfd / sizeof *holdfd; f++) close(holdfd[f]); log_Printf(LogPHASE, "PPP Started (%s mode).\n", mode2Nam(sw.mode)); DoLoop(bundle); AbortProgram(EX_NORMAL); return EX_NORMAL; }
/* * Move string from ``from'' to ``to'', interpreting ``~'' and $.... * Returns NULL if string expansion failed due to lack of buffer space. */ const char * InterpretArg(const char *from, char *to, size_t tosiz) { char *ptr, *startto, *endto; struct passwd *pwd; int len, instring; const char *env; instring = 0; startto = to; endto = to + tosiz - 1; while(issep(*from)) from++; while (*from != '\0') { if (to >= endto) return NULL; switch (*from) { case '"': instring = !instring; *to++ = *from++; break; case '\\': switch (*++from) { case '$': case '~': break; /* Swallow the escapes */ default: *to++ = '\\'; /* Pass the escapes on, maybe skipping \# */ break; } if (to >= endto) return NULL; *to++ = *from++; break; case '$': if (from[1] == '$') { *to = '\0'; /* For an empty var name below */ from += 2; } else if (from[1] == '{') { ptr = strchr(from+2, '}'); if (ptr) { len = ptr - from - 2; if (endto - to < len ) return NULL; if (len) { strncpy(to, from+2, len); to[len] = '\0'; from = ptr+1; } else { *to++ = *from++; continue; } } else { *to++ = *from++; continue; } } else { ptr = to; for (from++; (isalnum(*from) || *from == '_') && ptr < endto; from++) *ptr++ = *from; *ptr = '\0'; } if (to >= endto) return NULL; if (*to == '\0') *to++ = '$'; else if ((env = getenv(to)) != NULL) { if (endto - to < strlen(env)) return NULL; strncpy(to, env, endto - to); *endto = '\0'; to += strlen(to); } break; case '~': ptr = strchr(++from, '/'); len = ptr ? ptr - from : strlen(from); if (len == 0) pwd = getpwuid(ID0realuid()); else { if (endto - to < len) return NULL; strncpy(to, from, len); to[len] = '\0'; pwd = getpwnam(to); } if (to >= endto) return NULL; if (pwd == NULL) *to++ = '~'; else { if (endto - to < strlen(pwd->pw_dir)) return NULL; strncpy(to, pwd->pw_dir, endto - to); *endto = '\0'; to += strlen(to); from += len; } endpwent(); break; default: *to++ = *from++; break; } } while (to > startto) { to--; if (!issep(*to)) { to++; break; } } *to = '\0'; return from; }
struct device * exec_Create(struct physical *p) { struct execdevice *dev; dev = NULL; if (p->fd < 0) { if (*p->name.full == '!') { int fids[2], type; if ((dev = malloc(sizeof *dev)) == NULL) { log_Printf(LogWARN, "%s: Cannot allocate an exec device: %s\n", p->link.name, strerror(errno)); return NULL; } dev->fd_out = -1; p->fd--; /* We own the device but maybe can't use it - change fd */ type = physical_IsSync(p) ? SOCK_DGRAM : SOCK_STREAM; if (socketpair(AF_UNIX, type, PF_UNSPEC, fids) < 0) { log_Printf(LogPHASE, "Unable to create pipe for line exec: %s\n", strerror(errno)); free(dev); dev = NULL; } else { static int child_status; /* This variable is abused ! */ int stat, argc, i, ret, wret, pidpipe[2]; pid_t pid, realpid; char *argv[MAXARGS]; stat = fcntl(fids[0], F_GETFL, 0); if (stat > 0) { stat |= O_NONBLOCK; fcntl(fids[0], F_SETFL, stat); } realpid = getpid(); if (pipe(pidpipe) == -1) { log_Printf(LogPHASE, "Unable to pipe for line exec: %s\n", strerror(errno)); close(fids[1]); close(fids[0]); free(dev); dev = NULL; } else switch ((pid = fork())) { case -1: log_Printf(LogPHASE, "Unable to fork for line exec: %s\n", strerror(errno)); close(pidpipe[0]); close(pidpipe[1]); close(fids[1]); close(fids[0]); break; case 0: close(pidpipe[0]); close(fids[0]); timer_TermService(); #ifndef NOSUID setuid(ID0realuid()); #endif child_status = 0; switch ((pid = vfork())) { case 0: close(pidpipe[1]); break; case -1: ret = errno; log_Printf(LogPHASE, "Unable to vfork to drop parent: %s\n", strerror(errno)); close(pidpipe[1]); _exit(ret); default: write(pidpipe[1], &pid, sizeof pid); close(pidpipe[1]); _exit(child_status); /* The error from exec() ! */ } log_Printf(LogDEBUG, "Exec'ing ``%s''\n", p->name.base); if ((argc = MakeArgs(p->name.base, argv, VECSIZE(argv), PARSE_REDUCE|PARSE_NOHASH)) < 0) { log_Printf(LogWARN, "Syntax error in exec command\n"); _exit(ESRCH); } command_Expand(argv, argc, (char const *const *)argv, p->dl->bundle, 0, realpid); dup2(fids[1], STDIN_FILENO); dup2(fids[1], STDOUT_FILENO); dup2(fids[1], STDERR_FILENO); for (i = getdtablesize(); i > STDERR_FILENO; i--) fcntl(i, F_SETFD, 1); execvp(*argv, argv); child_status = errno; /* Only works for vfork() */ printf("execvp failed: %s: %s\r\n", *argv, strerror(child_status)); _exit(child_status); break; default: close(pidpipe[1]); close(fids[1]); if (read(pidpipe[0], &p->session_owner, sizeof p->session_owner) != sizeof p->session_owner) p->session_owner = (pid_t)-1; close(pidpipe[0]); while ((wret = waitpid(pid, &stat, 0)) == -1 && errno == EINTR) ; if (wret == -1) { log_Printf(LogWARN, "Waiting for child process: %s\n", strerror(errno)); close(fids[0]); p->session_owner = (pid_t)-1; break; } else if (WIFSIGNALED(stat)) { log_Printf(LogWARN, "Child process received sig %d !\n", WTERMSIG(stat)); close(fids[0]); p->session_owner = (pid_t)-1; break; } else if (WIFSTOPPED(stat)) { log_Printf(LogWARN, "Child process received stop sig %d !\n", WSTOPSIG(stat)); /* I guess that's ok.... */ } else if ((ret = WEXITSTATUS(stat))) { log_Printf(LogWARN, "Cannot exec \"%s\": %s\n", p->name.base, strerror(ret)); close(fids[0]); p->session_owner = (pid_t)-1; break; } p->fd = fids[0]; log_Printf(LogDEBUG, "Using descriptor %d for child\n", p->fd); } } } } else { struct stat st; if (fstat(p->fd, &st) != -1 && (st.st_mode & S_IFIFO)) { if ((dev = malloc(sizeof *dev)) == NULL) log_Printf(LogWARN, "%s: Cannot allocate an exec device: %s\n", p->link.name, strerror(errno)); else if (p->fd == STDIN_FILENO) { log_Printf(LogPHASE, "%s: Using stdin/stdout to communicate with " "parent (pipe mode)\n", p->link.name); dev->fd_out = dup(STDOUT_FILENO); /* Hook things up so that we monitor dev->fd_out */ p->desc.UpdateSet = exec_UpdateSet; p->desc.IsSet = exec_IsSet; } else dev->fd_out = -1; } } if (dev) { memcpy(&dev->dev, &baseexecdevice, sizeof dev->dev); physical_SetupStack(p, dev->dev.name, PHYSICAL_NOFORCE); if (p->cfg.cd.necessity != CD_DEFAULT) log_Printf(LogWARN, "Carrier settings ignored\n"); return &dev->dev; } return NULL; }