1ae85765e1e27b51db605c99fe6237a79cdc474a
[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/m_setjmp.h>
18 #include <h/rcvmail.h>
19 #include <h/scansbr.h>
20 #include <h/tws.h>
21 #include <h/mts.h>
22 #include <signal.h>
23 #include <fcntl.h>
24
25 #include <utmp.h>
26
27 #ifndef HAVE_GETUTENT
28 # ifndef UTMP_FILE
29 #  ifdef _PATH_UTMP
30 #   define UTMP_FILE _PATH_UTMP
31 #  else
32 #   define UTMP_FILE "/etc/utmp"
33 #  endif
34 # endif
35 #endif
36
37 #define SCANFMT \
38 "%2(hour{dtimenow}):%02(min{dtimenow}): %<(size)%5(size) %>%<{encrypted}E%>\
39 %<(mymbox{from})%<{to}To:%14(friendly{to})%>%>%<(zero)%17(friendly{from})%>  \
40 %{subject}%<{body}<<%{body}>>%>"
41
42 static struct swit switches[] = {
43 #define BIFFSW  0
44     { "biff", 0 },
45 #define FORMSW  1
46     { "form formatfile", 0 },
47 #define FMTSW   2
48     { "format string", 5 },
49 #define WIDTHSW 3
50     { "width columns", 0 },
51 #define NLSW    4
52     { "newline", 0 },
53 #define NNLSW   5
54     { "nonewline", 0 },
55 #define BELSW   6
56     { "bell", 0 },
57 #define NBELSW  7
58     { "nobell", 0 },
59 #define VERSIONSW 8
60     { "version", 0 },
61 #define HELPSW  9
62     { "help", 0 },
63     { NULL, 0 }
64 };
65
66 static jmp_buf myctx;
67 static int bell = 1;
68 static int newline = 1;
69 static int biff = 0;
70 static int width = 0;
71 static char *form = NULL;
72 static char *format = NULL;
73
74 /*
75  * external prototypes
76  */
77 char *getusername(void);
78
79 /*
80  * static prototypes
81  */
82 static void alrmser (int);
83 static int message_fd (char **);
84 static int header_fd (void);
85 static void alert (char *, int);
86
87
88 int
89 main (int argc, char **argv)
90 {
91     int md, vecp = 0;
92     char *cp, *user, buf[BUFSIZ], tty[BUFSIZ];
93     char **argp, **arguments, *vec[MAXARGS];
94 #ifdef HAVE_GETUTENT
95     struct utmp * utp;
96 #else
97     struct utmp ut;
98     register FILE *uf;
99 #endif
100
101 #ifdef LOCALE
102     setlocale(LC_ALL, "");
103 #endif
104     invo_name = r1bindex (argv[0], '/');
105
106     /* read user profile/context */
107     context_read();
108
109     mts_init (invo_name);
110     arguments = getarguments (invo_name, argc, argv, 1);
111     argp = arguments;
112
113     while ((cp = *argp++)) {
114         if (*cp == '-') {
115             switch (smatch (++cp, switches)) {
116                 case AMBIGSW: 
117                     ambigsw (cp, switches);
118                     done (1);
119                 case UNKWNSW: 
120                     vec[vecp++] = --cp;
121                     continue;
122
123                 case HELPSW: 
124                     snprintf (buf, sizeof(buf), "%s [command ...]", invo_name);
125                     print_help (buf, switches, 1);
126                     done (1);
127                 case VERSIONSW:
128                     print_version(invo_name);
129                     done (1);
130
131                 case BIFFSW:
132                     biff = 1;
133                     continue;
134
135                 case FORMSW: 
136                     if (!(form = *argp++) || *form == '-')
137                         adios (NULL, "missing argument to %s", argp[-2]);
138                     format = NULL;
139                     continue;
140                 case FMTSW: 
141                     if (!(format = *argp++) || *format == '-')
142                         adios (NULL, "missing argument to %s", argp[-2]);
143                     form = NULL;
144                     continue;
145
146                 case WIDTHSW:
147                     if (!(cp = *argp++) || *cp == '-')
148                         adios(NULL, "missing argument to %s", argp[-2]);
149                     width = atoi(cp);
150                     continue;
151                 case NLSW:
152                     newline = 1;
153                     continue;
154                 case NNLSW:
155                     newline = 0;
156                     continue;
157                 case BELSW:
158                     bell = 1;
159                     continue;
160                 case NBELSW:
161                     bell = 0;
162                     continue;
163
164             }
165         }
166         vec[vecp++] = cp;
167     }
168     vec[vecp] = 0;
169
170     if ((md = vecp ? message_fd (vec) : header_fd ()) == NOTOK)
171         exit (RCV_MBX);
172
173     user = getusername();
174
175 #ifdef HAVE_GETUTENT
176     setutent();
177     while ((utp = getutent()) != NULL) {
178         if (
179 #ifdef HAVE_STRUCT_UTMP_UT_TYPE
180                utp->ut_type == USER_PROCESS 
181                &&
182 #endif
183                utp->ut_name[0] != 0
184                && utp->ut_line[0] != 0
185                && strncmp (user, utp->ut_name, sizeof(utp->ut_name)) == 0) {
186             strncpy (tty, utp->ut_line, sizeof(utp->ut_line));
187             alert (tty, md);
188         }
189     }
190     endutent();
191 #else
192     if ((uf = fopen (UTMP_FILE, "r")) == NULL)
193         exit (RCV_MBX);
194     while (fread ((char *) &ut, sizeof(ut), 1, uf) == 1)
195         if (ut.ut_name[0] != 0
196                 && strncmp (user, ut.ut_name, sizeof(ut.ut_name)) == 0) {
197             strncpy (tty, ut.ut_line, sizeof(ut.ut_line));
198             alert (tty, md);
199         }
200     fclose (uf);
201 #endif
202
203     exit (RCV_MOK);
204     return 0;  /* dead code to satisfy the compiler */
205 }
206
207
208 static void
209 alrmser (int i)
210 {
211     NMH_UNUSED (i);
212
213     longjmp (myctx, 1);
214 }
215
216
217 static int
218 message_fd (char **vec)
219 {
220     pid_t child_id;
221     int bytes, fd, seconds;
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 = m_vfork()) == NOTOK) {
229         /* fork error */
230         close (fd);
231         return header_fd ();
232     } else if (child_id) {
233         /* parent process */
234         if (!m_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 (!m_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