1 #!/usr/bin/python 2 # Abuse the docstring so running this with /bin/sh will do the right thing 3 """exec" "/usr/bin/python" "$0""" 4 __doc__ = "Create a local user based on a launchpad account" 5 __all__ = ["create_user"] 6 7 import os, pwd, subprocess, urllib2, xml.dom.minidom 8 9 lp = "https://launchpad.net/~" 10 11 def verify_exists(lpuser): 12 try: 13 urllib2.urlopen(lp + lpuser) 14 return True 15 except:
1 #!/usr/bin/python
2 # Abuse the docstring so running this with /bin/sh will do the right thing
3 """exec" "/usr/bin/python" "$0"""
4 __doc__ = "Create a local user based on a launchpad account"
5 __all__ = ["create_user"]
6
7 import os, pwd, subprocess, urllib2, xml.dom.minidom
8
9 lp = "https://launchpad.net/~"
10
11 def verify_exists(lpuser):
12 try:
13 urllib2.urlopen(lp + lpuser)
14 return True
15 except:
16 return False
17
18 def get_realname(lpuser):
19 rdf = urllib2.urlopen(lp + lpuser + "/+rdf").read()
20 tree = xml.dom.minidom.parseString(rdf)
21 return tree.getElementsByTagName("foaf:name")[0].firstChild.nodeValue
22
23 def create_user(local_name, launchpad_name=None):
24 if not launchpad_name:
25 launchpad_name = local_name
26
27 # Sanity checking
28 try:
29 pwd.getpwnam(local_name)
30 except:
31 pass
32 else:
33 raise LookupError("%s already exists on your local system" % local_name)
34 if not verify_exists(launchpad_name):
35 raise LookupError("%s does not have a launchpad account" % launchpad_name)
36 try:
37 sshkey = urllib2.urlopen(lp + launchpad_name + "/+sshkeys").read()
38 except:
39 raise LookupError("%s has not yet uploaded his/her SSH key to launchpad" % launchpad_name)
40
41 # Add the user
42 realname = get_realname(launchpad_name)
43 if subprocess.call(["/usr/sbin/adduser",
44 "--gecos", realname,
45 "--disabled-password",
46 local_name]):
47 raise RuntimeError("Could not create user %s" % local_name)
48 user = pwd.getpwnam(local_name)
49 os.mkdir(os.path.join(user.pw_dir, ".ssh"), 0700)
50 os.chown(os.path.join(user.pw_dir, ".ssh"), user.pw_uid, user.pw_gid)
51 fd = os.open(os.path.join(user.pw_dir, ".ssh", "authorized_keys2"), os.O_WRONLY | os.O_CREAT, 0600)
52 os.write(fd, sshkey)
53 os.close(fd)
54 os.chown(os.path.join(user.pw_dir, ".ssh", "authorized_keys2"), user.pw_uid, user.pw_gid)
55
56 if __name__ == '__main__':
57 import optparse, sys
58
59 parser = optparse.OptionParser(usage="%prog [options] name\nCreate a local account based on a launchpad.net account")
60 parser.add_option('-l', '--launchpad-name',
61 dest="launchpad_name",
62 default=None,
63 help="Use this launchpad account",
64 metavar="NAME")
65 opts, args = parser.parse_args()
66
67 if len(args) != 1:
68 parser.print_help()
69 sys.exit(1)
70
71 if os.geteuid() != 0:
72 print >>sys.stderr, "Only root can add users to the system"
73 sys.exit(1)
74
75 local_name = args[0]
76 launchpad_name = opts.launchpad_name or local_name
77 try:
78 create_user(local_name, launchpad_name)
79 except LookupError, e:
80 print >>sys.stderr, e.message
81 sys.exit(1)
Show all