[Samba] Machine account in Users home

Ed Plese ed at edplese.com
Thu Jan 18 14:39:38 GMT 2007


On Wed, Jan 17, 2007 at 06:23:19PM -0500, M Azer wrote:
> Hello, Am i suppose to see win machine names in users home? they are created
> when users login using their workstations
> here is my home directory
> 
> [cat at itbox CAD]$ ls -l
> total 20
> drwx------ 3 ad01$             Domain Computers   4096 Jan 17 17:57 ad01_
> drwx------ 3 administrator   Domain Users       4096 Jan 17 17:59
> administrator
> drwx------ 3 cat                  Domain Users       4096 Jan 17 18:07 cat
> drwx------ 4 mina.azer       Domain Users       4096 Jan 17 17:57 mina.azer
> drwx------ 3 vdc2$
> 
> as u can see the first and last item are machine accounts .. is that right??

If your users only connect to the server through Samba then you can
utilize the "root preexec" option to run a script as root when a user
connects to the share.

In this script you can create the directory if it does not exist, set
permissions, set quotas, etc.  More applicable to your question though
is that you can create the directory only if it's not for a machine
account (i.e. create it if it doesn't end with an "_").  

The script would look something like:


#!/bin/bash
USERNAME=$1
LOGFILE=/var/log/mkhomedir.log
HOME_PATH=/home/MYDOMAIN

echo $USERNAME | egrep -q "[^_]$"
if [ $? -ne 0 ]; then
  # ends with _ so just exit
  echo "not creating directory for $USERNAME" >> $LOGFILE
  exit 1
fi

if [ ! -d "$HOME_PATH/$USERNAME" ]; then
  mkdir "$HOME_PATH/$USERNAME"
  chown "$USERNAME" "$HOME_PATH/$USERNAME"
fi


When I've used this method I would modify the regular expression to only
match our specific naming pattern for usernames.  So for example it
would be something like "[a-z]+[0-9]*" instead of "[^_]$", but this obviously
varies from site to site.

Also, you'd probably want to add additional error checking and logging.
Remember that this runs as root so you really want to be careful with
what you're doing.

Once you have the script made up you can set it in smb.conf to be called when
someone connects to their home directory:

[homes]
  root preexec = /etc/samba/mkhomedir.sh '%U'

The %U represents the username but if you have more than one domain you
might want to use %D as well to pass the domain to the script too.  See
the smb.conf manpage for all other variables.


Ed Plese


More information about the samba mailing list