s3-lsa: implement _lsa_EnumAccountsWithUserRight().
[ira/wip.git] / source3 / rpc_server / srv_lsa_nt.c
index f187432baffe6f155edef08d297e1d70e6a5cf76..f5cfc538400213dbe43bef70fb1fd5cd4a8773e6 100644 (file)
@@ -333,49 +333,6 @@ static NTSTATUS make_lsa_object_sd(TALLOC_CTX *mem_ctx, SEC_DESC **sd, size_t *s
        return NT_STATUS_OK;
 }
 
-#if 0  /* AD DC work in ongoing in Samba 4 */
-
-/***************************************************************************
- Init_dns_dom_info.
-***************************************************************************/
-
-static void init_dns_dom_info(LSA_DNS_DOM_INFO *r_l, const char *nb_name,
-                             const char *dns_name, const char *forest_name,
-                             struct GUID *dom_guid, DOM_SID *dom_sid)
-{
-       if (nb_name && *nb_name) {
-               init_unistr2(&r_l->uni_nb_dom_name, nb_name, UNI_FLAGS_NONE);
-               init_uni_hdr(&r_l->hdr_nb_dom_name, &r_l->uni_nb_dom_name);
-               r_l->hdr_nb_dom_name.uni_max_len += 2;
-               r_l->uni_nb_dom_name.uni_max_len += 1;
-       }
-
-       if (dns_name && *dns_name) {
-               init_unistr2(&r_l->uni_dns_dom_name, dns_name, UNI_FLAGS_NONE);
-               init_uni_hdr(&r_l->hdr_dns_dom_name, &r_l->uni_dns_dom_name);
-               r_l->hdr_dns_dom_name.uni_max_len += 2;
-               r_l->uni_dns_dom_name.uni_max_len += 1;
-       }
-
-       if (forest_name && *forest_name) {
-               init_unistr2(&r_l->uni_forest_name, forest_name, UNI_FLAGS_NONE);
-               init_uni_hdr(&r_l->hdr_forest_name, &r_l->uni_forest_name);
-               r_l->hdr_forest_name.uni_max_len += 2;
-               r_l->uni_forest_name.uni_max_len += 1;
-       }
-
-       /* how do we init the guid ? probably should write an init fn */
-       if (dom_guid) {
-               memcpy(&r_l->dom_guid, dom_guid, sizeof(struct GUID));
-       }
-
-       if (dom_sid) {
-               r_l->ptr_dom_sid = 1;
-               init_dom_sid2(&r_l->dom_sid, dom_sid);
-       }
-}
-#endif /* AD DC work in ongoing in Samba 4 */
-
 
 /***************************************************************************
  _lsa_OpenPolicy2
@@ -392,7 +349,9 @@ NTSTATUS _lsa_OpenPolicy2(pipes_struct *p,
        NTSTATUS status;
 
        /* Work out max allowed. */
-       map_max_allowed_access(p->server_info->ptok, &des_access);
+       map_max_allowed_access(p->server_info->ptok,
+                              &p->server_info->utok,
+                              &des_access);
 
        /* map the generic bits to the lsa policy ones */
        se_map_generic(&des_access, &lsa_policy_mapping);
