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