#!/usr/bin/env python # Copyright (C) 2012 Peter Hatina # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, see . import os import sys from cura.cura_options import CuraBasicOptions from cura.cura_address import CuraIpv4Addr, CuraIpv4AddrGenerator from cura.cura_client_user import CuraUserClient class CuraUserOptions(CuraBasicOptions): def __init__(self): script_name = os.path.basename(__file__) super(self.__class__, self).__init__( "Available actions:\n" " list-users, list-groups, group-members\n" " useradd\n\n" "Group-members:\n" " " + script_name + " [options] group-members groupName\n\n" "Useradd:\n" " " + script_name + " [options] useradd newUsername\n\n") self.m_parser.set_usage("Usage: %prog [options] action") self.m_parser.add_option("-s", "--shell", action = "store", dest = "shell", help = "login shell of the new account") self.m_parser.add_option("-r", "--system-account", action = "store_true", dest = "system_account", help = "create a system account") self.m_parser.add_option("-m", "--create-home", action = "store_true", dest = "create_home", help = "create the user's home directory") self.m_parser.add_option("-M", "--no-create-home", action = "store_false", dest = "create_home", help = "do not create the user's home directory") self.m_parser.add_option("-N", "--no-user-group", action = "store_false", dest = "create_group", help = "do not create a group with the same name as the user") self.m_parser.add_option("-g", "--gid", action = "store", dest = "gid", help = "name or ID of the primary group of the new account") self.m_parser.add_option("", "--gecos", action = "store", dest = "gecos", help = "GECOS information for new user") self.m_parser.add_option("-d", "--home-dir", action = "store", dest = "home_dir", help = "home directory of the new account") self.m_parser.add_option("-n", "--new-password", action = "store", dest = "new_password", help = "password for the new account") self.m_parser.add_option("-i", "--uid", action = "store", dest = "uid", help = "user ID of the new account") @property def good(self): if not len(self.m_pos_options): return False elif self.m_pos_options[0] == "group-members": return len(self.m_pos_options) == 2 elif self.m_pos_options[0] == "useradd": return len(self.m_pos_options) == 2 return len(self.m_pos_options) == 1 @property def action(self): return self.m_pos_options[0] if self.good and self.m_pos_options[0] else "" @property def group(self): return self.m_pos_options[1] if self.good and self.m_pos_options[1] else "" @property def newUsername(self): return self.m_pos_options[1] if self.good and self.m_pos_options[1] else "" @property def shell(self): return self.m_options.shell if self.good and self.m_options.shell else "" @property def systemAccount(self): return self.m_options.system_account if self.good and self.m_options.system_account else False @property def createHome(self): return self.m_options.create_home in (None, True) if self.good else False @property def createGroup(self): return self.m_options.create_group in (None, True) if self.good else True @property def gid(self): return self.m_options.gid if self.good and self.m_options.gid else "" @property def gecos(self): return self.m_options.gecos if self.good and self.m_options.gecos else "" @property def homeDir(self): return self.m_options.home_dir if self.good and self.m_options.home_dir else "" @property def newPassword(self): return self.m_options.new_password if self.good and self.m_options.new_password else "" @property def uid(self): return self.m_options.uid if self.good and self.m_options.uid else "" if __name__ == "__main__": options = CuraUserOptions() options.parse(sys.argv) if not options.good: sys.stderr.write( "Wrong tool usage. Run " + __file__ + " --help for detailed help.\n") sys.exit(1) client_failed = False client_hostnames = CuraIpv4AddrGenerator(options.hostname).enumerate() client_action = options.action.lower() for client_hostname in client_hostnames: if not len(client_hostname): continue client = CuraUserClient(client_hostname, options.username, options.password) actions = { "list-users" : client.listUsers, "list-groups" : client.listGroups } if client_action == "group-members": (rval, rparam) = client.listGroupMembers(options.group) elif client_action == "useradd": (rval, rparam) = client.userAdd( options.newUsername, shell = options.shell, systemAccount = options.systemAccount, createHome = options.createHome, createGroup = options.createGroup, homeDir = options.homeDir, uid = options.uid, gid = options.gid, password = options.newPassword, gecos = options.gecos) elif client_action in actions: (rval, rparam) = actions[client_action]() else: sys.stderr.write("No such action to perform!\n") sys.exit(1) if rval: sys.stdout.write("%s: %s\n" % (client_hostname, rparam if rparam else "ok")) else: sys.stderr.write("%s: %s\n" % (client_hostname, rparam)) client_failed = True sys.exit(client_failed)