"""
ntlogon.py written by Timothy (rhacer) Grant
Copyright 1999 - 2002 by Timothy Grant
is distributed under the terms of the GNU Public License.
The format for the configuration file is as follows:
While there is some room for confusion, we attempt to process things in
order of specificity: Global first, Group second, User third, OS Type
forth. This order can be debated forever, but it seems to make the most
sense.
# Everything in the Global section applies to all users logging on to the
# network
[Global]
@ECHO "Welcome to our network!!!"
NET TIME \\\\servername /SET /YES
NET USE F: \\\\servername\\globalshare /YES
# Map the private user area in the global section so we don't have to
# create individual user entries for each user!
NET USE U: \\\\servername\\%U /YES
# Group entries, User entries and OS entries each start with the
# keyword followed by a dash followed by--appropriately enough the Group
# name, the User name, or the OS name.
[Group-admin]
@ECHO "Welcome administrators!"
NET USE G: \\\\servername\\adminshare1 /YES
NET USE I: \\\\servername\\adminshare2 /YES
[Group-peons]
@ECHO "Be grateful we let you use computers!"
NET USE G: \\\\servername\\peonshare1 /YES
[Group-hackers]
@ECHO "What can I do for you today great one?"
NET USE G: \\\\servername\\hackershare1 /YES
NET USE I: \\\\servername\\adminshare2 /YES
[User-fred]
@ECHO "Hello there Fred!"
NET USE F: \\\\servername\\fredsspecialshare /YES
[OS-WfWg]
@ECHO "Time to upgrade it?"
# End configuration file
usage: ntlogon [-g | --group=groupname]
[-u | --user=username]
[-o | --os=osname]
[-m | --machine=netbiosname]
[-f | --templatefile=filename]
[-d | --dir=netlogon directory]
[-v | --version]
[-h | --help]
[--pause]
[--debug]
"""
import sys
import getopt
import re
import string
import os
version = "ntlogon.py v0.8"
def buildScript(buf, sections, group, user, ostype, machine, debug, pause):
"""
buildScript() Takes the contents of the template file and builds
a DOS batch file to be executed as an NT logon script. It does this
by determining which sections of the configuration file should be included
and creating a list object that contains each line contained in each
included section. The list object is then returned to the calling
routine.
All comments (#) are removed. A REM is inserted to show
which section of the configuration file each line comes from.
We leave blanklines as they are sometimes useful for debugging
We also replace all of the Samba macros (e.g., %U, %G, %a, %m) with their
expanded versions which have been passed to us by smbd
"""
hdrstring = ''
script = []
macros = {
'U': user,
'G': group,
'a': ostype,
'm': machine
}
for s in sections:
idx = 0
while idx < len(buf):
ln = buf[idx]
if s == 'Global':
hdrstring = '\[ *' + s + ' *\]'
elif s == 'Group':
hdrstring = '\[ *' + s + ' *- *' + group + ' *\]'
elif s == 'User':
hdrstring = '\[ *' + s + ' *- *' + user + ' *\]'
elif s == 'OS':
hdrstring = '\[ *' + s + ' *- *' + ostype + ' *\]'
elif s == 'Machine':
hdrstring = '\[ *' + s + ' *- *' + machine + ' *\]'
if re.search(r'(?i)' + hdrstring, ln):
idx = idx + 1
x = re.match(r'([^#\r\n]*)', ln)
if debug:
print 'rem ' + x.group(1) + ' commands'
else:
script.append('rem ' + x.group(1) + ' commands')
while not re.search(r'.*\[.*\].*', buf[idx]):
x = re.match(r'([^#\r\n]*)', buf[idx])
if string.strip(x.group(1)) != '' :
line = x.group(1)
vardef = re.match(r'(.*)=(.*)', line)
if vardef:
varname = string.strip(vardef.group(1)) varsub = string.strip(vardef.group(2))
if varname == '':
print "Error: No substition name specified line: %d" % idx
sys.exit(1)
if varsub == '':
print "Error: No substitution text provided line: %d" % idx
sys.exit(1)
if macros.has_key(varname):
print "Warning: macro %s redefined line: %d" % (varname, idx)
macros[varname] = varsub
idx = idx + 1
continue
for varname in macros.keys():
line = re.sub(r'%' + varname + r'(\W)',
macros[varname] + r'\1', line)
if debug:
print line
if pause:
print 'pause'
else:
script.append(line)
idx = idx + 1
if idx == len(buf):
break
idx = idx + 1
if debug:
print ''
else:
script.append('')
return script
def run():
"""
run() everything starts here. The main routine reads the command line
arguments, opens and reads the configuration file.
"""
configfile = '/etc/ntlogon.conf' group = '' user = '' ostype = '' machine = '' outfile = 'logon.bat' debug = 0 pause = 0 outdir = '/usr/local/samba/netlogon/'
sections = ['Global', 'Machine', 'OS', 'Group', 'User']
options, args = getopt.getopt(sys.argv[1:], 'd:f:g:ho:u:m:v',
['templatefile=',
'group=',
'help',
'os=',
'user=',
'machine=',
'dir=',
'version',
'pause',
'debug'])
for i in options:
if (i[0] == '-f') or (i[0] == '--templatefile'):
configfile = i[1]
elif (i[0] == '-g') or (i[0] == '--group'):
group = i[1]
elif (i[0] == '-o') or (i[0] == '--os'):
ostype = i[1]
elif (i[0] == '-u') or (i[0] == '--user'):
user = i[1]
outfile = user + '.bat'
elif (i[0] == '-m') or (i[0] == '--machine'):
machine = i[1]
elif (i[0] == '-d') or (i[0] == '--dir'):
outdir = i[1]
elif (i[0] == '--debug'):
debug = 1
elif (i[0] == '--pause'):
pause = 1
elif (i[0] == '-v') or (i[0] == '--version'):
print version
sys.exit(0)
elif (i[0] == '-h') or (i[0] == '--help'):
print __doc__
sys.exit(0)
try:
iFile = open(configfile, 'r')
except IOError:
print 'Unable to open configuration file: ' + configfile
sys.exit(1)
if not debug:
try:
oFile = open(outdir + outfile, 'w')
except IOError:
print 'Unable to open logon script file: ' + outdir + outfile
sys.exit(1)
buf = iFile.readlines()
script = buildScript(buf, sections, group, user, ostype, machine, debug, pause)
if not debug:
for ln in script:
oFile.write(ln + '\r\n')
if pause:
if string.strip(ln) != '': oFile.write('pause' + '\r\n')
if __name__ == '__main__':
run()
else:
print "Module ntlogon.py imported."
print "To run, type: ntlogon.run()"
print "To reload after changes to the source, type: reload(ntlogon)"