1    | /*  spawn.c
2    | 
3    |     Source file for spawn operations for  PGPsendmail  (wrapper to sendmail).
4    | 
5    |     Copyright (C) 1994-1998  Richard Gooch
6    | 
7    |     This program is free software; you can redistribute it and/or modify
8    |     it under the terms of the GNU General Public License as published by
9    |     the Free Software Foundation; either version 2 of the License, or
10   |     (at your option) any later version.
11   | 
12   |     This program is distributed in the hope that it will be useful,
13   |     but WITHOUT ANY WARRANTY; without even the implied warranty of
14   |     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   |     GNU General Public License for more details.
16   | 
17   |     You should have received a copy of the GNU General Public License
18   |     along with this program; if not, write to the Free Software
19   |     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20   | 
21   |     Richard Gooch may be reached by email at  rgooch@atnf.csiro.au
22   |     The postal address is:
23   |       Richard Gooch, c/o ATNF, P. O. Box 76, Epping, N.S.W., 2121, Australia.
24   | */
25   | 
26   | /*  This programme intercepts messages sent by user mail agents to the
27   |     sendmail daemon and checks to see if messages can be encrypted using the
28   |     recipient's PGP public keys.
29   | 
30   | 
31   |     Written by      Richard Gooch   31-MAY-1994
32   | 
33   |     Updated by      Richard Gooch   31-MAY-1994: Extracted from  pgpsendmail.c
34   | 
35   |     Updated by      Richard Gooch   18-JUN-1994: Made error messages more
36   |   explicit.
37   | 
38   |     Updated by      Richard Gooch   27-JUN-1994: Copied  set_env  from
39   |   pgpdaemon.c
40   | 
41   |     Updated by      Richard Gooch   5-JUL-1994: Changed to use of  m_copy  .
42   | 
43   |     Updated by      Richard Gooch   14-JUL-1994: Moved  copy_data  and  set_env
44   |   to  misc.c
45   | 
46   |     Updated by      Richard Gooch   3-DEC-1994: Fixed bug for externally set
47   |   error descriptor.
48   | 
49   |     Updated by      Richard Gooch   25-SEP-1997: Used new ERRSTRING macro.
50   | 
51   |     Last updated by Richard Gooch   10-JUL-1998: Removed definitions of system
52   |   errlist array.
53   | 
54   | 
55   | */
56   | #include <stdio.h>
57   | #include <stdlib.h>
58   | #include <errno.h>
59   | #include <sys/types.h> 
60   | #include <unistd.h>
61   | #include <string.h>
62   | 
63   | 
64   | /* #include "pgpsendmail.h" */
65   | 
66   | #define ERRSTRING strerror(errno)
67   | 
68   | #define LINE_LENGTH 1024
69   | #define STRING_LENGTH 255
70   | 
71   | int spawn_job (char *path, char *argv[], int *in_fd, int *out_fd, int *err_fd)
72   | /*  This routine will fork(2) and execvp(2) a process.
73   |     The file to execute must be pointed to by  path  .
74   |     The NULL terminated list of arguments which will be passed to  main  must
75   |     be pointed to by  argv  .
76   |     The input file descriptor (fd = 0) for the process must be pointed to by
77   |     in_fd  .If the value here is less than 0, then a pipe to the process is
78   |     opened and the writeable end is written to the storage pointed to by  in_fd
79   |     The standard output file descriptor (fd = 1) for the process must be
80   |     pointed to by  out_fd  .If the value here is less than 0, then a pipe to
81   |     the process is opened and the readable end is written to the storage
82   |     pointed to by  out_fd  .
83   |     The standard error output file descriptor (fd = 2) for the process must be
84   |     pointed to by  err_fd  .If the value here is less than 0, then a pipe to
85   |     the process is opened and the readable end is written to the storage
86   |     pointed to by  err_fd  .
87   |     The routine returns the child process ID on success, else it returns -1.
88   | */
89   | {
90   |     int child_pid;
91   |     /*    char txt[LINE_LENGTH]; */
92   |     int sd1[2], sd2[2];
93   | 
94   |     if (pipe(sd1) == -1)
95   |       {
96   | 	perror("pipe failed");
97   | 	return(1);
98   |       }
99   |     if (pipe(sd2) == -1)
100  |       {
101  | 	perror("pipe failed");
102  | 	return(1);
103  |       }
104  | 
105  |     /*  Fork and exec  */
106  |     switch ( child_pid = fork () )
107  |     {
108  |       case 0:
109  | 	/*  Child: exec  */
110  | 	close(sd1[0]);
111  | 
112  | 	dup2( sd1[1], 1 );   /* stdout */
113  | 
114  | 	dup2( sd1[1], 2 );    /* stderr */
115  | 
116  | 	execvp (path, argv);
117  | 
118  | 	fprintf (stderr, "Could not exec: \"%s\"\t%s\n", path, ERRSTRING);
119  | 	exit (1); 
120  | 	break;
121  |       case -1:
122  | 	/*  Error  */
123  | 	fprintf (stderr, "Could not fork\t%s\n", ERRSTRING);
124  | 	return (-1);
125  | 	break;
126  |       default:
127  | 	/*  Parent  */
128  | 	break;
129  |     }
130  |     /*  Parent only  */
131  | 
132  |     close(sd1[1]);
133  | 
134  |     dup2 (sd1[0], 0); 
135  |     
136  |     /*        printf("Reading child output\n");
137  |     while (read(0, txt, 10000) != 0)
138  |       printf("child read %s\n", txt);
139  | 
140  |       printf("Finished reading child output\n");  */
141  | 
142  |     return (child_pid);
143  | }   /*  End Function spawn_job  */