static int m_chkids (void) { int i; pid_t pid; if (getuid () == geteuid ()) return (-1); for (i = 0; (pid = fork ()) == -1 && i < 5; i++) sleep (5); switch (pid) { case -1: break; case 0: /* It's not necessary to call unregister_for_removal(0) because the child calls _exit() in context_save(). */ setgid (getgid ()); setuid (getuid ()); break; default: pidwait (pid, -1); break; } return pid; }
static int m_chkids (void) { int i; pid_t pid; if (getuid () == geteuid ()) return (-1); for (i = 0; (pid = fork ()) == -1 && i < 5; i++) sleep (5); switch (pid) { case -1: break; case 0: setgid (getgid ()); setuid (getuid ()); break; default: pidwait (pid, -1); break; } return pid; }
static int show_content_aux2 (CT ct, int serial, int alternate, char *cracked, char *buffer, int fd, int xlist, int xpause, int xstdin, int xtty) { pid_t child_id; int i; char *vec[4], exec[BUFSIZ + sizeof "exec "]; if (debugsw || cracked) { fflush (stdout); fprintf (stderr, "%s msg %s", cracked ? "storing" : "show", ct->c_file); if (ct->c_partno) fprintf (stderr, " part %s", ct->c_partno); if (cracked) fprintf (stderr, " using command (cd %s; %s)\n", cracked, buffer); else fprintf (stderr, " using command %s\n", buffer); } if (xpid < 0 || (xtty && xpid)) { if (xpid < 0) xpid = -xpid; pidcheck(pidwait (xpid, NOTOK)); xpid = 0; } if (xlist) { char prompt[BUFSIZ]; if (ct->c_type == CT_MULTIPART) list_content (ct, -1, 1, 0, 0); else list_switch (ct, -1, 1, 0, 0); if (xpause && isatty (fileno (stdout))) { int intr; SIGNAL_HANDLER istat; if (SOprintf ("Press <return> to show content...")) printf ("Press <return> to show content..."); istat = SIGNAL (SIGINT, intrser); if ((intr = sigsetjmp (intrenv, 1)) == OK) { fflush (stdout); prompt[0] = 0; read (fileno (stdout), prompt, sizeof(prompt)); } SIGNAL (SIGINT, istat); if (intr != OK || prompt[0] == 'n') { (*ct->c_ceclosefnx) (ct); return (alternate ? DONE : NOTOK); } if (prompt[0] == 'q') done(OK); } } snprintf (exec, sizeof(exec), "exec %s", buffer); vec[0] = "/bin/sh"; vec[1] = "-c"; vec[2] = exec; vec[3] = NULL; fflush (stdout); for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++) sleep (5); switch (child_id) { case NOTOK: advise ("fork", "unable to"); (*ct->c_ceclosefnx) (ct); return NOTOK; case OK: if (cracked) chdir (cracked); if (!xstdin) dup2 (fd, 0); close (fd); execvp ("/bin/sh", vec); fprintf (stderr, "unable to exec "); perror ("/bin/sh"); _exit (-1); /* NOTREACHED */ default: if (!serial) { ct->c_pid = child_id; if (xtty) xpid = child_id; } else { pidcheck (pidXwait (child_id, NULL)); } if (fd != NOTOK) (*ct->c_ceclosefnx) (ct); return (alternate ? DONE : OK); } }
int main (int argc, char **argv) { pid_t child_id = OK; int i, status, isdf = 0; int distsw = 0, vecp = 0; char *cp, *dfolder = NULL, *dmsg = NULL; char *msg = NULL, **ap, **argp, backup[BUFSIZ]; char buf[BUFSIZ], **arguments, *vec[MAXARGS]; if (nmh_init(argv[0], 1)) { return 1; } arguments = getarguments (invo_name, argc, argv, 1); argp = arguments; vec[vecp++] = invo_name; vec[vecp++] = "-whom"; vec[vecp++] = "-library"; vec[vecp++] = getcpy (m_maildir ("")); /* Don't need to feed fileproc or mhlproc to post because it doesn't use them when used for whom. */ while ((cp = *argp++)) { if (*cp == '-') { switch (smatch (++cp, switches)) { case AMBIGSW: ambigsw (cp, switches); done (1); case UNKWNSW: adios (NULL, "-%s unknown", cp); case HELPSW: snprintf (buf, sizeof(buf), "%s [switches] [file]", invo_name); print_help (buf, switches, 1); done (0); case VERSIONSW: print_version(invo_name); done (0); case CHKSW: case NOCHKSW: case SNOOPSW: case SASLSW: case TLSSW: case INITTLSSW: case NTLSSW: vec[vecp++] = --cp; continue; case DRAFTSW: msg = draft; continue; case DFOLDSW: if (dfolder) adios (NULL, "only one draft folder at a time!"); if (!(cp = *argp++) || *cp == '-') adios (NULL, "missing argument to %s", argp[-2]); dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp, *cp != '@' ? TFOLDER : TSUBCWF); continue; case DMSGSW: if (dmsg) adios (NULL, "only one draft message at a time!"); if (!(dmsg = *argp++) || *dmsg == '-') adios (NULL, "missing argument to %s", argp[-2]); continue; case NDFLDSW: dfolder = NULL; isdf = NOTOK; continue; case ALIASW: case CLIESW: case SERVSW: case USERSW: case PORTSW: case SASLMECHSW: case MTSSW: vec[vecp++] = --cp; if (!(cp = *argp++) || *cp == '-') adios (NULL, "missing argument to %s", argp[-2]); vec[vecp++] = cp; continue; } } if (msg) adios (NULL, "only one draft at a time!"); else vec[vecp++] = msg = cp; } /* allow Aliasfile: profile entry */ if ((cp = context_find ("Aliasfile"))) { char *dp = NULL; for (ap = brkstring(dp = getcpy(cp), " ", "\n"); ap && *ap; ap++) { vec[vecp++] = "-alias"; vec[vecp++] = *ap; } } if (msg == NULL) { cp = getcpy (m_draft (dfolder, dmsg, 1, &isdf)); msg = vec[vecp++] = cp; } if ((cp = getenv ("mhdist")) && *cp && (distsw = atoi (cp)) && (cp = getenv ("mhaltmsg")) && *cp) { if (distout (msg, cp, backup) == NOTOK) done (1); vec[vecp++] = "-dist"; distsw++; } vec[vecp] = NULL; closefds (3); if (distsw) { for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++) sleep (5); } switch (distsw ? child_id : OK) { case NOTOK: advise (NULL, "unable to fork, so checking directly..."); case OK: execvp (postproc, vec); fprintf (stderr, "unable to exec "); perror (postproc); _exit (-1); default: SIGNAL (SIGHUP, SIG_IGN); SIGNAL (SIGINT, SIG_IGN); SIGNAL (SIGQUIT, SIG_IGN); SIGNAL (SIGTERM, SIG_IGN); status = pidwait(child_id, OK); (void) m_unlink (msg); if (rename (backup, msg) == NOTOK) adios (msg, "unable to rename %s to", backup); done (status); } return 0; /* dead code to satisfy the compiler */ }