#!/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,1999 by Christian Ordig <chr.ordig@gmx.net>                  #
#                                                                            #
#  Changes:                                                                  #
#   1998 (July): initial release                                             #
#   1999 (May) : fixed a problem which corrupted the shadow file when there  #
#                are duplicate usernames                                     #
#	                                                                     #
#  Attention: NIS/NIS+ is not supported, yet!                                #
##############################################################################

echo "Written 1998, 1999 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

#### Checking for duplicate usernames

echo "Checking for duplicate usernames..."
awk -F ':' \
    '{i++;new_user[i]=$1};
    END {lines='`awk "{l+=1}; END {print l};" /etc/shadow`'; \
	pid='`echo $$`'; \
	while (x<lines) { \
	    x++;
    	    getline < "/etc/passwd"; \
	    user[x]=$1; \
	} \
    for (nu=1;nu<=i;nu++) { \
	for (u=1;u<=lines;u++) { \
	    if (new_user[nu]~user[u]) { \
		printf ("User %s already exists!\n",user[u]); \
		flag=1;
	    } \
	} \
    } \
    if (flag~1) {
	print(" "); \
	print ("You are trying to create users which already exist!");\
	print ("mkusers will be aborted and no user will be created!");\
	kill=sprintf("kill %s",pid); \
	system (kill); \
    } \
}' $1
    


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