winbindd: use passdb backend for well-known SIDs
[amitay/samba.git] / source3 / winbindd / winbindd_util.c
index d4a1cf36547bd34d19dd2a669cd172a95c1dd333..bfe6ccad233f46c6cbcd539263da91ff15a360a4 100644 (file)
@@ -22,6 +22,7 @@
 
 #include "includes.h"
 #include "winbindd.h"
+#include "lib/util_unixsids.h"
 #include "secrets.h"
 #include "../libcli/security/security.h"
 #include "../libcli/auth/pam_errors.h"
 #include "passdb.h"
 #include "source4/lib/messaging/messaging.h"
 #include "librpc/gen_ndr/ndr_lsa.h"
+#include "auth/credentials/credentials.h"
+#include "libsmb/samlogon_cache.h"
 
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_WINBIND
 
-extern struct winbindd_methods cache_methods;
+static struct winbindd_domain *
+add_trusted_domain_from_tdc(const struct winbindd_tdc_domain *tdc);
 
 /**
  * @file winbindd_util.c
@@ -87,10 +91,13 @@ struct winbindd_domain *wb_next_domain(struct winbindd_domain *domain)
                domain = domain->next;
        }
 
-       if ((domain != NULL)
-           && sid_check_is_our_sam(&domain->sid)) {
+       if ((domain != NULL) &&
+           (lp_server_role() != ROLE_ACTIVE_DIRECTORY_DC) &&
+           sid_check_is_our_sam(&domain->sid))
+       {
                domain = domain->next;
        }
+
        return domain;
 }
 
@@ -115,16 +122,39 @@ static bool is_in_internal_domain(const struct dom_sid *sid)
    If the domain already exists in the list,
    return it and don't re-initialize.  */
 
