#!/bin/bash
##############################################################################
#                                                                            #
#  This script creates users using data stored in a file with the following  #
#  format:                                                                   #
#    - one line per userinformation                                          #
#    - datafields are seperated by colons (:)                                #
#    - Syntax of the datarecord:                                             #
#                                                                            #
#          username:initial group:[other groups]:[comment]:[shell]:password  #
#          - initial group: the user's starting group                        #
#          - other groups: comma seperated list of all groups the user is in #
#            (no spaces!!!!)                                                 #
#          - comment: full username, for example (no colons (:) !!!!)        #
#          - shell: if left out, the user is given the standard shell.       #
#          - password: plain password (no colons (:) !!!!)                   #
#                                                                            #
#    - datafields in "[..]" are optional (DON'T use a SPACE to leave them    #
#      empty, just don't put anything between the colons "::" !!!)           #
#                                                                            #
#  Copyright: Released 1998 under the terms of the GNU GPL                   #
#  Written 1998 by Christian Ordig <chr.ordig@gmx.net>                       #
#                                                                            #
##############################################################################

echo "Written 1998 by Christian Ordig <chr.ordig@gmx.net>"
if [ "$LOGNAME" != "root" ]; then echo; echo "You must be root to create users !"; exit; fi
if [ "$1" == "$NULL" ]; then echo; echo "Syntax: mkuser file_with_userinfos"; exit; fi

#### Users are created now ...

echo "Creating users ..."
eval `awk -F ':' \
    '{printf ("useradd -m -g %s ",$2)}; \
    length($3)>0 {printf ("-G %s ",$3)}; \
    length($5)>0 {printf ("-s %s ",$5)}; \
    length($4)>0 {printf ("-c %s ",$4)}; \
    {printf ("%s\n;",$1)};' $1`


#### The password is encrypted and written to /etc/shadow now ...
    
echo "Giving each user his password ..."
awk -F ':' \
    '{i++; u[i]=$1; p[i]=$6}; \
    END {lines='`awk "{l+=1}; END {print l};" /etc/shadow`'; \
    for (a=1;a<=i;a++) \
    { \
	name=sprintf("/tmp/p%s",a);
	com=sprintf("`cat %s | crypt > %s`",name,name);
	print p[a] > name; close(name);
	system (com);
	getline crypted[a] < name;
	com=sprintf("rm %s",name);
	system(com);
	while (x<=lines) { \
	    x++;getline < "/etc/shadow"; \
	    if ($1~u[a]) \
		{printf ("%s:%s:%s:%s:%s:%s:%s:%s:%s\n",$1,crypted[a],$3,$4,$5,$6,$7,$8,$9) > "/tmp/shadow2"; break} \
	    else 
		{print $0 > "/tmp/shadow2"}; 
	}; 
    }; 
    };' $1
    mv /tmp/shadow2 /etc/shadow