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