r5503: - add torture test which tests for invalid printernames
[samba.git] / source4 / rpc_server / spoolss / spoolssdb.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    interface functions for the spoolss database
5
6    Copyright (C) Andrew Tridgell 2004
7    Copyright (C) Tim Potter 2004
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25 #include "lib/ldb/include/ldb.h"
26
27 struct spoolssdb_context {
28         struct ldb_context *ldb;
29 };
30
31 /*
32   this is used to catch debug messages from ldb
33 */
34 static void spoolssdb_debug(void *context, enum ldb_debug_level level, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(3, 0);
35 static void spoolssdb_debug(void *context, enum ldb_debug_level level, const char *fmt, va_list ap)
36 {
37         char *s = NULL;
38         if (DEBUGLEVEL < 4 && level > LDB_DEBUG_WARNING) {
39                 return;
40         }
41         vasprintf(&s, fmt, ap);
42         if (!s) return;
43         DEBUG(level, ("spoolssdb: %s\n", s));
44         free(s);
45 }
46
47 /*
48   connect to the spoolss database
49   return an opaque context pointer on success, or NULL on failure
50  */
51 void *spoolssdb_connect(void)
52 {
53         struct spoolssdb_context *ctx;
54         /*
55           the way that unix fcntl locking works forces us to have a
56           static ldb handle here rather than a much more sensible
57           approach of having the ldb handle as part of the
58           spoolss_OpenPrinter() pipe state. Otherwise we would try to open
59           the ldb more than once, and tdb would rightly refuse the
60           second open due to the broken nature of unix locking.
61         */
62         static struct ldb_context *static_spoolss_db;
63
64         if (static_spoolss_db == NULL) {
65                 static_spoolss_db = ldb_connect(lp_spoolss_url(), 0, NULL);
66                 if (static_spoolss_db == NULL) {
67                         return NULL;
68                 }
69         }
70
71         ldb_set_debug(static_spoolss_db, spoolssdb_debug, NULL);
72
73         ctx = malloc_p(struct spoolssdb_context);
74         if (!ctx) {
75                 errno = ENOMEM;
76                 return NULL;
77         }
78
79         ctx->ldb = static_spoolss_db;
80
81         return ctx;
82 }
83
84 /* close a connection to the spoolss db */
85 void spoolssdb_close(void *ctx)
86 {
87         struct spoolssdb_context *spoolss_ctx = ctx;
88         /* we don't actually close due to broken posix locking semantics */
89         spoolss_ctx->ldb = NULL;
90         free(spoolss_ctx);
91 }
92
93 /*
94   search the db for the specified attributes - varargs variant
95 */
96 int spoolssdb_search(void *ctx,
97                  TALLOC_CTX *mem_ctx, 
98                  const char *basedn,
99                  struct ldb_message ***res,
100                  const char * const *attrs,
101                  const char *format, ...) _PRINTF_ATTRIBUTE(6,7)
102 {
103         struct spoolssdb_context *spoolss_ctx = ctx;
104         va_list ap;
105         int count;
106
107         va_start(ap, format);
108         count = gendb_search_v(spoolss_ctx->ldb, mem_ctx, basedn, res, attrs, format, ap);
109         va_end(ap);
110
111         return count;
112 }
113