[ale] Useradd Replacement or Upgrade

Mike Harrison meuon at geeklabs.com
Thu Jul 27 11:48:28 EDT 2006


> > that allows you to add new users that have a period in the username?  I 
> > would prefer source code, but if there is a working binary already 

Binary? It's so shell/perl/php scriptable, why bother. 
There is no magic to useradd.. the issue really is how do you WANT to lay 
out users directories and such. Here is a SIMPLE perl script that should
have more sanity checks in it, but it works for me and you should be able 
to modify it however you need for your system(s). 


#!/usr/bin/perl
#Makes a user for domain/login Courier Mail Server or general user.
#syntax:  mailuser.pl login at domain.com|Jon Doe|passwd

# if not exist will create
# if exists will reset password / dirs / perms

$input = <@ARGV> ;
@d = split(/\|/,$input) ;
$login = $d[0] ;
$junk = shift ;
$textpasswd = shift ;
($prelogin,$domain) = split(/\@/,$d[0]) ;
open(PW,"/etc/passwd") ;
$exists = "F" ;
while(<PW>) {
  @p = split(/\:/) ;
  $uid = $p[2] ;
  if ($uid > $maxuid) { $maxuid = $uid ; } ;
  if(/^$login/) { print "Login $login Exists\n" ; $exists = "T" ; } ;
} ;
close PW ;
$passwd = crypt($textpasswd,"Z8") ;
if ($exists eq "F") {
  mkdir("/home/domains/$domain") ;
  $uid = $maxuid + 1 ;
  open(PWW,">>/etc/passwd") ;
  print PWW "$login:x:$uid:100::/home/domains/$domain/$prelogin:/sbin/nologin\n" ;
  close PWW ;
  open(PWS,">>/etc/shadow") ;
  print PWS "$login:$passwd:12733:0:99999:7:::\n" ;
  close PWS ;
  system("cp -r /etc/skel /home/domains/$domain/$prelogin") ;
} ;
system("usermod -p $passwd $login") ;
system("chown -R $login:users /home/domains/$domain/$prelogin") ;





More information about the Ale mailing list