Fri 5 Jun 2009

Scipting New Users in Subversion and Trac

Posted at 14:06 -0500

I manage a set of Trac sites and Subversion repositories for our research group. While I'll keep the structure consistent between them for simplicity, adding new users requires updates all over the place; the htdigest file, Trac's permission system, the authz file, etc.

To make my life easier, I've scripted the process. Here's the script, for anyone else who may benefit.

#!/bin/bash

# add_develop.bash
# Author: Rick Wagner
# guardian72@guardian72.com
# http://guardian72.com
#
# Date: 05JUN09
#
# Script to add developers to our Subversion
# and Trac sites. Generates a pseudo-random
# password and adds appropriate entries to
# and htdigest file [1], Subversion authz file [2],
# Trac authzpolicy file [3], and the Trac
# environment's permission table.
# Works by assuming the location
# of certain files and group name.
#
# Usage: add_developer.bash 
# Example:
# $ ./add_developer.bash joe
# New user info: joe, kf6xLu3z
#
# [1] http://httpd.apache.org/docs/2.0/programs/htdigest.html
# [2] http://tinyurl.com/r26vz
# [3] http://trac.edgewall.org/wiki/TracFineGrainedPermissions

# directories of Trac env and svn repo
TRAC_ENV=/var/trac/mytracsite
SVN_REPO=/var/svn/mysvnrepo

# auth realm expected by Apache
AUTH_REALM=myauthrealm

# file locations
HTDIGEST_FILE=$SVN_REPO/conf/passwd.htdigest
AUTHZ_FILE=$SVN_REPO/conf/authz
AUTHZ_CONF_FILE=$TRAC_ENV/conf/authzpolicy.conf

# back things up
cp -f $HTDIGEST_FILE $HTDIGEST_FILE.bak
cp -f $AUTHZ_FILE $AUTHZ_FILE.bak
cp -f $AUTHZ_CONF_FILE $AUTHZ_CONF_FILE.bak

# get username argument
username=$1

# generate a pseudo-random password
rand_pw=`< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c8`

# hash the username, realm, and password
htdigest_hash=`printf $username:$AUTH_REALM:$rand_pw | md5sum -`

# build an htdigest appropriate line, and tack it onto the file
echo "$username:$AUTH_REALM:${htdigest_hash:0:32}" >> $HTDIGEST_FILE

# add the developer to the trac site
trac-admin $TRAC_ENV permission add $username developer
echo "New user info: $username, $rand_pw"

# add user to developer group in authz file
# and Trac authz policy file
# yes, this could be done with sed
for auth_file in $AUTHZ_FILE $AUTHZ_CONF_FILE
  do
  while read line
    do
    if [[ ${line:0:11} == 'developer =' ]]
        then
      # strip the new line, and add the username with another newline
        line=`echo "$line" | tr "\n" ","`
        echo "$line $username" >> $auth_file.tmp      
    else
        echo "$line" >> $auth_file.tmp      
    fi
  done < $auth_file
  mv $auth_file.tmp $auth_file
done

Topics: