use mkstemp on systems that have it
[mmh] / uip / rcvtty.c
1
2 /*
3  * rcvtty.c -- a rcvmail program (a lot like rcvalert) handling IPC ttys
4  *
5  * $Id$
6  */
7
8 #include <h/mh.h>
9 #include <h/signals.h>
10 #include <h/rcvmail.h>
11 #include <h/scansbr.h>
12 #include <zotnet/tws/tws.h>
13 #include <signal.h>
14 #include <fcntl.h>
15
16 #include <utmp.h>
17 #ifndef UTMP_FILE
18 # ifdef _PATH_UTMP
19 #  define UTMP_FILE _PATH_UTMP
20 # else
21 #  define UTMP_FILE "/etc/utmp"
22 # endif
23 #endif
24
25 #define SCANFMT \
26 "%2(hour{dtimenow}):%02(min{dtimenow}): %<(size)%5(size) %>%<{encrypted}E%>\
27 %<(mymbox{from})%<{to}To:%14(friendly{to})%>%>%<(zero)%17(friendly{from})%>  \
28 %{subject}%<{body}<<%{body}>>%>"
29
30 static struct swit switches[] = {
31 #define BIFFSW  0
32     { "biff", 0 },
33 #define FORMSW  1
34     { "form formatfile", 0 },
35 #define FMTSW   2
36     { "format string", 5 },
37 #define WIDTHSW 3
38     { "width columns", 0 },
39 #define NLSW    4
40     { "newline", 0 },
41 #define NNLSW   5
42     { "nonewline", 0 },
43 #define BELSW   6
44     { "bell", 0 },
45 #define NBELSW  7
46     { "nobell", 0 },
47 #define VERSIONSW 8
48     { "version", 0 },
49 #define HELPSW  9
50     { "help", 4 },
51     { NULL, 0 }
52 };
53
54 static jmp_buf myctx;
55 static int bell = 1;
56 static int newline = 1;
57 static int biff = 0;
58 static int width = 0;
59 static char *form = NULL;
60 static char *format = NULL;
61
62 /*
63  * external prototypes
64  */
65 char *getusername(void);
66
67 /*
68  * static prototypes
69  */
70 static RETSIGTYPE alrmser (int);
71 static int message_fd (char **);
72 static int header_fd (void);
73 static void alert (char *, int);
74
75
76 int
77 main (int argc, char **argv)
78 {
79     int md, vecp = 0;
80     char *cp, *user, buf[BUFSIZ], tty[BUFSIZ];
81     char **argp, **arguments, *vec[MAXARGS];
82     struct utmp ut;
83     register FILE *uf;
84
85 #ifdef LOCALE
86     setlocale(LC_ALL, "");
87 #endif
88     invo_name = r1bindex (argv[0], '/');
89
90     /* read user profile/context */
91     context_read();
92
93     mts_init (invo_name);
94     arguments = getarguments (invo_name, argc, argv, 1);
95     argp = arguments;
96
97     while ((cp = *argp++)) {
98         if (*cp == '-') {
99             switch (smatch (++cp, switches)) {
100                 case AMBIGSW: 
101                     ambigsw (cp, switches);
102                     done (1);
103                 case UNKWNSW: 
104                     vec[vecp++] = --cp;
105                     continue;
106
107                 case HELPSW: 
108                     snprintf (buf, sizeof(buf), "%s [command ...]", invo_name);
109                     print_help (buf, switches, 1);
110                     done (1);
111                 case VERSIONSW:
112                     print_version(invo_name);
113                     done (1);
114
115                 case BIFFSW:
116                     biff = 1;
117                     continue;
118
119                 case FORMSW: 
120                     if (!(form = *argp++) || *form == '-')
121                         adios (NULL, "missing argument to %s", argp[-2]);
122                     format = NULL;
123                     continue;
124                 case FMTSW: 
125                     if (!(format = *argp++) || *format == '-')
126                         adios (NULL, "missing argument to %s", argp[-2]);
127                     form = NULL;
128                     continue;
129
130                 case WIDTHSW:
131                     if (!(cp = *argp++) || *cp == '-')
132                         adios(NULL, "missing argument to %s", argp[-2]);
133                     width = atoi(cp);
134                     continue;
135                 case NLSW:
136                     newline = 1;
137                     continue;
138                 case NNLSW:
139                     newline = 0;
140                     continue;
141                 case BELSW:
142                     bell = 1;
143                     continue;
144                 case NBELSW:
145                     bell = 0;
146                     continue;
147
148             }
149         }
150         vec[vecp++] = cp;
151     }
152     vec[vecp] = 0;
153
154     if ((md = vecp ? message_fd (vec) : header_fd ()) == NOTOK)
155         exit (RCV_MBX);
156
157     user = getusername();
158     if ((uf = fopen (UTMP_FILE, "r")) == NULL)
159         exit (RCV_MBX);
160
161     while (fread ((char *) &ut, sizeof(ut), 1, uf) == 1)
162         if (ut.ut_name[0] != 0
163                 && strncmp (user, ut.ut_name, sizeof(ut.ut_name)) == 0) {
164             strncpy (tty, ut.ut_line, sizeof(ut.ut_line));
165             alert (tty, md);
166         }
167
168     fclose (uf);
169     exit (RCV_MOK);
170 }
171
172
173 static RETSIGTYPE
174 alrmser (int i)
175 {
176 #ifndef RELIABLE_SIGNALS
177     SIGNAL (SIGALRM, alrmser);
178 #endif
179
180     longjmp (myctx, 1);
181 }
182
183
184 static int
185 message_fd (char **vec)
186 {
187     pid_t child_id;
188     int bytes, fd, seconds;
189     char tmpfil[BUFSIZ];
190     struct stat st;
191
192 #ifdef HAVE_MKSTEMP
193     unlink (mkstemp (strncpy (tmpfil, "/tmp/rcvttyXXXXX", sizeof(tmpfil))));
194 #else
195     unlink (mktemp (strncpy (tmpfil, "/tmp/rcvttyXXXXX", sizeof(tmpfil))));
196 #endif
197     if ((fd = open (tmpfil, O_RDWR | O_CREAT | O_TRUNC, 0600)) == NOTOK)
198         return header_fd ();
199     unlink (tmpfil);
200
201     if ((child_id = vfork()) == NOTOK) {
202         /* fork error */
203         close (fd);
204         return header_fd ();
205     } else if (child_id) {
206         /* parent process */
207         if (!setjmp (myctx)) {
208             SIGNAL (SIGALRM, alrmser);
209             bytes = fstat(fileno (stdin), &st) != NOTOK ? (int) st.st_size : 100;
210
211             /* amount of time to wait depends on message size */
212             if (bytes <= 100) {
213                 /* give at least 5 minutes */
214                 seconds = 300;
215             } else if (bytes >= 90000) {
216                 /* but 30 minutes should be long enough */
217                 seconds = 1800;
218             } else {
219                 seconds = (bytes / 60) + 300;
220             }
221             alarm ((unsigned int) seconds);
222             pidwait(child_id, OK);
223             alarm (0);
224
225             if (fstat (fd, &st) != NOTOK && st.st_size > (off_t) 0)
226                 return fd;
227         } else {
228             /*
229              * Ruthlessly kill the child and anything
230              * else in its process group.
231              */
232             KILLPG(child_id, SIGKILL);
233         }
234         close (fd);
235         return header_fd ();
236     }
237
238     /* child process */
239     rewind (stdin);
240     if (dup2 (fd, 1) == NOTOK || dup2 (fd, 2) == NOTOK)
241         _exit (-1);
242     closefds (3);
243     setpgid ((pid_t) 0, getpid ());     /* put in own process group */
244     execvp (vec[0], vec);
245     _exit (-1);
246 }
247
248
249 static int
250 header_fd (void)
251 {
252     int fd;
253     char *nfs, tmpfil[BUFSIZ];
254
255     strncpy (tmpfil, m_tmpfil (invo_name), sizeof(tmpfil));
256     if ((fd = open (tmpfil, O_RDWR | O_CREAT | O_TRUNC, 0600)) == NOTOK)
257         return NOTOK;
258     unlink (tmpfil);
259
260     rewind (stdin);
261
262     /* get new format string */
263     nfs = new_fs (form, format, SCANFMT);
264     scan (stdin, 0, 0, nfs, width, 0, 0, NULL, 0L, 0);
265     if (newline)
266         write (fd, "\n\r", 2);
267     write (fd, scanl, strlen (scanl));
268     if (bell)
269         write (fd, "\007", 1);
270
271     return fd;
272 }
273
274
275 static void
276 alert (char *tty, int md)
277 {
278     int i, td, mask;
279     char buffer[BUFSIZ], ttyspec[BUFSIZ];
280     struct stat st;
281
282     snprintf (ttyspec, sizeof(ttyspec), "/dev/%s", tty);
283
284     /*
285      * The mask depends on whether we are checking for
286      * write permission based on `biff' or `mesg'.
287      */
288     mask = biff ? S_IEXEC : (S_IWRITE >> 3);
289     if (stat (ttyspec, &st) == NOTOK || (st.st_mode & mask) == 0)
290         return;
291
292     if (!setjmp (myctx)) {
293         SIGNAL (SIGALRM, alrmser);
294         alarm (2);
295         td = open (ttyspec, O_WRONLY);
296         alarm (0);
297         if (td == NOTOK)
298             return;
299     } else {
300         alarm (0);
301         return;
302     }
303
304     lseek (md, (off_t) 0, SEEK_SET);
305
306     while ((i = read (md, buffer, sizeof(buffer))) > 0)
307         if (write (td, buffer, i) != i)
308             break;
309
310     close (td);
311 }
312