[debian-lan-devel] [debian-lan] 03/03: Fixes and modifications in scripts: 'add2gosa' and 'debian-lan'

Andreas B. Mundt andi at moszumanska.debian.org
Sun Dec 1 20:47:42 UTC 2013


This is an automated email from the git hooks/post-receive script.

andi pushed a commit to branch wheezy
in repository debian-lan.

commit 8a12460348b37584828a13e5505670d009962d8c
Author: Andreas B. Mundt <andi at debian.org>
Date:   Wed Nov 27 21:50:33 2013 +0100

    Fixes and modifications in scripts:  'add2gosa' and 'debian-lan'
    
    Improve 'add2gosa':
      * Use TABs to separate columns (limited support for 'more complex'
        names).
      * Check input file for encoding and (some) errors.
      * Make sure the generated username is unique.
      * Fixes and improvements.
    
    Fix newline in 'debian-lan'.
    
    (cherry picked from commit 88c67911c39ac2d48b1f3b7bdee7a2137688c80d)
---
 fai/config/files/usr/local/sbin/add2gosa/GOSA      | 78 +++++++++++++++++-----
 .../files/usr/local/sbin/debian-lan/SERVER_A       |  2 +-
 2 files changed, 63 insertions(+), 17 deletions(-)

diff --git a/fai/config/files/usr/local/sbin/add2gosa/GOSA b/fai/config/files/usr/local/sbin/add2gosa/GOSA
index a672225..dab66f2 100755
--- a/fai/config/files/usr/local/sbin/add2gosa/GOSA
+++ b/fai/config/files/usr/local/sbin/add2gosa/GOSA
@@ -16,10 +16,25 @@ sync_nscd(){
 }
 
 mk_uname() {
-    GNAME=${1,,}
-    FNAME=${2,,}
-    echo ${GNAME::4}${FNAME::4}
-    #echo ${GNAME}_${FNAME}
+    # Convert to ASCII:
+    local FNAME=$(echo $1 | iconv -f UTF-8 -t ASCII//TRANSLIT)
+    local GNAME=$(echo $2 | iconv -f UTF-8 -t ASCII//TRANSLIT)
+    # lower case:
+    FNAME=${FNAME,,}
+    GNAME=${GNAME,,}
+    # Check if username is not yet in use:
+    N=3
+    UNAME=${FNAME::$N}${GNAME::$N}
+    sync_nscd
+    while getent passwd $UNAME > /dev/null || getent group $UNAME > /dev/null; do
+        N=$(($N+1))
+        if [ $N -gt 5 ] ; then
+            UNAME=${FNAME::3}${GNAME::3}$((N-5))
+        else
+            UNAME=${FNAME::$N}${GNAME::$N}
+        fi
+    done
+    echo $UNAME
 }
 
 ou2LDAP() {
@@ -32,8 +47,9 @@ ou2LDAP() {
 
 user2LDAP() {
     set +e
-    GNAME=$1
-    FNAME=$2
+    local FNAME=$1
+    local GNAME=$2
+    local GECOS="$(echo $GNAME $FNAME | iconv -f UTF-8 -t ASCII//TRANSLIT)"
     _USER="$3"
     _GROUP="$_USER"
 
@@ -59,6 +75,7 @@ user2LDAP() {
     _extractldif 5 | \
         sed -e "s|<GNAME>|$GNAME|g" \
         -e "s|<FNAME>|$FNAME|g" \
+        -e "s|<GECOS>|$GECOS|g" \
         -e "s|<PWHASH>|$PWHASH|g" \
         | _filterldif | _utf8encode | _ldapadd
     [ $? -eq 0 ] || end_die "Error adding user '$_USER' to LDAP."
@@ -131,10 +148,12 @@ MINCLS=2  # minimal number of character classes
 if [ ! -r "$FILE" ] ; then
     cat <<EOF
 Usage: add2gosa <file> [ou=<GOsa Department>[,ou=...] [--no-map]]
-Where <file> contains rows of first and last names:
 
-    <First Name> <Last Name>
-         ...        ...
+The UTF-8 or ASCII encoded <file> contains rows of last and first names,
+separated by a TAB:
+
+     <LastName>     <FirstName>
+        ....            ....
 
 Empty lines or lines starting with a '#' will be ignored.  The
 generated password is appended to the line during processing, the line
@@ -152,6 +171,7 @@ Examples:
 
            add2gosa <file>
 
+
   * add users to department 'ou=2013,ou=students', home directory
     '/<default>/students/2013/<username>':
 
@@ -162,6 +182,29 @@ EOF
     exit 1
 fi
 
+TMPFILE=$(mktemp)
+
+# Test if the input file is valid.
+# Remove all comments:
+grep -Ev "^(#|[[:space:]]*$)" $FILE | sed "s/\#.*//g" > $TMPFILE
+
+# Check number of columns and encoding:
+L=$(awk -F "\t" '{if (NF!=2) {print NR ":\t" $0;}}' $TMPFILE)
+E=$(file -b $TMPFILE)
+if [ "$E" != "UTF-8 Unicode text" ] && [ "$E" != "ASCII text" ] ; then
+    echo "ERROR:  The encoding of '${FILE}' seems to be: '$E'."
+    echo "        Convert '${FILE}' to UTF-8 and try again."
+    exit 1
+elif [ -n "$L" ] ; then
+    echo "$L"
+    echo "ERROR:  There are lines with more or less than 2 columns in '${FILE}', see above."
+    echo "        Fix the problematic lines and try again."
+    exit 1
+else
+    echo "Input file '${FILE}' seems to be valid."
+fi
+
+
 sync_nscd
 # Test if dn exists:
 _ldapsearch "$SUFFIX" "(objectClass=organizationalUnit)" "dn" \
@@ -176,20 +219,23 @@ echo
 
 chmod 600 $FILE
 IFS=$'\n'
-for LINE in $(grep -Ev "^(#|[[:space:]]*$)" $FILE | sed "s/\#.*//g" | awk '{print $1, $2, $3}') ; do
-    GNAME=`echo "$LINE" | cut -d " " -f1`
-    FNAME=`echo "$LINE" | cut -d " " -f2`
-    USERNAME=$(mk_uname ${GNAME} ${FNAME})
+for LINE in $(awk '{print $0}' $TMPFILE) ; do
+    FNAME=$(echo "$LINE" | awk -F "\t" '{print $1}')
+    GNAME=$(echo "$LINE" | awk -F "\t" '{print $2}')
+    # Create $USERNAME:
+    USERNAME=$(mk_uname "$FNAME" "$GNAME")
     echo "---------------- $USERNAME ----------------"
     PASSWD=$(createPASSWD)
     PWHASH=$(slappasswd -s $PASSWD -h {SSHA})
     echo "Password and hash created."
-    sed -i "s|\($GNAME[[:space:]]\+$FNAME\)|\# \1:\t $USERNAME\t ${PASSWD}|" $FILE
-    user2LDAP "$GNAME" "$FNAME" "$USERNAME" "$PWHASH"
+    # Add username and password to $FILE (only first occurence):
+    sed -i "0,/^[[:space:]]*\($FNAME[[:space:]]\+$GNAME\)[[:space:]]*$/s||\# \1\t$USERNAME\t${PASSWD}|" $FILE
+    user2LDAP "$FNAME" "$GNAME" "$USERNAME" "$PWHASH"
     USERDN="dn=uid=$USERNAME,$USUFFIX,$SUFFIX"
     kadmin.local -q "add_principal -pw "$PASSWD" -x $USERDN $USERNAME"
     echo
 done
+rm $TMPFILE
 
 cat <<EOF
    ===================== IMPORTANT NOTICE =====================
@@ -224,7 +270,7 @@ end_ok
 #####sn: <FNAME>
 #####givenName: <GNAME>
 #####cn: <GNAME> <FNAME>
-#####gecos: <GNAME> <FNAME>
+#####gecos: <GECOS>
 #####uid: <user>
 #####homeDirectory: <home>
 #####loginShell: <shell>
diff --git a/fai/config/files/usr/local/sbin/debian-lan/SERVER_A b/fai/config/files/usr/local/sbin/debian-lan/SERVER_A
index 80414fb..0edf4cf 100755
--- a/fai/config/files/usr/local/sbin/debian-lan/SERVER_A
+++ b/fai/config/files/usr/local/sbin/debian-lan/SERVER_A
@@ -280,7 +280,7 @@ case $COMMAND in
                 esac
                 sed -i "0,/\(host ${NAME}.*\) A1:B2:C3:D4:E5:\w\{2\};/s//\1 ${HWaddr};/" ${DHCPCONF}
                 MACHINE=$(grep $HWaddr ${DHCPCONF} | awk -F " " '{print $2}')
-                echo -n "MAC address $HWaddr added as: ${MACHINE}"
+                echo "MAC address $HWaddr added as: ${MACHINE}"
                 add2log ${MACHINE}
             fi
             echo

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/collab-maint/debian-lan.git



More information about the debian-lan-devel mailing list