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