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