s3: remove POLICY_HND.
[samba.git] / source3 / libads / ldap_printer.c
index a8c1148093ef55032d48c35fc5087097ad7a561b..9be366dc29d599334c443a6da3b6935984325b6e 100644 (file)
@@ -1,11 +1,11 @@
 /* 
    Unix SMB/CIFS implementation.
    ads (active directory) printer utility library
-   Copyright (C) Jim McDonough 2002
+   Copyright (C) Jim McDonough <jmcd@us.ibm.com> 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
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
@@ -14,8 +14,7 @@
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "includes.h"
     Note that results "res" may be allocated on return so that the
     results can be used.  It should be freed using ads_msgfree.
 */
-ADS_STATUS ads_find_printer_on_server(ADS_STRUCT *ads, void **res,
-                                     const char *printer, const char *servername)
+ ADS_STATUS ads_find_printer_on_server(ADS_STRUCT *ads, LDAPMessage **res,
+                                      const char *printer,
+                                      const char *servername)
 {
        ADS_STATUS status;
-       char *srv_dn, **srv_cn, *exp;
+       char *srv_dn, **srv_cn, *s = NULL;
        const char *attrs[] = {"*", "nTSecurityDescriptor", NULL};
 
        status = ads_find_machine_acct(ads, res, servername);
        if (!ADS_ERR_OK(status)) {
-               DEBUG(1, ("ads_add_printer: cannot find host %s in ads\n",
+               DEBUG(1, ("ads_find_printer_on_server: cannot find host %s in ads\n",
                          servername));
                return status;
        }
-       srv_dn = ldap_get_dn(ads->ld, *res);
+       if (ads_count_replies(ads, *res) != 1) {
+               if (res) {
+                       ads_msgfree(ads, *res);
+                       *res = NULL;
+               }
+               return ADS_ERROR(LDAP_NO_SUCH_OBJECT);
+       }
+       srv_dn = ldap_get_dn(ads->ldap.ld, *res);
+       if (srv_dn == NULL) {
+               if (res) {
+                       ads_msgfree(ads, *res);
+                       *res = NULL;
+               }
+               return ADS_ERROR(LDAP_NO_MEMORY);
+       }
        srv_cn = ldap_explode_dn(srv_dn, 1);
-       ads_msgfree(ads, *res);
+       if (srv_cn == NULL) {
+               ldap_memfree(srv_dn);
+               if (res) {
+                       ads_msgfree(ads, *res);
+                       *res = NULL;
+               }
+               return ADS_ERROR(LDAP_INVALID_DN_SYNTAX);
+       }
+       if (res) {
+               ads_msgfree(ads, *res);
+               *res = NULL;
+       }
 
-       asprintf(&exp, "(cn=%s-%s)", srv_cn[0], printer);
-       status = ads_search(ads, res, exp, attrs);
+       if (asprintf(&s, "(cn=%s-%s)", srv_cn[0], printer) == -1) {
+               ldap_memfree(srv_dn);
+               return ADS_ERROR(LDAP_NO_MEMORY);
+       }
+       status = ads_search(ads, res, s, attrs);
 
        ldap_memfree(srv_dn);
        ldap_value_free(srv_cn);
-       free(exp);
+       SAFE_FREE(s);
        return status;  
 }
 
+ ADS_STATUS ads_find_printers(ADS_STRUCT *ads, LDAPMessage **res)
+{
+       const char *ldap_expr;
+       const char *attrs[] = { "objectClass", "printerName", "location", "driverName",
+                               "serverName", "description", NULL };
+
+       /* For the moment only display all printers */
+
+       ldap_expr = "(&(!(showInAdvancedViewOnly=TRUE))(uncName=*)"
+               "(objectCategory=printQueue))";
+
+       return ads_search(ads, res, ldap_expr, attrs);
+}
+
 /*
   modify a printer entry in the directory
 */
@@ -75,29 +117,34 @@ ADS_STATUS ads_add_printer_entry(ADS_STRUCT *ads, char *prt_dn,
 /*
   map a REG_SZ to an ldap mod
 */
-static BOOL map_sz(TALLOC_CTX *ctx, ADS_MODLIST *mods, 
+static bool map_sz(TALLOC_CTX *ctx, ADS_MODLIST *mods, 
                            const REGISTRY_VALUE *value)
 {
        char *str_value = NULL;
+       size_t converted_size;
        ADS_STATUS status;
 
        if (value->type != REG_SZ)
-               return False;
+               return false;
 
        if (value->size && *((smb_ucs2_t *) value->data_p)) {
-               pull_ucs2_talloc(ctx, (void **) &str_value, 
-                                (const smb_ucs2_t *) value->data_p);
+               if (!pull_ucs2_talloc(ctx, &str_value,
+                                     (const smb_ucs2_t *) value->data_p,
+                                     &converted_size))
+               {
+                       return false;
+               }
                status = ads_mod_str(ctx, mods, value->valuename, str_value);
                return ADS_ERR_OK(status);
        }
-       return True;
+       return true;
                
 }
 
 /*
   map a REG_DWORD to an ldap mod
 */
-static BOOL map_dword(TALLOC_CTX *ctx, ADS_MODLIST *mods, 
+static bool map_dword(TALLOC_CTX *ctx, ADS_MODLIST *mods, 
                      const REGISTRY_VALUE *value)
 {
        char *str_value = NULL;
@@ -106,6 +153,9 @@ static BOOL map_dword(TALLOC_CTX *ctx, ADS_MODLIST *mods,
        if (value->type != REG_DWORD)
                return False;
        str_value = talloc_asprintf(ctx, "%d", *((uint32 *) value->data_p));
+       if (!str_value) {
+               return False;
+       }
        status = ads_mod_str(ctx, mods, value->valuename, str_value);
        return ADS_ERR_OK(status);
 }
@@ -113,7 +163,7 @@ static BOOL map_dword(TALLOC_CTX *ctx, ADS_MODLIST *mods,
 /*
   map a boolean REG_BINARY to an ldap mod
 */
-static BOOL map_bool(TALLOC_CTX *ctx, ADS_MODLIST *mods,
+static bool map_bool(TALLOC_CTX *ctx, ADS_MODLIST *mods,
                     const REGISTRY_VALUE *value)
 {
        char *str_value;
@@ -123,6 +173,9 @@ static BOOL map_bool(TALLOC_CTX *ctx, ADS_MODLIST *mods,
                return False;
        str_value =  talloc_asprintf(ctx, "%s", 
                                     *(value->data_p) ? "TRUE" : "FALSE");
+       if (!str_value) {
+               return False;
+       }
        status = ads_mod_str(ctx, mods, value->valuename, str_value);
        return ADS_ERR_OK(status);
 }
@@ -130,10 +183,11 @@ static BOOL map_bool(TALLOC_CTX *ctx, ADS_MODLIST *mods,
 /*
   map a REG_MULTI_SZ to an ldap mod
 */
-static BOOL map_multi_sz(TALLOC_CTX *ctx, ADS_MODLIST *mods,
+static bool map_multi_sz(TALLOC_CTX *ctx, ADS_MODLIST *mods,
                         const REGISTRY_VALUE *value)
 {
        char **str_values = NULL;
+       size_t converted_size;
        smb_ucs2_t *cur_str = (smb_ucs2_t *) value->data_p;
         uint32 size = 0, num_vals = 0, i=0;
        ADS_STATUS status;
@@ -148,16 +202,19 @@ static BOOL map_multi_sz(TALLOC_CTX *ctx, ADS_MODLIST *mods,
        };
 
        if (num_vals) {
-               str_values = talloc(ctx, 
-                                   (num_vals + 1) * sizeof(smb_ucs2_t *));
+               str_values = TALLOC_ARRAY(ctx, char *, num_vals + 1);
+               if (!str_values) {
+                       return False;
+               }
                memset(str_values, '\0', 
-                      (num_vals + 1) * sizeof(smb_ucs2_t *));
+                      (num_vals + 1) * sizeof(char *));
 
                cur_str = (smb_ucs2_t *) value->data_p;
-               for (i=0; i < num_vals; i++)
-                       cur_str += pull_ucs2_talloc(ctx, 
-                                                   (void **) &str_values[i], 
-                                                   cur_str);
+               for (i=0; i < num_vals; i++) {
+                       cur_str += pull_ucs2_talloc(ctx, &str_values[i],
+                                                   cur_str, &converted_size) ?
+                           converted_size : (size_t)-1;
+               }
 
                status = ads_mod_strlist(ctx, mods, value->valuename, 
                                         (const char **) str_values);
@@ -167,8 +224,8 @@ static BOOL map_multi_sz(TALLOC_CTX *ctx, ADS_MODLIST *mods,
 }
 
 struct valmap_to_ads {
-       char *valname;
-       BOOL (*fn)(TALLOC_CTX *, ADS_MODLIST *, const REGISTRY_VALUE *);
+       const char *valname;
+       bool (*fn)(TALLOC_CTX *, ADS_MODLIST *, const REGISTRY_VALUE *);
 };
 
 /*
@@ -177,10 +234,11 @@ struct valmap_to_ads {
 static void map_regval_to_ads(TALLOC_CTX *ctx, ADS_MODLIST *mods, 
                              REGISTRY_VALUE *value)
 {
-       struct valmap_to_ads map[] = {
+       const struct valmap_to_ads map[] = {
                {SPOOL_REG_ASSETNUMBER, map_sz},
                {SPOOL_REG_BYTESPERMINUTE, map_dword},
                {SPOOL_REG_DEFAULTPRIORITY, map_dword},
+               {SPOOL_REG_DESCRIPTION, map_sz},
                {SPOOL_REG_DRIVERNAME, map_sz},
                {SPOOL_REG_DRIVERVERSION, map_dword},
                {SPOOL_REG_FLAGS, map_dword},
@@ -246,97 +304,110 @@ static void map_regval_to_ads(TALLOC_CTX *ctx, ADS_MODLIST *mods,
 }
 
 
-WERROR get_remote_printer_publishing_data(struct cli_state *cli, 
+WERROR get_remote_printer_publishing_data(struct rpc_pipe_client *cli, 
                                          TALLOC_CTX *mem_ctx,
                                          ADS_MODLIST *mods,
-                                         char *printer)
+                                         const char *printer)
 {
        WERROR result;
-       char *printername, *servername;
-       REGVAL_CTR dsdriver_ctr, dsspooler_ctr;
-       BOOL got_dsdriver = False, got_dsspooler = False;
-       uint32 needed, i;
-       POLICY_HND pol;
-
-       asprintf(&servername, "\\\\%s", cli->desthost);
-       asprintf(&printername, "%s\\%s", servername, printer);
-       if (!servername || !printername) {
+       char *printername;
+       struct spoolss_PrinterEnumValues *info;
+       uint32_t count;
+       uint32 i;
+       struct policy_handle pol;
+
+       if ((asprintf(&printername, "%s\\%s", cli->srv_name_slash, printer) == -1)) {
                DEBUG(3, ("Insufficient memory\n"));
                return WERR_NOMEM;
        }
-       
-       result = cli_spoolss_open_printer_ex(cli, mem_ctx, printername, 
-                                            "", MAXIMUM_ALLOWED_ACCESS, 
-                                            servername, cli->user_name, &pol);
+
+       result = rpccli_spoolss_openprinter_ex(cli, mem_ctx,
+                                              printername,
+                                              SEC_FLAG_MAXIMUM_ALLOWED,
+                                              &pol);
        if (!W_ERROR_IS_OK(result)) {
                DEBUG(3, ("Unable to open printer %s, error is %s.\n",
-                         printername, dos_errstr(result)));
+                         printername, win_errstr(result)));
+               SAFE_FREE(printername);
                return result;
        }
-       
-       result = cli_spoolss_enumprinterdataex(cli, mem_ctx, 0, &needed, 
-                                              &pol, SPOOL_DSDRIVER_KEY, NULL);
 
-       if (W_ERROR_V(result) == ERRmoredata)
-               result = cli_spoolss_enumprinterdataex(cli, mem_ctx, needed, 
-                                                      NULL, &pol, 
-                                                      SPOOL_DSDRIVER_KEY,
-                                                      &dsdriver_ctr);
+       result = rpccli_spoolss_enumprinterdataex(cli, mem_ctx, &pol,
+                                                 SPOOL_DSDRIVER_KEY,
+                                                 0,
+                                                 &count,
+                                                 &info);
 
        if (!W_ERROR_IS_OK(result)) {
                DEBUG(3, ("Unable to do enumdataex on %s, error is %s.\n",
-                         printername, dos_errstr(result)));
+                         printername, win_errstr(result)));
        } else {
-
                /* Have the data we need now, so start building */
-               got_dsdriver = True;
-               for (i=0; i < dsdriver_ctr.num_values; i++)
-                       map_regval_to_ads(mem_ctx, mods, 
-                                         dsdriver_ctr.values[i]);
+               for (i=0; i < count; i++) {
+                       REGISTRY_VALUE v;
+                       DATA_BLOB blob;
+
+                       result = push_spoolss_PrinterData(mem_ctx, &blob,
+                                                         info[i].type,
+                                                         info[i].data);
+                       if (W_ERROR_IS_OK(result)) {
+                               fstrcpy(v.valuename, info[i].value_name);
+                               v.type = info[i].type;
+                               v.data_p = blob.data;
+                               v.size = blob.length;
+
+                               map_regval_to_ads(mem_ctx, mods, &v);
+                       }
+               }
        }
-       
-       result = cli_spoolss_enumprinterdataex(cli, mem_ctx, 0, &needed, 
-                                              &pol, SPOOL_DSSPOOLER_KEY, 
-                                              NULL);
-
-       if (W_ERROR_V(result) == ERRmoredata)
-               result = cli_spoolss_enumprinterdataex(cli, mem_ctx, needed, 
-                                                      NULL, &pol, 
-                                                      SPOOL_DSSPOOLER_KEY,
-                                                      &dsspooler_ctr);
 
+       result = rpccli_spoolss_enumprinterdataex(cli, mem_ctx, &pol,
+                                                 SPOOL_DSSPOOLER_KEY,
+                                                 0,
+                                                 &count,
+                                                 &info);
        if (!W_ERROR_IS_OK(result)) {
                DEBUG(3, ("Unable to do enumdataex on %s, error is %s.\n",
-                         printername, dos_errstr(result)));
+                         printername, win_errstr(result)));
        } else {
-               got_dsspooler = True;
-               for (i=0; i < dsspooler_ctr.num_values; i++)
-                       map_regval_to_ads(mem_ctx, mods, 
-                                         dsspooler_ctr.values[i]);
+               for (i=0; i < count; i++) {
+                       REGISTRY_VALUE v;
+                       DATA_BLOB blob;
+
+                       result = push_spoolss_PrinterData(mem_ctx, &blob,
+                                                         info[i].type,
+                                                         info[i].data);
+                       if (W_ERROR_IS_OK(result)) {
+                               fstrcpy(v.valuename, info[i].value_name);
+                               v.type = info[i].type;
+                               v.data_p = blob.data;
+                               v.size = blob.length;
+
+                               map_regval_to_ads(mem_ctx, mods, &v);
+                       }
+               }
        }
-       
+
        ads_mod_str(mem_ctx, mods, SPOOL_REG_PRINTERNAME, printer);
 
-       if (got_dsdriver) regval_ctr_destroy(&dsdriver_ctr);
-       if (got_dsspooler) regval_ctr_destroy(&dsspooler_ctr);
-       cli_spoolss_close_printer(cli, mem_ctx, &pol);
+       rpccli_spoolss_ClosePrinter(cli, mem_ctx, &pol, NULL);
+       SAFE_FREE(printername);
 
        return result;
 }
 
-BOOL get_local_printer_publishing_data(TALLOC_CTX *mem_ctx,
+bool get_local_printer_publishing_data(TALLOC_CTX *mem_ctx,
                                       ADS_MODLIST *mods,
                                       NT_PRINTER_DATA *data)
 {
        uint32 key,val;
 
        for (key=0; key < data->num_keys; key++) {
-               REGVAL_CTR ctr = data->keys[key].values;
-               for (val=0; val < ctr.num_values; val++)
-                       map_regval_to_ads(mem_ctx, mods, ctr.values[val]);
+               REGVAL_CTR *ctr = data->keys[key].values;
+               for (val=0; val < ctr->num_values; val++)
+                       map_regval_to_ads(mem_ctx, mods, ctr->values[val]);
        }
        return True;
 }
 
 #endif
-