Relayouted all switch statements: case aligns with switch.
[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}%<{body}<<%{body}>>%>"
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         mts_init(invo_name);
108         arguments = getarguments(invo_name, argc, argv, 1);
109         argp = arguments;
110
111         while ((cp = *argp++)) {
112                 if (*cp == '-') {
113                         switch (smatch(++cp, switches)) {
114                         case AMBIGSW:
115                                 ambigsw(cp, switches);
116                                 done(1);
117                         case UNKWNSW:
118                                 vec[vecp++] = --cp;
119                                 continue;
120
121                         case HELPSW:
122                                 snprintf(buf, sizeof(buf), "%s [command ...]",
123                                                 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",
137                                                         argp[-2]);
138                                 format = NULL;
139                                 continue;
140                         case FMTSW:
141                                 if (!(format = *argp++) || *format == '-')
142                                         adios(NULL, "missing argument to %s",
143                                                         argp[-2]);
144                                 form = NULL;
145                                 continue;
146
147                         case WIDTHSW:
148                                 if (!(cp = *argp++) || *cp == '-')
149                                         adios(NULL, "missing argument to %s",
150                                                         argp[-2]);
151                                 width = atoi(cp);
152                                 continue;
153                         case NLSW:
154                                 newline = 1;
155                                 continue;
156                         case NNLSW:
157                                 newline = 0;
158                                 continue;
159                         case BELSW:
160                                 bell = 1;
161                                 continue;
162                         case NBELSW:
163                                 bell = 0;
164                                 continue;
165
166                         }
167                 }
168                 vec[vecp++] = cp;
169         }
170         vec[vecp] = 0;
171
172         if ((md = vecp ? message_fd(vec) : header_fd()) == NOTOK)
173                 exit(RCV_MBX);
174
175         user = getusername();
176
177 #ifdef HAVE_GETUTENT
178         setutent();
179         while ((utp = getutent()) != NULL) {
180                 if (
181 #ifdef HAVE_STRUCT_UTMP_UT_TYPE
182                                 utp->ut_type == USER_PROCESS &&
183 #endif
184                                 utp->ut_name[0] != 0 && utp->ut_line[0] != 0
185                                 && strncmp(user, utp->ut_name,
186                                 sizeof(utp->ut_name)) == 0) {
187                         strncpy(tty, utp->ut_line, sizeof(utp->ut_line));
188                         alert(tty, md);
189                 }
190         }
191         endutent();
192 #else
193         if ((uf = fopen(UTMP_FILE, "r")) == NULL)
194                 exit(RCV_MBX);
195         while (fread((char *) &ut, sizeof(ut), 1, uf) == 1)
196                 if (ut.ut_name[0] != 0 &&
197                                 strncmp(user, ut.ut_name, sizeof(ut.ut_name))
198                                 == 0) {
199                         strncpy(tty, ut.ut_line, sizeof(ut.ut_line));
200                         alert(tty, md);
201                 }
202         fclose(uf);
203 #endif
204
205         exit(RCV_MOK);
206         return 0;  /* dead code to satisfy the compiler */
207 }
208
209
210 static RETSIGTYPE
211 alrmser(int i)
212 {
213 #ifndef RELIABLE_SIGNALS
214         SIGNAL(SIGALRM, alrmser);
215 #endif
216
217         longjmp(myctx, 1);
218 }
219
220
221 static int
222 message_fd(char **vec)
223 {
224         pid_t child_id;
225         int bytes, fd, seconds;
226         char tmpfil[BUFSIZ];
227         struct stat st;
228
229 #ifdef HAVE_MKSTEMP
230         fd = mkstemp(strncpy(tmpfil, "/tmp/rcvttyXXXXX", sizeof(tmpfil)));
231 #else
232         unlink(mktemp(strncpy(tmpfil, "/tmp/rcvttyXXXXX", sizeof(tmpfil))));
233         if ((fd = open(tmpfil, O_RDWR | O_CREAT | O_TRUNC, 0600)) == NOTOK)
234                 return header_fd();
235 #endif
236         unlink(tmpfil);
237
238         if ((child_id = vfork()) == NOTOK) {
239                 /* fork error */
240                 close(fd);
241                 return header_fd();
242         } else if (child_id) {
243                 /* parent process */
244                 if (!setjmp(myctx)) {
245                         SIGNAL(SIGALRM, alrmser);
246                         bytes = fstat(fileno(stdin), &st) != NOTOK ?
247                                         (int) st.st_size : 100;
248
249                         /* amount of time to wait depends on message size */
250                         if (bytes <= 100) {
251                                 /* give at least 5 minutes */
252                                 seconds = 300;
253                         } else if (bytes >= 90000) {
254                                 /* but 30 minutes should be long enough */
255                                 seconds = 1800;
256                         } else {
257                                 seconds = (bytes / 60) + 300;
258                         }
259                         alarm((unsigned int) seconds);
260                         pidwait(child_id, OK);
261                         alarm(0);
262
263                         if (fstat(fd, &st) != NOTOK && st.st_size > (off_t) 0)
264                                 return fd;
265                 } else {
266                         /*
267                         ** Ruthlessly kill the child and anything
268                         ** else in its process group.
269                         */
270                         KILLPG(child_id, SIGKILL);
271                 }
272                 close(fd);
273                 return header_fd();
274         }
275
276         /* child process */
277         rewind(stdin);
278         if (dup2(fd, 1) == NOTOK || dup2(fd, 2) == NOTOK)
279                 _exit(-1);
280         closefds(3);
281         setpgid((pid_t) 0, getpid());  /* put in own process group */
282         execvp(vec[0], vec);
283         _exit(-1);
284         return 1;  /* dead code to satisfy compiler */
285 }
286
287
288 static int
289 header_fd(void)
290 {
291         int fd;
292         char *nfs;
293         char *tfile = NULL;
294
295         tfile = m_mktemp2(NULL, invo_name, &fd, NULL);
296         if (tfile == NULL) return NOTOK;
297         unlink(tfile);
298
299         rewind(stdin);
300
301         /* get new format string */
302         nfs = new_fs(form, format, SCANFMT);
303         scan(stdin, 0, 0, nfs, width, 0, 0, NULL, 0L, 0);
304         if (newline)
305                 write(fd, "\n\r", 2);
306         write(fd, scanl, strlen(scanl));
307         if (bell)
308                 write(fd, "\007", 1);
309
310         return fd;
311 }
312
313
314 static void
315 alert(char *tty, int md)
316 {
317         int i, td, mask;
318         char buffer[BUFSIZ], ttyspec[BUFSIZ];
319         struct stat st;
320
321         snprintf(ttyspec, sizeof(ttyspec), "/dev/%s", tty);
322
323         /*
324         ** The mask depends on whether we are checking for
325         ** write permission based on `biff' or `mesg'.
326         */
327         mask = biff ? S_IEXEC : (S_IWRITE >> 3);
328         if (stat(ttyspec, &st) == NOTOK || (st.st_mode & mask) == 0)
329                 return;
330
331         if (!setjmp(myctx)) {
332                 SIGNAL(SIGALRM, alrmser);
333                 alarm(2);
334                 td = open(ttyspec, O_WRONLY);
335                 alarm(0);
336                 if (td == NOTOK)
337                         return;
338         } else {
339                 alarm(0);
340                 return;
341         }
342
343         lseek(md, (off_t) 0, SEEK_SET);
344
345         while ((i = read(md, buffer, sizeof(buffer))) > 0)
346                 if (write(td, buffer, i) != i)
347                         break;
348
349         close(td);
350 }