Added NMH_UNUSED macro for suppressing warnings about unused parameters
[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, fd, seconds;
221     char tmpfil[BUFSIZ];
222     struct stat st;
223
224     fd = mkstemp (strncpy (tmpfil, "/tmp/rcvttyXXXXX", sizeof(tmpfil)));
225     unlink (tmpfil);
226
227     if ((child_id = vfork()) == NOTOK) {
228         /* fork error */
229         close (fd);
230         return header_fd ();
231     } else if (child_id) {
232         /* parent process */
233         if (!setjmp (myctx)) {
234             SIGNAL (SIGALRM, alrmser);
235             bytes = fstat(fileno (stdin), &st) != NOTOK ? (int) st.st_size : 100;
236
237             /* amount of time to wait depends on message size */
238             if (bytes <= 100) {
239                 /* give at least 5 minutes */
240                 seconds = 300;
241             } else if (bytes >= 90000) {
242                 /* but 30 minutes should be long enough */
243                 seconds = 1800;
244             } else {
245                 seconds = (bytes / 60) + 300;
246             }
247             alarm ((unsigned int) seconds);
248             pidwait(child_id, OK);
249             alarm (0);
250
251             if (fstat (fd, &st) != NOTOK && st.st_size > (off_t) 0)
252                 return fd;
253         } else {
254             /*
255              * Ruthlessly kill the child and anything
256              * else in its process group.
257              */
258             killpg(child_id, SIGKILL);
259         }
260         close (fd);
261         return header_fd ();
262     }
263
264     /* child process */
265     rewind (stdin);
266     if (dup2 (fd, 1) == NOTOK || dup2 (fd, 2) == NOTOK)
267         _exit (-1);
268     closefds (3);
269     setpgid ((pid_t) 0, getpid ());     /* put in own process group */
270     execvp (vec[0], vec);
271     _exit (-1);
272     return 1;  /* dead code to satisfy compiler */
273 }
274
275
276 static int
277 header_fd (void)
278 {
279     int fd;
280     char *nfs;
281     char *tfile = NULL;
282
283     tfile = m_mktemp2(NULL, invo_name, &fd, NULL);
284     if (tfile == NULL) return NOTOK;
285     unlink (tfile);
286
287     rewind (stdin);
288
289     /* get new format string */
290     nfs = new_fs (form, format, SCANFMT);
291     scan (stdin, 0, 0, nfs, width, 0, 0, NULL, 0L, 0);
292     if (newline)
293         write (fd, "\n\r", 2);
294     write (fd, scanl, strlen (scanl));
295     if (bell)
296         write (fd, "\007", 1);
297
298     return fd;
299 }
300
301
302 static void
303 alert (char *tty, int md)
304 {
305     int i, td, mask;
306     char buffer[BUFSIZ], ttyspec[BUFSIZ];
307     struct stat st;
308
309     snprintf (ttyspec, sizeof(ttyspec), "/dev/%s", tty);
310
311     /*
312      * The mask depends on whether we are checking for
313      * write permission based on `biff' or `mesg'.
314      */
315     mask = biff ? S_IEXEC : (S_IWRITE >> 3);
316     if (stat (ttyspec, &st) == NOTOK || (st.st_mode & mask) == 0)
317         return;
318
319     if (!setjmp (myctx)) {
320         SIGNAL (SIGALRM, alrmser);
321         alarm (2);
322         td = open (ttyspec, O_WRONLY);
323         alarm (0);
324         if (td == NOTOK)
325             return;
326     } else {
327         alarm (0);
328         return;
329     }
330
331     lseek (md, (off_t) 0, SEEK_SET);
332
333     while ((i = read (md, buffer, sizeof(buffer))) > 0)
334         if (write (td, buffer, i) != i)
335             break;
336
337     close (td);
338 }
339