3 * rcvtty.c -- a rcvmail program (a lot like rcvalert) handling IPC ttys
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.
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
16 #include <h/signals.h>
17 #include <h/m_setjmp.h>
18 #include <h/rcvmail.h>
19 #include <h/scansbr.h>
30 # define UTMP_FILE _PATH_UTMP
32 # define UTMP_FILE "/etc/utmp"
38 "%2(hour{dtimenow}):%02(min{dtimenow}): %<(size)%5(size) %>%<{encrypted}E%>\
39 %<(mymbox{from})%<{to}To:%14(friendly{to})%>%>%<(zero)%17(friendly{from})%> \
40 %{subject}%<{body}<<%{body}>>%>"
42 static struct swit switches[] = {
46 { "form formatfile", 0 },
48 { "format string", 5 },
50 { "width columns", 0 },
68 static int newline = 1;
71 static char *form = NULL;
72 static char *format = NULL;
77 char *getusername(void);
82 static void alrmser (int);
83 static int message_fd (char **);
84 static int header_fd (void);
85 static void alert (char *, int);
89 main (int argc, char **argv)
92 char *cp, *user, buf[BUFSIZ], tty[BUFSIZ];
93 char **argp, **arguments, *vec[MAXARGS];
102 setlocale(LC_ALL, "");
104 invo_name = r1bindex (argv[0], '/');
106 /* read user profile/context */
109 mts_init (invo_name);
110 arguments = getarguments (invo_name, argc, argv, 1);
113 while ((cp = *argp++)) {
115 switch (smatch (++cp, switches)) {
117 ambigsw (cp, switches);
124 snprintf (buf, sizeof(buf), "%s [command ...]", invo_name);
125 print_help (buf, switches, 1);
128 print_version(invo_name);
136 if (!(form = *argp++) || *form == '-')
137 adios (NULL, "missing argument to %s", argp[-2]);
141 if (!(format = *argp++) || *format == '-')
142 adios (NULL, "missing argument to %s", argp[-2]);
147 if (!(cp = *argp++) || *cp == '-')
148 adios(NULL, "missing argument to %s", argp[-2]);
170 if ((md = vecp ? message_fd (vec) : header_fd ()) == NOTOK)
173 user = getusername();
177 while ((utp = getutent()) != NULL) {
179 #ifdef HAVE_STRUCT_UTMP_UT_TYPE
180 utp->ut_type == USER_PROCESS
184 && utp->ut_line[0] != 0
185 && strncmp (user, utp->ut_name, sizeof(utp->ut_name)) == 0) {
186 strncpy (tty, utp->ut_line, sizeof(utp->ut_line));
192 if ((uf = fopen (UTMP_FILE, "r")) == NULL)
194 while (fread ((char *) &ut, sizeof(ut), 1, uf) == 1)
195 if (ut.ut_name[0] != 0
196 && strncmp (user, ut.ut_name, sizeof(ut.ut_name)) == 0) {
197 strncpy (tty, ut.ut_line, sizeof(ut.ut_line));
204 return 0; /* dead code to satisfy the compiler */
218 message_fd (char **vec)
221 int bytes, fd, seconds;
225 fd = mkstemp (strncpy (tmpfil, "/tmp/rcvttyXXXXX", sizeof(tmpfil)));
228 if ((child_id = m_vfork()) == NOTOK) {
232 } else if (child_id) {
234 if (!m_setjmp (myctx)) {
235 SIGNAL (SIGALRM, alrmser);
236 bytes = fstat(fileno (stdin), &st) != NOTOK ? (int) st.st_size : 100;
238 /* amount of time to wait depends on message size */
240 /* give at least 5 minutes */
242 } else if (bytes >= 90000) {
243 /* but 30 minutes should be long enough */
246 seconds = (bytes / 60) + 300;
248 alarm ((unsigned int) seconds);
249 pidwait(child_id, OK);
252 if (fstat (fd, &st) != NOTOK && st.st_size > (off_t) 0)
256 * Ruthlessly kill the child and anything
257 * else in its process group.
259 killpg(child_id, SIGKILL);
267 if (dup2 (fd, 1) == NOTOK || dup2 (fd, 2) == NOTOK)
270 setpgid ((pid_t) 0, getpid ()); /* put in own process group */
271 execvp (vec[0], vec);
273 return 1; /* dead code to satisfy compiler */
284 tfile = m_mktemp2(NULL, invo_name, &fd, NULL);
285 if (tfile == NULL) return NOTOK;
290 /* get new format string */
291 nfs = new_fs (form, format, SCANFMT);
292 scan (stdin, 0, 0, nfs, width, 0, 0, NULL, 0L, 0);
294 write (fd, "\n\r", 2);
295 write (fd, scanl, strlen (scanl));
297 write (fd, "\007", 1);
304 alert (char *tty, int md)
307 char buffer[BUFSIZ], ttyspec[BUFSIZ];
310 snprintf (ttyspec, sizeof(ttyspec), "/dev/%s", tty);
313 * The mask depends on whether we are checking for
314 * write permission based on `biff' or `mesg'.
316 mask = biff ? S_IEXEC : (S_IWRITE >> 3);
317 if (stat (ttyspec, &st) == NOTOK || (st.st_mode & mask) == 0)
320 if (!m_setjmp (myctx)) {
321 SIGNAL (SIGALRM, alrmser);
323 td = open (ttyspec, O_WRONLY);
332 lseek (md, (off_t) 0, SEEK_SET);
334 while ((i = read (md, buffer, sizeof(buffer))) > 0)
335 if (write (td, buffer, i) != i)