+int
+sendmail_init (char *client, char *server, int watch, int verbose,
+ int debug, int onex, int queued)
+{
+ int i, result, vecp;
+ int pdi[2], pdo[2];
+ char *vec[15];
+
+ if (watch)
+ verbose = TRUE;
+
+ sm_verbose = verbose;
+ sm_debug = debug;
+ if (sm_rfp != NULL && sm_wfp != NULL)
+ return RP_OK;
+
+ if (client == NULL || *client == '\0') {
+ if (clientname)
+ client = clientname;
+ else
+ client = LocalName(); /* no clientname -> LocalName */
+ }
+
+#ifdef ZMAILER
+ if (client == NULL || *client == '\0')
+ client = "localhost";
+#endif
+
+ if (pipe (pdi) == NOTOK)
+ return sm_ierror ("no pipes");
+ if (pipe (pdo) == NOTOK) {
+ close (pdi[0]);
+ close (pdi[1]);
+ return sm_ierror ("no pipes");
+ }
+
+ for (i = 0; (sm_child = fork ()) == NOTOK && i < 5; i++)
+ sleep (5);
+
+ switch (sm_child) {
+ case NOTOK:
+ close (pdo[0]);
+ close (pdo[1]);
+ close (pdi[0]);
+ close (pdi[1]);
+ return sm_ierror ("unable to fork");
+
+ case OK:
+ if (pdo[0] != fileno (stdin))
+ dup2 (pdo[0], fileno (stdin));
+ if (pdi[1] != fileno (stdout))
+ dup2 (pdi[1], fileno (stdout));
+ if (pdi[1] != fileno (stderr))
+ dup2 (pdi[1], fileno (stderr));
+ for (i = fileno (stderr) + 1; i < NBITS; i++)
+ close (i);
+
+ vecp = 0;
+ vec[vecp++] = r1bindex (sendmail, '/');
+ vec[vecp++] = "-bs";
+#ifndef ZMAILER
+ vec[vecp++] = watch ? "-odi" : queued ? "-odq" : "-odb";
+ vec[vecp++] = "-oem";
+ vec[vecp++] = "-om";
+# ifndef RAND
+ if (verbose)
+ vec[vecp++] = "-ov";
+# endif /* not RAND */
+#endif /* not ZMAILER */
+ vec[vecp++] = NULL;
+
+ setgid (getegid ());
+ setuid (geteuid ());
+ execvp (sendmail, vec);
+ fprintf (stderr, "unable to exec ");
+ perror (sendmail);
+ _exit (-1); /* NOTREACHED */
+
+ default:
+ SIGNAL (SIGALRM, alrmser);
+ SIGNAL (SIGPIPE, SIG_IGN);
+
+ close (pdi[1]);
+ close (pdo[0]);
+ if ((sm_rfp = fdopen (pdi[0], "r")) == NULL
+ || (sm_wfp = fdopen (pdo[1], "w")) == NULL) {
+ close (pdi[0]);
+ close (pdo[1]);
+ sm_rfp = sm_wfp = NULL;
+ return sm_ierror ("unable to fdopen");
+ }
+ sm_alarmed = 0;
+ alarm (SM_OPEN);
+ result = smhear ();
+ alarm (0);
+ switch (result) {
+ case 220:
+ break;
+
+ default:
+ sm_end (NOTOK);
+ return RP_RPLY;
+ }
+
+ if (client && *client) {
+ doingEHLO = 1;
+ result = smtalk (SM_HELO, "EHLO %s", client);
+ doingEHLO = 0;
+
+ if (500 <= result && result <= 599)
+ result = smtalk (SM_HELO, "HELO %s", client);
+
+ switch (result) {
+ case 250:
+ break;
+
+ default:
+ sm_end (NOTOK);
+ return RP_RPLY;
+ }
+ }
+
+#ifndef ZMAILER
+ if (onex)
+ smtalk (SM_HELO, "ONEX");
+#endif
+ if (watch)
+ smtalk (SM_HELO, "VERB on");
+
+ return RP_OK;
+ }
+}