Index: trunk/tools/subversion/user-management/homedirectorymanager.py |
— | — | @@ -38,8 +38,9 @@ |
39 | 39 | self.dryRun = False |
40 | 40 | self.debugStatus = False |
41 | 41 | |
42 | | - os.system('nscd -i passwd') |
43 | | - os.system('nscd -i group') |
| 42 | + if (os.path.exists('/usr/sbin/nscd')): |
| 43 | + os.system('nscd -i passwd') |
| 44 | + os.system('nscd -i group') |
44 | 45 | |
45 | 46 | def run(self): |
46 | 47 | parser = OptionParser(conflict_handler="resolve") |
— | — | @@ -114,31 +115,35 @@ |
115 | 116 | alreadyCreated = [] |
116 | 117 | |
117 | 118 | for user in users.keys(): |
118 | | - if user not in self.excludedFromCreation: |
119 | | - if os.path.exists(self.savedir + user): |
120 | | - # User's home directory already exists |
121 | | - alreadyCreated.append(user) |
122 | | - continue |
123 | | - if not os.path.exists(self.basedir + user): |
124 | | - self.log( "Creating a home directory for %s at %s%s" % (user, self.basedir, user) ) |
125 | | - if not self.dryRun: |
126 | | - os.mkdir(self.basedir + user, 0700) |
127 | | - os.mkdir(self.basedir + user + '/.ssh', 0700) |
128 | | - self.writeKeys(user, users[user]['sshPublicKey']) |
129 | | - os.chmod(self.basedir + user + '/.ssh/authorized_keys', 0600) |
130 | | - for skeldir,skels in self.skelFiles.iteritems(): |
131 | | - for skel in skels: |
132 | | - shutil.copy(skeldir + skel, self.basedir + user + "/") |
133 | | - os.chmod(self.basedir + user + "/" + skel, 0600) |
134 | | - newGid = users[user]['gidNumber'] |
135 | | - newUid = users[user]['uidNumber'] |
136 | | - os.chown(self.basedir + user, newUid, newGid) |
137 | | - for root, dirs, files in os.walk(self.basedir + user): |
138 | | - for name in files: |
139 | | - os.chown(os.path.join(root, name), newUid, newGid) |
140 | | - for name in dirs: |
141 | | - os.chown(os.path.join(root, name), newUid, newGid) |
| 119 | + if user in self.excludedFromCreation: |
| 120 | + continue |
142 | 121 | |
| 122 | + if os.path.exists(self.savedir + user): |
| 123 | + # User's home directory already exists |
| 124 | + alreadyCreated.append(user) |
| 125 | + continue |
| 126 | + |
| 127 | + if os.path.exists(self.basedir + user): |
| 128 | + continue |
| 129 | + |
| 130 | + self.log( "Creating a home directory for %s at %s%s" % (user, self.basedir, user) ) |
| 131 | + self.mkdir(self.basedir + user, 0700) |
| 132 | + self.mkdir(self.basedir + user + '/.ssh', 0700) |
| 133 | + self.writeKeys(user, users[user]['sshPublicKey']) |
| 134 | + self.chmod(self.basedir + user + '/.ssh/authorized_keys', 0600) |
| 135 | + for skeldir,skels in self.skelFiles.iteritems(): |
| 136 | + for skel in skels: |
| 137 | + shutil.copy(skeldir + skel, self.basedir + user + "/") |
| 138 | + self.chmod(self.basedir + user + "/" + skel, 0600) |
| 139 | + newGid = users[user]['gidNumber'] |
| 140 | + newUid = users[user]['uidNumber'] |
| 141 | + self.chown(self.basedir + user, newUid, newGid) |
| 142 | + for root, dirs, files in self.walk(self.basedir + user): |
| 143 | + for name in files: |
| 144 | + self.chown(os.path.join(root, name), newUid, newGid) |
| 145 | + for name in dirs: |
| 146 | + self.chown(os.path.join(root, name), newUid, newGid) |
| 147 | + |
143 | 148 | if alreadyCreated != []: |
144 | 149 | self.log( "The following users already have a home directory in the SAVE directory: " + ", ".join(alreadyCreated) ) |
145 | 150 | |
— | — | @@ -169,9 +174,7 @@ |
170 | 175 | |
171 | 176 | # Write a list of keys to the user's authorized_keys file |
172 | 177 | def writeKeys(self, user, keys): |
173 | | - f = open(self.basedir + user + '/.ssh/authorized_keys', 'w') |
174 | | - f.writelines(keys) |
175 | | - f.close() |
| 178 | + self.writeFile(self.basedir + user + '/.ssh/authorized_keys', ''.join(keys)) |
176 | 179 | |
177 | 180 | # Moved deleted users to SAVE |
178 | 181 | def moveUsers(self, users): |
— | — | @@ -196,58 +199,61 @@ |
197 | 200 | self.deleteUser(userdir) |
198 | 201 | |
199 | 202 | def renameUser(self, olduserdir, newuserdir): |
200 | | - self.log( "Moving " + self.basedir + olduserdir + " to " + self.basedir + newuserdir ) |
201 | | - if not self.dryRun: |
202 | | - os.rename(self.basedir + olduserdir, self.basedir + newuserdir) |
| 203 | + self.rename(self.basedir + olduserdir, self.basedir + newuserdir) |
203 | 204 | |
204 | 205 | def deleteUser(self, userdir): |
205 | 206 | # User has been deleted, move user's home directory to SAVE |
206 | 207 | if os.path.isdir(self.savedir + userdir): |
207 | 208 | self.log( userdir + " exists at both " + self.basedir + userdir + " and " + self.savedir + userdir ) |
208 | 209 | else: |
209 | | - self.log( "Moving " + self.basedir + userdir + " to " + self.savedir + userdir ) |
210 | | - if not self.dryRun: |
211 | | - os.rename(self.basedir + userdir, self.savedir + userdir) |
| 210 | + self.rename(self.basedir + userdir, self.savedir + userdir) |
212 | 211 | |
213 | 212 | # Changes the group ownership of a directory when a user's gid changes |
214 | 213 | def changeGid(self, users): |
215 | 214 | for userdir in os.listdir(self.basedir): |
216 | | - if os.path.isdir(self.basedir + userdir) and userdir not in self.excludedFromModification: |
217 | | - stat = os.stat(self.basedir + userdir) |
218 | | - gid = stat.st_gid |
219 | | - if userdir in users.keys() and users[userdir]["gidNumber"] != gid: |
220 | | - newGid = users[userdir]["gidNumber"] |
221 | | - self.log( "Changing group ownership of %s%s to %s; was set to %s" % (self.basedir, userdir, newGid, gid) ) |
222 | | - if not self.dryRun: |
223 | | - # Python doesn't have a recursive chown, so we have to walk the directory |
224 | | - # and change everything manually |
225 | | - self.logDebug("Doing chgrp for: " + self.basedir + userdir + " with gid: " + str(gid)) |
226 | | - os.chown(self.basedir + userdir, -1, newGid) |
227 | | - for root, dirs, files in os.walk(self.basedir + userdir): |
228 | | - for name in files: |
229 | | - os.chown(os.path.join(root, name), -1, newGid) |
230 | | - for name in dirs: |
231 | | - os.chown(os.path.join(root, name), -1, newGid) |
| 215 | + if not os.path.isdir(self.basedir + userdir) or userdir in self.excludedFromModification: |
| 216 | + continue |
232 | 217 | |
| 218 | + stat = os.stat(self.basedir + userdir) |
| 219 | + gid = stat.st_gid |
| 220 | + if userdir not in users.keys() or users[userdir]["gidNumber"] == gid: |
| 221 | + continue |
| 222 | + |
| 223 | + newGid = users[userdir]["gidNumber"] |
| 224 | + self.log( "Changing group ownership of %s%s to %s; was set to %s" % (self.basedir, userdir, newGid, gid) ) |
| 225 | + |
| 226 | + # Python doesn't have a recursive chown, so we have to walk the directory |
| 227 | + # and change everything manually |
| 228 | + self.logDebug("Doing chgrp for: " + self.basedir + userdir + " with gid: " + str(gid)) |
| 229 | + self.chown(self.basedir + userdir, -1, newGid) |
| 230 | + for root, dirs, files in os.walk(self.basedir + userdir): |
| 231 | + for name in files: |
| 232 | + self.chown(os.path.join(root, name), -1, newGid) |
| 233 | + for name in dirs: |
| 234 | + self.chown(os.path.join(root, name), -1, newGid) |
| 235 | + |
233 | 236 | # Changes the ownership of a directory when a user's uid changes |
234 | 237 | def changeUid(self, users): |
235 | 238 | for userdir in os.listdir(self.basedir): |
236 | | - if os.path.isdir(self.basedir + userdir) and userdir not in self.excludedFromModification: |
237 | | - stat = os.stat(self.basedir + userdir) |
238 | | - uid = stat.st_uid |
239 | | - if userdir in users.keys() and users[userdir]["uidNumber"] != uid: |
240 | | - newUid = users[userdir]["uidNumber"] |
241 | | - self.log( "Changing ownership of %s%s to %s; was set to %s" % (self.basedir, userdir, newUid, uid) ) |
242 | | - if not self.dryRun: |
243 | | - # Python doesn't have a recursive chown, so we have to walk the directory |
244 | | - # and change everything manually |
245 | | - os.chown(self.basedir + userdir, newUid, -1) |
246 | | - for root, dirs, files in os.walk(self.basedir + userdir): |
247 | | - for name in files: |
248 | | - os.chown(os.path.join(root, name), newUid, -1) |
249 | | - for name in dirs: |
250 | | - os.chown(os.path.join(root, name), newUid, -1) |
| 239 | + if not os.path.isdir(self.basedir + userdir) or userdir in self.excludedFromModification: |
| 240 | + continue |
251 | 241 | |
| 242 | + stat = os.stat(self.basedir + userdir) |
| 243 | + uid = stat.st_uid |
| 244 | + if userdir not in users.keys() or users[userdir]["uidNumber"] == uid: |
| 245 | + continue |
| 246 | + |
| 247 | + newUid = users[userdir]["uidNumber"] |
| 248 | + self.log( "Changing ownership of %s%s to %s; was set to %s" % (self.basedir, userdir, newUid, uid) ) |
| 249 | + # Python doesn't have a recursive chown, so we have to walk the directory |
| 250 | + # and change everything manually |
| 251 | + self.chown(self.basedir + userdir, newUid, -1) |
| 252 | + for root, dirs, files in os.walk(self.basedir + userdir): |
| 253 | + for name in files: |
| 254 | + self.chown(os.path.join(root, name), newUid, -1) |
| 255 | + for name in dirs: |
| 256 | + self.chown(os.path.join(root, name), newUid, -1) |
| 257 | + |
252 | 258 | def log(self, logstring): |
253 | 259 | print datetime.datetime.now().strftime("%m/%d/%Y - %H:%M:%S - ") + logstring |
254 | 260 | |
— | — | @@ -255,6 +261,38 @@ |
256 | 262 | if self.debugStatus == True: |
257 | 263 | sys.stderr.write("Debug: " + logstring + "\n") |
258 | 264 | |
| 265 | + def chown(self, path, user, group): |
| 266 | + if not self.dryRun: |
| 267 | + os.chown(self.basedir + userdir, -1, newGid) |
| 268 | + if self.dryRun or self.debugStatus: |
| 269 | + self.log('chown %s %d %d' % (path, user, group)) |
| 270 | + |
| 271 | + def mkdir(self, path): |
| 272 | + if not self.dryRun: |
| 273 | + os.mkdir(path) |
| 274 | + if self.dryRun or self.debugStatus: |
| 275 | + self.log('mkdir %s' % (path)) |
| 276 | + |
| 277 | + def chmod(self, path, mode): |
| 278 | + if not self.dryRun: |
| 279 | + os.chmod(path) |
| 280 | + if self.dryRun or self.debugStatus: |
| 281 | + self.log('chmod %s %o' % (path, mode)) |
| 282 | + |
| 283 | + def writeFile(self, path, contents): |
| 284 | + if not self.dryRun: |
| 285 | + f = open(path, 'w') |
| 286 | + f.write(contents) |
| 287 | + f.close() |
| 288 | + if self.dryRun or self.debugStatus: |
| 289 | + self.log("\nwrite file %s:\n%s" % (path, contents)) |
| 290 | + |
| 291 | + def rename(self, oldPath, newPath): |
| 292 | + if not self.dryRun: |
| 293 | + os.rename(oldPath, newPath) |
| 294 | + if self.dryRun or self.debugStatus: |
| 295 | + self.log('rename %s %s' % (oldPath, newPath)) |
| 296 | + |
259 | 297 | def main(): |
260 | 298 | homeDirectoryManager = HomeDirectoryManager() |
261 | 299 | homeDirectoryManager.run() |
Index: trunk/tools/subversion/user-management/add-ldap-user |
— | — | @@ -25,10 +25,13 @@ |
26 | 26 | parser.add_option("--cn", action="store", dest="cn", help="The user's CN (default: firstname + lastname, or username, if first/last not set)") |
27 | 27 | parser.add_option("--firstname", action="store", dest="givenName", help="The user's first name (default: username)") |
28 | 28 | parser.add_option("--lastname", action="store", dest="sn", help="The user's last name (default: username)") |
| 29 | + parser.add_option("--dry-run", action="store_true", dest="dryRun", help="Dry run mode (no changes)") |
29 | 30 | (options, args) = parser.parse_args() |
30 | 31 | |
31 | 32 | if len(args) != 2: |
32 | 33 | parser.error("add-ldap-user expects exactly two arguments.") |
| 34 | + |
| 35 | + dryRun = parser.options.dryRun |
33 | 36 | |
34 | 37 | ldapSupportLib.setBindInfoByOptions(options, parser) |
35 | 38 | |
— | — | @@ -52,7 +55,7 @@ |
53 | 56 | c.setopt(c.WRITEFUNCTION, buffer.write) |
54 | 57 | c.perform() |
55 | 58 | c.close() |
56 | | - raw_keys = buffer.getvalue() |
| 59 | + raw_keys = buffer.getvalue().splitlines(True) |
57 | 60 | else: |
58 | 61 | file = open(keyLocation, 'r') |
59 | 62 | raw_keys = file.readlines() |
— | — | @@ -118,11 +121,16 @@ |
119 | 122 | userEntry['sn'] = sn |
120 | 123 | userEntry['cn'] = cn |
121 | 124 | |
122 | | - modlist = ldap.modlist.addModlist(userEntry) |
123 | | - ds.add_s(dn, modlist) |
| 125 | + if not dryRun: |
| 126 | + modlist = ldap.modlist.addModlist(userEntry) |
| 127 | + ds.add_s(dn, modlist) |
| 128 | + else: |
| 129 | + for key in userEntry.keys(): |
| 130 | + print "%s -> %s" % (key, userEntry[key]) |
124 | 131 | |
125 | 132 | userdict = {uid: {"uidNumber": int(uidNumber), "gidNumber": int(gidNumber), "sshPublicKey": keys}} |
126 | 133 | hdm = homedirectorymanager.HomeDirectoryManager() |
| 134 | + hdm.dryRun = self.dryRun |
127 | 135 | hdm.createHomeDir(userdict) |
128 | 136 | except ldap.UNWILLING_TO_PERFORM, msg: |
129 | 137 | sys.stderr.write("LDAP was unwilling to create the user. Error was: %s\n" % msg[0]["info"]) |