summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Sommerseth <dazo@users.sourceforge.net>2008-12-15 01:30:03 +0100
committerDavid Sommerseth <dazo@users.sourceforge.net>2008-12-15 01:30:03 +0100
commitef6e4d43896c117fc2148f184a1fa10e2f64fd59 (patch)
treeee0874bd6f8c7d1d4eaa9bbee5ed02f25c421169
parent254426af12a0994a91cc7e47e5995186926ab60d (diff)
Completed rewriting show_users(...) function for XML
In short, completed the work begun in commit 6a8f3b190bfdfa772461a4fc019c11d759916160
-rw-r--r--eurephiadm/CMakeLists.txt1
-rw-r--r--eurephiadm/commands/users.c168
2 files changed, 97 insertions, 72 deletions
diff --git a/eurephiadm/CMakeLists.txt b/eurephiadm/CMakeLists.txt
index 8725593..ee5f57b 100644
--- a/eurephiadm/CMakeLists.txt
+++ b/eurephiadm/CMakeLists.txt
@@ -15,6 +15,7 @@ SET(efw_ipt_SRC
../common/eurephia_values.c
../common/eurephiadb_session_common.c
../common/eurephia_admin_common.c
+ ../common/eurephia_xml.c
../common/passwd.c
../common/sha512.c
../database/eurephiadb.c
diff --git a/eurephiadm/commands/users.c b/eurephiadm/commands/users.c
index f92fcf5..ad19a50 100644
--- a/eurephiadm/commands/users.c
+++ b/eurephiadm/commands/users.c
@@ -26,12 +26,14 @@
#ifdef HAVE_LIBXML2
#include <libxml/parser.h>
#include <libxml/tree.h>
+#include <libxml/xpath.h>
#endif
#define MODULE "eurephia::Users"
#include <eurephia_nullsafe.h>
#include <eurephia_context.h>
#include <eurephia_log.h>
+#include <eurephia_xml.h>
#include <eurephia_values_struct.h>
#include <eurephiadb_session_struct.h>
#include <eurephia_admin_struct.h>
@@ -189,42 +191,90 @@ int list_users(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int
return 0;
}
-char *accountFlags_str(int flags) {
+char *xmlFlags2str(xmlXPathContext *xpathCTX, const char *xpath) {
+ xmlXPathObject *flagsObj = NULL;
+ int i = 0;
static char flagstr[8194];
memset(&flagstr, 0, 8194);
- if( flags == 0 ) {
- return "-";
+ flagsObj = xmlXPathEvalExpression((xmlChar *)xpath, xpathCTX);
+ if( flagsObj == NULL ) {
+ fprintf(stderr,"Error: unable to evaluate xpath expression \"%s\"\n", xpath);
+ return flagstr;
}
- if( (flags & ACCFLAG_NEVERUSED) ) {
- strcat(flagstr, "NEVERUSED ");
- }
- if( (flags & ACCFLAG_OPENSESSION) ) {
- strcat(flagstr, "OPENSESSION ");
- }
- if( (flags & ACCFLAG_BLACKLISTED) ) {
- strcat(flagstr, "BLACKLISTED ");
- }
- if( (flags & ACCFLAG_DEACTIVATED) ) {
- strcat(flagstr, "DEACTIVATED ");
- }
- if( (flags & ACCFLAG_ERRATTEMPT) ) {
- strcat(flagstr, "ERRATTEMPT ");
+ for( i = 0; i < flagsObj->nodesetval->nodeNr; i++ ) {
+ char *flag = xmlExtractContent(flagsObj->nodesetval->nodeTab[i]);
+ if( flag != NULL ) {
+ strncat(flagstr, flag, 8192-strlen_nullsafe(flagstr));
+ }
}
- if( (flags & ACCFLAG_RSETLASTUSED) ) {
- strcat(flagstr, "RSETLASTUSED");
+ xmlXPathFreeObject(flagsObj);
+ return flagstr;
+}
+
+void xmlPrint_certs(xmlXPathContext *certsXP, const char *xpath) {
+ xmlXPathObject *certsObj = NULL;
+ xmlNode *certnode = NULL;
+ int i = 0;
+
+ certsObj = xmlXPathEvalExpression((xmlChar *)xpath, certsXP);
+ if( certsObj == NULL ) {
+ fprintf(stderr,"Error: unable to evaluate xpath expression \"%s\"\n", xpath);
+ return;
}
- if( (flags & ACCFLAG_RSETLOGINCNT) ) {
- strcat(flagstr, "RSETLOGINCNT");
+
+
+ printf(" %3s (D) %-35.35s %33.33s\n %-49.49s %19.19s\n",
+ "ID", "Common name", "Organisation", "e-mail", "Registered");
+#ifdef FIREWALL
+ printf(" %-44.44s %24.24s\n", "Firewall access profile", "FW Destination");
+#endif
+ printf(" --------------------------------------------------------------------"
+ "----------\n");
+
+ for( i = 0; i < certsObj->nodesetval->nodeNr; i++ ) {
+ xmlNode *acpr = NULL;
+
+ certnode = certsObj->nodesetval->nodeTab[i];
+ if( certnode->type != XML_ELEMENT_NODE) {
+ // Skip nodes which is not element nodes
+ continue;
+ }
+ if( xmlStrcmp(certnode->name, (xmlChar *)"certificate") != 0 ) {
+ // Skip nodes which is not a certificate "collection"
+ continue;
+ }
+
+ acpr = xmlFindNode(certnode, "access_profile");
+
+ printf(" %3s (%1s) %-35.35s %33.33s\n %-49.49s %19.19s\n",
+ xmlGetAttrValue(certnode->properties, "certid"),
+ xmlGetAttrValue(certnode->properties, "depth"),
+ xmlGetNodeContent(certnode, "common_name"),
+ xmlGetNodeContent(certnode, "organisation"),
+ xmlGetNodeContent(certnode, "email"),
+ xmlGetAttrValue(certnode->properties, "registered"));
+#ifdef FIREWALL
+ printf(" %-44.44s %24.24s\n",
+ xmlExtractContent(acpr),
+ xmlGetAttrValue(acpr->properties, "fwdestination"));
+#endif
+ if( (i+1) < certsObj->nodesetval->nodeNr ) {
+ printf("\n");
+ }
}
- return flagstr;
+ printf(" --------------------------------------------------------------------"
+ "----------\n");
+
+ xmlXPathFreeObject(certsObj);
}
// Show account information for a particular user
int show_user(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int argc, char **argv) {
+ xmlXPathContext *user_XP = NULL;
char *uname = NULL;
int i, crit_set = 0;
long int show_info = USERINFO_user | USERINFO_certs;
@@ -308,62 +358,36 @@ int show_user(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int
}
xmlFreeDoc(srch);
- // Just for debug purpose, until everything is ported over to parse the XML result
- xmlSaveFormatFileEnc("-", user, "UTF-8", 1);
+ // Create a XPath context for functions which users XPath to locate info
+ user_XP = xmlXPathNewContext(user);
+ if( user_XP == NULL ) {
+ fprintf(stderr,"Error: unable to create new XPath context\n");
+ return 1;
+ }
if( show_info & USERINFO_user ) {
- /*
- field_print_int("User id", user->uid);
- field_print_str("User name", user->username);
- field_print_int("Login count", user->logincount);
- field_print_str("Last accessed", user->last_accessed);
- field_print_str("Activated", user->activated);
- field_print_str("Dectivated", user->deactivated);
- field_print_str("Flags", accountFlags_str(user->account_flags));
- */
- printf("user info\n");
+ xmlNode *lastacc = NULL, *user_n = xmlDocGetRootElement(user);
+
+ lastacc = xmlFindNode(user_n, "last_accessed");
+
+ field_print_str("User id", xmlGetAttrValue(user_n->properties, "uid"));
+ field_print_str("User name", xmlGetNodeContent(user_n, "username"));
+ field_print_str("Login count", xmlGetAttrValue(lastacc->properties, "logincount"));
+ field_print_str("Last accessed", xmlExtractContent(lastacc));
+ field_print_str("Activated", xmlGetNodeContent(user_n, "activated"));
+ field_print_str("Dectivated", xmlGetNodeContent(user_n, "deactivated"));
+ field_print_str("Flags", xmlFlags2str(user_XP, "/user/flags/flag"));
+ printf("\n");
}
- // Show associated certificates if we have that info
- /*
- if( (user->certlist != NULL) && (user->certlist->num_certs > 0) ) {
- eurephiaCERTINFO *crt = NULL;
-
- field_print_int("Associated certificates", user->certlist->num_certs);
- printf(" %3s (D) %-35.35s %33.33s\n %-49.49s %19.19s\n",
- "ID", "Common name", "Organisation", "e-mail", "Registered");
-#ifdef FIREWALL
- printf(" %-44.44s %24.24s\n", "Firewall access profile", "FW Destination");
-#endif
- printf(" --------------------------------------------------------------------"
- "----------\n");
- for( crt = user->certlist->certs; crt != NULL; crt = crt->next) {
- printf(" %3i (%1i) %-35.35s %33.33s\n %-49.49s %19.19s\n",
- crt->certid, crt->depth, crt->common_name, crt->organisation,
- crt->email, crt->registered);
-#ifdef FIREWALL
- printf(" %-44.44s %24.24s\n",
- ((crt->access != NULL) && (crt->access->access_descr != NULL)
- ? crt->access->access_descr : "(No firewall profile setup)"),
- ((crt->access != NULL) && (crt->access->fwprofile != NULL)
- ? crt->access->fwprofile : "-")
-
- );
-#endif
- if( crt->next != NULL ) {
- printf("\n");
- }
- }
- printf(" --------------------------------------------------------------------"
- "----------\n");
- } else {
- // If we wanted to show associated certs, and didn't find any - inform about it
- if( (show_info & USERINFO_certs) == USERINFO_certs ) {
- field_print_str("Associated certificates", "None");
- }
+ // Show associated certificates if we was asked to show this info
+ if( show_info & USERINFO_certs ) {
+ xmlPrint_certs(user_XP, "/user/certificates/certificate");
}
- */
+
+ // Clean up
+ xmlXPathFreeContext(user_XP);
xmlFreeDoc(user);
free_nullsafe(uname);