3 * rcvtty.c -- a rcvmail program (a lot like rcvalert) handling IPC ttys
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
12 /* Changed to use getutent() and friends. Assumes that when getutent() exists,
13 * a number of other things also exist. Please check.
14 * Ruud de Rooij <ruud@ruud.org> Sun, 28 May 2000 17:28:55 +0200
18 #include <h/signals.h>
19 #include <h/rcvmail.h>
20 #include <h/scansbr.h>
31 # define UTMP_FILE _PATH_UTMP
33 # define UTMP_FILE "/etc/utmp"
39 "%2(hour{dtimenow}):%02(min{dtimenow}): %<(size)%5(size) %>%<{encrypted}E%>\
40 %<(mymbox{from})%<{to}To:%14(friendly{to})%>%>%<(zero)%17(friendly{from})%> \
41 %{subject}%<{body}<<%{body}>>%>"
43 static struct swit switches[] = {
47 { "form formatfile", 0 },
49 { "format string", 5 },
51 { "width columns", 0 },
69 static int newline = 1;
72 static char *form = NULL;
73 static char *format = NULL;
78 char *getusername(void);
83 static RETSIGTYPE alrmser (int);
84 static int message_fd (char **);
85 static int header_fd (void);
86 static void alert (char *, int);
90 main (int argc, char **argv)
93 char *cp, *user, buf[BUFSIZ], tty[BUFSIZ];
94 char **argp, **arguments, *vec[MAXARGS];
103 setlocale(LC_ALL, "");
105 invo_name = r1bindex (argv[0], '/');
107 /* read user profile/context */
110 mts_init (invo_name);
111 arguments = getarguments (invo_name, argc, argv, 1);
114 while ((cp = *argp++)) {
116 switch (smatch (++cp, switches)) {
118 ambigsw (cp, switches);
125 snprintf (buf, sizeof(buf), "%s [command ...]", invo_name);
126 print_help (buf, switches, 1);
129 print_version(invo_name);
137 if (!(form = *argp++) || *form == '-')
138 adios (NULL, "missing argument to %s", argp[-2]);
142 if (!(format = *argp++) || *format == '-')
143 adios (NULL, "missing argument to %s", argp[-2]);
148 if (!(cp = *argp++) || *cp == '-')
149 adios(NULL, "missing argument to %s", argp[-2]);
171 if ((md = vecp ? message_fd (vec) : header_fd ()) == NOTOK)
174 user = getusername();
178 while ((utp = getutent()) != NULL) {
179 if (utp->ut_type == USER_PROCESS
180 && utp->ut_user[0] != 0
181 && utp->ut_line[0] != 0
182 && strncmp (user, utp->ut_user, sizeof(utp->ut_user)) == 0) {
183 strncpy (tty, utp->ut_line, sizeof(utp->ut_line));
189 if ((uf = fopen (UTMP_FILE, "r")) == NULL)
191 while (fread ((char *) &ut, sizeof(ut), 1, uf) == 1)
192 if (ut.ut_name[0] != 0
193 && strncmp (user, ut.ut_name, sizeof(ut.ut_name)) == 0) {
194 strncpy (tty, ut.ut_line, sizeof(ut.ut_line));
201 return 0; /* dead code to satisfy the compiler */
208 #ifndef RELIABLE_SIGNALS
209 SIGNAL (SIGALRM, alrmser);
217 message_fd (char **vec)
220 int bytes, fd, seconds;
225 fd = mkstemp (strncpy (tmpfil, "/tmp/rcvttyXXXXX", sizeof(tmpfil)));
227 unlink (mktemp (strncpy (tmpfil, "/tmp/rcvttyXXXXX", sizeof(tmpfil))));
228 if ((fd = open (tmpfil, O_RDWR | O_CREAT | O_TRUNC, 0600)) == NOTOK)
233 if ((child_id = vfork()) == NOTOK) {
237 } else if (child_id) {
239 if (!setjmp (myctx)) {
240 SIGNAL (SIGALRM, alrmser);
241 bytes = fstat(fileno (stdin), &st) != NOTOK ? (int) st.st_size : 100;
243 /* amount of time to wait depends on message size */
245 /* give at least 5 minutes */
247 } else if (bytes >= 90000) {
248 /* but 30 minutes should be long enough */
251 seconds = (bytes / 60) + 300;
253 alarm ((unsigned int) seconds);
254 pidwait(child_id, OK);
257 if (fstat (fd, &st) != NOTOK && st.st_size > (off_t) 0)
261 * Ruthlessly kill the child and anything
262 * else in its process group.
264 KILLPG(child_id, SIGKILL);
272 if (dup2 (fd, 1) == NOTOK || dup2 (fd, 2) == NOTOK)
275 setpgid ((pid_t) 0, getpid ()); /* put in own process group */
276 execvp (vec[0], vec);
278 return 1; /* dead code to satisfy compiler */
286 char *nfs, tmpfil[BUFSIZ];
288 strncpy (tmpfil, m_tmpfil (invo_name), sizeof(tmpfil));
289 if ((fd = open (tmpfil, O_RDWR | O_CREAT | O_TRUNC, 0600)) == NOTOK)
295 /* get new format string */
296 nfs = new_fs (form, format, SCANFMT);
297 scan (stdin, 0, 0, nfs, width, 0, 0, NULL, 0L, 0);
299 write (fd, "\n\r", 2);
300 write (fd, scanl, strlen (scanl));
302 write (fd, "\007", 1);
309 alert (char *tty, int md)
312 char buffer[BUFSIZ], ttyspec[BUFSIZ];
315 snprintf (ttyspec, sizeof(ttyspec), "/dev/%s", tty);
318 * The mask depends on whether we are checking for
319 * write permission based on `biff' or `mesg'.
321 mask = biff ? S_IEXEC : (S_IWRITE >> 3);
322 if (stat (ttyspec, &st) == NOTOK || (st.st_mode & mask) == 0)
325 if (!setjmp (myctx)) {
326 SIGNAL (SIGALRM, alrmser);
328 td = open (ttyspec, O_WRONLY);
337 lseek (md, (off_t) 0, SEEK_SET);
339 while ((i = read (md, buffer, sizeof(buffer))) > 0)
340 if (write (td, buffer, i) != i)