srv_spoolss_nt.c: fix wrong value in debug message
[Samba.git] / source3 / rpc_server / spoolss / srv_spoolss_nt.c
blob055ff17252647fd86517c7f18f8513c431978012
1 /*
2 * Unix SMB/CIFS implementation.
3 * RPC Pipe client / server routines
4 * Copyright (C) Andrew Tridgell 1992-2000,
5 * Copyright (C) Luke Kenneth Casson Leighton 1996-2000,
6 * Copyright (C) Jean François Micouleau 1998-2000,
7 * Copyright (C) Jeremy Allison 2001-2002,
8 * Copyright (C) Gerald Carter 2000-2004,
9 * Copyright (C) Tim Potter 2001-2002.
10 * Copyright (C) Guenther Deschner 2009-2010.
11 * Copyright (C) Andreas Schneider 2010.
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 3 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, see <http://www.gnu.org/licenses/>.
27 /* Since the SPOOLSS rpc routines are basically DOS 16-bit calls wrapped
28 up, all the errors returned are DOS errors, not NT status codes. */
30 #include "includes.h"
31 #include "libsmb/namequery.h"
32 #include "ntdomain.h"
33 #include "nt_printing.h"
34 #include "srv_spoolss_util.h"
35 #include "librpc/gen_ndr/ndr_spoolss.h"
36 #include "librpc/gen_ndr/ndr_spoolss_scompat.h"
37 #include "../librpc/gen_ndr/ndr_spoolss_c.h"
38 #include "rpc_client/init_spoolss.h"
39 #include "rpc_client/cli_pipe.h"
40 #include "../libcli/security/security.h"
41 #include "librpc/gen_ndr/ndr_security.h"
42 #include "registry.h"
43 #include "include/printing.h"
44 #include "secrets.h"
45 #include "../librpc/gen_ndr/netlogon.h"
46 #include "rpc_misc.h"
47 #include "printing/notify.h"
48 #include "serverid.h"
49 #include "../libcli/registry/util_reg.h"
50 #include "smbd/smbd.h"
51 #include "smbd/globals.h"
52 #include "auth.h"
53 #include "messages.h"
54 #include "rpc_server/spoolss/srv_spoolss_nt.h"
55 #include "util_tdb.h"
56 #include "libsmb/libsmb.h"
57 #include "printing/printer_list.h"
58 #include "../lib/tsocket/tsocket.h"
59 #include "rpc_client/cli_winreg_spoolss.h"
60 #include "../libcli/smb/smbXcli_base.h"
61 #include "rpc_server/spoolss/srv_spoolss_handle.h"
62 #include "lib/gencache.h"
63 #include "rpc_server/rpc_server.h"
64 #include "librpc/rpc/dcesrv_core.h"
65 #include "printing/nt_printing_migrate_internal.h"
66 #include "lib/util/string_wrappers.h"
68 /* macros stolen from s4 spoolss server */
69 #define SPOOLSS_BUFFER_UNION(fn,info,level) \
70 ((info)?ndr_size_##fn(info, level, 0):0)
72 #define SPOOLSS_BUFFER_UNION_ARRAY(mem_ctx,fn,info,level,count) \
73 ((info)?ndr_size_##fn##_info(mem_ctx, level, count, info):0)
75 #define SPOOLSS_BUFFER_ARRAY(mem_ctx,fn,info,count) \
76 ((info)?ndr_size_##fn##_info(mem_ctx, count, info):0)
78 #define SPOOLSS_BUFFER_OK(val_true,val_false) ((r->in.offered >= *r->out.needed)?val_true:val_false)
80 #undef DBGC_CLASS
81 #define DBGC_CLASS DBGC_RPC_SRV
83 #ifndef MAX_OPEN_PRINTER_EXS
84 #define MAX_OPEN_PRINTER_EXS 50
85 #endif
87 #define GLOBAL_SPOOLSS_OS_MAJOR_DEFAULT 5
88 #define GLOBAL_SPOOLSS_OS_MINOR_DEFAULT 2
89 #define GLOBAL_SPOOLSS_OS_BUILD_DEFAULT 3790
90 #define GLOBAL_SPOOLSS_ARCHITECTURE SPOOLSS_ARCHITECTURE_x64
92 static struct printer_handle *printers_list;
94 struct printer_session_counter {
95 struct printer_session_counter *next;
96 struct printer_session_counter *prev;
98 int snum;
99 uint32_t counter;
102 static struct printer_session_counter *counter_list;
104 struct notify_back_channel {
105 struct notify_back_channel *prev, *next;
107 /* associated client */
108 struct sockaddr_storage client_address;
110 /* print notify back-channel pipe handle*/
111 struct rpc_pipe_client *cli_pipe;
112 struct cli_state *cli;
113 uint32_t active_connections;
116 static struct notify_back_channel *back_channels;
118 /* Map generic permissions to printer object specific permissions */
120 const struct standard_mapping printer_std_mapping = {
121 PRINTER_READ,
122 PRINTER_WRITE,
123 PRINTER_EXECUTE,
124 PRINTER_ALL_ACCESS
127 /* Map generic permissions to print server object specific permissions */
129 const struct standard_mapping printserver_std_mapping = {
130 SERVER_READ,
131 SERVER_WRITE,
132 SERVER_EXECUTE,
133 SERVER_ALL_ACCESS
136 /* API table for Xcv Monitor functions */
138 struct xcv_api_table {
139 const char *name;
140 WERROR(*fn) (TALLOC_CTX *mem_ctx, struct security_token *token, DATA_BLOB *in, DATA_BLOB *out, uint32_t *needed);
143 static void prune_printername_cache(void);
145 /********************************************************************
146 * Canonicalize servername.
147 ********************************************************************/
149 static const char *canon_servername(const char *servername)
151 const char *pservername = servername;
153 if (servername == NULL) {
154 return "";
157 while (*pservername == '\\') {
158 pservername++;
160 return pservername;
163 /* translate between internal status numbers and NT status numbers */
164 static int nt_printj_status(int v)
166 switch (v) {
167 case LPQ_QUEUED:
168 return 0;
169 case LPQ_PAUSED:
170 return JOB_STATUS_PAUSED;
171 case LPQ_SPOOLING:
172 return JOB_STATUS_SPOOLING;
173 case LPQ_PRINTING:
174 return JOB_STATUS_PRINTING;
175 case LPQ_ERROR:
176 return JOB_STATUS_ERROR;
177 case LPQ_DELETING:
178 return JOB_STATUS_DELETING;
179 case LPQ_OFFLINE:
180 return JOB_STATUS_OFFLINE;
181 case LPQ_PAPEROUT:
182 return JOB_STATUS_PAPEROUT;
183 case LPQ_PRINTED:
184 return JOB_STATUS_PRINTED;
185 case LPQ_DELETED:
186 return JOB_STATUS_DELETED;
187 case LPQ_BLOCKED:
188 return JOB_STATUS_BLOCKED_DEVQ;
189 case LPQ_USER_INTERVENTION:
190 return JOB_STATUS_USER_INTERVENTION;
192 return 0;
195 static int nt_printq_status(int v)
197 switch (v) {
198 case LPQ_PAUSED:
199 return PRINTER_STATUS_PAUSED;
200 case LPQ_QUEUED:
201 case LPQ_SPOOLING:
202 case LPQ_PRINTING:
203 return 0;
205 return 0;
208 /***************************************************************************
209 Disconnect from the client
210 ****************************************************************************/
212 static void srv_spoolss_replycloseprinter(int snum,
213 struct printer_handle *prn_hnd)
215 WERROR result;
216 NTSTATUS status;
219 * Tell the specific printing tdb we no longer want messages for this printer
220 * by deregistering our PID.
223 if (!print_notify_deregister_pid(snum)) {
224 DEBUG(0, ("Failed to register our pid for printer %s\n",
225 lp_const_servicename(snum)));
228 /* weird if the test succeeds !!! */
229 if (prn_hnd->notify.cli_chan == NULL ||
230 prn_hnd->notify.cli_chan->cli_pipe == NULL ||
231 prn_hnd->notify.cli_chan->cli_pipe->binding_handle == NULL ||
232 prn_hnd->notify.cli_chan->active_connections == 0) {
233 DEBUG(0, ("Trying to close unexisting backchannel!\n"));
234 DLIST_REMOVE(back_channels, prn_hnd->notify.cli_chan);
235 TALLOC_FREE(prn_hnd->notify.cli_chan);
236 return;
239 status = dcerpc_spoolss_ReplyClosePrinter(
240 prn_hnd->notify.cli_chan->cli_pipe->binding_handle,
241 talloc_tos(),
242 &prn_hnd->notify.cli_hnd,
243 &result);
244 if (!NT_STATUS_IS_OK(status)) {
245 DEBUG(0, ("dcerpc_spoolss_ReplyClosePrinter failed [%s].\n",
246 nt_errstr(status)));
247 result = ntstatus_to_werror(status);
248 } else if (!W_ERROR_IS_OK(result)) {
249 DEBUG(0, ("reply_close_printer failed [%s].\n",
250 win_errstr(result)));
253 /* if it's the last connection, deconnect the IPC$ share */
254 if (prn_hnd->notify.cli_chan->active_connections == 1) {
256 cli_shutdown(prn_hnd->notify.cli_chan->cli);
257 DLIST_REMOVE(back_channels, prn_hnd->notify.cli_chan);
258 TALLOC_FREE(prn_hnd->notify.cli_chan);
260 if (prn_hnd->notify.msg_ctx != NULL) {
261 messaging_deregister(prn_hnd->notify.msg_ctx,
262 MSG_PRINTER_NOTIFY2, NULL);
266 if (prn_hnd->notify.cli_chan) {
267 prn_hnd->notify.cli_chan->active_connections--;
268 prn_hnd->notify.cli_chan = NULL;
272 /****************************************************************************
273 Functions to free a printer entry datastruct.
274 ****************************************************************************/
276 static int printer_entry_destructor(struct printer_handle *Printer)
278 if (Printer->notify.cli_chan != NULL &&
279 Printer->notify.cli_chan->active_connections > 0) {
280 int snum = -1;
282 switch(Printer->printer_type) {
283 case SPLHND_SERVER:
284 srv_spoolss_replycloseprinter(snum, Printer);
285 break;
287 case SPLHND_PRINTER:
288 snum = print_queue_snum(Printer->sharename);
289 if (snum != -1) {
290 srv_spoolss_replycloseprinter(snum, Printer);
292 break;
293 default:
294 break;
298 Printer->notify.flags=0;
299 Printer->notify.options=0;
300 Printer->notify.localmachine[0]='\0';
301 Printer->notify.printerlocal=0;
302 TALLOC_FREE(Printer->notify.option);
303 TALLOC_FREE(Printer->devmode);
305 /* Remove from the internal list. */
306 DLIST_REMOVE(printers_list, Printer);
307 return 0;
310 /****************************************************************************
311 find printer index by handle
312 ****************************************************************************/
314 static struct printer_handle *find_printer_index_by_hnd(struct pipes_struct *p,
315 struct policy_handle *hnd)
317 struct printer_handle *find_printer = NULL;
318 NTSTATUS status;
320 find_printer = find_policy_by_hnd(p,
321 hnd,
322 DCESRV_HANDLE_ANY,
323 struct printer_handle,
324 &status);
325 if (!NT_STATUS_IS_OK(status)) {
326 DEBUG(2,("find_printer_index_by_hnd: Printer handle not found: "));
327 return NULL;
330 return find_printer;
333 /****************************************************************************
334 Close printer index by handle.
335 ****************************************************************************/
337 static bool close_printer_handle(struct pipes_struct *p, struct policy_handle *hnd)
339 struct printer_handle *Printer = find_printer_index_by_hnd(p, hnd);
341 if (!Printer) {
342 DEBUG(2,("close_printer_handle: Invalid handle (%s:%u:%u)\n",
343 OUR_HANDLE(hnd)));
344 return false;
347 close_policy_hnd(p, hnd);
349 return true;
352 /****************************************************************************
353 Delete a printer given a handle.
354 ****************************************************************************/
356 static WERROR delete_printer_hook(TALLOC_CTX *ctx, struct security_token *token,
357 const char *sharename,
358 struct messaging_context *msg_ctx)
360 const struct loadparm_substitution *lp_sub =
361 loadparm_s3_global_substitution();
362 char *cmd = lp_deleteprinter_command(talloc_tos(), lp_sub);
363 char *command = NULL;
364 int ret;
365 bool is_print_op = false;
367 /* can't fail if we don't try */
369 if ( !*cmd )
370 return WERR_OK;
372 command = talloc_asprintf(ctx,
373 "%s \"%s\"",
374 cmd, sharename);
375 if (!command) {
376 return WERR_NOT_ENOUGH_MEMORY;
378 if ( token )
379 is_print_op = security_token_has_privilege(token, SEC_PRIV_PRINT_OPERATOR);
381 DEBUG(10,("Running [%s]\n", command));
383 /********** BEGIN SePrintOperatorPrivlege BLOCK **********/
385 if ( is_print_op )
386 become_root();
388 ret = smbrun(command, NULL, NULL);
389 if (ret == 0) {
390 /* Tell everyone we updated smb.conf. */
391 messaging_send_all(msg_ctx, MSG_SMB_CONF_UPDATED, NULL, 0);
394 if ( is_print_op )
395 unbecome_root();
397 /********** END SePrintOperatorPrivlege BLOCK **********/
399 DEBUGADD(10,("returned [%d]\n", ret));
401 TALLOC_FREE(command);
403 if (ret != 0)
404 return WERR_INVALID_HANDLE; /* What to return here? */
406 return WERR_OK;
409 /****************************************************************************
410 Delete a printer given a handle.
411 ****************************************************************************/
413 static WERROR delete_printer_handle(struct pipes_struct *p, struct policy_handle *hnd)
415 struct printer_handle *Printer = find_printer_index_by_hnd(p, hnd);
416 WERROR result;
418 if (!Printer) {
419 DEBUG(2,("delete_printer_handle: Invalid handle (%s:%u:%u)\n",
420 OUR_HANDLE(hnd)));
421 return WERR_INVALID_HANDLE;
425 * It turns out that Windows allows delete printer on a handle
426 * opened by an admin user, then used on a pipe handle created
427 * by an anonymous user..... but they're working on security.... riiight !
428 * JRA.
431 if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
432 DEBUG(3, ("delete_printer_handle: denied by handle\n"));
433 return WERR_ACCESS_DENIED;
436 /* this does not need a become root since the access check has been
437 done on the handle already */
439 result = winreg_delete_printer_key_internal(p->mem_ctx,
440 get_session_info_system(),
441 p->msg_ctx,
442 Printer->sharename,
443 "");
444 if (!W_ERROR_IS_OK(result)) {
445 DEBUG(3,("Error deleting printer %s\n", Printer->sharename));
446 return WERR_INVALID_HANDLE;
449 result = delete_printer_hook(p->mem_ctx, p->session_info->security_token,
450 Printer->sharename, p->msg_ctx);
451 if (!W_ERROR_IS_OK(result)) {
452 return result;
454 prune_printername_cache();
455 return WERR_OK;
458 /****************************************************************************
459 Return the snum of a printer corresponding to an handle.
460 ****************************************************************************/
462 static bool get_printer_snum(struct pipes_struct *p, struct policy_handle *hnd,
463 int *number, struct share_params **params)
465 struct printer_handle *Printer = find_printer_index_by_hnd(p, hnd);
467 if (!Printer) {
468 DEBUG(2,("get_printer_snum: Invalid handle (%s:%u:%u)\n",
469 OUR_HANDLE(hnd)));
470 return false;
473 switch (Printer->printer_type) {
474 case SPLHND_PRINTER:
475 DEBUG(4,("short name:%s\n", Printer->sharename));
476 *number = print_queue_snum(Printer->sharename);
477 return (*number != -1);
478 case SPLHND_SERVER:
479 return false;
480 default:
481 return false;
485 /****************************************************************************
486 Set printer handle type.
487 Check if it's \\server or \\server\printer
488 ****************************************************************************/
490 static bool set_printer_hnd_printertype(struct printer_handle *Printer, const char *handlename)
492 DEBUG(3,("Setting printer type=%s\n", handlename));
494 /* it's a print server */
495 if (handlename && *handlename=='\\' && *(handlename+1)=='\\' && !strchr_m(handlename+2, '\\')) {
496 DEBUGADD(4,("Printer is a print server\n"));
497 Printer->printer_type = SPLHND_SERVER;
499 /* it's a printer (set_printer_hnd_name() will handle port monitors */
500 else {
501 DEBUGADD(4,("Printer is a printer\n"));
502 Printer->printer_type = SPLHND_PRINTER;
505 return true;
508 static void prune_printername_cache_fn(const char *key, const char *value,
509 time_t timeout, void *private_data)
511 gencache_del(key);
514 static void prune_printername_cache(void)
516 gencache_iterate(prune_printername_cache_fn, NULL, "PRINTERNAME/*");
519 /****************************************************************************
520 Set printer handle name.. Accept names like \\server, \\server\printer,
521 \\server\SHARE, & "\\server\,XcvMonitor Standard TCP/IP Port" See
522 the MSDN docs regarding OpenPrinter() for details on the XcvData() and
523 XcvDataPort() interface.
524 ****************************************************************************/
526 static WERROR set_printer_hnd_name(TALLOC_CTX *mem_ctx,
527 const struct auth_session_info *session_info,
528 struct messaging_context *msg_ctx,
529 struct printer_handle *Printer,
530 const char *handlename)
532 int snum;
533 int n_services=lp_numservices();
534 char *aprinter;
535 const char *printername;
536 const char *servername = NULL;
537 fstring sname;
538 bool found = false;
539 struct spoolss_PrinterInfo2 *info2 = NULL;
540 WERROR result;
541 char *p;
544 * Hopefully nobody names his printers like this. Maybe \ or ,
545 * are illegal in printer names even?
547 const char printer_not_found[] = "Printer \\, !@#$%^&*( not found";
548 char *cache_key;
549 char *tmp;
551 DEBUG(4,("Setting printer name=%s (len=%lu)\n", handlename,
552 (unsigned long)strlen(handlename)));
554 aprinter = discard_const_p(char, handlename);
555 if ( *handlename == '\\' ) {
556 servername = canon_servername(handlename);
557 if ( (aprinter = strchr_m( servername, '\\' )) != NULL ) {
558 *aprinter = '\0';
559 aprinter++;
561 if (!is_myname_or_ipaddr(servername)) {
562 return WERR_INVALID_PRINTER_NAME;
564 Printer->servername = talloc_asprintf(Printer, "\\\\%s", servername);
565 if (Printer->servername == NULL) {
566 return WERR_NOT_ENOUGH_MEMORY;
570 if (Printer->printer_type == SPLHND_SERVER) {
571 return WERR_OK;
574 if (Printer->printer_type != SPLHND_PRINTER) {
575 return WERR_INVALID_HANDLE;
578 DEBUGADD(5, ("searching for [%s]\n", aprinter));
580 p = strchr(aprinter, ',');
581 if (p != NULL) {
582 char *p2 = p;
583 p++;
584 if (*p == ' ') {
585 p++;
587 if (strncmp(p, "DrvConvert", strlen("DrvConvert")) == 0) {
588 *p2 = '\0';
589 } else if (strncmp(p, "LocalOnly", strlen("LocalOnly")) == 0) {
590 *p2 = '\0';
594 if (p) {
595 DEBUGADD(5, ("stripped handlename: [%s]\n", aprinter));
598 /* check for the Port Monitor Interface */
599 if ( strequal( aprinter, SPL_XCV_MONITOR_TCPMON ) ) {
600 Printer->printer_type = SPLHND_PORTMON_TCP;
601 fstrcpy(sname, SPL_XCV_MONITOR_TCPMON);
602 found = true;
604 else if ( strequal( aprinter, SPL_XCV_MONITOR_LOCALMON ) ) {
605 Printer->printer_type = SPLHND_PORTMON_LOCAL;
606 fstrcpy(sname, SPL_XCV_MONITOR_LOCALMON);
607 found = true;
610 cache_key = talloc_asprintf(talloc_tos(), "PRINTERNAME/%s", aprinter);
611 if (cache_key == NULL) {
612 return WERR_NOT_ENOUGH_MEMORY;
616 * With hundreds of printers, the "for" loop iterating all
617 * shares can be quite expensive, as it is done on every
618 * OpenPrinter. The loop maps "aprinter" to "sname", the
619 * result of which we cache in gencache.
621 if (gencache_get(cache_key, talloc_tos(), &tmp, NULL)) {
622 found = (strcmp(tmp, printer_not_found) != 0);
623 if (!found) {
624 DEBUG(4, ("Printer %s not found\n", aprinter));
625 TALLOC_FREE(tmp);
626 return WERR_INVALID_PRINTER_NAME;
628 fstrcpy(sname, tmp);
629 TALLOC_FREE(tmp);
632 /* Search all sharenames first as this is easier than pulling
633 the printer_info_2 off of disk. Don't use find_service() since
634 that calls out to map_username() */
636 /* do another loop to look for printernames */
637 for (snum = 0; !found && snum < n_services; snum++) {
638 const char *printer = lp_const_servicename(snum);
640 /* no point going on if this is not a printer */
641 if (!(lp_snum_ok(snum) && lp_printable(snum))) {
642 continue;
645 /* ignore [printers] share */
646 if (strequal(printer, "printers")) {
647 continue;
650 fstrcpy(sname, printer);
651 if (strequal(aprinter, printer)) {
652 found = true;
653 break;
656 /* no point looking up the printer object if
657 we aren't allowing printername != sharename */
658 if (lp_force_printername(snum)) {
659 continue;
662 result = winreg_get_printer_internal(mem_ctx,
663 session_info,
664 msg_ctx,
665 sname,
666 &info2);
667 if ( !W_ERROR_IS_OK(result) ) {
668 DEBUG(2,("set_printer_hnd_name: failed to lookup printer [%s] -- result [%s]\n",
669 sname, win_errstr(result)));
670 continue;
673 printername = strrchr(info2->printername, '\\');
674 if (printername == NULL) {
675 printername = info2->printername;
676 } else {
677 printername++;
680 if (strequal(printername, aprinter)) {
681 found = true;
682 break;
685 DEBUGADD(10, ("printername: %s\n", printername));
687 TALLOC_FREE(info2);
690 if (!found) {
691 gencache_set(cache_key, printer_not_found,
692 time(NULL) + 300);
693 TALLOC_FREE(cache_key);
694 DEBUGADD(4,("Printer not found\n"));
695 return WERR_INVALID_PRINTER_NAME;
698 gencache_set(cache_key, sname, time(NULL) + 300);
699 TALLOC_FREE(cache_key);
701 DEBUGADD(4,("set_printer_hnd_name: Printer found: %s -> %s\n", aprinter, sname));
703 strlcpy(Printer->sharename, sname, sizeof(Printer->sharename));
705 return WERR_OK;
708 /****************************************************************************
709 Find first available printer slot. creates a printer handle for you.
710 ****************************************************************************/
712 static WERROR open_printer_hnd(struct pipes_struct *p,
713 struct policy_handle *hnd,
714 const char *name,
715 uint32_t access_granted)
717 struct printer_handle *new_printer;
718 WERROR result;
720 DEBUG(10,("open_printer_hnd: name [%s]\n", name));
722 new_printer = talloc_zero(p->mem_ctx, struct printer_handle);
723 if (new_printer == NULL) {
724 return WERR_NOT_ENOUGH_MEMORY;
726 talloc_set_destructor(new_printer, printer_entry_destructor);
728 /* This also steals the printer_handle on the policy_handle */
729 if (!create_policy_hnd(p, hnd, 0, new_printer)) {
730 TALLOC_FREE(new_printer);
731 return WERR_INVALID_HANDLE;
734 /* Add to the internal list. */
735 DLIST_ADD(printers_list, new_printer);
737 new_printer->notify.option=NULL;
739 if (!set_printer_hnd_printertype(new_printer, name)) {
740 close_printer_handle(p, hnd);
741 return WERR_INVALID_HANDLE;
744 result = set_printer_hnd_name(p->mem_ctx,
745 get_session_info_system(),
746 p->msg_ctx,
747 new_printer, name);
748 if (!W_ERROR_IS_OK(result)) {
749 close_printer_handle(p, hnd);
750 return result;
753 new_printer->access_granted = access_granted;
755 DBG_INFO("%d printer handles active\n", (int)num_pipe_handles());
757 return WERR_OK;
760 /***************************************************************************
761 check to see if the client motify handle is monitoring the notification
762 given by (notify_type, notify_field).
763 **************************************************************************/
765 static bool is_monitoring_event_flags(uint32_t flags, uint16_t notify_type,
766 uint16_t notify_field)
768 return true;
771 static bool is_monitoring_event(struct printer_handle *p, uint16_t notify_type,
772 uint16_t notify_field)
774 struct spoolss_NotifyOption *option = p->notify.option;
775 uint32_t i, j;
778 * Flags should always be zero when the change notify
779 * is registered by the client's spooler. A user Win32 app
780 * might use the flags though instead of the NOTIFY_OPTION_INFO
781 * --jerry
784 if (!option) {
785 return false;
788 if (p->notify.flags)
789 return is_monitoring_event_flags(
790 p->notify.flags, notify_type, notify_field);
792 for (i = 0; i < option->count; i++) {
794 /* Check match for notify_type */
796 if (option->types[i].type != notify_type)
797 continue;
799 /* Check match for field */
801 for (j = 0; j < option->types[i].count; j++) {
802 if (option->types[i].fields[j].field == notify_field) {
803 return true;
808 DEBUG(10, ("Open handle for \\\\%s\\%s is not monitoring 0x%02x/0x%02x\n",
809 p->servername, p->sharename, notify_type, notify_field));
811 return false;
814 #define SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(_data, _integer) \
815 _data->data.integer[0] = _integer; \
816 _data->data.integer[1] = 0;
819 #define SETUP_SPOOLSS_NOTIFY_DATA_STRING(_data, _p) \
820 _data->data.string.string = talloc_strdup(mem_ctx, _p); \
821 if (!_data->data.string.string) {\
822 _data->data.string.size = 0; \
824 _data->data.string.size = strlen_m_term(_p) * 2;
826 #define SETUP_SPOOLSS_NOTIFY_DATA_DEVMODE(_data, _devmode) \
827 _data->data.devmode.devmode = _devmode;
829 static void init_systemtime_buffer(TALLOC_CTX *mem_ctx,
830 struct tm *t,
831 const char **pp,
832 uint32_t *plen)
834 struct spoolss_Time st;
835 uint32_t len = 16;
836 char *p;
838 if (!init_systemtime(&st, t)) {
839 return;
842 p = talloc_array(mem_ctx, char, len);
843 if (!p) {
844 return;
848 * Systemtime must be linearized as a set of UINT16's.
849 * Fix from Benjamin (Bj) Kuit bj@it.uts.edu.au
852 SSVAL(p, 0, st.year);
853 SSVAL(p, 2, st.month);
854 SSVAL(p, 4, st.day_of_week);
855 SSVAL(p, 6, st.day);
856 SSVAL(p, 8, st.hour);
857 SSVAL(p, 10, st.minute);
858 SSVAL(p, 12, st.second);
859 SSVAL(p, 14, st.millisecond);
861 *pp = p;
862 *plen = len;
865 /* Convert a notification message to a struct spoolss_Notify */
867 static void notify_one_value(struct spoolss_notify_msg *msg,
868 struct spoolss_Notify *data,
869 TALLOC_CTX *mem_ctx)
871 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, msg->notify.value[0]);
874 static void notify_string(struct spoolss_notify_msg *msg,
875 struct spoolss_Notify *data,
876 TALLOC_CTX *mem_ctx)
878 /* The length of the message includes the trailing \0 */
880 data->data.string.size = msg->len * 2;
881 data->data.string.string = talloc_strdup(mem_ctx, msg->notify.data);
882 if (!data->data.string.string) {
883 data->data.string.size = 0;
884 return;
888 static void notify_system_time(struct spoolss_notify_msg *msg,
889 struct spoolss_Notify *data,
890 TALLOC_CTX *mem_ctx)
892 data->data.string.string = NULL;
893 data->data.string.size = 0;
895 if (msg->len != sizeof(time_t)) {
896 DEBUG(5, ("notify_system_time: received wrong sized message (%d)\n",
897 msg->len));
898 return;
901 init_systemtime_buffer(mem_ctx, gmtime((time_t *)msg->notify.data),
902 &data->data.string.string,
903 &data->data.string.size);
906 struct notify2_message_table {
907 const char *name;
908 void (*fn)(struct spoolss_notify_msg *msg,
909 struct spoolss_Notify *data, TALLOC_CTX *mem_ctx);
912 static struct notify2_message_table printer_notify_table[] = {
913 /* 0x00 */ { "PRINTER_NOTIFY_FIELD_SERVER_NAME", notify_string },
914 /* 0x01 */ { "PRINTER_NOTIFY_FIELD_PRINTER_NAME", notify_string },
915 /* 0x02 */ { "PRINTER_NOTIFY_FIELD_SHARE_NAME", notify_string },
916 /* 0x03 */ { "PRINTER_NOTIFY_FIELD_PORT_NAME", notify_string },
917 /* 0x04 */ { "PRINTER_NOTIFY_FIELD_DRIVER_NAME", notify_string },
918 /* 0x05 */ { "PRINTER_NOTIFY_FIELD_COMMENT", notify_string },
919 /* 0x06 */ { "PRINTER_NOTIFY_FIELD_LOCATION", notify_string },
920 /* 0x07 */ { "PRINTER_NOTIFY_FIELD_DEVMODE", NULL },
921 /* 0x08 */ { "PRINTER_NOTIFY_FIELD_SEPFILE", notify_string },
922 /* 0x09 */ { "PRINTER_NOTIFY_FIELD_PRINT_PROCESSOR", notify_string },
923 /* 0x0a */ { "PRINTER_NOTIFY_FIELD_PARAMETERS", NULL },
924 /* 0x0b */ { "PRINTER_NOTIFY_FIELD_DATATYPE", notify_string },
925 /* 0x0c */ { "PRINTER_NOTIFY_FIELD_SECURITY_DESCRIPTOR", NULL },
926 /* 0x0d */ { "PRINTER_NOTIFY_FIELD_ATTRIBUTES", notify_one_value },
927 /* 0x0e */ { "PRINTER_NOTIFY_FIELD_PRIORITY", notify_one_value },
928 /* 0x0f */ { "PRINTER_NOTIFY_FIELD_DEFAULT_PRIORITY", NULL },
929 /* 0x10 */ { "PRINTER_NOTIFY_FIELD_START_TIME", NULL },
930 /* 0x11 */ { "PRINTER_NOTIFY_FIELD_UNTIL_TIME", NULL },
931 /* 0x12 */ { "PRINTER_NOTIFY_FIELD_STATUS", notify_one_value },
934 static struct notify2_message_table job_notify_table[] = {
935 /* 0x00 */ { "JOB_NOTIFY_FIELD_PRINTER_NAME", NULL },
936 /* 0x01 */ { "JOB_NOTIFY_FIELD_MACHINE_NAME", NULL },
937 /* 0x02 */ { "JOB_NOTIFY_FIELD_PORT_NAME", NULL },
938 /* 0x03 */ { "JOB_NOTIFY_FIELD_USER_NAME", notify_string },
939 /* 0x04 */ { "JOB_NOTIFY_FIELD_NOTIFY_NAME", NULL },
940 /* 0x05 */ { "JOB_NOTIFY_FIELD_DATATYPE", NULL },
941 /* 0x06 */ { "JOB_NOTIFY_FIELD_PRINT_PROCESSOR", NULL },
942 /* 0x07 */ { "JOB_NOTIFY_FIELD_PARAMETERS", NULL },
943 /* 0x08 */ { "JOB_NOTIFY_FIELD_DRIVER_NAME", NULL },
944 /* 0x09 */ { "JOB_NOTIFY_FIELD_DEVMODE", NULL },
945 /* 0x0a */ { "JOB_NOTIFY_FIELD_STATUS", notify_one_value },
946 /* 0x0b */ { "JOB_NOTIFY_FIELD_STATUS_STRING", NULL },
947 /* 0x0c */ { "JOB_NOTIFY_FIELD_SECURITY_DESCRIPTOR", NULL },
948 /* 0x0d */ { "JOB_NOTIFY_FIELD_DOCUMENT", notify_string },
949 /* 0x0e */ { "JOB_NOTIFY_FIELD_PRIORITY", NULL },
950 /* 0x0f */ { "JOB_NOTIFY_FIELD_POSITION", NULL },
951 /* 0x10 */ { "JOB_NOTIFY_FIELD_SUBMITTED", notify_system_time },
952 /* 0x11 */ { "JOB_NOTIFY_FIELD_START_TIME", NULL },
953 /* 0x12 */ { "JOB_NOTIFY_FIELD_UNTIL_TIME", NULL },
954 /* 0x13 */ { "JOB_NOTIFY_FIELD_TIME", NULL },
955 /* 0x14 */ { "JOB_NOTIFY_FIELD_TOTAL_PAGES", notify_one_value },
956 /* 0x15 */ { "JOB_NOTIFY_FIELD_PAGES_PRINTED", NULL },
957 /* 0x16 */ { "JOB_NOTIFY_FIELD_TOTAL_BYTES", notify_one_value },
958 /* 0x17 */ { "JOB_NOTIFY_FIELD_BYTES_PRINTED", NULL },
962 /***********************************************************************
963 Allocate talloc context for container object
964 **********************************************************************/
966 static void notify_msg_ctr_init( SPOOLSS_NOTIFY_MSG_CTR *ctr )
968 if ( !ctr )
969 return;
971 ctr->ctx = talloc_init("notify_msg_ctr_init %p", ctr);
973 return;
976 /***********************************************************************
977 release all allocated memory and zero out structure
978 **********************************************************************/
980 static void notify_msg_ctr_destroy( SPOOLSS_NOTIFY_MSG_CTR *ctr )
982 if ( !ctr )
983 return;
985 if ( ctr->ctx )
986 talloc_destroy(ctr->ctx);
988 ZERO_STRUCTP(ctr);
990 return;
993 /***********************************************************************
994 **********************************************************************/
996 static TALLOC_CTX* notify_ctr_getctx( SPOOLSS_NOTIFY_MSG_CTR *ctr )
998 if ( !ctr )
999 return NULL;
1001 return ctr->ctx;
1004 /***********************************************************************
1005 **********************************************************************/
1007 static SPOOLSS_NOTIFY_MSG_GROUP* notify_ctr_getgroup( SPOOLSS_NOTIFY_MSG_CTR *ctr, uint32_t idx )
1009 if ( !ctr || !ctr->msg_groups )
1010 return NULL;
1012 if ( idx >= ctr->num_groups )
1013 return NULL;
1015 return &ctr->msg_groups[idx];
1019 /***********************************************************************
1020 How many groups of change messages do we have ?
1021 **********************************************************************/
1023 static int notify_msg_ctr_numgroups( SPOOLSS_NOTIFY_MSG_CTR *ctr )
1025 if ( !ctr )
1026 return 0;
1028 return ctr->num_groups;
1031 /***********************************************************************
1032 Add a SPOOLSS_NOTIFY_MSG_CTR to the correct group
1033 **********************************************************************/
1035 static int notify_msg_ctr_addmsg( SPOOLSS_NOTIFY_MSG_CTR *ctr, SPOOLSS_NOTIFY_MSG *msg )
1037 SPOOLSS_NOTIFY_MSG_GROUP *groups = NULL;
1038 SPOOLSS_NOTIFY_MSG_GROUP *msg_grp = NULL;
1039 SPOOLSS_NOTIFY_MSG *msg_list = NULL;
1040 int i, new_slot;
1042 if ( !ctr || !msg )
1043 return 0;
1045 /* loop over all groups looking for a matching printer name */
1047 for ( i=0; i<ctr->num_groups; i++ ) {
1048 if ( strcmp(ctr->msg_groups[i].printername, msg->printer) == 0 )
1049 break;
1052 /* add a new group? */
1054 if ( i == ctr->num_groups ) {
1055 ctr->num_groups++;
1057 if ( !(groups = talloc_realloc( ctr->ctx, ctr->msg_groups, SPOOLSS_NOTIFY_MSG_GROUP, ctr->num_groups)) ) {
1058 DEBUG(0,("notify_msg_ctr_addmsg: talloc_realloc() failed!\n"));
1059 return 0;
1061 ctr->msg_groups = groups;
1063 /* clear the new entry and set the printer name */
1065 ZERO_STRUCT( ctr->msg_groups[ctr->num_groups-1] );
1066 fstrcpy( ctr->msg_groups[ctr->num_groups-1].printername, msg->printer );
1069 /* add the change messages; 'i' is the correct index now regardless */
1071 msg_grp = &ctr->msg_groups[i];
1073 msg_grp->num_msgs++;
1075 if ( !(msg_list = talloc_realloc( ctr->ctx, msg_grp->msgs, SPOOLSS_NOTIFY_MSG, msg_grp->num_msgs )) ) {
1076 DEBUG(0,("notify_msg_ctr_addmsg: talloc_realloc() failed for new message [%d]!\n", msg_grp->num_msgs));
1077 return 0;
1079 msg_grp->msgs = msg_list;
1081 new_slot = msg_grp->num_msgs-1;
1082 memcpy( &msg_grp->msgs[new_slot], msg, sizeof(SPOOLSS_NOTIFY_MSG) );
1084 /* need to allocate own copy of data */
1086 if ( msg->len != 0 )
1087 msg_grp->msgs[new_slot].notify.data = (char *)
1088 talloc_memdup( ctr->ctx, msg->notify.data, msg->len );
1090 return ctr->num_groups;
1093 static void construct_info_data(struct spoolss_Notify *info_data,
1094 enum spoolss_NotifyType type,
1095 uint16_t field, int id);
1097 /***********************************************************************
1098 Send a change notication message on all handles which have a call
1099 back registered
1100 **********************************************************************/
1102 static int build_notify2_messages(TALLOC_CTX *mem_ctx,
1103 struct printer_handle *prn_hnd,
1104 SPOOLSS_NOTIFY_MSG *messages,
1105 uint32_t num_msgs,
1106 struct spoolss_Notify **_notifies,
1107 size_t *_count)
1109 struct spoolss_Notify *notifies;
1110 SPOOLSS_NOTIFY_MSG *msg;
1111 size_t count = 0;
1112 uint32_t id;
1113 uint32_t i;
1115 notifies = talloc_zero_array(mem_ctx,
1116 struct spoolss_Notify, num_msgs);
1117 if (!notifies) {
1118 return ENOMEM;
1121 for (i = 0; i < num_msgs; i++) {
1123 msg = &messages[i];
1125 /* Are we monitoring this event? */
1127 if (!is_monitoring_event(prn_hnd, msg->type, msg->field)) {
1128 continue;
1131 DEBUG(10, ("Sending message type [0x%x] field [0x%2x] "
1132 "for printer [%s]\n",
1133 msg->type, msg->field, prn_hnd->sharename));
1136 * if the is a printer notification handle and not a job
1137 * notification type, then set the id to 0.
1138 * Otherwise just use what was specified in the message.
1140 * When registering change notification on a print server
1141 * handle we always need to send back the id (snum) matching
1142 * the printer for which the change took place.
1143 * For change notify registered on a printer handle,
1144 * this does not matter and the id should be 0.
1146 * --jerry
1149 if ((msg->type == PRINTER_NOTIFY_TYPE) &&
1150 (prn_hnd->printer_type == SPLHND_PRINTER)) {
1151 id = 0;
1152 } else {
1153 id = msg->id;
1156 /* Convert unix jobid to smb jobid */
1158 if (msg->flags & SPOOLSS_NOTIFY_MSG_UNIX_JOBID) {
1159 id = sysjob_to_jobid(msg->id);
1161 if (id == -1) {
1162 DEBUG(3, ("no such unix jobid %d\n",
1163 msg->id));
1164 continue;
1168 construct_info_data(&notifies[count],
1169 msg->type, msg->field, id);
1171 switch(msg->type) {
1172 case PRINTER_NOTIFY_TYPE:
1173 if (printer_notify_table[msg->field].fn) {
1174 printer_notify_table[msg->field].fn(msg,
1175 &notifies[count], mem_ctx);
1177 break;
1179 case JOB_NOTIFY_TYPE:
1180 if (job_notify_table[msg->field].fn) {
1181 job_notify_table[msg->field].fn(msg,
1182 &notifies[count], mem_ctx);
1184 break;
1186 default:
1187 DEBUG(5, ("Unknown notification type %d\n",
1188 msg->type));
1189 continue;
1192 count++;
1195 *_notifies = notifies;
1196 *_count = count;
1198 return 0;
1201 static int send_notify2_printer(TALLOC_CTX *mem_ctx,
1202 struct printer_handle *prn_hnd,
1203 SPOOLSS_NOTIFY_MSG_GROUP *msg_group)
1205 struct spoolss_Notify *notifies;
1206 size_t count = 0;
1207 union spoolss_ReplyPrinterInfo info;
1208 struct spoolss_NotifyInfo info0;
1209 uint32_t reply_result;
1210 NTSTATUS status;
1211 WERROR werr;
1212 int ret;
1214 /* Is there notification on this handle? */
1215 if (prn_hnd->notify.cli_chan == NULL ||
1216 prn_hnd->notify.cli_chan->cli_pipe == NULL ||
1217 prn_hnd->notify.cli_chan->cli_pipe->binding_handle == NULL ||
1218 prn_hnd->notify.cli_chan->active_connections == 0) {
1219 return 0;
1222 DEBUG(10, ("Client connected! [\\\\%s\\%s]\n",
1223 prn_hnd->servername, prn_hnd->sharename));
1225 /* For this printer? Print servers always receive notifications. */
1226 if ((prn_hnd->printer_type == SPLHND_PRINTER) &&
1227 (!strequal(msg_group->printername, prn_hnd->sharename))) {
1228 return 0;
1231 DEBUG(10,("Our printer\n"));
1233 /* build the array of change notifications */
1234 ret = build_notify2_messages(mem_ctx, prn_hnd,
1235 msg_group->msgs,
1236 msg_group->num_msgs,
1237 &notifies, &count);
1238 if (ret) {
1239 return ret;
1242 info0.version = 0x2;
1243 info0.flags = count ? 0x00020000 /* ??? */ : PRINTER_NOTIFY_INFO_DISCARDED;
1244 info0.count = count;
1245 info0.notifies = notifies;
1247 info.info0 = &info0;
1249 status = dcerpc_spoolss_RouterReplyPrinterEx(
1250 prn_hnd->notify.cli_chan->cli_pipe->binding_handle,
1251 mem_ctx,
1252 &prn_hnd->notify.cli_hnd,
1253 prn_hnd->notify.change, /* color */
1254 prn_hnd->notify.flags,
1255 &reply_result,
1256 0, /* reply_type, must be 0 */
1257 info, &werr);
1258 if (!NT_STATUS_IS_OK(status)) {
1259 DEBUG(1, ("dcerpc_spoolss_RouterReplyPrinterEx to client: %s "
1260 "failed: %s\n",
1261 prn_hnd->notify.cli_chan->cli_pipe->srv_name_slash,
1262 nt_errstr(status)));
1263 werr = ntstatus_to_werror(status);
1264 } else if (!W_ERROR_IS_OK(werr)) {
1265 DEBUG(1, ("RouterReplyPrinterEx to client: %s "
1266 "failed: %s\n",
1267 prn_hnd->notify.cli_chan->cli_pipe->srv_name_slash,
1268 win_errstr(werr)));
1270 switch (reply_result) {
1271 case 0:
1272 break;
1273 case PRINTER_NOTIFY_INFO_DISCARDED:
1274 case PRINTER_NOTIFY_INFO_DISCARDNOTED:
1275 case PRINTER_NOTIFY_INFO_COLOR_MISMATCH:
1276 break;
1277 default:
1278 break;
1281 return 0;
1284 static void send_notify2_changes( SPOOLSS_NOTIFY_MSG_CTR *ctr, uint32_t idx )
1286 struct printer_handle *p;
1287 TALLOC_CTX *mem_ctx = notify_ctr_getctx( ctr );
1288 SPOOLSS_NOTIFY_MSG_GROUP *msg_group = notify_ctr_getgroup( ctr, idx );
1289 int ret;
1291 if ( !msg_group ) {
1292 DEBUG(5,("send_notify2_changes() called with no msg group!\n"));
1293 return;
1296 if (!msg_group->msgs) {
1297 DEBUG(5, ("send_notify2_changes() called with no messages!\n"));
1298 return;
1301 DEBUG(8,("send_notify2_changes: Enter...[%s]\n", msg_group->printername));
1303 /* loop over all printers */
1305 for (p = printers_list; p; p = p->next) {
1306 ret = send_notify2_printer(mem_ctx, p, msg_group);
1307 if (ret) {
1308 goto done;
1312 done:
1313 DEBUG(8,("send_notify2_changes: Exit...\n"));
1314 return;
1317 /***********************************************************************
1318 **********************************************************************/
1320 static bool notify2_unpack_msg( SPOOLSS_NOTIFY_MSG *msg, struct timeval *tv, void *buf, size_t len )
1323 uint32_t tv_sec, tv_usec;
1324 size_t offset = 0;
1326 /* Unpack message */
1328 offset += tdb_unpack((uint8_t *)buf + offset, len - offset, "f",
1329 msg->printer);
1331 offset += tdb_unpack((uint8_t *)buf + offset, len - offset, "ddddddd",
1332 &tv_sec, &tv_usec,
1333 &msg->type, &msg->field, &msg->id, &msg->len, &msg->flags);
1335 if (msg->len == 0)
1336 tdb_unpack((uint8_t *)buf + offset, len - offset, "dd",
1337 &msg->notify.value[0], &msg->notify.value[1]);
1338 else
1339 tdb_unpack((uint8_t *)buf + offset, len - offset, "B",
1340 &msg->len, &msg->notify.data);
1342 DEBUG(3, ("notify2_unpack_msg: got NOTIFY2 message for printer %s, jobid %u type %d, field 0x%02x, flags 0x%04x\n",
1343 msg->printer, (unsigned int)msg->id, msg->type, msg->field, msg->flags));
1345 tv->tv_sec = tv_sec;
1346 tv->tv_usec = tv_usec;
1348 if (msg->len == 0)
1349 DEBUG(3, ("notify2_unpack_msg: value1 = %d, value2 = %d\n", msg->notify.value[0],
1350 msg->notify.value[1]));
1351 else
1352 dump_data(3, (uint8_t *)msg->notify.data, msg->len);
1354 return true;
1357 /********************************************************************
1358 Receive a notify2 message list
1359 ********************************************************************/
1361 static void receive_notify2_message_list(struct messaging_context *msg,
1362 void *private_data,
1363 uint32_t msg_type,
1364 struct server_id server_id,
1365 DATA_BLOB *data)
1367 size_t msg_count, i;
1368 char *buf = (char *)data->data;
1369 char *msg_ptr;
1370 size_t msg_len;
1371 SPOOLSS_NOTIFY_MSG notify;
1372 SPOOLSS_NOTIFY_MSG_CTR messages;
1373 int num_groups;
1375 if (data->length < 4) {
1376 DEBUG(0,("receive_notify2_message_list: bad message format (len < 4)!\n"));
1377 return;
1380 msg_count = IVAL(buf, 0);
1381 msg_ptr = buf + 4;
1383 DEBUG(5, ("receive_notify2_message_list: got %lu messages in list\n", (unsigned long)msg_count));
1385 if (msg_count == 0) {
1386 DEBUG(0,("receive_notify2_message_list: bad message format (msg_count == 0) !\n"));
1387 return;
1390 /* initialize the container */
1392 ZERO_STRUCT( messages );
1393 notify_msg_ctr_init( &messages );
1396 * build message groups for each printer identified
1397 * in a change_notify msg. Remember that a PCN message
1398 * includes the handle returned for the srv_spoolss_replyopenprinter()
1399 * call. Therefore messages are grouped according to printer handle.
1402 for ( i=0; i<msg_count; i++ ) {
1403 struct timeval msg_tv;
1405 if (msg_ptr + 4 - buf > data->length) {
1406 DEBUG(0,("receive_notify2_message_list: bad message format (len > buf_size) !\n"));
1407 return;
1410 msg_len = IVAL(msg_ptr,0);
1411 msg_ptr += 4;
1413 if (msg_ptr + msg_len - buf > data->length) {
1414 DEBUG(0,("receive_notify2_message_list: bad message format (bad len) !\n"));
1415 return;
1418 /* unpack messages */
1420 ZERO_STRUCT( notify );
1421 notify2_unpack_msg( &notify, &msg_tv, msg_ptr, msg_len );
1422 msg_ptr += msg_len;
1424 /* add to correct list in container */
1426 notify_msg_ctr_addmsg( &messages, &notify );
1428 /* free memory that might have been allocated by notify2_unpack_msg() */
1430 if ( notify.len != 0 )
1431 SAFE_FREE( notify.notify.data );
1434 /* process each group of messages */
1436 num_groups = notify_msg_ctr_numgroups( &messages );
1437 for ( i=0; i<num_groups; i++ )
1438 send_notify2_changes( &messages, i );
1441 /* cleanup */
1443 DEBUG(10,("receive_notify2_message_list: processed %u messages\n",
1444 (uint32_t)msg_count ));
1446 notify_msg_ctr_destroy( &messages );
1448 return;
1451 /********************************************************************
1452 Send a message to ourself about new driver being installed
1453 so we can upgrade the information for each printer bound to this
1454 driver
1455 ********************************************************************/
1457 static bool srv_spoolss_drv_upgrade_printer(const char *drivername,
1458 struct messaging_context *msg_ctx)
1460 int len = strlen(drivername);
1462 if (!len)
1463 return false;
1465 DEBUG(10,("srv_spoolss_drv_upgrade_printer: Sending message about driver upgrade [%s]\n",
1466 drivername));
1468 messaging_send_buf(msg_ctx, messaging_server_id(msg_ctx),
1469 MSG_PRINTER_DRVUPGRADE,
1470 (const uint8_t *)drivername, len+1);
1472 return true;
1475 void srv_spoolss_cleanup(void)
1477 struct printer_session_counter *session_counter;
1479 for (session_counter = counter_list;
1480 session_counter != NULL;
1481 session_counter = counter_list) {
1482 DLIST_REMOVE(counter_list, session_counter);
1483 TALLOC_FREE(session_counter);
1487 /**********************************************************************
1488 callback to receive a MSG_PRINTER_DRVUPGRADE message and interate
1489 over all printers, upgrading ones as necessary
1490 This is now *ONLY* called inside the background lpq updater. JRA.
1491 **********************************************************************/
1493 void do_drv_upgrade_printer(struct messaging_context *msg,
1494 void *private_data,
1495 uint32_t msg_type,
1496 struct server_id server_id,
1497 DATA_BLOB *data)
1499 TALLOC_CTX *tmp_ctx;
1500 const struct auth_session_info *session_info = get_session_info_system();
1501 struct spoolss_PrinterInfo2 *pinfo2;
1502 WERROR result;
1503 const char *drivername;
1504 int snum;
1505 int n_services = lp_numservices();
1506 struct dcerpc_binding_handle *b = NULL;
1508 tmp_ctx = talloc_new(NULL);
1509 if (!tmp_ctx) return;
1511 drivername = talloc_strndup(tmp_ctx, (const char *)data->data, data->length);
1512 if (!drivername) {
1513 DEBUG(0, ("do_drv_upgrade_printer: Out of memoery ?!\n"));
1514 goto done;
1517 DEBUG(10, ("do_drv_upgrade_printer: "
1518 "Got message for new driver [%s]\n", drivername));
1520 /* Iterate the printer list */
1522 for (snum = 0; snum < n_services; snum++) {
1523 if (!lp_snum_ok(snum) || !lp_printable(snum)) {
1524 continue;
1527 /* ignore [printers] share */
1528 if (strequal(lp_const_servicename(snum), "printers")) {
1529 continue;
1532 if (b == NULL) {
1533 result = winreg_printer_binding_handle(tmp_ctx,
1534 session_info,
1535 msg,
1536 &b);
1537 if (!W_ERROR_IS_OK(result)) {
1538 break;
1542 result = winreg_get_printer(tmp_ctx, b,
1543 lp_const_servicename(snum),
1544 &pinfo2);
1546 if (!W_ERROR_IS_OK(result)) {
1547 continue;
1550 if (!pinfo2->drivername) {
1551 continue;
1554 if (strcmp(drivername, pinfo2->drivername) != 0) {
1555 continue;
1558 DEBUG(6,("Updating printer [%s]\n", pinfo2->printername));
1560 /* all we care about currently is the change_id */
1561 result = winreg_printer_update_changeid(tmp_ctx, b,
1562 pinfo2->printername);
1564 if (!W_ERROR_IS_OK(result)) {
1565 DEBUG(3, ("do_drv_upgrade_printer: "
1566 "Failed to update changeid [%s]\n",
1567 win_errstr(result)));
1571 /* all done */
1572 done:
1573 talloc_free(tmp_ctx);
1576 /********************************************************************
1577 Update the cache for all printq's with a registered client
1578 connection
1579 ********************************************************************/
1581 void update_monitored_printq_cache(struct messaging_context *msg_ctx)
1583 struct printer_handle *printer = printers_list;
1584 int snum;
1586 /* loop through all printers and update the cache where
1587 a client is connected */
1588 while (printer) {
1589 if ((printer->printer_type == SPLHND_PRINTER) &&
1590 ((printer->notify.cli_chan != NULL) &&
1591 (printer->notify.cli_chan->active_connections > 0))) {
1592 snum = print_queue_snum(printer->sharename);
1593 print_queue_status(msg_ctx, snum, NULL, NULL);
1596 printer = printer->next;
1599 return;
1602 /****************************************************************
1603 _spoolss_OpenPrinter
1604 ****************************************************************/
1606 WERROR _spoolss_OpenPrinter(struct pipes_struct *p,
1607 struct spoolss_OpenPrinter *r)
1609 struct spoolss_OpenPrinterEx e;
1610 struct spoolss_UserLevel1 level1;
1611 WERROR werr;
1613 ZERO_STRUCT(level1);
1615 e.in.printername = r->in.printername;
1616 e.in.datatype = r->in.datatype;
1617 e.in.devmode_ctr = r->in.devmode_ctr;
1618 e.in.access_mask = r->in.access_mask;
1619 e.in.userlevel_ctr.level = 1;
1620 e.in.userlevel_ctr.user_info.level1 = &level1;
1622 e.out.handle = r->out.handle;
1624 werr = _spoolss_OpenPrinterEx(p, &e);
1626 if (W_ERROR_EQUAL(werr, WERR_INVALID_PARAMETER)) {
1627 /* OpenPrinterEx returns this for a bad
1628 * printer name. We must return WERR_INVALID_PRINTER_NAME
1629 * instead.
1631 werr = WERR_INVALID_PRINTER_NAME;
1634 return werr;
1637 static WERROR copy_devicemode(TALLOC_CTX *mem_ctx,
1638 struct spoolss_DeviceMode *orig,
1639 struct spoolss_DeviceMode **dest)
1641 struct spoolss_DeviceMode *dm;
1643 dm = talloc(mem_ctx, struct spoolss_DeviceMode);
1644 if (!dm) {
1645 return WERR_NOT_ENOUGH_MEMORY;
1648 /* copy all values, then duplicate strings and structs */
1649 *dm = *orig;
1651 dm->devicename = talloc_strdup(dm, orig->devicename);
1652 if (!dm->devicename) {
1653 return WERR_NOT_ENOUGH_MEMORY;
1655 dm->formname = talloc_strdup(dm, orig->formname);
1656 if (!dm->formname) {
1657 return WERR_NOT_ENOUGH_MEMORY;
1659 if (orig->driverextra_data.data) {
1660 dm->driverextra_data.data =
1661 (uint8_t *) talloc_memdup(dm, orig->driverextra_data.data,
1662 orig->driverextra_data.length);
1663 if (!dm->driverextra_data.data) {
1664 return WERR_NOT_ENOUGH_MEMORY;
1668 *dest = dm;
1669 return WERR_OK;
1672 /****************************************************************
1673 _spoolss_OpenPrinterEx
1674 ****************************************************************/
1676 WERROR _spoolss_OpenPrinterEx(struct pipes_struct *p,
1677 struct spoolss_OpenPrinterEx *r)
1679 int snum;
1680 char *raddr;
1681 char *rhost;
1682 struct printer_handle *Printer=NULL;
1683 WERROR result;
1684 int rc;
1686 if (!r->in.printername) {
1687 return WERR_INVALID_PARAMETER;
1690 if (!*r->in.printername) {
1691 return WERR_INVALID_PARAMETER;
1694 if (r->in.userlevel_ctr.level > 3) {
1695 return WERR_INVALID_PARAMETER;
1697 if ((r->in.userlevel_ctr.level == 1 && !r->in.userlevel_ctr.user_info.level1) ||
1698 (r->in.userlevel_ctr.level == 2 && !r->in.userlevel_ctr.user_info.level2) ||
1699 (r->in.userlevel_ctr.level == 3 && !r->in.userlevel_ctr.user_info.level3)) {
1700 return WERR_INVALID_PARAMETER;
1704 * The printcap printer share inventory is updated on client
1705 * enumeration. For clients that do not perform enumeration prior to
1706 * access, such as cupssmbadd, we reinitialise the printer share
1707 * inventory on open as well.
1709 become_root();
1710 delete_and_reload_printers();
1711 unbecome_root();
1713 /* some sanity check because you can open a printer or a print server */
1714 /* aka: \\server\printer or \\server */
1716 DEBUGADD(3,("checking name: %s\n", r->in.printername));
1718 result = open_printer_hnd(p, r->out.handle, r->in.printername, 0);
1719 if (!W_ERROR_IS_OK(result)) {
1720 DEBUG(3,("_spoolss_OpenPrinterEx: Cannot open a printer handle "
1721 "for printer %s\n", r->in.printername));
1722 ZERO_STRUCTP(r->out.handle);
1723 return result;
1726 Printer = find_printer_index_by_hnd(p, r->out.handle);
1727 if ( !Printer ) {
1728 DEBUG(0,("_spoolss_OpenPrinterEx: logic error. Can't find printer "
1729 "handle we created for printer %s\n", r->in.printername));
1730 close_printer_handle(p, r->out.handle);
1731 ZERO_STRUCTP(r->out.handle);
1732 return WERR_INVALID_PARAMETER;
1736 * First case: the user is opening the print server:
1738 * Disallow MS AddPrinterWizard if parameter disables it. A Win2k
1739 * client 1st tries an OpenPrinterEx with access==0, MUST be allowed.
1741 * Then both Win2k and WinNT clients try an OpenPrinterEx with
1742 * SERVER_ALL_ACCESS, which we allow only if the user is root (uid=0)
1743 * or if the user is listed in the smb.conf printer admin parameter.
1745 * Then they try OpenPrinterEx with SERVER_READ which we allow. This lets the
1746 * client view printer folder, but does not show the MSAPW.
1748 * Note: this test needs code to check access rights here too. Jeremy
1749 * could you look at this?
1751 * Second case: the user is opening a printer:
1752 * NT doesn't let us connect to a printer if the connecting user
1753 * doesn't have print permission.
1755 * Third case: user is opening a Port Monitor
1756 * access checks same as opening a handle to the print server.
1759 switch (Printer->printer_type )
1761 case SPLHND_SERVER:
1762 case SPLHND_PORTMON_TCP:
1763 case SPLHND_PORTMON_LOCAL:
1764 /* Printserver handles use global struct... */
1766 snum = -1;
1768 if (r->in.access_mask & SEC_FLAG_MAXIMUM_ALLOWED) {
1769 r->in.access_mask |= SERVER_ACCESS_ADMINISTER;
1770 r->in.access_mask |= SERVER_ACCESS_ENUMERATE;
1773 /* Map standard access rights to object specific access rights */
1775 se_map_standard(&r->in.access_mask,
1776 &printserver_std_mapping);
1778 /* Deny any object specific bits that don't apply to print
1779 servers (i.e printer and job specific bits) */
1781 r->in.access_mask &= SEC_MASK_SPECIFIC;
1783 if (r->in.access_mask &
1784 ~(SERVER_ACCESS_ADMINISTER | SERVER_ACCESS_ENUMERATE)) {
1785 DEBUG(3, ("access DENIED for non-printserver bits\n"));
1786 close_printer_handle(p, r->out.handle);
1787 ZERO_STRUCTP(r->out.handle);
1788 return WERR_ACCESS_DENIED;
1791 /* Allow admin access */
1793 if ( r->in.access_mask & SERVER_ACCESS_ADMINISTER )
1795 if (!lp_show_add_printer_wizard()) {
1796 close_printer_handle(p, r->out.handle);
1797 ZERO_STRUCTP(r->out.handle);
1798 return WERR_ACCESS_DENIED;
1801 /* if the user is not root, doesn't have SE_PRINT_OPERATOR privilege,
1802 and not a printer admin, then fail */
1804 if ((p->session_info->unix_token->uid != sec_initial_uid()) &&
1805 !security_token_has_privilege(p->session_info->security_token, SEC_PRIV_PRINT_OPERATOR) &&
1806 !nt_token_check_sid(&global_sid_Builtin_Print_Operators,
1807 p->session_info->security_token)) {
1808 close_printer_handle(p, r->out.handle);
1809 ZERO_STRUCTP(r->out.handle);
1810 DEBUG(3,("access DENIED as user is not root, "
1811 "has no printoperator privilege and is "
1812 "not a member of the printoperator builtin group\n"));
1813 return WERR_ACCESS_DENIED;
1816 r->in.access_mask = SERVER_ACCESS_ADMINISTER;
1818 else
1820 r->in.access_mask = SERVER_ACCESS_ENUMERATE;
1823 DEBUG(4,("Setting print server access = %s\n", (r->in.access_mask == SERVER_ACCESS_ADMINISTER)
1824 ? "SERVER_ACCESS_ADMINISTER" : "SERVER_ACCESS_ENUMERATE" ));
1826 break;
1828 case SPLHND_PRINTER:
1829 /* NT doesn't let us connect to a printer if the connecting user
1830 doesn't have print permission. */
1832 if (!get_printer_snum(p, r->out.handle, &snum, NULL)) {
1833 close_printer_handle(p, r->out.handle);
1834 ZERO_STRUCTP(r->out.handle);
1835 return WERR_INVALID_HANDLE;
1838 if (r->in.access_mask == SEC_FLAG_MAXIMUM_ALLOWED) {
1839 r->in.access_mask = PRINTER_ACCESS_ADMINISTER;
1842 se_map_standard(&r->in.access_mask, &printer_std_mapping);
1844 /* map an empty access mask to the minimum access mask */
1845 if (r->in.access_mask == 0x0)
1846 r->in.access_mask = PRINTER_ACCESS_USE;
1849 * If we are not serving the printer driver for this printer,
1850 * map PRINTER_ACCESS_ADMINISTER to PRINTER_ACCESS_USE. This
1851 * will keep NT clients happy --jerry
1854 if (lp_use_client_driver(snum)
1855 && (r->in.access_mask & PRINTER_ACCESS_ADMINISTER))
1857 r->in.access_mask = PRINTER_ACCESS_USE;
1860 /* check smb.conf parameters and the the sec_desc */
1861 raddr = tsocket_address_inet_addr_string(p->remote_address,
1862 p->mem_ctx);
1863 if (raddr == NULL) {
1864 return WERR_NOT_ENOUGH_MEMORY;
1867 rc = get_remote_hostname(p->remote_address,
1868 &rhost,
1869 p->mem_ctx);
1870 if (rc < 0) {
1871 return WERR_NOT_ENOUGH_MEMORY;
1873 if (strequal(rhost, "UNKNOWN")) {
1874 rhost = raddr;
1877 if (!allow_access(lp_hosts_deny(snum), lp_hosts_allow(snum),
1878 rhost, raddr)) {
1879 DEBUG(3, ("access DENIED (hosts allow/deny) for printer open\n"));
1880 ZERO_STRUCTP(r->out.handle);
1881 return WERR_ACCESS_DENIED;
1884 if (!user_ok_token(uidtoname(p->session_info->unix_token->uid), NULL,
1885 p->session_info->security_token, snum) ||
1886 !W_ERROR_IS_OK(print_access_check(p->session_info,
1887 p->msg_ctx,
1888 snum,
1889 r->in.access_mask))) {
1890 DEBUG(3, ("access DENIED for printer open\n"));
1891 close_printer_handle(p, r->out.handle);
1892 ZERO_STRUCTP(r->out.handle);
1893 return WERR_ACCESS_DENIED;
1896 if ((r->in.access_mask & SEC_MASK_SPECIFIC)& ~(PRINTER_ACCESS_ADMINISTER|PRINTER_ACCESS_USE)) {
1897 DEBUG(3, ("access DENIED for printer open - unknown bits\n"));
1898 close_printer_handle(p, r->out.handle);
1899 ZERO_STRUCTP(r->out.handle);
1900 return WERR_ACCESS_DENIED;
1903 if (r->in.access_mask & PRINTER_ACCESS_ADMINISTER)
1904 r->in.access_mask = PRINTER_ACCESS_ADMINISTER;
1905 else
1906 r->in.access_mask = PRINTER_ACCESS_USE;
1908 DEBUG(4,("Setting printer access = %s\n", (r->in.access_mask == PRINTER_ACCESS_ADMINISTER)
1909 ? "PRINTER_ACCESS_ADMINISTER" : "PRINTER_ACCESS_USE" ));
1911 winreg_create_printer_internal(p->mem_ctx,
1912 get_session_info_system(),
1913 p->msg_ctx,
1914 lp_const_servicename(snum));
1916 break;
1918 default:
1919 /* sanity check to prevent programmer error */
1920 ZERO_STRUCTP(r->out.handle);
1921 return WERR_INVALID_HANDLE;
1924 Printer->access_granted = r->in.access_mask;
1927 * If the client sent a devmode in the OpenPrinter() call, then
1928 * save it here in case we get a job submission on this handle
1931 if ((Printer->printer_type != SPLHND_SERVER)
1932 && (r->in.devmode_ctr.devmode != NULL)) {
1933 copy_devicemode(NULL, r->in.devmode_ctr.devmode,
1934 &Printer->devmode);
1937 return WERR_OK;
1940 /****************************************************************
1941 _spoolss_ClosePrinter
1942 ****************************************************************/
1944 WERROR _spoolss_ClosePrinter(struct pipes_struct *p,
1945 struct spoolss_ClosePrinter *r)
1947 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
1949 if (Printer && Printer->document_started) {
1950 struct spoolss_EndDocPrinter e;
1952 e.in.handle = r->in.handle;
1954 _spoolss_EndDocPrinter(p, &e);
1957 if (!close_printer_handle(p, r->in.handle))
1958 return WERR_INVALID_HANDLE;
1960 /* clear the returned printer handle. Observed behavior
1961 from Win2k server. Don't think this really matters.
1962 Previous code just copied the value of the closed
1963 handle. --jerry */
1965 ZERO_STRUCTP(r->out.handle);
1967 return WERR_OK;
1970 /****************************************************************
1971 _spoolss_DeletePrinter
1972 ****************************************************************/
1974 WERROR _spoolss_DeletePrinter(struct pipes_struct *p,
1975 struct spoolss_DeletePrinter *r)
1977 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
1978 WERROR result;
1979 int snum;
1981 if (Printer && Printer->document_started) {
1982 struct spoolss_EndDocPrinter e;
1984 e.in.handle = r->in.handle;
1986 _spoolss_EndDocPrinter(p, &e);
1989 if (get_printer_snum(p, r->in.handle, &snum, NULL)) {
1990 winreg_delete_printer_key_internal(p->mem_ctx,
1991 get_session_info_system(),
1992 p->msg_ctx,
1993 lp_const_servicename(snum),
1994 "");
1997 result = delete_printer_handle(p, r->in.handle);
1999 return result;
2002 /*******************************************************************
2003 * static function to lookup the version id corresponding to an
2004 * long architecture string
2005 ******************************************************************/
2007 static const struct print_architecture_table_node archi_table[]= {
2009 {"Windows 4.0", SPL_ARCH_WIN40, 0 },
2010 {"Windows NT x86", SPL_ARCH_W32X86, 2 },
2011 {"Windows NT R4000", SPL_ARCH_W32MIPS, 2 },
2012 {"Windows NT Alpha_AXP", SPL_ARCH_W32ALPHA, 2 },
2013 {"Windows NT PowerPC", SPL_ARCH_W32PPC, 2 },
2014 {"Windows IA64", SPL_ARCH_IA64, 3 },
2015 {"Windows x64", SPL_ARCH_X64, 3 },
2016 {NULL, "", -1 }
2019 static const int drv_cversion[] = {SPOOLSS_DRIVER_VERSION_9X,
2020 SPOOLSS_DRIVER_VERSION_NT35,
2021 SPOOLSS_DRIVER_VERSION_NT4,
2022 SPOOLSS_DRIVER_VERSION_200X,
2023 -1};
2025 static int get_version_id(const char *arch)
2027 int i;
2029 for (i=0; archi_table[i].long_archi != NULL; i++)
2031 if (strcmp(arch, archi_table[i].long_archi) == 0)
2032 return (archi_table[i].version);
2035 return -1;
2038 /****************************************************************
2039 _spoolss_DeletePrinterDriver
2040 ****************************************************************/
2042 WERROR _spoolss_DeletePrinterDriver(struct pipes_struct *p,
2043 struct spoolss_DeletePrinterDriver *r)
2046 struct spoolss_DriverInfo8 *info = NULL;
2047 int version;
2048 WERROR status;
2049 struct dcerpc_binding_handle *b;
2050 TALLOC_CTX *tmp_ctx = NULL;
2051 int i;
2052 bool found;
2054 /* if the user is not root, doesn't have SE_PRINT_OPERATOR privilege,
2055 and not a printer admin, then fail */
2057 if ((p->session_info->unix_token->uid != sec_initial_uid()) &&
2058 !security_token_has_privilege(p->session_info->security_token,
2059 SEC_PRIV_PRINT_OPERATOR)) {
2060 return WERR_ACCESS_DENIED;
2063 if (r->in.architecture == NULL || r->in.driver == NULL) {
2064 return WERR_INVALID_ENVIRONMENT;
2067 /* check that we have a valid driver name first */
2069 if ((version = get_version_id(r->in.architecture)) == -1) {
2070 return WERR_INVALID_ENVIRONMENT;
2073 tmp_ctx = talloc_new(p->mem_ctx);
2074 if (!tmp_ctx) {
2075 return WERR_NOT_ENOUGH_MEMORY;
2078 status = winreg_printer_binding_handle(tmp_ctx,
2079 get_session_info_system(),
2080 p->msg_ctx,
2081 &b);
2082 if (!W_ERROR_IS_OK(status)) {
2083 goto done;
2086 for (found = false, i = 0; drv_cversion[i] >= 0; i++) {
2087 status = winreg_get_driver(tmp_ctx, b,
2088 r->in.architecture, r->in.driver,
2089 drv_cversion[i], &info);
2090 if (!W_ERROR_IS_OK(status)) {
2091 DEBUG(5, ("skipping del of driver with version %d\n",
2092 drv_cversion[i]));
2093 continue;
2095 found = true;
2097 if (printer_driver_in_use(tmp_ctx, b, info)) {
2098 status = WERR_PRINTER_DRIVER_IN_USE;
2099 goto done;
2102 status = winreg_del_driver(tmp_ctx, b, info, drv_cversion[i]);
2103 if (!W_ERROR_IS_OK(status)) {
2104 DEBUG(0, ("failed del of driver with version %d\n",
2105 drv_cversion[i]));
2106 goto done;
2109 if (found == false) {
2110 DEBUG(0, ("driver %s not found for deletion\n", r->in.driver));
2111 status = WERR_UNKNOWN_PRINTER_DRIVER;
2112 } else {
2113 status = WERR_OK;
2116 done:
2117 talloc_free(tmp_ctx);
2119 return status;
2122 static WERROR spoolss_dpd_version(TALLOC_CTX *mem_ctx,
2123 struct pipes_struct *p,
2124 struct spoolss_DeletePrinterDriverEx *r,
2125 struct dcerpc_binding_handle *b,
2126 struct spoolss_DriverInfo8 *info)
2128 WERROR status;
2129 bool delete_files;
2131 if (printer_driver_in_use(mem_ctx, b, info)) {
2132 status = WERR_PRINTER_DRIVER_IN_USE;
2133 goto done;
2137 * we have a couple of cases to consider.
2138 * (1) Are any files in use? If so and DPD_DELETE_ALL_FILES is set,
2139 * then the delete should fail if **any** files overlap with
2140 * other drivers
2141 * (2) If DPD_DELETE_UNUSED_FILES is set, then delete all
2142 * non-overlapping files
2143 * (3) If neither DPD_DELETE_ALL_FILES nor DPD_DELETE_UNUSED_FILES
2144 * are set, then do not delete any files
2145 * Refer to MSDN docs on DeletePrinterDriverEx() for details.
2148 delete_files = r->in.delete_flags
2149 & (DPD_DELETE_ALL_FILES | DPD_DELETE_UNUSED_FILES);
2152 if (delete_files) {
2153 bool in_use = printer_driver_files_in_use(mem_ctx, b, info);
2154 if (in_use && (r->in.delete_flags & DPD_DELETE_ALL_FILES)) {
2155 status = WERR_PRINTER_DRIVER_IN_USE;
2156 goto done;
2159 * printer_driver_files_in_use() has trimmed overlapping files
2160 * from info so they are not removed on DPD_DELETE_UNUSED_FILES
2165 status = winreg_del_driver(mem_ctx, b, info, info->version);
2166 if (!W_ERROR_IS_OK(status)) {
2167 goto done;
2171 * now delete any associated files if delete_files is
2172 * true. Even if this part failes, we return succes
2173 * because the driver doesn not exist any more
2175 if (delete_files) {
2176 delete_driver_files(p->session_info, info);
2179 done:
2180 return status;
2183 /****************************************************************
2184 _spoolss_DeletePrinterDriverEx
2185 ****************************************************************/
2187 WERROR _spoolss_DeletePrinterDriverEx(struct pipes_struct *p,
2188 struct spoolss_DeletePrinterDriverEx *r)
2190 struct spoolss_DriverInfo8 *info = NULL;
2191 WERROR status;
2192 struct dcerpc_binding_handle *b;
2193 TALLOC_CTX *tmp_ctx = NULL;
2194 int i;
2195 bool found;
2197 /* if the user is not root, doesn't have SE_PRINT_OPERATOR privilege,
2198 and not a printer admin, then fail */
2200 if ((p->session_info->unix_token->uid != sec_initial_uid()) &&
2201 !security_token_has_privilege(p->session_info->security_token,
2202 SEC_PRIV_PRINT_OPERATOR)) {
2203 return WERR_ACCESS_DENIED;
2206 if (r->in.architecture == NULL || r->in.driver == NULL) {
2207 return WERR_INVALID_ENVIRONMENT;
2210 /* check that we have a valid driver name first */
2211 if (get_version_id(r->in.architecture) == -1) {
2212 /* this is what NT returns */
2213 return WERR_INVALID_ENVIRONMENT;
2216 tmp_ctx = talloc_new(p->mem_ctx);
2217 if (!tmp_ctx) {
2218 return WERR_NOT_ENOUGH_MEMORY;
2221 status = winreg_printer_binding_handle(tmp_ctx,
2222 get_session_info_system(),
2223 p->msg_ctx,
2224 &b);
2225 if (!W_ERROR_IS_OK(status)) {
2226 goto done;
2229 for (found = false, i = 0; drv_cversion[i] >= 0; i++) {
2230 if ((r->in.delete_flags & DPD_DELETE_SPECIFIC_VERSION)
2231 && (drv_cversion[i] != r->in.version)) {
2232 continue;
2235 /* check if a driver with this version exists before delete */
2236 status = winreg_get_driver(tmp_ctx, b,
2237 r->in.architecture, r->in.driver,
2238 drv_cversion[i], &info);
2239 if (!W_ERROR_IS_OK(status)) {
2240 DEBUG(5, ("skipping del of driver with version %d\n",
2241 drv_cversion[i]));
2242 continue;
2244 found = true;
2246 status = spoolss_dpd_version(tmp_ctx, p, r, b, info);
2247 if (!W_ERROR_IS_OK(status)) {
2248 DEBUG(0, ("failed to delete driver with version %d\n",
2249 drv_cversion[i]));
2250 goto done;
2253 if (found == false) {
2254 DEBUG(0, ("driver %s not found for deletion\n", r->in.driver));
2255 status = WERR_UNKNOWN_PRINTER_DRIVER;
2256 } else {
2257 status = WERR_OK;
2260 done:
2261 talloc_free(tmp_ctx);
2262 return status;
2266 /********************************************************************
2267 GetPrinterData on a printer server Handle.
2268 ********************************************************************/
2270 static WERROR getprinterdata_printer_server(TALLOC_CTX *mem_ctx,
2271 const char *value,
2272 enum winreg_Type *type,
2273 union spoolss_PrinterData *data)
2275 DEBUG(8,("getprinterdata_printer_server:%s\n", value));
2277 if (!strcasecmp_m(value, "W3SvcInstalled")) {
2278 *type = REG_DWORD;
2279 SIVAL(&data->value, 0, 0x00);
2280 return WERR_OK;
2283 if (!strcasecmp_m(value, "BeepEnabled")) {
2284 *type = REG_DWORD;
2285 SIVAL(&data->value, 0, 0x00);
2286 return WERR_OK;
2289 if (!strcasecmp_m(value, "EventLog")) {
2290 *type = REG_DWORD;
2291 /* formally was 0x1b */
2292 SIVAL(&data->value, 0, 0x00);
2293 return WERR_OK;
2296 if (!strcasecmp_m(value, "NetPopup")) {
2297 *type = REG_DWORD;
2298 SIVAL(&data->value, 0, 0x00);
2299 return WERR_OK;
2302 if (!strcasecmp_m(value, "MajorVersion")) {
2303 *type = REG_DWORD;
2305 /* Windows NT 4.0 seems to not allow uploading of drivers
2306 to a server that reports 0x3 as the MajorVersion.
2307 need to investigate more how Win2k gets around this .
2308 -- jerry */
2310 if (RA_WINNT == get_remote_arch()) {
2311 SIVAL(&data->value, 0, 0x02);
2312 } else {
2313 SIVAL(&data->value, 0, 0x03);
2316 return WERR_OK;
2319 if (!strcasecmp_m(value, "MinorVersion")) {
2320 *type = REG_DWORD;
2321 SIVAL(&data->value, 0, 0x00);
2322 return WERR_OK;
2325 /* REG_BINARY
2326 * uint32_t size = 0x114
2327 * uint32_t major = 5
2328 * uint32_t minor = [0|1]
2329 * uint32_t build = [2195|2600]
2330 * extra unicode string = e.g. "Service Pack 3"
2332 if (!strcasecmp_m(value, "OSVersion")) {
2333 DATA_BLOB blob;
2334 enum ndr_err_code ndr_err;
2335 struct spoolss_OSVersion os;
2338 * Set the default OSVersion to:
2340 * Windows Server 2003R2 SP2 (5.2.3790)
2342 * used to be Windows 2000 (5.0.2195)
2344 os.major = lp_parm_int(GLOBAL_SECTION_SNUM,
2345 "spoolss", "os_major",
2346 GLOBAL_SPOOLSS_OS_MAJOR_DEFAULT);
2347 os.minor = lp_parm_int(GLOBAL_SECTION_SNUM,
2348 "spoolss", "os_minor",
2349 GLOBAL_SPOOLSS_OS_MINOR_DEFAULT);
2350 os.build = lp_parm_int(GLOBAL_SECTION_SNUM,
2351 "spoolss", "os_build",
2352 GLOBAL_SPOOLSS_OS_BUILD_DEFAULT);
2353 os.extra_string = ""; /* leave extra string empty */
2355 ndr_err = ndr_push_struct_blob(&blob, mem_ctx, &os,
2356 (ndr_push_flags_fn_t)ndr_push_spoolss_OSVersion);
2357 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2358 return WERR_GEN_FAILURE;
2361 if (DEBUGLEVEL >= 10) {
2362 NDR_PRINT_DEBUG(spoolss_OSVersion, &os);
2365 *type = REG_BINARY;
2366 data->binary = blob;
2368 return WERR_OK;
2372 if (!strcasecmp_m(value, "DefaultSpoolDirectory")) {
2373 *type = REG_SZ;
2375 data->string = talloc_strdup(mem_ctx, SPOOLSS_DEFAULT_SERVER_PATH);
2376 W_ERROR_HAVE_NO_MEMORY(data->string);
2378 return WERR_OK;
2381 if (!strcasecmp_m(value, "Architecture")) {
2382 *type = REG_SZ;
2383 data->string = talloc_strdup(mem_ctx,
2384 lp_parm_const_string(GLOBAL_SECTION_SNUM, "spoolss", "architecture", GLOBAL_SPOOLSS_ARCHITECTURE));
2385 W_ERROR_HAVE_NO_MEMORY(data->string);
2387 return WERR_OK;
2390 if (!strcasecmp_m(value, "DsPresent")) {
2391 *type = REG_DWORD;
2393 /* only show the publish check box if we are a
2394 member of a AD domain */
2396 if (lp_security() == SEC_ADS) {
2397 SIVAL(&data->value, 0, 0x01);
2398 } else {
2399 SIVAL(&data->value, 0, 0x00);
2401 return WERR_OK;
2404 if (!strcasecmp_m(value, "DNSMachineName")) {
2405 const char *hostname = get_mydnsfullname();
2407 if (!hostname) {
2408 return WERR_FILE_NOT_FOUND;
2411 *type = REG_SZ;
2412 data->string = talloc_strdup(mem_ctx, hostname);
2413 W_ERROR_HAVE_NO_MEMORY(data->string);
2415 return WERR_OK;
2418 *type = REG_NONE;
2420 return WERR_INVALID_PARAMETER;
2423 /****************************************************************
2424 _spoolss_GetPrinterData
2425 ****************************************************************/
2427 WERROR _spoolss_GetPrinterData(struct pipes_struct *p,
2428 struct spoolss_GetPrinterData *r)
2430 struct spoolss_GetPrinterDataEx r2;
2432 r2.in.handle = r->in.handle;
2433 r2.in.key_name = "PrinterDriverData";
2434 r2.in.value_name = r->in.value_name;
2435 r2.in.offered = r->in.offered;
2436 r2.out.type = r->out.type;
2437 r2.out.data = r->out.data;
2438 r2.out.needed = r->out.needed;
2440 return _spoolss_GetPrinterDataEx(p, &r2);
2443 /*********************************************************
2444 Connect to the client machine.
2445 **********************************************************/
2447 static bool spoolss_connect_to_client(struct rpc_pipe_client **pp_pipe, struct cli_state **pp_cli,
2448 struct sockaddr_storage *client_ss, const char *remote_machine)
2450 NTSTATUS ret;
2451 struct sockaddr_storage rm_addr;
2452 char addr[INET6_ADDRSTRLEN];
2453 struct cli_credentials *anon_creds = NULL;
2455 if ( is_zero_addr(client_ss) ) {
2456 DEBUG(2,("spoolss_connect_to_client: resolving %s\n",
2457 remote_machine));
2458 if ( !resolve_name( remote_machine, &rm_addr, 0x20, false) ) {
2459 DEBUG(2,("spoolss_connect_to_client: Can't resolve address for %s\n", remote_machine));
2460 return false;
2462 print_sockaddr(addr, sizeof(addr), &rm_addr);
2463 } else {
2464 rm_addr = *client_ss;
2465 print_sockaddr(addr, sizeof(addr), &rm_addr);
2466 DEBUG(5,("spoolss_connect_to_client: Using address %s (no name resolution necessary)\n",
2467 addr));
2470 if (ismyaddr((struct sockaddr *)(void *)&rm_addr)) {
2471 DEBUG(0,("spoolss_connect_to_client: Machine %s is one of our addresses. Cannot add to ourselves.\n",
2472 addr));
2473 return false;
2476 anon_creds = cli_credentials_init_anon(NULL);
2477 if (anon_creds == NULL) {
2478 DBG_ERR("cli_credentials_init_anon() failed\n");
2479 return false;
2482 /* setup the connection */
2483 ret = cli_full_connection_creds( pp_cli, lp_netbios_name(), remote_machine,
2484 &rm_addr, 0, "IPC$", "IPC",
2485 anon_creds,
2486 CLI_FULL_CONNECTION_IPC);
2487 TALLOC_FREE(anon_creds);
2488 if ( !NT_STATUS_IS_OK( ret ) ) {
2489 DEBUG(2,("spoolss_connect_to_client: connection to [%s] failed!\n",
2490 remote_machine ));
2491 return false;
2494 if ( smbXcli_conn_protocol((*pp_cli)->conn) < PROTOCOL_NT1 ) {
2495 DEBUG(0,("spoolss_connect_to_client: machine %s didn't negotiate NT protocol.\n", remote_machine));
2496 cli_shutdown(*pp_cli);
2497 return false;
2501 * Ok - we have an anonymous connection to the IPC$ share.
2502 * Now start the NT Domain stuff :-).
2505 ret = cli_rpc_pipe_open_noauth(*pp_cli, &ndr_table_spoolss, pp_pipe);
2506 if (!NT_STATUS_IS_OK(ret)) {
2507 DEBUG(2,("spoolss_connect_to_client: unable to open the spoolss pipe on machine %s. Error was : %s.\n",
2508 remote_machine, nt_errstr(ret)));
2509 cli_shutdown(*pp_cli);
2510 return false;
2513 return true;
2516 /***************************************************************************
2517 Connect to the client.
2518 ****************************************************************************/
2520 static bool srv_spoolss_replyopenprinter(int snum, const char *printer,
2521 uint32_t localprinter,
2522 enum winreg_Type type,
2523 struct policy_handle *handle,
2524 struct notify_back_channel **_chan,
2525 struct sockaddr_storage *client_ss,
2526 struct messaging_context *msg_ctx)
2528 WERROR result;
2529 NTSTATUS status;
2530 struct notify_back_channel *chan;
2532 for (chan = back_channels; chan; chan = chan->next) {
2533 if (memcmp(&chan->client_address, client_ss,
2534 sizeof(struct sockaddr_storage)) == 0) {
2535 break;
2540 * If it's the first connection, contact the client
2541 * and connect to the IPC$ share anonymously
2543 if (!chan) {
2544 fstring unix_printer;
2546 /* the +2 is to strip the leading 2 backslashs */
2547 fstrcpy(unix_printer, printer + 2);
2549 chan = talloc_zero(NULL, struct notify_back_channel);
2550 if (!chan) {
2551 return false;
2553 chan->client_address = *client_ss;
2555 if (!spoolss_connect_to_client(&chan->cli_pipe, &chan->cli, client_ss, unix_printer)) {
2556 TALLOC_FREE(chan);
2557 return false;
2560 DLIST_ADD(back_channels, chan);
2562 messaging_register(msg_ctx, NULL, MSG_PRINTER_NOTIFY2,
2563 receive_notify2_message_list);
2566 if (chan->cli_pipe == NULL ||
2567 chan->cli_pipe->binding_handle == NULL) {
2568 DEBUG(0, ("srv_spoolss_replyopenprinter: error - "
2569 "NULL %s for printer %s\n",
2570 chan->cli_pipe == NULL ?
2571 "chan->cli_pipe" : "chan->cli_pipe->binding_handle",
2572 printer));
2573 return false;
2577 * Tell the specific printing tdb we want messages for this printer
2578 * by registering our PID.
2581 if (!print_notify_register_pid(snum)) {
2582 DEBUG(0, ("Failed to register our pid for printer %s\n",
2583 printer));
2586 status = dcerpc_spoolss_ReplyOpenPrinter(chan->cli_pipe->binding_handle,
2587 talloc_tos(),
2588 printer,
2589 localprinter,
2590 type,
2592 NULL,
2593 handle,
2594 &result);
2595 if (!NT_STATUS_IS_OK(status)) {
2596 DEBUG(5, ("dcerpc_spoolss_ReplyOpenPrinter returned [%s]\n", nt_errstr(status)));
2597 result = ntstatus_to_werror(status);
2598 } else if (!W_ERROR_IS_OK(result)) {
2599 DEBUG(5, ("ReplyOpenPrinter returned [%s]\n", win_errstr(result)));
2602 chan->active_connections++;
2603 *_chan = chan;
2605 return (W_ERROR_IS_OK(result));
2608 /****************************************************************
2609 ****************************************************************/
2611 static struct spoolss_NotifyOption *dup_spoolss_NotifyOption(TALLOC_CTX *mem_ctx,
2612 const struct spoolss_NotifyOption *r)
2614 struct spoolss_NotifyOption *option;
2615 uint32_t i,k;
2617 if (!r) {
2618 return NULL;
2621 option = talloc_zero(mem_ctx, struct spoolss_NotifyOption);
2622 if (!option) {
2623 return NULL;
2626 *option = *r;
2628 if (!option->count) {
2629 return option;
2632 option->types = talloc_zero_array(option,
2633 struct spoolss_NotifyOptionType, option->count);
2634 if (!option->types) {
2635 talloc_free(option);
2636 return NULL;
2639 for (i=0; i < option->count; i++) {
2640 option->types[i] = r->types[i];
2642 if (option->types[i].count) {
2643 option->types[i].fields = talloc_zero_array(option,
2644 union spoolss_Field, option->types[i].count);
2645 if (!option->types[i].fields) {
2646 talloc_free(option);
2647 return NULL;
2649 for (k=0; k<option->types[i].count; k++) {
2650 option->types[i].fields[k] =
2651 r->types[i].fields[k];
2656 return option;
2659 /****************************************************************
2660 * _spoolss_RemoteFindFirstPrinterChangeNotifyEx
2662 * before replying OK: status=0 a rpc call is made to the workstation
2663 * asking ReplyOpenPrinter
2665 * in fact ReplyOpenPrinter is the changenotify equivalent on the spoolss pipe
2666 * called from api_spoolss_rffpcnex
2667 ****************************************************************/
2669 WERROR _spoolss_RemoteFindFirstPrinterChangeNotifyEx(struct pipes_struct *p,
2670 struct spoolss_RemoteFindFirstPrinterChangeNotifyEx *r)
2672 int snum = -1;
2673 struct spoolss_NotifyOption *option = r->in.notify_options;
2674 struct sockaddr_storage client_ss;
2675 ssize_t client_len;
2677 /* store the notify value in the printer struct */
2679 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
2681 if (!Printer) {
2682 DEBUG(2,("_spoolss_RemoteFindFirstPrinterChangeNotifyEx: "
2683 "Invalid handle (%s:%u:%u).\n",
2684 OUR_HANDLE(r->in.handle)));
2685 return WERR_INVALID_HANDLE;
2688 Printer->notify.flags = r->in.flags;
2689 Printer->notify.options = r->in.options;
2690 Printer->notify.printerlocal = r->in.printer_local;
2691 Printer->notify.msg_ctx = p->msg_ctx;
2693 TALLOC_FREE(Printer->notify.option);
2694 Printer->notify.option = dup_spoolss_NotifyOption(Printer, option);
2696 fstrcpy(Printer->notify.localmachine, r->in.local_machine);
2698 /* Connect to the client machine and send a ReplyOpenPrinter */
2700 if ( Printer->printer_type == SPLHND_SERVER)
2701 snum = -1;
2702 else if ( (Printer->printer_type == SPLHND_PRINTER) &&
2703 !get_printer_snum(p, r->in.handle, &snum, NULL) )
2704 return WERR_INVALID_HANDLE;
2706 DEBUG(10,("_spoolss_RemoteFindFirstPrinterChangeNotifyEx: "
2707 "remote_address is %s\n",
2708 tsocket_address_string(p->remote_address, p->mem_ctx)));
2710 if (!lp_print_notify_backchannel(snum)) {
2711 DEBUG(10, ("_spoolss_RemoteFindFirstPrinterChangeNotifyEx: "
2712 "backchannel disabled\n"));
2713 return WERR_RPC_S_SERVER_UNAVAILABLE;
2716 client_len = tsocket_address_bsd_sockaddr(p->remote_address,
2717 (struct sockaddr *) &client_ss,
2718 sizeof(struct sockaddr_storage));
2719 if (client_len < 0) {
2720 return WERR_NOT_ENOUGH_MEMORY;
2723 if(!srv_spoolss_replyopenprinter(snum, Printer->notify.localmachine,
2724 Printer->notify.printerlocal, REG_SZ,
2725 &Printer->notify.cli_hnd,
2726 &Printer->notify.cli_chan,
2727 &client_ss, p->msg_ctx)) {
2728 return WERR_RPC_S_SERVER_UNAVAILABLE;
2731 return WERR_OK;
2734 /*******************************************************************
2735 * fill a notify_info_data with the servername
2736 ********************************************************************/
2738 static void spoolss_notify_server_name(struct messaging_context *msg_ctx,
2739 int snum,
2740 struct spoolss_Notify *data,
2741 print_queue_struct *queue,
2742 struct spoolss_PrinterInfo2 *pinfo2,
2743 TALLOC_CTX *mem_ctx)
2745 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, pinfo2->servername);
2748 /*******************************************************************
2749 * fill a notify_info_data with the printername (not including the servername).
2750 ********************************************************************/
2752 static void spoolss_notify_printer_name(struct messaging_context *msg_ctx,
2753 int snum,
2754 struct spoolss_Notify *data,
2755 print_queue_struct *queue,
2756 struct spoolss_PrinterInfo2 *pinfo2,
2757 TALLOC_CTX *mem_ctx)
2759 /* the notify name should not contain the \\server\ part */
2760 const char *p = strrchr(pinfo2->printername, '\\');
2762 if (!p) {
2763 p = pinfo2->printername;
2764 } else {
2765 p++;
2768 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, p);
2771 /*******************************************************************
2772 * fill a notify_info_data with the servicename
2773 ********************************************************************/
2775 static void spoolss_notify_share_name(struct messaging_context *msg_ctx,
2776 int snum,
2777 struct spoolss_Notify *data,
2778 print_queue_struct *queue,
2779 struct spoolss_PrinterInfo2 *pinfo2,
2780 TALLOC_CTX *mem_ctx)
2782 const struct loadparm_substitution *lp_sub =
2783 loadparm_s3_global_substitution();
2785 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, lp_servicename(talloc_tos(), lp_sub, snum));
2788 /*******************************************************************
2789 * fill a notify_info_data with the port name
2790 ********************************************************************/
2792 static void spoolss_notify_port_name(struct messaging_context *msg_ctx,
2793 int snum,
2794 struct spoolss_Notify *data,
2795 print_queue_struct *queue,
2796 struct spoolss_PrinterInfo2 *pinfo2,
2797 TALLOC_CTX *mem_ctx)
2799 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, pinfo2->portname);
2802 /*******************************************************************
2803 * fill a notify_info_data with the printername
2804 * but it doesn't exist, have to see what to do
2805 ********************************************************************/
2807 static void spoolss_notify_driver_name(struct messaging_context *msg_ctx,
2808 int snum,
2809 struct spoolss_Notify *data,
2810 print_queue_struct *queue,
2811 struct spoolss_PrinterInfo2 *pinfo2,
2812 TALLOC_CTX *mem_ctx)
2814 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, pinfo2->drivername);
2817 /*******************************************************************
2818 * fill a notify_info_data with the comment
2819 ********************************************************************/
2821 static void spoolss_notify_comment(struct messaging_context *msg_ctx,
2822 int snum,
2823 struct spoolss_Notify *data,
2824 print_queue_struct *queue,
2825 struct spoolss_PrinterInfo2 *pinfo2,
2826 TALLOC_CTX *mem_ctx)
2828 const struct loadparm_substitution *lp_sub =
2829 loadparm_s3_global_substitution();
2830 const char *p;
2832 if (*pinfo2->comment == '\0') {
2833 p = lp_comment(talloc_tos(), lp_sub, snum);
2834 } else {
2835 p = pinfo2->comment;
2838 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, p);
2841 /*******************************************************************
2842 * fill a notify_info_data with the comment
2843 * location = "Room 1, floor 2, building 3"
2844 ********************************************************************/
2846 static void spoolss_notify_location(struct messaging_context *msg_ctx,
2847 int snum,
2848 struct spoolss_Notify *data,
2849 print_queue_struct *queue,
2850 struct spoolss_PrinterInfo2 *pinfo2,
2851 TALLOC_CTX *mem_ctx)
2853 const char *loc = pinfo2->location;
2854 NTSTATUS status;
2856 status = printer_list_get_printer(mem_ctx,
2857 pinfo2->sharename,
2858 NULL,
2859 &loc,
2860 NULL);
2861 if (NT_STATUS_IS_OK(status)) {
2862 if (loc == NULL) {
2863 loc = pinfo2->location;
2867 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, loc);
2870 /*******************************************************************
2871 * fill a notify_info_data with the device mode
2872 * jfm:xxxx don't to it for know but that's a real problem !!!
2873 ********************************************************************/
2875 static void spoolss_notify_devmode(struct messaging_context *msg_ctx,
2876 int snum,
2877 struct spoolss_Notify *data,
2878 print_queue_struct *queue,
2879 struct spoolss_PrinterInfo2 *pinfo2,
2880 TALLOC_CTX *mem_ctx)
2882 /* for a dummy implementation we have to zero the fields */
2883 SETUP_SPOOLSS_NOTIFY_DATA_DEVMODE(data, NULL);
2886 /*******************************************************************
2887 * fill a notify_info_data with the separator file name
2888 ********************************************************************/
2890 static void spoolss_notify_sepfile(struct messaging_context *msg_ctx,
2891 int snum,
2892 struct spoolss_Notify *data,
2893 print_queue_struct *queue,
2894 struct spoolss_PrinterInfo2 *pinfo2,
2895 TALLOC_CTX *mem_ctx)
2897 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, pinfo2->sepfile);
2900 /*******************************************************************
2901 * fill a notify_info_data with the print processor
2902 * jfm:xxxx return always winprint to indicate we don't do anything to it
2903 ********************************************************************/
2905 static void spoolss_notify_print_processor(struct messaging_context *msg_ctx,
2906 int snum,
2907 struct spoolss_Notify *data,
2908 print_queue_struct *queue,
2909 struct spoolss_PrinterInfo2 *pinfo2,
2910 TALLOC_CTX *mem_ctx)
2912 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, pinfo2->printprocessor);
2915 /*******************************************************************
2916 * fill a notify_info_data with the print processor options
2917 * jfm:xxxx send an empty string
2918 ********************************************************************/
2920 static void spoolss_notify_parameters(struct messaging_context *msg_ctx,
2921 int snum,
2922 struct spoolss_Notify *data,
2923 print_queue_struct *queue,
2924 struct spoolss_PrinterInfo2 *pinfo2,
2925 TALLOC_CTX *mem_ctx)
2927 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, pinfo2->parameters);
2930 /*******************************************************************
2931 * fill a notify_info_data with the data type
2932 * jfm:xxxx always send RAW as data type
2933 ********************************************************************/
2935 static void spoolss_notify_datatype(struct messaging_context *msg_ctx,
2936 int snum,
2937 struct spoolss_Notify *data,
2938 print_queue_struct *queue,
2939 struct spoolss_PrinterInfo2 *pinfo2,
2940 TALLOC_CTX *mem_ctx)
2942 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, pinfo2->datatype);
2945 /*******************************************************************
2946 * fill a notify_info_data with the security descriptor
2947 * jfm:xxxx send an null pointer to say no security desc
2948 * have to implement security before !
2949 ********************************************************************/
2951 static void spoolss_notify_security_desc(struct messaging_context *msg_ctx,
2952 int snum,
2953 struct spoolss_Notify *data,
2954 print_queue_struct *queue,
2955 struct spoolss_PrinterInfo2 *pinfo2,
2956 TALLOC_CTX *mem_ctx)
2958 if (pinfo2->secdesc == NULL) {
2959 data->data.sd.sd = NULL;
2960 } else {
2961 data->data.sd.sd = security_descriptor_copy(mem_ctx,
2962 pinfo2->secdesc);
2964 data->data.sd.sd_size = ndr_size_security_descriptor(data->data.sd.sd,
2968 /*******************************************************************
2969 * fill a notify_info_data with the attributes
2970 * jfm:xxxx a samba printer is always shared
2971 ********************************************************************/
2973 static void spoolss_notify_attributes(struct messaging_context *msg_ctx,
2974 int snum,
2975 struct spoolss_Notify *data,
2976 print_queue_struct *queue,
2977 struct spoolss_PrinterInfo2 *pinfo2,
2978 TALLOC_CTX *mem_ctx)
2980 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, pinfo2->attributes);
2983 /*******************************************************************
2984 * fill a notify_info_data with the priority
2985 ********************************************************************/
2987 static void spoolss_notify_priority(struct messaging_context *msg_ctx,
2988 int snum,
2989 struct spoolss_Notify *data,
2990 print_queue_struct *queue,
2991 struct spoolss_PrinterInfo2 *pinfo2,
2992 TALLOC_CTX *mem_ctx)
2994 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, pinfo2->priority);
2997 /*******************************************************************
2998 * fill a notify_info_data with the default priority
2999 ********************************************************************/
3001 static void spoolss_notify_default_priority(struct messaging_context *msg_ctx,
3002 int snum,
3003 struct spoolss_Notify *data,
3004 print_queue_struct *queue,
3005 struct spoolss_PrinterInfo2 *pinfo2,
3006 TALLOC_CTX *mem_ctx)
3008 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, pinfo2->defaultpriority);
3011 /*******************************************************************
3012 * fill a notify_info_data with the start time
3013 ********************************************************************/
3015 static void spoolss_notify_start_time(struct messaging_context *msg_ctx,
3016 int snum,
3017 struct spoolss_Notify *data,
3018 print_queue_struct *queue,
3019 struct spoolss_PrinterInfo2 *pinfo2,
3020 TALLOC_CTX *mem_ctx)
3022 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, pinfo2->starttime);
3025 /*******************************************************************
3026 * fill a notify_info_data with the until time
3027 ********************************************************************/
3029 static void spoolss_notify_until_time(struct messaging_context *msg_ctx,
3030 int snum,
3031 struct spoolss_Notify *data,
3032 print_queue_struct *queue,
3033 struct spoolss_PrinterInfo2 *pinfo2,
3034 TALLOC_CTX *mem_ctx)
3036 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, pinfo2->untiltime);
3039 /*******************************************************************
3040 * fill a notify_info_data with the status
3041 ********************************************************************/
3043 static void spoolss_notify_status(struct messaging_context *msg_ctx,
3044 int snum,
3045 struct spoolss_Notify *data,
3046 print_queue_struct *queue,
3047 struct spoolss_PrinterInfo2 *pinfo2,
3048 TALLOC_CTX *mem_ctx)
3050 print_status_struct status;
3052 print_queue_length(msg_ctx, snum, &status);
3053 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, status.status);
3056 /*******************************************************************
3057 * fill a notify_info_data with the number of jobs queued
3058 ********************************************************************/
3060 static void spoolss_notify_cjobs(struct messaging_context *msg_ctx,
3061 int snum,
3062 struct spoolss_Notify *data,
3063 print_queue_struct *queue,
3064 struct spoolss_PrinterInfo2 *pinfo2,
3065 TALLOC_CTX *mem_ctx)
3067 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(
3068 data, print_queue_length(msg_ctx, snum, NULL));
3071 /*******************************************************************
3072 * fill a notify_info_data with the average ppm
3073 ********************************************************************/
3075 static void spoolss_notify_average_ppm(struct messaging_context *msg_ctx,
3076 int snum,
3077 struct spoolss_Notify *data,
3078 print_queue_struct *queue,
3079 struct spoolss_PrinterInfo2 *pinfo2,
3080 TALLOC_CTX *mem_ctx)
3082 /* always respond 8 pages per minutes */
3083 /* a little hard ! */
3084 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, pinfo2->averageppm);
3087 /*******************************************************************
3088 * fill a notify_info_data with username
3089 ********************************************************************/
3091 static void spoolss_notify_username(struct messaging_context *msg_ctx,
3092 int snum,
3093 struct spoolss_Notify *data,
3094 print_queue_struct *queue,
3095 struct spoolss_PrinterInfo2 *pinfo2,
3096 TALLOC_CTX *mem_ctx)
3098 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, queue->fs_user);
3101 /*******************************************************************
3102 * fill a notify_info_data with job status
3103 ********************************************************************/
3105 static void spoolss_notify_job_status(struct messaging_context *msg_ctx,
3106 int snum,
3107 struct spoolss_Notify *data,
3108 print_queue_struct *queue,
3109 struct spoolss_PrinterInfo2 *pinfo2,
3110 TALLOC_CTX *mem_ctx)
3112 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, nt_printj_status(queue->status));
3115 /*******************************************************************
3116 * fill a notify_info_data with job name
3117 ********************************************************************/
3119 static void spoolss_notify_job_name(struct messaging_context *msg_ctx,
3120 int snum,
3121 struct spoolss_Notify *data,
3122 print_queue_struct *queue,
3123 struct spoolss_PrinterInfo2 *pinfo2,
3124 TALLOC_CTX *mem_ctx)
3126 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, queue->fs_file);
3129 /*******************************************************************
3130 * fill a notify_info_data with job status
3131 ********************************************************************/
3133 static void spoolss_notify_job_status_string(struct messaging_context *msg_ctx,
3134 int snum,
3135 struct spoolss_Notify *data,
3136 print_queue_struct *queue,
3137 struct spoolss_PrinterInfo2 *pinfo2,
3138 TALLOC_CTX *mem_ctx)
3141 * Now we're returning job status codes we just return a "" here. JRA.
3144 const char *p = "";
3146 #if 0 /* NO LONGER NEEDED - JRA. 02/22/2001 */
3147 p = "unknown";
3149 switch (queue->status) {
3150 case LPQ_QUEUED:
3151 p = "Queued";
3152 break;
3153 case LPQ_PAUSED:
3154 p = ""; /* NT provides the paused string */
3155 break;
3156 case LPQ_SPOOLING:
3157 p = "Spooling";
3158 break;
3159 case LPQ_PRINTING:
3160 p = "Printing";
3161 break;
3163 #endif /* NO LONGER NEEDED. */
3165 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, p);
3168 /*******************************************************************
3169 * fill a notify_info_data with job time
3170 ********************************************************************/
3172 static void spoolss_notify_job_time(struct messaging_context *msg_ctx,
3173 int snum,
3174 struct spoolss_Notify *data,
3175 print_queue_struct *queue,
3176 struct spoolss_PrinterInfo2 *pinfo2,
3177 TALLOC_CTX *mem_ctx)
3179 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, 0);
3182 /*******************************************************************
3183 * fill a notify_info_data with job size
3184 ********************************************************************/
3186 static void spoolss_notify_job_size(struct messaging_context *msg_ctx,
3187 int snum,
3188 struct spoolss_Notify *data,
3189 print_queue_struct *queue,
3190 struct spoolss_PrinterInfo2 *pinfo2,
3191 TALLOC_CTX *mem_ctx)
3193 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, queue->size);
3196 /*******************************************************************
3197 * fill a notify_info_data with page info
3198 ********************************************************************/
3199 static void spoolss_notify_total_pages(struct messaging_context *msg_ctx,
3200 int snum,
3201 struct spoolss_Notify *data,
3202 print_queue_struct *queue,
3203 struct spoolss_PrinterInfo2 *pinfo2,
3204 TALLOC_CTX *mem_ctx)
3206 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, queue->page_count);
3209 /*******************************************************************
3210 * fill a notify_info_data with pages printed info.
3211 ********************************************************************/
3212 static void spoolss_notify_pages_printed(struct messaging_context *msg_ctx,
3213 int snum,
3214 struct spoolss_Notify *data,
3215 print_queue_struct *queue,
3216 struct spoolss_PrinterInfo2 *pinfo2,
3217 TALLOC_CTX *mem_ctx)
3219 /* Add code when back-end tracks this */
3220 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, 0);
3223 /*******************************************************************
3224 Fill a notify_info_data with job position.
3225 ********************************************************************/
3227 static void spoolss_notify_job_position(struct messaging_context *msg_ctx,
3228 int snum,
3229 struct spoolss_Notify *data,
3230 print_queue_struct *queue,
3231 struct spoolss_PrinterInfo2 *pinfo2,
3232 TALLOC_CTX *mem_ctx)
3234 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, queue->sysjob);
3237 /*******************************************************************
3238 Fill a notify_info_data with submitted time.
3239 ********************************************************************/
3241 static void spoolss_notify_submitted_time(struct messaging_context *msg_ctx,
3242 int snum,
3243 struct spoolss_Notify *data,
3244 print_queue_struct *queue,
3245 struct spoolss_PrinterInfo2 *pinfo2,
3246 TALLOC_CTX *mem_ctx)
3248 data->data.string.string = NULL;
3249 data->data.string.size = 0;
3251 init_systemtime_buffer(mem_ctx, gmtime(&queue->time),
3252 &data->data.string.string,
3253 &data->data.string.size);
3257 struct s_notify_info_data_table
3259 enum spoolss_NotifyType type;
3260 uint16_t field;
3261 const char *name;
3262 enum spoolss_NotifyTable variable_type;
3263 void (*fn) (struct messaging_context *msg_ctx,
3264 int snum, struct spoolss_Notify *data,
3265 print_queue_struct *queue,
3266 struct spoolss_PrinterInfo2 *pinfo2,
3267 TALLOC_CTX *mem_ctx);
3270 /* A table describing the various print notification constants and
3271 whether the notification data is a pointer to a variable sized
3272 buffer, a one value uint32_t or a two value uint32_t. */
3274 static const struct s_notify_info_data_table notify_info_data_table[] =
3276 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_SERVER_NAME, "PRINTER_NOTIFY_FIELD_SERVER_NAME", NOTIFY_TABLE_STRING, spoolss_notify_server_name },
3277 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_PRINTER_NAME, "PRINTER_NOTIFY_FIELD_PRINTER_NAME", NOTIFY_TABLE_STRING, spoolss_notify_printer_name },
3278 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_SHARE_NAME, "PRINTER_NOTIFY_FIELD_SHARE_NAME", NOTIFY_TABLE_STRING, spoolss_notify_share_name },
3279 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_PORT_NAME, "PRINTER_NOTIFY_FIELD_PORT_NAME", NOTIFY_TABLE_STRING, spoolss_notify_port_name },
3280 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_DRIVER_NAME, "PRINTER_NOTIFY_FIELD_DRIVER_NAME", NOTIFY_TABLE_STRING, spoolss_notify_driver_name },
3281 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_COMMENT, "PRINTER_NOTIFY_FIELD_COMMENT", NOTIFY_TABLE_STRING, spoolss_notify_comment },
3282 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_LOCATION, "PRINTER_NOTIFY_FIELD_LOCATION", NOTIFY_TABLE_STRING, spoolss_notify_location },
3283 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_DEVMODE, "PRINTER_NOTIFY_FIELD_DEVMODE", NOTIFY_TABLE_DEVMODE, spoolss_notify_devmode },
3284 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_SEPFILE, "PRINTER_NOTIFY_FIELD_SEPFILE", NOTIFY_TABLE_STRING, spoolss_notify_sepfile },
3285 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_PRINT_PROCESSOR, "PRINTER_NOTIFY_FIELD_PRINT_PROCESSOR", NOTIFY_TABLE_STRING, spoolss_notify_print_processor },
3286 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_PARAMETERS, "PRINTER_NOTIFY_FIELD_PARAMETERS", NOTIFY_TABLE_STRING, spoolss_notify_parameters },
3287 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_DATATYPE, "PRINTER_NOTIFY_FIELD_DATATYPE", NOTIFY_TABLE_STRING, spoolss_notify_datatype },
3288 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_SECURITY_DESCRIPTOR, "PRINTER_NOTIFY_FIELD_SECURITY_DESCRIPTOR", NOTIFY_TABLE_SECURITYDESCRIPTOR, spoolss_notify_security_desc },
3289 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_ATTRIBUTES, "PRINTER_NOTIFY_FIELD_ATTRIBUTES", NOTIFY_TABLE_DWORD, spoolss_notify_attributes },
3290 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_PRIORITY, "PRINTER_NOTIFY_FIELD_PRIORITY", NOTIFY_TABLE_DWORD, spoolss_notify_priority },
3291 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_DEFAULT_PRIORITY, "PRINTER_NOTIFY_FIELD_DEFAULT_PRIORITY", NOTIFY_TABLE_DWORD, spoolss_notify_default_priority },
3292 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_START_TIME, "PRINTER_NOTIFY_FIELD_START_TIME", NOTIFY_TABLE_DWORD, spoolss_notify_start_time },
3293 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_UNTIL_TIME, "PRINTER_NOTIFY_FIELD_UNTIL_TIME", NOTIFY_TABLE_DWORD, spoolss_notify_until_time },
3294 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_STATUS, "PRINTER_NOTIFY_FIELD_STATUS", NOTIFY_TABLE_DWORD, spoolss_notify_status },
3295 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_STATUS_STRING, "PRINTER_NOTIFY_FIELD_STATUS_STRING", NOTIFY_TABLE_STRING, NULL },
3296 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_CJOBS, "PRINTER_NOTIFY_FIELD_CJOBS", NOTIFY_TABLE_DWORD, spoolss_notify_cjobs },
3297 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_AVERAGE_PPM, "PRINTER_NOTIFY_FIELD_AVERAGE_PPM", NOTIFY_TABLE_DWORD, spoolss_notify_average_ppm },
3298 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_TOTAL_PAGES, "PRINTER_NOTIFY_FIELD_TOTAL_PAGES", NOTIFY_TABLE_DWORD, NULL },
3299 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_PAGES_PRINTED, "PRINTER_NOTIFY_FIELD_PAGES_PRINTED", NOTIFY_TABLE_DWORD, NULL },
3300 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_TOTAL_BYTES, "PRINTER_NOTIFY_FIELD_TOTAL_BYTES", NOTIFY_TABLE_DWORD, NULL },
3301 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_BYTES_PRINTED, "PRINTER_NOTIFY_FIELD_BYTES_PRINTED", NOTIFY_TABLE_DWORD, NULL },
3302 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_PRINTER_NAME, "JOB_NOTIFY_FIELD_PRINTER_NAME", NOTIFY_TABLE_STRING, spoolss_notify_printer_name },
3303 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_MACHINE_NAME, "JOB_NOTIFY_FIELD_MACHINE_NAME", NOTIFY_TABLE_STRING, spoolss_notify_server_name },
3304 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_PORT_NAME, "JOB_NOTIFY_FIELD_PORT_NAME", NOTIFY_TABLE_STRING, spoolss_notify_port_name },
3305 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_USER_NAME, "JOB_NOTIFY_FIELD_USER_NAME", NOTIFY_TABLE_STRING, spoolss_notify_username },
3306 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_NOTIFY_NAME, "JOB_NOTIFY_FIELD_NOTIFY_NAME", NOTIFY_TABLE_STRING, spoolss_notify_username },
3307 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_DATATYPE, "JOB_NOTIFY_FIELD_DATATYPE", NOTIFY_TABLE_STRING, spoolss_notify_datatype },
3308 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_PRINT_PROCESSOR, "JOB_NOTIFY_FIELD_PRINT_PROCESSOR", NOTIFY_TABLE_STRING, spoolss_notify_print_processor },
3309 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_PARAMETERS, "JOB_NOTIFY_FIELD_PARAMETERS", NOTIFY_TABLE_STRING, spoolss_notify_parameters },
3310 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_DRIVER_NAME, "JOB_NOTIFY_FIELD_DRIVER_NAME", NOTIFY_TABLE_STRING, spoolss_notify_driver_name },
3311 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_DEVMODE, "JOB_NOTIFY_FIELD_DEVMODE", NOTIFY_TABLE_DEVMODE, spoolss_notify_devmode },
3312 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_STATUS, "JOB_NOTIFY_FIELD_STATUS", NOTIFY_TABLE_DWORD, spoolss_notify_job_status },
3313 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_STATUS_STRING, "JOB_NOTIFY_FIELD_STATUS_STRING", NOTIFY_TABLE_STRING, spoolss_notify_job_status_string },
3314 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_SECURITY_DESCRIPTOR, "JOB_NOTIFY_FIELD_SECURITY_DESCRIPTOR", NOTIFY_TABLE_SECURITYDESCRIPTOR, NULL },
3315 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_DOCUMENT, "JOB_NOTIFY_FIELD_DOCUMENT", NOTIFY_TABLE_STRING, spoolss_notify_job_name },
3316 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_PRIORITY, "JOB_NOTIFY_FIELD_PRIORITY", NOTIFY_TABLE_DWORD, spoolss_notify_priority },
3317 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_POSITION, "JOB_NOTIFY_FIELD_POSITION", NOTIFY_TABLE_DWORD, spoolss_notify_job_position },
3318 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_SUBMITTED, "JOB_NOTIFY_FIELD_SUBMITTED", NOTIFY_TABLE_TIME, spoolss_notify_submitted_time },
3319 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_START_TIME, "JOB_NOTIFY_FIELD_START_TIME", NOTIFY_TABLE_DWORD, spoolss_notify_start_time },
3320 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_UNTIL_TIME, "JOB_NOTIFY_FIELD_UNTIL_TIME", NOTIFY_TABLE_DWORD, spoolss_notify_until_time },
3321 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_TIME, "JOB_NOTIFY_FIELD_TIME", NOTIFY_TABLE_DWORD, spoolss_notify_job_time },
3322 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_TOTAL_PAGES, "JOB_NOTIFY_FIELD_TOTAL_PAGES", NOTIFY_TABLE_DWORD, spoolss_notify_total_pages },
3323 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_PAGES_PRINTED, "JOB_NOTIFY_FIELD_PAGES_PRINTED", NOTIFY_TABLE_DWORD, spoolss_notify_pages_printed },
3324 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_TOTAL_BYTES, "JOB_NOTIFY_FIELD_TOTAL_BYTES", NOTIFY_TABLE_DWORD, spoolss_notify_job_size },
3327 /*******************************************************************
3328 Return the variable_type of info_data structure.
3329 ********************************************************************/
3331 static enum spoolss_NotifyTable variable_type_of_notify_info_data(enum spoolss_NotifyType type,
3332 uint16_t field)
3334 int i=0;
3336 for (i = 0; i < ARRAY_SIZE(notify_info_data_table); i++) {
3337 if ( (notify_info_data_table[i].type == type) &&
3338 (notify_info_data_table[i].field == field) ) {
3339 return notify_info_data_table[i].variable_type;
3343 DEBUG(5, ("invalid notify data type %d/%d\n", type, field));
3345 return (enum spoolss_NotifyTable) 0;
3348 /****************************************************************************
3349 ****************************************************************************/
3351 static bool search_notify(enum spoolss_NotifyType type,
3352 uint16_t field,
3353 int *value)
3355 int i;
3357 for (i = 0; i < ARRAY_SIZE(notify_info_data_table); i++) {
3358 if (notify_info_data_table[i].type == type &&
3359 notify_info_data_table[i].field == field &&
3360 notify_info_data_table[i].fn != NULL) {
3361 *value = i;
3362 return true;
3366 return false;
3369 /****************************************************************************
3370 ****************************************************************************/
3372 static void construct_info_data(struct spoolss_Notify *info_data,
3373 enum spoolss_NotifyType type,
3374 uint16_t field, int id)
3376 info_data->type = type;
3377 info_data->field.field = field;
3378 info_data->variable_type = variable_type_of_notify_info_data(type, field);
3379 info_data->job_id = id;
3382 /*******************************************************************
3384 * fill a notify_info struct with info asked
3386 ********************************************************************/
3388 static bool construct_notify_printer_info(struct messaging_context *msg_ctx,
3389 struct printer_handle *print_hnd,
3390 struct spoolss_NotifyInfo *info,
3391 struct spoolss_PrinterInfo2 *pinfo2,
3392 int snum,
3393 const struct spoolss_NotifyOptionType *option_type,
3394 uint32_t id,
3395 TALLOC_CTX *mem_ctx)
3397 const struct loadparm_substitution *lp_sub =
3398 loadparm_s3_global_substitution();
3399 int field_num,j;
3400 enum spoolss_NotifyType type;
3401 uint16_t field;
3403 struct spoolss_Notify *current_data;
3405 type = option_type->type;
3407 DEBUG(4,("construct_notify_printer_info: Notify type: [%s], number of notify info: [%d] on printer: [%s]\n",
3408 (type == PRINTER_NOTIFY_TYPE ? "PRINTER_NOTIFY_TYPE" : "JOB_NOTIFY_TYPE"),
3409 option_type->count, lp_servicename(talloc_tos(), lp_sub, snum)));
3411 for(field_num=0; field_num < option_type->count; field_num++) {
3412 field = option_type->fields[field_num].field;
3414 DEBUG(4,("construct_notify_printer_info: notify [%d]: type [%x], field [%x]\n", field_num, type, field));
3416 if (!search_notify(type, field, &j) )
3417 continue;
3419 info->notifies = talloc_realloc(info, info->notifies,
3420 struct spoolss_Notify,
3421 info->count + 1);
3422 if (info->notifies == NULL) {
3423 DEBUG(2,("construct_notify_printer_info: failed to enlarge buffer info->data!\n"));
3424 return false;
3427 current_data = &info->notifies[info->count];
3429 construct_info_data(current_data, type, field, id);
3431 DEBUG(10, ("construct_notify_printer_info: "
3432 "calling [%s] snum=%d printername=[%s])\n",
3433 notify_info_data_table[j].name, snum,
3434 pinfo2->printername));
3436 notify_info_data_table[j].fn(msg_ctx, snum, current_data,
3437 NULL, pinfo2, mem_ctx);
3439 info->count++;
3442 return true;
3445 /*******************************************************************
3447 * fill a notify_info struct with info asked
3449 ********************************************************************/
3451 static bool construct_notify_jobs_info(struct messaging_context *msg_ctx,
3452 print_queue_struct *queue,
3453 struct spoolss_NotifyInfo *info,
3454 struct spoolss_PrinterInfo2 *pinfo2,
3455 int snum,
3456 const struct spoolss_NotifyOptionType *option_type,
3457 uint32_t id,
3458 TALLOC_CTX *mem_ctx)
3460 int field_num,j;
3461 enum spoolss_NotifyType type;
3462 uint16_t field;
3463 struct spoolss_Notify *current_data;
3465 DEBUG(4,("construct_notify_jobs_info\n"));
3467 type = option_type->type;
3469 DEBUGADD(4,("Notify type: [%s], number of notify info: [%d]\n",
3470 (type == PRINTER_NOTIFY_TYPE ? "PRINTER_NOTIFY_TYPE" : "JOB_NOTIFY_TYPE"),
3471 option_type->count));
3473 for(field_num=0; field_num<option_type->count; field_num++) {
3474 field = option_type->fields[field_num].field;
3476 if (!search_notify(type, field, &j) )
3477 continue;
3479 info->notifies = talloc_realloc(info, info->notifies,
3480 struct spoolss_Notify,
3481 info->count + 1);
3482 if (info->notifies == NULL) {
3483 DEBUG(2,("construct_notify_jobs_info: failed to enlarg buffer info->data!\n"));
3484 return false;
3487 current_data=&(info->notifies[info->count]);
3489 construct_info_data(current_data, type, field, id);
3490 notify_info_data_table[j].fn(msg_ctx, snum, current_data,
3491 queue, pinfo2, mem_ctx);
3492 info->count++;
3495 return true;
3499 * JFM: The enumeration is not that simple, it's even non obvious.
3501 * let's take an example: I want to monitor the PRINTER SERVER for
3502 * the printer's name and the number of jobs currently queued.
3503 * So in the NOTIFY_OPTION, I have one NOTIFY_OPTION_TYPE structure.
3504 * Its type is PRINTER_NOTIFY_TYPE and it has 2 fields NAME and CJOBS.
3506 * I have 3 printers on the back of my server.
3508 * Now the response is a NOTIFY_INFO structure, with 6 NOTIFY_INFO_DATA
3509 * structures.
3510 * Number Data Id
3511 * 1 printer 1 name 1
3512 * 2 printer 1 cjob 1
3513 * 3 printer 2 name 2
3514 * 4 printer 2 cjob 2
3515 * 5 printer 3 name 3
3516 * 6 printer 3 name 3
3518 * that's the print server case, the printer case is even worse.
3521 /*******************************************************************
3523 * enumerate all printers on the printserver
3524 * fill a notify_info struct with info asked
3526 ********************************************************************/
3528 static WERROR printserver_notify_info(struct pipes_struct *p,
3529 struct policy_handle *hnd,
3530 struct spoolss_NotifyInfo *info,
3531 TALLOC_CTX *mem_ctx)
3533 const struct loadparm_substitution *lp_sub =
3534 loadparm_s3_global_substitution();
3535 int snum;
3536 struct printer_handle *Printer = find_printer_index_by_hnd(p, hnd);
3537 int n_services=lp_numservices();
3538 int i;
3539 struct spoolss_NotifyOption *option;
3540 struct spoolss_NotifyOptionType option_type;
3541 struct spoolss_PrinterInfo2 *pinfo2 = NULL;
3542 WERROR result;
3544 DEBUG(4,("printserver_notify_info\n"));
3546 if (!Printer)
3547 return WERR_INVALID_HANDLE;
3549 option = Printer->notify.option;
3551 info->version = 2;
3552 info->notifies = NULL;
3553 info->count = 0;
3555 /* a bug in xp sp2 rc2 causes it to send a fnpcn request without
3556 sending a ffpcn() request first */
3558 if ( !option )
3559 return WERR_INVALID_HANDLE;
3561 for (i=0; i<option->count; i++) {
3562 option_type = option->types[i];
3564 if (option_type.type != PRINTER_NOTIFY_TYPE)
3565 continue;
3567 for (snum = 0; snum < n_services; snum++) {
3568 if (!lp_browseable(snum) ||
3569 !lp_snum_ok(snum) ||
3570 !lp_printable(snum)) {
3571 continue; /* skip */
3574 /* Maybe we should use the SYSTEM session_info here... */
3575 result = winreg_get_printer_internal(mem_ctx,
3576 get_session_info_system(),
3577 p->msg_ctx,
3578 lp_servicename(talloc_tos(), lp_sub, snum),
3579 &pinfo2);
3580 if (!W_ERROR_IS_OK(result)) {
3581 DEBUG(4, ("printserver_notify_info: "
3582 "Failed to get printer [%s]\n",
3583 lp_servicename(talloc_tos(), lp_sub, snum)));
3584 continue;
3588 construct_notify_printer_info(p->msg_ctx,
3589 Printer, info,
3590 pinfo2, snum,
3591 &option_type, snum,
3592 mem_ctx);
3594 TALLOC_FREE(pinfo2);
3598 #if 0
3600 * Debugging information, don't delete.
3603 DEBUG(1,("dumping the NOTIFY_INFO\n"));
3604 DEBUGADD(1,("info->version:[%d], info->flags:[%d], info->count:[%d]\n", info->version, info->flags, info->count));
3605 DEBUGADD(1,("num\ttype\tfield\tres\tid\tsize\tenc_type\n"));
3607 for (i=0; i<info->count; i++) {
3608 DEBUGADD(1,("[%d]\t[%d]\t[%d]\t[%d]\t[%d]\t[%d]\t[%d]\n",
3609 i, info->data[i].type, info->data[i].field, info->data[i].reserved,
3610 info->data[i].id, info->data[i].size, info->data[i].enc_type));
3612 #endif
3614 return WERR_OK;
3617 /*******************************************************************
3619 * fill a notify_info struct with info asked
3621 ********************************************************************/
3623 static WERROR printer_notify_info(struct pipes_struct *p,
3624 struct policy_handle *hnd,
3625 struct spoolss_NotifyInfo *info,
3626 TALLOC_CTX *mem_ctx)
3628 const struct loadparm_substitution *lp_sub =
3629 loadparm_s3_global_substitution();
3630 int snum;
3631 struct printer_handle *Printer = find_printer_index_by_hnd(p, hnd);
3632 int i;
3633 uint32_t id;
3634 struct spoolss_NotifyOption *option;
3635 struct spoolss_NotifyOptionType option_type;
3636 int count,j;
3637 print_queue_struct *queue=NULL;
3638 print_status_struct status;
3639 struct spoolss_PrinterInfo2 *pinfo2 = NULL;
3640 WERROR result;
3641 struct tdb_print_db *pdb;
3643 DEBUG(4,("printer_notify_info\n"));
3645 if (!Printer)
3646 return WERR_INVALID_HANDLE;
3648 option = Printer->notify.option;
3649 id = 0x0;
3651 info->version = 2;
3652 info->notifies = NULL;
3653 info->count = 0;
3655 /* a bug in xp sp2 rc2 causes it to send a fnpcn request without
3656 sending a ffpcn() request first */
3658 if ( !option )
3659 return WERR_INVALID_HANDLE;
3661 if (!get_printer_snum(p, hnd, &snum, NULL)) {
3662 return WERR_INVALID_HANDLE;
3665 pdb = get_print_db_byname(Printer->sharename);
3666 if (pdb == NULL) {
3667 return WERR_INVALID_HANDLE;
3670 /* Maybe we should use the SYSTEM session_info here... */
3671 result = winreg_get_printer_internal(mem_ctx,
3672 get_session_info_system(),
3673 p->msg_ctx,
3674 lp_servicename(talloc_tos(), lp_sub, snum), &pinfo2);
3675 if (!W_ERROR_IS_OK(result)) {
3676 result = WERR_INVALID_HANDLE;
3677 goto err_pdb_drop;
3681 * When sending a PRINTER_NOTIFY_FIELD_SERVER_NAME we should send the
3682 * correct servername.
3684 pinfo2->servername = talloc_strdup(pinfo2, Printer->servername);
3685 if (pinfo2->servername == NULL) {
3686 result = WERR_NOT_ENOUGH_MEMORY;
3687 goto err_pdb_drop;
3690 for (i = 0; i < option->count; i++) {
3691 option_type = option->types[i];
3693 switch (option_type.type) {
3694 case PRINTER_NOTIFY_TYPE:
3695 if (construct_notify_printer_info(p->msg_ctx,
3696 Printer, info,
3697 pinfo2, snum,
3698 &option_type, id,
3699 mem_ctx)) {
3700 id--;
3702 break;
3704 case JOB_NOTIFY_TYPE:
3706 count = print_queue_status(p->msg_ctx, snum, &queue,
3707 &status);
3709 for (j = 0; j < count; j++) {
3710 uint32_t jobid;
3711 jobid = sysjob_to_jobid_pdb(pdb,
3712 queue[j].sysjob);
3713 if (jobid == (uint32_t)-1) {
3714 DEBUG(2, ("ignoring untracked job %d\n",
3715 queue[j].sysjob));
3716 continue;
3718 /* FIXME check return value */
3719 construct_notify_jobs_info(p->msg_ctx,
3720 &queue[j], info,
3721 pinfo2, snum,
3722 &option_type,
3723 jobid,
3724 mem_ctx);
3727 SAFE_FREE(queue);
3728 break;
3733 * Debugging information, don't delete.
3736 DEBUG(1,("dumping the NOTIFY_INFO\n"));
3737 DEBUGADD(1,("info->version:[%d], info->flags:[%d], info->count:[%d]\n", info->version, info->flags, info->count));
3738 DEBUGADD(1,("num\ttype\tfield\tres\tid\tsize\tenc_type\n"));
3740 for (i=0; i<info->count; i++) {
3741 DEBUGADD(1,("[%d]\t[%d]\t[%d]\t[%d]\t[%d]\t[%d]\t[%d]\n",
3742 i, info->data[i].type, info->data[i].field, info->data[i].reserved,
3743 info->data[i].id, info->data[i].size, info->data[i].enc_type));
3747 talloc_free(pinfo2);
3748 result = WERR_OK;
3749 err_pdb_drop:
3750 release_print_db(pdb);
3751 return result;
3754 /****************************************************************
3755 _spoolss_RouterRefreshPrinterChangeNotify
3756 ****************************************************************/
3758 WERROR _spoolss_RouterRefreshPrinterChangeNotify(struct pipes_struct *p,
3759 struct spoolss_RouterRefreshPrinterChangeNotify *r)
3761 struct spoolss_NotifyInfo *info;
3763 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
3764 WERROR result = WERR_INVALID_HANDLE;
3766 /* we always have a spoolss_NotifyInfo struct */
3767 info = talloc_zero(p->mem_ctx, struct spoolss_NotifyInfo);
3768 if (!info) {
3769 result = WERR_NOT_ENOUGH_MEMORY;
3770 goto done;
3773 *r->out.info = info;
3775 if (!Printer) {
3776 DEBUG(2,("_spoolss_RouterRefreshPrinterChangeNotify: "
3777 "Invalid handle (%s:%u:%u).\n",
3778 OUR_HANDLE(r->in.handle)));
3779 goto done;
3782 DEBUG(4,("Printer type %x\n",Printer->printer_type));
3785 * We are now using the change value, and
3786 * I should check for PRINTER_NOTIFY_OPTIONS_REFRESH but as
3787 * I don't have a global notification system, I'm sending back all the
3788 * information even when _NOTHING_ has changed.
3791 /* We need to keep track of the change value to send back in
3792 RRPCN replies otherwise our updates are ignored. */
3794 Printer->notify.fnpcn = true;
3796 if (Printer->notify.cli_chan != NULL &&
3797 Printer->notify.cli_chan->active_connections > 0) {
3798 DEBUG(10,("_spoolss_RouterRefreshPrinterChangeNotify: "
3799 "Saving change value in request [%x]\n",
3800 r->in.change_low));
3801 Printer->notify.change = r->in.change_low;
3804 /* just ignore the spoolss_NotifyOption */
3806 switch (Printer->printer_type) {
3807 case SPLHND_SERVER:
3808 result = printserver_notify_info(p, r->in.handle,
3809 info, p->mem_ctx);
3810 break;
3812 case SPLHND_PRINTER:
3813 result = printer_notify_info(p, r->in.handle,
3814 info, p->mem_ctx);
3815 break;
3818 Printer->notify.fnpcn = false;
3820 done:
3821 return result;
3824 /********************************************************************
3825 ********************************************************************/
3827 static WERROR create_printername(TALLOC_CTX *mem_ctx,
3828 const char *servername,
3829 const char *printername,
3830 const char **printername_p)
3832 /* FIXME: add lp_force_printername() */
3834 if (servername == NULL) {
3835 *printername_p = talloc_strdup(mem_ctx, printername);
3836 W_ERROR_HAVE_NO_MEMORY(*printername_p);
3837 return WERR_OK;
3840 if (servername[0] == '\\' && servername[1] == '\\') {
3841 servername += 2;
3844 *printername_p = talloc_asprintf(mem_ctx, "\\\\%s\\%s", servername, printername);
3845 W_ERROR_HAVE_NO_MEMORY(*printername_p);
3847 return WERR_OK;
3850 /********************************************************************
3851 ********************************************************************/
3853 static void compose_devicemode_devicename(struct spoolss_DeviceMode *dm,
3854 const char *printername)
3856 if (dm == NULL) {
3857 return;
3860 dm->devicename = talloc_strndup(dm, printername,
3861 MIN(strlen(printername), 31));
3864 /********************************************************************
3865 * construct_printer_info_0
3866 * fill a printer_info_0 struct
3867 ********************************************************************/
3869 static WERROR construct_printer_info0(TALLOC_CTX *mem_ctx,
3870 const struct auth_session_info *session_info,
3871 struct messaging_context *msg_ctx,
3872 struct spoolss_PrinterInfo2 *info2,
3873 const char *servername,
3874 struct spoolss_PrinterInfo0 *r,
3875 int snum)
3877 int count;
3878 struct printer_session_counter *session_counter;
3879 struct timeval setuptime;
3880 print_status_struct status;
3881 WERROR result;
3882 int os_major, os_minor, os_build;
3883 const char *architecture;
3884 uint32_t processor_architecture, processor_type;
3886 result = create_printername(mem_ctx, servername, info2->printername, &r->printername);
3887 if (!W_ERROR_IS_OK(result)) {
3888 return result;
3891 if (servername) {
3892 r->servername = talloc_strdup(mem_ctx, servername);
3893 W_ERROR_HAVE_NO_MEMORY(r->servername);
3894 } else {
3895 r->servername = NULL;
3898 count = print_queue_length(msg_ctx, snum, &status);
3900 /* check if we already have a counter for this printer */
3901 for (session_counter = counter_list; session_counter; session_counter = session_counter->next) {
3902 if (session_counter->snum == snum)
3903 break;
3906 /* it's the first time, add it to the list */
3907 if (session_counter == NULL) {
3908 session_counter = talloc_zero(counter_list, struct printer_session_counter);
3909 W_ERROR_HAVE_NO_MEMORY(session_counter);
3910 session_counter->snum = snum;
3911 session_counter->counter = 0;
3912 DLIST_ADD(counter_list, session_counter);
3915 /* increment it */
3916 session_counter->counter++;
3918 r->cjobs = count;
3919 r->total_jobs = 0;
3920 r->total_bytes = 0;
3922 get_startup_time(&setuptime);
3923 init_systemtime(&r->time, gmtime(&setuptime.tv_sec));
3925 /* JFM:
3926 * the global_counter should be stored in a TDB as it's common to all the clients
3927 * and should be zeroed on samba startup
3929 r->global_counter = session_counter->counter;
3930 r->total_pages = 0;
3932 /* in 2.2 we reported ourselves as 0x0004 and 0x0565 */
3933 os_major = lp_parm_int(GLOBAL_SECTION_SNUM,
3934 "spoolss", "os_major",
3935 GLOBAL_SPOOLSS_OS_MAJOR_DEFAULT);
3936 os_minor = lp_parm_int(GLOBAL_SECTION_SNUM,
3937 "spoolss", "os_minor",
3938 GLOBAL_SPOOLSS_OS_MINOR_DEFAULT);
3939 os_build = lp_parm_int(GLOBAL_SECTION_SNUM,
3940 "spoolss", "os_build",
3941 GLOBAL_SPOOLSS_OS_BUILD_DEFAULT);
3943 SCVAL(&r->version, 0, os_major);
3944 SCVAL(&r->version, 1, os_minor);
3945 SSVAL(&r->version, 2, os_build);
3947 architecture = lp_parm_const_string(GLOBAL_SECTION_SNUM,
3948 "spoolss",
3949 "architecture",
3950 GLOBAL_SPOOLSS_ARCHITECTURE);
3952 if (strequal(architecture, SPOOLSS_ARCHITECTURE_x64)) {
3953 processor_architecture = PROCESSOR_ARCHITECTURE_AMD64;
3954 processor_type = PROCESSOR_AMD_X8664;
3955 } else {
3956 processor_architecture = PROCESSOR_ARCHITECTURE_INTEL;
3957 processor_type = PROCESSOR_INTEL_PENTIUM;
3960 r->free_build = SPOOLSS_RELEASE_BUILD;
3961 r->spooling = 0;
3962 r->max_spooling = 0;
3963 r->session_counter = session_counter->counter;
3964 r->num_error_out_of_paper = 0x0;
3965 r->num_error_not_ready = 0x0; /* number of print failure */
3966 r->job_error = 0x0;
3967 r->number_of_processors = 0x1;
3968 r->processor_type = processor_type;
3969 r->high_part_total_bytes = 0x0;
3971 /* ChangeID in milliseconds*/
3972 winreg_printer_get_changeid_internal(mem_ctx, session_info, msg_ctx,
3973 info2->sharename, &r->change_id);
3975 r->last_error = WERR_OK;
3976 r->status = nt_printq_status(status.status);
3977 r->enumerate_network_printers = 0x0;
3978 r->c_setprinter = 0x0;
3979 r->processor_architecture = processor_architecture;
3980 r->processor_level = 0x6; /* 6 ???*/
3981 r->ref_ic = 0;
3982 r->reserved2 = 0;
3983 r->reserved3 = 0;
3985 return WERR_OK;
3989 /********************************************************************
3990 * construct_printer_info1
3991 * fill a spoolss_PrinterInfo1 struct
3992 ********************************************************************/
3994 static WERROR construct_printer_info1(TALLOC_CTX *mem_ctx,
3995 const struct spoolss_PrinterInfo2 *info2,
3996 uint32_t flags,
3997 const char *servername,
3998 struct spoolss_PrinterInfo1 *r,
3999 int snum)
4001 const struct loadparm_substitution *lp_sub =
4002 loadparm_s3_global_substitution();
4003 WERROR result;
4005 r->flags = flags;
4007 if (info2->comment == NULL || info2->comment[0] == '\0') {
4008 r->comment = lp_comment(mem_ctx, lp_sub, snum);
4009 } else {
4010 r->comment = talloc_strdup(mem_ctx, info2->comment); /* saved comment */
4012 W_ERROR_HAVE_NO_MEMORY(r->comment);
4014 result = create_printername(mem_ctx, servername, info2->printername, &r->name);
4015 if (!W_ERROR_IS_OK(result)) {
4016 return result;
4019 r->description = talloc_asprintf(mem_ctx, "%s,%s,%s",
4020 r->name,
4021 info2->drivername,
4022 r->comment);
4023 W_ERROR_HAVE_NO_MEMORY(r->description);
4025 return WERR_OK;
4028 /********************************************************************
4029 * construct_printer_info2
4030 * fill a spoolss_PrinterInfo2 struct
4031 ********************************************************************/
4033 static WERROR construct_printer_info2(TALLOC_CTX *mem_ctx,
4034 struct messaging_context *msg_ctx,
4035 const struct spoolss_PrinterInfo2 *info2,
4036 const char *servername,
4037 struct spoolss_PrinterInfo2 *r,
4038 int snum)
4040 const struct loadparm_substitution *lp_sub =
4041 loadparm_s3_global_substitution();
4042 int count;
4043 print_status_struct status;
4044 WERROR result;
4046 count = print_queue_length(msg_ctx, snum, &status);
4048 if (servername) {
4049 r->servername = talloc_strdup(mem_ctx, servername);
4050 W_ERROR_HAVE_NO_MEMORY(r->servername);
4051 } else {
4052 r->servername = NULL;
4055 result = create_printername(mem_ctx, servername, info2->printername, &r->printername);
4056 if (!W_ERROR_IS_OK(result)) {
4057 return result;
4060 r->sharename = lp_servicename(mem_ctx, lp_sub, snum);
4061 W_ERROR_HAVE_NO_MEMORY(r->sharename);
4062 r->portname = talloc_strdup(mem_ctx, info2->portname);
4063 W_ERROR_HAVE_NO_MEMORY(r->portname);
4064 r->drivername = talloc_strdup(mem_ctx, info2->drivername);
4065 W_ERROR_HAVE_NO_MEMORY(r->drivername);
4067 if (info2->comment[0] == '\0') {
4068 r->comment = lp_comment(mem_ctx, lp_sub, snum);
4069 } else {
4070 r->comment = talloc_strdup(mem_ctx, info2->comment);
4072 W_ERROR_HAVE_NO_MEMORY(r->comment);
4074 r->location = talloc_strdup(mem_ctx, info2->location);
4075 if (info2->location[0] == '\0') {
4076 const char *loc = NULL;
4077 NTSTATUS nt_status;
4079 nt_status = printer_list_get_printer(mem_ctx,
4080 info2->sharename,
4081 NULL,
4082 &loc,
4083 NULL);
4084 if (NT_STATUS_IS_OK(nt_status)) {
4085 if (loc != NULL) {
4086 r->location = talloc_strdup(mem_ctx, loc);
4090 W_ERROR_HAVE_NO_MEMORY(r->location);
4092 r->sepfile = talloc_strdup(mem_ctx, info2->sepfile);
4093 W_ERROR_HAVE_NO_MEMORY(r->sepfile);
4094 r->printprocessor = talloc_strdup(mem_ctx, info2->printprocessor);
4095 W_ERROR_HAVE_NO_MEMORY(r->printprocessor);
4096 r->datatype = talloc_strdup(mem_ctx, info2->datatype);
4097 W_ERROR_HAVE_NO_MEMORY(r->datatype);
4098 r->parameters = talloc_strdup(mem_ctx, info2->parameters);
4099 W_ERROR_HAVE_NO_MEMORY(r->parameters);
4101 r->attributes = info2->attributes;
4103 r->priority = info2->priority;
4104 r->defaultpriority = info2->defaultpriority;
4105 r->starttime = info2->starttime;
4106 r->untiltime = info2->untiltime;
4107 r->status = nt_printq_status(status.status);
4108 r->cjobs = count;
4109 r->averageppm = info2->averageppm;
4111 if (info2->devmode != NULL) {
4112 result = copy_devicemode(mem_ctx,
4113 info2->devmode,
4114 &r->devmode);
4115 if (!W_ERROR_IS_OK(result)) {
4116 return result;
4118 } else if (lp_default_devmode(snum)) {
4119 result = spoolss_create_default_devmode(mem_ctx,
4120 info2->printername,
4121 &r->devmode);
4122 if (!W_ERROR_IS_OK(result)) {
4123 return result;
4125 } else {
4126 r->devmode = NULL;
4127 DEBUG(8,("Returning NULL Devicemode!\n"));
4130 compose_devicemode_devicename(r->devmode, r->printername);
4132 r->secdesc = NULL;
4134 if (info2->secdesc != NULL) {
4135 /* don't use talloc_steal() here unless you do a deep steal of all
4136 the SEC_DESC members */
4138 r->secdesc = security_descriptor_copy(mem_ctx, info2->secdesc);
4139 if (r->secdesc == NULL) {
4140 return WERR_NOT_ENOUGH_MEMORY;
4144 return WERR_OK;
4147 /********************************************************************
4148 * construct_printer_info3
4149 * fill a spoolss_PrinterInfo3 struct
4150 ********************************************************************/
4152 static WERROR construct_printer_info3(TALLOC_CTX *mem_ctx,
4153 const struct spoolss_PrinterInfo2 *info2,
4154 const char *servername,
4155 struct spoolss_PrinterInfo3 *r,
4156 int snum)
4158 /* These are the components of the SD we are returning. */
4160 if (info2->secdesc != NULL) {
4161 /* don't use talloc_steal() here unless you do a deep steal of all
4162 the SEC_DESC members */
4164 r->secdesc = security_descriptor_copy(mem_ctx, info2->secdesc);
4165 if (r->secdesc == NULL) {
4166 return WERR_NOT_ENOUGH_MEMORY;
4170 return WERR_OK;
4173 /********************************************************************
4174 * construct_printer_info4
4175 * fill a spoolss_PrinterInfo4 struct
4176 ********************************************************************/
4178 static WERROR construct_printer_info4(TALLOC_CTX *mem_ctx,
4179 const struct spoolss_PrinterInfo2 *info2,
4180 const char *servername,
4181 struct spoolss_PrinterInfo4 *r,
4182 int snum)
4184 WERROR result;
4186 result = create_printername(mem_ctx, servername, info2->printername, &r->printername);
4187 if (!W_ERROR_IS_OK(result)) {
4188 return result;
4191 if (servername) {
4192 r->servername = talloc_strdup(mem_ctx, servername);
4193 W_ERROR_HAVE_NO_MEMORY(r->servername);
4194 } else {
4195 r->servername = NULL;
4198 r->attributes = info2->attributes;
4200 return WERR_OK;
4203 /********************************************************************
4204 * construct_printer_info5
4205 * fill a spoolss_PrinterInfo5 struct
4206 ********************************************************************/
4208 static WERROR construct_printer_info5(TALLOC_CTX *mem_ctx,
4209 const struct spoolss_PrinterInfo2 *info2,
4210 const char *servername,
4211 struct spoolss_PrinterInfo5 *r,
4212 int snum)
4214 WERROR result;
4216 result = create_printername(mem_ctx, servername, info2->printername, &r->printername);
4217 if (!W_ERROR_IS_OK(result)) {
4218 return result;
4221 r->portname = talloc_strdup(mem_ctx, info2->portname);
4222 W_ERROR_HAVE_NO_MEMORY(r->portname);
4224 r->attributes = info2->attributes;
4227 * These two are not used by NT+ according to MSDN. However the values
4228 * we saw on Windows Server 2012 and 2016 are always set to the 0xafc8.
4230 r->device_not_selected_timeout = 0xafc8; /* 45 sec */
4231 r->transmission_retry_timeout = 0xafc8; /* 45 sec */
4233 return WERR_OK;
4236 /********************************************************************
4237 * construct_printer_info_6
4238 * fill a spoolss_PrinterInfo6 struct
4239 ********************************************************************/
4241 static WERROR construct_printer_info6(TALLOC_CTX *mem_ctx,
4242 struct messaging_context *msg_ctx,
4243 const struct spoolss_PrinterInfo2 *info2,
4244 const char *servername,
4245 struct spoolss_PrinterInfo6 *r,
4246 int snum)
4248 print_status_struct status;
4250 print_queue_length(msg_ctx, snum, &status);
4252 r->status = nt_printq_status(status.status);
4254 return WERR_OK;
4257 /********************************************************************
4258 * construct_printer_info7
4259 * fill a spoolss_PrinterInfo7 struct
4260 ********************************************************************/
4262 static WERROR construct_printer_info7(TALLOC_CTX *mem_ctx,
4263 struct messaging_context *msg_ctx,
4264 const char *servername,
4265 struct spoolss_PrinterInfo7 *r,
4266 int snum)
4268 const struct loadparm_substitution *lp_sub =
4269 loadparm_s3_global_substitution();
4270 const struct auth_session_info *session_info;
4271 struct spoolss_PrinterInfo2 *pinfo2 = NULL;
4272 char *printer;
4273 WERROR werr;
4274 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
4275 if (tmp_ctx == NULL) {
4276 return WERR_NOT_ENOUGH_MEMORY;
4279 session_info = get_session_info_system();
4280 SMB_ASSERT(session_info != NULL);
4282 printer = lp_servicename(tmp_ctx, lp_sub, snum);
4283 if (printer == NULL) {
4284 DEBUG(0, ("invalid printer snum %d\n", snum));
4285 werr = WERR_INVALID_PARAMETER;
4286 goto out_tmp_free;
4289 if (is_printer_published(tmp_ctx, session_info, msg_ctx,
4290 servername, printer, &pinfo2)) {
4291 struct GUID guid;
4292 char *guidstr;
4293 werr = nt_printer_guid_get(tmp_ctx, session_info, msg_ctx,
4294 printer, &guid);
4295 if (!W_ERROR_IS_OK(werr)) {
4297 * If we do not have a GUID entry in the registry, then
4298 * try to retrieve it from AD and store it now.
4300 werr = nt_printer_guid_retrieve(tmp_ctx, printer,
4301 &guid);
4302 if (!W_ERROR_IS_OK(werr)) {
4303 DBG_NOTICE("Failed to retrieve GUID for "
4304 "printer [%s] from AD - %s\n",
4305 printer,
4306 win_errstr(werr));
4307 if (W_ERROR_EQUAL(werr, WERR_FILE_NOT_FOUND)) {
4309 * If we did not find it in AD, then it
4310 * is unpublished and we should reflect
4311 * this in the registry and return
4312 * success.
4314 DBG_WARNING("Unpublish printer [%s]\n",
4315 pinfo2->sharename);
4316 nt_printer_publish(tmp_ctx,
4317 session_info,
4318 msg_ctx,
4319 pinfo2,
4320 DSPRINT_UNPUBLISH);
4321 r->guid = talloc_strdup(mem_ctx, "");
4322 r->action = DSPRINT_UNPUBLISH;
4324 if (r->guid == NULL) {
4325 werr = WERR_NOT_ENOUGH_MEMORY;
4326 } else {
4327 werr = WERR_OK;
4330 goto out_tmp_free;
4333 werr = nt_printer_guid_store(msg_ctx, printer, guid);
4334 if (!W_ERROR_IS_OK(werr)) {
4335 DEBUG(3, ("failed to store printer %s guid\n",
4336 printer));
4340 /* [MS-RPRN] section 2.2: must use curly-braced GUIDs */
4341 guidstr = GUID_string2(mem_ctx, &guid);
4342 if (guidstr == NULL) {
4343 werr = WERR_NOT_ENOUGH_MEMORY;
4344 goto out_tmp_free;
4346 /* Convert GUID string to uppercase otherwise printers
4347 * are pruned */
4348 r->guid = talloc_strdup_upper(mem_ctx, guidstr);
4349 r->action = DSPRINT_PUBLISH;
4351 TALLOC_FREE(guidstr);
4352 } else {
4353 r->guid = talloc_strdup(mem_ctx, "");
4354 r->action = DSPRINT_UNPUBLISH;
4356 if (r->guid == NULL) {
4357 werr = WERR_NOT_ENOUGH_MEMORY;
4358 goto out_tmp_free;
4361 werr = WERR_OK;
4362 out_tmp_free:
4363 talloc_free(tmp_ctx);
4364 return werr;
4367 /********************************************************************
4368 * construct_printer_info8
4369 * fill a spoolss_PrinterInfo8 struct
4370 ********************************************************************/
4372 static WERROR construct_printer_info8(TALLOC_CTX *mem_ctx,
4373 const struct spoolss_PrinterInfo2 *info2,
4374 const char *servername,
4375 struct spoolss_DeviceModeInfo *r,
4376 int snum)
4378 WERROR result;
4379 const char *printername;
4381 result = create_printername(mem_ctx, servername, info2->printername, &printername);
4382 if (!W_ERROR_IS_OK(result)) {
4383 return result;
4386 if (info2->devmode != NULL) {
4387 result = copy_devicemode(mem_ctx,
4388 info2->devmode,
4389 &r->devmode);
4390 if (!W_ERROR_IS_OK(result)) {
4391 return result;
4393 } else if (lp_default_devmode(snum)) {
4394 result = spoolss_create_default_devmode(mem_ctx,
4395 info2->printername,
4396 &r->devmode);
4397 if (!W_ERROR_IS_OK(result)) {
4398 return result;
4400 } else {
4401 r->devmode = NULL;
4402 DEBUG(8,("Returning NULL Devicemode!\n"));
4405 compose_devicemode_devicename(r->devmode, printername);
4407 return WERR_OK;
4410 /********************************************************************
4411 Spoolss_enumprinters.
4412 ********************************************************************/
4414 static WERROR enum_all_printers_info_level(TALLOC_CTX *mem_ctx,
4415 const struct auth_session_info *session_info,
4416 struct messaging_context *msg_ctx,
4417 const char *servername,
4418 uint32_t level,
4419 uint32_t flags,
4420 union spoolss_PrinterInfo **info_p,
4421 uint32_t *count_p)
4423 int snum;
4424 int n_services;
4425 union spoolss_PrinterInfo *info = NULL;
4426 uint32_t count = 0;
4427 WERROR result = WERR_OK;
4428 struct dcerpc_binding_handle *b = NULL;
4429 TALLOC_CTX *tmp_ctx = NULL;
4431 tmp_ctx = talloc_new(mem_ctx);
4432 if (!tmp_ctx) {
4433 return WERR_NOT_ENOUGH_MEMORY;
4437 * printer shares are updated on client enumeration. The background
4438 * printer process updates printer_list.tdb at regular intervals.
4440 become_root();
4441 delete_and_reload_printers();
4442 unbecome_root();
4444 n_services = lp_numservices();
4445 *count_p = 0;
4446 *info_p = NULL;
4448 for (snum = 0; snum < n_services; snum++) {
4450 const char *printer;
4451 struct spoolss_PrinterInfo2 *info2;
4453 if (!snum_is_shared_printer(snum)) {
4454 continue;
4457 printer = lp_const_servicename(snum);
4459 DEBUG(4,("Found a printer in smb.conf: %s[%x]\n",
4460 printer, snum));
4462 if (b == NULL) {
4463 result = winreg_printer_binding_handle(tmp_ctx,
4464 session_info,
4465 msg_ctx,
4466 &b);
4467 if (!W_ERROR_IS_OK(result)) {
4468 goto out;
4472 result = winreg_create_printer(tmp_ctx, b,
4473 printer);
4474 if (!W_ERROR_IS_OK(result)) {
4475 goto out;
4478 info = talloc_realloc(tmp_ctx, info,
4479 union spoolss_PrinterInfo,
4480 count + 1);
4481 if (!info) {
4482 result = WERR_NOT_ENOUGH_MEMORY;
4483 goto out;
4486 result = winreg_get_printer(tmp_ctx, b,
4487 printer, &info2);
4488 if (!W_ERROR_IS_OK(result)) {
4489 goto out;
4492 switch (level) {
4493 case 0:
4494 result = construct_printer_info0(info, session_info,
4495 msg_ctx, info2,
4496 servername,
4497 &info[count].info0, snum);
4498 break;
4499 case 1:
4500 result = construct_printer_info1(info, info2, flags,
4501 servername,
4502 &info[count].info1, snum);
4503 break;
4504 case 2:
4505 result = construct_printer_info2(info, msg_ctx, info2,
4506 servername,
4507 &info[count].info2, snum);
4508 break;
4509 case 4:
4510 result = construct_printer_info4(info, info2,
4511 servername,
4512 &info[count].info4, snum);
4513 break;
4514 case 5:
4515 result = construct_printer_info5(info, info2,
4516 servername,
4517 &info[count].info5, snum);
4518 break;
4520 default:
4521 result = WERR_INVALID_LEVEL;
4522 goto out;
4525 if (!W_ERROR_IS_OK(result)) {
4526 goto out;
4529 count++;
4532 out:
4533 if (W_ERROR_IS_OK(result)) {
4534 *info_p = talloc_move(mem_ctx, &info);
4535 *count_p = count;
4538 talloc_free(tmp_ctx);
4540 return result;
4543 /********************************************************************
4544 * handle enumeration of printers at level 0
4545 ********************************************************************/
4547 static WERROR enumprinters_level0(TALLOC_CTX *mem_ctx,
4548 const struct auth_session_info *session_info,
4549 struct messaging_context *msg_ctx,
4550 uint32_t flags,
4551 const char *servername,
4552 union spoolss_PrinterInfo **info,
4553 uint32_t *count)
4555 DEBUG(4,("enum_all_printers_info_0\n"));
4557 return enum_all_printers_info_level(mem_ctx, session_info, msg_ctx,
4558 servername, 0, flags, info, count);
4562 /********************************************************************
4563 ********************************************************************/
4565 static WERROR enum_all_printers_info_1(TALLOC_CTX *mem_ctx,
4566 const struct auth_session_info *session_info,
4567 struct messaging_context *msg_ctx,
4568 const char *servername,
4569 uint32_t flags,
4570 union spoolss_PrinterInfo **info,
4571 uint32_t *count)
4573 DEBUG(4,("enum_all_printers_info_1\n"));
4575 return enum_all_printers_info_level(mem_ctx, session_info, msg_ctx,
4576 servername, 1, flags, info, count);
4579 /********************************************************************
4580 enum_all_printers_info_1_local.
4581 *********************************************************************/
4583 static WERROR enum_all_printers_info_1_local(TALLOC_CTX *mem_ctx,
4584 const struct auth_session_info *session_info,
4585 struct messaging_context *msg_ctx,
4586 const char *servername,
4587 union spoolss_PrinterInfo **info,
4588 uint32_t *count)
4590 DEBUG(4,("enum_all_printers_info_1_local\n"));
4592 return enum_all_printers_info_1(mem_ctx, session_info, msg_ctx,
4593 servername, PRINTER_ENUM_ICON8, info, count);
4596 /********************************************************************
4597 enum_all_printers_info_1_name.
4598 *********************************************************************/
4600 static WERROR enum_all_printers_info_1_name(TALLOC_CTX *mem_ctx,
4601 const struct auth_session_info *session_info,
4602 struct messaging_context *msg_ctx,
4603 const char *servername,
4604 union spoolss_PrinterInfo **info,
4605 uint32_t *count)
4607 const char *s = servername;
4609 DEBUG(4,("enum_all_printers_info_1_name\n"));
4611 if (servername != NULL &&
4612 (servername[0] == '\\') && (servername[1] == '\\')) {
4613 s = servername + 2;
4616 if (!is_myname_or_ipaddr(s)) {
4617 return WERR_INVALID_NAME;
4620 return enum_all_printers_info_1(mem_ctx, session_info, msg_ctx,
4621 servername, PRINTER_ENUM_ICON8, info, count);
4624 /********************************************************************
4625 enum_all_printers_info_1_network.
4626 *********************************************************************/
4628 static WERROR enum_all_printers_info_1_network(TALLOC_CTX *mem_ctx,
4629 const struct auth_session_info *session_info,
4630 struct messaging_context *msg_ctx,
4631 const char *servername,
4632 union spoolss_PrinterInfo **info,
4633 uint32_t *count)
4635 const char *s = servername;
4637 DEBUG(4,("enum_all_printers_info_1_network\n"));
4639 /* If we respond to a enum_printers level 1 on our name with flags
4640 set to PRINTER_ENUM_REMOTE with a list of printers then these
4641 printers incorrectly appear in the APW browse list.
4642 Specifically the printers for the server appear at the workgroup
4643 level where all the other servers in the domain are
4644 listed. Windows responds to this call with a
4645 WERR_CAN_NOT_COMPLETE so we should do the same. */
4647 if (servername != NULL &&
4648 (servername[0] == '\\') && (servername[1] == '\\')) {
4649 s = servername + 2;
4652 if (is_myname_or_ipaddr(s)) {
4653 return WERR_CAN_NOT_COMPLETE;
4656 return enum_all_printers_info_1(mem_ctx, session_info, msg_ctx,
4657 servername, PRINTER_ENUM_NAME, info, count);
4660 /********************************************************************
4661 * api_spoolss_enumprinters
4663 * called from api_spoolss_enumprinters (see this to understand)
4664 ********************************************************************/
4666 static WERROR enum_all_printers_info_2(TALLOC_CTX *mem_ctx,
4667 const struct auth_session_info *session_info,
4668 struct messaging_context *msg_ctx,
4669 const char *servername,
4670 union spoolss_PrinterInfo **info,
4671 uint32_t *count)
4673 DEBUG(4,("enum_all_printers_info_2\n"));
4675 return enum_all_printers_info_level(mem_ctx, session_info, msg_ctx,
4676 servername, 2, 0, info, count);
4679 /********************************************************************
4680 * handle enumeration of printers at level 1
4681 ********************************************************************/
4683 static WERROR enumprinters_level1(TALLOC_CTX *mem_ctx,
4684 const struct auth_session_info *session_info,
4685 struct messaging_context *msg_ctx,
4686 uint32_t flags,
4687 const char *servername,
4688 union spoolss_PrinterInfo **info,
4689 uint32_t *count)
4691 /* Not all the flags are equals */
4693 if (flags & PRINTER_ENUM_LOCAL) {
4694 return enum_all_printers_info_1_local(mem_ctx, session_info,
4695 msg_ctx, servername, info, count);
4698 if (flags & PRINTER_ENUM_NAME) {
4699 return enum_all_printers_info_1_name(mem_ctx, session_info,
4700 msg_ctx, servername, info,
4701 count);
4704 if (flags & PRINTER_ENUM_NETWORK) {
4705 return enum_all_printers_info_1_network(mem_ctx, session_info,
4706 msg_ctx, servername, info,
4707 count);
4710 return WERR_OK; /* NT4sp5 does that */
4713 /********************************************************************
4714 * handle enumeration of printers at level 2
4715 ********************************************************************/
4717 static WERROR enumprinters_level2(TALLOC_CTX *mem_ctx,
4718 const struct auth_session_info *session_info,
4719 struct messaging_context *msg_ctx,
4720 uint32_t flags,
4721 const char *servername,
4722 union spoolss_PrinterInfo **info,
4723 uint32_t *count)
4725 if (flags & PRINTER_ENUM_LOCAL) {
4727 return enum_all_printers_info_2(mem_ctx, session_info, msg_ctx,
4728 servername,
4729 info, count);
4732 if (flags & PRINTER_ENUM_NAME) {
4733 if (servername && !is_myname_or_ipaddr(canon_servername(servername))) {
4734 return WERR_INVALID_NAME;
4737 return enum_all_printers_info_2(mem_ctx, session_info, msg_ctx,
4738 servername,
4739 info, count);
4742 if (flags & PRINTER_ENUM_REMOTE) {
4743 return WERR_INVALID_LEVEL;
4746 return WERR_OK;
4749 /********************************************************************
4750 * handle enumeration of printers at level 4
4751 ********************************************************************/
4753 static WERROR enumprinters_level4(TALLOC_CTX *mem_ctx,
4754 const struct auth_session_info *session_info,
4755 struct messaging_context *msg_ctx,
4756 uint32_t flags,
4757 const char *servername,
4758 union spoolss_PrinterInfo **info,
4759 uint32_t *count)
4761 DEBUG(4,("enum_all_printers_info_4\n"));
4763 return enum_all_printers_info_level(mem_ctx, session_info, msg_ctx,
4764 servername, 4, flags, info, count);
4768 /********************************************************************
4769 * handle enumeration of printers at level 5
4770 ********************************************************************/
4772 static WERROR enumprinters_level5(TALLOC_CTX *mem_ctx,
4773 const struct auth_session_info *session_info,
4774 struct messaging_context *msg_ctx,
4775 uint32_t flags,
4776 const char *servername,
4777 union spoolss_PrinterInfo **info,
4778 uint32_t *count)
4780 DEBUG(4,("enum_all_printers_info_5\n"));
4782 return enum_all_printers_info_level(mem_ctx, session_info, msg_ctx,
4783 servername, 5, flags, info, count);
4786 /****************************************************************
4787 _spoolss_EnumPrinters
4788 ****************************************************************/
4790 WERROR _spoolss_EnumPrinters(struct pipes_struct *p,
4791 struct spoolss_EnumPrinters *r)
4793 const struct auth_session_info *session_info = get_session_info_system();
4794 WERROR result;
4796 /* that's an [in out] buffer */
4798 if (!r->in.buffer && (r->in.offered != 0)) {
4799 return WERR_INVALID_PARAMETER;
4802 DEBUG(4,("_spoolss_EnumPrinters\n"));
4804 *r->out.needed = 0;
4805 *r->out.count = 0;
4806 *r->out.info = NULL;
4809 * Level 1:
4810 * flags==PRINTER_ENUM_NAME
4811 * if name=="" then enumerates all printers
4812 * if name!="" then enumerate the printer
4813 * flags==PRINTER_ENUM_REMOTE
4814 * name is NULL, enumerate printers
4815 * Level 2: name!="" enumerates printers, name can't be NULL
4816 * Level 3: doesn't exist
4817 * Level 4: does a local registry lookup
4818 * Level 5: same as Level 2
4821 if (r->in.server && r->in.server[0] == '\0') {
4822 r->in.server = NULL;
4825 switch (r->in.level) {
4826 case 0:
4827 result = enumprinters_level0(p->mem_ctx, session_info,
4828 p->msg_ctx, r->in.flags,
4829 r->in.server,
4830 r->out.info, r->out.count);
4831 break;
4832 case 1:
4833 result = enumprinters_level1(p->mem_ctx, session_info,
4834 p->msg_ctx, r->in.flags,
4835 r->in.server,
4836 r->out.info, r->out.count);
4837 break;
4838 case 2:
4839 result = enumprinters_level2(p->mem_ctx, session_info,
4840 p->msg_ctx, r->in.flags,
4841 r->in.server,
4842 r->out.info, r->out.count);
4843 break;
4844 case 4:
4845 result = enumprinters_level4(p->mem_ctx, session_info,
4846 p->msg_ctx, r->in.flags,
4847 r->in.server,
4848 r->out.info, r->out.count);
4849 break;
4850 case 5:
4851 result = enumprinters_level5(p->mem_ctx, session_info,
4852 p->msg_ctx, r->in.flags,
4853 r->in.server,
4854 r->out.info, r->out.count);
4855 break;
4856 default:
4857 return WERR_INVALID_LEVEL;
4860 if (!W_ERROR_IS_OK(result)) {
4861 return result;
4864 *r->out.needed = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
4865 spoolss_EnumPrinters,
4866 *r->out.info, r->in.level,
4867 *r->out.count);
4868 *r->out.info = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
4869 *r->out.count = SPOOLSS_BUFFER_OK(*r->out.count, 0);
4871 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
4874 /****************************************************************
4875 _spoolss_GetPrinter
4876 ****************************************************************/
4878 WERROR _spoolss_GetPrinter(struct pipes_struct *p,
4879 struct spoolss_GetPrinter *r)
4881 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
4882 struct spoolss_PrinterInfo2 *info2 = NULL;
4883 WERROR result = WERR_OK;
4884 int snum;
4886 /* that's an [in out] buffer */
4888 if (!r->in.buffer && (r->in.offered != 0)) {
4889 result = WERR_INVALID_PARAMETER;
4890 goto err_info_free;
4893 *r->out.needed = 0;
4895 if (Printer == NULL) {
4896 result = WERR_INVALID_HANDLE;
4897 goto err_info_free;
4900 if (Printer->printer_type == SPLHND_SERVER) {
4902 struct dcerpc_binding_handle *b;
4904 if (r->in.level != 3) {
4905 result = WERR_INVALID_LEVEL;
4906 goto err_info_free;
4909 result = winreg_printer_binding_handle(p->mem_ctx,
4910 get_session_info_system(),
4911 p->msg_ctx,
4912 &b);
4913 if (!W_ERROR_IS_OK(result)) {
4914 goto err_info_free;
4917 result = winreg_get_printserver_secdesc(p->mem_ctx,
4919 &r->out.info->info3.secdesc);
4920 if (!W_ERROR_IS_OK(result)) {
4921 goto err_info_free;
4924 goto done;
4927 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
4928 result = WERR_INVALID_HANDLE;
4929 goto err_info_free;
4932 result = winreg_get_printer_internal(p->mem_ctx,
4933 get_session_info_system(),
4934 p->msg_ctx,
4935 lp_const_servicename(snum),
4936 &info2);
4937 if (!W_ERROR_IS_OK(result)) {
4938 goto err_info_free;
4941 switch (r->in.level) {
4942 case 0:
4943 result = construct_printer_info0(p->mem_ctx,
4944 get_session_info_system(),
4945 p->msg_ctx,
4946 info2,
4947 Printer->servername,
4948 &r->out.info->info0,
4949 snum);
4950 break;
4951 case 1:
4952 result = construct_printer_info1(p->mem_ctx, info2,
4953 PRINTER_ENUM_ICON8,
4954 Printer->servername,
4955 &r->out.info->info1, snum);
4956 break;
4957 case 2:
4958 result = construct_printer_info2(p->mem_ctx, p->msg_ctx, info2,
4959 Printer->servername,
4960 &r->out.info->info2, snum);
4961 break;
4962 case 3:
4963 result = construct_printer_info3(p->mem_ctx, info2,
4964 Printer->servername,
4965 &r->out.info->info3, snum);
4966 break;
4967 case 4:
4968 result = construct_printer_info4(p->mem_ctx, info2,
4969 Printer->servername,
4970 &r->out.info->info4, snum);
4971 break;
4972 case 5:
4973 result = construct_printer_info5(p->mem_ctx, info2,
4974 Printer->servername,
4975 &r->out.info->info5, snum);
4976 break;
4977 case 6:
4978 result = construct_printer_info6(p->mem_ctx, p->msg_ctx, info2,
4979 Printer->servername,
4980 &r->out.info->info6, snum);
4981 break;
4982 case 7:
4983 result = construct_printer_info7(p->mem_ctx, p->msg_ctx,
4984 Printer->servername,
4985 &r->out.info->info7, snum);
4986 break;
4987 case 8:
4988 result = construct_printer_info8(p->mem_ctx, info2,
4989 Printer->servername,
4990 &r->out.info->info8, snum);
4991 break;
4992 default:
4993 result = WERR_INVALID_LEVEL;
4994 break;
4996 TALLOC_FREE(info2);
4998 if (!W_ERROR_IS_OK(result)) {
4999 DEBUG(0, ("_spoolss_GetPrinter: failed to construct printer info level %d - %s\n",
5000 r->in.level, win_errstr(result)));
5001 goto err_info_free;
5003 done:
5004 *r->out.needed = SPOOLSS_BUFFER_UNION(spoolss_PrinterInfo,
5005 r->out.info, r->in.level);
5006 r->out.info = SPOOLSS_BUFFER_OK(r->out.info, NULL);
5008 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
5010 err_info_free:
5011 TALLOC_FREE(r->out.info);
5012 return result;
5015 /********************************************************************
5016 ********************************************************************/
5018 #define FILL_DRIVER_STRING(mem_ctx, in, out) \
5019 do { \
5020 if (in && strlen(in)) { \
5021 out = talloc_strdup(mem_ctx, in); \
5022 } else { \
5023 out = talloc_strdup(mem_ctx, ""); \
5025 W_ERROR_HAVE_NO_MEMORY(out); \
5026 } while (0);
5028 #define FILL_DRIVER_UNC_STRING(mem_ctx, server, arch, ver, in, out) \
5029 do { \
5030 if (in && strlen(in)) { \
5031 out = talloc_asprintf(mem_ctx, "\\\\%s\\print$\\%s\\%d\\%s", server, get_short_archi(arch), ver, in); \
5032 } else { \
5033 out = talloc_strdup(mem_ctx, ""); \
5035 W_ERROR_HAVE_NO_MEMORY(out); \
5036 } while (0);
5038 static WERROR string_array_from_driver_info(TALLOC_CTX *mem_ctx,
5039 const char **string_array,
5040 const char ***presult,
5041 const char *cservername,
5042 const char *arch,
5043 int version)
5045 size_t i;
5046 size_t num_strings = 0;
5047 const char **array = NULL;
5049 if (string_array == NULL) {
5050 return WERR_INVALID_PARAMETER;
5053 for (i=0; string_array[i] && string_array[i][0] != '\0'; i++) {
5054 const char *str = NULL;
5056 if (cservername == NULL || arch == NULL) {
5057 FILL_DRIVER_STRING(mem_ctx, string_array[i], str);
5058 } else {
5059 FILL_DRIVER_UNC_STRING(mem_ctx, cservername, arch, version, string_array[i], str);
5062 if (!add_string_to_array(mem_ctx, str, &array, &num_strings)) {
5063 TALLOC_FREE(array);
5064 return WERR_NOT_ENOUGH_MEMORY;
5068 if (i > 0) {
5069 ADD_TO_ARRAY(mem_ctx, const char *, NULL,
5070 &array, &num_strings);
5073 if (presult != NULL) {
5074 *presult = array;
5075 } else {
5076 talloc_free(array);
5079 return WERR_OK;
5082 /********************************************************************
5083 * fill a spoolss_DriverInfo1 struct
5084 ********************************************************************/
5086 static WERROR fill_printer_driver_info1(TALLOC_CTX *mem_ctx,
5087 struct spoolss_DriverInfo1 *r,
5088 const struct spoolss_DriverInfo8 *driver,
5089 const char *servername)
5091 r->driver_name = talloc_strdup(mem_ctx, driver->driver_name);
5092 W_ERROR_HAVE_NO_MEMORY(r->driver_name);
5094 return WERR_OK;
5097 /********************************************************************
5098 * fill a spoolss_DriverInfo2 struct
5099 ********************************************************************/
5101 static WERROR fill_printer_driver_info2(TALLOC_CTX *mem_ctx,
5102 struct spoolss_DriverInfo2 *r,
5103 const struct spoolss_DriverInfo8 *driver,
5104 const char *servername)
5107 const char *cservername = canon_servername(servername);
5109 r->version = driver->version;
5111 r->driver_name = talloc_strdup(mem_ctx, driver->driver_name);
5112 W_ERROR_HAVE_NO_MEMORY(r->driver_name);
5113 r->architecture = talloc_strdup(mem_ctx, driver->architecture);
5114 W_ERROR_HAVE_NO_MEMORY(r->architecture);
5116 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
5117 driver->architecture,
5118 driver->version,
5119 driver->driver_path,
5120 r->driver_path);
5122 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
5123 driver->architecture,
5124 driver->version,
5125 driver->data_file,
5126 r->data_file);
5128 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
5129 driver->architecture,
5130 driver->version,
5131 driver->config_file,
5132 r->config_file);
5134 return WERR_OK;
5137 /********************************************************************
5138 * fill a spoolss_DriverInfo3 struct
5139 ********************************************************************/
5141 static WERROR fill_printer_driver_info3(TALLOC_CTX *mem_ctx,
5142 struct spoolss_DriverInfo3 *r,
5143 const struct spoolss_DriverInfo8 *driver,
5144 const char *servername)
5146 const char *cservername = canon_servername(servername);
5148 r->version = driver->version;
5150 r->driver_name = talloc_strdup(mem_ctx, driver->driver_name);
5151 W_ERROR_HAVE_NO_MEMORY(r->driver_name);
5152 r->architecture = talloc_strdup(mem_ctx, driver->architecture);
5153 W_ERROR_HAVE_NO_MEMORY(r->architecture);
5155 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
5156 driver->architecture,
5157 driver->version,
5158 driver->driver_path,
5159 r->driver_path);
5161 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
5162 driver->architecture,
5163 driver->version,
5164 driver->data_file,
5165 r->data_file);
5167 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
5168 driver->architecture,
5169 driver->version,
5170 driver->config_file,
5171 r->config_file);
5173 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
5174 driver->architecture,
5175 driver->version,
5176 driver->help_file,
5177 r->help_file);
5179 FILL_DRIVER_STRING(mem_ctx,
5180 driver->monitor_name,
5181 r->monitor_name);
5183 FILL_DRIVER_STRING(mem_ctx,
5184 driver->default_datatype,
5185 r->default_datatype);
5187 return string_array_from_driver_info(mem_ctx,
5188 driver->dependent_files,
5189 &r->dependent_files,
5190 cservername,
5191 driver->architecture,
5192 driver->version);
5195 /********************************************************************
5196 * fill a spoolss_DriverInfo4 struct
5197 ********************************************************************/
5199 static WERROR fill_printer_driver_info4(TALLOC_CTX *mem_ctx,
5200 struct spoolss_DriverInfo4 *r,
5201 const struct spoolss_DriverInfo8 *driver,
5202 const char *servername)
5204 const char *cservername = canon_servername(servername);
5205 WERROR result;
5207 r->version = driver->version;
5209 r->driver_name = talloc_strdup(mem_ctx, driver->driver_name);
5210 W_ERROR_HAVE_NO_MEMORY(r->driver_name);
5211 r->architecture = talloc_strdup(mem_ctx, driver->architecture);
5212 W_ERROR_HAVE_NO_MEMORY(r->architecture);
5214 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
5215 driver->architecture,
5216 driver->version,
5217 driver->driver_path,
5218 r->driver_path);
5220 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
5221 driver->architecture,
5222 driver->version,
5223 driver->data_file,
5224 r->data_file);
5226 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
5227 driver->architecture,
5228 driver->version,
5229 driver->config_file,
5230 r->config_file);
5232 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
5233 driver->architecture,
5234 driver->version,
5235 driver->help_file,
5236 r->help_file);
5238 result = string_array_from_driver_info(mem_ctx,
5239 driver->dependent_files,
5240 &r->dependent_files,
5241 cservername,
5242 driver->architecture,
5243 driver->version);
5244 if (!W_ERROR_IS_OK(result)) {
5245 return result;
5248 FILL_DRIVER_STRING(mem_ctx,
5249 driver->monitor_name,
5250 r->monitor_name);
5252 FILL_DRIVER_STRING(mem_ctx,
5253 driver->default_datatype,
5254 r->default_datatype);
5257 result = string_array_from_driver_info(mem_ctx,
5258 driver->previous_names,
5259 &r->previous_names,
5260 NULL, NULL, 0);
5262 return result;
5265 /********************************************************************
5266 * fill a spoolss_DriverInfo5 struct
5267 ********************************************************************/
5269 static WERROR fill_printer_driver_info5(TALLOC_CTX *mem_ctx,
5270 struct spoolss_DriverInfo5 *r,
5271 const struct spoolss_DriverInfo8 *driver,
5272 const char *servername)
5274 const char *cservername = canon_servername(servername);
5276 r->version = driver->version;
5278 r->driver_name = talloc_strdup(mem_ctx, driver->driver_name);
5279 W_ERROR_HAVE_NO_MEMORY(r->driver_name);
5280 r->architecture = talloc_strdup(mem_ctx, driver->architecture);
5281 W_ERROR_HAVE_NO_MEMORY(r->architecture);
5283 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
5284 driver->architecture,
5285 driver->version,
5286 driver->driver_path,
5287 r->driver_path);
5289 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
5290 driver->architecture,
5291 driver->version,
5292 driver->data_file,
5293 r->data_file);
5295 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
5296 driver->architecture,
5297 driver->version,
5298 driver->config_file,
5299 r->config_file);
5301 r->driver_attributes = 0;
5302 r->config_version = 0;
5303 r->driver_version = 0;
5305 return WERR_OK;
5307 /********************************************************************
5308 * fill a spoolss_DriverInfo6 struct
5309 ********************************************************************/
5311 static WERROR fill_printer_driver_info6(TALLOC_CTX *mem_ctx,
5312 struct spoolss_DriverInfo6 *r,
5313 const struct spoolss_DriverInfo8 *driver,
5314 const char *servername)
5316 const char *cservername = canon_servername(servername);
5317 WERROR result;
5319 r->version = driver->version;
5321 r->driver_name = talloc_strdup(mem_ctx, driver->driver_name);
5322 W_ERROR_HAVE_NO_MEMORY(r->driver_name);
5323 r->architecture = talloc_strdup(mem_ctx, driver->architecture);
5324 W_ERROR_HAVE_NO_MEMORY(r->architecture);
5326 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
5327 driver->architecture,
5328 driver->version,
5329 driver->driver_path,
5330 r->driver_path);
5332 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
5333 driver->architecture,
5334 driver->version,
5335 driver->data_file,
5336 r->data_file);
5338 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
5339 driver->architecture,
5340 driver->version,
5341 driver->config_file,
5342 r->config_file);
5344 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
5345 driver->architecture,
5346 driver->version,
5347 driver->help_file,
5348 r->help_file);
5350 FILL_DRIVER_STRING(mem_ctx,
5351 driver->monitor_name,
5352 r->monitor_name);
5354 FILL_DRIVER_STRING(mem_ctx,
5355 driver->default_datatype,
5356 r->default_datatype);
5358 result = string_array_from_driver_info(mem_ctx,
5359 driver->dependent_files,
5360 &r->dependent_files,
5361 cservername,
5362 driver->architecture,
5363 driver->version);
5364 if (!W_ERROR_IS_OK(result)) {
5365 return result;
5368 result = string_array_from_driver_info(mem_ctx,
5369 driver->previous_names,
5370 &r->previous_names,
5371 NULL, NULL, 0);
5372 if (!W_ERROR_IS_OK(result)) {
5373 return result;
5376 r->driver_date = driver->driver_date;
5377 r->driver_version = driver->driver_version;
5379 FILL_DRIVER_STRING(mem_ctx,
5380 driver->manufacturer_name,
5381 r->manufacturer_name);
5382 FILL_DRIVER_STRING(mem_ctx,
5383 driver->manufacturer_url,
5384 r->manufacturer_url);
5385 FILL_DRIVER_STRING(mem_ctx,
5386 driver->hardware_id,
5387 r->hardware_id);
5388 FILL_DRIVER_STRING(mem_ctx,
5389 driver->provider,
5390 r->provider);
5392 return WERR_OK;
5395 /********************************************************************
5396 * fill a spoolss_DriverInfo8 struct
5397 ********************************************************************/
5399 static WERROR fill_printer_driver_info8(TALLOC_CTX *mem_ctx,
5400 struct spoolss_DriverInfo8 *r,
5401 const struct spoolss_DriverInfo8 *driver,
5402 const char *servername)
5404 const char *cservername = canon_servername(servername);
5405 WERROR result;
5407 r->version = driver->version;
5409 r->driver_name = talloc_strdup(mem_ctx, driver->driver_name);
5410 W_ERROR_HAVE_NO_MEMORY(r->driver_name);
5411 r->architecture = talloc_strdup(mem_ctx, driver->architecture);
5412 W_ERROR_HAVE_NO_MEMORY(r->architecture);
5414 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
5415 driver->architecture,
5416 driver->version,
5417 driver->driver_path,
5418 r->driver_path);
5420 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
5421 driver->architecture,
5422 driver->version,
5423 driver->data_file,
5424 r->data_file);
5426 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
5427 driver->architecture,
5428 driver->version,
5429 driver->config_file,
5430 r->config_file);
5432 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
5433 driver->architecture,
5434 driver->version,
5435 driver->help_file,
5436 r->help_file);
5438 FILL_DRIVER_STRING(mem_ctx,
5439 driver->monitor_name,
5440 r->monitor_name);
5442 FILL_DRIVER_STRING(mem_ctx,
5443 driver->default_datatype,
5444 r->default_datatype);
5446 result = string_array_from_driver_info(mem_ctx,
5447 driver->dependent_files,
5448 &r->dependent_files,
5449 cservername,
5450 driver->architecture,
5451 driver->version);
5452 if (!W_ERROR_IS_OK(result)) {
5453 return result;
5456 result = string_array_from_driver_info(mem_ctx,
5457 driver->previous_names,
5458 &r->previous_names,
5459 NULL, NULL, 0);
5460 if (!W_ERROR_IS_OK(result)) {
5461 return result;
5464 r->driver_date = driver->driver_date;
5465 r->driver_version = driver->driver_version;
5467 FILL_DRIVER_STRING(mem_ctx,
5468 driver->manufacturer_name,
5469 r->manufacturer_name);
5470 FILL_DRIVER_STRING(mem_ctx,
5471 driver->manufacturer_url,
5472 r->manufacturer_url);
5473 FILL_DRIVER_STRING(mem_ctx,
5474 driver->hardware_id,
5475 r->hardware_id);
5476 FILL_DRIVER_STRING(mem_ctx,
5477 driver->provider,
5478 r->provider);
5480 FILL_DRIVER_STRING(mem_ctx,
5481 driver->print_processor,
5482 r->print_processor);
5483 FILL_DRIVER_STRING(mem_ctx,
5484 driver->vendor_setup,
5485 r->vendor_setup);
5487 result = string_array_from_driver_info(mem_ctx,
5488 driver->color_profiles,
5489 &r->color_profiles,
5490 NULL, NULL, 0);
5491 if (!W_ERROR_IS_OK(result)) {
5492 return result;
5495 FILL_DRIVER_STRING(mem_ctx,
5496 driver->inf_path,
5497 r->inf_path);
5499 r->printer_driver_attributes = driver->printer_driver_attributes;
5501 result = string_array_from_driver_info(mem_ctx,
5502 driver->core_driver_dependencies,
5503 &r->core_driver_dependencies,
5504 NULL, NULL, 0);
5505 if (!W_ERROR_IS_OK(result)) {
5506 return result;
5509 r->min_inbox_driver_ver_date = driver->min_inbox_driver_ver_date;
5510 r->min_inbox_driver_ver_version = driver->min_inbox_driver_ver_version;
5512 return WERR_OK;
5515 #if 0 /* disabled until marshalling issues are resolved - gd */
5516 /********************************************************************
5517 ********************************************************************/
5519 static WERROR fill_spoolss_DriverFileInfo(TALLOC_CTX *mem_ctx,
5520 struct spoolss_DriverFileInfo *r,
5521 const char *cservername,
5522 const char *file_name,
5523 enum spoolss_DriverFileType file_type,
5524 uint32_t file_version)
5526 r->file_name = talloc_asprintf(mem_ctx, "\\\\%s%s",
5527 cservername, file_name);
5528 W_ERROR_HAVE_NO_MEMORY(r->file_name);
5529 r->file_type = file_type;
5530 r->file_version = file_version;
5532 return WERR_OK;
5535 /********************************************************************
5536 ********************************************************************/
5538 static WERROR spoolss_DriverFileInfo_from_driver(TALLOC_CTX *mem_ctx,
5539 const struct spoolss_DriverInfo8 *driver,
5540 const char *cservername,
5541 struct spoolss_DriverFileInfo **info_p,
5542 uint32_t *count_p)
5544 struct spoolss_DriverFileInfo *info = NULL;
5545 uint32_t count = 0;
5546 WERROR result;
5547 uint32_t i;
5549 *info_p = NULL;
5550 *count_p = 0;
5552 if (strlen(driver->driver_path)) {
5553 info = talloc_realloc(mem_ctx, info,
5554 struct spoolss_DriverFileInfo,
5555 count + 1);
5556 W_ERROR_HAVE_NO_MEMORY(info);
5557 result = fill_spoolss_DriverFileInfo(info,
5558 &info[count],
5559 cservername,
5560 driver->driver_path,
5561 SPOOLSS_DRIVER_FILE_TYPE_RENDERING,
5563 W_ERROR_NOT_OK_RETURN(result);
5564 count++;
5567 if (strlen(driver->config_file)) {
5568 info = talloc_realloc(mem_ctx, info,
5569 struct spoolss_DriverFileInfo,
5570 count + 1);
5571 W_ERROR_HAVE_NO_MEMORY(info);
5572 result = fill_spoolss_DriverFileInfo(info,
5573 &info[count],
5574 cservername,
5575 driver->config_file,
5576 SPOOLSS_DRIVER_FILE_TYPE_CONFIGURATION,
5578 W_ERROR_NOT_OK_RETURN(result);
5579 count++;
5582 if (strlen(driver->data_file)) {
5583 info = talloc_realloc(mem_ctx, info,
5584 struct spoolss_DriverFileInfo,
5585 count + 1);
5586 W_ERROR_HAVE_NO_MEMORY(info);
5587 result = fill_spoolss_DriverFileInfo(info,
5588 &info[count],
5589 cservername,
5590 driver->data_file,
5591 SPOOLSS_DRIVER_FILE_TYPE_DATA,
5593 W_ERROR_NOT_OK_RETURN(result);
5594 count++;
5597 if (strlen(driver->help_file)) {
5598 info = talloc_realloc(mem_ctx, info,
5599 struct spoolss_DriverFileInfo,
5600 count + 1);
5601 W_ERROR_HAVE_NO_MEMORY(info);
5602 result = fill_spoolss_DriverFileInfo(info,
5603 &info[count],
5604 cservername,
5605 driver->help_file,
5606 SPOOLSS_DRIVER_FILE_TYPE_HELP,
5608 W_ERROR_NOT_OK_RETURN(result);
5609 count++;
5612 for (i=0; driver->dependent_files[i] && driver->dependent_files[i][0] != '\0'; i++) {
5613 info = talloc_realloc(mem_ctx, info,
5614 struct spoolss_DriverFileInfo,
5615 count + 1);
5616 W_ERROR_HAVE_NO_MEMORY(info);
5617 result = fill_spoolss_DriverFileInfo(info,
5618 &info[count],
5619 cservername,
5620 driver->dependent_files[i],
5621 SPOOLSS_DRIVER_FILE_TYPE_OTHER,
5623 W_ERROR_NOT_OK_RETURN(result);
5624 count++;
5627 *info_p = info;
5628 *count_p = count;
5630 return WERR_OK;
5633 /********************************************************************
5634 * fill a spoolss_DriverInfo101 struct
5635 ********************************************************************/
5637 static WERROR fill_printer_driver_info101(TALLOC_CTX *mem_ctx,
5638 struct spoolss_DriverInfo101 *r,
5639 const struct spoolss_DriverInfo8 *driver,
5640 const char *servername)
5642 const char *cservername = canon_servername(servername);
5643 WERROR result;
5645 r->version = driver->version;
5647 r->driver_name = talloc_strdup(mem_ctx, driver->driver_name);
5648 W_ERROR_HAVE_NO_MEMORY(r->driver_name);
5649 r->architecture = talloc_strdup(mem_ctx, driver->architecture);
5650 W_ERROR_HAVE_NO_MEMORY(r->architecture);
5652 result = spoolss_DriverFileInfo_from_driver(mem_ctx, driver,
5653 cservername,
5654 &r->file_info,
5655 &r->file_count);
5656 if (!W_ERROR_IS_OK(result)) {
5657 return result;
5660 FILL_DRIVER_STRING(mem_ctx,
5661 driver->monitor_name,
5662 r->monitor_name);
5664 FILL_DRIVER_STRING(mem_ctx,
5665 driver->default_datatype,
5666 r->default_datatype);
5668 result = string_array_from_driver_info(mem_ctx,
5669 driver->previous_names,
5670 &r->previous_names,
5671 NULL, NULL, 0);
5672 if (!W_ERROR_IS_OK(result)) {
5673 return result;
5676 r->driver_date = driver->driver_date;
5677 r->driver_version = driver->driver_version;
5679 FILL_DRIVER_STRING(mem_ctx,
5680 driver->manufacturer_name,
5681 r->manufacturer_name);
5682 FILL_DRIVER_STRING(mem_ctx,
5683 driver->manufacturer_url,
5684 r->manufacturer_url);
5685 FILL_DRIVER_STRING(mem_ctx,
5686 driver->hardware_id,
5687 r->hardware_id);
5688 FILL_DRIVER_STRING(mem_ctx,
5689 driver->provider,
5690 r->provider);
5692 return WERR_OK;
5694 #endif
5695 /********************************************************************
5696 ********************************************************************/
5698 static WERROR construct_printer_driver_info_level(TALLOC_CTX *mem_ctx,
5699 const struct auth_session_info *session_info,
5700 struct messaging_context *msg_ctx,
5701 uint32_t level,
5702 union spoolss_DriverInfo *r,
5703 int snum,
5704 const char *servername,
5705 const char *architecture,
5706 uint32_t version)
5708 struct spoolss_PrinterInfo2 *pinfo2 = NULL;
5709 struct spoolss_DriverInfo8 *driver;
5710 WERROR result;
5711 struct dcerpc_binding_handle *b;
5712 TALLOC_CTX *tmp_ctx = NULL;
5714 if (level == 101) {
5715 return WERR_INVALID_LEVEL;
5718 tmp_ctx = talloc_new(mem_ctx);
5719 if (!tmp_ctx) {
5720 return WERR_NOT_ENOUGH_MEMORY;
5723 result = winreg_printer_binding_handle(tmp_ctx,
5724 session_info,
5725 msg_ctx,
5726 &b);
5727 if (!W_ERROR_IS_OK(result)) {
5728 goto done;
5731 result = winreg_get_printer(tmp_ctx, b,
5732 lp_const_servicename(snum),
5733 &pinfo2);
5734 if (!W_ERROR_IS_OK(result)) {
5735 DBG_ERR("Failed to get printer info2 for [%s]: %s\n",
5736 lp_const_servicename(snum), win_errstr(result));
5737 result = WERR_INVALID_PRINTER_NAME;
5738 goto done;
5741 if (pinfo2->drivername == NULL || pinfo2->drivername[0] == '\0') {
5742 return WERR_UNKNOWN_PRINTER_DRIVER;
5745 DBG_INFO("Construct printer driver [%s] for [%s]\n",
5746 pinfo2->drivername,
5747 pinfo2->sharename);
5749 result = winreg_get_driver(tmp_ctx, b,
5750 architecture,
5751 pinfo2->drivername, version, &driver);
5753 DBG_INFO("winreg_get_driver() status: %s\n",
5754 win_errstr(result));
5756 if (!W_ERROR_IS_OK(result)) {
5758 * Is this a W2k client ?
5761 if (version < 3) {
5762 result = WERR_UNKNOWN_PRINTER_DRIVER;
5763 goto done;
5766 /* Yes - try again with a WinNT driver. */
5767 version = 2;
5768 result = winreg_get_driver(tmp_ctx, b,
5769 architecture,
5770 pinfo2->drivername,
5771 version, &driver);
5772 DEBUG(8,("construct_printer_driver_level: status: %s\n",
5773 win_errstr(result)));
5774 if (!W_ERROR_IS_OK(result)) {
5775 result = WERR_UNKNOWN_PRINTER_DRIVER;
5776 goto done;
5780 /* these are allocated on mem_ctx and not tmp_ctx because they are
5781 * the 'return value' and need to utlive this call */
5782 switch (level) {
5783 case 1:
5784 result = fill_printer_driver_info1(mem_ctx, &r->info1, driver, servername);
5785 break;
5786 case 2:
5787 result = fill_printer_driver_info2(mem_ctx, &r->info2, driver, servername);
5788 break;
5789 case 3:
5790 result = fill_printer_driver_info3(mem_ctx, &r->info3, driver, servername);
5791 break;
5792 case 4:
5793 result = fill_printer_driver_info4(mem_ctx, &r->info4, driver, servername);
5794 break;
5795 case 5:
5796 result = fill_printer_driver_info5(mem_ctx, &r->info5, driver, servername);
5797 break;
5798 case 6:
5799 result = fill_printer_driver_info6(mem_ctx, &r->info6, driver, servername);
5800 break;
5801 case 8:
5802 result = fill_printer_driver_info8(mem_ctx, &r->info8, driver, servername);
5803 break;
5804 #if 0 /* disabled until marshalling issues are resolved - gd */
5805 case 101:
5806 result = fill_printer_driver_info101(mem_ctx, &r->info101, driver, servername);
5807 break;
5808 #endif
5809 default:
5810 result = WERR_INVALID_LEVEL;
5811 break;
5814 done:
5815 talloc_free(tmp_ctx);
5816 return result;
5819 /****************************************************************
5820 _spoolss_GetPrinterDriver2
5821 ****************************************************************/
5823 WERROR _spoolss_GetPrinterDriver2(struct pipes_struct *p,
5824 struct spoolss_GetPrinterDriver2 *r)
5826 struct printer_handle *printer;
5827 WERROR result;
5828 uint32_t version = r->in.client_major_version;
5830 int snum;
5832 /* that's an [in out] buffer */
5834 if (!r->in.buffer && (r->in.offered != 0)) {
5835 result = WERR_INVALID_PARAMETER;
5836 goto err_info_free;
5839 DEBUG(4,("_spoolss_GetPrinterDriver2\n"));
5841 if (!(printer = find_printer_index_by_hnd(p, r->in.handle))) {
5842 DEBUG(0,("_spoolss_GetPrinterDriver2: invalid printer handle!\n"));
5843 result = WERR_INVALID_PRINTER_NAME;
5844 goto err_info_free;
5847 *r->out.needed = 0;
5848 *r->out.server_major_version = 0;
5849 *r->out.server_minor_version = 0;
5851 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
5852 result = WERR_INVALID_HANDLE;
5853 goto err_info_free;
5856 if (r->in.client_major_version == SPOOLSS_DRIVER_VERSION_2012) {
5857 DEBUG(3,("_spoolss_GetPrinterDriver2: v4 driver requested, "
5858 "downgrading to v3\n"));
5859 version = SPOOLSS_DRIVER_VERSION_200X;
5862 result = construct_printer_driver_info_level(p->mem_ctx,
5863 get_session_info_system(),
5864 p->msg_ctx,
5865 r->in.level, r->out.info,
5866 snum, printer->servername,
5867 r->in.architecture,
5868 version);
5869 if (!W_ERROR_IS_OK(result)) {
5870 goto err_info_free;
5873 *r->out.needed = SPOOLSS_BUFFER_UNION(spoolss_DriverInfo,
5874 r->out.info, r->in.level);
5875 r->out.info = SPOOLSS_BUFFER_OK(r->out.info, NULL);
5877 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
5879 err_info_free:
5880 TALLOC_FREE(r->out.info);
5881 return result;
5885 /****************************************************************
5886 _spoolss_StartPagePrinter
5887 ****************************************************************/
5889 WERROR _spoolss_StartPagePrinter(struct pipes_struct *p,
5890 struct spoolss_StartPagePrinter *r)
5892 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
5894 if (!Printer) {
5895 DEBUG(3,("_spoolss_StartPagePrinter: "
5896 "Error in startpageprinter printer handle\n"));
5897 return WERR_INVALID_HANDLE;
5900 Printer->page_started = true;
5901 return WERR_OK;
5904 /****************************************************************
5905 _spoolss_EndPagePrinter
5906 ****************************************************************/
5908 WERROR _spoolss_EndPagePrinter(struct pipes_struct *p,
5909 struct spoolss_EndPagePrinter *r)
5911 int snum;
5913 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
5915 if (!Printer) {
5916 DEBUG(2,("_spoolss_EndPagePrinter: Invalid handle (%s:%u:%u).\n",
5917 OUR_HANDLE(r->in.handle)));
5918 return WERR_INVALID_HANDLE;
5921 if (!get_printer_snum(p, r->in.handle, &snum, NULL))
5922 return WERR_INVALID_HANDLE;
5924 Printer->page_started = false;
5925 print_job_endpage(p->msg_ctx, snum, Printer->jobid);
5927 return WERR_OK;
5930 /****************************************************************
5931 _spoolss_StartDocPrinter
5932 ****************************************************************/
5934 WERROR _spoolss_StartDocPrinter(struct pipes_struct *p,
5935 struct spoolss_StartDocPrinter *r)
5937 struct spoolss_DocumentInfo1 *info_1;
5938 int snum;
5939 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
5940 WERROR werr;
5941 char *rhost;
5942 int rc;
5944 if (!Printer) {
5945 DEBUG(2,("_spoolss_StartDocPrinter: "
5946 "Invalid handle (%s:%u:%u)\n",
5947 OUR_HANDLE(r->in.handle)));
5948 return WERR_INVALID_HANDLE;
5951 if (Printer->jobid) {
5952 DEBUG(2, ("_spoolss_StartDocPrinter: "
5953 "StartDocPrinter called twice! "
5954 "(existing jobid = %d)\n", Printer->jobid));
5955 return WERR_INVALID_HANDLE;
5958 if (r->in.info_ctr->level != 1) {
5959 return WERR_INVALID_LEVEL;
5962 info_1 = r->in.info_ctr->info.info1;
5965 * a nice thing with NT is it doesn't listen to what you tell it.
5966 * when asked to send _only_ RAW datas, it tries to send datas
5967 * in EMF format.
5969 * So I add checks like in NT Server ...
5972 if (info_1->datatype) {
5974 * The v4 driver model used in Windows 8 declares print jobs
5975 * intended to bypass the XPS processing layer by setting
5976 * datatype to "XPS_PASS" instead of "RAW".
5978 if ((strcmp(info_1->datatype, "RAW") != 0)
5979 && (strcmp(info_1->datatype, "XPS_PASS") != 0)) {
5980 *r->out.job_id = 0;
5981 return WERR_INVALID_DATATYPE;
5985 /* get the share number of the printer */
5986 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
5987 return WERR_INVALID_HANDLE;
5990 rc = get_remote_hostname(p->remote_address,
5991 &rhost,
5992 p->mem_ctx);
5993 if (rc < 0) {
5994 return WERR_NOT_ENOUGH_MEMORY;
5996 if (strequal(rhost,"UNKNOWN")) {
5997 rhost = tsocket_address_inet_addr_string(p->remote_address,
5998 p->mem_ctx);
5999 if (rhost == NULL) {
6000 return WERR_NOT_ENOUGH_MEMORY;
6004 werr = print_job_start(p->session_info,
6005 p->msg_ctx,
6006 rhost,
6007 snum,
6008 info_1->document_name,
6009 info_1->output_file,
6010 Printer->devmode,
6011 &Printer->jobid);
6013 /* An error occurred in print_job_start() so return an appropriate
6014 NT error code. */
6016 if (!W_ERROR_IS_OK(werr)) {
6017 return werr;
6020 Printer->document_started = true;
6021 *r->out.job_id = Printer->jobid;
6023 return WERR_OK;
6026 /****************************************************************
6027 _spoolss_EndDocPrinter
6028 ****************************************************************/
6030 WERROR _spoolss_EndDocPrinter(struct pipes_struct *p,
6031 struct spoolss_EndDocPrinter *r)
6033 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
6034 NTSTATUS status;
6035 int snum;
6037 if (!Printer) {
6038 DEBUG(2,("_spoolss_EndDocPrinter: Invalid handle (%s:%u:%u)\n",
6039 OUR_HANDLE(r->in.handle)));
6040 return WERR_INVALID_HANDLE;
6043 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
6044 return WERR_INVALID_HANDLE;
6047 Printer->document_started = false;
6048 status = print_job_end(p->msg_ctx, snum, Printer->jobid, NORMAL_CLOSE);
6049 if (!NT_STATUS_IS_OK(status)) {
6050 DEBUG(2, ("_spoolss_EndDocPrinter: "
6051 "print_job_end failed [%s]\n",
6052 nt_errstr(status)));
6055 Printer->jobid = 0;
6056 return ntstatus_to_werror(status);
6059 /****************************************************************
6060 _spoolss_WritePrinter
6061 ****************************************************************/
6063 WERROR _spoolss_WritePrinter(struct pipes_struct *p,
6064 struct spoolss_WritePrinter *r)
6066 ssize_t buffer_written;
6067 int snum;
6068 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
6070 if (!Printer) {
6071 DEBUG(2,("_spoolss_WritePrinter: Invalid handle (%s:%u:%u)\n",
6072 OUR_HANDLE(r->in.handle)));
6073 *r->out.num_written = r->in._data_size;
6074 return WERR_INVALID_HANDLE;
6077 if (!get_printer_snum(p, r->in.handle, &snum, NULL))
6078 return WERR_INVALID_HANDLE;
6080 /* print_job_write takes care of checking for PJOB_SMBD_SPOOLING */
6081 buffer_written = print_job_write(global_event_context(),p->msg_ctx,
6082 snum, Printer->jobid,
6083 (const char *)r->in.data.data,
6084 (size_t)r->in._data_size);
6085 if (buffer_written == (ssize_t)-1) {
6086 *r->out.num_written = 0;
6087 if (errno == ENOSPC)
6088 return WERR_NO_SPOOL_SPACE;
6089 else
6090 return WERR_ACCESS_DENIED;
6093 *r->out.num_written = r->in._data_size;
6095 return WERR_OK;
6098 /********************************************************************
6099 * api_spoolss_getprinter
6100 * called from the spoolss dispatcher
6102 ********************************************************************/
6104 static WERROR control_printer(struct policy_handle *handle, uint32_t command,
6105 struct pipes_struct *p)
6107 const struct auth_session_info *session_info = p->session_info;
6108 int snum;
6109 WERROR errcode = WERR_INVALID_FUNCTION;
6110 struct printer_handle *Printer = find_printer_index_by_hnd(p, handle);
6112 if (!Printer) {
6113 DEBUG(2,("control_printer: Invalid handle (%s:%u:%u)\n",
6114 OUR_HANDLE(handle)));
6115 return WERR_INVALID_HANDLE;
6118 if (!get_printer_snum(p, handle, &snum, NULL))
6119 return WERR_INVALID_HANDLE;
6121 switch (command) {
6122 case SPOOLSS_PRINTER_CONTROL_PAUSE:
6123 errcode = print_queue_pause(session_info, p->msg_ctx, snum);
6124 break;
6125 case SPOOLSS_PRINTER_CONTROL_RESUME:
6126 case SPOOLSS_PRINTER_CONTROL_UNPAUSE:
6127 errcode = print_queue_resume(session_info, p->msg_ctx, snum);
6128 break;
6129 case SPOOLSS_PRINTER_CONTROL_PURGE:
6130 errcode = print_queue_purge(session_info, p->msg_ctx, snum);
6131 break;
6132 default:
6133 return WERR_INVALID_LEVEL;
6136 return errcode;
6140 /****************************************************************
6141 _spoolss_AbortPrinter
6142 * From MSDN: "Deletes printer's spool file if printer is configured
6143 * for spooling"
6144 ****************************************************************/
6146 WERROR _spoolss_AbortPrinter(struct pipes_struct *p,
6147 struct spoolss_AbortPrinter *r)
6149 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
6150 int snum;
6151 WERROR errcode = WERR_OK;
6153 if (!Printer) {
6154 DEBUG(2,("_spoolss_AbortPrinter: Invalid handle (%s:%u:%u)\n",
6155 OUR_HANDLE(r->in.handle)));
6156 return WERR_INVALID_HANDLE;
6159 if (!get_printer_snum(p, r->in.handle, &snum, NULL))
6160 return WERR_INVALID_HANDLE;
6162 if (!Printer->document_started) {
6163 return WERR_SPL_NO_STARTDOC;
6166 errcode = print_job_delete(p->session_info,
6167 p->msg_ctx,
6168 snum,
6169 Printer->jobid);
6171 return errcode;
6174 /********************************************************************
6175 * called by spoolss_api_setprinter
6176 * when updating a printer description
6177 ********************************************************************/
6179 static WERROR update_printer_sec(struct policy_handle *handle,
6180 struct pipes_struct *p,
6181 struct sec_desc_buf *secdesc_ctr)
6183 struct spoolss_security_descriptor *new_secdesc = NULL;
6184 struct spoolss_security_descriptor *old_secdesc = NULL;
6185 const char *printer = NULL;
6186 WERROR result;
6187 int snum = -1;
6188 struct printer_handle *Printer = find_printer_index_by_hnd(p, handle);
6189 struct dcerpc_binding_handle *b;
6190 TALLOC_CTX *tmp_ctx = NULL;
6191 bool ok = false;
6193 if (!Printer) {
6194 DEBUG(2,("update_printer_sec: Invalid handle (%s:%u:%u)\n",
6195 OUR_HANDLE(handle)));
6197 result = WERR_INVALID_HANDLE;
6198 goto done;
6201 if (secdesc_ctr == NULL) {
6202 DEBUG(10,("update_printer_sec: secdesc_ctr is NULL !\n"));
6203 result = WERR_INVALID_PARAMETER;
6204 goto done;
6207 switch (Printer->printer_type) {
6208 case SPLHND_SERVER:
6209 break;
6210 case SPLHND_PRINTER:
6211 if (!get_printer_snum(p, handle, &snum, NULL)) {
6212 DEBUG(2,("update_printer_sec: Invalid handle (%s:%u:%u)\n",
6213 OUR_HANDLE(handle)));
6214 result = WERR_INVALID_HANDLE;
6215 goto done;
6217 printer = lp_const_servicename(snum);
6218 break;
6219 default:
6220 break;
6223 /* Check the user has permissions to change the security
6224 descriptor. By experimentation with two NT machines, the user
6225 requires Full Access to the printer to change security
6226 information. */
6228 switch (Printer->printer_type) {
6229 case SPLHND_SERVER:
6230 ok = Printer->access_granted == SERVER_ACCESS_ADMINISTER;
6231 break;
6232 case SPLHND_PRINTER:
6233 ok = Printer->access_granted == PRINTER_ACCESS_ADMINISTER;
6234 break;
6235 default:
6236 break;
6239 if (!ok) {
6240 DEBUG(4,("update_printer_sec: updated denied by printer permissions "
6241 "(access_granted: 0x%08x)\n", Printer->access_granted));
6242 result = WERR_ACCESS_DENIED;
6243 goto done;
6246 tmp_ctx = talloc_new(p->mem_ctx);
6247 if (!tmp_ctx) {
6248 return WERR_NOT_ENOUGH_MEMORY;
6251 result = winreg_printer_binding_handle(tmp_ctx,
6252 get_session_info_system(),
6253 p->msg_ctx,
6254 &b);
6255 if (!W_ERROR_IS_OK(result)) {
6256 goto done;
6259 /* NT seems to like setting the security descriptor even though
6260 nothing may have actually changed. */
6262 if (printer != NULL) {
6263 result = winreg_get_printer_secdesc(tmp_ctx, b,
6264 printer,
6265 &old_secdesc);
6266 } else {
6267 result = winreg_get_printserver_secdesc(tmp_ctx, b,
6268 &old_secdesc);
6270 if (!W_ERROR_IS_OK(result)) {
6271 DEBUG(2,("update_printer_sec: winreg_get_printer_secdesc_internal() failed\n"));
6272 result = WERR_INVALID_HANDLE;
6273 goto done;
6276 if (DEBUGLEVEL >= 10) {
6277 struct dom_sid_buf buf;
6278 struct security_acl *the_acl;
6279 int i;
6281 the_acl = old_secdesc->dacl;
6282 DEBUG(10, ("old_secdesc_ctr for %s has %d aces:\n",
6283 printer, the_acl->num_aces));
6285 for (i = 0; i < the_acl->num_aces; i++) {
6286 DEBUG(10, ("%s 0x%08x\n",
6287 dom_sid_str_buf(
6288 &the_acl->aces[i].trustee,
6289 &buf),
6290 the_acl->aces[i].access_mask));
6293 the_acl = secdesc_ctr->sd->dacl;
6295 if (the_acl) {
6296 DEBUG(10, ("secdesc_ctr for %s has %d aces:\n",
6297 printer, the_acl->num_aces));
6299 for (i = 0; i < the_acl->num_aces; i++) {
6300 DEBUG(10, ("%s 0x%08x\n",
6301 dom_sid_str_buf(
6302 &the_acl->aces[i].trustee,
6303 &buf),
6304 the_acl->aces[i].access_mask));
6306 } else {
6307 DEBUG(10, ("dacl for secdesc_ctr is NULL\n"));
6311 new_secdesc = sec_desc_merge(tmp_ctx, secdesc_ctr->sd, old_secdesc);
6312 if (new_secdesc == NULL) {
6313 result = WERR_NOT_ENOUGH_MEMORY;
6314 goto done;
6317 if (security_descriptor_equal(new_secdesc, old_secdesc)) {
6318 result = WERR_OK;
6319 goto done;
6322 if (printer != NULL) {
6323 result = winreg_set_printer_secdesc(tmp_ctx, b,
6324 printer,
6325 new_secdesc);
6326 } else {
6327 result = winreg_set_printserver_secdesc(tmp_ctx, b,
6328 new_secdesc);
6331 done:
6332 talloc_free(tmp_ctx);
6333 return result;
6336 /********************************************************************
6337 Canonicalize printer info from a client
6338 ********************************************************************/
6340 static bool check_printer_ok(TALLOC_CTX *mem_ctx,
6341 struct spoolss_SetPrinterInfo2 *info2,
6342 int snum)
6344 fstring printername;
6345 const char *p;
6347 DEBUG(5,("check_printer_ok: servername=%s printername=%s sharename=%s "
6348 "portname=%s drivername=%s comment=%s location=%s\n",
6349 info2->servername, info2->printername, info2->sharename,
6350 info2->portname, info2->drivername, info2->comment,
6351 info2->location));
6353 /* we force some elements to "correct" values */
6354 info2->servername = talloc_asprintf(mem_ctx, "\\\\%s", lp_netbios_name());
6355 if (info2->servername == NULL) {
6356 return false;
6358 info2->sharename = talloc_strdup(mem_ctx, lp_const_servicename(snum));
6359 if (info2->sharename == NULL) {
6360 return false;
6363 /* check to see if we allow printername != sharename */
6364 if (lp_force_printername(snum)) {
6365 info2->printername = talloc_asprintf(mem_ctx, "\\\\%s\\%s",
6366 lp_netbios_name(), info2->sharename);
6367 } else {
6368 /* make sure printername is in \\server\printername format */
6369 fstrcpy(printername, info2->printername);
6370 p = printername;
6371 if ( printername[0] == '\\' && printername[1] == '\\' ) {
6372 if ( (p = strchr_m( &printername[2], '\\' )) != NULL )
6373 p++;
6376 info2->printername = talloc_asprintf(mem_ctx, "\\\\%s\\%s",
6377 lp_netbios_name(), p);
6379 if (info2->printername == NULL) {
6380 return false;
6383 info2->attributes |= PRINTER_ATTRIBUTE_SAMBA;
6384 info2->attributes &= ~PRINTER_ATTRIBUTE_NOT_SAMBA;
6386 return true;
6389 /****************************************************************************
6390 ****************************************************************************/
6392 static WERROR add_port_hook(TALLOC_CTX *ctx, struct security_token *token, const char *portname, const char *uri)
6394 const struct loadparm_substitution *lp_sub =
6395 loadparm_s3_global_substitution();
6396 char *cmd = lp_addport_command(talloc_tos(), lp_sub);
6397 char *command = NULL;
6398 int ret;
6399 bool is_print_op = false;
6401 if ( !*cmd ) {
6402 return WERR_ACCESS_DENIED;
6405 command = talloc_asprintf(ctx,
6406 "%s \"%s\" \"%s\"", cmd, portname, uri );
6407 if (!command) {
6408 return WERR_NOT_ENOUGH_MEMORY;
6411 if ( token )
6412 is_print_op = security_token_has_privilege(token, SEC_PRIV_PRINT_OPERATOR);
6414 DEBUG(10,("Running [%s]\n", command));
6416 /********* BEGIN SePrintOperatorPrivilege **********/
6418 if ( is_print_op )
6419 become_root();
6421 ret = smbrun(command, NULL, NULL);
6423 if ( is_print_op )
6424 unbecome_root();
6426 /********* END SePrintOperatorPrivilege **********/
6428 DEBUGADD(10,("returned [%d]\n", ret));
6430 TALLOC_FREE(command);
6432 if ( ret != 0 ) {
6433 return WERR_ACCESS_DENIED;
6436 return WERR_OK;
6439 /****************************************************************************
6440 ****************************************************************************/
6442 static bool spoolss_conn_snum_used(struct smbd_server_connection *sconn,
6443 int snum)
6446 * As we do not know if we are embedded in the file server process
6447 * or not, we have to pretend that all shares are in use.
6449 return true;
6452 static bool add_printer_hook(TALLOC_CTX *ctx, struct security_token *token,
6453 struct spoolss_SetPrinterInfo2 *info2,
6454 const char *remote_machine,
6455 struct messaging_context *msg_ctx)
6457 const struct loadparm_substitution *lp_sub =
6458 loadparm_s3_global_substitution();
6459 char *cmd = lp_addprinter_command(talloc_tos(), lp_sub);
6460 char **qlines;
6461 char *command = NULL;
6462 int numlines;
6463 int ret;
6464 int fd;
6465 bool is_print_op = false;
6467 if (!remote_machine) {
6468 return false;
6471 command = talloc_asprintf(ctx,
6472 "%s \"%s\" \"%s\" \"%s\" \"%s\" \"%s\" \"%s\" \"%s\"",
6473 cmd, info2->printername, info2->sharename,
6474 info2->portname, info2->drivername,
6475 info2->location, info2->comment, remote_machine);
6476 if (!command) {
6477 return false;
6480 if ( token )
6481 is_print_op = security_token_has_privilege(token, SEC_PRIV_PRINT_OPERATOR);
6483 DEBUG(10,("Running [%s]\n", command));
6485 /********* BEGIN SePrintOperatorPrivilege **********/
6487 if ( is_print_op )
6488 become_root();
6490 ret = smbrun(command, &fd, NULL);
6491 if (ret == 0) {
6492 /* Tell everyone we updated smb.conf. */
6493 messaging_send_all(msg_ctx, MSG_SMB_CONF_UPDATED, NULL, 0);
6496 if ( is_print_op )
6497 unbecome_root();
6499 /********* END SePrintOperatorPrivilege **********/
6501 DEBUGADD(10,("returned [%d]\n", ret));
6503 TALLOC_FREE(command);
6505 if ( ret != 0 ) {
6506 if (fd != -1)
6507 close(fd);
6508 return false;
6511 /* reload our services immediately */
6512 become_root();
6513 reload_services(NULL, spoolss_conn_snum_used, false);
6514 unbecome_root();
6516 numlines = 0;
6517 /* Get lines and convert them back to dos-codepage */
6518 qlines = fd_lines_load(fd, &numlines, 0, NULL);
6519 DEBUGADD(10,("Lines returned = [%d]\n", numlines));
6520 close(fd);
6522 /* Set the portname to what the script says the portname should be. */
6523 /* but don't require anything to be return from the script exit a good error code */
6525 if (numlines) {
6526 /* Set the portname to what the script says the portname should be. */
6527 info2->portname = talloc_strdup(ctx, qlines[0]);
6528 DEBUGADD(6,("Line[0] = [%s]\n", qlines[0]));
6531 TALLOC_FREE(qlines);
6532 return true;
6535 static WERROR update_dsspooler(TALLOC_CTX *mem_ctx,
6536 const struct auth_session_info *session_info,
6537 struct messaging_context *msg_ctx,
6538 int snum,
6539 struct spoolss_SetPrinterInfo2 *printer,
6540 struct spoolss_PrinterInfo2 *old_printer)
6542 bool force_update = (old_printer == NULL);
6543 const char *dnsdomname;
6544 const char *longname;
6545 const char *uncname;
6546 const char *spooling;
6547 DATA_BLOB buffer;
6548 WERROR result = WERR_OK;
6549 struct dcerpc_binding_handle *b;
6550 TALLOC_CTX *tmp_ctx;
6551 bool ok;
6553 tmp_ctx = talloc_new(mem_ctx);
6554 if (!tmp_ctx) {
6555 return WERR_NOT_ENOUGH_MEMORY;
6558 result = winreg_printer_binding_handle(tmp_ctx,
6559 session_info,
6560 msg_ctx,
6561 &b);
6562 if (!W_ERROR_IS_OK(result)) {
6563 goto done;
6566 if (printer->drivername != NULL &&
6567 (force_update ||
6568 !strequal(printer->drivername, old_printer->drivername))) {
6569 ok = push_reg_sz(tmp_ctx, &buffer, printer->drivername);
6570 if (!ok) {
6571 DEBUG(0, ("%s data corrupted\n", SPOOL_REG_DRIVERNAME));
6572 result = WERR_INVALID_DATA;
6573 goto done;
6575 result = winreg_set_printer_dataex(tmp_ctx, b,
6576 printer->sharename,
6577 SPOOL_DSSPOOLER_KEY,
6578 SPOOL_REG_DRIVERNAME,
6579 REG_SZ,
6580 buffer.data,
6581 buffer.length);
6582 if (!W_ERROR_IS_OK(result)) {
6583 DEBUG(0, ("Failed to set %s\n", SPOOL_REG_DRIVERNAME));
6584 goto done;
6587 if (!force_update) {
6588 DEBUG(10,("update_printer: changing driver [%s]! Sending event!\n",
6589 printer->drivername));
6591 notify_printer_driver(global_event_context(), msg_ctx,
6592 snum, printer->drivername ?
6593 printer->drivername : "");
6597 if (printer->comment != NULL &&
6598 (force_update ||
6599 !strequal(printer->comment, old_printer->comment))) {
6600 ok = push_reg_sz(tmp_ctx, &buffer, printer->comment);
6601 if (!ok) {
6602 DEBUG(0, ("comment data corrupted\n"));
6603 result = WERR_INVALID_DATA;
6604 goto done;
6606 result = winreg_set_printer_dataex(tmp_ctx, b,
6607 printer->sharename,
6608 SPOOL_DSSPOOLER_KEY,
6609 SPOOL_REG_DESCRIPTION,
6610 REG_SZ,
6611 buffer.data,
6612 buffer.length);
6613 if (!W_ERROR_IS_OK(result)) {
6614 DEBUG(0, ("Failed to set %s\n", SPOOL_REG_DESCRIPTION));
6615 goto done;
6618 if (!force_update) {
6619 notify_printer_comment(global_event_context(), msg_ctx,
6620 snum, printer->comment ?
6621 printer->comment : "");
6625 if (printer->sharename != NULL &&
6626 (force_update ||
6627 !strequal(printer->sharename, old_printer->sharename))) {
6628 ok = push_reg_sz(tmp_ctx, &buffer, printer->sharename);
6629 if (!ok) {
6630 DEBUG(0, ("sharename data corrupted\n"));
6631 result = WERR_INVALID_DATA;
6632 goto done;
6634 result = winreg_set_printer_dataex(tmp_ctx, b,
6635 printer->sharename,
6636 SPOOL_DSSPOOLER_KEY,
6637 SPOOL_REG_PRINTSHARENAME,
6638 REG_SZ,
6639 buffer.data,
6640 buffer.length);
6641 if (!W_ERROR_IS_OK(result)) {
6642 DEBUG(0, ("Failed to set %s\n", SPOOL_REG_PRINTSHARENAME));
6643 goto done;
6646 if (!force_update) {
6647 notify_printer_sharename(global_event_context(),
6648 msg_ctx,
6649 snum, printer->sharename ?
6650 printer->sharename : "");
6653 /* name change, purge any cache entries for the old */
6654 prune_printername_cache();
6657 if (printer->printername != NULL &&
6658 (force_update ||
6659 !strequal(printer->printername, old_printer->printername))) {
6660 const char *p;
6662 p = strrchr(printer->printername, '\\' );
6663 if (p != NULL) {
6664 p++;
6665 } else {
6666 p = printer->printername;
6669 ok = push_reg_sz(tmp_ctx, &buffer, p);
6670 if (!ok) {
6671 DEBUG(0, ("printername data corrupted\n"));
6672 result = WERR_INVALID_DATA;
6673 goto done;
6675 result = winreg_set_printer_dataex(tmp_ctx, b,
6676 printer->sharename,
6677 SPOOL_DSSPOOLER_KEY,
6678 SPOOL_REG_PRINTERNAME,
6679 REG_SZ,
6680 buffer.data,
6681 buffer.length);
6682 if (!W_ERROR_IS_OK(result)) {
6683 DBG_ERR("Failed to set %s\n", SPOOL_REG_PRINTERNAME);
6684 goto done;
6687 if (!force_update) {
6688 notify_printer_printername(global_event_context(),
6689 msg_ctx, snum, p ? p : "");
6692 /* name change, purge any cache entries for the old */
6693 prune_printername_cache();
6696 if (printer->portname != NULL &&
6697 (force_update ||
6698 !strequal(printer->portname, old_printer->portname))) {
6699 ok = push_reg_sz(tmp_ctx, &buffer, printer->portname);
6700 if (!ok) {
6701 DEBUG(0, ("portname data corrupted\n"));
6702 result = WERR_INVALID_DATA;
6703 goto done;
6705 result = winreg_set_printer_dataex(tmp_ctx, b,
6706 printer->sharename,
6707 SPOOL_DSSPOOLER_KEY,
6708 SPOOL_REG_PORTNAME,
6709 REG_SZ,
6710 buffer.data,
6711 buffer.length);
6712 if (!W_ERROR_IS_OK(result)) {
6713 DEBUG(0, ("Failed to set %s\n", SPOOL_REG_PORTNAME));
6714 goto done;
6717 if (!force_update) {
6718 notify_printer_port(global_event_context(),
6719 msg_ctx, snum, printer->portname ?
6720 printer->portname : "");
6724 if (printer->location != NULL &&
6725 (force_update ||
6726 !strequal(printer->location, old_printer->location))) {
6727 ok = push_reg_sz(tmp_ctx, &buffer, printer->location);
6728 if (!ok) {
6729 DEBUG(0, ("location data corrupted\n"));
6730 result = WERR_INVALID_DATA;
6731 goto done;
6733 result = winreg_set_printer_dataex(tmp_ctx, b,
6734 printer->sharename,
6735 SPOOL_DSSPOOLER_KEY,
6736 SPOOL_REG_LOCATION,
6737 REG_SZ,
6738 buffer.data,
6739 buffer.length);
6740 if (!W_ERROR_IS_OK(result)) {
6741 DEBUG(0, ("Failed to set %s\n", SPOOL_REG_LOCATION));
6742 goto done;
6745 if (!force_update) {
6746 notify_printer_location(global_event_context(),
6747 msg_ctx, snum,
6748 printer->location ?
6749 printer->location : "");
6753 if (printer->sepfile != NULL &&
6754 (force_update ||
6755 !strequal(printer->sepfile, old_printer->sepfile))) {
6756 ok = push_reg_sz(tmp_ctx, &buffer, printer->sepfile);
6757 if (!ok) {
6758 DEBUG(0, ("sepfile data corrupted\n"));
6759 result = WERR_INVALID_DATA;
6760 goto done;
6762 result = winreg_set_printer_dataex(tmp_ctx, b,
6763 printer->sharename,
6764 SPOOL_DSSPOOLER_KEY,
6765 SPOOL_REG_PRINTSEPARATORFILE,
6766 REG_SZ,
6767 buffer.data,
6768 buffer.length);
6769 if (!W_ERROR_IS_OK(result)) {
6770 DEBUG(0, ("Failed to set %s\n", SPOOL_REG_PRINTSEPARATORFILE));
6771 goto done;
6774 if (!force_update) {
6775 notify_printer_sepfile(global_event_context(),
6776 msg_ctx, snum,
6777 printer->sepfile ?
6778 printer->sepfile : "");
6782 if (printer->starttime != 0 &&
6783 (force_update ||
6784 printer->starttime != old_printer->starttime)) {
6785 buffer = data_blob_talloc(tmp_ctx, NULL, 4);
6786 SIVAL(buffer.data, 0, printer->starttime);
6787 result = winreg_set_printer_dataex(tmp_ctx, b,
6788 printer->sharename,
6789 SPOOL_DSSPOOLER_KEY,
6790 SPOOL_REG_PRINTSTARTTIME,
6791 REG_DWORD,
6792 buffer.data,
6793 buffer.length);
6794 if (!W_ERROR_IS_OK(result)) {
6795 DEBUG(0, ("Failed to set %s\n", SPOOL_REG_PRINTSTARTTIME));
6796 goto done;
6800 if (printer->untiltime != 0 &&
6801 (force_update ||
6802 printer->untiltime != old_printer->untiltime)) {
6803 buffer = data_blob_talloc(tmp_ctx, NULL, 4);
6804 SIVAL(buffer.data, 0, printer->untiltime);
6805 result = winreg_set_printer_dataex(tmp_ctx, b,
6806 printer->sharename,
6807 SPOOL_DSSPOOLER_KEY,
6808 SPOOL_REG_PRINTENDTIME,
6809 REG_DWORD,
6810 buffer.data,
6811 buffer.length);
6812 if (!W_ERROR_IS_OK(result)) {
6813 DEBUG(0, ("Failed to set %s\n", SPOOL_REG_PRINTENDTIME));
6814 goto done;
6818 if (force_update || printer->priority != old_printer->priority) {
6819 buffer = data_blob_talloc(tmp_ctx, NULL, 4);
6820 SIVAL(buffer.data, 0, printer->priority);
6821 result = winreg_set_printer_dataex(tmp_ctx, b,
6822 printer->sharename,
6823 SPOOL_DSSPOOLER_KEY,
6824 SPOOL_REG_PRIORITY,
6825 REG_DWORD,
6826 buffer.data,
6827 buffer.length);
6828 if (!W_ERROR_IS_OK(result)) {
6829 DEBUG(0, ("Failed to set %s\n", SPOOL_REG_PRINTENDTIME));
6830 goto done;
6834 if (force_update || printer->attributes != old_printer->attributes) {
6835 buffer = data_blob_talloc(tmp_ctx, NULL, 4);
6836 SIVAL(buffer.data, 0, (printer->attributes &
6837 PRINTER_ATTRIBUTE_KEEPPRINTEDJOBS));
6838 result = winreg_set_printer_dataex(tmp_ctx, b,
6839 printer->sharename,
6840 SPOOL_DSSPOOLER_KEY,
6841 SPOOL_REG_PRINTKEEPPRINTEDJOBS,
6842 REG_DWORD,
6843 buffer.data,
6844 buffer.length);
6845 if (!W_ERROR_IS_OK(result)) {
6846 DEBUG(0, ("Failed to set %s\n", SPOOL_REG_PRINTENDTIME));
6847 goto done;
6850 switch (printer->attributes & 0x3) {
6851 case 0:
6852 spooling = SPOOL_REGVAL_PRINTWHILESPOOLING;
6853 break;
6854 case 1:
6855 spooling = SPOOL_REGVAL_PRINTAFTERSPOOLED;
6856 break;
6857 case 2:
6858 spooling = SPOOL_REGVAL_PRINTDIRECT;
6859 break;
6860 default:
6861 spooling = "unknown";
6863 ok = push_reg_sz(tmp_ctx, &buffer, spooling);
6864 if (!ok) {
6865 DEBUG(0, ("printSpooling data corrupted\n"));
6866 result = WERR_INVALID_DATA;
6867 goto done;
6869 winreg_set_printer_dataex(tmp_ctx, b,
6870 printer->sharename,
6871 SPOOL_DSSPOOLER_KEY,
6872 SPOOL_REG_PRINTSPOOLING,
6873 REG_SZ,
6874 buffer.data,
6875 buffer.length);
6878 ok = push_reg_sz(tmp_ctx, &buffer, lp_netbios_name());
6879 if (!ok) {
6880 DEBUG(0, ("shortServerName data corrupted\n"));
6881 result = WERR_INVALID_DATA;
6882 goto done;
6884 result = winreg_set_printer_dataex(tmp_ctx, b,
6885 printer->sharename,
6886 SPOOL_DSSPOOLER_KEY,
6887 SPOOL_REG_SHORTSERVERNAME,
6888 REG_SZ,
6889 buffer.data,
6890 buffer.length);
6891 if (!W_ERROR_IS_OK(result)) {
6892 DEBUG(0, ("Failed to set %s\n", SPOOL_REG_SHORTSERVERNAME));
6893 goto done;
6896 dnsdomname = get_mydnsfullname();
6897 if (dnsdomname != NULL && dnsdomname[0] != '\0') {
6898 longname = talloc_strdup(tmp_ctx, dnsdomname);
6899 } else {
6900 longname = talloc_strdup(tmp_ctx, lp_netbios_name());
6902 if (longname == NULL) {
6903 result = WERR_NOT_ENOUGH_MEMORY;
6904 goto done;
6907 ok = push_reg_sz(tmp_ctx, &buffer, longname);
6908 if (!ok) {
6909 DEBUG(0, ("longname data corrupted\n"));
6910 result = WERR_INVALID_DATA;
6911 goto done;
6913 result = winreg_set_printer_dataex(tmp_ctx, b,
6914 printer->sharename,
6915 SPOOL_DSSPOOLER_KEY,
6916 SPOOL_REG_SERVERNAME,
6917 REG_SZ,
6918 buffer.data,
6919 buffer.length);
6920 if (!W_ERROR_IS_OK(result)) {
6921 DEBUG(0, ("Failed to set %s\n", SPOOL_REG_SERVERNAME));
6922 goto done;
6925 uncname = talloc_asprintf(tmp_ctx, "\\\\%s\\%s",
6926 lp_netbios_name(), printer->sharename);
6927 ok = push_reg_sz(tmp_ctx, &buffer, uncname);
6928 if (!ok) {
6929 DEBUG(0, ("uncName data corrupted\n"));
6930 result = WERR_INVALID_DATA;
6931 goto done;
6933 result = winreg_set_printer_dataex(tmp_ctx, b,
6934 printer->sharename,
6935 SPOOL_DSSPOOLER_KEY,
6936 SPOOL_REG_UNCNAME,
6937 REG_SZ,
6938 buffer.data,
6939 buffer.length);
6940 if (!W_ERROR_IS_OK(result)) {
6941 DEBUG(0, ("Failed to set %s\n", SPOOL_REG_UNCNAME));
6942 goto done;
6945 done:
6946 talloc_free(tmp_ctx);
6947 return result;
6950 /********************************************************************
6951 * Called by spoolss_api_setprinter
6952 * when updating a printer description.
6953 ********************************************************************/
6955 static WERROR update_printer(struct pipes_struct *p,
6956 struct policy_handle *handle,
6957 struct spoolss_SetPrinterInfoCtr *info_ctr,
6958 struct spoolss_DeviceMode *devmode)
6960 uint32_t printer_mask = SPOOLSS_PRINTER_INFO_ALL;
6961 struct spoolss_SetPrinterInfo2 *printer = info_ctr->info.info2;
6962 struct spoolss_PrinterInfo2 *old_printer;
6963 struct printer_handle *Printer = find_printer_index_by_hnd(p, handle);
6964 const struct loadparm_substitution *lp_sub =
6965 loadparm_s3_global_substitution();
6966 int snum;
6967 WERROR result = WERR_OK;
6968 TALLOC_CTX *tmp_ctx;
6969 struct dcerpc_binding_handle *b;
6971 DEBUG(8,("update_printer\n"));
6973 tmp_ctx = talloc_new(p->mem_ctx);
6974 if (tmp_ctx == NULL) {
6975 return WERR_NOT_ENOUGH_MEMORY;
6978 if (!Printer) {
6979 result = WERR_INVALID_HANDLE;
6980 goto done;
6983 if (!get_printer_snum(p, handle, &snum, NULL)) {
6984 result = WERR_INVALID_HANDLE;
6985 goto done;
6988 result = winreg_printer_binding_handle(tmp_ctx,
6989 get_session_info_system(),
6990 p->msg_ctx,
6991 &b);
6992 if (!W_ERROR_IS_OK(result)) {
6993 goto done;
6996 result = winreg_get_printer(tmp_ctx, b,
6997 lp_const_servicename(snum),
6998 &old_printer);
6999 if (!W_ERROR_IS_OK(result)) {
7000 result = WERR_INVALID_HANDLE;
7001 goto done;
7004 /* Do sanity check on the requested changes for Samba */
7005 if (!check_printer_ok(tmp_ctx, printer, snum)) {
7006 result = WERR_INVALID_PARAMETER;
7007 goto done;
7010 /* FIXME!!! If the driver has changed we really should verify that
7011 it is installed before doing much else --jerry */
7013 /* Check calling user has permission to update printer description */
7014 if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
7015 DEBUG(3, ("update_printer: printer property change denied by handle\n"));
7016 result = WERR_ACCESS_DENIED;
7017 goto done;
7020 /* Call addprinter hook */
7021 /* Check changes to see if this is really needed */
7023 if (*lp_addprinter_command(talloc_tos(), lp_sub) &&
7024 (!strequal(printer->drivername, old_printer->drivername) ||
7025 !strequal(printer->comment, old_printer->comment) ||
7026 !strequal(printer->portname, old_printer->portname) ||
7027 !strequal(printer->location, old_printer->location)) )
7029 char *raddr;
7031 raddr = tsocket_address_inet_addr_string(p->remote_address,
7032 p->mem_ctx);
7033 if (raddr == NULL) {
7034 return WERR_NOT_ENOUGH_MEMORY;
7037 /* add_printer_hook() will call reload_services() */
7038 if (!add_printer_hook(tmp_ctx, p->session_info->security_token,
7039 printer, raddr,
7040 p->msg_ctx)) {
7041 result = WERR_ACCESS_DENIED;
7042 goto done;
7046 result = update_dsspooler(tmp_ctx,
7047 get_session_info_system(),
7048 p->msg_ctx,
7049 snum,
7050 printer,
7051 old_printer);
7052 if (!W_ERROR_IS_OK(result)) {
7053 goto done;
7056 printer_mask &= ~SPOOLSS_PRINTER_INFO_SECDESC;
7058 if (devmode == NULL) {
7059 printer_mask &= ~SPOOLSS_PRINTER_INFO_DEVMODE;
7061 result = winreg_update_printer(tmp_ctx, b,
7062 printer->sharename,
7063 printer_mask,
7064 printer,
7065 devmode,
7066 NULL);
7068 done:
7069 talloc_free(tmp_ctx);
7071 return result;
7074 /****************************************************************************
7075 ****************************************************************************/
7076 static WERROR publish_or_unpublish_printer(struct pipes_struct *p,
7077 struct policy_handle *handle,
7078 struct spoolss_SetPrinterInfo7 *info7)
7080 #ifdef HAVE_ADS
7081 const struct loadparm_substitution *lp_sub =
7082 loadparm_s3_global_substitution();
7083 struct spoolss_PrinterInfo2 *pinfo2 = NULL;
7084 WERROR result;
7085 int snum;
7086 struct printer_handle *Printer;
7088 if ( lp_security() != SEC_ADS ) {
7089 return WERR_INVALID_LEVEL;
7092 Printer = find_printer_index_by_hnd(p, handle);
7094 DEBUG(5,("publish_or_unpublish_printer, action = %d\n",info7->action));
7096 if (!Printer)
7097 return WERR_INVALID_HANDLE;
7099 if (!get_printer_snum(p, handle, &snum, NULL))
7100 return WERR_INVALID_HANDLE;
7102 result = winreg_get_printer_internal(p->mem_ctx,
7103 get_session_info_system(),
7104 p->msg_ctx,
7105 lp_servicename(talloc_tos(), lp_sub, snum),
7106 &pinfo2);
7107 if (!W_ERROR_IS_OK(result)) {
7108 return WERR_INVALID_HANDLE;
7111 nt_printer_publish(pinfo2,
7112 get_session_info_system(),
7113 p->msg_ctx,
7114 pinfo2,
7115 info7->action);
7117 TALLOC_FREE(pinfo2);
7118 return WERR_OK;
7119 #else
7120 return WERR_INVALID_LEVEL;
7121 #endif
7124 /********************************************************************
7125 ********************************************************************/
7127 static WERROR update_printer_devmode(struct pipes_struct *p,
7128 struct policy_handle *handle,
7129 struct spoolss_DeviceMode *devmode)
7131 int snum;
7132 struct printer_handle *Printer = find_printer_index_by_hnd(p, handle);
7133 uint32_t info2_mask = SPOOLSS_PRINTER_INFO_DEVMODE;
7135 DEBUG(8,("update_printer_devmode\n"));
7137 if (!Printer) {
7138 return WERR_INVALID_HANDLE;
7141 if (!get_printer_snum(p, handle, &snum, NULL)) {
7142 return WERR_INVALID_HANDLE;
7145 /* Check calling user has permission to update printer description */
7146 if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
7147 DEBUG(3, ("update_printer: printer property change denied by handle\n"));
7148 return WERR_ACCESS_DENIED;
7151 return winreg_update_printer_internal(p->mem_ctx,
7152 get_session_info_system(),
7153 p->msg_ctx,
7154 lp_const_servicename(snum),
7155 info2_mask,
7156 NULL,
7157 devmode,
7158 NULL);
7162 /****************************************************************
7163 _spoolss_SetPrinter
7164 ****************************************************************/
7166 WERROR _spoolss_SetPrinter(struct pipes_struct *p,
7167 struct spoolss_SetPrinter *r)
7169 WERROR result;
7171 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
7173 if (!Printer) {
7174 DEBUG(2,("_spoolss_SetPrinter: Invalid handle (%s:%u:%u)\n",
7175 OUR_HANDLE(r->in.handle)));
7176 return WERR_INVALID_HANDLE;
7179 /* check the level */
7180 switch (r->in.info_ctr->level) {
7181 case 0:
7182 return control_printer(r->in.handle, r->in.command, p);
7183 case 2:
7184 result = update_printer(p, r->in.handle,
7185 r->in.info_ctr,
7186 r->in.devmode_ctr->devmode);
7187 if (!W_ERROR_IS_OK(result))
7188 return result;
7189 if (r->in.secdesc_ctr->sd)
7190 result = update_printer_sec(r->in.handle, p,
7191 r->in.secdesc_ctr);
7192 return result;
7193 case 3:
7194 return update_printer_sec(r->in.handle, p,
7195 r->in.secdesc_ctr);
7196 case 4: {
7197 struct spoolss_PrinterInfo2 *old_printer;
7198 struct spoolss_SetPrinterInfo2 *set_old_printer;
7199 struct spoolss_SetPrinterInfoCtr *info_ctr;
7200 struct dcerpc_binding_handle *b;
7201 int snum;
7202 TALLOC_CTX *tmp_ctx;
7204 tmp_ctx = talloc_new(p->mem_ctx);
7205 if (tmp_ctx == NULL) {
7206 return WERR_NOT_ENOUGH_MEMORY;
7209 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
7210 TALLOC_FREE(tmp_ctx);
7211 return WERR_INVALID_HANDLE;
7214 result = winreg_printer_binding_handle(tmp_ctx,
7215 get_session_info_system(),
7216 p->msg_ctx,
7217 &b);
7218 if (!W_ERROR_IS_OK(result)) {
7219 TALLOC_FREE(tmp_ctx);
7220 return result;
7223 result = winreg_get_printer(tmp_ctx, b,
7224 lp_const_servicename(snum),
7225 &old_printer);
7226 if (!W_ERROR_IS_OK(result)) {
7227 TALLOC_FREE(tmp_ctx);
7228 return WERR_INVALID_HANDLE;
7231 old_printer->servername = talloc_strdup(tmp_ctx, r->in.info_ctr->info.info4->servername);
7232 if (old_printer->servername == NULL) {
7233 TALLOC_FREE(tmp_ctx);
7234 return WERR_NOT_ENOUGH_MEMORY;
7237 old_printer->printername = talloc_strdup(tmp_ctx, r->in.info_ctr->info.info4->printername);
7238 if (old_printer->printername == NULL) {
7239 TALLOC_FREE(tmp_ctx);
7240 return WERR_NOT_ENOUGH_MEMORY;
7243 old_printer->attributes = r->in.info_ctr->info.info4->attributes;
7245 set_old_printer = talloc_zero(tmp_ctx, struct spoolss_SetPrinterInfo2);
7246 if (set_old_printer == NULL) {
7247 TALLOC_FREE(tmp_ctx);
7248 return WERR_NOT_ENOUGH_MEMORY;
7251 spoolss_printerinfo2_to_setprinterinfo2(old_printer, set_old_printer);
7253 info_ctr = talloc_zero(tmp_ctx, struct spoolss_SetPrinterInfoCtr);
7254 if (info_ctr == NULL) {
7255 TALLOC_FREE(tmp_ctx);
7256 return WERR_NOT_ENOUGH_MEMORY;
7259 info_ctr->level = 2;
7260 info_ctr->info.info2 = set_old_printer;
7262 result = update_printer(p, r->in.handle,
7263 info_ctr,
7264 r->in.devmode_ctr->devmode);
7266 if (!W_ERROR_IS_OK(result)) {
7267 TALLOC_FREE(tmp_ctx);
7268 return result;
7271 if (r->in.secdesc_ctr->sd) {
7272 result = update_printer_sec(r->in.handle, p,
7273 r->in.secdesc_ctr);
7276 TALLOC_FREE(tmp_ctx);
7277 return result;
7279 case 7:
7280 return publish_or_unpublish_printer(p, r->in.handle,
7281 r->in.info_ctr->info.info7);
7282 case 8:
7283 return update_printer_devmode(p, r->in.handle,
7284 r->in.devmode_ctr->devmode);
7285 default:
7286 return WERR_INVALID_LEVEL;
7290 /****************************************************************
7291 _spoolss_FindClosePrinterNotify
7292 ****************************************************************/
7294 WERROR _spoolss_FindClosePrinterNotify(struct pipes_struct *p,
7295 struct spoolss_FindClosePrinterNotify *r)
7297 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
7299 if (!Printer) {
7300 DEBUG(2,("_spoolss_FindClosePrinterNotify: "
7301 "Invalid handle (%s:%u:%u)\n", OUR_HANDLE(r->in.handle)));
7302 return WERR_INVALID_HANDLE;
7305 if (Printer->notify.cli_chan != NULL &&
7306 Printer->notify.cli_chan->active_connections > 0) {
7307 int snum = -1;
7309 if (Printer->printer_type == SPLHND_PRINTER) {
7310 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
7311 return WERR_INVALID_HANDLE;
7315 srv_spoolss_replycloseprinter(snum, Printer);
7318 Printer->notify.flags=0;
7319 Printer->notify.options=0;
7320 Printer->notify.localmachine[0]='\0';
7321 Printer->notify.printerlocal=0;
7322 TALLOC_FREE(Printer->notify.option);
7324 return WERR_OK;
7327 /****************************************************************
7328 _spoolss_AddJob
7329 ****************************************************************/
7331 WERROR _spoolss_AddJob(struct pipes_struct *p,
7332 struct spoolss_AddJob *r)
7334 if (!r->in.buffer && (r->in.offered != 0)) {
7335 return WERR_INVALID_PARAMETER;
7338 /* this is what a NT server returns for AddJob. AddJob must fail on
7339 * non-local printers */
7341 if (r->in.level != 1) {
7342 return WERR_INVALID_LEVEL;
7345 return WERR_INVALID_PARAMETER;
7348 /****************************************************************************
7349 fill_job_info1
7350 ****************************************************************************/
7352 static WERROR fill_job_info1(TALLOC_CTX *mem_ctx,
7353 struct spoolss_JobInfo1 *r,
7354 const print_queue_struct *queue,
7355 uint32_t jobid,
7356 int position, int snum,
7357 struct spoolss_PrinterInfo2 *pinfo2)
7359 const struct loadparm_substitution *lp_sub =
7360 loadparm_s3_global_substitution();
7361 struct tm *t;
7363 t = gmtime(&queue->time);
7365 r->job_id = jobid;
7367 r->printer_name = lp_servicename(mem_ctx, lp_sub, snum);
7368 W_ERROR_HAVE_NO_MEMORY(r->printer_name);
7369 r->server_name = talloc_strdup(mem_ctx, pinfo2->servername);
7370 W_ERROR_HAVE_NO_MEMORY(r->server_name);
7371 r->user_name = talloc_strdup(mem_ctx, queue->fs_user);
7372 W_ERROR_HAVE_NO_MEMORY(r->user_name);
7373 r->document_name = talloc_strdup(mem_ctx, queue->fs_file);
7374 W_ERROR_HAVE_NO_MEMORY(r->document_name);
7375 r->data_type = talloc_strdup(mem_ctx, "RAW");
7376 W_ERROR_HAVE_NO_MEMORY(r->data_type);
7377 r->text_status = talloc_strdup(mem_ctx, "");
7378 W_ERROR_HAVE_NO_MEMORY(r->text_status);
7380 r->status = nt_printj_status(queue->status);
7381 r->priority = queue->priority;
7382 r->position = position;
7383 r->total_pages = queue->page_count;
7384 r->pages_printed = 0; /* ??? */
7386 init_systemtime(&r->submitted, t);
7388 return WERR_OK;
7391 /****************************************************************************
7392 fill_job_info2
7393 ****************************************************************************/
7395 static WERROR fill_job_info2(TALLOC_CTX *mem_ctx,
7396 struct spoolss_JobInfo2 *r,
7397 const print_queue_struct *queue,
7398 uint32_t jobid,
7399 int position, int snum,
7400 struct spoolss_PrinterInfo2 *pinfo2,
7401 struct spoolss_DeviceMode *devmode)
7403 const struct loadparm_substitution *lp_sub =
7404 loadparm_s3_global_substitution();
7405 struct tm *t;
7407 t = gmtime(&queue->time);
7409 r->job_id = jobid;
7411 r->printer_name = lp_servicename(mem_ctx, lp_sub, snum);
7412 W_ERROR_HAVE_NO_MEMORY(r->printer_name);
7413 r->server_name = talloc_strdup(mem_ctx, pinfo2->servername);
7414 W_ERROR_HAVE_NO_MEMORY(r->server_name);
7415 r->user_name = talloc_strdup(mem_ctx, queue->fs_user);
7416 W_ERROR_HAVE_NO_MEMORY(r->user_name);
7417 r->document_name = talloc_strdup(mem_ctx, queue->fs_file);
7418 W_ERROR_HAVE_NO_MEMORY(r->document_name);
7419 r->notify_name = talloc_strdup(mem_ctx, queue->fs_user);
7420 W_ERROR_HAVE_NO_MEMORY(r->notify_name);
7421 r->data_type = talloc_strdup(mem_ctx, "RAW");
7422 W_ERROR_HAVE_NO_MEMORY(r->data_type);
7423 r->print_processor = talloc_strdup(mem_ctx, "winprint");
7424 W_ERROR_HAVE_NO_MEMORY(r->print_processor);
7425 r->parameters = talloc_strdup(mem_ctx, "");
7426 W_ERROR_HAVE_NO_MEMORY(r->parameters);
7427 r->driver_name = talloc_strdup(mem_ctx, pinfo2->drivername);
7428 W_ERROR_HAVE_NO_MEMORY(r->driver_name);
7430 r->devmode = devmode;
7432 r->text_status = talloc_strdup(mem_ctx, "");
7433 W_ERROR_HAVE_NO_MEMORY(r->text_status);
7435 r->secdesc = NULL;
7437 r->status = nt_printj_status(queue->status);
7438 r->priority = queue->priority;
7439 r->position = position;
7440 r->start_time = 0;
7441 r->until_time = 0;
7442 r->total_pages = queue->page_count;
7443 r->size = queue->size;
7444 init_systemtime(&r->submitted, t);
7445 r->time = 0;
7446 r->pages_printed = 0; /* ??? */
7448 return WERR_OK;
7451 /****************************************************************************
7452 Enumjobs at level 1.
7453 ****************************************************************************/
7455 static WERROR enumjobs_level1(TALLOC_CTX *mem_ctx,
7456 const print_queue_struct *queue,
7457 uint32_t num_queues, int snum,
7458 struct spoolss_PrinterInfo2 *pinfo2,
7459 union spoolss_JobInfo **info_p,
7460 uint32_t *count)
7462 union spoolss_JobInfo *info;
7463 int i;
7464 WERROR result = WERR_OK;
7465 uint32_t num_filled;
7466 struct tdb_print_db *pdb;
7468 info = talloc_array(mem_ctx, union spoolss_JobInfo, num_queues);
7469 if (info == NULL) {
7470 result = WERR_NOT_ENOUGH_MEMORY;
7471 goto err_out;
7474 pdb = get_print_db_byname(pinfo2->sharename);
7475 if (pdb == NULL) {
7476 result = WERR_INVALID_PARAMETER;
7477 goto err_info_free;
7480 num_filled = 0;
7481 for (i = 0; i < num_queues; i++) {
7482 uint32_t jobid = sysjob_to_jobid_pdb(pdb, queue[i].sysjob);
7483 if (jobid == (uint32_t)-1) {
7484 DEBUG(4, ("skipping sysjob %d\n", queue[i].sysjob));
7485 continue;
7488 result = fill_job_info1(info,
7489 &info[num_filled].info1,
7490 &queue[i],
7491 jobid,
7493 snum,
7494 pinfo2);
7495 if (!W_ERROR_IS_OK(result)) {
7496 goto err_pdb_drop;
7499 num_filled++;
7502 release_print_db(pdb);
7503 *info_p = info;
7504 *count = num_filled;
7506 return WERR_OK;
7508 err_pdb_drop:
7509 release_print_db(pdb);
7510 err_info_free:
7511 TALLOC_FREE(info);
7512 err_out:
7513 *count = 0;
7514 return result;
7517 /****************************************************************************
7518 Enumjobs at level 2.
7519 ****************************************************************************/
7521 static WERROR enumjobs_level2(TALLOC_CTX *mem_ctx,
7522 const print_queue_struct *queue,
7523 uint32_t num_queues, int snum,
7524 struct spoolss_PrinterInfo2 *pinfo2,
7525 union spoolss_JobInfo **info_p,
7526 uint32_t *count)
7528 union spoolss_JobInfo *info;
7529 int i;
7530 WERROR result = WERR_OK;
7531 uint32_t num_filled;
7532 struct tdb_print_db *pdb;
7534 info = talloc_array(mem_ctx, union spoolss_JobInfo, num_queues);
7535 if (info == NULL) {
7536 result = WERR_NOT_ENOUGH_MEMORY;
7537 goto err_out;
7540 pdb = get_print_db_byname(pinfo2->sharename);
7541 if (pdb == NULL) {
7542 result = WERR_INVALID_PARAMETER;
7543 goto err_info_free;
7546 num_filled = 0;
7547 for (i = 0; i< num_queues; i++) {
7548 struct spoolss_DeviceMode *devmode;
7549 uint32_t jobid = sysjob_to_jobid_pdb(pdb, queue[i].sysjob);
7550 if (jobid == (uint32_t)-1) {
7551 DEBUG(4, ("skipping sysjob %d\n", queue[i].sysjob));
7552 continue;
7555 result = spoolss_create_default_devmode(info,
7556 pinfo2->printername,
7557 &devmode);
7558 if (!W_ERROR_IS_OK(result)) {
7559 DEBUG(3, ("Can't proceed w/o a devmode!"));
7560 goto err_pdb_drop;
7563 result = fill_job_info2(info,
7564 &info[num_filled].info2,
7565 &queue[i],
7566 jobid,
7568 snum,
7569 pinfo2,
7570 devmode);
7571 if (!W_ERROR_IS_OK(result)) {
7572 goto err_pdb_drop;
7574 num_filled++;
7577 release_print_db(pdb);
7578 *info_p = info;
7579 *count = num_filled;
7581 return WERR_OK;
7583 err_pdb_drop:
7584 release_print_db(pdb);
7585 err_info_free:
7586 TALLOC_FREE(info);
7587 err_out:
7588 *count = 0;
7589 return result;
7592 /****************************************************************************
7593 Enumjobs at level 3.
7594 ****************************************************************************/
7596 static WERROR enumjobs_level3(TALLOC_CTX *mem_ctx,
7597 const print_queue_struct *queue,
7598 uint32_t num_queues, int snum,
7599 struct spoolss_PrinterInfo2 *pinfo2,
7600 union spoolss_JobInfo **info_p,
7601 uint32_t *count)
7603 union spoolss_JobInfo *info;
7604 int i;
7605 WERROR result = WERR_OK;
7606 uint32_t num_filled;
7607 struct tdb_print_db *pdb;
7609 info = talloc_array(mem_ctx, union spoolss_JobInfo, num_queues);
7610 if (info == NULL) {
7611 result = WERR_NOT_ENOUGH_MEMORY;
7612 goto err_out;
7615 pdb = get_print_db_byname(pinfo2->sharename);
7616 if (pdb == NULL) {
7617 result = WERR_INVALID_PARAMETER;
7618 goto err_info_free;
7621 num_filled = 0;
7622 for (i = 0; i < num_queues; i++) {
7623 uint32_t jobid = sysjob_to_jobid_pdb(pdb, queue[i].sysjob);
7624 if (jobid == (uint32_t)-1) {
7625 DEBUG(4, ("skipping sysjob %d\n", queue[i].sysjob));
7626 continue;
7629 info[num_filled].info3.job_id = jobid;
7630 /* next_job_id is overwritten on next iteration */
7631 info[num_filled].info3.next_job_id = 0;
7632 info[num_filled].info3.reserved = 0;
7634 if (num_filled > 0) {
7635 info[num_filled - 1].info3.next_job_id = jobid;
7637 num_filled++;
7640 release_print_db(pdb);
7641 *info_p = info;
7642 *count = num_filled;
7644 return WERR_OK;
7646 err_info_free:
7647 TALLOC_FREE(info);
7648 err_out:
7649 *count = 0;
7650 return result;
7653 /****************************************************************
7654 _spoolss_EnumJobs
7655 ****************************************************************/
7657 WERROR _spoolss_EnumJobs(struct pipes_struct *p,
7658 struct spoolss_EnumJobs *r)
7660 WERROR result;
7661 struct spoolss_PrinterInfo2 *pinfo2 = NULL;
7662 int snum;
7663 print_status_struct prt_status;
7664 print_queue_struct *queue = NULL;
7665 uint32_t count;
7667 /* that's an [in out] buffer */
7669 if (!r->in.buffer && (r->in.offered != 0)) {
7670 return WERR_INVALID_PARAMETER;
7673 if ((r->in.level != 1) && (r->in.level != 2) && (r->in.level != 3)) {
7674 DEBUG(4, ("EnumJobs level %d not supported\n", r->in.level));
7675 return WERR_INVALID_LEVEL;
7678 DEBUG(4,("_spoolss_EnumJobs\n"));
7680 *r->out.needed = 0;
7681 *r->out.count = 0;
7682 *r->out.info = NULL;
7684 /* lookup the printer snum and tdb entry */
7686 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
7687 return WERR_INVALID_HANDLE;
7690 result = winreg_get_printer_internal(p->mem_ctx,
7691 get_session_info_system(),
7692 p->msg_ctx,
7693 lp_const_servicename(snum),
7694 &pinfo2);
7695 if (!W_ERROR_IS_OK(result)) {
7696 return result;
7699 count = print_queue_status(p->msg_ctx, snum, &queue, &prt_status);
7700 DEBUGADD(4,("count:[%d], status:[%d], [%s]\n",
7701 count, prt_status.status, prt_status.message));
7703 if (count == 0) {
7704 SAFE_FREE(queue);
7705 TALLOC_FREE(pinfo2);
7706 return WERR_OK;
7709 switch (r->in.level) {
7710 case 1:
7711 result = enumjobs_level1(p->mem_ctx, queue, count, snum,
7712 pinfo2, r->out.info, r->out.count);
7713 break;
7714 case 2:
7715 result = enumjobs_level2(p->mem_ctx, queue, count, snum,
7716 pinfo2, r->out.info, r->out.count);
7717 break;
7718 case 3:
7719 result = enumjobs_level3(p->mem_ctx, queue, count, snum,
7720 pinfo2, r->out.info, r->out.count);
7721 break;
7722 default:
7723 SMB_ASSERT(false); /* level checked on entry */
7724 break;
7727 SAFE_FREE(queue);
7728 TALLOC_FREE(pinfo2);
7730 if (!W_ERROR_IS_OK(result)) {
7731 return result;
7734 *r->out.needed = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
7735 spoolss_EnumJobs,
7736 *r->out.info, r->in.level,
7737 *r->out.count);
7738 *r->out.info = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
7739 *r->out.count = SPOOLSS_BUFFER_OK(*r->out.count, 0);
7741 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
7744 /****************************************************************
7745 _spoolss_ScheduleJob
7746 ****************************************************************/
7748 WERROR _spoolss_ScheduleJob(struct pipes_struct *p,
7749 struct spoolss_ScheduleJob *r)
7751 return WERR_OK;
7754 /****************************************************************
7755 ****************************************************************/
7757 static WERROR spoolss_setjob_1(TALLOC_CTX *mem_ctx,
7758 struct messaging_context *msg_ctx,
7759 const char *printer_name,
7760 uint32_t job_id,
7761 struct spoolss_SetJobInfo1 *r)
7763 char *old_doc_name;
7765 if (!print_job_get_name(mem_ctx, printer_name, job_id, &old_doc_name)) {
7766 return WERR_INVALID_HANDLE;
7769 if (strequal(old_doc_name, r->document_name)) {
7770 return WERR_OK;
7773 if (!print_job_set_name(global_event_context(), msg_ctx,
7774 printer_name, job_id, r->document_name)) {
7775 return WERR_INVALID_HANDLE;
7778 return WERR_OK;
7781 /****************************************************************
7782 _spoolss_SetJob
7783 ****************************************************************/
7785 WERROR _spoolss_SetJob(struct pipes_struct *p,
7786 struct spoolss_SetJob *r)
7788 const struct auth_session_info *session_info = p->session_info;
7789 int snum;
7790 WERROR errcode = WERR_INVALID_FUNCTION;
7792 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
7793 return WERR_INVALID_HANDLE;
7796 if (!print_job_exists(lp_const_servicename(snum), r->in.job_id)) {
7797 return WERR_INVALID_PRINTER_NAME;
7800 switch (r->in.command) {
7801 case SPOOLSS_JOB_CONTROL_CANCEL:
7802 case SPOOLSS_JOB_CONTROL_DELETE:
7803 errcode = print_job_delete(session_info, p->msg_ctx,
7804 snum, r->in.job_id);
7805 if (W_ERROR_EQUAL(errcode, WERR_PRINTER_HAS_JOBS_QUEUED)) {
7806 errcode = WERR_OK;
7808 break;
7809 case SPOOLSS_JOB_CONTROL_PAUSE:
7810 errcode = print_job_pause(session_info, p->msg_ctx,
7811 snum, r->in.job_id);
7812 break;
7813 case SPOOLSS_JOB_CONTROL_RESTART:
7814 case SPOOLSS_JOB_CONTROL_RESUME:
7815 errcode = print_job_resume(session_info, p->msg_ctx,
7816 snum, r->in.job_id);
7817 break;
7818 case SPOOLSS_JOB_CONTROL_NOOP:
7819 errcode = WERR_OK;
7820 break;
7821 default:
7822 return WERR_INVALID_LEVEL;
7825 if (!W_ERROR_IS_OK(errcode)) {
7826 return errcode;
7829 if (r->in.ctr == NULL) {
7830 return errcode;
7833 switch (r->in.ctr->level) {
7834 case 1:
7835 errcode = spoolss_setjob_1(p->mem_ctx, p->msg_ctx,
7836 lp_const_servicename(snum),
7837 r->in.job_id,
7838 r->in.ctr->info.info1);
7839 break;
7840 case 2:
7841 case 3:
7842 case 4:
7843 default:
7844 return WERR_INVALID_LEVEL;
7847 return errcode;
7850 /****************************************************************************
7851 Enumerates all printer drivers by level and architecture.
7852 ****************************************************************************/
7854 static WERROR enumprinterdrivers_level_by_architecture(TALLOC_CTX *mem_ctx,
7855 const struct auth_session_info *session_info,
7856 struct messaging_context *msg_ctx,
7857 const char *servername,
7858 const char *architecture,
7859 uint32_t level,
7860 union spoolss_DriverInfo **info_p,
7861 uint32_t *count_p)
7863 int i;
7864 uint32_t version;
7865 struct spoolss_DriverInfo8 *driver;
7866 union spoolss_DriverInfo *info = NULL;
7867 uint32_t count = 0;
7868 WERROR result = WERR_OK;
7869 uint32_t num_drivers;
7870 const char **drivers;
7871 struct dcerpc_binding_handle *b;
7872 TALLOC_CTX *tmp_ctx = NULL;
7874 *count_p = 0;
7875 *info_p = NULL;
7877 tmp_ctx = talloc_new(mem_ctx);
7878 if (!tmp_ctx) {
7879 return WERR_NOT_ENOUGH_MEMORY;
7882 result = winreg_printer_binding_handle(tmp_ctx,
7883 session_info,
7884 msg_ctx,
7885 &b);
7886 if (!W_ERROR_IS_OK(result)) {
7887 goto out;
7890 for (version=0; version<DRIVER_MAX_VERSION; version++) {
7891 result = winreg_get_driver_list(tmp_ctx, b,
7892 architecture, version,
7893 &num_drivers, &drivers);
7894 if (!W_ERROR_IS_OK(result)) {
7895 goto out;
7897 DEBUG(4, ("we have:[%d] drivers in environment"
7898 " [%s] and version [%d]\n",
7899 num_drivers, architecture, version));
7901 if (num_drivers != 0) {
7902 info = talloc_realloc(tmp_ctx, info,
7903 union spoolss_DriverInfo,
7904 count + num_drivers);
7905 if (!info) {
7906 DEBUG(0,("enumprinterdrivers_level_by_architecture: "
7907 "failed to enlarge driver info buffer!\n"));
7908 result = WERR_NOT_ENOUGH_MEMORY;
7909 goto out;
7913 for (i = 0; i < num_drivers; i++) {
7914 DEBUG(5, ("\tdriver: [%s]\n", drivers[i]));
7916 result = winreg_get_driver(tmp_ctx, b,
7917 architecture, drivers[i],
7918 version, &driver);
7919 if (!W_ERROR_IS_OK(result)) {
7920 goto out;
7923 switch (level) {
7924 case 1:
7925 result = fill_printer_driver_info1(info, &info[count+i].info1,
7926 driver, servername);
7927 break;
7928 case 2:
7929 result = fill_printer_driver_info2(info, &info[count+i].info2,
7930 driver, servername);
7931 break;
7932 case 3:
7933 result = fill_printer_driver_info3(info, &info[count+i].info3,
7934 driver, servername);
7935 break;
7936 case 4:
7937 result = fill_printer_driver_info4(info, &info[count+i].info4,
7938 driver, servername);
7939 break;
7940 case 5:
7941 result = fill_printer_driver_info5(info, &info[count+i].info5,
7942 driver, servername);
7943 break;
7944 case 6:
7945 result = fill_printer_driver_info6(info, &info[count+i].info6,
7946 driver, servername);
7947 break;
7948 case 8:
7949 result = fill_printer_driver_info8(info, &info[count+i].info8,
7950 driver, servername);
7951 break;
7952 default:
7953 result = WERR_INVALID_LEVEL;
7954 break;
7957 TALLOC_FREE(driver);
7959 if (!W_ERROR_IS_OK(result)) {
7960 goto out;
7964 count += num_drivers;
7965 TALLOC_FREE(drivers);
7968 out:
7969 if (W_ERROR_IS_OK(result)) {
7970 *info_p = talloc_move(mem_ctx, &info);
7971 *count_p = count;
7974 talloc_free(tmp_ctx);
7975 return result;
7978 /****************************************************************************
7979 Enumerates all printer drivers by level.
7980 ****************************************************************************/
7982 static WERROR enumprinterdrivers_level(TALLOC_CTX *mem_ctx,
7983 const struct auth_session_info *session_info,
7984 struct messaging_context *msg_ctx,
7985 const char *servername,
7986 const char *architecture,
7987 uint32_t level,
7988 union spoolss_DriverInfo **info_p,
7989 uint32_t *count_p)
7991 uint32_t a,i;
7992 WERROR result = WERR_OK;
7994 if (strequal(architecture, SPOOLSS_ARCHITECTURE_ALL)) {
7996 for (a=0; archi_table[a].long_archi != NULL; a++) {
7998 union spoolss_DriverInfo *info = NULL;
7999 uint32_t count = 0;
8001 result = enumprinterdrivers_level_by_architecture(mem_ctx,
8002 session_info,
8003 msg_ctx,
8004 servername,
8005 archi_table[a].long_archi,
8006 level,
8007 &info,
8008 &count);
8009 if (!W_ERROR_IS_OK(result)) {
8010 continue;
8013 for (i=0; i < count; i++) {
8014 ADD_TO_ARRAY(mem_ctx, union spoolss_DriverInfo,
8015 info[i], info_p, count_p);
8019 return result;
8022 return enumprinterdrivers_level_by_architecture(mem_ctx,
8023 session_info,
8024 msg_ctx,
8025 servername,
8026 architecture,
8027 level,
8028 info_p,
8029 count_p);
8032 /****************************************************************
8033 _spoolss_EnumPrinterDrivers
8034 ****************************************************************/
8036 WERROR _spoolss_EnumPrinterDrivers(struct pipes_struct *p,
8037 struct spoolss_EnumPrinterDrivers *r)
8039 const char *cservername;
8040 WERROR result;
8042 /* that's an [in out] buffer */
8044 if (!r->in.buffer && (r->in.offered != 0)) {
8045 return WERR_INVALID_PARAMETER;
8048 DEBUG(4,("_spoolss_EnumPrinterDrivers\n"));
8050 *r->out.needed = 0;
8051 *r->out.count = 0;
8052 *r->out.info = NULL;
8054 cservername = canon_servername(r->in.server);
8056 if (!is_myname_or_ipaddr(cservername)) {
8057 return WERR_UNKNOWN_PRINTER_DRIVER;
8060 result = enumprinterdrivers_level(p->mem_ctx,
8061 get_session_info_system(),
8062 p->msg_ctx,
8063 cservername,
8064 r->in.environment,
8065 r->in.level,
8066 r->out.info,
8067 r->out.count);
8068 if (!W_ERROR_IS_OK(result)) {
8069 return result;
8072 *r->out.needed = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
8073 spoolss_EnumPrinterDrivers,
8074 *r->out.info, r->in.level,
8075 *r->out.count);
8076 *r->out.info = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
8077 *r->out.count = SPOOLSS_BUFFER_OK(*r->out.count, 0);
8079 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
8082 /****************************************************************
8083 _spoolss_EnumForms
8084 ****************************************************************/
8086 WERROR _spoolss_EnumForms(struct pipes_struct *p,
8087 struct spoolss_EnumForms *r)
8089 WERROR result;
8091 *r->out.count = 0;
8092 *r->out.needed = 0;
8093 *r->out.info = NULL;
8095 /* that's an [in out] buffer */
8097 if (!r->in.buffer && (r->in.offered != 0) ) {
8098 return WERR_INVALID_PARAMETER;
8101 DEBUG(4,("_spoolss_EnumForms\n"));
8102 DEBUGADD(5,("Offered buffer size [%d]\n", r->in.offered));
8103 DEBUGADD(5,("Info level [%d]\n", r->in.level));
8105 switch (r->in.level) {
8106 case 1:
8107 result = winreg_printer_enumforms1_internal(p->mem_ctx,
8108 get_session_info_system(),
8109 p->msg_ctx,
8110 r->out.count,
8111 r->out.info);
8112 break;
8113 default:
8114 result = WERR_INVALID_LEVEL;
8115 break;
8118 if (!W_ERROR_IS_OK(result)) {
8119 return result;
8122 if (*r->out.count == 0) {
8123 return WERR_NO_MORE_ITEMS;
8126 *r->out.needed = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
8127 spoolss_EnumForms,
8128 *r->out.info, r->in.level,
8129 *r->out.count);
8130 *r->out.info = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
8131 *r->out.count = SPOOLSS_BUFFER_OK(*r->out.count, 0);
8133 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
8136 /****************************************************************
8137 _spoolss_GetForm
8138 ****************************************************************/
8140 WERROR _spoolss_GetForm(struct pipes_struct *p,
8141 struct spoolss_GetForm *r)
8143 WERROR result;
8145 /* that's an [in out] buffer */
8147 if (!r->in.buffer && (r->in.offered != 0)) {
8148 TALLOC_FREE(r->out.info);
8149 return WERR_INVALID_PARAMETER;
8152 DEBUG(4,("_spoolss_GetForm\n"));
8153 DEBUGADD(5,("Offered buffer size [%d]\n", r->in.offered));
8154 DEBUGADD(5,("Info level [%d]\n", r->in.level));
8156 switch (r->in.level) {
8157 case 1:
8158 result = winreg_printer_getform1_internal(p->mem_ctx,
8159 get_session_info_system(),
8160 p->msg_ctx,
8161 r->in.form_name,
8162 &r->out.info->info1);
8163 break;
8164 default:
8165 result = WERR_INVALID_LEVEL;
8166 break;
8169 if (!W_ERROR_IS_OK(result)) {
8170 TALLOC_FREE(r->out.info);
8171 return result;
8174 *r->out.needed = SPOOLSS_BUFFER_UNION(spoolss_FormInfo,
8175 r->out.info, r->in.level);
8176 r->out.info = SPOOLSS_BUFFER_OK(r->out.info, NULL);
8178 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
8181 /****************************************************************************
8182 ****************************************************************************/
8184 static WERROR fill_port_1(TALLOC_CTX *mem_ctx,
8185 struct spoolss_PortInfo1 *r,
8186 const char *name)
8188 r->port_name = talloc_strdup(mem_ctx, name);
8189 W_ERROR_HAVE_NO_MEMORY(r->port_name);
8191 return WERR_OK;
8194 /****************************************************************************
8195 TODO: This probably needs distinguish between TCP/IP and Local ports
8196 somehow.
8197 ****************************************************************************/
8199 static WERROR fill_port_2(TALLOC_CTX *mem_ctx,
8200 struct spoolss_PortInfo2 *r,
8201 const char *name)
8203 r->port_name = talloc_strdup(mem_ctx, name);
8204 W_ERROR_HAVE_NO_MEMORY(r->port_name);
8206 r->monitor_name = talloc_strdup(mem_ctx, "Local Monitor");
8207 W_ERROR_HAVE_NO_MEMORY(r->monitor_name);
8209 r->description = talloc_strdup(mem_ctx, SPL_LOCAL_PORT);
8210 W_ERROR_HAVE_NO_MEMORY(r->description);
8212 r->port_type = SPOOLSS_PORT_TYPE_WRITE;
8213 r->reserved = 0;
8215 return WERR_OK;
8219 /****************************************************************************
8220 wrapper around the enumer ports command
8221 ****************************************************************************/
8223 static WERROR enumports_hook(TALLOC_CTX *ctx, int *count, char ***lines)
8225 const struct loadparm_substitution *lp_sub =
8226 loadparm_s3_global_substitution();
8227 char *cmd = lp_enumports_command(talloc_tos(), lp_sub);
8228 char **qlines = NULL;
8229 char *command = NULL;
8230 int numlines;
8231 int ret;
8232 int fd;
8234 *count = 0;
8235 *lines = NULL;
8237 /* if no hook then just fill in the default port */
8239 if ( !*cmd ) {
8240 if (!(qlines = talloc_array( NULL, char*, 2 ))) {
8241 return WERR_NOT_ENOUGH_MEMORY;
8243 if (!(qlines[0] = talloc_strdup(qlines, SAMBA_PRINTER_PORT_NAME ))) {
8244 TALLOC_FREE(qlines);
8245 return WERR_NOT_ENOUGH_MEMORY;
8247 qlines[1] = NULL;
8248 numlines = 1;
8250 else {
8251 /* we have a valid enumport command */
8253 command = talloc_asprintf(ctx, "%s \"%d\"", cmd, 1);
8254 if (!command) {
8255 return WERR_NOT_ENOUGH_MEMORY;
8258 DEBUG(10,("Running [%s]\n", command));
8259 ret = smbrun(command, &fd, NULL);
8260 DEBUG(10,("Returned [%d]\n", ret));
8261 TALLOC_FREE(command);
8262 if (ret != 0) {
8263 if (fd != -1) {
8264 close(fd);
8266 return WERR_ACCESS_DENIED;
8269 numlines = 0;
8270 qlines = fd_lines_load(fd, &numlines, 0, NULL);
8271 DEBUGADD(10,("Lines returned = [%d]\n", numlines));
8272 close(fd);
8275 *count = numlines;
8276 *lines = qlines;
8278 return WERR_OK;
8281 /****************************************************************************
8282 enumports level 1.
8283 ****************************************************************************/
8285 static WERROR enumports_level_1(TALLOC_CTX *mem_ctx,
8286 union spoolss_PortInfo **info_p,
8287 uint32_t *count)
8289 union spoolss_PortInfo *info = NULL;
8290 int i=0;
8291 WERROR result = WERR_OK;
8292 char **qlines = NULL;
8293 int numlines = 0;
8295 result = enumports_hook(talloc_tos(), &numlines, &qlines );
8296 if (!W_ERROR_IS_OK(result)) {
8297 goto out;
8300 if (numlines) {
8301 info = talloc_array(mem_ctx, union spoolss_PortInfo, numlines);
8302 if (!info) {
8303 DEBUG(10,("Returning WERR_NOT_ENOUGH_MEMORY\n"));
8304 result = WERR_NOT_ENOUGH_MEMORY;
8305 goto out;
8308 for (i=0; i<numlines; i++) {
8309 DEBUG(6,("Filling port number [%d] with port [%s]\n", i, qlines[i]));
8310 result = fill_port_1(info, &info[i].info1, qlines[i]);
8311 if (!W_ERROR_IS_OK(result)) {
8312 goto out;
8316 TALLOC_FREE(qlines);
8318 out:
8319 if (!W_ERROR_IS_OK(result)) {
8320 TALLOC_FREE(info);
8321 TALLOC_FREE(qlines);
8322 *count = 0;
8323 *info_p = NULL;
8324 return result;
8327 *info_p = info;
8328 *count = numlines;
8330 return WERR_OK;
8333 /****************************************************************************
8334 enumports level 2.
8335 ****************************************************************************/
8337 static WERROR enumports_level_2(TALLOC_CTX *mem_ctx,
8338 union spoolss_PortInfo **info_p,
8339 uint32_t *count)
8341 union spoolss_PortInfo *info = NULL;
8342 int i=0;
8343 WERROR result = WERR_OK;
8344 char **qlines = NULL;
8345 int numlines = 0;
8347 result = enumports_hook(talloc_tos(), &numlines, &qlines );
8348 if (!W_ERROR_IS_OK(result)) {
8349 goto out;
8352 if (numlines) {
8353 info = talloc_array(mem_ctx, union spoolss_PortInfo, numlines);
8354 if (!info) {
8355 DEBUG(10,("Returning WERR_NOT_ENOUGH_MEMORY\n"));
8356 result = WERR_NOT_ENOUGH_MEMORY;
8357 goto out;
8360 for (i=0; i<numlines; i++) {
8361 DEBUG(6,("Filling port number [%d] with port [%s]\n", i, qlines[i]));
8362 result = fill_port_2(info, &info[i].info2, qlines[i]);
8363 if (!W_ERROR_IS_OK(result)) {
8364 goto out;
8368 TALLOC_FREE(qlines);
8370 out:
8371 if (!W_ERROR_IS_OK(result)) {
8372 TALLOC_FREE(info);
8373 TALLOC_FREE(qlines);
8374 *count = 0;
8375 *info_p = NULL;
8376 return result;
8379 *info_p = info;
8380 *count = numlines;
8382 return WERR_OK;
8385 /****************************************************************
8386 _spoolss_EnumPorts
8387 ****************************************************************/
8389 WERROR _spoolss_EnumPorts(struct pipes_struct *p,
8390 struct spoolss_EnumPorts *r)
8392 WERROR result;
8394 /* that's an [in out] buffer */
8396 if (!r->in.buffer && (r->in.offered != 0)) {
8397 return WERR_INVALID_PARAMETER;
8400 DEBUG(4,("_spoolss_EnumPorts\n"));
8402 *r->out.count = 0;
8403 *r->out.needed = 0;
8404 *r->out.info = NULL;
8406 switch (r->in.level) {
8407 case 1:
8408 result = enumports_level_1(p->mem_ctx, r->out.info,
8409 r->out.count);
8410 break;
8411 case 2:
8412 result = enumports_level_2(p->mem_ctx, r->out.info,
8413 r->out.count);
8414 break;
8415 default:
8416 return WERR_INVALID_LEVEL;
8419 if (!W_ERROR_IS_OK(result)) {
8420 return result;
8423 *r->out.needed = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
8424 spoolss_EnumPorts,
8425 *r->out.info, r->in.level,
8426 *r->out.count);
8427 *r->out.info = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
8428 *r->out.count = SPOOLSS_BUFFER_OK(*r->out.count, 0);
8430 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
8433 /****************************************************************************
8434 ****************************************************************************/
8436 static WERROR spoolss_addprinterex_level_2(struct pipes_struct *p,
8437 const char *server,
8438 struct spoolss_SetPrinterInfoCtr *info_ctr,
8439 struct spoolss_DeviceMode *devmode,
8440 struct security_descriptor *secdesc,
8441 struct spoolss_UserLevelCtr *user_ctr,
8442 struct policy_handle *handle)
8444 struct spoolss_SetPrinterInfo2 *info2 = info_ctr->info.info2;
8445 uint32_t info2_mask = SPOOLSS_PRINTER_INFO_ALL;
8446 const struct loadparm_substitution *lp_sub =
8447 loadparm_s3_global_substitution();
8448 int snum;
8449 WERROR err = WERR_OK;
8451 /* samba does not have a concept of local, non-shared printers yet, so
8452 * make sure we always setup sharename - gd */
8453 if ((info2->sharename == NULL || info2->sharename[0] == '\0') &&
8454 (info2->printername != NULL && info2->printername[0] != '\0')) {
8455 DEBUG(5, ("spoolss_addprinterex_level_2: "
8456 "no sharename has been set, setting printername %s as sharename\n",
8457 info2->printername));
8458 info2->sharename = info2->printername;
8461 /* check to see if the printer already exists */
8462 if ((snum = print_queue_snum(info2->sharename)) != -1) {
8463 DEBUG(5, ("spoolss_addprinterex_level_2: Attempted to add a printer named [%s] when one already existed!\n",
8464 info2->sharename));
8465 return WERR_PRINTER_ALREADY_EXISTS;
8468 if (!lp_force_printername(GLOBAL_SECTION_SNUM)) {
8469 if ((snum = print_queue_snum(info2->printername)) != -1) {
8470 DEBUG(5, ("spoolss_addprinterex_level_2: Attempted to add a printer named [%s] when one already existed!\n",
8471 info2->printername));
8472 return WERR_PRINTER_ALREADY_EXISTS;
8476 /* validate printer info struct */
8477 if (!info2->printername || strlen(info2->printername) == 0) {
8478 return WERR_INVALID_PRINTER_NAME;
8480 if (!info2->portname || strlen(info2->portname) == 0) {
8481 return WERR_UNKNOWN_PORT;
8483 if (!info2->drivername || strlen(info2->drivername) == 0) {
8484 return WERR_UNKNOWN_PRINTER_DRIVER;
8486 if (!info2->printprocessor || strlen(info2->printprocessor) == 0) {
8487 return WERR_UNKNOWN_PRINTPROCESSOR;
8490 /* FIXME!!! smbd should check to see if the driver is installed before
8491 trying to add a printer like this --jerry */
8493 if (*lp_addprinter_command(talloc_tos(), lp_sub) ) {
8494 char *raddr;
8496 raddr = tsocket_address_inet_addr_string(p->remote_address,
8497 p->mem_ctx);
8498 if (raddr == NULL) {
8499 return WERR_NOT_ENOUGH_MEMORY;
8502 if ( !add_printer_hook(p->mem_ctx, p->session_info->security_token,
8503 info2, raddr,
8504 p->msg_ctx) ) {
8505 return WERR_ACCESS_DENIED;
8507 } else {
8508 DEBUG(0,("spoolss_addprinterex_level_2: add printer for printer %s called and no "
8509 "smb.conf parameter \"addprinter command\" is defined. This "
8510 "parameter must exist for this call to succeed\n",
8511 info2->sharename ));
8514 if ((snum = print_queue_snum(info2->sharename)) == -1) {
8515 return WERR_ACCESS_DENIED;
8518 /* you must be a printer admin to add a new printer */
8519 if (!W_ERROR_IS_OK(print_access_check(p->session_info,
8520 p->msg_ctx,
8521 snum,
8522 PRINTER_ACCESS_ADMINISTER))) {
8523 return WERR_ACCESS_DENIED;
8527 * Do sanity check on the requested changes for Samba.
8530 if (!check_printer_ok(p->mem_ctx, info2, snum)) {
8531 return WERR_INVALID_PARAMETER;
8534 if (devmode == NULL) {
8535 info2_mask = ~SPOOLSS_PRINTER_INFO_DEVMODE;
8538 err = update_dsspooler(p->mem_ctx,
8539 get_session_info_system(),
8540 p->msg_ctx,
8542 info2,
8543 NULL);
8544 if (!W_ERROR_IS_OK(err)) {
8545 return err;
8548 err = winreg_update_printer_internal(p->mem_ctx,
8549 get_session_info_system(),
8550 p->msg_ctx,
8551 info2->sharename,
8552 info2_mask,
8553 info2,
8554 devmode,
8555 secdesc);
8556 if (!W_ERROR_IS_OK(err)) {
8557 return err;
8560 err = open_printer_hnd(p, handle, info2->printername, PRINTER_ACCESS_ADMINISTER);
8561 if (!W_ERROR_IS_OK(err)) {
8562 /* Handle open failed - remove addition. */
8563 ZERO_STRUCTP(handle);
8564 return err;
8567 return WERR_OK;
8570 /****************************************************************
8571 _spoolss_AddPrinterEx
8572 ****************************************************************/
8574 WERROR _spoolss_AddPrinterEx(struct pipes_struct *p,
8575 struct spoolss_AddPrinterEx *r)
8577 switch (r->in.info_ctr->level) {
8578 case 1:
8579 /* we don't handle yet */
8580 /* but I know what to do ... */
8581 return WERR_INVALID_LEVEL;
8582 case 2:
8583 return spoolss_addprinterex_level_2(p, r->in.server,
8584 r->in.info_ctr,
8585 r->in.devmode_ctr->devmode,
8586 r->in.secdesc_ctr->sd,
8587 r->in.userlevel_ctr,
8588 r->out.handle);
8589 default:
8590 return WERR_INVALID_LEVEL;
8594 /****************************************************************
8595 _spoolss_AddPrinter
8596 ****************************************************************/
8598 WERROR _spoolss_AddPrinter(struct pipes_struct *p,
8599 struct spoolss_AddPrinter *r)
8601 struct spoolss_AddPrinterEx a;
8602 struct spoolss_UserLevelCtr userlevel_ctr;
8604 ZERO_STRUCT(userlevel_ctr);
8606 userlevel_ctr.level = 1;
8608 a.in.server = r->in.server;
8609 a.in.info_ctr = r->in.info_ctr;
8610 a.in.devmode_ctr = r->in.devmode_ctr;
8611 a.in.secdesc_ctr = r->in.secdesc_ctr;
8612 a.in.userlevel_ctr = &userlevel_ctr;
8613 a.out.handle = r->out.handle;
8615 return _spoolss_AddPrinterEx(p, &a);
8618 /****************************************************************
8619 _spoolss_AddPrinterDriverEx
8620 ****************************************************************/
8622 WERROR _spoolss_AddPrinterDriverEx(struct pipes_struct *p,
8623 struct spoolss_AddPrinterDriverEx *r)
8625 WERROR err = WERR_OK;
8626 const char *driver_name = NULL;
8627 const char *driver_directory = NULL;
8628 uint32_t version;
8631 * we only support the semantics of AddPrinterDriver()
8632 * i.e. only copy files that are newer than existing ones
8635 if (r->in.flags == 0) {
8636 return WERR_INVALID_PARAMETER;
8639 if (!(r->in.flags & APD_COPY_ALL_FILES) &&
8640 !(r->in.flags & APD_COPY_NEW_FILES)) {
8641 return WERR_ACCESS_DENIED;
8644 /* FIXME */
8645 if (r->in.info_ctr->level != 3 &&
8646 r->in.info_ctr->level != 6 &&
8647 r->in.info_ctr->level != 8) {
8648 DEBUG(0,("%s: level %d not yet implemented\n", __func__,
8649 r->in.info_ctr->level));
8650 return WERR_INVALID_LEVEL;
8653 DEBUG(5,("Cleaning driver's information\n"));
8654 err = clean_up_driver_struct(p->mem_ctx,
8655 p->session_info,
8656 r->in.info_ctr,
8657 r->in.flags,
8658 &driver_directory);
8659 if (!W_ERROR_IS_OK(err)) {
8660 DBG_ERR("clean_up_driver_struct failed - %s\n",
8661 win_errstr(err));
8662 goto done;
8665 DEBUG(5,("Moving driver to final destination\n"));
8666 err = move_driver_to_download_area(p->session_info,
8667 r->in.info_ctr,
8668 driver_directory);
8669 if (!W_ERROR_IS_OK(err)) {
8670 DBG_ERR("move_driver_to_download_area failed - %s\n",
8671 win_errstr(err));
8672 goto done;
8675 err = winreg_add_driver_internal(p->mem_ctx,
8676 get_session_info_system(),
8677 p->msg_ctx,
8678 r->in.info_ctr,
8679 &driver_name,
8680 &version);
8681 if (!W_ERROR_IS_OK(err)) {
8682 DBG_ERR("winreg_add_driver_internal failed - %s\n",
8683 win_errstr(err));
8684 goto done;
8688 * I think this is where he DrvUpgradePrinter() hook would be
8689 * be called in a driver's interface DLL on a Windows NT 4.0/2k
8690 * server. Right now, we just need to send ourselves a message
8691 * to update each printer bound to this driver. --jerry
8694 if (!srv_spoolss_drv_upgrade_printer(driver_name, p->msg_ctx)) {
8695 DEBUG(0,("%s: Failed to send message about upgrading driver [%s]!\n",
8696 __func__, driver_name));
8699 done:
8700 return err;
8703 /****************************************************************
8704 _spoolss_AddPrinterDriver
8705 ****************************************************************/
8707 WERROR _spoolss_AddPrinterDriver(struct pipes_struct *p,
8708 struct spoolss_AddPrinterDriver *r)
8710 struct spoolss_AddPrinterDriverEx a;
8712 switch (r->in.info_ctr->level) {
8713 case 2:
8714 case 3:
8715 case 4:
8716 case 5:
8717 break;
8718 default:
8719 return WERR_INVALID_LEVEL;
8722 a.in.servername = r->in.servername;
8723 a.in.info_ctr = r->in.info_ctr;
8724 a.in.flags = APD_COPY_NEW_FILES;
8726 return _spoolss_AddPrinterDriverEx(p, &a);
8729 /****************************************************************************
8730 ****************************************************************************/
8732 struct _spoolss_paths {
8733 int type;
8734 const char *share;
8735 const char *dir;
8738 enum { SPOOLSS_DRIVER_PATH, SPOOLSS_PRTPROCS_PATH };
8740 static const struct _spoolss_paths spoolss_paths[]= {
8741 { SPOOLSS_DRIVER_PATH, "print$", "DRIVERS" },
8742 { SPOOLSS_PRTPROCS_PATH, "prnproc$", "PRTPROCS" }
8745 static WERROR compose_spoolss_server_path(TALLOC_CTX *mem_ctx,
8746 const char *servername,
8747 const char *environment,
8748 int component,
8749 char **path)
8751 const char *pservername = NULL;
8752 const char *long_archi;
8753 const char *short_archi;
8755 *path = NULL;
8757 /* environment may be empty */
8758 if (environment && strlen(environment)) {
8759 long_archi = environment;
8760 } else {
8761 long_archi = lp_parm_const_string(GLOBAL_SECTION_SNUM,
8762 "spoolss", "architecture",
8763 GLOBAL_SPOOLSS_ARCHITECTURE);
8766 /* servername may be empty */
8767 if (servername && strlen(servername)) {
8768 pservername = canon_servername(servername);
8770 if (!is_myname_or_ipaddr(pservername)) {
8771 return WERR_INVALID_PARAMETER;
8775 if (!(short_archi = get_short_archi(long_archi))) {
8776 return WERR_INVALID_ENVIRONMENT;
8779 switch (component) {
8780 case SPOOLSS_PRTPROCS_PATH:
8781 case SPOOLSS_DRIVER_PATH:
8782 if (pservername) {
8783 *path = talloc_asprintf(mem_ctx,
8784 "\\\\%s\\%s\\%s",
8785 pservername,
8786 spoolss_paths[component].share,
8787 short_archi);
8788 } else {
8789 *path = talloc_asprintf(mem_ctx, "%s\\%s\\%s",
8790 SPOOLSS_DEFAULT_SERVER_PATH,
8791 spoolss_paths[component].dir,
8792 short_archi);
8794 break;
8795 default:
8796 return WERR_INVALID_PARAMETER;
8799 if (!*path) {
8800 return WERR_NOT_ENOUGH_MEMORY;
8803 return WERR_OK;
8806 /****************************************************************************
8807 ****************************************************************************/
8809 static WERROR getprinterdriverdir_level_1(TALLOC_CTX *mem_ctx,
8810 const char *servername,
8811 const char *environment,
8812 struct spoolss_DriverDirectoryInfo1 *r)
8814 WERROR werr;
8815 char *path = NULL;
8817 werr = compose_spoolss_server_path(mem_ctx,
8818 servername,
8819 environment,
8820 SPOOLSS_DRIVER_PATH,
8821 &path);
8822 if (!W_ERROR_IS_OK(werr)) {
8823 return werr;
8826 DEBUG(4,("printer driver directory: [%s]\n", path));
8828 r->directory_name = path;
8830 return WERR_OK;
8833 /****************************************************************
8834 _spoolss_GetPrinterDriverDirectory
8835 ****************************************************************/
8837 WERROR _spoolss_GetPrinterDriverDirectory(struct pipes_struct *p,
8838 struct spoolss_GetPrinterDriverDirectory *r)
8840 WERROR werror;
8842 /* that's an [in out] buffer */
8844 if (!r->in.buffer && (r->in.offered != 0)) {
8845 TALLOC_FREE(r->out.info);
8846 return WERR_INVALID_PARAMETER;
8849 DEBUG(5,("_spoolss_GetPrinterDriverDirectory: level %d\n",
8850 r->in.level));
8852 *r->out.needed = 0;
8854 /* r->in.level is ignored */
8856 werror = getprinterdriverdir_level_1(p->mem_ctx,
8857 r->in.server,
8858 r->in.environment,
8859 &r->out.info->info1);
8860 if (!W_ERROR_IS_OK(werror)) {
8861 TALLOC_FREE(r->out.info);
8862 return werror;
8865 *r->out.needed = SPOOLSS_BUFFER_UNION(spoolss_DriverDirectoryInfo,
8866 r->out.info, r->in.level);
8867 r->out.info = SPOOLSS_BUFFER_OK(r->out.info, NULL);
8869 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
8872 /****************************************************************
8873 _spoolss_EnumPrinterData
8874 ****************************************************************/
8876 WERROR _spoolss_EnumPrinterData(struct pipes_struct *p,
8877 struct spoolss_EnumPrinterData *r)
8879 WERROR result;
8880 struct spoolss_EnumPrinterDataEx r2;
8881 uint32_t count;
8882 struct spoolss_PrinterEnumValues *info, *val = NULL;
8883 uint32_t needed;
8885 r2.in.handle = r->in.handle;
8886 r2.in.key_name = "PrinterDriverData";
8887 r2.in.offered = 0;
8888 r2.out.count = &count;
8889 r2.out.info = &info;
8890 r2.out.needed = &needed;
8892 result = _spoolss_EnumPrinterDataEx(p, &r2);
8893 if (W_ERROR_EQUAL(result, WERR_MORE_DATA)) {
8894 r2.in.offered = needed;
8895 result = _spoolss_EnumPrinterDataEx(p, &r2);
8897 if (!W_ERROR_IS_OK(result)) {
8898 return result;
8902 * The NT machine wants to know the biggest size of value and data
8904 * cf: MSDN EnumPrinterData remark section
8907 if (!r->in.value_offered && !r->in.data_offered) {
8908 uint32_t biggest_valuesize = 0;
8909 uint32_t biggest_datasize = 0;
8910 int i, name_length;
8912 DEBUGADD(6,("Activating NT mega-hack to find sizes\n"));
8914 for (i=0; i<count; i++) {
8916 name_length = strlen(info[i].value_name);
8917 if (strlen(info[i].value_name) > biggest_valuesize) {
8918 biggest_valuesize = name_length;
8921 if (info[i].data_length > biggest_datasize) {
8922 biggest_datasize = info[i].data_length;
8925 DEBUG(6,("current values: [%d], [%d]\n", biggest_valuesize,
8926 biggest_datasize));
8929 /* the value is an UNICODE string but real_value_size is the length
8930 in bytes including the trailing 0 */
8932 *r->out.value_needed = 2 * (1 + biggest_valuesize);
8933 *r->out.data_needed = biggest_datasize;
8935 DEBUG(6,("final values: [%d], [%d]\n",
8936 *r->out.value_needed, *r->out.data_needed));
8938 return WERR_OK;
8941 if (r->in.enum_index < count) {
8942 val = &info[r->in.enum_index];
8945 if (val == NULL) {
8946 /* out_value should default to "" or else NT4 has
8947 problems unmarshalling the response */
8949 if (r->in.value_offered) {
8950 *r->out.value_needed = 1;
8951 r->out.value_name = talloc_strdup(r, "");
8952 if (!r->out.value_name) {
8953 return WERR_NOT_ENOUGH_MEMORY;
8955 } else {
8956 r->out.value_name = NULL;
8957 *r->out.value_needed = 0;
8960 /* the data is counted in bytes */
8962 *r->out.data_needed = r->in.data_offered;
8964 result = WERR_NO_MORE_ITEMS;
8965 } else {
8967 * the value is:
8968 * - counted in bytes in the request
8969 * - counted in UNICODE chars in the max reply
8970 * - counted in bytes in the real size
8972 * take a pause *before* coding not *during* coding
8975 /* name */
8976 if (r->in.value_offered) {
8977 r->out.value_name = talloc_strdup(r, val->value_name);
8978 if (!r->out.value_name) {
8979 return WERR_NOT_ENOUGH_MEMORY;
8981 *r->out.value_needed = val->value_name_len;
8982 } else {
8983 r->out.value_name = NULL;
8984 *r->out.value_needed = 0;
8987 /* type */
8989 *r->out.type = val->type;
8991 /* data - counted in bytes */
8994 * See the section "Dynamically Typed Query Parameters"
8995 * in MS-RPRN.
8998 if (r->out.data && val->data && val->data->data &&
8999 val->data_length && r->in.data_offered) {
9000 memcpy(r->out.data, val->data->data,
9001 MIN(val->data_length,r->in.data_offered));
9004 *r->out.data_needed = val->data_length;
9006 result = WERR_OK;
9009 return result;
9012 /****************************************************************
9013 _spoolss_SetPrinterData
9014 ****************************************************************/
9016 WERROR _spoolss_SetPrinterData(struct pipes_struct *p,
9017 struct spoolss_SetPrinterData *r)
9019 struct spoolss_SetPrinterDataEx r2;
9021 r2.in.handle = r->in.handle;
9022 r2.in.key_name = "PrinterDriverData";
9023 r2.in.value_name = r->in.value_name;
9024 r2.in.type = r->in.type;
9025 r2.in.data = r->in.data;
9026 r2.in.offered = r->in.offered;
9028 return _spoolss_SetPrinterDataEx(p, &r2);
9031 /****************************************************************
9032 _spoolss_ResetPrinter
9033 ****************************************************************/
9035 WERROR _spoolss_ResetPrinter(struct pipes_struct *p,
9036 struct spoolss_ResetPrinter *r)
9038 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
9039 int snum;
9041 DEBUG(5,("_spoolss_ResetPrinter\n"));
9044 * All we do is to check to see if the handle and queue is valid.
9045 * This call really doesn't mean anything to us because we only
9046 * support RAW printing. --jerry
9049 if (!Printer) {
9050 DEBUG(2,("_spoolss_ResetPrinter: Invalid handle (%s:%u:%u).\n",
9051 OUR_HANDLE(r->in.handle)));
9052 return WERR_INVALID_HANDLE;
9055 if (!get_printer_snum(p, r->in.handle, &snum, NULL))
9056 return WERR_INVALID_HANDLE;
9059 /* blindly return success */
9060 return WERR_OK;
9063 /****************************************************************
9064 _spoolss_DeletePrinterData
9065 ****************************************************************/
9067 WERROR _spoolss_DeletePrinterData(struct pipes_struct *p,
9068 struct spoolss_DeletePrinterData *r)
9070 struct spoolss_DeletePrinterDataEx r2;
9072 r2.in.handle = r->in.handle;
9073 r2.in.key_name = "PrinterDriverData";
9074 r2.in.value_name = r->in.value_name;
9076 return _spoolss_DeletePrinterDataEx(p, &r2);
9079 /****************************************************************
9080 _spoolss_AddForm
9081 ****************************************************************/
9083 WERROR _spoolss_AddForm(struct pipes_struct *p,
9084 struct spoolss_AddForm *r)
9086 struct spoolss_AddFormInfo1 *form;
9087 int snum = -1;
9088 WERROR status = WERR_OK;
9089 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
9090 struct dcerpc_binding_handle *b;
9091 TALLOC_CTX *tmp_ctx = NULL;
9093 DEBUG(5,("_spoolss_AddForm\n"));
9095 if (!Printer) {
9096 DEBUG(2,("_spoolss_AddForm: Invalid handle (%s:%u:%u).\n",
9097 OUR_HANDLE(r->in.handle)));
9098 return WERR_INVALID_HANDLE;
9101 /* if the user is not root, doesn't have SE_PRINT_OPERATOR privilege,
9102 and not a printer admin, then fail */
9104 if ((p->session_info->unix_token->uid != sec_initial_uid()) &&
9105 !security_token_has_privilege(p->session_info->security_token,
9106 SEC_PRIV_PRINT_OPERATOR)) {
9107 DEBUG(2,("_spoolss_Addform: denied by insufficient permissions.\n"));
9108 return WERR_ACCESS_DENIED;
9111 if (r->in.info_ctr->level != 1) {
9112 return WERR_INVALID_LEVEL;
9115 form = r->in.info_ctr->info.info1;
9116 if (!form) {
9117 return WERR_INVALID_PARAMETER;
9120 switch (form->flags) {
9121 case SPOOLSS_FORM_USER:
9122 case SPOOLSS_FORM_BUILTIN:
9123 case SPOOLSS_FORM_PRINTER:
9124 break;
9125 default:
9126 return WERR_INVALID_PARAMETER;
9129 tmp_ctx = talloc_new(p->mem_ctx);
9130 if (!tmp_ctx) {
9131 return WERR_NOT_ENOUGH_MEMORY;
9134 status = winreg_printer_binding_handle(tmp_ctx,
9135 get_session_info_system(),
9136 p->msg_ctx,
9137 &b);
9138 if (!W_ERROR_IS_OK(status)) {
9139 goto done;
9142 status = winreg_printer_addform1(tmp_ctx, b, form);
9143 if (!W_ERROR_IS_OK(status)) {
9144 goto done;
9148 * ChangeID must always be set if this is a printer
9150 if (Printer->printer_type == SPLHND_PRINTER) {
9151 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
9152 status = WERR_INVALID_HANDLE;
9153 goto done;
9156 status = winreg_printer_update_changeid(tmp_ctx, b,
9157 lp_const_servicename(snum));
9160 done:
9161 talloc_free(tmp_ctx);
9162 return status;
9165 /****************************************************************
9166 _spoolss_DeleteForm
9167 ****************************************************************/
9169 WERROR _spoolss_DeleteForm(struct pipes_struct *p,
9170 struct spoolss_DeleteForm *r)
9172 const char *form_name = r->in.form_name;
9173 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
9174 int snum = -1;
9175 WERROR status = WERR_OK;
9176 struct dcerpc_binding_handle *b;
9177 TALLOC_CTX *tmp_ctx = NULL;
9179 DEBUG(5,("_spoolss_DeleteForm\n"));
9181 if (!Printer) {
9182 DEBUG(2,("_spoolss_DeleteForm: Invalid handle (%s:%u:%u).\n",
9183 OUR_HANDLE(r->in.handle)));
9184 return WERR_INVALID_HANDLE;
9187 if ((p->session_info->unix_token->uid != sec_initial_uid()) &&
9188 !security_token_has_privilege(p->session_info->security_token,
9189 SEC_PRIV_PRINT_OPERATOR)) {
9190 DEBUG(2,("_spoolss_DeleteForm: denied by insufficient permissions.\n"));
9191 return WERR_ACCESS_DENIED;
9194 tmp_ctx = talloc_new(p->mem_ctx);
9195 if (!tmp_ctx) {
9196 return WERR_NOT_ENOUGH_MEMORY;
9199 status = winreg_printer_binding_handle(tmp_ctx,
9200 get_session_info_system(),
9201 p->msg_ctx,
9202 &b);
9203 if (!W_ERROR_IS_OK(status)) {
9204 goto done;
9207 status = winreg_printer_deleteform1(tmp_ctx, b, form_name);
9208 if (!W_ERROR_IS_OK(status)) {
9209 goto done;
9213 * ChangeID must always be set if this is a printer
9215 if (Printer->printer_type == SPLHND_PRINTER) {
9216 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
9217 status = WERR_INVALID_HANDLE;
9218 goto done;
9221 status = winreg_printer_update_changeid(tmp_ctx, b,
9222 lp_const_servicename(snum));
9225 done:
9226 talloc_free(tmp_ctx);
9227 return status;
9230 /****************************************************************
9231 _spoolss_SetForm
9232 ****************************************************************/
9234 WERROR _spoolss_SetForm(struct pipes_struct *p,
9235 struct spoolss_SetForm *r)
9237 struct spoolss_AddFormInfo1 *form;
9238 const char *form_name = r->in.form_name;
9239 int snum = -1;
9240 WERROR status = WERR_OK;
9241 struct dcerpc_binding_handle *b;
9242 TALLOC_CTX *tmp_ctx = NULL;
9244 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
9246 DEBUG(5,("_spoolss_SetForm\n"));
9248 if (!Printer) {
9249 DEBUG(2,("_spoolss_SetForm: Invalid handle (%s:%u:%u).\n",
9250 OUR_HANDLE(r->in.handle)));
9251 return WERR_INVALID_HANDLE;
9254 /* if the user is not root, doesn't have SE_PRINT_OPERATOR privilege,
9255 and not a printer admin, then fail */
9257 if ((p->session_info->unix_token->uid != sec_initial_uid()) &&
9258 !security_token_has_privilege(p->session_info->security_token,
9259 SEC_PRIV_PRINT_OPERATOR)) {
9260 DEBUG(2,("_spoolss_Setform: denied by insufficient permissions.\n"));
9261 return WERR_ACCESS_DENIED;
9264 if (r->in.info_ctr->level != 1) {
9265 return WERR_INVALID_LEVEL;
9268 form = r->in.info_ctr->info.info1;
9269 if (!form) {
9270 return WERR_INVALID_PARAMETER;
9273 tmp_ctx = talloc_new(p->mem_ctx);
9274 if (!tmp_ctx) {
9275 return WERR_NOT_ENOUGH_MEMORY;
9278 status = winreg_printer_binding_handle(tmp_ctx,
9279 get_session_info_system(),
9280 p->msg_ctx,
9281 &b);
9282 if (!W_ERROR_IS_OK(status)) {
9283 goto done;
9286 status = winreg_printer_setform1(tmp_ctx, b,
9287 form_name,
9288 form);
9289 if (!W_ERROR_IS_OK(status)) {
9290 goto done;
9294 * ChangeID must always be set if this is a printer
9296 if (Printer->printer_type == SPLHND_PRINTER) {
9297 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
9298 status = WERR_INVALID_HANDLE;
9299 goto done;
9302 status = winreg_printer_update_changeid(tmp_ctx, b,
9303 lp_const_servicename(snum));
9306 done:
9307 talloc_free(tmp_ctx);
9308 return status;
9311 /****************************************************************************
9312 fill_print_processor1
9313 ****************************************************************************/
9315 static WERROR fill_print_processor1(TALLOC_CTX *mem_ctx,
9316 struct spoolss_PrintProcessorInfo1 *r,
9317 const char *print_processor_name)
9319 r->print_processor_name = talloc_strdup(mem_ctx, print_processor_name);
9320 W_ERROR_HAVE_NO_MEMORY(r->print_processor_name);
9322 return WERR_OK;
9325 /****************************************************************************
9326 enumprintprocessors level 1.
9327 ****************************************************************************/
9329 static WERROR enumprintprocessors_level_1(TALLOC_CTX *mem_ctx,
9330 union spoolss_PrintProcessorInfo **info_p,
9331 uint32_t *count)
9333 union spoolss_PrintProcessorInfo *info;
9334 WERROR result;
9336 info = talloc_array(mem_ctx, union spoolss_PrintProcessorInfo, 1);
9337 W_ERROR_HAVE_NO_MEMORY(info);
9339 *count = 1;
9341 result = fill_print_processor1(info, &info[0].info1, "winprint");
9342 if (!W_ERROR_IS_OK(result)) {
9343 goto out;
9346 out:
9347 if (!W_ERROR_IS_OK(result)) {
9348 TALLOC_FREE(info);
9349 *count = 0;
9350 return result;
9353 *info_p = info;
9355 return WERR_OK;
9358 /****************************************************************
9359 _spoolss_EnumPrintProcessors
9360 ****************************************************************/
9362 WERROR _spoolss_EnumPrintProcessors(struct pipes_struct *p,
9363 struct spoolss_EnumPrintProcessors *r)
9365 WERROR result;
9367 /* that's an [in out] buffer */
9369 if (!r->in.buffer && (r->in.offered != 0)) {
9370 return WERR_INVALID_PARAMETER;
9373 DEBUG(5,("_spoolss_EnumPrintProcessors\n"));
9376 * Enumerate the print processors ...
9378 * Just reply with "winprint", to keep NT happy
9379 * and I can use my nice printer checker.
9382 *r->out.count = 0;
9383 *r->out.needed = 0;
9384 *r->out.info = NULL;
9386 if (!get_short_archi(r->in.environment)) {
9387 return WERR_INVALID_ENVIRONMENT;
9390 switch (r->in.level) {
9391 case 1:
9392 result = enumprintprocessors_level_1(p->mem_ctx, r->out.info,
9393 r->out.count);
9394 break;
9395 default:
9396 return WERR_INVALID_LEVEL;
9399 if (!W_ERROR_IS_OK(result)) {
9400 return result;
9403 *r->out.needed = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
9404 spoolss_EnumPrintProcessors,
9405 *r->out.info, r->in.level,
9406 *r->out.count);
9407 *r->out.info = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
9408 *r->out.count = SPOOLSS_BUFFER_OK(*r->out.count, 0);
9410 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
9413 /****************************************************************************
9414 fill_printprocdatatype1
9415 ****************************************************************************/
9417 static WERROR fill_printprocdatatype1(TALLOC_CTX *mem_ctx,
9418 struct spoolss_PrintProcDataTypesInfo1 *r,
9419 const char *name_array)
9421 r->name_array = talloc_strdup(mem_ctx, name_array);
9422 W_ERROR_HAVE_NO_MEMORY(r->name_array);
9424 return WERR_OK;
9427 /****************************************************************************
9428 enumprintprocdatatypes level 1.
9429 ****************************************************************************/
9431 static WERROR enumprintprocdatatypes_level_1(TALLOC_CTX *mem_ctx,
9432 union spoolss_PrintProcDataTypesInfo **info_p,
9433 uint32_t *count)
9435 WERROR result;
9436 union spoolss_PrintProcDataTypesInfo *info;
9438 info = talloc_array(mem_ctx, union spoolss_PrintProcDataTypesInfo, 1);
9439 W_ERROR_HAVE_NO_MEMORY(info);
9441 *count = 1;
9443 result = fill_printprocdatatype1(info, &info[0].info1, "RAW");
9444 if (!W_ERROR_IS_OK(result)) {
9445 goto out;
9448 out:
9449 if (!W_ERROR_IS_OK(result)) {
9450 TALLOC_FREE(info);
9451 *count = 0;
9452 return result;
9455 *info_p = info;
9457 return WERR_OK;
9460 /****************************************************************
9461 _spoolss_EnumPrintProcessorDataTypes
9462 ****************************************************************/
9464 WERROR _spoolss_EnumPrintProcessorDataTypes(struct pipes_struct *p,
9465 struct spoolss_EnumPrintProcessorDataTypes *r)
9467 WERROR result;
9469 /* that's an [in out] buffer */
9471 if (!r->in.buffer && (r->in.offered != 0)) {
9472 return WERR_INVALID_PARAMETER;
9475 DEBUG(5,("_spoolss_EnumPrintProcessorDataTypes\n"));
9477 *r->out.count = 0;
9478 *r->out.needed = 0;
9479 *r->out.info = NULL;
9481 if (r->in.print_processor_name == NULL ||
9482 !strequal(r->in.print_processor_name, "winprint")) {
9483 return WERR_UNKNOWN_PRINTPROCESSOR;
9486 switch (r->in.level) {
9487 case 1:
9488 result = enumprintprocdatatypes_level_1(p->mem_ctx, r->out.info,
9489 r->out.count);
9490 break;
9491 default:
9492 return WERR_INVALID_LEVEL;
9495 if (!W_ERROR_IS_OK(result)) {
9496 return result;
9499 *r->out.needed = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
9500 spoolss_EnumPrintProcessorDataTypes,
9501 *r->out.info, r->in.level,
9502 *r->out.count);
9503 *r->out.info = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
9504 *r->out.count = SPOOLSS_BUFFER_OK(*r->out.count, 0);
9506 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
9509 /****************************************************************************
9510 fill_monitor_1
9511 ****************************************************************************/
9513 static WERROR fill_monitor_1(TALLOC_CTX *mem_ctx,
9514 struct spoolss_MonitorInfo1 *r,
9515 const char *monitor_name)
9517 r->monitor_name = talloc_strdup(mem_ctx, monitor_name);
9518 W_ERROR_HAVE_NO_MEMORY(r->monitor_name);
9520 return WERR_OK;
9523 /****************************************************************************
9524 fill_monitor_2
9525 ****************************************************************************/
9527 static WERROR fill_monitor_2(TALLOC_CTX *mem_ctx,
9528 struct spoolss_MonitorInfo2 *r,
9529 const char *monitor_name,
9530 const char *environment,
9531 const char *dll_name)
9533 r->monitor_name = talloc_strdup(mem_ctx, monitor_name);
9534 W_ERROR_HAVE_NO_MEMORY(r->monitor_name);
9535 r->environment = talloc_strdup(mem_ctx, environment);
9536 W_ERROR_HAVE_NO_MEMORY(r->environment);
9537 r->dll_name = talloc_strdup(mem_ctx, dll_name);
9538 W_ERROR_HAVE_NO_MEMORY(r->dll_name);
9540 return WERR_OK;
9543 /****************************************************************************
9544 enumprintmonitors level 1.
9545 ****************************************************************************/
9547 static WERROR enumprintmonitors_level_1(TALLOC_CTX *mem_ctx,
9548 union spoolss_MonitorInfo **info_p,
9549 uint32_t *count)
9551 union spoolss_MonitorInfo *info;
9552 WERROR result = WERR_OK;
9554 info = talloc_array(mem_ctx, union spoolss_MonitorInfo, 2);
9555 W_ERROR_HAVE_NO_MEMORY(info);
9557 *count = 2;
9559 result = fill_monitor_1(info, &info[0].info1,
9560 SPL_LOCAL_PORT);
9561 if (!W_ERROR_IS_OK(result)) {
9562 goto out;
9565 result = fill_monitor_1(info, &info[1].info1,
9566 SPL_TCPIP_PORT);
9567 if (!W_ERROR_IS_OK(result)) {
9568 goto out;
9571 out:
9572 if (!W_ERROR_IS_OK(result)) {
9573 TALLOC_FREE(info);
9574 *count = 0;
9575 return result;
9578 *info_p = info;
9580 return WERR_OK;
9583 /****************************************************************************
9584 enumprintmonitors level 2.
9585 ****************************************************************************/
9587 static WERROR enumprintmonitors_level_2(TALLOC_CTX *mem_ctx,
9588 union spoolss_MonitorInfo **info_p,
9589 uint32_t *count)
9591 union spoolss_MonitorInfo *info;
9592 WERROR result = WERR_OK;
9593 const char *architecture;
9595 info = talloc_array(mem_ctx, union spoolss_MonitorInfo, 2);
9596 W_ERROR_HAVE_NO_MEMORY(info);
9598 *count = 2;
9600 architecture = lp_parm_const_string(GLOBAL_SECTION_SNUM,
9601 "spoolss",
9602 "architecture",
9603 GLOBAL_SPOOLSS_ARCHITECTURE);
9605 result = fill_monitor_2(info, &info[0].info2,
9606 SPL_LOCAL_PORT,
9607 architecture,
9608 "localmon.dll");
9609 if (!W_ERROR_IS_OK(result)) {
9610 goto out;
9613 result = fill_monitor_2(info, &info[1].info2,
9614 SPL_TCPIP_PORT,
9615 architecture,
9616 "tcpmon.dll");
9617 if (!W_ERROR_IS_OK(result)) {
9618 goto out;
9621 out:
9622 if (!W_ERROR_IS_OK(result)) {
9623 TALLOC_FREE(info);
9624 *count = 0;
9625 return result;
9628 *info_p = info;
9630 return WERR_OK;
9633 /****************************************************************
9634 _spoolss_EnumMonitors
9635 ****************************************************************/
9637 WERROR _spoolss_EnumMonitors(struct pipes_struct *p,
9638 struct spoolss_EnumMonitors *r)
9640 WERROR result;
9642 /* that's an [in out] buffer */
9644 if (!r->in.buffer && (r->in.offered != 0)) {
9645 return WERR_INVALID_PARAMETER;
9648 DEBUG(5,("_spoolss_EnumMonitors\n"));
9651 * Enumerate the print monitors ...
9653 * Just reply with "Local Port", to keep NT happy
9654 * and I can use my nice printer checker.
9657 *r->out.count = 0;
9658 *r->out.needed = 0;
9659 *r->out.info = NULL;
9661 switch (r->in.level) {
9662 case 1:
9663 result = enumprintmonitors_level_1(p->mem_ctx, r->out.info,
9664 r->out.count);
9665 break;
9666 case 2:
9667 result = enumprintmonitors_level_2(p->mem_ctx, r->out.info,
9668 r->out.count);
9669 break;
9670 default:
9671 return WERR_INVALID_LEVEL;
9674 if (!W_ERROR_IS_OK(result)) {
9675 return result;
9678 *r->out.needed = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
9679 spoolss_EnumMonitors,
9680 *r->out.info, r->in.level,
9681 *r->out.count);
9682 *r->out.info = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
9683 *r->out.count = SPOOLSS_BUFFER_OK(*r->out.count, 0);
9685 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
9688 /****************************************************************************
9689 ****************************************************************************/
9691 static WERROR getjob_level_1(TALLOC_CTX *mem_ctx,
9692 const print_queue_struct *queue,
9693 int count, int snum,
9694 struct spoolss_PrinterInfo2 *pinfo2,
9695 uint32_t jobid,
9696 int sysjob,
9697 struct spoolss_JobInfo1 *r)
9699 int i = 0;
9700 bool found = false;
9702 for (i=0; i<count; i++) {
9703 if (queue[i].sysjob == sysjob) {
9704 found = true;
9705 break;
9709 if (found == false) {
9710 /* NT treats not found as bad param... yet another bad choice */
9711 return WERR_INVALID_PARAMETER;
9714 return fill_job_info1(mem_ctx,
9716 &queue[i],
9717 jobid,
9719 snum,
9720 pinfo2);
9723 /****************************************************************************
9724 ****************************************************************************/
9726 static WERROR getjob_level_2(TALLOC_CTX *mem_ctx,
9727 const print_queue_struct *queue,
9728 int count, int snum,
9729 struct spoolss_PrinterInfo2 *pinfo2,
9730 uint32_t jobid,
9731 int sysjob,
9732 struct spoolss_JobInfo2 *r)
9734 int i = 0;
9735 bool found = false;
9736 struct spoolss_DeviceMode *devmode;
9737 WERROR result;
9739 for (i=0; i<count; i++) {
9740 if (queue[i].sysjob == sysjob) {
9741 found = true;
9742 break;
9746 if (found == false) {
9747 /* NT treats not found as bad param... yet another bad
9748 choice */
9749 return WERR_INVALID_PARAMETER;
9753 * if the print job does not have a DEVMODE associated with it,
9754 * just use the one for the printer. A NULL devicemode is not
9755 * a failure condition
9758 devmode = print_job_devmode(mem_ctx, lp_const_servicename(snum), jobid);
9759 if (!devmode) {
9760 result = spoolss_create_default_devmode(mem_ctx,
9761 pinfo2->printername,
9762 &devmode);
9763 if (!W_ERROR_IS_OK(result)) {
9764 DEBUG(3, ("Can't proceed w/o a devmode!"));
9765 return result;
9769 return fill_job_info2(mem_ctx,
9771 &queue[i],
9772 jobid,
9774 snum,
9775 pinfo2,
9776 devmode);
9779 /****************************************************************
9780 _spoolss_GetJob
9781 ****************************************************************/
9783 WERROR _spoolss_GetJob(struct pipes_struct *p,
9784 struct spoolss_GetJob *r)
9786 WERROR result = WERR_OK;
9787 struct spoolss_PrinterInfo2 *pinfo2 = NULL;
9788 const char *svc_name;
9789 int sysjob;
9790 int snum;
9791 int count;
9792 struct tdb_print_db *pdb;
9793 print_queue_struct *queue = NULL;
9794 print_status_struct prt_status;
9796 /* that's an [in out] buffer */
9798 if (!r->in.buffer && (r->in.offered != 0)) {
9799 result = WERR_INVALID_PARAMETER;
9800 goto err_jinfo_free;
9803 DEBUG(5,("_spoolss_GetJob\n"));
9805 *r->out.needed = 0;
9807 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
9808 result = WERR_INVALID_HANDLE;
9809 goto err_jinfo_free;
9812 svc_name = lp_const_servicename(snum);
9813 if (svc_name == NULL) {
9814 result = WERR_INVALID_PARAMETER;
9815 goto err_jinfo_free;
9818 result = winreg_get_printer_internal(p->mem_ctx,
9819 get_session_info_system(),
9820 p->msg_ctx,
9821 svc_name,
9822 &pinfo2);
9823 if (!W_ERROR_IS_OK(result)) {
9824 goto err_jinfo_free;
9827 pdb = get_print_db_byname(svc_name);
9828 if (pdb == NULL) {
9829 DEBUG(3, ("failed to get print db for svc %s\n", svc_name));
9830 result = WERR_INVALID_PARAMETER;
9831 goto err_pinfo_free;
9834 sysjob = jobid_to_sysjob_pdb(pdb, r->in.job_id);
9835 release_print_db(pdb);
9836 if (sysjob == -1) {
9837 DEBUG(3, ("no sysjob for spoolss jobid %u\n", r->in.job_id));
9838 result = WERR_INVALID_PARAMETER;
9839 goto err_pinfo_free;
9842 count = print_queue_status(p->msg_ctx, snum, &queue, &prt_status);
9844 DEBUGADD(4,("count:[%d], prt_status:[%d], [%s]\n",
9845 count, prt_status.status, prt_status.message));
9847 switch (r->in.level) {
9848 case 1:
9849 result = getjob_level_1(p->mem_ctx,
9850 queue, count, snum, pinfo2,
9851 r->in.job_id, sysjob,
9852 &r->out.info->info1);
9853 break;
9854 case 2:
9855 result = getjob_level_2(p->mem_ctx,
9856 queue, count, snum, pinfo2,
9857 r->in.job_id, sysjob,
9858 &r->out.info->info2);
9859 break;
9860 default:
9861 result = WERR_INVALID_LEVEL;
9862 break;
9865 SAFE_FREE(queue);
9866 TALLOC_FREE(pinfo2);
9868 if (!W_ERROR_IS_OK(result)) {
9869 goto err_jinfo_free;
9872 *r->out.needed = SPOOLSS_BUFFER_UNION(spoolss_JobInfo, r->out.info,
9873 r->in.level);
9874 r->out.info = SPOOLSS_BUFFER_OK(r->out.info, NULL);
9876 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
9878 err_pinfo_free:
9879 TALLOC_FREE(pinfo2);
9880 err_jinfo_free:
9881 TALLOC_FREE(r->out.info);
9882 return result;
9885 /****************************************************************
9886 _spoolss_GetPrinterDataEx
9887 ****************************************************************/
9889 WERROR _spoolss_GetPrinterDataEx(struct pipes_struct *p,
9890 struct spoolss_GetPrinterDataEx *r)
9893 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
9894 const char *printer;
9895 int snum = 0;
9896 WERROR result = WERR_OK;
9897 DATA_BLOB blob;
9898 enum winreg_Type val_type = REG_NONE;
9899 uint8_t *val_data = NULL;
9900 uint32_t val_size = 0;
9901 struct dcerpc_binding_handle *b;
9902 TALLOC_CTX *tmp_ctx;
9904 DEBUG(4,("_spoolss_GetPrinterDataEx\n"));
9906 DEBUG(10, ("_spoolss_GetPrinterDataEx: key => [%s], value => [%s]\n",
9907 r->in.key_name, r->in.value_name));
9909 /* in case of problem, return some default values */
9911 *r->out.needed = 0;
9912 *r->out.type = REG_NONE;
9914 tmp_ctx = talloc_new(p->mem_ctx);
9915 if (!tmp_ctx) {
9916 return WERR_NOT_ENOUGH_MEMORY;
9919 if (!Printer) {
9920 DEBUG(2,("_spoolss_GetPrinterDataEx: Invalid handle (%s:%u:%u).\n",
9921 OUR_HANDLE(r->in.handle)));
9922 result = WERR_INVALID_HANDLE;
9923 goto done;
9926 /* Is the handle to a printer or to the server? */
9928 if (Printer->printer_type == SPLHND_SERVER) {
9930 union spoolss_PrinterData data;
9932 result = getprinterdata_printer_server(tmp_ctx,
9933 r->in.value_name,
9934 r->out.type,
9935 &data);
9936 if (!W_ERROR_IS_OK(result)) {
9937 goto done;
9940 result = push_spoolss_PrinterData(tmp_ctx, &blob,
9941 *r->out.type, &data);
9942 if (!W_ERROR_IS_OK(result)) {
9943 goto done;
9946 *r->out.needed = blob.length;
9948 if (r->in.offered >= *r->out.needed) {
9949 memcpy(r->out.data, blob.data, blob.length);
9952 result = WERR_OK;
9953 goto done;
9956 /* check to see if the keyname is valid */
9957 if (!strlen(r->in.key_name)) {
9958 result = WERR_INVALID_PARAMETER;
9959 goto done;
9962 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
9963 result = WERR_INVALID_HANDLE;
9964 goto done;
9966 printer = lp_const_servicename(snum);
9968 result = winreg_printer_binding_handle(tmp_ctx,
9969 get_session_info_system(),
9970 p->msg_ctx,
9971 &b);
9972 if (!W_ERROR_IS_OK(result)) {
9973 goto done;
9976 /* XP sends this and wants the ChangeID value from PRINTER_INFO_0 */
9977 if (strequal(r->in.key_name, SPOOL_PRINTERDATA_KEY) &&
9978 strequal(r->in.value_name, "ChangeId")) {
9979 *r->out.type = REG_DWORD;
9980 *r->out.needed = 4;
9981 if (r->in.offered >= *r->out.needed) {
9982 uint32_t changeid = 0;
9984 result = winreg_printer_get_changeid(tmp_ctx, b,
9985 printer,
9986 &changeid);
9987 if (!W_ERROR_IS_OK(result)) {
9988 goto done;
9991 SIVAL(r->out.data, 0, changeid);
9992 result = WERR_OK;
9994 goto done;
9997 result = winreg_get_printer_dataex(tmp_ctx, b,
9998 printer,
9999 r->in.key_name,
10000 r->in.value_name,
10001 &val_type,
10002 &val_data,
10003 &val_size);
10004 if (!W_ERROR_IS_OK(result)) {
10005 goto done;
10008 *r->out.needed = val_size;
10009 *r->out.type = val_type;
10011 if (r->in.offered >= *r->out.needed) {
10012 memcpy(r->out.data, val_data, val_size);
10015 done:
10016 /* NOTE: do not replace type when returning WERR_MORE_DATA */
10018 if (W_ERROR_IS_OK(result)) {
10019 result = SPOOLSS_BUFFER_OK(WERR_OK, WERR_MORE_DATA);
10022 talloc_free(tmp_ctx);
10023 return result;
10026 /****************************************************************
10027 _spoolss_SetPrinterDataEx
10028 ****************************************************************/
10030 WERROR _spoolss_SetPrinterDataEx(struct pipes_struct *p,
10031 struct spoolss_SetPrinterDataEx *r)
10033 const struct loadparm_substitution *lp_sub =
10034 loadparm_s3_global_substitution();
10035 struct spoolss_PrinterInfo2 *pinfo2 = NULL;
10036 int snum = 0;
10037 WERROR result = WERR_OK;
10038 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
10039 char *oid_string;
10040 struct dcerpc_binding_handle *b;
10041 TALLOC_CTX *tmp_ctx;
10043 DEBUG(4,("_spoolss_SetPrinterDataEx\n"));
10045 /* From MSDN documentation of SetPrinterDataEx: pass request to
10046 SetPrinterData if key is "PrinterDriverData" */
10048 if (!Printer) {
10049 DEBUG(2,("_spoolss_SetPrinterDataEx: Invalid handle (%s:%u:%u).\n",
10050 OUR_HANDLE(r->in.handle)));
10051 return WERR_INVALID_HANDLE;
10054 if (Printer->printer_type == SPLHND_SERVER) {
10055 DEBUG(10,("_spoolss_SetPrinterDataEx: "
10056 "Not implemented for server handles yet\n"));
10057 return WERR_INVALID_PARAMETER;
10060 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
10061 return WERR_INVALID_HANDLE;
10065 * Access check : NT returns "access denied" if you make a
10066 * SetPrinterData call without the necessary privildge.
10067 * we were originally returning OK if nothing changed
10068 * which made Win2k issue **a lot** of SetPrinterData
10069 * when connecting to a printer --jerry
10072 if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
10073 DEBUG(3, ("_spoolss_SetPrinterDataEx: "
10074 "change denied by handle access permissions\n"));
10075 return WERR_ACCESS_DENIED;
10078 tmp_ctx = talloc_new(p->mem_ctx);
10079 if (!tmp_ctx) {
10080 return WERR_NOT_ENOUGH_MEMORY;
10083 result = winreg_printer_binding_handle(tmp_ctx,
10084 get_session_info_system(),
10085 p->msg_ctx,
10086 &b);
10087 if (!W_ERROR_IS_OK(result)) {
10088 goto done;
10091 result = winreg_get_printer(tmp_ctx, b,
10092 lp_servicename(talloc_tos(), lp_sub, snum),
10093 &pinfo2);
10094 if (!W_ERROR_IS_OK(result)) {
10095 goto done;
10098 /* check for OID in valuename */
10100 oid_string = strchr(r->in.value_name, ',');
10101 if (oid_string) {
10102 *oid_string = '\0';
10103 oid_string++;
10106 /* save the registry data */
10108 result = winreg_set_printer_dataex(tmp_ctx, b,
10109 pinfo2->sharename,
10110 r->in.key_name,
10111 r->in.value_name,
10112 r->in.type,
10113 r->in.data,
10114 r->in.offered);
10116 if (W_ERROR_IS_OK(result)) {
10117 /* save the OID if one was specified */
10118 if (oid_string) {
10119 char *str = talloc_asprintf(tmp_ctx, "%s\\%s",
10120 r->in.key_name, SPOOL_OID_KEY);
10121 if (!str) {
10122 result = WERR_NOT_ENOUGH_MEMORY;
10123 goto done;
10127 * I'm not checking the status here on purpose. Don't know
10128 * if this is right, but I'm returning the status from the
10129 * previous set_printer_dataex() call. I have no idea if
10130 * this is right. --jerry
10132 winreg_set_printer_dataex(tmp_ctx, b,
10133 pinfo2->sharename,
10134 str,
10135 r->in.value_name,
10136 REG_SZ,
10137 (uint8_t *) oid_string,
10138 strlen(oid_string) + 1);
10141 result = winreg_printer_update_changeid(tmp_ctx, b,
10142 lp_const_servicename(snum));
10146 done:
10147 talloc_free(tmp_ctx);
10148 return result;
10151 /****************************************************************
10152 _spoolss_DeletePrinterDataEx
10153 ****************************************************************/
10155 WERROR _spoolss_DeletePrinterDataEx(struct pipes_struct *p,
10156 struct spoolss_DeletePrinterDataEx *r)
10158 const char *printer;
10159 int snum=0;
10160 WERROR status = WERR_OK;
10161 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
10163 DEBUG(5,("_spoolss_DeletePrinterDataEx\n"));
10165 if (!Printer) {
10166 DEBUG(2,("_spoolss_DeletePrinterDataEx: "
10167 "Invalid handle (%s:%u:%u).\n",
10168 OUR_HANDLE(r->in.handle)));
10169 return WERR_INVALID_HANDLE;
10172 if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
10173 DEBUG(3, ("_spoolss_DeletePrinterDataEx: "
10174 "printer properties change denied by handle\n"));
10175 return WERR_ACCESS_DENIED;
10178 if (!r->in.value_name || !r->in.key_name) {
10179 return WERR_NOT_ENOUGH_MEMORY;
10182 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
10183 return WERR_INVALID_HANDLE;
10185 printer = lp_const_servicename(snum);
10187 status = winreg_delete_printer_dataex_internal(p->mem_ctx,
10188 get_session_info_system(),
10189 p->msg_ctx,
10190 printer,
10191 r->in.key_name,
10192 r->in.value_name);
10193 if (W_ERROR_IS_OK(status)) {
10194 status = winreg_printer_update_changeid_internal(p->mem_ctx,
10195 get_session_info_system(),
10196 p->msg_ctx,
10197 printer);
10200 return status;
10203 /****************************************************************
10204 _spoolss_EnumPrinterKey
10205 ****************************************************************/
10207 WERROR _spoolss_EnumPrinterKey(struct pipes_struct *p,
10208 struct spoolss_EnumPrinterKey *r)
10210 uint32_t num_keys;
10211 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
10212 int snum = 0;
10213 WERROR result = WERR_FILE_NOT_FOUND;
10214 const char **array = NULL;
10215 DATA_BLOB blob;
10217 DEBUG(4,("_spoolss_EnumPrinterKey\n"));
10219 if (!Printer) {
10220 DEBUG(2,("_spoolss_EnumPrinterKey: Invalid handle (%s:%u:%u).\n",
10221 OUR_HANDLE(r->in.handle)));
10222 return WERR_INVALID_HANDLE;
10225 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
10226 return WERR_INVALID_HANDLE;
10229 result = winreg_enum_printer_key_internal(p->mem_ctx,
10230 get_session_info_system(),
10231 p->msg_ctx,
10232 lp_const_servicename(snum),
10233 r->in.key_name,
10234 &num_keys,
10235 &array);
10236 if (!W_ERROR_IS_OK(result)) {
10237 goto done;
10240 if (!push_reg_multi_sz(p->mem_ctx, &blob, array)) {
10241 result = WERR_NOT_ENOUGH_MEMORY;
10242 goto done;
10245 *r->out._ndr_size = r->in.offered / 2;
10246 *r->out.needed = blob.length;
10248 if (r->in.offered < *r->out.needed) {
10249 result = WERR_MORE_DATA;
10250 } else {
10251 result = WERR_OK;
10252 r->out.key_buffer->string_array = array;
10255 done:
10256 if (!W_ERROR_IS_OK(result)) {
10257 TALLOC_FREE(array);
10258 if (!W_ERROR_EQUAL(result, WERR_MORE_DATA)) {
10259 *r->out.needed = 0;
10263 return result;
10266 /****************************************************************
10267 _spoolss_DeletePrinterKey
10268 ****************************************************************/
10270 WERROR _spoolss_DeletePrinterKey(struct pipes_struct *p,
10271 struct spoolss_DeletePrinterKey *r)
10273 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
10274 int snum=0;
10275 WERROR status;
10276 const char *printer;
10277 struct dcerpc_binding_handle *b;
10278 TALLOC_CTX *tmp_ctx;
10280 DEBUG(5,("_spoolss_DeletePrinterKey\n"));
10282 if (!Printer) {
10283 DEBUG(2,("_spoolss_DeletePrinterKey: Invalid handle (%s:%u:%u).\n",
10284 OUR_HANDLE(r->in.handle)));
10285 return WERR_INVALID_HANDLE;
10288 /* if keyname == NULL, return error */
10289 if ( !r->in.key_name )
10290 return WERR_INVALID_PARAMETER;
10292 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
10293 return WERR_INVALID_HANDLE;
10296 if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
10297 DEBUG(3, ("_spoolss_DeletePrinterKey: "
10298 "printer properties change denied by handle\n"));
10299 return WERR_ACCESS_DENIED;
10302 printer = lp_const_servicename(snum);
10304 tmp_ctx = talloc_new(p->mem_ctx);
10305 if (!tmp_ctx) {
10306 return WERR_NOT_ENOUGH_MEMORY;
10309 status = winreg_printer_binding_handle(tmp_ctx,
10310 get_session_info_system(),
10311 p->msg_ctx,
10312 &b);
10313 if (!W_ERROR_IS_OK(status)) {
10314 goto done;
10317 /* delete the key and all subkeys */
10318 status = winreg_delete_printer_key(tmp_ctx, b,
10319 printer,
10320 r->in.key_name);
10321 if (W_ERROR_IS_OK(status)) {
10322 status = winreg_printer_update_changeid(tmp_ctx, b,
10323 printer);
10326 done:
10327 talloc_free(tmp_ctx);
10328 return status;
10331 /****************************************************************
10332 _spoolss_EnumPrinterDataEx
10333 ****************************************************************/
10335 WERROR _spoolss_EnumPrinterDataEx(struct pipes_struct *p,
10336 struct spoolss_EnumPrinterDataEx *r)
10338 uint32_t count = 0;
10339 struct spoolss_PrinterEnumValues *info = NULL;
10340 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
10341 int snum;
10342 WERROR result;
10344 DEBUG(4,("_spoolss_EnumPrinterDataEx\n"));
10346 *r->out.count = 0;
10347 *r->out.needed = 0;
10348 *r->out.info = NULL;
10350 if (!Printer) {
10351 DEBUG(2,("_spoolss_EnumPrinterDataEx: Invalid handle (%s:%u:%u1<).\n",
10352 OUR_HANDLE(r->in.handle)));
10353 return WERR_INVALID_HANDLE;
10357 * first check for a keyname of NULL or "". Win2k seems to send
10358 * this a lot and we should send back WERR_INVALID_PARAMETER
10359 * no need to spend time looking up the printer in this case.
10360 * --jerry
10363 if (!strlen(r->in.key_name)) {
10364 result = WERR_INVALID_PARAMETER;
10365 goto done;
10368 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
10369 return WERR_INVALID_HANDLE;
10372 /* now look for a match on the key name */
10373 result = winreg_enum_printer_dataex_internal(p->mem_ctx,
10374 get_session_info_system(),
10375 p->msg_ctx,
10376 lp_const_servicename(snum),
10377 r->in.key_name,
10378 &count,
10379 &info);
10380 if (!W_ERROR_IS_OK(result)) {
10381 goto done;
10384 #if 0 /* FIXME - gd */
10385 /* housekeeping information in the reply */
10387 /* Fix from Martin Zielinski <mz@seh.de> - ensure
10388 * the hand marshalled container size is a multiple
10389 * of 4 bytes for RPC alignment.
10392 if (needed % 4) {
10393 needed += 4-(needed % 4);
10395 #endif
10396 *r->out.count = count;
10397 *r->out.info = info;
10399 done:
10400 if (!W_ERROR_IS_OK(result)) {
10401 return result;
10404 *r->out.needed = SPOOLSS_BUFFER_ARRAY(p->mem_ctx,
10405 spoolss_EnumPrinterDataEx,
10406 *r->out.info,
10407 *r->out.count);
10408 *r->out.info = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
10409 *r->out.count = SPOOLSS_BUFFER_OK(*r->out.count, *r->out.count);
10411 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_MORE_DATA);
10414 /****************************************************************************
10415 ****************************************************************************/
10417 static WERROR getprintprocessordirectory_level_1(TALLOC_CTX *mem_ctx,
10418 const char *servername,
10419 const char *environment,
10420 struct spoolss_PrintProcessorDirectoryInfo1 *r)
10422 WERROR werr;
10423 char *path = NULL;
10425 werr = compose_spoolss_server_path(mem_ctx,
10426 servername,
10427 environment,
10428 SPOOLSS_PRTPROCS_PATH,
10429 &path);
10430 if (!W_ERROR_IS_OK(werr)) {
10431 return werr;
10434 DEBUG(4,("print processor directory: [%s]\n", path));
10436 r->directory_name = path;
10438 return WERR_OK;
10441 /****************************************************************
10442 _spoolss_GetPrintProcessorDirectory
10443 ****************************************************************/
10445 WERROR _spoolss_GetPrintProcessorDirectory(struct pipes_struct *p,
10446 struct spoolss_GetPrintProcessorDirectory *r)
10448 WERROR result;
10449 char *prnproc_share = NULL;
10450 bool prnproc_share_exists = false;
10451 int snum;
10453 /* that's an [in out] buffer */
10455 if (!r->in.buffer && (r->in.offered != 0)) {
10456 result = WERR_INVALID_PARAMETER;
10457 goto err_info_free;
10460 DEBUG(5,("_spoolss_GetPrintProcessorDirectory: level %d\n",
10461 r->in.level));
10463 *r->out.needed = 0;
10465 /* r->in.level is ignored */
10467 /* We always should reply with a local print processor directory so that
10468 * users are not forced to have a [prnproc$] share on the Samba spoolss
10469 * server, if users decide to do so, lets announce it though - Guenther */
10471 snum = find_service(talloc_tos(), "prnproc$", &prnproc_share);
10472 if (!prnproc_share) {
10473 result = WERR_NOT_ENOUGH_MEMORY;
10474 goto err_info_free;
10476 if (snum != -1) {
10477 prnproc_share_exists = true;
10480 result = getprintprocessordirectory_level_1(p->mem_ctx,
10481 prnproc_share_exists ? r->in.server : NULL,
10482 r->in.environment,
10483 &r->out.info->info1);
10484 if (!W_ERROR_IS_OK(result)) {
10485 goto err_info_free;
10488 *r->out.needed = SPOOLSS_BUFFER_UNION(spoolss_PrintProcessorDirectoryInfo,
10489 r->out.info, r->in.level);
10490 r->out.info = SPOOLSS_BUFFER_OK(r->out.info, NULL);
10492 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
10494 err_info_free:
10495 TALLOC_FREE(r->out.info);
10496 return result;
10499 /*******************************************************************
10500 ********************************************************************/
10502 static bool push_monitorui_buf(TALLOC_CTX *mem_ctx, DATA_BLOB *buf,
10503 const char *dllname)
10505 enum ndr_err_code ndr_err;
10506 struct spoolss_MonitorUi ui;
10508 ui.dll_name = dllname;
10510 ndr_err = ndr_push_struct_blob(buf, mem_ctx, &ui,
10511 (ndr_push_flags_fn_t)ndr_push_spoolss_MonitorUi);
10512 if (NDR_ERR_CODE_IS_SUCCESS(ndr_err) && (DEBUGLEVEL >= 10)) {
10513 NDR_PRINT_DEBUG(spoolss_MonitorUi, &ui);
10515 return NDR_ERR_CODE_IS_SUCCESS(ndr_err);
10518 /*******************************************************************
10519 Streams the monitor UI DLL name in UNICODE
10520 *******************************************************************/
10522 static WERROR xcvtcp_monitorui(TALLOC_CTX *mem_ctx,
10523 struct security_token *token, DATA_BLOB *in,
10524 DATA_BLOB *out, uint32_t *needed)
10526 const char *dllname = "tcpmonui.dll";
10528 *needed = (strlen(dllname)+1) * 2;
10530 if (out->length < *needed) {
10531 return WERR_INSUFFICIENT_BUFFER;
10534 if (!push_monitorui_buf(mem_ctx, out, dllname)) {
10535 return WERR_NOT_ENOUGH_MEMORY;
10538 return WERR_OK;
10541 /*******************************************************************
10542 ********************************************************************/
10544 static bool pull_port_data_1(TALLOC_CTX *mem_ctx,
10545 struct spoolss_PortData1 *port1,
10546 const DATA_BLOB *buf)
10548 enum ndr_err_code ndr_err;
10549 ndr_err = ndr_pull_struct_blob(buf, mem_ctx, port1,
10550 (ndr_pull_flags_fn_t)ndr_pull_spoolss_PortData1);
10551 if (NDR_ERR_CODE_IS_SUCCESS(ndr_err) && (DEBUGLEVEL >= 10)) {
10552 NDR_PRINT_DEBUG(spoolss_PortData1, port1);
10554 return NDR_ERR_CODE_IS_SUCCESS(ndr_err);
10557 /*******************************************************************
10558 ********************************************************************/
10560 static bool pull_port_data_2(TALLOC_CTX *mem_ctx,
10561 struct spoolss_PortData2 *port2,
10562 const DATA_BLOB *buf)
10564 enum ndr_err_code ndr_err;
10565 ndr_err = ndr_pull_struct_blob(buf, mem_ctx, port2,
10566 (ndr_pull_flags_fn_t)ndr_pull_spoolss_PortData2);
10567 if (NDR_ERR_CODE_IS_SUCCESS(ndr_err) && (DEBUGLEVEL >= 10)) {
10568 NDR_PRINT_DEBUG(spoolss_PortData2, port2);
10570 return NDR_ERR_CODE_IS_SUCCESS(ndr_err);
10573 /*******************************************************************
10574 Create a new TCP/IP port
10575 *******************************************************************/
10577 static WERROR xcvtcp_addport(TALLOC_CTX *mem_ctx,
10578 struct security_token *token, DATA_BLOB *in,
10579 DATA_BLOB *out, uint32_t *needed)
10581 struct spoolss_PortData1 port1;
10582 struct spoolss_PortData2 port2;
10583 char *device_uri = NULL;
10584 uint32_t version;
10586 const char *portname;
10587 const char *hostaddress;
10588 const char *queue;
10589 uint32_t port_number;
10590 uint32_t protocol;
10592 /* peek for spoolss_PortData version */
10594 if (!in || (in->length < (128 + 4))) {
10595 return WERR_GEN_FAILURE;
10598 version = IVAL(in->data, 128);
10600 switch (version) {
10601 case 1:
10602 ZERO_STRUCT(port1);
10604 if (!pull_port_data_1(mem_ctx, &port1, in)) {
10605 return WERR_NOT_ENOUGH_MEMORY;
10608 portname = port1.portname;
10609 hostaddress = port1.hostaddress;
10610 queue = port1.queue;
10611 protocol = port1.protocol;
10612 port_number = port1.port_number;
10614 break;
10615 case 2:
10616 ZERO_STRUCT(port2);
10618 if (!pull_port_data_2(mem_ctx, &port2, in)) {
10619 return WERR_NOT_ENOUGH_MEMORY;
10622 portname = port2.portname;
10623 hostaddress = port2.hostaddress;
10624 queue = port2.queue;
10625 protocol = port2.protocol;
10626 port_number = port2.port_number;
10628 break;
10629 default:
10630 DEBUG(1,("xcvtcp_addport: "
10631 "unknown version of port_data: %d\n", version));
10632 return WERR_UNKNOWN_PORT;
10635 /* create the device URI and call the add_port_hook() */
10637 switch (protocol) {
10638 case PROTOCOL_RAWTCP_TYPE:
10639 device_uri = talloc_asprintf(mem_ctx,
10640 "socket://%s:%d/", hostaddress,
10641 port_number);
10642 break;
10644 case PROTOCOL_LPR_TYPE:
10645 device_uri = talloc_asprintf(mem_ctx,
10646 "lpr://%s/%s", hostaddress, queue );
10647 break;
10649 default:
10650 return WERR_UNKNOWN_PORT;
10653 if (!device_uri) {
10654 return WERR_NOT_ENOUGH_MEMORY;
10657 return add_port_hook(mem_ctx, token, portname, device_uri);
10660 /*******************************************************************
10661 *******************************************************************/
10663 struct xcv_api_table xcvtcp_cmds[] = {
10664 { "MonitorUI", xcvtcp_monitorui },
10665 { "AddPort", xcvtcp_addport},
10666 { NULL, NULL }
10669 static WERROR process_xcvtcp_command(TALLOC_CTX *mem_ctx,
10670 struct security_token *token, const char *command,
10671 DATA_BLOB *inbuf,
10672 DATA_BLOB *outbuf,
10673 uint32_t *needed )
10675 int i;
10677 DEBUG(10,("process_xcvtcp_command: Received command \"%s\"\n", command));
10679 for ( i=0; xcvtcp_cmds[i].name; i++ ) {
10680 if ( strcmp( command, xcvtcp_cmds[i].name ) == 0 )
10681 return xcvtcp_cmds[i].fn(mem_ctx, token, inbuf, outbuf, needed);
10684 return WERR_INVALID_FUNCTION;
10687 /*******************************************************************
10688 *******************************************************************/
10689 #if 0 /* don't support management using the "Local Port" monitor */
10691 static WERROR xcvlocal_monitorui(TALLOC_CTX *mem_ctx,
10692 struct security_token *token, DATA_BLOB *in,
10693 DATA_BLOB *out, uint32_t *needed)
10695 const char *dllname = "localui.dll";
10697 *needed = (strlen(dllname)+1) * 2;
10699 if (out->length < *needed) {
10700 return WERR_INSUFFICIENT_BUFFER;
10703 if (!push_monitorui_buf(mem_ctx, out, dllname)) {
10704 return WERR_NOT_ENOUGH_MEMORY;
10707 return WERR_OK;
10710 /*******************************************************************
10711 *******************************************************************/
10713 struct xcv_api_table xcvlocal_cmds[] = {
10714 { "MonitorUI", xcvlocal_monitorui },
10715 { NULL, NULL }
10717 #else
10718 struct xcv_api_table xcvlocal_cmds[] = {
10719 { NULL, NULL }
10721 #endif
10725 /*******************************************************************
10726 *******************************************************************/
10728 static WERROR process_xcvlocal_command(TALLOC_CTX *mem_ctx,
10729 struct security_token *token, const char *command,
10730 DATA_BLOB *inbuf, DATA_BLOB *outbuf,
10731 uint32_t *needed)
10733 int i;
10735 DEBUG(10,("process_xcvlocal_command: Received command \"%s\"\n", command));
10737 for ( i=0; xcvlocal_cmds[i].name; i++ ) {
10738 if ( strcmp( command, xcvlocal_cmds[i].name ) == 0 )
10739 return xcvlocal_cmds[i].fn(mem_ctx, token, inbuf, outbuf, needed);
10741 return WERR_INVALID_FUNCTION;
10744 /****************************************************************
10745 _spoolss_XcvData
10746 ****************************************************************/
10748 WERROR _spoolss_XcvData(struct pipes_struct *p,
10749 struct spoolss_XcvData *r)
10751 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
10752 DATA_BLOB out_data = data_blob_null;
10753 WERROR werror;
10755 if (!Printer) {
10756 DEBUG(2,("_spoolss_XcvData: Invalid handle (%s:%u:%u).\n",
10757 OUR_HANDLE(r->in.handle)));
10758 return WERR_INVALID_HANDLE;
10761 /* Has to be a handle to the TCP/IP port monitor */
10763 if ( !(Printer->printer_type & (SPLHND_PORTMON_LOCAL|SPLHND_PORTMON_TCP)) ) {
10764 DEBUG(2,("_spoolss_XcvData: Call only valid for Port Monitors\n"));
10765 return WERR_INVALID_HANDLE;
10768 /* requires administrative access to the server */
10770 if ( !(Printer->access_granted & SERVER_ACCESS_ADMINISTER) ) {
10771 DEBUG(2,("_spoolss_XcvData: denied by handle permissions.\n"));
10772 return WERR_ACCESS_DENIED;
10775 /* Allocate the outgoing buffer */
10777 if (r->in.out_data_size) {
10778 out_data = data_blob_talloc_zero(p->mem_ctx, r->in.out_data_size);
10779 if (out_data.data == NULL) {
10780 return WERR_NOT_ENOUGH_MEMORY;
10784 switch ( Printer->printer_type ) {
10785 case SPLHND_PORTMON_TCP:
10786 werror = process_xcvtcp_command(p->mem_ctx,
10787 p->session_info->security_token,
10788 r->in.function_name,
10789 &r->in.in_data, &out_data,
10790 r->out.needed);
10791 break;
10792 case SPLHND_PORTMON_LOCAL:
10793 werror = process_xcvlocal_command(p->mem_ctx,
10794 p->session_info->security_token,
10795 r->in.function_name,
10796 &r->in.in_data, &out_data,
10797 r->out.needed);
10798 break;
10799 default:
10800 werror = WERR_INVALID_PRINT_MONITOR;
10803 if (!W_ERROR_IS_OK(werror)) {
10804 return werror;
10807 *r->out.status_code = 0;
10809 if (r->out.out_data && out_data.data && r->in.out_data_size && out_data.length) {
10810 memcpy(r->out.out_data, out_data.data,
10811 MIN(r->in.out_data_size, out_data.length));
10814 return WERR_OK;
10817 /****************************************************************
10818 _spoolss_AddPrintProcessor
10819 ****************************************************************/
10821 WERROR _spoolss_AddPrintProcessor(struct pipes_struct *p,
10822 struct spoolss_AddPrintProcessor *r)
10824 /* for now, just indicate success and ignore the add. We'll
10825 automatically set the winprint processor for printer
10826 entries later. Used to debug the LexMark Optra S 1855 PCL
10827 driver --jerry */
10829 return WERR_OK;
10832 /****************************************************************
10833 _spoolss_AddPort
10834 ****************************************************************/
10836 WERROR _spoolss_AddPort(struct pipes_struct *p,
10837 struct spoolss_AddPort *r)
10839 /* do what w2k3 does */
10841 return WERR_NOT_SUPPORTED;
10844 /****************************************************************
10845 _spoolss_GetPrinterDriver
10846 ****************************************************************/
10848 WERROR _spoolss_GetPrinterDriver(struct pipes_struct *p,
10849 struct spoolss_GetPrinterDriver *r)
10851 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10852 return WERR_NOT_SUPPORTED;
10855 /****************************************************************
10856 _spoolss_ReadPrinter
10857 ****************************************************************/
10859 WERROR _spoolss_ReadPrinter(struct pipes_struct *p,
10860 struct spoolss_ReadPrinter *r)
10862 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10863 return WERR_NOT_SUPPORTED;
10866 /****************************************************************
10867 _spoolss_WaitForPrinterChange
10868 ****************************************************************/
10870 WERROR _spoolss_WaitForPrinterChange(struct pipes_struct *p,
10871 struct spoolss_WaitForPrinterChange *r)
10873 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10874 return WERR_NOT_SUPPORTED;
10877 /****************************************************************
10878 _spoolss_ConfigurePort
10879 ****************************************************************/
10881 WERROR _spoolss_ConfigurePort(struct pipes_struct *p,
10882 struct spoolss_ConfigurePort *r)
10884 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10885 return WERR_NOT_SUPPORTED;
10888 /****************************************************************
10889 _spoolss_DeletePort
10890 ****************************************************************/
10892 WERROR _spoolss_DeletePort(struct pipes_struct *p,
10893 struct spoolss_DeletePort *r)
10895 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10896 return WERR_NOT_SUPPORTED;
10899 /****************************************************************
10900 _spoolss_CreatePrinterIC
10901 ****************************************************************/
10903 WERROR _spoolss_CreatePrinterIC(struct pipes_struct *p,
10904 struct spoolss_CreatePrinterIC *r)
10906 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10907 return WERR_NOT_SUPPORTED;
10910 /****************************************************************
10911 _spoolss_PlayGDIScriptOnPrinterIC
10912 ****************************************************************/
10914 WERROR _spoolss_PlayGDIScriptOnPrinterIC(struct pipes_struct *p,
10915 struct spoolss_PlayGDIScriptOnPrinterIC *r)
10917 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10918 return WERR_NOT_SUPPORTED;
10921 /****************************************************************
10922 _spoolss_DeletePrinterIC
10923 ****************************************************************/
10925 WERROR _spoolss_DeletePrinterIC(struct pipes_struct *p,
10926 struct spoolss_DeletePrinterIC *r)
10928 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10929 return WERR_NOT_SUPPORTED;
10932 /****************************************************************
10933 _spoolss_AddPrinterConnection
10934 ****************************************************************/
10936 WERROR _spoolss_AddPrinterConnection(struct pipes_struct *p,
10937 struct spoolss_AddPrinterConnection *r)
10939 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10940 return WERR_NOT_SUPPORTED;
10943 /****************************************************************
10944 _spoolss_DeletePrinterConnection
10945 ****************************************************************/
10947 WERROR _spoolss_DeletePrinterConnection(struct pipes_struct *p,
10948 struct spoolss_DeletePrinterConnection *r)
10950 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10951 return WERR_NOT_SUPPORTED;
10954 /****************************************************************
10955 _spoolss_PrinterMessageBox
10956 ****************************************************************/
10958 WERROR _spoolss_PrinterMessageBox(struct pipes_struct *p,
10959 struct spoolss_PrinterMessageBox *r)
10961 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10962 return WERR_NOT_SUPPORTED;
10965 /****************************************************************
10966 _spoolss_AddMonitor
10967 ****************************************************************/
10969 WERROR _spoolss_AddMonitor(struct pipes_struct *p,
10970 struct spoolss_AddMonitor *r)
10972 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10973 return WERR_NOT_SUPPORTED;
10976 /****************************************************************
10977 _spoolss_DeleteMonitor
10978 ****************************************************************/
10980 WERROR _spoolss_DeleteMonitor(struct pipes_struct *p,
10981 struct spoolss_DeleteMonitor *r)
10983 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10984 return WERR_NOT_SUPPORTED;
10987 /****************************************************************
10988 _spoolss_DeletePrintProcessor
10989 ****************************************************************/
10991 WERROR _spoolss_DeletePrintProcessor(struct pipes_struct *p,
10992 struct spoolss_DeletePrintProcessor *r)
10994 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10995 return WERR_NOT_SUPPORTED;
10998 /****************************************************************
10999 _spoolss_AddPrintProvidor
11000 ****************************************************************/
11002 WERROR _spoolss_AddPrintProvidor(struct pipes_struct *p,
11003 struct spoolss_AddPrintProvidor *r)
11005 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
11006 return WERR_NOT_SUPPORTED;
11009 /****************************************************************
11010 _spoolss_DeletePrintProvidor
11011 ****************************************************************/
11013 WERROR _spoolss_DeletePrintProvidor(struct pipes_struct *p,
11014 struct spoolss_DeletePrintProvidor *r)
11016 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
11017 return WERR_NOT_SUPPORTED;
11020 /****************************************************************
11021 _spoolss_FindFirstPrinterChangeNotification
11022 ****************************************************************/
11024 WERROR _spoolss_FindFirstPrinterChangeNotification(struct pipes_struct *p,
11025 struct spoolss_FindFirstPrinterChangeNotification *r)
11027 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
11028 return WERR_NOT_SUPPORTED;
11031 /****************************************************************
11032 _spoolss_FindNextPrinterChangeNotification
11033 ****************************************************************/
11035 WERROR _spoolss_FindNextPrinterChangeNotification(struct pipes_struct *p,
11036 struct spoolss_FindNextPrinterChangeNotification *r)
11038 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
11039 return WERR_NOT_SUPPORTED;
11042 /****************************************************************
11043 _spoolss_RouterFindFirstPrinterChangeNotificationOld
11044 ****************************************************************/
11046 WERROR _spoolss_RouterFindFirstPrinterChangeNotificationOld(struct pipes_struct *p,
11047 struct spoolss_RouterFindFirstPrinterChangeNotificationOld *r)
11049 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
11050 return WERR_NOT_SUPPORTED;
11053 /****************************************************************
11054 _spoolss_ReplyOpenPrinter
11055 ****************************************************************/
11057 WERROR _spoolss_ReplyOpenPrinter(struct pipes_struct *p,
11058 struct spoolss_ReplyOpenPrinter *r)
11060 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
11061 return WERR_NOT_SUPPORTED;
11064 /****************************************************************
11065 _spoolss_RouterReplyPrinter
11066 ****************************************************************/
11068 WERROR _spoolss_RouterReplyPrinter(struct pipes_struct *p,
11069 struct spoolss_RouterReplyPrinter *r)
11071 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
11072 return WERR_NOT_SUPPORTED;
11075 /****************************************************************
11076 _spoolss_ReplyClosePrinter
11077 ****************************************************************/
11079 WERROR _spoolss_ReplyClosePrinter(struct pipes_struct *p,
11080 struct spoolss_ReplyClosePrinter *r)
11082 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
11083 return WERR_NOT_SUPPORTED;
11086 /****************************************************************
11087 _spoolss_AddPortEx
11088 ****************************************************************/
11090 WERROR _spoolss_AddPortEx(struct pipes_struct *p,
11091 struct spoolss_AddPortEx *r)
11093 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
11094 return WERR_NOT_SUPPORTED;
11097 /****************************************************************
11098 _spoolss_RouterFindFirstPrinterChangeNotification
11099 ****************************************************************/
11101 WERROR _spoolss_RouterFindFirstPrinterChangeNotification(struct pipes_struct *p,
11102 struct spoolss_RouterFindFirstPrinterChangeNotification *r)
11104 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
11105 return WERR_NOT_SUPPORTED;
11108 /****************************************************************
11109 _spoolss_SpoolerInit
11110 ****************************************************************/
11112 WERROR _spoolss_SpoolerInit(struct pipes_struct *p,
11113 struct spoolss_SpoolerInit *r)
11115 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
11116 return WERR_NOT_SUPPORTED;
11119 /****************************************************************
11120 _spoolss_ResetPrinterEx
11121 ****************************************************************/
11123 WERROR _spoolss_ResetPrinterEx(struct pipes_struct *p,
11124 struct spoolss_ResetPrinterEx *r)
11126 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
11127 return WERR_NOT_SUPPORTED;
11130 /****************************************************************
11131 _spoolss_RouterReplyPrinterEx
11132 ****************************************************************/
11134 WERROR _spoolss_RouterReplyPrinterEx(struct pipes_struct *p,
11135 struct spoolss_RouterReplyPrinterEx *r)
11137 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
11138 return WERR_NOT_SUPPORTED;
11141 /****************************************************************
11142 _spoolss_44
11143 ****************************************************************/
11145 WERROR _spoolss_44(struct pipes_struct *p,
11146 struct spoolss_44 *r)
11148 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
11149 return WERR_NOT_SUPPORTED;
11152 /****************************************************************
11153 _spoolss_SetPort
11154 ****************************************************************/
11156 WERROR _spoolss_SetPort(struct pipes_struct *p,
11157 struct spoolss_SetPort *r)
11159 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
11160 return WERR_NOT_SUPPORTED;
11163 /****************************************************************
11164 _spoolss_4a
11165 ****************************************************************/
11167 WERROR _spoolss_4a(struct pipes_struct *p,
11168 struct spoolss_4a *r)
11170 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
11171 return WERR_NOT_SUPPORTED;
11174 /****************************************************************
11175 _spoolss_4b
11176 ****************************************************************/
11178 WERROR _spoolss_4b(struct pipes_struct *p,
11179 struct spoolss_4b *r)
11181 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
11182 return WERR_NOT_SUPPORTED;
11185 /****************************************************************
11186 _spoolss_4c
11187 ****************************************************************/
11189 WERROR _spoolss_4c(struct pipes_struct *p,
11190 struct spoolss_4c *r)
11192 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
11193 return WERR_NOT_SUPPORTED;
11196 /****************************************************************
11197 _spoolss_53
11198 ****************************************************************/
11200 WERROR _spoolss_53(struct pipes_struct *p,
11201 struct spoolss_53 *r)
11203 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
11204 return WERR_NOT_SUPPORTED;
11207 /****************************************************************
11208 _spoolss_AddPerMachineConnection
11209 ****************************************************************/
11211 WERROR _spoolss_AddPerMachineConnection(struct pipes_struct *p,
11212 struct spoolss_AddPerMachineConnection *r)
11214 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
11215 return WERR_NOT_SUPPORTED;
11218 /****************************************************************
11219 _spoolss_DeletePerMachineConnection
11220 ****************************************************************/
11222 WERROR _spoolss_DeletePerMachineConnection(struct pipes_struct *p,
11223 struct spoolss_DeletePerMachineConnection *r)
11225 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
11226 return WERR_NOT_SUPPORTED;
11229 /****************************************************************
11230 _spoolss_EnumPerMachineConnections
11231 ****************************************************************/
11233 WERROR _spoolss_EnumPerMachineConnections(struct pipes_struct *p,
11234 struct spoolss_EnumPerMachineConnections *r)
11236 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
11237 return WERR_NOT_SUPPORTED;
11240 /****************************************************************
11241 _spoolss_5a
11242 ****************************************************************/
11244 WERROR _spoolss_5a(struct pipes_struct *p,
11245 struct spoolss_5a *r)
11247 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
11248 return WERR_NOT_SUPPORTED;
11251 /****************************************************************
11252 _spoolss_5b
11253 ****************************************************************/
11255 WERROR _spoolss_5b(struct pipes_struct *p,
11256 struct spoolss_5b *r)
11258 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
11259 return WERR_NOT_SUPPORTED;
11262 /****************************************************************
11263 _spoolss_5c
11264 ****************************************************************/
11266 WERROR _spoolss_5c(struct pipes_struct *p,
11267 struct spoolss_5c *r)
11269 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
11270 return WERR_NOT_SUPPORTED;
11273 /****************************************************************
11274 _spoolss_5d
11275 ****************************************************************/
11277 WERROR _spoolss_5d(struct pipes_struct *p,
11278 struct spoolss_5d *r)
11280 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
11281 return WERR_NOT_SUPPORTED;
11284 /****************************************************************
11285 _spoolss_5e
11286 ****************************************************************/
11288 WERROR _spoolss_5e(struct pipes_struct *p,
11289 struct spoolss_5e *r)
11291 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
11292 return WERR_NOT_SUPPORTED;
11295 /****************************************************************
11296 _spoolss_5f
11297 ****************************************************************/
11299 WERROR _spoolss_5f(struct pipes_struct *p,
11300 struct spoolss_5f *r)
11302 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
11303 return WERR_NOT_SUPPORTED;
11306 /****************************************************************
11307 _spoolss_60
11308 ****************************************************************/
11310 WERROR _spoolss_60(struct pipes_struct *p,
11311 struct spoolss_60 *r)
11313 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
11314 return WERR_NOT_SUPPORTED;
11317 /****************************************************************
11318 _spoolss_SendRecvBidiData
11319 ****************************************************************/
11321 WERROR _spoolss_SendRecvBidiData(struct pipes_struct *p,
11322 struct spoolss_SendRecvBidiData *r)
11324 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
11325 return WERR_NOT_SUPPORTED;
11328 /****************************************************************
11329 _spoolss_62
11330 ****************************************************************/
11332 WERROR _spoolss_62(struct pipes_struct *p,
11333 struct spoolss_62 *r)
11335 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
11336 return WERR_NOT_SUPPORTED;
11339 /****************************************************************
11340 _spoolss_63
11341 ****************************************************************/
11343 WERROR _spoolss_63(struct pipes_struct *p,
11344 struct spoolss_63 *r)
11346 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
11347 return WERR_NOT_SUPPORTED;
11350 /****************************************************************
11351 _spoolss_64
11352 ****************************************************************/
11354 WERROR _spoolss_64(struct pipes_struct *p,
11355 struct spoolss_64 *r)
11357 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
11358 return WERR_NOT_SUPPORTED;
11361 /****************************************************************
11362 _spoolss_65
11363 ****************************************************************/
11365 WERROR _spoolss_65(struct pipes_struct *p,
11366 struct spoolss_65 *r)
11368 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
11369 return WERR_NOT_SUPPORTED;
11372 /****************************************************************
11373 _spoolss_GetCorePrinterDrivers
11374 ****************************************************************/
11376 HRESULT _spoolss_GetCorePrinterDrivers(struct pipes_struct *p,
11377 struct spoolss_GetCorePrinterDrivers *r)
11379 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
11380 return HRES_ERROR_NOT_SUPPORTED;
11383 /****************************************************************
11384 _spoolss_67
11385 ****************************************************************/
11387 WERROR _spoolss_67(struct pipes_struct *p,
11388 struct spoolss_67 *r)
11390 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
11391 return WERR_NOT_SUPPORTED;
11394 /****************************************************************
11395 _spoolss_GetPrinterDriverPackagePath
11396 ****************************************************************/
11398 HRESULT _spoolss_GetPrinterDriverPackagePath(struct pipes_struct *p,
11399 struct spoolss_GetPrinterDriverPackagePath *r)
11401 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
11402 return HRES_ERROR_NOT_SUPPORTED;
11405 /****************************************************************
11406 _spoolss_69
11407 ****************************************************************/
11409 WERROR _spoolss_69(struct pipes_struct *p,
11410 struct spoolss_69 *r)
11412 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
11413 return WERR_NOT_SUPPORTED;
11416 /****************************************************************
11417 _spoolss_6a
11418 ****************************************************************/
11420 WERROR _spoolss_6a(struct pipes_struct *p,
11421 struct spoolss_6a *r)
11423 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
11424 return WERR_NOT_SUPPORTED;
11427 /****************************************************************
11428 _spoolss_6b
11429 ****************************************************************/
11431 WERROR _spoolss_6b(struct pipes_struct *p,
11432 struct spoolss_6b *r)
11434 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
11435 return WERR_NOT_SUPPORTED;
11438 /****************************************************************
11439 _spoolss_6c
11440 ****************************************************************/
11442 WERROR _spoolss_6c(struct pipes_struct *p,
11443 struct spoolss_6c *r)
11445 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
11446 return WERR_NOT_SUPPORTED;
11449 /****************************************************************
11450 _spoolss_6d
11451 ****************************************************************/
11453 WERROR _spoolss_6d(struct pipes_struct *p,
11454 struct spoolss_6d *r)
11456 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
11457 return WERR_NOT_SUPPORTED;
11460 /****************************************************************
11461 _spoolss_GetJobNamedPropertyValue
11462 ****************************************************************/
11464 WERROR _spoolss_GetJobNamedPropertyValue(struct pipes_struct *p,
11465 struct spoolss_GetJobNamedPropertyValue *r)
11467 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
11468 return WERR_NOT_SUPPORTED;
11471 /****************************************************************
11472 _spoolss_SetJobNamedProperty
11473 ****************************************************************/
11475 WERROR _spoolss_SetJobNamedProperty(struct pipes_struct *p,
11476 struct spoolss_SetJobNamedProperty *r)
11478 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
11479 return WERR_NOT_SUPPORTED;
11482 /****************************************************************
11483 _spoolss_DeleteJobNamedProperty
11484 ****************************************************************/
11486 WERROR _spoolss_DeleteJobNamedProperty(struct pipes_struct *p,
11487 struct spoolss_DeleteJobNamedProperty *r)
11489 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
11490 return WERR_NOT_SUPPORTED;
11493 /****************************************************************
11494 _spoolss_EnumJobNamedProperties
11495 ****************************************************************/
11497 WERROR _spoolss_EnumJobNamedProperties(struct pipes_struct *p,
11498 struct spoolss_EnumJobNamedProperties *r)
11500 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
11501 return WERR_NOT_SUPPORTED;
11504 /****************************************************************
11505 _spoolss_72
11506 ****************************************************************/
11508 WERROR _spoolss_72(struct pipes_struct *p,
11509 struct spoolss_72 *r)
11511 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
11512 return WERR_NOT_SUPPORTED;
11515 /****************************************************************
11516 _spoolss_73
11517 ****************************************************************/
11519 WERROR _spoolss_73(struct pipes_struct *p,
11520 struct spoolss_73 *r)
11522 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
11523 return WERR_NOT_SUPPORTED;
11526 /****************************************************************
11527 _spoolss_RpcLogJobInfoForBranchOffice
11528 ****************************************************************/
11530 WERROR _spoolss_LogJobInfoForBranchOffice(struct pipes_struct *p,
11531 struct spoolss_LogJobInfoForBranchOffice *r)
11533 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
11534 return WERR_NOT_SUPPORTED;
11537 static NTSTATUS spoolss__op_init_server(struct dcesrv_context *dce_ctx,
11538 const struct dcesrv_endpoint_server *ep_server);
11540 static NTSTATUS spoolss__op_shutdown_server(struct dcesrv_context *dce_ctx,
11541 const struct dcesrv_endpoint_server *ep_server);
11543 #define DCESRV_INTERFACE_SPOOLSS_INIT_SERVER \
11544 spoolss_init_server
11546 #define DCESRV_INTERFACE_SPOOLSS_SHUTDOWN_SERVER \
11547 spoolss_shutdown_server
11549 static NTSTATUS spoolss_init_server(struct dcesrv_context *dce_ctx,
11550 const struct dcesrv_endpoint_server *ep_server)
11552 struct messaging_context *msg_ctx = global_messaging_context();
11553 NTSTATUS status;
11554 bool ok;
11556 status = dcesrv_init_ep_server(dce_ctx, "winreg");
11557 if (!NT_STATUS_IS_OK(status)) {
11558 return status;
11562 * Migrate the printers first.
11564 ok = nt_printing_tdb_migrate(msg_ctx);
11565 if (!ok) {
11566 return NT_STATUS_UNSUCCESSFUL;
11569 return spoolss__op_init_server(dce_ctx, ep_server);
11572 static NTSTATUS spoolss_shutdown_server(struct dcesrv_context *dce_ctx,
11573 const struct dcesrv_endpoint_server *ep_server)
11575 srv_spoolss_cleanup();
11577 return spoolss__op_shutdown_server(dce_ctx, ep_server);
11580 /* include the generated boilerplate */
11581 #include "librpc/gen_ndr/ndr_spoolss_scompat.c"