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 */
33 #include <sys/types.h>
45 * here, we make a definition for the externally accessible function
46 * in this file (this definition is required for static a module
47 * but strongly encouraged generally) it is used to instruct the
48 * modules include file to define the function prototypes.
51 #define PAM_SM_SESSION
53 #include <security/pam_modules.h>
54 #include <security/_pam_macros.h>
55 #include <security/_pam_modutil.h>
58 /* argument parsing */
59 #define MKHOMEDIR_DEBUG 020 /* keep quiet about things */
60 #define MKHOMEDIR_QUIET 040 /* keep quiet about things */
62 static unsigned int UMask = 0022;
63 static char SkelDir[BUFSIZ] = "/etc/skel"; /* THIS MODULE IS NOT THREAD SAFE */
67 _log_err (int err, const char *format, ...)
71 va_start(args, format);
72 openlog("PAM-mkhomedir", LOG_CONS|LOG_PID, LOG_AUTH);
73 vsyslog(err, format, args);
79 _pam_parse (int flags, int argc, const char **argv)
83 /* does the appliction require quiet? */
84 if ((flags & PAM_SILENT) == PAM_SILENT)
85 ctrl |= MKHOMEDIR_QUIET;
87 /* step through arguments */
88 for (; argc-- > 0; ++argv)
90 if (!strcmp(*argv, "silent")) {
91 ctrl |= MKHOMEDIR_QUIET;
92 } else if (!strncmp(*argv,"umask=",6)) {
93 UMask = strtol(*argv+6,0,0);
94 } else if (!strncmp(*argv,"skel=",5)) {
95 strncpy(SkelDir,*argv+5,sizeof(SkelDir));
96 SkelDir[sizeof(SkelDir)-1] = '\0';
98 _log_err(LOG_ERR, "unknown option; %s", *argv);
102 D(("ctrl = %o", ctrl));
106 /* This common function is used to send a message to the applications
107 conversion function. Our only use is to ask the application to print
108 an informative message that we are creating a home directory */
109 static int converse(pam_handle_t * pamh, int ctrl, int nargs
110 ,struct pam_message **message
111 ,struct pam_response **response)
114 const void *void_conv;
115 const struct pam_conv *conv;
117 D(("begin to converse"));
119 retval = pam_get_item(pamh, PAM_CONV, &void_conv);
120 conv = (const struct pam_conv *)void_conv;
121 if (retval == PAM_SUCCESS && conv)
124 retval = conv->conv(nargs, (const struct pam_message **) message
125 ,response, conv->appdata_ptr);
127 D(("returned from application's conversation function"));
129 if (retval != PAM_SUCCESS && (ctrl & MKHOMEDIR_DEBUG))
131 _log_err(LOG_DEBUG, "conversation failure [%s]"
132 ,pam_strerror(pamh, retval));
138 _log_err(LOG_ERR, "couldn't obtain coversation function [%s]"
139 ,pam_strerror(pamh, retval));
140 if (retval == PAM_SUCCESS)
141 retval = PAM_BAD_ITEM; /* conv was NULL */
144 D(("ready to return from module conversation"));
146 return retval; /* propagate error status */
149 /* Ask the application to display a short text string for us. */
151 make_remark (pam_handle_t *pamh, int ctrl, const char *remark)
155 if ((ctrl & MKHOMEDIR_QUIET) != MKHOMEDIR_QUIET)
157 struct pam_message msg[1], *mesg[1];
158 struct pam_response *resp = NULL;
161 msg[0].msg_style = PAM_TEXT_INFO;
164 retval = converse(pamh, ctrl, 1, mesg, &resp);
169 _pam_drop_reply(resp, 1);
174 D(("keeping quiet"));
175 retval = PAM_SUCCESS;
178 D(("returning %s", pam_strerror(pamh, retval)));
183 rec_mkdir (const char *dir, int mode)
186 char *parent = strdup (dir);
191 cp = strrchr (parent, '/');
198 if (stat (parent, &st) == -1 && errno == ENOENT)
199 if (rec_mkdir (parent, mode) != 0)
208 if (mkdir (dir, mode) != 0 && errno != EEXIST)
214 /* Do the actual work of creating a home dir */
216 create_homedir (pam_handle_t * pamh, int ctrl,
217 const struct passwd *pwd,
218 const char *source, const char *dest)
223 int retval = PAM_AUTH_ERR;
225 /* Mention what is happening, if the notification fails that is OK */
226 if (snprintf(remark,sizeof(remark),"Creating directory '%s'.", dest) == -1)
227 return PAM_PERM_DENIED;
229 make_remark(pamh, ctrl, remark);
231 /* Create the new directory */
232 if (rec_mkdir (dest,0755) != 0)
234 _log_err(LOG_DEBUG, "unable to create directory %s",dest);
235 return PAM_PERM_DENIED;
238 /* See if we need to copy the skel dir over. */
239 if ((source == NULL) || (strlen(source) == 0))
241 retval = PAM_SUCCESS;
245 /* Scan the directory */
246 D = opendir (source);
249 _log_err(LOG_DEBUG, "unable to read directory %s",source);
250 retval = PAM_PERM_DENIED;
254 for (Dir = readdir(D); Dir != 0; Dir = readdir(D))
261 char *newsource = NULL, *newdest = NULL;
262 /* track length of buffers */
263 int nslen = 0, ndlen = 0;
264 int slen = strlen(source), dlen = strlen(dest);
266 char newsource[PATH_MAX], newdest[PATH_MAX];
269 /* Skip some files.. */
270 if (strcmp(Dir->d_name,".") == 0 ||
271 strcmp(Dir->d_name,"..") == 0)
274 /* Determine what kind of file it is. */
276 nslen = slen + strlen(Dir->d_name) + 2;
280 retval = PAM_BUF_ERR;
284 if ((newsource = malloc (nslen)) == NULL)
286 retval = PAM_BUF_ERR;
290 sprintf(newsource, "%s/%s", source, Dir->d_name);
292 snprintf(newsource,sizeof(newsource),"%s/%s",source,Dir->d_name);
295 if (lstat(newsource,&St) != 0)
307 /* We'll need the new file's name. */
309 ndlen = dlen + strlen(Dir->d_name)+2;
313 retval = PAM_BUF_ERR;
317 if ((newdest = malloc(ndlen)) == NULL)
320 retval = PAM_BUF_ERR;
324 sprintf (newdest, "%s/%s", dest, Dir->d_name);
326 snprintf (newdest,sizeof (newdest),"%s/%s",dest,Dir->d_name);
329 /* If it's a directory, recurse. */
330 if (S_ISDIR(St.st_mode))
332 retval = create_homedir (pamh, ctrl, pwd, newsource, newdest);
335 free(newsource); newsource = NULL;
336 free(newdest); newdest = NULL;
339 if (retval != PAM_SUCCESS)
347 /* If it's a symlink, create a new link. */
348 if (S_ISLNK(St.st_mode))
352 char *pointed = NULL;
357 pointed = (char *) malloc(size);
363 pointedlen = readlink (newsource, pointed, size);
364 if ( pointedlen < 0 ) break;
365 if ( pointedlen < size ) break;
370 if ( pointedlen < 0 )
373 pointed[pointedlen] = 0;
375 char pointed[PATH_MAX];
376 memset(pointed, 0, sizeof(pointed));
378 pointedlen = readlink(newsource, pointed, sizeof(pointed) - 1);
381 if ( pointedlen >= 0 ) {
382 if(symlink(pointed, newdest) == 0)
384 if (lchown(newdest,pwd->pw_uid,pwd->pw_gid) != 0)
387 _log_err(LOG_DEBUG, "unable to change perms on link %s",
394 return PAM_PERM_DENIED;
402 free(newsource); newsource = NULL;
403 free(newdest); newdest = NULL;
408 /* If it's not a regular file, it's probably not a good idea to create
409 * the new device node, FIFO, or whatever it is. */
410 if (!S_ISREG(St.st_mode))
413 free(newsource); newsource = NULL;
414 free(newdest); newdest = NULL;
419 /* Open the source file */
420 if ((SrcFd = open(newsource,O_RDONLY)) < 0 || fstat(SrcFd,&St) != 0)
423 _log_err(LOG_DEBUG, "unable to open src file %s",newsource);
426 free(newsource); newsource = NULL;
427 free(newdest); newdest = NULL;
430 return PAM_PERM_DENIED;
434 /* Open the dest file */
435 if ((DestFd = open(newdest,O_WRONLY | O_TRUNC | O_CREAT,0600)) < 0)
439 _log_err(LOG_DEBUG, "unable to open dest file %s",newdest);
442 free(newsource); newsource = NULL;
443 free(newdest); newdest = NULL;
445 return PAM_PERM_DENIED;
448 /* Set the proper ownership and permissions for the module. We make
449 the file a+w and then mask it with the set mask. This preseves
451 if (fchmod(DestFd,(St.st_mode | 0222) & (~UMask)) != 0 ||
452 fchown(DestFd,pwd->pw_uid,pwd->pw_gid) != 0)
457 _log_err(LOG_DEBUG, "unable to chang perms on copy %s",newdest);
460 free(newsource); newsource = NULL;
461 free(newdest); newdest = NULL;
464 return PAM_PERM_DENIED;
470 Res = _pammodutil_read(SrcFd,remark,sizeof(remark));
476 if (_pammodutil_write(DestFd,remark,Res) == Res)
480 /* If we get here, pammodutil_read returned a -1 or
481 _pammodutil_write returned something unexpected. */
485 _log_err(LOG_DEBUG, "unable to perform IO");
488 free(newsource); newsource = NULL;
489 free(newdest); newdest = NULL;
492 return PAM_PERM_DENIED;
499 free(newsource); newsource = NULL;
500 free(newdest); newdest = NULL;
506 retval = PAM_SUCCESS;
510 if (chmod(dest,0777 & (~UMask)) != 0 ||
511 chown(dest,pwd->pw_uid,pwd->pw_gid) != 0)
513 _log_err(LOG_DEBUG, "unable to change perms on directory %s",dest);
514 return PAM_PERM_DENIED;
520 /* --- authentication management functions (only) --- */
523 int pam_sm_open_session(pam_handle_t * pamh, int flags, int argc
528 const struct passwd *pwd;
531 /* Parse the flag values */
532 ctrl = _pam_parse(flags, argc, argv);
534 /* Determine the user name so we can get the home directory */
535 retval = pam_get_item(pamh, PAM_USER, &user);
536 if (retval != PAM_SUCCESS || user == NULL || *(const char *)user == '\0')
538 _log_err(LOG_NOTICE, "user unknown");
539 return PAM_USER_UNKNOWN;
542 /* Get the password entry */
543 pwd = _pammodutil_getpwnam (pamh, user);
546 D(("couldn't identify user %s", user));
547 return PAM_CRED_INSUFFICIENT;
550 /* Stat the home directory, if something exists then we assume it is
551 correct and return a success*/
552 if (stat(pwd->pw_dir,&St) == 0)
555 return create_homedir(pamh,ctrl,pwd,SkelDir,pwd->pw_dir);
560 int pam_sm_close_session(pam_handle_t * pamh, int flags, int argc
568 /* static module data */
569 struct pam_module _pam_mkhomedir_modstruct =
576 pam_sm_close_session,