diff options
Diffstat (limited to 'usr.sbin/ctld/ctld.cc')
| -rw-r--r-- | usr.sbin/ctld/ctld.cc | 72 |
1 files changed, 20 insertions, 52 deletions
diff --git a/usr.sbin/ctld/ctld.cc b/usr.sbin/ctld/ctld.cc index ba65befa2d0a..558ddb8ac6aa 100644 --- a/usr.sbin/ctld/ctld.cc +++ b/usr.sbin/ctld/ctld.cc @@ -143,42 +143,14 @@ conf_delete(struct conf *conf) free(conf); } -static struct auth * -auth_new(struct auth_group *ag) -{ - struct auth *auth; - - auth = reinterpret_cast<struct auth *>(calloc(1, sizeof(*auth))); - if (auth == NULL) - log_err(1, "calloc"); - auth->a_auth_group = ag; - TAILQ_INSERT_TAIL(&ag->ag_auths, auth, a_next); - return (auth); -} - -static void -auth_delete(struct auth *auth) -{ - TAILQ_REMOVE(&auth->a_auth_group->ag_auths, auth, a_next); - - free(auth->a_user); - free(auth->a_secret); - free(auth->a_mutual_user); - free(auth->a_mutual_secret); - free(auth); -} - const struct auth * auth_find(const struct auth_group *ag, const char *user) { - const struct auth *auth; + auto it = ag->ag_auths.find(user); + if (it == ag->ag_auths.end()) + return (nullptr); - TAILQ_FOREACH(auth, &ag->ag_auths, a_next) { - if (strcmp(auth->a_user, user) == 0) - return (auth); - } - - return (NULL); + return (&it->second); } static void @@ -188,6 +160,7 @@ auth_check_secret_length(const struct auth_group *ag, const char *user, size_t len; len = strlen(secret); + assert(len != 0); if (len > 16) { log_warnx("%s for user \"%s\", %s, is too long; it should be " "at most 16 characters long", secret_type, user, @@ -204,8 +177,6 @@ bool auth_new_chap(struct auth_group *ag, const char *user, const char *secret) { - struct auth *auth; - if (ag->ag_type == AG_TYPE_UNKNOWN) ag->ag_type = AG_TYPE_CHAP; if (ag->ag_type != AG_TYPE_CHAP) { @@ -216,9 +187,12 @@ auth_new_chap(struct auth_group *ag, const char *user, auth_check_secret_length(ag, user, secret, "secret"); - auth = auth_new(ag); - auth->a_user = checked_strdup(user); - auth->a_secret = checked_strdup(secret); + const auto &pair = ag->ag_auths.try_emplace(user, secret); + if (!pair.second) { + log_warnx("duplicate credentials for user \"%s\" for %s", + user, ag->ag_label); + return (false); + } return (true); } @@ -227,8 +201,6 @@ bool auth_new_chap_mutual(struct auth_group *ag, const char *user, const char *secret, const char *user2, const char *secret2) { - struct auth *auth; - if (ag->ag_type == AG_TYPE_UNKNOWN) ag->ag_type = AG_TYPE_CHAP_MUTUAL; if (ag->ag_type != AG_TYPE_CHAP_MUTUAL) { @@ -240,11 +212,13 @@ auth_new_chap_mutual(struct auth_group *ag, const char *user, auth_check_secret_length(ag, user, secret, "secret"); auth_check_secret_length(ag, user, secret2, "mutual secret"); - auth = auth_new(ag); - auth->a_user = checked_strdup(user); - auth->a_secret = checked_strdup(secret); - auth->a_mutual_user = checked_strdup(user2); - auth->a_mutual_secret = checked_strdup(secret2); + const auto &pair = ag->ag_auths.try_emplace(user, secret, user2, + secret2); + if (!pair.second) { + log_warnx("duplicate credentials for user \"%s\" for %s", + user, ag->ag_label); + return (false); + } return (true); } @@ -442,13 +416,10 @@ auth_group_create(struct conf *conf, const char *name, char *label) { struct auth_group *ag; - ag = reinterpret_cast<struct auth_group *>(calloc(1, sizeof(*ag))); - if (ag == NULL) - log_err(1, "calloc"); + ag = new auth_group(); if (name != NULL) ag->ag_name = checked_strdup(name); ag->ag_label = label; - TAILQ_INIT(&ag->ag_auths); TAILQ_INIT(&ag->ag_names); TAILQ_INIT(&ag->ag_portals); ag->ag_conf = conf; @@ -485,14 +456,11 @@ auth_group_new(struct conf *conf, struct target *target) void auth_group_delete(struct auth_group *ag) { - struct auth *auth, *auth_tmp; struct auth_name *auth_name, *auth_name_tmp; struct auth_portal *auth_portal, *auth_portal_tmp; TAILQ_REMOVE(&ag->ag_conf->conf_auth_groups, ag, ag_next); - TAILQ_FOREACH_SAFE(auth, &ag->ag_auths, a_next, auth_tmp) - auth_delete(auth); TAILQ_FOREACH_SAFE(auth_name, &ag->ag_names, an_next, auth_name_tmp) auth_name_delete(auth_name); TAILQ_FOREACH_SAFE(auth_portal, &ag->ag_portals, ap_next, @@ -500,7 +468,7 @@ auth_group_delete(struct auth_group *ag) auth_portal_delete(auth_portal); free(ag->ag_label); free(ag->ag_name); - free(ag); + delete ag; } struct auth_group * |
