#!/usr/bin/perl # # genlogon.pl # # Perl script to generate user logon scripts on the fly, when users # connect from a Windows client. This script should be called from smb.conf # with the %U, %G, %L, %a, %H and %D parameters. I.e: # # root preexec = genlogon.pl %U %G %L %a %H %D # # The script generated will perform # the following: # # 1. Log the user connection to /var/log/samba/log.logon # 2. Set the PC's time to the Linux server time (which is maintained # daily to the National Institute of Standard's Atomic clock on the # internet. # 3. Connect the user's home drive to G: # 4. Connect network drives. # 5. Connect network printers. # # This scrip requires Samba 3.0.10 or later with "enable privileges = Yes", # and no usermap. # It also requires no profiles share, the profiles will be stored on the clients. $USER = $ARGV[0]; $GROUP = $ARGV[1]; $SERVER = $ARGV[2]; $OS = $ARGV[3]; $HOMEDIR = $ARGV[4]; $DOMAIN = $ARGV[5]; sub member_of_group { my $user = $_[0]; my $group = $_[1]; my $group_already = 0; # This line checks against the unix "groups" command, to see the secondary groups of a user. my @usergroups = split( /\s/, do { open my $groups, "-|", groups => $user; <$groups> } ); foreach (@usergroups) { if ($_ eq $group) { $group_already = 1; } } return $group_already; } # Set UNKNOWN to Lanman # if ($OS eq "UNKNOWN") { $OS = "Lanman"; } # Log client connection # ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); $year += 1900; open LOG, ">>/var/log/samba/log.logon"; printf LOG "%02d-%02d-%04d %02d:%02d:%02d $DOMAIN\\\\$SERVER\\$USER.$GROUP logged in using $OS home: $HOMEDIR\n", $mday, $mon + 1, $year, $hour, $min, $sec; # Older windows don't know about persistent but versions # based on NT technology do. Strange that WfWg is Ok. # if (($OS eq "Win95") || ($OS eq "OS2")) { $OPTS = ""; } elsif ($OS eq "Lanman") { $OPTS = " >NUL"; } elsif ($OS eq "WfWg") { $OPTS = " /PERSISTENT:NO >NUL"; } else { $OPTS = " /PERSISTENT:NO"; } # Create trash directory # system ("/usr/bin/mkdir -p /home/samba/trash/$USER"); system ("/usr/bin/chown $USER:$GROUP /home/samba/trash/$USER"); # Start generating logon script # open LOGON, ">/home/samba/logon/scripts/$USER.bat"; print LOGON "\@ECHO OFF\r\n"; print LOGON "NET TIME \\\\$SERVER /SET /YES\r\n"; # WfWg has a habbit of setting home persistent, clean # the shares before connecting. # if ($OS eq "WfWg") { print LOGON "NET USE G: /DELETE >NUL\r\n"; } if ($OS eq "Lanman") { print LOGON "NET USE /PERSISTENT:NO >NUL\r\n"; } # Samba doesn't force the home directory, set it here. # This isn't really a home directory, but a personal network storage. # print LOGON "NET USE G: \\\\$SERVER\\$USER$OPTS\r\n"; # Mount other shares # if (open (F_IN, "/etc/samba/shares.map")) { my $line; my $arr; while ($line = ) { if (substr($line, 0, 1) ne "#") { chomp ($line); @arr = split (/,/, $line); if ( (member_of_group ($USER, $arr[0])) || ((substr($arr[1], 0, 3) eq "LPT") && (($OS eq "WfWg") || ($OS eq "Lanman")))) { print LOGON "NET USE $arr[1] $arr[2]$OPTS\r\n"; printf LOG "%02d-%02d-%04d %02d:%02d:%02d NET USE $arr[1] $arr[2]\n", $mday, $mon + 1, $year, $hour, $min, $sec; } } } } # All done! Close the output files. close LOGON; close LOG; system ("/usr/bin/sync");