2 Unix SMB/CIFS implementation.
3 ads (active directory) printer utility library
4 Copyright (C) Jim McDonough 2002
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.
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.
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.
26 find a printer given the name and the hostname
27 Note that results "res" may be allocated on return so that the
28 results can be used. It should be freed using ads_msgfree.
30 ADS_STATUS ads_find_printer_on_server(ADS_STRUCT *ads, void **res,
31 char *printer, char *servername)
35 const char *attrs[] = {"*", "nTSecurityDescriptor", NULL};
37 status = ads_find_machine_acct(ads, res, servername);
38 if (!ADS_ERR_OK(status)) {
39 DEBUG(1, ("ads_add_printer: cannot find host %s in ads\n",
43 srv_dn = ldap_get_dn(ads->ld, *res);
44 ads_msgfree(ads, *res);
46 asprintf(&exp, "(printerName=%s)", printer);
47 status = ads_do_search(ads, srv_dn, LDAP_SCOPE_SUBTREE,
55 modify an entire printer entry in the directory
57 ADS_STATUS ads_mod_printer_entry(ADS_STRUCT *ads, char *prt_dn,
58 const ADS_PRINTER_ENTRY *prt)
63 /* allocate the list */
64 *mods = ads_init_mods();
66 /* add the attributes to the list - required ones first */
67 ads_mod_repl(mods, "printerName", prt->printerName);
68 ads_mod_repl(mods, "serverName", prt->serverName);
69 ads_mod_repl(mods, "shortServerName", prt->shortServerName);
70 ads_mod_repl(mods, "uNCName", prt->uNCName);
71 ads_mod_repl(mods, "versionNumber", prt->versionNumber);
73 /* now the optional ones */
74 ads_mod_repl_list(mods, "description", prt->description);
75 ads_mod_repl(mods, "assetNumber",prt->assetNumber);
76 ads_mod_repl(mods, "bytesPerMinute",prt->bytesPerMinute);
77 ads_mod_repl(mods, "defaultPriority",prt->defaultPriority);
78 ads_mod_repl(mods, "driverName", prt->driverName);
79 ads_mod_repl(mods, "driverVersion",prt->driverVersion);
80 ads_mod_repl(mods, "location", prt->location);
81 ads_mod_repl(mods, "operatingSystem",prt->operatingSystem);
82 ads_mod_repl(mods, "operatingSystemHotfix",prt->operatingSystemHotfix);
83 ads_mod_repl(mods, "operatingSystemServicePack",
84 prt->operatingSystemServicePack);
85 ads_mod_repl(mods, "operatingSystemVersion",
86 prt->operatingSystemVersion);
87 ads_mod_repl(mods, "physicalLocationObject",
88 prt->physicalLocationObject);
89 ads_mod_repl_list(mods, "portName", prt->portName);
90 ads_mod_repl(mods, "printStartTime", prt->printStartTime);
91 ads_mod_repl(mods, "printEndTime", prt->printEndTime);
92 ads_mod_repl_list(mods, "printBinNames", prt->printBinNames);
93 /*... and many others */
95 /* do the ldap modify */
96 status = ads_gen_mod(ads, prt_dn, *mods);
98 /* free mod list, mods, and values */
106 add a printer to the directory
108 static ADS_STATUS ads_add_printer_entry(ADS_STRUCT *ads, char *prt_dn,
109 const ADS_PRINTER_ENTRY *prt)
113 /* These are the fields a printQueue must contain */
114 status = ads_gen_add(ads, prt_dn,
115 "uNCName", prt->uNCName, NULL,
116 "versionNumber", prt->versionNumber, NULL,
117 "serverName", prt->serverName, NULL,
118 "shortServerName", prt->shortServerName, NULL,
119 "printerName", prt->printerName, NULL,
120 "objectClass", "printQueue", NULL,
127 publish a printer in the ADS
130 ADS_STATUS ads_add_printer(ADS_STRUCT *ads, const ADS_PRINTER_ENTRY *prt)
134 char *host_dn, *prt_dn;
135 const char *attrs[] = {"*", "nTSecurityDescriptor", NULL};
137 status = ads_find_machine_acct(ads, (void **)&res,
138 prt->shortServerName);
139 if (!ADS_ERR_OK(status)) {
140 DEBUG(1, ("ads_add_printer: cannot find host %s in ads\n",
141 prt->shortServerName));
144 host_dn = ldap_get_dn(ads->ld, res);
145 ads_msgfree(ads, res);
147 /* printer dn is cn=server-printer followed by host dn */
148 asprintf(&prt_dn, "cn=%s-%s,%s", prt->shortServerName,
149 prt->printerName, host_dn);
151 status = ads_search_dn(ads, &res, prt_dn, attrs);
153 if (ADS_ERR_OK(status) && ads_count_replies(ads, res)) {
154 DEBUG(1, ("ads_add_printer: printer %s already exists\n",
156 /* nothing to do, just free results */
157 ads_msgfree(ads, res);
159 ads_msgfree(ads, res);
160 status = ads_add_printer_entry(ads, prt_dn, prt);
161 if (!ADS_ERR_OK(status)) {
162 DEBUG(0, ("ads_add_printer: ads_add_printer_entry failed\n"));
167 status = ads_search_dn(ads, &res, prt_dn, attrs);
169 if (ADS_ERR_OK(status) && ads_count_replies(ads, res)) {
170 /* need to retrieve GUID from results
172 status = ads_mod_printer_entry(ads, prt_dn, prt);
175 ads_msgfree(ads, res);