[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
group
has been implemented deleting the sub group need be implemented.
f)viewing the permission on the path to the group/users. - yet to be
implemented
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

#!/usr/bin/python
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):
  sectionname=lines[0][1:-1]
  section={}
  previous_line_continued=0
  prev_key = None
  counter = 0
  for i in lines[1:]:
    counter = counter + 1
    line = i.strip()
    if len(line) == 0:
      continue
    if line[0] == '#':
      continue
    elif line[0] == '[':
      break
 
    fields=[]
    if previous_line_continued == 0:
      fields = line.split('=')
    else:
      fields.append(prev_key)
      fields.append(line)
    if line[-1] == '\\':
      previous_line_continued=1
    else:
      previous_line_continued=0
    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:
          section[fields[0].strip()]=[]
        valuelist = fields[1].strip().split(',')
        if len(valuelist) > 0:
          if len(valuelist[-1].strip()) == 0:
            valuelist = valuelist[0:-1]
        for i in valuelist:
          section[fields[0].strip()].append(i.strip())
      else:
        section[fields[0].strip()]=fields[1].strip()
  return counter, sectionname, section

def saveAuthUserFile(sections, output_file_name):
  if output_file_name == None:
    print 'Please provide the output_file_name'
    help()
    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)
          outf.write(username)
          if entry_num != total_entries-1:
            outf.write(', ')
          entrylen = entrylen + len(username) + 2
          if entrylen > max_characters_per_line:
            outf.write('\\\n')
            entrylen = start_entrylen
          entry_num = entry_num + 1
      else:
        outf.write(entry + '=' + sections[section][entry])
      outf.write('\n')
    outf.write('\n')
  outf.close()

def getsections(input_file_name):
  f = file(input_file_name)
  lines = f.read().split('\n')[0:-1]
  total_lines = len(lines)
  lines_parsed = 0
  f.close()
  sectiondict={}
  while lines_parsed < total_lines-1:
    section_tuple = parsesection(lines[lines_parsed:])
    lines_parsed = lines_parsed + section_tuple[0]
    sectiondict[section_tuple[1]]=section_tuple[2]
  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. '
    help()
    return 1
  userdict={}
  sections=getsections(input_file_name)
  for i in sections['groups']:
    for j in sections['groups'][i]:
      userdict[j]=''

  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
          continue
      userdict[j]=''
  if userdict.has_key('*') == True:
    del userdict['*']
    userdict['Anonymous']=''
  userlist = userdict.keys()
  userlist.sort()
  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. '
    help()
    return 1
  userdict={}
  groups=getsections(input_file_name)['groups'].keys()
  groups.sort()
  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'] = {}
  sections['groups'][group]=[]
  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'] = {}
  else:
    if sections['groups'].has_key(group) == False:
      sections['groups'][group]=[]
  sections['groups'][group].append(user)
  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
  else:
    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:
  help()
  sys.exit(1)

options = getopt.getopt(sys.argv[2:], 'i:o:', ['user=', 'group=', 'path', 'perms'])
input_file_name=None
output_file_name=None
group=None
user=None
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':
  listusers(input_file_name)
elif sys.argv[1]=='delete-user':
  deleteuser()
elif sys.argv[1]=='list-groups':
  listgroups(input_file_name)
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':
  deletegroup()

---------------------------------------------------------------------
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.

This site is subject to the Apache Privacy Policy and the Apache Public Forum Archive Policy.