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