3 * rcvtty.c -- a rcvmail program (a lot like rcvalert) handling IPC ttys
8 /* Changed to use getutent() and friends. Assumes that when getutent() exists,
9 * a number of other things also exist. Please check.
10 * Ruud de Rooij <ruud@ruud.org> Sun, 28 May 2000 17:28:55 +0200
14 #include <h/signals.h>
15 #include <h/rcvmail.h>
16 #include <h/scansbr.h>
27 # define UTMP_FILE _PATH_UTMP
29 # define UTMP_FILE "/etc/utmp"
35 "%2(hour{dtimenow}):%02(min{dtimenow}): %<(size)%5(size) %>%<{encrypted}E%>\
36 %<(mymbox{from})%<{to}To:%14(friendly{to})%>%>%<(zero)%17(friendly{from})%> \
37 %{subject}%<{body}<<%{body}>>%>"
39 static struct swit switches[] = {
43 { "form formatfile", 0 },
45 { "format string", 5 },
47 { "width columns", 0 },
65 static int newline = 1;
68 static char *form = NULL;
69 static char *format = NULL;
74 char *getusername(void);
79 static RETSIGTYPE alrmser (int);
80 static int message_fd (char **);
81 static int header_fd (void);
82 static void alert (char *, int);
86 main (int argc, char **argv)
89 char *cp, *user, buf[BUFSIZ], tty[BUFSIZ];
90 char **argp, **arguments, *vec[MAXARGS];
99 setlocale(LC_ALL, "");
101 invo_name = r1bindex (argv[0], '/');
103 /* read user profile/context */
106 mts_init (invo_name);
107 arguments = getarguments (invo_name, argc, argv, 1);
110 while ((cp = *argp++)) {
112 switch (smatch (++cp, switches)) {
114 ambigsw (cp, switches);
121 snprintf (buf, sizeof(buf), "%s [command ...]", invo_name);
122 print_help (buf, switches, 1);
125 print_version(invo_name);
133 if (!(form = *argp++) || *form == '-')
134 adios (NULL, "missing argument to %s", argp[-2]);
138 if (!(format = *argp++) || *format == '-')
139 adios (NULL, "missing argument to %s", argp[-2]);
144 if (!(cp = *argp++) || *cp == '-')
145 adios(NULL, "missing argument to %s", argp[-2]);
167 if ((md = vecp ? message_fd (vec) : header_fd ()) == NOTOK)
170 user = getusername();
174 while ((utp = getutent()) != NULL) {
175 if (utp->ut_type == USER_PROCESS
176 && utp->ut_user[0] != 0
177 && utp->ut_line[0] != 0
178 && strncmp (user, utp->ut_user, sizeof(utp->ut_user)) == 0) {
179 strncpy (tty, utp->ut_line, sizeof(utp->ut_line));
185 if ((uf = fopen (UTMP_FILE, "r")) == NULL)
187 while (fread ((char *) &ut, sizeof(ut), 1, uf) == 1)
188 if (ut.ut_name[0] != 0
189 && strncmp (user, ut.ut_name, sizeof(ut.ut_name)) == 0) {
190 strncpy (tty, ut.ut_line, sizeof(ut.ut_line));
197 return 0; /* dead code to satisfy the compiler */
204 #ifndef RELIABLE_SIGNALS
205 SIGNAL (SIGALRM, alrmser);
213 message_fd (char **vec)
216 int bytes, fd, seconds;
221 fd = mkstemp (strncpy (tmpfil, "/tmp/rcvttyXXXXX", sizeof(tmpfil)));
223 unlink (mktemp (strncpy (tmpfil, "/tmp/rcvttyXXXXX", sizeof(tmpfil))));
224 if ((fd = open (tmpfil, O_RDWR | O_CREAT | O_TRUNC, 0600)) == NOTOK)
229 if ((child_id = vfork()) == NOTOK) {
233 } else if (child_id) {
235 if (!setjmp (myctx)) {
236 SIGNAL (SIGALRM, alrmser);
237 bytes = fstat(fileno (stdin), &st) != NOTOK ? (int) st.st_size : 100;
239 /* amount of time to wait depends on message size */
241 /* give at least 5 minutes */
243 } else if (bytes >= 90000) {
244 /* but 30 minutes should be long enough */
247 seconds = (bytes / 60) + 300;
249 alarm ((unsigned int) seconds);
250 pidwait(child_id, OK);
253 if (fstat (fd, &st) != NOTOK && st.st_size > (off_t) 0)
257 * Ruthlessly kill the child and anything
258 * else in its process group.
260 KILLPG(child_id, SIGKILL);
268 if (dup2 (fd, 1) == NOTOK || dup2 (fd, 2) == NOTOK)
271 setpgid ((pid_t) 0, getpid ()); /* put in own process group */
272 execvp (vec[0], vec);
274 return 1; /* dead code to satisfy compiler */
282 char *nfs, tmpfil[BUFSIZ];
284 strncpy (tmpfil, m_tmpfil (invo_name), sizeof(tmpfil));
285 if ((fd = open (tmpfil, O_RDWR | O_CREAT | O_TRUNC, 0600)) == NOTOK)
291 /* get new format string */
292 nfs = new_fs (form, format, SCANFMT);
293 scan (stdin, 0, 0, nfs, width, 0, 0, NULL, 0L, 0);
295 write (fd, "\n\r", 2);
296 write (fd, scanl, strlen (scanl));
298 write (fd, "\007", 1);
305 alert (char *tty, int md)
308 char buffer[BUFSIZ], ttyspec[BUFSIZ];
311 snprintf (ttyspec, sizeof(ttyspec), "/dev/%s", tty);
314 * The mask depends on whether we are checking for
315 * write permission based on `biff' or `mesg'.
317 mask = biff ? S_IEXEC : (S_IWRITE >> 3);
318 if (stat (ttyspec, &st) == NOTOK || (st.st_mode & mask) == 0)
321 if (!setjmp (myctx)) {
322 SIGNAL (SIGALRM, alrmser);
324 td = open (ttyspec, O_WRONLY);
333 lseek (md, (off_t) 0, SEEK_SET);
335 while ((i = read (md, buffer, sizeof(buffer))) > 0)
336 if (write (td, buffer, i) != i)