第一次透過 PAM_LDAP 登入時,可以使用 PAM_MKHOMEDIR 自動創建使用者家目錄,但是原本的程式有缺陷,不能遞迴地建立目錄,只好自己 patch。
例:若 /home 存在,可以建立 /home/sars 目錄,但無法建立 /home/student/sars。
從 /usr/ports/security/pam_mkhomedir/work/pam_mkhomedir-0.1 修改:
79,80c79,80
< if (mkdir(dir, mode) != 0 && errno != EEXIST) {
< PAM_LOG("mkdir(%s)", dir);
---
> if (opendir(dir)!=NULL) {
> /* exist */
86a87,105
>
> /* create DIR recursively */
> int index = 0;
> char *tempstr = (void*)malloc(strlen(dir));
> strcpy(tempstr,dir);
>
> for (index = 1; tempstr[index] != '\0'; index++) {
> /* Create the new directory */
> if ( tempstr[index] == '/' ) {
> tempstr[index] = '\0';
> mkdir(tempstr, mode);
> chown(tempstr, 0, 0);
> tempstr[index] = '/';
> }
> }
>
> mkdir(dir, mode);
> free(tempstr);
> /* end of modify */