]> diplodocus.org Git - nmh/blob - uip/rcvtty.c
447c6e402b191b7da739bbad09dd10d0066da523
[nmh] / uip / rcvtty.c
1
2 /*
3 * rcvtty.c -- a rcvmail program (a lot like rcvalert) handling IPC ttys
4 *
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
8 */
9
10 /* Changed to use getutent() and friends. Assumes that when getutent() exists,
11 * a number of other things also exist. Please check.
12 * Ruud de Rooij <ruud@ruud.org> Sun, 28 May 2000 17:28:55 +0200
13 */
14
15 #include <h/mh.h>
16 #include <h/signals.h>
17 #include <setjmp.h>
18 #include <h/rcvmail.h>
19 #include <h/scansbr.h>
20 #include <h/tws.h>
21 #include <h/mts.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 = 0;
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
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 setlocale(LC_ALL, "");
86 invo_name = r1bindex (argv[0], '/');
87
88 /* read user profile/context */
89 context_read();
90
91 mts_init (invo_name);
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 = 1;
115 continue;
116
117 case FORMSW:
118 if (!(form = *argp++) || *form == '-')
119 adios (NULL, "missing argument to %s", argp[-2]);
120 format = NULL;
121 continue;
122 case FMTSW:
123 if (!(format = *argp++) || *format == '-')
124 adios (NULL, "missing argument to %s", argp[-2]);
125 form = NULL;
126 continue;
127
128 case WIDTHSW:
129 if (!(cp = *argp++) || *cp == '-')
130 adios(NULL, "missing argument to %s", argp[-2]);
131 width = atoi(cp);
132 continue;
133 case NLSW:
134 newline = 1;
135 continue;
136 case NNLSW:
137 newline = 0;
138 continue;
139 case BELSW:
140 bell = 1;
141 continue;
142 case NBELSW:
143 bell = 0;
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 /* volatile to prevent "might be clobbered" warning from gcc: */
192 volatile int fd;
193 char tmpfil[BUFSIZ];
194 struct stat st;
195
196 fd = mkstemp (strncpy (tmpfil, "/tmp/rcvttyXXXXX", sizeof(tmpfil)));
197 unlink (tmpfil);
198
199 if ((child_id = fork()) == NOTOK) {
200 /* fork error */
201 close (fd);
202 return header_fd ();
203 } else 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 > (off_t) 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 ((pid_t) 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
257 tfile = m_mktemp2(NULL, invo_name, &fd, NULL);
258 if (tfile == NULL) return NOTOK;
259 unlink (tfile);
260
261 rewind (stdin);
262
263 /* get new format string */
264 nfs = new_fs (form, format, SCANFMT);
265 scan (stdin, 0, 0, nfs, width, 0, 0, NULL, 0L, 0);
266 scan_finished ();
267 if (newline)
268 write (fd, "\n\r", 2);
269 write (fd, scanl, strlen (scanl));
270 if (bell)
271 write (fd, "\007", 1);
272
273 return fd;
274 }
275
276
277 #if HAVE_GETUTXENT
278 static void
279 alert (char *tty, int md)
280 {
281 int i, td, mask;
282 char buffer[BUFSIZ], ttyspec[BUFSIZ];
283 struct stat st;
284
285 snprintf (ttyspec, sizeof(ttyspec), "/dev/%s", tty);
286
287 /*
288 * The mask depends on whether we are checking for
289 * write permission based on `biff' or `mesg'.
290 */
291 mask = biff ? S_IEXEC : (S_IWRITE >> 3);
292 if (stat (ttyspec, &st) == NOTOK || (st.st_mode & mask) == 0)
293 return;
294
295 if (!setjmp (myctx)) {
296 SIGNAL (SIGALRM, alrmser);
297 alarm (2);
298 td = open (ttyspec, O_WRONLY);
299 alarm (0);
300 if (td == NOTOK)
301 return;
302 } else {
303 alarm (0);
304 return;
305 }
306
307 lseek (md, (off_t) 0, SEEK_SET);
308
309 while ((i = read (md, buffer, sizeof(buffer))) > 0)
310 if (write (td, buffer, i) != i)
311 break;
312
313 close (td);
314 }
315 #endif /* HAVE_GETUTXENT */