@@ -560,7 +519,7 @@ NTSTATUS _lsa_QueryInfoPolicy(pipes_struct *p,
        }
 
        switch (r->in.level) {
-       case 0x02:
+       case LSA_POLICY_INFO_AUDIT_EVENTS:
                {
 
                uint32 policy_def = LSA_AUDIT_POLICY_ALL;
@@ -592,7 +551,7 @@ NTSTATUS _lsa_QueryInfoPolicy(pipes_struct *p,
 
                break;
                }
-       case 0x03:
+       case LSA_POLICY_INFO_DOMAIN:
                /* check if the user has enough rights */
                if (!(handle->access & LSA_POLICY_VIEW_LOCAL_INFORMATION))
                        return NT_STATUS_ACCESS_DENIED;
@@ -628,7 +587,7 @@ NTSTATUS _lsa_QueryInfoPolicy(pipes_struct *p,
                }
                init_dom_query_3(&info->domain, name, sid);
                break;
-       case 0x05:
+       case LSA_POLICY_INFO_ACCOUNT_DOMAIN:
                /* check if the user has enough rights */
                if (!(handle->access & LSA_POLICY_VIEW_LOCAL_INFORMATION))
                        return NT_STATUS_ACCESS_DENIED;
@@ -639,7 +598,7 @@ NTSTATUS _lsa_QueryInfoPolicy(pipes_struct *p,
 
                init_dom_query_5(&info->account_domain, name, sid);
                break;
-       case 0x06:
+       case LSA_POLICY_INFO_ROLE:
                /* check if the user has enough rights */
                if (!(handle->access & LSA_POLICY_VIEW_LOCAL_INFORMATION))
                        return NT_STATUS_ACCESS_DENIED;
@@ -661,6 +620,32 @@ NTSTATUS _lsa_QueryInfoPolicy(pipes_struct *p,
                                break;
                }
                break;
+       case LSA_POLICY_INFO_DNS: {
+               struct pdb_domain_info *dominfo;
+
+               if ((pdb_capabilities() & PDB_CAP_ADS) == 0) {
+                       DEBUG(10, ("Not replying to LSA_POLICY_INFO_DNS "
+                                  "without ADS passdb backend\n"));
+                       status = NT_STATUS_INVALID_INFO_CLASS;
+                       break;
+               }
+
+               dominfo = pdb_get_domain_info(info);
+               if (dominfo == NULL) {
+                       status = NT_STATUS_NO_MEMORY;
+                       break;
+               }
+
+               init_lsa_StringLarge(&info->dns.name,
+                                    dominfo->name);
+               init_lsa_StringLarge(&info->dns.dns_domain,
+                                    dominfo->dns_domain);
+               init_lsa_StringLarge(&info->dns.dns_forest,
+                                    dominfo->dns_forest);
+               info->dns.domain_guid = dominfo->guid;
+               info->dns.sid = &dominfo->sid;
+               break;
+       }
        default:
                DEBUG(0,("_lsa_QueryInfoPolicy: unknown info level in Lsa Query: %d\n",
                        r->in.level));
@@ -1645,7 +1630,9 @@ NTSTATUS _lsa_OpenAccount(pipes_struct *p,
         * handle - so don't check against policy handle. */
 
        /* Work out max allowed. */
-       map_max_allowed_access(p->server_info->ptok, &des_access);
+       map_max_allowed_access(p->server_info->ptok,
+                              &p->server_info->utok,
+                              &des_access);
 
        /* map the generic bits to the lsa account ones */
        se_map_generic(&des_access, &lsa_account_mapping);
@@ -1715,23 +1702,22 @@ NTSTATUS _lsa_EnumPrivsAccount(pipes_struct *p,
        if (!(info->access & LSA_ACCOUNT_VIEW))
                return NT_STATUS_ACCESS_DENIED;
 
-       if ( !get_privileges_for_sids( &mask, &info->sid, 1 ) )
-               return NT_STATUS_OBJECT_NAME_NOT_FOUND;
+       get_privileges_for_sids(&mask, &info->sid, 1);
 
        privilege_set_init( &privileges );
 
+       priv_set = TALLOC_ZERO_P(p->mem_ctx, struct lsa_PrivilegeSet);
+       if (!priv_set) {
+               status = NT_STATUS_NO_MEMORY;
+               goto done;
+       }
+
        if ( se_priv_to_privilege_set( &privileges, &mask ) ) {
 
                DEBUG(10,("_lsa_EnumPrivsAccount: %s has %d privileges\n",
                          sid_string_dbg(&info->sid),
                          privileges.count));
 
-               priv_set = TALLOC_ZERO_P(p->mem_ctx, struct lsa_PrivilegeSet);
-               if (!priv_set) {
-                       status = NT_STATUS_NO_MEMORY;
-                       goto done;
-               }
-
                luid_attrs = TALLOC_ZERO_ARRAY(p->mem_ctx,
                                               struct lsa_LUIDAttribute,
                                               privileges.count);
@@ -1750,11 +1736,14 @@ NTSTATUS _lsa_EnumPrivsAccount(pipes_struct *p,
                priv_set->unknown = 0;
                priv_set->set = luid_attrs;
 
-               *r->out.privs = priv_set;
        } else {
-               status = NT_STATUS_NO_SUCH_PRIVILEGE;
+               priv_set->count = 0;
+               priv_set->unknown = 0;
+               priv_set->set = NULL;
        }
 
+       *r->out.privs = priv_set;
+
  done:
        privilege_set_free( &privileges );
 
@@ -1976,74 +1965,6 @@ NTSTATUS _lsa_QuerySecurity(pipes_struct *p,
        return status;
 }
 
-#if 0  /* AD DC work in ongoing in Samba 4 */
-
-/***************************************************************************
- ***************************************************************************/
-
- NTSTATUS _lsa_query_info2(pipes_struct *p, LSA_Q_QUERY_INFO2 *q_u, LSA_R_QUERY_INFO2 *r_u)
-{
-       struct lsa_info *handle;
-       const char *nb_name;
-       char *dns_name = NULL;
-       char *forest_name = NULL;
-       DOM_SID *sid = NULL;
-       struct GUID guid;
-       fstring dnsdomname;
-
-       ZERO_STRUCT(guid);
-       r_u->status = NT_STATUS_OK;
-
-       if (!find_policy_by_hnd(p, &q_u->pol, (void **)(void *)&handle))
-               return NT_STATUS_INVALID_HANDLE;
-
-       switch (q_u->info_class) {
-       case 0x0c:
-               /* check if the user has enough rights */
-               if (!(handle->access & LSA_POLICY_VIEW_LOCAL_INFORMATION))
-                       return NT_STATUS_ACCESS_DENIED;
-
-               /* Request PolicyPrimaryDomainInformation. */
-               switch (lp_server_role()) {
-                       case ROLE_DOMAIN_PDC:
-                       case ROLE_DOMAIN_BDC:
-                               nb_name = get_global_sam_name();
-                               /* ugly temp hack for these next two */
-
-                               /* This should be a 'netbios domain -> DNS domain' mapping */
-                               dnsdomname = get_mydnsdomname(p->mem_ctx);
-                               if (!dnsdomname || !*dnsdomname) {
-                                       return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
-                               }
-                               strlower_m(dnsdomname);
-
-                               dns_name = dnsdomname;
-                               forest_name = dnsdomname;
-
-                               sid = get_global_sam_sid();
-                               secrets_fetch_domain_guid(lp_workgroup(), &guid);
-                               break;
-                       default:
-                               return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
-               }
-               init_dns_dom_info(&r_u->info.dns_dom_info, nb_name, dns_name,
-                                 forest_name,&guid,sid);
-               break;
-       default:
-               DEBUG(0,("_lsa_query_info2: unknown info level in Lsa Query: %d\n", q_u->info_class));
-               r_u->status = NT_STATUS_INVALID_INFO_CLASS;
-               break;
-       }
-
-       if (NT_STATUS_IS_OK(r_u->status)) {
-               r_u->ptr = 0x1;
-               r_u->info_class = q_u->info_class;
-       }
-
-       return r_u->status;
-}
-#endif /* AD DC work in ongoing in Samba 4 */
-
 /***************************************************************************
  _lsa_AddAccountRights
  ***************************************************************************/
@@ -2261,20 +2182,25 @@ NTSTATUS _lsa_EnumAccountRights(pipes_struct *p,
 
        sid_copy( &sid, r->in.sid );
 
-       if ( !get_privileges_for_sids( &mask, &sid, 1 ) )
+       /* according to MS-LSAD 3.1.4.5.10 it is required to return
+        * NT_STATUS_OBJECT_NAME_NOT_FOUND if the account sid was not found in
+        * the lsa database */
+
+       if (!get_privileges_for_sids(&mask, &sid, 1)) {
                return NT_STATUS_OBJECT_NAME_NOT_FOUND;
+       }
 
-       privilege_set_init( &privileges );
+       status = privilege_set_init(&privileges);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
 
-       if ( se_priv_to_privilege_set( &privileges, &mask ) ) {
+       se_priv_to_privilege_set(&privileges, &mask);
 
-               DEBUG(10,("_lsa_EnumAccountRights: %s has %d privileges\n",
-                         sid_string_dbg(&sid), privileges.count));
+       DEBUG(10,("_lsa_EnumAccountRights: %s has %d privileges\n",
+                 sid_string_dbg(&sid), privileges.count));
 
-               status = init_lsa_right_set(p->mem_ctx, r->out.rights, &privileges);
-       } else {
-               status = NT_STATUS_NO_SUCH_PRIVILEGE;
-       }
+       status = init_lsa_right_set(p->mem_ctx, r->out.rights, &privileges);
 
        privilege_set_free( &privileges );
 
@@ -2320,6 +2246,63 @@ NTSTATUS _lsa_LookupPrivValue(pipes_struct *p,
        return NT_STATUS_OK;
 }
 
+/***************************************************************************
+ _lsa_EnumAccountsWithUserRight
+ ***************************************************************************/
+
+NTSTATUS _lsa_EnumAccountsWithUserRight(pipes_struct *p,
+                                       struct lsa_EnumAccountsWithUserRight *r)
+{
+       NTSTATUS status;
+       struct lsa_info *info = NULL;
+       struct dom_sid *sids = NULL;
+       int num_sids = 0;
+       uint32_t i;
+       SE_PRIV mask;
+
+       if (!find_policy_by_hnd(p, r->in.handle, (void **)(void *)&info)) {
+               return NT_STATUS_INVALID_HANDLE;
+       }
+
+       if (info->type != LSA_HANDLE_POLICY_TYPE) {
+               return NT_STATUS_INVALID_HANDLE;
+       }
+
+       if (!(info->access & LSA_POLICY_LOOKUP_NAMES)) {
+               return NT_STATUS_ACCESS_DENIED;
+       }
+
+       if (!r->in.name || !r->in.name->string) {
+               return NT_STATUS_NO_SUCH_PRIVILEGE;
+       }
+
+       if (!se_priv_from_name(r->in.name->string, &mask)) {
+               return NT_STATUS_NO_SUCH_PRIVILEGE;
+       }
+
+       status = privilege_enum_sids(&mask, p->mem_ctx,
+                                    &sids, &num_sids);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+
+       r->out.sids->num_sids = num_sids;
+       r->out.sids->sids = talloc_array(p->mem_ctx, struct lsa_SidPtr,
+                                        r->out.sids->num_sids);
+
+       for (i=0; i < r->out.sids->num_sids; i++) {
+               r->out.sids->sids[i].sid = sid_dup_talloc(r->out.sids->sids,
+                                                         &sids[i]);
+               if (!r->out.sids->sids[i].sid) {
+                       TALLOC_FREE(r->out.sids->sids);
+                       r->out.sids->num_sids = 0;
+                       return NT_STATUS_NO_MEMORY;
+               }
+       }
+
+       return NT_STATUS_OK;
+}
+
 /*
  * From here on the server routines are just dummy ones to make smbd link with
  * librpc/gen_ndr/srv_lsa.c. These routines are actually never called, we are
@@ -2392,12 +2375,6 @@ NTSTATUS _lsa_LookupPrivName(pipes_struct *p, struct lsa_LookupPrivName *r)
        return NT_STATUS_NOT_IMPLEMENTED;
 }
 
-NTSTATUS _lsa_EnumAccountsWithUserRight(pipes_struct *p, struct lsa_EnumAccountsWithUserRight *r)
-{
-       p->rng_fault_state = True;
-       return NT_STATUS_NOT_IMPLEMENTED;
-}
-
 NTSTATUS _lsa_QueryTrustedDomainInfoBySid(pipes_struct *p, struct lsa_QueryTrustedDomainInfoBySid *r)
 {
        p->rng_fault_state = True;
@@ -2428,10 +2405,22 @@ NTSTATUS _lsa_RetrievePrivateData(pipes_struct *p, struct lsa_RetrievePrivateDat
        return NT_STATUS_NOT_IMPLEMENTED;
 }
 
-NTSTATUS _lsa_QueryInfoPolicy2(pipes_struct *p, struct lsa_QueryInfoPolicy2 *r)
+NTSTATUS _lsa_QueryInfoPolicy2(pipes_struct *p,
+                              struct lsa_QueryInfoPolicy2 *r2)
 {
-       p->rng_fault_state = True;
-       return NT_STATUS_NOT_IMPLEMENTED;
+       struct lsa_QueryInfoPolicy r;
+
+       if ((pdb_capabilities() & PDB_CAP_ADS) == 0) {
+               p->rng_fault_state = True;
+               return NT_STATUS_NOT_IMPLEMENTED;
+       }
+
+       ZERO_STRUCT(r);
+       r.in.handle = r2->in.handle;
+       r.in.level = r2->in.level;
+       r.out.info = r2->out.info;
+
+       return _lsa_QueryInfoPolicy(p, &r);
 }
 
 NTSTATUS _lsa_SetInfoPolicy2(pipes_struct *p, struct lsa_SetInfoPolicy2 *r)