]> diplodocus.org Git - nmh/blob - uip/rcvtty.c
Removed temporary probes added in commit
[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 #ifdef LOCALE
85 setlocale(LC_ALL, "");
86 #endif
87 invo_name = r1bindex (argv[0], '/');
88
89 /* read user profile/context */
90 context_read();
91
92 mts_init (invo_name);
93 arguments = getarguments (invo_name, argc, argv, 1);
94 argp = arguments;
95
96 while ((cp = *argp++)) {
97 if (*cp == '-') {
98 switch (smatch (++cp, switches)) {
99 case AMBIGSW:
100 ambigsw (cp, switches);
101 done (1);
102 case UNKWNSW:
103 vec[vecp++] = --cp;
104 continue;
105
106 case HELPSW:
107 snprintf (buf, sizeof(buf), "%s [command ...]", invo_name);
108 print_help (buf, switches, 1);
109 done (0);
110 case VERSIONSW:
111 print_version(invo_name);
112 done (0);
113
114 case BIFFSW:
115 biff = 1;
116 continue;
117
118 case FORMSW:
119 if (!(form = *argp++) || *form == '-')
120 adios (NULL, "missing argument to %s", argp[-2]);
121 format = NULL;
122 continue;
123 case FMTSW:
124 if (!(format = *argp++) || *format == '-')
125 adios (NULL, "missing argument to %s", argp[-2]);
126 form = NULL;
127 continue;
128
129 case WIDTHSW:
130 if (!(cp = *argp++) || *cp == '-')
131 adios(NULL, "missing argument to %s", argp[-2]);
132 width = atoi(cp);
133 continue;
134 case NLSW:
135 newline = 1;
136 continue;
137 case NNLSW:
138 newline = 0;
139 continue;
140 case BELSW:
141 bell = 1;
142 continue;
143 case NBELSW:
144 bell = 0;
145 continue;
146
147 }
148 }
149 vec[vecp++] = cp;
150 }
151 vec[vecp] = 0;
152
153 if ((md = vecp ? message_fd (vec) : header_fd ()) == NOTOK)
154 exit (RCV_MBX);
155
156 user = getusername();
157
158 #if HAVE_GETUTXENT
159 setutxent();
160 while ((utp = getutxent()) != NULL) {
161 if (utp->ut_type == USER_PROCESS && utp->ut_user[0] != 0
162 && utp->ut_line[0] != 0
163 && strncmp (user, utp->ut_user, sizeof(utp->ut_user)) == 0) {
164 strncpy (tty, utp->ut_line, sizeof(utp->ut_line));
165 alert (tty, md);
166 }
167 }
168 endutxent();
169 #else
170 NMH_UNUSED (tty);
171 NMH_UNUSED (utp);
172 #endif /* HAVE_GETUTXENT */
173
174 exit (RCV_MOK);
175 }
176
177
178 static void
179 alrmser (int i)
180 {
181 NMH_UNUSED (i);
182
183 longjmp (myctx, 1);
184 }
185
186
187 static int
188 message_fd (char **vec)
189 {
190 pid_t child_id;
191 int bytes, seconds;
192 /* volatile to prevent "might be clobbered" warning from gcc: */
193 volatile int fd;
194 char tmpfil[BUFSIZ];
195 struct stat st;
196
197 fd = mkstemp (strncpy (tmpfil, "/tmp/rcvttyXXXXX", sizeof(tmpfil)));
198 unlink (tmpfil);
199
200 if ((child_id = fork()) == NOTOK) {
201 /* fork error */
202 close (fd);
203 return header_fd ();
204 } else if (child_id) {
205 /* parent process */
206 if (!setjmp (myctx)) {
207 SIGNAL (SIGALRM, alrmser);
208 bytes = fstat(fileno (stdin), &st) != NOTOK ? (int) st.st_size : 100;
209
210 /* amount of time to wait depends on message size */
211 if (bytes <= 100) {
212 /* give at least 5 minutes */
213 seconds = 300;
214 } else if (bytes >= 90000) {
215 /* but 30 minutes should be long enough */
216 seconds = 1800;
217 } else {
218 seconds = (bytes / 60) + 300;
219 }
220 alarm ((unsigned int) seconds);
221 pidwait(child_id, OK);
222 alarm (0);
223
224 if (fstat (fd, &st) != NOTOK && st.st_size > (off_t) 0)
225 return fd;
226 } else {
227 /*
228 * Ruthlessly kill the child and anything
229 * else in its process group.
230 */
231 killpg(child_id, SIGKILL);
232 }
233 close (fd);
234 return header_fd ();
235 }
236
237 /* child process */
238 rewind (stdin);
239 if (dup2 (fd, 1) == NOTOK || dup2 (fd, 2) == NOTOK)
240 _exit (-1);
241 closefds (3);
242 setpgid ((pid_t) 0, getpid ()); /* put in own process group */
243 if (execvp (vec[0], vec) == NOTOK) {
244 _exit (-1);
245 }
246
247 return NOTOK;
248 }
249
250
251 static int
252 header_fd (void)
253 {
254 int fd;
255 char *nfs;
256 char *tfile = NULL;
257
258 tfile = m_mktemp2(NULL, invo_name, &fd, NULL);
259 if (tfile == NULL) return NOTOK;
260 unlink (tfile);
261
262 rewind (stdin);
263
264 /* get new format string */
265 nfs = new_fs (form, format, SCANFMT);
266 scan (stdin, 0, 0, nfs, width, 0, 0, NULL, 0L, 0);
267 scan_finished ();
268 if (newline)
269 write (fd, "\n\r", 2);
270 write (fd, scanl, strlen (scanl));
271 if (bell)
272 write (fd, "\007", 1);
273
274 return fd;
275 }
276
277
278 #if HAVE_GETUTXENT
279 static void
280 alert (char *tty, int md)
281 {
282 int i, td, mask;
283 char buffer[BUFSIZ], ttyspec[BUFSIZ];
284 struct stat st;
285
286 snprintf (ttyspec, sizeof(ttyspec), "/dev/%s", tty);
287
288 /*
289 * The mask depends on whether we are checking for
290 * write permission based on `biff' or `mesg'.
291 */
292 mask = biff ? S_IEXEC : (S_IWRITE >> 3);
293 if (stat (ttyspec, &st) == NOTOK || (st.st_mode & mask) == 0)
294 return;
295
296 if (!setjmp (myctx)) {
297 SIGNAL (SIGALRM, alrmser);
298 alarm (2);
299 td = open (ttyspec, O_WRONLY);
300 alarm (0);
301 if (td == NOTOK)
302 return;
303 } else {
304 alarm (0);
305 return;
306 }
307
308 lseek (md, (off_t) 0, SEEK_SET);
309
310 while ((i = read (md, buffer, sizeof(buffer))) > 0)
311 if (write (td, buffer, i) != i)
312 break;
313
314 close (td);
315 }
316 #endif /* HAVE_GETUTXENT */