27ab6dec6d7d9eb9867ce722c65fd186f36ac6ed
[sfrench/samba-autobuild/.git] / source4 / ntptr / simple_ldb / ntptr_simple_ldb.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Simple LDB NTPTR backend
5
6    Copyright (C) Stefan (metze) Metzmacher 2005
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21 /*
22   This implements a NTPTR backend that store
23   all objects (Printers, Ports, Monitors, PrinterDrivers ...)
24   in a ldb database, but doesn't do real printing.
25
26   This is just used for testing how some of
27   the SPOOLSS protocol details should work
28 */
29
30 #include "includes.h"
31 #include "ntptr/ntptr.h"
32 #include "librpc/gen_ndr/ndr_spoolss.h"
33 #include <ldb.h>
34 #include "auth/auth.h"
35 #include "dsdb/samdb/samdb.h"
36 #include "ldb_wrap.h"
37 #include "../lib/util/util_ldb.h"
38 #include "librpc/gen_ndr/dcerpc.h"
39 #include "rpc_server/dcerpc_server.h"
40 #include "rpc_server/common/common.h"
41 #include "param/param.h"
42
43 /*
44   connect to the SPOOLSS database
45   return a ldb_context pointer on success, or NULL on failure
46  */
47 static struct ldb_context *sptr_db_connect(TALLOC_CTX *mem_ctx, struct tevent_context *ev_ctx, struct loadparm_context *lp_ctx)
48 {
49         return ldb_wrap_connect(mem_ctx, ev_ctx, lp_ctx, lpcfg_spoolss_url(lp_ctx), system_session(lp_ctx),
50                                 NULL, 0);
51 }
52
53 static int sptr_db_search(struct ldb_context *ldb,
54                           TALLOC_CTX *mem_ctx,
55                           struct ldb_dn *basedn,
56                           struct ldb_message ***res,
57                           const char * const *attrs,
58                           const char *format, ...) PRINTF_ATTRIBUTE(6,7);
59
60 static int sptr_db_search(struct ldb_context *ldb,
61                           TALLOC_CTX *mem_ctx,
62                           struct ldb_dn *basedn,
63                           struct ldb_message ***res,
64                           const char * const *attrs,
65                           const char *format, ...)
66 {
67         va_list ap;
68         int count;
69
70         va_start(ap, format);
71         count = gendb_search_v(ldb, mem_ctx, basedn, res, attrs, format, ap);
72         va_end(ap);
73
74         return count;
75 }
76
77 #define SET_STRING(ldb, mod, attr, value) do { \
78         if (value == NULL) return WERR_INVALID_PARAM; \
79         if (ldb_msg_add_string(mod, attr, value) != LDB_SUCCESS) { \
80                 return WERR_NOMEM; \
81         } \
82 } while (0)
83
84 #define SET_UINT(ldb, mod, attr, value) do { \
85         if (samdb_msg_add_uint(ldb, (TALLOC_CTX *)mod, mod, attr, value) != LDB_SUCCESS) { \
86                 return WERR_NOMEM; \
87         } \
88 } while (0)
89
90 static NTSTATUS sptr_init_context(struct ntptr_context *ntptr)
91 {
92         struct ldb_context *sptr_db = sptr_db_connect(ntptr, ntptr->ev_ctx, ntptr->lp_ctx);
93         NT_STATUS_HAVE_NO_MEMORY(sptr_db);
94
95         ntptr->private_data = sptr_db;
96
97         return NT_STATUS_OK;
98 }
99
100 /* PrintServer functions */
101 static WERROR sptr_OpenPrintServer(struct ntptr_context *ntptr, TALLOC_CTX *mem_ctx,
102                                    struct spoolss_OpenPrinterEx *r,
103                                    const char *server_name,
104                                    struct ntptr_GenericHandle **_server)
105 {
106         struct ntptr_GenericHandle *server;
107
108         /* TODO: do access check here! */
109
110         server = talloc(mem_ctx, struct ntptr_GenericHandle);
111         W_ERROR_HAVE_NO_MEMORY(server);
112
113         server->type            = NTPTR_HANDLE_SERVER;
114         server->ntptr           = ntptr;
115         server->object_name     = talloc_strdup(server, server_name);
116         W_ERROR_HAVE_NO_MEMORY(server->object_name);
117         server->access_mask     = 0;
118         server->private_data    = NULL;
119
120         *_server = server;
121         return WERR_OK;
122 }
123
124 /*
125  * PrintServer PrinterData functions
126  */
127
128 static WERROR sptr_PrintServerData(struct ntptr_GenericHandle *server,
129                                    TALLOC_CTX *mem_ctx,
130                                    const char *value_name,
131                                    union spoolss_PrinterData *r,
132                                    enum winreg_Type *type)
133 {
134         struct dcerpc_server_info *server_info = lpcfg_dcerpc_server_info(mem_ctx, server->ntptr->lp_ctx);
135         if (strcmp("W3SvcInstalled", value_name) == 0) {
136                 *type           = REG_DWORD;
137                 r->value        = 0;
138                 return WERR_OK;
139         } else if (strcmp("BeepEnabled", value_name) == 0) {
140                 *type           = REG_DWORD;
141                 r->value        = 0;
142                 return WERR_OK;
143         } else if (strcmp("EventLog", value_name) == 0) {
144                 *type           = REG_DWORD;
145                 r->value        = 0;
146                 return WERR_OK;
147         } else if (strcmp("NetPopup", value_name) == 0) {
148                 *type           = REG_DWORD;
149                 r->value        = 0;
150                 return WERR_OK;
151         } else if (strcmp("NetPopupToComputer", value_name) == 0) {
152                 *type           = REG_DWORD;
153                 r->value        = 0;
154                 return  WERR_OK;
155         } else if (strcmp("MajorVersion", value_name) == 0) {
156                 *type           = REG_DWORD;
157                 r->value        = 3;
158                 return WERR_OK;
159         } else if (strcmp("MinorVersion", value_name) == 0) {
160                 *type           = REG_DWORD;
161                 r->value        = 0;
162                 return WERR_OK;
163         } else if (strcmp("DefaultSpoolDirectory", value_name) == 0) {
164                 *type           = REG_SZ;
165                 r->string       = "C:\\PRINTERS";
166                 return  WERR_OK;
167         } else if (strcmp("Architecture", value_name) == 0) {
168                 *type           = REG_SZ;
169                 r->string       = SPOOLSS_ARCHITECTURE_NT_X86;
170                 return  WERR_OK;
171         } else if (strcmp("DsPresent", value_name) == 0) {
172                 *type           = REG_DWORD;
173                 r->value        = 1;
174                 return WERR_OK;
175         } else if (strcmp("OSVersion", value_name) == 0) {
176                 DATA_BLOB blob;
177                 enum ndr_err_code ndr_err;
178                 struct spoolss_OSVersion os;
179
180                 os.major                = server_info->version_major;
181                 os.minor                = server_info->version_minor;
182                 os.build                = server_info->version_build;
183                 os.extra_string         = "";
184
185                 ndr_err = ndr_push_struct_blob(&blob, mem_ctx, &os, (ndr_push_flags_fn_t)ndr_push_spoolss_OSVersion);
186                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
187                         return WERR_GENERAL_FAILURE;
188                 }
189
190                 *type           = REG_BINARY;
191                 r->binary       = blob;
192                 return WERR_OK;
193         } else if (strcmp("OSVersionEx", value_name) == 0) {
194                 DATA_BLOB blob;
195                 enum ndr_err_code ndr_err;
196                 struct spoolss_OSVersionEx os_ex;
197
198                 os_ex.major             = server_info->version_major;
199                 os_ex.minor             = server_info->version_minor;
200                 os_ex.build             = server_info->version_build;
201                 os_ex.extra_string      = "";
202                 os_ex.service_pack_major= 0;
203                 os_ex.service_pack_minor= 0;
204                 os_ex.suite_mask        = 0;
205                 os_ex.product_type      = 0;
206                 os_ex.reserved          = 0;
207
208                 ndr_err = ndr_push_struct_blob(&blob, mem_ctx, &os_ex, (ndr_push_flags_fn_t)ndr_push_spoolss_OSVersionEx);
209                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
210                         return WERR_GENERAL_FAILURE;
211                 }
212
213                 *type           = REG_BINARY;
214                 r->binary       = blob;
215                 return WERR_OK;
216         } else if (strcmp("DNSMachineName", value_name) == 0) {
217                 const char *dnsdomain = lpcfg_dnsdomain(server->ntptr->lp_ctx);
218
219                 if (dnsdomain == NULL) return WERR_INVALID_PARAM;
220
221                 *type           = REG_SZ;
222                 r->string       = talloc_asprintf(mem_ctx, "%s.%s",
223                                                           lpcfg_netbios_name(server->ntptr->lp_ctx),
224                                                           dnsdomain);
225                 W_ERROR_HAVE_NO_MEMORY(r->string);
226                 return WERR_OK;
227         }
228
229         return WERR_INVALID_PARAM;
230 }
231
232 static WERROR sptr_GetPrintServerData(struct ntptr_GenericHandle *server, TALLOC_CTX *mem_ctx,
233                                       struct spoolss_GetPrinterData *r)
234 {
235         WERROR result;
236         union spoolss_PrinterData data;
237         DATA_BLOB blob;
238         enum ndr_err_code ndr_err;
239
240         result = sptr_PrintServerData(server, mem_ctx, r->in.value_name, &data, r->out.type);
241         if (!W_ERROR_IS_OK(result)) {
242                 return result;
243         }
244
245         ndr_err = ndr_push_union_blob(&blob, mem_ctx, 
246                                       &data, *r->out.type, (ndr_push_flags_fn_t)ndr_push_spoolss_PrinterData);
247         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
248                 return WERR_GENERAL_FAILURE;
249         }
250
251         *r->out.needed = blob.length;
252
253         if (r->in.offered >= *r->out.needed) {
254                 memcpy(r->out.data, blob.data, blob.length);
255         }
256
257         return WERR_OK;
258 }
259
260 /* PrintServer Form functions */
261 static WERROR sptr_EnumPrintServerForms(struct ntptr_GenericHandle *server, TALLOC_CTX *mem_ctx,
262                                         struct spoolss_EnumForms *r)
263 {
264         struct ldb_context *sptr_db = talloc_get_type(server->ntptr->private_data, struct ldb_context);
265         struct ldb_message **msgs;
266         int count;
267         int i;
268         union spoolss_FormInfo *info;
269
270         count = sptr_db_search(sptr_db, mem_ctx,
271                                 ldb_dn_new(mem_ctx, sptr_db, "CN=Forms,CN=PrintServer"),
272                                 &msgs, NULL, "(&(objectClass=form))");
273
274         if (count == 0) return WERR_OK;
275         if (count < 0) return WERR_GENERAL_FAILURE;
276
277         info = talloc_array(mem_ctx, union spoolss_FormInfo, count);
278         W_ERROR_HAVE_NO_MEMORY(info);
279
280         switch (r->in.level) {
281         case 1:
282                 for (i=0; i < count; i++) {
283                         info[i].info1.flags             = ldb_msg_find_attr_as_uint(msgs[i], "flags", SPOOLSS_FORM_BUILTIN);
284
285                         info[i].info1.form_name         = ldb_msg_find_attr_as_string(msgs[i], "form-name", NULL);
286                         W_ERROR_HAVE_NO_MEMORY(info[i].info1.form_name);
287
288                         info[i].info1.size.width        = ldb_msg_find_attr_as_uint(msgs[i], "size-width", 0);
289                         info[i].info1.size.height       = ldb_msg_find_attr_as_uint(msgs[i], "size-height", 0);
290
291                         info[i].info1.area.left         = ldb_msg_find_attr_as_uint(msgs[i], "area-left", 0);
292                         info[i].info1.area.top          = ldb_msg_find_attr_as_uint(msgs[i], "area-top", 0);
293                         info[i].info1.area.right        = ldb_msg_find_attr_as_uint(msgs[i], "area-right", 0);
294                         info[i].info1.area.bottom       = ldb_msg_find_attr_as_uint(msgs[i], "area-bottom", 0);
295                 }
296                 break;
297         default:
298                 return WERR_UNKNOWN_LEVEL;
299         }
300
301         *r->out.info    = info;
302         *r->out.count   = count;
303         return WERR_OK;
304 }
305
306 static WERROR sptr_AddPrintServerForm(struct ntptr_GenericHandle *server, TALLOC_CTX *mem_ctx,
307                                       struct spoolss_AddForm *r)
308 {
309         struct ldb_context *sptr_db = talloc_get_type(server->ntptr->private_data, struct ldb_context);
310         struct ldb_message *msg,**msgs;
311         const char * const attrs[] = {"flags", NULL };
312         int count, ret;
313
314         /* TODO: do checks access here
315          * if (!(server->access_mask & desired_access)) {
316          *      return WERR_FOOBAR;
317          * }
318          */
319
320         switch (r->in.level) {
321         case 1:
322                 if (!r->in.info.info1) {
323                         return WERR_FOOBAR;
324                 }
325                 count = sptr_db_search(sptr_db, mem_ctx,
326                                        ldb_dn_new(mem_ctx, sptr_db, "CN=Forms,CN=PrintServer"),
327                                        &msgs, attrs, "(&(form-name=%s)(objectClass=form))",
328                                        r->in.info.info1->form_name);
329
330                 if (count == 1) return WERR_FOOBAR;
331                 if (count > 1) return WERR_FOOBAR;
332                 if (count < 0) return WERR_GENERAL_FAILURE;
333
334                 if (r->in.info.info1->flags != SPOOLSS_FORM_USER) {
335                         return WERR_FOOBAR;
336                 }
337
338                 msg = ldb_msg_new(mem_ctx);
339                 W_ERROR_HAVE_NO_MEMORY(msg);
340
341                 /* add core elements to the ldb_message for the Form */
342                 msg->dn = ldb_dn_new_fmt(msg, sptr_db, "form-name=%s,CN=Forms,CN=PrintServer", r->in.info.info1->form_name);
343                 SET_STRING(sptr_db, msg, "objectClass", "form");
344
345                 SET_UINT(sptr_db, msg, "flags", r->in.info.info1->flags);
346
347                 SET_STRING(sptr_db, msg, "form-name", r->in.info.info1->form_name);
348
349                 SET_UINT(sptr_db, msg, "size-width", r->in.info.info1->size.width);
350                 SET_UINT(sptr_db, msg, "size-height", r->in.info.info1->size.height);
351
352                 SET_UINT(sptr_db, msg, "area-left", r->in.info.info1->area.left);
353                 SET_UINT(sptr_db, msg, "area-top", r->in.info.info1->area.top);
354                 SET_UINT(sptr_db, msg, "area-right", r->in.info.info1->area.right);
355                 SET_UINT(sptr_db, msg, "area-bottom", r->in.info.info1->area.bottom);
356                 break;
357         default:
358                 return WERR_UNKNOWN_LEVEL;
359         }
360
361         ret = ldb_add(sptr_db, msg);
362         if (ret != 0) {
363                 return WERR_FOOBAR;
364         }
365
366         return WERR_OK;
367 }
368
369 static WERROR sptr_SetPrintServerForm(struct ntptr_GenericHandle *server, TALLOC_CTX *mem_ctx,
370                                       struct spoolss_SetForm *r)
371 {
372         struct ldb_context *sptr_db = talloc_get_type(server->ntptr->private_data, struct ldb_context);
373         struct ldb_message *msg,**msgs;
374         const char * const attrs[] = { "flags", NULL};
375         int count, ret;
376         enum spoolss_FormFlags flags;
377
378         /* TODO: do checks access here
379          * if (!(server->access_mask & desired_access)) {
380          *      return WERR_FOOBAR;
381          * }
382          */
383
384         switch (r->in.level) {
385         case 1:
386                 if (!r->in.info.info1) {
387                         return WERR_FOOBAR;
388                 }
389
390                 count = sptr_db_search(sptr_db, mem_ctx,
391                                        ldb_dn_new(mem_ctx, sptr_db, "CN=Forms,CN=PrintServer"),
392                                        &msgs, attrs, "(&(form-name=%s)(objectClass=form))",
393                                        r->in.info.info1->form_name);
394
395                 if (count == 0) return WERR_FOOBAR;
396                 if (count > 1) return WERR_FOOBAR;
397                 if (count < 0) return WERR_GENERAL_FAILURE;
398
399                 flags = ldb_msg_find_attr_as_uint(msgs[0], "flags", SPOOLSS_FORM_BUILTIN);
400                 if (flags != SPOOLSS_FORM_USER) {
401                         return WERR_FOOBAR;
402                 }
403
404                 msg = ldb_msg_new(mem_ctx);
405                 W_ERROR_HAVE_NO_MEMORY(msg);
406
407                 /* add core elements to the ldb_message for the user */
408                 msg->dn = msgs[0]->dn;
409
410                 SET_UINT(sptr_db, msg, "flags", r->in.info.info1->flags);
411
412                 SET_STRING(sptr_db, msg, "form-name", r->in.info.info1->form_name);
413
414                 SET_UINT(sptr_db, msg, "size-width", r->in.info.info1->size.width);
415                 SET_UINT(sptr_db, msg, "size-height", r->in.info.info1->size.height);
416
417                 SET_UINT(sptr_db, msg, "area-left", r->in.info.info1->area.left);
418                 SET_UINT(sptr_db, msg, "area-top", r->in.info.info1->area.top);
419                 SET_UINT(sptr_db, msg, "area-right", r->in.info.info1->area.right);
420                 SET_UINT(sptr_db, msg, "area-bottom", r->in.info.info1->area.bottom);
421                 break;
422         default:
423                 return WERR_UNKNOWN_LEVEL;
424         }
425
426         ret = dsdb_replace(sptr_db, msg, 0);
427         if (ret != 0) {
428                 return WERR_FOOBAR;
429         }
430
431         return WERR_OK;
432 }
433
434 static WERROR sptr_DeletePrintServerForm(struct ntptr_GenericHandle *server, TALLOC_CTX *mem_ctx,
435                                          struct spoolss_DeleteForm *r)
436 {
437         struct ldb_context *sptr_db = talloc_get_type(server->ntptr->private_data, struct ldb_context);
438         struct ldb_message **msgs;
439         const char * const attrs[] = { "flags", NULL};
440         int count, ret;
441         enum spoolss_FormFlags flags;
442
443         /* TODO: do checks access here
444          * if (!(server->access_mask & desired_access)) {
445          *      return WERR_FOOBAR;
446          * }
447          */
448
449         if (!r->in.form_name) {
450                 return WERR_FOOBAR;
451         }
452
453         count = sptr_db_search(sptr_db, mem_ctx,
454                                ldb_dn_new(mem_ctx, sptr_db, "CN=Forms,CN=PrintServer"),
455                                &msgs, attrs, "(&(form-name=%s)(objectclass=form))",
456                                r->in.form_name);
457
458         if (count == 0) return WERR_FOOBAR;
459         if (count > 1) return WERR_FOOBAR;
460         if (count < 0) return WERR_GENERAL_FAILURE;
461
462         flags = ldb_msg_find_attr_as_uint(msgs[0], "flags", SPOOLSS_FORM_BUILTIN);
463         if (flags != SPOOLSS_FORM_USER) {
464                 return WERR_FOOBAR;
465         }
466
467         ret = ldb_delete(sptr_db, msgs[0]->dn);
468         if (ret != 0) {
469                 return WERR_FOOBAR;
470         }
471
472         return WERR_OK;
473 }
474
475 /* PrintServer Driver functions */
476 static WERROR sptr_EnumPrinterDrivers(struct ntptr_context *ntptr, TALLOC_CTX *mem_ctx,
477                                       struct spoolss_EnumPrinterDrivers *r)
478 {
479         return WERR_OK;
480 }
481
482 static WERROR sptr_GetPrinterDriverDirectory(struct ntptr_context *ntptr, TALLOC_CTX *mem_ctx,
483                                              struct spoolss_GetPrinterDriverDirectory *r)
484 {
485         union spoolss_DriverDirectoryInfo *info;
486         const char *prefix;
487         const char *postfix;
488
489         /*
490          * NOTE: normally r->in.level is 1, but both w2k3 and nt4 sp6a
491          *        are ignoring the r->in.level completely, so we do :-)
492          */
493        
494         /*
495          * TODO: check the server name is ours
496          * - if it's a invalid UNC then return WERR_INVALID_NAME
497          * - if it's the wrong host name return WERR_INVALID_PARAM
498          * - if it's "" then we need to return a local WINDOWS path
499          */
500         if (!r->in.server || !r->in.server[0]) {
501                 prefix = "C:\\DRIVERS";
502         } else {
503                 prefix = talloc_asprintf(mem_ctx, "%s\\print$", r->in.server);
504                 W_ERROR_HAVE_NO_MEMORY(prefix);
505         }
506
507         if (r->in.environment && strcmp(SPOOLSS_ARCHITECTURE_NT_X86, r->in.environment) == 0) {
508                 postfix = "W32X86";
509         } else {
510                 return WERR_INVALID_ENVIRONMENT;
511         }
512
513         info = talloc(mem_ctx, union spoolss_DriverDirectoryInfo);
514         W_ERROR_HAVE_NO_MEMORY(info);
515
516         info->info1.directory_name      = talloc_asprintf(mem_ctx, "%s\\%s", prefix, postfix);
517         W_ERROR_HAVE_NO_MEMORY(info->info1.directory_name);
518
519         r->out.info = info;
520         return WERR_OK;
521 }
522
523 /* Printer functions */
524 static WERROR sptr_EnumPrinters(struct ntptr_context *ntptr, TALLOC_CTX *mem_ctx,
525                                 struct spoolss_EnumPrinters *r)
526 {
527         struct ldb_context *sptr_db = talloc_get_type(ntptr->private_data, struct ldb_context);
528         struct ldb_message **msgs;
529         int count;
530         int i;
531         union spoolss_PrinterInfo *info;
532
533         count = sptr_db_search(sptr_db, mem_ctx, NULL, &msgs, NULL,
534                                "(&(objectclass=printer))");
535
536         if (count == 0) return WERR_OK;
537         if (count < 0) return WERR_GENERAL_FAILURE;
538
539         info = talloc_array(mem_ctx, union spoolss_PrinterInfo, count);
540         W_ERROR_HAVE_NO_MEMORY(info);
541
542         switch(r->in.level) {
543         case 1:
544                 for (i = 0; i < count; i++) {
545                         info[i].info1.flags             = ldb_msg_find_attr_as_uint(msgs[i], "flags", 0);
546
547                         info[i].info1.name              = ldb_msg_find_attr_as_string(msgs[i], "name", "");
548                         W_ERROR_HAVE_NO_MEMORY(info[i].info1.name);
549
550                         info[i].info1.description       = ldb_msg_find_attr_as_string(msgs[i], "description", "");
551                         W_ERROR_HAVE_NO_MEMORY(info[i].info1.description);
552
553                         info[i].info1.comment           = ldb_msg_find_attr_as_string(msgs[i], "comment", NULL);
554                 }
555                 break;
556         case 2:
557                 for (i = 0; i < count; i++) {
558                         info[i].info2.servername        = ldb_msg_find_attr_as_string(msgs[i], "servername", "");
559                         W_ERROR_HAVE_NO_MEMORY(info[i].info2.servername);
560
561                         info[i].info2.printername       = ldb_msg_find_attr_as_string(msgs[i], "printername", "");
562                         W_ERROR_HAVE_NO_MEMORY(info[i].info2.printername);
563
564                         info[i].info2.sharename         = ldb_msg_find_attr_as_string(msgs[i], "sharename", "");
565                         W_ERROR_HAVE_NO_MEMORY(info[i].info2.sharename);
566
567                         info[i].info2.portname          = ldb_msg_find_attr_as_string(msgs[i], "portname", "");
568                         W_ERROR_HAVE_NO_MEMORY(info[i].info2.portname);
569
570                         info[i].info2.drivername        = ldb_msg_find_attr_as_string(msgs[i], "drivername", "");
571                         W_ERROR_HAVE_NO_MEMORY(info[i].info2.drivername);
572
573                         info[i].info2.comment           = ldb_msg_find_attr_as_string(msgs[i], "comment", NULL);
574
575                         info[i].info2.location          = ldb_msg_find_attr_as_string(msgs[i], "location", NULL);
576
577                         info[i].info2.devmode           = NULL;
578
579                         info[i].info2.sepfile           = ldb_msg_find_attr_as_string(msgs[i], "sepfile", NULL);
580
581                         info[i].info2.printprocessor    = ldb_msg_find_attr_as_string(msgs[i], "printprocessor", "");
582                         W_ERROR_HAVE_NO_MEMORY(info[i].info2.printprocessor);
583
584                         info[i].info2.datatype          = ldb_msg_find_attr_as_string(msgs[i], "datatype", "");
585                         W_ERROR_HAVE_NO_MEMORY(info[i].info2.datatype);
586
587                         info[i].info2.parameters        = ldb_msg_find_attr_as_string(msgs[i], "parameters", NULL);
588
589                         info[i].info2.secdesc           = NULL;
590
591                         info[i].info2.attributes        = ldb_msg_find_attr_as_uint(msgs[i], "attributes", 0);
592                         info[i].info2.priority          = ldb_msg_find_attr_as_uint(msgs[i], "priority", 0);
593                         info[i].info2.defaultpriority   = ldb_msg_find_attr_as_uint(msgs[i], "defaultpriority", 0);
594                         info[i].info2.starttime         = ldb_msg_find_attr_as_uint(msgs[i], "starttime", 0);
595                         info[i].info2.untiltime         = ldb_msg_find_attr_as_uint(msgs[i], "untiltime", 0);
596                         info[i].info2.status            = ldb_msg_find_attr_as_uint(msgs[i], "status", 0);
597                         info[i].info2.cjobs             = ldb_msg_find_attr_as_uint(msgs[i], "cjobs", 0);
598                         info[i].info2.averageppm        = ldb_msg_find_attr_as_uint(msgs[i], "averageppm", 0);
599                 }
600                 break;
601         case 4:
602                 for (i = 0; i < count; i++) {
603                         info[i].info4.printername       = ldb_msg_find_attr_as_string(msgs[i], "printername", "");
604                         W_ERROR_HAVE_NO_MEMORY(info[i].info2.printername);
605
606                         info[i].info4.servername        = ldb_msg_find_attr_as_string(msgs[i], "servername", "");
607                         W_ERROR_HAVE_NO_MEMORY(info[i].info2.servername);
608
609                         info[i].info4.attributes        = ldb_msg_find_attr_as_uint(msgs[i], "attributes", 0);
610                 }
611                 break;
612         case 5:
613                 for (i = 0; i < count; i++) {
614                         info[i].info5.printername       = ldb_msg_find_attr_as_string(msgs[i], "name", "");
615                         W_ERROR_HAVE_NO_MEMORY(info[i].info5.printername);
616
617                         info[i].info5.portname          = ldb_msg_find_attr_as_string(msgs[i], "port", "");
618                         W_ERROR_HAVE_NO_MEMORY(info[i].info5.portname);
619
620                         info[i].info5.attributes        = ldb_msg_find_attr_as_uint(msgs[i], "attributes", 0);
621                         info[i].info5.device_not_selected_timeout = ldb_msg_find_attr_as_uint(msgs[i], "device_not_selected_timeout", 0);
622                         info[i].info5.transmission_retry_timeout  = ldb_msg_find_attr_as_uint(msgs[i], "transmission_retry_timeout", 0);
623                 }
624                 break;
625         default:
626                 return WERR_UNKNOWN_LEVEL;
627         }
628
629         *r->out.info    = info;
630         *r->out.count   = count;
631         return WERR_OK;
632 }
633
634 static WERROR sptr_OpenPrinter(struct ntptr_context *ntptr, TALLOC_CTX *mem_ctx,
635                                struct spoolss_OpenPrinterEx *r,
636                                const char *printer_name,
637                                struct ntptr_GenericHandle **printer)
638 {
639         return WERR_INVALID_PRINTER_NAME;
640 }
641
642 /* port functions */
643 static WERROR sptr_EnumPorts(struct ntptr_context *ntptr, TALLOC_CTX *mem_ctx,
644                              struct spoolss_EnumPorts *r)
645 {
646         struct ldb_context *sptr_db = talloc_get_type(ntptr->private_data, struct ldb_context);
647         struct ldb_message **msgs;
648         int count;
649         int i;
650         union spoolss_PortInfo *info;
651
652         count = sptr_db_search(sptr_db, mem_ctx, NULL, &msgs, NULL,
653                                "(&(objectclass=port))");
654
655         if (count == 0) return WERR_OK;
656         if (count < 0) return WERR_GENERAL_FAILURE;
657
658         info = talloc_array(mem_ctx, union spoolss_PortInfo, count);
659         W_ERROR_HAVE_NO_MEMORY(info);
660
661         switch (r->in.level) {
662         case 1:
663                 for (i = 0; i < count; i++) {
664                         info[i].info1.port_name         = ldb_msg_find_attr_as_string(msgs[i], "port-name", "");
665                         W_ERROR_HAVE_NO_MEMORY(info[i].info1.port_name);
666                 }
667                 break;
668         case 2:
669                 for (i=0; i < count; i++) {
670                         info[i].info2.port_name         = ldb_msg_find_attr_as_string(msgs[i], "port-name", "");
671                         W_ERROR_HAVE_NO_MEMORY(info[i].info2.port_name);
672
673                         info[i].info2.monitor_name      = ldb_msg_find_attr_as_string(msgs[i], "monitor-name", "");
674                         W_ERROR_HAVE_NO_MEMORY(info[i].info2.monitor_name);
675
676                         info[i].info2.description       = ldb_msg_find_attr_as_string(msgs[i], "description", "");
677                         W_ERROR_HAVE_NO_MEMORY(info[i].info2.description);
678
679                         info[i].info2.port_type         = ldb_msg_find_attr_as_uint(msgs[i], "port-type", SPOOLSS_PORT_TYPE_WRITE);
680                         info[i].info2.reserved          = ldb_msg_find_attr_as_uint(msgs[i], "reserved", 0);
681                 }
682                 break;
683         default:
684                 return WERR_UNKNOWN_LEVEL;
685         }
686
687         *r->out.info    = info;
688         *r->out.count   = count;
689         return WERR_OK;
690 }
691
692 /* monitor functions */
693 static WERROR sptr_EnumMonitors(struct ntptr_context *ntptr, TALLOC_CTX *mem_ctx,
694                                 struct spoolss_EnumMonitors *r)
695 {
696         struct ldb_context *sptr_db = talloc_get_type(ntptr->private_data, struct ldb_context);
697         struct ldb_message **msgs;
698         int count;
699         int i;
700         union spoolss_MonitorInfo *info;
701
702         count = sptr_db_search(sptr_db, mem_ctx, NULL, &msgs, NULL,
703                                "(&(objectclass=monitor))");
704
705         if (count == 0) return WERR_OK;
706         if (count < 0) return WERR_GENERAL_FAILURE;
707
708         info = talloc_array(mem_ctx, union spoolss_MonitorInfo, count);
709         W_ERROR_HAVE_NO_MEMORY(info);
710
711         switch (r->in.level) {
712         case 1:
713                 for (i = 0; i < count; i++) {
714                         info[i].info1.monitor_name      = ldb_msg_find_attr_as_string(msgs[i], "monitor-name", "");
715                         W_ERROR_HAVE_NO_MEMORY(info[i].info1.monitor_name);
716                 }
717                 break;
718         case 2:
719                 for (i=0; i < count; i++) {
720                         info[i].info2.monitor_name      = ldb_msg_find_attr_as_string(msgs[i], "monitor-name", "");
721                         W_ERROR_HAVE_NO_MEMORY(info[i].info2.monitor_name);
722
723                         info[i].info2.environment       = ldb_msg_find_attr_as_string(msgs[i], "environment", "");
724                         W_ERROR_HAVE_NO_MEMORY(info[i].info2.environment);
725
726                         info[i].info2.dll_name          = ldb_msg_find_attr_as_string(msgs[i], "dll-name", "");
727                         W_ERROR_HAVE_NO_MEMORY(info[i].info2.dll_name);
728                 }
729                 break;
730         default:
731                 return WERR_UNKNOWN_LEVEL;
732         }
733
734         *r->out.info    = info;
735         *r->out.count   = count;
736         return WERR_OK;
737 }
738
739 /* Printer Form functions */
740 static WERROR sptr_GetPrinterForm(struct ntptr_GenericHandle *printer, TALLOC_CTX *mem_ctx,
741                                   struct spoolss_GetForm *r)
742 {
743         struct ldb_context *sptr_db = talloc_get_type(printer->ntptr->private_data, struct ldb_context);
744         struct ldb_message **msgs;
745         struct ldb_dn *base_dn;
746         int count;
747         union spoolss_FormInfo *info;
748
749         /* TODO: do checks access here
750          * if (!(printer->access_mask & desired_access)) {
751          *      return WERR_FOOBAR;
752          * }
753          */
754
755         base_dn = ldb_dn_new_fmt(mem_ctx, sptr_db, "CN=Forms,CN=%s,CN=Printers", printer->object_name);
756         W_ERROR_HAVE_NO_MEMORY(base_dn);
757
758         count = sptr_db_search(sptr_db, mem_ctx, base_dn, &msgs, NULL,
759                                "(&(form-name=%s)(objectClass=form))",
760                                r->in.form_name);
761
762         if (count == 0) return WERR_FOOBAR;
763         if (count > 1) return WERR_FOOBAR;
764         if (count < 0) return WERR_GENERAL_FAILURE;
765
766         info = talloc(mem_ctx, union spoolss_FormInfo);
767         W_ERROR_HAVE_NO_MEMORY(info);
768
769         switch (r->in.level) {
770         case 1:
771                 info->info1.flags       = ldb_msg_find_attr_as_uint(msgs[0], "flags", SPOOLSS_FORM_BUILTIN);
772
773                 info->info1.form_name   = ldb_msg_find_attr_as_string(msgs[0], "form-name", NULL);
774                 W_ERROR_HAVE_NO_MEMORY(info->info1.form_name);
775
776                 info->info1.size.width  = ldb_msg_find_attr_as_uint(msgs[0], "size-width", 0);
777                 info->info1.size.height = ldb_msg_find_attr_as_uint(msgs[0], "size-height", 0);
778
779                 info->info1.area.left   = ldb_msg_find_attr_as_uint(msgs[0], "area-left", 0);
780                 info->info1.area.top    = ldb_msg_find_attr_as_uint(msgs[0], "area-top", 0);
781                 info->info1.area.right  = ldb_msg_find_attr_as_uint(msgs[0], "area-right", 0);
782                 info->info1.area.bottom = ldb_msg_find_attr_as_uint(msgs[0], "area-bottom", 0);
783                 break;
784         default:
785                 return WERR_UNKNOWN_LEVEL;
786         }
787
788         r->out.info     = info;
789         return WERR_OK;
790 }
791
792 static WERROR sptr_GetPrintProcessorDirectory(struct ntptr_context *ntptr, TALLOC_CTX *mem_ctx,
793                                               struct spoolss_GetPrintProcessorDirectory *r)
794 {
795         union spoolss_PrintProcessorDirectoryInfo *info;
796         const char *prefix;
797         const char *postfix;
798
799         /*
800          * NOTE: normally r->in.level is 1, but both w2k3 and nt4 sp6a
801          *        are ignoring the r->in.level completely, so we do :-)
802          */
803
804         /*
805          * TODO: check the server name is ours
806          * - if it's a invalid UNC then return WERR_INVALID_NAME
807          * - if it's the wrong host name return WERR_INVALID_PARAM
808          * - if it's "" then we need to return a local WINDOWS path
809          */
810         if (!r->in.server || !r->in.server[0]) {
811                 prefix = "C:\\PRTPROCS";
812         } else {
813                 prefix = talloc_asprintf(mem_ctx, "%s\\prnproc$", r->in.server);
814                 W_ERROR_HAVE_NO_MEMORY(prefix);
815         }
816
817         if (r->in.environment && strcmp(SPOOLSS_ARCHITECTURE_NT_X86, r->in.environment) == 0) {
818                 postfix = "W32X86";
819         } else {
820                 return WERR_INVALID_ENVIRONMENT;
821         }
822
823         info = talloc(mem_ctx, union spoolss_PrintProcessorDirectoryInfo);
824         W_ERROR_HAVE_NO_MEMORY(info);
825
826         info->info1.directory_name      = talloc_asprintf(mem_ctx, "%s\\%s", prefix, postfix);
827         W_ERROR_HAVE_NO_MEMORY(info->info1.directory_name);
828
829         r->out.info = info;
830         return WERR_OK;
831 }
832
833
834 /*
835   initialialise the simble ldb backend, registering ourselves with the ntptr subsystem
836  */
837 static const struct ntptr_ops ntptr_simple_ldb_ops = {
838         .name                           = "simple_ldb",
839         .init_context                   = sptr_init_context,
840
841         /* PrintServer functions */
842         .OpenPrintServer                = sptr_OpenPrintServer,
843 /*      .XcvDataPrintServer             = sptr_XcvDataPrintServer,
844 */
845         /* PrintServer PrinterData functions */
846 /*      .EnumPrintServerData            = sptr_EnumPrintServerData,
847 */      .GetPrintServerData             = sptr_GetPrintServerData,
848 /*      .SetPrintServerData             = sptr_SetPrintServerData,
849         .DeletePrintServerData          = sptr_DeletePrintServerData,
850 */
851         /* PrintServer Form functions */
852         .EnumPrintServerForms           = sptr_EnumPrintServerForms,
853         .AddPrintServerForm             = sptr_AddPrintServerForm,
854         .SetPrintServerForm             = sptr_SetPrintServerForm,
855         .DeletePrintServerForm          = sptr_DeletePrintServerForm,
856
857         /* PrintServer Driver functions */
858         .EnumPrinterDrivers             = sptr_EnumPrinterDrivers,
859 /*      .AddPrinterDriver               = sptr_AddPrinterDriver,
860         .DeletePrinterDriver            = sptr_DeletePrinterDriver,
861 */      .GetPrinterDriverDirectory      = sptr_GetPrinterDriverDirectory,
862
863         /* Port functions */
864         .EnumPorts                      = sptr_EnumPorts,
865 /*      .OpenPort                       = sptr_OpenPort,
866         .XcvDataPort                    = sptr_XcvDataPort,
867 */
868         /* Monitor functions */
869         .EnumMonitors                   = sptr_EnumMonitors,
870 /*      .OpenMonitor                    = sptr_OpenMonitor,
871         .XcvDataMonitor                 = sptr_XcvDataMonitor,
872 */
873         /* PrintProcessor functions */
874 /*      .EnumPrintProcessors            = sptr_EnumPrintProcessors,
875 */
876         .GetPrintProcessorDirectory     = sptr_GetPrintProcessorDirectory,
877
878         /* Printer functions */
879         .EnumPrinters                   = sptr_EnumPrinters,
880         .OpenPrinter                    = sptr_OpenPrinter,
881 /*      .AddPrinter                     = sptr_AddPrinter,
882         .GetPrinter                     = sptr_GetPrinter,
883         .SetPrinter                     = sptr_SetPrinter,
884         .DeletePrinter                  = sptr_DeletePrinter,
885         .XcvDataPrinter                 = sptr_XcvDataPrinter,
886 */
887         /* Printer Driver functions */
888 /*      .GetPrinterDriver               = sptr_GetPrinterDriver,
889 */
890         /* Printer PrinterData functions */
891 /*      .EnumPrinterData                = sptr_EnumPrinterData,
892         .GetPrinterData                 = sptr_GetPrinterData,
893         .SetPrinterData                 = sptr_SetPrinterData,
894         .DeletePrinterData              = sptr_DeletePrinterData,
895 */
896         /* Printer Form functions */
897 /*      .EnumPrinterForms               = sptr_EnumPrinterForms,
898         .AddPrinterForm                 = sptr_AddPrinterForm,
899 */      .GetPrinterForm                 = sptr_GetPrinterForm,
900 /*      .SetPrinterForm                 = sptr_SetPrinterForm,
901         .DeletePrinterForm              = sptr_DeletePrinterForm,
902 */
903         /* Printer Job functions */
904 /*      .EnumJobs                       = sptr_EnumJobs,
905         .AddJob                         = sptr_AddJob,
906         .ScheduleJob                    = sptr_ScheduleJob,
907         .GetJob                         = sptr_GetJob,
908         .SetJob                         = sptr_SetJob,
909 */
910         /* Printer Printing functions */
911 /*      .StartDocPrinter                = sptr_StartDocPrinter,
912         .EndDocPrinter                  = sptr_EndDocPrinter,
913         .StartPagePrinter               = sptr_StartPagePrinter,
914         .EndPagePrinter                 = sptr_EndPagePrinter,
915         .WritePrinter                   = sptr_WritePrinter,
916         .ReadPrinter                    = sptr_ReadPrinter,
917 */};
918
919 NTSTATUS ntptr_simple_ldb_init(void)
920 {
921         NTSTATUS ret;
922
923         ret = ntptr_register(&ntptr_simple_ldb_ops);
924         if (!NT_STATUS_IS_OK(ret)) {
925                 DEBUG(0,("Failed to register NTPTR '%s' backend!\n",
926                          ntptr_simple_ldb_ops.name));
927         }
928
929         return ret;
930 }