def deploy_sysdb(comm, opts): """Put inital data to SysDB. @param comm a communicator. The following proxies should be available: - Tartarus.deployPrx.UserManager of type Tartarus::SysDB::UserManager - Tartarus.deployPrx.GroupManager of type Tartarus::SysDB::GroupManager - Tartarys.deployPrx.SysDBService of type Tartarus::core::Service @param opts a dictionary { option name : option value }. The following options are used *Name* *Type* *Madatory* *Comment* name String M n/a """ prx = comm.propertyToProxy('Tartarus.deployPrx.SysDBService') _checked_configure(prx, opts.get('sysdb_force')) prx = comm.propertyToProxy('Tartarus.deployPrx.UserManager') um = SysDB.UserManagerPrx.checkedCast(prx) prx = comm.propertyToProxy('Tartarus.deployPrx.GroupManager') gm = SysDB.GroupManagerPrx.checkedCast(prx) admins_gid = gm.create(SysDB.GroupRecord(-1, "netadmins", "Network administrators")) users_gid = gm.create(SysDB.GroupRecord(-1, "netusers", "Network users")) uid = um.create(SysDB.UserRecord(-1, admins_gid, opts['name'], "System administrator")) gm.addUsers(users_gid, [uid])
def _group_exists(self, con, gid): cur = self._dbh.execute(con, "SELECT name FROM groups WHERE gid == %s", gid - self._go) if len(cur.fetchall()) != 1: raise I.GroupNotFound("Group not found", "searching for group in database", gid)
def delete(self, con, gid, current): n = gid - self._go cur = self._dbh.execute(con, "DELETE FROM groups WHERE gid == %s", n) if cur.rowcount != 1: raise I.GroupNotFound("Group not found", "deleting group", gid) con.commit()
def getById(self, con, uid, current): cur = self._dbh.execute(con, _user_query + " WHERE uid == %s", uid - self._uo) res = cur.fetchall() if len(res) == 1: return self._db2users(res)[0] #XXX: RETURN USER WITH gid=-1 raise I.UserNotFound("User not found", "retrieving user by id", uid)
def getByName(self, con, name, current): cur = self._dbh.execute(con, "SELECT gid, name, description FROM groups " "WHERE name == %s", name) res = cur.fetchall() if len(res) != 1: raise I.GroupNotFound("Group not found", "Could not get group information for %s" % name, -1) return self._db2groups(res)[0]
def getById(self, con, gid, current): cur = self._dbh.execute(con, "SELECT gid, name, description FROM groups " "WHERE gid == %s", gid - self._go) res = cur.fetchall() if len(res) != 1: raise I.GroupNotFound("Group not found", "retrieving group by id" ,gid) return self._db2groups(res)[0]
def getByName(self, con, name, current): cur = self._dbh.execute(con, _user_query + " WHERE users.name == %s", name) res = cur.fetchall() if len(res) == 1: return self._db2users(res)[0] #XXX: RETURN USER WITH gid=-1 raise I.UserNotFound("User not found", "retrieving data for user %s" % name, -1)
def sysdb_deploy(wiz): wiz.dialog.info('Configuring SysDB...') wiz.sysdb_service.configure({'force': 'force'}) prx = wiz.comm.propertyToProxy('Tartarus.deployPrx.UserManager') um = SysDB.UserManagerPrx.checkedCast(prx) prx = wiz.comm.propertyToProxy('Tartarus.deployPrx.GroupManager') gm = SysDB.GroupManagerPrx.checkedCast(prx) admins_gid = gm.create(SysDB.GroupRecord(-1, "netadmins", "Network administrators")) users_gid = gm.create(SysDB.GroupRecord(-1, "netusers", "Network users")) for name, full_name, _, is_adm in wiz.opts['users']: if is_adm: uid = um.create(SysDB.UserRecord(-1, admins_gid, name, full_name)) gm.addUsers(users_gid, [uid]) else: um.create(SysDB.UserRecord(-1, users_gid, name, full_name))
def getGroupsForUserId(self, con, uid, current): cur = self._dbh.execute(con, "SELECT groups.gid " "FROM groups, group_entries " "WHERE groups.gid == group_entries.gid " "AND group_entries.uid == %s", uid - self._uo) res = cur.fetchall() if len(res) == 0: #user definitly has primary group... raise I.UserNotFound("User not found", "retrieving groups for user id", uid) return [ x[0] + self._go for x in res ]
def delUserFromGroups(self, con, uid, groups, current): ids = tuple((i - self._go for i in groups)) ps = '(' + ', '.join(('%s' for x in ids)) +')' cur = self._dbh.execute(con, "DELETE FROM real_group_entries " "WHERE uid == %s AND gid IN " + ps, uid - self._uo, *ids) if cur.rowcount != len(groups): if current.ctx.get("PartialStrategy") != "Partial": #XXX: search for wrong uid and raise I.GroupNotFound raise I.GroupNotFound("Group not found", "Some groups were not found", -1) con.commit()
def getUsers(self, con, userids, current): ids = tuple((i - self._uo for i in userids)) ps = '(' + ', '.join(('%s' for x in ids)) + ')' cur = self._dbh.execute(con, _user_query + " WHERE uid IN " + ps, *ids) res = self._db2users(cur.fetchall()) if (len(res) != len(userids) and current.ctx.get("PartialStrategy") != "Partial"): retrieved = set((u.uid for u in res)) for i in userids: if i not in retrieved: raise I.UserNotFound("User not found", "retrieving multiple users", i) return res
def getGroupsForUserName(self, con, name, current): cur = self._dbh.execute(con, "SELECT groups.gid " "FROM groups, group_entries, users " "WHERE groups.gid == group_entries.gid " "AND group_entries.uid == users.uid " "AND users.name == %s", name) res = cur.fetchall() if len(res) == 0: #user definitly has primary group... raise I.UserNotFound("User not found", "Could not find user %s" % name, -1) return [ x[0] + self._go for x in res ]
def getGroups(self, con, groupids, current): ids = tuple((i - self._go for i in groupids)) ps = '(' + ', '.join(('%s' for x in ids)) +')' cur = self._dbh.execute(con, "SELECT gid, name, description FROM groups " + "WHERE gid IN " + ps, *ids) res = self._db2groups(cur.fetchall()) if (len(res) != len(groupids) and current.ctx.get("PartialStrategy") != "Partial"): retrieved = set( (g.gid for g in res) ) for i in groupids: if i not in retrieved: raise I.GroupNotFound("Group not found", "retrieving multiple groups", i) return res
def modify(self, con, group, current): if not self._good_name.match(group.name): raise C.ValueError("Invalid group name: %s" % group.name) try: cur = self._dbh.execute(con, "UPDATE groups SET " "name=%s, description=%s " "WHERE gid == %s", group.name, group.description, group.gid - self._go) except self._dbh.IntegrityError: raise C.AlreadyExistsError("Group already exists", group.name) if cur.rowcount != 1: raise I.GroupNotFound("Group not found", "modifying group", group.gid) con.commit()
def modify(self, con, user, current): if not self._good_name.match(user.name): raise C.ValueError("Invalid user name: %s" % user.name) uid = user.uid - self._uo gid = user.gid - self._go try: cur = self._dbh.execute( con, "UPDATE users SET " "name=%s, gid=%s, fullname=%s, shell=%s " "WHERE uid == %s", user.name, gid, user.fullName, user.shell, uid) except self._dbh.IntegrityError: raise C.AlreadyExistsError("User already exists", user.name) if cur.rowcount != 1: raise I.UserNotFound("User not found", "changing user record", user.uid) con.commit()
def _db2users(self, mas): return [I.UserRecord(uid + self._uo, gid + self._go, str(name), str(fn), s) for uid, gid, name, fn, s in mas]
def _db2groups(self, mas): return [I.GroupRecord(gid + self._go, str(name), str(descr)) for gid, name, descr in mas]
def delete(self, con, uid, current): cur = self._dbh.execute(con, "DELETE FROM users WHERE uid=%s", uid - self._uo) if cur.rowcount != 1: raise I.UserNotFound("User not found", "deleting user", uid) con.commit()