mkhomedir_helper.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /* mkhomedir_helper - helper for pam_mkhomedir module
  2. Released under the GNU LGPL version 2 or later
  3. Copyright (c) Red Hat, Inc., 2009
  4. Originally written by Jason Gunthorpe <jgg@debian.org> Feb 1999
  5. Structure taken from pam_lastlogin by Andrew Morgan
  6. <morgan@parc.power.net> 1996
  7. */
  8. #include "config.h"
  9. #include <stdarg.h>
  10. #include <sys/types.h>
  11. #include <sys/stat.h>
  12. #include <fcntl.h>
  13. #include <unistd.h>
  14. #include <pwd.h>
  15. #include <errno.h>
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <dirent.h>
  20. #include <syslog.h>
  21. #include <security/pam_ext.h>
  22. #include <security/pam_modutil.h>
  23. static unsigned long u_mask = 0022;
  24. static unsigned long home_mode = 0;
  25. static char skeldir[BUFSIZ] = "/etc/skel";
  26. /* Do the actual work of creating a home dir */
  27. static int
  28. create_homedir(const struct passwd *pwd,
  29. const char *source, const char *dest)
  30. {
  31. char remark[BUFSIZ];
  32. DIR *d;
  33. struct dirent *dent;
  34. int retval = PAM_SESSION_ERR;
  35. /* Create the new directory */
  36. if (mkdir(dest, 0700) && errno != EEXIST)
  37. {
  38. pam_syslog(NULL, LOG_ERR, "unable to create directory %s: %m", dest);
  39. return PAM_PERM_DENIED;
  40. }
  41. /* See if we need to copy the skel dir over. */
  42. if ((source == NULL) || (strlen(source) == 0))
  43. {
  44. retval = PAM_SUCCESS;
  45. goto go_out;
  46. }
  47. /* Scan the directory */
  48. d = opendir(source);
  49. if (d == NULL)
  50. {
  51. pam_syslog(NULL, LOG_DEBUG, "unable to read directory %s: %m", source);
  52. retval = PAM_PERM_DENIED;
  53. goto go_out;
  54. }
  55. for (dent = readdir(d); dent != NULL; dent = readdir(d))
  56. {
  57. int srcfd;
  58. int destfd;
  59. int res;
  60. struct stat st;
  61. #ifndef PATH_MAX
  62. char *newsource = NULL, *newdest = NULL;
  63. /* track length of buffers */
  64. int nslen = 0, ndlen = 0;
  65. int slen = strlen(source), dlen = strlen(dest);
  66. #else
  67. char newsource[PATH_MAX], newdest[PATH_MAX];
  68. #endif
  69. /* Skip some files.. */
  70. if (strcmp(dent->d_name,".") == 0 ||
  71. strcmp(dent->d_name,"..") == 0)
  72. continue;
  73. /* Determine what kind of file it is. */
  74. #ifndef PATH_MAX
  75. nslen = slen + strlen(dent->d_name) + 2;
  76. if (nslen <= 0)
  77. {
  78. retval = PAM_BUF_ERR;
  79. goto go_out;
  80. }
  81. if ((newsource = malloc(nslen)) == NULL)
  82. {
  83. retval = PAM_BUF_ERR;
  84. goto go_out;
  85. }
  86. sprintf(newsource, "%s/%s", source, dent->d_name);
  87. #else
  88. snprintf(newsource, sizeof(newsource), "%s/%s", source, dent->d_name);
  89. #endif
  90. if (lstat(newsource, &st) != 0)
  91. #ifndef PATH_MAX
  92. {
  93. free(newsource);
  94. newsource = NULL;
  95. continue;
  96. }
  97. #else
  98. continue;
  99. #endif
  100. /* We'll need the new file's name. */
  101. #ifndef PATH_MAX
  102. ndlen = dlen + strlen(dent->d_name)+2;
  103. if (ndlen <= 0)
  104. {
  105. retval = PAM_BUF_ERR;
  106. goto go_out;
  107. }
  108. if ((newdest = malloc(ndlen)) == NULL)
  109. {
  110. free (newsource);
  111. retval = PAM_BUF_ERR;
  112. goto go_out;
  113. }
  114. sprintf (newdest, "%s/%s", dest, dent->d_name);
  115. #else
  116. snprintf (newdest, sizeof (newdest), "%s/%s", dest, dent->d_name);
  117. #endif
  118. /* If it's a directory, recurse. */
  119. if (S_ISDIR(st.st_mode))
  120. {
  121. retval = create_homedir(pwd, newsource, newdest);
  122. #ifndef PATH_MAX
  123. free(newsource); newsource = NULL;
  124. free(newdest); newdest = NULL;
  125. #endif
  126. if (retval != PAM_SUCCESS)
  127. {
  128. closedir(d);
  129. goto go_out;
  130. }
  131. continue;
  132. }
  133. /* If it's a symlink, create a new link. */
  134. if (S_ISLNK(st.st_mode))
  135. {
  136. int pointedlen = 0;
  137. #ifndef PATH_MAX
  138. char *pointed = NULL;
  139. {
  140. int size = 100;
  141. while (1) {
  142. pointed = malloc(size);
  143. if (pointed == NULL) {
  144. free(newsource);
  145. free(newdest);
  146. return PAM_BUF_ERR;
  147. }
  148. pointedlen = readlink(newsource, pointed, size);
  149. if (pointedlen < 0) break;
  150. if (pointedlen < size) break;
  151. free(pointed);
  152. size *= 2;
  153. }
  154. }
  155. if (pointedlen < 0)
  156. free(pointed);
  157. else
  158. pointed[pointedlen] = 0;
  159. #else
  160. char pointed[PATH_MAX];
  161. memset(pointed, 0, sizeof(pointed));
  162. pointedlen = readlink(newsource, pointed, sizeof(pointed) - 1);
  163. #endif
  164. if (pointedlen >= 0) {
  165. if(symlink(pointed, newdest) == 0)
  166. {
  167. if (lchown(newdest, pwd->pw_uid, pwd->pw_gid) != 0)
  168. {
  169. pam_syslog(NULL, LOG_DEBUG,
  170. "unable to change perms on link %s: %m", newdest);
  171. closedir(d);
  172. #ifndef PATH_MAX
  173. free(pointed);
  174. free(newsource);
  175. free(newdest);
  176. #endif
  177. return PAM_PERM_DENIED;
  178. }
  179. }
  180. #ifndef PATH_MAX
  181. free(pointed);
  182. #endif
  183. }
  184. #ifndef PATH_MAX
  185. free(newsource); newsource = NULL;
  186. free(newdest); newdest = NULL;
  187. #endif
  188. continue;
  189. }
  190. /* If it's not a regular file, it's probably not a good idea to create
  191. * the new device node, FIFO, or whatever it is. */
  192. if (!S_ISREG(st.st_mode))
  193. {
  194. #ifndef PATH_MAX
  195. free(newsource); newsource = NULL;
  196. free(newdest); newdest = NULL;
  197. #endif
  198. continue;
  199. }
  200. /* Open the source file */
  201. if ((srcfd = open(newsource, O_RDONLY)) < 0 || fstat(srcfd, &st) != 0)
  202. {
  203. pam_syslog(NULL, LOG_DEBUG,
  204. "unable to open or stat src file %s: %m", newsource);
  205. if (srcfd >= 0)
  206. close(srcfd);
  207. closedir(d);
  208. #ifndef PATH_MAX
  209. free(newsource); newsource = NULL;
  210. free(newdest); newdest = NULL;
  211. #endif
  212. return PAM_PERM_DENIED;
  213. }
  214. /* Open the dest file */
  215. if ((destfd = open(newdest, O_WRONLY | O_TRUNC | O_CREAT, 0600)) < 0)
  216. {
  217. pam_syslog(NULL, LOG_DEBUG,
  218. "unable to open dest file %s: %m", newdest);
  219. close(srcfd);
  220. closedir(d);
  221. #ifndef PATH_MAX
  222. free(newsource); newsource = NULL;
  223. free(newdest); newdest = NULL;
  224. #endif
  225. return PAM_PERM_DENIED;
  226. }
  227. /* Set the proper ownership and permissions for the module. We make
  228. the file a+w and then mask it with the set mask. This preserves
  229. execute bits */
  230. if (fchmod(destfd, (st.st_mode | 0222) & (~u_mask)) != 0 ||
  231. fchown(destfd, pwd->pw_uid, pwd->pw_gid) != 0)
  232. {
  233. pam_syslog(NULL, LOG_DEBUG,
  234. "unable to change perms on copy %s: %m", newdest);
  235. close(srcfd);
  236. close(destfd);
  237. closedir(d);
  238. #ifndef PATH_MAX
  239. free(newsource); newsource = NULL;
  240. free(newdest); newdest = NULL;
  241. #endif
  242. return PAM_PERM_DENIED;
  243. }
  244. /* Copy the file */
  245. do
  246. {
  247. res = pam_modutil_read(srcfd, remark, sizeof(remark));
  248. if (res == 0)
  249. continue;
  250. if (res > 0) {
  251. if (pam_modutil_write(destfd, remark, res) == res)
  252. continue;
  253. }
  254. /* If we get here, pam_modutil_read returned a -1 or
  255. pam_modutil_write returned something unexpected. */
  256. pam_syslog(NULL, LOG_DEBUG, "unable to perform IO: %m");
  257. close(srcfd);
  258. close(destfd);
  259. closedir(d);
  260. #ifndef PATH_MAX
  261. free(newsource); newsource = NULL;
  262. free(newdest); newdest = NULL;
  263. #endif
  264. return PAM_PERM_DENIED;
  265. }
  266. while (res != 0);
  267. close(srcfd);
  268. close(destfd);
  269. #ifndef PATH_MAX
  270. free(newsource); newsource = NULL;
  271. free(newdest); newdest = NULL;
  272. #endif
  273. }
  274. closedir(d);
  275. retval = PAM_SUCCESS;
  276. go_out:
  277. if (chmod(dest, 0777 & (~u_mask)) != 0 ||
  278. chown(dest, pwd->pw_uid, pwd->pw_gid) != 0)
  279. {
  280. pam_syslog(NULL, LOG_DEBUG,
  281. "unable to change perms on directory %s: %m", dest);
  282. return PAM_PERM_DENIED;
  283. }
  284. return retval;
  285. }
  286. static int
  287. create_homedir_helper(const struct passwd *_pwd,
  288. const char *_skeldir, const char *_homedir)
  289. {
  290. int retval = PAM_SESSION_ERR;
  291. retval = create_homedir(_pwd, _skeldir, _homedir);
  292. if (chmod(_homedir, home_mode) != 0)
  293. {
  294. pam_syslog(NULL, LOG_DEBUG,
  295. "unable to change perms on home directory %s: %m", _homedir);
  296. return PAM_PERM_DENIED;
  297. }
  298. return retval;
  299. }
  300. static int
  301. make_parent_dirs(char *dir, int make)
  302. {
  303. int rc = PAM_SUCCESS;
  304. char *cp = strrchr(dir, '/');
  305. struct stat st;
  306. if (!cp)
  307. return rc;
  308. if (cp != dir) {
  309. *cp = '\0';
  310. if (stat(dir, &st) && errno == ENOENT)
  311. rc = make_parent_dirs(dir, 1);
  312. *cp = '/';
  313. if (rc != PAM_SUCCESS)
  314. return rc;
  315. }
  316. if (make && mkdir(dir, 0755) && errno != EEXIST) {
  317. pam_syslog(NULL, LOG_ERR, "unable to create directory %s: %m", dir);
  318. return PAM_PERM_DENIED;
  319. }
  320. return rc;
  321. }
  322. int
  323. main(int argc, char *argv[])
  324. {
  325. struct passwd *pwd;
  326. struct stat st;
  327. char *eptr;
  328. if (argc < 2) {
  329. fprintf(stderr, "Usage: %s <username> [<umask> [<skeldir> [<home_mode>]]]\n", argv[0]);
  330. return PAM_SESSION_ERR;
  331. }
  332. pwd = getpwnam(argv[1]);
  333. if (pwd == NULL) {
  334. pam_syslog(NULL, LOG_ERR, "User unknown.");
  335. return PAM_USER_UNKNOWN;
  336. }
  337. if (argc >= 3) {
  338. errno = 0;
  339. u_mask = strtoul(argv[2], &eptr, 0);
  340. if (errno != 0 || *eptr != '\0') {
  341. pam_syslog(NULL, LOG_ERR, "Bogus umask value %s", argv[2]);
  342. return PAM_SESSION_ERR;
  343. }
  344. }
  345. if (argc >= 4) {
  346. if (strlen(argv[3]) >= sizeof(skeldir)) {
  347. pam_syslog(NULL, LOG_ERR, "Too long skeldir path.");
  348. return PAM_SESSION_ERR;
  349. }
  350. strcpy(skeldir, argv[3]);
  351. }
  352. if (argc >= 5) {
  353. errno = 0;
  354. home_mode = strtoul(argv[4], &eptr, 0);
  355. if (errno != 0 || *eptr != '\0') {
  356. pam_syslog(NULL, LOG_ERR, "Bogus home_mode value %s", argv[4]);
  357. return PAM_SESSION_ERR;
  358. }
  359. }
  360. if (home_mode == 0)
  361. home_mode = 0777 & ~u_mask;
  362. /* Stat the home directory, if something exists then we assume it is
  363. correct and return a success */
  364. if (stat(pwd->pw_dir, &st) == 0)
  365. return PAM_SUCCESS;
  366. if (make_parent_dirs(pwd->pw_dir, 0) != PAM_SUCCESS)
  367. return PAM_PERM_DENIED;
  368. return create_homedir_helper(pwd, skeldir, pwd->pw_dir);
  369. }