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 const char *printer, const char *servername)
34 char *srv_dn, **srv_cn, *exp;
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 srv_cn = ldap_explode_dn(srv_dn, 1);
45 ads_msgfree(ads, *res);
47 asprintf(&exp, "(cn=%s-%s)", srv_cn[0], printer);
48 status = ads_search(ads, res, exp, attrs);
51 ldap_value_free(srv_cn);
57 modify an entire printer entry in the directory
59 ADS_STATUS ads_mod_printer_entry(ADS_STRUCT *ads, char *prt_dn,
60 const ADS_PRINTER_ENTRY *prt)
66 if (!(ctx = talloc_init_named("mod_printer_entry")))
67 return ADS_ERROR(LDAP_NO_MEMORY);
69 /* allocate the list */
70 mods = ads_init_mods(ctx);
72 /* add the attributes to the list - required ones first */
73 ads_mod_str(ctx, &mods, "printerName", prt->printerName);
74 ads_mod_str(ctx, &mods, "serverName", prt->serverName);
75 ads_mod_str(ctx, &mods, "shortServerName", prt->shortServerName);
76 ads_mod_str(ctx, &mods, "uNCName", prt->uNCName);
77 ads_mod_str(ctx, &mods, "versionNumber", prt->versionNumber);
79 /* now the optional ones */
80 ads_mod_strlist(ctx, &mods, "description", (const char **)prt->description);
81 ads_mod_str(ctx, &mods, "assetNumber",prt->assetNumber);
82 ads_mod_str(ctx, &mods, "bytesPerMinute",prt->bytesPerMinute);
83 ads_mod_str(ctx, &mods, "defaultPriority",prt->defaultPriority);
84 ads_mod_str(ctx, &mods, "driverName", prt->driverName);
85 ads_mod_str(ctx, &mods, "driverVersion",prt->driverVersion);
86 ads_mod_str(ctx, &mods, "location", prt->location);
87 ads_mod_str(ctx, &mods, "operatingSystem",prt->operatingSystem);
88 ads_mod_str(ctx, &mods, "operatingSystemHotfix",
89 prt->operatingSystemHotfix);
90 ads_mod_str(ctx, &mods, "operatingSystemServicePack",
91 prt->operatingSystemServicePack);
92 ads_mod_str(ctx, &mods, "operatingSystemVersion",
93 prt->operatingSystemVersion);
94 ads_mod_str(ctx, &mods, "physicalLocationObject",
95 prt->physicalLocationObject);
96 ads_mod_strlist(ctx, &mods, "portName", (const char **)prt->portName);
97 ads_mod_str(ctx, &mods, "printStartTime", prt->printStartTime);
98 ads_mod_str(ctx, &mods, "printEndTime", prt->printEndTime);
99 ads_mod_strlist(ctx, &mods, "printBinNames", (const char **)prt->printBinNames);
100 /*... and many others */
102 /* do the ldap modify */
103 status = ads_gen_mod(ads, prt_dn, mods);
105 /* free mod list, mods, and values */
112 add a printer to the directory
114 static ADS_STATUS ads_add_printer_entry(ADS_STRUCT *ads, char *prt_dn,
115 const ADS_PRINTER_ENTRY *prt)
121 if (!(ctx = talloc_init_named("add_printer_entry")))
122 return ADS_ERROR(LDAP_NO_MEMORY);
124 if (!(mods = ads_init_mods(ctx)))
125 return ADS_ERROR(LDAP_NO_MEMORY);
127 /* These are the fields a printQueue must contain */
128 ads_mod_str(ctx, &mods, "uNCName", prt->uNCName);
129 ads_mod_str(ctx, &mods, "versionNumber", prt->versionNumber);
130 ads_mod_str(ctx, &mods, "serverName", prt->serverName);
131 ads_mod_str(ctx, &mods, "shortServerName", prt->shortServerName);
132 ads_mod_str(ctx, &mods, "printerName", prt->printerName);
133 ads_mod_str(ctx, &mods, "objectClass", "printQueue");
136 status = ads_gen_add(ads, prt_dn, mods);
144 publish a printer in the ADS
147 ADS_STATUS ads_add_printer(ADS_STRUCT *ads, const ADS_PRINTER_ENTRY *prt)
151 char *host_dn, *prt_dn;
152 const char *attrs[] = {"*", "nTSecurityDescriptor", NULL};
154 status = ads_find_machine_acct(ads, (void **)&res,
155 prt->shortServerName);
156 if (!ADS_ERR_OK(status)) {
157 DEBUG(1, ("ads_add_printer: cannot find host %s in ads\n",
158 prt->shortServerName));
161 host_dn = ads_get_dn(ads, res);
162 ads_msgfree(ads, res);
164 ads_find_printer_on_server(ads, &res, prt->printerName,
165 prt->shortServerName);
167 if (ADS_ERR_OK(status) && ads_count_replies(ads, res)) {
168 DEBUG(1, ("ads_add_printer: printer %s already exists\n",
170 /* nothing to do, just free results */
171 ads_msgfree(ads, res);
173 ads_msgfree(ads, res);
174 status = ads_add_printer_entry(ads, prt_dn, prt);
175 if (!ADS_ERR_OK(status)) {
176 DEBUG(0, ("ads_add_printer: ads_add_printer_entry failed\n"));
181 status = ads_search_dn(ads, &res, prt_dn, attrs);
183 if (ADS_ERR_OK(status) && ads_count_replies(ads, res)) {
184 /* need to retrieve GUID from results
186 status = ads_mod_printer_entry(ads, prt_dn, prt);
189 ads_msgfree(ads, res);