]> diplodocus.org Git - nmh/blob - uip/rcvtty.c
INSTALL never documented the etc/*.old thing. Documented the new etc/*.prev
[nmh] / uip / rcvtty.c
1
2 /*
3 * rcvtty.c -- a rcvmail program (a lot like rcvalert) handling IPC ttys
4 *
5 * $Id$
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 <h/rcvmail.h>
16 #include <h/scansbr.h>
17 #include <h/tws.h>
18 #include <zotnet/mts/mts.h>
19 #include <signal.h>
20 #include <fcntl.h>
21
22 #include <utmp.h>
23
24 #ifndef HAVE_GETUTENT
25 # ifndef UTMP_FILE
26 # ifdef _PATH_UTMP
27 # define UTMP_FILE _PATH_UTMP
28 # else
29 # define UTMP_FILE "/etc/utmp"
30 # endif
31 # endif
32 #endif
33
34 #define SCANFMT \
35 "%2(hour{dtimenow}):%02(min{dtimenow}): %<(size)%5(size) %>%<{encrypted}E%>\
36 %<(mymbox{from})%<{to}To:%14(friendly{to})%>%>%<(zero)%17(friendly{from})%> \
37 %{subject}%<{body}<<%{body}>>%>"
38
39 static struct swit switches[] = {
40 #define BIFFSW 0
41 { "biff", 0 },
42 #define FORMSW 1
43 { "form formatfile", 0 },
44 #define FMTSW 2
45 { "format string", 5 },
46 #define WIDTHSW 3
47 { "width columns", 0 },
48 #define NLSW 4
49 { "newline", 0 },
50 #define NNLSW 5
51 { "nonewline", 0 },
52 #define BELSW 6
53 { "bell", 0 },
54 #define NBELSW 7
55 { "nobell", 0 },
56 #define VERSIONSW 8
57 { "version", 0 },
58 #define HELPSW 9
59 { "help", 0 },
60 { NULL, 0 }
61 };
62
63 static jmp_buf myctx;
64 static int bell = 1;
65 static int newline = 1;
66 static int biff = 0;
67 static int width = 0;
68 static char *form = NULL;
69 static char *format = NULL;
70
71 /*
72 * external prototypes
73 */
74 char *getusername(void);
75
76 /*
77 * static prototypes
78 */
79 static RETSIGTYPE alrmser (int);
80 static int message_fd (char **);
81 static int header_fd (void);
82 static void alert (char *, int);
83
84
85 int
86 main (int argc, char **argv)
87 {
88 int md, vecp = 0;
89 char *cp, *user, buf[BUFSIZ], tty[BUFSIZ];
90 char **argp, **arguments, *vec[MAXARGS];
91 #ifdef HAVE_GETUTENT
92 struct utmp * utp;
93 #else
94 struct utmp ut;
95 register FILE *uf;
96 #endif
97
98 #ifdef LOCALE
99 setlocale(LC_ALL, "");
100 #endif
101 invo_name = r1bindex (argv[0], '/');
102
103 /* read user profile/context */
104 context_read();
105
106 mts_init (invo_name);
107 arguments = getarguments (invo_name, argc, argv, 1);
108 argp = arguments;
109
110 while ((cp = *argp++)) {
111 if (*cp == '-') {
112 switch (smatch (++cp, switches)) {
113 case AMBIGSW:
114 ambigsw (cp, switches);
115 done (1);
116 case UNKWNSW:
117 vec[vecp++] = --cp;
118 continue;
119
120 case HELPSW:
121 snprintf (buf, sizeof(buf), "%s [command ...]", invo_name);
122 print_help (buf, switches, 1);
123 done (1);
124 case VERSIONSW:
125 print_version(invo_name);
126 done (1);
127
128 case BIFFSW:
129 biff = 1;
130 continue;
131
132 case FORMSW:
133 if (!(form = *argp++) || *form == '-')
134 adios (NULL, "missing argument to %s", argp[-2]);
135 format = NULL;
136 continue;
137 case FMTSW:
138 if (!(format = *argp++) || *format == '-')
139 adios (NULL, "missing argument to %s", argp[-2]);
140 form = NULL;
141 continue;
142
143 case WIDTHSW:
144 if (!(cp = *argp++) || *cp == '-')
145 adios(NULL, "missing argument to %s", argp[-2]);
146 width = atoi(cp);
147 continue;
148 case NLSW:
149 newline = 1;
150 continue;
151 case NNLSW:
152 newline = 0;
153 continue;
154 case BELSW:
155 bell = 1;
156 continue;
157 case NBELSW:
158 bell = 0;
159 continue;
160
161 }
162 }
163 vec[vecp++] = cp;
164 }
165 vec[vecp] = 0;
166
167 if ((md = vecp ? message_fd (vec) : header_fd ()) == NOTOK)
168 exit (RCV_MBX);
169
170 user = getusername();
171
172 #ifdef HAVE_GETUTENT
173 setutent();
174 while ((utp = getutent()) != NULL) {
175 if (utp->ut_type == USER_PROCESS
176 && utp->ut_user[0] != 0
177 && utp->ut_line[0] != 0
178 && strncmp (user, utp->ut_user, sizeof(utp->ut_user)) == 0) {
179 strncpy (tty, utp->ut_line, sizeof(utp->ut_line));
180 alert (tty, md);
181 }
182 }
183 endutent();
184 #else
185 if ((uf = fopen (UTMP_FILE, "r")) == NULL)
186 exit (RCV_MBX);
187 while (fread ((char *) &ut, sizeof(ut), 1, uf) == 1)
188 if (ut.ut_name[0] != 0
189 && strncmp (user, ut.ut_name, sizeof(ut.ut_name)) == 0) {
190 strncpy (tty, ut.ut_line, sizeof(ut.ut_line));
191 alert (tty, md);
192 }
193 fclose (uf);
194 #endif
195
196 exit (RCV_MOK);
197 return 0; /* dead code to satisfy the compiler */
198 }
199
200
201 static RETSIGTYPE
202 alrmser (int i)
203 {
204 #ifndef RELIABLE_SIGNALS
205 SIGNAL (SIGALRM, alrmser);
206 #endif
207
208 longjmp (myctx, 1);
209 }
210
211
212 static int
213 message_fd (char **vec)
214 {
215 pid_t child_id;
216 int bytes, fd, seconds;
217 char tmpfil[BUFSIZ];
218 struct stat st;
219
220 #ifdef HAVE_MKSTEMP
221 fd = mkstemp (strncpy (tmpfil, "/tmp/rcvttyXXXXX", sizeof(tmpfil)));
222 #else
223 unlink (mktemp (strncpy (tmpfil, "/tmp/rcvttyXXXXX", sizeof(tmpfil))));
224 if ((fd = open (tmpfil, O_RDWR | O_CREAT | O_TRUNC, 0600)) == NOTOK)
225 return header_fd ();
226 #endif
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, tmpfil[BUFSIZ];
283
284 strncpy (tmpfil, m_tmpfil (invo_name), sizeof(tmpfil));
285 if ((fd = open (tmpfil, O_RDWR | O_CREAT | O_TRUNC, 0600)) == NOTOK)
286 return NOTOK;
287 unlink (tmpfil);
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