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/rcvmail.h>
18 #include <h/scansbr.h>
29 # define UTMP_FILE _PATH_UTMP
31 # define UTMP_FILE "/etc/utmp"
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}>>%>"
41 static struct swit switches[] = {
45 { "form formatfile", 0 },
47 { "format string", 5 },
49 { "width columns", 0 },
67 static int newline = 1;
70 static char *form = NULL;
71 static char *format = NULL;
76 char *getusername(void);
81 static RETSIGTYPE alrmser (int);
82 static int message_fd (char **);
83 static int header_fd (void);
84 static void alert (char *, int);
88 main (int argc, char **argv)
91 char *cp, *user, buf[BUFSIZ], tty[BUFSIZ];
92 char **argp, **arguments, *vec[MAXARGS];
101 setlocale(LC_ALL, "");
103 invo_name = r1bindex (argv[0], '/');
105 /* read user profile/context */
108 mts_init (invo_name);
109 arguments = getarguments (invo_name, argc, argv, 1);
112 while ((cp = *argp++)) {
114 switch (smatch (++cp, switches)) {
116 ambigsw (cp, switches);
123 snprintf (buf, sizeof(buf), "%s [command ...]", invo_name);
124 print_help (buf, switches, 1);
127 print_version(invo_name);
135 if (!(form = *argp++) || *form == '-')
136 adios (NULL, "missing argument to %s", argp[-2]);
140 if (!(format = *argp++) || *format == '-')
141 adios (NULL, "missing argument to %s", argp[-2]);
146 if (!(cp = *argp++) || *cp == '-')
147 adios(NULL, "missing argument to %s", argp[-2]);
169 if ((md = vecp ? message_fd (vec) : header_fd ()) == NOTOK)
172 user = getusername();
176 while ((utp = getutent()) != NULL) {
178 #ifdef HAVE_STRUCT_UTMP_UT_TYPE
179 utp->ut_type == USER_PROCESS
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));
191 if ((uf = fopen (UTMP_FILE, "r")) == NULL)
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));
203 return 0; /* dead code to satisfy the compiler */
210 #ifndef RELIABLE_SIGNALS
211 SIGNAL (SIGALRM, alrmser);
219 message_fd (char **vec)
222 int bytes, fd, seconds;
227 fd = mkstemp (strncpy (tmpfil, "/tmp/rcvttyXXXXX", sizeof(tmpfil)));
229 unlink (mktemp (strncpy (tmpfil, "/tmp/rcvttyXXXXX", sizeof(tmpfil))));
230 if ((fd = open (tmpfil, O_RDWR | O_CREAT | O_TRUNC, 0600)) == NOTOK)
235 if ((child_id = vfork()) == NOTOK) {
239 } else if (child_id) {
241 if (!setjmp (myctx)) {
242 SIGNAL (SIGALRM, alrmser);
243 bytes = fstat(fileno (stdin), &st) != NOTOK ? (int) st.st_size : 100;
245 /* amount of time to wait depends on message size */
247 /* give at least 5 minutes */
249 } else if (bytes >= 90000) {
250 /* but 30 minutes should be long enough */
253 seconds = (bytes / 60) + 300;
255 alarm ((unsigned int) seconds);
256 pidwait(child_id, OK);
259 if (fstat (fd, &st) != NOTOK && st.st_size > (off_t) 0)
263 * Ruthlessly kill the child and anything
264 * else in its process group.
266 KILLPG(child_id, SIGKILL);
274 if (dup2 (fd, 1) == NOTOK || dup2 (fd, 2) == NOTOK)
277 setpgid ((pid_t) 0, getpid ()); /* put in own process group */
278 execvp (vec[0], vec);
280 return 1; /* dead code to satisfy compiler */
291 tfile = m_mktemp2(NULL, invo_name, &fd, NULL);
292 if (tfile == NULL) return NOTOK;
297 /* get new format string */
298 nfs = new_fs (form, format, SCANFMT);
299 scan (stdin, 0, 0, nfs, width, 0, 0, NULL, 0L, 0);
301 write (fd, "\n\r", 2);
302 write (fd, scanl, strlen (scanl));
304 write (fd, "\007", 1);
311 alert (char *tty, int md)
314 char buffer[BUFSIZ], ttyspec[BUFSIZ];
317 snprintf (ttyspec, sizeof(ttyspec), "/dev/%s", tty);
320 * The mask depends on whether we are checking for
321 * write permission based on `biff' or `mesg'.
323 mask = biff ? S_IEXEC : (S_IWRITE >> 3);
324 if (stat (ttyspec, &st) == NOTOK || (st.st_mode & mask) == 0)
327 if (!setjmp (myctx)) {
328 SIGNAL (SIGALRM, alrmser);
330 td = open (ttyspec, O_WRONLY);
339 lseek (md, (off_t) 0, SEEK_SET);
341 while ((i = read (md, buffer, sizeof(buffer))) > 0)
342 if (write (td, buffer, i) != i)