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
28 * $FreeBSD: src/contrib/libpam/modules/pam_mkhomedir/pam_mkhomedir.c,v 1.1.1.1.2.2 2001/06/11 15:28:20 markm Exp $
29 * $DragonFly: src/contrib/libpam/modules/pam_mkhomedir/Attic/pam_mkhomedir.c,v 1.2 2003/06/17 04:24:03 dillon Exp $
32 /* I want snprintf dammit */
35 #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>
57 /* argument parsing */
58 #define MKHOMEDIR_DEBUG 020 /* keep quiet about things */
59 #define MKHOMEDIR_QUIET 040 /* keep quiet about things */
61 static unsigned int UMask = 0022;
62 static char SkelDir[BUFSIZ] = "/etc/skel";
65 static void _log_err(int err, const char *format, ...)
69 va_start(args, format);
70 openlog("PAM-mkhomedir", LOG_CONS|LOG_PID, LOG_AUTH);
71 vsyslog(err, format, args);
76 static int _pam_parse(int flags, int argc, const char **argv)
80 /* does the appliction require quiet? */
81 if ((flags & PAM_SILENT) == PAM_SILENT)
82 ctrl |= MKHOMEDIR_QUIET;
84 /* step through arguments */
85 for (; argc-- > 0; ++argv)
87 if (!strcmp(*argv, "silent"))
89 ctrl |= MKHOMEDIR_QUIET;
91 else if (!strncmp(*argv,"umask=",6))
92 UMask = strtol(*argv+6,0,0);
93 else if (!strncmp(*argv,"skel=",5))
94 strcpy(SkelDir,*argv+5);
97 _log_err(LOG_ERR, "unknown option; %s", *argv);
101 D(("ctrl = %o", ctrl));
105 /* This common function is used to send a message to the applications
106 conversion function. Our only use is to ask the application to print
107 an informative message that we are creating a home directory */
108 static int converse(pam_handle_t * pamh, int ctrl, int nargs
109 ,struct pam_message **message
110 ,struct pam_response **response)
113 struct pam_conv *conv;
115 D(("begin to converse"));
117 retval = pam_get_item(pamh, PAM_CONV, (const void **) &conv);
118 if (retval == PAM_SUCCESS)
121 retval = conv->conv(nargs, (const struct pam_message **) message
122 ,response, conv->appdata_ptr);
124 D(("returned from application's conversation function"));
126 if (retval != PAM_SUCCESS && (ctrl & MKHOMEDIR_DEBUG))
128 _log_err(LOG_DEBUG, "conversation failure [%s]"
129 ,pam_strerror(pamh, retval));
135 _log_err(LOG_ERR, "couldn't obtain coversation function [%s]"
136 ,pam_strerror(pamh, retval));
139 D(("ready to return from module conversation"));
141 return retval; /* propagate error status */
144 /* Ask the application to display a short text string for us. */
145 static int make_remark(pam_handle_t * pamh, int ctrl, const char *remark)
149 if ((ctrl & MKHOMEDIR_QUIET) != MKHOMEDIR_QUIET)
151 struct pam_message msg[1], *mesg[1];
152 struct pam_response *resp = NULL;
155 msg[0].msg_style = PAM_TEXT_INFO;
158 retval = converse(pamh, ctrl, 1, mesg, &resp);
163 _pam_drop_reply(resp, 1);
168 D(("keeping quiet"));
169 retval = PAM_SUCCESS;
172 D(("returning %s", pam_strerror(pamh, retval)));
176 /* Do the actual work of creating a home dir */
177 static int create_homedir(pam_handle_t * pamh, int ctrl,
178 const struct passwd *pwd)
184 /* Some scratch space */
185 remark = malloc(BUFSIZ);
188 D(("no memory for last login remark"));
192 /* Mention what is happening, if the notification fails that is OK */
193 if (snprintf(remark,BUFSIZ,"Creating home directory '%s'.",
195 return PAM_PERM_DENIED;
197 make_remark(pamh, ctrl, remark);
199 /* Crete the home directory */
200 if (mkdir(pwd->pw_dir,0700) != 0)
203 _log_err(LOG_DEBUG, "unable to create home directory %s",pwd->pw_dir);
204 return PAM_PERM_DENIED;
206 if (chmod(pwd->pw_dir,0777 & (~UMask)) != 0 ||
207 chown(pwd->pw_dir,pwd->pw_uid,pwd->pw_gid) != 0)
210 _log_err(LOG_DEBUG, "unable to chance perms on home directory %s",pwd->pw_dir);
211 return PAM_PERM_DENIED;
214 /* See if we need to copy the skel dir over. */
221 /* Scan the directory */
222 D = opendir(SkelDir);
226 _log_err(LOG_DEBUG, "unable to read directory %s",SkelDir);
227 return PAM_PERM_DENIED;
230 for (Dir = readdir(D); Dir != 0; Dir = readdir(D))
237 /* Skip some files.. */
238 if (strcmp(Dir->d_name,".") == 0 ||
239 strcmp(Dir->d_name,"..") == 0)
242 /* Check if it is a directory */
243 snprintf(remark,BUFSIZ,"%s/%s",SkelDir,Dir->d_name);
244 if (stat(remark,&St) != 0)
246 if (S_ISDIR(St.st_mode))
248 snprintf(remark,BUFSIZ,"%s/%s",pwd->pw_dir,Dir->d_name);
249 if (mkdir(remark,(St.st_mode | 0222) & (~UMask)) != 0 ||
250 chmod(remark,(St.st_mode | 0222) & (~UMask)) != 0 ||
251 chown(remark,pwd->pw_uid,pwd->pw_gid) != 0)
254 _log_err(LOG_DEBUG, "unable to change perms on copy %s",remark);
255 return PAM_PERM_DENIED;
260 /* Open the source file */
261 if ((SrcFd = open(remark,O_RDONLY)) < 0 || fstat(SrcFd,&St) != 0)
264 _log_err(LOG_DEBUG, "unable to open src file %s",remark);
265 return PAM_PERM_DENIED;
269 /* Open the dest file */
270 snprintf(remark,BUFSIZ,"%s/%s",pwd->pw_dir,Dir->d_name);
271 if ((DestFd = open(remark,O_WRONLY | O_TRUNC | O_CREAT,0600)) < 0)
275 _log_err(LOG_DEBUG, "unable to open dest file %s",remark);
276 return PAM_PERM_DENIED;
279 /* Set the proper ownership and permissions for the module. We make
280 the file a+w and then mask it with the set mask. This preseves
282 if (fchmod(DestFd,(St.st_mode | 0222) & (~UMask)) != 0 ||
283 fchown(DestFd,pwd->pw_uid,pwd->pw_gid) != 0)
286 _log_err(LOG_DEBUG, "unable to chang perms on copy %s",remark);
287 return PAM_PERM_DENIED;
293 Res = read(SrcFd,remark,BUFSIZ);
294 if (Res < 0 || write(DestFd,remark,Res) != Res)
299 _log_err(LOG_DEBUG, "unable to perform IO");
300 return PAM_PERM_DENIED;
312 /* --- authentication management functions (only) --- */
315 int pam_sm_open_session(pam_handle_t * pamh, int flags, int argc
320 const struct passwd *pwd;
323 /* Parse the flag values */
324 ctrl = _pam_parse(flags, argc, argv);
326 /* Determine the user name so we can get the home directory */
327 retval = pam_get_item(pamh, PAM_USER, (const void **) &user);
328 if (retval != PAM_SUCCESS || user == NULL || *user == '\0')
330 _log_err(LOG_NOTICE, "user unknown");
331 return PAM_USER_UNKNOWN;
334 /* Get the password entry */
335 pwd = getpwnam(user);
338 D(("couldn't identify user %s", user));
339 return PAM_CRED_INSUFFICIENT;
342 /* Stat the home directory, if something exists then we assume it is
343 correct and return a success*/
344 if (stat(pwd->pw_dir,&St) == 0)
347 return create_homedir(pamh,ctrl,pwd);
352 int pam_sm_close_session(pam_handle_t * pamh, int flags, int argc
360 /* static module data */
361 struct pam_module _pam_mkhomedir_modstruct =
368 pam_sm_close_session,