s3-build: avoid to globally include printing and spoolss headers.
[vlendec/samba-autobuild/.git] / source3 / libads / ldap_printer.c
index 0185bf78113cd92d62081e2dc9b4e42b18491750..c997218a480fb4847c856a1b84e3344343dc041c 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,
    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"
+#include "../librpc/gen_ndr/cli_spoolss.h"
+#include "rpc_client/cli_spoolss.h"
+#include "registry.h"
+#include "registry/reg_objects.h"
+#include "nt_printing.h"
 
 #ifdef HAVE_ADS
 
     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 an entire printer entry in the directory
+  modify a printer entry in the directory
 */
 ADS_STATUS ads_mod_printer_entry(ADS_STRUCT *ads, char *prt_dn,
-                                const ADS_PRINTER_ENTRY *prt)
+                                TALLOC_CTX *ctx, const ADS_MODLIST *mods)
+{
+       return ads_gen_mod(ads, prt_dn, *mods);
+}
+
+/*
+  add a printer to the directory
+*/
+ADS_STATUS ads_add_printer_entry(ADS_STRUCT *ads, char *prt_dn,
+                                       TALLOC_CTX *ctx, ADS_MODLIST *mods)
 {
-       ADS_MODLIST mods;
+       ads_mod_str(ctx, mods, "objectClass", "printQueue");
+       return ads_gen_add(ads, prt_dn, *mods);
+}
+
+/*
+  map a REG_SZ to an ldap mod
+*/
+static bool map_sz(TALLOC_CTX *ctx, ADS_MODLIST *mods, 
+                  struct regval_blob *value)
+{
+       char *str_value = NULL;
+       size_t converted_size;
        ADS_STATUS status;
-       TALLOC_CTX *ctx;
 
-       if (!(ctx = talloc_init_named("mod_printer_entry")))
-               return ADS_ERROR(LDAP_NO_MEMORY);
+       if (regval_type(value) != REG_SZ)
+               return false;
 
-       /* allocate the list */
-       mods = ads_init_mods(ctx);
-
-       /* add the attributes to the list - required ones first */
-       ads_mod_str(ctx, &mods, "printerName", prt->printerName);
-       ads_mod_str(ctx, &mods, "serverName", prt->serverName);
-       ads_mod_str(ctx, &mods, "shortServerName", prt->shortServerName);
-       ads_mod_str(ctx, &mods, "uNCName", prt->uNCName);
-       ads_mod_str(ctx, &mods, "versionNumber", prt->versionNumber);
-
-       /* now the optional ones */
-       ads_mod_strlist(ctx, &mods, "description", (const char **)prt->description);
-       ads_mod_str(ctx, &mods, "assetNumber",prt->assetNumber);
-       ads_mod_str(ctx, &mods, "bytesPerMinute",prt->bytesPerMinute);
-       ads_mod_str(ctx, &mods, "defaultPriority",prt->defaultPriority);
-       ads_mod_str(ctx, &mods, "driverName", prt->driverName);
-       ads_mod_str(ctx, &mods, "driverVersion",prt->driverVersion);
-       ads_mod_str(ctx, &mods, "location", prt->location);
-       ads_mod_str(ctx, &mods, "operatingSystem",prt->operatingSystem);
-       ads_mod_str(ctx, &mods, "operatingSystemHotfix",
-                    prt->operatingSystemHotfix);
-       ads_mod_str(ctx, &mods, "operatingSystemServicePack",
-                    prt->operatingSystemServicePack);
-       ads_mod_str(ctx, &mods, "operatingSystemVersion",
-                    prt->operatingSystemVersion);
-       ads_mod_str(ctx, &mods, "physicalLocationObject",
-                    prt->physicalLocationObject);
-       ads_mod_strlist(ctx, &mods, "portName", (const char **)prt->portName);
-       ads_mod_str(ctx, &mods, "printStartTime", prt->printStartTime);
-       ads_mod_str(ctx, &mods, "printEndTime", prt->printEndTime);
-       ads_mod_strlist(ctx, &mods, "printBinNames", (const char **)prt->printBinNames);
-       /*... and many others */
-
-       /* do the ldap modify */
-       status = ads_gen_mod(ads, prt_dn, mods);
-
-       /* free mod list, mods, and values */
-       talloc_destroy(ctx); 
-
-       return status;
+       if (regval_size(value) && *((smb_ucs2_t *) regval_data_p(value))) {
+               if (!pull_ucs2_talloc(ctx, &str_value,
+                                     (const smb_ucs2_t *) regval_data_p(value),
+                                     &converted_size))
+               {
+                       return false;
+               }
+               status = ads_mod_str(ctx, mods, regval_name(value), str_value);
+               return ADS_ERR_OK(status);
+       }
+       return true;
+               
 }
 
 /*
-  add a printer to the directory
+  map a REG_DWORD to an ldap mod
 */
