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
33 #include <sys/types.h>
46 * here, we make a definition for the externally accessible function
47 * in this file (this definition is required for static a module
48 * but strongly encouraged generally) it is used to instruct the
49 * modules include file to define the function prototypes.
52 #define PAM_SM_SESSION
54 #include <security/pam_modules.h>
55 #include <security/_pam_macros.h>
56 #include <security/_pam_modutil.h>
59 /* argument parsing */
60 #define MKHOMEDIR_DEBUG 020 /* keep quiet about things */
61 #define MKHOMEDIR_QUIET 040 /* keep quiet about things */
63 static unsigned int UMask = 0022;
64 static char SkelDir[BUFSIZ] = "/etc/skel"; /* THIS MODULE IS NOT THREAD SAFE */
68 _log_err (int err, const char *format, ...)
72 va_start(args, format);
73 openlog("PAM-mkhomedir", LOG_CONS|LOG_PID, LOG_AUTH);
74 vsyslog(err, format, args);
80 _pam_parse (int flags, int argc, const char **argv)
84 /* does the appliction require quiet? */
85 if ((flags & PAM_SILENT) == PAM_SILENT)
86 ctrl |= MKHOMEDIR_QUIET;
88 /* step through arguments */
89 for (; argc-- > 0; ++argv)
91 if (!strcmp(*argv, "silent")) {
92 ctrl |= MKHOMEDIR_QUIET;
93 } else if (!strncmp(*argv,"umask=",6)) {
94 UMask = strtol(*argv+6,0,0);
95 } else if (!strncmp(*argv,"skel=",5)) {
96 strncpy(SkelDir,*argv+5,sizeof(SkelDir));
97 SkelDir[sizeof(SkelDir)-1] = '\0';
99 _log_err(LOG_ERR, "unknown option; %s", *argv);
103 D(("ctrl = %o", ctrl));
107 /* This common function is used to send a message to the applications
108 conversion function. Our only use is to ask the application to print
109 an informative message that we are creating a home directory */
110 static int converse(pam_handle_t * pamh, int ctrl, int nargs
111 ,struct pam_message **message
112 ,struct pam_response **response)
115 const void *void_conv;
116 const struct pam_conv *conv;
118 D(("begin to converse"));
120 retval = pam_get_item(pamh, PAM_CONV, &void_conv);
121 conv = (const struct pam_conv *)void_conv;
122 if (retval == PAM_SUCCESS && conv)
125 retval = conv->conv(nargs, (const struct pam_message **) message
126 ,response, conv->appdata_ptr);
128 D(("returned from application's conversation function"));
130 if (retval != PAM_SUCCESS && (ctrl & MKHOMEDIR_DEBUG))
132 _log_err(LOG_DEBUG, "conversation failure [%s]"
133 ,pam_strerror(pamh, retval));
139 _log_err(LOG_ERR, "couldn't obtain coversation function [%s]"
140 ,pam_strerror(pamh, retval));
141 if (retval == PAM_SUCCESS)
142 retval = PAM_BAD_ITEM; /* conv was NULL */
145 D(("ready to return from module conversation"));
147 return retval; /* propagate error status */
150 /* Ask the application to display a short text string for us. */
152 make_remark (pam_handle_t *pamh, int ctrl, const char *remark)
156 if ((ctrl & MKHOMEDIR_QUIET) != MKHOMEDIR_QUIET)
158 struct pam_message msg[1], *mesg[1];
159 struct pam_response *resp = NULL;
162 msg[0].msg_style = PAM_TEXT_INFO;
165 retval = converse(pamh, ctrl, 1, mesg, &resp);
170 _pam_drop_reply(resp, 1);
175 D(("keeping quiet"));
176 retval = PAM_SUCCESS;
179 D(("returning %s", pam_strerror(pamh, retval)));
184 rec_mkdir (const char *dir, mode_t mode)
187 char *parent = strdup (dir);
192 cp = strrchr (parent, '/');
199 if (stat (parent, &st) == -1 && errno == ENOENT)
200 if (rec_mkdir (parent, mode) != 0)
209 if (mkdir (dir, mode) != 0 && errno != EEXIST)
215 /* Do the actual work of creating a home dir */
217 create_homedir (pam_handle_t * pamh, int ctrl,
218 const struct passwd *pwd,
219 const char *source, const char *dest)
224 int retval = PAM_AUTH_ERR;
226 /* Mention what is happening, if the notification fails that is OK */
227 if (snprintf(remark,sizeof(remark),"Creating directory '%s'.", dest) == -1)
228 return PAM_PERM_DENIED;
230 make_remark(pamh, ctrl, remark);
232 /* Create the new directory */
233 if (rec_mkdir (dest,0755) != 0)
235 _log_err(LOG_DEBUG, "unable to create directory %s",dest);
236 return PAM_PERM_DENIED;
239 /* See if we need to copy the skel dir over. */
240 if ((source == NULL) || (strlen(source) == 0))
242 retval = PAM_SUCCESS;
246 /* Scan the directory */
247 D = opendir (source);
250 _log_err(LOG_DEBUG, "unable to read directory %s",source);
251 retval = PAM_PERM_DENIED;
255 for (Dir = readdir(D); Dir != 0; Dir = readdir(D))
262 char *newsource = NULL, *newdest = NULL;
263 /* track length of buffers */
264 int nslen = 0, ndlen = 0;
265 int slen = strlen(source), dlen = strlen(dest);
267 char newsource[PATH_MAX], newdest[PATH_MAX];
270 /* Skip some files.. */
271 if (strcmp(Dir->d_name,".") == 0 ||
272 strcmp(Dir->d_name,"..") == 0)
275 /* Determine what kind of file it is. */
277 nslen = slen + strlen(Dir->d_name) + 2;
281 retval = PAM_BUF_ERR;
285 if ((newsource = malloc (nslen)) == NULL)
287 retval = PAM_BUF_ERR;
291 sprintf(newsource, "%s/%s", source, Dir->d_name);
293 snprintf(newsource,sizeof(newsource),"%s/%s",source,Dir->d_name);
296 if (lstat(newsource,&St) != 0)
308 /* We'll need the new file's name. */
310 ndlen = dlen + strlen(Dir->d_name)+2;
314 retval = PAM_BUF_ERR;
318 if ((newdest = malloc(ndlen)) == NULL)
321 retval = PAM_BUF_ERR;
325 sprintf (newdest, "%s/%s", dest, Dir->d_name);
327 snprintf (newdest,sizeof (newdest),"%s/%s",dest,Dir->d_name);
330 /* If it's a directory, recurse. */
331 if (S_ISDIR(St.st_mode))
333 retval = create_homedir (pamh, ctrl, pwd, newsource, newdest);
336 free(newsource); newsource = NULL;
337 free(newdest); newdest = NULL;
340 if (retval != PAM_SUCCESS)
348 /* If it's a symlink, create a new link. */
349 if (S_ISLNK(St.st_mode))
353 char *pointed = NULL;
358 pointed = (char *) malloc(size);
364 pointedlen = readlink (newsource, pointed, size);
365 if ( pointedlen < 0 ) break;
366 if ( pointedlen < size ) break;
371 if ( pointedlen < 0 )
374 pointed[pointedlen] = 0;
376 char pointed[PATH_MAX];
377 memset(pointed, 0, sizeof(pointed));
379 pointedlen = readlink(newsource, pointed, sizeof(pointed) - 1);
382 if ( pointedlen >= 0 ) {
383 if(symlink(pointed, newdest) == 0)
385 if (lchown(newdest,pwd->pw_uid,pwd->pw_gid) != 0)
388 _log_err(LOG_DEBUG, "unable to change perms on link %s",
395 return PAM_PERM_DENIED;
403 free(newsource); newsource = NULL;
404 free(newdest); newdest = NULL;
409 /* If it's not a regular file, it's probably not a good idea to create
410 * the new device node, FIFO, or whatever it is. */
411 if (!S_ISREG(St.st_mode))
414 free(newsource); newsource = NULL;
415 free(newdest); newdest = NULL;
420 /* Open the source file */
421 if ((SrcFd = open(newsource,O_RDONLY)) < 0 || fstat(SrcFd,&St) != 0)
424 _log_err(LOG_DEBUG, "unable to open src file %s",newsource);
427 free(newsource); newsource = NULL;
428 free(newdest); newdest = NULL;
431 return PAM_PERM_DENIED;
435 /* Open the dest file */
436 if ((DestFd = open(newdest,O_WRONLY | O_TRUNC | O_CREAT,0600)) < 0)
440 _log_err(LOG_DEBUG, "unable to open dest file %s",newdest);
443 free(newsource); newsource = NULL;
444 free(newdest); newdest = NULL;
446 return PAM_PERM_DENIED;
449 /* Set the proper ownership and permissions for the module. We make
450 the file a+w and then mask it with the set mask. This preseves
452 if (fchmod(DestFd,(St.st_mode | 0222) & (~UMask)) != 0 ||
453 fchown(DestFd,pwd->pw_uid,pwd->pw_gid) != 0)
458 _log_err(LOG_DEBUG, "unable to chang perms on copy %s",newdest);
461 free(newsource); newsource = NULL;
462 free(newdest); newdest = NULL;
465 return PAM_PERM_DENIED;
471 Res = _pammodutil_read(SrcFd,remark,sizeof(remark));
477 if (_pammodutil_write(DestFd,remark,Res) == Res)
481 /* If we get here, pammodutil_read returned a -1 or
482 _pammodutil_write returned something unexpected. */
486 _log_err(LOG_DEBUG, "unable to perform IO");
489 free(newsource); newsource = NULL;
490 free(newdest); newdest = NULL;
493 return PAM_PERM_DENIED;
500 free(newsource); newsource = NULL;
501 free(newdest); newdest = NULL;
507 retval = PAM_SUCCESS;
511 if (chmod(dest,0777 & (~UMask)) != 0 ||
512 chown(dest,pwd->pw_uid,pwd->pw_gid) != 0)
514 _log_err(LOG_DEBUG, "unable to change perms on directory %s",dest);
515 return PAM_PERM_DENIED;
521 /* --- authentication management functions (only) --- */
524 int pam_sm_open_session(pam_handle_t * pamh, int flags, int argc
529 const struct passwd *pwd;
532 /* Parse the flag values */
533 ctrl = _pam_parse(flags, argc, argv);
535 /* Determine the user name so we can get the home directory */
536 retval = pam_get_item(pamh, PAM_USER, &user);
537 if (retval != PAM_SUCCESS || user == NULL || *(const char *)user == '\0')
539 _log_err(LOG_NOTICE, "user unknown");
540 return PAM_USER_UNKNOWN;
543 /* Get the password entry */
544 pwd = _pammodutil_getpwnam (pamh, user);
547 D(("couldn't identify user %s", user));
548 return PAM_CRED_INSUFFICIENT;
551 /* Stat the home directory, if something exists then we assume it is
552 correct and return a success*/
553 if (stat(pwd->pw_dir,&St) == 0)
556 return create_homedir(pamh,ctrl,pwd,SkelDir,pwd->pw_dir);
561 int pam_sm_close_session (pam_handle_t * pamh UNUSED, int flags UNUSED,
562 int argc UNUSED, const char **argv UNUSED)
569 /* static module data */
570 struct pam_module _pam_mkhomedir_modstruct =
577 pam_sm_close_session,