Finish removal of iconv_convenience in public API's.
[bbaumbach/samba-autobuild/.git] / source4 / ntptr / simple_ldb / ntptr_simple_ldb.c
index e59d84ff919d6dd94299a8c887c932e603fa9b17..ef456f05dff93a129a18c45af5994eaeb5dd2ae5 100644 (file)
@@ -34,7 +34,7 @@
 #include "auth/auth.h"
 #include "dsdb/samdb/samdb.h"
 #include "ldb_wrap.h"
-#include "util/util_ldb.h"
+#include "../lib/util/util_ldb.h"
 #include "rpc_server/common/common.h"
 #include "param/param.h"
 
   connect to the SPOOLSS database
   return a ldb_context pointer on success, or NULL on failure
  */
-static struct ldb_context *sptr_db_connect(TALLOC_CTX *mem_ctx)
+static struct ldb_context *sptr_db_connect(TALLOC_CTX *mem_ctx, struct tevent_context *ev_ctx, struct loadparm_context *lp_ctx)
 {
-       return ldb_wrap_connect(mem_ctx, global_loadparm, lp_spoolss_url(global_loadparm), system_session(mem_ctx), 
-                               NULL, 0, NULL);
+       return ldb_wrap_connect(mem_ctx, ev_ctx, lp_ctx, lp_spoolss_url(lp_ctx), system_session(lp_ctx), 
+                               NULL, 0);
 }
 
 static int sptr_db_search(struct ldb_context *ldb,
@@ -87,7 +87,7 @@ static int sptr_db_search(struct ldb_context *ldb,
 
 static NTSTATUS sptr_init_context(struct ntptr_context *ntptr)
 {
-       struct ldb_context *sptr_db = sptr_db_connect(ntptr);
+       struct ldb_context *sptr_db = sptr_db_connect(ntptr, ntptr->ev_ctx, ntptr->lp_ctx);
        NT_STATUS_HAVE_NO_MEMORY(sptr_db);
 
        ntptr->private_data = sptr_db;
@@ -122,57 +122,62 @@ static WERROR sptr_OpenPrintServer(struct ntptr_context *ntptr, TALLOC_CTX *mem_
 /*
  * PrintServer PrinterData functions
  */
-static WERROR sptr_GetPrintServerData(struct ntptr_GenericHandle *server, TALLOC_CTX *mem_ctx,
-                                     struct spoolss_GetPrinterData *r)
+
+static WERROR sptr_PrintServerData(struct ntptr_GenericHandle *server,
+                                  TALLOC_CTX *mem_ctx,
+                                  const char *value_name,
+                                  union spoolss_PrinterData *r,
+                                  enum winreg_Type *type)
 {
-       if (strcmp("W3SvcInstalled", r->in.value_name) == 0) {
-               r->out.type             = SPOOLSS_PRINTER_DATA_TYPE_UINT32;
-               r->out.data.value       = 0;
+       struct dcerpc_server_info *server_info = lp_dcerpc_server_info(mem_ctx, server->ntptr->lp_ctx);
+       if (strcmp("W3SvcInstalled", value_name) == 0) {
+               *type           = REG_DWORD;
+               r->value        = 0;
                return WERR_OK;
-       } else if (strcmp("BeepEnabled", r->in.value_name) == 0) {
-               r->out.type             = SPOOLSS_PRINTER_DATA_TYPE_UINT32;
-               r->out.data.value       = 0;
+       } else if (strcmp("BeepEnabled", value_name) == 0) {
+               *type           = REG_DWORD;
+               r->value        = 0;
                return WERR_OK;
-       } else if (strcmp("EventLog", r->in.value_name) == 0) {
-               r->out.type             = SPOOLSS_PRINTER_DATA_TYPE_UINT32;
-               r->out.data.value       = 0;
+       } else if (strcmp("EventLog", value_name) == 0) {
+               *type           = REG_DWORD;
+               r->value        = 0;
                return WERR_OK;
-       } else if (strcmp("NetPopup", r->in.value_name) == 0) {
-               r->out.type             = SPOOLSS_PRINTER_DATA_TYPE_UINT32;
-               r->out.data.value       = 0;
+       } else if (strcmp("NetPopup", value_name) == 0) {
+               *type           = REG_DWORD;
+               r->value        = 0;
                return WERR_OK;
-       } else if (strcmp("NetPopupToComputer", r->in.value_name) == 0) {
-               r->out.type             = SPOOLSS_PRINTER_DATA_TYPE_UINT32;
-               r->out.data.value       = 0;
+       } else if (strcmp("NetPopupToComputer", value_name) == 0) {
+               *type           = REG_DWORD;
+               r->value        = 0;
                return  WERR_OK;
-       } else if (strcmp("MajorVersion", r->in.value_name) == 0) {
-               r->out.type             = SPOOLSS_PRINTER_DATA_TYPE_UINT32;
-               r->out.data.value       = 3;
+       } else if (strcmp("MajorVersion", value_name) == 0) {
+               *type           = REG_DWORD;
+               r->value        = 3;
                return WERR_OK;
-       } else if (strcmp("MinorVersion", r->in.value_name) == 0) {
-               r->out.type             = SPOOLSS_PRINTER_DATA_TYPE_UINT32;
-               r->out.data.value       = 0;
+       } else if (strcmp("MinorVersion", value_name) == 0) {
+               *type           = REG_DWORD;
+               r->value        = 0;
                return WERR_OK;
-       } else if (strcmp("DefaultSpoolDirectory", r->in.value_name) == 0) {
-               r->out.type             = SPOOLSS_PRINTER_DATA_TYPE_STRING;
-               r->out.data.string      = "C:\\PRINTERS";
+       } else if (strcmp("DefaultSpoolDirectory", value_name) == 0) {
+               *type           = REG_SZ;
+               r->string       = "C:\\PRINTERS";
                return  WERR_OK;
-       } else if (strcmp("Architecture", r->in.value_name) == 0) {
-               r->out.type             = SPOOLSS_PRINTER_DATA_TYPE_STRING;
-               r->out.data.string      = SPOOLSS_ARCHITECTURE_NT_X86;
+       } else if (strcmp("Architecture", value_name) == 0) {
+               *type           = REG_SZ;
+               r->string       = SPOOLSS_ARCHITECTURE_NT_X86;
                return  WERR_OK;
-       } else if (strcmp("DsPresent", r->in.value_name) == 0) {
-               r->out.type             = SPOOLSS_PRINTER_DATA_TYPE_UINT32;
-               r->out.data.value       = 1;
+       } else if (strcmp("DsPresent", value_name) == 0) {
+               *type           = REG_DWORD;
+               r->value        = 1;
                return WERR_OK;
-       } else if (strcmp("OSVersion", r->in.value_name) == 0) {
+       } else if (strcmp("OSVersion", value_name) == 0) {
                DATA_BLOB blob;
                enum ndr_err_code ndr_err;
                struct spoolss_OSVersion os;
 
-               os.major                = dcesrv_common_get_version_major(mem_ctx, NULL);
-               os.minor                = dcesrv_common_get_version_minor(mem_ctx, NULL);
-               os.build                = dcesrv_common_get_version_build(mem_ctx, NULL);
+               os.major                = server_info->version_major;
+               os.minor                = server_info->version_minor;
+               os.build                = server_info->version_build;
                os.extra_string         = "";
 
                ndr_err = ndr_push_struct_blob(&blob, mem_ctx, &os, (ndr_push_flags_fn_t)ndr_push_spoolss_OSVersion);
@@ -180,43 +185,76 @@ static WERROR sptr_GetPrintServerData(struct ntptr_GenericHandle *server, TALLOC
                        return WERR_GENERAL_FAILURE;
                }
 
-               r->out.type             = SPOOLSS_PRINTER_DATA_TYPE_BINARY;
-               r->out.data.binary      = blob;
+               *type           = REG_BINARY;
+               r->binary       = blob;
                return WERR_OK;
-       } else if (strcmp("OSVersionEx", r->in.value_name) == 0) {
+       } else if (strcmp("OSVersionEx", value_name) == 0) {
                DATA_BLOB blob;
                enum ndr_err_code ndr_err;
                struct spoolss_OSVersionEx os_ex;
 
-               os_ex.major             = dcesrv_common_get_version_major(mem_ctx, NULL);
-               os_ex.minor             = dcesrv_common_get_version_minor(mem_ctx, NULL);
-               os_ex.build             = dcesrv_common_get_version_build(mem_ctx, NULL);
-               os_ex.extra_string              = "";
-               os_ex.unknown2          = 0;
-               os_ex.unknown3          = 0;
+               os_ex.major             = server_info->version_major;
+               os_ex.minor             = server_info->version_minor;
+               os_ex.build             = server_info->version_build;
+               os_ex.extra_string      = "";
+               os_ex.service_pack_major= 0;
+               os_ex.service_pack_minor= 0;
+               os_ex.suite_mask        = 0;
+               os_ex.product_type      = 0;
+               os_ex.reserved          = 0;
 
                ndr_err = ndr_push_struct_blob(&blob, mem_ctx, &os_ex, (ndr_push_flags_fn_t)ndr_push_spoolss_OSVersionEx);
                if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
                        return WERR_GENERAL_FAILURE;
                }
 
-               r->out.type             = SPOOLSS_PRINTER_DATA_TYPE_BINARY;
-               r->out.data.binary      = blob;
+               *type           = REG_BINARY;
+               r->binary       = blob;
                return WERR_OK;
-       } else if (strcmp("DNSMachineName", r->in.value_name) == 0) {
-               if (!lp_realm(global_loadparm)) return WERR_INVALID_PARAM;
-
-               r->out.type             = SPOOLSS_PRINTER_DATA_TYPE_STRING;
-               r->out.data.string      = talloc_asprintf(mem_ctx, "%s.%s",
-                                                                  lp_netbios_name(global_loadparm),
-                                                                  lp_realm(global_loadparm));
-               W_ERROR_HAVE_NO_MEMORY(r->out.data.string);
+       } else if (strcmp("DNSMachineName", value_name) == 0) {
+               const char *dnsdomain = lp_dnsdomain(server->ntptr->lp_ctx);
+
+               if (dnsdomain == NULL) return WERR_INVALID_PARAM;
+
+               *type           = REG_SZ;
+               r->string       = talloc_asprintf(mem_ctx, "%s.%s",
+                                                         lp_netbios_name(server->ntptr->lp_ctx),
+                                                         dnsdomain);
+               W_ERROR_HAVE_NO_MEMORY(r->string);
                return WERR_OK;
        }
 
        return WERR_INVALID_PARAM;
 }
 
+static WERROR sptr_GetPrintServerData(struct ntptr_GenericHandle *server, TALLOC_CTX *mem_ctx,
+                                     struct spoolss_GetPrinterData *r)
+{
+       WERROR result;
+       union spoolss_PrinterData data;
+       DATA_BLOB blob;
+       enum ndr_err_code ndr_err;
+
+       result = sptr_PrintServerData(server, mem_ctx, r->in.value_name, &data, r->out.type);
+       if (!W_ERROR_IS_OK(result)) {
+               return result;
+       }
+
+       ndr_err = ndr_push_union_blob(&blob, mem_ctx, 
+                                     &data, *r->out.type, (ndr_push_flags_fn_t)ndr_push_spoolss_PrinterData);
+       if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
+               return WERR_GENERAL_FAILURE;
+       }
+
+       *r->out.needed = blob.length;
+
+       if (r->in.offered >= *r->out.needed) {
+               memcpy(r->out.data, blob.data, blob.length);
+       }
+
+       return WERR_OK;
+}
+
 /* PrintServer Form functions */
 static WERROR sptr_EnumPrintServerForms(struct ntptr_GenericHandle *server, TALLOC_CTX *mem_ctx,
                                        struct spoolss_EnumForms *r)
@@ -258,8 +296,8 @@ static WERROR sptr_EnumPrintServerForms(struct ntptr_GenericHandle *server, TALL
                return WERR_UNKNOWN_LEVEL;
        }
 
-       r->out.info     = info;
-       r->out.count    = count;
+       *r->out.info    = info;
+       *r->out.count   = count;
        return WERR_OK;
 }
 
@@ -318,7 +356,7 @@ static WERROR sptr_AddPrintServerForm(struct ntptr_GenericHandle *server, TALLOC
                return WERR_UNKNOWN_LEVEL;
        }
 
-       ret = samdb_add(sptr_db, mem_ctx, msg);
+       ret = ldb_add(sptr_db, msg);
        if (ret != 0) {
                return WERR_FOOBAR;
        }
@@ -383,7 +421,7 @@ static WERROR sptr_SetPrintServerForm(struct ntptr_GenericHandle *server, TALLOC
                return WERR_UNKNOWN_LEVEL;
        }
 
-       ret = samdb_replace(sptr_db, mem_ctx, msg);
+       ret = dsdb_replace(sptr_db, msg, 0);
        if (ret != 0) {
                return WERR_FOOBAR;
        }
@@ -424,7 +462,7 @@ static WERROR sptr_DeletePrintServerForm(struct ntptr_GenericHandle *server, TAL
                return WERR_FOOBAR;
        }
 
-       ret = samdb_delete(sptr_db, mem_ctx, msgs[0]->dn);
+       ret = ldb_delete(sptr_db, msgs[0]->dn);
        if (ret != 0) {
                return WERR_FOOBAR;
        }
@@ -586,8 +624,8 @@ static WERROR sptr_EnumPrinters(struct ntptr_context *ntptr, TALLOC_CTX *mem_ctx
                return WERR_UNKNOWN_LEVEL;
        }
 
-       r->out.info     = info;
-       r->out.count    = count;
+       *r->out.info    = info;
+       *r->out.count   = count;
        return WERR_OK;
 }
 
@@ -644,8 +682,8 @@ static WERROR sptr_EnumPorts(struct ntptr_context *ntptr, TALLOC_CTX *mem_ctx,
                return WERR_UNKNOWN_LEVEL;
        }
 
-       r->out.info     = info;
-       r->out.count    = count;
+       *r->out.info    = info;
+       *r->out.count   = count;
        return WERR_OK;
 }
 