-static ADS_STATUS ads_add_printer_entry(ADS_STRUCT *ads, char *prt_dn,
-                                       const ADS_PRINTER_ENTRY *prt)
+static bool map_dword(TALLOC_CTX *ctx, ADS_MODLIST *mods, 
+                     struct regval_blob *value)
 {
-        ADS_STATUS status;
-       TALLOC_CTX *ctx;
-       ADS_MODLIST mods;
+       char *str_value = NULL;
+       ADS_STATUS status;
 
-       if (!(ctx = talloc_init_named("add_printer_entry")))
-               return ADS_ERROR(LDAP_NO_MEMORY);
+       if (regval_type(value) != REG_DWORD)
+               return False;
+       str_value = talloc_asprintf(ctx, "%d", *((uint32 *) regval_data_p(value)));
+       if (!str_value) {
+               return False;
+       }
+       status = ads_mod_str(ctx, mods, regval_name(value), str_value);
+       return ADS_ERR_OK(status);
+}
 
-       if (!(mods = ads_init_mods(ctx)))
-               return ADS_ERROR(LDAP_NO_MEMORY);
+/*
+  map a boolean REG_BINARY to an ldap mod
+*/
+static bool map_bool(TALLOC_CTX *ctx, ADS_MODLIST *mods,
+                    struct regval_blob *value)
+{
+       char *str_value;
+       ADS_STATUS status;
 
-       /* These are the fields a printQueue must contain */
-       ads_mod_str(ctx, &mods, "uNCName", prt->uNCName);
-       ads_mod_str(ctx, &mods, "versionNumber", prt->versionNumber);
-       ads_mod_str(ctx, &mods, "serverName", prt->serverName);
-       ads_mod_str(ctx, &mods, "shortServerName", prt->shortServerName);
-       ads_mod_str(ctx, &mods, "printerName", prt->printerName);
-       ads_mod_str(ctx, &mods, "objectClass", "printQueue");
+       if ((regval_type(value) != REG_BINARY) || (regval_size(value) != 1))
+               return False;
+       str_value =  talloc_asprintf(ctx, "%s", 
+                                    *(regval_data_p(value)) ? "TRUE" : "FALSE");
+       if (!str_value) {
+               return False;
+       }
+       status = ads_mod_str(ctx, mods, regval_name(value), str_value);
+       return ADS_ERR_OK(status);
+}
 
+/*
+  map a REG_MULTI_SZ to an ldap mod
+*/
+static bool map_multi_sz(TALLOC_CTX *ctx, ADS_MODLIST *mods,
+                        struct regval_blob *value)
+{
+       char **str_values = NULL;
+       size_t converted_size;
+       smb_ucs2_t *cur_str = (smb_ucs2_t *) regval_data_p(value);
+        uint32 size = 0, num_vals = 0, i=0;
+       ADS_STATUS status;
+
+       if (regval_type(value) != REG_MULTI_SZ)
+               return False;
 
-       status = ads_gen_add(ads, prt_dn, mods);
+       while(cur_str && *cur_str && (size < regval_size(value))) {
+               size += 2 * (strlen_w(cur_str) + 1);
+               cur_str += strlen_w(cur_str) + 1;
+               num_vals++;
+       };
 
-       talloc_destroy(ctx);
+       if (num_vals) {
+               str_values = TALLOC_ARRAY(ctx, char *, num_vals + 1);
+               if (!str_values) {
+                       return False;
+               }
+               memset(str_values, '\0', 
+                      (num_vals + 1) * sizeof(char *));
+
+               cur_str = (smb_ucs2_t *) regval_data_p(value);
+               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;
+               }
 
-        return status;
+               status = ads_mod_strlist(ctx, mods, regval_name(value),
+                                        (const char **) str_values);
+               return ADS_ERR_OK(status);
+       } 
+       return True;
 }
 
