Given Jeremy's positive response, and a lack of one from tpot, I'll commit
[kai/samba.git] / source3 / libsmb / cli_lsarpc.c
index 249225d8943a42b85d185d6a4df68056975cc7e6..9d07eb1d1ea958e64f898b0e8718e89dbbb2dc83 100644 (file)
@@ -5,7 +5,8 @@
    Copyright (C) Andrew Tridgell              1992-1997,2000,
    Copyright (C) Luke Kenneth Casson Leighton 1996-1997,2000,
    Copyright (C) Paul Ashton                       1997,2000,
-   Copyright (C) Elrond                                 2000.
+   Copyright (C) Elrond                                 2000,
+   Copyright (C) Rafal Szczesniak                       2002
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
  * security authority", which is half of a password database.
  **/
 
-/** Opens a SMB connection and connects to the LSARPC pipe.
- *
- * @param cli Uninitialised client handle.
- * @param system_name NETBIOS name of the machine to connect to.
- * @param creds User credentials to connect as.
- * @returns Initialised client handle.
- */
-struct cli_state *cli_lsa_initialise(struct cli_state *cli, char *system_name,
-                                    struct ntuser_creds *creds)
-{
-        return cli_pipe_initialise(cli, system_name, PIPE_LSARPC, creds);
-}
-
 /** Open a LSA policy handle
  *
  * @param cli Handle on an initialised SMB connection */
