3 * rcvtty.c -- a rcvmail program (a lot like rcvalert) handling IPC ttys
10 #include <h/rcvmail.h>
11 #include <h/scansbr.h>
12 #include <zotnet/tws/tws.h>
19 # define UTMP_FILE _PATH_UTMP
21 # define UTMP_FILE "/etc/utmp"
26 "%2(hour{dtimenow}):%02(min{dtimenow}): %<(size)%5(size) %>%<{encrypted}E%>\
27 %<(mymbox{from})%<{to}To:%14(friendly{to})%>%>%<(zero)%17(friendly{from})%> \
28 %{subject}%<{body}<<%{body}>>%>"
30 static struct swit switches[] = {
34 { "form formatfile", 0 },
36 { "format string", 5 },
38 { "width columns", 0 },
56 static int newline = 1;
59 static char *form = NULL;
60 static char *format = NULL;
65 char *getusername(void);
70 static RETSIGTYPE alrmser (int);
71 static int message_fd (char **);
72 static int header_fd (void);
73 static void alert (char *, int);
77 main (int argc, char **argv)
80 char *cp, *user, buf[BUFSIZ], tty[BUFSIZ];
81 char **argp, **arguments, *vec[MAXARGS];
86 setlocale(LC_ALL, "");
88 invo_name = r1bindex (argv[0], '/');
90 /* read user profile/context */
94 arguments = getarguments (invo_name, argc, argv, 1);
97 while ((cp = *argp++)) {
99 switch (smatch (++cp, switches)) {
101 ambigsw (cp, switches);
108 snprintf (buf, sizeof(buf), "%s [command ...]", invo_name);
109 print_help (buf, switches, 1);
112 print_version(invo_name);
120 if (!(form = *argp++) || *form == '-')
121 adios (NULL, "missing argument to %s", argp[-2]);
125 if (!(format = *argp++) || *format == '-')
126 adios (NULL, "missing argument to %s", argp[-2]);
131 if (!(cp = *argp++) || *cp == '-')
132 adios(NULL, "missing argument to %s", argp[-2]);
154 if ((md = vecp ? message_fd (vec) : header_fd ()) == NOTOK)
157 user = getusername();
158 if ((uf = fopen (UTMP_FILE, "r")) == NULL)
161 while (fread ((char *) &ut, sizeof(ut), 1, uf) == 1)
162 if (ut.ut_name[0] != 0
163 && strncmp (user, ut.ut_name, sizeof(ut.ut_name)) == 0) {
164 strncpy (tty, ut.ut_line, sizeof(ut.ut_line));
176 #ifndef RELIABLE_SIGNALS
177 SIGNAL (SIGALRM, alrmser);
185 message_fd (char **vec)
188 int bytes, fd, seconds;
193 unlink (mkstemp (strncpy (tmpfil, "/tmp/rcvttyXXXXX", sizeof(tmpfil))));
195 unlink (mktemp (strncpy (tmpfil, "/tmp/rcvttyXXXXX", sizeof(tmpfil))));
197 if ((fd = open (tmpfil, O_RDWR | O_CREAT | O_TRUNC, 0600)) == NOTOK)
201 if ((child_id = vfork()) == NOTOK) {
205 } else if (child_id) {
207 if (!setjmp (myctx)) {
208 SIGNAL (SIGALRM, alrmser);
209 bytes = fstat(fileno (stdin), &st) != NOTOK ? (int) st.st_size : 100;
211 /* amount of time to wait depends on message size */
213 /* give at least 5 minutes */
215 } else if (bytes >= 90000) {
216 /* but 30 minutes should be long enough */
219 seconds = (bytes / 60) + 300;
221 alarm ((unsigned int) seconds);
222 pidwait(child_id, OK);
225 if (fstat (fd, &st) != NOTOK && st.st_size > (off_t) 0)
229 * Ruthlessly kill the child and anything
230 * else in its process group.
232 KILLPG(child_id, SIGKILL);
240 if (dup2 (fd, 1) == NOTOK || dup2 (fd, 2) == NOTOK)
243 setpgid ((pid_t) 0, getpid ()); /* put in own process group */
244 execvp (vec[0], vec);
253 char *nfs, tmpfil[BUFSIZ];
255 strncpy (tmpfil, m_tmpfil (invo_name), sizeof(tmpfil));
256 if ((fd = open (tmpfil, O_RDWR | O_CREAT | O_TRUNC, 0600)) == NOTOK)
262 /* get new format string */
263 nfs = new_fs (form, format, SCANFMT);
264 scan (stdin, 0, 0, nfs, width, 0, 0, NULL, 0L, 0);
266 write (fd, "\n\r", 2);
267 write (fd, scanl, strlen (scanl));
269 write (fd, "\007", 1);
276 alert (char *tty, int md)
279 char buffer[BUFSIZ], ttyspec[BUFSIZ];
282 snprintf (ttyspec, sizeof(ttyspec), "/dev/%s", tty);
285 * The mask depends on whether we are checking for
286 * write permission based on `biff' or `mesg'.
288 mask = biff ? S_IEXEC : (S_IWRITE >> 3);
289 if (stat (ttyspec, &st) == NOTOK || (st.st_mode & mask) == 0)
292 if (!setjmp (myctx)) {
293 SIGNAL (SIGALRM, alrmser);
295 td = open (ttyspec, O_WRONLY);
304 lseek (md, (off_t) 0, SEEK_SET);
306 while ((i = read (md, buffer, sizeof(buffer))) > 0)
307 if (write (td, buffer, i) != i)