+struct valmap_to_ads {
+       const char *valname;
+       bool (*fn)(TALLOC_CTX *, ADS_MODLIST *, struct regval_blob *);
+};
+
 /*
-  publish a printer in the ADS
+  map a REG_SZ to an ldap mod
 */
+static void map_regval_to_ads(TALLOC_CTX *ctx, ADS_MODLIST *mods, 
+                             struct regval_blob *value)
+{
+       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},
+               {SPOOL_REG_LOCATION, map_sz},
+               {SPOOL_REG_OPERATINGSYSTEM, map_sz},
+               {SPOOL_REG_OPERATINGSYSTEMHOTFIX, map_sz},
+               {SPOOL_REG_OPERATINGSYSTEMSERVICEPACK, map_sz},
+               {SPOOL_REG_OPERATINGSYSTEMVERSION, map_sz},
+               {SPOOL_REG_PORTNAME, map_multi_sz},
+               {SPOOL_REG_PRINTATTRIBUTES, map_dword},
+               {SPOOL_REG_PRINTBINNAMES, map_multi_sz},
+               {SPOOL_REG_PRINTCOLLATE, map_bool},
+               {SPOOL_REG_PRINTCOLOR, map_bool},
+               {SPOOL_REG_PRINTDUPLEXSUPPORTED, map_bool},
+               {SPOOL_REG_PRINTENDTIME, map_dword},
+               {SPOOL_REG_PRINTFORMNAME, map_sz},
+               {SPOOL_REG_PRINTKEEPPRINTEDJOBS, map_bool},
+               {SPOOL_REG_PRINTLANGUAGE, map_multi_sz},
+               {SPOOL_REG_PRINTMACADDRESS, map_sz},
+               {SPOOL_REG_PRINTMAXCOPIES, map_sz},
+               {SPOOL_REG_PRINTMAXRESOLUTIONSUPPORTED, map_dword},
+               {SPOOL_REG_PRINTMAXXEXTENT, map_dword},
+               {SPOOL_REG_PRINTMAXYEXTENT, map_dword},
+               {SPOOL_REG_PRINTMEDIAREADY, map_multi_sz},
+               {SPOOL_REG_PRINTMEDIASUPPORTED, map_multi_sz},
+               {SPOOL_REG_PRINTMEMORY, map_dword},
+               {SPOOL_REG_PRINTMINXEXTENT, map_dword},
+               {SPOOL_REG_PRINTMINYEXTENT, map_dword},
+               {SPOOL_REG_PRINTNETWORKADDRESS, map_sz},
+               {SPOOL_REG_PRINTNOTIFY, map_sz},
+               {SPOOL_REG_PRINTNUMBERUP, map_dword},
+               {SPOOL_REG_PRINTORIENTATIONSSUPPORTED, map_multi_sz},
+               {SPOOL_REG_PRINTOWNER, map_sz},
+               {SPOOL_REG_PRINTPAGESPERMINUTE, map_dword},
+               {SPOOL_REG_PRINTRATE, map_dword},
+               {SPOOL_REG_PRINTRATEUNIT, map_sz},
+               {SPOOL_REG_PRINTSEPARATORFILE, map_sz},
+               {SPOOL_REG_PRINTSHARENAME, map_sz},
+               {SPOOL_REG_PRINTSPOOLING, map_sz},
+               {SPOOL_REG_PRINTSTAPLINGSUPPORTED, map_bool},
+               {SPOOL_REG_PRINTSTARTTIME, map_dword},
+               {SPOOL_REG_PRINTSTATUS, map_sz},
+               {SPOOL_REG_PRIORITY, map_dword},
+               {SPOOL_REG_SERVERNAME, map_sz},
+               {SPOOL_REG_SHORTSERVERNAME, map_sz},
+               {SPOOL_REG_UNCNAME, map_sz},
+               {SPOOL_REG_URL, map_sz},
+               {SPOOL_REG_VERSIONNUMBER, map_dword},
+               {NULL, NULL}
+       };
+       int i;
+
+       for (i=0; map[i].valname; i++) {
+               if (StrCaseCmp(map[i].valname, regval_name(value)) == 0) {
+                       if (!map[i].fn(ctx, mods, value)) {
+                               DEBUG(5, ("Add of value %s to modlist failed\n", regval_name(value)));
+                       } else {
+                               DEBUG(7, ("Mapped value %s\n", regval_name(value)));
+                       }
+                       
+               }
+       }
+}
 