@@ -99,6 +87,9 @@ NTSTATUS cli_lsa_open_policy(struct cli_state *cli, TALLOC_CTX *mem_ctx,
 
        if (NT_STATUS_IS_OK(result = r.status)) {
                *pol = r.pol;
+#ifdef __INSURE__
+               pol->marker = malloc(1);
+#endif
        }
 
  done:
@@ -160,6 +151,9 @@ NTSTATUS cli_lsa_open_policy2(struct cli_state *cli, TALLOC_CTX *mem_ctx,
 
        if (NT_STATUS_IS_OK(result = r.status)) {
                *pol = r.pol;
+#ifdef __INSURE__
+               pol->marker = (char *)malloc(1);
+#endif
        }
 
  done:
@@ -207,6 +201,9 @@ NTSTATUS cli_lsa_close(struct cli_state *cli, TALLOC_CTX *mem_ctx,
        /* Return output parameters */
 
        if (NT_STATUS_IS_OK(result = r.status)) {
+#ifdef __INSURE__
+               SAFE_FREE(pol->marker);
+#endif
                *pol = r.pol;
        }
 
@@ -221,7 +218,7 @@ NTSTATUS cli_lsa_close(struct cli_state *cli, TALLOC_CTX *mem_ctx,
 
 NTSTATUS cli_lsa_lookup_sids(struct cli_state *cli, TALLOC_CTX *mem_ctx,
                              POLICY_HND *pol, int num_sids, DOM_SID *sids, 
-                             char ***domains, char ***names, uint32 **types, int *num_names)
+                             char ***domains, char ***names, uint32 **types)
 {
        prs_struct qbuf, rbuf;
        LSA_Q_LOOKUP_SIDS q;
@@ -265,13 +262,13 @@ NTSTATUS cli_lsa_lookup_sids(struct cli_state *cli, TALLOC_CTX *mem_ctx,
        result = r.status;
 
        if (!NT_STATUS_IS_OK(result) &&
-               NT_STATUS_V(result) != NT_STATUS_V(NT_STATUS_FILES_OPEN)) {
+           NT_STATUS_V(result) != NT_STATUS_V(STATUS_SOME_UNMAPPED)) {
+         
                /* An actual error occured */
 
                goto done;
        }
 
-
        /* Return output parameters */
 
        if (r.mapped_count == 0) {
@@ -279,28 +276,28 @@ NTSTATUS cli_lsa_lookup_sids(struct cli_state *cli, TALLOC_CTX *mem_ctx,
                goto done;
        }
 
-       (*num_names) = r.mapped_count;
-       result = NT_STATUS_OK;
-
-       if (!((*domains) = (char **)talloc(mem_ctx, sizeof(char *) * r.mapped_count))) {
+       if (!((*domains) = (char **)talloc(mem_ctx, sizeof(char *) *
+                                          num_sids))) {
                DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
                result = NT_STATUS_UNSUCCESSFUL;
                goto done;
        }
 
-       if (!((*names) = (char **)talloc(mem_ctx, sizeof(char *) * r.mapped_count))) {
+       if (!((*names) = (char **)talloc(mem_ctx, sizeof(char *) *
+                                        num_sids))) {
                DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
                result = NT_STATUS_UNSUCCESSFUL;
                goto done;
        }
 
-       if (!((*types) = (uint32 *)talloc(mem_ctx, sizeof(uint32) * r.mapped_count))) {
+       if (!((*types) = (uint32 *)talloc(mem_ctx, sizeof(uint32) *
+                                         num_sids))) {
                DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
                result = NT_STATUS_UNSUCCESSFUL;
                goto done;
        }
                
-       for (i = 0; i < r.mapped_count; i++) {
+       for (i = 0; i < num_sids; i++) {
                fstring name, dom_name;
                uint32 dom_idx = t_names.name[i].domain_idx;
 
@@ -339,8 +336,9 @@ NTSTATUS cli_lsa_lookup_sids(struct cli_state *cli, TALLOC_CTX *mem_ctx,
 /** Lookup a list of names */
 
 NTSTATUS cli_lsa_lookup_names(struct cli_state *cli, TALLOC_CTX *mem_ctx,
-                              POLICY_HND *pol, int num_names, const char **names, 
-                              DOM_SID **sids, uint32 **types, int *num_sids)
+                              POLICY_HND *pol, int num_names, 
+                             const char **names, DOM_SID **sids, 
+                             uint32 **types)
 {
        prs_struct qbuf, rbuf;
        LSA_Q_LOOKUP_NAMES q;
@@ -379,13 +377,14 @@ NTSTATUS cli_lsa_lookup_names(struct cli_state *cli, TALLOC_CTX *mem_ctx,
 
        result = r.status;
 
-       if (!NT_STATUS_IS_OK(result)) {
+       if (!NT_STATUS_IS_OK(result) && NT_STATUS_V(result) !=
+           NT_STATUS_V(STATUS_SOME_UNMAPPED)) {
+
                /* An actual error occured */
 
                goto done;
        }
 
-
        /* Return output parameters */
 
        if (r.mapped_count == 0) {
@@ -393,22 +392,21 @@ NTSTATUS cli_lsa_lookup_names(struct cli_state *cli, TALLOC_CTX *mem_ctx,
                goto done;
        }
 
-       (*num_sids) = r.mapped_count;
-       result = NT_STATUS_OK;
-
-       if (!((*sids = (DOM_SID *)talloc(mem_ctx, sizeof(DOM_SID) * r.mapped_count)))) {
+       if (!((*sids = (DOM_SID *)talloc(mem_ctx, sizeof(DOM_SID) *
+                                        num_names)))) {
                DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
                result = NT_STATUS_UNSUCCESSFUL;
                goto done;
        }
 
-       if (!((*types = (uint32 *)talloc(mem_ctx, sizeof(uint32) * r.mapped_count)))) {
+       if (!((*types = (uint32 *)talloc(mem_ctx, sizeof(uint32) *
+                                        num_names)))) {
                DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
                result = NT_STATUS_UNSUCCESSFUL;
                goto done;
        }
 
-       for (i = 0; i < r.mapped_count; i++) {
+       for (i = 0; i < num_names; i++) {
                DOM_RID2 *t_rids = r.dom_rid;
                uint32 dom_idx = t_rids[i].rid_idx;
                uint32 dom_rid = t_rids[i].rid;
@@ -527,12 +525,25 @@ NTSTATUS cli_lsa_query_info_policy(struct cli_state *cli, TALLOC_CTX *mem_ctx,
        return result;
 }
 
-/** Enumerate list of trusted domains */
+/**
+ * Enumerate list of trusted domains
+ *
+ * @param cli client state (cli_state) structure of the connection
+ * @param mem_ctx memory context
+ * @param pol opened lsa policy handle
+ * @param enum_ctx enumeration context ie. index of first returned domain entry
+ * @param pref_num_domains preferred max number of entries returned in one response
+ * @param num_domains total number of trusted domains returned by response
+ * @param domain_names returned trusted domain names
+ * @param domain_sids returned trusted domain sids
+ *
+ * @return nt status code of response
+ **/
 
 NTSTATUS cli_lsa_enum_trust_dom(struct cli_state *cli, TALLOC_CTX *mem_ctx,
                                 POLICY_HND *pol, uint32 *enum_ctx, 
-                                uint32 *num_domains, char ***domain_names, 
-                                DOM_SID **domain_sids)
+                                uint32 *pref_num_domains, uint32 *num_domains,
+                               char ***domain_names, DOM_SID **domain_sids)
 {
        prs_struct qbuf, rbuf;
        LSA_Q_ENUM_TRUST_DOM q;
@@ -550,7 +561,7 @@ NTSTATUS cli_lsa_enum_trust_dom(struct cli_state *cli, TALLOC_CTX *mem_ctx,
 
        /* Marshall data and send request */
 
-        init_q_enum_trust_dom(&q, pol, *enum_ctx, 0xffffffff);
+        init_q_enum_trust_dom(&q, pol, *enum_ctx, *pref_num_domains);
 
        if (!lsa_io_q_enum_trust_dom("", &q, &qbuf, 0) ||
            !rpc_api_pipe_req(cli, LSA_ENUMTRUSTDOM, &qbuf, &rbuf)) {
@@ -567,16 +578,15 @@ NTSTATUS cli_lsa_enum_trust_dom(struct cli_state *cli, TALLOC_CTX *mem_ctx,
 
        result = r.status;
 
-       if (!NT_STATUS_IS_OK(result) && 
-           NT_STATUS_V(result) != NT_STATUS_V(NT_STATUS_NO_MORE_ENTRIES)) {
+       if (!NT_STATUS_IS_OK(result) &&
+           !NT_STATUS_EQUAL(result, NT_STATUS_NO_MORE_ENTRIES) &&
+           !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES)) {
 
                /* An actual error ocured */
 
                goto done;
        }
 
-       result = NT_STATUS_OK;
-
        /* Return output parameters */
 
        if (r.num_domains) {
@@ -607,7 +617,7 @@ NTSTATUS cli_lsa_enum_trust_dom(struct cli_state *cli, TALLOC_CTX *mem_ctx,
 
                        unistr2_to_ascii(tmp, &r.uni_domain_name[i], 
                                         sizeof(tmp) - 1);
-                       (*domain_names)[i] = strdup(tmp);
+                       (*domain_names)[i] = talloc_strdup(mem_ctx, tmp);
                        sid_copy(&(*domain_sids)[i], &r.domain_sid[i].sid);
                }
        }
@@ -1052,7 +1062,10 @@ NTSTATUS cli_lsa_query_secobj(struct cli_state *cli, TALLOC_CTX *mem_ctx,
        return result;
 }
 
-/** Fetch a DOMAIN sid. Does complete cli setup / teardown anonymously. */
+#if 0
+
+/** An example of how to use the routines in this file.  Fetch a DOMAIN
+    sid. Does complete cli setup / teardown anonymously. */
 
 BOOL fetch_domain_sid( char *domain, char *remote_machine, DOM_SID *psid)
 {
@@ -1131,14 +1144,14 @@ Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
        result = cli_lsa_open_policy(&cli, cli.mem_ctx, True, SEC_RIGHTS_QUERY_VALUE, &lsa_pol);
        if (!NT_STATUS_IS_OK(result)) {
                DEBUG(0, ("fetch_domain_sid: Error opening lsa policy handle. %s\n",
-                       get_nt_error_msg(result) ));
+                       nt_errstr(result) ));
                goto done;
        }
  
        result = cli_lsa_query_info_policy(&cli, cli.mem_ctx, &lsa_pol, 5, domain, psid);
        if (!NT_STATUS_IS_OK(result)) {
                DEBUG(0, ("fetch_domain_sid: Error querying lsa policy handle. %s\n",
-                       get_nt_error_msg(result) ));
+                       nt_errstr(result) ));
                goto done;
        }
  
@@ -1150,4 +1163,6 @@ Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
        return ret;
 }
 
+#endif
+
 /** @} **/