Copied atexit() code from fakesmtp.c to fakepop.c so that its
[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 <setjmp.h>
18 #include <h/rcvmail.h>
19 #include <h/scansbr.h>
20 #include <h/tws.h>
21 #include <h/mts.h>
22 #include <signal.h>
23 #include <fcntl.h>
24
25 #ifdef HAVE_GETUTXENT
26 #include <utmpx.h>
27 #endif /* HAVE_GETUTXENT */
28
29 #define SCANFMT \
30 "%2(hour{dtimenow}):%02(min{dtimenow}): %<(size)%5(size) %>%<{encrypted}E%>\
31 %<(mymbox{from})%<{to}To:%14(friendly{to})%>%>%<(zero)%17(friendly{from})%>  \
32 %{subject}%<{body}<<%{body}>>%>"
33
34 static struct swit switches[] = {
35 #define BIFFSW  0
36     { "biff", 0 },
37 #define FORMSW  1
38     { "form formatfile", 0 },
39 #define FMTSW   2
40     { "format string", 5 },
41 #define WIDTHSW 3
42     { "width columns", 0 },
43 #define NLSW    4
44     { "newline", 0 },
45 #define NNLSW   5
46     { "nonewline", 0 },
47 #define BELSW   6
48     { "bell", 0 },
49 #define NBELSW  7
50     { "nobell", 0 },
51 #define VERSIONSW 8
52     { "version", 0 },
53 #define HELPSW  9
54     { "help", 0 },
55     { NULL, 0 }
56 };
57
58 static jmp_buf myctx;
59 static int bell = 1;
60 static int newline = 1;
61 static int biff = 0;
62 static int width = 0;
63 static char *form = NULL;
64 static char *format = NULL;
65
66 /*
67  * external prototypes
68  */
69 char *getusername(void);
70
71 /*
72  * static prototypes
73  */
74 static void alrmser (int);
75 static int message_fd (char **);
76 static int header_fd (void);
77 static void alert (char *, int);
78
79
80 int
81 main (int argc, char **argv)
82 {
83     int md, vecp = 0;
84     char *cp, *user, buf[BUFSIZ], tty[BUFSIZ];
85     char **argp, **arguments, *vec[MAXARGS];
86     struct utmpx *utp;
87 #ifdef LOCALE
88     setlocale(LC_ALL, "");
89 #endif
90     invo_name = r1bindex (argv[0], '/');
91
92     /* read user profile/context */
93     context_read();
94
95     mts_init (invo_name);
96     arguments = getarguments (invo_name, argc, argv, 1);
97     argp = arguments;
98
99     while ((cp = *argp++)) {
100         if (*cp == '-') {
101             switch (smatch (++cp, switches)) {
102                 case AMBIGSW: 
103                     ambigsw (cp, switches);
104                     done (1);
105                 case UNKWNSW: 
106                     vec[vecp++] = --cp;
107                     continue;
108
109                 case HELPSW: 
110                     snprintf (buf, sizeof(buf), "%s [command ...]", invo_name);
111                     print_help (buf, switches, 1);
112                     done (0);
113                 case VERSIONSW:
114                     print_version(invo_name);
115                     done (0);
116
117                 case BIFFSW:
118                     biff = 1;
119                     continue;
120
121                 case FORMSW: 
122                     if (!(form = *argp++) || *form == '-')
123                         adios (NULL, "missing argument to %s", argp[-2]);
124                     format = NULL;
125                     continue;
126                 case FMTSW: 
127                     if (!(format = *argp++) || *format == '-')
128                         adios (NULL, "missing argument to %s", argp[-2]);
129                     form = NULL;
130                     continue;
131
132                 case WIDTHSW:
133                     if (!(cp = *argp++) || *cp == '-')
134                         adios(NULL, "missing argument to %s", argp[-2]);
135                     width = atoi(cp);
136                     continue;
137                 case NLSW:
138                     newline = 1;
139                     continue;
140                 case NNLSW:
141                     newline = 0;
142                     continue;
143                 case BELSW:
144                     bell = 1;
145                     continue;
146                 case NBELSW:
147                     bell = 0;
148                     continue;
149
150             }
151         }
152         vec[vecp++] = cp;
153     }
154     vec[vecp] = 0;
155
156     if ((md = vecp ? message_fd (vec) : header_fd ()) == NOTOK)
157         exit (RCV_MBX);
158
159     user = getusername();
160
161 #if HAVE_GETUTXENT
162     setutxent();
163     while ((utp = getutxent()) != NULL) {
164         if (utp->ut_type == USER_PROCESS && utp->ut_user[0] != 0
165                && utp->ut_line[0] != 0
166                && strncmp (user, utp->ut_user, sizeof(utp->ut_user)) == 0) {
167             strncpy (tty, utp->ut_line, sizeof(utp->ut_line));
168             alert (tty, md);
169         }
170     }
171     endutxent();
172 #endif /* HAVE_GETUTXENT */
173
174     exit (RCV_MOK);
175     return 0;  /* dead code to satisfy the compiler */
176 }
177
178
179 static void
180 alrmser (int i)
181 {
182     NMH_UNUSED (i);
183
184     longjmp (myctx, 1);
185 }
186
187
188 static int
189 message_fd (char **vec)
190 {
191     pid_t child_id;
192     int bytes, fd, seconds;
193     char tmpfil[BUFSIZ];
194     struct stat st;
195
196     fd = mkstemp (strncpy (tmpfil, "/tmp/rcvttyXXXXX", sizeof(tmpfil)));
197     unlink (tmpfil);
198
199     if ((child_id = vfork()) == NOTOK) {
200         /* fork error */
201         close (fd);
202         return header_fd ();
203     } else if (child_id) {
204         /* parent process */
205         if (!setjmp (myctx)) {
206             SIGNAL (SIGALRM, alrmser);
207             bytes = fstat(fileno (stdin), &st) != NOTOK ? (int) st.st_size : 100;
208
209             /* amount of time to wait depends on message size */
210             if (bytes <= 100) {
211                 /* give at least 5 minutes */
212                 seconds = 300;
213             } else if (bytes >= 90000) {
214                 /* but 30 minutes should be long enough */
215                 seconds = 1800;
216             } else {
217                 seconds = (bytes / 60) + 300;
218             }
219             alarm ((unsigned int) seconds);
220             pidwait(child_id, OK);
221             alarm (0);
222
223             if (fstat (fd, &st) != NOTOK && st.st_size > (off_t) 0)
224                 return fd;
225         } else {
226             /*
227              * Ruthlessly kill the child and anything
228              * else in its process group.
229              */
230             killpg(child_id, SIGKILL);
231         }
232         close (fd);
233         return header_fd ();
234     }
235
236     /* child process */
237     rewind (stdin);
238     if (dup2 (fd, 1) == NOTOK || dup2 (fd, 2) == NOTOK)
239         _exit (-1);
240     closefds (3);
241     setpgid ((pid_t) 0, getpid ());     /* put in own process group */
242     execvp (vec[0], vec);
243     _exit (-1);
244     return 1;  /* dead code to satisfy compiler */
245 }
246
247
248 static int
249 header_fd (void)
250 {
251     int fd;
252     char *nfs;
253     char *tfile = NULL;
254
255     tfile = m_mktemp2(NULL, invo_name, &fd, NULL);
256     if (tfile == NULL) return NOTOK;
257     unlink (tfile);
258
259     rewind (stdin);
260
261     /* get new format string */
262     nfs = new_fs (form, format, SCANFMT);
263     scan (stdin, 0, 0, nfs, width, 0, 0, NULL, 0L, 0);
264     if (newline)
265         write (fd, "\n\r", 2);
266     write (fd, scanl, strlen (scanl));
267     if (bell)
268         write (fd, "\007", 1);
269
270     return fd;
271 }
272
273
274 static void
275 alert (char *tty, int md)
276 {
277     int i, td, mask;
278     char buffer[BUFSIZ], ttyspec[BUFSIZ];
279     struct stat st;
280
281     snprintf (ttyspec, sizeof(ttyspec), "/dev/%s", tty);
282
283     /*
284      * The mask depends on whether we are checking for
285      * write permission based on `biff' or `mesg'.
286      */
287     mask = biff ? S_IEXEC : (S_IWRITE >> 3);
288     if (stat (ttyspec, &st) == NOTOK || (st.st_mode & mask) == 0)
289         return;
290
291     if (!setjmp (myctx)) {
292         SIGNAL (SIGALRM, alrmser);
293         alarm (2);
294         td = open (ttyspec, O_WRONLY);
295         alarm (0);
296         if (td == NOTOK)
297             return;
298     } else {
299         alarm (0);
300         return;
301     }
302
303     lseek (md, (off_t) 0, SEEK_SET);
304
305     while ((i = read (md, buffer, sizeof(buffer))) > 0)
306         if (write (td, buffer, i) != i)
307             break;
308
309     close (td);
310 }
311