3 ntlogon.py written by Timothy Grant
5 Copyright 1999 by Avalon Technology Group, Inc.
7 This programme is copyright 1999 by Avalon Technology Group, Inc. it
8 is distributed under the terms of the GNU Public License.
11 The format for the configuration file is as follows:
13 While there is some room for confusion, we attempt to process things in
14 order of specificity: Global first, Group second, User third, OS Type
15 forth. This order can be debated forever, but it seems to make the most
18 # Everything in the Global section applies to all users logging on to the
21 @ECHO "Welcome to our network!!!"
22 NET TIME \\\\servername /SET /YES
23 NET USE F: \\\\servername\\globalshare /YES
25 # Map the private user area in the global section so we don't have to
26 # create individual user entries for each user!
27 NET USE U: \\\\servername\\%U /YES
29 # Group entries, User entries and OS entries each start with the
30 # keyword followed by a dash followed by--appropriately enough the Group
31 # name, the User name, or the OS name.
33 @ECHO "Welcome administrators!"
34 NET USE G: \\\\servername\\adminshare1 /YES
35 NET USE I: \\\\servername\\adminshare2 /YES
38 @ECHO "Be grateful we let you use computers!"
39 NET USE G: \\\\servername\\peonshare1 /YES
42 @ECHO "What can I do for you today great one?"
43 NET USE G: \\\\servername\\hackershare1 /YES
44 NET USE I: \\\\servername\\adminshare2 /YES
47 @ECHO "Hello there Fred!"
48 NET USE F: \\\\servername\\fredsspecialshare /YES
51 @ECHO "Time to upgrade it?"
53 # End configuration file
55 usage: ntlogon [-g | --group=groupname]
56 [-u | --user=username]
58 [-m | --machine=netbiosname]
59 [-f | --templatefile=filename]
60 [-d | --dir=netlogon directory]
67 #" This quote mark is an artifact of the inability of my editor to
68 # correctly colour code anything after the triple-quoted docstring.
69 # if your editor does not have this flaw, feel free to remove it.
78 version = "ntlogon.py v0.8"
80 def buildScript(buf, sections, group, user, ostype, machine, debug, pause):
82 buildScript() Takes the contents of the template file and builds
83 a DOS batch file to be executed as an NT logon script. It does this
84 by determining which sections of the configuration file should be included
85 and creating a list object that contains each line contained in each
86 included section. The list object is then returned to the calling
89 All comments (#) are removed. A REM is inserted to show
90 which section of the configuration file each line comes from.
91 We leave blanklines as they are sometimes useful for debugging
93 We also replace all of the Samba macros (e.g., %U, %G, %a, %m) with their
94 expanded versions which have been passed to us by smbd
100 # These are the Samba macros that we currently know about.
101 # any user defined macros will also be added to this dictionary.
102 # We do not store the % sign as part of the macro name.
103 # The replace routine will prepend the % sign to all possible
114 # Process each section defined in the list sections
117 # print 'searching for: ' + s
121 while idx < len(buf):
125 # We need to set up a regex for each possible section we
126 # know about. This is slightly complicated due to the fact
127 # that section headers contain user defined text.
130 hdrstring = '\[ *' + s + ' *\]'
132 hdrstring = '\[ *' + s + ' *- *' + group + ' *\]'
134 hdrstring = '\[ *' + s + ' *- *' + user + ' *\]'
136 hdrstring = '\[ *' + s + ' *- *' + ostype + ' *\]'
138 hdrstring = '\[ *' + s + ' *- *' + machine + ' *\]'
141 # See if we have found a section header
143 if re.search(r'(?i)' + hdrstring, ln):
144 idx = idx + 1 # increment the counter to move to the next
147 x = re.match(r'([^#\r\n]*)', ln) # Determine the section
148 # name and strip out CR/LF
149 # and comment information
152 print 'rem ' + x.group(1) + ' commands'
154 # create the rem at the beginning of each section of the
156 script.append('rem ' + x.group(1) + ' commands')
159 # process each line until we have found another section
162 while not re.search(r'.*\[.*\].*', buf[idx]):
165 # strip comments and line endings
167 x = re.match(r'([^#\r\n]*)', buf[idx])
169 if string.strip(x.group(1)) != '' :
170 # if there is still content after stripping comments and
171 # line endings then this is a line to process
176 # Check to see if this is a macro definition line
178 vardef = re.match(r'(.*)=(.*)', line)
181 varname = string.strip(vardef.group(1)) # Strip leading and
182 varsub = string.strip(vardef.group(2)) # and trailing spaces
185 print "Error: No substition name specified line: %d" % idx
189 print "Error: No substitution text provided line: %d" % idx
192 if macros.has_key(varname):
193 print "Warning: macro %s redefined line: %d" % (varname, idx)
195 macros[varname] = varsub
200 # Replace all the macros that we currently
203 # Iterate over the dictionary that contains all known
204 # macro substitutions.
206 # We test for a macro name by prepending % to each dictionary
209 for varname in macros.keys():
210 line = re.sub(r'%' + varname + r'(\W)',
211 macros[varname] + r'\1', line)
223 break # if we have reached the end of the file
226 idx = idx + 1 # increment the line counter
239 run() everything starts here. The main routine reads the command line
240 arguments, opens and reads the configuration file.
242 configfile = '/etc/ntlogon.conf' # Default configuration file
243 group = '' # Default group
244 user = '' # Default user
245 ostype = '' # Default os
246 machine = '' # Default machine type
247 outfile = 'logon.bat' # Default batch file name
248 # this file name WILL take on the form
249 # username.bat if a username is specified
250 debug = 0 # Default debugging mode
251 pause = 0 # Default pause mode
252 outdir = '/usr/local/samba/netlogon/' # Default netlogon directory
254 sections = ['Global', 'Machine', 'OS', 'Group', 'User'] # Currently supported
258 options, args = getopt.getopt(sys.argv[1:], 'd:f:g:ho:u:m:v',
271 # Process the command line arguments
274 # template file to process
275 if (i[0] == '-f') or (i[0] == '--templatefile'):
277 # print 'configfile = ' + configfile
279 # define the group to be used
280 elif (i[0] == '-g') or (i[0] == '--group'):
282 # print 'group = ' + group
285 elif (i[0] == '-o') or (i[0] == '--os'):
290 elif (i[0] == '-u') or (i[0] == '--user'):
292 outfile = user + '.bat' # Setup the output file name
293 # print 'user = ' + user
296 elif (i[0] == '-m') or (i[0] == '--machine'):
299 # define the netlogon directory
300 elif (i[0] == '-d') or (i[0] == '--dir'):
302 # print 'outdir = ' + outdir
304 # if we are asked to turn on debug info, do so.
305 elif (i[0] == '--debug'):
307 # print 'debug = ' + debug
309 # if we are asked to turn on the automatic pause functionality, do so
310 elif (i[0] == '--pause'):
312 # print 'pause = ' + pause
314 # if we are asked for the version number, print it.
315 elif (i[0] == '-v') or (i[0] == '--version'):
319 # if we are asked for help print the docstring.
320 elif (i[0] == '-h') or (i[0] == '--help'):
325 # open the configuration file
328 iFile = open(configfile, 'r')
330 print 'Unable to open configuration file: ' + configfile
335 # open the output file
339 oFile = open(outdir + outfile, 'w')
341 print 'Unable to open logon script file: ' + outdir + outfile
344 buf = iFile.readlines() # read in the entire configuration file
347 # call the script building routine
349 script = buildScript(buf, sections, group, user, ostype, machine, debug, pause)
352 # write out the script file
356 oFile.write(ln + '\r\n')
358 if string.strip(ln) != '': # Because whitespace is
359 oFile.write('pause' + '\r\n') # is a useful tool, we
360 # don't put pauses after
367 # immediate-mode commands, for drag-and-drop or execfile() execution
369 if __name__ == '__main__':
372 print "Module ntlogon.py imported."
373 print "To run, type: ntlogon.run()"
374 print "To reload after changes to the source, type: reload(ntlogon)"