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