From b8932403b077b694b64b37118ad31b989f2c5947 Mon Sep 17 00:00:00 2001 From: David Howells Date: Thu, 15 May 2014 12:07:42 +0100 Subject: [PATCH] Implement "pts creategroup" Signed-off-by: David Howells --- suite/argparse.py | 48 +++++++++++++++++ suite/commands/pts/cg.py | 1 + suite/commands/pts/creategroup.py | 90 +++++++++++++++++++++++++++++++ suite/commands/pts/createuser.py | 2 +- 4 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 suite/commands/pts/cg.py create mode 100644 suite/commands/pts/creategroup.py diff --git a/suite/argparse.py b/suite/argparse.py index 07745b9..cfef3ba 100644 --- a/suite/argparse.py +++ b/suite/argparse.py @@ -87,6 +87,54 @@ def get_verbose(switch, params): set_verbosity() return params +def do_get_id(i): + if i == "": + raise AFSArgumentError("UID/GID identifier is empty string") + if not i.isnumeric() and (i[0] != "-" or not i[1:].isnumeric()): + raise AFSArgumentError("UID/GID identifier is not numeric") + return int(i) + +def get_id(switch, params): + return do_get_id(params[0]) + +def get_ids(switch, params): + ids = [] + for i in params: + ids.append(do_get_id(i)) + return ids + +def do_get_uid(i): + if i == "": + raise AFSArgumentError("UID identifier is empty string") + if not i.isnumeric(): + raise AFSArgumentError("UID identifier is a positive integer") + return int(i) + +def get_uid(switch, params): + return do_get_uid(params[0]) + +def get_uids(switch, params): + ids = [] + for i in params: + ids.append(do_get_uid(i)) + return ids + +def do_get_gid(i): + if i == "": + raise AFSArgumentError("GID identifier is empty string") + if i[0] != "-" or not i[1:].isnumeric(): + raise AFSArgumentError("GID identifier is a positive integer") + return int(i) + +def get_gid(switch, params): + return do_get_gid(params[0]) + +def get_gids(switch, params): + ids = [] + for i in params: + ids.append(do_get_gid(i)) + return ids + ############################################################################### # # Parse an argument list according to a defined set of switches. diff --git a/suite/commands/pts/cg.py b/suite/commands/pts/cg.py new file mode 100644 index 0000000..1d7b456 --- /dev/null +++ b/suite/commands/pts/cg.py @@ -0,0 +1 @@ +alias = "creategroup" diff --git a/suite/commands/pts/creategroup.py b/suite/commands/pts/creategroup.py new file mode 100644 index 0000000..ae9aada --- /dev/null +++ b/suite/commands/pts/creategroup.py @@ -0,0 +1,90 @@ +# +# AFS Server management toolkit: Create groups +# -*- coding: utf-8 -*- +# + +__copyright__ = """ +Copyright (C) 2014 Red Hat, Inc. All Rights Reserved. +Written by David Howells (dhowells@redhat.com) + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public Licence version 2 as +published by the Free Software Foundation. + +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 Licence for more details. + +You should have received a copy of the GNU General Public Licence +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +""" + +from afs.argparse import * +from afs.lib.output import * +import kafs +import sys + +help = "Create an (empty) Protection Database group entry" + +command_arguments = [ + [ "name", get_strings, "rm", "+" ], + [ "owner", get_string, "os" "" ], + [ "id", get_gids, "om", "+" ], + [ "cell", get_cell, "os", "" ], + [ "noauth", get_auth, "fn" ], + [ "localauth", get_auth, "fn" ], + [ "verbose", get_verbose, "fn" ], + [ "encrypt", get_dummy, "fn" ], + [ "force", get_dummy, "fn" ], +] + +cant_combine_arguments = [ + ( "cell", "localauth" ), + ( "noauth", "localauth" ), +] + +argument_size_limits = { + "name" : kafs.PR_MAXNAMELEN, +} + +description = r""" +Create an (empty) Protection Database group entry +""" + +def main(params): + exitcode = 0 + cell = params["cell"] + prcache = cell.get_prcache(params) + + oid = 0 + if "owner" in params: + oid = prcache.name_or_id_to_id(params["owner"]) + + names = params["name"] + if "id" in params: + ids = params["id"] + else: + ids = [] + + for i in range(0, len(names)): + name = names[i] + try: + verbose("Adding group ", name, "\n") + if i < len(ids): + new_id = int(ids[i]) + ret = cell.call_pt_server(params, kafs.PR_INewEntry, name, new_id, oid) + else: + ret = cell.call_pt_server(params, kafs.PR_NewEntry, name, kafs.PRGRP, oid) + new_id = ret.id + output("Group ", name, " has id ", new_id, "\n") + except kafs.AbortPREXIST: + error("Entry for name already exists ; unable to create group ", name, "\n") + if "force" not in params: + break + except kafs.AbortPRIDEXIST: + error("Entry for id already exists ; unable to create group ", name, " with id ", new_id, "\n") + if "force" not in params: + break + return exitcode diff --git a/suite/commands/pts/createuser.py b/suite/commands/pts/createuser.py index b8e8f20..065f65b 100644 --- a/suite/commands/pts/createuser.py +++ b/suite/commands/pts/createuser.py @@ -30,7 +30,7 @@ help = "Create a user or machine entry in the Protection Database" command_arguments = [ [ "name", get_strings, "rm", "+" ], - [ "id", get_strings, "om", "+" ], + [ "id", get_uids, "om", "+" ], [ "cell", get_cell, "os", "" ], [ "noauth", get_auth, "fn" ], [ "localauth", get_auth, "fn" ], -- 2.49.0