--- ntlogon.orig 2009-04-24 13:30:40.000000000 +0200 +++ ntlogon 2009-04-27 17:28:01.000000000 +0200 @@ -50,8 +50,7 @@ # End configuration file -usage: ntlogon [-g | --group=groupname] - [-u | --user=username] +usage: ntlogon [-u | --user=username] [-o | --os=osname] [-m | --machine=netbiosname] [-f | --templatefile=filename] @@ -72,10 +71,24 @@ import re import string import os +import grp -version = "ntlogon.py v0.8" +version = "ntlogon.py v0.9" -def buildScript(buf, sections, group, user, ostype, machine, debug, pause): +def findusergroups(user): + """ + Iterate through the group-database and find all groups the + user is a member of. + The list of groups is returned as a list. + """ + groups=[] + for name,pwd,gid,members in grp.getgrall(): + if user in members: + groups.append(name) + return groups + + +def buildScript(buf, sections, 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 @@ -103,7 +116,6 @@ # macros = { 'U': user, - 'G': group, 'a': ostype, 'm': machine } @@ -111,6 +123,14 @@ # # Process each section defined in the list sections # + if len(user) > 0: + grouplist=findusergroups(user) + groupre=r'(' + for nextgroup in grouplist: + groupre=groupre+r'('+nextgroup+r')' + r'||' + + groupre=groupre[:-2] + r')' #remove the last '||' + for s in sections: # print 'searching for: ' + s @@ -127,7 +147,7 @@ if s == 'Global': hdrstring = '\[ *' + s + ' *\]' elif s == 'Group': - hdrstring = '\[ *' + s + ' *- *' + group + ' *\]' + hdrstring = '\[ *' + s + ' *- *' + groupre + ' *\]' elif s == 'User': hdrstring = '\[ *' + s + ' *- *' + user + ' *\]' elif s == 'OS': @@ -221,7 +241,8 @@ break # if we have reached the end of the file # stop processing. - idx = idx + 1 # increment the line counter + else: + idx = idx + 1 # increment the line counter if debug: print '' @@ -253,9 +274,8 @@ # configuration file # sections - options, args = getopt.getopt(sys.argv[1:], 'd:f:g:ho:u:m:v', + options, args = getopt.getopt(sys.argv[1:], 'd:f:ho:u:m:v', ['templatefile=', - 'group=', 'help', 'os=', 'user=', @@ -274,10 +294,6 @@ configfile = i[1] # print 'configfile = ' + configfile - # define the group to be used - elif (i[0] == '-g') or (i[0] == '--group'): - group = i[1] - # print 'group = ' + group # define the os type elif (i[0] == '-o') or (i[0] == '--os'): @@ -344,7 +360,7 @@ # # call the script building routine # - script = buildScript(buf, sections, group, user, ostype, machine, debug, pause) + script = buildScript(buf, sections, user, ostype, machine, debug, pause) # # write out the script file