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