script/autobuild.py: build 'samba' using --picky-developer
[Samba.git] / source3 / printing / nt_printing_ads.c
blob5a5a17751d844d97e0985f595a74dcd58e7677d9
1 /*
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/>.
22 #include "includes.h"
23 #include "../librpc/gen_ndr/spoolss.h"
24 #include "rpc_server/spoolss/srv_spoolss_util.h"
25 #include "nt_printing.h"
26 #include "ads.h"
27 #include "secrets.h"
28 #include "krb5_env.h"
29 #include "../libcli/registry/util_reg.h"
30 #include "auth.h"
31 #include "../librpc/ndr/libndr.h"
32 #include "rpc_client/cli_winreg_spoolss.h"
34 #ifdef HAVE_ADS
35 /*****************************************************************
36 ****************************************************************/
38 static void store_printer_guid(struct messaging_context *msg_ctx,
39 const char *printer, struct GUID guid)
41 TALLOC_CTX *tmp_ctx;
42 struct auth_session_info *session_info = NULL;
43 const char *guid_str;
44 DATA_BLOB blob;
45 NTSTATUS status;
46 WERROR result;
48 tmp_ctx = talloc_new(NULL);
49 if (!tmp_ctx) {
50 DEBUG(0, ("store_printer_guid: Out of memory?!\n"));
51 return;
54 status = make_session_info_system(tmp_ctx, &session_info);
55 if (!NT_STATUS_IS_OK(status)) {
56 DEBUG(0, ("store_printer_guid: "
57 "Could not create system session_info\n"));
58 goto done;
61 guid_str = GUID_string(tmp_ctx, &guid);
62 if (!guid_str) {
63 DEBUG(0, ("store_printer_guid: Out of memory?!\n"));
64 goto done;
67 /* We used to store this as a REG_BINARY but that causes
68 Vista to whine */
70 if (!push_reg_sz(tmp_ctx, &blob, guid_str)) {
71 DEBUG(0, ("store_printer_guid: "
72 "Could not marshall string %s for objectGUID\n",
73 guid_str));
74 goto done;
77 result = winreg_set_printer_dataex_internal(tmp_ctx, session_info, msg_ctx,
78 printer,
79 SPOOL_DSSPOOLER_KEY, "objectGUID",
80 REG_SZ, blob.data, blob.length);
81 if (!W_ERROR_IS_OK(result)) {
82 DEBUG(0, ("store_printer_guid: "
83 "Failed to store GUID for printer %s\n", printer));
86 done:
87 talloc_free(tmp_ctx);
90 WERROR nt_printer_guid_get(TALLOC_CTX *mem_ctx,
91 const struct auth_session_info *session_info,
92 struct messaging_context *msg_ctx,
93 const char *printer, struct GUID *guid)
95 TALLOC_CTX *tmp_ctx;
96 enum winreg_Type type;
97 DATA_BLOB blob;
98 uint32_t len;
99 NTSTATUS status;
100 WERROR result;
102 tmp_ctx = talloc_new(mem_ctx);
103 if (tmp_ctx == NULL) {
104 DEBUG(0, ("out of memory?!\n"));
105 return WERR_NOMEM;
108 result = winreg_get_printer_dataex_internal(tmp_ctx, session_info,
109 msg_ctx, printer,
110 SPOOL_DSSPOOLER_KEY,
111 "objectGUID",
112 &type,
113 &blob.data,
114 &len);
115 if (!W_ERROR_IS_OK(result)) {
116 DEBUG(0, ("Failed to get GUID for printer %s\n", printer));
117 goto out_ctx_free;
119 blob.length = (size_t)len;
121 /* We used to store the guid as REG_BINARY, then swapped
122 to REG_SZ for Vista compatibility so check for both */
124 switch (type) {
125 case REG_SZ: {
126 bool ok;
127 const char *guid_str;
128 ok = pull_reg_sz(tmp_ctx, &blob, &guid_str);
129 if (!ok) {
130 DEBUG(0, ("Failed to unmarshall GUID for printer %s\n",
131 printer));
132 result = WERR_REG_CORRUPT;
133 goto out_ctx_free;
135 status = GUID_from_string(guid_str, guid);
136 if (!NT_STATUS_IS_OK(status)) {
137 DEBUG(0, ("bad GUID for printer %s\n", printer));
138 result = ntstatus_to_werror(status);
139 goto out_ctx_free;
141 break;
143 case REG_BINARY:
144 if (blob.length != sizeof(struct GUID)) {
145 DEBUG(0, ("bad GUID for printer %s\n", printer));
146 result = WERR_REG_CORRUPT;
147 goto out_ctx_free;
149 memcpy(guid, blob.data, sizeof(struct GUID));
150 break;
151 default:
152 DEBUG(0,("GUID value stored as invalid type (%d)\n", type));
153 result = WERR_REG_CORRUPT;
154 goto out_ctx_free;
155 break;
157 result = WERR_OK;
159 out_ctx_free:
160 talloc_free(tmp_ctx);
161 return result;
164 static WERROR nt_printer_info_to_mods(TALLOC_CTX *ctx,
165 struct spoolss_PrinterInfo2 *info2,
166 ADS_MODLIST *mods)
168 char *info_str;
170 ads_mod_str(ctx, mods, SPOOL_REG_PRINTERNAME, info2->sharename);
171 ads_mod_str(ctx, mods, SPOOL_REG_SHORTSERVERNAME, lp_netbios_name());
172 ads_mod_str(ctx, mods, SPOOL_REG_SERVERNAME, get_mydnsfullname());
174 info_str = talloc_asprintf(ctx, "\\\\%s\\%s",
175 get_mydnsfullname(), info2->sharename);
176 if (info_str == NULL) {
177 return WERR_NOMEM;
179 ads_mod_str(ctx, mods, SPOOL_REG_UNCNAME, info_str);
181 info_str = talloc_asprintf(ctx, "%d", 4);
182 if (info_str == NULL) {
183 return WERR_NOMEM;
185 ads_mod_str(ctx, mods, SPOOL_REG_VERSIONNUMBER, info_str);
187 /* empty strings in the mods list result in an attrubute error */
188 if (strlen(info2->drivername) != 0)
189 ads_mod_str(ctx, mods, SPOOL_REG_DRIVERNAME, info2->drivername);
190 if (strlen(info2->location) != 0)
191 ads_mod_str(ctx, mods, SPOOL_REG_LOCATION, info2->location);
192 if (strlen(info2->comment) != 0)
193 ads_mod_str(ctx, mods, SPOOL_REG_DESCRIPTION, info2->comment);
194 if (strlen(info2->portname) != 0)
195 ads_mod_str(ctx, mods, SPOOL_REG_PORTNAME, info2->portname);
196 if (strlen(info2->sepfile) != 0)
197 ads_mod_str(ctx, mods, SPOOL_REG_PRINTSEPARATORFILE, info2->sepfile);
199 info_str = talloc_asprintf(ctx, "%u", info2->starttime);
200 if (info_str == NULL) {
201 return WERR_NOMEM;
203 ads_mod_str(ctx, mods, SPOOL_REG_PRINTSTARTTIME, info_str);
205 info_str = talloc_asprintf(ctx, "%u", info2->untiltime);
206 if (info_str == NULL) {
207 return WERR_NOMEM;
209 ads_mod_str(ctx, mods, SPOOL_REG_PRINTENDTIME, info_str);
211 info_str = talloc_asprintf(ctx, "%u", info2->priority);
212 if (info_str == NULL) {
213 return WERR_NOMEM;
215 ads_mod_str(ctx, mods, SPOOL_REG_PRIORITY, info_str);
217 if (info2->attributes & PRINTER_ATTRIBUTE_KEEPPRINTEDJOBS) {
218 ads_mod_str(ctx, mods, SPOOL_REG_PRINTKEEPPRINTEDJOBS, "TRUE");
219 } else {
220 ads_mod_str(ctx, mods, SPOOL_REG_PRINTKEEPPRINTEDJOBS, "FALSE");
223 switch (info2->attributes & 0x3) {
224 case 0:
225 ads_mod_str(ctx, mods, SPOOL_REG_PRINTSPOOLING,
226 SPOOL_REGVAL_PRINTWHILESPOOLING);
227 break;
228 case 1:
229 ads_mod_str(ctx, mods, SPOOL_REG_PRINTSPOOLING,
230 SPOOL_REGVAL_PRINTAFTERSPOOLED);
231 break;
232 case 2:
233 ads_mod_str(ctx, mods, SPOOL_REG_PRINTSPOOLING,
234 SPOOL_REGVAL_PRINTDIRECT);
235 break;
236 default:
237 DEBUG(3, ("unsupported printer attributes %x\n",
238 info2->attributes));
241 return WERR_OK;
244 static WERROR nt_printer_publish_ads(struct messaging_context *msg_ctx,
245 ADS_STRUCT *ads,
246 struct spoolss_PrinterInfo2 *pinfo2)
248 ADS_STATUS ads_rc;
249 LDAPMessage *res;
250 char *prt_dn = NULL, *srv_dn, *srv_cn_0, *srv_cn_escaped, *sharename_escaped;
251 char *srv_dn_utf8, **srv_cn_utf8;
252 TALLOC_CTX *ctx;
253 ADS_MODLIST mods;
254 const char *attrs[] = {"objectGUID", NULL};
255 struct GUID guid;
256 WERROR win_rc = WERR_OK;
257 size_t converted_size;
258 const char *printer = pinfo2->sharename;
260 /* build the ads mods */
261 ctx = talloc_init("nt_printer_publish_ads");
262 if (ctx == NULL) {
263 return WERR_NOMEM;
266 DEBUG(5, ("publishing printer %s\n", printer));
268 /* figure out where to publish */
269 ads_rc = ads_find_machine_acct(ads, &res, lp_netbios_name());
270 if (!ADS_ERR_OK(ads_rc)) {
271 DEBUG(0, ("failed to find machine account for %s\n",
272 lp_netbios_name()));
273 TALLOC_FREE(ctx);
274 return WERR_NOT_FOUND;
277 /* We use ldap_get_dn here as we need the answer
278 * in utf8 to call ldap_explode_dn(). JRA. */
280 srv_dn_utf8 = ldap_get_dn((LDAP *)ads->ldap.ld, (LDAPMessage *)res);
281 ads_msgfree(ads, res);
282 if (!srv_dn_utf8) {
283 TALLOC_FREE(ctx);
284 return WERR_SERVER_UNAVAILABLE;
286 srv_cn_utf8 = ldap_explode_dn(srv_dn_utf8, 1);
287 if (!srv_cn_utf8) {
288 TALLOC_FREE(ctx);
289 ldap_memfree(srv_dn_utf8);
290 return WERR_SERVER_UNAVAILABLE;
292 /* Now convert to CH_UNIX. */
293 if (!pull_utf8_talloc(ctx, &srv_dn, srv_dn_utf8, &converted_size)) {
294 TALLOC_FREE(ctx);
295 ldap_memfree(srv_dn_utf8);
296 ldap_memfree(srv_cn_utf8);
297 return WERR_SERVER_UNAVAILABLE;
299 if (!pull_utf8_talloc(ctx, &srv_cn_0, srv_cn_utf8[0], &converted_size)) {
300 TALLOC_FREE(ctx);
301 ldap_memfree(srv_dn_utf8);
302 ldap_memfree(srv_cn_utf8);
303 TALLOC_FREE(srv_dn);
304 return WERR_SERVER_UNAVAILABLE;
307 ldap_memfree(srv_dn_utf8);
308 ldap_memfree(srv_cn_utf8);
310 srv_cn_escaped = escape_rdn_val_string_alloc(srv_cn_0);
311 if (!srv_cn_escaped) {
312 TALLOC_FREE(ctx);
313 return WERR_SERVER_UNAVAILABLE;
315 sharename_escaped = escape_rdn_val_string_alloc(printer);
316 if (!sharename_escaped) {
317 SAFE_FREE(srv_cn_escaped);
318 TALLOC_FREE(ctx);
319 return WERR_SERVER_UNAVAILABLE;
322 prt_dn = talloc_asprintf(ctx, "cn=%s-%s,%s", srv_cn_escaped, sharename_escaped, srv_dn);
324 SAFE_FREE(srv_cn_escaped);
325 SAFE_FREE(sharename_escaped);
327 mods = ads_init_mods(ctx);
329 if (mods == NULL) {
330 TALLOC_FREE(ctx);
331 return WERR_NOMEM;
334 win_rc = nt_printer_info_to_mods(ctx, pinfo2, &mods);
335 if (!W_ERROR_IS_OK(win_rc)) {
336 TALLOC_FREE(ctx);
337 return win_rc;
340 /* publish it */
341 ads_rc = ads_mod_printer_entry(ads, prt_dn, ctx, &mods);
342 if (ads_rc.err.rc == LDAP_NO_SUCH_OBJECT) {
343 int i;
344 for (i=0; mods[i] != 0; i++)
346 mods[i] = (LDAPMod *)-1;
347 ads_rc = ads_add_printer_entry(ads, prt_dn, ctx, &mods);
350 if (!ADS_ERR_OK(ads_rc)) {
351 DEBUG(3, ("error publishing %s: %s\n",
352 printer, ads_errstr(ads_rc)));
355 /* retreive the guid and store it locally */
356 if (ADS_ERR_OK(ads_search_dn(ads, &res, prt_dn, attrs))) {
357 bool guid_ok;
358 ZERO_STRUCT(guid);
359 guid_ok = ads_pull_guid(ads, res, &guid);
360 ads_msgfree(ads, res);
361 if (guid_ok) {
362 store_printer_guid(msg_ctx, printer, guid);
365 TALLOC_FREE(ctx);
367 return win_rc;
370 static WERROR nt_printer_unpublish_ads(ADS_STRUCT *ads,
371 const char *printer)
373 ADS_STATUS ads_rc;
374 LDAPMessage *res = NULL;
375 char *prt_dn = NULL;
377 DEBUG(5, ("unpublishing printer %s\n", printer));
379 /* remove the printer from the directory */
380 ads_rc = ads_find_printer_on_server(ads, &res,
381 printer, lp_netbios_name());
383 if (ADS_ERR_OK(ads_rc) && res && ads_count_replies(ads, res)) {
384 prt_dn = ads_get_dn(ads, talloc_tos(), res);
385 if (!prt_dn) {
386 ads_msgfree(ads, res);
387 return WERR_NOMEM;
389 ads_rc = ads_del_dn(ads, prt_dn);
390 TALLOC_FREE(prt_dn);
393 if (res) {
394 ads_msgfree(ads, res);
396 return WERR_OK;
399 /****************************************************************************
400 * Publish a printer in the directory
402 * @param mem_ctx memory context
403 * @param session_info session_info to access winreg pipe
404 * @param pinfo2 printer information
405 * @param action publish/unpublish action
406 * @return WERROR indicating status of publishing
407 ***************************************************************************/
409 WERROR nt_printer_publish(TALLOC_CTX *mem_ctx,
410 const struct auth_session_info *session_info,
411 struct messaging_context *msg_ctx,
412 struct spoolss_PrinterInfo2 *pinfo2,
413 int action)
415 uint32_t info2_mask = SPOOLSS_PRINTER_INFO_ATTRIBUTES;
416 struct spoolss_SetPrinterInfo2 *sinfo2;
417 ADS_STATUS ads_rc;
418 ADS_STRUCT *ads = NULL;
419 WERROR win_rc;
420 char *old_krb5ccname = NULL;
422 sinfo2 = talloc_zero(mem_ctx, struct spoolss_SetPrinterInfo2);
423 if (!sinfo2) {
424 return WERR_NOMEM;
427 switch (action) {
428 case DSPRINT_PUBLISH:
429 case DSPRINT_UPDATE:
430 pinfo2->attributes |= PRINTER_ATTRIBUTE_PUBLISHED;
431 break;
432 case DSPRINT_UNPUBLISH:
433 pinfo2->attributes &= (~PRINTER_ATTRIBUTE_PUBLISHED);
434 break;
435 default:
436 win_rc = WERR_NOT_SUPPORTED;
437 goto done;
440 sinfo2->attributes = pinfo2->attributes;
442 win_rc = winreg_update_printer_internal(mem_ctx, session_info, msg_ctx,
443 pinfo2->sharename, info2_mask,
444 sinfo2, NULL, NULL);
445 if (!W_ERROR_IS_OK(win_rc)) {
446 DEBUG(3, ("err %d saving data\n", W_ERROR_V(win_rc)));
447 goto done;
450 TALLOC_FREE(sinfo2);
452 ads = ads_init(lp_realm(), lp_workgroup(), NULL);
453 if (!ads) {
454 DEBUG(3, ("ads_init() failed\n"));
455 win_rc = WERR_SERVER_UNAVAILABLE;
456 goto done;
458 old_krb5ccname = getenv(KRB5_ENV_CCNAME);
459 setenv(KRB5_ENV_CCNAME, "MEMORY:prtpub_cache", 1);
460 SAFE_FREE(ads->auth.password);
461 ads->auth.password = secrets_fetch_machine_password(lp_workgroup(),
462 NULL, NULL);
464 /* ads_connect() will find the DC for us */
465 ads_rc = ads_connect(ads);
466 if (!ADS_ERR_OK(ads_rc)) {
467 DEBUG(3, ("ads_connect failed: %s\n", ads_errstr(ads_rc)));
468 win_rc = WERR_ACCESS_DENIED;
469 goto done;
472 switch (action) {
473 case DSPRINT_PUBLISH:
474 case DSPRINT_UPDATE:
475 win_rc = nt_printer_publish_ads(msg_ctx, ads, pinfo2);
476 break;
477 case DSPRINT_UNPUBLISH:
478 win_rc = nt_printer_unpublish_ads(ads, pinfo2->sharename);
479 break;
482 done:
483 ads_destroy(&ads);
484 ads_kdestroy("MEMORY:prtpub_cache");
485 unsetenv(KRB5_ENV_CCNAME);
486 if (old_krb5ccname) {
487 setenv(KRB5_ENV_CCNAME, old_krb5ccname, 0);
489 return win_rc;
492 WERROR check_published_printers(struct messaging_context *msg_ctx)
494 ADS_STATUS ads_rc;
495 ADS_STRUCT *ads = NULL;
496 int snum;
497 int n_services = lp_numservices();
498 TALLOC_CTX *tmp_ctx = NULL;
499 struct auth_session_info *session_info = NULL;
500 struct spoolss_PrinterInfo2 *pinfo2;
501 NTSTATUS status;
502 WERROR result;
503 char *old_krb5ccname = NULL;
505 tmp_ctx = talloc_new(NULL);
506 if (!tmp_ctx) return WERR_NOMEM;
508 ads = ads_init(lp_realm(), lp_workgroup(), NULL);
509 if (!ads) {
510 DEBUG(3, ("ads_init() failed\n"));
511 return WERR_SERVER_UNAVAILABLE;
513 old_krb5ccname = getenv(KRB5_ENV_CCNAME);
514 setenv(KRB5_ENV_CCNAME, "MEMORY:prtpub_cache", 1);
515 SAFE_FREE(ads->auth.password);
516 ads->auth.password = secrets_fetch_machine_password(lp_workgroup(),
517 NULL, NULL);
519 /* ads_connect() will find the DC for us */
520 ads_rc = ads_connect(ads);
521 if (!ADS_ERR_OK(ads_rc)) {
522 DEBUG(3, ("ads_connect failed: %s\n", ads_errstr(ads_rc)));
523 result = WERR_ACCESS_DENIED;
524 goto done;
527 status = make_session_info_system(tmp_ctx, &session_info);
528 if (!NT_STATUS_IS_OK(status)) {
529 DEBUG(0, ("check_published_printers: "
530 "Could not create system session_info\n"));
531 result = WERR_ACCESS_DENIED;
532 goto done;
535 for (snum = 0; snum < n_services; snum++) {
536 if (!lp_snum_ok(snum) || !lp_printable(snum)) {
537 continue;
540 result = winreg_get_printer_internal(tmp_ctx, session_info, msg_ctx,
541 lp_servicename(talloc_tos(), snum),
542 &pinfo2);
543 if (!W_ERROR_IS_OK(result)) {
544 continue;
547 if (pinfo2->attributes & PRINTER_ATTRIBUTE_PUBLISHED) {
548 nt_printer_publish_ads(msg_ctx, ads, pinfo2);
551 TALLOC_FREE(pinfo2);
554 result = WERR_OK;
555 done:
556 ads_destroy(&ads);
557 ads_kdestroy("MEMORY:prtpub_cache");
558 unsetenv(KRB5_ENV_CCNAME);
559 if (old_krb5ccname) {
560 setenv(KRB5_ENV_CCNAME, old_krb5ccname, 0);
562 talloc_free(tmp_ctx);
563 return result;
566 bool is_printer_published(TALLOC_CTX *mem_ctx,
567 const struct auth_session_info *session_info,
568 struct messaging_context *msg_ctx,
569 const char *servername,
570 const char *printer,
571 struct spoolss_PrinterInfo2 **info2)
573 struct spoolss_PrinterInfo2 *pinfo2 = NULL;
574 WERROR result;
575 struct dcerpc_binding_handle *b;
577 result = winreg_printer_binding_handle(mem_ctx,
578 session_info,
579 msg_ctx,
580 &b);
581 if (!W_ERROR_IS_OK(result)) {
582 return false;
585 result = winreg_get_printer(mem_ctx, b,
586 printer, &pinfo2);
587 if (!W_ERROR_IS_OK(result)) {
588 return false;
591 if (!(pinfo2->attributes & PRINTER_ATTRIBUTE_PUBLISHED)) {
592 TALLOC_FREE(pinfo2);
593 return false;
596 if (info2) {
597 *info2 = talloc_move(mem_ctx, &pinfo2);
599 talloc_free(pinfo2);
600 return true;
602 #else
603 WERROR nt_printer_guid_get(TALLOC_CTX *mem_ctx,
604 const struct auth_session_info *session_info,
605 struct messaging_context *msg_ctx,
606 const char *printer, struct GUID *guid)
608 return WERR_NOT_SUPPORTED;
611 WERROR nt_printer_publish(TALLOC_CTX *mem_ctx,
612 const struct auth_session_info *session_info,
613 struct messaging_context *msg_ctx,
614 struct spoolss_PrinterInfo2 *pinfo2,
615 int action)
617 return WERR_OK;
620 WERROR check_published_printers(struct messaging_context *msg_ctx)
622 return WERR_OK;
625 bool is_printer_published(TALLOC_CTX *mem_ctx,
626 const struct auth_session_info *session_info,
627 struct messaging_context *msg_ctx,
628 const char *servername,
629 const char *printer,
630 struct spoolss_PrinterInfo2 **info2)
632 return False;
634 #endif /* HAVE_ADS */