]> diplodocus.org Git - nmh/blob - uip/msgchk.c
lock_file.c: close(2) file descriptor on failure, avoiding leak.
[nmh] / uip / msgchk.c
1 /* msgchk.c -- check for mail
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include <h/mh.h>
9 #include <h/mts.h>
10 #include <h/tws.h>
11 #include <h/utils.h>
12 #include <pwd.h>
13
14 #include <h/popsbr.h>
15
16 #ifndef CYRUS_SASL
17 # define SASLminc(a) (a)
18 #else
19 # define SASLminc(a) 0
20 #endif
21
22 #ifndef TLS_SUPPORT
23 # define TLSminc(a) (a)
24 #else
25 # define TLSminc(a) 0
26 #endif
27
28 #define MSGCHK_SWITCHES \
29 X("date", 0, DATESW) \
30 X("nodate", 0, NDATESW) \
31 X("notify type", 0, NOTESW) \
32 X("nonotify type", 0, NNOTESW) \
33 X("host hostname", 0, HOSTSW) \
34 X("user username", 0, USERSW) \
35 X("port name/number", 0, PORTSW) \
36 X("version", 0, VERSIONSW) \
37 X("help", 0, HELPSW) \
38 X("snoop", 0, SNOOPSW) \
39 X("sasl", SASLminc(4), SASLSW) \
40 X("nosasl", SASLminc(6), NOSASLSW) \
41 X("saslmech", SASLminc(5), SASLMECHSW) \
42 X("authservice", SASLminc(0), AUTHSERVICESW) \
43 X("initialtls", TLSminc(-10), INITTLSSW) \
44 X("notls", TLSminc(-5), NOTLSSW) \
45 X("certverify", TLSminc(-10), CERTVERSW) \
46 X("nocertverify", TLSminc(-12), NOCERTVERSW) \
47 X("proxy command", 0, PROXYSW) \
48
49 #define X(sw, minchars, id) id,
50 DEFINE_SWITCH_ENUM(MSGCHK);
51 #undef X
52
53 #define X(sw, minchars, id) { sw, minchars, id },
54 DEFINE_SWITCH_ARRAY(MSGCHK, switches);
55 #undef X
56
57 /*
58 * Maximum numbers of users we can check (plus
59 * one for the NULL vector at the end).
60 */
61 #define MAXVEC 51
62
63 #define NT_NONE 0x0
64 #ifdef NT_NONE
65 #endif /* Use NT_NONE to prevent warning from gcc -Wunused-macros. */
66 #define NT_MAIL 0x1
67 #define NT_NMAI 0x2
68 #define NT_ALL (NT_MAIL | NT_NMAI)
69
70 #define NONEOK 0x0
71 #define UUCPOLD 0x1
72 #define UUCPNEW 0x2
73 #define UUCPOK (UUCPOLD | UUCPNEW)
74 #define MMDFOLD 0x4
75 #define MMDFNEW 0x8
76 #define MMDFOK (MMDFOLD | MMDFNEW)
77
78
79 /*
80 * static prototypes
81 */
82 static int donote (char *, int) PURE;
83 static int checkmail (char *, char *, int, int, int);
84 static int remotemail (char *, char *, char *, char *, int, int, int, int,
85 char *, int, const char *);
86
87
88 int
89 main (int argc, char **argv)
90 {
91 int datesw = 1, notifysw = NT_ALL;
92 int status = 0, sasl = 0, tls = 0, noverify = 0;
93 int snoop = 0, vecp = 0;
94 char *cp, *host = NULL, *port = NULL, *user = NULL, *proxy = NULL;
95 char buf[BUFSIZ], *saslmech = NULL, *auth_svc = NULL;
96 char **argp, **arguments, *vec[MAXVEC];
97 struct passwd *pw;
98
99 if (nmh_init(argv[0], 1)) { return 1; }
100
101 mts_init ();
102
103 arguments = getarguments (invo_name, argc, argv, 1);
104 argp = arguments;
105
106 while ((cp = *argp++)) {
107 if (*cp == '-') {
108 switch (smatch (++cp, switches)) {
109 case AMBIGSW:
110 ambigsw (cp, switches);
111 done (1);
112 case UNKWNSW:
113 adios (NULL, "-%s unknown", cp);
114
115 case HELPSW:
116 snprintf (buf, sizeof(buf), "%s [switches] [users ...]",
117 invo_name);
118 print_help (buf, switches, 1);
119 done (0);
120 case VERSIONSW:
121 print_version(invo_name);
122 done (0);
123
124 case DATESW:
125 datesw++;
126 continue;
127 case NDATESW:
128 datesw = 0;
129 continue;
130
131 case NOTESW:
132 if (!(cp = *argp++) || *cp == '-')
133 adios (NULL, "missing argument to %s", argp[-2]);
134 notifysw |= donote (cp, 1);
135 continue;
136 case NNOTESW:
137 if (!(cp = *argp++) || *cp == '-')
138 adios (NULL, "missing argument to %s", argp[-2]);
139 notifysw &= ~donote (cp, 0);
140 continue;
141
142 case HOSTSW:
143 if (!(host = *argp++) || *host == '-')
144 adios (NULL, "missing argument to %s", argp[-2]);
145 continue;
146
147 case PORTSW:
148 if (!(port = *argp++) || *port == '-')
149 adios (NULL, "missing argument to %s", argp[-2]);
150 continue;
151
152 case USERSW:
153 if (!(cp = *argp++) || *cp == '-')
154 adios (NULL, "missing argument to %s", argp[-2]);
155 if (vecp >= MAXVEC-1)
156 adios (NULL, "you can only check %d users at a time", MAXVEC-1);
157 user = vec[vecp++] = cp;
158 continue;
159
160 case SNOOPSW:
161 snoop++;
162 continue;
163
164 case SASLSW:
165 sasl++;
166 continue;
167
168 case NOSASLSW:
169 sasl = 0;
170 continue;
171
172 case SASLMECHSW:
173 if (!(saslmech = *argp++) || *saslmech == '-')
174 adios (NULL, "missing argument to %s", argp[-2]);
175 continue;
176
177 case INITTLSSW:
178 tls++;
179 continue;
180
181 case NOTLSSW:
182 tls = 0;
183 continue;
184
185 case CERTVERSW:
186 noverify = 0;
187 continue;
188
189 case NOCERTVERSW:
190 noverify++;
191 continue;
192
193 case AUTHSERVICESW:
194 #ifdef OAUTH_SUPPORT
195 if (!(auth_svc = *argp++) || *auth_svc == '-')
196 adios (NULL, "missing argument to %s", argp[-2]);
197 #else
198 adios (NULL, "not built with OAuth support");
199 #endif
200 continue;
201
202 case PROXYSW:
203 if (!(proxy = *argp++) || *proxy == '-')
204 adios (NULL, "missing argument to %s", argp[-2]);
205 continue;
206 }
207 }
208 if (vecp >= MAXVEC-1)
209 adios (NULL, "you can only check %d users at a time", MAXVEC-1);
210 vec[vecp++] = cp;
211 }
212
213 /*
214 * If -host is not specified by user
215 */
216 if (!host || !*host) {
217 /*
218 * If "pophost" is specified in mts.conf,
219 * use it as default value.
220 */
221 if (pophost && *pophost)
222 host = pophost;
223 }
224 if (!host || !*host)
225 host = NULL;
226
227 if (vecp != 0)
228 vec[vecp] = NULL;
229
230 if (host) {
231 int tlsflag = 0;
232
233 if (tls)
234 tlsflag |= P_INITTLS;
235
236 if (noverify)
237 tlsflag |= P_NOVERIFY;
238
239 if (vecp == 0) {
240 status = remotemail (host, port, user, proxy, notifysw, 1,
241 snoop, sasl, saslmech, tlsflag, auth_svc);
242 } else {
243 for (vecp = 0; vec[vecp]; vecp++)
244 status += remotemail (host, port, vec[vecp], proxy, notifysw, 0,
245 snoop, sasl, saslmech, tlsflag, auth_svc);
246 }
247 } else {
248 if (user == NULL) user = getusername ();
249 if (vecp == 0) {
250 char *home;
251
252 /* Not sure this check makes sense... */
253 if (!geteuid() || NULL == (home = getenv("HOME"))) {
254 pw = getpwnam (user);
255 if (pw == NULL)
256 adios (NULL, "unable to get information about user");
257 home = pw->pw_dir;
258 }
259 status = checkmail (user, home, datesw, notifysw, 1);
260 } else {
261 for (vecp = 0; vec[vecp]; vecp++) {
262 if ((pw = getpwnam (vec[vecp])))
263 status += checkmail (pw->pw_name, pw->pw_dir, datesw, notifysw, 0);
264 else
265 inform("no such user as %s", vec[vecp]);
266 }
267 }
268 } /* host == NULL */
269
270 done (status);
271 return 1;
272 }
273
274
275 #define NOTE_SWITCHES \
276 X("all", 0, NALLSW) \
277 X("mail", 0, NMAISW) \
278 X("nomail", 0, NNMAISW) \
279
280 #define X(sw, minchars, id) id,
281 DEFINE_SWITCH_ENUM(NOTE);
282 #undef X
283
284 #define X(sw, minchars, id) { sw, minchars, id },
285 DEFINE_SWITCH_ARRAY(NOTE, ntswitches);
286 #undef X
287
288
289 static int
290 donote (char *cp, int ntflag)
291 {
292 switch (smatch (cp, ntswitches)) {
293 case AMBIGSW:
294 ambigsw (cp, ntswitches);
295 done (1);
296 case UNKWNSW:
297 adios (NULL, "-%snotify %s unknown", ntflag ? "" : "no", cp);
298
299 case NALLSW:
300 return NT_ALL;
301 case NMAISW:
302 return NT_MAIL;
303 case NNMAISW:
304 return NT_NMAI;
305 }
306
307 return 0; /* Before 1999-07-15, garbage was returned if control got here. */
308 }
309
310
311 static int
312 checkmail (char *user, char *home, int datesw, int notifysw, int personal)
313 {
314 int mf, status;
315 char buffer[BUFSIZ];
316 struct stat st;
317
318 snprintf (buffer, sizeof(buffer), "%s/%s", mmdfldir[0] ? mmdfldir : home, mmdflfil[0] ? mmdflfil : user);
319 if (datesw) {
320 st.st_size = 0;
321 st.st_atime = st.st_mtime = 0;
322 }
323 mf = (stat (buffer, &st) == NOTOK || st.st_size == 0) ? NONEOK
324 : st.st_atime <= st.st_mtime ? MMDFNEW : MMDFOLD;
325
326 if ((mf & UUCPOK) || (mf & MMDFOK)) {
327 if (notifysw & NT_MAIL) {
328 if (personal)
329 printf ("You have ");
330 else
331 printf ("%s has ", user);
332 if (mf & UUCPOK)
333 printf ("%s old-style bell", mf & UUCPOLD ? "old" : "new");
334 if ((mf & UUCPOK) && (mf & MMDFOK))
335 printf (" and ");
336 if (mf & MMDFOK)
337 printf ("%s%s", mf & MMDFOLD ? "old" : "new",
338 mf & UUCPOK ? " Internet" : "");
339 printf (" mail waiting");
340 } else {
341 notifysw = 0;
342 }
343 status = 0;
344 }
345 else {
346 if (notifysw & NT_NMAI)
347 printf (personal ? "You don't %s%s" : "%s doesn't %s",
348 personal ? "" : user, "have any mail waiting");
349 else
350 notifysw = 0;
351
352 status = 1;
353 }
354
355 if (notifysw)
356 if (datesw && st.st_atime)
357 printf ("; last read on %s", dtime (&st.st_atime, 1));
358 if (notifysw)
359 putchar('\n');
360
361 return status;
362 }
363
364
365 extern char response[];
366
367 static int
368 remotemail (char *host, char *port, char *user, char *proxy, int notifysw,
369 int personal, int snoop, int sasl, char *saslmech, int tls,
370 const char *auth_svc)
371 {
372 int nmsgs, nbytes, status;
373
374 if (auth_svc == NULL) {
375 if (saslmech && ! strcasecmp(saslmech, "xoauth2")) {
376 adios (NULL, "must specify -authservice with -saslmech xoauth2");
377 }
378 } else {
379 if (user == NULL) {
380 adios (NULL, "must specify -user with -saslmech xoauth2");
381 }
382 }
383
384 /* open the POP connection */
385 if (pop_init (host, port, user, proxy, snoop, sasl, saslmech, tls,
386 auth_svc) == NOTOK
387 || pop_stat (&nmsgs, &nbytes) == NOTOK /* check for messages */
388 || pop_quit () == NOTOK) { /* quit POP connection */
389 inform("%s", response);
390 return 1;
391 }
392
393 if (nmsgs) {
394 if (notifysw & NT_MAIL) {
395 if (personal)
396 printf ("You have ");
397 else
398 printf ("%s has ", user);
399
400 printf ("%d message%s (%d bytes)",
401 nmsgs, PLURALS(nmsgs), nbytes);
402 }
403 else
404 notifysw = 0;
405
406 status = 0;
407 } else {
408 if (notifysw & NT_NMAI)
409 printf (personal ? "You don't %s%s" : "%s doesn't %s",
410 personal ? "" : user, "have any mail waiting");
411 else
412 notifysw = 0;
413 status = 1;
414 }
415 if (notifysw)
416 printf (" on %s\n", host);
417
418 return status;
419 }