]> granicus.if.org Git - linux-pam/blob - modules/pam_mail/pam_mail.c
Relevant BUGIDs: 111648
[linux-pam] / modules / pam_mail / pam_mail.c
1 /* pam_mail module */
2
3 /*
4  * $Id$
5  *
6  * Written by Andrew Morgan <morgan@linux.kernel.org> 1996/3/11
7  * $HOME additions by David Kinchlea <kinch@kinch.ark.com> 1997/1/7
8  * mailhash additions by Chris Adams <cadams@ro.com> 1998/7/11
9  */
10
11 #define DEFAULT_MAIL_DIRECTORY    "/var/spool/mail"
12 #define MAIL_FILE_FORMAT          "%s%s/%s"
13 #define MAIL_ENV_NAME             "MAIL"
14 #define MAIL_ENV_FORMAT           MAIL_ENV_NAME "=%s"
15 #define YOUR_MAIL_VERBOSE_FORMAT  "You have %s mail in %s."
16 #define YOUR_MAIL_STANDARD_FORMAT "You have %smail."
17 #define NO_MAIL_STANDARD_FORMAT   "No mail." 
18
19 #define _BSD_SOURCE
20
21 #ifdef linux
22 # define _GNU_SOURCE
23 # include <features.h>
24 #endif
25
26 #include <ctype.h>
27 #include <pwd.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <syslog.h>
33 #include <sys/stat.h>
34 #include <sys/types.h>
35 #include <unistd.h>
36 #include <dirent.h>
37
38 #ifdef WANT_PWDB
39 #include <pwdb/pwdb_public.h>
40 #endif
41
42 /*
43  * here, we make a definition for the externally accessible function
44  * in this file (this definition is required for static a module
45  * but strongly encouraged generally) it is used to instruct the
46  * modules include file to define the function prototypes.
47  */
48
49 #define PAM_SM_SESSION
50 #define PAM_SM_AUTH
51
52 #include <security/pam_modules.h>
53 #include <security/_pam_macros.h>
54
55 /* some syslogging */
56
57 static void _log_err(int err, const char *format, ...)
58 {
59     va_list args;
60
61     va_start(args, format);
62     openlog("PAM-mail", LOG_CONS|LOG_PID, LOG_AUTH);
63     vsyslog(err, format, args);
64     va_end(args);
65     closelog();
66 }
67
68 /* argument parsing */
69
70 #define PAM_DEBUG_ARG           0x0001
71 #define PAM_NO_LOGIN            0x0002
72 #define PAM_LOGOUT_TOO          0x0004
73 #define PAM_NEW_MAIL_DIR        0x0010
74 #define PAM_MAIL_SILENT         0x0020
75 #define PAM_NO_ENV              0x0040
76 #define PAM_HOME_MAIL           0x0100
77 #define PAM_EMPTY_TOO           0x0200
78 #define PAM_STANDARD_MAIL       0x0400
79 #define PAM_QUIET_MAIL          0x1000
80
81 static int _pam_parse(int flags, int argc, const char **argv, char **maildir,
82                       int *hashcount)
83 {
84     int ctrl=0;
85
86     if (flags & PAM_SILENT) {
87         ctrl |= PAM_MAIL_SILENT;
88     }
89
90     *hashcount = 0;
91
92     /* step through arguments */
93     for (; argc-- > 0; ++argv) {
94
95         /* generic options */
96
97         if (!strcmp(*argv,"debug"))
98             ctrl |= PAM_DEBUG_ARG;
99         else if (!strcmp(*argv,"quiet"))
100             ctrl |= PAM_QUIET_MAIL;
101         else if (!strcmp(*argv,"standard"))
102             ctrl |= PAM_STANDARD_MAIL | PAM_EMPTY_TOO;
103         else if (!strncmp(*argv,"dir=",4)) {
104             *maildir = x_strdup(4+*argv);
105             if (*maildir != NULL) {
106                 D(("new mail directory: %s", *maildir));
107                 ctrl |= PAM_NEW_MAIL_DIR;
108             } else {
109                 _log_err(LOG_CRIT,
110                          "failed to duplicate mail directory - ignored");
111             }
112         } else if (!strncmp(*argv,"hash=",5)) {
113             char *ep = NULL;
114             *hashcount = strtol(*argv+5,&ep,10);
115             if (!ep || (*hashcount < 0)) {
116                 *hashcount = 0;
117             }
118         } else if (!strcmp(*argv,"close")) {
119             ctrl |= PAM_LOGOUT_TOO;
120         } else if (!strcmp(*argv,"nopen")) {
121             ctrl |= PAM_NO_LOGIN;
122         } else if (!strcmp(*argv,"noenv")) {
123             ctrl |= PAM_NO_ENV;
124         } else if (!strcmp(*argv,"empty")) {
125             ctrl |= PAM_EMPTY_TOO;
126         } else {
127             _log_err(LOG_ERR,"pam_parse: unknown option; %s",*argv);
128         }
129     }
130
131     if ((*hashcount != 0) && !(ctrl & PAM_NEW_MAIL_DIR)) {
132         *maildir = x_strdup(DEFAULT_MAIL_DIRECTORY);
133         ctrl |= PAM_NEW_MAIL_DIR;
134     }
135
136     return ctrl;
137 }
138
139 /* a front end for conversations */
140
141 static int converse(pam_handle_t *pamh, int ctrl, int nargs
142                     , struct pam_message **message
143                     , struct pam_response **response)
144 {
145     int retval;
146     struct pam_conv *conv;
147
148     D(("begin to converse"));
149
150     retval = pam_get_item( pamh, PAM_CONV, (const void **) &conv ) ; 
151     if ( retval == PAM_SUCCESS ) {
152
153         retval = conv->conv(nargs, ( const struct pam_message ** ) message
154                             , response, conv->appdata_ptr);
155
156         D(("returned from application's conversation function"));
157
158         if (retval != PAM_SUCCESS && (PAM_DEBUG_ARG & ctrl) ) {
159             _log_err(LOG_DEBUG, "conversation failure [%s]"
160                      , pam_strerror(pamh, retval));
161         }
162
163     } else {
164         _log_err(LOG_ERR, "couldn't obtain coversation function [%s]"
165                  , pam_strerror(pamh, retval));
166     }
167
168     D(("ready to return from module conversation"));
169
170     return retval;                  /* propagate error status */
171 }
172
173 static int get_folder(pam_handle_t *pamh, int ctrl,
174                       char **path_mail, char **folder_p, int hashcount)
175 {
176     int retval;
177     const char *user, *path;
178     char *folder;
179     const struct passwd *pwd=NULL;
180
181     retval = pam_get_user(pamh, &user, NULL);
182     if (retval != PAM_SUCCESS || user == NULL) {
183         _log_err(LOG_ERR, "no user specified");
184         return PAM_USER_UNKNOWN;
185     }
186
187     if (ctrl & PAM_NEW_MAIL_DIR) {
188         path = *path_mail;
189         if (*path == '~') {       /* support for $HOME delivery */
190             pwd = getpwnam(user);
191             if (pwd == NULL) {
192                 _log_err(LOG_ERR, "user [%s] unknown", user);
193                 _pam_overwrite(*path_mail);
194                 _pam_drop(*path_mail);
195                 return PAM_USER_UNKNOWN;
196             }
197             /*
198              * "~/xxx" and "~xxx" are treated as same
199              */
200             if (!*++path || (*path == '/' && !*++path)) {
201                 _log_err(LOG_ALERT, "badly formed mail path [%s]", *path_mail);
202                 _pam_overwrite(*path_mail);
203                 _pam_drop(*path_mail);
204                 return PAM_ABORT;
205             }
206             ctrl |= PAM_HOME_MAIL;
207             if (hashcount != 0) {
208                 _log_err(LOG_ALERT, "can't do hash= and home directory mail");
209             }
210         }
211     } else {
212         path = DEFAULT_MAIL_DIRECTORY;
213     }
214
215     /* put folder together */
216
217     if (ctrl & PAM_HOME_MAIL) {
218         folder = malloc(sizeof(MAIL_FILE_FORMAT)
219                         +strlen(pwd->pw_dir)+strlen(path));
220     } else {
221         folder = malloc(sizeof(MAIL_FILE_FORMAT)+strlen(path)+strlen(user)
222                         +2*hashcount);
223     }
224
225     if (folder != NULL) {
226         if (ctrl & PAM_HOME_MAIL) {
227             sprintf(folder, MAIL_FILE_FORMAT, pwd->pw_dir, "", path);
228         } else {
229             int i;
230             char *hash = malloc(2*hashcount+1);
231
232             if (hash) {
233                 for (i = 0; i < hashcount; i++) {
234                     hash[2*i] = '/';
235                     hash[2*i+1] = user[i];
236                 }
237                 hash[2*i] = '\0';
238                 sprintf(folder, MAIL_FILE_FORMAT, path, hash, user);
239                 _pam_overwrite(hash);
240                 _pam_drop(hash);
241             } else {
242                 sprintf(folder, "error");
243             }
244         }
245         D(("folder =[%s]", folder));
246     }
247
248     /* tidy up */
249
250     _pam_overwrite(*path_mail);
251     _pam_drop(*path_mail);
252     user = NULL;
253
254     if (folder == NULL) {
255         _log_err(LOG_CRIT, "out of memory for mail folder");
256         return PAM_BUF_ERR;
257     }
258
259     *folder_p = folder;
260     folder = NULL;
261
262     return PAM_SUCCESS;
263 }
264
265 static const char *get_mail_status(int ctrl, const char *folder)
266 {
267     const char *type = NULL;
268     static char dir[256];
269     struct stat mail_st;
270     struct dirent **namelist;
271     int i;
272
273     if (stat(folder, &mail_st) == 0) {
274         if (S_ISDIR(mail_st.st_mode)) { /* Assume Maildir format */
275             sprintf(dir, "%.250s/new", folder);
276             i = scandir(dir, &namelist, 0, alphasort);
277             if (i > 2) {
278                 type = "new";
279                 while (--i)
280                     free(namelist[i]);
281             } else {
282                 while (--i >= 0)
283                     free(namelist[i]);
284                 sprintf(dir, "%.250s/cur", folder);
285                 i = scandir(dir, &namelist, 0, alphasort);
286                 if (i > 2) {
287                     type = "old";
288                     while (--i)
289                         free(namelist[i]);
290                 } else if (ctrl & PAM_EMPTY_TOO) {
291                     while (--i >= 0)
292                         free(namelist[i]);
293                     type = "no";
294                 } else {
295                     type = NULL;
296                 }
297             }
298         } else {
299             if (mail_st.st_size > 0) {
300                 if (mail_st.st_atime < mail_st.st_mtime) /* new */
301                     type = (ctrl & PAM_STANDARD_MAIL) ? "new " : "new";
302                 else /* old */
303                     type = (ctrl & PAM_STANDARD_MAIL) ? "" : "old";
304             } else if (ctrl & PAM_EMPTY_TOO) {
305                 type = "no";
306             } else {
307                 type = NULL;
308             }
309         }
310     }
311
312     memset(dir, 0, 256);
313     memset(&mail_st, 0, sizeof(mail_st));
314     D(("user has %s mail in %s folder", type, folder));
315     return type;
316 }
317
318 static int report_mail(pam_handle_t *pamh, int ctrl
319                        , const char *type, const char *folder)
320 {
321     int retval;
322
323     if (!(ctrl & PAM_MAIL_SILENT) || ((ctrl & PAM_QUIET_MAIL) && strcmp(type, "new"))) {
324         char *remark;
325
326         if (ctrl & PAM_STANDARD_MAIL)
327             if (!strcmp(type, "no"))
328                 remark = malloc(strlen(NO_MAIL_STANDARD_FORMAT)+1);
329             else
330                 remark = malloc(strlen(YOUR_MAIL_STANDARD_FORMAT)+strlen(type)+1);
331         else
332             remark = malloc(strlen(YOUR_MAIL_VERBOSE_FORMAT)+strlen(type)+strlen(folder)+1);
333         if (remark == NULL) {
334             retval = PAM_BUF_ERR;
335         } else {
336             struct pam_message msg[1], *mesg[1];
337             struct pam_response *resp=NULL;
338
339             if (ctrl & PAM_STANDARD_MAIL)
340                 if (!strcmp(type, "no"))
341                     sprintf(remark, NO_MAIL_STANDARD_FORMAT);
342                 else
343                     sprintf(remark, YOUR_MAIL_STANDARD_FORMAT, type);
344             else
345                 sprintf(remark, YOUR_MAIL_VERBOSE_FORMAT, type, folder);
346
347             mesg[0] = &msg[0];
348             msg[0].msg_style = PAM_TEXT_INFO;
349             msg[0].msg = remark;
350
351             retval = converse(pamh, ctrl, 1, mesg, &resp);
352
353             _pam_overwrite(remark);
354             _pam_drop(remark);
355             if (resp)
356                 _pam_drop_reply(resp, 1);
357         }
358     } else {
359         D(("keeping quiet"));
360         retval = PAM_SUCCESS;
361     }
362
363     D(("returning %s", pam_strerror(pamh, retval)));
364     return retval;
365 }
366
367 static int _do_mail(pam_handle_t *, int, int, const char **, int);
368
369 /* --- authentication functions --- */
370
371 PAM_EXTERN
372 int pam_sm_authenticate(pam_handle_t *pamh,int flags,int argc,
373     const char **argv)
374 {
375     return PAM_IGNORE;
376 }
377
378 /* Checking mail as part of authentication */
379 PAM_EXTERN
380 int pam_sm_setcred(pam_handle_t *pamh, int flags, int argc,
381     const char **argv)
382 {
383     if (!(flags & (PAM_ESTABLISH_CRED|PAM_DELETE_CRED)))
384       return PAM_IGNORE;
385     return _do_mail(pamh,flags,argc,argv,(flags & PAM_ESTABLISH_CRED));
386 }
387
388 /* --- session management functions --- */
389
390 PAM_EXTERN
391 int pam_sm_close_session(pam_handle_t *pamh,int flags,int argc
392                          ,const char **argv)
393 {
394     return _do_mail(pamh,flags,argc,argv,0);;
395 }
396
397 /* Checking mail as part of the session management */
398 PAM_EXTERN
399 int pam_sm_open_session(pam_handle_t *pamh, int flags, int argc,
400     const char **argv)
401 {
402     return _do_mail(pamh,flags,argc,argv,1);
403 }
404
405
406 /* --- The Beaf (Tm) --- */
407
408 static int _do_mail(pam_handle_t *pamh, int flags, int argc,
409     const char **argv, int est)
410 {
411     int retval, ctrl, hashcount;
412     char *path_mail=NULL, *folder;
413     const char *type;
414
415     /*
416      * this module (un)sets the MAIL environment variable, and checks if
417      * the user has any new mail.
418      */
419
420     ctrl = _pam_parse(flags, argc, argv, &path_mail, &hashcount);
421
422     /* Do we have anything to do? */
423
424     if (flags & PAM_SILENT)
425         return PAM_SUCCESS;
426
427     /* which folder? */
428
429     retval = get_folder(pamh, ctrl, &path_mail, &folder, hashcount);
430     if (retval != PAM_SUCCESS) {
431         D(("failed to find folder"));
432         return retval;
433     }
434
435     /* set the MAIL variable? */
436
437     if (!(ctrl & PAM_NO_ENV) && est) {
438         char *tmp;
439
440         tmp = malloc(strlen(folder)+sizeof(MAIL_ENV_FORMAT));
441         if (tmp != NULL) {
442             sprintf(tmp, MAIL_ENV_FORMAT, folder);
443             D(("setting env: %s", tmp));
444             retval = pam_putenv(pamh, tmp);
445             _pam_overwrite(tmp);
446             _pam_drop(tmp);
447             if (retval != PAM_SUCCESS) {
448                 _pam_overwrite(folder);
449                 _pam_drop(folder);
450                 _log_err(LOG_CRIT, "unable to set " MAIL_ENV_NAME " variable");
451                 return retval;
452             }
453         } else {
454             _log_err(LOG_CRIT, "no memory for " MAIL_ENV_NAME " variable");
455             _pam_overwrite(folder);
456             _pam_drop(folder);
457             return retval;
458         }
459     } else {
460         D(("not setting " MAIL_ENV_NAME " variable"));
461     }
462
463     /*
464      * OK. we've got the mail folder... what about its status?
465      */
466
467     if ((est && !(ctrl & PAM_NO_LOGIN))
468         || (!est && (ctrl & PAM_LOGOUT_TOO))) {
469         type = get_mail_status(ctrl, folder);
470         if (type != NULL) {
471             retval = report_mail(pamh, ctrl, type, folder);
472             type = NULL;
473         }
474     }
475     
476     /* Delete environment variable? */  
477     if (!est)
478         (void) pam_putenv(pamh, MAIL_ENV_NAME);
479
480     _pam_overwrite(folder); /* clean up */
481     _pam_drop(folder);
482
483     /* indicate success or failure */
484
485     return retval;
486 }
487
488 #ifdef PAM_STATIC
489
490 /* static module data */
491
492 struct pam_module _pam_mail_modstruct = {
493      "pam_mail",
494      pam_sm_authenticate,
495      pam_sm_setcred,
496      NULL,
497      pam_sm_open_session,
498      pam_sm_close_session,
499      NULL,
500 };
501
502 #endif
503
504 /* end of module definition */