]> diplodocus.org Git - nmh/blob - uip/rcvtty.c
6f8ded3586caa928402876eff810ccf8f0a65f41
[nmh] / uip / rcvtty.c
1 /* rcvtty.c -- a rcvmail program (a lot like rcvalert) handling IPC ttys
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 /* Changed to use getutent() and friends. Assumes that when getutent() exists,
9 * a number of other things also exist. Please check.
10 * Ruud de Rooij <ruud@ruud.org> Sun, 28 May 2000 17:28:55 +0200
11 */
12
13 #include "h/mh.h"
14 #include "scansbr.h"
15 #include "sbr/getarguments.h"
16 #include "sbr/smatch.h"
17 #include "sbr/closefds.h"
18 #include "sbr/ambigsw.h"
19 #include "sbr/print_version.h"
20 #include "sbr/print_help.h"
21 #include "sbr/error.h"
22 #include "h/signals.h"
23 #include <setjmp.h>
24 #include "h/rcvmail.h"
25 #include "h/fmt_scan.h"
26 #include "h/tws.h"
27 #include "h/mts.h"
28 #include "h/done.h"
29 #include "h/utils.h"
30 #include "sbr/m_mktemp.h"
31 #include <fcntl.h>
32
33 #ifdef HAVE_GETUTXENT
34 #include <utmpx.h>
35 #endif /* HAVE_GETUTXENT */
36
37 #define SCANFMT \
38 "%2(hour{dtimenow}):%02(min{dtimenow}): %<(size)%5(size) %>%<{encrypted}E%>\
39 %<(mymbox{from})%<{to}To:%14(friendly{to})%>%>%<(zero)%17(friendly{from})%> \
40 %{subject}%<{body}<<%{body}>>%>"
41
42 #define RCVTTY_SWITCHES \
43 X("biff", 0, BIFFSW) \
44 X("form formatfile", 0, FORMSW) \
45 X("format string", 5, FMTSW) \
46 X("width columns", 0, WIDTHSW) \
47 X("newline", 0, NLSW) \
48 X("nonewline", 0, NNLSW) \
49 X("bell", 0, BELSW) \
50 X("nobell", 0, NBELSW) \
51 X("version", 0, VERSIONSW) \
52 X("help", 0, HELPSW) \
53
54 #define X(sw, minchars, id) id,
55 DEFINE_SWITCH_ENUM(RCVTTY);
56 #undef X
57
58 #define X(sw, minchars, id) { sw, minchars, id },
59 DEFINE_SWITCH_ARRAY(RCVTTY, switches);
60 #undef X
61
62 static jmp_buf myctx;
63 static bool bell = true;
64 static bool newline = true;
65 static bool biff;
66 static int width = -1;
67 static char *form = NULL;
68 static char *format = NULL;
69
70 /*
71 * static prototypes
72 */
73 static void alrmser (int);
74 static int message_fd (char **);
75 static int header_fd (void);
76 #if HAVE_GETUTXENT
77 static void alert (char *, int);
78 #endif /* HAVE_GETUTXENT */
79
80
81 int
82 main (int argc, char **argv)
83 {
84 int md, vecp = 0;
85 char *cp, *user, buf[BUFSIZ], tty[BUFSIZ];
86 char **argp, **arguments, *vec[MAXARGS];
87 struct utmpx *utp;
88
89 if (nmh_init(argv[0], true, false)) { return 1; }
90
91 mts_init ();
92 arguments = getarguments (invo_name, argc, argv, 1);
93 argp = arguments;
94
95 while ((cp = *argp++)) {
96 if (*cp == '-') {
97 switch (smatch (++cp, switches)) {
98 case AMBIGSW:
99 ambigsw (cp, switches);
100 done (1);
101 case UNKWNSW:
102 vec[vecp++] = --cp;
103 continue;
104
105 case HELPSW:
106 snprintf (buf, sizeof(buf), "%s [command ...]", invo_name);
107 print_help (buf, switches, 1);
108 done (0);
109 case VERSIONSW:
110 print_version(invo_name);
111 done (0);
112
113 case BIFFSW:
114 biff = true;
115 continue;
116
117 case FORMSW:
118 if (!(form = *argp++) || *form == '-')
119 die("missing argument to %s", argp[-2]);
120 format = NULL;
121 continue;
122 case FMTSW:
123 if (!(format = *argp++) || *format == '-')
124 die("missing argument to %s", argp[-2]);
125 form = NULL;
126 continue;
127
128 case WIDTHSW:
129 if (!(cp = *argp++) || *cp == '-')
130 die("missing argument to %s", argp[-2]);
131 width = atoi(cp);
132 continue;
133 case NLSW:
134 newline = true;
135 continue;
136 case NNLSW:
137 newline = false;
138 continue;
139 case BELSW:
140 bell = true;
141 continue;
142 case NBELSW:
143 bell = false;
144 continue;
145
146 }
147 }
148 vec[vecp++] = cp;
149 }
150 vec[vecp] = 0;
151
152 if ((md = vecp ? message_fd (vec) : header_fd ()) == NOTOK)
153 exit (RCV_MBX);
154
155 user = getusername();
156
157 #if HAVE_GETUTXENT
158 setutxent();
159 while ((utp = getutxent()) != NULL) {
160 if (utp->ut_type == USER_PROCESS && utp->ut_user[0] != 0
161 && utp->ut_line[0] != 0
162 && strncmp (user, utp->ut_user, sizeof(utp->ut_user)) == 0) {
163 strncpy (tty, utp->ut_line, sizeof(utp->ut_line));
164 alert (tty, md);
165 }
166 }
167 endutxent();
168 #else
169 NMH_UNUSED (tty);
170 NMH_UNUSED (utp);
171 #endif /* HAVE_GETUTXENT */
172
173 exit (RCV_MOK);
174 }
175
176
177 static void
178 alrmser (int i)
179 {
180 NMH_UNUSED (i);
181
182 longjmp (myctx, 1);
183 }
184
185
186 static int
187 message_fd (char **vec)
188 {
189 pid_t child_id;
190 int bytes, seconds;
191 int fd;
192 char *tfile;
193 struct stat st;
194
195 if ((tfile = m_mktemp2(NULL, invo_name, &fd, NULL)) == NULL) {
196 inform("unable to create temporary file in %s", get_temp_dir());
197 return NOTOK;
198 }
199 (void) m_unlink(tfile); /* Use fd, no longer need the file name. */
200
201 if ((child_id = fork()) == NOTOK) {
202 /* fork error */
203 close (fd);
204 return header_fd ();
205 }
206 if (child_id) {
207 /* parent process */
208 if (!setjmp (myctx)) {
209 SIGNAL (SIGALRM, alrmser);
210 bytes = fstat(fileno (stdin), &st) != NOTOK ? (int) st.st_size : 100;
211
212 /* amount of time to wait depends on message size */
213 if (bytes <= 100) {
214 /* give at least 5 minutes */
215 seconds = 300;
216 } else if (bytes >= 90000) {
217 /* but 30 minutes should be long enough */
218 seconds = 1800;
219 } else {
220 seconds = (bytes / 60) + 300;
221 }
222 alarm ((unsigned int) seconds);
223 pidwait(child_id, OK);
224 alarm (0);
225
226 if (fstat (fd, &st) != NOTOK && st.st_size > 0)
227 return fd;
228 } else {
229 /*
230 * Ruthlessly kill the child and anything
231 * else in its process group.
232 */
233 killpg(child_id, SIGKILL);
234 }
235 close (fd);
236 return header_fd ();
237 }
238
239 /* child process */
240 rewind (stdin);
241 if (dup2 (fd, 1) == NOTOK || dup2 (fd, 2) == NOTOK)
242 _exit(1);
243 closefds (3);
244 setpgid(0, getpid()); /* put in own process group */
245 if (execvp (vec[0], vec) == NOTOK) {
246 _exit(1);
247 }
248
249 return NOTOK;
250 }
251
252
253 static int
254 header_fd (void)
255 {
256 int fd;
257 char *nfs;
258 char *tfile = NULL;
259 charstring_t scanl = NULL;
260
261 if ((tfile = m_mktemp2(NULL, invo_name, &fd, NULL)) == NULL) {
262 inform("unable to create temporary file in %s", get_temp_dir());
263 return NOTOK;
264 }
265 (void) m_unlink(tfile); /* Use fd, no longer need the file name. */
266
267 rewind (stdin);
268
269 /* get new format string */
270 nfs = new_fs (form, format, SCANFMT);
271 scan (stdin, 0, 0, nfs, width, 0, 0, NULL, 0L, 0, &scanl);
272 scan_finished ();
273 if (newline) {
274 if (write (fd, "\n\r", 2) < 0) {
275 advise (tfile, "write LF/CR");
276 }
277 }
278 if (write (fd, charstring_buffer (scanl), charstring_bytes (scanl)) < 0) {
279 advise (tfile, "write");
280 }
281 charstring_free (scanl);
282 if (bell) {
283 if (write (fd, "\007", 1) < 0) {
284 advise (tfile, "write BEL");
285 }
286 }
287
288 return fd;
289 }
290
291
292 #if HAVE_GETUTXENT
293 static void
294 alert (char *tty, int md)
295 {
296 int i, td, mask;
297 char buffer[BUFSIZ], ttyspec[BUFSIZ];
298 struct stat st;
299
300 snprintf (ttyspec, sizeof(ttyspec), "/dev/%s", tty);
301
302 /*
303 * The mask depends on whether we are checking for
304 * write permission based on `biff' or `mesg'.
305 */
306 mask = biff ? S_IEXEC : (S_IWRITE >> 3);
307 if (stat (ttyspec, &st) == NOTOK || (st.st_mode & mask) == 0)
308 return;
309
310 if (setjmp (myctx)) {
311 alarm (0);
312 return;
313 }
314 SIGNAL (SIGALRM, alrmser);
315 alarm (2);
316 td = open (ttyspec, O_WRONLY);
317 alarm (0);
318 if (td == NOTOK)
319 return;
320
321 lseek(md, 0, SEEK_SET);
322
323 while ((i = read (md, buffer, sizeof(buffer))) > 0)
324 if (write (td, buffer, i) != i)
325 break;
326
327 close (td);
328 }
329 #endif /* HAVE_GETUTXENT */