1 /* PAM Make Home Dir module
3 This module will create a users home directory if it does not exist
4 when the session begins. This allows users to be present in central
5 database (such as nis, kerb or ldap) without using a distributed
6 file system or pre-creating a large number of directories.
8 Here is a sample /etc/pam.d/login file for Debian GNU/Linux
11 auth requisite pam_securetty.so
12 auth sufficient pam_ldap.so
13 auth required pam_pwdb.so
14 auth optional pam_group.so
15 auth optional pam_mail.so
16 account requisite pam_time.so
17 account sufficient pam_ldap.so
18 account required pam_pwdb.so
19 session required pam_mkhomedir.so skel=/etc/skel/ umask=0022
20 session required pam_pwdb.so
21 session optional pam_lastlog.so
22 password required pam_pwdb.so
24 Released under the GNU LGPL version 2 or later
25 Originally written by Jason Gunthorpe <jgg@debian.org> Feb 1999
26 Structure taken from pam_lastlogin by Andrew Morgan
27 <morgan@parc.power.net> 1996
30 /* I want snprintf dammit */
36 #include <sys/types.h>
48 * here, we make a definition for the externally accessible function
49 * in this file (this definition is required for static a module
50 * but strongly encouraged generally) it is used to instruct the
51 * modules include file to define the function prototypes.
54 #define PAM_SM_SESSION
56 #include <security/pam_modules.h>
57 #include <security/_pam_macros.h>
58 #include <security/_pam_modutil.h>
61 /* argument parsing */
62 #define MKHOMEDIR_DEBUG 020 /* keep quiet about things */
63 #define MKHOMEDIR_QUIET 040 /* keep quiet about things */
65 static unsigned int UMask = 0022;
66 static char SkelDir[BUFSIZ] = "/etc/skel"; /* THIS MODULE IS NOT THREAD SAFE */
70 _log_err (int err, const char *format, ...)
74 va_start(args, format);
75 openlog("PAM-mkhomedir", LOG_CONS|LOG_PID, LOG_AUTH);
76 vsyslog(err, format, args);
82 _pam_parse (int flags, int argc, const char **argv)
86 /* does the appliction require quiet? */
87 if ((flags & PAM_SILENT) == PAM_SILENT)
88 ctrl |= MKHOMEDIR_QUIET;
90 /* step through arguments */
91 for (; argc-- > 0; ++argv)
93 if (!strcmp(*argv, "silent")) {
94 ctrl |= MKHOMEDIR_QUIET;
95 } else if (!strncmp(*argv,"umask=",6)) {
96 UMask = strtol(*argv+6,0,0);
97 } else if (!strncmp(*argv,"skel=",5)) {
98 strncpy(SkelDir,*argv+5,sizeof(SkelDir));
99 SkelDir[sizeof(SkelDir)-1] = '\0';
101 _log_err(LOG_ERR, "unknown option; %s", *argv);
105 D(("ctrl = %o", ctrl));
109 /* This common function is used to send a message to the applications
110 conversion function. Our only use is to ask the application to print
111 an informative message that we are creating a home directory */
112 static int converse(pam_handle_t * pamh, int ctrl, int nargs
113 ,struct pam_message **message
114 ,struct pam_response **response)
117 const void *void_conv;
118 const struct pam_conv *conv;
120 D(("begin to converse"));
122 retval = pam_get_item(pamh, PAM_CONV, &void_conv);
123 conv = (const struct pam_conv *)void_conv;
124 if (retval == PAM_SUCCESS && conv)
127 retval = conv->conv(nargs, (const struct pam_message **) message
128 ,response, conv->appdata_ptr);
130 D(("returned from application's conversation function"));
132 if (retval != PAM_SUCCESS && (ctrl & MKHOMEDIR_DEBUG))
134 _log_err(LOG_DEBUG, "conversation failure [%s]"
135 ,pam_strerror(pamh, retval));
141 _log_err(LOG_ERR, "couldn't obtain coversation function [%s]"
142 ,pam_strerror(pamh, retval));
143 if (retval == PAM_SUCCESS)
144 retval = PAM_BAD_ITEM; /* conv was NULL */
147 D(("ready to return from module conversation"));
149 return retval; /* propagate error status */
152 /* Ask the application to display a short text string for us. */
154 make_remark (pam_handle_t *pamh, int ctrl, const char *remark)
158 if ((ctrl & MKHOMEDIR_QUIET) != MKHOMEDIR_QUIET)
160 struct pam_message msg[1], *mesg[1];
161 struct pam_response *resp = NULL;
164 msg[0].msg_style = PAM_TEXT_INFO;
167 retval = converse(pamh, ctrl, 1, mesg, &resp);
172 _pam_drop_reply(resp, 1);
177 D(("keeping quiet"));
178 retval = PAM_SUCCESS;
181 D(("returning %s", pam_strerror(pamh, retval)));
186 rec_mkdir (const char *dir, mode_t mode)
189 char *parent = strdup (dir);
194 cp = strrchr (parent, '/');
201 if (stat (parent, &st) == -1 && errno == ENOENT)
202 if (rec_mkdir (parent, mode) != 0)
211 if (mkdir (dir, mode) != 0 && errno != EEXIST)
217 /* Do the actual work of creating a home dir */
219 create_homedir (pam_handle_t * pamh, int ctrl,
220 const struct passwd *pwd,
221 const char *source, const char *dest)
226 int retval = PAM_AUTH_ERR;
228 /* Mention what is happening, if the notification fails that is OK */
229 if (snprintf(remark,sizeof(remark),"Creating directory '%s'.", dest) == -1)
230 return PAM_PERM_DENIED;
232 make_remark(pamh, ctrl, remark);
234 /* Create the new directory */
235 if (rec_mkdir (dest,0755) != 0)
237 _log_err(LOG_DEBUG, "unable to create directory %s",dest);
238 return PAM_PERM_DENIED;
241 /* See if we need to copy the skel dir over. */
242 if ((source == NULL) || (strlen(source) == 0))
244 retval = PAM_SUCCESS;
248 /* Scan the directory */
249 D = opendir (source);
252 _log_err(LOG_DEBUG, "unable to read directory %s",source);
253 retval = PAM_PERM_DENIED;
257 for (Dir = readdir(D); Dir != 0; Dir = readdir(D))
264 char *newsource = NULL, *newdest = NULL;
265 /* track length of buffers */
266 int nslen = 0, ndlen = 0;
267 int slen = strlen(source), dlen = strlen(dest);
269 char newsource[PATH_MAX], newdest[PATH_MAX];
272 /* Skip some files.. */
273 if (strcmp(Dir->d_name,".") == 0 ||
274 strcmp(Dir->d_name,"..") == 0)
277 /* Determine what kind of file it is. */
279 nslen = slen + strlen(Dir->d_name) + 2;
283 retval = PAM_BUF_ERR;
287 if ((newsource = malloc (nslen)) == NULL)
289 retval = PAM_BUF_ERR;
293 sprintf(newsource, "%s/%s", source, Dir->d_name);
295 snprintf(newsource,sizeof(newsource),"%s/%s",source,Dir->d_name);
298 if (lstat(newsource,&St) != 0)
310 /* We'll need the new file's name. */
312 ndlen = dlen + strlen(Dir->d_name)+2;
316 retval = PAM_BUF_ERR;
320 if ((newdest = malloc(ndlen)) == NULL)
323 retval = PAM_BUF_ERR;
327 sprintf (newdest, "%s/%s", dest, Dir->d_name);
329 snprintf (newdest,sizeof (newdest),"%s/%s",dest,Dir->d_name);
332 /* If it's a directory, recurse. */
333 if (S_ISDIR(St.st_mode))
335 retval = create_homedir (pamh, ctrl, pwd, newsource, newdest);
338 free(newsource); newsource = NULL;
339 free(newdest); newdest = NULL;
342 if (retval != PAM_SUCCESS)
350 /* If it's a symlink, create a new link. */
351 if (S_ISLNK(St.st_mode))
355 char *pointed = NULL;
360 pointed = (char *) malloc(size);
366 pointedlen = readlink (newsource, pointed, size);
367 if ( pointedlen < 0 ) break;
368 if ( pointedlen < size ) break;
373 if ( pointedlen < 0 )
376 pointed[pointedlen] = 0;
378 char pointed[PATH_MAX];
379 memset(pointed, 0, sizeof(pointed));
381 pointedlen = readlink(newsource, pointed, sizeof(pointed) - 1);
384 if ( pointedlen >= 0 ) {
385 if(symlink(pointed, newdest) == 0)
387 if (lchown(newdest,pwd->pw_uid,pwd->pw_gid) != 0)
390 _log_err(LOG_DEBUG, "unable to change perms on link %s",
397 return PAM_PERM_DENIED;
405 free(newsource); newsource = NULL;
406 free(newdest); newdest = NULL;
411 /* If it's not a regular file, it's probably not a good idea to create
412 * the new device node, FIFO, or whatever it is. */
413 if (!S_ISREG(St.st_mode))
416 free(newsource); newsource = NULL;
417 free(newdest); newdest = NULL;
422 /* Open the source file */
423 if ((SrcFd = open(newsource,O_RDONLY)) < 0 || fstat(SrcFd,&St) != 0)
426 _log_err(LOG_DEBUG, "unable to open src file %s",newsource);
429 free(newsource); newsource = NULL;
430 free(newdest); newdest = NULL;
433 return PAM_PERM_DENIED;
437 /* Open the dest file */
438 if ((DestFd = open(newdest,O_WRONLY | O_TRUNC | O_CREAT,0600)) < 0)
442 _log_err(LOG_DEBUG, "unable to open dest file %s",newdest);
445 free(newsource); newsource = NULL;
446 free(newdest); newdest = NULL;
448 return PAM_PERM_DENIED;
451 /* Set the proper ownership and permissions for the module. We make
452 the file a+w and then mask it with the set mask. This preseves
454 if (fchmod(DestFd,(St.st_mode | 0222) & (~UMask)) != 0 ||
455 fchown(DestFd,pwd->pw_uid,pwd->pw_gid) != 0)
460 _log_err(LOG_DEBUG, "unable to chang perms on copy %s",newdest);
463 free(newsource); newsource = NULL;
464 free(newdest); newdest = NULL;
467 return PAM_PERM_DENIED;
473 Res = _pammodutil_read(SrcFd,remark,sizeof(remark));
479 if (_pammodutil_write(DestFd,remark,Res) == Res)
483 /* If we get here, pammodutil_read returned a -1 or
484 _pammodutil_write returned something unexpected. */
488 _log_err(LOG_DEBUG, "unable to perform IO");
491 free(newsource); newsource = NULL;
492 free(newdest); newdest = NULL;
495 return PAM_PERM_DENIED;
502 free(newsource); newsource = NULL;
503 free(newdest); newdest = NULL;
509 retval = PAM_SUCCESS;
513 if (chmod(dest,0777 & (~UMask)) != 0 ||
514 chown(dest,pwd->pw_uid,pwd->pw_gid) != 0)
516 _log_err(LOG_DEBUG, "unable to change perms on directory %s",dest);
517 return PAM_PERM_DENIED;
523 /* --- authentication management functions (only) --- */
526 int pam_sm_open_session(pam_handle_t * pamh, int flags, int argc
531 const struct passwd *pwd;
534 /* Parse the flag values */
535 ctrl = _pam_parse(flags, argc, argv);
537 /* Determine the user name so we can get the home directory */
538 retval = pam_get_item(pamh, PAM_USER, &user);
539 if (retval != PAM_SUCCESS || user == NULL || *(const char *)user == '\0')
541 _log_err(LOG_NOTICE, "user unknown");
542 return PAM_USER_UNKNOWN;
545 /* Get the password entry */
546 pwd = _pammodutil_getpwnam (pamh, user);
549 D(("couldn't identify user %s", user));
550 return PAM_CRED_INSUFFICIENT;
553 /* Stat the home directory, if something exists then we assume it is
554 correct and return a success*/
555 if (stat(pwd->pw_dir,&St) == 0)
558 return create_homedir(pamh,ctrl,pwd,SkelDir,pwd->pw_dir);
563 int pam_sm_close_session (pam_handle_t * pamh UNUSED, int flags UNUSED,
564 int argc UNUSED, const char **argv UNUSED)
571 /* static module data */
572 struct pam_module _pam_mkhomedir_modstruct =
579 pam_sm_close_session,