2 * Unix SMB/CIFS implementation.
3 * RPC Pipe client / server routines
4 * Copyright (C) Andrew Tridgell 1992-2000,
5 * Copyright (C) Jean François Micouleau 1998-2000.
6 * Copyright (C) Gerald Carter 2002-2005.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, see <http://www.gnu.org/licenses/>.
23 #include "../librpc/gen_ndr/spoolss.h"
24 #include "rpc_server/srv_spoolss_util.h"
25 #include "nt_printing.h"
29 /*****************************************************************
30 ****************************************************************/
32 static void store_printer_guid(const char *printer, struct GUID guid)
35 struct auth_serversupplied_info *server_info = NULL;
41 tmp_ctx = talloc_new(NULL);
43 DEBUG(0, ("store_printer_guid: Out of memory?!\n"));
47 status = make_server_info_system(tmp_ctx, &server_info);
48 if (!NT_STATUS_IS_OK(status)) {
49 DEBUG(0, ("store_printer_guid: "
50 "Could not create system server_info\n"));
54 guid_str = GUID_string(tmp_ctx, &guid);
56 DEBUG(0, ("store_printer_guid: Out of memory?!\n"));
60 /* We used to store this as a REG_BINARY but that causes
63 if (!push_reg_sz(tmp_ctx, &blob, guid_str)) {
64 DEBUG(0, ("store_printer_guid: "
65 "Could not marshall string %s for objectGUID\n",
70 result = winreg_set_printer_dataex(tmp_ctx, server_info, printer,
71 SPOOL_DSSPOOLER_KEY, "objectGUID",
72 REG_SZ, blob.data, blob.length);
73 if (!W_ERROR_IS_OK(result)) {
74 DEBUG(0, ("store_printer_guid: "
75 "Failed to store GUID for printer %s\n", printer));
82 static WERROR nt_printer_publish_ads(ADS_STRUCT *ads,
83 struct spoolss_PrinterInfo2 *pinfo2)
87 char *prt_dn = NULL, *srv_dn, *srv_cn_0, *srv_cn_escaped, *sharename_escaped;
88 char *srv_dn_utf8, **srv_cn_utf8;
91 const char *attrs[] = {"objectGUID", NULL};
93 WERROR win_rc = WERR_OK;
94 size_t converted_size;
95 const char *printer = pinfo2->sharename;
97 /* build the ads mods */
98 ctx = talloc_init("nt_printer_publish_ads");
103 DEBUG(5, ("publishing printer %s\n", printer));
105 /* figure out where to publish */
106 ads_find_machine_acct(ads, &res, global_myname());
108 /* We use ldap_get_dn here as we need the answer
109 * in utf8 to call ldap_explode_dn(). JRA. */
111 srv_dn_utf8 = ldap_get_dn((LDAP *)ads->ldap.ld, (LDAPMessage *)res);
114 return WERR_SERVER_UNAVAILABLE;
116 ads_msgfree(ads, res);
117 srv_cn_utf8 = ldap_explode_dn(srv_dn_utf8, 1);
120 ldap_memfree(srv_dn_utf8);
121 return WERR_SERVER_UNAVAILABLE;
123 /* Now convert to CH_UNIX. */
124 if (!pull_utf8_talloc(ctx, &srv_dn, srv_dn_utf8, &converted_size)) {
126 ldap_memfree(srv_dn_utf8);
127 ldap_memfree(srv_cn_utf8);
128 return WERR_SERVER_UNAVAILABLE;
130 if (!pull_utf8_talloc(ctx, &srv_cn_0, srv_cn_utf8[0], &converted_size)) {
132 ldap_memfree(srv_dn_utf8);
133 ldap_memfree(srv_cn_utf8);
135 return WERR_SERVER_UNAVAILABLE;
138 ldap_memfree(srv_dn_utf8);
139 ldap_memfree(srv_cn_utf8);
141 srv_cn_escaped = escape_rdn_val_string_alloc(srv_cn_0);
142 if (!srv_cn_escaped) {
144 return WERR_SERVER_UNAVAILABLE;
146 sharename_escaped = escape_rdn_val_string_alloc(printer);
147 if (!sharename_escaped) {
148 SAFE_FREE(srv_cn_escaped);
150 return WERR_SERVER_UNAVAILABLE;
153 prt_dn = talloc_asprintf(ctx, "cn=%s-%s,%s", srv_cn_escaped, sharename_escaped, srv_dn);
155 SAFE_FREE(srv_cn_escaped);
156 SAFE_FREE(sharename_escaped);
158 mods = ads_init_mods(ctx);
166 ads_mod_str(ctx, &mods, SPOOL_REG_PRINTERNAME, printer);
169 ads_rc = ads_mod_printer_entry(ads, prt_dn, ctx, &mods);
170 if (ads_rc.err.rc == LDAP_NO_SUCH_OBJECT) {
172 for (i=0; mods[i] != 0; i++)
174 mods[i] = (LDAPMod *)-1;
175 ads_rc = ads_add_printer_entry(ads, prt_dn, ctx, &mods);
178 if (!ADS_ERR_OK(ads_rc)) {
179 DEBUG(3, ("error publishing %s: %s\n",
180 printer, ads_errstr(ads_rc)));
183 /* retreive the guid and store it locally */
184 if (ADS_ERR_OK(ads_search_dn(ads, &res, prt_dn, attrs))) {
186 ads_pull_guid(ads, res, &guid);
187 ads_msgfree(ads, res);
188 store_printer_guid(printer, guid);
195 static WERROR nt_printer_unpublish_ads(ADS_STRUCT *ads,
199 LDAPMessage *res = NULL;
202 DEBUG(5, ("unpublishing printer %s\n", printer));
204 /* remove the printer from the directory */
205 ads_rc = ads_find_printer_on_server(ads, &res,
206 printer, global_myname());
208 if (ADS_ERR_OK(ads_rc) && res && ads_count_replies(ads, res)) {
209 prt_dn = ads_get_dn(ads, talloc_tos(), res);
211 ads_msgfree(ads, res);
214 ads_rc = ads_del_dn(ads, prt_dn);
219 ads_msgfree(ads, res);
224 /****************************************************************************
225 * Publish a printer in the directory
227 * @param mem_ctx memory context
228 * @param server_info server_info to access winreg pipe
229 * @param pinfo2 printer information
230 * @param action publish/unpublish action
231 * @return WERROR indicating status of publishing
232 ***************************************************************************/
234 WERROR nt_printer_publish(TALLOC_CTX *mem_ctx,
235 struct auth_serversupplied_info *server_info,
236 struct spoolss_PrinterInfo2 *pinfo2,
239 uint32_t info2_mask = SPOOLSS_PRINTER_INFO_ATTRIBUTES;
240 struct spoolss_SetPrinterInfo2 *sinfo2;
242 ADS_STRUCT *ads = NULL;
245 sinfo2 = talloc_zero(mem_ctx, struct spoolss_SetPrinterInfo2);
251 case DSPRINT_PUBLISH:
253 pinfo2->attributes |= PRINTER_ATTRIBUTE_PUBLISHED;
255 case DSPRINT_UNPUBLISH:
256 pinfo2->attributes ^= PRINTER_ATTRIBUTE_PUBLISHED;
259 win_rc = WERR_NOT_SUPPORTED;
263 sinfo2->attributes = pinfo2->attributes;
265 win_rc = winreg_update_printer(mem_ctx, server_info,
266 pinfo2->sharename, info2_mask,
268 if (!W_ERROR_IS_OK(win_rc)) {
269 DEBUG(3, ("err %d saving data\n", W_ERROR_V(win_rc)));
275 ads = ads_init(lp_realm(), lp_workgroup(), NULL);
277 DEBUG(3, ("ads_init() failed\n"));
278 win_rc = WERR_SERVER_UNAVAILABLE;
281 setenv(KRB5_ENV_CCNAME, "MEMORY:prtpub_cache", 1);
282 SAFE_FREE(ads->auth.password);
283 ads->auth.password = secrets_fetch_machine_password(lp_workgroup(),
286 /* ads_connect() will find the DC for us */
287 ads_rc = ads_connect(ads);
288 if (!ADS_ERR_OK(ads_rc)) {
289 DEBUG(3, ("ads_connect failed: %s\n", ads_errstr(ads_rc)));
290 win_rc = WERR_ACCESS_DENIED;
295 case DSPRINT_PUBLISH:
297 win_rc = nt_printer_publish_ads(ads, pinfo2);
299 case DSPRINT_UNPUBLISH:
300 win_rc = nt_printer_unpublish_ads(ads, pinfo2->sharename);
309 WERROR check_published_printers(void)
312 ADS_STRUCT *ads = NULL;
314 int n_services = lp_numservices();
315 TALLOC_CTX *tmp_ctx = NULL;
316 struct auth_serversupplied_info *server_info = NULL;
317 struct spoolss_PrinterInfo2 *pinfo2;
321 tmp_ctx = talloc_new(NULL);
322 if (!tmp_ctx) return WERR_NOMEM;
324 ads = ads_init(lp_realm(), lp_workgroup(), NULL);
326 DEBUG(3, ("ads_init() failed\n"));
327 return WERR_SERVER_UNAVAILABLE;
329 setenv(KRB5_ENV_CCNAME, "MEMORY:prtpub_cache", 1);
330 SAFE_FREE(ads->auth.password);
331 ads->auth.password = secrets_fetch_machine_password(lp_workgroup(),
334 /* ads_connect() will find the DC for us */
335 ads_rc = ads_connect(ads);
336 if (!ADS_ERR_OK(ads_rc)) {
337 DEBUG(3, ("ads_connect failed: %s\n", ads_errstr(ads_rc)));
338 result = WERR_ACCESS_DENIED;
342 status = make_server_info_system(tmp_ctx, &server_info);
343 if (!NT_STATUS_IS_OK(status)) {
344 DEBUG(0, ("check_published_printers: "
345 "Could not create system server_info\n"));
346 result = WERR_ACCESS_DENIED;
350 for (snum = 0; snum < n_services; snum++) {
351 if (!lp_snum_ok(snum) || !lp_print_ok(snum)) {
355 result = winreg_get_printer(tmp_ctx, server_info, NULL,
356 lp_servicename(snum), &pinfo2);
357 if (!W_ERROR_IS_OK(result)) {
361 if (pinfo2->attributes & PRINTER_ATTRIBUTE_PUBLISHED) {
362 nt_printer_publish_ads(ads, pinfo2);
371 ads_kdestroy("MEMORY:prtpub_cache");
372 talloc_free(tmp_ctx);
376 bool is_printer_published(TALLOC_CTX *mem_ctx,
377 struct auth_serversupplied_info *server_info,
378 char *servername, char *printer, struct GUID *guid,
379 struct spoolss_PrinterInfo2 **info2)
381 struct spoolss_PrinterInfo2 *pinfo2 = NULL;
382 enum winreg_Type type;
388 result = winreg_get_printer(mem_ctx, server_info,
389 servername, printer, &pinfo2);
390 if (!W_ERROR_IS_OK(result)) {
394 if (!(pinfo2->attributes & PRINTER_ATTRIBUTE_PUBLISHED)) {
403 /* fetching printer guids really ought to be a separate function. */
405 result = winreg_get_printer_dataex(mem_ctx, server_info, printer,
406 SPOOL_DSSPOOLER_KEY, "objectGUID",
407 &type, &data, &data_size);
408 if (!W_ERROR_IS_OK(result)) {
413 /* We used to store the guid as REG_BINARY, then swapped
414 to REG_SZ for Vista compatibility so check for both */
418 status = GUID_from_string((char *)data, guid);
419 if (!NT_STATUS_IS_OK(status)) {
426 if (data_size != sizeof(struct GUID)) {
430 memcpy(guid, data, sizeof(struct GUID));
433 DEBUG(0,("is_printer_published: GUID value stored as "
434 "invaluid type (%d)\n", type));
440 *info2 = talloc_move(mem_ctx, &pinfo2);
446 WERROR nt_printer_publish(TALLOC_CTX *mem_ctx,
447 struct auth_serversupplied_info *server_info,
448 struct spoolss_PrinterInfo2 *pinfo2,
454 WERROR check_published_printers(void)
459 bool is_printer_published(TALLOC_CTX *mem_ctx,
460 struct auth_serversupplied_info *server_info,
461 char *servername, char *printer, struct GUID *guid,
462 struct spoolss_PrinterInfo2 **info2)
466 #endif /* HAVE_ADS */