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 { "width columns", 0 },
63 static int newline = 1;
66 static char *form = NULL;
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];
91 setlocale(LC_ALL, "");
93 invo_name = mhbasename(argv[0]);
95 /* read user profile/context */
98 arguments = getarguments(invo_name, argc, argv, 1);
101 while ((cp = *argp++)) {
103 switch (smatch(++cp, switches)) {
105 ambigsw(cp, switches);
112 snprintf(buf, sizeof(buf), "%s [command ...]",
114 print_help(buf, switches, 1);
117 print_version(invo_name);
125 if (!(form = *argp++) || *form == '-')
126 adios(NULL, "missing argument to %s",
131 if (!(cp = *argp++) || *cp == '-')
132 adios(NULL, "missing argument to %s",
155 if ((md = vecp ? message_fd(vec) : header_fd()) == NOTOK)
158 user = getusername();
162 while ((utp = getutent()) != NULL) {
164 #ifdef HAVE_STRUCT_UTMP_UT_TYPE
165 utp->ut_type == USER_PROCESS &&
167 utp->ut_name[0] != 0 && utp->ut_line[0] != 0
168 && strncmp(user, utp->ut_name,
169 sizeof(utp->ut_name)) == 0) {
170 strncpy(tty, utp->ut_line, sizeof(utp->ut_line));
176 if ((uf = fopen(UTMP_FILE, "r")) == NULL)
178 while (fread((char *) &ut, sizeof(ut), 1, uf) == 1)
179 if (ut.ut_name[0] != 0 &&
180 strncmp(user, ut.ut_name, sizeof(ut.ut_name))
182 strncpy(tty, ut.ut_line, sizeof(ut.ut_line));
189 return 0; /* dead code to satisfy the compiler */
196 #ifndef RELIABLE_SIGNALS
197 SIGNAL(SIGALRM, alrmser);
205 message_fd(char **vec)
208 int bytes, fd, seconds;
213 fd = mkstemp(strncpy(tmpfil, "/tmp/rcvttyXXXXX", sizeof(tmpfil)));
215 unlink(mktemp(strncpy(tmpfil, "/tmp/rcvttyXXXXX", sizeof(tmpfil))));
216 if ((fd = open(tmpfil, O_RDWR | O_CREAT | O_TRUNC, 0600)) == NOTOK)
221 if ((child_id = fork()) == NOTOK) {
225 } else if (child_id) {
227 if (!setjmp(myctx)) {
228 SIGNAL(SIGALRM, alrmser);
229 bytes = fstat(fileno(stdin), &st) != NOTOK ?
230 (int) st.st_size : 100;
232 /* amount of time to wait depends on message size */
234 /* give at least 5 minutes */
236 } else if (bytes >= 90000) {
237 /* but 30 minutes should be long enough */
240 seconds = (bytes / 60) + 300;
242 alarm((unsigned int) seconds);
243 pidwait(child_id, OK);
246 if (fstat(fd, &st) != NOTOK && st.st_size > (off_t) 0)
250 ** Ruthlessly kill the child and anything
251 ** else in its process group.
253 KILLPG(child_id, SIGKILL);
261 if (dup2(fd, 1) == NOTOK || dup2(fd, 2) == NOTOK)
264 setpgid((pid_t) 0, getpid()); /* put in own process group */
267 return 1; /* dead code to satisfy compiler */
278 tfile = m_mktemp2(NULL, invo_name, &fd, NULL);
279 if (tfile == NULL) return NOTOK;
284 /* get new format string */
285 nfs = new_fs(form, SCANFMT);
286 scan(stdin, 0, 0, nfs, width, 0, 0, NULL, 0L, 0);
288 write(fd, "\n\r", 2);
289 write(fd, scanl, strlen(scanl));
291 write(fd, "\007", 1);
298 alert(char *tty, int md)
301 char buffer[BUFSIZ], ttyspec[BUFSIZ];
304 snprintf(ttyspec, sizeof(ttyspec), "/dev/%s", tty);
307 ** The mask depends on whether we are checking for
308 ** write permission based on `biff' or `mesg'.
310 mask = biff ? S_IEXEC : (S_IWRITE >> 3);
311 if (stat(ttyspec, &st) == NOTOK || (st.st_mode & mask) == 0)
314 if (!setjmp(myctx)) {
315 SIGNAL(SIGALRM, alrmser);
317 td = open(ttyspec, O_WRONLY);
326 lseek(md, (off_t) 0, SEEK_SET);
328 while ((i = read(md, buffer, sizeof(buffer))) > 0)
329 if (write(td, buffer, i) != i)