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>
13 #include <zotnet/mts/mts.h>
20 # define UTMP_FILE _PATH_UTMP
22 # define UTMP_FILE "/etc/utmp"
27 "%2(hour{dtimenow}):%02(min{dtimenow}): %<(size)%5(size) %>%<{encrypted}E%>\
28 %<(mymbox{from})%<{to}To:%14(friendly{to})%>%>%<(zero)%17(friendly{from})%> \
29 %{subject}%<{body}<<%{body}>>%>"
31 static struct swit switches[] = {
35 { "form formatfile", 0 },
37 { "format string", 5 },
39 { "width columns", 0 },
57 static int newline = 1;
60 static char *form = NULL;
61 static char *format = NULL;
66 char *getusername(void);
71 static RETSIGTYPE alrmser (int);
72 static int message_fd (char **);
73 static int header_fd (void);
74 static void alert (char *, int);
78 main (int argc, char **argv)
81 char *cp, *user, buf[BUFSIZ], tty[BUFSIZ];
82 char **argp, **arguments, *vec[MAXARGS];
87 setlocale(LC_ALL, "");
89 invo_name = r1bindex (argv[0], '/');
91 /* read user profile/context */
95 arguments = getarguments (invo_name, argc, argv, 1);
98 while ((cp = *argp++)) {
100 switch (smatch (++cp, switches)) {
102 ambigsw (cp, switches);
109 snprintf (buf, sizeof(buf), "%s [command ...]", invo_name);
110 print_help (buf, switches, 1);
113 print_version(invo_name);
121 if (!(form = *argp++) || *form == '-')
122 adios (NULL, "missing argument to %s", argp[-2]);
126 if (!(format = *argp++) || *format == '-')
127 adios (NULL, "missing argument to %s", argp[-2]);
132 if (!(cp = *argp++) || *cp == '-')
133 adios(NULL, "missing argument to %s", argp[-2]);
155 if ((md = vecp ? message_fd (vec) : header_fd ()) == NOTOK)
158 user = getusername();
159 if ((uf = fopen (UTMP_FILE, "r")) == NULL)
162 while (fread ((char *) &ut, sizeof(ut), 1, uf) == 1)
163 if (ut.ut_name[0] != 0
164 && strncmp (user, ut.ut_name, sizeof(ut.ut_name)) == 0) {
165 strncpy (tty, ut.ut_line, sizeof(ut.ut_line));
171 return 0; /* dead code to satisfy the compiler */
178 #ifndef RELIABLE_SIGNALS
179 SIGNAL (SIGALRM, alrmser);
187 message_fd (char **vec)
190 int bytes, fd, seconds;
195 fd = mkstemp (strncpy (tmpfil, "/tmp/rcvttyXXXXX", sizeof(tmpfil)));
197 unlink (mktemp (strncpy (tmpfil, "/tmp/rcvttyXXXXX", sizeof(tmpfil))));
198 if ((fd = open (tmpfil, O_RDWR | O_CREAT | O_TRUNC, 0600)) == NOTOK)
203 if ((child_id = vfork()) == NOTOK) {
207 } else if (child_id) {
209 if (!setjmp (myctx)) {
210 SIGNAL (SIGALRM, alrmser);
211 bytes = fstat(fileno (stdin), &st) != NOTOK ? (int) st.st_size : 100;
213 /* amount of time to wait depends on message size */
215 /* give at least 5 minutes */
217 } else if (bytes >= 90000) {
218 /* but 30 minutes should be long enough */
221 seconds = (bytes / 60) + 300;
223 alarm ((unsigned int) seconds);
224 pidwait(child_id, OK);
227 if (fstat (fd, &st) != NOTOK && st.st_size > (off_t) 0)
231 * Ruthlessly kill the child and anything
232 * else in its process group.
234 KILLPG(child_id, SIGKILL);
242 if (dup2 (fd, 1) == NOTOK || dup2 (fd, 2) == NOTOK)
245 setpgid ((pid_t) 0, getpid ()); /* put in own process group */
246 execvp (vec[0], vec);
248 return 1; /* dead code to satisfy compiler */
256 char *nfs, tmpfil[BUFSIZ];
258 strncpy (tmpfil, m_tmpfil (invo_name), sizeof(tmpfil));
259 if ((fd = open (tmpfil, O_RDWR | O_CREAT | O_TRUNC, 0600)) == NOTOK)
265 /* get new format string */
266 nfs = new_fs (form, format, SCANFMT);
267 scan (stdin, 0, 0, nfs, width, 0, 0, NULL, 0L, 0);
269 write (fd, "\n\r", 2);
270 write (fd, scanl, strlen (scanl));
272 write (fd, "\007", 1);
279 alert (char *tty, int md)
282 char buffer[BUFSIZ], ttyspec[BUFSIZ];
285 snprintf (ttyspec, sizeof(ttyspec), "/dev/%s", tty);
288 * The mask depends on whether we are checking for
289 * write permission based on `biff' or `mesg'.
291 mask = biff ? S_IEXEC : (S_IWRITE >> 3);
292 if (stat (ttyspec, &st) == NOTOK || (st.st_mode & mask) == 0)
295 if (!setjmp (myctx)) {
296 SIGNAL (SIGALRM, alrmser);
298 td = open (ttyspec, O_WRONLY);
307 lseek (md, (off_t) 0, SEEK_SET);
309 while ((i = read (md, buffer, sizeof(buffer))) > 0)
310 if (write (td, buffer, i) != i)