r1271: Return spoolss enumprinters info level1 from spoolss.ldb - woot!
[nivanova/samba-autobuild/.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
26 struct spoolssdb_context {
27         struct ldb_context *ldb;
28 };
29
30 /*
31   this is used to catch debug messages from ldb
32 */
33 void spoolssdb_debug(void *context, enum ldb_debug_level level, const char *fmt, va_list ap)
34 {
35         char *s = NULL;
36         if (DEBUGLEVEL < 4 && level > LDB_DEBUG_WARNING) {
37                 return;
38         }
39         vasprintf(&s, fmt, ap);
40         if (!s) return;
41         DEBUG(level, ("spoolssdb: %s\n", s));
42         free(s);
43 }
44
45 /*
46   connect to the spoolss database
47   return an opaque context pointer on success, or NULL on failure
48  */
49 void *spoolssdb_connect(void)
50 {
51         struct spoolssdb_context *ctx;
52         /*
53           the way that unix fcntl locking works forces us to have a
54           static ldb handle here rather than a much more sensible
55           approach of having the ldb handle as part of the
56           spoolss_OpenPrinter() pipe state. Otherwise we would try to open
57           the ldb more than once, and tdb would rightly refuse the
58           second open due to the broken nature of unix locking.
59         */
60         static struct ldb_context *static_spoolss_db;
61
62         if (static_spoolss_db == NULL) {
63                 static_spoolss_db = ldb_connect(lp_spoolss_url(), 0, NULL);
64                 if (static_spoolss_db == NULL) {
65                         return NULL;
66                 }
67         }
68
69         ldb_set_debug(static_spoolss_db, spoolssdb_debug, NULL);
70
71         ctx = malloc_p(struct spoolssdb_context);
72         if (!ctx) {
73                 errno = ENOMEM;
74                 return NULL;
75         }
76
77         ctx->ldb = static_spoolss_db;
78
79         return ctx;
80 }
81
82 /* close a connection to the spoolss db */
83 void spoolssdb_close(void *ctx)
84 {
85         struct spoolssdb_context *spoolss_ctx = ctx;
86         /* we don't actually close due to broken posix locking semantics */
87         spoolss_ctx->ldb = NULL;
88         free(spoolss_ctx);
89 }
90
91 /*
92   search the db for the specified attributes - varargs variant
93 */
94 int spoolssdb_search(void *ctx,
95                  TALLOC_CTX *mem_ctx, 
96                  const char *basedn,
97                  struct ldb_message ***res,
98                  const char * const *attrs,
99                  const char *format, ...) _PRINTF_ATTRIBUTE(6,7)
100 {
101         struct spoolssdb_context *spoolss_ctx = ctx;
102         va_list ap;
103         int count;
104
105         va_start(ap, format);
106         count = gendb_search_v(spoolss_ctx->ldb, mem_ctx, basedn, res, attrs, format, ap);
107         va_end(ap);
108
109         return count;
110 }
111