int main(int argc,const char *const *argv,const char *const *envp) { struct passwd *pw; const char *account; char strnum[FMT_ULONG]; account = *++argv; if (!account || !*++argv) usage(); pw = getpwnam(account); if (!pw) strerr_die3x(111,FATAL,"unknown account ",account); if (!pathexec_env("HOME",pw->pw_dir)) nomem(); if (!pathexec_env("SHELL",pw->pw_shell)) nomem(); if (!pathexec_env("USER",pw->pw_name)) nomem(); strnum[fmt_ulong(strnum,pw->pw_gid)] = 0; if (!pathexec_env("GID",strnum)) nomem(); strnum[fmt_ulong(strnum,pw->pw_uid)] = 0; if (!pathexec_env("UID",strnum)) nomem(); if (chdir(pw->pw_dir) != 0) strerr_die3sys(111,FATAL,"unable to chdir to ", pw->pw_dir); if (prot_gid(pw->pw_gid) == -1) strerr_die2sys(111,FATAL,"unable to setgid"); if (prot_gids(pw->pw_name, pw->pw_gid) == -1) strerr_die2sys(111,FATAL,"unable to initgroups"); if (prot_uid(pw->pw_uid) == -1) strerr_die2sys(111,FATAL,"unable to setuid"); pathexec_run(*argv,argv,envp); strerr_die3sys(111,FATAL,"unable to run ",*argv); }
void edir(const char *dirname) { int wdir; DIR *dir; direntry *d; int i; if ((wdir =open_read(".")) == -1) fatal("unable to open current working directory"); if (chdir(dirname)) fatal2("unable to switch to directory", dirname); if (! (dir =opendir("."))) fatal2("unable to open directory", dirname); for (;;) { errno =0; d =readdir(dir); if (! d) { if (errno) fatal2("unable to read directory", dirname); break; } if (d->d_name[0] == '.') continue; if (openreadclose(d->d_name, &sa, 256) == -1) { if ((errno == error_isdir) && env_dir) { if (verbose) strerr_warn6(WARNING, "unable to read ", dirname, "/", d->d_name, ": ", &strerr_sys); continue; } else strerr_die6sys(111, FATAL, "unable to read ", dirname, "/", d->d_name, ": "); } if (sa.len) { sa.len =byte_chr(sa.s, sa.len, '\n'); while (sa.len && (sa.s[sa.len -1] == ' ' || sa.s[sa.len -1] == '\t')) --sa.len; for (i =0; i < sa.len; ++i) if (! sa.s[i]) sa.s[i] ='\n'; if (! stralloc_0(&sa)) die_nomem(); if (! pathexec_env(d->d_name, sa.s)) die_nomem(); } else if (! pathexec_env(d->d_name, 0)) die_nomem(); } closedir(dir); if (fchdir(wdir) == -1) fatal("unable to switch to starting directory"); close(wdir); }
static int env_str(const char *env,const char *val) { if (envsa) { return env_val(env,val,str_len(val)); if (!stralloc_cats(envsa,env)) return 0; if (!stralloc_catb(envsa,"=",1)) return 0; if (!stralloc_catb(envsa,val,str_len(val) + 1)) return 0; return 1; } return pathexec_env(env,val); }
static int env_val(const char *env,const char *val,int len) { if (envsa) { if (!stralloc_cats(envsa,env)) return 0; if (!stralloc_catb(envsa,"=",1)) return 0; if (!stralloc_catb(envsa,val,len)) return 0; if (!stralloc_0(envsa)) return 0; return 1; } if (!stralloc_copyb(&etemp,val,len)) return 0; if (!stralloc_0(&etemp)) return 0; return pathexec_env(env,etemp.s); }
void euidgid(char *user, unsigned int ext) { struct uidgid ugid; char bufnum[FMT_ULONG]; if (ext) { if (! uidgids_get(&ugid, user)) { if (*user == ':') fatalx("invalid uid/gids", user +1); if (errno) fatal("unable to get password/group file entry"); fatalx("unknown user/group", user); } } else if (! uidgid_get(&ugid, user)) { if (errno) fatal("unable to get password file entry"); fatalx("unknown account", user); } bufnum[fmt_ulong(bufnum, *ugid.gid)] =0; if (! pathexec_env("GID", bufnum)) die_nomem(); bufnum[fmt_ulong(bufnum, ugid.uid)] =0; if (! pathexec_env("UID", bufnum)) die_nomem(); }
void ucspi_env() { char *l =local_hostname.s; char *r =remote_hostname.s; /* setup ucspi env */ if (! pathexec_env("PROTO", "TCP")) drop_nomem(); if (! pathexec_env("TCPLOCALIP", local_ip)) drop_nomem(); if (! pathexec_env("TCPLOCALPORT", local_port)) drop_nomem(); if (! pathexec_env("TCPLOCALHOST", *l ? l : 0)) drop_nomem(); if (! pathexec_env("TCPREMOTEIP", remote_ip)) drop_nomem(); if (! pathexec_env("TCPREMOTEPORT", remote_port)) drop_nomem(); if (! pathexec_env("TCPREMOTEHOST", *r ? r : 0)) drop_nomem(); if (! pathexec_env("TCPREMOTEINFO", 0)) drop_nomem(); /* additional */ if (phcc > 0) { bufnum[fmt_ulong(bufnum, phcc)] =0; if (! pathexec_env("TCPCONCURRENCY", bufnum)) drop_nomem(); } }
void env(const char *s,const char *t) { if (!pathexec_env(s,t)) drop_nomem(); }
main(int argc,char **argv) { int fakev4=0; unsigned long u; int opt; char *x; int j; int s; int cloop; dns_random_init(seed); close(6); close(7); sig_ignore(sig_pipe); while ((opt = getopt(argc,argv,"46dDvqQhHrRi:p:t:T:l:I:")) != opteof) switch(opt) { case '4': noipv6 = 1; break; case '6': forcev6 = 1; break; case 'd': flagdelay = 1; break; case 'D': flagdelay = 0; break; case 'v': verbosity = 2; break; case 'q': verbosity = 0; break; case 'Q': verbosity = 1; break; case 'l': forcelocal = optarg; break; case 'H': flagremotehost = 0; break; case 'h': flagremotehost = 1; break; case 'R': flagremoteinfo = 0; break; case 'r': flagremoteinfo = 1; break; case 't': scan_ulong(optarg,&itimeout); break; case 'T': j = scan_ulong(optarg,&ctimeout[0]); if (optarg[j] == '+') ++j; scan_ulong(optarg + j,&ctimeout[1]); break; case 'i': if (!scan_ip6(optarg,iplocal)) usage(); break; case 'I': netif=socket_getifidx(optarg); break; case 'p': scan_ulong(optarg,&u); portlocal = u; break; default: usage(); } argv += optind; if (!verbosity) buffer_2->fd = -1; hostname = *argv; if (!hostname) usage(); if (!hostname[0] || str_equal(hostname,"0")) hostname = (noipv6?"127.0.0.1":"::1"); x = *++argv; if (!x) usage(); if (!x[scan_ulong(x,&u)]) portremote = u; else { struct servent *se; se = getservbyname(x,"tcp"); if (!se) strerr_die3x(111,FATAL,"unable to figure out port number for ",x); portremote = ntohs(se->s_port); /* i continue to be amazed at the stupidity of the s_port interface */ } if (!*++argv) usage(); if (!stralloc_copys(&tmp,hostname)) nomem(); if (dns_ip6_qualify(&addresses,&fqdn,&tmp) == -1) strerr_die4sys(111,FATAL,"temporarily unable to figure out IP address for ",hostname,": "); if (addresses.len < 16) strerr_die3x(111,FATAL,"no IP address for ",hostname); if (addresses.len == 16) { ctimeout[0] += ctimeout[1]; ctimeout[1] = 0; } for (cloop = 0;cloop < 2;++cloop) { if (!stralloc_copys(&moreaddresses,"")) nomem(); for (j = 0;j + 16 <= addresses.len;j += 4) { s = socket_tcp6(); if (s == -1) strerr_die2sys(111,FATAL,"unable to create socket: "); if (socket_bind6(s,iplocal,portlocal,netif) == -1) strerr_die2sys(111,FATAL,"unable to bind socket: "); if (timeoutconn6(s,addresses.s + j,portremote,ctimeout[cloop],netif) == 0) goto CONNECTED; close(s); if (!cloop && ctimeout[1] && (errno == error_timeout)) { if (!stralloc_catb(&moreaddresses,addresses.s + j,16)) nomem(); } else { strnum[fmt_ulong(strnum,portremote)] = 0; if (ip6_isv4mapped(addresses.s+j)) ipstr[ip4_fmt(ipstr,addresses.s + j + 12)] = 0; else ipstr[ip6_fmt(ipstr,addresses.s + j)] = 0; strerr_warn5(CONNECT,ipstr," port ",strnum,": ",&strerr_sys); } } if (!stralloc_copy(&addresses,&moreaddresses)) nomem(); } _exit(111); CONNECTED: if (!flagdelay) socket_tcpnodelay(s); /* if it fails, bummer */ if (socket_local6(s,iplocal,&portlocal,&netif) == -1) strerr_die2sys(111,FATAL,"unable to get local address: "); if (!forcev6 && (ip6_isv4mapped(iplocal) || byte_equal(iplocal,16,V6any))) fakev4=1; if (!pathexec_env("PROTO",fakev4?"TCP":"TCP6")) nomem(); strnum[fmt_ulong(strnum,portlocal)] = 0; if (!pathexec_env("TCPLOCALPORT",strnum)) nomem(); if (fakev4) ipstr[ip4_fmt(ipstr,iplocal+12)] = 0; else ipstr[ip6_fmt(ipstr,iplocal)] = 0; if (!pathexec_env("TCPLOCALIP",ipstr)) nomem(); x = forcelocal; if (!x) if (dns_name6(&tmp,iplocal) == 0) { if (!stralloc_0(&tmp)) nomem(); x = tmp.s; } if (!pathexec_env("TCPLOCALHOST",x)) nomem(); if (socket_remote6(s,ipremote,&portremote,&netif) == -1) strerr_die2sys(111,FATAL,"unable to get remote address: "); strnum[fmt_ulong(strnum,portremote)] = 0; if (!pathexec_env("TCPREMOTEPORT",strnum)) nomem(); if (fakev4) ipstr[ip4_fmt(ipstr,ipremote+12)] = 0; else ipstr[ip6_fmt(ipstr,ipremote)] = 0; if (!pathexec_env("TCPREMOTEIP",ipstr)) nomem(); if (verbosity >= 2) strerr_warn4("tcpclient: connected to ",ipstr," port ",strnum,0); x = 0; if (flagremotehost) if (dns_name6(&tmp,ipremote) == 0) { if (!stralloc_0(&tmp)) nomem(); x = tmp.s; } if (!pathexec_env("TCPREMOTEHOST",x)) nomem(); x = 0; if (flagremoteinfo) if (remoteinfo6(&tmp,ipremote,portremote,iplocal,portlocal,itimeout,netif) == 0) { if (!stralloc_0(&tmp)) nomem(); x = tmp.s; } if (!pathexec_env("TCPREMOTEINFO",x)) nomem(); if (fd_move(6,s) == -1) strerr_die2sys(111,FATAL,"unable to set up descriptor 6: "); if (fd_copy(7,6) == -1) strerr_die2sys(111,FATAL,"unable to set up descriptor 7: "); sig_uncatch(sig_pipe); pathexec(argv); strerr_die4sys(111,FATAL,"unable to run ",*argv,": "); }
int main (int argc, char const *const *argv, char const *const *envp) { unsigned int strict = el_getstrict() ; unsigned int b = 0 ; unsigned int n = 0 ; unsigned int sharp ; unsigned int i = 0 ; PROG = "shift" ; { subgetopt_t l = SUBGETOPT_ZERO ; for (;;) { register int opt = subgetopt_r(argc, argv, "n:b:", &l) ; if (opt == -1) break ; switch (opt) { case 'n' : if (!uint0_scan(l.arg, &n)) dieusage() ; i = 1 ; break ; case 'b' : if (!uint0_scan(l.arg, &b)) dieusage() ; i = 1 ; break ; default : dieusage() ; } } argc -= l.ind ; argv += l.ind ; } if (!argc) dieusage() ; if (i) i = 0 ; else n = 1 ; { char const *x = env_get2(envp, "#") ; if (!x) strerr_dienotset(100, "#") ; if (!uint0_scan(x, &sharp)) strerr_dieinvalid(100, "#") ; } /* Shift n args */ if (n > sharp) { if (strict) { char fmtn[UINT_FMT] ; char fmtsharp[UINT_FMT] ; fmtn[uint_fmt(fmtn, n)] = 0 ; fmtsharp[uint_fmt(fmtsharp, sharp)] = 0 ; if (strict == 1) strerr_warnwu5x("shift", " ", fmtn, " arguments: got ", fmtsharp) ; else strerr_diefu5x(100, "shift", " ", fmtn, " arguments: got ", fmtsharp) ; } n = sharp ; } /* Shift b blocks */ for (; i < b ; i++) { for (;;) { char const *x ; unsigned int base = n ; char fmt[UINT_FMT] ; fmt[uint_fmt(fmt, ++n)] = 0 ; if (n > sharp) { char fmti[UINT_FMT] ; fmti[uint_fmt(fmt, i)] = 0 ; strerr_diefu6x(100, "shift", " block ", fmti, ": too few arguments (", fmt, ")") ; } x = env_get2(envp, fmt) ; if (!x) strerr_dienotset(100, fmt) ; if ((x[0] == EXECLINE_BLOCK_END_CHAR) && (!EXECLINE_BLOCK_END_CHAR || !x[1])) break ; if ((x[0] != EXECLINE_BLOCK_QUOTE_CHAR) && strict) { char fmti[UINT_FMT] ; char fmtp[UINT_FMT] ; fmti[uint_fmt(fmti, i)] = 0 ; fmtp[uint_fmt(fmtp, n - base)] = 0 ; if (strict == 1) strerr_warnw6x("unquoted positional ", x, " at block ", fmti, " position ", fmtp) ; else strerr_dief6x(100, "unquoted positional ", x, " at block ", fmti, " position ", fmtp) ; } } } /* n = shift value; modify the env */ { register unsigned int i = 1 ; char fmt[UINT_FMT] ; fmt[uint_fmt(fmt, sharp - n)] = 0 ; if (!pathexec_env("#", fmt)) strerr_diefu1sys(111, "pathexec_env") ; for (; i <= sharp ; i++) { char fmu[UINT_FMT] ; fmt[uint_fmt(fmt, i)] = 0 ; fmu[uint_fmt(fmu, i + n)] = 0 ; if (!pathexec_env(fmt, i <= (sharp - n) ? env_get2(envp, fmu) : 0)) strerr_diefu1sys(111, "pathexec_env") ; } } pathexec(argv) ; strerr_dieexec(111, argv[0]) ; }
void env(const char *s,const char *t) { if (!pathexec_env(s,t)) errmem; }
int ipsvd_instruct(stralloc *inst, stralloc *match, char *ip) { char *insts; unsigned int instslen; int delim; int i, j; int rc =IPSVD_DEFAULT; if (inst->s && inst->len) { insts =inst->s; instslen =inst->len; while ((i =byte_chr(insts, instslen, 0)) < instslen) { switch(*insts) { case '+': if ((delim =str_chr(insts, '=')) <= 1) break; /* empty inst */ if (insts[delim] == '=') { insts[delim] =0; if (! pathexec_env(insts +1, insts +delim +1)) return(-1); insts[delim] ='='; } else if (! pathexec_env(insts +1, 0)) return(-1); break; case 'C': if (! phccmax) break; delim =scan_ulong(insts +1, &phccmax); if (insts[delim +1] == ':') { if (ipsvd_fmt_msg(&msg, insts +delim +2) == -1) return(-1); if (! stralloc_0(&msg)) return(-1); phccmsg =msg.s; } break; case '=': if (ip && (rc != IPSVD_INSTRUCT)) { unsigned int next; rc =IPSVD_DENY; next =str_chr(insts +1, ':'); ++next; if ((next == 2) && (insts[1] == '0')) { if (! stralloc_copys(&sa, ip)) return(-1); } else if (! stralloc_copyb(&sa, insts +1, next -1)) return(-1); if (insts[next] != 0) ++next; if ((dns_ip4(&ips, &sa) == -1) || (ips.len < 4)) if (dns_ip4_qualify(&ips, &fqdn, &sa) == -1) { if (! stralloc_0(&sa)) return(-1); strerr_warn5(progname, ": warning: ", "unable to look up ip address: ", sa.s, ": ", &strerr_sys); break; } if (ips.len < 4) { if (! stralloc_0(&sa)) return(-1); strerr_warn4(progname, ": warning: ", "unable to look up ip address: ", sa.s, 0); break; } for (j =0; j +4 <= ips.len; j +=4) { char tmp[IP4_FMT]; tmp[ipsvd_fmt_ip(tmp, ips.s +j)] =0; if (str_equal(tmp, ip)) { inst->len =insts -inst->s +i +1; if (insts[next]) { forward =insts +next; return(IPSVD_FORWARD); } return(IPSVD_INSTRUCT); } } } break; case 0: case '#': /* skip empty line and comment */ break; default: strerr_warn6(progname, ": warning: ", "bad instruction: ", match->s, ": ", insts, 0); } insts +=i +1; instslen -=i +1; } } if (rc == IPSVD_DEFAULT) return(IPSVD_INSTRUCT); return(rc); }
int main() { if (!pathexec_env("NAME", "VALUE")) return 1; return 0; }