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