-static struct winbindd_domain *add_trusted_domain(const char *domain_name, const char *alt_name,
-                                                 struct winbindd_methods *methods,
-                                                 const struct dom_sid *sid)
+static struct winbindd_domain *
+add_trusted_domain(const char *domain_name, const char *alt_name,
+                  const struct dom_sid *sid)
+{
+       struct winbindd_tdc_domain tdc;
+
+       ZERO_STRUCT(tdc);
+
+       tdc.domain_name = domain_name;
+       tdc.dns_name = alt_name;
+       if (sid) {
+               sid_copy(&tdc.sid, sid);
+       }
+
+       return add_trusted_domain_from_tdc(&tdc);
+}
+
+/* Add a trusted domain out of a trusted domain cache
+   entry
+*/
+static struct winbindd_domain *
+add_trusted_domain_from_tdc(const struct winbindd_tdc_domain *tdc)
 {
        struct winbindd_domain *domain;
        const char *alternative_name = NULL;
-       char *idmap_config_option;
-       const char *param;
        const char **ignored_domains, **dom;
        int role = lp_server_role();
+       const char *domain_name = tdc->domain_name;
+       const struct dom_sid *sid = &tdc->sid;
+
+       if (is_null_sid(sid)) {
+               sid = NULL;
+       }
 
        ignored_domains = lp_parm_string_list(-1, "winbind", "ignore domains", NULL);
        for (dom=ignored_domains; dom && *dom; dom++) {
@@ -136,8 +166,8 @@ static struct winbindd_domain *add_trusted_domain(const char *domain_name, const
 
        /* use alt_name if available to allow DNS lookups */
 
-       if (alt_name && *alt_name) {
-               alternative_name = alt_name;
+       if (tdc->dns_name && *tdc->dns_name) {
+               alternative_name = tdc->dns_name;
        }
 
        /* We can't call domain_list() as this function is called from
@@ -149,8 +179,7 @@ static struct winbindd_domain *add_trusted_domain(const char *domain_name, const
                        break;
                }
 
-               if (alternative_name && *alternative_name)
-               {
+               if (alternative_name) {
                        if (strequal(alternative_name, domain->name) ||
                            strequal(alternative_name, domain->alt_name))
                        {
@@ -158,12 +187,7 @@ static struct winbindd_domain *add_trusted_domain(const char *domain_name, const
                        }
                }
 
-               if (sid)
-               {
-                       if (is_null_sid(sid)) {
-                               continue;
-                       }
-
+               if (sid != NULL) {
                        if (dom_sid_equal(sid, &domain->sid)) {
                                break;
                        }
@@ -212,18 +236,20 @@ static struct winbindd_domain *add_trusted_domain(const char *domain_name, const
                }
        }
 
-       domain->methods = methods;
        domain->backend = NULL;
        domain->internal = is_internal_domain(sid);
        domain->sequence_number = DOM_SEQUENCE_NONE;
        domain->last_seq_check = 0;
-       domain->initialized = False;
+       domain->initialized = false;
        domain->online = is_internal_domain(sid);
        domain->check_online_timeout = 0;
        domain->dc_probe_pid = (pid_t)-1;
-       if (sid) {
+       if (sid != NULL) {
                sid_copy(&domain->sid, sid);
        }
+       domain->domain_flags = tdc->trust_flags;
+       domain->domain_type = tdc->trust_type;
+       domain->domain_trust_attribs = tdc->trust_attribs;
 
        /* Is this our primary domain ? */
        if (strequal(domain_name, get_global_sam_name()) &&
@@ -241,49 +267,22 @@ static struct winbindd_domain *add_trusted_domain(const char *domain_name, const
                if (lp_security() == SEC_ADS) {
                        domain->active_directory = true;
                }
+       } else if (!domain->internal) {
+               if (domain->domain_type == LSA_TRUST_TYPE_UPLEVEL) {
+                       domain->active_directory = true;
+               }
        }
 
        /* Link to domain list */
-       DLIST_ADD_END(_domain_list, domain, struct winbindd_domain *);
+       DLIST_ADD_END(_domain_list, domain);
 
        wcache_tdc_add_domain( domain );
 
-       idmap_config_option = talloc_asprintf(talloc_tos(), "idmap config %s",
-                                             domain->name);
-       if (idmap_config_option == NULL) {
-               DEBUG(0, ("talloc failed, not looking for idmap config\n"));
-               goto done;
-       }
-
-       param = lp_parm_const_string(-1, idmap_config_option, "range", NULL);
-
-       DEBUG(10, ("%s : range = %s\n", idmap_config_option,
-                  param ? param : "not defined"));
-
-       if (param != NULL) {
-               unsigned low_id, high_id;
-               if (sscanf(param, "%u - %u", &low_id, &high_id) != 2) {
-                       DEBUG(1, ("invalid range syntax in %s: %s\n",
-                                 idmap_config_option, param));
-                       goto done;
-               }
-               if (low_id > high_id) {
-                       DEBUG(1, ("invalid range in %s: %s\n",
-                                 idmap_config_option, param));
-                       goto done;
-               }
-               domain->have_idmap_config = true;
-               domain->id_range_low = low_id;
-               domain->id_range_high = high_id;
-       }
-
-done:
-
        setup_domain_child(domain);
 
-       DEBUG(2,("Added domain %s %s %s\n",
-                domain->name, domain->alt_name,
-                &domain->sid?sid_string_dbg(&domain->sid):""));
+       DEBUG(2,
+             ("Added domain %s %s %s\n", domain->name, domain->alt_name,
+              !is_null_sid(&domain->sid) ? sid_string_dbg(&domain->sid) : ""));
 
        return domain;
 }
@@ -341,24 +340,52 @@ static void trustdom_list_done(struct tevent_req *req)
        struct winbindd_response *response;
        int res, err;
        char *p;
+       struct winbindd_tdc_domain trust_params = {0};
+       ptrdiff_t extra_len;
+       bool within_forest = false;
+
+       /*
+        * Only when we enumerate our primary domain
+        * or our forest root domain, we should keep
+        * the NETR_TRUST_FLAG_IN_FOREST flag, in
+        * all other cases we need to clear it as the domain
+        * is not part of our forest.
+        */
+       if (state->domain->primary) {
+               within_forest = true;
+       } else if (domain_is_forest_root(state->domain)) {
+               within_forest = true;
+       }
 
        res = wb_domain_request_recv(req, state, &response, &err);
        if ((res == -1) || (response->result != WINBINDD_OK)) {
-               DEBUG(1, ("Could not receive trustdoms\n"));
+               DBG_WARNING("Could not receive trusts for domain %s\n",
+                           state->domain->name);
                TALLOC_FREE(state);
                return;
        }
 
+       if (response->length < sizeof(struct winbindd_response)) {
+               DBG_ERR("ill-formed trustdom response - short length\n");
+               TALLOC_FREE(state);
+               return;
+       }
+
+       extra_len = response->length - sizeof(struct winbindd_response);
+
        p = (char *)response->extra_data.data;
 
-       while ((p != NULL) && (*p != '\0')) {
+       while ((p - (char *)response->extra_data.data) < extra_len) {
                char *q, *sidstr, *alt_name;
-               struct dom_sid sid;
-               char *alternate_name = NULL;
+
+               DBG_DEBUG("parsing response line '%s'\n", p);
+
+               ZERO_STRUCT(trust_params);
+               trust_params.domain_name = p;
 
                alt_name = strchr(p, '\\');
                if (alt_name == NULL) {
-                       DEBUG(0, ("Got invalid trustdom response\n"));
+                       DBG_ERR("Got invalid trustdom response\n");
                        break;
                }
 
@@ -367,26 +394,60 @@ static void trustdom_list_done(struct tevent_req *req)
 
                sidstr = strchr(alt_name, '\\');
                if (sidstr == NULL) {
-                       DEBUG(0, ("Got invalid trustdom response\n"));
+                       DBG_ERR("Got invalid trustdom response\n");
                        break;
                }
 
                *sidstr = '\0';
                sidstr += 1;
 
-               q = strchr(sidstr, '\n');
-               if (q != NULL)
-                       *q = '\0';
+               /* use the real alt_name if we have one, else pass in NULL */
+               if (!strequal(alt_name, "(null)")) {
+                       trust_params.dns_name = alt_name;
+               }
+
+               q = strtok(sidstr, "\\");
+               if (q == NULL) {
+                       DBG_ERR("Got invalid trustdom response\n");
+                       break;
+               }
 
-               if (!string_to_sid(&sid, sidstr)) {
+               if (!string_to_sid(&trust_params.sid, sidstr)) {
                        DEBUG(0, ("Got invalid trustdom response\n"));
                        break;
                }
 
-               /* use the real alt_name if we have one, else pass in NULL */
+               q = strtok(NULL, "\\");
+               if (q == NULL) {
+                       DBG_ERR("Got invalid trustdom response\n");
+                       break;
+               }
 
-               if ( !strequal( alt_name, "(null)" ) )
-                       alternate_name = alt_name;
+               trust_params.trust_flags = (uint32_t)strtoul(q, NULL, 10);
+
+               q = strtok(NULL, "\\");
+               if (q == NULL) {
+                       DBG_ERR("Got invalid trustdom response\n");
+                       break;
+               }
+
+               trust_params.trust_type = (uint32_t)strtoul(q, NULL, 10);
+
+               q = strtok(NULL, "\n");
+               if (q == NULL) {
+                       DBG_ERR("Got invalid trustdom response\n");
+                       break;
+               }
+
+               trust_params.trust_attribs = (uint32_t)strtoul(q, NULL, 10);
+
+               if (!within_forest) {
+                       trust_params.trust_flags &= ~NETR_TRUST_FLAG_IN_FOREST;
+               }
+
+               if (!state->domain->primary) {
+                       trust_params.trust_flags &= ~NETR_TRUST_FLAG_PRIMARY;
+               }
 
                /*
                 * We always call add_trusted_domain() cause on an existing
@@ -394,13 +455,9 @@ static void trustdom_list_done(struct tevent_req *req)
                 * This is important because we need the SID for sibling
                 * domains.
                 */
-               (void)add_trusted_domain(p, alternate_name,
-                                           &cache_methods,
-                                           &sid);
+               (void)add_trusted_domain_from_tdc(&trust_params);
 
-               p=q;
-               if (p != NULL)
-                       p += 1;
+               p = q + strlen(q) + 1;
        }
 
        /*
@@ -467,10 +524,7 @@ static void rescan_forest_root_trusts( void )
                d = find_domain_from_name_noinit( dom_list[i].domain_name );
 
                if ( !d ) {
-                       d = add_trusted_domain( dom_list[i].domain_name,
-                                               dom_list[i].dns_name,
-                                               &cache_methods,
-                                               &dom_list[i].sid );
+                       d = add_trusted_domain_from_tdc(&dom_list[i]);
                }
 
                if (d == NULL) {
@@ -536,10 +590,7 @@ static void rescan_forest_trusts( void )
                           about it */
 
                        if ( !d ) {
-                               d = add_trusted_domain( dom_list[i].domain_name,
-                                                       dom_list[i].dns_name,
-                                                       &cache_methods,
-                                                       &dom_list[i].sid );
+                               d = add_trusted_domain_from_tdc(&dom_list[i]);
                        }
 
                        if (d == NULL) {
@@ -666,7 +717,6 @@ static void wb_imsg_new_trusted_domain(struct imessaging_context *msg,
 
        d = add_trusted_domain(info.netbios_name.string,
                               info.domain_name.string,
-                              &cache_methods,
                               info.sid);
        if (d == NULL) {
                TALLOC_FREE(frame);
@@ -698,6 +748,49 @@ static void wb_imsg_new_trusted_domain(struct imessaging_context *msg,
        TALLOC_FREE(frame);
 }
 
+/*
+ * We did not get the secret when we queried secrets.tdb, so read it
+ * from secrets.tdb and re-sync the databases
+ */
+static bool migrate_secrets_tdb_to_ldb(struct winbindd_domain *domain)
+{
+       bool ok;
+       struct cli_credentials *creds;
+       NTSTATUS can_migrate = pdb_get_trust_credentials(domain->name,
+                                                        NULL, domain, &creds);
+       if (!NT_STATUS_IS_OK(can_migrate)) {
+               DEBUG(0, ("Failed to fetch our own, local AD domain join "
+                       "password for winbindd's internal use, both from "
+                       "secrets.tdb and secrets.ldb: %s\n",
+                       nt_errstr(can_migrate)));
+               return false;
+       }
+
+       /*
+        * NOTE: It is very unlikely we end up here if there is an
+        * oldpass, because a new password is created at
+        * classicupgrade, so this is not a concern.
+        */
+       ok = secrets_store_machine_pw_sync(cli_credentials_get_password(creds),
+                  NULL /* oldpass */,
+                  cli_credentials_get_domain(creds),
+                  cli_credentials_get_realm(creds),
+                  cli_credentials_get_salt_principal(creds),
+                  0, /* Supported enc types, unused */
+                  &domain->sid,
+                  cli_credentials_get_password_last_changed_time(creds),
+                  cli_credentials_get_secure_channel_type(creds),
+                  false /* do_delete: Do not delete */);
+       TALLOC_FREE(creds);
+       if (ok == false) {
+               DEBUG(0, ("Failed to write our our own, "
+                         "local AD domain join password for "
+                         "winbindd's internal use into secrets.tdb\n"));
+               return false;
+       }
+       return true;
+}
+
 /* Look up global info for the winbind daemon */
 bool init_domain_list(void)
 {
@@ -709,8 +802,7 @@ bool init_domain_list(void)
 
        /* BUILTIN domain */
 
-       (void)add_trusted_domain("BUILTIN", NULL, &cache_methods,
-                                   &global_sid_Builtin);
+       (void)add_trusted_domain("BUILTIN", NULL, &global_sid_Builtin);
 
        /* Local SAM */
 
@@ -719,12 +811,22 @@ bool init_domain_list(void)
                enum netr_SchannelType sec_chan_type;
                const char *account_name;
                struct samr_Password current_nt_hash;
+               struct pdb_domain_info *pdb_domain_info;
                bool ok;
 
-               domain = add_trusted_domain(get_global_sam_name(), lp_dnsdomain(),
-                                           &cache_methods, get_global_sam_sid());
+               pdb_domain_info = pdb_get_domain_info(talloc_tos());
+               if (pdb_domain_info == NULL) {
+                       DEBUG(0, ("Failed to fetch our own, local AD "
+                               "domain info from sam.ldb\n"));
+                       return false;
+               }
+               domain = add_trusted_domain(pdb_domain_info->name,
+                                       pdb_domain_info->dns_domain,
+                                       &pdb_domain_info->sid);
+               TALLOC_FREE(pdb_domain_info);
                if (domain == NULL) {
-                       DEBUG(0, ("Failed to add our own, local AD domain to winbindd's internal list\n"));
+                       DEBUG(0, ("Failed to add our own, local AD "
+                               "domain to winbindd's internal list\n"));
                        return false;
                }
 
@@ -736,8 +838,32 @@ bool init_domain_list(void)
                                       &account_name,
                                       &sec_chan_type);
                if (!ok) {
-                       DEBUG(0, ("Failed to fetch our own, local AD domain join password for winbindd's internal use\n"));
-                       return false;
+                       /*
+                        * If get_trust_pw_hash() fails, then try and
+                        * fetch the password from the more recent of
+                        * secrets.{ldb,tdb} using the
+                        * pdb_get_trust_credentials()
+                        */
+                       ok = migrate_secrets_tdb_to_ldb(domain);
+
+                       if (ok == false) {
+                               DEBUG(0, ("Failed to migrate our own, "
+                                         "local AD domain join password for "
+                                         "winbindd's internal use into "
+                                         "secrets.tdb\n"));
+                               return false;
+                       }
+                       ok = get_trust_pw_hash(domain->name,
+                                              current_nt_hash.hash,
+                                              &account_name,
+                                              &sec_chan_type);
+                       if (ok == false) {
+                               DEBUG(0, ("Failed to find our our own, just "
+                                         "written local AD domain join "
+                                         "password for winbindd's internal "
+                                         "use in secrets.tdb\n"));
+                               return false;
+                       }
                }
                if (sec_chan_type == SEC_CHAN_RODC) {
                        domain->rodc = true;
@@ -745,7 +871,7 @@ bool init_domain_list(void)
 
        } else {
                (void)add_trusted_domain(get_global_sam_name(), NULL,
-                                        &cache_methods, get_global_sam_sid());
+                                        get_global_sam_sid());
        }
        /* Add ourselves as the first entry. */
 
@@ -758,8 +884,8 @@ bool init_domain_list(void)
                        return False;
                }
 
-               domain = add_trusted_domain( lp_workgroup(), lp_realm(),
-                                            &cache_methods, &our_sid);
+               domain = add_trusted_domain(lp_workgroup(), lp_realm(),
+                                           &our_sid);
                if (domain) {
                        /* Even in the parent winbindd we'll need to
                           talk to the DC, so try and see if we can
@@ -879,39 +1005,23 @@ struct winbindd_domain *find_our_domain(void)
        return NULL;
 }
 
-struct winbindd_domain *find_root_domain(void)
-{
-       struct winbindd_domain *ours = find_our_domain();
-
-       if (ours->forest_name == NULL) {
-               return NULL;
-       }
-
-       return find_domain_from_name( ours->forest_name );
-}
-
-struct winbindd_domain *find_builtin_domain(void)
-{
-       struct winbindd_domain *domain;
-
-       domain = find_domain_from_sid(&global_sid_Builtin);
-       if (domain == NULL) {
-               smb_panic("Could not find BUILTIN domain");
-       }
-
-       return domain;
-}
-
 /* Find the appropriate domain to lookup a name or SID */
 
 struct winbindd_domain *find_lookup_domain_from_sid(const struct dom_sid *sid)
 {
-       /* SIDs in the S-1-22-{1,2} domain should be handled by our passdb */
+       DBG_DEBUG("SID [%s]\n", sid_string_dbg(sid));
+
+       /*
+        * SIDs in the S-1-22-{1,2} domain and well-known SIDs should be handled
+        * by our passdb.
+        */
 
        if ( sid_check_is_in_unix_groups(sid) ||
             sid_check_is_unix_groups(sid) ||
             sid_check_is_in_unix_users(sid) ||
-            sid_check_is_unix_users(sid) )
+            sid_check_is_unix_users(sid) ||
+            sid_check_is_wellknown_domain(sid, NULL) ||
+            sid_check_is_in_wellknown_domain(sid) )
        {
                return find_domain_from_sid(get_global_sam_sid());
        }
@@ -920,8 +1030,6 @@ struct winbindd_domain *find_lookup_domain_from_sid(const struct dom_sid *sid)
         * one to contact the external DC's. On member servers the internal
         * domains are different: These are part of the local SAM. */
 
-       DEBUG(10, ("find_lookup_domain_from_sid(%s)\n", sid_string_dbg(sid)));
-
        if (IS_DC || is_internal_domain(sid) || is_in_internal_domain(sid)) {
                DEBUG(10, ("calling find_domain_from_sid\n"));
                return find_domain_from_sid(sid);
@@ -990,10 +1098,11 @@ bool parse_domain_user(const char *domuser, fstring domain, fstring user)
 
        if ( !p ) {
                fstrcpy(user, domuser);
+               p = strchr(domuser, '@');
 
-               if ( assume_domain(lp_workgroup())) {
+               if ( assume_domain(lp_workgroup()) && p == NULL) {
                        fstrcpy(domain, lp_workgroup());
-               } else if ((p = strchr(domuser, '@')) != NULL) {
+               } else if (p != NULL) {
                        fstrcpy(domain, p + 1);
                        user[PTR_DIFF(p, domuser)] = 0;
                } else {
@@ -1052,12 +1161,18 @@ bool canonicalize_username(fstring username_inout, fstring domain, fstring user)
     Also, if omit DOMAIN if 'winbind trusted domains only = true', as the
     username is then unqualified in unix
 
+    On an AD DC we always fill DOMAIN\\USERNAME.
+
     We always canonicalize as UPPERCASE DOMAIN, lowercase username.
 */
 void fill_domain_username(fstring name, const char *domain, const char *user, bool can_assume)
 {
        fstring tmp_user;
 
+       if (lp_server_role() == ROLE_ACTIVE_DIRECTORY_DC) {
+               can_assume = false;
+       }
+
        fstrcpy(tmp_user, user);
        (void)strlower_m(tmp_user);
 
@@ -1081,6 +1196,10 @@ char *fill_domain_username_talloc(TALLOC_CTX *mem_ctx,
 {
        char *tmp_user, *name;
 
+       if (lp_server_role() == ROLE_ACTIVE_DIRECTORY_DC) {
+               can_assume = false;
+       }
+
        tmp_user = talloc_strdup(mem_ctx, user);
        if (!strlower_m(tmp_user)) {
                TALLOC_FREE(tmp_user);
@@ -1114,10 +1233,26 @@ struct winbindd_cli_state *winbindd_client_list(void)
        return _client_list;
 }
 
+/* Return list-tail of all connected clients */
+
+struct winbindd_cli_state *winbindd_client_list_tail(void)
+{
+       return DLIST_TAIL(_client_list);
+}
+
+/* Return previous (read:newer) client in list */
+
+struct winbindd_cli_state *
+winbindd_client_list_prev(struct winbindd_cli_state *cli)
+{
+       return DLIST_PREV(cli);
+}
+
 /* Add a connection to the list */
 
 void winbindd_add_client(struct winbindd_cli_state *cli)
 {
+       cli->last_access = time(NULL);
        DLIST_ADD(_client_list, cli);
        _num_clients++;
 }
@@ -1130,6 +1265,14 @@ void winbindd_remove_client(struct winbindd_cli_state *cli)
        _num_clients--;
 }
 
+/* Move a client to head or list */
+
+void winbindd_promote_client(struct winbindd_cli_state *cli)
+{
+       cli->last_access = time(NULL);
+       DLIST_PROMOTE(_client_list, cli);
+}
+
 /* Return number of open clients */
 
 int winbindd_num_clients(void)
@@ -1137,8 +1280,7 @@ int winbindd_num_clients(void)
        return _num_clients;
 }
 
-NTSTATUS lookup_usergroups_cached(struct winbindd_domain *domain,
-                                 TALLOC_CTX *mem_ctx,
+NTSTATUS lookup_usergroups_cached(TALLOC_CTX *mem_ctx,
                                  const struct dom_sid *user_sid,
                                  uint32_t *p_num_groups, struct dom_sid **user_sids)
 {
@@ -1157,11 +1299,6 @@ NTSTATUS lookup_usergroups_cached(struct winbindd_domain *domain,
                return NT_STATUS_OBJECT_NAME_NOT_FOUND;
        }
 
-       if (info3->base.groups.count == 0) {
-               TALLOC_FREE(info3);
-               return NT_STATUS_UNSUCCESSFUL;
-       }
-
        /*
         * Before bug #7843 the "Domain Local" groups were added with a
         * lookupuseraliases call, but this isn't done anymore for our domain
@@ -1514,7 +1651,7 @@ bool parse_sidlist(TALLOC_CTX *mem_ctx, const char *sidstr,
                        DEBUG(1, ("Could not parse sid %s\n", p));
                        return false;
                }
-               if ((q == NULL) || (q[0] != '\n')) {
+               if (q[0] != '\n') {
                        DEBUG(1, ("Got invalid sidstr: %s\n", p));
                        return false;
                }
@@ -1527,3 +1664,67 @@ bool parse_sidlist(TALLOC_CTX *mem_ctx, const char *sidstr,
        }
        return True;
 }
+
+bool parse_xidlist(TALLOC_CTX *mem_ctx, const char *xidstr,
+                  struct unixid **pxids, uint32_t *pnum_xids)
+{
+       const char *p;
+       struct unixid *xids = NULL;
+       uint32_t num_xids = 0;
+
+       p = xidstr;
+       if (p == NULL) {
+               return false;
+       }
+
+       while (p[0] != '\0') {
+               struct unixid *tmp;
+               struct unixid xid;
+               unsigned long long id;
+               char *endp;
+
+               switch (p[0]) {
+               case 'U':
+                       xid = (struct unixid) { .type = ID_TYPE_UID };
+                       break;
+               case 'G':
+                       xid = (struct unixid) { .type = ID_TYPE_GID };
+                       break;
+               default:
+                       return false;
+               }
+
+               p += 1;
+
+               id = strtoull(p, &endp, 10);
+               if ((id == ULLONG_MAX) && (errno == ERANGE)) {
+                       goto fail;
+               }
+               if (*endp != '\n') {
+                       goto fail;
+               }
+               p = endp+1;
+
+               xid.id = id;
+               if ((unsigned long long)xid.id != id) {
+                       goto fail;
+               }
+
+               tmp = talloc_realloc(mem_ctx, xids, struct unixid, num_xids+1);
+               if (tmp == NULL) {
+                       return 0;
+               }
+               xids = tmp;
+
+               xids[num_xids] = xid;
+               num_xids += 1;
+       }
+
+       *pxids = xids;
+       *pnum_xids = num_xids;
+       return true;
+
+fail:
+       TALLOC_FREE(xids);
+       return false;
+}