]> diplodocus.org Git - nmh/blob - uip/rcvtty.c
Feed fileproc and mhlproc from rcvdist, send, and whatnow to post.
[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 <h/rcvmail.h>
18 #include <h/scansbr.h>
19 #include <h/tws.h>
20 #include <h/mts.h>
21 #include <signal.h>
22 #include <fcntl.h>
23
24 #include <utmp.h>
25
26 #ifndef HAVE_GETUTENT
27 # ifndef UTMP_FILE
28 # ifdef _PATH_UTMP
29 # define UTMP_FILE _PATH_UTMP
30 # else
31 # define UTMP_FILE "/etc/utmp"
32 # endif
33 # endif
34 #endif
35
36 #define SCANFMT \
37 "%2(hour{dtimenow}):%02(min{dtimenow}): %<(size)%5(size) %>%<{encrypted}E%>\
38 %<(mymbox{from})%<{to}To:%14(friendly{to})%>%>%<(zero)%17(friendly{from})%> \
39 %{subject}%<{body}<<%{body}>>%>"
40
41 static struct swit switches[] = {
42 #define BIFFSW 0
43 { "biff", 0 },
44 #define FORMSW 1
45 { "form formatfile", 0 },
46 #define FMTSW 2
47 { "format string", 5 },
48 #define WIDTHSW 3
49 { "width columns", 0 },
50 #define NLSW 4
51 { "newline", 0 },
52 #define NNLSW 5
53 { "nonewline", 0 },
54 #define BELSW 6
55 { "bell", 0 },
56 #define NBELSW 7
57 { "nobell", 0 },
58 #define VERSIONSW 8
59 { "version", 0 },
60 #define HELPSW 9
61 { "help", 0 },
62 { NULL, 0 }
63 };
64
65 static jmp_buf myctx;
66 static int bell = 1;
67 static int newline = 1;
68 static int biff = 0;
69 static int width = 0;
70 static char *form = NULL;
71 static char *format = NULL;
72
73 /*
74 * external prototypes
75 */
76 char *getusername(void);
77
78 /*
79 * static prototypes
80 */
81 static RETSIGTYPE alrmser (int);
82 static int message_fd (char **);
83 static int header_fd (void);
84 static void alert (char *, int);
85
86
87 int
88 main (int argc, char **argv)
89 {
90 int md, vecp = 0;
91 char *cp, *user, buf[BUFSIZ], tty[BUFSIZ];
92 char **argp, **arguments, *vec[MAXARGS];
93 #ifdef HAVE_GETUTENT
94 struct utmp * utp;
95 #else
96 struct utmp ut;
97 register FILE *uf;
98 #endif
99
100 #ifdef LOCALE
101 setlocale(LC_ALL, "");
102 #endif
103 invo_name = r1bindex (argv[0], '/');
104
105 /* read user profile/context */
106 context_read();
107
108 mts_init (invo_name);
109 arguments = getarguments (invo_name, argc, argv, 1);
110 argp = arguments;
111
112 while ((cp = *argp++)) {
113 if (*cp == '-') {
114 switch (smatch (++cp, switches)) {
115 case AMBIGSW:
116 ambigsw (cp, switches);
117 done (1);
118 case UNKWNSW:
119 vec[vecp++] = --cp;
120 continue;
121
122 case HELPSW:
123 snprintf (buf, sizeof(buf), "%s [command ...]", invo_name);
124 print_help (buf, switches, 1);
125 done (1);
126 case VERSIONSW:
127 print_version(invo_name);
128 done (1);
129
130 case BIFFSW:
131 biff = 1;
132 continue;
133
134 case FORMSW:
135 if (!(form = *argp++) || *form == '-')
136 adios (NULL, "missing argument to %s", argp[-2]);
137 format = NULL;
138 continue;
139 case FMTSW:
140 if (!(format = *argp++) || *format == '-')
141 adios (NULL, "missing argument to %s", argp[-2]);
142 form = NULL;
143 continue;
144
145 case WIDTHSW:
146 if (!(cp = *argp++) || *cp == '-')
147 adios(NULL, "missing argument to %s", argp[-2]);
148 width = atoi(cp);
149 continue;
150 case NLSW:
151 newline = 1;
152 continue;
153 case NNLSW:
154 newline = 0;
155 continue;
156 case BELSW:
157 bell = 1;
158 continue;
159 case NBELSW:
160 bell = 0;
161 continue;
162
163 }
164 }
165 vec[vecp++] = cp;
166 }
167 vec[vecp] = 0;
168
169 if ((md = vecp ? message_fd (vec) : header_fd ()) == NOTOK)
170 exit (RCV_MBX);
171
172 user = getusername();
173
174 #ifdef HAVE_GETUTENT
175 setutent();
176 while ((utp = getutent()) != NULL) {
177 if (
178 #ifdef HAVE_STRUCT_UTMP_UT_TYPE
179 utp->ut_type == USER_PROCESS
180 &&
181 #endif
182 utp->ut_name[0] != 0
183 && utp->ut_line[0] != 0
184 && strncmp (user, utp->ut_name, sizeof(utp->ut_name)) == 0) {
185 strncpy (tty, utp->ut_line, sizeof(utp->ut_line));
186 alert (tty, md);
187 }
188 }
189 endutent();
190 #else
191 if ((uf = fopen (UTMP_FILE, "r")) == NULL)
192 exit (RCV_MBX);
193 while (fread ((char *) &ut, sizeof(ut), 1, uf) == 1)
194 if (ut.ut_name[0] != 0
195 && strncmp (user, ut.ut_name, sizeof(ut.ut_name)) == 0) {
196 strncpy (tty, ut.ut_line, sizeof(ut.ut_line));
197 alert (tty, md);
198 }
199 fclose (uf);
200 #endif
201
202 exit (RCV_MOK);
203 return 0; /* dead code to satisfy the compiler */
204 }
205
206
207 static RETSIGTYPE
208 alrmser (int i)
209 {
210 #ifndef RELIABLE_SIGNALS
211 SIGNAL (SIGALRM, alrmser);
212 #endif
213
214 longjmp (myctx, 1);
215 }
216
217
218 static int
219 message_fd (char **vec)
220 {
221 pid_t child_id;
222 int bytes, fd, seconds;
223 char tmpfil[BUFSIZ];
224 struct stat st;
225
226 fd = mkstemp (strncpy (tmpfil, "/tmp/rcvttyXXXXX", sizeof(tmpfil)));
227 unlink (tmpfil);
228
229 if ((child_id = vfork()) == NOTOK) {
230 /* fork error */
231 close (fd);
232 return header_fd ();
233 } else if (child_id) {
234 /* parent process */
235 if (!setjmp (myctx)) {
236 SIGNAL (SIGALRM, alrmser);
237 bytes = fstat(fileno (stdin), &st) != NOTOK ? (int) st.st_size : 100;
238
239 /* amount of time to wait depends on message size */
240 if (bytes <= 100) {
241 /* give at least 5 minutes */
242 seconds = 300;
243 } else if (bytes >= 90000) {
244 /* but 30 minutes should be long enough */
245 seconds = 1800;
246 } else {
247 seconds = (bytes / 60) + 300;
248 }
249 alarm ((unsigned int) seconds);
250 pidwait(child_id, OK);
251 alarm (0);
252
253 if (fstat (fd, &st) != NOTOK && st.st_size > (off_t) 0)
254 return fd;
255 } else {
256 /*
257 * Ruthlessly kill the child and anything
258 * else in its process group.
259 */
260 killpg(child_id, SIGKILL);
261 }
262 close (fd);
263 return header_fd ();
264 }
265
266 /* child process */
267 rewind (stdin);
268 if (dup2 (fd, 1) == NOTOK || dup2 (fd, 2) == NOTOK)
269 _exit (-1);
270 closefds (3);
271 setpgid ((pid_t) 0, getpid ()); /* put in own process group */
272 execvp (vec[0], vec);
273 _exit (-1);
274 return 1; /* dead code to satisfy compiler */
275 }
276
277
278 static int
279 header_fd (void)
280 {
281 int fd;
282 char *nfs;
283 char *tfile = NULL;
284
285 tfile = m_mktemp2(NULL, invo_name, &fd, NULL);
286 if (tfile == NULL) return NOTOK;
287 unlink (tfile);
288
289 rewind (stdin);
290
291 /* get new format string */
292 nfs = new_fs (form, format, SCANFMT);
293 scan (stdin, 0, 0, nfs, width, 0, 0, NULL, 0L, 0);
294 if (newline)
295 write (fd, "\n\r", 2);
296 write (fd, scanl, strlen (scanl));
297 if (bell)
298 write (fd, "\007", 1);
299
300 return fd;
301 }
302
303
304 static void
305 alert (char *tty, int md)
306 {
307 int i, td, mask;
308 char buffer[BUFSIZ], ttyspec[BUFSIZ];
309 struct stat st;
310
311 snprintf (ttyspec, sizeof(ttyspec), "/dev/%s", tty);
312
313 /*
314 * The mask depends on whether we are checking for
315 * write permission based on `biff' or `mesg'.
316 */
317 mask = biff ? S_IEXEC : (S_IWRITE >> 3);
318 if (stat (ttyspec, &st) == NOTOK || (st.st_mode & mask) == 0)
319 return;
320
321 if (!setjmp (myctx)) {
322 SIGNAL (SIGALRM, alrmser);
323 alarm (2);
324 td = open (ttyspec, O_WRONLY);
325 alarm (0);
326 if (td == NOTOK)
327 return;
328 } else {
329 alarm (0);
330 return;
331 }
332
333 lseek (md, (off_t) 0, SEEK_SET);
334
335 while ((i = read (md, buffer, sizeof(buffer))) > 0)
336 if (write (td, buffer, i) != i)
337 break;
338
339 close (td);
340 }
341