0

I would need to create a script that daily checks if users have a home folder home/testuser and if not, then create one. The issue is that I would like to exclude built-in accounts from this example: Administrator, guest, samba and others. The problem occurs when users are created using aduc and not with zentyal.

I would use cron for the daily check, but lacking the knowledge how to exclude the built-in users.

Thanks in advance!

  • 2
    We typically do not take programming assignments at no charge here. Instead, if you have issues with an existing script, we can help take a look. – vanadium Apr 27 '22 at 10:17
  • What sort of problems are you ignoring? Why, in you environment, can users' `$HOME` directories vanish? What buggy tool are you using that fails to create `$HOME`? Is this an XY Problem? Read `man 5 passwd;man getent`. Can't you just keep a list of userids (or UIDs, or home directories) to not check? – waltinator Apr 27 '22 at 16:05
  • @waltinator The problem occurs when a user is created via ADUC. If the user is created directly in Zentyal then home folder is created. – Rikisorsa112 Apr 28 '22 at 06:55

1 Answers1

0

As has been said, we don't normally answer programming questions here, but this sounded like a fun five minute exercise.

This is offered to you without any indication that it works as you expect because my system always creates the users home, and no, I am not about to start playing around and messing up the passwd file to test this for you. So, run this in a text box first, or comment out the bits that do anything (mkdir and chown) and put in some echo commands.

All normal caveats apply: no support is offered or implied; the cheques in the post; and my favourite - its fix in the next release.

#!/bin/bash
# Check if users home directory exists 

while read -r LINE; do
  userID=`echo $LINE | awk -F: '{print $3}' `
  if [ "$userID" -ge "1000" -a "$userID" -lt "65534" ] ; then
    userName=`echo $LINE | awk -F: '{print $1}' `
    userHome=`echo $LINE | awk -F: '{print $6}' `
    userGroup=`echo $LINE | awk -F: '{print $4}' `

    if [ ! -d "$userHome" ]; then 
      echo found user $userID $userName is missing home:  $userHome
      mkdir $userHome 
      chown $userName:$userGroup $userHome 
    fi
  fi 
done < /etc/passwd
# Check if users home directory exists 

while read -r LINE; do
  userID=`echo $LINE | awk -F: '{print $3}' `
  if [ "$userID" -ge "1000" -a "$userID" -lt "65534" ] ; then
    userName=`echo $LINE | awk -F: '{print $1}' `
    userHome=`echo $LINE | awk -F: '{print $6}' `
    userGroup=`echo $LINE | awk -F: '{print $4}' `

    if [ ! -d "$userHome" ]; then 
      echo found user $userID $userName is missing home:  $userHome
      mkdir $userHome 
      chown $userName:$userGroup $userHome 
    fi
  fi 
done < /etc/passwd

Do be careful - you can do a lot of damage to your system running any sort of file as root (which this will need). TEST TEST AND TEST AGAIN.