-ADS_STATUS ads_add_printer(ADS_STRUCT *ads, const ADS_PRINTER_ENTRY *prt)
+
+WERROR get_remote_printer_publishing_data(struct rpc_pipe_client *cli, 
+                                         TALLOC_CTX *mem_ctx,
+                                         ADS_MODLIST *mods,
+                                         const char *printer)
 {
-       ADS_STATUS status;
-       void *res;
-       char *host_dn, *prt_dn;
-       const char *attrs[] = {"*", "nTSecurityDescriptor", NULL};
+       WERROR result;
+       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;
+       }
 
-       status = ads_find_machine_acct(ads, (void **)&res,
-                                      prt->shortServerName);
-       if (!ADS_ERR_OK(status)) {
-               DEBUG(1, ("ads_add_printer: cannot find host %s in ads\n",
-                         prt->shortServerName));
-               return status;
+       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, win_errstr(result)));
+               SAFE_FREE(printername);
+               return result;
        }
-       host_dn = ads_get_dn(ads, res);
-       ads_msgfree(ads, res);
 
-       ads_find_printer_on_server(ads, &res, prt->printerName, 
-                                  prt->shortServerName);
+       result = rpccli_spoolss_enumprinterdataex(cli, mem_ctx, &pol,
+                                                 SPOOL_DSDRIVER_KEY,
+                                                 0,
+                                                 &count,
+                                                 &info);
 
-       if (ADS_ERR_OK(status) && ads_count_replies(ads, res)) {
-               DEBUG(1, ("ads_add_printer: printer %s already exists\n",
-                         prt->printerName));
-               /* nothing to do, just free results */
-               ads_msgfree(ads, res);
+       if (!W_ERROR_IS_OK(result)) {
+               DEBUG(3, ("Unable to do enumdataex on %s, error is %s.\n",
+                         printername, win_errstr(result)));
        } else {
-               ads_msgfree(ads, res);
-               status = ads_add_printer_entry(ads, prt_dn, prt);
-               if (!ADS_ERR_OK(status)) {
-                       DEBUG(0, ("ads_add_printer: ads_add_printer_entry failed\n"));
-                       return status;
+               /* Have the data we need now, so start building */
+               for (i=0; i < count; i++) {
+                       struct regval_blob *v;
+
+                       v = regval_compose(mem_ctx, info[i].value_name,
+                                          info[i].type,
+                                          info[i].data->data,
+                                          info[i].data->length);
+                       if (v == NULL) {
+                               return WERR_NOMEM;
+                       }
+
+                       map_regval_to_ads(mem_ctx, mods, v);
+                       talloc_free(v);
                }
        }
 
-       status = ads_search_dn(ads, &res, prt_dn, attrs);
-
-       if (ADS_ERR_OK(status) && ads_count_replies(ads, res)) {
-               /* need to retrieve GUID from results
-                  prt->GUID */
-               status = ads_mod_printer_entry(ads, prt_dn, prt);
+       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, win_errstr(result)));
+       } else {
+               for (i=0; i < count; i++) {
+                       struct regval_blob *v;
+
+                       v = regval_compose(mem_ctx, info[i].value_name,
+                                          info[i].type,
+                                          info[i].data->data,
+                                          info[i].data->length);
+                       if (v == NULL) {
+                               return WERR_NOMEM;
+                       }
+
+                       map_regval_to_ads(mem_ctx, mods, v);
+                       talloc_free(v);
+               }
        }
 
-       ads_msgfree(ads, res);
+       ads_mod_str(mem_ctx, mods, SPOOL_REG_PRINTERNAME, printer);
+
+       rpccli_spoolss_ClosePrinter(cli, mem_ctx, &pol, NULL);
+       SAFE_FREE(printername);
 
+       return result;
+}
+
+static bool get_local_printer_publishing_data(TALLOC_CTX *mem_ctx,
+                                      ADS_MODLIST *mods,
+                                      NT_PRINTER_DATA *data)
+{
+       uint32 key,val;
 
-       return status;
+       for (key=0; key < data->num_keys; key++) {
+               struct regval_ctr *ctr = data->keys[key].values;
+               for (val=0; val < regval_ctr_numvals(ctr); val++)
+                       map_regval_to_ads(mem_ctx, mods, regval_ctr_specific_value(ctr, val));
+       }
+       return True;
 }
 
 #endif