From: Noel Power Date: Fri, 20 Oct 2023 10:46:56 +0000 (+0100) Subject: s3/winbindd: Add new parse_domain_user function X-Git-Tag: talloc-2.4.2~1167 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d4341d4884244b6dbc7cce8f0ef150964fcacc86;p=thirdparty%2Fsamba.git s3/winbindd: Add new parse_domain_user function Adds a new parse_domain_user function which doesn't use fstrings but instead uses talloc allocated out strings (created from passed in ctx) Signed-off-by: Noel Power Reviewed-by: Andrew Bartlett Reviewed-by: Andreas Schneider --- diff --git a/source3/winbindd/winbindd_proto.h b/source3/winbindd/winbindd_proto.h index 4adc09b667a..90433ee3a36 100644 --- a/source3/winbindd/winbindd_proto.h +++ b/source3/winbindd/winbindd_proto.h @@ -516,6 +516,21 @@ struct winbindd_domain *find_our_domain(void); struct winbindd_domain *find_default_route_domain(void); struct winbindd_domain *find_lookup_domain_from_sid(const struct dom_sid *sid); struct winbindd_domain *find_lookup_domain_from_name(const char *domain_name); +/** + * Parse a DOMAIN\user or UPN string into a domain, namespace and a user + * + * @param[in] ctx talloc context + * @param[in] domuser a DOMAIN\user or UPN string + * @param[out] namespace + * @param[out] domain + * @param[out] user + * @return bool indicating success or failure + */ +bool parse_domain_user(TALLOC_CTX *ctx, + const char *domuser, + char **namespace, + char **domain, + char **user); bool parse_domain_user_fstr(const char *domuser, fstring namespace, fstring domain, diff --git a/source3/winbindd/winbindd_util.c b/source3/winbindd/winbindd_util.c index 8592164a045..1f3786376f2 100644 --- a/source3/winbindd/winbindd_util.c +++ b/source3/winbindd/winbindd_util.c @@ -1546,6 +1546,91 @@ static bool assume_domain(const char *domain) return False; } +/* Parse a DOMAIN\user or UPN string into a domain, namespace and a user */ +bool parse_domain_user(TALLOC_CTX *ctx, + const char *domuser, + char **pnamespace, + char **pdomain, + char **puser) +{ + char *p = NULL; + char *namespace = NULL; + char *domain = NULL; + char *user = NULL; + + if (strlen(domuser) == 0) { + return false; + } + + p = strchr(domuser, *lp_winbind_separator()); + if (p != NULL) { + user = talloc_strdup(ctx, p + 1); + if (user == NULL) { + goto fail; + } + domain = talloc_strdup(ctx, + domuser); + if (domain == NULL) { + goto fail; + } + domain[PTR_DIFF(p, domuser)] = '\0'; + namespace = talloc_strdup(ctx, domain); + if (namespace == NULL) { + goto fail; + } + } else { + user = talloc_strdup(ctx, domuser); + if (user == NULL) { + goto fail; + } + p = strchr(domuser, '@'); + if (p != NULL) { + /* upn */ + namespace = talloc_strdup(ctx, p + 1); + if (namespace == NULL) { + goto fail; + } + domain = talloc_strdup(ctx, ""); + if (domain == NULL) { + goto fail; + } + + } else if (assume_domain(lp_workgroup())) { + domain = talloc_strdup(ctx, lp_workgroup()); + if (domain == NULL) { + goto fail; + } + namespace = talloc_strdup(ctx, domain); + if (namespace == NULL) { + goto fail; + } + } else { + namespace = talloc_strdup(ctx, lp_netbios_name()); + if (namespace == NULL) { + goto fail; + } + domain = talloc_strdup(ctx, ""); + if (domain == NULL) { + goto fail; + } + } + } + + if (!strupper_m(domain)) { + goto fail; + } + + *pnamespace = namespace; + *pdomain = domain; + *puser = user; + return true; +fail: + TALLOC_FREE(user); + TALLOC_FREE(domain); + TALLOC_FREE(namespace); + return false; +} + /* Parse a DOMAIN\user or UPN string into a domain, namespace and a user */ bool parse_domain_user_fstr(const char *domuser, fstring namespace,