3
ntlogon.py written by Timothy (rhacer) Grant
5
Copyright 1999 - 2002 by Timothy Grant
7
is distributed under the terms of the GNU Public License.
9
The format for the configuration file is as follows:
11
While there is some room for confusion, we attempt to process things in
12
order of specificity: Global first, Group second, User third, OS Type
13
forth. This order can be debated forever, but it seems to make the most
16
# Everything in the Global section applies to all users logging on to the
19
@ECHO "Welcome to our network!!!"
20
NET TIME \\\\servername /SET /YES
21
NET USE F: \\\\servername\\globalshare /YES
23
# Map the private user area in the global section so we don't have to
24
# create individual user entries for each user!
25
NET USE U: \\\\servername\\%U /YES
27
# Group entries, User entries and OS entries each start with the
28
# keyword followed by a dash followed by--appropriately enough the Group
29
# name, the User name, or the OS name.
31
@ECHO "Welcome administrators!"
32
NET USE G: \\\\servername\\adminshare1 /YES
33
NET USE I: \\\\servername\\adminshare2 /YES
36
@ECHO "Be grateful we let you use computers!"
37
NET USE G: \\\\servername\\peonshare1 /YES
40
@ECHO "What can I do for you today great one?"
41
NET USE G: \\\\servername\\hackershare1 /YES
42
NET USE I: \\\\servername\\adminshare2 /YES
45
@ECHO "Hello there Fred!"
46
NET USE F: \\\\servername\\fredsspecialshare /YES
49
@ECHO "Time to upgrade it?"
51
# End configuration file
53
usage: ntlogon [-g | --group=groupname]
54
[-u | --user=username]
56
[-m | --machine=netbiosname]
57
[-f | --templatefile=filename]
58
[-d | --dir=netlogon directory]
65
#" This quote mark is an artifact of the inability of my editor to
66
# correctly colour code anything after the triple-quoted docstring.
67
# if your editor does not have this flaw, feel free to remove it.
76
version = "ntlogon.py v0.8"
78
def buildScript(buf, sections, group, user, ostype, machine, debug, pause):
80
buildScript() Takes the contents of the template file and builds
81
a DOS batch file to be executed as an NT logon script. It does this
82
by determining which sections of the configuration file should be included
83
and creating a list object that contains each line contained in each
84
included section. The list object is then returned to the calling
87
All comments (#) are removed. A REM is inserted to show
88
which section of the configuration file each line comes from.
89
We leave blanklines as they are sometimes useful for debugging
91
We also replace all of the Samba macros (e.g., %U, %G, %a, %m) with their
92
expanded versions which have been passed to us by smbd
98
# These are the Samba macros that we currently know about.
99
# any user defined macros will also be added to this dictionary.
100
# We do not store the % sign as part of the macro name.
101
# The replace routine will prepend the % sign to all possible
112
# Process each section defined in the list sections
115
# print 'searching for: ' + s
119
while idx < len(buf):
123
# We need to set up a regex for each possible section we
124
# know about. This is slightly complicated due to the fact
125
# that section headers contain user defined text.
128
hdrstring = '\[ *' + s + ' *\]'
130
hdrstring = '\[ *' + s + ' *- *' + group + ' *\]'
132
hdrstring = '\[ *' + s + ' *- *' + user + ' *\]'
134
hdrstring = '\[ *' + s + ' *- *' + ostype + ' *\]'
136
hdrstring = '\[ *' + s + ' *- *' + machine + ' *\]'
139
# See if we have found a section header
141
if re.search(r'(?i)' + hdrstring, ln):
142
idx = idx + 1 # increment the counter to move to the next
145
x = re.match(r'([^#\r\n]*)', ln) # Determine the section
146
# name and strip out CR/LF
147
# and comment information
150
print 'rem ' + x.group(1) + ' commands'
152
# create the rem at the beginning of each section of the
154
script.append('rem ' + x.group(1) + ' commands')
157
# process each line until we have found another section
160
while not re.search(r'.*\[.*\].*', buf[idx]):
163
# strip comments and line endings
165
x = re.match(r'([^#\r\n]*)', buf[idx])
167
if string.strip(x.group(1)) != '' :
168
# if there is still content after stripping comments and
169
# line endings then this is a line to process
174
# Check to see if this is a macro definition line
176
vardef = re.match(r'(.*)=(.*)', line)
179
varname = string.strip(vardef.group(1)) # Strip leading and
180
varsub = string.strip(vardef.group(2)) # and trailing spaces
183
print "Error: No substition name specified line: %d" % idx
187
print "Error: No substitution text provided line: %d" % idx
190
if macros.has_key(varname):
191
print "Warning: macro %s redefined line: %d" % (varname, idx)
193
macros[varname] = varsub
198
# Replace all the macros that we currently
201
# Iterate over the dictionary that contains all known
202
# macro substitutions.
204
# We test for a macro name by prepending % to each dictionary
207
for varname in macros.keys():
208
line = re.sub(r'%' + varname + r'(\W)',
209
macros[varname] + r'\1', line)
221
break # if we have reached the end of the file
224
idx = idx + 1 # increment the line counter
237
run() everything starts here. The main routine reads the command line
238
arguments, opens and reads the configuration file.
240
configfile = '/etc/ntlogon.conf' # Default configuration file
241
group = '' # Default group
242
user = '' # Default user
243
ostype = '' # Default os
244
machine = '' # Default machine type
245
outfile = 'logon.bat' # Default batch file name
246
# this file name WILL take on the form
247
# username.bat if a username is specified
248
debug = 0 # Default debugging mode
249
pause = 0 # Default pause mode
250
outdir = '/usr/local/samba/netlogon/' # Default netlogon directory
252
sections = ['Global', 'Machine', 'OS', 'Group', 'User'] # Currently supported
256
options, args = getopt.getopt(sys.argv[1:], 'd:f:g:ho:u:m:v',
269
# Process the command line arguments
272
# template file to process
273
if (i[0] == '-f') or (i[0] == '--templatefile'):
275
# print 'configfile = ' + configfile
277
# define the group to be used
278
elif (i[0] == '-g') or (i[0] == '--group'):
280
# print 'group = ' + group
283
elif (i[0] == '-o') or (i[0] == '--os'):
288
elif (i[0] == '-u') or (i[0] == '--user'):
290
outfile = user + '.bat' # Setup the output file name
291
# print 'user = ' + user
294
elif (i[0] == '-m') or (i[0] == '--machine'):
297
# define the netlogon directory
298
elif (i[0] == '-d') or (i[0] == '--dir'):
300
# print 'outdir = ' + outdir
302
# if we are asked to turn on debug info, do so.
303
elif (i[0] == '--debug'):
305
# print 'debug = ' + debug
307
# if we are asked to turn on the automatic pause functionality, do so
308
elif (i[0] == '--pause'):
310
# print 'pause = ' + pause
312
# if we are asked for the version number, print it.
313
elif (i[0] == '-v') or (i[0] == '--version'):
317
# if we are asked for help print the docstring.
318
elif (i[0] == '-h') or (i[0] == '--help'):
323
# open the configuration file
326
iFile = open(configfile, 'r')
328
print 'Unable to open configuration file: ' + configfile
333
# open the output file
337
oFile = open(outdir + outfile, 'w')
339
print 'Unable to open logon script file: ' + outdir + outfile
342
buf = iFile.readlines() # read in the entire configuration file
345
# call the script building routine
347
script = buildScript(buf, sections, group, user, ostype, machine, debug, pause)
350
# write out the script file
354
oFile.write(ln + '\r\n')
356
if string.strip(ln) != '': # Because whitespace
357
oFile.write('pause' + '\r\n') # is a useful tool, we
358
# don't put pauses after
365
# immediate-mode commands, for drag-and-drop or execfile() execution
367
if __name__ == '__main__':
370
print "Module ntlogon.py imported."
371
print "To run, type: ntlogon.run()"
372
print "To reload after changes to the source, type: reload(ntlogon)"