2 * rcvtty.c -- a rcvmail program (a lot like rcvalert) handling IPC ttys
4 * This code is Copyright (c) 2002, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
9 /* Changed to use getutent() and friends. Assumes that when getutent() exists,
10 * a number of other things also exist. Please check.
11 * Ruud de Rooij <ruud@ruud.org> Sun, 28 May 2000 17:28:55 +0200
15 #include <h/signals.h>
16 #include <h/rcvmail.h>
17 #include <h/scansbr.h>
28 # define UTMP_FILE _PATH_UTMP
30 # define UTMP_FILE "/etc/utmp"
36 "%2(hour{dtimenow}):%02(min{dtimenow}): %<(size)%5(size) %>%<{encrypted}E%>\
37 %<(mymbox{from})%<{to}To:%14(friendly{to})%>%>%<(zero)%17(friendly{from})%> \
38 %{subject}%<{body}<<%{body}>>%>"
40 static struct swit switches[] = {
44 { "form formatfile", 0 },
46 { "format string", 5 },
48 { "width columns", 0 },
66 static int newline = 1;
69 static char *form = NULL;
70 static char *format = NULL;
75 char *getusername(void);
80 static RETSIGTYPE alrmser (int);
81 static int message_fd (char **);
82 static int header_fd (void);
83 static void alert (char *, int);
87 main (int argc, char **argv)
90 char *cp, *user, buf[BUFSIZ], tty[BUFSIZ];
91 char **argp, **arguments, *vec[MAXARGS];
100 setlocale(LC_ALL, "");
102 invo_name = r1bindex (argv[0], '/');
104 /* read user profile/context */
107 mts_init (invo_name);
108 arguments = getarguments (invo_name, argc, argv, 1);
111 while ((cp = *argp++)) {
113 switch (smatch (++cp, switches)) {
115 ambigsw (cp, switches);
122 snprintf (buf, sizeof(buf), "%s [command ...]", invo_name);
123 print_help (buf, switches, 1);
126 print_version(invo_name);
134 if (!(form = *argp++) || *form == '-')
135 adios (NULL, "missing argument to %s", argp[-2]);
139 if (!(format = *argp++) || *format == '-')
140 adios (NULL, "missing argument to %s", argp[-2]);
145 if (!(cp = *argp++) || *cp == '-')
146 adios(NULL, "missing argument to %s", argp[-2]);
168 if ((md = vecp ? message_fd (vec) : header_fd ()) == NOTOK)
171 user = getusername();
175 while ((utp = getutent()) != NULL) {
177 #ifdef HAVE_STRUCT_UTMP_UT_TYPE
178 utp->ut_type == USER_PROCESS
182 && utp->ut_line[0] != 0
183 && strncmp (user, utp->ut_name, sizeof(utp->ut_name)) == 0) {
184 strncpy (tty, utp->ut_line, sizeof(utp->ut_line));
190 if ((uf = fopen (UTMP_FILE, "r")) == NULL)
192 while (fread ((char *) &ut, sizeof(ut), 1, uf) == 1)
193 if (ut.ut_name[0] != 0
194 && strncmp (user, ut.ut_name, sizeof(ut.ut_name)) == 0) {
195 strncpy (tty, ut.ut_line, sizeof(ut.ut_line));
202 return 0; /* dead code to satisfy the compiler */
209 #ifndef RELIABLE_SIGNALS
210 SIGNAL (SIGALRM, alrmser);
218 message_fd (char **vec)
221 int bytes, fd, seconds;
226 fd = mkstemp (strncpy (tmpfil, "/tmp/rcvttyXXXXX", sizeof(tmpfil)));
228 unlink (mktemp (strncpy (tmpfil, "/tmp/rcvttyXXXXX", sizeof(tmpfil))));
229 if ((fd = open (tmpfil, O_RDWR | O_CREAT | O_TRUNC, 0600)) == NOTOK)
234 if ((child_id = vfork()) == NOTOK) {
238 } else if (child_id) {
240 if (!setjmp (myctx)) {
241 SIGNAL (SIGALRM, alrmser);
242 bytes = fstat(fileno (stdin), &st) != NOTOK ? (int) st.st_size : 100;
244 /* amount of time to wait depends on message size */
246 /* give at least 5 minutes */
248 } else if (bytes >= 90000) {
249 /* but 30 minutes should be long enough */
252 seconds = (bytes / 60) + 300;
254 alarm ((unsigned int) seconds);
255 pidwait(child_id, OK);
258 if (fstat (fd, &st) != NOTOK && st.st_size > (off_t) 0)
262 * Ruthlessly kill the child and anything
263 * else in its process group.
265 KILLPG(child_id, SIGKILL);
273 if (dup2 (fd, 1) == NOTOK || dup2 (fd, 2) == NOTOK)
276 setpgid ((pid_t) 0, getpid ()); /* put in own process group */
277 execvp (vec[0], vec);
279 return 1; /* dead code to satisfy compiler */
290 tfile = m_mktemp2(NULL, invo_name, &fd, NULL);
291 if (tfile == NULL) return NOTOK;
296 /* get new format string */
297 nfs = new_fs (form, format, SCANFMT);
298 scan (stdin, 0, 0, nfs, width, 0, 0, NULL, 0L, 0);
300 write (fd, "\n\r", 2);
301 write (fd, scanl, strlen (scanl));
303 write (fd, "\007", 1);
310 alert (char *tty, int md)
313 char buffer[BUFSIZ], ttyspec[BUFSIZ];
316 snprintf (ttyspec, sizeof(ttyspec), "/dev/%s", tty);
319 * The mask depends on whether we are checking for
320 * write permission based on `biff' or `mesg'.
322 mask = biff ? S_IEXEC : (S_IWRITE >> 3);
323 if (stat (ttyspec, &st) == NOTOK || (st.st_mode & mask) == 0)
326 if (!setjmp (myctx)) {
327 SIGNAL (SIGALRM, alrmser);
329 td = open (ttyspec, O_WRONLY);
338 lseek (md, (off_t) 0, SEEK_SET);
340 while ((i = read (md, buffer, sizeof(buffer))) > 0)
341 if (write (td, buffer, i) != i)