]> diplodocus.org Git - nmh/blob - uip/rcvtty.c
Apply David Levine's fix from whomfile() to sendfile() as well.
[nmh] / uip / rcvtty.c
1
2 /*
3 * rcvtty.c -- a rcvmail program (a lot like rcvalert) handling IPC ttys
4 *
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
8 */
9
10 /* Changed to use getutent() and friends. Assumes that when getutent() exists,
11 * a number of other things also exist. Please check.
12 * Ruud de Rooij <ruud@ruud.org> Sun, 28 May 2000 17:28:55 +0200
13 */
14
15 #include <h/mh.h>
16 #include <h/signals.h>
17 #include <setjmp.h>
18 #include <h/rcvmail.h>
19 #include <h/scansbr.h>
20 #include <h/tws.h>
21 #include <h/mts.h>
22 #include <signal.h>
23 #include <fcntl.h>
24
25 #ifdef HAVE_GETUTXENT
26 #include <utmpx.h>
27 #endif /* HAVE_GETUTXENT */
28
29 #define SCANFMT \
30 "%2(hour{dtimenow}):%02(min{dtimenow}): %<(size)%5(size) %>%<{encrypted}E%>\
31 %<(mymbox{from})%<{to}To:%14(friendly{to})%>%>%<(zero)%17(friendly{from})%> \
32 %{subject}%<{body}<<%{body}>>%>"
33
34 #define RCVTTY_SWITCHES \
35 X("biff", 0, BIFFSW) \
36 X("form formatfile", 0, FORMSW) \
37 X("format string", 5, FMTSW) \
38 X("width columns", 0, WIDTHSW) \
39 X("newline", 0, NLSW) \
40 X("nonewline", 0, NNLSW) \
41 X("bell", 0, BELSW) \
42 X("nobell", 0, NBELSW) \
43 X("version", 0, VERSIONSW) \
44 X("help", 0, HELPSW) \
45
46 #define X(sw, minchars, id) id,
47 DEFINE_SWITCH_ENUM(RCVTTY);
48 #undef X
49
50 #define X(sw, minchars, id) { sw, minchars, id },
51 DEFINE_SWITCH_ARRAY(RCVTTY, switches);
52 #undef X
53
54 static jmp_buf myctx;
55 static int bell = 1;
56 static int newline = 1;
57 static int biff = 0;
58 static int width = 0;
59 static char *form = NULL;
60 static char *format = NULL;
61
62 /*
63 * external prototypes
64 */
65 char *getusername(void);
66
67 /*
68 * static prototypes
69 */
70 static void alrmser (int);
71 static int message_fd (char **);
72 static int header_fd (void);
73 static void alert (char *, int);
74
75
76 int
77 main (int argc, char **argv)
78 {
79 int md, vecp = 0;
80 char *cp, *user, buf[BUFSIZ], tty[BUFSIZ];
81 char **argp, **arguments, *vec[MAXARGS];
82 struct utmpx *utp;
83 #ifdef LOCALE
84 setlocale(LC_ALL, "");
85 #endif
86 invo_name = r1bindex (argv[0], '/');
87
88 /* read user profile/context */
89 context_read();
90
91 mts_init (invo_name);
92 arguments = getarguments (invo_name, argc, argv, 1);
93 argp = arguments;
94
95 while ((cp = *argp++)) {
96 if (*cp == '-') {
97 switch (smatch (++cp, switches)) {
98 case AMBIGSW:
99 ambigsw (cp, switches);
100 done (1);
101 case UNKWNSW:
102 vec[vecp++] = --cp;
103 continue;
104
105 case HELPSW:
106 snprintf (buf, sizeof(buf), "%s [command ...]", invo_name);
107 print_help (buf, switches, 1);
108 done (0);
109 case VERSIONSW:
110 print_version(invo_name);
111 done (0);
112
113 case BIFFSW:
114 biff = 1;
115 continue;
116
117 case FORMSW:
118 if (!(form = *argp++) || *form == '-')
119 adios (NULL, "missing argument to %s", argp[-2]);
120 format = NULL;
121 continue;
122 case FMTSW:
123 if (!(format = *argp++) || *format == '-')
124 adios (NULL, "missing argument to %s", argp[-2]);
125 form = NULL;
126 continue;
127
128 case WIDTHSW:
129 if (!(cp = *argp++) || *cp == '-')
130 adios(NULL, "missing argument to %s", argp[-2]);
131 width = atoi(cp);
132 continue;
133 case NLSW:
134 newline = 1;
135 continue;
136 case NNLSW:
137 newline = 0;
138 continue;
139 case BELSW:
140 bell = 1;
141 continue;
142 case NBELSW:
143 bell = 0;
144 continue;
145
146 }
147 }
148 vec[vecp++] = cp;
149 }
150 vec[vecp] = 0;
151
152 if ((md = vecp ? message_fd (vec) : header_fd ()) == NOTOK)
153 exit (RCV_MBX);
154
155 user = getusername();
156
157 #if HAVE_GETUTXENT
158 setutxent();
159 while ((utp = getutxent()) != NULL) {
160 if (utp->ut_type == USER_PROCESS && utp->ut_user[0] != 0
161 && utp->ut_line[0] != 0
162 && strncmp (user, utp->ut_user, sizeof(utp->ut_user)) == 0) {
163 strncpy (tty, utp->ut_line, sizeof(utp->ut_line));
164 alert (tty, md);
165 }
166 }
167 endutxent();
168 #endif /* HAVE_GETUTXENT */
169
170 exit (RCV_MOK);
171 return 0; /* dead code to satisfy the compiler */
172 }
173
174
175 static void
176 alrmser (int i)
177 {
178 NMH_UNUSED (i);
179
180 longjmp (myctx, 1);
181 }
182
183
184 static int
185 message_fd (char **vec)
186 {
187 pid_t child_id;
188 int bytes, fd, seconds;
189 char tmpfil[BUFSIZ];
190 struct stat st;
191
192 fd = mkstemp (strncpy (tmpfil, "/tmp/rcvttyXXXXX", sizeof(tmpfil)));
193 unlink (tmpfil);
194
195 if ((child_id = fork()) == NOTOK) {
196 /* fork error */
197 close (fd);
198 return header_fd ();
199 } else if (child_id) {
200 /* parent process */
201 if (!setjmp (myctx)) {
202 SIGNAL (SIGALRM, alrmser);
203 bytes = fstat(fileno (stdin), &st) != NOTOK ? (int) st.st_size : 100;
204
205 /* amount of time to wait depends on message size */
206 if (bytes <= 100) {
207 /* give at least 5 minutes */
208 seconds = 300;
209 } else if (bytes >= 90000) {
210 /* but 30 minutes should be long enough */
211 seconds = 1800;
212 } else {
213 seconds = (bytes / 60) + 300;
214 }
215 alarm ((unsigned int) seconds);
216 pidwait(child_id, OK);
217 alarm (0);
218
219 if (fstat (fd, &st) != NOTOK && st.st_size > (off_t) 0)
220 return fd;
221 } else {
222 /*
223 * Ruthlessly kill the child and anything
224 * else in its process group.
225 */
226 killpg(child_id, SIGKILL);
227 }
228 close (fd);
229 return header_fd ();
230 }
231
232 /* child process */
233 rewind (stdin);
234 if (dup2 (fd, 1) == NOTOK || dup2 (fd, 2) == NOTOK)
235 _exit (-1);
236 closefds (3);
237 setpgid ((pid_t) 0, getpid ()); /* put in own process group */
238 execvp (vec[0], vec);
239 _exit (-1);
240 return 1; /* dead code to satisfy compiler */
241 }
242
243
244 static int
245 header_fd (void)
246 {
247 int fd;
248 char *nfs;
249 char *tfile = NULL;
250
251 tfile = m_mktemp2(NULL, invo_name, &fd, NULL);
252 if (tfile == NULL) return NOTOK;
253 unlink (tfile);
254
255 rewind (stdin);
256
257 /* get new format string */
258 nfs = new_fs (form, format, SCANFMT);
259 scan (stdin, 0, 0, nfs, width, 0, 0, NULL, 0L, 0);
260 scan_finished ();
261 if (newline)
262 write (fd, "\n\r", 2);
263 write (fd, scanl, strlen (scanl));
264 if (bell)
265 write (fd, "\007", 1);
266
267 return fd;
268 }
269
270
271 static void
272 alert (char *tty, int md)
273 {
274 int i, td, mask;
275 char buffer[BUFSIZ], ttyspec[BUFSIZ];
276 struct stat st;
277
278 snprintf (ttyspec, sizeof(ttyspec), "/dev/%s", tty);
279
280 /*
281 * The mask depends on whether we are checking for
282 * write permission based on `biff' or `mesg'.
283 */
284 mask = biff ? S_IEXEC : (S_IWRITE >> 3);
285 if (stat (ttyspec, &st) == NOTOK || (st.st_mode & mask) == 0)
286 return;
287
288 if (!setjmp (myctx)) {
289 SIGNAL (SIGALRM, alrmser);
290 alarm (2);
291 td = open (ttyspec, O_WRONLY);
292 alarm (0);
293 if (td == NOTOK)
294 return;
295 } else {
296 alarm (0);
297 return;
298 }
299
300 lseek (md, (off_t) 0, SEEK_SET);
301
302 while ((i = read (md, buffer, sizeof(buffer))) > 0)
303 if (write (td, buffer, i) != i)
304 break;
305
306 close (td);
307 }
308