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