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