Macintosh Logon Scripts

Here is a set of Mac logon scripts I wrote for a Windows/AD environment. There are a set of plist files that launch perl scripts and different stages. The logon scripts are local and self-update from the sysvol directory.

edu.fitsuny.mac_logon_init.plist





	Label
	edu.fitsuny.edu.mac_system_logon
	ProgramArguments
	
		/etc/etsscripts/mac_logon_init.pl
	
	RunAtLoad
	


edu.fitsuny.mac_user_logon.plist





	Label
	edu.fitsuny.mac_user_logon
	ProgramArguments
	
		/etc/etsscripts/mac_user_logon.pl
	
	RunAtLoad
	


mac_system_logon.pl

#!/usr/bin/perl -l
#Please make your function a sub and then call it.
#FYI: an error here will break the entire script
open (LOG, '>>/tmp/logon_system.txt');
print LOG "Running System Hook V1.0 \n";

#remember with admitmac the username might be @fitsuny.edu
$username = $ARGV[0];


#updates the loader.pl just in case we make changes there.
updateloader();

sub updateloader
{
	system('smbclient //fit-c108-01/netlogon Password -U mac_logon -c \'get \\Macintosh\\installer\\mac_logon_init.pl /etc/etsscripts/mac_logon_init.pl\'');
	if ($? != 0)
	{ #try dc 1
		system('smbclient //fit-c108-02/netlogon Password -U mac_logon -c \'get \\Macintosh\\installer\\mac_logon_init.pl /etc/etsscripts/mac_logon_init.pl\'');
	if ($? != 0)
	{# failed again print error
		print LOG 'SYSTEM: Cannot download loader.pl script\n';
	}

	chmod(0755,'/etc/etsscripts/mac_logon_init.pl');
}
}
#!/usr/bin/perl -l
#Please make your process a sub and then call it.
#FYI: an error here will break the entire script
#This runs at user the user profie is in ~/ || mkdir() does not understand ~/
#
open (LOG, '>/tmp/logon_user.txt');
print LOG "Running User Hook V1.0 \n";

#remember with admitmac the username might be @fitsuny.edu

my $username = $ENV{'USER'};
my $profiledir = $ENV{'HOME'};

if ( $username =~ m/[@]/ )
{
	my @tmpusername = split(/@/,$username);
        $username = $tmpusername[0];
}
print LOG "USER: username " . $username . "\n";

#system test script, an example of giving the user a GUI popup box from a perl script
#system('osascript -l AppleScript -e \'tell Application "Finder" to display dialog "Call Frank"\'');

#get Prirole once
$usertype = get_prirole();
print LOG "USER: using prirole " . $usertype . "\n";

disable_dsstore();
mount_winfs();
#emgmesg();