@@ -691,9 +729,8 @@ static WERROR sptr_EnumMonitors(struct ntptr_context *ntptr, TALLOC_CTX *mem_ctx
                return WERR_UNKNOWN_LEVEL;
        }
 
-       r->out.info     = info;
-       r->out.count    = count;
-       return WERR_OK;
+       *r->out.info    = info;
+       *r->out.count   = count;
        return WERR_OK;
 }
 
@@ -750,6 +787,47 @@ static WERROR sptr_GetPrinterForm(struct ntptr_GenericHandle *printer, TALLOC_CT
        return WERR_OK;
 }
 
+static WERROR sptr_GetPrintProcessorDirectory(struct ntptr_context *ntptr, TALLOC_CTX *mem_ctx,
+                                             struct spoolss_GetPrintProcessorDirectory *r)
+{
+       union spoolss_PrintProcessorDirectoryInfo *info;
+       const char *prefix;
+       const char *postfix;
+
+       /*
+        * NOTE: normally r->in.level is 1, but both w2k3 and nt4 sp6a
+        *        are ignoring the r->in.level completely, so we do :-)
+        */
+
+       /*
+        * TODO: check the server name is ours
+        * - if it's a invalid UNC then return WERR_INVALID_NAME
+        * - if it's the wrong host name return WERR_INVALID_PARAM
+        * - if it's "" then we need to return a local WINDOWS path
+        */
+       if (!r->in.server || !r->in.server[0]) {
+               prefix = "C:\\PRTPROCS";
+       } else {
+               prefix = talloc_asprintf(mem_ctx, "%s\\prnproc$", r->in.server);
+               W_ERROR_HAVE_NO_MEMORY(prefix);
+       }
+
+       if (r->in.environment && strcmp(SPOOLSS_ARCHITECTURE_NT_X86, r->in.environment) == 0) {
+               postfix = "W32X86";
+       } else {
+               return WERR_INVALID_ENVIRONMENT;
+       }
+
+       info = talloc(mem_ctx, union spoolss_PrintProcessorDirectoryInfo);
+       W_ERROR_HAVE_NO_MEMORY(info);
+
+       info->info1.directory_name      = talloc_asprintf(mem_ctx, "%s\\%s", prefix, postfix);
+       W_ERROR_HAVE_NO_MEMORY(info->info1.directory_name);
+
+       r->out.info = info;
+       return WERR_OK;
+}
+
 
 /*
   initialialise the simble ldb backend, registering ourselves with the ntptr subsystem
@@ -793,6 +871,8 @@ static const struct ntptr_ops ntptr_simple_ldb_ops = {
        /* PrintProcessor functions */
 /*     .EnumPrintProcessors            = sptr_EnumPrintProcessors,
 */
+       .GetPrintProcessorDirectory     = sptr_GetPrintProcessorDirectory,
+
        /* Printer functions */
        .EnumPrinters                   = sptr_EnumPrinters,
        .OpenPrinter                    = sptr_OpenPrinter,