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