#their profile is in Users which means they are not using admit mac
if ($profiledir =~ m/\/Users\//)
{
	mount_stanNDrive();	
}



sub disable_dsstore
{
	#this will disable .DS_STORE on network volumes
	#This will stop some weird bugs

	system('defaults write com.apple.desktopservices DSDontWriteNetworkStores true');
}

sub mount_stanNDrive
{
	use Net::LDAP;

	$ldap = Net::LDAP->new ('fit-c108-01') or die "$@";
	$ad = $ldap->bind('cn=ldapsearch,ou=fitusers,dc=fitsuny,dc=edu', password => 'password);
	$ad = $ldap->search (base => 'ou=FitUsers,dc=fitsuny,dc=edu',
        	                filter => "(&(cn=" . $username . "))",
        	                attrs => ['homeDirectory'],
        	                );

	$ad->code && die $ad->error;


	$winHomeDir =  $ad->entry->get_value('homeDirectory');
	$ad = $ldap->unbind;

	$winHomeDir =~ s/\\\\/smb:\/\//;
	$winHomeDir =~ s/\\/\//g;

	$osascript = qq[mount volume "$winHomeDir"];
	system( "osascript -e '$osascript'");

}
sub mount_winfs
{
	#check to make sure mount exists
	#check pri-role to see what server they are on

	my $pilot_user  = shift;

	my $servershare;
	$servershare = 'fs-c108-03/student_home$/'  if ifmember($username,'students') ;
	$servershare = 'fs-c108-04/faculty_home$/'  if ifmember($username,'faculty')  ;

	return unless $servershare; # Don't attempt mount if no Y: drive for user

	my $winfs_path = '//' . $servershare . $username;
	$mntfolder = $ENV{'HOME'} . '/Desktop/' . $username . '_CAMPUSFILES';

	print LOG $servershare . "\n";
	print LOG $mntfolder . "\n";
	system('mkdir ' . $mntfolder);
    	system('mount_smbfs -o nobrowse ' . $winfs_path . ' ' . $mntfolder);

	#get list of mounts
	$mountlist = `mount`;
	if ($mountlist =~ m/CAMPUSFILES/ix) 
	{# Successful mount
	} 
	else 
	{# Match attempt failed
		system('rm -dfr ' . $mntfolder);
	}

}

sub get_prirole
{
	my $ldapsearch = qq[ldapsearch -h ldap.fitsuny.edu -b "dc=fitsuny,dc=edu" -LLL -x "(uid=$username)" fitprirole];
	my $usertype = `$ldapsearch`;

	$usertype =~ s/^.*fitprirole:\s+(.*)$/$1/s;

	return $usertype;
}

sub ifmember
{
#does not do nested groups.

	$_username = shift;
	$_group = shift;
	use Net::LDAP;
	$ldap = Net::LDAP->new ('fit-c108-01') or die "$@";
	$ad = $ldap->bind('cn=ldapsearch,ou=fitusers,dc=fitsuny,dc=edu', password => 'password');
	$ad = $ldap->search (base =>'ou=FiTUsers,dc=fitsuny,dc=edu',
				filter => "(&(cn=" . $_username . ")(memberof=cn=" . $_group . ",ou=fitgroups,dc=fitsuny,dc=edu))",
				#attrs => ['memberOf'],
				);
	$ad->code && $ad->error;

	return $ad->count();
				

}


sub emgmesg
{
$message = "LOGIN TO MyFIT TO REGISTER FOR EMERGENCY MESSAGE NOTIFICATIONS
Get timely announcements in the event of a campus emergency or a weather-related closure of the college via text message, email, or voicemail.

Would you like to sign up now?";

#icons Types (stop, note, caution)
$icon = 'note';

#button types (ok, yesno)
$buttons = 'yesno';

#action
$action = 'http://myfit.fitnyc.edu';

if($icon eq 'stop')
	{
		$ic = '0';
	}
	elsif($icon eq 'note')
	{
		$ic = '1';
	}
	elsif($icon eq 'caution')
	{
		$ic = '2';
	}

	if($buttons eq 'ok')
	{
		$bu = '{"OK"}';
	}
	elsif($buttons eq 'yesno')
	{
		$bu = '{"YES","NO"}';
	}

	#mac way
	$result = `/usr/bin/osascript <<-EOF

    tell application "System Events"
        activate
        display dialog "$message" buttons $bu with icon $ic
    end tell

EOF`;

chomp($result);
if (($buttons eq 'yesno') && ($result eq 'button returned:YES'))
{
	system('open ' . $action);
}
}

vbscript logon script

Our old logon script from 2002

'****************************************************************
'* Logon Script 2k      *
'* This should be the end to multiple logon scripts *
'* This will be the logon script for all labs.  *
'* ps. Nothing is a cure all.    *
'* Its written in vbscript http://msdn.microsoft.com *
'*        *
'* Stephen@2k2     *
'* Edited by:      *
'*  Robert *  
'*      and                                         *
'*  Mike *
'****************************************************************

'Date Modified Feb 6, 2002
On error resume next
Dim WshNetwork, WshSHell, Return, Desktop, HKCU
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set WshShell = WScript.CreateObject("Wscript.Shell")
Desktop=WshShell.SpecialFolders("Desktop")
username=left(WshNetwork.Computername,3)
dim number

'Novell Drive Mappings.
call winmap("l:","\\Thurston_w\vol1","lab")

'return=WshShell.run ("subst p: /d", 0, true) 
'return=WshShell.run("subst p: c:\temp", 0, true)

'Citrix Applications
call shortcut("CITRIX APPS","%userprofile%\application data\icaclient\CITRIX APPS.lnk","C:\Program Files\Internet Explorer\")

'Just put your commands for what lab ur in.
Select Case username
Case "PUB"
 Wshnetwork.addwindowsprinterconnection("\\ginger\pub4000")
 Wshnetwork.setdefaultprinter("\\ginger\pub4000")
 If right(WSHNetwork.Computername,2) <> 30 Then
  Wshnetwork.addwindowsprinterconnection("\\ginger\puboki8")
 End if
 call winmap("n:","\\ginger\shared","lab")
 Call shortcut("Paint Shop Pro","L:\XPAPPS\Paint Shop Pro\Psp.exe","L:\XPAPPS\Paint Shop Pro")
Case "MHK"
 Wshnetwork.addwindowsprinterconnection("\\ginger\mhk5m")
 wshnetwork.setdefaultprinter("\\ginger\mhk5m")
 call winmap("n:","\\ginger\shared","lab")
Case "NUR"
 Wshnetwork.addwindowsprinterconnection("\\ginger\nur4000")
 wshnetwork.setdefaultprinter("\\ginger\nur4000")
 call winmap("n:","\\ginger\shared","lab")
 call shortcut("Inventor","C:\Program Files\Autodesk\Inventor 5.3\Bin\Inventor.exe","C:\Program Files\Autodesk\Inventor 5.3\Bin")
Case "BPM"
 Wshnetwork.addwindowsprinterconnection("\\ginger\bpm4000")
 wshnetwork.setdefaultprinter("\\ginger\bpm4000")
 If right(WSHNetwork.Computername,2) < number =" right(WshNetwork.Computername,2)" number = "03"> 32 Then
  Wshnetwork.addwindowsprinterconnection("\\ginger\don4000")
 End if
Case "LRN"
 Wshnetwork.addwindowsprinterconnection("\\ginger\lrn4000")
 Wshnetwork.setdefaultprinter("\\ginger\lrn4000")
 call winmap("n:","\\ginger\shared","lab") 
 call shortcut("EVE 2","C:\Program Files\EVE2\eve2.exe","C:\Program Files\EVE2")
 call shortcut("SPSS11","C:\Program Files\SPSS\spsswin.exe","C:\Program Files\SPSS")
Case "DON"
 Wshnetwork.addwindowsprinterconnection("\\ginger\don4000")
 Wshnetwork.setdefaultprinter("\\ginger\don4000")
 If right(WSHNetwork.Computername,2) <> 25 Then
  Wshnetwork.addwindowsprinterconnection("\\ginger\dtp4M")
  Wshsetdefaultprinter("\\ginger\dtp4M")
 End if 
 call winmap("n:","\\ginger\shared",username)
 call shortcut("Quark Xpress","L:\XPAPPS\QuarkXPress\QuarkXPress Passport.exe","L:\XPAPPS\QuarkXPress\QuarkXPress Passport.exe")
 call shortcut("SHARED","N:\SHARE2147\","N:\SHARE2147\")
 call shortcut("PAGEMAKER","C:\Program Files\Adobe\PageMaker 7.0\Pm70.exe","C:\Program Files\Adobe\PageMaker 7.0")
 call shortcut("PHOTOSHOP","C:\Program Files\Adobe\Photoshop 7.0\Photoshop.exe","C:\Program Files\Adobe\Photoshop 7.0")
 call shortcut("SPSS11","C:\Program Files\SPSS\spsswin.exe","C:\Program Files\SPSS") 
 Call shortcut("Paint Shop Pro","L:\XPAPPS\Paint Shop Pro\Psp.exe","L:\XPAPPS\Paint Shop Pro")
 call shortcut("EVE","%userprofile%\application data\icaclient\EVE2.lnk","c:\program files\citrix\ica client\")
Case "CAD"
 Wshnetwork.addwindowsprinterconnection("\\ginger\cad4000")
 wshnetwork.setdefaultprinter("\\ginger\cad4000")
 Wshnetwork.addwindowsprinterconnection("\\ginger\cad700")
 
 If right(WSHNetwork.Computername,2) > 6 Then
  Call shortcut("DB UNLOCK","C:\Program Files\Manufacturers Technologies\Costimator\DbUnlock.exe","C:\Program Files\Manufacturers Technologies\Costimator\")
  Call shortcut("COSTIMATOR","C:\Program Files\Manufacturers Technologies\Costimator\MTIMain.exe","C:\Program Files\Manufacturers Technologies\Costimator\")
 End if
 call winmap("n:","\\ginger\shared","lab")
 Call shortcut("POM","C:\program files\POMwin32\pom32start.exe","C:\program files\POMwin32\pom32start.exe")
 Call shortcut("Solidworks","C:\program files\solidworks\sldworks.exe","C:\program files\solidworks\")
Case "PHY"
 Wshnetwork.addwindowsprinterconnection("\\ginger\phy4000")
 wshnetwork.setdefaultprinter("\\ginger\phy4000")
 'G: Drive for the Genetics program
 call winmap("g:","\\island\dfs\vol1\xpapps\genetics","lab")
 'genetics software needs diff to run diff is called karyo.exe
 call shortcut("Karyotypes","L:\XPAPPS\GENETICSa\karyopw.exe","L:\XPAPPS\GENETICSa")
 call shortcut("Sky","C:\Program Files\Software Bisque\TheSky\SKY.exe","C:\Program Files\Software Bisque\TheSky")
 call shortcut("Genetics","L:\XPAPPS\GENETICS\Genetics\Start Genetics.exe","L:\XPAPPS\GENETICS\genetics")
 call winmap("n:","\\ginger\shared","lab")
 call shortcut("SHARED","N:\SHARE2107\","N:\SHARE2107\")
Case Else
End Select
'************************************************************************************************************
'*                                             FUNCTIONS          *
'************************************************************************************************************
Function unmap(drv)
  Dim fso
  Set fso = CreateObject("Scripting.FileSystemObject")
  If fso.DriveExists(drv) Then WshNetwork.removeNetworkDrive drv & ":"
End Function

Function winmap(drive,dest,user)
unmap(drive)
Wshnetwork.mapnetworkdrive drive, dest, false, user, ""
End Function

Function shortcut(name,dest,working)
Set shelllink = wshshell.createshortcut(desktop & "\" & name &".lnk")
shelllink.targetpath= dest
shelllink.workingdirectory= working
shelllink.windowstyle = 1
shelllink.save
End Function

Hidden Logon Script

This little vbscript wrapper will let you start a logon script hidden. Our logon scripts are batch and perl files and they all start with the black box. If you launch the script like hidelaunch.vbs logon.bat it will start the logon script hidden.

Set wshShell = CreateObject("WScript.Shell")
set args = wscript.arguments

command = ""

for each strArg in args
 command = strArg + " "
next

wshshell.run command, 0, false

Logon Tracker

This is the client part of a script that collects the username and computer name and sends it to a database for tracking purposes, when I find the server side of the script I will post that also.

#!/usr/bin/perl
#Grabs Certain local data and send it to an ICC database.

$user = $ENV{'USER'};
use Sys::Hostname;

use LWP::UserAgent; 
my $ua = new LWP::UserAgent;

my $response
= $ua->post('http://web.url.com/getlinfo.asp',
{ user => $user,
machine => hostname,
});

my $content = $response->content;