diff options
Diffstat (limited to 'eurephiadm/commands')
-rw-r--r-- | eurephiadm/commands/users.c | 126 |
1 files changed, 87 insertions, 39 deletions
diff --git a/eurephiadm/commands/users.c b/eurephiadm/commands/users.c index e2e1df7..a8e9d93 100644 --- a/eurephiadm/commands/users.c +++ b/eurephiadm/commands/users.c @@ -58,12 +58,6 @@ // TO BE REMOVED WHEN ALL FUNCTIONS ARE PORTED TO NEW eDBadminUserAccount() API // These functions are here only to make eurephiadm compile. -static xmlDoc *eDBadminGetUserInfo(eurephiaCTX *ctx, int show_info, xmlDoc *srch_xml) { - return NULL; -} -static int eDBadminUpdateUser(eurephiaCTX *ctx, int uid, xmlDoc *update_xml) { - return 0; -} static int eDBadminDeleteUser(eurephiaCTX *ctx, int uid, xmlDoc *update_xml) { return 0; } @@ -292,7 +286,9 @@ int show_user(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int int i, crit_set = 0, lastlog_verb = 0; long int show_info = USERINFO_user | USERINFO_certs; xmlDoc *user_xml = NULL, *srch_xml = NULL; - xmlNode *srch_root = NULL; + xmlNode *srch_root = NULL, *fmap_n = NULL; + xmlChar flags[34]; + #ifdef FIREWALL const char *xsltparams[] = {"view", NULL, "firewall", "'1'", "view_digest", "'0'", NULL}; #else @@ -312,10 +308,14 @@ int show_user(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int assert((ctx != NULL) && (ctx->dbc != NULL) && (ctx->dbc->config != NULL)); - // Create a fieldMapping XML document, used for searching the user database - eurephiaXML_CreateDoc(ctx, 1, "fieldMapping", &srch_xml, &srch_root); - xmlNewProp(srch_root, (xmlChar *) "table", (xmlChar *)"users"); // Which table to search in + // Create a UserAccount query XML document + eurephiaXML_CreateDoc(ctx, 1, "UserAccount", &srch_xml, &srch_root); + xmlNewProp(srch_root, (xmlChar*) "mode", (xmlChar *) "view"); + + // Setup the fieldMapping - to narrow the search + fmap_n = xmlNewChild(srch_root, NULL, (xmlChar *) "fieldMapping", NULL); + xmlNewProp(fmap_n, (xmlChar *) "table", (xmlChar *)"users"); // Argument parsing crit_set = 0; @@ -326,7 +326,7 @@ int show_user(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int fprintf(stderr, "%s: Invalid user id\n", MODULE); return 1; } - xmlNewChild(srch_root, NULL, (xmlChar *)"uid", (xmlChar *) optargs[0]); + xmlNewChild(fmap_n, NULL, (xmlChar *)"uid", (xmlChar *) optargs[0]); crit_set++; break; @@ -335,7 +335,7 @@ int show_user(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int fprintf(stderr, "%s: User name too short\n", MODULE); return 1; } - xmlNewChild(srch_root, NULL, (xmlChar *)"username", (xmlChar *)optargs[0]); + xmlNewChild(fmap_n, NULL, (xmlChar *)"username", (xmlChar *)optargs[0]); crit_set++; break; @@ -375,13 +375,32 @@ int show_user(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int return 1; } - // Search and find the user which was requested - if( (user_xml = eDBadminGetUserInfo(ctx, show_info, srch_xml)) == NULL ) { + xmlStrPrintf(flags, 32, (xmlChar *) "%ld%c", show_info, 0); + xmlNewChild(srch_root, NULL, (xmlChar *) "extractFlags", flags); + + // Do the query against the database + if( (user_xml = eDBadminUserAccount(ctx, srch_xml)) == NULL ) { + xmlFreeDoc(srch_xml); fprintf(stderr, "%s: User not found\n", MODULE); return 1; } xmlFreeDoc(srch_xml); + if( eurephiaXML_IsResultMsg(ctx, user_xml) ) { + eurephiaRESULT *res = eurephiaXML_ParseResultMsg(ctx, user_xml); + if( res->resultType == exmlERROR ) { + fprintf(stderr, "%s: %s\n", MODULE, res->message); + free_nullsafe(ctx, res); + xmlFreeDoc(user_xml); + return 1; + } + free_nullsafe(ctx, res); + } else { + xmlFreeDoc(user_xml); + fprintf(stderr, "%s: Unknown error\n", MODULE); + return 2; + } + if( show_info & USERINFO_user ) { xsltparams[1] = "'userinfo'"; xslt_print_xmldoc(stdout, cfg, user_xml, "users.xsl", xsltparams); @@ -402,12 +421,14 @@ int show_user(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int xsltparams[1] = "'details'"; } - user_n = eurephiaXML_getRoot(ctx, user_xml, "user", 1); + user_n = eurephiaXML_getRoot(ctx, user_xml, "UserAccount", 1); + user_n = xmlFindNode(user_n, "Account"); if( user_n == NULL ) { fprintf(stderr, "Could not retrieve valid data\n"); xmlFreeDoc(user_xml); return 1; } + printf("** Lastlog entries for %s\n\n", xmlGetNodeContent(user_n, "username")); xslt_print_xmldoc(stdout, cfg, user_xml, "lastlog.xsl", xsltparams); @@ -426,8 +447,6 @@ int show_user(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int } - - /** * This function handles activation, deactivation and deletion of an account * @@ -440,11 +459,12 @@ int show_user(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int * @return returns 0 on success, otherwise 1. */ int account_activation(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int argc, char **argv) { - xmlDoc *user_xml = NULL, *update_xml = NULL, *srch_xml = NULL, *tmp_xml = NULL; - xmlNode *user_n = NULL, *update_n = NULL, *srch_root = NULL, *tmp_n = NULL; + xmlDoc *user_xml = NULL, *update_xml = NULL, *srch_xml = NULL, *tmp_xml = NULL, *res_xml = NULL; + xmlNode *user_n = NULL, *update_n = NULL, *srch_root = NULL, *tmp_n = NULL, *fmap_n = NULL; eurephiaRESULT *res = NULL; char *uid_str = NULL, *activated = NULL, *deactivated = NULL, *actmode_str = NULL, *newpwd = NULL; int i, actmode = 0, rc = 0, crit_set = 0; + xmlChar flags[34]; e_options activargs[] = { {"--uid", "-i", 1}, @@ -473,9 +493,16 @@ int account_activation(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES * return 1; } - // Create a fieldMapping XML document, used for searching the user database - eurephiaXML_CreateDoc(ctx, 1, "fieldMapping", &srch_xml, &srch_root); - xmlNewProp(srch_root, (xmlChar *) "table", (xmlChar *)"users"); // Which table to search in + // Create a UserAccount query XML document + eurephiaXML_CreateDoc(ctx, 1, "UserAccount", &srch_xml, &srch_root); + xmlNewProp(srch_root, (xmlChar*) "mode", (xmlChar *) "view"); + + // Setup the fieldMapping - to narrow the search + fmap_n = xmlNewChild(srch_root, NULL, (xmlChar *) "fieldMapping", NULL); + xmlNewProp(fmap_n, (xmlChar *) "table", (xmlChar *)"users"); + + xmlStrPrintf(flags, 32, (xmlChar *) "%ld%c", USERINFO_user, 0); + xmlNewChild(srch_root, NULL, (xmlChar *) "extractFlags", flags); // Argument parsing crit_set = 0; @@ -486,7 +513,7 @@ int account_activation(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES * fprintf(stderr, "%s: Invalid user id\n", MODULE); return 1; } - xmlNewChild(srch_root, NULL, (xmlChar *)"uid", (xmlChar *) optargs[0]); + xmlNewChild(fmap_n, NULL, (xmlChar *)"uid", (xmlChar *) optargs[0]); crit_set++; break; @@ -495,7 +522,7 @@ int account_activation(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES * fprintf(stderr, "%s: User name too short\n", MODULE); return 1; } - xmlNewChild(srch_root, NULL, (xmlChar *)"username", (xmlChar *)optargs[0]); + xmlNewChild(fmap_n, NULL, (xmlChar *)"username", (xmlChar *)optargs[0]); crit_set++; break; @@ -528,29 +555,40 @@ int account_activation(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES * return 1; } - // Search and find the user which was requested - if( (user_xml = eDBadminGetUserInfo(ctx, USERINFO_user, srch_xml)) == NULL ) { - fprintf(stderr, "%s: User not found\n", MODULE); + // Do the query against the database + if( (user_xml = eDBadminUserAccount(ctx, srch_xml)) == NULL ) { xmlFreeDoc(srch_xml); + fprintf(stderr, "%s: User not found\n", MODULE); return 1; } xmlFreeDoc(srch_xml); - // Parse the user information we received - user_n = eurephiaXML_getRoot(ctx, user_xml, "user", 1); + if( eurephiaXML_IsResultMsg(ctx, user_xml) ) { + eurephiaRESULT *res = eurephiaXML_ParseResultMsg(ctx, user_xml); + if( res->resultType == exmlERROR ) { + fprintf(stderr, "%s: %s\n", MODULE, res->message); + free_nullsafe(ctx, res); + xmlFreeDoc(user_xml); + return 1; + } + free_nullsafe(ctx, res); + } + + user_n = eurephiaXML_getRoot(ctx, user_xml, "UserAccount", 1); + user_n = xmlFindNode(user_n, "Account"); if( user_n == NULL ) { - eurephia_log(ctx, LOG_ERROR, 0, "%s: Could not find user node in the XML document", MODULE); xmlFreeDoc(user_xml); + fprintf(stderr, "%s: No user account information found\n", MODULE); return 1; } - uid_str = xmlGetAttrValue(user_n->properties, "uid"); activated = defaultValue(xmlGetNodeContent(user_n, "activated"), NULL); deactivated = defaultValue(xmlGetNodeContent(user_n, "deactivated"), NULL); // Create a new XML document which will be used to update the user account - eurephiaXML_CreateDoc(ctx, 1, "update_user", &update_xml, &update_n); + eurephiaXML_CreateDoc(ctx, 1, "UserAccount", &update_xml, &update_n); assert( (update_xml != NULL) && (update_n != NULL) ); + xmlNewProp(update_n, (xmlChar *) "mode", (xmlChar *) "update"); xmlNewProp(update_n, (xmlChar *) "uid", (xmlChar *) uid_str); // Add fieldMapping - to correctly map eurephia fields into the database fields @@ -560,7 +598,7 @@ int account_activation(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES * switch( actmode ) { case 'a': // Activate a user account if( (activated != NULL) && (deactivated == NULL) ) { - printf("User account is already active\n"); + printf("%s: User account is already active\n", MODULE); goto exit; } else { // Set activated field to now() @@ -573,7 +611,7 @@ int account_activation(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES * } // Do the update of the user account - rc = eDBadminUpdateUser(ctx, atoi_nullsafe(uid_str), update_xml); + res_xml = eDBadminUserAccount(ctx, update_xml); break; case 'd': // Deactivate a user account @@ -591,7 +629,7 @@ int account_activation(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES * } // Do the update of the user account - rc = eDBadminUpdateUser(ctx, atoi_nullsafe(uid_str), update_xml); + res_xml = eDBadminUserAccount(ctx, update_xml); break; case 'p': // Change password for a user @@ -626,7 +664,7 @@ int account_activation(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES * // Update with new password tmp_n = xmlNewChild(update_n, NULL, (xmlChar *) "password", (xmlChar *)newpwd); xmlNewProp(tmp_n, (xmlChar *) "pwhash", (xmlChar *) "none"); - rc = eDBadminUpdateUser(ctx, atoi_nullsafe(uid_str), update_xml); + res_xml = eDBadminUserAccount(ctx, update_xml); free_nullsafe(ctx, newpwd); break; @@ -671,11 +709,21 @@ int account_activation(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES * break; } - if( rc == 1 ) { - printf("%s: User account is %s\n", MODULE, actmode_str); + + if( eurephiaXML_IsResultMsg(ctx, res_xml) ) { + eurephiaRESULT *res = eurephiaXML_ParseResultMsg(ctx, res_xml); + if( res->resultType == exmlERROR ) { + fprintf(stderr, "%s: %s\n", MODULE, res->message); + rc = 0; + } else { + fprintf(stdout, "%s: %s\n", MODULE, res->message); + rc = 1; + } + free_nullsafe(ctx, res); } else { - fprintf(stderr, "%s: Operation failed\n", MODULE); + fprintf(stderr, "%s: Unknown error\n", MODULE); } + xmlFreeDoc(res_xml); exit: xmlFreeDoc(user_xml); |