Fix file header description and copyright (from cut-and-paste laziness)
[bbaumbach/samba-autobuild/.git] / source3 / libads / ldap_printer.c
1 /* 
2    Unix SMB/CIFS implementation.
3    ads (active directory) printer utility library
4    Copyright (C) Jim McDonough 2002
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22
23 #ifdef HAVE_ADS
24
25 /*
26   modify an entire printer entry in the directory
27 */
28 ADS_STATUS ads_mod_printer_entry(ADS_STRUCT *ads, char *prt_dn,
29                                  const ADS_PRINTER_ENTRY *prt)
30 {
31         LDAPMod **mods;
32         ADS_STATUS status;
33
34         /* allocate the list */
35         mods = ads_mod_list_start(sizeof(prt) / sizeof(char *));
36
37         /* add the attributes to the list - required ones first */
38         ads_mod_repl(mods, "printerName", prt->printerName);
39         ads_mod_repl(mods, "serverName", prt->serverName);
40         ads_mod_repl(mods, "shortServerName", prt->shortServerName);
41         ads_mod_repl(mods, "uNCName", prt->uNCName);
42         ads_mod_repl(mods, "versionNumber", prt->versionNumber);
43         /* now the optional ones */
44         ads_mod_repl_list(mods, "description", prt->description);
45         ads_mod_repl(mods, "driverName", prt->driverName);
46         ads_mod_repl(mods, "location", prt->location);
47         ads_mod_repl_list(mods, "portName", prt->portName);
48         ads_mod_repl(mods, "printStartTime", prt->printStartTime);
49         ads_mod_repl(mods, "printEndTime", prt->printEndTime);
50         ads_mod_repl_list(mods, "printBinNames", prt->printBinNames);
51
52         /* and many others */
53
54         /* do the ldap modify */
55         status = ads_gen_mod(ads, prt_dn, mods);
56
57         /* free mod list, mods, and values */
58         ads_mod_list_end(mods); 
59
60         return status;
61 }
62         
63
64 /*
65   add a printer to the directory
66 */
67 static ADS_STATUS ads_add_printer_entry(ADS_STRUCT *ads, char *prt_dn,
68                                         const ADS_PRINTER_ENTRY *prt)
69 {
70         ADS_STATUS status;
71  
72         /* These are the fields a printQueue must contain */
73         status = ads_gen_add(ads, prt_dn,
74                              "uNCName", prt->uNCName, NULL,
75                              "versionNumber", prt->versionNumber, NULL,
76                              "serverName", prt->serverName, NULL,
77                              "shortServerName", prt->shortServerName, NULL,
78                              "printerName", prt->printerName, NULL,
79                              "objectClass", "printQueue", NULL,
80                              NULL);
81
82         return status;
83 }
84
85 /*
86   publish a printer in the ADS
87 */
88
89 ADS_STATUS ads_add_printer(ADS_STRUCT *ads, const ADS_PRINTER_ENTRY *prt)
90 {
91         ADS_STATUS status;
92         void *res;
93         char *host_dn, *prt_dn;
94         const char *attrs[] = {"*", "nTSecurityDescriptor", NULL};
95
96         status = ads_find_machine_acct(ads, (void **)&res, 
97                                        prt->shortServerName);
98         if (!ADS_ERR_OK(status)) {
99                 DEBUG(1, ("ads_add_printer: cannot find host %s in ads\n",
100                           prt->shortServerName));
101                 return status;
102         }
103         host_dn = ldap_get_dn(ads->ld, res);
104         ads_msgfree(ads, res);
105
106         /* printer dn is cn=server-printer followed by host dn */
107         asprintf(&prt_dn, "cn=%s-%s,%s", prt->shortServerName,
108                  prt->printerName, host_dn);
109
110         status = ads_search_dn(ads, res, prt_dn, attrs);
111
112         if (ADS_ERR_OK(status) && ads_count_replies(ads, res)) {
113                 DEBUG(1, ("ads_add_printer: printer %s already exists\n",
114                           prt->printerName));
115                 /* nothing to do, just free results */
116                 ads_msgfree(ads, res);
117         } else {
118                 ads_msgfree(ads, res);
119                 status = ads_add_printer_entry(ads, prt_dn, prt);
120                 if (!ADS_ERR_OK(status)) {
121                         DEBUG(0, ("ads_add_printer: ads_add_printer_entry failed\n"));
122                         return status;
123                 }
124         }
125
126         status = ads_mod_printer_entry(ads, prt_dn, prt);
127
128         return status;
129 }
130
131 #endif