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>
54 #include <security/_pam_modutil.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"; /* THIS MODULE IS NOT THREAD SAFE */
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")) {
88 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 strncpy(SkelDir,*argv+5,sizeof(SkelDir));
93 SkelDir[sizeof(SkelDir)-1] = '\0';
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 && conv)
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));
135 if (retval == PAM_SUCCESS)
136 retval = PAM_BAD_ITEM; /* conv was NULL */
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,
179 const char *source, const char *dest)
185 /* Mention what is happening, if the notification fails that is OK */
186 if (snprintf(remark,sizeof(remark),"Creating directory '%s'.", dest) == -1)
187 return PAM_PERM_DENIED;
189 make_remark(pamh, ctrl, remark);
191 /* Create the new directory */
192 if (mkdir(dest,0700) != 0)
194 _log_err(LOG_DEBUG, "unable to create directory %s",dest);
195 return PAM_PERM_DENIED;
197 if (chmod(dest,0777 & (~UMask)) != 0 ||
198 chown(dest,pwd->pw_uid,pwd->pw_gid) != 0)
200 _log_err(LOG_DEBUG, "unable to change perms on directory %s",dest);
201 return PAM_PERM_DENIED;
204 /* See if we need to copy the skel dir over. */
205 if ((source == NULL) || (strlen(source) == 0))
210 /* Scan the directory */
214 _log_err(LOG_DEBUG, "unable to read directory %s",source);
215 return PAM_PERM_DENIED;
218 for (Dir = readdir(D); Dir != 0; Dir = readdir(D))
224 char newsource[PATH_MAX], newdest[PATH_MAX];
226 /* Skip some files.. */
227 if (strcmp(Dir->d_name,".") == 0 ||
228 strcmp(Dir->d_name,"..") == 0)
231 /* Determine what kind of file it is. */
232 snprintf(newsource,sizeof(newsource),"%s/%s",source,Dir->d_name);
233 if (lstat(newsource,&St) != 0)
236 /* We'll need the new file's name. */
237 snprintf(newdest,sizeof(newdest),"%s/%s",dest,Dir->d_name);
239 /* If it's a directory, recurse. */
240 if (S_ISDIR(St.st_mode))
242 int retval = create_homedir(pamh, ctrl, pwd, newsource, newdest);
243 if (retval != PAM_SUCCESS) {
250 /* If it's a symlink, create a new link. */
251 if (S_ISLNK(St.st_mode))
253 char pointed[PATH_MAX];
254 memset(pointed, 0, sizeof(pointed));
255 if(readlink(newsource, pointed, sizeof(pointed) - 1) != -1)
257 if(symlink(pointed, newdest) == 0)
259 if (lchown(newdest,pwd->pw_uid,pwd->pw_gid) != 0)
262 _log_err(LOG_DEBUG, "unable to chang perms on link %s",
264 return PAM_PERM_DENIED;
271 /* If it's not a regular file, it's probably not a good idea to create
272 * the new device node, FIFO, or whatever it is. */
273 if (!S_ISREG(St.st_mode))
278 /* Open the source file */
279 if ((SrcFd = open(newsource,O_RDONLY)) < 0 || fstat(SrcFd,&St) != 0)
282 _log_err(LOG_DEBUG, "unable to open src file %s",newsource);
283 return PAM_PERM_DENIED;
287 /* Open the dest file */
288 if ((DestFd = open(newdest,O_WRONLY | O_TRUNC | O_CREAT,0600)) < 0)
292 _log_err(LOG_DEBUG, "unable to open dest file %s",newdest);
293 return PAM_PERM_DENIED;
296 /* Set the proper ownership and permissions for the module. We make
297 the file a+w and then mask it with the set mask. This preseves
299 if (fchmod(DestFd,(St.st_mode | 0222) & (~UMask)) != 0 ||
300 fchown(DestFd,pwd->pw_uid,pwd->pw_gid) != 0)
305 _log_err(LOG_DEBUG, "unable to chang perms on copy %s",newdest);
306 return PAM_PERM_DENIED;
312 Res = _pammodutil_read(SrcFd,remark,sizeof(remark));
318 if (_pammodutil_write(DestFd,remark,Res) == Res)
322 /* If we get here, pammodutil_read returned a -1 or
323 _pammodutil_write returned something unexpected. */
327 _log_err(LOG_DEBUG, "unable to perform IO");
328 return PAM_PERM_DENIED;
339 /* --- authentication management functions (only) --- */
342 int pam_sm_open_session(pam_handle_t * pamh, int flags, int argc
347 const struct passwd *pwd;
350 /* Parse the flag values */
351 ctrl = _pam_parse(flags, argc, argv);
353 /* Determine the user name so we can get the home directory */
354 retval = pam_get_item(pamh, PAM_USER, (const void **) &user);
355 if (retval != PAM_SUCCESS || user == NULL || *user == '\0')
357 _log_err(LOG_NOTICE, "user unknown");
358 return PAM_USER_UNKNOWN;
361 /* Get the password entry */
362 pwd = _pammodutil_getpwnam (pamh, user);
365 D(("couldn't identify user %s", user));
366 return PAM_CRED_INSUFFICIENT;
369 /* Stat the home directory, if something exists then we assume it is
370 correct and return a success*/
371 if (stat(pwd->pw_dir,&St) == 0)
374 return create_homedir(pamh,ctrl,pwd,SkelDir,pwd->pw_dir);
379 int pam_sm_close_session(pam_handle_t * pamh, int flags, int argc
387 /* static module data */
388 struct pam_module _pam_mkhomedir_modstruct =
395 pam_sm_close_session,