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/spoolss/srv_spoolss_util.h"
25 #include "nt_printing.h"
29 #include "../libcli/registry/util_reg.h"
31 #include "../librpc/ndr/libndr.h"
32 #include "rpc_client/cli_winreg_spoolss.h"
35 /*****************************************************************
36 ****************************************************************/
38 static void store_printer_guid(struct messaging_context
*msg_ctx
,
39 const char *printer
, struct GUID guid
)
42 struct auth_session_info
*session_info
= NULL
;
48 tmp_ctx
= talloc_new(NULL
);
50 DEBUG(0, ("store_printer_guid: Out of memory?!\n"));
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"));
61 guid_str
= GUID_string(tmp_ctx
, &guid
);
63 DEBUG(0, ("store_printer_guid: Out of memory?!\n"));
67 /* We used to store this as a REG_BINARY but that causes
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",
77 result
= winreg_set_printer_dataex_internal(tmp_ctx
, session_info
, msg_ctx
,
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
));
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
)
96 enum winreg_Type type
;
102 tmp_ctx
= talloc_new(mem_ctx
);
103 if (tmp_ctx
== NULL
) {
104 DEBUG(0, ("out of memory?!\n"));
108 result
= winreg_get_printer_dataex_internal(tmp_ctx
, session_info
,
115 if (!W_ERROR_IS_OK(result
)) {
116 DEBUG(0, ("Failed to get GUID for printer %s\n", printer
));
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 */
127 const char *guid_str
;
128 ok
= pull_reg_sz(tmp_ctx
, &blob
, &guid_str
);
130 DEBUG(0, ("Failed to unmarshall GUID for printer %s\n",
132 result
= WERR_REG_CORRUPT
;
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
);
144 if (blob
.length
!= sizeof(struct GUID
)) {
145 DEBUG(0, ("bad GUID for printer %s\n", printer
));
146 result
= WERR_REG_CORRUPT
;
149 memcpy(guid
, blob
.data
, sizeof(struct GUID
));
152 DEBUG(0,("GUID value stored as invalid type (%d)\n", type
));
153 result
= WERR_REG_CORRUPT
;
160 talloc_free(tmp_ctx
);
164 static WERROR
nt_printer_info_to_mods(TALLOC_CTX
*ctx
,
165 struct spoolss_PrinterInfo2
*info2
,
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
) {
179 ads_mod_str(ctx
, mods
, SPOOL_REG_UNCNAME
, info_str
);
181 info_str
= talloc_asprintf(ctx
, "%d", 4);
182 if (info_str
== NULL
) {
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
) {
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
) {
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
) {
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");
220 ads_mod_str(ctx
, mods
, SPOOL_REG_PRINTKEEPPRINTEDJOBS
, "FALSE");
223 switch (info2
->attributes
& 0x3) {
225 ads_mod_str(ctx
, mods
, SPOOL_REG_PRINTSPOOLING
,
226 SPOOL_REGVAL_PRINTWHILESPOOLING
);
229 ads_mod_str(ctx
, mods
, SPOOL_REG_PRINTSPOOLING
,
230 SPOOL_REGVAL_PRINTAFTERSPOOLED
);
233 ads_mod_str(ctx
, mods
, SPOOL_REG_PRINTSPOOLING
,
234 SPOOL_REGVAL_PRINTDIRECT
);
237 DEBUG(3, ("unsupported printer attributes %x\n",
244 static WERROR
nt_printer_publish_ads(struct messaging_context
*msg_ctx
,
246 struct spoolss_PrinterInfo2
*pinfo2
)
250 char *prt_dn
= NULL
, *srv_dn
, *srv_cn_0
, *srv_cn_escaped
, *sharename_escaped
;
251 char *srv_dn_utf8
, **srv_cn_utf8
;
254 const char *attrs
[] = {"objectGUID", NULL
};
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");
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",
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
);
284 return WERR_SERVER_UNAVAILABLE
;
286 srv_cn_utf8
= ldap_explode_dn(srv_dn_utf8
, 1);
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
)) {
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
)) {
301 ldap_memfree(srv_dn_utf8
);
302 ldap_memfree(srv_cn_utf8
);
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
) {
313 return WERR_SERVER_UNAVAILABLE
;
315 sharename_escaped
= escape_rdn_val_string_alloc(printer
);
316 if (!sharename_escaped
) {
317 SAFE_FREE(srv_cn_escaped
);
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
);
334 win_rc
= nt_printer_info_to_mods(ctx
, pinfo2
, &mods
);
335 if (!W_ERROR_IS_OK(win_rc
)) {
341 ads_rc
= ads_mod_printer_entry(ads
, prt_dn
, ctx
, &mods
);
342 if (ads_rc
.err
.rc
== LDAP_NO_SUCH_OBJECT
) {
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
))) {
359 guid_ok
= ads_pull_guid(ads
, res
, &guid
);
360 ads_msgfree(ads
, res
);
362 store_printer_guid(msg_ctx
, printer
, guid
);
370 static WERROR
nt_printer_unpublish_ads(ADS_STRUCT
*ads
,
374 LDAPMessage
*res
= 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
);
386 ads_msgfree(ads
, res
);
389 ads_rc
= ads_del_dn(ads
, prt_dn
);
394 ads_msgfree(ads
, res
);
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
,
415 uint32_t info2_mask
= SPOOLSS_PRINTER_INFO_ATTRIBUTES
;
416 struct spoolss_SetPrinterInfo2
*sinfo2
;
418 ADS_STRUCT
*ads
= NULL
;
420 char *old_krb5ccname
= NULL
;
422 sinfo2
= talloc_zero(mem_ctx
, struct spoolss_SetPrinterInfo2
);
428 case DSPRINT_PUBLISH
:
430 pinfo2
->attributes
|= PRINTER_ATTRIBUTE_PUBLISHED
;
432 case DSPRINT_UNPUBLISH
:
433 pinfo2
->attributes
&= (~PRINTER_ATTRIBUTE_PUBLISHED
);
436 win_rc
= WERR_NOT_SUPPORTED
;
440 sinfo2
->attributes
= pinfo2
->attributes
;
442 win_rc
= winreg_update_printer_internal(mem_ctx
, session_info
, msg_ctx
,
443 pinfo2
->sharename
, info2_mask
,
445 if (!W_ERROR_IS_OK(win_rc
)) {
446 DEBUG(3, ("err %d saving data\n", W_ERROR_V(win_rc
)));
452 ads
= ads_init(lp_realm(), lp_workgroup(), NULL
);
454 DEBUG(3, ("ads_init() failed\n"));
455 win_rc
= WERR_SERVER_UNAVAILABLE
;
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(),
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
;
473 case DSPRINT_PUBLISH
:
475 win_rc
= nt_printer_publish_ads(msg_ctx
, ads
, pinfo2
);
477 case DSPRINT_UNPUBLISH
:
478 win_rc
= nt_printer_unpublish_ads(ads
, pinfo2
->sharename
);
484 ads_kdestroy("MEMORY:prtpub_cache");
485 unsetenv(KRB5_ENV_CCNAME
);
486 if (old_krb5ccname
) {
487 setenv(KRB5_ENV_CCNAME
, old_krb5ccname
, 0);
492 WERROR
check_published_printers(struct messaging_context
*msg_ctx
)
495 ADS_STRUCT
*ads
= NULL
;
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
;
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
);
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(),
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
;
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
;
535 for (snum
= 0; snum
< n_services
; snum
++) {
536 if (!lp_snum_ok(snum
) || !lp_print_ok(snum
)) {
540 result
= winreg_get_printer_internal(tmp_ctx
, session_info
, msg_ctx
,
541 lp_servicename(talloc_tos(), snum
),
543 if (!W_ERROR_IS_OK(result
)) {
547 if (pinfo2
->attributes
& PRINTER_ATTRIBUTE_PUBLISHED
) {
548 nt_printer_publish_ads(msg_ctx
, ads
, pinfo2
);
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
);
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
,
571 struct spoolss_PrinterInfo2
**info2
)
573 struct spoolss_PrinterInfo2
*pinfo2
= NULL
;
575 struct dcerpc_binding_handle
*b
;
577 result
= winreg_printer_binding_handle(mem_ctx
,
581 if (!W_ERROR_IS_OK(result
)) {
585 result
= winreg_get_printer(mem_ctx
, b
,
587 if (!W_ERROR_IS_OK(result
)) {
591 if (!(pinfo2
->attributes
& PRINTER_ATTRIBUTE_PUBLISHED
)) {
597 *info2
= talloc_move(mem_ctx
, &pinfo2
);
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
,
620 WERROR
check_published_printers(struct messaging_context
*msg_ctx
)
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
,
630 struct spoolss_PrinterInfo2
**info2
)
634 #endif /* HAVE_ADS */