[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Making an adduser script (python)



Not really sure if this is a "dev" question or an "admin" question, but 
I'll start in dev since this involves programming.

I'm writing a python script to create a new user, doing all the things 
that Panther does in the Accounts System Preference Pane.  I culled the 
web and looked at various shell scripts and posts about what "adduser" 
should do on OS X.

This mostly started out as an exercise to find everything that is 
involved with creating a new user and as more experience in writing 
system tools in python.  I think I've got most of it almost done 
(including making the entry into /etc/httpd/users).

I have one vexing problem remaining:  I can't get the password to be 
set.

After a few quick google's, I settled on using pexpect to script 
passwd.  The script already has to run as root, so it should be simple. 
   I wrote a quick script, which works w/o problem:

	import pexpect
	
	user = "test"
	password = "test"
	
	pw = pexpect.spawn("passwd %s" % user)
	pw.expect("password:")
	pw.sendline(password)
	pw.expect("password:")
	pw.sendline(password)


I added this to my program as a procedure.  I commented out the main 
call to create the user and ran it. It worked as expected, changing the 
password of the user.

I destroy the user in System Preferences.   Then run my script.  User 
is created (as before), but the password is NOT set.

I'm a bit puzzled at what's going on. Why will it work when called on 
an existing user (even one made by my script) but not one made in the 
same run?  I've already tried putting a pause (5 seconds) in the 
script, and that didn't seem to work.

  I'm posting the entire script in my idisk, even though it's not 
finished yet.  Feel free to make any comments on what the script 
should/should not do.

	idisk: http://idisk.mac.com/tbrown-Public/
	web: http://homepage.mac.com/tbrown/FileSharing1.html
	
But here's the relevant sections (missing a few routines, curious can 
look at the full script).  Hopefully python isn't that bad to read here 
(the niutil lines do look a bit cryptic here).

import os, commands
#http://sourceforge.net/projects/pexpect/
import pexpect


def add_user(username, realname, password, home=None, 
shell="/bin/tcsh", gid=None):
     uid = find_next_user_uid()
     if home == None:
         home = "/Users/%s" % username

     createCmd = "niutil -create / /users/%s" % username
     doUnixCommand(createCmd)

     createPropCmd = 'niutil -createprop / /users/%s %s "%s"'

     doUnixCommand(createPropCmd % (username, "uid", uid))
     doUnixCommand(createPropCmd % (username, "realname", realname))
     doUnixCommand(createPropCmd % (username, "home", home))
     doUnixCommand(createPropCmd % (username, "shell", shell))
     doUnixCommand(createPropCmd % (username, "passwd", "*"))

     # under panther, new users aren't staff, they have a separate group 
for them.

     # if not passed a gid, create one based on the username
     # this is the default condition under panther
     # figure out what is needed to create an admin account?
     if gid==None:
         gid = create_group(username)

     doUnixCommand(createPropCmd % (username, "gid", gid))
     create_home(home, username, gid)
     create_apache_conf(username)
     # not working here, try it in main.
     #set_password(username, password)

def set_password(username, password):
     pw = pexpect.spawn("passwd %s" % username)
     pw.expect("password:")
     pw.sendline(password)
     pw.expect("password:")
     pw.sendline(password)
     print "password changed for %s" % username


def main():
     uid = find_next_user_uid()

     # should figure out running as root first

     #print "next user uid is %s" % uid
     #print "next system uid is %s" % find_next_system_uid()
     add_user("test", "Test User", "test")
     # works when called alone on an existing user
     # even one created by add_user in another run
     set_password("test", "test")