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