2 ** rcvtty.c -- a rcvmail program (a lot like rcvalert) handling IPC ttys
4 ** This code is Copyright (c) 2002, by the authors of nmh. See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
9 /* Changed to use getutent() and friends. Assumes that when getutent() exists,
10 ** a number of other things also exist. Please check.
11 ** Ruud de Rooij <ruud@ruud.org> Sun, 28 May 2000 17:28:55 +0200
15 #include <h/signals.h>
16 #include <h/rcvmail.h>
17 #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})%> \
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 static RETSIGTYPE alrmser(int);
75 static int message_fd(char **);
76 static int header_fd(void);
77 static void alert(char *, int);
81 main(int argc, char **argv)
84 char *cp, *user, buf[BUFSIZ], tty[BUFSIZ];
85 char **argp, **arguments, *vec[MAXARGS];
94 setlocale(LC_ALL, "");
96 invo_name = mhbasename(argv[0]);
98 /* read user profile/context */
101 arguments = getarguments(invo_name, argc, argv, 1);
104 while ((cp = *argp++)) {
106 switch (smatch(++cp, switches)) {
108 ambigsw(cp, switches);
115 snprintf(buf, sizeof(buf), "%s [command ...]",
117 print_help(buf, switches, 1);
120 print_version(invo_name);
128 if (!(form = *argp++) || *form == '-')
129 adios(NULL, "missing argument to %s",
134 if (!(format = *argp++) || *format == '-')
135 adios(NULL, "missing argument to %s",
141 if (!(cp = *argp++) || *cp == '-')
142 adios(NULL, "missing argument to %s",
165 if ((md = vecp ? message_fd(vec) : header_fd()) == NOTOK)
168 user = getusername();
172 while ((utp = getutent()) != NULL) {
174 #ifdef HAVE_STRUCT_UTMP_UT_TYPE
175 utp->ut_type == USER_PROCESS &&
177 utp->ut_name[0] != 0 && utp->ut_line[0] != 0
178 && strncmp(user, utp->ut_name,
179 sizeof(utp->ut_name)) == 0) {
180 strncpy(tty, utp->ut_line, sizeof(utp->ut_line));
186 if ((uf = fopen(UTMP_FILE, "r")) == NULL)
188 while (fread((char *) &ut, sizeof(ut), 1, uf) == 1)
189 if (ut.ut_name[0] != 0 &&
190 strncmp(user, ut.ut_name, sizeof(ut.ut_name))
192 strncpy(tty, ut.ut_line, sizeof(ut.ut_line));
199 return 0; /* dead code to satisfy the compiler */
206 #ifndef RELIABLE_SIGNALS
207 SIGNAL(SIGALRM, alrmser);
215 message_fd(char **vec)
218 int bytes, fd, seconds;
223 fd = mkstemp(strncpy(tmpfil, "/tmp/rcvttyXXXXX", sizeof(tmpfil)));
225 unlink(mktemp(strncpy(tmpfil, "/tmp/rcvttyXXXXX", sizeof(tmpfil))));
226 if ((fd = open(tmpfil, O_RDWR | O_CREAT | O_TRUNC, 0600)) == NOTOK)
231 if ((child_id = fork()) == NOTOK) {
235 } else if (child_id) {
237 if (!setjmp(myctx)) {
238 SIGNAL(SIGALRM, alrmser);
239 bytes = fstat(fileno(stdin), &st) != NOTOK ?
240 (int) st.st_size : 100;
242 /* amount of time to wait depends on message size */
244 /* give at least 5 minutes */
246 } else if (bytes >= 90000) {
247 /* but 30 minutes should be long enough */
250 seconds = (bytes / 60) + 300;
252 alarm((unsigned int) seconds);
253 pidwait(child_id, OK);
256 if (fstat(fd, &st) != NOTOK && st.st_size > (off_t) 0)
260 ** Ruthlessly kill the child and anything
261 ** else in its process group.
263 KILLPG(child_id, SIGKILL);
271 if (dup2(fd, 1) == NOTOK || dup2(fd, 2) == NOTOK)
274 setpgid((pid_t) 0, getpid()); /* put in own process group */
277 return 1; /* dead code to satisfy compiler */
288 tfile = m_mktemp2(NULL, invo_name, &fd, NULL);
289 if (tfile == NULL) return NOTOK;
294 /* get new format string */
295 nfs = new_fs(form, format, SCANFMT);
296 scan(stdin, 0, 0, nfs, width, 0, 0, NULL, 0L, 0);
298 write(fd, "\n\r", 2);
299 write(fd, scanl, strlen(scanl));
301 write(fd, "\007", 1);
308 alert(char *tty, int md)
311 char buffer[BUFSIZ], ttyspec[BUFSIZ];
314 snprintf(ttyspec, sizeof(ttyspec), "/dev/%s", tty);
317 ** The mask depends on whether we are checking for
318 ** write permission based on `biff' or `mesg'.
320 mask = biff ? S_IEXEC : (S_IWRITE >> 3);
321 if (stat(ttyspec, &st) == NOTOK || (st.st_mode & mask) == 0)
324 if (!setjmp(myctx)) {
325 SIGNAL(SIGALRM, alrmser);
327 td = open(ttyspec, O_WRONLY);
336 lseek(md, (off_t) 0, SEEK_SET);
338 while ((i = read(md, buffer, sizeof(buffer))) > 0)
339 if (write(td, buffer, i) != i)