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>
44 * here, we make a definition for the externally accessible function
45 * in this file (this definition is required for static a module
46 * but strongly encouraged generally) it is used to instruct the
47 * modules include file to define the function prototypes.
50 #define PAM_SM_SESSION
52 #include <security/pam_modules.h>
53 #include <security/_pam_macros.h>
55 /* argument parsing */
56 #define MKHOMEDIR_DEBUG 020 /* keep quiet about things */
57 #define MKHOMEDIR_QUIET 040 /* keep quiet about things */
59 static unsigned int UMask = 0022;
60 static char SkelDir[BUFSIZ] = "/etc/skel";
63 static void _log_err(int err, const char *format, ...)
67 va_start(args, format);
68 openlog("PAM-mkhomedir", LOG_CONS|LOG_PID, LOG_AUTH);
69 vsyslog(err, format, args);
74 static int _pam_parse(int flags, int argc, const char **argv)
78 /* does the appliction require quiet? */
79 if ((flags & PAM_SILENT) == PAM_SILENT)
80 ctrl |= MKHOMEDIR_QUIET;
82 /* step through arguments */
83 for (; argc-- > 0; ++argv)
85 if (!strcmp(*argv, "silent"))
87 ctrl |= MKHOMEDIR_QUIET;
89 else if (!strncmp(*argv,"umask=",6))
90 UMask = strtol(*argv+6,0,0);
91 else if (!strncmp(*argv,"skel=",5))
92 strcpy(SkelDir,*argv+5);
95 _log_err(LOG_ERR, "unknown option; %s", *argv);
99 D(("ctrl = %o", ctrl));
103 /* This common function is used to send a message to the applications
104 conversion function. Our only use is to ask the application to print
105 an informative message that we are creating a home directory */
106 static int converse(pam_handle_t * pamh, int ctrl, int nargs
107 ,struct pam_message **message
108 ,struct pam_response **response)
111 struct pam_conv *conv;
113 D(("begin to converse"));
115 retval = pam_get_item(pamh, PAM_CONV, (const void **) &conv);
116 if (retval == PAM_SUCCESS)
119 retval = conv->conv(nargs, (const struct pam_message **) message
120 ,response, conv->appdata_ptr);
122 D(("returned from application's conversation function"));
124 if (retval != PAM_SUCCESS && (ctrl & MKHOMEDIR_DEBUG))
126 _log_err(LOG_DEBUG, "conversation failure [%s]"
127 ,pam_strerror(pamh, retval));
133 _log_err(LOG_ERR, "couldn't obtain coversation function [%s]"
134 ,pam_strerror(pamh, retval));
137 D(("ready to return from module conversation"));
139 return retval; /* propagate error status */
142 /* Ask the application to display a short text string for us. */
143 static int make_remark(pam_handle_t * pamh, int ctrl, const char *remark)
147 if ((ctrl & MKHOMEDIR_QUIET) != MKHOMEDIR_QUIET)
149 struct pam_message msg[1], *mesg[1];
150 struct pam_response *resp = NULL;
153 msg[0].msg_style = PAM_TEXT_INFO;
156 retval = converse(pamh, ctrl, 1, mesg, &resp);
161 _pam_drop_reply(resp, 1);
166 D(("keeping quiet"));
167 retval = PAM_SUCCESS;
170 D(("returning %s", pam_strerror(pamh, retval)));
174 /* Do the actual work of creating a home dir */
175 static int create_homedir(pam_handle_t * pamh, int ctrl,
176 const struct passwd *pwd)
182 /* Some scratch space */
183 remark = malloc(BUFSIZ);
186 D(("no memory for last login remark"));
190 /* Mention what is happening, if the notification fails that is OK */
191 if (snprintf(remark,BUFSIZ,"Creating home directory '%s'.",
193 return PAM_PERM_DENIED;
195 make_remark(pamh, ctrl, remark);
197 /* Crete the home directory */
198 if (mkdir(pwd->pw_dir,0700) != 0)
201 _log_err(LOG_DEBUG, "unable to create home directory %s",pwd->pw_dir);
202 return PAM_PERM_DENIED;
204 if (chmod(pwd->pw_dir,0777 & (~UMask)) != 0 ||
205 chown(pwd->pw_dir,pwd->pw_uid,pwd->pw_gid) != 0)
208 _log_err(LOG_DEBUG, "unable to chance perms on home directory %s",pwd->pw_dir);
209 return PAM_PERM_DENIED;
212 /* See if we need to copy the skel dir over. */
219 /* Scan the directory */
220 D = opendir(SkelDir);
224 _log_err(LOG_DEBUG, "unable to read directory %s",SkelDir);
225 return PAM_PERM_DENIED;
228 for (Dir = readdir(D); Dir != 0; Dir = readdir(D))
235 /* Skip some files.. */
236 if (strcmp(Dir->d_name,".") == 0 ||
237 strcmp(Dir->d_name,"..") == 0)
240 /* Check if it is a directory */
241 snprintf(remark,BUFSIZ,"%s/%s",SkelDir,Dir->d_name);
242 if (stat(remark,&St) != 0)
244 if (S_ISDIR(St.st_mode))
246 snprintf(remark,BUFSIZ,"%s/%s",pwd->pw_dir,Dir->d_name);
247 if (mkdir(remark,(St.st_mode | 0222) & (~UMask)) != 0 ||
248 chmod(remark,(St.st_mode | 0222) & (~UMask)) != 0 ||
249 chown(remark,pwd->pw_uid,pwd->pw_gid) != 0)
252 _log_err(LOG_DEBUG, "unable to change perms on copy %s",remark);
253 return PAM_PERM_DENIED;
258 /* Open the source file */
259 if ((SrcFd = open(remark,O_RDONLY)) < 0 || fstat(SrcFd,&St) != 0)
262 _log_err(LOG_DEBUG, "unable to open src file %s",remark);
263 return PAM_PERM_DENIED;
267 /* Open the dest file */
268 snprintf(remark,BUFSIZ,"%s/%s",pwd->pw_dir,Dir->d_name);
269 if ((DestFd = open(remark,O_WRONLY | O_TRUNC | O_CREAT,0600)) < 0)
273 _log_err(LOG_DEBUG, "unable to open dest file %s",remark);
274 return PAM_PERM_DENIED;
277 /* Set the proper ownership and permissions for the module. We make
278 the file a+w and then mask it with the set mask. This preseves
280 if (fchmod(DestFd,(St.st_mode | 0222) & (~UMask)) != 0 ||
281 fchown(DestFd,pwd->pw_uid,pwd->pw_gid) != 0)
284 _log_err(LOG_DEBUG, "unable to chang perms on copy %s",remark);
285 return PAM_PERM_DENIED;
291 Res = read(SrcFd,remark,BUFSIZ);
292 if (Res < 0 || write(DestFd,remark,Res) != Res)
297 _log_err(LOG_DEBUG, "unable to perform IO");
298 return PAM_PERM_DENIED;
310 /* --- authentication management functions (only) --- */
313 int pam_sm_open_session(pam_handle_t * pamh, int flags, int argc
318 const struct passwd *pwd;
321 /* Parse the flag values */
322 ctrl = _pam_parse(flags, argc, argv);
324 /* Determine the user name so we can get the home directory */
325 retval = pam_get_item(pamh, PAM_USER, (const void **) &user);
326 if (retval != PAM_SUCCESS || user == NULL || *user == '\0')
328 _log_err(LOG_NOTICE, "user unknown");
329 return PAM_USER_UNKNOWN;
332 /* Get the password entry */
333 pwd = getpwnam(user);
336 D(("couldn't identify user %s", user));
337 return PAM_CRED_INSUFFICIENT;
340 /* Stat the home directory, if something exists then we assume it is
341 correct and return a success*/
342 if (stat(pwd->pw_dir,&St) == 0)
345 return create_homedir(pamh,ctrl,pwd);
350 int pam_sm_close_session(pam_handle_t * pamh, int flags, int argc
358 /* static module data */
359 struct pam_module _pam_mkhomedir_modstruct =
366 pam_sm_close_session,