]> diplodocus.org Git - nmh/blob - uip/rcvtty.c
Replaced hard-coded value with MAX_DELIMITER_SIZE in m_getfld.c.
[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 extern m_getfld_state_t gstate;
35
36 static struct swit switches[] = {
37 #define BIFFSW 0
38 { "biff", 0 },
39 #define FORMSW 1
40 { "form formatfile", 0 },
41 #define FMTSW 2
42 { "format string", 5 },
43 #define WIDTHSW 3
44 { "width columns", 0 },
45 #define NLSW 4
46 { "newline", 0 },
47 #define NNLSW 5
48 { "nonewline", 0 },
49 #define BELSW 6
50 { "bell", 0 },
51 #define NBELSW 7
52 { "nobell", 0 },
53 #define VERSIONSW 8
54 { "version", 0 },
55 #define HELPSW 9
56 { "help", 0 },
57 { NULL, 0 }
58 };
59
60 static jmp_buf myctx;
61 static int bell = 1;
62 static int newline = 1;
63 static int biff = 0;
64 static int width = 0;
65 static char *form = NULL;
66 static char *format = NULL;
67
68 /*
69 * external prototypes
70 */
71 char *getusername(void);
72
73 /*
74 * static prototypes
75 */
76 static void alrmser (int);
77 static int message_fd (char **);
78 static int header_fd (void);
79 static void alert (char *, int);
80
81
82 int
83 main (int argc, char **argv)
84 {
85 int md, vecp = 0;
86 char *cp, *user, buf[BUFSIZ], tty[BUFSIZ];
87 char **argp, **arguments, *vec[MAXARGS];
88 struct utmpx *utp;
89 #ifdef LOCALE
90 setlocale(LC_ALL, "");
91 #endif
92 invo_name = r1bindex (argv[0], '/');
93
94 /* read user profile/context */
95 context_read();
96
97 mts_init (invo_name);
98 arguments = getarguments (invo_name, argc, argv, 1);
99 argp = arguments;
100
101 while ((cp = *argp++)) {
102 if (*cp == '-') {
103 switch (smatch (++cp, switches)) {
104 case AMBIGSW:
105 ambigsw (cp, switches);
106 done (1);
107 case UNKWNSW:
108 vec[vecp++] = --cp;
109 continue;
110
111 case HELPSW:
112 snprintf (buf, sizeof(buf), "%s [command ...]", invo_name);
113 print_help (buf, switches, 1);
114 done (0);
115 case VERSIONSW:
116 print_version(invo_name);
117 done (0);
118
119 case BIFFSW:
120 biff = 1;
121 continue;
122
123 case FORMSW:
124 if (!(form = *argp++) || *form == '-')
125 adios (NULL, "missing argument to %s", argp[-2]);
126 format = NULL;
127 continue;
128 case FMTSW:
129 if (!(format = *argp++) || *format == '-')
130 adios (NULL, "missing argument to %s", argp[-2]);
131 form = NULL;
132 continue;
133
134 case WIDTHSW:
135 if (!(cp = *argp++) || *cp == '-')
136 adios(NULL, "missing argument to %s", argp[-2]);
137 width = atoi(cp);
138 continue;
139 case NLSW:
140 newline = 1;
141 continue;
142 case NNLSW:
143 newline = 0;
144 continue;
145 case BELSW:
146 bell = 1;
147 continue;
148 case NBELSW:
149 bell = 0;
150 continue;
151
152 }
153 }
154 vec[vecp++] = cp;
155 }
156 vec[vecp] = 0;
157
158 if ((md = vecp ? message_fd (vec) : header_fd ()) == NOTOK)
159 exit (RCV_MBX);
160
161 user = getusername();
162
163 #if HAVE_GETUTXENT
164 setutxent();
165 while ((utp = getutxent()) != NULL) {
166 if (utp->ut_type == USER_PROCESS && utp->ut_user[0] != 0
167 && utp->ut_line[0] != 0
168 && strncmp (user, utp->ut_user, sizeof(utp->ut_user)) == 0) {
169 strncpy (tty, utp->ut_line, sizeof(utp->ut_line));
170 alert (tty, md);
171 }
172 }
173 endutxent();
174 #endif /* HAVE_GETUTXENT */
175
176 exit (RCV_MOK);
177 return 0; /* dead code to satisfy the compiler */
178 }
179
180
181 static void
182 alrmser (int i)
183 {
184 NMH_UNUSED (i);
185
186 longjmp (myctx, 1);
187 }
188
189
190 static int
191 message_fd (char **vec)
192 {
193 pid_t child_id;
194 int bytes, fd, seconds;
195 char tmpfil[BUFSIZ];
196 struct stat st;
197
198 fd = mkstemp (strncpy (tmpfil, "/tmp/rcvttyXXXXX", sizeof(tmpfil)));
199 unlink (tmpfil);
200
201 if ((child_id = vfork()) == NOTOK) {
202 /* fork error */
203 close (fd);
204 return header_fd ();
205 } else if (child_id) {
206 /* parent process */
207 if (!setjmp (myctx)) {
208 SIGNAL (SIGALRM, alrmser);
209 bytes = fstat(fileno (stdin), &st) != NOTOK ? (int) st.st_size : 100;
210
211 /* amount of time to wait depends on message size */
212 if (bytes <= 100) {
213 /* give at least 5 minutes */
214 seconds = 300;
215 } else if (bytes >= 90000) {
216 /* but 30 minutes should be long enough */
217 seconds = 1800;
218 } else {
219 seconds = (bytes / 60) + 300;
220 }
221 alarm ((unsigned int) seconds);
222 pidwait(child_id, OK);
223 alarm (0);
224
225 if (fstat (fd, &st) != NOTOK && st.st_size > (off_t) 0)
226 return fd;
227 } else {
228 /*
229 * Ruthlessly kill the child and anything
230 * else in its process group.
231 */
232 killpg(child_id, SIGKILL);
233 }
234 close (fd);
235 return header_fd ();
236 }
237
238 /* child process */
239 rewind (stdin);
240 if (dup2 (fd, 1) == NOTOK || dup2 (fd, 2) == NOTOK)
241 _exit (-1);
242 closefds (3);
243 setpgid ((pid_t) 0, getpid ()); /* put in own process group */
244 execvp (vec[0], vec);
245 _exit (-1);
246 return 1; /* dead code to satisfy compiler */
247 }
248
249
250 static int
251 header_fd (void)
252 {
253 int fd;
254 char *nfs;
255 char *tfile = NULL;
256
257 tfile = m_mktemp2(NULL, invo_name, &fd, NULL);
258 if (tfile == NULL) return NOTOK;
259 unlink (tfile);
260
261 rewind (stdin);
262
263 /* get new format string */
264 nfs = new_fs (form, format, SCANFMT);
265 m_getfld_state_init (&gstate);
266 scan (stdin, 0, 0, nfs, width, 0, 0, NULL, 0L, 0);
267 m_getfld_state_destroy (&gstate);
268 if (newline)
269 write (fd, "\n\r", 2);
270 write (fd, scanl, strlen (scanl));
271 if (bell)
272 write (fd, "\007", 1);
273
274 return fd;
275 }
276
277
278 static void
279 alert (char *tty, int md)
280 {
281 int i, td, mask;
282 char buffer[BUFSIZ], ttyspec[BUFSIZ];
283 struct stat st;
284
285 snprintf (ttyspec, sizeof(ttyspec), "/dev/%s", tty);
286
287 /*
288 * The mask depends on whether we are checking for
289 * write permission based on `biff' or `mesg'.
290 */
291 mask = biff ? S_IEXEC : (S_IWRITE >> 3);
292 if (stat (ttyspec, &st) == NOTOK || (st.st_mode & mask) == 0)
293 return;
294
295 if (!setjmp (myctx)) {
296 SIGNAL (SIGALRM, alrmser);
297 alarm (2);
298 td = open (ttyspec, O_WRONLY);
299 alarm (0);
300 if (td == NOTOK)
301 return;
302 } else {
303 alarm (0);
304 return;
305 }
306
307 lseek (md, (off_t) 0, SEEK_SET);
308
309 while ((i = read (md, buffer, sizeof(buffer))) > 0)
310 if (write (td, buffer, i) != i)
311 break;
312
313 close (td);
314 }
315