[svn.haxx.se] · SVN Dev · SVN Users · SVN Org · TSVN Dev · TSVN Users · Subclipse Dev · Subclipse Users · this month's index

utility to manage the AuthUserFile(mod_authz_svn)

From: Kamesh Jayachandran <kamesh_at_collab.net>
Date: 2005-12-16 20:12:01 CET

Hi All,
I intend to write a small utility that would help manage/generate the
Authorization file,
a)listing the users - implemented
b)adding the group - implemented
c)deleting the group - yet to be done
d)adding user to the group -implemented
e)deleting the user/subgroup from the group - deleting the user from the
has been implemented deleting the sub group need be implemented.
f)viewing the permission on the path to the group/users. - yet to be
g)granting/revoking the permissions to the path - yet to be implemented.

Would like to know your thoughts on the same.

Attaching the utility at its current stage

With regards
Kamesh Jayachandran

import getopt
import sys
import time
def help():
  print 'Usage:'
  print sys.argv[0], 'list-users -i /path/to/AuthUserFile -o /path/to/AuthUserFile'
  print sys.argv[0], 'list-groups -i /path/to/AuthUserFile -o /path/to/AuthUserFile'
  print sys.argv[0], 'add-group -i /path/to/AuthUserFile -o /path/to/AuthUserFile --group=group_name_to_add'
  print sys.argv[0], 'delete-user username_to_delete -i /path/to/AuthUserFile -o /path/to/AuthUserFile'
  print sys.argv[0], 'delete-group groupname_to_delete -i /path/to/AuthUserFile -o /path/to/AuthUserFile'
  print sys.argv[0], 'add-user-to-group --user=user --group=group -i /path/to/AuthUserFile -o /path/to/AuthUserFile'
  print sys.argv[0], 'delete-user-from-group --user=user --group=group -i /path/to/AuthUserFile -o /path/to/AuthUserFile'
  print sys.argv[0], 'unassociate-from-group groupname_to_unassociate_from --users=comma_seperated_list_of_users --groups=comma_seperated_list_of_groups -i /path/to/AuthUserFile -o /path/to/AuthUserFile'
  print sys.argv[0], 'grant-perms --path= --users= --groups= --perms -i /path/to/AuthUserFile -o /path/to/AuthUserFile'
  print sys.argv[0], 'list-perms --path= --users= --groups= -i /path/to/AuthUserFile -o /path/to/AuthUserFile'

def listperms(path, user, group):
  print 'permissions by virtue of user'
  print '----------------'
  print 'permissions by virtue of group'

def parsesection(lines):
  prev_key = None
  counter = 0
  for i in lines[1:]:
    counter = counter + 1
    line = i.strip()
    if len(line) == 0:
    if line[0] == '#':
    elif line[0] == '[':
    if previous_line_continued == 0:
      fields = line.split('=')
    if line[-1] == '\\':
    if len(fields) == 2:
      prev_key = fields[0].strip()
      if len(fields[1]) > 0:
        if fields[1][-1] == '\\':
                 fields[1] = fields[1][0:-1]
      if sectionname == 'groups':
        if section.has_key(fields[0]) == False:
        valuelist = fields[1].strip().split(',')
        if len(valuelist) > 0:
          if len(valuelist[-1].strip()) == 0:
            valuelist = valuelist[0:-1]
        for i in valuelist:
  return counter, sectionname, section

def saveAuthUserFile(sections, output_file_name):
  if output_file_name == None:
    print 'Please provide the output_file_name'
    return 1
  max_characters_per_line = 76
  outf = file(output_file_name, 'w')
  for section in sections:
    outf.write('[' + section + ']\n')
    for entry in sections[section]:
      if section == 'groups':
        entrylen = len(entry) + 3
        start_entrylen = entrylen
        total_entries = len(sections[section][entry])
        entry_num = 0
        outf.write(entry + ' = ')
        for username in sections[section][entry]:
          if entrylen == start_entrylen and entry_num !=0:
            outf.write(' ' * start_entrylen)
          if entry_num != total_entries-1:
            outf.write(', ')
          entrylen = entrylen + len(username) + 2
          if entrylen > max_characters_per_line:
            entrylen = start_entrylen
          entry_num = entry_num + 1
        outf.write(entry + '=' + sections[section][entry])

def getsections(input_file_name):
  f = file(input_file_name)
  lines = f.read().split('\n')[0:-1]
  total_lines = len(lines)
  lines_parsed = 0
  while lines_parsed < total_lines-1:
    section_tuple = parsesection(lines[lines_parsed:])
    lines_parsed = lines_parsed + section_tuple[0]
  return sectiondict

def listusers(input_file_name):
  if input_file_name == None:
    print 'To list the users you need to give the AuthUserFile. If you don\'t have such a file start with some commands like add-group etc. '
    return 1
  for i in sections['groups']:
    for j in sections['groups'][i]:

  del sections['groups']

  for i in sections:
    for j in sections[i]:
      auth_settings = j.strip()
      if len(auth_settings) > 0:
        if auth_settings[0] == '@':
          #entries starting with is group we don't get the usernames from this entry
  if userdict.has_key('*') == True:
    del userdict['*']
  userlist = userdict.keys()
  for i in userlist:
    print i

def deleteuser():
  print 'deleted the user'

def listgroups(input_file_name):
  if input_file_name == None:
    print 'To list the groups you need to give the AuthUserFile. If you don\'t have such a file start with some commands like add-group etc. '
    return 1
  for i in groups:
    print i

def addgroup(group, input_file_name, output_file_name):
  sections = getsections(input_file_name)
  if sections.has_key('groups') == False:
    sections['groups'] = {}
  saveAuthUserFile(sections, output_file_name)

def addusertogroup(user, group, input_file_name, output_file_name):
  sections = getsections(input_file_name)
  print sections['groups']
  if sections.has_key('groups') == False:
    sections['groups'] = {}
    if sections['groups'].has_key(group) == False:
  saveAuthUserFile(sections, output_file_name)

def deleteuserfromgroup(user, group, input_file_name, output_file_name):
  sections = getsections(input_file_name)
  if sections.has_key('groups') == False:
    return 1
    if sections['groups'].has_key(group) == False:
      return 1
  index = 0
  for i in sections['groups'][group]:
    if i == user:
      del sections['groups'][group][index]
    index = index + 1

  saveAuthUserFile(sections, output_file_name)

def deletegroup():
  print 'deleted the group'

if len(sys.argv) < 2:

options = getopt.getopt(sys.argv[2:], 'i:o:', ['user=', 'group=', 'path', 'perms'])
for i in options[0]:
  if i[0] == '-i':
    input_file_name = i[1]
  if i[0] == '-o':
    output_file_name = i[1]
  if i[0] == '--group':
    group = i[1]
  if i[0] == '--user':
    user = i[1]
if sys.argv[1]=='list-users':
elif sys.argv[1]=='delete-user':
elif sys.argv[1]=='list-groups':
elif sys.argv[1]=='add-group':
  addgroup(group, input_file_name, output_file_name)
elif sys.argv[1]=='add-user-to-group':
  addusertogroup(user, group, input_file_name, output_file_name)
elif sys.argv[1]=='delete-user-from-group':
  deleteuserfromgroup(user, group, input_file_name, output_file_name)
elif sys.argv[1]=='delete-group':

To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org
Received on Fri Dec 16 20:12:16 2005

This is an archived mail posted to the Subversion Dev mailing list.