diff options
-rw-r--r-- | eurephiadm/commands/lastlog.c | 145 | ||||
-rw-r--r-- | eurephiadm/commands/users.c | 13 | ||||
-rw-r--r-- | xslt/eurephiadm/lastlog.xsl | 189 |
3 files changed, 214 insertions, 133 deletions
diff --git a/eurephiadm/commands/lastlog.c b/eurephiadm/commands/lastlog.c index 9985cf1..84e16e9 100644 --- a/eurephiadm/commands/lastlog.c +++ b/eurephiadm/commands/lastlog.c @@ -43,115 +43,7 @@ #include "../argparser.h" #include "../field_print.h" - -void xmlPrint_lastlog(xmlXPathContext *xpathCTX, int verb, const char *xpath) { - xmlXPathObject *lastlogObj = NULL; - int i = 0; - char tmp[66]; - - // Find the certificates in the given XPath query - lastlogObj = xmlXPathEvalExpression((xmlChar *)xpath, xpathCTX); - if( lastlogObj == NULL ) { - fprintf(stderr,"Error: unable to evaluate xpath expression \"%s\"\n", xpath); - return; - } - - // If no certs were found, inform and exit - if( lastlogObj->nodesetval->nodeNr == 0 ) { - printf("No lastlog entries found.\n"); - printf("\n"); - xmlXPathFreeObject(lastlogObj); - return; - } - - memset(&tmp, 0, 66); - printf(" %-10.10s %-22.22s %19.19s %19.19s\n", - "Status", "Login", "Logout", "Session closed"); - switch( verb ) { - case 1: - printf(" %-10.10s %-22.22s %19.19s %19.19s\n", - "Protocol", "Remote:port", "VPN MAC", "VPN IP"); - printf(" %-37.37s %37.37s\n", "Common name", "Organisation"); - break; - case 2: - printf(" %-10.10s %-22.22s %19.19s %19.19s\n", - "Protocol", "Remote:port", "VPN MAC", "VPN IP"); - printf(" %-37.37s %37.37s\n", "Common name / Username", "Organisation"); - break; - } -#ifdef FIREWALL - if( verb > 0 ) { - printf(" %-37.37s %37.37s\n", "Firewall access profile", "FW Destination"); - } -#endif - printf("---------------------------------------------------------------------" - "----------\n"); - - for( i = 0; i < lastlogObj->nodesetval->nodeNr; i++ ) { - xmlNode *session_n = lastlogObj->nodesetval->nodeTab[i]; - xmlNode *conn = NULL, *cert = NULL, *acpr; - - if( (session_n == NULL) || (session_n->type != XML_ELEMENT_NODE) ) { - continue; - } - - conn = xmlFindNode(session_n, "connection"); - cert = xmlFindNode(session_n, "certificate"); - acpr = xmlFindNode(cert, "access_profile"); - - printf(" %-10.10s %-22.22s %19.19s %19.19s\n", - xmlGetAttrValue(session_n->properties, "session_status"), - xmlGetNodeContent(session_n, "login"), - defaultValue(xmlGetNodeContent(session_n, "logout"), "-"), - defaultValue(xmlGetNodeContent(session_n, "session_closed"), "-") - ); - - if( verb > 0 ) { - snprintf(tmp, 64, "%s:%s", - xmlGetNodeContent(conn, "remote_host"), - xmlGetNodeContent(conn, "remote_port")); - printf(" %-10.10s %-22.22s %19.19s %19.19s\n", - xmlGetNodeContent(conn, "protocol"), - tmp, - defaultValue(xmlGetNodeContent(conn, "vpn_macaddr"), "(not available)"), - xmlGetNodeContent(conn, "vpn_ipaddr") - ); - - switch( verb ) { - case 1: - printf(" %-37.37s %37.37s\n", - xmlGetNodeContent(cert, "common_name"), - xmlGetNodeContent(cert, "organisation") - ); - break; - case 2: - snprintf(tmp, 64, "%s / %s", - xmlGetNodeContent(cert, "common_name"), - xmlGetNodeContent(session_n, "username")); - printf(" %-37.37s %37.37s\n", - tmp, - xmlGetNodeContent(cert, "organisation") - ); - break; - } - -#ifdef FIREWALL - printf(" %-37.37s %37.37s\n", - defaultValue(xmlExtractContent(acpr), "(not set)"), - defaultValue(xmlGetAttrValue(acpr->properties, "fwdestination"), "-") - ); -#endif - if( (i+1) < lastlogObj->nodesetval->nodeNr ) { - printf("\n"); - } - } - } - - printf("---------------------------------------------------------------------" - "----------\n"); - - xmlXPathFreeObject(lastlogObj); -} +#include "../xsltparser.h" void help_Lastlog() @@ -182,11 +74,15 @@ void help_Lastlog() int cmd_Lastlog(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int argc, char **argv) { - xmlDoc *list_xml = NULL, *srch_xml = NULL; - xmlNode *list_n = NULL, *fmap_n = NULL, *srch_n = NULL; - xmlXPathContext *lastlog_XP = NULL; - int i = 0, verb = 0; + xmlDoc *lastlog_xml = NULL, *srch_xml = NULL; + xmlNode *fmap_n = NULL, *srch_n = NULL; + int i = 0; char *sortkeys = NULL; +#ifdef FIREWALL + char *xsltparams[] = {"view", "'list'", "firewall", "'1'", NULL}; +#else + char *xsltparams[] = {"view", "'list'", "firewall", "'0'", NULL}; +#endif e_options listargs[] = { {"--certid", "-c", 1}, @@ -257,7 +153,7 @@ int cmd_Lastlog(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, in break; case 'v': - verb = 2; + xsltparams[1] = "'details2'"; break; default: @@ -266,28 +162,15 @@ int cmd_Lastlog(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, in } } - list_xml = eDBadminGetLastlog(ctx, srch_xml, sortkeys); + lastlog_xml = eDBadminGetLastlog(ctx, srch_xml, sortkeys); xmlFreeDoc(srch_xml); - if( list_xml == NULL ) { - fprintf(stderr, "%s: Error retrieving lastlog entries\n", MODULE); - return 1; - } - - list_n = eurephiaXML_getRoot(ctx, list_xml, "lastlog", 1); - if( list_n == NULL ) { + if( lastlog_xml == NULL ) { fprintf(stderr, "%s: Error retrieving lastlog entries\n", MODULE); - xmlFreeDoc(list_xml); return 1; } - lastlog_XP = xmlXPathNewContext(list_xml); - if( lastlog_XP == NULL ) { - fprintf(stderr,"Error: unable to create new XPath context\n"); - return 1; - } - xmlPrint_lastlog(lastlog_XP, verb, "/eurephia/lastlog/session"); - xmlXPathFreeContext(lastlog_XP); + xslt_print_xmldoc(stdout, cfg, lastlog_xml, "lastlog.xsl", (const char **) xsltparams); - xmlFreeDoc(list_xml); + xmlFreeDoc(lastlog_xml); return 0; } diff --git a/eurephiadm/commands/users.c b/eurephiadm/commands/users.c index 54b4f06..581cf18 100644 --- a/eurephiadm/commands/users.c +++ b/eurephiadm/commands/users.c @@ -46,13 +46,13 @@ #include "../get_console_input.h" #include "../field_print.h" #include "../parse_certificate_files.h" +#include "../xsltparser.h" // Reuse code which is in certificats.c #define SHOWCERTS_FIREWALL 0x001 #define SHOWCERTS_DIGEST 0x002 void xmlPrint_certs(xmlXPathContext *certsXP, const char *xpath, int show_flags); -void xmlPrint_lastlog(xmlXPathContext *xpathCTX, int verb, const char *xpath); int register_certificate(eurephiaCTX *ctx, int depth, const char *digest, const char *cname, const char *org, const char *email); @@ -378,8 +378,17 @@ int show_user(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int } if( show_info & USERINFO_lastlog ) { +#ifdef FIREWALL + char *xsltparams[] = {"view", "'list'", "firewall", "'1'", NULL}; +#else + char *xsltparams[] = {"view", "'list'", "firewall", "'0'", NULL}; +#endif + if( lastlog_verb ) { + xsltparams[1] = "'details'"; + } + printf("** Lastlog entries for %s\n\n", xmlGetNodeContent(user_n, "username")); - xmlPrint_lastlog(user_XP, lastlog_verb, "/eurephia/user/lastlog/session"); + xslt_print_xmldoc(stdout, cfg, user_xml, "lastlog.xsl", (const char **)xsltparams); } if( (show_info & USERINFO_attempts) || (show_info & USERINFO_blacklist) ) { diff --git a/xslt/eurephiadm/lastlog.xsl b/xslt/eurephiadm/lastlog.xsl new file mode 100644 index 0000000..a1da821 --- /dev/null +++ b/xslt/eurephiadm/lastlog.xsl @@ -0,0 +1,189 @@ +<?xml version="1.0"?> +<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> + <xsl:output method="text" encoding="UTF-8"/> + <xsl:strip-space elements="*"/> + + <xsl:template match="/eurephia"> + <xsl:choose> + <xsl:when test="$view = 'list'"> + <xsl:apply-templates select="lastlog|user/lastlog" mode="list"/> + </xsl:when> + <xsl:when test="$view = 'details' or $view = 'details2'"> + <xsl:apply-templates select="lastlog|user/lastlog" mode="details"/> + </xsl:when> + <xsl:otherwise> + <xsl:message terminate="yes">Invalid view: <xsl:value-of select="$view"/></xsl:message> + </xsl:otherwise> + </xsl:choose> + </xsl:template> + + <xsl:template match="lastlog" mode="list"> + <xsl:text> Status Login Logout Session closed </xsl:text> + <xsl:text>------------------------------------------------------------------------------- </xsl:text> + <xsl:apply-templates select="session" mode="list"/> + <xsl:text>------------------------------------------------------------------------------- </xsl:text> + </xsl:template> + + <xsl:template match="lastlog/session" mode="list"> + <xsl:text> </xsl:text> + <xsl:call-template name="left-align"> + <xsl:with-param name="value" select="@session_status"/> + <xsl:with-param name="width" select="10"/> + </xsl:call-template><xsl:text> </xsl:text> + <xsl:call-template name="left-align"> + <xsl:with-param name="value" select="login"/> + <xsl:with-param name="width" select="19"/> + </xsl:call-template><xsl:text> </xsl:text> + + <xsl:variable name="logout"> + <xsl:choose> + <xsl:when test="logout != ''"><xsl:value-of select="logout"/></xsl:when> + <xsl:otherwise>-</xsl:otherwise> + </xsl:choose> + </xsl:variable> + <xsl:call-template name="right-align"> + <xsl:with-param name="value" select="$logout"/> + <xsl:with-param name="width" select="19"/> + </xsl:call-template><xsl:text> </xsl:text> + + <xsl:variable name="sessionclose"> + <xsl:choose> + <xsl:when test="session_closed != ''"><xsl:value-of select="session_closed"/></xsl:when> + <xsl:otherwise>-</xsl:otherwise> + </xsl:choose> + </xsl:variable> + <xsl:call-template name="right-align"> + <xsl:with-param name="value" select="$sessionclose"/> + <xsl:with-param name="width" select="19"/> + </xsl:call-template> + <xsl:text> </xsl:text> + </xsl:template> + + + <xsl:template match="lastlog" mode="details"> + <xsl:text> Status Login Logout Session closed </xsl:text> + <xsl:text> Protocol Remote:port VPN MAC VPN IP </xsl:text> + <xsl:choose> + <xsl:when test="$view = 'details2'"> + <xsl:text> Common name / Username Organisation </xsl:text> + </xsl:when> + <xsl:otherwise> + <xsl:text> Common name Organisation </xsl:text> + </xsl:otherwise> + </xsl:choose> + <xsl:if test="$firewall = '1'"> + <xsl:text> Firewall access profile FW Destination </xsl:text> + </xsl:if> + <xsl:text>------------------------------------------------------------------------------- </xsl:text> + <xsl:apply-templates select="session" mode="details"/> + <xsl:text>------------------------------------------------------------------------------- </xsl:text> + </xsl:template> + + <xsl:template match="lastlog/session" mode="details"> + <xsl:text> </xsl:text> + <xsl:call-template name="left-align"> + <xsl:with-param name="value" select="@session_status"/> + <xsl:with-param name="width" select="10"/> + </xsl:call-template><xsl:text> </xsl:text> + <xsl:call-template name="left-align"> + <xsl:with-param name="value" select="login"/> + <xsl:with-param name="width" select="19"/> + </xsl:call-template><xsl:text> </xsl:text> + + <xsl:variable name="logout"> + <xsl:choose> + <xsl:when test="logout != ''"><xsl:value-of select="logout"/></xsl:when> + <xsl:otherwise>-</xsl:otherwise> + </xsl:choose> + </xsl:variable> + <xsl:call-template name="right-align"> + <xsl:with-param name="value" select="$logout"/> + <xsl:with-param name="width" select="19"/> + </xsl:call-template><xsl:text> </xsl:text> + + <xsl:variable name="sessionclose"> + <xsl:choose> + <xsl:when test="session_closed != ''"><xsl:value-of select="session_closed"/></xsl:when> + <xsl:otherwise>-</xsl:otherwise> + </xsl:choose> + </xsl:variable> + <xsl:call-template name="right-align"> + <xsl:with-param name="value" select="$sessionclose"/> + <xsl:with-param name="width" select="19"/> + </xsl:call-template> + <xsl:text> </xsl:text> + + <xsl:call-template name="left-align"> + <xsl:with-param name="value" select="connection/protocol"/> + <xsl:with-param name="width" select="11"/> + </xsl:call-template> + <xsl:text> </xsl:text> + <xsl:call-template name="left-align"> + <xsl:with-param name="value" select="concat(connection/remote_host,':',connection/remote_port)"/> + <xsl:with-param name="width" select="22"/> + </xsl:call-template> + <xsl:text> </xsl:text> + <xsl:call-template name="right-align"> + <xsl:with-param name="value" select="connection/vpn_macaddr"/> + <xsl:with-param name="width" select="19"/> + </xsl:call-template> + <xsl:text> </xsl:text> + <xsl:call-template name="right-align"> + <xsl:with-param name="value" select="connection/vpn_ipaddr"/> + <xsl:with-param name="width" select="19"/> + </xsl:call-template> + <xsl:text> </xsl:text> + + <xsl:choose> + <xsl:when test="$view = 'details2'"> + <xsl:call-template name="left-align"> + <xsl:with-param name="value" select="concat(certificate/common_name,' / ',username)"/> + <xsl:with-param name="width" select="37"/> + </xsl:call-template> + </xsl:when> + <xsl:otherwise> + <xsl:call-template name="left-align"> + <xsl:with-param name="value" select="certificate/common_name"/> + <xsl:with-param name="width" select="37"/> + </xsl:call-template> + </xsl:otherwise> + </xsl:choose> + <xsl:text> </xsl:text> + <xsl:call-template name="right-align"> + <xsl:with-param name="value" select="certificate/organisation"/> + <xsl:with-param name="width" select="37"/> + </xsl:call-template> + + <xsl:if test="$firewall = '1'"> + <xsl:text> </xsl:text> + <xsl:call-template name="left-align"> + <xsl:with-param name="value" select="certificate/access_profile"/> + <xsl:with-param name="width" select="37"/> + </xsl:call-template> + <xsl:text> </xsl:text> + <xsl:call-template name="right-align"> + <xsl:with-param name="value" select="certificate/access_profile/@fwdestination"/> + <xsl:with-param name="width" select="37"/> + </xsl:call-template> + </xsl:if> + + <xsl:text> </xsl:text> + <xsl:if test="last() > position()"> + <xsl:text> </xsl:text> + </xsl:if> + </xsl:template> + + + <xsl:template name="left-align"> + <xsl:param name="value"/> + <xsl:param name="width"/> + <xsl:value-of select="substring(concat($value, ' '), 1, $width)"/> + </xsl:template> + + <xsl:template name="right-align"> + <xsl:param name="value"/> + <xsl:param name="width"/> + <xsl:value-of select="concat(substring(' ', 1, $width - string-length($value)), $value)"/> + </xsl:template> + +</xsl:stylesheet> |