diff options
author | David Sommerseth <dazo@users.sourceforge.net> | 2009-09-22 00:22:10 +0200 |
---|---|---|
committer | David Sommerseth <dazo@users.sourceforge.net> | 2009-09-22 00:22:10 +0200 |
commit | 678de6fcad1bac80ea4a9554a46377ac2b927393 (patch) | |
tree | 951a60fc7a3307b3b2d036f18908f371b0866582 | |
parent | 5b0b63ab816ce0cfd01b851efee3ffdadd644eae (diff) | |
download | eurephia-678de6fcad1bac80ea4a9554a46377ac2b927393.tar.gz eurephia-678de6fcad1bac80ea4a9554a46377ac2b927393.tar.xz eurephia-678de6fcad1bac80ea4a9554a46377ac2b927393.zip |
Unified eDBadminGetCertificateInfo(), eDBadminAddCertificate() and eDBadminDeleteCertificate() into eDBadminCertificate()
Updated eurephiadm utility to make use of this new unified API
-rw-r--r-- | database/eurephiadb.c | 4 | ||||
-rw-r--r-- | database/eurephiadb_driver.h | 9 | ||||
-rw-r--r-- | database/sqlite/administration/certificates.c | 212 | ||||
-rw-r--r-- | eurephiadm/commands/certificates.c | 80 | ||||
-rw-r--r-- | eurephiadm/commands/users.c | 37 |
5 files changed, 210 insertions, 132 deletions
diff --git a/database/eurephiadb.c b/database/eurephiadb.c index e01b4f1..6a12aea 100644 --- a/database/eurephiadb.c +++ b/database/eurephiadb.c @@ -121,9 +121,7 @@ int eDBlink_init(eurephiaCTX *ctx, const char *dbdriver, const int minver) eDBadminUpdateUser = eGetSym(ctx, ctx->eurephia_driver, "eDBadminUpdateUser"); eDBadminDeleteUser = eGetSym(ctx, ctx->eurephia_driver, "eDBadminDeleteUser"); - eDBadminGetCertificateInfo = eGetSym(ctx, ctx->eurephia_driver, "eDBadminGetCertificateInfo"); - eDBadminAddCertificate = eGetSym(ctx, ctx->eurephia_driver, "eDBadminAddCertificate"); - eDBadminDeleteCertificate = eGetSym(ctx, ctx->eurephia_driver, "eDBadminDeleteCertificate"); + eDBadminCertificate = eGetSym(ctx, ctx->eurephia_driver, "eDBadminCertificate"); eDBadminUserCertsLink = eGetSym(ctx, ctx->eurephia_driver, "eDBadminUserCertsLink"); diff --git a/database/eurephiadb_driver.h b/database/eurephiadb_driver.h index 57b51e0..d90b2f5 100644 --- a/database/eurephiadb_driver.h +++ b/database/eurephiadb_driver.h @@ -604,7 +604,7 @@ int (*eDBadminDeleteUser) (eurephiaCTX *ctx, const int uid, xmlDoc *userinfo); * * @see eurephiaXML_CreateDoc(), eurephiaXML_getRoot() */ -xmlDoc *(*eDBadminGetCertificateInfo) (eurephiaCTX *ctx, xmlDoc *srchxml, const char *sortkeys); +// xmlDoc *(*eDBadminGetCertificateInfo) (eurephiaCTX *ctx, xmlDoc *srchxml, const char *sortkeys); /** @@ -632,7 +632,7 @@ xmlDoc *(*eDBadminGetCertificateInfo) (eurephiaCTX *ctx, xmlDoc *srchxml, const * @return The function returns certid of the newly registered certificate on success, otherwise -1 * @see eurephiaXML_CreateDoc() */ -int (*eDBadminAddCertificate) (eurephiaCTX *ctx, xmlDoc *certinfo_xml); +// int (*eDBadminAddCertificate) (eurephiaCTX *ctx, xmlDoc *certinfo_xml); /** @@ -660,7 +660,10 @@ int (*eDBadminAddCertificate) (eurephiaCTX *ctx, xmlDoc *certinfo_xml); * @return Returns 1 on success, otherwise 0. * @see eurephiaXML_CreateDoc() */ -int (*eDBadminDeleteCertificate) (eurephiaCTX *ctx, xmlDoc *certinfo_xml); +// int (*eDBadminDeleteCertificate) (eurephiaCTX *ctx, xmlDoc *certinfo_xml); + + +xmlDoc *(*eDBadminCertificate) (eurephiaCTX *ctx, xmlDoc *certxml); /** diff --git a/database/sqlite/administration/certificates.c b/database/sqlite/administration/certificates.c index f103827..53b50c2 100644 --- a/database/sqlite/administration/certificates.c +++ b/database/sqlite/administration/certificates.c @@ -58,44 +58,25 @@ void xmlReplaceChars(xmlChar *str, char s, char r); /** - * @copydoc eDBadminGetCertificateInfo() + * Internal function. Retrieves info about one or more certificates + * + * @param ctx eurephiaCTX + * @param srch_map eDBfieldMap defining the query + * @param sortkeys String containing the sort order of the fields + * + * @return Returns a valid xmlDoc containing the query, + * or a eurephia XML document with an error message. */ -xmlDoc *eDBadminGetCertificateInfo(eurephiaCTX *ctx, xmlDoc *srchxml, const char *sortkeys) { - xmlDoc *certlist = NULL; - xmlNode *srch_n = NULL, *cert_n = NULL, *tmp_n = NULL; - eDBfieldMap *srch_map = NULL, *ptr = NULL; +static xmlDoc *certificate_list(eurephiaCTX *ctx, eDBfieldMap *srch_map, const char *sortkeys) { + xmlDoc *certlist = NULL; + xmlNode *cert_n = NULL, *tmp_n = NULL; + eDBfieldMap *ptr = NULL; dbresult *res = NULL; xmlChar tmp[2050]; - char *dbsort = NULL; int i; - DEBUG(ctx, 20, "Function call: eDBadminGetCertificateInfo(ctx, xmlDoc, '%s')", sortkeys); - assert( (ctx != NULL) && (srchxml != NULL) ); - - if( (ctx->context_type != ECTX_ADMIN_CONSOLE) && (ctx->context_type != ECTX_ADMIN_WEB) ) { - eurephia_log(ctx, LOG_CRITICAL, 0, - "eurephia admin function call attempted with wrong context type"); - return NULL; - } - - if( sortkeys != NULL ) { - dbsort = eDBmkSortKeyString(tbl_sqlite_certs, sortkeys); - } - - srch_n = eurephiaXML_getRoot(ctx, srchxml, "certificate_info", 1); - if( srch_n == NULL ) { - eurephia_log(ctx, LOG_ERROR, 0, "Could not find a valid XML for looking up certificates"); - return NULL; - } - - srch_n = xmlFindNode(srch_n, "fieldMapping"); - if( srch_n == NULL ) { - eurephia_log(ctx, LOG_ERROR, 0, "Could not find a valid XML for looking up certificates"); - return NULL; - } - - srch_map = eDBxmlMapping(ctx, tbl_sqlite_certs, NULL, srch_n); - assert( srch_map != NULL ); + DEBUG(ctx, 21, "Function call: certificates_list(ctx, fieldMap, '%s')", sortkeys); + assert( (ctx != NULL) && (srch_map != NULL) ); // Replace spaces with underscore in common name and // in organisation fields, to comply with OpenVPN standards @@ -107,11 +88,11 @@ xmlDoc *eDBadminGetCertificateInfo(eurephiaCTX *ctx, xmlDoc *srchxml, const char res = sqlite_query_mapped(ctx, SQL_SELECT, "SELECT depth, digest, common_name, organisation, email, registered, certid" - " FROM openvpn_certificates", NULL, srch_map, dbsort); + " FROM openvpn_certificates", NULL, srch_map, sortkeys); if( res == NULL ) { - eDBfreeMapping(srch_map); eurephia_log(ctx, LOG_ERROR, 0, "Could not query the certificate table"); - return NULL; + return eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, + "Could not query the database for certificate info"); } memset(&tmp, 0, 2050); @@ -138,45 +119,35 @@ xmlDoc *eDBadminGetCertificateInfo(eurephiaCTX *ctx, xmlDoc *srchxml, const char sqlite_xml_value(tmp_n, XML_NODE, "email", res, i, 4); } sqlite_free_results(res); - eDBfreeMapping(srch_map); return certlist; } /** - * @copydoc eDBadminAddCertificate() + * Internal function. Registers a new certificate and saves it in the database + * + * @param ctx eurephiaCTX + * @param crtinf_map eDBfieldMap containing certificate info to be registered + * + * @return Returns an eurephia ResultMsg XML document with a result string. On fatal errors, + * NULL is returned */ -int eDBadminAddCertificate(eurephiaCTX *ctx, xmlDoc *certinfo_xml) { - xmlNode *crtinf_n = NULL; - eDBfieldMap *crtinf_map = NULL, *ptr = NULL; +static xmlDoc *certificate_add(eurephiaCTX *ctx, eDBfieldMap *crtinf_map) { + xmlDoc *res_d = NULL; + xmlNode *info_n = NULL; + eDBfieldMap *ptr = NULL; dbresult *res = NULL; - int certid = 0; - DEBUG(ctx, 20, "Function call: eDBadminAddCertificate(ctx, xmlDoc)"); - assert( (ctx != NULL) && (certinfo_xml != NULL) ); + DEBUG(ctx, 21, "Function call: certificate_add(ctx, xmlDoc)"); + assert( (ctx != NULL) && (crtinf_map != NULL) ); if( (ctx->context_type != ECTX_ADMIN_CONSOLE) && (ctx->context_type != ECTX_ADMIN_WEB) ) { eurephia_log(ctx, LOG_CRITICAL, 0, "eurephia admin function call attempted with wrong context type"); - return 0; - } - - crtinf_n = eurephiaXML_getRoot(ctx, certinfo_xml, "register_certificate", 1); - if( crtinf_n == NULL ) { - eurephia_log(ctx, LOG_ERROR, 0, "Could not find a valid XML for registering certificate"); - return 0; - } - - crtinf_n = xmlFindNode(crtinf_n, "fieldMapping"); - if( crtinf_n == NULL ) { - eurephia_log(ctx, LOG_ERROR, 0, "Could not find a valid XML for registering certificate"); - return 0; + return NULL; } - crtinf_map = eDBxmlMapping(ctx, tbl_sqlite_certs, NULL, crtinf_n); - assert( crtinf_map != NULL ); - // Replace spaces with underscore in common name and // in organisation fields, to comply with OpenVPN standards for( ptr = crtinf_map; ptr != NULL; ptr = ptr->next ) { @@ -186,53 +157,53 @@ int eDBadminAddCertificate(eurephiaCTX *ctx, xmlDoc *certinfo_xml) { } // Register the certificate - res = sqlite_query_mapped(ctx, SQL_INSERT, "INSERT INTO openvpn_certificates", crtinf_map, NULL, NULL); + res = sqlite_query_mapped(ctx, SQL_INSERT, "INSERT INTO openvpn_certificates", + crtinf_map, NULL, NULL); if( res == NULL ) { eurephia_log(ctx, LOG_FATAL, 0, "Could not register the certificate"); - certid = -1; + res_d = eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, "Could not register the certificate"); } else { - certid = res->last_insert_id; + xmlChar *certid = malloc_nullsafe(ctx, 34); + assert( certid != NULL ); + + // Prepare an information tag/node with the certid value + xmlStrPrintf(certid, 32, (xmlChar *) "%ld", res->last_insert_id); + info_n = xmlNewNode(NULL, (xmlChar *)"certificate"); + xmlNewProp(info_n, (xmlChar *) "certid", certid); + + res_d = eurephiaXML_ResultMsg(ctx, exmlRESULT, info_n, + "Certificate registered (certid %ld)", res->last_insert_id); + xmlFreeNode(info_n); } sqlite_free_results(res); - eDBfreeMapping(crtinf_map); - return certid; + return res_d; } /** - * @copydoc eDBadminDeleteCertificate() + * Internal function. Deletes one or more certificates from the database + * + * @param ctx eurephiaCTX + * @param crtinf_map eDBfieldMap with information about certificate(s) to delete + * + * @return Returns an eurephia ResultMsg XML document with a result string. On fatal errors, + * NULL is returned */ -int eDBadminDeleteCertificate(eurephiaCTX *ctx, xmlDoc *certinfo_xml) { - int rc = 0; - xmlNode *crtinf_n = NULL; - eDBfieldMap *crtinf_map = NULL, *ptr = NULL; +static xmlDoc *certificate_delete(eurephiaCTX *ctx, eDBfieldMap *crtinf_map) { + xmlDoc *res_d = NULL; + eDBfieldMap *ptr = NULL; dbresult *res = NULL; - DEBUG(ctx, 20, "Function call: eDBadminDeleteCertificate(ctx, xmlDoc)"); - assert( (ctx != NULL) && (certinfo_xml != NULL) ); + DEBUG(ctx, 21, "Function call: certificate_delete(ctx, xmlDoc)"); + assert( (ctx != NULL) && (crtinf_map != NULL) ); if( (ctx->context_type != ECTX_ADMIN_CONSOLE) && (ctx->context_type != ECTX_ADMIN_WEB) ) { eurephia_log(ctx, LOG_CRITICAL, 0, "eurephia admin function call attempted with wrong context type"); - return 0; - } - - crtinf_n = eurephiaXML_getRoot(ctx, certinfo_xml, "delete_certificate", 1); - if( crtinf_n == NULL ) { - eurephia_log(ctx, LOG_ERROR, 0, "Could not find a valid XML for the delete certificate request"); - return 0; - } - - crtinf_n = xmlFindNode(crtinf_n, "fieldMapping"); - if( crtinf_n == NULL ) { - eurephia_log(ctx, LOG_ERROR, 0, "Could not find a valid XML for the delete certificate request"); - return 0; + return NULL; } - crtinf_map = eDBxmlMapping(ctx, tbl_sqlite_certs, NULL, crtinf_n); - assert( crtinf_map != NULL ); - // Replace spaces with underscore in common name and // in organisation fields, to comply with OpenVPN standards for( ptr = crtinf_map; ptr != NULL; ptr = ptr->next ) { @@ -242,15 +213,70 @@ int eDBadminDeleteCertificate(eurephiaCTX *ctx, xmlDoc *certinfo_xml) { } // Register the certificate - res = sqlite_query_mapped(ctx, SQL_DELETE, "DELETE FROM openvpn_certificates", NULL, crtinf_map, NULL); + res = sqlite_query_mapped(ctx, SQL_DELETE, "DELETE FROM openvpn_certificates", + NULL, crtinf_map, NULL); if( res == NULL ) { eurephia_log(ctx, LOG_FATAL, 0, "Could not complete the delete certificate request"); - rc = 0; + res_d = eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, "Could not delete the certificate(s)"); } else { - rc = 1; + res_d = eurephiaXML_ResultMsg(ctx, exmlRESULT, NULL, "%i %s deleted", + sqlite_get_affected_rows(res), + (sqlite_get_affected_rows(res) != 1 + ? "certificates":"certificate") + ); } sqlite_free_results(res); - eDBfreeMapping(crtinf_map); - return rc; + return res_d; +} + +/** + * @copydoc eDBadminCertificate() + */ +xmlDoc *eDBadminCertificate(eurephiaCTX *ctx, xmlDoc *qryxml) { + eDBfieldMap *fmap = NULL; + char *mode = NULL; + xmlDoc *resxml = NULL; + xmlNode *root_n = NULL, *fieldmap_n = NULL; + + DEBUG(ctx, 20, "Function call: eDBadminCertificate(ctx, {xmlDoc})"); + assert( (ctx != NULL) && (qryxml != NULL) ); + + if( (ctx->context_type != ECTX_ADMIN_CONSOLE) && (ctx->context_type != ECTX_ADMIN_WEB) ) { + eurephia_log(ctx, LOG_CRITICAL, 0, + "eurephia admin function call attempted with wrong context type"); + return NULL; + } + + root_n = eurephiaXML_getRoot(ctx, qryxml, "certificates", 1); + if( root_n == NULL ) { + eurephia_log(ctx, LOG_CRITICAL, 0, "Invalid XML input."); + return NULL; + } + mode = xmlGetAttrValue(root_n->properties, "mode"); + if( mode == NULL ) { + eurephia_log(ctx, LOG_ERROR, 0, "Missing mode attribute"); + return NULL; + } + + fieldmap_n = xmlFindNode(root_n, "fieldMapping"); + if( fieldmap_n == NULL ) { + eurephia_log(ctx, LOG_ERROR, 0, "Missing fieldMapping"); + return NULL; + } + fmap = eDBxmlMapping(ctx, tbl_sqlite_certs, NULL, fieldmap_n); + + if( strcmp(mode, "list") == 0 ) { + char *sortkeys = xmlGetNodeContent(root_n, "sortkeys"); + resxml = certificate_list(ctx, fmap, eDBmkSortKeyString(tbl_sqlite_certs, sortkeys)); + } else if( strcmp(mode, "register") == 0 ) { + resxml = certificate_add(ctx, fmap); + } else if( strcmp(mode, "delete") == 0 ) { + resxml = certificate_delete(ctx, fmap); + } else { + eurephia_log(ctx, LOG_ERROR, 0, "Certificates - Unknown mode: '%s'", mode); + resxml = eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, "Unknown mode '%s'", mode); + } + eDBfreeMapping(fmap); + return resxml; } diff --git a/eurephiadm/commands/certificates.c b/eurephiadm/commands/certificates.c index 287b2a8..32c8b52 100644 --- a/eurephiadm/commands/certificates.c +++ b/eurephiadm/commands/certificates.c @@ -167,15 +167,17 @@ int help_Certificates2(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES * int register_certificate(eurephiaCTX *ctx, int depth, const char *digest, const char *cname, const char *org, const char *email) { - xmlDoc *cert_xml = NULL; + xmlDoc *cert_xml = NULL, *res_xml = NULL; xmlNode *cert_n = NULL; + eurephiaRESULT *res = NULL; char tmp[66], *cname_cp = NULL, *org_cp = NULL; int certid = 0; assert( ctx != NULL ); - eurephiaXML_CreateDoc(ctx, 1, "register_certificate", &cert_xml, &cert_n); + eurephiaXML_CreateDoc(ctx, 1, "certificates", &cert_xml, &cert_n); assert( (cert_xml != NULL) || (cert_n != NULL) ); + xmlNewProp(cert_n, (xmlChar *) "mode", (xmlChar *) "register"); cert_n = xmlNewChild(cert_n, NULL, (xmlChar *) "fieldMapping", NULL); xmlNewProp(cert_n, (xmlChar *) "table", (xmlChar *) "certificates"); @@ -193,11 +195,29 @@ int register_certificate(eurephiaCTX *ctx, int depth, const char *digest, xmlNewChild(cert_n, NULL, (xmlChar *) "email", (xmlChar *) email); // Register the certificate - certid = eDBadminAddCertificate(ctx, cert_xml); - if( certid > 0 ) { - fprintf(stdout, "%s: Certificate registered successfully (certid %i)\n", MODULE, certid); - } else { + res_xml = eDBadminCertificate(ctx, cert_xml); + if( res_xml == NULL ) { fprintf(stderr, "%s: Failed to register certificate\n", MODULE); + } else { + res = eurephiaXML_ParseResultMsg(ctx, res_xml); + if( res == NULL ) { + fprintf(stderr, "%s: Failed to register certificate. No results available\n", MODULE); + certid = 0; + } else if( res->resultType == exmlRESULT ) { + cert_n = xmlFindNode(res->details, "certificate"); + if( cert_n == NULL ) { + fprintf(stderr, "%s: Did not receive certificate ID of the newly registered" + " certificate\n", MODULE); + certid = 0; + } else { + certid = atoi_nullsafe(xmlGetAttrValue(cert_n->properties, "certid")); + fprintf(stdout, "%s: %s\n", MODULE, res->message); + } + } else { + fprintf(stderr, "%s: %s\n", MODULE, res->message); + certid = 0; + } + free_nullsafe(ctx, res); } xmlFreeDoc(cert_xml); free_nullsafe(ctx, cname_cp); @@ -374,8 +394,9 @@ int add_cert(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int a * @return returns 0 on success, otherwise 1. */ int delete_cert(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int argc, char **argv) { - xmlDoc *cert_xml = NULL, *delete_xml = NULL, *certlist = NULL; + xmlDoc *cert_xml = NULL, *delete_xml = NULL, *certlist = NULL, *res_xml = NULL; xmlNode *cert_n = NULL, *search_n = NULL, *delete_n = NULL; + eurephiaRESULT *res = NULL; char *digest = NULL, *cname = NULL, *org = NULL, *email = NULL, *certid = NULL, confirm[5]; const char *xsltparams[] = {"view_digest", "'1'", "firewall", "'0'", NULL}; int i, rc = 0, count = 0; @@ -431,7 +452,11 @@ int delete_cert(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, in } // Create a field mapping containing our search criteria(s) - eurephiaXML_CreateDoc(ctx, 1, "certificate_info", &cert_xml, &cert_n); + eurephiaXML_CreateDoc(ctx, 1, "certificates", &cert_xml, &cert_n); + assert( (cert_xml != NULL) && (cert_n != NULL)); + xmlNewProp(cert_n, (xmlChar *) "mode", (xmlChar *) "list"); + xmlNewChild(cert_n, NULL, (xmlChar *) "sortkeys", (xmlChar *) "certid"); + search_n = xmlNewChild(cert_n, NULL, (xmlChar *) "fieldMapping", NULL); xmlNewProp(search_n, (xmlChar *) "table", (xmlChar *) "certificates"); @@ -452,7 +477,7 @@ int delete_cert(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, in } // Look up the certificate info and display it - certlist = eDBadminGetCertificateInfo(ctx, cert_xml, "certid"); + certlist = eDBadminCertificate(ctx, cert_xml); if( certlist == NULL ) { return 0; } @@ -477,15 +502,29 @@ int delete_cert(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, in } // If user accepts to delete certificate(s), do so - eurephiaXML_CreateDoc(ctx, 1, "delete_certificate", &delete_xml, &delete_n); + eurephiaXML_CreateDoc(ctx, 1, "certificates", &delete_xml, &delete_n); + assert( (delete_xml != NULL) && (delete_n != NULL)); + xmlNewProp(delete_n, (xmlChar *) "mode", (xmlChar *) "delete"); xmlAddChild(delete_n, xmlCopyNode(search_n, 1)); - if( eDBadminDeleteCertificate(ctx, delete_xml) == 1 ) { - printf("%s: Certificate%s deleted successfully\n", MODULE, (count != 1 ? "s" : "")); - rc = 0; - } else { + res_xml = eDBadminCertificate(ctx, delete_xml); + if( res_xml == NULL ) { fprintf(stderr, "%s: Failed to delete the certificate%s\n", MODULE, (count != 1 ? "s" : "")); rc = 1; + } else { + res = eurephiaXML_ParseResultMsg(ctx, res_xml); + if( res == NULL ) { + fprintf(stderr, "%s: Failed to delete the certificate%s\n", MODULE, + (count != 1 ? "s" : "")); + rc = 1; + } else if( res->resultType == exmlERROR ) { + fprintf(stderr, "%s: %s\n", MODULE, res->message); + rc = 1; + } else { + fprintf(stdout, "%s: %s\n", MODULE, res->message); + rc = 0; + } + free_nullsafe(ctx, res); } xmlFreeDoc(delete_xml); @@ -544,12 +583,19 @@ int list_certs(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int } // Create an empty field mapping to get all certificates - eurephiaXML_CreateDoc(ctx, 1, "certificate_info", &srch_xml, &srch_n); + eurephiaXML_CreateDoc(ctx, 1, "certificates", &srch_xml, &srch_n); + assert( (srch_xml != NULL) && (srch_n != NULL)); + xmlNewProp(srch_n, (xmlChar *) "mode", (xmlChar *) "list"); + xmlNewChild(srch_n, NULL, (xmlChar *) "sortkeys", (xmlChar *) sortkeys); + srch_n = xmlNewChild(srch_n, NULL, (xmlChar *) "fieldMapping", NULL); xmlNewProp(srch_n, (xmlChar *) "table", (xmlChar *) "certificates"); + xmlSaveFormatFileEnc("-", srch_xml, "UTF-8", 1); + + // Look up the certificate info and display it - certlist = eDBadminGetCertificateInfo(ctx, srch_xml, sortkeys); + certlist = eDBadminCertificate(ctx, srch_xml); if( certlist == NULL ) { xmlFreeDoc(srch_xml); fprintf(stderr, "%s: Failed to query for certificates\n", MODULE); @@ -557,7 +603,7 @@ int list_certs(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int } cert_n = eurephiaXML_getRoot(ctx, certlist, "certificates", 1); - if( atoi_nullsafe(xmlGetAttrValue(cert_n->properties, "certificates")) == 0 ) { + if( (cert_n == NULL) || atoi_nullsafe(xmlGetAttrValue(cert_n->properties, "certificates")) == 0 ) { printf("%s: No certificates found\n", MODULE); rc = 0; goto exit; diff --git a/eurephiadm/commands/users.c b/eurephiadm/commands/users.c index 18d8101..e314dfe 100644 --- a/eurephiadm/commands/users.c +++ b/eurephiadm/commands/users.c @@ -611,24 +611,24 @@ int account_activation(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES * xmlNewProp(update_n, (xmlChar *) "table", (xmlChar *) "usercerts"); xmlNewChild(update_n, NULL, (xmlChar *) "uid", (xmlChar *) uid_str); - tmp_xml = eDBadminUserCertsLink(ctx, update_xml); + tmp_xml = eDBadminUserCertsLink(ctx, update_xml); if( tmp_xml == NULL ) { fprintf(stderr, "%s: Failed to remove the user-certs link\n", MODULE); } - res = eurephiaXML_ParseResultMsg(ctx, tmp_xml); + res = eurephiaXML_ParseResultMsg(ctx, tmp_xml); if( res == NULL ) { - fprintf(stderr, "%s: Failed to remove user <-> certificate link. " + fprintf(stderr, "%s: Failed to remove user <-> certificate link. " "No results received\n", MODULE); rc = 0; } else { if( res->resultType == exmlERROR ) { fprintf(stderr, "%s: %s\n", MODULE, res->message); }; - rc = 1; + rc = 1; free_nullsafe(ctx, res); - } - xmlFreeDoc(tmp_xml); + } + xmlFreeDoc(tmp_xml); break; } @@ -725,7 +725,7 @@ int add_user(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int a certfile_format = CERTFILE_PKCS12; break; - case 'd': + case 'D': if( strlen_nullsafe(optargs[0]) < 59 ) { fprintf(stderr, "%s: Certificate digest is too short\n", MODULE); return 1; @@ -811,12 +811,16 @@ int add_user(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int a int certcount = 0; // when we have certificate digest, look that up - eurephiaXML_CreateDoc(ctx, 1, "certificate_info", &cert_xml, &cert_n); + eurephiaXML_CreateDoc(ctx, 1, "certificates", &cert_xml, &cert_n); + assert( (cert_xml != NULL) && (cert_n != NULL) ); + xmlNewProp(cert_n, (xmlChar *) "mode", (xmlChar *) "list"); + xmlNewChild(cert_n, NULL, (xmlChar *) "sortkeys", (xmlChar *) "certid"); + cert_n = xmlNewChild(cert_n, NULL, (xmlChar *) "fieldMapping", NULL); xmlNewProp(cert_n, (xmlChar *) "table", (xmlChar *) "certificates"); xmlNewChild(cert_n, NULL, (xmlChar *) "digest", (xmlChar *) digest); - certlist = eDBadminGetCertificateInfo(ctx, cert_xml, "certid"); + certlist = eDBadminCertificate(ctx, cert_xml); if( certlist == NULL ) { fprintf(stderr, "%s: Error while looking up certificate info.\n" "%s: User account is not associated with any certificates\n", @@ -896,20 +900,21 @@ int add_user(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int a xmlStrPrintf(tmp, 64, (xmlChar *) "%i%c", certid, '\0'); xmlNewChild(usercert_n, NULL, (xmlChar *) "certid", tmp); - res_xml = eDBadminUserCertsLink(ctx, usercert_xml); + res_xml = eDBadminUserCertsLink(ctx, usercert_xml); if( res_xml == NULL ) { fprintf(stderr, "%s: Failed to register user <-> certificate link\n", MODULE); - goto exit; + goto exit; } - res = eurephiaXML_ParseResultMsg(ctx, res_xml); - if( res == NULL ) { + res = eurephiaXML_ParseResultMsg(ctx, res_xml); + if( res == NULL ) { fprintf(stderr, "%s: Failed to register user <-> certificate link. " "No results received\n", MODULE); } else if( res->resultType == exmlERROR ) { - fprintf(stderr, "%s: %s\n", MODULE, res->message); - } - xmlFreeDoc(res_xml); + fprintf(stderr, "%s: %s\n", MODULE, res->message); + } + free_nullsafe(ctx, res); + xmlFreeDoc(res_xml); } exit: return (uid > 0); |