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