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