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