diff options
| author | David Sommerseth <dazo@users.sourceforge.net> | 2008-12-22 12:37:48 +0100 |
|---|---|---|
| committer | David Sommerseth <dazo@users.sourceforge.net> | 2008-12-22 12:37:48 +0100 |
| commit | ed084c0dedcc0a8a24b9f2e37fdf79f73a27b6fb (patch) | |
| tree | 6f1d1aee3bae8516351e45ece57e23880f8c8455 | |
| parent | 27c831c4997f045449c65851ca7a09878b02a7ee (diff) | |
sqlite3 - Return uid or certid of newly registered users or certificates
| -rw-r--r-- | database/sqlite/administration.c | 21 | ||||
| -rw-r--r-- | eurephiadm/commands/certificates.c | 16 | ||||
| -rw-r--r-- | eurephiadm/commands/users.c | 13 |
3 files changed, 30 insertions, 20 deletions
diff --git a/database/sqlite/administration.c b/database/sqlite/administration.c index ee4f132..3cdf76b 100644 --- a/database/sqlite/administration.c +++ b/database/sqlite/administration.c @@ -689,7 +689,8 @@ xmlDoc *eDBadminGetUserInfo(eurephiaCTX *ctx, int getInfo, xmlDoc *srch) { // This function will add a user to the openvpn_users table, based on the -// XML document given. +// XML document given. The function returns the UID of the new user. On +// failure, the function returns -1 // // XML format: // <eurephia format="1"> @@ -705,7 +706,7 @@ int eDBadminAddUser(eurephiaCTX *ctx, xmlDoc *usrinf) { dbresult *res = NULL; xmlNode *usrinf_n = NULL; eDBfieldMap *usrinf_map = NULL; - int rc = 0; + int uid = 0; assert( (ctx != NULL) && (usrinf != NULL) ); @@ -729,14 +730,14 @@ int eDBadminAddUser(eurephiaCTX *ctx, xmlDoc *usrinf) { res = sqlite_query_mapped(ctx, SQL_INSERT, "INSERT INTO openvpn_users", usrinf_map, NULL, NULL); if( res == NULL ) { eurephia_log(ctx, LOG_FATAL, 0, "Could not register the new user account"); - rc = 0; + uid = -1; } else { - rc = 1; + uid = res->last_insert_id; } sqlite_free_results(res); eDBfreeMapping(usrinf_map); - return rc; + return uid; } @@ -963,11 +964,13 @@ xmlDoc *eDBadminGetCertificateInfo(eurephiaCTX *ctx, xmlDoc *srchxml, const char // </register_certificate> // </eurephia> // +// The function returns certid of the newly registered certificate on success, +// and -1 on failure int eDBadminAddCertificate(eurephiaCTX *ctx, xmlDoc *certxml) { - int rc = 0; xmlNode *crtinf_n = NULL; eDBfieldMap *crtinf_map = NULL, *ptr = NULL; dbresult *res = NULL; + int certid = 0; assert( (ctx != NULL) && (certxml != NULL) ); @@ -998,14 +1001,14 @@ int eDBadminAddCertificate(eurephiaCTX *ctx, xmlDoc *certxml) { 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"); - rc = 0; + certid = -1; } else { - rc = 1; + certid = res->last_insert_id; } sqlite_free_results(res); eDBfreeMapping(crtinf_map); - return rc; + return certid; } diff --git a/eurephiadm/commands/certificates.c b/eurephiadm/commands/certificates.c index 40e24d1..a4b07cd 100644 --- a/eurephiadm/commands/certificates.c +++ b/eurephiadm/commands/certificates.c @@ -166,7 +166,7 @@ int register_certificate(eurephiaCTX *ctx, int depth, const char *digest, xmlDoc *cert_xml = NULL; xmlNode *cert_n = NULL; char tmp[66], *cname_cp = NULL, *org_cp = NULL; - int rc = 0; + int certid = 0; assert( ctx != NULL ); @@ -189,14 +189,17 @@ int register_certificate(eurephiaCTX *ctx, int depth, const char *digest, xmlNewChild(cert_n, NULL, (xmlChar *) "email", (xmlChar *) email); // Register the certificate - rc = eDBadminAddCertificate(ctx, cert_xml); - fprintf(stdout, "%s: %s\n", MODULE, - (rc == 1 ? "Certificate registered successfully" : "Failed to register certificate")); + certid = eDBadminAddCertificate(ctx, cert_xml); + if( certid > 0 ) { + fprintf(stdout, "%s: Certificate registered successfully (certid %i)\n", MODULE, certid); + } else { + fprintf(stderr, "%s: Failed to register certificate\n", MODULE); + } xmlFreeDoc(cert_xml); free_nullsafe(cname_cp); free_nullsafe(org_cp); - return (rc != 1); + return certid; } int add_cert(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int argc, char **argv) { @@ -332,7 +335,8 @@ int add_cert(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int a } #endif - rc = register_certificate(ctx, depth, digest, cname, org, email); + // register_certificate returns value < 1, it means registration failed => rc = 1 + rc = (register_certificate(ctx, depth, digest, cname, org, email) < 1); exit: free_nullsafe(digest); free_nullsafe(cname); diff --git a/eurephiadm/commands/users.c b/eurephiadm/commands/users.c index 26d5eb7..d9d856d 100644 --- a/eurephiadm/commands/users.c +++ b/eurephiadm/commands/users.c @@ -720,7 +720,7 @@ int add_user(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int a xmlDoc *user_xml = NULL; xmlNode *node = NULL, *node2 = NULL; struct stat cert_stat; - int i = 0, certid = 0, rc = 0; + int i = 0, certid = 0, uid = 0; char *uname = NULL, *passwd = NULL, *certfile = NULL, *digest = NULL; e_options addu_args[] = { @@ -851,16 +851,19 @@ int add_user(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int a xmlNewProp(node2, (xmlChar *) "pwhash", (xmlChar *) "none"); // Add the user - rc = eDBadminAddUser(ctx, user_xml); - fprintf(stdout, "%s: %s\n", MODULE, - (rc == 1 ? "User registered successfully" : "Failed to register user")); + uid = eDBadminAddUser(ctx, user_xml); + if( uid > 0 ) { + fprintf(stdout, "%s: User registered successfully (user id %i)\n", MODULE, uid); + } else { + fprintf(stderr, "%s: Failed to register user\n", MODULE); + } memset(passwd, 0, strlen_nullsafe(passwd)); free_nullsafe(passwd); xmlFreeDoc(user_xml); // FIXME: Register certificate info - return (rc != 1); + return (uid > 0); } |
