From abcc9f16474898de076168e023eb8f17c7ed8d11 Mon Sep 17 00:00:00 2001 From: James Prestwood Date: Mon, 8 Apr 2019 11:15:19 -0700 Subject: [PATCH] util: add APIs to get username/domain from identity mschaputil already had similar functionality, but ERP will need this as well. These two functions will also handle identities with either '@' or '\' to separate the user and domain. --- src/util.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ src/util.h | 3 +++ 2 files changed, 49 insertions(+) diff --git a/src/util.c b/src/util.c index 49561c73..1d89159c 100644 --- a/src/util.c +++ b/src/util.c @@ -160,3 +160,49 @@ bool util_is_broadcast_address(const uint8_t *addr) return !memcmp(addr, bcast_addr, 6); } + +const char *util_get_domain(const char *identity) +{ + static char domain[256]; + const char *c; + + memset(domain, 0, sizeof(domain)); + + for (c = identity; *c; c++) { + switch (*c) { + case '\\': + strncpy(domain, identity, c - identity); + return domain; + case '@': + strcpy(domain, c + 1); + return domain; + default: + continue; + } + } + + return identity; +} + +const char *util_get_username(const char *identity) +{ + static char username[256]; + const char *c; + + memset(username, 0, sizeof(username)); + + for (c = identity; *c; c++) { + switch (*c) { + case '\\': + strcpy(username, c + 1); + return username; + case '@': + strncpy(username, identity, c - identity); + return username; + default: + continue; + } + } + + return identity; +} diff --git a/src/util.h b/src/util.h index d379aa1b..55075b67 100644 --- a/src/util.h +++ b/src/util.h @@ -39,6 +39,9 @@ bool util_string_to_address(const char *str, uint8_t *addr); bool util_is_group_address(const uint8_t *addr); bool util_is_broadcast_address(const uint8_t *addr); +const char *util_get_domain(const char *identity); +const char *util_get_username(const char *identity); + static inline uint8_t util_bit_field(const uint8_t oct, int start, int num) { unsigned char mask = (1 << num) - 1;