diff options
author | David Sommerseth <dazo@users.sourceforge.net> | 2012-11-02 18:12:52 +0100 |
---|---|---|
committer | David Sommerseth <dazo@users.sourceforge.net> | 2012-11-02 18:12:52 +0100 |
commit | 7f791662efeea219395c79b45f1dc1465ddedd62 (patch) | |
tree | 217d191e1eeaf46e77653c6e32d3a32953303de7 /eurephiadm | |
parent | 02b70ff6c325dce3983abe6b88e4fb7348de830f (diff) | |
download | eurephia-7f791662efeea219395c79b45f1dc1465ddedd62.tar.gz eurephia-7f791662efeea219395c79b45f1dc1465ddedd62.tar.xz eurephia-7f791662efeea219395c79b45f1dc1465ddedd62.zip |
eurephiadm/usercerts: Added support for username instead of uid on add/delete operations
Instead of having to look up the UID manually when adding a user-cert link, it
is now possible to user --username | -u instead of --uid | -i and provide a
username directly.
Signed-off-by: David Sommerseth <dazo@users.sourceforge.net>
Diffstat (limited to 'eurephiadm')
-rw-r--r-- | eurephiadm/commands/usercerts.c | 87 |
1 files changed, 67 insertions, 20 deletions
diff --git a/eurephiadm/commands/usercerts.c b/eurephiadm/commands/usercerts.c index b0bbf34..2da7a25 100644 --- a/eurephiadm/commands/usercerts.c +++ b/eurephiadm/commands/usercerts.c @@ -64,7 +64,8 @@ void display_usercerts_help(int page) { printf("The add mode will register a new link between a user account and a certificate.\n" "\n" " -c | --certid Required - Certificate ID\n" - " -i | --uid Required - User account ID\n" + " -i | --uid Required if no username - User account ID\n" + " -u | --username Required if no uid - User name of account\n" #ifdef FIREWALL " -a | --accessprofile Firewall profile ID to use for this access\n" #endif @@ -75,7 +76,8 @@ void display_usercerts_help(int page) { printf("The delete mode will delete a link between a user account and a certificate.\n" "\n" " -c | --certid Certificate ID\n" - " -i | --uid User account ID\n" + " -i | --uid Required if no username - User account ID\n" + " -u | --username Required if no uid - User name of account\n" " -n | --uicid Unique record id of certificate and user account link\n" #ifdef FIREWALL " -a | --accessprofile Firewall profile ID\n" @@ -238,10 +240,12 @@ int add_del_usercert(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cf xmlNode *usercert_n = NULL; eurephiaRESULT *res = NULL; int i = 0, rc = 0, actmode = 0; - char *certid = NULL, *uid = NULL, *uicid = NULL, *actmode_str = NULL, *accessprofile = NULL; + char *certid = NULL, *uid = NULL, *username = NULL, *uicid = NULL, *actmode_str = NULL, *accessprofile = NULL; + char uid_lookup[18]; e_options addargs[] = { {"--uid", "-i", 1}, + {"--username", "-u", 1}, {"--certid", "-c", 1}, {"--uicid", "-n", 1}, #ifdef FIREWALL @@ -261,32 +265,37 @@ int add_del_usercert(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cf actmode_str = "deleted"; } - eurephiaXML_CreateDoc(ctx, 1, "usercerts", &usercert_xml, &usercert_n); - assert( (usercert_xml != NULL) && (usercert_n != NULL) ); - - xmlNewProp(usercert_n, (xmlChar *) "mode", (xmlChar *) (actmode == 'D' ? "remove" : "register")); - usercert_n = xmlNewChild(usercert_n, NULL, (xmlChar *) "fieldMapping", NULL); - xmlNewProp(usercert_n, (xmlChar *) "table", (xmlChar *) "usercerts"); - for( i = 1; i < argc; i++ ) { switch( eurephia_getopt(&i, argc, argv, addargs) ) { case 'i': + if( username != NULL ) { + fprintf(stderr, "%s: --uid and --username cannot be used together\n", MODULE); + rc = 1; + goto exit; + } if( atoi_nullsafe(optargs[0]) < 1 ) { fprintf(stderr, "%s: User ID must be a positive number (>0)\n", MODULE); rc = 1; goto exit; } - xmlNewChild(usercert_n, NULL, (xmlChar *) "uid", (xmlChar *) optargs[0]); uid = optargs[0]; break; + case 'u': + if( uid != NULL ) { + fprintf(stderr, "%s: --username and --uid cannot be used together\n", MODULE); + rc = 1; + goto exit; + } + username = optargs[0]; + break; + case 'c': if( atoi_nullsafe(optargs[0]) < 1 ) { fprintf(stderr,"%s: Certificate ID must be a positive number (>0)\n",MODULE); rc = 1; goto exit; } - xmlNewChild(usercert_n, NULL, (xmlChar *) "certid", (xmlChar *) optargs[0]); certid = optargs[0]; break; @@ -298,7 +307,6 @@ int add_del_usercert(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cf rc = 1; goto exit; } - xmlNewChild(usercert_n, NULL, (xmlChar *) "accessprofile", (xmlChar *) optargs[0]); accessprofile = optargs[0]; break; #endif @@ -314,7 +322,6 @@ int add_del_usercert(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cf rc = 1; goto exit; } - xmlNewChild(usercert_n, NULL, (xmlChar *) "uicid", (xmlChar *) optargs[0]); uicid = optargs[0]; break; @@ -330,21 +337,61 @@ int add_del_usercert(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cf } } - if( (actmode == 'A') && ((certid == NULL) || (uid == NULL)) ) { - fprintf(stderr, "%s: You must provide both a user ID (--uid) and " + if( (actmode == 'A') && ((certid == NULL) || (((uid == NULL)) && (username == NULL))) ) { + fprintf(stderr, "%s: You must provide both a user ID (--uid or --username) and " "a certificate ID (--certid)\n", MODULE); rc = 1; goto exit; } - if( (actmode == 'D') && (certid == NULL) && (uid == NULL) + if( (actmode == 'D') && (certid == NULL) && (uid == NULL) && (username == NULL) && (uicid == NULL) && (accessprofile == NULL)) { - fprintf(stderr, "%s: You must provide at least --uid, --certid, " - "--uicid or --accessprofile\n", MODULE); + fprintf(stderr, "%s: You must provide at least --uid, --username, " + "--certid, --uicid or --accessprofile\n", MODULE); rc = 1; goto exit; } + /* If a username were given instead of uid, look up the uid first */ + if( username != NULL ) { + int uid_i = -1; + + memset(&uid_lookup, 0, 18); + uid_i = eDBget_uid(ctx, 0, username); + if( uid_i < 0 ) { + fprintf(stderr, "%s: Failed to lookup the user ID for '%s'\n", MODULE, username); + rc = 2; + goto exit; + } + snprintf(uid_lookup, 16, "%i", uid_i); + uid = uid_lookup; + fprintf(stdout, "%s: Username %s has uid %s\n", MODULE, username, uid); + } + + eurephiaXML_CreateDoc(ctx, 1, "usercerts", &usercert_xml, &usercert_n); + assert( (usercert_xml != NULL) && (usercert_n != NULL) ); + + xmlNewProp(usercert_n, (xmlChar *) "mode", (xmlChar *) (actmode == 'D' ? "remove" : "register")); + usercert_n = xmlNewChild(usercert_n, NULL, (xmlChar *) "fieldMapping", NULL); + xmlNewProp(usercert_n, (xmlChar *) "table", (xmlChar *) "usercerts"); + + if( uicid != NULL ) { + xmlNewChild(usercert_n, NULL, (xmlChar *) "uicid", (xmlChar *) uicid); + } + + if( uid != NULL ) { + xmlNewChild(usercert_n, NULL, (xmlChar *) "uid", (xmlChar *) uid); + } + + if( certid != NULL ) { + xmlNewChild(usercert_n, NULL, (xmlChar *) "certid", (xmlChar *) certid); + } +#ifdef FIREWALL + if( accessprofile != NULL ) { + xmlNewChild(usercert_n, NULL, (xmlChar *) "accessprofile", (xmlChar *) accessprofile); + } +#endif + resxml = eDBadminUserCertsLink(ctx, usercert_xml); if( resxml == NULL ) { fprintf(stderr, "%s: Failed to update user <-> certificate link\n", MODULE); @@ -367,9 +414,9 @@ int add_del_usercert(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cf } free_nullsafe(ctx, res); xmlFreeDoc(resxml); + xmlFreeDoc(usercert_xml); exit: - xmlFreeDoc(usercert_xml); return rc; } |