s3/winbindd: Add new parse_domain_user function
authorNoel Power <noel.power@suse.com>
Fri, 20 Oct 2023 10:46:56 +0000 (11:46 +0100)
committerNoel Power <npower@samba.org>
Tue, 24 Oct 2023 12:43:37 +0000 (12:43 +0000)
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 <noel.power@suse.com>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
Reviewed-by: Andreas Schneider <asn@samba.org>
source3/winbindd/winbindd_proto.h
source3/winbindd/winbindd_util.c

index 4adc09b667a9a073f4c9de9084627195b9f79685..90433ee3a36da811c1c966240ce94a40f5e4d050 100644 (file)
@@ -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,
index 8592164a04541b9bf82d0df287ca85d256b3d726..1f3786376f271c9ce07c2be257a92391b53e182d 100644 (file)
@@ -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,