spoolss: accept XPS_PASS datatype used by Windows 8
[Samba.git] / source3 / rpc_server / spoolss / srv_spoolss_nt.c
blobfa5f0229289a1c0f4a9830accd7273ad270cd6e1
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 "ntdomain.h"
32 #include "nt_printing.h"
33 #include "srv_spoolss_util.h"
34 #include "../librpc/gen_ndr/srv_spoolss.h"
35 #include "../librpc/gen_ndr/ndr_spoolss_c.h"
36 #include "rpc_client/init_spoolss.h"
37 #include "rpc_client/cli_pipe.h"
38 #include "../libcli/security/security.h"
39 #include "librpc/gen_ndr/ndr_security.h"
40 #include "registry.h"
41 #include "include/printing.h"
42 #include "secrets.h"
43 #include "../librpc/gen_ndr/netlogon.h"
44 #include "rpc_misc.h"
45 #include "printing/notify.h"
46 #include "serverid.h"
47 #include "../libcli/registry/util_reg.h"
48 #include "smbd/smbd.h"
49 #include "smbd/globals.h"
50 #include "auth.h"
51 #include "messages.h"
52 #include "rpc_server/spoolss/srv_spoolss_nt.h"
53 #include "util_tdb.h"
54 #include "libsmb/libsmb.h"
55 #include "printing/printer_list.h"
56 #include "../lib/tsocket/tsocket.h"
57 #include "rpc_client/cli_winreg_spoolss.h"
58 #include "../libcli/smb/smbXcli_base.h"
60 /* macros stolen from s4 spoolss server */
61 #define SPOOLSS_BUFFER_UNION(fn,info,level) \
62 ((info)?ndr_size_##fn(info, level, 0):0)
64 #define SPOOLSS_BUFFER_UNION_ARRAY(mem_ctx,fn,info,level,count) \
65 ((info)?ndr_size_##fn##_info(mem_ctx, level, count, info):0)
67 #define SPOOLSS_BUFFER_ARRAY(mem_ctx,fn,info,count) \
68 ((info)?ndr_size_##fn##_info(mem_ctx, count, info):0)
70 #define SPOOLSS_BUFFER_OK(val_true,val_false) ((r->in.offered >= *r->out.needed)?val_true:val_false)
72 #undef DBGC_CLASS
73 #define DBGC_CLASS DBGC_RPC_SRV
75 #ifndef MAX_OPEN_PRINTER_EXS
76 #define MAX_OPEN_PRINTER_EXS 50
77 #endif
79 struct notify_back_channel;
81 /* structure to store the printer handles */
82 /* and a reference to what it's pointing to */
83 /* and the notify info asked about */
84 /* that's the central struct */
85 struct printer_handle {
86 struct printer_handle *prev, *next;
87 bool document_started;
88 bool page_started;
89 uint32 jobid; /* jobid in printing backend */
90 int printer_type;
91 const char *servername;
92 fstring sharename;
93 uint32 type;
94 uint32 access_granted;
95 struct {
96 uint32 flags;
97 uint32 options;
98 fstring localmachine;
99 uint32 printerlocal;
100 struct spoolss_NotifyOption *option;
101 struct policy_handle cli_hnd;
102 struct notify_back_channel *cli_chan;
103 uint32 change;
104 /* are we in a FindNextPrinterChangeNotify() call? */
105 bool fnpcn;
106 struct messaging_context *msg_ctx;
107 } notify;
108 struct {
109 fstring machine;
110 fstring user;
111 } client;
113 /* devmode sent in the OpenPrinter() call */
114 struct spoolss_DeviceMode *devmode;
116 /* TODO cache the printer info2 structure */
117 struct spoolss_PrinterInfo2 *info2;
121 static struct printer_handle *printers_list;
123 struct printer_session_counter {
124 struct printer_session_counter *next;
125 struct printer_session_counter *prev;
127 int snum;
128 uint32_t counter;
131 static struct printer_session_counter *counter_list;
133 struct notify_back_channel {
134 struct notify_back_channel *prev, *next;
136 /* associated client */
137 struct sockaddr_storage client_address;
139 /* print notify back-channel pipe handle*/
140 struct rpc_pipe_client *cli_pipe;
141 uint32_t active_connections;
144 static struct notify_back_channel *back_channels;
146 /* Map generic permissions to printer object specific permissions */
148 const struct standard_mapping printer_std_mapping = {
149 PRINTER_READ,
150 PRINTER_WRITE,
151 PRINTER_EXECUTE,
152 PRINTER_ALL_ACCESS
155 /* Map generic permissions to print server object specific permissions */
157 const struct standard_mapping printserver_std_mapping = {
158 SERVER_READ,
159 SERVER_WRITE,
160 SERVER_EXECUTE,
161 SERVER_ALL_ACCESS
164 /* API table for Xcv Monitor functions */
166 struct xcv_api_table {
167 const char *name;
168 WERROR(*fn) (TALLOC_CTX *mem_ctx, struct security_token *token, DATA_BLOB *in, DATA_BLOB *out, uint32_t *needed);
171 static void prune_printername_cache(void);
173 /********************************************************************
174 * Canonicalize servername.
175 ********************************************************************/
177 static const char *canon_servername(const char *servername)
179 const char *pservername = servername;
180 while (*pservername == '\\') {
181 pservername++;
183 return pservername;
186 /* translate between internal status numbers and NT status numbers */
187 static int nt_printj_status(int v)
189 switch (v) {
190 case LPQ_QUEUED:
191 return 0;
192 case LPQ_PAUSED:
193 return JOB_STATUS_PAUSED;
194 case LPQ_SPOOLING:
195 return JOB_STATUS_SPOOLING;
196 case LPQ_PRINTING:
197 return JOB_STATUS_PRINTING;
198 case LPQ_ERROR:
199 return JOB_STATUS_ERROR;
200 case LPQ_DELETING:
201 return JOB_STATUS_DELETING;
202 case LPQ_OFFLINE:
203 return JOB_STATUS_OFFLINE;
204 case LPQ_PAPEROUT:
205 return JOB_STATUS_PAPEROUT;
206 case LPQ_PRINTED:
207 return JOB_STATUS_PRINTED;
208 case LPQ_DELETED:
209 return JOB_STATUS_DELETED;
210 case LPQ_BLOCKED:
211 return JOB_STATUS_BLOCKED_DEVQ;
212 case LPQ_USER_INTERVENTION:
213 return JOB_STATUS_USER_INTERVENTION;
215 return 0;
218 static int nt_printq_status(int v)
220 switch (v) {
221 case LPQ_PAUSED:
222 return PRINTER_STATUS_PAUSED;
223 case LPQ_QUEUED:
224 case LPQ_SPOOLING:
225 case LPQ_PRINTING:
226 return 0;
228 return 0;
231 /***************************************************************************
232 Disconnect from the client
233 ****************************************************************************/
235 static void srv_spoolss_replycloseprinter(int snum,
236 struct printer_handle *prn_hnd)
238 WERROR result;
239 NTSTATUS status;
242 * Tell the specific printing tdb we no longer want messages for this printer
243 * by deregistering our PID.
246 if (!print_notify_deregister_pid(snum)) {
247 DEBUG(0, ("Failed to register our pid for printer %s\n",
248 lp_const_servicename(snum)));
251 /* weird if the test succeeds !!! */
252 if (prn_hnd->notify.cli_chan == NULL ||
253 prn_hnd->notify.cli_chan->cli_pipe == NULL ||
254 prn_hnd->notify.cli_chan->cli_pipe->binding_handle == NULL ||
255 prn_hnd->notify.cli_chan->active_connections == 0) {
256 DEBUG(0, ("Trying to close unexisting backchannel!\n"));
257 DLIST_REMOVE(back_channels, prn_hnd->notify.cli_chan);
258 TALLOC_FREE(prn_hnd->notify.cli_chan);
259 return;
262 status = dcerpc_spoolss_ReplyClosePrinter(
263 prn_hnd->notify.cli_chan->cli_pipe->binding_handle,
264 talloc_tos(),
265 &prn_hnd->notify.cli_hnd,
266 &result);
267 if (!NT_STATUS_IS_OK(status)) {
268 DEBUG(0, ("dcerpc_spoolss_ReplyClosePrinter failed [%s].\n",
269 nt_errstr(status)));
270 result = ntstatus_to_werror(status);
271 } else if (!W_ERROR_IS_OK(result)) {
272 DEBUG(0, ("reply_close_printer failed [%s].\n",
273 win_errstr(result)));
276 /* if it's the last connection, deconnect the IPC$ share */
277 if (prn_hnd->notify.cli_chan->active_connections == 1) {
279 cli_shutdown(rpc_pipe_np_smb_conn(prn_hnd->notify.cli_chan->cli_pipe));
280 DLIST_REMOVE(back_channels, prn_hnd->notify.cli_chan);
281 TALLOC_FREE(prn_hnd->notify.cli_chan);
283 if (prn_hnd->notify.msg_ctx != NULL) {
284 messaging_deregister(prn_hnd->notify.msg_ctx,
285 MSG_PRINTER_NOTIFY2, NULL);
289 if (prn_hnd->notify.cli_chan) {
290 prn_hnd->notify.cli_chan->active_connections--;
291 prn_hnd->notify.cli_chan = NULL;
295 /****************************************************************************
296 Functions to free a printer entry datastruct.
297 ****************************************************************************/
299 static int printer_entry_destructor(struct printer_handle *Printer)
301 if (Printer->notify.cli_chan != NULL &&
302 Printer->notify.cli_chan->active_connections > 0) {
303 int snum = -1;
305 switch(Printer->printer_type) {
306 case SPLHND_SERVER:
307 srv_spoolss_replycloseprinter(snum, Printer);
308 break;
310 case SPLHND_PRINTER:
311 snum = print_queue_snum(Printer->sharename);
312 if (snum != -1) {
313 srv_spoolss_replycloseprinter(snum, Printer);
315 break;
316 default:
317 break;
321 Printer->notify.flags=0;
322 Printer->notify.options=0;
323 Printer->notify.localmachine[0]='\0';
324 Printer->notify.printerlocal=0;
325 TALLOC_FREE(Printer->notify.option);
326 TALLOC_FREE(Printer->devmode);
328 /* Remove from the internal list. */
329 DLIST_REMOVE(printers_list, Printer);
330 return 0;
333 /****************************************************************************
334 find printer index by handle
335 ****************************************************************************/
337 static struct printer_handle *find_printer_index_by_hnd(struct pipes_struct *p,
338 struct policy_handle *hnd)
340 struct printer_handle *find_printer = NULL;
342 if(!find_policy_by_hnd(p,hnd,(void **)(void *)&find_printer)) {
343 DEBUG(2,("find_printer_index_by_hnd: Printer handle not found: "));
344 return NULL;
347 return find_printer;
350 /****************************************************************************
351 Close printer index by handle.
352 ****************************************************************************/
354 static bool close_printer_handle(struct pipes_struct *p, struct policy_handle *hnd)
356 struct printer_handle *Printer = find_printer_index_by_hnd(p, hnd);
358 if (!Printer) {
359 DEBUG(2,("close_printer_handle: Invalid handle (%s:%u:%u)\n",
360 OUR_HANDLE(hnd)));
361 return false;
364 close_policy_hnd(p, hnd);
366 return true;
369 /****************************************************************************
370 Delete a printer given a handle.
371 ****************************************************************************/
373 static WERROR delete_printer_hook(TALLOC_CTX *ctx, struct security_token *token,
374 const char *sharename,
375 struct messaging_context *msg_ctx)
377 char *cmd = lp_deleteprinter_cmd(talloc_tos());
378 char *command = NULL;
379 int ret;
380 bool is_print_op = false;
382 /* can't fail if we don't try */
384 if ( !*cmd )
385 return WERR_OK;
387 command = talloc_asprintf(ctx,
388 "%s \"%s\"",
389 cmd, sharename);
390 if (!command) {
391 return WERR_NOMEM;
393 if ( token )
394 is_print_op = security_token_has_privilege(token, SEC_PRIV_PRINT_OPERATOR);
396 DEBUG(10,("Running [%s]\n", command));
398 /********** BEGIN SePrintOperatorPrivlege BLOCK **********/
400 if ( is_print_op )
401 become_root();
403 if ( (ret = smbrun(command, NULL)) == 0 ) {
404 /* Tell everyone we updated smb.conf. */
405 message_send_all(msg_ctx, MSG_SMB_CONF_UPDATED, NULL, 0, NULL);
408 if ( is_print_op )
409 unbecome_root();
411 /********** END SePrintOperatorPrivlege BLOCK **********/
413 DEBUGADD(10,("returned [%d]\n", ret));
415 TALLOC_FREE(command);
417 if (ret != 0)
418 return WERR_BADFID; /* What to return here? */
420 return WERR_OK;
423 /****************************************************************************
424 Delete a printer given a handle.
425 ****************************************************************************/
427 static WERROR delete_printer_handle(struct pipes_struct *p, struct policy_handle *hnd)
429 struct printer_handle *Printer = find_printer_index_by_hnd(p, hnd);
430 WERROR result;
432 if (!Printer) {
433 DEBUG(2,("delete_printer_handle: Invalid handle (%s:%u:%u)\n",
434 OUR_HANDLE(hnd)));
435 return WERR_BADFID;
439 * It turns out that Windows allows delete printer on a handle
440 * opened by an admin user, then used on a pipe handle created
441 * by an anonymous user..... but they're working on security.... riiight !
442 * JRA.
445 if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
446 DEBUG(3, ("delete_printer_handle: denied by handle\n"));
447 return WERR_ACCESS_DENIED;
450 /* this does not need a become root since the access check has been
451 done on the handle already */
453 result = winreg_delete_printer_key_internal(p->mem_ctx,
454 get_session_info_system(),
455 p->msg_ctx,
456 Printer->sharename,
457 "");
458 if (!W_ERROR_IS_OK(result)) {
459 DEBUG(3,("Error deleting printer %s\n", Printer->sharename));
460 return WERR_BADFID;
463 result = delete_printer_hook(p->mem_ctx, p->session_info->security_token,
464 Printer->sharename, p->msg_ctx);
465 if (!W_ERROR_IS_OK(result)) {
466 return result;
468 prune_printername_cache();
469 return WERR_OK;
472 /****************************************************************************
473 Return the snum of a printer corresponding to an handle.
474 ****************************************************************************/
476 static bool get_printer_snum(struct pipes_struct *p, struct policy_handle *hnd,
477 int *number, struct share_params **params)
479 struct printer_handle *Printer = find_printer_index_by_hnd(p, hnd);
481 if (!Printer) {
482 DEBUG(2,("get_printer_snum: Invalid handle (%s:%u:%u)\n",
483 OUR_HANDLE(hnd)));
484 return false;
487 switch (Printer->printer_type) {
488 case SPLHND_PRINTER:
489 DEBUG(4,("short name:%s\n", Printer->sharename));
490 *number = print_queue_snum(Printer->sharename);
491 return (*number != -1);
492 case SPLHND_SERVER:
493 return false;
494 default:
495 return false;
499 /****************************************************************************
500 Set printer handle type.
501 Check if it's \\server or \\server\printer
502 ****************************************************************************/
504 static bool set_printer_hnd_printertype(struct printer_handle *Printer, const char *handlename)
506 DEBUG(3,("Setting printer type=%s\n", handlename));
508 /* it's a print server */
509 if (handlename && *handlename=='\\' && *(handlename+1)=='\\' && !strchr_m(handlename+2, '\\')) {
510 DEBUGADD(4,("Printer is a print server\n"));
511 Printer->printer_type = SPLHND_SERVER;
513 /* it's a printer (set_printer_hnd_name() will handle port monitors */
514 else {
515 DEBUGADD(4,("Printer is a printer\n"));
516 Printer->printer_type = SPLHND_PRINTER;
519 return true;
522 static void prune_printername_cache_fn(const char *key, const char *value,
523 time_t timeout, void *private_data)
525 gencache_del(key);
528 static void prune_printername_cache(void)
530 gencache_iterate(prune_printername_cache_fn, NULL, "PRINTERNAME/*");
533 /****************************************************************************
534 Set printer handle name.. Accept names like \\server, \\server\printer,
535 \\server\SHARE, & "\\server\,XcvMonitor Standard TCP/IP Port" See
536 the MSDN docs regarding OpenPrinter() for details on the XcvData() and
537 XcvDataPort() interface.
538 ****************************************************************************/
540 static WERROR set_printer_hnd_name(TALLOC_CTX *mem_ctx,
541 const struct auth_session_info *session_info,
542 struct messaging_context *msg_ctx,
543 struct printer_handle *Printer,
544 const char *handlename)
546 int snum;
547 int n_services=lp_numservices();
548 char *aprinter;
549 const char *printername;
550 const char *servername = NULL;
551 fstring sname;
552 bool found = false;
553 struct spoolss_PrinterInfo2 *info2 = NULL;
554 WERROR result;
555 char *p;
558 * Hopefully nobody names his printers like this. Maybe \ or ,
559 * are illegal in printer names even?
561 const char printer_not_found[] = "Printer \\, !@#$%^&*( not found";
562 char *cache_key;
563 char *tmp;
565 DEBUG(4,("Setting printer name=%s (len=%lu)\n", handlename,
566 (unsigned long)strlen(handlename)));
568 aprinter = discard_const_p(char, handlename);
569 if ( *handlename == '\\' ) {
570 servername = canon_servername(handlename);
571 if ( (aprinter = strchr_m( servername, '\\' )) != NULL ) {
572 *aprinter = '\0';
573 aprinter++;
575 if (!is_myname_or_ipaddr(servername)) {
576 return WERR_INVALID_PRINTER_NAME;
578 Printer->servername = talloc_asprintf(Printer, "\\\\%s", servername);
579 if (Printer->servername == NULL) {
580 return WERR_NOMEM;
584 if (Printer->printer_type == SPLHND_SERVER) {
585 return WERR_OK;
588 if (Printer->printer_type != SPLHND_PRINTER) {
589 return WERR_INVALID_HANDLE;
592 DEBUGADD(5, ("searching for [%s]\n", aprinter));
594 p = strchr(aprinter, ',');
595 if (p != NULL) {
596 char *p2 = p;
597 p++;
598 if (*p == ' ') {
599 p++;
601 if (strncmp(p, "DrvConvert", strlen("DrvConvert")) == 0) {
602 *p2 = '\0';
603 } else if (strncmp(p, "LocalOnly", strlen("LocalOnly")) == 0) {
604 *p2 = '\0';
608 if (p) {
609 DEBUGADD(5, ("stripped handlename: [%s]\n", aprinter));
612 /* check for the Port Monitor Interface */
613 if ( strequal( aprinter, SPL_XCV_MONITOR_TCPMON ) ) {
614 Printer->printer_type = SPLHND_PORTMON_TCP;
615 fstrcpy(sname, SPL_XCV_MONITOR_TCPMON);
616 found = true;
618 else if ( strequal( aprinter, SPL_XCV_MONITOR_LOCALMON ) ) {
619 Printer->printer_type = SPLHND_PORTMON_LOCAL;
620 fstrcpy(sname, SPL_XCV_MONITOR_LOCALMON);
621 found = true;
625 * With hundreds of printers, the "for" loop iterating all
626 * shares can be quite expensive, as it is done on every
627 * OpenPrinter. The loop maps "aprinter" to "sname", the
628 * result of which we cache in gencache.
631 cache_key = talloc_asprintf(talloc_tos(), "PRINTERNAME/%s",
632 aprinter);
633 if ((cache_key != NULL) && gencache_get(cache_key, &tmp, NULL)) {
635 found = (strcmp(tmp, printer_not_found) != 0);
636 if (!found) {
637 DEBUG(4, ("Printer %s not found\n", aprinter));
638 SAFE_FREE(tmp);
639 return WERR_INVALID_PRINTER_NAME;
641 fstrcpy(sname, tmp);
642 SAFE_FREE(tmp);
645 /* Search all sharenames first as this is easier than pulling
646 the printer_info_2 off of disk. Don't use find_service() since
647 that calls out to map_username() */
649 /* do another loop to look for printernames */
650 for (snum = 0; !found && snum < n_services; snum++) {
651 const char *printer = lp_const_servicename(snum);
653 /* no point going on if this is not a printer */
654 if (!(lp_snum_ok(snum) && lp_print_ok(snum))) {
655 continue;
658 /* ignore [printers] share */
659 if (strequal(printer, "printers")) {
660 continue;
663 fstrcpy(sname, printer);
664 if (strequal(aprinter, printer)) {
665 found = true;
666 break;
669 /* no point looking up the printer object if
670 we aren't allowing printername != sharename */
671 if (lp_force_printername(snum)) {
672 continue;
675 result = winreg_get_printer_internal(mem_ctx,
676 session_info,
677 msg_ctx,
678 sname,
679 &info2);
680 if ( !W_ERROR_IS_OK(result) ) {
681 DEBUG(2,("set_printer_hnd_name: failed to lookup printer [%s] -- result [%s]\n",
682 sname, win_errstr(result)));
683 continue;
686 printername = strrchr(info2->printername, '\\');
687 if (printername == NULL) {
688 printername = info2->printername;
689 } else {
690 printername++;
693 if (strequal(printername, aprinter)) {
694 found = true;
695 break;
698 DEBUGADD(10, ("printername: %s\n", printername));
700 TALLOC_FREE(info2);
703 if ( !found ) {
704 if (cache_key != NULL) {
705 gencache_set(cache_key, printer_not_found,
706 time(NULL)+300);
707 TALLOC_FREE(cache_key);
709 DEBUGADD(4,("Printer not found\n"));
710 return WERR_INVALID_PRINTER_NAME;
713 if (cache_key != NULL) {
714 gencache_set(cache_key, sname, time(NULL)+300);
715 TALLOC_FREE(cache_key);
718 DEBUGADD(4,("set_printer_hnd_name: Printer found: %s -> %s\n", aprinter, sname));
720 strlcpy(Printer->sharename, sname, sizeof(Printer->sharename));
722 return WERR_OK;
725 /****************************************************************************
726 Find first available printer slot. creates a printer handle for you.
727 ****************************************************************************/
729 static WERROR open_printer_hnd(struct pipes_struct *p,
730 struct policy_handle *hnd,
731 const char *name,
732 uint32_t access_granted)
734 struct printer_handle *new_printer;
735 WERROR result;
737 DEBUG(10,("open_printer_hnd: name [%s]\n", name));
739 new_printer = talloc_zero(p->mem_ctx, struct printer_handle);
740 if (new_printer == NULL) {
741 return WERR_NOMEM;
743 talloc_set_destructor(new_printer, printer_entry_destructor);
745 /* This also steals the printer_handle on the policy_handle */
746 if (!create_policy_hnd(p, hnd, new_printer)) {
747 TALLOC_FREE(new_printer);
748 return WERR_INVALID_HANDLE;
751 /* Add to the internal list. */
752 DLIST_ADD(printers_list, new_printer);
754 new_printer->notify.option=NULL;
756 if (!set_printer_hnd_printertype(new_printer, name)) {
757 close_printer_handle(p, hnd);
758 return WERR_INVALID_HANDLE;
761 result = set_printer_hnd_name(p->mem_ctx,
762 get_session_info_system(),
763 p->msg_ctx,
764 new_printer, name);
765 if (!W_ERROR_IS_OK(result)) {
766 close_printer_handle(p, hnd);
767 return result;
770 new_printer->access_granted = access_granted;
772 DEBUG(5, ("%d printer handles active\n",
773 (int)num_pipe_handles(p)));
775 return WERR_OK;
778 /***************************************************************************
779 check to see if the client motify handle is monitoring the notification
780 given by (notify_type, notify_field).
781 **************************************************************************/
783 static bool is_monitoring_event_flags(uint32_t flags, uint16_t notify_type,
784 uint16_t notify_field)
786 return true;
789 static bool is_monitoring_event(struct printer_handle *p, uint16_t notify_type,
790 uint16_t notify_field)
792 struct spoolss_NotifyOption *option = p->notify.option;
793 uint32_t i, j;
796 * Flags should always be zero when the change notify
797 * is registered by the client's spooler. A user Win32 app
798 * might use the flags though instead of the NOTIFY_OPTION_INFO
799 * --jerry
802 if (!option) {
803 return false;
806 if (p->notify.flags)
807 return is_monitoring_event_flags(
808 p->notify.flags, notify_type, notify_field);
810 for (i = 0; i < option->count; i++) {
812 /* Check match for notify_type */
814 if (option->types[i].type != notify_type)
815 continue;
817 /* Check match for field */
819 for (j = 0; j < option->types[i].count; j++) {
820 if (option->types[i].fields[j].field == notify_field) {
821 return true;
826 DEBUG(10, ("Open handle for \\\\%s\\%s is not monitoring 0x%02x/0x%02x\n",
827 p->servername, p->sharename, notify_type, notify_field));
829 return false;
832 #define SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(_data, _integer) \
833 _data->data.integer[0] = _integer; \
834 _data->data.integer[1] = 0;
837 #define SETUP_SPOOLSS_NOTIFY_DATA_STRING(_data, _p) \
838 _data->data.string.string = talloc_strdup(mem_ctx, _p); \
839 if (!_data->data.string.string) {\
840 _data->data.string.size = 0; \
842 _data->data.string.size = strlen_m_term(_p) * 2;
844 #define SETUP_SPOOLSS_NOTIFY_DATA_DEVMODE(_data, _devmode) \
845 _data->data.devmode.devmode = _devmode;
847 #define SETUP_SPOOLSS_NOTIFY_DATA_SECDESC(_data, _sd) \
848 _data->data.sd.sd = dup_sec_desc(mem_ctx, _sd); \
849 if (!_data->data.sd.sd) { \
850 _data->data.sd.sd_size = 0; \
852 _data->data.sd.sd_size = \
853 ndr_size_security_descriptor(_data->data.sd.sd, 0);
855 static void init_systemtime_buffer(TALLOC_CTX *mem_ctx,
856 struct tm *t,
857 const char **pp,
858 uint32_t *plen)
860 struct spoolss_Time st;
861 uint32_t len = 16;
862 char *p;
864 if (!init_systemtime(&st, t)) {
865 return;
868 p = talloc_array(mem_ctx, char, len);
869 if (!p) {
870 return;
874 * Systemtime must be linearized as a set of UINT16's.
875 * Fix from Benjamin (Bj) Kuit bj@it.uts.edu.au
878 SSVAL(p, 0, st.year);
879 SSVAL(p, 2, st.month);
880 SSVAL(p, 4, st.day_of_week);
881 SSVAL(p, 6, st.day);
882 SSVAL(p, 8, st.hour);
883 SSVAL(p, 10, st.minute);
884 SSVAL(p, 12, st.second);
885 SSVAL(p, 14, st.millisecond);
887 *pp = p;
888 *plen = len;
891 /* Convert a notification message to a struct spoolss_Notify */
893 static void notify_one_value(struct spoolss_notify_msg *msg,
894 struct spoolss_Notify *data,
895 TALLOC_CTX *mem_ctx)
897 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, msg->notify.value[0]);
900 static void notify_string(struct spoolss_notify_msg *msg,
901 struct spoolss_Notify *data,
902 TALLOC_CTX *mem_ctx)
904 /* The length of the message includes the trailing \0 */
906 data->data.string.size = msg->len * 2;
907 data->data.string.string = talloc_strdup(mem_ctx, msg->notify.data);
908 if (!data->data.string.string) {
909 data->data.string.size = 0;
910 return;
914 static void notify_system_time(struct spoolss_notify_msg *msg,
915 struct spoolss_Notify *data,
916 TALLOC_CTX *mem_ctx)
918 data->data.string.string = NULL;
919 data->data.string.size = 0;
921 if (msg->len != sizeof(time_t)) {
922 DEBUG(5, ("notify_system_time: received wrong sized message (%d)\n",
923 msg->len));
924 return;
927 init_systemtime_buffer(mem_ctx, gmtime((time_t *)msg->notify.data),
928 &data->data.string.string,
929 &data->data.string.size);
932 struct notify2_message_table {
933 const char *name;
934 void (*fn)(struct spoolss_notify_msg *msg,
935 struct spoolss_Notify *data, TALLOC_CTX *mem_ctx);
938 static struct notify2_message_table printer_notify_table[] = {
939 /* 0x00 */ { "PRINTER_NOTIFY_FIELD_SERVER_NAME", notify_string },
940 /* 0x01 */ { "PRINTER_NOTIFY_FIELD_PRINTER_NAME", notify_string },
941 /* 0x02 */ { "PRINTER_NOTIFY_FIELD_SHARE_NAME", notify_string },
942 /* 0x03 */ { "PRINTER_NOTIFY_FIELD_PORT_NAME", notify_string },
943 /* 0x04 */ { "PRINTER_NOTIFY_FIELD_DRIVER_NAME", notify_string },
944 /* 0x05 */ { "PRINTER_NOTIFY_FIELD_COMMENT", notify_string },
945 /* 0x06 */ { "PRINTER_NOTIFY_FIELD_LOCATION", notify_string },
946 /* 0x07 */ { "PRINTER_NOTIFY_FIELD_DEVMODE", NULL },
947 /* 0x08 */ { "PRINTER_NOTIFY_FIELD_SEPFILE", notify_string },
948 /* 0x09 */ { "PRINTER_NOTIFY_FIELD_PRINT_PROCESSOR", notify_string },
949 /* 0x0a */ { "PRINTER_NOTIFY_FIELD_PARAMETERS", NULL },
950 /* 0x0b */ { "PRINTER_NOTIFY_FIELD_DATATYPE", notify_string },
951 /* 0x0c */ { "PRINTER_NOTIFY_FIELD_SECURITY_DESCRIPTOR", NULL },
952 /* 0x0d */ { "PRINTER_NOTIFY_FIELD_ATTRIBUTES", notify_one_value },
953 /* 0x0e */ { "PRINTER_NOTIFY_FIELD_PRIORITY", notify_one_value },
954 /* 0x0f */ { "PRINTER_NOTIFY_FIELD_DEFAULT_PRIORITY", NULL },
955 /* 0x10 */ { "PRINTER_NOTIFY_FIELD_START_TIME", NULL },
956 /* 0x11 */ { "PRINTER_NOTIFY_FIELD_UNTIL_TIME", NULL },
957 /* 0x12 */ { "PRINTER_NOTIFY_FIELD_STATUS", notify_one_value },
960 static struct notify2_message_table job_notify_table[] = {
961 /* 0x00 */ { "JOB_NOTIFY_FIELD_PRINTER_NAME", NULL },
962 /* 0x01 */ { "JOB_NOTIFY_FIELD_MACHINE_NAME", NULL },
963 /* 0x02 */ { "JOB_NOTIFY_FIELD_PORT_NAME", NULL },
964 /* 0x03 */ { "JOB_NOTIFY_FIELD_USER_NAME", notify_string },
965 /* 0x04 */ { "JOB_NOTIFY_FIELD_NOTIFY_NAME", NULL },
966 /* 0x05 */ { "JOB_NOTIFY_FIELD_DATATYPE", NULL },
967 /* 0x06 */ { "JOB_NOTIFY_FIELD_PRINT_PROCESSOR", NULL },
968 /* 0x07 */ { "JOB_NOTIFY_FIELD_PARAMETERS", NULL },
969 /* 0x08 */ { "JOB_NOTIFY_FIELD_DRIVER_NAME", NULL },
970 /* 0x09 */ { "JOB_NOTIFY_FIELD_DEVMODE", NULL },
971 /* 0x0a */ { "JOB_NOTIFY_FIELD_STATUS", notify_one_value },
972 /* 0x0b */ { "JOB_NOTIFY_FIELD_STATUS_STRING", NULL },
973 /* 0x0c */ { "JOB_NOTIFY_FIELD_SECURITY_DESCRIPTOR", NULL },
974 /* 0x0d */ { "JOB_NOTIFY_FIELD_DOCUMENT", notify_string },
975 /* 0x0e */ { "JOB_NOTIFY_FIELD_PRIORITY", NULL },
976 /* 0x0f */ { "JOB_NOTIFY_FIELD_POSITION", NULL },
977 /* 0x10 */ { "JOB_NOTIFY_FIELD_SUBMITTED", notify_system_time },
978 /* 0x11 */ { "JOB_NOTIFY_FIELD_START_TIME", NULL },
979 /* 0x12 */ { "JOB_NOTIFY_FIELD_UNTIL_TIME", NULL },
980 /* 0x13 */ { "JOB_NOTIFY_FIELD_TIME", NULL },
981 /* 0x14 */ { "JOB_NOTIFY_FIELD_TOTAL_PAGES", notify_one_value },
982 /* 0x15 */ { "JOB_NOTIFY_FIELD_PAGES_PRINTED", NULL },
983 /* 0x16 */ { "JOB_NOTIFY_FIELD_TOTAL_BYTES", notify_one_value },
984 /* 0x17 */ { "JOB_NOTIFY_FIELD_BYTES_PRINTED", NULL },
988 /***********************************************************************
989 Allocate talloc context for container object
990 **********************************************************************/
992 static void notify_msg_ctr_init( SPOOLSS_NOTIFY_MSG_CTR *ctr )
994 if ( !ctr )
995 return;
997 ctr->ctx = talloc_init("notify_msg_ctr_init %p", ctr);
999 return;
1002 /***********************************************************************
1003 release all allocated memory and zero out structure
1004 **********************************************************************/
1006 static void notify_msg_ctr_destroy( SPOOLSS_NOTIFY_MSG_CTR *ctr )
1008 if ( !ctr )
1009 return;
1011 if ( ctr->ctx )
1012 talloc_destroy(ctr->ctx);
1014 ZERO_STRUCTP(ctr);
1016 return;
1019 /***********************************************************************
1020 **********************************************************************/
1022 static TALLOC_CTX* notify_ctr_getctx( SPOOLSS_NOTIFY_MSG_CTR *ctr )
1024 if ( !ctr )
1025 return NULL;
1027 return ctr->ctx;
1030 /***********************************************************************
1031 **********************************************************************/
1033 static SPOOLSS_NOTIFY_MSG_GROUP* notify_ctr_getgroup( SPOOLSS_NOTIFY_MSG_CTR *ctr, uint32_t idx )
1035 if ( !ctr || !ctr->msg_groups )
1036 return NULL;
1038 if ( idx >= ctr->num_groups )
1039 return NULL;
1041 return &ctr->msg_groups[idx];
1045 /***********************************************************************
1046 How many groups of change messages do we have ?
1047 **********************************************************************/
1049 static int notify_msg_ctr_numgroups( SPOOLSS_NOTIFY_MSG_CTR *ctr )
1051 if ( !ctr )
1052 return 0;
1054 return ctr->num_groups;
1057 /***********************************************************************
1058 Add a SPOOLSS_NOTIFY_MSG_CTR to the correct group
1059 **********************************************************************/
1061 static int notify_msg_ctr_addmsg( SPOOLSS_NOTIFY_MSG_CTR *ctr, SPOOLSS_NOTIFY_MSG *msg )
1063 SPOOLSS_NOTIFY_MSG_GROUP *groups = NULL;
1064 SPOOLSS_NOTIFY_MSG_GROUP *msg_grp = NULL;
1065 SPOOLSS_NOTIFY_MSG *msg_list = NULL;
1066 int i, new_slot;
1068 if ( !ctr || !msg )
1069 return 0;
1071 /* loop over all groups looking for a matching printer name */
1073 for ( i=0; i<ctr->num_groups; i++ ) {
1074 if ( strcmp(ctr->msg_groups[i].printername, msg->printer) == 0 )
1075 break;
1078 /* add a new group? */
1080 if ( i == ctr->num_groups ) {
1081 ctr->num_groups++;
1083 if ( !(groups = talloc_realloc( ctr->ctx, ctr->msg_groups, SPOOLSS_NOTIFY_MSG_GROUP, ctr->num_groups)) ) {
1084 DEBUG(0,("notify_msg_ctr_addmsg: talloc_realloc() failed!\n"));
1085 return 0;
1087 ctr->msg_groups = groups;
1089 /* clear the new entry and set the printer name */
1091 ZERO_STRUCT( ctr->msg_groups[ctr->num_groups-1] );
1092 fstrcpy( ctr->msg_groups[ctr->num_groups-1].printername, msg->printer );
1095 /* add the change messages; 'i' is the correct index now regardless */
1097 msg_grp = &ctr->msg_groups[i];
1099 msg_grp->num_msgs++;
1101 if ( !(msg_list = talloc_realloc( ctr->ctx, msg_grp->msgs, SPOOLSS_NOTIFY_MSG, msg_grp->num_msgs )) ) {
1102 DEBUG(0,("notify_msg_ctr_addmsg: talloc_realloc() failed for new message [%d]!\n", msg_grp->num_msgs));
1103 return 0;
1105 msg_grp->msgs = msg_list;
1107 new_slot = msg_grp->num_msgs-1;
1108 memcpy( &msg_grp->msgs[new_slot], msg, sizeof(SPOOLSS_NOTIFY_MSG) );
1110 /* need to allocate own copy of data */
1112 if ( msg->len != 0 )
1113 msg_grp->msgs[new_slot].notify.data = (char *)
1114 talloc_memdup( ctr->ctx, msg->notify.data, msg->len );
1116 return ctr->num_groups;
1119 static void construct_info_data(struct spoolss_Notify *info_data,
1120 enum spoolss_NotifyType type,
1121 uint16_t field, int id);
1123 /***********************************************************************
1124 Send a change notication message on all handles which have a call
1125 back registered
1126 **********************************************************************/
1128 static int build_notify2_messages(TALLOC_CTX *mem_ctx,
1129 struct printer_handle *prn_hnd,
1130 SPOOLSS_NOTIFY_MSG *messages,
1131 uint32_t num_msgs,
1132 struct spoolss_Notify **_notifies,
1133 int *_count)
1135 struct spoolss_Notify *notifies;
1136 SPOOLSS_NOTIFY_MSG *msg;
1137 int count = 0;
1138 uint32_t id;
1139 int i;
1141 notifies = talloc_zero_array(mem_ctx,
1142 struct spoolss_Notify, num_msgs);
1143 if (!notifies) {
1144 return ENOMEM;
1147 for (i = 0; i < num_msgs; i++) {
1149 msg = &messages[i];
1151 /* Are we monitoring this event? */
1153 if (!is_monitoring_event(prn_hnd, msg->type, msg->field)) {
1154 continue;
1157 DEBUG(10, ("Sending message type [0x%x] field [0x%2x] "
1158 "for printer [%s]\n",
1159 msg->type, msg->field, prn_hnd->sharename));
1162 * if the is a printer notification handle and not a job
1163 * notification type, then set the id to 0.
1164 * Otherwise just use what was specified in the message.
1166 * When registering change notification on a print server
1167 * handle we always need to send back the id (snum) matching
1168 * the printer for which the change took place.
1169 * For change notify registered on a printer handle,
1170 * this does not matter and the id should be 0.
1172 * --jerry
1175 if ((msg->type == PRINTER_NOTIFY_TYPE) &&
1176 (prn_hnd->printer_type == SPLHND_PRINTER)) {
1177 id = 0;
1178 } else {
1179 id = msg->id;
1182 /* Convert unix jobid to smb jobid */
1184 if (msg->flags & SPOOLSS_NOTIFY_MSG_UNIX_JOBID) {
1185 id = sysjob_to_jobid(msg->id);
1187 if (id == -1) {
1188 DEBUG(3, ("no such unix jobid %d\n",
1189 msg->id));
1190 continue;
1194 construct_info_data(&notifies[count],
1195 msg->type, msg->field, id);
1197 switch(msg->type) {
1198 case PRINTER_NOTIFY_TYPE:
1199 if (printer_notify_table[msg->field].fn) {
1200 printer_notify_table[msg->field].fn(msg,
1201 &notifies[count], mem_ctx);
1203 break;
1205 case JOB_NOTIFY_TYPE:
1206 if (job_notify_table[msg->field].fn) {
1207 job_notify_table[msg->field].fn(msg,
1208 &notifies[count], mem_ctx);
1210 break;
1212 default:
1213 DEBUG(5, ("Unknown notification type %d\n",
1214 msg->type));
1215 continue;
1218 count++;
1221 *_notifies = notifies;
1222 *_count = count;
1224 return 0;
1227 static int send_notify2_printer(TALLOC_CTX *mem_ctx,
1228 struct printer_handle *prn_hnd,
1229 SPOOLSS_NOTIFY_MSG_GROUP *msg_group)
1231 struct spoolss_Notify *notifies;
1232 int count = 0;
1233 union spoolss_ReplyPrinterInfo info;
1234 struct spoolss_NotifyInfo info0;
1235 uint32_t reply_result;
1236 NTSTATUS status;
1237 WERROR werr;
1238 int ret;
1240 /* Is there notification on this handle? */
1241 if (prn_hnd->notify.cli_chan == NULL ||
1242 prn_hnd->notify.cli_chan->cli_pipe == NULL ||
1243 prn_hnd->notify.cli_chan->cli_pipe->binding_handle == NULL ||
1244 prn_hnd->notify.cli_chan->active_connections == 0) {
1245 return 0;
1248 DEBUG(10, ("Client connected! [\\\\%s\\%s]\n",
1249 prn_hnd->servername, prn_hnd->sharename));
1251 /* For this printer? Print servers always receive notifications. */
1252 if ((prn_hnd->printer_type == SPLHND_PRINTER) &&
1253 (!strequal(msg_group->printername, prn_hnd->sharename))) {
1254 return 0;
1257 DEBUG(10,("Our printer\n"));
1259 /* build the array of change notifications */
1260 ret = build_notify2_messages(mem_ctx, prn_hnd,
1261 msg_group->msgs,
1262 msg_group->num_msgs,
1263 &notifies, &count);
1264 if (ret) {
1265 return ret;
1268 info0.version = 0x2;
1269 info0.flags = count ? 0x00020000 /* ??? */ : PRINTER_NOTIFY_INFO_DISCARDED;
1270 info0.count = count;
1271 info0.notifies = notifies;
1273 info.info0 = &info0;
1275 status = dcerpc_spoolss_RouterReplyPrinterEx(
1276 prn_hnd->notify.cli_chan->cli_pipe->binding_handle,
1277 mem_ctx,
1278 &prn_hnd->notify.cli_hnd,
1279 prn_hnd->notify.change, /* color */
1280 prn_hnd->notify.flags,
1281 &reply_result,
1282 0, /* reply_type, must be 0 */
1283 info, &werr);
1284 if (!NT_STATUS_IS_OK(status)) {
1285 DEBUG(1, ("dcerpc_spoolss_RouterReplyPrinterEx to client: %s "
1286 "failed: %s\n",
1287 prn_hnd->notify.cli_chan->cli_pipe->srv_name_slash,
1288 nt_errstr(status)));
1289 werr = ntstatus_to_werror(status);
1290 } else if (!W_ERROR_IS_OK(werr)) {
1291 DEBUG(1, ("RouterReplyPrinterEx to client: %s "
1292 "failed: %s\n",
1293 prn_hnd->notify.cli_chan->cli_pipe->srv_name_slash,
1294 win_errstr(werr)));
1296 switch (reply_result) {
1297 case 0:
1298 break;
1299 case PRINTER_NOTIFY_INFO_DISCARDED:
1300 case PRINTER_NOTIFY_INFO_DISCARDNOTED:
1301 case PRINTER_NOTIFY_INFO_COLOR_MISMATCH:
1302 break;
1303 default:
1304 break;
1307 return 0;
1310 static void send_notify2_changes( SPOOLSS_NOTIFY_MSG_CTR *ctr, uint32_t idx )
1312 struct printer_handle *p;
1313 TALLOC_CTX *mem_ctx = notify_ctr_getctx( ctr );
1314 SPOOLSS_NOTIFY_MSG_GROUP *msg_group = notify_ctr_getgroup( ctr, idx );
1315 int ret;
1317 if ( !msg_group ) {
1318 DEBUG(5,("send_notify2_changes() called with no msg group!\n"));
1319 return;
1322 if (!msg_group->msgs) {
1323 DEBUG(5, ("send_notify2_changes() called with no messages!\n"));
1324 return;
1327 DEBUG(8,("send_notify2_changes: Enter...[%s]\n", msg_group->printername));
1329 /* loop over all printers */
1331 for (p = printers_list; p; p = p->next) {
1332 ret = send_notify2_printer(mem_ctx, p, msg_group);
1333 if (ret) {
1334 goto done;
1338 done:
1339 DEBUG(8,("send_notify2_changes: Exit...\n"));
1340 return;
1343 /***********************************************************************
1344 **********************************************************************/
1346 static bool notify2_unpack_msg( SPOOLSS_NOTIFY_MSG *msg, struct timeval *tv, void *buf, size_t len )
1349 uint32_t tv_sec, tv_usec;
1350 size_t offset = 0;
1352 /* Unpack message */
1354 offset += tdb_unpack((uint8_t *)buf + offset, len - offset, "f",
1355 msg->printer);
1357 offset += tdb_unpack((uint8_t *)buf + offset, len - offset, "ddddddd",
1358 &tv_sec, &tv_usec,
1359 &msg->type, &msg->field, &msg->id, &msg->len, &msg->flags);
1361 if (msg->len == 0)
1362 tdb_unpack((uint8_t *)buf + offset, len - offset, "dd",
1363 &msg->notify.value[0], &msg->notify.value[1]);
1364 else
1365 tdb_unpack((uint8_t *)buf + offset, len - offset, "B",
1366 &msg->len, &msg->notify.data);
1368 DEBUG(3, ("notify2_unpack_msg: got NOTIFY2 message for printer %s, jobid %u type %d, field 0x%02x, flags 0x%04x\n",
1369 msg->printer, (unsigned int)msg->id, msg->type, msg->field, msg->flags));
1371 tv->tv_sec = tv_sec;
1372 tv->tv_usec = tv_usec;
1374 if (msg->len == 0)
1375 DEBUG(3, ("notify2_unpack_msg: value1 = %d, value2 = %d\n", msg->notify.value[0],
1376 msg->notify.value[1]));
1377 else
1378 dump_data(3, (uint8_t *)msg->notify.data, msg->len);
1380 return true;
1383 /********************************************************************
1384 Receive a notify2 message list
1385 ********************************************************************/
1387 static void receive_notify2_message_list(struct messaging_context *msg,
1388 void *private_data,
1389 uint32_t msg_type,
1390 struct server_id server_id,
1391 DATA_BLOB *data)
1393 size_t msg_count, i;
1394 char *buf = (char *)data->data;
1395 char *msg_ptr;
1396 size_t msg_len;
1397 SPOOLSS_NOTIFY_MSG notify;
1398 SPOOLSS_NOTIFY_MSG_CTR messages;
1399 int num_groups;
1401 if (data->length < 4) {
1402 DEBUG(0,("receive_notify2_message_list: bad message format (len < 4)!\n"));
1403 return;
1406 msg_count = IVAL(buf, 0);
1407 msg_ptr = buf + 4;
1409 DEBUG(5, ("receive_notify2_message_list: got %lu messages in list\n", (unsigned long)msg_count));
1411 if (msg_count == 0) {
1412 DEBUG(0,("receive_notify2_message_list: bad message format (msg_count == 0) !\n"));
1413 return;
1416 /* initialize the container */
1418 ZERO_STRUCT( messages );
1419 notify_msg_ctr_init( &messages );
1422 * build message groups for each printer identified
1423 * in a change_notify msg. Remember that a PCN message
1424 * includes the handle returned for the srv_spoolss_replyopenprinter()
1425 * call. Therefore messages are grouped according to printer handle.
1428 for ( i=0; i<msg_count; i++ ) {
1429 struct timeval msg_tv;
1431 if (msg_ptr + 4 - buf > data->length) {
1432 DEBUG(0,("receive_notify2_message_list: bad message format (len > buf_size) !\n"));
1433 return;
1436 msg_len = IVAL(msg_ptr,0);
1437 msg_ptr += 4;
1439 if (msg_ptr + msg_len - buf > data->length) {
1440 DEBUG(0,("receive_notify2_message_list: bad message format (bad len) !\n"));
1441 return;
1444 /* unpack messages */
1446 ZERO_STRUCT( notify );
1447 notify2_unpack_msg( &notify, &msg_tv, msg_ptr, msg_len );
1448 msg_ptr += msg_len;
1450 /* add to correct list in container */
1452 notify_msg_ctr_addmsg( &messages, &notify );
1454 /* free memory that might have been allocated by notify2_unpack_msg() */
1456 if ( notify.len != 0 )
1457 SAFE_FREE( notify.notify.data );
1460 /* process each group of messages */
1462 num_groups = notify_msg_ctr_numgroups( &messages );
1463 for ( i=0; i<num_groups; i++ )
1464 send_notify2_changes( &messages, i );
1467 /* cleanup */
1469 DEBUG(10,("receive_notify2_message_list: processed %u messages\n",
1470 (uint32_t)msg_count ));
1472 notify_msg_ctr_destroy( &messages );
1474 return;
1477 /********************************************************************
1478 Send a message to ourself about new driver being installed
1479 so we can upgrade the information for each printer bound to this
1480 driver
1481 ********************************************************************/
1483 static bool srv_spoolss_drv_upgrade_printer(const char *drivername,
1484 struct messaging_context *msg_ctx)
1486 int len = strlen(drivername);
1488 if (!len)
1489 return false;
1491 DEBUG(10,("srv_spoolss_drv_upgrade_printer: Sending message about driver upgrade [%s]\n",
1492 drivername));
1494 messaging_send_buf(msg_ctx, messaging_server_id(msg_ctx),
1495 MSG_PRINTER_DRVUPGRADE,
1496 (const uint8_t *)drivername, len+1);
1498 return true;
1501 void srv_spoolss_cleanup(void)
1503 struct printer_session_counter *session_counter;
1505 for (session_counter = counter_list;
1506 session_counter != NULL;
1507 session_counter = counter_list) {
1508 DLIST_REMOVE(counter_list, session_counter);
1509 TALLOC_FREE(session_counter);
1513 /**********************************************************************
1514 callback to receive a MSG_PRINTER_DRVUPGRADE message and interate
1515 over all printers, upgrading ones as necessary
1516 **********************************************************************/
1518 void do_drv_upgrade_printer(struct messaging_context *msg,
1519 void *private_data,
1520 uint32_t msg_type,
1521 struct server_id server_id,
1522 DATA_BLOB *data)
1524 TALLOC_CTX *tmp_ctx;
1525 const struct auth_session_info *session_info = get_session_info_system();
1526 struct spoolss_PrinterInfo2 *pinfo2;
1527 WERROR result;
1528 const char *drivername;
1529 int snum;
1530 int n_services = lp_numservices();
1531 struct dcerpc_binding_handle *b = NULL;
1533 tmp_ctx = talloc_new(NULL);
1534 if (!tmp_ctx) return;
1536 drivername = talloc_strndup(tmp_ctx, (const char *)data->data, data->length);
1537 if (!drivername) {
1538 DEBUG(0, ("do_drv_upgrade_printer: Out of memoery ?!\n"));
1539 goto done;
1542 DEBUG(10, ("do_drv_upgrade_printer: "
1543 "Got message for new driver [%s]\n", drivername));
1545 /* Iterate the printer list */
1547 for (snum = 0; snum < n_services; snum++) {
1548 if (!lp_snum_ok(snum) || !lp_print_ok(snum)) {
1549 continue;
1552 /* ignore [printers] share */
1553 if (strequal(lp_const_servicename(snum), "printers")) {
1554 continue;
1557 if (b == NULL) {
1558 result = winreg_printer_binding_handle(tmp_ctx,
1559 session_info,
1560 msg,
1561 &b);
1562 if (!W_ERROR_IS_OK(result)) {
1563 break;
1567 result = winreg_get_printer(tmp_ctx, b,
1568 lp_const_servicename(snum),
1569 &pinfo2);
1571 if (!W_ERROR_IS_OK(result)) {
1572 continue;
1575 if (!pinfo2->drivername) {
1576 continue;
1579 if (strcmp(drivername, pinfo2->drivername) != 0) {
1580 continue;
1583 DEBUG(6,("Updating printer [%s]\n", pinfo2->printername));
1585 /* all we care about currently is the change_id */
1586 result = winreg_printer_update_changeid(tmp_ctx, b,
1587 pinfo2->printername);
1589 if (!W_ERROR_IS_OK(result)) {
1590 DEBUG(3, ("do_drv_upgrade_printer: "
1591 "Failed to update changeid [%s]\n",
1592 win_errstr(result)));
1596 /* all done */
1597 done:
1598 talloc_free(tmp_ctx);
1601 /********************************************************************
1602 Update the cache for all printq's with a registered client
1603 connection
1604 ********************************************************************/
1606 void update_monitored_printq_cache(struct messaging_context *msg_ctx)
1608 struct printer_handle *printer = printers_list;
1609 int snum;
1611 /* loop through all printers and update the cache where
1612 a client is connected */
1613 while (printer) {
1614 if ((printer->printer_type == SPLHND_PRINTER) &&
1615 ((printer->notify.cli_chan != NULL) &&
1616 (printer->notify.cli_chan->active_connections > 0))) {
1617 snum = print_queue_snum(printer->sharename);
1618 print_queue_status(msg_ctx, snum, NULL, NULL);
1621 printer = printer->next;
1624 return;
1627 /****************************************************************
1628 _spoolss_OpenPrinter
1629 ****************************************************************/
1631 WERROR _spoolss_OpenPrinter(struct pipes_struct *p,
1632 struct spoolss_OpenPrinter *r)
1634 struct spoolss_OpenPrinterEx e;
1635 WERROR werr;
1637 ZERO_STRUCT(e.in.userlevel);
1639 e.in.printername = r->in.printername;
1640 e.in.datatype = r->in.datatype;
1641 e.in.devmode_ctr = r->in.devmode_ctr;
1642 e.in.access_mask = r->in.access_mask;
1643 e.in.level = 0;
1645 e.out.handle = r->out.handle;
1647 werr = _spoolss_OpenPrinterEx(p, &e);
1649 if (W_ERROR_EQUAL(werr, WERR_INVALID_PARAM)) {
1650 /* OpenPrinterEx returns this for a bad
1651 * printer name. We must return WERR_INVALID_PRINTER_NAME
1652 * instead.
1654 werr = WERR_INVALID_PRINTER_NAME;
1657 return werr;
1660 static WERROR copy_devicemode(TALLOC_CTX *mem_ctx,
1661 struct spoolss_DeviceMode *orig,
1662 struct spoolss_DeviceMode **dest)
1664 struct spoolss_DeviceMode *dm;
1666 dm = talloc(mem_ctx, struct spoolss_DeviceMode);
1667 if (!dm) {
1668 return WERR_NOMEM;
1671 /* copy all values, then duplicate strings and structs */
1672 *dm = *orig;
1674 dm->devicename = talloc_strdup(dm, orig->devicename);
1675 if (!dm->devicename) {
1676 return WERR_NOMEM;
1678 dm->formname = talloc_strdup(dm, orig->formname);
1679 if (!dm->formname) {
1680 return WERR_NOMEM;
1682 if (orig->driverextra_data.data) {
1683 dm->driverextra_data.data =
1684 (uint8_t *) talloc_memdup(dm, orig->driverextra_data.data,
1685 orig->driverextra_data.length);
1686 if (!dm->driverextra_data.data) {
1687 return WERR_NOMEM;
1691 *dest = dm;
1692 return WERR_OK;
1695 /****************************************************************
1696 _spoolss_OpenPrinterEx
1697 ****************************************************************/
1699 WERROR _spoolss_OpenPrinterEx(struct pipes_struct *p,
1700 struct spoolss_OpenPrinterEx *r)
1702 int snum;
1703 char *raddr;
1704 char *rhost;
1705 struct printer_handle *Printer=NULL;
1706 WERROR result;
1707 int rc;
1709 if (!r->in.printername) {
1710 return WERR_INVALID_PARAM;
1713 if (!*r->in.printername) {
1714 return WERR_INVALID_PARAM;
1717 if (r->in.level > 3) {
1718 return WERR_INVALID_PARAM;
1720 if ((r->in.level == 1 && !r->in.userlevel.level1) ||
1721 (r->in.level == 2 && !r->in.userlevel.level2) ||
1722 (r->in.level == 3 && !r->in.userlevel.level3)) {
1723 return WERR_INVALID_PARAM;
1726 /* some sanity check because you can open a printer or a print server */
1727 /* aka: \\server\printer or \\server */
1729 DEBUGADD(3,("checking name: %s\n", r->in.printername));
1731 result = open_printer_hnd(p, r->out.handle, r->in.printername, 0);
1732 if (!W_ERROR_IS_OK(result)) {
1733 DEBUG(3,("_spoolss_OpenPrinterEx: Cannot open a printer handle "
1734 "for printer %s\n", r->in.printername));
1735 ZERO_STRUCTP(r->out.handle);
1736 return result;
1739 Printer = find_printer_index_by_hnd(p, r->out.handle);
1740 if ( !Printer ) {
1741 DEBUG(0,("_spoolss_OpenPrinterEx: logic error. Can't find printer "
1742 "handle we created for printer %s\n", r->in.printername));
1743 close_printer_handle(p, r->out.handle);
1744 ZERO_STRUCTP(r->out.handle);
1745 return WERR_INVALID_PARAM;
1749 * First case: the user is opening the print server:
1751 * Disallow MS AddPrinterWizard if parameter disables it. A Win2k
1752 * client 1st tries an OpenPrinterEx with access==0, MUST be allowed.
1754 * Then both Win2k and WinNT clients try an OpenPrinterEx with
1755 * SERVER_ALL_ACCESS, which we allow only if the user is root (uid=0)
1756 * or if the user is listed in the smb.conf printer admin parameter.
1758 * Then they try OpenPrinterEx with SERVER_READ which we allow. This lets the
1759 * client view printer folder, but does not show the MSAPW.
1761 * Note: this test needs code to check access rights here too. Jeremy
1762 * could you look at this?
1764 * Second case: the user is opening a printer:
1765 * NT doesn't let us connect to a printer if the connecting user
1766 * doesn't have print permission.
1768 * Third case: user is opening a Port Monitor
1769 * access checks same as opening a handle to the print server.
1772 switch (Printer->printer_type )
1774 case SPLHND_SERVER:
1775 case SPLHND_PORTMON_TCP:
1776 case SPLHND_PORTMON_LOCAL:
1777 /* Printserver handles use global struct... */
1779 snum = -1;
1781 /* Map standard access rights to object specific access rights */
1783 se_map_standard(&r->in.access_mask,
1784 &printserver_std_mapping);
1786 /* Deny any object specific bits that don't apply to print
1787 servers (i.e printer and job specific bits) */
1789 r->in.access_mask &= SEC_MASK_SPECIFIC;
1791 if (r->in.access_mask &
1792 ~(SERVER_ACCESS_ADMINISTER | SERVER_ACCESS_ENUMERATE)) {
1793 DEBUG(3, ("access DENIED for non-printserver bits\n"));
1794 close_printer_handle(p, r->out.handle);
1795 ZERO_STRUCTP(r->out.handle);
1796 return WERR_ACCESS_DENIED;
1799 /* Allow admin access */
1801 if ( r->in.access_mask & SERVER_ACCESS_ADMINISTER )
1803 if (!lp_ms_add_printer_wizard()) {
1804 close_printer_handle(p, r->out.handle);
1805 ZERO_STRUCTP(r->out.handle);
1806 return WERR_ACCESS_DENIED;
1809 /* if the user is not root, doesn't have SE_PRINT_OPERATOR privilege,
1810 and not a printer admin, then fail */
1812 if ((p->session_info->unix_token->uid != sec_initial_uid()) &&
1813 !security_token_has_privilege(p->session_info->security_token, SEC_PRIV_PRINT_OPERATOR) &&
1814 !nt_token_check_sid(&global_sid_Builtin_Print_Operators,
1815 p->session_info->security_token)) {
1816 close_printer_handle(p, r->out.handle);
1817 ZERO_STRUCTP(r->out.handle);
1818 DEBUG(3,("access DENIED as user is not root, "
1819 "has no printoperator privilege, "
1820 "not a member of the printoperator builtin group and "
1821 "is not in printer admin list"));
1822 return WERR_ACCESS_DENIED;
1825 r->in.access_mask = SERVER_ACCESS_ADMINISTER;
1827 else
1829 r->in.access_mask = SERVER_ACCESS_ENUMERATE;
1832 DEBUG(4,("Setting print server access = %s\n", (r->in.access_mask == SERVER_ACCESS_ADMINISTER)
1833 ? "SERVER_ACCESS_ADMINISTER" : "SERVER_ACCESS_ENUMERATE" ));
1835 /* We fall through to return WERR_OK */
1836 break;
1838 case SPLHND_PRINTER:
1839 /* NT doesn't let us connect to a printer if the connecting user
1840 doesn't have print permission. */
1842 if (!get_printer_snum(p, r->out.handle, &snum, NULL)) {
1843 close_printer_handle(p, r->out.handle);
1844 ZERO_STRUCTP(r->out.handle);
1845 return WERR_BADFID;
1848 if (r->in.access_mask == SEC_FLAG_MAXIMUM_ALLOWED) {
1849 r->in.access_mask = PRINTER_ACCESS_ADMINISTER;
1852 se_map_standard(&r->in.access_mask, &printer_std_mapping);
1854 /* map an empty access mask to the minimum access mask */
1855 if (r->in.access_mask == 0x0)
1856 r->in.access_mask = PRINTER_ACCESS_USE;
1859 * If we are not serving the printer driver for this printer,
1860 * map PRINTER_ACCESS_ADMINISTER to PRINTER_ACCESS_USE. This
1861 * will keep NT clients happy --jerry
1864 if (lp_use_client_driver(snum)
1865 && (r->in.access_mask & PRINTER_ACCESS_ADMINISTER))
1867 r->in.access_mask = PRINTER_ACCESS_USE;
1870 /* check smb.conf parameters and the the sec_desc */
1871 raddr = tsocket_address_inet_addr_string(p->remote_address,
1872 p->mem_ctx);
1873 if (raddr == NULL) {
1874 return WERR_NOMEM;
1877 rc = get_remote_hostname(p->remote_address,
1878 &rhost,
1879 p->mem_ctx);
1880 if (rc < 0) {
1881 return WERR_NOMEM;
1883 if (strequal(rhost, "UNKNOWN")) {
1884 rhost = raddr;
1887 if (!allow_access(lp_hostsdeny(snum), lp_hostsallow(snum),
1888 rhost, raddr)) {
1889 DEBUG(3, ("access DENIED (hosts allow/deny) for printer open\n"));
1890 ZERO_STRUCTP(r->out.handle);
1891 return WERR_ACCESS_DENIED;
1894 if (!user_ok_token(uidtoname(p->session_info->unix_token->uid), NULL,
1895 p->session_info->security_token, snum) ||
1896 !print_access_check(p->session_info,
1897 p->msg_ctx,
1898 snum,
1899 r->in.access_mask)) {
1900 DEBUG(3, ("access DENIED for printer open\n"));
1901 close_printer_handle(p, r->out.handle);
1902 ZERO_STRUCTP(r->out.handle);
1903 return WERR_ACCESS_DENIED;
1906 if ((r->in.access_mask & SEC_MASK_SPECIFIC)& ~(PRINTER_ACCESS_ADMINISTER|PRINTER_ACCESS_USE)) {
1907 DEBUG(3, ("access DENIED for printer open - unknown bits\n"));
1908 close_printer_handle(p, r->out.handle);
1909 ZERO_STRUCTP(r->out.handle);
1910 return WERR_ACCESS_DENIED;
1913 if (r->in.access_mask & PRINTER_ACCESS_ADMINISTER)
1914 r->in.access_mask = PRINTER_ACCESS_ADMINISTER;
1915 else
1916 r->in.access_mask = PRINTER_ACCESS_USE;
1918 DEBUG(4,("Setting printer access = %s\n", (r->in.access_mask == PRINTER_ACCESS_ADMINISTER)
1919 ? "PRINTER_ACCESS_ADMINISTER" : "PRINTER_ACCESS_USE" ));
1921 winreg_create_printer_internal(p->mem_ctx,
1922 get_session_info_system(),
1923 p->msg_ctx,
1924 lp_const_servicename(snum));
1926 break;
1928 default:
1929 /* sanity check to prevent programmer error */
1930 ZERO_STRUCTP(r->out.handle);
1931 return WERR_BADFID;
1934 Printer->access_granted = r->in.access_mask;
1937 * If the client sent a devmode in the OpenPrinter() call, then
1938 * save it here in case we get a job submission on this handle
1941 if ((Printer->printer_type != SPLHND_SERVER)
1942 && (r->in.devmode_ctr.devmode != NULL)) {
1943 copy_devicemode(NULL, r->in.devmode_ctr.devmode,
1944 &Printer->devmode);
1947 return WERR_OK;
1950 /****************************************************************
1951 _spoolss_ClosePrinter
1952 ****************************************************************/
1954 WERROR _spoolss_ClosePrinter(struct pipes_struct *p,
1955 struct spoolss_ClosePrinter *r)
1957 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
1959 if (Printer && Printer->document_started) {
1960 struct spoolss_EndDocPrinter e;
1962 e.in.handle = r->in.handle;
1964 _spoolss_EndDocPrinter(p, &e);
1967 if (!close_printer_handle(p, r->in.handle))
1968 return WERR_BADFID;
1970 /* clear the returned printer handle. Observed behavior
1971 from Win2k server. Don't think this really matters.
1972 Previous code just copied the value of the closed
1973 handle. --jerry */
1975 ZERO_STRUCTP(r->out.handle);
1977 return WERR_OK;
1980 /****************************************************************
1981 _spoolss_DeletePrinter
1982 ****************************************************************/
1984 WERROR _spoolss_DeletePrinter(struct pipes_struct *p,
1985 struct spoolss_DeletePrinter *r)
1987 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
1988 WERROR result;
1989 int snum;
1991 if (Printer && Printer->document_started) {
1992 struct spoolss_EndDocPrinter e;
1994 e.in.handle = r->in.handle;
1996 _spoolss_EndDocPrinter(p, &e);
1999 if (get_printer_snum(p, r->in.handle, &snum, NULL)) {
2000 winreg_delete_printer_key_internal(p->mem_ctx,
2001 get_session_info_system(),
2002 p->msg_ctx,
2003 lp_const_servicename(snum),
2004 "");
2007 result = delete_printer_handle(p, r->in.handle);
2009 return result;
2012 /*******************************************************************
2013 * static function to lookup the version id corresponding to an
2014 * long architecture string
2015 ******************************************************************/
2017 static const struct print_architecture_table_node archi_table[]= {
2019 {"Windows 4.0", SPL_ARCH_WIN40, 0 },
2020 {"Windows NT x86", SPL_ARCH_W32X86, 2 },
2021 {"Windows NT R4000", SPL_ARCH_W32MIPS, 2 },
2022 {"Windows NT Alpha_AXP", SPL_ARCH_W32ALPHA, 2 },
2023 {"Windows NT PowerPC", SPL_ARCH_W32PPC, 2 },
2024 {"Windows IA64", SPL_ARCH_IA64, 3 },
2025 {"Windows x64", SPL_ARCH_X64, 3 },
2026 {NULL, "", -1 }
2029 static const int drv_cversion[] = {SPOOLSS_DRIVER_VERSION_9X,
2030 SPOOLSS_DRIVER_VERSION_NT35,
2031 SPOOLSS_DRIVER_VERSION_NT4,
2032 SPOOLSS_DRIVER_VERSION_200X,
2033 -1};
2035 static int get_version_id(const char *arch)
2037 int i;
2039 for (i=0; archi_table[i].long_archi != NULL; i++)
2041 if (strcmp(arch, archi_table[i].long_archi) == 0)
2042 return (archi_table[i].version);
2045 return -1;
2048 /****************************************************************
2049 _spoolss_DeletePrinterDriver
2050 ****************************************************************/
2052 WERROR _spoolss_DeletePrinterDriver(struct pipes_struct *p,
2053 struct spoolss_DeletePrinterDriver *r)
2056 struct spoolss_DriverInfo8 *info = NULL;
2057 int version;
2058 WERROR status;
2059 struct dcerpc_binding_handle *b;
2060 TALLOC_CTX *tmp_ctx = NULL;
2061 int i;
2062 bool found;
2064 /* if the user is not root, doesn't have SE_PRINT_OPERATOR privilege,
2065 and not a printer admin, then fail */
2067 if ((p->session_info->unix_token->uid != sec_initial_uid()) &&
2068 !security_token_has_privilege(p->session_info->security_token,
2069 SEC_PRIV_PRINT_OPERATOR)) {
2070 return WERR_ACCESS_DENIED;
2073 /* check that we have a valid driver name first */
2075 if ((version = get_version_id(r->in.architecture)) == -1) {
2076 return WERR_INVALID_ENVIRONMENT;
2079 tmp_ctx = talloc_new(p->mem_ctx);
2080 if (!tmp_ctx) {
2081 return WERR_NOMEM;
2084 status = winreg_printer_binding_handle(tmp_ctx,
2085 get_session_info_system(),
2086 p->msg_ctx,
2087 &b);
2088 if (!W_ERROR_IS_OK(status)) {
2089 goto done;
2092 for (found = false, i = 0; drv_cversion[i] >= 0; i++) {
2093 status = winreg_get_driver(tmp_ctx, b,
2094 r->in.architecture, r->in.driver,
2095 drv_cversion[i], &info);
2096 if (!W_ERROR_IS_OK(status)) {
2097 DEBUG(5, ("skipping del of driver with version %d\n",
2098 drv_cversion[i]));
2099 continue;
2101 found = true;
2103 if (printer_driver_in_use(tmp_ctx, b, info)) {
2104 status = WERR_PRINTER_DRIVER_IN_USE;
2105 goto done;
2108 status = winreg_del_driver(tmp_ctx, b, info, drv_cversion[i]);
2109 if (!W_ERROR_IS_OK(status)) {
2110 DEBUG(0, ("failed del of driver with version %d\n",
2111 drv_cversion[i]));
2112 goto done;
2115 if (found == false) {
2116 DEBUG(0, ("driver %s not found for deletion\n", r->in.driver));
2117 status = WERR_UNKNOWN_PRINTER_DRIVER;
2118 } else {
2119 status = WERR_OK;
2122 done:
2123 talloc_free(tmp_ctx);
2125 return status;
2128 static WERROR spoolss_dpd_version(TALLOC_CTX *mem_ctx,
2129 struct pipes_struct *p,
2130 struct spoolss_DeletePrinterDriverEx *r,
2131 struct dcerpc_binding_handle *b,
2132 struct spoolss_DriverInfo8 *info)
2134 WERROR status;
2135 bool delete_files;
2137 if (printer_driver_in_use(mem_ctx, b, info)) {
2138 status = WERR_PRINTER_DRIVER_IN_USE;
2139 goto done;
2143 * we have a couple of cases to consider.
2144 * (1) Are any files in use? If so and DPD_DELETE_ALL_FILES is set,
2145 * then the delete should fail if **any** files overlap with
2146 * other drivers
2147 * (2) If DPD_DELETE_UNUSED_FILES is set, then delete all
2148 * non-overlapping files
2149 * (3) If neither DPD_DELETE_ALL_FILES nor DPD_DELETE_UNUSED_FILES
2150 * are set, then do not delete any files
2151 * Refer to MSDN docs on DeletePrinterDriverEx() for details.
2154 delete_files = r->in.delete_flags
2155 & (DPD_DELETE_ALL_FILES | DPD_DELETE_UNUSED_FILES);
2158 if (delete_files) {
2159 bool in_use = printer_driver_files_in_use(mem_ctx, b, info);
2160 if (in_use && (r->in.delete_flags & DPD_DELETE_ALL_FILES)) {
2161 status = WERR_PRINTER_DRIVER_IN_USE;
2162 goto done;
2165 * printer_driver_files_in_use() has trimmed overlapping files
2166 * from info so they are not removed on DPD_DELETE_UNUSED_FILES
2171 status = winreg_del_driver(mem_ctx, b, info, info->version);
2172 if (!W_ERROR_IS_OK(status)) {
2173 goto done;
2177 * now delete any associated files if delete_files is
2178 * true. Even if this part failes, we return succes
2179 * because the driver doesn not exist any more
2181 if (delete_files) {
2182 delete_driver_files(p->session_info, info);
2185 done:
2186 return status;
2189 /****************************************************************
2190 _spoolss_DeletePrinterDriverEx
2191 ****************************************************************/
2193 WERROR _spoolss_DeletePrinterDriverEx(struct pipes_struct *p,
2194 struct spoolss_DeletePrinterDriverEx *r)
2196 struct spoolss_DriverInfo8 *info = NULL;
2197 WERROR status;
2198 struct dcerpc_binding_handle *b;
2199 TALLOC_CTX *tmp_ctx = NULL;
2200 int i;
2201 bool found;
2203 /* if the user is not root, doesn't have SE_PRINT_OPERATOR privilege,
2204 and not a printer admin, then fail */
2206 if ((p->session_info->unix_token->uid != sec_initial_uid()) &&
2207 !security_token_has_privilege(p->session_info->security_token,
2208 SEC_PRIV_PRINT_OPERATOR)) {
2209 return WERR_ACCESS_DENIED;
2212 /* check that we have a valid driver name first */
2213 if (get_version_id(r->in.architecture) == -1) {
2214 /* this is what NT returns */
2215 return WERR_INVALID_ENVIRONMENT;
2218 tmp_ctx = talloc_new(p->mem_ctx);
2219 if (!tmp_ctx) {
2220 return WERR_NOMEM;
2223 status = winreg_printer_binding_handle(tmp_ctx,
2224 get_session_info_system(),
2225 p->msg_ctx,
2226 &b);
2227 if (!W_ERROR_IS_OK(status)) {
2228 goto done;
2231 for (found = false, i = 0; drv_cversion[i] >= 0; i++) {
2232 if ((r->in.delete_flags & DPD_DELETE_SPECIFIC_VERSION)
2233 && (drv_cversion[i] != r->in.version)) {
2234 continue;
2237 /* check if a driver with this version exists before delete */
2238 status = winreg_get_driver(tmp_ctx, b,
2239 r->in.architecture, r->in.driver,
2240 drv_cversion[i], &info);
2241 if (!W_ERROR_IS_OK(status)) {
2242 DEBUG(5, ("skipping del of driver with version %d\n",
2243 drv_cversion[i]));
2244 continue;
2246 found = true;
2248 status = spoolss_dpd_version(tmp_ctx, p, r, b, info);
2249 if (!W_ERROR_IS_OK(status)) {
2250 DEBUG(0, ("failed to delete driver with version %d\n",
2251 drv_cversion[i]));
2252 goto done;
2255 if (found == false) {
2256 DEBUG(0, ("driver %s not found for deletion\n", r->in.driver));
2257 status = WERR_UNKNOWN_PRINTER_DRIVER;
2258 } else {
2259 status = WERR_OK;
2262 done:
2263 talloc_free(tmp_ctx);
2264 return status;
2268 /********************************************************************
2269 GetPrinterData on a printer server Handle.
2270 ********************************************************************/
2272 static WERROR getprinterdata_printer_server(TALLOC_CTX *mem_ctx,
2273 const char *value,
2274 enum winreg_Type *type,
2275 union spoolss_PrinterData *data)
2277 DEBUG(8,("getprinterdata_printer_server:%s\n", value));
2279 if (!strcasecmp_m(value, "W3SvcInstalled")) {
2280 *type = REG_DWORD;
2281 SIVAL(&data->value, 0, 0x00);
2282 return WERR_OK;
2285 if (!strcasecmp_m(value, "BeepEnabled")) {
2286 *type = REG_DWORD;
2287 SIVAL(&data->value, 0, 0x00);
2288 return WERR_OK;
2291 if (!strcasecmp_m(value, "EventLog")) {
2292 *type = REG_DWORD;
2293 /* formally was 0x1b */
2294 SIVAL(&data->value, 0, 0x00);
2295 return WERR_OK;
2298 if (!strcasecmp_m(value, "NetPopup")) {
2299 *type = REG_DWORD;
2300 SIVAL(&data->value, 0, 0x00);
2301 return WERR_OK;
2304 if (!strcasecmp_m(value, "MajorVersion")) {
2305 *type = REG_DWORD;
2307 /* Windows NT 4.0 seems to not allow uploading of drivers
2308 to a server that reports 0x3 as the MajorVersion.
2309 need to investigate more how Win2k gets around this .
2310 -- jerry */
2312 if (RA_WINNT == get_remote_arch()) {
2313 SIVAL(&data->value, 0, 0x02);
2314 } else {
2315 SIVAL(&data->value, 0, 0x03);
2318 return WERR_OK;
2321 if (!strcasecmp_m(value, "MinorVersion")) {
2322 *type = REG_DWORD;
2323 SIVAL(&data->value, 0, 0x00);
2324 return WERR_OK;
2327 /* REG_BINARY
2328 * uint32_t size = 0x114
2329 * uint32_t major = 5
2330 * uint32_t minor = [0|1]
2331 * uint32_t build = [2195|2600]
2332 * extra unicode string = e.g. "Service Pack 3"
2334 if (!strcasecmp_m(value, "OSVersion")) {
2335 DATA_BLOB blob;
2336 enum ndr_err_code ndr_err;
2337 struct spoolss_OSVersion os;
2339 os.major = 5; /* Windows 2000 == 5.0 */
2340 os.minor = 0;
2341 os.build = 2195; /* build */
2342 os.extra_string = ""; /* leave extra string empty */
2344 ndr_err = ndr_push_struct_blob(&blob, mem_ctx, &os,
2345 (ndr_push_flags_fn_t)ndr_push_spoolss_OSVersion);
2346 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2347 return WERR_GENERAL_FAILURE;
2350 *type = REG_BINARY;
2351 data->binary = blob;
2353 return WERR_OK;
2357 if (!strcasecmp_m(value, "DefaultSpoolDirectory")) {
2358 *type = REG_SZ;
2360 data->string = talloc_strdup(mem_ctx, "C:\\PRINTERS");
2361 W_ERROR_HAVE_NO_MEMORY(data->string);
2363 return WERR_OK;
2366 if (!strcasecmp_m(value, "Architecture")) {
2367 *type = REG_SZ;
2368 data->string = talloc_strdup(mem_ctx,
2369 lp_parm_const_string(GLOBAL_SECTION_SNUM, "spoolss", "architecture", SPOOLSS_ARCHITECTURE_NT_X86));
2370 W_ERROR_HAVE_NO_MEMORY(data->string);
2372 return WERR_OK;
2375 if (!strcasecmp_m(value, "DsPresent")) {
2376 *type = REG_DWORD;
2378 /* only show the publish check box if we are a
2379 member of a AD domain */
2381 if (lp_security() == SEC_ADS) {
2382 SIVAL(&data->value, 0, 0x01);
2383 } else {
2384 SIVAL(&data->value, 0, 0x00);
2386 return WERR_OK;
2389 if (!strcasecmp_m(value, "DNSMachineName")) {
2390 const char *hostname = get_mydnsfullname();
2392 if (!hostname) {
2393 return WERR_BADFILE;
2396 *type = REG_SZ;
2397 data->string = talloc_strdup(mem_ctx, hostname);
2398 W_ERROR_HAVE_NO_MEMORY(data->string);
2400 return WERR_OK;
2403 *type = REG_NONE;
2405 return WERR_INVALID_PARAM;
2408 /****************************************************************
2409 _spoolss_GetPrinterData
2410 ****************************************************************/
2412 WERROR _spoolss_GetPrinterData(struct pipes_struct *p,
2413 struct spoolss_GetPrinterData *r)
2415 struct spoolss_GetPrinterDataEx r2;
2417 r2.in.handle = r->in.handle;
2418 r2.in.key_name = "PrinterDriverData";
2419 r2.in.value_name = r->in.value_name;
2420 r2.in.offered = r->in.offered;
2421 r2.out.type = r->out.type;
2422 r2.out.data = r->out.data;
2423 r2.out.needed = r->out.needed;
2425 return _spoolss_GetPrinterDataEx(p, &r2);
2428 /*********************************************************
2429 Connect to the client machine.
2430 **********************************************************/
2432 static bool spoolss_connect_to_client(struct rpc_pipe_client **pp_pipe,
2433 struct sockaddr_storage *client_ss, const char *remote_machine)
2435 NTSTATUS ret;
2436 struct cli_state *the_cli;
2437 struct sockaddr_storage rm_addr;
2438 char addr[INET6_ADDRSTRLEN];
2440 if ( is_zero_addr(client_ss) ) {
2441 DEBUG(2,("spoolss_connect_to_client: resolving %s\n",
2442 remote_machine));
2443 if ( !resolve_name( remote_machine, &rm_addr, 0x20, false) ) {
2444 DEBUG(2,("spoolss_connect_to_client: Can't resolve address for %s\n", remote_machine));
2445 return false;
2447 print_sockaddr(addr, sizeof(addr), &rm_addr);
2448 } else {
2449 rm_addr = *client_ss;
2450 print_sockaddr(addr, sizeof(addr), &rm_addr);
2451 DEBUG(5,("spoolss_connect_to_client: Using address %s (no name resolution necessary)\n",
2452 addr));
2455 if (ismyaddr((struct sockaddr *)(void *)&rm_addr)) {
2456 DEBUG(0,("spoolss_connect_to_client: Machine %s is one of our addresses. Cannot add to ourselves.\n",
2457 addr));
2458 return false;
2461 /* setup the connection */
2462 ret = cli_full_connection( &the_cli, lp_netbios_name(), remote_machine,
2463 &rm_addr, 0, "IPC$", "IPC",
2464 "", /* username */
2465 "", /* domain */
2466 "", /* password */
2467 0, lp_client_signing());
2469 if ( !NT_STATUS_IS_OK( ret ) ) {
2470 DEBUG(2,("spoolss_connect_to_client: connection to [%s] failed!\n",
2471 remote_machine ));
2472 return false;
2475 if ( smbXcli_conn_protocol(the_cli->conn) != PROTOCOL_NT1 ) {
2476 DEBUG(0,("spoolss_connect_to_client: machine %s didn't negotiate NT protocol.\n", remote_machine));
2477 cli_shutdown(the_cli);
2478 return false;
2482 * Ok - we have an anonymous connection to the IPC$ share.
2483 * Now start the NT Domain stuff :-).
2486 ret = cli_rpc_pipe_open_noauth(the_cli, &ndr_table_spoolss.syntax_id, pp_pipe);
2487 if (!NT_STATUS_IS_OK(ret)) {
2488 DEBUG(2,("spoolss_connect_to_client: unable to open the spoolss pipe on machine %s. Error was : %s.\n",
2489 remote_machine, nt_errstr(ret)));
2490 cli_shutdown(the_cli);
2491 return false;
2494 return true;
2497 /***************************************************************************
2498 Connect to the client.
2499 ****************************************************************************/
2501 static bool srv_spoolss_replyopenprinter(int snum, const char *printer,
2502 uint32_t localprinter,
2503 enum winreg_Type type,
2504 struct policy_handle *handle,
2505 struct notify_back_channel **_chan,
2506 struct sockaddr_storage *client_ss,
2507 struct messaging_context *msg_ctx)
2509 WERROR result;
2510 NTSTATUS status;
2511 struct notify_back_channel *chan;
2513 for (chan = back_channels; chan; chan = chan->next) {
2514 if (memcmp(&chan->client_address, client_ss,
2515 sizeof(struct sockaddr_storage)) == 0) {
2516 break;
2521 * If it's the first connection, contact the client
2522 * and connect to the IPC$ share anonymously
2524 if (!chan) {
2525 fstring unix_printer;
2527 /* the +2 is to strip the leading 2 backslashs */
2528 fstrcpy(unix_printer, printer + 2);
2530 chan = talloc_zero(NULL, struct notify_back_channel);
2531 if (!chan) {
2532 return false;
2534 chan->client_address = *client_ss;
2536 if (!spoolss_connect_to_client(&chan->cli_pipe, client_ss, unix_printer)) {
2537 TALLOC_FREE(chan);
2538 return false;
2541 DLIST_ADD(back_channels, chan);
2543 messaging_register(msg_ctx, NULL, MSG_PRINTER_NOTIFY2,
2544 receive_notify2_message_list);
2547 if (chan->cli_pipe == NULL ||
2548 chan->cli_pipe->binding_handle == NULL) {
2549 DEBUG(0, ("srv_spoolss_replyopenprinter: error - "
2550 "NULL %s for printer %s\n",
2551 chan->cli_pipe == NULL ?
2552 "chan->cli_pipe" : "chan->cli_pipe->binding_handle",
2553 printer));
2554 return false;
2558 * Tell the specific printing tdb we want messages for this printer
2559 * by registering our PID.
2562 if (!print_notify_register_pid(snum)) {
2563 DEBUG(0, ("Failed to register our pid for printer %s\n",
2564 printer));
2567 status = dcerpc_spoolss_ReplyOpenPrinter(chan->cli_pipe->binding_handle,
2568 talloc_tos(),
2569 printer,
2570 localprinter,
2571 type,
2573 NULL,
2574 handle,
2575 &result);
2576 if (!NT_STATUS_IS_OK(status)) {
2577 DEBUG(5, ("dcerpc_spoolss_ReplyOpenPrinter returned [%s]\n", nt_errstr(status)));
2578 result = ntstatus_to_werror(status);
2579 } else if (!W_ERROR_IS_OK(result)) {
2580 DEBUG(5, ("ReplyOpenPrinter returned [%s]\n", win_errstr(result)));
2583 chan->active_connections++;
2584 *_chan = chan;
2586 return (W_ERROR_IS_OK(result));
2589 /****************************************************************
2590 ****************************************************************/
2592 static struct spoolss_NotifyOption *dup_spoolss_NotifyOption(TALLOC_CTX *mem_ctx,
2593 const struct spoolss_NotifyOption *r)
2595 struct spoolss_NotifyOption *option;
2596 uint32_t i,k;
2598 if (!r) {
2599 return NULL;
2602 option = talloc_zero(mem_ctx, struct spoolss_NotifyOption);
2603 if (!option) {
2604 return NULL;
2607 *option = *r;
2609 if (!option->count) {
2610 return option;
2613 option->types = talloc_zero_array(option,
2614 struct spoolss_NotifyOptionType, option->count);
2615 if (!option->types) {
2616 talloc_free(option);
2617 return NULL;
2620 for (i=0; i < option->count; i++) {
2621 option->types[i] = r->types[i];
2623 if (option->types[i].count) {
2624 option->types[i].fields = talloc_zero_array(option,
2625 union spoolss_Field, option->types[i].count);
2626 if (!option->types[i].fields) {
2627 talloc_free(option);
2628 return NULL;
2630 for (k=0; k<option->types[i].count; k++) {
2631 option->types[i].fields[k] =
2632 r->types[i].fields[k];
2637 return option;
2640 /****************************************************************
2641 * _spoolss_RemoteFindFirstPrinterChangeNotifyEx
2643 * before replying OK: status=0 a rpc call is made to the workstation
2644 * asking ReplyOpenPrinter
2646 * in fact ReplyOpenPrinter is the changenotify equivalent on the spoolss pipe
2647 * called from api_spoolss_rffpcnex
2648 ****************************************************************/
2650 WERROR _spoolss_RemoteFindFirstPrinterChangeNotifyEx(struct pipes_struct *p,
2651 struct spoolss_RemoteFindFirstPrinterChangeNotifyEx *r)
2653 int snum = -1;
2654 struct spoolss_NotifyOption *option = r->in.notify_options;
2655 struct sockaddr_storage client_ss;
2656 ssize_t client_len;
2658 /* store the notify value in the printer struct */
2660 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
2662 if (!Printer) {
2663 DEBUG(2,("_spoolss_RemoteFindFirstPrinterChangeNotifyEx: "
2664 "Invalid handle (%s:%u:%u).\n",
2665 OUR_HANDLE(r->in.handle)));
2666 return WERR_BADFID;
2669 Printer->notify.flags = r->in.flags;
2670 Printer->notify.options = r->in.options;
2671 Printer->notify.printerlocal = r->in.printer_local;
2672 Printer->notify.msg_ctx = p->msg_ctx;
2674 TALLOC_FREE(Printer->notify.option);
2675 Printer->notify.option = dup_spoolss_NotifyOption(Printer, option);
2677 fstrcpy(Printer->notify.localmachine, r->in.local_machine);
2679 /* Connect to the client machine and send a ReplyOpenPrinter */
2681 if ( Printer->printer_type == SPLHND_SERVER)
2682 snum = -1;
2683 else if ( (Printer->printer_type == SPLHND_PRINTER) &&
2684 !get_printer_snum(p, r->in.handle, &snum, NULL) )
2685 return WERR_BADFID;
2687 DEBUG(10,("_spoolss_RemoteFindFirstPrinterChangeNotifyEx: "
2688 "remote_address is %s\n",
2689 tsocket_address_string(p->remote_address, p->mem_ctx)));
2691 if (!lp_print_notify_backchannel(snum)) {
2692 DEBUG(10, ("_spoolss_RemoteFindFirstPrinterChangeNotifyEx: "
2693 "backchannel disabled\n"));
2694 return WERR_SERVER_UNAVAILABLE;
2697 client_len = tsocket_address_bsd_sockaddr(p->remote_address,
2698 (struct sockaddr *) &client_ss,
2699 sizeof(struct sockaddr_storage));
2700 if (client_len < 0) {
2701 return WERR_NOMEM;
2704 if(!srv_spoolss_replyopenprinter(snum, Printer->notify.localmachine,
2705 Printer->notify.printerlocal, REG_SZ,
2706 &Printer->notify.cli_hnd,
2707 &Printer->notify.cli_chan,
2708 &client_ss, p->msg_ctx)) {
2709 return WERR_SERVER_UNAVAILABLE;
2712 return WERR_OK;
2715 /*******************************************************************
2716 * fill a notify_info_data with the servername
2717 ********************************************************************/
2719 static void spoolss_notify_server_name(struct messaging_context *msg_ctx,
2720 int snum,
2721 struct spoolss_Notify *data,
2722 print_queue_struct *queue,
2723 struct spoolss_PrinterInfo2 *pinfo2,
2724 TALLOC_CTX *mem_ctx)
2726 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, pinfo2->servername);
2729 /*******************************************************************
2730 * fill a notify_info_data with the printername (not including the servername).
2731 ********************************************************************/
2733 static void spoolss_notify_printer_name(struct messaging_context *msg_ctx,
2734 int snum,
2735 struct spoolss_Notify *data,
2736 print_queue_struct *queue,
2737 struct spoolss_PrinterInfo2 *pinfo2,
2738 TALLOC_CTX *mem_ctx)
2740 /* the notify name should not contain the \\server\ part */
2741 const char *p = strrchr(pinfo2->printername, '\\');
2743 if (!p) {
2744 p = pinfo2->printername;
2745 } else {
2746 p++;
2749 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, p);
2752 /*******************************************************************
2753 * fill a notify_info_data with the servicename
2754 ********************************************************************/
2756 static void spoolss_notify_share_name(struct messaging_context *msg_ctx,
2757 int snum,
2758 struct spoolss_Notify *data,
2759 print_queue_struct *queue,
2760 struct spoolss_PrinterInfo2 *pinfo2,
2761 TALLOC_CTX *mem_ctx)
2763 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, lp_servicename(talloc_tos(), snum));
2766 /*******************************************************************
2767 * fill a notify_info_data with the port name
2768 ********************************************************************/
2770 static void spoolss_notify_port_name(struct messaging_context *msg_ctx,
2771 int snum,
2772 struct spoolss_Notify *data,
2773 print_queue_struct *queue,
2774 struct spoolss_PrinterInfo2 *pinfo2,
2775 TALLOC_CTX *mem_ctx)
2777 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, pinfo2->portname);
2780 /*******************************************************************
2781 * fill a notify_info_data with the printername
2782 * but it doesn't exist, have to see what to do
2783 ********************************************************************/
2785 static void spoolss_notify_driver_name(struct messaging_context *msg_ctx,
2786 int snum,
2787 struct spoolss_Notify *data,
2788 print_queue_struct *queue,
2789 struct spoolss_PrinterInfo2 *pinfo2,
2790 TALLOC_CTX *mem_ctx)
2792 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, pinfo2->drivername);
2795 /*******************************************************************
2796 * fill a notify_info_data with the comment
2797 ********************************************************************/
2799 static void spoolss_notify_comment(struct messaging_context *msg_ctx,
2800 int snum,
2801 struct spoolss_Notify *data,
2802 print_queue_struct *queue,
2803 struct spoolss_PrinterInfo2 *pinfo2,
2804 TALLOC_CTX *mem_ctx)
2806 const char *p;
2808 if (*pinfo2->comment == '\0') {
2809 p = lp_comment(talloc_tos(), snum);
2810 } else {
2811 p = pinfo2->comment;
2814 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, p);
2817 /*******************************************************************
2818 * fill a notify_info_data with the comment
2819 * location = "Room 1, floor 2, building 3"
2820 ********************************************************************/
2822 static void spoolss_notify_location(struct messaging_context *msg_ctx,
2823 int snum,
2824 struct spoolss_Notify *data,
2825 print_queue_struct *queue,
2826 struct spoolss_PrinterInfo2 *pinfo2,
2827 TALLOC_CTX *mem_ctx)
2829 const char *loc = pinfo2->location;
2830 NTSTATUS status;
2832 status = printer_list_get_printer(mem_ctx,
2833 pinfo2->sharename,
2834 NULL,
2835 &loc,
2836 NULL);
2837 if (NT_STATUS_IS_OK(status)) {
2838 if (loc == NULL) {
2839 loc = pinfo2->location;
2843 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, loc);
2846 /*******************************************************************
2847 * fill a notify_info_data with the device mode
2848 * jfm:xxxx don't to it for know but that's a real problem !!!
2849 ********************************************************************/
2851 static void spoolss_notify_devmode(struct messaging_context *msg_ctx,
2852 int snum,
2853 struct spoolss_Notify *data,
2854 print_queue_struct *queue,
2855 struct spoolss_PrinterInfo2 *pinfo2,
2856 TALLOC_CTX *mem_ctx)
2858 /* for a dummy implementation we have to zero the fields */
2859 SETUP_SPOOLSS_NOTIFY_DATA_DEVMODE(data, NULL);
2862 /*******************************************************************
2863 * fill a notify_info_data with the separator file name
2864 ********************************************************************/
2866 static void spoolss_notify_sepfile(struct messaging_context *msg_ctx,
2867 int snum,
2868 struct spoolss_Notify *data,
2869 print_queue_struct *queue,
2870 struct spoolss_PrinterInfo2 *pinfo2,
2871 TALLOC_CTX *mem_ctx)
2873 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, pinfo2->sepfile);
2876 /*******************************************************************
2877 * fill a notify_info_data with the print processor
2878 * jfm:xxxx return always winprint to indicate we don't do anything to it
2879 ********************************************************************/
2881 static void spoolss_notify_print_processor(struct messaging_context *msg_ctx,
2882 int snum,
2883 struct spoolss_Notify *data,
2884 print_queue_struct *queue,
2885 struct spoolss_PrinterInfo2 *pinfo2,
2886 TALLOC_CTX *mem_ctx)
2888 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, pinfo2->printprocessor);
2891 /*******************************************************************
2892 * fill a notify_info_data with the print processor options
2893 * jfm:xxxx send an empty string
2894 ********************************************************************/
2896 static void spoolss_notify_parameters(struct messaging_context *msg_ctx,
2897 int snum,
2898 struct spoolss_Notify *data,
2899 print_queue_struct *queue,
2900 struct spoolss_PrinterInfo2 *pinfo2,
2901 TALLOC_CTX *mem_ctx)
2903 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, pinfo2->parameters);
2906 /*******************************************************************
2907 * fill a notify_info_data with the data type
2908 * jfm:xxxx always send RAW as data type
2909 ********************************************************************/
2911 static void spoolss_notify_datatype(struct messaging_context *msg_ctx,
2912 int snum,
2913 struct spoolss_Notify *data,
2914 print_queue_struct *queue,
2915 struct spoolss_PrinterInfo2 *pinfo2,
2916 TALLOC_CTX *mem_ctx)
2918 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, pinfo2->datatype);
2921 /*******************************************************************
2922 * fill a notify_info_data with the security descriptor
2923 * jfm:xxxx send an null pointer to say no security desc
2924 * have to implement security before !
2925 ********************************************************************/
2927 static void spoolss_notify_security_desc(struct messaging_context *msg_ctx,
2928 int snum,
2929 struct spoolss_Notify *data,
2930 print_queue_struct *queue,
2931 struct spoolss_PrinterInfo2 *pinfo2,
2932 TALLOC_CTX *mem_ctx)
2934 SETUP_SPOOLSS_NOTIFY_DATA_SECDESC(data, pinfo2->secdesc);
2937 /*******************************************************************
2938 * fill a notify_info_data with the attributes
2939 * jfm:xxxx a samba printer is always shared
2940 ********************************************************************/
2942 static void spoolss_notify_attributes(struct messaging_context *msg_ctx,
2943 int snum,
2944 struct spoolss_Notify *data,
2945 print_queue_struct *queue,
2946 struct spoolss_PrinterInfo2 *pinfo2,
2947 TALLOC_CTX *mem_ctx)
2949 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, pinfo2->attributes);
2952 /*******************************************************************
2953 * fill a notify_info_data with the priority
2954 ********************************************************************/
2956 static void spoolss_notify_priority(struct messaging_context *msg_ctx,
2957 int snum,
2958 struct spoolss_Notify *data,
2959 print_queue_struct *queue,
2960 struct spoolss_PrinterInfo2 *pinfo2,
2961 TALLOC_CTX *mem_ctx)
2963 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, pinfo2->priority);
2966 /*******************************************************************
2967 * fill a notify_info_data with the default priority
2968 ********************************************************************/
2970 static void spoolss_notify_default_priority(struct messaging_context *msg_ctx,
2971 int snum,
2972 struct spoolss_Notify *data,
2973 print_queue_struct *queue,
2974 struct spoolss_PrinterInfo2 *pinfo2,
2975 TALLOC_CTX *mem_ctx)
2977 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, pinfo2->defaultpriority);
2980 /*******************************************************************
2981 * fill a notify_info_data with the start time
2982 ********************************************************************/
2984 static void spoolss_notify_start_time(struct messaging_context *msg_ctx,
2985 int snum,
2986 struct spoolss_Notify *data,
2987 print_queue_struct *queue,
2988 struct spoolss_PrinterInfo2 *pinfo2,
2989 TALLOC_CTX *mem_ctx)
2991 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, pinfo2->starttime);
2994 /*******************************************************************
2995 * fill a notify_info_data with the until time
2996 ********************************************************************/
2998 static void spoolss_notify_until_time(struct messaging_context *msg_ctx,
2999 int snum,
3000 struct spoolss_Notify *data,
3001 print_queue_struct *queue,
3002 struct spoolss_PrinterInfo2 *pinfo2,
3003 TALLOC_CTX *mem_ctx)
3005 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, pinfo2->untiltime);
3008 /*******************************************************************
3009 * fill a notify_info_data with the status
3010 ********************************************************************/
3012 static void spoolss_notify_status(struct messaging_context *msg_ctx,
3013 int snum,
3014 struct spoolss_Notify *data,
3015 print_queue_struct *queue,
3016 struct spoolss_PrinterInfo2 *pinfo2,
3017 TALLOC_CTX *mem_ctx)
3019 print_status_struct status;
3021 print_queue_length(msg_ctx, snum, &status);
3022 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, status.status);
3025 /*******************************************************************
3026 * fill a notify_info_data with the number of jobs queued
3027 ********************************************************************/
3029 static void spoolss_notify_cjobs(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(
3037 data, print_queue_length(msg_ctx, snum, NULL));
3040 /*******************************************************************
3041 * fill a notify_info_data with the average ppm
3042 ********************************************************************/
3044 static void spoolss_notify_average_ppm(struct messaging_context *msg_ctx,
3045 int snum,
3046 struct spoolss_Notify *data,
3047 print_queue_struct *queue,
3048 struct spoolss_PrinterInfo2 *pinfo2,
3049 TALLOC_CTX *mem_ctx)
3051 /* always respond 8 pages per minutes */
3052 /* a little hard ! */
3053 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, pinfo2->averageppm);
3056 /*******************************************************************
3057 * fill a notify_info_data with username
3058 ********************************************************************/
3060 static void spoolss_notify_username(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_STRING(data, queue->fs_user);
3070 /*******************************************************************
3071 * fill a notify_info_data with job status
3072 ********************************************************************/
3074 static void spoolss_notify_job_status(struct messaging_context *msg_ctx,
3075 int snum,
3076 struct spoolss_Notify *data,
3077 print_queue_struct *queue,
3078 struct spoolss_PrinterInfo2 *pinfo2,
3079 TALLOC_CTX *mem_ctx)
3081 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, nt_printj_status(queue->status));
3084 /*******************************************************************
3085 * fill a notify_info_data with job name
3086 ********************************************************************/
3088 static void spoolss_notify_job_name(struct messaging_context *msg_ctx,
3089 int snum,
3090 struct spoolss_Notify *data,
3091 print_queue_struct *queue,
3092 struct spoolss_PrinterInfo2 *pinfo2,
3093 TALLOC_CTX *mem_ctx)
3095 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, queue->fs_file);
3098 /*******************************************************************
3099 * fill a notify_info_data with job status
3100 ********************************************************************/
3102 static void spoolss_notify_job_status_string(struct messaging_context *msg_ctx,
3103 int snum,
3104 struct spoolss_Notify *data,
3105 print_queue_struct *queue,
3106 struct spoolss_PrinterInfo2 *pinfo2,
3107 TALLOC_CTX *mem_ctx)
3110 * Now we're returning job status codes we just return a "" here. JRA.
3113 const char *p = "";
3115 #if 0 /* NO LONGER NEEDED - JRA. 02/22/2001 */
3116 p = "unknown";
3118 switch (queue->status) {
3119 case LPQ_QUEUED:
3120 p = "Queued";
3121 break;
3122 case LPQ_PAUSED:
3123 p = ""; /* NT provides the paused string */
3124 break;
3125 case LPQ_SPOOLING:
3126 p = "Spooling";
3127 break;
3128 case LPQ_PRINTING:
3129 p = "Printing";
3130 break;
3132 #endif /* NO LONGER NEEDED. */
3134 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, p);
3137 /*******************************************************************
3138 * fill a notify_info_data with job time
3139 ********************************************************************/
3141 static void spoolss_notify_job_time(struct messaging_context *msg_ctx,
3142 int snum,
3143 struct spoolss_Notify *data,
3144 print_queue_struct *queue,
3145 struct spoolss_PrinterInfo2 *pinfo2,
3146 TALLOC_CTX *mem_ctx)
3148 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, 0);
3151 /*******************************************************************
3152 * fill a notify_info_data with job size
3153 ********************************************************************/
3155 static void spoolss_notify_job_size(struct messaging_context *msg_ctx,
3156 int snum,
3157 struct spoolss_Notify *data,
3158 print_queue_struct *queue,
3159 struct spoolss_PrinterInfo2 *pinfo2,
3160 TALLOC_CTX *mem_ctx)
3162 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, queue->size);
3165 /*******************************************************************
3166 * fill a notify_info_data with page info
3167 ********************************************************************/
3168 static void spoolss_notify_total_pages(struct messaging_context *msg_ctx,
3169 int snum,
3170 struct spoolss_Notify *data,
3171 print_queue_struct *queue,
3172 struct spoolss_PrinterInfo2 *pinfo2,
3173 TALLOC_CTX *mem_ctx)
3175 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, queue->page_count);
3178 /*******************************************************************
3179 * fill a notify_info_data with pages printed info.
3180 ********************************************************************/
3181 static void spoolss_notify_pages_printed(struct messaging_context *msg_ctx,
3182 int snum,
3183 struct spoolss_Notify *data,
3184 print_queue_struct *queue,
3185 struct spoolss_PrinterInfo2 *pinfo2,
3186 TALLOC_CTX *mem_ctx)
3188 /* Add code when back-end tracks this */
3189 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, 0);
3192 /*******************************************************************
3193 Fill a notify_info_data with job position.
3194 ********************************************************************/
3196 static void spoolss_notify_job_position(struct messaging_context *msg_ctx,
3197 int snum,
3198 struct spoolss_Notify *data,
3199 print_queue_struct *queue,
3200 struct spoolss_PrinterInfo2 *pinfo2,
3201 TALLOC_CTX *mem_ctx)
3203 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, queue->sysjob);
3206 /*******************************************************************
3207 Fill a notify_info_data with submitted time.
3208 ********************************************************************/
3210 static void spoolss_notify_submitted_time(struct messaging_context *msg_ctx,
3211 int snum,
3212 struct spoolss_Notify *data,
3213 print_queue_struct *queue,
3214 struct spoolss_PrinterInfo2 *pinfo2,
3215 TALLOC_CTX *mem_ctx)
3217 data->data.string.string = NULL;
3218 data->data.string.size = 0;
3220 init_systemtime_buffer(mem_ctx, gmtime(&queue->time),
3221 &data->data.string.string,
3222 &data->data.string.size);
3226 struct s_notify_info_data_table
3228 enum spoolss_NotifyType type;
3229 uint16_t field;
3230 const char *name;
3231 enum spoolss_NotifyTable variable_type;
3232 void (*fn) (struct messaging_context *msg_ctx,
3233 int snum, struct spoolss_Notify *data,
3234 print_queue_struct *queue,
3235 struct spoolss_PrinterInfo2 *pinfo2,
3236 TALLOC_CTX *mem_ctx);
3239 /* A table describing the various print notification constants and
3240 whether the notification data is a pointer to a variable sized
3241 buffer, a one value uint32_t or a two value uint32_t. */
3243 static const struct s_notify_info_data_table notify_info_data_table[] =
3245 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_SERVER_NAME, "PRINTER_NOTIFY_FIELD_SERVER_NAME", NOTIFY_TABLE_STRING, spoolss_notify_server_name },
3246 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_PRINTER_NAME, "PRINTER_NOTIFY_FIELD_PRINTER_NAME", NOTIFY_TABLE_STRING, spoolss_notify_printer_name },
3247 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_SHARE_NAME, "PRINTER_NOTIFY_FIELD_SHARE_NAME", NOTIFY_TABLE_STRING, spoolss_notify_share_name },
3248 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_PORT_NAME, "PRINTER_NOTIFY_FIELD_PORT_NAME", NOTIFY_TABLE_STRING, spoolss_notify_port_name },
3249 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_DRIVER_NAME, "PRINTER_NOTIFY_FIELD_DRIVER_NAME", NOTIFY_TABLE_STRING, spoolss_notify_driver_name },
3250 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_COMMENT, "PRINTER_NOTIFY_FIELD_COMMENT", NOTIFY_TABLE_STRING, spoolss_notify_comment },
3251 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_LOCATION, "PRINTER_NOTIFY_FIELD_LOCATION", NOTIFY_TABLE_STRING, spoolss_notify_location },
3252 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_DEVMODE, "PRINTER_NOTIFY_FIELD_DEVMODE", NOTIFY_TABLE_DEVMODE, spoolss_notify_devmode },
3253 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_SEPFILE, "PRINTER_NOTIFY_FIELD_SEPFILE", NOTIFY_TABLE_STRING, spoolss_notify_sepfile },
3254 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_PRINT_PROCESSOR, "PRINTER_NOTIFY_FIELD_PRINT_PROCESSOR", NOTIFY_TABLE_STRING, spoolss_notify_print_processor },
3255 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_PARAMETERS, "PRINTER_NOTIFY_FIELD_PARAMETERS", NOTIFY_TABLE_STRING, spoolss_notify_parameters },
3256 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_DATATYPE, "PRINTER_NOTIFY_FIELD_DATATYPE", NOTIFY_TABLE_STRING, spoolss_notify_datatype },
3257 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_SECURITY_DESCRIPTOR, "PRINTER_NOTIFY_FIELD_SECURITY_DESCRIPTOR", NOTIFY_TABLE_SECURITYDESCRIPTOR, spoolss_notify_security_desc },
3258 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_ATTRIBUTES, "PRINTER_NOTIFY_FIELD_ATTRIBUTES", NOTIFY_TABLE_DWORD, spoolss_notify_attributes },
3259 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_PRIORITY, "PRINTER_NOTIFY_FIELD_PRIORITY", NOTIFY_TABLE_DWORD, spoolss_notify_priority },
3260 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_DEFAULT_PRIORITY, "PRINTER_NOTIFY_FIELD_DEFAULT_PRIORITY", NOTIFY_TABLE_DWORD, spoolss_notify_default_priority },
3261 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_START_TIME, "PRINTER_NOTIFY_FIELD_START_TIME", NOTIFY_TABLE_DWORD, spoolss_notify_start_time },
3262 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_UNTIL_TIME, "PRINTER_NOTIFY_FIELD_UNTIL_TIME", NOTIFY_TABLE_DWORD, spoolss_notify_until_time },
3263 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_STATUS, "PRINTER_NOTIFY_FIELD_STATUS", NOTIFY_TABLE_DWORD, spoolss_notify_status },
3264 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_STATUS_STRING, "PRINTER_NOTIFY_FIELD_STATUS_STRING", NOTIFY_TABLE_STRING, NULL },
3265 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_CJOBS, "PRINTER_NOTIFY_FIELD_CJOBS", NOTIFY_TABLE_DWORD, spoolss_notify_cjobs },
3266 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_AVERAGE_PPM, "PRINTER_NOTIFY_FIELD_AVERAGE_PPM", NOTIFY_TABLE_DWORD, spoolss_notify_average_ppm },
3267 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_TOTAL_PAGES, "PRINTER_NOTIFY_FIELD_TOTAL_PAGES", NOTIFY_TABLE_DWORD, NULL },
3268 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_PAGES_PRINTED, "PRINTER_NOTIFY_FIELD_PAGES_PRINTED", NOTIFY_TABLE_DWORD, NULL },
3269 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_TOTAL_BYTES, "PRINTER_NOTIFY_FIELD_TOTAL_BYTES", NOTIFY_TABLE_DWORD, NULL },
3270 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_BYTES_PRINTED, "PRINTER_NOTIFY_FIELD_BYTES_PRINTED", NOTIFY_TABLE_DWORD, NULL },
3271 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_PRINTER_NAME, "JOB_NOTIFY_FIELD_PRINTER_NAME", NOTIFY_TABLE_STRING, spoolss_notify_printer_name },
3272 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_MACHINE_NAME, "JOB_NOTIFY_FIELD_MACHINE_NAME", NOTIFY_TABLE_STRING, spoolss_notify_server_name },
3273 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_PORT_NAME, "JOB_NOTIFY_FIELD_PORT_NAME", NOTIFY_TABLE_STRING, spoolss_notify_port_name },
3274 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_USER_NAME, "JOB_NOTIFY_FIELD_USER_NAME", NOTIFY_TABLE_STRING, spoolss_notify_username },
3275 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_NOTIFY_NAME, "JOB_NOTIFY_FIELD_NOTIFY_NAME", NOTIFY_TABLE_STRING, spoolss_notify_username },
3276 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_DATATYPE, "JOB_NOTIFY_FIELD_DATATYPE", NOTIFY_TABLE_STRING, spoolss_notify_datatype },
3277 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_PRINT_PROCESSOR, "JOB_NOTIFY_FIELD_PRINT_PROCESSOR", NOTIFY_TABLE_STRING, spoolss_notify_print_processor },
3278 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_PARAMETERS, "JOB_NOTIFY_FIELD_PARAMETERS", NOTIFY_TABLE_STRING, spoolss_notify_parameters },
3279 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_DRIVER_NAME, "JOB_NOTIFY_FIELD_DRIVER_NAME", NOTIFY_TABLE_STRING, spoolss_notify_driver_name },
3280 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_DEVMODE, "JOB_NOTIFY_FIELD_DEVMODE", NOTIFY_TABLE_DEVMODE, spoolss_notify_devmode },
3281 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_STATUS, "JOB_NOTIFY_FIELD_STATUS", NOTIFY_TABLE_DWORD, spoolss_notify_job_status },
3282 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_STATUS_STRING, "JOB_NOTIFY_FIELD_STATUS_STRING", NOTIFY_TABLE_STRING, spoolss_notify_job_status_string },
3283 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_SECURITY_DESCRIPTOR, "JOB_NOTIFY_FIELD_SECURITY_DESCRIPTOR", NOTIFY_TABLE_SECURITYDESCRIPTOR, NULL },
3284 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_DOCUMENT, "JOB_NOTIFY_FIELD_DOCUMENT", NOTIFY_TABLE_STRING, spoolss_notify_job_name },
3285 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_PRIORITY, "JOB_NOTIFY_FIELD_PRIORITY", NOTIFY_TABLE_DWORD, spoolss_notify_priority },
3286 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_POSITION, "JOB_NOTIFY_FIELD_POSITION", NOTIFY_TABLE_DWORD, spoolss_notify_job_position },
3287 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_SUBMITTED, "JOB_NOTIFY_FIELD_SUBMITTED", NOTIFY_TABLE_TIME, spoolss_notify_submitted_time },
3288 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_START_TIME, "JOB_NOTIFY_FIELD_START_TIME", NOTIFY_TABLE_DWORD, spoolss_notify_start_time },
3289 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_UNTIL_TIME, "JOB_NOTIFY_FIELD_UNTIL_TIME", NOTIFY_TABLE_DWORD, spoolss_notify_until_time },
3290 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_TIME, "JOB_NOTIFY_FIELD_TIME", NOTIFY_TABLE_DWORD, spoolss_notify_job_time },
3291 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_TOTAL_PAGES, "JOB_NOTIFY_FIELD_TOTAL_PAGES", NOTIFY_TABLE_DWORD, spoolss_notify_total_pages },
3292 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_PAGES_PRINTED, "JOB_NOTIFY_FIELD_PAGES_PRINTED", NOTIFY_TABLE_DWORD, spoolss_notify_pages_printed },
3293 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_TOTAL_BYTES, "JOB_NOTIFY_FIELD_TOTAL_BYTES", NOTIFY_TABLE_DWORD, spoolss_notify_job_size },
3296 /*******************************************************************
3297 Return the variable_type of info_data structure.
3298 ********************************************************************/
3300 static enum spoolss_NotifyTable variable_type_of_notify_info_data(enum spoolss_NotifyType type,
3301 uint16_t field)
3303 int i=0;
3305 for (i = 0; i < ARRAY_SIZE(notify_info_data_table); i++) {
3306 if ( (notify_info_data_table[i].type == type) &&
3307 (notify_info_data_table[i].field == field) ) {
3308 return notify_info_data_table[i].variable_type;
3312 DEBUG(5, ("invalid notify data type %d/%d\n", type, field));
3314 return (enum spoolss_NotifyTable) 0;
3317 /****************************************************************************
3318 ****************************************************************************/
3320 static bool search_notify(enum spoolss_NotifyType type,
3321 uint16_t field,
3322 int *value)
3324 int i;
3326 for (i = 0; i < ARRAY_SIZE(notify_info_data_table); i++) {
3327 if (notify_info_data_table[i].type == type &&
3328 notify_info_data_table[i].field == field &&
3329 notify_info_data_table[i].fn != NULL) {
3330 *value = i;
3331 return true;
3335 return false;
3338 /****************************************************************************
3339 ****************************************************************************/
3341 static void construct_info_data(struct spoolss_Notify *info_data,
3342 enum spoolss_NotifyType type,
3343 uint16_t field, int id)
3345 info_data->type = type;
3346 info_data->field.field = field;
3347 info_data->variable_type = variable_type_of_notify_info_data(type, field);
3348 info_data->job_id = id;
3351 /*******************************************************************
3353 * fill a notify_info struct with info asked
3355 ********************************************************************/
3357 static bool construct_notify_printer_info(struct messaging_context *msg_ctx,
3358 struct printer_handle *print_hnd,
3359 struct spoolss_NotifyInfo *info,
3360 struct spoolss_PrinterInfo2 *pinfo2,
3361 int snum,
3362 const struct spoolss_NotifyOptionType *option_type,
3363 uint32_t id,
3364 TALLOC_CTX *mem_ctx)
3366 int field_num,j;
3367 enum spoolss_NotifyType type;
3368 uint16_t field;
3370 struct spoolss_Notify *current_data;
3372 type = option_type->type;
3374 DEBUG(4,("construct_notify_printer_info: Notify type: [%s], number of notify info: [%d] on printer: [%s]\n",
3375 (type == PRINTER_NOTIFY_TYPE ? "PRINTER_NOTIFY_TYPE" : "JOB_NOTIFY_TYPE"),
3376 option_type->count, lp_servicename(talloc_tos(), snum)));
3378 for(field_num=0; field_num < option_type->count; field_num++) {
3379 field = option_type->fields[field_num].field;
3381 DEBUG(4,("construct_notify_printer_info: notify [%d]: type [%x], field [%x]\n", field_num, type, field));
3383 if (!search_notify(type, field, &j) )
3384 continue;
3386 info->notifies = talloc_realloc(info, info->notifies,
3387 struct spoolss_Notify,
3388 info->count + 1);
3389 if (info->notifies == NULL) {
3390 DEBUG(2,("construct_notify_printer_info: failed to enlarge buffer info->data!\n"));
3391 return false;
3394 current_data = &info->notifies[info->count];
3396 construct_info_data(current_data, type, field, id);
3398 DEBUG(10, ("construct_notify_printer_info: "
3399 "calling [%s] snum=%d printername=[%s])\n",
3400 notify_info_data_table[j].name, snum,
3401 pinfo2->printername));
3403 notify_info_data_table[j].fn(msg_ctx, snum, current_data,
3404 NULL, pinfo2, mem_ctx);
3406 info->count++;
3409 return true;
3412 /*******************************************************************
3414 * fill a notify_info struct with info asked
3416 ********************************************************************/
3418 static bool construct_notify_jobs_info(struct messaging_context *msg_ctx,
3419 print_queue_struct *queue,
3420 struct spoolss_NotifyInfo *info,
3421 struct spoolss_PrinterInfo2 *pinfo2,
3422 int snum,
3423 const struct spoolss_NotifyOptionType *option_type,
3424 uint32_t id,
3425 TALLOC_CTX *mem_ctx)
3427 int field_num,j;
3428 enum spoolss_NotifyType type;
3429 uint16_t field;
3430 struct spoolss_Notify *current_data;
3432 DEBUG(4,("construct_notify_jobs_info\n"));
3434 type = option_type->type;
3436 DEBUGADD(4,("Notify type: [%s], number of notify info: [%d]\n",
3437 (type == PRINTER_NOTIFY_TYPE ? "PRINTER_NOTIFY_TYPE" : "JOB_NOTIFY_TYPE"),
3438 option_type->count));
3440 for(field_num=0; field_num<option_type->count; field_num++) {
3441 field = option_type->fields[field_num].field;
3443 if (!search_notify(type, field, &j) )
3444 continue;
3446 info->notifies = talloc_realloc(info, info->notifies,
3447 struct spoolss_Notify,
3448 info->count + 1);
3449 if (info->notifies == NULL) {
3450 DEBUG(2,("construct_notify_jobs_info: failed to enlarg buffer info->data!\n"));
3451 return false;
3454 current_data=&(info->notifies[info->count]);
3456 construct_info_data(current_data, type, field, id);
3457 notify_info_data_table[j].fn(msg_ctx, snum, current_data,
3458 queue, pinfo2, mem_ctx);
3459 info->count++;
3462 return true;
3466 * JFM: The enumeration is not that simple, it's even non obvious.
3468 * let's take an example: I want to monitor the PRINTER SERVER for
3469 * the printer's name and the number of jobs currently queued.
3470 * So in the NOTIFY_OPTION, I have one NOTIFY_OPTION_TYPE structure.
3471 * Its type is PRINTER_NOTIFY_TYPE and it has 2 fields NAME and CJOBS.
3473 * I have 3 printers on the back of my server.
3475 * Now the response is a NOTIFY_INFO structure, with 6 NOTIFY_INFO_DATA
3476 * structures.
3477 * Number Data Id
3478 * 1 printer 1 name 1
3479 * 2 printer 1 cjob 1
3480 * 3 printer 2 name 2
3481 * 4 printer 2 cjob 2
3482 * 5 printer 3 name 3
3483 * 6 printer 3 name 3
3485 * that's the print server case, the printer case is even worse.
3488 /*******************************************************************
3490 * enumerate all printers on the printserver
3491 * fill a notify_info struct with info asked
3493 ********************************************************************/
3495 static WERROR printserver_notify_info(struct pipes_struct *p,
3496 struct policy_handle *hnd,
3497 struct spoolss_NotifyInfo *info,
3498 TALLOC_CTX *mem_ctx)
3500 int snum;
3501 struct printer_handle *Printer = find_printer_index_by_hnd(p, hnd);
3502 int n_services=lp_numservices();
3503 int i;
3504 struct spoolss_NotifyOption *option;
3505 struct spoolss_NotifyOptionType option_type;
3506 struct spoolss_PrinterInfo2 *pinfo2 = NULL;
3507 WERROR result;
3509 DEBUG(4,("printserver_notify_info\n"));
3511 if (!Printer)
3512 return WERR_BADFID;
3514 option = Printer->notify.option;
3516 info->version = 2;
3517 info->notifies = NULL;
3518 info->count = 0;
3520 /* a bug in xp sp2 rc2 causes it to send a fnpcn request without
3521 sending a ffpcn() request first */
3523 if ( !option )
3524 return WERR_BADFID;
3526 for (i=0; i<option->count; i++) {
3527 option_type = option->types[i];
3529 if (option_type.type != PRINTER_NOTIFY_TYPE)
3530 continue;
3532 for (snum = 0; snum < n_services; snum++) {
3533 if (!lp_browseable(snum) ||
3534 !lp_snum_ok(snum) ||
3535 !lp_print_ok(snum)) {
3536 continue; /* skip */
3539 /* Maybe we should use the SYSTEM session_info here... */
3540 result = winreg_get_printer_internal(mem_ctx,
3541 get_session_info_system(),
3542 p->msg_ctx,
3543 lp_servicename(talloc_tos(), snum),
3544 &pinfo2);
3545 if (!W_ERROR_IS_OK(result)) {
3546 DEBUG(4, ("printserver_notify_info: "
3547 "Failed to get printer [%s]\n",
3548 lp_servicename(talloc_tos(), snum)));
3549 continue;
3553 construct_notify_printer_info(p->msg_ctx,
3554 Printer, info,
3555 pinfo2, snum,
3556 &option_type, snum,
3557 mem_ctx);
3559 TALLOC_FREE(pinfo2);
3563 #if 0
3565 * Debugging information, don't delete.
3568 DEBUG(1,("dumping the NOTIFY_INFO\n"));
3569 DEBUGADD(1,("info->version:[%d], info->flags:[%d], info->count:[%d]\n", info->version, info->flags, info->count));
3570 DEBUGADD(1,("num\ttype\tfield\tres\tid\tsize\tenc_type\n"));
3572 for (i=0; i<info->count; i++) {
3573 DEBUGADD(1,("[%d]\t[%d]\t[%d]\t[%d]\t[%d]\t[%d]\t[%d]\n",
3574 i, info->data[i].type, info->data[i].field, info->data[i].reserved,
3575 info->data[i].id, info->data[i].size, info->data[i].enc_type));
3577 #endif
3579 return WERR_OK;
3582 /*******************************************************************
3584 * fill a notify_info struct with info asked
3586 ********************************************************************/
3588 static WERROR printer_notify_info(struct pipes_struct *p,
3589 struct policy_handle *hnd,
3590 struct spoolss_NotifyInfo *info,
3591 TALLOC_CTX *mem_ctx)
3593 int snum;
3594 struct printer_handle *Printer = find_printer_index_by_hnd(p, hnd);
3595 int i;
3596 uint32_t id;
3597 struct spoolss_NotifyOption *option;
3598 struct spoolss_NotifyOptionType option_type;
3599 int count,j;
3600 print_queue_struct *queue=NULL;
3601 print_status_struct status;
3602 struct spoolss_PrinterInfo2 *pinfo2 = NULL;
3603 WERROR result;
3605 DEBUG(4,("printer_notify_info\n"));
3607 if (!Printer)
3608 return WERR_BADFID;
3610 option = Printer->notify.option;
3611 id = 0x0;
3613 info->version = 2;
3614 info->notifies = NULL;
3615 info->count = 0;
3617 /* a bug in xp sp2 rc2 causes it to send a fnpcn request without
3618 sending a ffpcn() request first */
3620 if ( !option )
3621 return WERR_BADFID;
3623 if (!get_printer_snum(p, hnd, &snum, NULL)) {
3624 return WERR_BADFID;
3627 /* Maybe we should use the SYSTEM session_info here... */
3628 result = winreg_get_printer_internal(mem_ctx,
3629 get_session_info_system(),
3630 p->msg_ctx,
3631 lp_servicename(talloc_tos(), snum), &pinfo2);
3632 if (!W_ERROR_IS_OK(result)) {
3633 return WERR_BADFID;
3637 * When sending a PRINTER_NOTIFY_FIELD_SERVER_NAME we should send the
3638 * correct servername.
3640 pinfo2->servername = talloc_strdup(pinfo2, Printer->servername);
3641 if (pinfo2->servername == NULL) {
3642 return WERR_NOMEM;
3645 for (i=0; i<option->count; i++) {
3646 option_type = option->types[i];
3648 switch (option_type.type) {
3649 case PRINTER_NOTIFY_TYPE:
3650 if (construct_notify_printer_info(p->msg_ctx,
3651 Printer, info,
3652 pinfo2, snum,
3653 &option_type, id,
3654 mem_ctx)) {
3655 id--;
3657 break;
3659 case JOB_NOTIFY_TYPE:
3661 count = print_queue_status(p->msg_ctx, snum, &queue,
3662 &status);
3664 for (j=0; j<count; j++) {
3665 construct_notify_jobs_info(p->msg_ctx,
3666 &queue[j], info,
3667 pinfo2, snum,
3668 &option_type,
3669 queue[j].sysjob,
3670 mem_ctx);
3673 SAFE_FREE(queue);
3674 break;
3679 * Debugging information, don't delete.
3682 DEBUG(1,("dumping the NOTIFY_INFO\n"));
3683 DEBUGADD(1,("info->version:[%d], info->flags:[%d], info->count:[%d]\n", info->version, info->flags, info->count));
3684 DEBUGADD(1,("num\ttype\tfield\tres\tid\tsize\tenc_type\n"));
3686 for (i=0; i<info->count; i++) {
3687 DEBUGADD(1,("[%d]\t[%d]\t[%d]\t[%d]\t[%d]\t[%d]\t[%d]\n",
3688 i, info->data[i].type, info->data[i].field, info->data[i].reserved,
3689 info->data[i].id, info->data[i].size, info->data[i].enc_type));
3693 talloc_free(pinfo2);
3694 return WERR_OK;
3697 /****************************************************************
3698 _spoolss_RouterRefreshPrinterChangeNotify
3699 ****************************************************************/
3701 WERROR _spoolss_RouterRefreshPrinterChangeNotify(struct pipes_struct *p,
3702 struct spoolss_RouterRefreshPrinterChangeNotify *r)
3704 struct spoolss_NotifyInfo *info;
3706 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
3707 WERROR result = WERR_BADFID;
3709 /* we always have a spoolss_NotifyInfo struct */
3710 info = talloc_zero(p->mem_ctx, struct spoolss_NotifyInfo);
3711 if (!info) {
3712 result = WERR_NOMEM;
3713 goto done;
3716 *r->out.info = info;
3718 if (!Printer) {
3719 DEBUG(2,("_spoolss_RouterRefreshPrinterChangeNotify: "
3720 "Invalid handle (%s:%u:%u).\n",
3721 OUR_HANDLE(r->in.handle)));
3722 goto done;
3725 DEBUG(4,("Printer type %x\n",Printer->printer_type));
3728 * We are now using the change value, and
3729 * I should check for PRINTER_NOTIFY_OPTIONS_REFRESH but as
3730 * I don't have a global notification system, I'm sending back all the
3731 * information even when _NOTHING_ has changed.
3734 /* We need to keep track of the change value to send back in
3735 RRPCN replies otherwise our updates are ignored. */
3737 Printer->notify.fnpcn = true;
3739 if (Printer->notify.cli_chan != NULL &&
3740 Printer->notify.cli_chan->active_connections > 0) {
3741 DEBUG(10,("_spoolss_RouterRefreshPrinterChangeNotify: "
3742 "Saving change value in request [%x]\n",
3743 r->in.change_low));
3744 Printer->notify.change = r->in.change_low;
3747 /* just ignore the spoolss_NotifyOption */
3749 switch (Printer->printer_type) {
3750 case SPLHND_SERVER:
3751 result = printserver_notify_info(p, r->in.handle,
3752 info, p->mem_ctx);
3753 break;
3755 case SPLHND_PRINTER:
3756 result = printer_notify_info(p, r->in.handle,
3757 info, p->mem_ctx);
3758 break;
3761 Printer->notify.fnpcn = false;
3763 done:
3764 return result;
3767 /********************************************************************
3768 ********************************************************************/
3770 static WERROR create_printername(TALLOC_CTX *mem_ctx,
3771 const char *servername,
3772 const char *printername,
3773 const char **printername_p)
3775 /* FIXME: add lp_force_printername() */
3777 if (servername == NULL) {
3778 *printername_p = talloc_strdup(mem_ctx, printername);
3779 W_ERROR_HAVE_NO_MEMORY(*printername_p);
3780 return WERR_OK;
3783 if (servername[0] == '\\' && servername[1] == '\\') {
3784 servername += 2;
3787 *printername_p = talloc_asprintf(mem_ctx, "\\\\%s\\%s", servername, printername);
3788 W_ERROR_HAVE_NO_MEMORY(*printername_p);
3790 return WERR_OK;
3793 /********************************************************************
3794 ********************************************************************/
3796 static void compose_devicemode_devicename(struct spoolss_DeviceMode *dm,
3797 const char *printername)
3799 if (dm == NULL) {
3800 return;
3803 dm->devicename = talloc_strndup(dm, printername,
3804 MIN(strlen(printername), 31));
3807 /********************************************************************
3808 * construct_printer_info_0
3809 * fill a printer_info_0 struct
3810 ********************************************************************/
3812 static WERROR construct_printer_info0(TALLOC_CTX *mem_ctx,
3813 const struct auth_session_info *session_info,
3814 struct messaging_context *msg_ctx,
3815 struct spoolss_PrinterInfo2 *info2,
3816 const char *servername,
3817 struct spoolss_PrinterInfo0 *r,
3818 int snum)
3820 int count;
3821 struct printer_session_counter *session_counter;
3822 struct timeval setuptime;
3823 print_status_struct status;
3824 WERROR result;
3826 result = create_printername(mem_ctx, servername, info2->printername, &r->printername);
3827 if (!W_ERROR_IS_OK(result)) {
3828 return result;
3831 if (servername) {
3832 r->servername = talloc_strdup(mem_ctx, servername);
3833 W_ERROR_HAVE_NO_MEMORY(r->servername);
3834 } else {
3835 r->servername = NULL;
3838 count = print_queue_length(msg_ctx, snum, &status);
3840 /* check if we already have a counter for this printer */
3841 for (session_counter = counter_list; session_counter; session_counter = session_counter->next) {
3842 if (session_counter->snum == snum)
3843 break;
3846 /* it's the first time, add it to the list */
3847 if (session_counter == NULL) {
3848 session_counter = talloc_zero(counter_list, struct printer_session_counter);
3849 W_ERROR_HAVE_NO_MEMORY(session_counter);
3850 session_counter->snum = snum;
3851 session_counter->counter = 0;
3852 DLIST_ADD(counter_list, session_counter);
3855 /* increment it */
3856 session_counter->counter++;
3858 r->cjobs = count;
3859 r->total_jobs = 0;
3860 r->total_bytes = 0;
3862 get_startup_time(&setuptime);
3863 init_systemtime(&r->time, gmtime(&setuptime.tv_sec));
3865 /* JFM:
3866 * the global_counter should be stored in a TDB as it's common to all the clients
3867 * and should be zeroed on samba startup
3869 r->global_counter = session_counter->counter;
3870 r->total_pages = 0;
3871 /* in 2.2 we reported ourselves as 0x0004 and 0x0565 */
3872 SSVAL(&r->version, 0, 0x0005); /* NT 5 */
3873 SSVAL(&r->version, 2, 0x0893); /* build 2195 */
3874 r->free_build = SPOOLSS_RELEASE_BUILD;
3875 r->spooling = 0;
3876 r->max_spooling = 0;
3877 r->session_counter = session_counter->counter;
3878 r->num_error_out_of_paper = 0x0;
3879 r->num_error_not_ready = 0x0; /* number of print failure */
3880 r->job_error = 0x0;
3881 r->number_of_processors = 0x1;
3882 r->processor_type = PROCESSOR_INTEL_PENTIUM; /* 586 Pentium ? */
3883 r->high_part_total_bytes = 0x0;
3885 /* ChangeID in milliseconds*/
3886 winreg_printer_get_changeid_internal(mem_ctx, session_info, msg_ctx,
3887 info2->sharename, &r->change_id);
3889 r->last_error = WERR_OK;
3890 r->status = nt_printq_status(status.status);
3891 r->enumerate_network_printers = 0x0;
3892 r->c_setprinter = 0x0;
3893 r->processor_architecture = PROCESSOR_ARCHITECTURE_INTEL;
3894 r->processor_level = 0x6; /* 6 ???*/
3895 r->ref_ic = 0;
3896 r->reserved2 = 0;
3897 r->reserved3 = 0;
3899 return WERR_OK;
3903 /********************************************************************
3904 * construct_printer_info1
3905 * fill a spoolss_PrinterInfo1 struct
3906 ********************************************************************/
3908 static WERROR construct_printer_info1(TALLOC_CTX *mem_ctx,
3909 const struct spoolss_PrinterInfo2 *info2,
3910 uint32_t flags,
3911 const char *servername,
3912 struct spoolss_PrinterInfo1 *r,
3913 int snum)
3915 WERROR result;
3917 r->flags = flags;
3919 if (info2->comment == NULL || info2->comment[0] == '\0') {
3920 r->comment = lp_comment(mem_ctx, snum);
3921 } else {
3922 r->comment = talloc_strdup(mem_ctx, info2->comment); /* saved comment */
3924 W_ERROR_HAVE_NO_MEMORY(r->comment);
3926 result = create_printername(mem_ctx, servername, info2->printername, &r->name);
3927 if (!W_ERROR_IS_OK(result)) {
3928 return result;
3931 r->description = talloc_asprintf(mem_ctx, "%s,%s,%s",
3932 r->name,
3933 info2->drivername,
3934 r->comment);
3935 W_ERROR_HAVE_NO_MEMORY(r->description);
3937 return WERR_OK;
3940 /********************************************************************
3941 * construct_printer_info2
3942 * fill a spoolss_PrinterInfo2 struct
3943 ********************************************************************/
3945 static WERROR construct_printer_info2(TALLOC_CTX *mem_ctx,
3946 struct messaging_context *msg_ctx,
3947 const struct spoolss_PrinterInfo2 *info2,
3948 const char *servername,
3949 struct spoolss_PrinterInfo2 *r,
3950 int snum)
3952 int count;
3953 print_status_struct status;
3954 WERROR result;
3956 count = print_queue_length(msg_ctx, snum, &status);
3958 if (servername) {
3959 r->servername = talloc_strdup(mem_ctx, servername);
3960 W_ERROR_HAVE_NO_MEMORY(r->servername);
3961 } else {
3962 r->servername = NULL;
3965 result = create_printername(mem_ctx, servername, info2->printername, &r->printername);
3966 if (!W_ERROR_IS_OK(result)) {
3967 return result;
3970 r->sharename = lp_servicename(mem_ctx, snum);
3971 W_ERROR_HAVE_NO_MEMORY(r->sharename);
3972 r->portname = talloc_strdup(mem_ctx, info2->portname);
3973 W_ERROR_HAVE_NO_MEMORY(r->portname);
3974 r->drivername = talloc_strdup(mem_ctx, info2->drivername);
3975 W_ERROR_HAVE_NO_MEMORY(r->drivername);
3977 if (info2->comment[0] == '\0') {
3978 r->comment = lp_comment(mem_ctx, snum);
3979 } else {
3980 r->comment = talloc_strdup(mem_ctx, info2->comment);
3982 W_ERROR_HAVE_NO_MEMORY(r->comment);
3984 r->location = talloc_strdup(mem_ctx, info2->location);
3985 if (info2->location[0] == '\0') {
3986 const char *loc = NULL;
3987 NTSTATUS nt_status;
3989 nt_status = printer_list_get_printer(mem_ctx,
3990 info2->sharename,
3991 NULL,
3992 &loc,
3993 NULL);
3994 if (NT_STATUS_IS_OK(nt_status)) {
3995 if (loc != NULL) {
3996 r->location = talloc_strdup(mem_ctx, loc);
4000 W_ERROR_HAVE_NO_MEMORY(r->location);
4002 r->sepfile = talloc_strdup(mem_ctx, info2->sepfile);
4003 W_ERROR_HAVE_NO_MEMORY(r->sepfile);
4004 r->printprocessor = talloc_strdup(mem_ctx, info2->printprocessor);
4005 W_ERROR_HAVE_NO_MEMORY(r->printprocessor);
4006 r->datatype = talloc_strdup(mem_ctx, info2->datatype);
4007 W_ERROR_HAVE_NO_MEMORY(r->datatype);
4008 r->parameters = talloc_strdup(mem_ctx, info2->parameters);
4009 W_ERROR_HAVE_NO_MEMORY(r->parameters);
4011 r->attributes = info2->attributes;
4013 r->priority = info2->priority;
4014 r->defaultpriority = info2->defaultpriority;
4015 r->starttime = info2->starttime;
4016 r->untiltime = info2->untiltime;
4017 r->status = nt_printq_status(status.status);
4018 r->cjobs = count;
4019 r->averageppm = info2->averageppm;
4021 if (info2->devmode != NULL) {
4022 result = copy_devicemode(mem_ctx,
4023 info2->devmode,
4024 &r->devmode);
4025 if (!W_ERROR_IS_OK(result)) {
4026 return result;
4028 } else if (lp_default_devmode(snum)) {
4029 result = spoolss_create_default_devmode(mem_ctx,
4030 info2->printername,
4031 &r->devmode);
4032 if (!W_ERROR_IS_OK(result)) {
4033 return result;
4035 } else {
4036 r->devmode = NULL;
4037 DEBUG(8,("Returning NULL Devicemode!\n"));
4040 compose_devicemode_devicename(r->devmode, r->printername);
4042 r->secdesc = NULL;
4044 if (info2->secdesc != NULL) {
4045 /* don't use talloc_steal() here unless you do a deep steal of all
4046 the SEC_DESC members */
4048 r->secdesc = dup_sec_desc(mem_ctx, info2->secdesc);
4051 return WERR_OK;
4054 /********************************************************************
4055 * construct_printer_info3
4056 * fill a spoolss_PrinterInfo3 struct
4057 ********************************************************************/
4059 static WERROR construct_printer_info3(TALLOC_CTX *mem_ctx,
4060 const struct spoolss_PrinterInfo2 *info2,
4061 const char *servername,
4062 struct spoolss_PrinterInfo3 *r,
4063 int snum)
4065 /* These are the components of the SD we are returning. */
4067 if (info2->secdesc != NULL) {
4068 /* don't use talloc_steal() here unless you do a deep steal of all
4069 the SEC_DESC members */
4071 r->secdesc = dup_sec_desc(mem_ctx, info2->secdesc);
4072 W_ERROR_HAVE_NO_MEMORY(r->secdesc);
4075 return WERR_OK;
4078 /********************************************************************
4079 * construct_printer_info4
4080 * fill a spoolss_PrinterInfo4 struct
4081 ********************************************************************/
4083 static WERROR construct_printer_info4(TALLOC_CTX *mem_ctx,
4084 const struct spoolss_PrinterInfo2 *info2,
4085 const char *servername,
4086 struct spoolss_PrinterInfo4 *r,
4087 int snum)
4089 WERROR result;
4091 result = create_printername(mem_ctx, servername, info2->printername, &r->printername);
4092 if (!W_ERROR_IS_OK(result)) {
4093 return result;
4096 if (servername) {
4097 r->servername = talloc_strdup(mem_ctx, servername);
4098 W_ERROR_HAVE_NO_MEMORY(r->servername);
4099 } else {
4100 r->servername = NULL;
4103 r->attributes = info2->attributes;
4105 return WERR_OK;
4108 /********************************************************************
4109 * construct_printer_info5
4110 * fill a spoolss_PrinterInfo5 struct
4111 ********************************************************************/
4113 static WERROR construct_printer_info5(TALLOC_CTX *mem_ctx,
4114 const struct spoolss_PrinterInfo2 *info2,
4115 const char *servername,
4116 struct spoolss_PrinterInfo5 *r,
4117 int snum)
4119 WERROR result;
4121 result = create_printername(mem_ctx, servername, info2->printername, &r->printername);
4122 if (!W_ERROR_IS_OK(result)) {
4123 return result;
4126 r->portname = talloc_strdup(mem_ctx, info2->portname);
4127 W_ERROR_HAVE_NO_MEMORY(r->portname);
4129 r->attributes = info2->attributes;
4131 /* these two are not used by NT+ according to MSDN */
4132 r->device_not_selected_timeout = 0x0; /* have seen 0x3a98 */
4133 r->transmission_retry_timeout = 0x0; /* have seen 0xafc8 */
4135 return WERR_OK;
4138 /********************************************************************
4139 * construct_printer_info_6
4140 * fill a spoolss_PrinterInfo6 struct
4141 ********************************************************************/
4143 static WERROR construct_printer_info6(TALLOC_CTX *mem_ctx,
4144 struct messaging_context *msg_ctx,
4145 const struct spoolss_PrinterInfo2 *info2,
4146 const char *servername,
4147 struct spoolss_PrinterInfo6 *r,
4148 int snum)
4150 print_status_struct status;
4152 print_queue_length(msg_ctx, snum, &status);
4154 r->status = nt_printq_status(status.status);
4156 return WERR_OK;
4159 /********************************************************************
4160 * construct_printer_info7
4161 * fill a spoolss_PrinterInfo7 struct
4162 ********************************************************************/
4164 static WERROR construct_printer_info7(TALLOC_CTX *mem_ctx,
4165 struct messaging_context *msg_ctx,
4166 const char *servername,
4167 struct spoolss_PrinterInfo7 *r,
4168 int snum)
4170 const struct auth_session_info *session_info;
4171 char *printer;
4172 WERROR werr;
4173 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
4174 if (tmp_ctx == NULL) {
4175 return WERR_NOMEM;
4178 session_info = get_session_info_system();
4179 SMB_ASSERT(session_info != NULL);
4181 printer = lp_servicename(tmp_ctx, snum);
4182 if (printer == NULL) {
4183 DEBUG(0, ("invalid printer snum %d\n", snum));
4184 werr = WERR_INVALID_PARAM;
4185 goto out_tmp_free;
4188 if (is_printer_published(tmp_ctx, session_info, msg_ctx,
4189 servername, printer, NULL)) {
4190 struct GUID guid;
4191 werr = nt_printer_guid_get(tmp_ctx, session_info, msg_ctx,
4192 printer, &guid);
4193 if (!W_ERROR_IS_OK(werr)) {
4194 goto out_tmp_free;
4196 r->guid = talloc_strdup_upper(mem_ctx, GUID_string2(mem_ctx, &guid));
4197 r->action = DSPRINT_PUBLISH;
4198 } else {
4199 r->guid = talloc_strdup(mem_ctx, "");
4200 r->action = DSPRINT_UNPUBLISH;
4202 if (r->guid == NULL) {
4203 werr = WERR_NOMEM;
4204 goto out_tmp_free;
4207 werr = WERR_OK;
4208 out_tmp_free:
4209 talloc_free(tmp_ctx);
4210 return werr;
4213 /********************************************************************
4214 * construct_printer_info8
4215 * fill a spoolss_PrinterInfo8 struct
4216 ********************************************************************/
4218 static WERROR construct_printer_info8(TALLOC_CTX *mem_ctx,
4219 const struct spoolss_PrinterInfo2 *info2,
4220 const char *servername,
4221 struct spoolss_DeviceModeInfo *r,
4222 int snum)
4224 WERROR result;
4225 const char *printername;
4227 result = create_printername(mem_ctx, servername, info2->printername, &printername);
4228 if (!W_ERROR_IS_OK(result)) {
4229 return result;
4232 if (info2->devmode != NULL) {
4233 result = copy_devicemode(mem_ctx,
4234 info2->devmode,
4235 &r->devmode);
4236 if (!W_ERROR_IS_OK(result)) {
4237 return result;
4239 } else if (lp_default_devmode(snum)) {
4240 result = spoolss_create_default_devmode(mem_ctx,
4241 info2->printername,
4242 &r->devmode);
4243 if (!W_ERROR_IS_OK(result)) {
4244 return result;
4246 } else {
4247 r->devmode = NULL;
4248 DEBUG(8,("Returning NULL Devicemode!\n"));
4251 compose_devicemode_devicename(r->devmode, printername);
4253 return WERR_OK;
4257 /********************************************************************
4258 ********************************************************************/
4260 static bool snum_is_shared_printer(int snum)
4262 return (lp_browseable(snum) && lp_snum_ok(snum) && lp_print_ok(snum));
4265 /********************************************************************
4266 Spoolss_enumprinters.
4267 ********************************************************************/
4269 static WERROR enum_all_printers_info_level(TALLOC_CTX *mem_ctx,
4270 const struct auth_session_info *session_info,
4271 struct messaging_context *msg_ctx,
4272 const char *servername,
4273 uint32_t level,
4274 uint32_t flags,
4275 union spoolss_PrinterInfo **info_p,
4276 uint32_t *count_p)
4278 int snum;
4279 int n_services = lp_numservices();
4280 union spoolss_PrinterInfo *info = NULL;
4281 uint32_t count = 0;
4282 WERROR result = WERR_OK;
4283 struct dcerpc_binding_handle *b = NULL;
4284 TALLOC_CTX *tmp_ctx = NULL;
4286 tmp_ctx = talloc_new(mem_ctx);
4287 if (!tmp_ctx) {
4288 return WERR_NOMEM;
4291 *count_p = 0;
4292 *info_p = NULL;
4294 for (snum = 0; snum < n_services; snum++) {
4296 const char *printer;
4297 struct spoolss_PrinterInfo2 *info2;
4299 if (!snum_is_shared_printer(snum)) {
4300 continue;
4303 printer = lp_const_servicename(snum);
4305 DEBUG(4,("Found a printer in smb.conf: %s[%x]\n",
4306 printer, snum));
4308 if (b == NULL) {
4309 result = winreg_printer_binding_handle(tmp_ctx,
4310 session_info,
4311 msg_ctx,
4312 &b);
4313 if (!W_ERROR_IS_OK(result)) {
4314 goto out;
4318 result = winreg_create_printer(tmp_ctx, b,
4319 printer);
4320 if (!W_ERROR_IS_OK(result)) {
4321 goto out;
4324 info = talloc_realloc(tmp_ctx, info,
4325 union spoolss_PrinterInfo,
4326 count + 1);
4327 if (!info) {
4328 result = WERR_NOMEM;
4329 goto out;
4332 result = winreg_get_printer(tmp_ctx, b,
4333 printer, &info2);
4334 if (!W_ERROR_IS_OK(result)) {
4335 goto out;
4338 switch (level) {
4339 case 0:
4340 result = construct_printer_info0(info, session_info,
4341 msg_ctx, info2,
4342 servername,
4343 &info[count].info0, snum);
4344 break;
4345 case 1:
4346 result = construct_printer_info1(info, info2, flags,
4347 servername,
4348 &info[count].info1, snum);
4349 break;
4350 case 2:
4351 result = construct_printer_info2(info, msg_ctx, info2,
4352 servername,
4353 &info[count].info2, snum);
4354 break;
4355 case 4:
4356 result = construct_printer_info4(info, info2,
4357 servername,
4358 &info[count].info4, snum);
4359 break;
4360 case 5:
4361 result = construct_printer_info5(info, info2,
4362 servername,
4363 &info[count].info5, snum);
4364 break;
4366 default:
4367 result = WERR_UNKNOWN_LEVEL;
4368 goto out;
4371 if (!W_ERROR_IS_OK(result)) {
4372 goto out;
4375 count++;
4378 out:
4379 if (W_ERROR_IS_OK(result)) {
4380 *info_p = talloc_move(mem_ctx, &info);
4381 *count_p = count;
4384 talloc_free(tmp_ctx);
4386 return result;
4389 /********************************************************************
4390 * handle enumeration of printers at level 0
4391 ********************************************************************/
4393 static WERROR enumprinters_level0(TALLOC_CTX *mem_ctx,
4394 const struct auth_session_info *session_info,
4395 struct messaging_context *msg_ctx,
4396 uint32_t flags,
4397 const char *servername,
4398 union spoolss_PrinterInfo **info,
4399 uint32_t *count)
4401 DEBUG(4,("enum_all_printers_info_0\n"));
4403 return enum_all_printers_info_level(mem_ctx, session_info, msg_ctx,
4404 servername, 0, flags, info, count);
4408 /********************************************************************
4409 ********************************************************************/
4411 static WERROR enum_all_printers_info_1(TALLOC_CTX *mem_ctx,
4412 const struct auth_session_info *session_info,
4413 struct messaging_context *msg_ctx,
4414 const char *servername,
4415 uint32_t flags,
4416 union spoolss_PrinterInfo **info,
4417 uint32_t *count)
4419 DEBUG(4,("enum_all_printers_info_1\n"));
4421 return enum_all_printers_info_level(mem_ctx, session_info, msg_ctx,
4422 servername, 1, flags, info, count);
4425 /********************************************************************
4426 enum_all_printers_info_1_local.
4427 *********************************************************************/
4429 static WERROR enum_all_printers_info_1_local(TALLOC_CTX *mem_ctx,
4430 const struct auth_session_info *session_info,
4431 struct messaging_context *msg_ctx,
4432 const char *servername,
4433 union spoolss_PrinterInfo **info,
4434 uint32_t *count)
4436 DEBUG(4,("enum_all_printers_info_1_local\n"));
4438 return enum_all_printers_info_1(mem_ctx, session_info, msg_ctx,
4439 servername, PRINTER_ENUM_ICON8, info, count);
4442 /********************************************************************
4443 enum_all_printers_info_1_name.
4444 *********************************************************************/
4446 static WERROR enum_all_printers_info_1_name(TALLOC_CTX *mem_ctx,
4447 const struct auth_session_info *session_info,
4448 struct messaging_context *msg_ctx,
4449 const char *servername,
4450 union spoolss_PrinterInfo **info,
4451 uint32_t *count)
4453 const char *s = servername;
4455 DEBUG(4,("enum_all_printers_info_1_name\n"));
4457 if (servername != NULL &&
4458 (servername[0] == '\\') && (servername[1] == '\\')) {
4459 s = servername + 2;
4462 if (!is_myname_or_ipaddr(s)) {
4463 return WERR_INVALID_NAME;
4466 return enum_all_printers_info_1(mem_ctx, session_info, msg_ctx,
4467 servername, PRINTER_ENUM_ICON8, info, count);
4470 /********************************************************************
4471 enum_all_printers_info_1_network.
4472 *********************************************************************/
4474 static WERROR enum_all_printers_info_1_network(TALLOC_CTX *mem_ctx,
4475 const struct auth_session_info *session_info,
4476 struct messaging_context *msg_ctx,
4477 const char *servername,
4478 union spoolss_PrinterInfo **info,
4479 uint32_t *count)
4481 const char *s = servername;
4483 DEBUG(4,("enum_all_printers_info_1_network\n"));
4485 /* If we respond to a enum_printers level 1 on our name with flags
4486 set to PRINTER_ENUM_REMOTE with a list of printers then these
4487 printers incorrectly appear in the APW browse list.
4488 Specifically the printers for the server appear at the workgroup
4489 level where all the other servers in the domain are
4490 listed. Windows responds to this call with a
4491 WERR_CAN_NOT_COMPLETE so we should do the same. */
4493 if (servername != NULL &&
4494 (servername[0] == '\\') && (servername[1] == '\\')) {
4495 s = servername + 2;
4498 if (is_myname_or_ipaddr(s)) {
4499 return WERR_CAN_NOT_COMPLETE;
4502 return enum_all_printers_info_1(mem_ctx, session_info, msg_ctx,
4503 servername, PRINTER_ENUM_NAME, info, count);
4506 /********************************************************************
4507 * api_spoolss_enumprinters
4509 * called from api_spoolss_enumprinters (see this to understand)
4510 ********************************************************************/
4512 static WERROR enum_all_printers_info_2(TALLOC_CTX *mem_ctx,
4513 const struct auth_session_info *session_info,
4514 struct messaging_context *msg_ctx,
4515 const char *servername,
4516 union spoolss_PrinterInfo **info,
4517 uint32_t *count)
4519 DEBUG(4,("enum_all_printers_info_2\n"));
4521 return enum_all_printers_info_level(mem_ctx, session_info, msg_ctx,
4522 servername, 2, 0, info, count);
4525 /********************************************************************
4526 * handle enumeration of printers at level 1
4527 ********************************************************************/
4529 static WERROR enumprinters_level1(TALLOC_CTX *mem_ctx,
4530 const struct auth_session_info *session_info,
4531 struct messaging_context *msg_ctx,
4532 uint32_t flags,
4533 const char *servername,
4534 union spoolss_PrinterInfo **info,
4535 uint32_t *count)
4537 /* Not all the flags are equals */
4539 if (flags & PRINTER_ENUM_LOCAL) {
4540 return enum_all_printers_info_1_local(mem_ctx, session_info,
4541 msg_ctx, servername, info, count);
4544 if (flags & PRINTER_ENUM_NAME) {
4545 return enum_all_printers_info_1_name(mem_ctx, session_info,
4546 msg_ctx, servername, info,
4547 count);
4550 if (flags & PRINTER_ENUM_NETWORK) {
4551 return enum_all_printers_info_1_network(mem_ctx, session_info,
4552 msg_ctx, servername, info,
4553 count);
4556 return WERR_OK; /* NT4sp5 does that */
4559 /********************************************************************
4560 * handle enumeration of printers at level 2
4561 ********************************************************************/
4563 static WERROR enumprinters_level2(TALLOC_CTX *mem_ctx,
4564 const struct auth_session_info *session_info,
4565 struct messaging_context *msg_ctx,
4566 uint32_t flags,
4567 const char *servername,
4568 union spoolss_PrinterInfo **info,
4569 uint32_t *count)
4571 if (flags & PRINTER_ENUM_LOCAL) {
4573 return enum_all_printers_info_2(mem_ctx, session_info, msg_ctx,
4574 servername,
4575 info, count);
4578 if (flags & PRINTER_ENUM_NAME) {
4579 if (servername && !is_myname_or_ipaddr(canon_servername(servername))) {
4580 return WERR_INVALID_NAME;
4583 return enum_all_printers_info_2(mem_ctx, session_info, msg_ctx,
4584 servername,
4585 info, count);
4588 if (flags & PRINTER_ENUM_REMOTE) {
4589 return WERR_UNKNOWN_LEVEL;
4592 return WERR_OK;
4595 /********************************************************************
4596 * handle enumeration of printers at level 4
4597 ********************************************************************/
4599 static WERROR enumprinters_level4(TALLOC_CTX *mem_ctx,
4600 const struct auth_session_info *session_info,
4601 struct messaging_context *msg_ctx,
4602 uint32_t flags,
4603 const char *servername,
4604 union spoolss_PrinterInfo **info,
4605 uint32_t *count)
4607 DEBUG(4,("enum_all_printers_info_4\n"));
4609 return enum_all_printers_info_level(mem_ctx, session_info, msg_ctx,
4610 servername, 4, flags, info, count);
4614 /********************************************************************
4615 * handle enumeration of printers at level 5
4616 ********************************************************************/
4618 static WERROR enumprinters_level5(TALLOC_CTX *mem_ctx,
4619 const struct auth_session_info *session_info,
4620 struct messaging_context *msg_ctx,
4621 uint32_t flags,
4622 const char *servername,
4623 union spoolss_PrinterInfo **info,
4624 uint32_t *count)
4626 DEBUG(4,("enum_all_printers_info_5\n"));
4628 return enum_all_printers_info_level(mem_ctx, session_info, msg_ctx,
4629 servername, 5, flags, info, count);
4632 /****************************************************************
4633 _spoolss_EnumPrinters
4634 ****************************************************************/
4636 WERROR _spoolss_EnumPrinters(struct pipes_struct *p,
4637 struct spoolss_EnumPrinters *r)
4639 const struct auth_session_info *session_info = get_session_info_system();
4640 WERROR result;
4642 /* that's an [in out] buffer */
4644 if (!r->in.buffer && (r->in.offered != 0)) {
4645 return WERR_INVALID_PARAM;
4648 DEBUG(4,("_spoolss_EnumPrinters\n"));
4650 *r->out.needed = 0;
4651 *r->out.count = 0;
4652 *r->out.info = NULL;
4655 * Level 1:
4656 * flags==PRINTER_ENUM_NAME
4657 * if name=="" then enumerates all printers
4658 * if name!="" then enumerate the printer
4659 * flags==PRINTER_ENUM_REMOTE
4660 * name is NULL, enumerate printers
4661 * Level 2: name!="" enumerates printers, name can't be NULL
4662 * Level 3: doesn't exist
4663 * Level 4: does a local registry lookup
4664 * Level 5: same as Level 2
4667 if (r->in.server && r->in.server[0] == '\0') {
4668 r->in.server = NULL;
4671 switch (r->in.level) {
4672 case 0:
4673 result = enumprinters_level0(p->mem_ctx, session_info,
4674 p->msg_ctx, r->in.flags,
4675 r->in.server,
4676 r->out.info, r->out.count);
4677 break;
4678 case 1:
4679 result = enumprinters_level1(p->mem_ctx, session_info,
4680 p->msg_ctx, r->in.flags,
4681 r->in.server,
4682 r->out.info, r->out.count);
4683 break;
4684 case 2:
4685 result = enumprinters_level2(p->mem_ctx, session_info,
4686 p->msg_ctx, r->in.flags,
4687 r->in.server,
4688 r->out.info, r->out.count);
4689 break;
4690 case 4:
4691 result = enumprinters_level4(p->mem_ctx, session_info,
4692 p->msg_ctx, r->in.flags,
4693 r->in.server,
4694 r->out.info, r->out.count);
4695 break;
4696 case 5:
4697 result = enumprinters_level5(p->mem_ctx, session_info,
4698 p->msg_ctx, r->in.flags,
4699 r->in.server,
4700 r->out.info, r->out.count);
4701 break;
4702 default:
4703 return WERR_UNKNOWN_LEVEL;
4706 if (!W_ERROR_IS_OK(result)) {
4707 return result;
4710 *r->out.needed = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
4711 spoolss_EnumPrinters,
4712 *r->out.info, r->in.level,
4713 *r->out.count);
4714 *r->out.info = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
4715 *r->out.count = SPOOLSS_BUFFER_OK(*r->out.count, 0);
4717 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
4720 /****************************************************************
4721 _spoolss_GetPrinter
4722 ****************************************************************/
4724 WERROR _spoolss_GetPrinter(struct pipes_struct *p,
4725 struct spoolss_GetPrinter *r)
4727 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
4728 struct spoolss_PrinterInfo2 *info2 = NULL;
4729 WERROR result = WERR_OK;
4730 int snum;
4732 /* that's an [in out] buffer */
4734 if (!r->in.buffer && (r->in.offered != 0)) {
4735 return WERR_INVALID_PARAM;
4738 *r->out.needed = 0;
4740 if (Printer == NULL) {
4741 return WERR_BADFID;
4744 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
4745 return WERR_BADFID;
4748 result = winreg_get_printer_internal(p->mem_ctx,
4749 get_session_info_system(),
4750 p->msg_ctx,
4751 lp_const_servicename(snum),
4752 &info2);
4753 if (!W_ERROR_IS_OK(result)) {
4754 goto out;
4757 switch (r->in.level) {
4758 case 0:
4759 result = construct_printer_info0(p->mem_ctx,
4760 get_session_info_system(),
4761 p->msg_ctx,
4762 info2,
4763 Printer->servername,
4764 &r->out.info->info0,
4765 snum);
4766 break;
4767 case 1:
4768 result = construct_printer_info1(p->mem_ctx, info2,
4769 PRINTER_ENUM_ICON8,
4770 Printer->servername,
4771 &r->out.info->info1, snum);
4772 break;
4773 case 2:
4774 result = construct_printer_info2(p->mem_ctx, p->msg_ctx, info2,
4775 Printer->servername,
4776 &r->out.info->info2, snum);
4777 break;
4778 case 3:
4779 result = construct_printer_info3(p->mem_ctx, info2,
4780 Printer->servername,
4781 &r->out.info->info3, snum);
4782 break;
4783 case 4:
4784 result = construct_printer_info4(p->mem_ctx, info2,
4785 Printer->servername,
4786 &r->out.info->info4, snum);
4787 break;
4788 case 5:
4789 result = construct_printer_info5(p->mem_ctx, info2,
4790 Printer->servername,
4791 &r->out.info->info5, snum);
4792 break;
4793 case 6:
4794 result = construct_printer_info6(p->mem_ctx, p->msg_ctx, info2,
4795 Printer->servername,
4796 &r->out.info->info6, snum);
4797 break;
4798 case 7:
4799 result = construct_printer_info7(p->mem_ctx, p->msg_ctx,
4800 Printer->servername,
4801 &r->out.info->info7, snum);
4802 break;
4803 case 8:
4804 result = construct_printer_info8(p->mem_ctx, info2,
4805 Printer->servername,
4806 &r->out.info->info8, snum);
4807 break;
4808 default:
4809 result = WERR_UNKNOWN_LEVEL;
4810 break;
4812 TALLOC_FREE(info2);
4814 out:
4815 if (!W_ERROR_IS_OK(result)) {
4816 DEBUG(0, ("_spoolss_GetPrinter: failed to construct printer info level %d - %s\n",
4817 r->in.level, win_errstr(result)));
4818 TALLOC_FREE(r->out.info);
4819 return result;
4822 *r->out.needed = SPOOLSS_BUFFER_UNION(spoolss_PrinterInfo,
4823 r->out.info, r->in.level);
4824 r->out.info = SPOOLSS_BUFFER_OK(r->out.info, NULL);
4826 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
4829 /********************************************************************
4830 ********************************************************************/
4832 #define FILL_DRIVER_STRING(mem_ctx, in, out) \
4833 do { \
4834 if (in && strlen(in)) { \
4835 out = talloc_strdup(mem_ctx, in); \
4836 } else { \
4837 out = talloc_strdup(mem_ctx, ""); \
4839 W_ERROR_HAVE_NO_MEMORY(out); \
4840 } while (0);
4842 #define FILL_DRIVER_UNC_STRING(mem_ctx, server, arch, ver, in, out) \
4843 do { \
4844 if (in && strlen(in)) { \
4845 out = talloc_asprintf(mem_ctx, "\\\\%s\\print$\\%s\\%d\\%s", server, get_short_archi(arch), ver, in); \
4846 } else { \
4847 out = talloc_strdup(mem_ctx, ""); \
4849 W_ERROR_HAVE_NO_MEMORY(out); \
4850 } while (0);
4852 static WERROR string_array_from_driver_info(TALLOC_CTX *mem_ctx,
4853 const char **string_array,
4854 const char ***presult,
4855 const char *cservername,
4856 const char *arch,
4857 int version)
4859 int i, num_strings = 0;
4860 const char **array = NULL;
4862 if (string_array == NULL) {
4863 return WERR_INVALID_PARAMETER;
4866 for (i=0; string_array[i] && string_array[i][0] != '\0'; i++) {
4867 const char *str = NULL;
4869 if (cservername == NULL || arch == NULL) {
4870 FILL_DRIVER_STRING(mem_ctx, string_array[i], str);
4871 } else {
4872 FILL_DRIVER_UNC_STRING(mem_ctx, cservername, arch, version, string_array[i], str);
4875 if (!add_string_to_array(mem_ctx, str, &array, &num_strings)) {
4876 TALLOC_FREE(array);
4877 return WERR_NOMEM;
4881 if (i > 0) {
4882 ADD_TO_ARRAY(mem_ctx, const char *, NULL,
4883 &array, &num_strings);
4886 if (presult != NULL) {
4887 *presult = array;
4888 } else {
4889 talloc_free(array);
4892 return WERR_OK;
4895 /********************************************************************
4896 * fill a spoolss_DriverInfo1 struct
4897 ********************************************************************/
4899 static WERROR fill_printer_driver_info1(TALLOC_CTX *mem_ctx,
4900 struct spoolss_DriverInfo1 *r,
4901 const struct spoolss_DriverInfo8 *driver,
4902 const char *servername)
4904 r->driver_name = talloc_strdup(mem_ctx, driver->driver_name);
4905 W_ERROR_HAVE_NO_MEMORY(r->driver_name);
4907 return WERR_OK;
4910 /********************************************************************
4911 * fill a spoolss_DriverInfo2 struct
4912 ********************************************************************/
4914 static WERROR fill_printer_driver_info2(TALLOC_CTX *mem_ctx,
4915 struct spoolss_DriverInfo2 *r,
4916 const struct spoolss_DriverInfo8 *driver,
4917 const char *servername)
4920 const char *cservername = canon_servername(servername);
4922 r->version = driver->version;
4924 r->driver_name = talloc_strdup(mem_ctx, driver->driver_name);
4925 W_ERROR_HAVE_NO_MEMORY(r->driver_name);
4926 r->architecture = talloc_strdup(mem_ctx, driver->architecture);
4927 W_ERROR_HAVE_NO_MEMORY(r->architecture);
4929 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4930 driver->architecture,
4931 driver->version,
4932 driver->driver_path,
4933 r->driver_path);
4935 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4936 driver->architecture,
4937 driver->version,
4938 driver->data_file,
4939 r->data_file);
4941 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4942 driver->architecture,
4943 driver->version,
4944 driver->config_file,
4945 r->config_file);
4947 return WERR_OK;
4950 /********************************************************************
4951 * fill a spoolss_DriverInfo3 struct
4952 ********************************************************************/
4954 static WERROR fill_printer_driver_info3(TALLOC_CTX *mem_ctx,
4955 struct spoolss_DriverInfo3 *r,
4956 const struct spoolss_DriverInfo8 *driver,
4957 const char *servername)
4959 const char *cservername = canon_servername(servername);
4961 r->version = driver->version;
4963 r->driver_name = talloc_strdup(mem_ctx, driver->driver_name);
4964 W_ERROR_HAVE_NO_MEMORY(r->driver_name);
4965 r->architecture = talloc_strdup(mem_ctx, driver->architecture);
4966 W_ERROR_HAVE_NO_MEMORY(r->architecture);
4968 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4969 driver->architecture,
4970 driver->version,
4971 driver->driver_path,
4972 r->driver_path);
4974 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4975 driver->architecture,
4976 driver->version,
4977 driver->data_file,
4978 r->data_file);
4980 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4981 driver->architecture,
4982 driver->version,
4983 driver->config_file,
4984 r->config_file);
4986 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4987 driver->architecture,
4988 driver->version,
4989 driver->help_file,
4990 r->help_file);
4992 FILL_DRIVER_STRING(mem_ctx,
4993 driver->monitor_name,
4994 r->monitor_name);
4996 FILL_DRIVER_STRING(mem_ctx,
4997 driver->default_datatype,
4998 r->default_datatype);
5000 return string_array_from_driver_info(mem_ctx,
5001 driver->dependent_files,
5002 &r->dependent_files,
5003 cservername,
5004 driver->architecture,
5005 driver->version);
5008 /********************************************************************
5009 * fill a spoolss_DriverInfo4 struct
5010 ********************************************************************/
5012 static WERROR fill_printer_driver_info4(TALLOC_CTX *mem_ctx,
5013 struct spoolss_DriverInfo4 *r,
5014 const struct spoolss_DriverInfo8 *driver,
5015 const char *servername)
5017 const char *cservername = canon_servername(servername);
5018 WERROR result;
5020 r->version = driver->version;
5022 r->driver_name = talloc_strdup(mem_ctx, driver->driver_name);
5023 W_ERROR_HAVE_NO_MEMORY(r->driver_name);
5024 r->architecture = talloc_strdup(mem_ctx, driver->architecture);
5025 W_ERROR_HAVE_NO_MEMORY(r->architecture);
5027 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
5028 driver->architecture,
5029 driver->version,
5030 driver->driver_path,
5031 r->driver_path);
5033 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
5034 driver->architecture,
5035 driver->version,
5036 driver->data_file,
5037 r->data_file);
5039 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
5040 driver->architecture,
5041 driver->version,
5042 driver->config_file,
5043 r->config_file);
5045 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
5046 driver->architecture,
5047 driver->version,
5048 driver->help_file,
5049 r->help_file);
5051 result = string_array_from_driver_info(mem_ctx,
5052 driver->dependent_files,
5053 &r->dependent_files,
5054 cservername,
5055 driver->architecture,
5056 driver->version);
5057 if (!W_ERROR_IS_OK(result)) {
5058 return result;
5061 FILL_DRIVER_STRING(mem_ctx,
5062 driver->monitor_name,
5063 r->monitor_name);
5065 FILL_DRIVER_STRING(mem_ctx,
5066 driver->default_datatype,
5067 r->default_datatype);
5070 result = string_array_from_driver_info(mem_ctx,
5071 driver->previous_names,
5072 &r->previous_names,
5073 NULL, NULL, 0);
5075 return result;
5078 /********************************************************************
5079 * fill a spoolss_DriverInfo5 struct
5080 ********************************************************************/
5082 static WERROR fill_printer_driver_info5(TALLOC_CTX *mem_ctx,
5083 struct spoolss_DriverInfo5 *r,
5084 const struct spoolss_DriverInfo8 *driver,
5085 const char *servername)
5087 const char *cservername = canon_servername(servername);
5089 r->version = driver->version;
5091 r->driver_name = talloc_strdup(mem_ctx, driver->driver_name);
5092 W_ERROR_HAVE_NO_MEMORY(r->driver_name);
5093 r->architecture = talloc_strdup(mem_ctx, driver->architecture);
5094 W_ERROR_HAVE_NO_MEMORY(r->architecture);
5096 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
5097 driver->architecture,
5098 driver->version,
5099 driver->driver_path,
5100 r->driver_path);
5102 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
5103 driver->architecture,
5104 driver->version,
5105 driver->data_file,
5106 r->data_file);
5108 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
5109 driver->architecture,
5110 driver->version,
5111 driver->config_file,
5112 r->config_file);
5114 r->driver_attributes = 0;
5115 r->config_version = 0;
5116 r->driver_version = 0;
5118 return WERR_OK;
5120 /********************************************************************
5121 * fill a spoolss_DriverInfo6 struct
5122 ********************************************************************/
5124 static WERROR fill_printer_driver_info6(TALLOC_CTX *mem_ctx,
5125 struct spoolss_DriverInfo6 *r,
5126 const struct spoolss_DriverInfo8 *driver,
5127 const char *servername)
5129 const char *cservername = canon_servername(servername);
5130 WERROR result;
5132 r->version = driver->version;
5134 r->driver_name = talloc_strdup(mem_ctx, driver->driver_name);
5135 W_ERROR_HAVE_NO_MEMORY(r->driver_name);
5136 r->architecture = talloc_strdup(mem_ctx, driver->architecture);
5137 W_ERROR_HAVE_NO_MEMORY(r->architecture);
5139 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
5140 driver->architecture,
5141 driver->version,
5142 driver->driver_path,
5143 r->driver_path);
5145 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
5146 driver->architecture,
5147 driver->version,
5148 driver->data_file,
5149 r->data_file);
5151 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
5152 driver->architecture,
5153 driver->version,
5154 driver->config_file,
5155 r->config_file);
5157 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
5158 driver->architecture,
5159 driver->version,
5160 driver->help_file,
5161 r->help_file);
5163 FILL_DRIVER_STRING(mem_ctx,
5164 driver->monitor_name,
5165 r->monitor_name);
5167 FILL_DRIVER_STRING(mem_ctx,
5168 driver->default_datatype,
5169 r->default_datatype);
5171 result = string_array_from_driver_info(mem_ctx,
5172 driver->dependent_files,
5173 &r->dependent_files,
5174 cservername,
5175 driver->architecture,
5176 driver->version);
5177 if (!W_ERROR_IS_OK(result)) {
5178 return result;
5181 result = string_array_from_driver_info(mem_ctx,
5182 driver->previous_names,
5183 &r->previous_names,
5184 NULL, NULL, 0);
5185 if (!W_ERROR_IS_OK(result)) {
5186 return result;
5189 r->driver_date = driver->driver_date;
5190 r->driver_version = driver->driver_version;
5192 FILL_DRIVER_STRING(mem_ctx,
5193 driver->manufacturer_name,
5194 r->manufacturer_name);
5195 FILL_DRIVER_STRING(mem_ctx,
5196 driver->manufacturer_url,
5197 r->manufacturer_url);
5198 FILL_DRIVER_STRING(mem_ctx,
5199 driver->hardware_id,
5200 r->hardware_id);
5201 FILL_DRIVER_STRING(mem_ctx,
5202 driver->provider,
5203 r->provider);
5205 return WERR_OK;
5208 /********************************************************************
5209 * fill a spoolss_DriverInfo8 struct
5210 ********************************************************************/
5212 static WERROR fill_printer_driver_info8(TALLOC_CTX *mem_ctx,
5213 struct spoolss_DriverInfo8 *r,
5214 const struct spoolss_DriverInfo8 *driver,
5215 const char *servername)
5217 const char *cservername = canon_servername(servername);
5218 WERROR result;
5220 r->version = driver->version;
5222 r->driver_name = talloc_strdup(mem_ctx, driver->driver_name);
5223 W_ERROR_HAVE_NO_MEMORY(r->driver_name);
5224 r->architecture = talloc_strdup(mem_ctx, driver->architecture);
5225 W_ERROR_HAVE_NO_MEMORY(r->architecture);
5227 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
5228 driver->architecture,
5229 driver->version,
5230 driver->driver_path,
5231 r->driver_path);
5233 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
5234 driver->architecture,
5235 driver->version,
5236 driver->data_file,
5237 r->data_file);
5239 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
5240 driver->architecture,
5241 driver->version,
5242 driver->config_file,
5243 r->config_file);
5245 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
5246 driver->architecture,
5247 driver->version,
5248 driver->help_file,
5249 r->help_file);
5251 FILL_DRIVER_STRING(mem_ctx,
5252 driver->monitor_name,
5253 r->monitor_name);
5255 FILL_DRIVER_STRING(mem_ctx,
5256 driver->default_datatype,
5257 r->default_datatype);
5259 result = string_array_from_driver_info(mem_ctx,
5260 driver->dependent_files,
5261 &r->dependent_files,
5262 cservername,
5263 driver->architecture,
5264 driver->version);
5265 if (!W_ERROR_IS_OK(result)) {
5266 return result;
5269 result = string_array_from_driver_info(mem_ctx,
5270 driver->previous_names,
5271 &r->previous_names,
5272 NULL, NULL, 0);
5273 if (!W_ERROR_IS_OK(result)) {
5274 return result;
5277 r->driver_date = driver->driver_date;
5278 r->driver_version = driver->driver_version;
5280 FILL_DRIVER_STRING(mem_ctx,
5281 driver->manufacturer_name,
5282 r->manufacturer_name);
5283 FILL_DRIVER_STRING(mem_ctx,
5284 driver->manufacturer_url,
5285 r->manufacturer_url);
5286 FILL_DRIVER_STRING(mem_ctx,
5287 driver->hardware_id,
5288 r->hardware_id);
5289 FILL_DRIVER_STRING(mem_ctx,
5290 driver->provider,
5291 r->provider);
5293 FILL_DRIVER_STRING(mem_ctx,
5294 driver->print_processor,
5295 r->print_processor);
5296 FILL_DRIVER_STRING(mem_ctx,
5297 driver->vendor_setup,
5298 r->vendor_setup);
5300 result = string_array_from_driver_info(mem_ctx,
5301 driver->color_profiles,
5302 &r->color_profiles,
5303 NULL, NULL, 0);
5304 if (!W_ERROR_IS_OK(result)) {
5305 return result;
5308 FILL_DRIVER_STRING(mem_ctx,
5309 driver->inf_path,
5310 r->inf_path);
5312 r->printer_driver_attributes = driver->printer_driver_attributes;
5314 result = string_array_from_driver_info(mem_ctx,
5315 driver->core_driver_dependencies,
5316 &r->core_driver_dependencies,
5317 NULL, NULL, 0);
5318 if (!W_ERROR_IS_OK(result)) {
5319 return result;
5322 r->min_inbox_driver_ver_date = driver->min_inbox_driver_ver_date;
5323 r->min_inbox_driver_ver_version = driver->min_inbox_driver_ver_version;
5325 return WERR_OK;
5328 #if 0 /* disabled until marshalling issues are resolved - gd */
5329 /********************************************************************
5330 ********************************************************************/
5332 static WERROR fill_spoolss_DriverFileInfo(TALLOC_CTX *mem_ctx,
5333 struct spoolss_DriverFileInfo *r,
5334 const char *cservername,
5335 const char *file_name,
5336 enum spoolss_DriverFileType file_type,
5337 uint32_t file_version)
5339 r->file_name = talloc_asprintf(mem_ctx, "\\\\%s%s",
5340 cservername, file_name);
5341 W_ERROR_HAVE_NO_MEMORY(r->file_name);
5342 r->file_type = file_type;
5343 r->file_version = file_version;
5345 return WERR_OK;
5348 /********************************************************************
5349 ********************************************************************/
5351 static WERROR spoolss_DriverFileInfo_from_driver(TALLOC_CTX *mem_ctx,
5352 const struct spoolss_DriverInfo8 *driver,
5353 const char *cservername,
5354 struct spoolss_DriverFileInfo **info_p,
5355 uint32_t *count_p)
5357 struct spoolss_DriverFileInfo *info = NULL;
5358 uint32_t count = 0;
5359 WERROR result;
5360 uint32_t i;
5362 *info_p = NULL;
5363 *count_p = 0;
5365 if (strlen(driver->driver_path)) {
5366 info = talloc_realloc(mem_ctx, info,
5367 struct spoolss_DriverFileInfo,
5368 count + 1);
5369 W_ERROR_HAVE_NO_MEMORY(info);
5370 result = fill_spoolss_DriverFileInfo(info,
5371 &info[count],
5372 cservername,
5373 driver->driver_path,
5374 SPOOLSS_DRIVER_FILE_TYPE_RENDERING,
5376 W_ERROR_NOT_OK_RETURN(result);
5377 count++;
5380 if (strlen(driver->config_file)) {
5381 info = talloc_realloc(mem_ctx, info,
5382 struct spoolss_DriverFileInfo,
5383 count + 1);
5384 W_ERROR_HAVE_NO_MEMORY(info);
5385 result = fill_spoolss_DriverFileInfo(info,
5386 &info[count],
5387 cservername,
5388 driver->config_file,
5389 SPOOLSS_DRIVER_FILE_TYPE_CONFIGURATION,
5391 W_ERROR_NOT_OK_RETURN(result);
5392 count++;
5395 if (strlen(driver->data_file)) {
5396 info = talloc_realloc(mem_ctx, info,
5397 struct spoolss_DriverFileInfo,
5398 count + 1);
5399 W_ERROR_HAVE_NO_MEMORY(info);
5400 result = fill_spoolss_DriverFileInfo(info,
5401 &info[count],
5402 cservername,
5403 driver->data_file,
5404 SPOOLSS_DRIVER_FILE_TYPE_DATA,
5406 W_ERROR_NOT_OK_RETURN(result);
5407 count++;
5410 if (strlen(driver->help_file)) {
5411 info = talloc_realloc(mem_ctx, info,
5412 struct spoolss_DriverFileInfo,
5413 count + 1);
5414 W_ERROR_HAVE_NO_MEMORY(info);
5415 result = fill_spoolss_DriverFileInfo(info,
5416 &info[count],
5417 cservername,
5418 driver->help_file,
5419 SPOOLSS_DRIVER_FILE_TYPE_HELP,
5421 W_ERROR_NOT_OK_RETURN(result);
5422 count++;
5425 for (i=0; driver->dependent_files[i] && driver->dependent_files[i][0] != '\0'; i++) {
5426 info = talloc_realloc(mem_ctx, info,
5427 struct spoolss_DriverFileInfo,
5428 count + 1);
5429 W_ERROR_HAVE_NO_MEMORY(info);
5430 result = fill_spoolss_DriverFileInfo(info,
5431 &info[count],
5432 cservername,
5433 driver->dependent_files[i],
5434 SPOOLSS_DRIVER_FILE_TYPE_OTHER,
5436 W_ERROR_NOT_OK_RETURN(result);
5437 count++;
5440 *info_p = info;
5441 *count_p = count;
5443 return WERR_OK;
5446 /********************************************************************
5447 * fill a spoolss_DriverInfo101 struct
5448 ********************************************************************/
5450 static WERROR fill_printer_driver_info101(TALLOC_CTX *mem_ctx,
5451 struct spoolss_DriverInfo101 *r,
5452 const struct spoolss_DriverInfo8 *driver,
5453 const char *servername)
5455 const char *cservername = canon_servername(servername);
5456 WERROR result;
5458 r->version = driver->version;
5460 r->driver_name = talloc_strdup(mem_ctx, driver->driver_name);
5461 W_ERROR_HAVE_NO_MEMORY(r->driver_name);
5462 r->architecture = talloc_strdup(mem_ctx, driver->architecture);
5463 W_ERROR_HAVE_NO_MEMORY(r->architecture);
5465 result = spoolss_DriverFileInfo_from_driver(mem_ctx, driver,
5466 cservername,
5467 &r->file_info,
5468 &r->file_count);
5469 if (!W_ERROR_IS_OK(result)) {
5470 return result;
5473 FILL_DRIVER_STRING(mem_ctx,
5474 driver->monitor_name,
5475 r->monitor_name);
5477 FILL_DRIVER_STRING(mem_ctx,
5478 driver->default_datatype,
5479 r->default_datatype);
5481 result = string_array_from_driver_info(mem_ctx,
5482 driver->previous_names,
5483 &r->previous_names,
5484 NULL, NULL, 0);
5485 if (!W_ERROR_IS_OK(result)) {
5486 return result;
5489 r->driver_date = driver->driver_date;
5490 r->driver_version = driver->driver_version;
5492 FILL_DRIVER_STRING(mem_ctx,
5493 driver->manufacturer_name,
5494 r->manufacturer_name);
5495 FILL_DRIVER_STRING(mem_ctx,
5496 driver->manufacturer_url,
5497 r->manufacturer_url);
5498 FILL_DRIVER_STRING(mem_ctx,
5499 driver->hardware_id,
5500 r->hardware_id);
5501 FILL_DRIVER_STRING(mem_ctx,
5502 driver->provider,
5503 r->provider);
5505 return WERR_OK;
5507 #endif
5508 /********************************************************************
5509 ********************************************************************/
5511 static WERROR construct_printer_driver_info_level(TALLOC_CTX *mem_ctx,
5512 const struct auth_session_info *session_info,
5513 struct messaging_context *msg_ctx,
5514 uint32_t level,
5515 union spoolss_DriverInfo *r,
5516 int snum,
5517 const char *servername,
5518 const char *architecture,
5519 uint32_t version)
5521 struct spoolss_PrinterInfo2 *pinfo2 = NULL;
5522 struct spoolss_DriverInfo8 *driver;
5523 WERROR result;
5524 struct dcerpc_binding_handle *b;
5525 TALLOC_CTX *tmp_ctx = NULL;
5527 if (level == 101) {
5528 return WERR_UNKNOWN_LEVEL;
5531 tmp_ctx = talloc_new(mem_ctx);
5532 if (!tmp_ctx) {
5533 return WERR_NOMEM;
5536 result = winreg_printer_binding_handle(tmp_ctx,
5537 session_info,
5538 msg_ctx,
5539 &b);
5540 if (!W_ERROR_IS_OK(result)) {
5541 goto done;
5544 result = winreg_get_printer(tmp_ctx, b,
5545 lp_const_servicename(snum),
5546 &pinfo2);
5548 DEBUG(8,("construct_printer_driver_info_level: status: %s\n",
5549 win_errstr(result)));
5551 if (!W_ERROR_IS_OK(result)) {
5552 result = WERR_INVALID_PRINTER_NAME;
5553 goto done;
5556 result = winreg_get_driver(tmp_ctx, b,
5557 architecture,
5558 pinfo2->drivername, version, &driver);
5560 DEBUG(8,("construct_printer_driver_info_level: status: %s\n",
5561 win_errstr(result)));
5563 if (!W_ERROR_IS_OK(result)) {
5565 * Is this a W2k client ?
5568 if (version < 3) {
5569 result = WERR_UNKNOWN_PRINTER_DRIVER;
5570 goto done;
5573 /* Yes - try again with a WinNT driver. */
5574 version = 2;
5575 result = winreg_get_driver(tmp_ctx, b,
5576 architecture,
5577 pinfo2->drivername,
5578 version, &driver);
5579 DEBUG(8,("construct_printer_driver_level: status: %s\n",
5580 win_errstr(result)));
5581 if (!W_ERROR_IS_OK(result)) {
5582 result = WERR_UNKNOWN_PRINTER_DRIVER;
5583 goto done;
5587 /* these are allocated on mem_ctx and not tmp_ctx because they are
5588 * the 'return value' and need to utlive this call */
5589 switch (level) {
5590 case 1:
5591 result = fill_printer_driver_info1(mem_ctx, &r->info1, driver, servername);
5592 break;
5593 case 2:
5594 result = fill_printer_driver_info2(mem_ctx, &r->info2, driver, servername);
5595 break;
5596 case 3:
5597 result = fill_printer_driver_info3(mem_ctx, &r->info3, driver, servername);
5598 break;
5599 case 4:
5600 result = fill_printer_driver_info4(mem_ctx, &r->info4, driver, servername);
5601 break;
5602 case 5:
5603 result = fill_printer_driver_info5(mem_ctx, &r->info5, driver, servername);
5604 break;
5605 case 6:
5606 result = fill_printer_driver_info6(mem_ctx, &r->info6, driver, servername);
5607 break;
5608 case 8:
5609 result = fill_printer_driver_info8(mem_ctx, &r->info8, driver, servername);
5610 break;
5611 #if 0 /* disabled until marshalling issues are resolved - gd */
5612 case 101:
5613 result = fill_printer_driver_info101(mem_ctx, &r->info101, driver, servername);
5614 break;
5615 #endif
5616 default:
5617 result = WERR_UNKNOWN_LEVEL;
5618 break;
5621 done:
5622 talloc_free(tmp_ctx);
5623 return result;
5626 /****************************************************************
5627 _spoolss_GetPrinterDriver2
5628 ****************************************************************/
5630 WERROR _spoolss_GetPrinterDriver2(struct pipes_struct *p,
5631 struct spoolss_GetPrinterDriver2 *r)
5633 struct printer_handle *printer;
5634 WERROR result;
5635 uint32_t version = r->in.client_major_version;
5637 int snum;
5639 /* that's an [in out] buffer */
5641 if (!r->in.buffer && (r->in.offered != 0)) {
5642 return WERR_INVALID_PARAM;
5645 DEBUG(4,("_spoolss_GetPrinterDriver2\n"));
5647 if (!(printer = find_printer_index_by_hnd(p, r->in.handle))) {
5648 DEBUG(0,("_spoolss_GetPrinterDriver2: invalid printer handle!\n"));
5649 return WERR_INVALID_PRINTER_NAME;
5652 *r->out.needed = 0;
5653 *r->out.server_major_version = 0;
5654 *r->out.server_minor_version = 0;
5656 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
5657 return WERR_BADFID;
5660 if (r->in.client_major_version == SPOOLSS_DRIVER_VERSION_2012) {
5661 DEBUG(3,("_spoolss_GetPrinterDriver2: v4 driver requested, "
5662 "downgrading to v3\n"));
5663 version = SPOOLSS_DRIVER_VERSION_200X;
5666 result = construct_printer_driver_info_level(p->mem_ctx,
5667 get_session_info_system(),
5668 p->msg_ctx,
5669 r->in.level, r->out.info,
5670 snum, printer->servername,
5671 r->in.architecture,
5672 version);
5673 if (!W_ERROR_IS_OK(result)) {
5674 TALLOC_FREE(r->out.info);
5675 return result;
5678 *r->out.needed = SPOOLSS_BUFFER_UNION(spoolss_DriverInfo,
5679 r->out.info, r->in.level);
5680 r->out.info = SPOOLSS_BUFFER_OK(r->out.info, NULL);
5682 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
5686 /****************************************************************
5687 _spoolss_StartPagePrinter
5688 ****************************************************************/
5690 WERROR _spoolss_StartPagePrinter(struct pipes_struct *p,
5691 struct spoolss_StartPagePrinter *r)
5693 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
5695 if (!Printer) {
5696 DEBUG(3,("_spoolss_StartPagePrinter: "
5697 "Error in startpageprinter printer handle\n"));
5698 return WERR_BADFID;
5701 Printer->page_started = true;
5702 return WERR_OK;
5705 /****************************************************************
5706 _spoolss_EndPagePrinter
5707 ****************************************************************/
5709 WERROR _spoolss_EndPagePrinter(struct pipes_struct *p,
5710 struct spoolss_EndPagePrinter *r)
5712 int snum;
5714 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
5716 if (!Printer) {
5717 DEBUG(2,("_spoolss_EndPagePrinter: Invalid handle (%s:%u:%u).\n",
5718 OUR_HANDLE(r->in.handle)));
5719 return WERR_BADFID;
5722 if (!get_printer_snum(p, r->in.handle, &snum, NULL))
5723 return WERR_BADFID;
5725 Printer->page_started = false;
5726 print_job_endpage(p->msg_ctx, snum, Printer->jobid);
5728 return WERR_OK;
5731 /****************************************************************
5732 _spoolss_StartDocPrinter
5733 ****************************************************************/
5735 WERROR _spoolss_StartDocPrinter(struct pipes_struct *p,
5736 struct spoolss_StartDocPrinter *r)
5738 struct spoolss_DocumentInfo1 *info_1;
5739 int snum;
5740 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
5741 WERROR werr;
5742 char *rhost;
5743 int rc;
5745 if (!Printer) {
5746 DEBUG(2,("_spoolss_StartDocPrinter: "
5747 "Invalid handle (%s:%u:%u)\n",
5748 OUR_HANDLE(r->in.handle)));
5749 return WERR_BADFID;
5752 if (Printer->jobid) {
5753 DEBUG(2, ("_spoolss_StartDocPrinter: "
5754 "StartDocPrinter called twice! "
5755 "(existing jobid = %d)\n", Printer->jobid));
5756 return WERR_INVALID_HANDLE;
5759 if (r->in.level != 1) {
5760 return WERR_UNKNOWN_LEVEL;
5763 info_1 = r->in.info.info1;
5766 * a nice thing with NT is it doesn't listen to what you tell it.
5767 * when asked to send _only_ RAW datas, it tries to send datas
5768 * in EMF format.
5770 * So I add checks like in NT Server ...
5773 if (info_1->datatype) {
5775 * The v4 driver model used in Windows 8 declares print jobs
5776 * intended to bypass the XPS processing layer by setting
5777 * datatype to "XPS_PASS" instead of "RAW".
5779 if ((strcmp(info_1->datatype, "RAW") != 0)
5780 && (strcmp(info_1->datatype, "XPS_PASS") != 0)) {
5781 *r->out.job_id = 0;
5782 return WERR_INVALID_DATATYPE;
5786 /* get the share number of the printer */
5787 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
5788 return WERR_BADFID;
5791 rc = get_remote_hostname(p->remote_address,
5792 &rhost,
5793 p->mem_ctx);
5794 if (rc < 0) {
5795 return WERR_NOMEM;
5797 if (strequal(rhost,"UNKNOWN")) {
5798 rhost = tsocket_address_inet_addr_string(p->remote_address,
5799 p->mem_ctx);
5800 if (rhost == NULL) {
5801 return WERR_NOMEM;
5805 werr = print_job_start(p->session_info,
5806 p->msg_ctx,
5807 rhost,
5808 snum,
5809 info_1->document_name,
5810 info_1->output_file,
5811 Printer->devmode,
5812 &Printer->jobid);
5814 /* An error occured in print_job_start() so return an appropriate
5815 NT error code. */
5817 if (!W_ERROR_IS_OK(werr)) {
5818 return werr;
5821 Printer->document_started = true;
5822 *r->out.job_id = Printer->jobid;
5824 return WERR_OK;
5827 /****************************************************************
5828 _spoolss_EndDocPrinter
5829 ****************************************************************/
5831 WERROR _spoolss_EndDocPrinter(struct pipes_struct *p,
5832 struct spoolss_EndDocPrinter *r)
5834 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
5835 NTSTATUS status;
5836 int snum;
5838 if (!Printer) {
5839 DEBUG(2,("_spoolss_EndDocPrinter: Invalid handle (%s:%u:%u)\n",
5840 OUR_HANDLE(r->in.handle)));
5841 return WERR_BADFID;
5844 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
5845 return WERR_BADFID;
5848 Printer->document_started = false;
5849 status = print_job_end(p->msg_ctx, snum, Printer->jobid, NORMAL_CLOSE);
5850 if (!NT_STATUS_IS_OK(status)) {
5851 DEBUG(2, ("_spoolss_EndDocPrinter: "
5852 "print_job_end failed [%s]\n",
5853 nt_errstr(status)));
5856 Printer->jobid = 0;
5857 return ntstatus_to_werror(status);
5860 /****************************************************************
5861 _spoolss_WritePrinter
5862 ****************************************************************/
5864 WERROR _spoolss_WritePrinter(struct pipes_struct *p,
5865 struct spoolss_WritePrinter *r)
5867 ssize_t buffer_written;
5868 int snum;
5869 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
5871 if (!Printer) {
5872 DEBUG(2,("_spoolss_WritePrinter: Invalid handle (%s:%u:%u)\n",
5873 OUR_HANDLE(r->in.handle)));
5874 *r->out.num_written = r->in._data_size;
5875 return WERR_BADFID;
5878 if (!get_printer_snum(p, r->in.handle, &snum, NULL))
5879 return WERR_BADFID;
5881 /* print_job_write takes care of checking for PJOB_SMBD_SPOOLING */
5882 buffer_written = print_job_write(server_event_context(),p->msg_ctx,
5883 snum, Printer->jobid,
5884 (const char *)r->in.data.data,
5885 (size_t)r->in._data_size);
5886 if (buffer_written == (ssize_t)-1) {
5887 *r->out.num_written = 0;
5888 if (errno == ENOSPC)
5889 return WERR_NO_SPOOL_SPACE;
5890 else
5891 return WERR_ACCESS_DENIED;
5894 *r->out.num_written = r->in._data_size;
5896 return WERR_OK;
5899 /********************************************************************
5900 * api_spoolss_getprinter
5901 * called from the spoolss dispatcher
5903 ********************************************************************/
5905 static WERROR control_printer(struct policy_handle *handle, uint32_t command,
5906 struct pipes_struct *p)
5908 const struct auth_session_info *session_info = p->session_info;
5909 int snum;
5910 WERROR errcode = WERR_BADFUNC;
5911 struct printer_handle *Printer = find_printer_index_by_hnd(p, handle);
5913 if (!Printer) {
5914 DEBUG(2,("control_printer: Invalid handle (%s:%u:%u)\n",
5915 OUR_HANDLE(handle)));
5916 return WERR_BADFID;
5919 if (!get_printer_snum(p, handle, &snum, NULL))
5920 return WERR_BADFID;
5922 switch (command) {
5923 case SPOOLSS_PRINTER_CONTROL_PAUSE:
5924 errcode = print_queue_pause(session_info, p->msg_ctx, snum);
5925 break;
5926 case SPOOLSS_PRINTER_CONTROL_RESUME:
5927 case SPOOLSS_PRINTER_CONTROL_UNPAUSE:
5928 errcode = print_queue_resume(session_info, p->msg_ctx, snum);
5929 break;
5930 case SPOOLSS_PRINTER_CONTROL_PURGE:
5931 errcode = print_queue_purge(session_info, p->msg_ctx, snum);
5932 break;
5933 default:
5934 return WERR_UNKNOWN_LEVEL;
5937 return errcode;
5941 /****************************************************************
5942 _spoolss_AbortPrinter
5943 * From MSDN: "Deletes printer's spool file if printer is configured
5944 * for spooling"
5945 ****************************************************************/
5947 WERROR _spoolss_AbortPrinter(struct pipes_struct *p,
5948 struct spoolss_AbortPrinter *r)
5950 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
5951 int snum;
5952 WERROR errcode = WERR_OK;
5954 if (!Printer) {
5955 DEBUG(2,("_spoolss_AbortPrinter: Invalid handle (%s:%u:%u)\n",
5956 OUR_HANDLE(r->in.handle)));
5957 return WERR_BADFID;
5960 if (!get_printer_snum(p, r->in.handle, &snum, NULL))
5961 return WERR_BADFID;
5963 if (!Printer->document_started) {
5964 return WERR_SPL_NO_STARTDOC;
5967 errcode = print_job_delete(p->session_info,
5968 p->msg_ctx,
5969 snum,
5970 Printer->jobid);
5972 return errcode;
5975 /********************************************************************
5976 * called by spoolss_api_setprinter
5977 * when updating a printer description
5978 ********************************************************************/
5980 static WERROR update_printer_sec(struct policy_handle *handle,
5981 struct pipes_struct *p,
5982 struct sec_desc_buf *secdesc_ctr)
5984 struct spoolss_security_descriptor *new_secdesc = NULL;
5985 struct spoolss_security_descriptor *old_secdesc = NULL;
5986 const char *printer;
5987 WERROR result;
5988 int snum;
5989 struct printer_handle *Printer = find_printer_index_by_hnd(p, handle);
5990 struct dcerpc_binding_handle *b;
5991 TALLOC_CTX *tmp_ctx = NULL;
5993 if (!Printer || !get_printer_snum(p, handle, &snum, NULL)) {
5994 DEBUG(2,("update_printer_sec: Invalid handle (%s:%u:%u)\n",
5995 OUR_HANDLE(handle)));
5997 result = WERR_BADFID;
5998 goto done;
6001 if (secdesc_ctr == NULL) {
6002 DEBUG(10,("update_printer_sec: secdesc_ctr is NULL !\n"));
6003 result = WERR_INVALID_PARAM;
6004 goto done;
6006 printer = lp_const_servicename(snum);
6008 /* Check the user has permissions to change the security
6009 descriptor. By experimentation with two NT machines, the user
6010 requires Full Access to the printer to change security
6011 information. */
6013 if ( Printer->access_granted != PRINTER_ACCESS_ADMINISTER ) {
6014 DEBUG(4,("update_printer_sec: updated denied by printer permissions\n"));
6015 result = WERR_ACCESS_DENIED;
6016 goto done;
6019 tmp_ctx = talloc_new(p->mem_ctx);
6020 if (!tmp_ctx) {
6021 return WERR_NOMEM;
6024 result = winreg_printer_binding_handle(tmp_ctx,
6025 get_session_info_system(),
6026 p->msg_ctx,
6027 &b);
6028 if (!W_ERROR_IS_OK(result)) {
6029 goto done;
6032 /* NT seems to like setting the security descriptor even though
6033 nothing may have actually changed. */
6034 result = winreg_get_printer_secdesc(tmp_ctx, b,
6035 printer,
6036 &old_secdesc);
6037 if (!W_ERROR_IS_OK(result)) {
6038 DEBUG(2,("update_printer_sec: winreg_get_printer_secdesc_internal() failed\n"));
6039 result = WERR_BADFID;
6040 goto done;
6043 if (DEBUGLEVEL >= 10) {
6044 struct security_acl *the_acl;
6045 int i;
6047 the_acl = old_secdesc->dacl;
6048 DEBUG(10, ("old_secdesc_ctr for %s has %d aces:\n",
6049 printer, the_acl->num_aces));
6051 for (i = 0; i < the_acl->num_aces; i++) {
6052 DEBUG(10, ("%s 0x%08x\n", sid_string_dbg(
6053 &the_acl->aces[i].trustee),
6054 the_acl->aces[i].access_mask));
6057 the_acl = secdesc_ctr->sd->dacl;
6059 if (the_acl) {
6060 DEBUG(10, ("secdesc_ctr for %s has %d aces:\n",
6061 printer, the_acl->num_aces));
6063 for (i = 0; i < the_acl->num_aces; i++) {
6064 DEBUG(10, ("%s 0x%08x\n", sid_string_dbg(
6065 &the_acl->aces[i].trustee),
6066 the_acl->aces[i].access_mask));
6068 } else {
6069 DEBUG(10, ("dacl for secdesc_ctr is NULL\n"));
6073 new_secdesc = sec_desc_merge(tmp_ctx, secdesc_ctr->sd, old_secdesc);
6074 if (new_secdesc == NULL) {
6075 result = WERR_NOMEM;
6076 goto done;
6079 if (security_descriptor_equal(new_secdesc, old_secdesc)) {
6080 result = WERR_OK;
6081 goto done;
6084 result = winreg_set_printer_secdesc(tmp_ctx, b,
6085 printer,
6086 new_secdesc);
6088 done:
6089 talloc_free(tmp_ctx);
6090 return result;
6093 /********************************************************************
6094 Canonicalize printer info from a client
6095 ********************************************************************/
6097 static bool check_printer_ok(TALLOC_CTX *mem_ctx,
6098 struct spoolss_SetPrinterInfo2 *info2,
6099 int snum)
6101 fstring printername;
6102 const char *p;
6104 DEBUG(5,("check_printer_ok: servername=%s printername=%s sharename=%s "
6105 "portname=%s drivername=%s comment=%s location=%s\n",
6106 info2->servername, info2->printername, info2->sharename,
6107 info2->portname, info2->drivername, info2->comment,
6108 info2->location));
6110 /* we force some elements to "correct" values */
6111 info2->servername = talloc_asprintf(mem_ctx, "\\\\%s", lp_netbios_name());
6112 if (info2->servername == NULL) {
6113 return false;
6115 info2->sharename = talloc_strdup(mem_ctx, lp_const_servicename(snum));
6116 if (info2->sharename == NULL) {
6117 return false;
6120 /* check to see if we allow printername != sharename */
6121 if (lp_force_printername(snum)) {
6122 info2->printername = talloc_asprintf(mem_ctx, "\\\\%s\\%s",
6123 lp_netbios_name(), info2->sharename);
6124 } else {
6125 /* make sure printername is in \\server\printername format */
6126 fstrcpy(printername, info2->printername);
6127 p = printername;
6128 if ( printername[0] == '\\' && printername[1] == '\\' ) {
6129 if ( (p = strchr_m( &printername[2], '\\' )) != NULL )
6130 p++;
6133 info2->printername = talloc_asprintf(mem_ctx, "\\\\%s\\%s",
6134 lp_netbios_name(), p);
6136 if (info2->printername == NULL) {
6137 return false;
6140 info2->attributes |= PRINTER_ATTRIBUTE_SAMBA;
6141 info2->attributes &= ~PRINTER_ATTRIBUTE_NOT_SAMBA;
6143 return true;
6146 /****************************************************************************
6147 ****************************************************************************/
6149 static WERROR add_port_hook(TALLOC_CTX *ctx, struct security_token *token, const char *portname, const char *uri)
6151 char *cmd = lp_addport_cmd(talloc_tos());
6152 char *command = NULL;
6153 int ret;
6154 bool is_print_op = false;
6156 if ( !*cmd ) {
6157 return WERR_ACCESS_DENIED;
6160 command = talloc_asprintf(ctx,
6161 "%s \"%s\" \"%s\"", cmd, portname, uri );
6162 if (!command) {
6163 return WERR_NOMEM;
6166 if ( token )
6167 is_print_op = security_token_has_privilege(token, SEC_PRIV_PRINT_OPERATOR);
6169 DEBUG(10,("Running [%s]\n", command));
6171 /********* BEGIN SePrintOperatorPrivilege **********/
6173 if ( is_print_op )
6174 become_root();
6176 ret = smbrun(command, NULL);
6178 if ( is_print_op )
6179 unbecome_root();
6181 /********* END SePrintOperatorPrivilege **********/
6183 DEBUGADD(10,("returned [%d]\n", ret));
6185 TALLOC_FREE(command);
6187 if ( ret != 0 ) {
6188 return WERR_ACCESS_DENIED;
6191 return WERR_OK;
6194 /****************************************************************************
6195 ****************************************************************************/
6197 static bool spoolss_conn_snum_used(struct smbd_server_connection *sconn,
6198 int snum)
6201 * As we do not know if we are embedded in the file server process
6202 * or not, we have to pretend that all shares are in use.
6204 return true;
6207 static bool add_printer_hook(TALLOC_CTX *ctx, struct security_token *token,
6208 struct spoolss_SetPrinterInfo2 *info2,
6209 const char *remote_machine,
6210 struct messaging_context *msg_ctx)
6212 char *cmd = lp_addprinter_cmd(talloc_tos());
6213 char **qlines;
6214 char *command = NULL;
6215 int numlines;
6216 int ret;
6217 int fd;
6218 bool is_print_op = false;
6220 if (!remote_machine) {
6221 return false;
6224 command = talloc_asprintf(ctx,
6225 "%s \"%s\" \"%s\" \"%s\" \"%s\" \"%s\" \"%s\" \"%s\"",
6226 cmd, info2->printername, info2->sharename,
6227 info2->portname, info2->drivername,
6228 info2->location, info2->comment, remote_machine);
6229 if (!command) {
6230 return false;
6233 if ( token )
6234 is_print_op = security_token_has_privilege(token, SEC_PRIV_PRINT_OPERATOR);
6236 DEBUG(10,("Running [%s]\n", command));
6238 /********* BEGIN SePrintOperatorPrivilege **********/
6240 if ( is_print_op )
6241 become_root();
6243 if ( (ret = smbrun(command, &fd)) == 0 ) {
6244 /* Tell everyone we updated smb.conf. */
6245 message_send_all(msg_ctx, MSG_SMB_CONF_UPDATED, NULL, 0, NULL);
6248 if ( is_print_op )
6249 unbecome_root();
6251 /********* END SePrintOperatorPrivilege **********/
6253 DEBUGADD(10,("returned [%d]\n", ret));
6255 TALLOC_FREE(command);
6257 if ( ret != 0 ) {
6258 if (fd != -1)
6259 close(fd);
6260 return false;
6263 /* reload our services immediately */
6264 become_root();
6265 reload_services(NULL, spoolss_conn_snum_used, false);
6266 unbecome_root();
6268 numlines = 0;
6269 /* Get lines and convert them back to dos-codepage */
6270 qlines = fd_lines_load(fd, &numlines, 0, NULL);
6271 DEBUGADD(10,("Lines returned = [%d]\n", numlines));
6272 close(fd);
6274 /* Set the portname to what the script says the portname should be. */
6275 /* but don't require anything to be return from the script exit a good error code */
6277 if (numlines) {
6278 /* Set the portname to what the script says the portname should be. */
6279 info2->portname = talloc_strdup(ctx, qlines[0]);
6280 DEBUGADD(6,("Line[0] = [%s]\n", qlines[0]));
6283 TALLOC_FREE(qlines);
6284 return true;
6287 static WERROR update_dsspooler(TALLOC_CTX *mem_ctx,
6288 const struct auth_session_info *session_info,
6289 struct messaging_context *msg_ctx,
6290 int snum,
6291 struct spoolss_SetPrinterInfo2 *printer,
6292 struct spoolss_PrinterInfo2 *old_printer)
6294 bool force_update = (old_printer == NULL);
6295 const char *dnsdomname;
6296 const char *longname;
6297 const char *uncname;
6298 const char *spooling;
6299 DATA_BLOB buffer;
6300 WERROR result = WERR_OK;
6301 struct dcerpc_binding_handle *b;
6302 TALLOC_CTX *tmp_ctx;
6303 bool ok;
6305 tmp_ctx = talloc_new(mem_ctx);
6306 if (!tmp_ctx) {
6307 return WERR_NOMEM;
6310 result = winreg_printer_binding_handle(tmp_ctx,
6311 session_info,
6312 msg_ctx,
6313 &b);
6314 if (!W_ERROR_IS_OK(result)) {
6315 goto done;
6318 if (printer->drivername != NULL &&
6319 (force_update ||
6320 !strequal(printer->drivername, old_printer->drivername))) {
6321 ok = push_reg_sz(tmp_ctx, &buffer, printer->drivername);
6322 if (!ok) {
6323 DEBUG(0, ("%s data corrupted\n", SPOOL_REG_DRIVERNAME));
6324 result = WERR_INVALID_DATA;
6325 goto done;
6327 result = winreg_set_printer_dataex(tmp_ctx, b,
6328 printer->sharename,
6329 SPOOL_DSSPOOLER_KEY,
6330 SPOOL_REG_DRIVERNAME,
6331 REG_SZ,
6332 buffer.data,
6333 buffer.length);
6334 if (!W_ERROR_IS_OK(result)) {
6335 DEBUG(0, ("Failed to set %s\n", SPOOL_REG_DRIVERNAME));
6336 goto done;
6339 if (!force_update) {
6340 DEBUG(10,("update_printer: changing driver [%s]! Sending event!\n",
6341 printer->drivername));
6343 notify_printer_driver(server_event_context(), msg_ctx,
6344 snum, printer->drivername ?
6345 printer->drivername : "");
6349 if (printer->comment != NULL &&
6350 (force_update ||
6351 !strequal(printer->comment, old_printer->comment))) {
6352 ok = push_reg_sz(tmp_ctx, &buffer, printer->comment);
6353 if (!ok) {
6354 DEBUG(0, ("comment data corrupted\n"));
6355 result = WERR_INVALID_DATA;
6356 goto done;
6358 result = winreg_set_printer_dataex(tmp_ctx, b,
6359 printer->sharename,
6360 SPOOL_DSSPOOLER_KEY,
6361 SPOOL_REG_DESCRIPTION,
6362 REG_SZ,
6363 buffer.data,
6364 buffer.length);
6365 if (!W_ERROR_IS_OK(result)) {
6366 DEBUG(0, ("Failed to set %s\n", SPOOL_REG_DESCRIPTION));
6367 goto done;
6370 if (!force_update) {
6371 notify_printer_comment(server_event_context(), msg_ctx,
6372 snum, printer->comment ?
6373 printer->comment : "");
6377 if (printer->sharename != NULL &&
6378 (force_update ||
6379 !strequal(printer->sharename, old_printer->sharename))) {
6380 ok = push_reg_sz(tmp_ctx, &buffer, printer->sharename);
6381 if (!ok) {
6382 DEBUG(0, ("sharename data corrupted\n"));
6383 result = WERR_INVALID_DATA;
6384 goto done;
6386 result = winreg_set_printer_dataex(tmp_ctx, b,
6387 printer->sharename,
6388 SPOOL_DSSPOOLER_KEY,
6389 SPOOL_REG_PRINTSHARENAME,
6390 REG_SZ,
6391 buffer.data,
6392 buffer.length);
6393 if (!W_ERROR_IS_OK(result)) {
6394 DEBUG(0, ("Failed to set %s\n", SPOOL_REG_PRINTSHARENAME));
6395 goto done;
6398 if (!force_update) {
6399 notify_printer_sharename(server_event_context(),
6400 msg_ctx,
6401 snum, printer->sharename ?
6402 printer->sharename : "");
6406 if (printer->printername != NULL &&
6407 (force_update ||
6408 !strequal(printer->printername, old_printer->printername))) {
6409 const char *p;
6411 p = strrchr(printer->printername, '\\' );
6412 if (p != NULL) {
6413 p++;
6414 } else {
6415 p = printer->printername;
6418 ok = push_reg_sz(tmp_ctx, &buffer, p);
6419 if (!ok) {
6420 DEBUG(0, ("printername data corrupted\n"));
6421 result = WERR_INVALID_DATA;
6422 goto done;
6424 result = winreg_set_printer_dataex(tmp_ctx, b,
6425 printer->sharename,
6426 SPOOL_DSSPOOLER_KEY,
6427 SPOOL_REG_PRINTERNAME,
6428 REG_SZ,
6429 buffer.data,
6430 buffer.length);
6431 if (!W_ERROR_IS_OK(result)) {
6432 DEBUG(0, ("Failed to set %s\n", SPOOL_REG_PRINTSHARENAME));
6433 goto done;
6436 if (!force_update) {
6437 notify_printer_printername(server_event_context(),
6438 msg_ctx, snum, p ? p : "");
6442 if (printer->portname != NULL &&
6443 (force_update ||
6444 !strequal(printer->portname, old_printer->portname))) {
6445 ok = push_reg_sz(tmp_ctx, &buffer, printer->portname);
6446 if (!ok) {
6447 DEBUG(0, ("portname data corrupted\n"));
6448 result = WERR_INVALID_DATA;
6449 goto done;
6451 result = winreg_set_printer_dataex(tmp_ctx, b,
6452 printer->sharename,
6453 SPOOL_DSSPOOLER_KEY,
6454 SPOOL_REG_PORTNAME,
6455 REG_SZ,
6456 buffer.data,
6457 buffer.length);
6458 if (!W_ERROR_IS_OK(result)) {
6459 DEBUG(0, ("Failed to set %s\n", SPOOL_REG_PORTNAME));
6460 goto done;
6463 if (!force_update) {
6464 notify_printer_port(server_event_context(),
6465 msg_ctx, snum, printer->portname ?
6466 printer->portname : "");
6470 if (printer->location != NULL &&
6471 (force_update ||
6472 !strequal(printer->location, old_printer->location))) {
6473 ok = push_reg_sz(tmp_ctx, &buffer, printer->location);
6474 if (!ok) {
6475 DEBUG(0, ("location data corrupted\n"));
6476 result = WERR_INVALID_DATA;
6477 goto done;
6479 result = winreg_set_printer_dataex(tmp_ctx, b,
6480 printer->sharename,
6481 SPOOL_DSSPOOLER_KEY,
6482 SPOOL_REG_LOCATION,
6483 REG_SZ,
6484 buffer.data,
6485 buffer.length);
6486 if (!W_ERROR_IS_OK(result)) {
6487 DEBUG(0, ("Failed to set %s\n", SPOOL_REG_LOCATION));
6488 goto done;
6491 if (!force_update) {
6492 notify_printer_location(server_event_context(),
6493 msg_ctx, snum,
6494 printer->location ?
6495 printer->location : "");
6499 if (printer->sepfile != NULL &&
6500 (force_update ||
6501 !strequal(printer->sepfile, old_printer->sepfile))) {
6502 ok = push_reg_sz(tmp_ctx, &buffer, printer->sepfile);
6503 if (!ok) {
6504 DEBUG(0, ("sepfile data corrupted\n"));
6505 result = WERR_INVALID_DATA;
6506 goto done;
6508 result = winreg_set_printer_dataex(tmp_ctx, b,
6509 printer->sharename,
6510 SPOOL_DSSPOOLER_KEY,
6511 SPOOL_REG_PRINTSEPARATORFILE,
6512 REG_SZ,
6513 buffer.data,
6514 buffer.length);
6515 if (!W_ERROR_IS_OK(result)) {
6516 DEBUG(0, ("Failed to set %s\n", SPOOL_REG_PRINTSEPARATORFILE));
6517 goto done;
6520 if (!force_update) {
6521 notify_printer_sepfile(server_event_context(),
6522 msg_ctx, snum,
6523 printer->sepfile ?
6524 printer->sepfile : "");
6528 if (printer->starttime != 0 &&
6529 (force_update ||
6530 printer->starttime != old_printer->starttime)) {
6531 buffer = data_blob_talloc(tmp_ctx, NULL, 4);
6532 SIVAL(buffer.data, 0, printer->starttime);
6533 result = winreg_set_printer_dataex(tmp_ctx, b,
6534 printer->sharename,
6535 SPOOL_DSSPOOLER_KEY,
6536 SPOOL_REG_PRINTSTARTTIME,
6537 REG_DWORD,
6538 buffer.data,
6539 buffer.length);
6540 if (!W_ERROR_IS_OK(result)) {
6541 DEBUG(0, ("Failed to set %s\n", SPOOL_REG_PRINTSTARTTIME));
6542 goto done;
6546 if (printer->untiltime != 0 &&
6547 (force_update ||
6548 printer->untiltime != old_printer->untiltime)) {
6549 buffer = data_blob_talloc(tmp_ctx, NULL, 4);
6550 SIVAL(buffer.data, 0, printer->untiltime);
6551 result = winreg_set_printer_dataex(tmp_ctx, b,
6552 printer->sharename,
6553 SPOOL_DSSPOOLER_KEY,
6554 SPOOL_REG_PRINTENDTIME,
6555 REG_DWORD,
6556 buffer.data,
6557 buffer.length);
6558 if (!W_ERROR_IS_OK(result)) {
6559 DEBUG(0, ("Failed to set %s\n", SPOOL_REG_PRINTENDTIME));
6560 goto done;
6564 if (force_update || printer->priority != old_printer->priority) {
6565 buffer = data_blob_talloc(tmp_ctx, NULL, 4);
6566 SIVAL(buffer.data, 0, printer->priority);
6567 result = winreg_set_printer_dataex(tmp_ctx, b,
6568 printer->sharename,
6569 SPOOL_DSSPOOLER_KEY,
6570 SPOOL_REG_PRIORITY,
6571 REG_DWORD,
6572 buffer.data,
6573 buffer.length);
6574 if (!W_ERROR_IS_OK(result)) {
6575 DEBUG(0, ("Failed to set %s\n", SPOOL_REG_PRINTENDTIME));
6576 goto done;
6580 if (force_update || printer->attributes != old_printer->attributes) {
6581 buffer = data_blob_talloc(tmp_ctx, NULL, 4);
6582 SIVAL(buffer.data, 0, (printer->attributes &
6583 PRINTER_ATTRIBUTE_KEEPPRINTEDJOBS));
6584 result = winreg_set_printer_dataex(tmp_ctx, b,
6585 printer->sharename,
6586 SPOOL_DSSPOOLER_KEY,
6587 SPOOL_REG_PRINTKEEPPRINTEDJOBS,
6588 REG_DWORD,
6589 buffer.data,
6590 buffer.length);
6591 if (!W_ERROR_IS_OK(result)) {
6592 DEBUG(0, ("Failed to set %s\n", SPOOL_REG_PRINTENDTIME));
6593 goto done;
6596 switch (printer->attributes & 0x3) {
6597 case 0:
6598 spooling = SPOOL_REGVAL_PRINTWHILESPOOLING;
6599 break;
6600 case 1:
6601 spooling = SPOOL_REGVAL_PRINTAFTERSPOOLED;
6602 break;
6603 case 2:
6604 spooling = SPOOL_REGVAL_PRINTDIRECT;
6605 break;
6606 default:
6607 spooling = "unknown";
6609 ok = push_reg_sz(tmp_ctx, &buffer, spooling);
6610 if (!ok) {
6611 DEBUG(0, ("printSpooling data corrupted\n"));
6612 result = WERR_INVALID_DATA;
6613 goto done;
6615 winreg_set_printer_dataex(tmp_ctx, b,
6616 printer->sharename,
6617 SPOOL_DSSPOOLER_KEY,
6618 SPOOL_REG_PRINTSPOOLING,
6619 REG_SZ,
6620 buffer.data,
6621 buffer.length);
6624 ok = push_reg_sz(tmp_ctx, &buffer, lp_netbios_name());
6625 if (!ok) {
6626 DEBUG(0, ("shortServerName data corrupted\n"));
6627 result = WERR_INVALID_DATA;
6628 goto done;
6630 result = winreg_set_printer_dataex(tmp_ctx, b,
6631 printer->sharename,
6632 SPOOL_DSSPOOLER_KEY,
6633 SPOOL_REG_SHORTSERVERNAME,
6634 REG_SZ,
6635 buffer.data,
6636 buffer.length);
6637 if (!W_ERROR_IS_OK(result)) {
6638 DEBUG(0, ("Failed to set %s\n", SPOOL_REG_SHORTSERVERNAME));
6639 goto done;
6642 dnsdomname = get_mydnsfullname();
6643 if (dnsdomname != NULL && dnsdomname[0] != '\0') {
6644 longname = talloc_strdup(tmp_ctx, dnsdomname);
6645 } else {
6646 longname = talloc_strdup(tmp_ctx, lp_netbios_name());
6648 if (longname == NULL) {
6649 result = WERR_NOMEM;
6650 goto done;
6653 ok = push_reg_sz(tmp_ctx, &buffer, longname);
6654 if (!ok) {
6655 DEBUG(0, ("longname data corrupted\n"));
6656 result = WERR_INVALID_DATA;
6657 goto done;
6659 result = winreg_set_printer_dataex(tmp_ctx, b,
6660 printer->sharename,
6661 SPOOL_DSSPOOLER_KEY,
6662 SPOOL_REG_SERVERNAME,
6663 REG_SZ,
6664 buffer.data,
6665 buffer.length);
6666 if (!W_ERROR_IS_OK(result)) {
6667 DEBUG(0, ("Failed to set %s\n", SPOOL_REG_SERVERNAME));
6668 goto done;
6671 uncname = talloc_asprintf(tmp_ctx, "\\\\%s\\%s",
6672 lp_netbios_name(), printer->sharename);
6673 ok = push_reg_sz(tmp_ctx, &buffer, uncname);
6674 if (!ok) {
6675 DEBUG(0, ("uncName data corrupted\n"));
6676 result = WERR_INVALID_DATA;
6677 goto done;
6679 result = winreg_set_printer_dataex(tmp_ctx, b,
6680 printer->sharename,
6681 SPOOL_DSSPOOLER_KEY,
6682 SPOOL_REG_UNCNAME,
6683 REG_SZ,
6684 buffer.data,
6685 buffer.length);
6686 if (!W_ERROR_IS_OK(result)) {
6687 DEBUG(0, ("Failed to set %s\n", SPOOL_REG_UNCNAME));
6688 goto done;
6691 done:
6692 talloc_free(tmp_ctx);
6693 return result;
6696 /********************************************************************
6697 * Called by spoolss_api_setprinter
6698 * when updating a printer description.
6699 ********************************************************************/
6701 static WERROR update_printer(struct pipes_struct *p,
6702 struct policy_handle *handle,
6703 struct spoolss_SetPrinterInfoCtr *info_ctr,
6704 struct spoolss_DeviceMode *devmode)
6706 uint32_t printer_mask = SPOOLSS_PRINTER_INFO_ALL;
6707 struct spoolss_SetPrinterInfo2 *printer = info_ctr->info.info2;
6708 struct spoolss_PrinterInfo2 *old_printer;
6709 struct printer_handle *Printer = find_printer_index_by_hnd(p, handle);
6710 int snum;
6711 WERROR result = WERR_OK;
6712 TALLOC_CTX *tmp_ctx;
6713 struct dcerpc_binding_handle *b;
6715 DEBUG(8,("update_printer\n"));
6717 tmp_ctx = talloc_new(p->mem_ctx);
6718 if (tmp_ctx == NULL) {
6719 return WERR_NOMEM;
6722 if (!Printer) {
6723 result = WERR_BADFID;
6724 goto done;
6727 if (!get_printer_snum(p, handle, &snum, NULL)) {
6728 result = WERR_BADFID;
6729 goto done;
6732 result = winreg_printer_binding_handle(tmp_ctx,
6733 get_session_info_system(),
6734 p->msg_ctx,
6735 &b);
6736 if (!W_ERROR_IS_OK(result)) {
6737 goto done;
6740 result = winreg_get_printer(tmp_ctx, b,
6741 lp_const_servicename(snum),
6742 &old_printer);
6743 if (!W_ERROR_IS_OK(result)) {
6744 result = WERR_BADFID;
6745 goto done;
6748 /* Do sanity check on the requested changes for Samba */
6749 if (!check_printer_ok(tmp_ctx, printer, snum)) {
6750 result = WERR_INVALID_PARAM;
6751 goto done;
6754 /* FIXME!!! If the driver has changed we really should verify that
6755 it is installed before doing much else --jerry */
6757 /* Check calling user has permission to update printer description */
6758 if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
6759 DEBUG(3, ("update_printer: printer property change denied by handle\n"));
6760 result = WERR_ACCESS_DENIED;
6761 goto done;
6764 /* Call addprinter hook */
6765 /* Check changes to see if this is really needed */
6767 if (*lp_addprinter_cmd(talloc_tos()) &&
6768 (!strequal(printer->drivername, old_printer->drivername) ||
6769 !strequal(printer->comment, old_printer->comment) ||
6770 !strequal(printer->portname, old_printer->portname) ||
6771 !strequal(printer->location, old_printer->location)) )
6773 char *raddr;
6775 raddr = tsocket_address_inet_addr_string(p->remote_address,
6776 p->mem_ctx);
6777 if (raddr == NULL) {
6778 return WERR_NOMEM;
6781 /* add_printer_hook() will call reload_services() */
6782 if (!add_printer_hook(tmp_ctx, p->session_info->security_token,
6783 printer, raddr,
6784 p->msg_ctx)) {
6785 result = WERR_ACCESS_DENIED;
6786 goto done;
6790 result = update_dsspooler(tmp_ctx,
6791 get_session_info_system(),
6792 p->msg_ctx,
6793 snum,
6794 printer,
6795 old_printer);
6796 if (!W_ERROR_IS_OK(result)) {
6797 goto done;
6800 printer_mask &= ~SPOOLSS_PRINTER_INFO_SECDESC;
6802 if (devmode == NULL) {
6803 printer_mask &= ~SPOOLSS_PRINTER_INFO_DEVMODE;
6805 result = winreg_update_printer(tmp_ctx, b,
6806 printer->sharename,
6807 printer_mask,
6808 printer,
6809 devmode,
6810 NULL);
6812 done:
6813 talloc_free(tmp_ctx);
6815 return result;
6818 /****************************************************************************
6819 ****************************************************************************/
6820 static WERROR publish_or_unpublish_printer(struct pipes_struct *p,
6821 struct policy_handle *handle,
6822 struct spoolss_SetPrinterInfo7 *info7)
6824 #ifdef HAVE_ADS
6825 struct spoolss_PrinterInfo2 *pinfo2 = NULL;
6826 WERROR result;
6827 int snum;
6828 struct printer_handle *Printer;
6830 if ( lp_security() != SEC_ADS ) {
6831 return WERR_UNKNOWN_LEVEL;
6834 Printer = find_printer_index_by_hnd(p, handle);
6836 DEBUG(5,("publish_or_unpublish_printer, action = %d\n",info7->action));
6838 if (!Printer)
6839 return WERR_BADFID;
6841 if (!get_printer_snum(p, handle, &snum, NULL))
6842 return WERR_BADFID;
6844 result = winreg_get_printer_internal(p->mem_ctx,
6845 get_session_info_system(),
6846 p->msg_ctx,
6847 lp_servicename(talloc_tos(), snum),
6848 &pinfo2);
6849 if (!W_ERROR_IS_OK(result)) {
6850 return WERR_BADFID;
6853 nt_printer_publish(pinfo2,
6854 get_session_info_system(),
6855 p->msg_ctx,
6856 pinfo2,
6857 info7->action);
6859 TALLOC_FREE(pinfo2);
6860 return WERR_OK;
6861 #else
6862 return WERR_UNKNOWN_LEVEL;
6863 #endif
6866 /********************************************************************
6867 ********************************************************************/
6869 static WERROR update_printer_devmode(struct pipes_struct *p,
6870 struct policy_handle *handle,
6871 struct spoolss_DeviceMode *devmode)
6873 int snum;
6874 struct printer_handle *Printer = find_printer_index_by_hnd(p, handle);
6875 uint32_t info2_mask = SPOOLSS_PRINTER_INFO_DEVMODE;
6877 DEBUG(8,("update_printer_devmode\n"));
6879 if (!Printer) {
6880 return WERR_BADFID;
6883 if (!get_printer_snum(p, handle, &snum, NULL)) {
6884 return WERR_BADFID;
6887 /* Check calling user has permission to update printer description */
6888 if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
6889 DEBUG(3, ("update_printer: printer property change denied by handle\n"));
6890 return WERR_ACCESS_DENIED;
6893 return winreg_update_printer_internal(p->mem_ctx,
6894 get_session_info_system(),
6895 p->msg_ctx,
6896 lp_const_servicename(snum),
6897 info2_mask,
6898 NULL,
6899 devmode,
6900 NULL);
6904 /****************************************************************
6905 _spoolss_SetPrinter
6906 ****************************************************************/
6908 WERROR _spoolss_SetPrinter(struct pipes_struct *p,
6909 struct spoolss_SetPrinter *r)
6911 WERROR result;
6913 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
6915 if (!Printer) {
6916 DEBUG(2,("_spoolss_SetPrinter: Invalid handle (%s:%u:%u)\n",
6917 OUR_HANDLE(r->in.handle)));
6918 return WERR_BADFID;
6921 /* check the level */
6922 switch (r->in.info_ctr->level) {
6923 case 0:
6924 return control_printer(r->in.handle, r->in.command, p);
6925 case 2:
6926 result = update_printer(p, r->in.handle,
6927 r->in.info_ctr,
6928 r->in.devmode_ctr->devmode);
6929 if (!W_ERROR_IS_OK(result))
6930 return result;
6931 if (r->in.secdesc_ctr->sd)
6932 result = update_printer_sec(r->in.handle, p,
6933 r->in.secdesc_ctr);
6934 return result;
6935 case 3:
6936 return update_printer_sec(r->in.handle, p,
6937 r->in.secdesc_ctr);
6938 case 7:
6939 return publish_or_unpublish_printer(p, r->in.handle,
6940 r->in.info_ctr->info.info7);
6941 case 8:
6942 return update_printer_devmode(p, r->in.handle,
6943 r->in.devmode_ctr->devmode);
6944 default:
6945 return WERR_UNKNOWN_LEVEL;
6949 /****************************************************************
6950 _spoolss_FindClosePrinterNotify
6951 ****************************************************************/
6953 WERROR _spoolss_FindClosePrinterNotify(struct pipes_struct *p,
6954 struct spoolss_FindClosePrinterNotify *r)
6956 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
6958 if (!Printer) {
6959 DEBUG(2,("_spoolss_FindClosePrinterNotify: "
6960 "Invalid handle (%s:%u:%u)\n", OUR_HANDLE(r->in.handle)));
6961 return WERR_BADFID;
6964 if (Printer->notify.cli_chan != NULL &&
6965 Printer->notify.cli_chan->active_connections > 0) {
6966 int snum = -1;
6968 if (Printer->printer_type == SPLHND_PRINTER) {
6969 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
6970 return WERR_BADFID;
6974 srv_spoolss_replycloseprinter(snum, Printer);
6977 Printer->notify.flags=0;
6978 Printer->notify.options=0;
6979 Printer->notify.localmachine[0]='\0';
6980 Printer->notify.printerlocal=0;
6981 TALLOC_FREE(Printer->notify.option);
6983 return WERR_OK;
6986 /****************************************************************
6987 _spoolss_AddJob
6988 ****************************************************************/
6990 WERROR _spoolss_AddJob(struct pipes_struct *p,
6991 struct spoolss_AddJob *r)
6993 if (!r->in.buffer && (r->in.offered != 0)) {
6994 return WERR_INVALID_PARAM;
6997 /* this is what a NT server returns for AddJob. AddJob must fail on
6998 * non-local printers */
7000 if (r->in.level != 1) {
7001 return WERR_UNKNOWN_LEVEL;
7004 return WERR_INVALID_PARAM;
7007 /****************************************************************************
7008 fill_job_info1
7009 ****************************************************************************/
7011 static WERROR fill_job_info1(TALLOC_CTX *mem_ctx,
7012 struct spoolss_JobInfo1 *r,
7013 const print_queue_struct *queue,
7014 int position, int snum,
7015 struct spoolss_PrinterInfo2 *pinfo2)
7017 struct tm *t;
7019 t = gmtime(&queue->time);
7021 r->job_id = queue->sysjob;
7023 r->printer_name = lp_servicename(mem_ctx, snum);
7024 W_ERROR_HAVE_NO_MEMORY(r->printer_name);
7025 r->server_name = talloc_strdup(mem_ctx, pinfo2->servername);
7026 W_ERROR_HAVE_NO_MEMORY(r->server_name);
7027 r->user_name = talloc_strdup(mem_ctx, queue->fs_user);
7028 W_ERROR_HAVE_NO_MEMORY(r->user_name);
7029 r->document_name = talloc_strdup(mem_ctx, queue->fs_file);
7030 W_ERROR_HAVE_NO_MEMORY(r->document_name);
7031 r->data_type = talloc_strdup(mem_ctx, "RAW");
7032 W_ERROR_HAVE_NO_MEMORY(r->data_type);
7033 r->text_status = talloc_strdup(mem_ctx, "");
7034 W_ERROR_HAVE_NO_MEMORY(r->text_status);
7036 r->status = nt_printj_status(queue->status);
7037 r->priority = queue->priority;
7038 r->position = position;
7039 r->total_pages = queue->page_count;
7040 r->pages_printed = 0; /* ??? */
7042 init_systemtime(&r->submitted, t);
7044 return WERR_OK;
7047 /****************************************************************************
7048 fill_job_info2
7049 ****************************************************************************/
7051 static WERROR fill_job_info2(TALLOC_CTX *mem_ctx,
7052 struct spoolss_JobInfo2 *r,
7053 const print_queue_struct *queue,
7054 int position, int snum,
7055 struct spoolss_PrinterInfo2 *pinfo2,
7056 struct spoolss_DeviceMode *devmode)
7058 struct tm *t;
7060 t = gmtime(&queue->time);
7062 r->job_id = queue->sysjob;
7064 r->printer_name = lp_servicename(mem_ctx, snum);
7065 W_ERROR_HAVE_NO_MEMORY(r->printer_name);
7066 r->server_name = talloc_strdup(mem_ctx, pinfo2->servername);
7067 W_ERROR_HAVE_NO_MEMORY(r->server_name);
7068 r->user_name = talloc_strdup(mem_ctx, queue->fs_user);
7069 W_ERROR_HAVE_NO_MEMORY(r->user_name);
7070 r->document_name = talloc_strdup(mem_ctx, queue->fs_file);
7071 W_ERROR_HAVE_NO_MEMORY(r->document_name);
7072 r->notify_name = talloc_strdup(mem_ctx, queue->fs_user);
7073 W_ERROR_HAVE_NO_MEMORY(r->notify_name);
7074 r->data_type = talloc_strdup(mem_ctx, "RAW");
7075 W_ERROR_HAVE_NO_MEMORY(r->data_type);
7076 r->print_processor = talloc_strdup(mem_ctx, "winprint");
7077 W_ERROR_HAVE_NO_MEMORY(r->print_processor);
7078 r->parameters = talloc_strdup(mem_ctx, "");
7079 W_ERROR_HAVE_NO_MEMORY(r->parameters);
7080 r->driver_name = talloc_strdup(mem_ctx, pinfo2->drivername);
7081 W_ERROR_HAVE_NO_MEMORY(r->driver_name);
7083 r->devmode = devmode;
7085 r->text_status = talloc_strdup(mem_ctx, "");
7086 W_ERROR_HAVE_NO_MEMORY(r->text_status);
7088 r->secdesc = NULL;
7090 r->status = nt_printj_status(queue->status);
7091 r->priority = queue->priority;
7092 r->position = position;
7093 r->start_time = 0;
7094 r->until_time = 0;
7095 r->total_pages = queue->page_count;
7096 r->size = queue->size;
7097 init_systemtime(&r->submitted, t);
7098 r->time = 0;
7099 r->pages_printed = 0; /* ??? */
7101 return WERR_OK;
7104 /****************************************************************************
7105 fill_job_info3
7106 ****************************************************************************/
7108 static WERROR fill_job_info3(TALLOC_CTX *mem_ctx,
7109 struct spoolss_JobInfo3 *r,
7110 const print_queue_struct *queue,
7111 const print_queue_struct *next_queue,
7112 int position, int snum,
7113 struct spoolss_PrinterInfo2 *pinfo2)
7115 r->job_id = queue->sysjob;
7116 r->next_job_id = 0;
7117 if (next_queue) {
7118 r->next_job_id = next_queue->sysjob;
7120 r->reserved = 0;
7122 return WERR_OK;
7125 /****************************************************************************
7126 Enumjobs at level 1.
7127 ****************************************************************************/
7129 static WERROR enumjobs_level1(TALLOC_CTX *mem_ctx,
7130 const print_queue_struct *queue,
7131 uint32_t num_queues, int snum,
7132 struct spoolss_PrinterInfo2 *pinfo2,
7133 union spoolss_JobInfo **info_p,
7134 uint32_t *count)
7136 union spoolss_JobInfo *info;
7137 int i;
7138 WERROR result = WERR_OK;
7140 info = talloc_array(mem_ctx, union spoolss_JobInfo, num_queues);
7141 W_ERROR_HAVE_NO_MEMORY(info);
7143 *count = num_queues;
7145 for (i=0; i<*count; i++) {
7146 result = fill_job_info1(info,
7147 &info[i].info1,
7148 &queue[i],
7150 snum,
7151 pinfo2);
7152 if (!W_ERROR_IS_OK(result)) {
7153 goto out;
7157 out:
7158 if (!W_ERROR_IS_OK(result)) {
7159 TALLOC_FREE(info);
7160 *count = 0;
7161 return result;
7164 *info_p = info;
7166 return WERR_OK;
7169 /****************************************************************************
7170 Enumjobs at level 2.
7171 ****************************************************************************/
7173 static WERROR enumjobs_level2(TALLOC_CTX *mem_ctx,
7174 const print_queue_struct *queue,
7175 uint32_t num_queues, int snum,
7176 struct spoolss_PrinterInfo2 *pinfo2,
7177 union spoolss_JobInfo **info_p,
7178 uint32_t *count)
7180 union spoolss_JobInfo *info;
7181 int i;
7182 WERROR result = WERR_OK;
7184 info = talloc_array(mem_ctx, union spoolss_JobInfo, num_queues);
7185 W_ERROR_HAVE_NO_MEMORY(info);
7187 *count = num_queues;
7189 for (i=0; i<*count; i++) {
7190 struct spoolss_DeviceMode *devmode;
7192 result = spoolss_create_default_devmode(info,
7193 pinfo2->printername,
7194 &devmode);
7195 if (!W_ERROR_IS_OK(result)) {
7196 DEBUG(3, ("Can't proceed w/o a devmode!"));
7197 goto out;
7200 result = fill_job_info2(info,
7201 &info[i].info2,
7202 &queue[i],
7204 snum,
7205 pinfo2,
7206 devmode);
7207 if (!W_ERROR_IS_OK(result)) {
7208 goto out;
7212 out:
7213 if (!W_ERROR_IS_OK(result)) {
7214 TALLOC_FREE(info);
7215 *count = 0;
7216 return result;
7219 *info_p = info;
7221 return WERR_OK;
7224 /****************************************************************************
7225 Enumjobs at level 3.
7226 ****************************************************************************/
7228 static WERROR enumjobs_level3(TALLOC_CTX *mem_ctx,
7229 const print_queue_struct *queue,
7230 uint32_t num_queues, int snum,
7231 struct spoolss_PrinterInfo2 *pinfo2,
7232 union spoolss_JobInfo **info_p,
7233 uint32_t *count)
7235 union spoolss_JobInfo *info;
7236 int i;
7237 WERROR result = WERR_OK;
7239 info = talloc_array(mem_ctx, union spoolss_JobInfo, num_queues);
7240 W_ERROR_HAVE_NO_MEMORY(info);
7242 *count = num_queues;
7244 for (i=0; i<*count; i++) {
7245 const print_queue_struct *next_queue = NULL;
7247 if (i+1 < *count) {
7248 next_queue = &queue[i+1];
7251 result = fill_job_info3(info,
7252 &info[i].info3,
7253 &queue[i],
7254 next_queue,
7256 snum,
7257 pinfo2);
7258 if (!W_ERROR_IS_OK(result)) {
7259 goto out;
7263 out:
7264 if (!W_ERROR_IS_OK(result)) {
7265 TALLOC_FREE(info);
7266 *count = 0;
7267 return result;
7270 *info_p = info;
7272 return WERR_OK;
7275 /****************************************************************
7276 _spoolss_EnumJobs
7277 ****************************************************************/
7279 WERROR _spoolss_EnumJobs(struct pipes_struct *p,
7280 struct spoolss_EnumJobs *r)
7282 WERROR result;
7283 struct spoolss_PrinterInfo2 *pinfo2 = NULL;
7284 int snum;
7285 print_status_struct prt_status;
7286 print_queue_struct *queue = NULL;
7287 uint32_t count;
7289 /* that's an [in out] buffer */
7291 if (!r->in.buffer && (r->in.offered != 0)) {
7292 return WERR_INVALID_PARAM;
7295 DEBUG(4,("_spoolss_EnumJobs\n"));
7297 *r->out.needed = 0;
7298 *r->out.count = 0;
7299 *r->out.info = NULL;
7301 /* lookup the printer snum and tdb entry */
7303 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
7304 return WERR_BADFID;
7307 result = winreg_get_printer_internal(p->mem_ctx,
7308 get_session_info_system(),
7309 p->msg_ctx,
7310 lp_const_servicename(snum),
7311 &pinfo2);
7312 if (!W_ERROR_IS_OK(result)) {
7313 return result;
7316 count = print_queue_status(p->msg_ctx, snum, &queue, &prt_status);
7317 DEBUGADD(4,("count:[%d], status:[%d], [%s]\n",
7318 count, prt_status.status, prt_status.message));
7320 if (count == 0) {
7321 SAFE_FREE(queue);
7322 TALLOC_FREE(pinfo2);
7323 return WERR_OK;
7326 switch (r->in.level) {
7327 case 1:
7328 result = enumjobs_level1(p->mem_ctx, queue, count, snum,
7329 pinfo2, r->out.info, r->out.count);
7330 break;
7331 case 2:
7332 result = enumjobs_level2(p->mem_ctx, queue, count, snum,
7333 pinfo2, r->out.info, r->out.count);
7334 break;
7335 case 3:
7336 result = enumjobs_level3(p->mem_ctx, queue, count, snum,
7337 pinfo2, r->out.info, r->out.count);
7338 break;
7339 default:
7340 result = WERR_UNKNOWN_LEVEL;
7341 break;
7344 SAFE_FREE(queue);
7345 TALLOC_FREE(pinfo2);
7347 if (!W_ERROR_IS_OK(result)) {
7348 return result;
7351 *r->out.needed = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
7352 spoolss_EnumJobs,
7353 *r->out.info, r->in.level,
7354 *r->out.count);
7355 *r->out.info = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
7356 *r->out.count = SPOOLSS_BUFFER_OK(*r->out.count, 0);
7358 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
7361 /****************************************************************
7362 _spoolss_ScheduleJob
7363 ****************************************************************/
7365 WERROR _spoolss_ScheduleJob(struct pipes_struct *p,
7366 struct spoolss_ScheduleJob *r)
7368 return WERR_OK;
7371 /****************************************************************
7372 ****************************************************************/
7374 static WERROR spoolss_setjob_1(TALLOC_CTX *mem_ctx,
7375 struct messaging_context *msg_ctx,
7376 const char *printer_name,
7377 uint32_t job_id,
7378 struct spoolss_SetJobInfo1 *r)
7380 char *old_doc_name;
7382 if (!print_job_get_name(mem_ctx, printer_name, job_id, &old_doc_name)) {
7383 return WERR_BADFID;
7386 if (strequal(old_doc_name, r->document_name)) {
7387 return WERR_OK;
7390 if (!print_job_set_name(server_event_context(), msg_ctx,
7391 printer_name, job_id, r->document_name)) {
7392 return WERR_BADFID;
7395 return WERR_OK;
7398 /****************************************************************
7399 _spoolss_SetJob
7400 ****************************************************************/
7402 WERROR _spoolss_SetJob(struct pipes_struct *p,
7403 struct spoolss_SetJob *r)
7405 const struct auth_session_info *session_info = p->session_info;
7406 int snum;
7407 WERROR errcode = WERR_BADFUNC;
7409 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
7410 return WERR_BADFID;
7413 if (!print_job_exists(lp_const_servicename(snum), r->in.job_id)) {
7414 return WERR_INVALID_PRINTER_NAME;
7417 switch (r->in.command) {
7418 case SPOOLSS_JOB_CONTROL_CANCEL:
7419 case SPOOLSS_JOB_CONTROL_DELETE:
7420 errcode = print_job_delete(session_info, p->msg_ctx,
7421 snum, r->in.job_id);
7422 if (W_ERROR_EQUAL(errcode, WERR_PRINTER_HAS_JOBS_QUEUED)) {
7423 errcode = WERR_OK;
7425 break;
7426 case SPOOLSS_JOB_CONTROL_PAUSE:
7427 errcode = print_job_pause(session_info, p->msg_ctx,
7428 snum, r->in.job_id);
7429 break;
7430 case SPOOLSS_JOB_CONTROL_RESTART:
7431 case SPOOLSS_JOB_CONTROL_RESUME:
7432 errcode = print_job_resume(session_info, p->msg_ctx,
7433 snum, r->in.job_id);
7434 break;
7435 case 0:
7436 errcode = WERR_OK;
7437 break;
7438 default:
7439 return WERR_UNKNOWN_LEVEL;
7442 if (!W_ERROR_IS_OK(errcode)) {
7443 return errcode;
7446 if (r->in.ctr == NULL) {
7447 return errcode;
7450 switch (r->in.ctr->level) {
7451 case 1:
7452 errcode = spoolss_setjob_1(p->mem_ctx, p->msg_ctx,
7453 lp_const_servicename(snum),
7454 r->in.job_id,
7455 r->in.ctr->info.info1);
7456 break;
7457 case 2:
7458 case 3:
7459 case 4:
7460 default:
7461 return WERR_UNKNOWN_LEVEL;
7464 return errcode;
7467 /****************************************************************************
7468 Enumerates all printer drivers by level and architecture.
7469 ****************************************************************************/
7471 static WERROR enumprinterdrivers_level_by_architecture(TALLOC_CTX *mem_ctx,
7472 const struct auth_session_info *session_info,
7473 struct messaging_context *msg_ctx,
7474 const char *servername,
7475 const char *architecture,
7476 uint32_t level,
7477 union spoolss_DriverInfo **info_p,
7478 uint32_t *count_p)
7480 int i;
7481 uint32_t version;
7482 struct spoolss_DriverInfo8 *driver;
7483 union spoolss_DriverInfo *info = NULL;
7484 uint32_t count = 0;
7485 WERROR result = WERR_OK;
7486 uint32_t num_drivers;
7487 const char **drivers;
7488 struct dcerpc_binding_handle *b;
7489 TALLOC_CTX *tmp_ctx = NULL;
7491 *count_p = 0;
7492 *info_p = NULL;
7494 tmp_ctx = talloc_new(mem_ctx);
7495 if (!tmp_ctx) {
7496 return WERR_NOMEM;
7499 result = winreg_printer_binding_handle(tmp_ctx,
7500 session_info,
7501 msg_ctx,
7502 &b);
7503 if (!W_ERROR_IS_OK(result)) {
7504 goto out;
7507 for (version=0; version<DRIVER_MAX_VERSION; version++) {
7508 result = winreg_get_driver_list(tmp_ctx, b,
7509 architecture, version,
7510 &num_drivers, &drivers);
7511 if (!W_ERROR_IS_OK(result)) {
7512 goto out;
7514 DEBUG(4, ("we have:[%d] drivers in environment"
7515 " [%s] and version [%d]\n",
7516 num_drivers, architecture, version));
7518 if (num_drivers != 0) {
7519 info = talloc_realloc(tmp_ctx, info,
7520 union spoolss_DriverInfo,
7521 count + num_drivers);
7522 if (!info) {
7523 DEBUG(0,("enumprinterdrivers_level_by_architecture: "
7524 "failed to enlarge driver info buffer!\n"));
7525 result = WERR_NOMEM;
7526 goto out;
7530 for (i = 0; i < num_drivers; i++) {
7531 DEBUG(5, ("\tdriver: [%s]\n", drivers[i]));
7533 result = winreg_get_driver(tmp_ctx, b,
7534 architecture, drivers[i],
7535 version, &driver);
7536 if (!W_ERROR_IS_OK(result)) {
7537 goto out;
7540 switch (level) {
7541 case 1:
7542 result = fill_printer_driver_info1(info, &info[count+i].info1,
7543 driver, servername);
7544 break;
7545 case 2:
7546 result = fill_printer_driver_info2(info, &info[count+i].info2,
7547 driver, servername);
7548 break;
7549 case 3:
7550 result = fill_printer_driver_info3(info, &info[count+i].info3,
7551 driver, servername);
7552 break;
7553 case 4:
7554 result = fill_printer_driver_info4(info, &info[count+i].info4,
7555 driver, servername);
7556 break;
7557 case 5:
7558 result = fill_printer_driver_info5(info, &info[count+i].info5,
7559 driver, servername);
7560 break;
7561 case 6:
7562 result = fill_printer_driver_info6(info, &info[count+i].info6,
7563 driver, servername);
7564 break;
7565 case 8:
7566 result = fill_printer_driver_info8(info, &info[count+i].info8,
7567 driver, servername);
7568 break;
7569 default:
7570 result = WERR_UNKNOWN_LEVEL;
7571 break;
7574 TALLOC_FREE(driver);
7576 if (!W_ERROR_IS_OK(result)) {
7577 goto out;
7581 count += num_drivers;
7582 TALLOC_FREE(drivers);
7585 out:
7586 if (W_ERROR_IS_OK(result)) {
7587 *info_p = talloc_move(mem_ctx, &info);
7588 *count_p = count;
7591 talloc_free(tmp_ctx);
7592 return result;
7595 /****************************************************************************
7596 Enumerates all printer drivers by level.
7597 ****************************************************************************/
7599 static WERROR enumprinterdrivers_level(TALLOC_CTX *mem_ctx,
7600 const struct auth_session_info *session_info,
7601 struct messaging_context *msg_ctx,
7602 const char *servername,
7603 const char *architecture,
7604 uint32_t level,
7605 union spoolss_DriverInfo **info_p,
7606 uint32_t *count_p)
7608 uint32_t a,i;
7609 WERROR result = WERR_OK;
7611 if (strequal(architecture, SPOOLSS_ARCHITECTURE_ALL)) {
7613 for (a=0; archi_table[a].long_archi != NULL; a++) {
7615 union spoolss_DriverInfo *info = NULL;
7616 uint32_t count = 0;
7618 result = enumprinterdrivers_level_by_architecture(mem_ctx,
7619 session_info,
7620 msg_ctx,
7621 servername,
7622 archi_table[a].long_archi,
7623 level,
7624 &info,
7625 &count);
7626 if (!W_ERROR_IS_OK(result)) {
7627 continue;
7630 for (i=0; i < count; i++) {
7631 ADD_TO_ARRAY(mem_ctx, union spoolss_DriverInfo,
7632 info[i], info_p, count_p);
7636 return result;
7639 return enumprinterdrivers_level_by_architecture(mem_ctx,
7640 session_info,
7641 msg_ctx,
7642 servername,
7643 architecture,
7644 level,
7645 info_p,
7646 count_p);
7649 /****************************************************************
7650 _spoolss_EnumPrinterDrivers
7651 ****************************************************************/
7653 WERROR _spoolss_EnumPrinterDrivers(struct pipes_struct *p,
7654 struct spoolss_EnumPrinterDrivers *r)
7656 const char *cservername;
7657 WERROR result;
7659 /* that's an [in out] buffer */
7661 if (!r->in.buffer && (r->in.offered != 0)) {
7662 return WERR_INVALID_PARAM;
7665 DEBUG(4,("_spoolss_EnumPrinterDrivers\n"));
7667 *r->out.needed = 0;
7668 *r->out.count = 0;
7669 *r->out.info = NULL;
7671 cservername = canon_servername(r->in.server);
7673 if (!is_myname_or_ipaddr(cservername)) {
7674 return WERR_UNKNOWN_PRINTER_DRIVER;
7677 result = enumprinterdrivers_level(p->mem_ctx,
7678 get_session_info_system(),
7679 p->msg_ctx,
7680 cservername,
7681 r->in.environment,
7682 r->in.level,
7683 r->out.info,
7684 r->out.count);
7685 if (!W_ERROR_IS_OK(result)) {
7686 return result;
7689 *r->out.needed = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
7690 spoolss_EnumPrinterDrivers,
7691 *r->out.info, r->in.level,
7692 *r->out.count);
7693 *r->out.info = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
7694 *r->out.count = SPOOLSS_BUFFER_OK(*r->out.count, 0);
7696 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
7699 /****************************************************************
7700 _spoolss_EnumForms
7701 ****************************************************************/
7703 WERROR _spoolss_EnumForms(struct pipes_struct *p,
7704 struct spoolss_EnumForms *r)
7706 WERROR result;
7708 *r->out.count = 0;
7709 *r->out.needed = 0;
7710 *r->out.info = NULL;
7712 /* that's an [in out] buffer */
7714 if (!r->in.buffer && (r->in.offered != 0) ) {
7715 return WERR_INVALID_PARAM;
7718 DEBUG(4,("_spoolss_EnumForms\n"));
7719 DEBUGADD(5,("Offered buffer size [%d]\n", r->in.offered));
7720 DEBUGADD(5,("Info level [%d]\n", r->in.level));
7722 switch (r->in.level) {
7723 case 1:
7724 result = winreg_printer_enumforms1_internal(p->mem_ctx,
7725 get_session_info_system(),
7726 p->msg_ctx,
7727 r->out.count,
7728 r->out.info);
7729 break;
7730 default:
7731 result = WERR_UNKNOWN_LEVEL;
7732 break;
7735 if (!W_ERROR_IS_OK(result)) {
7736 return result;
7739 if (*r->out.count == 0) {
7740 return WERR_NO_MORE_ITEMS;
7743 *r->out.needed = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
7744 spoolss_EnumForms,
7745 *r->out.info, r->in.level,
7746 *r->out.count);
7747 *r->out.info = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
7748 *r->out.count = SPOOLSS_BUFFER_OK(*r->out.count, 0);
7750 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
7753 /****************************************************************
7754 _spoolss_GetForm
7755 ****************************************************************/
7757 WERROR _spoolss_GetForm(struct pipes_struct *p,
7758 struct spoolss_GetForm *r)
7760 WERROR result;
7762 /* that's an [in out] buffer */
7764 if (!r->in.buffer && (r->in.offered != 0)) {
7765 return WERR_INVALID_PARAM;
7768 DEBUG(4,("_spoolss_GetForm\n"));
7769 DEBUGADD(5,("Offered buffer size [%d]\n", r->in.offered));
7770 DEBUGADD(5,("Info level [%d]\n", r->in.level));
7772 switch (r->in.level) {
7773 case 1:
7774 result = winreg_printer_getform1_internal(p->mem_ctx,
7775 get_session_info_system(),
7776 p->msg_ctx,
7777 r->in.form_name,
7778 &r->out.info->info1);
7779 break;
7780 default:
7781 result = WERR_UNKNOWN_LEVEL;
7782 break;
7785 if (!W_ERROR_IS_OK(result)) {
7786 TALLOC_FREE(r->out.info);
7787 return result;
7790 *r->out.needed = SPOOLSS_BUFFER_UNION(spoolss_FormInfo,
7791 r->out.info, r->in.level);
7792 r->out.info = SPOOLSS_BUFFER_OK(r->out.info, NULL);
7794 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
7797 /****************************************************************************
7798 ****************************************************************************/
7800 static WERROR fill_port_1(TALLOC_CTX *mem_ctx,
7801 struct spoolss_PortInfo1 *r,
7802 const char *name)
7804 r->port_name = talloc_strdup(mem_ctx, name);
7805 W_ERROR_HAVE_NO_MEMORY(r->port_name);
7807 return WERR_OK;
7810 /****************************************************************************
7811 TODO: This probably needs distinguish between TCP/IP and Local ports
7812 somehow.
7813 ****************************************************************************/
7815 static WERROR fill_port_2(TALLOC_CTX *mem_ctx,
7816 struct spoolss_PortInfo2 *r,
7817 const char *name)
7819 r->port_name = talloc_strdup(mem_ctx, name);
7820 W_ERROR_HAVE_NO_MEMORY(r->port_name);
7822 r->monitor_name = talloc_strdup(mem_ctx, "Local Monitor");
7823 W_ERROR_HAVE_NO_MEMORY(r->monitor_name);
7825 r->description = talloc_strdup(mem_ctx, SPL_LOCAL_PORT);
7826 W_ERROR_HAVE_NO_MEMORY(r->description);
7828 r->port_type = SPOOLSS_PORT_TYPE_WRITE;
7829 r->reserved = 0;
7831 return WERR_OK;
7835 /****************************************************************************
7836 wrapper around the enumer ports command
7837 ****************************************************************************/
7839 static WERROR enumports_hook(TALLOC_CTX *ctx, int *count, char ***lines)
7841 char *cmd = lp_enumports_cmd(talloc_tos());
7842 char **qlines = NULL;
7843 char *command = NULL;
7844 int numlines;
7845 int ret;
7846 int fd;
7848 *count = 0;
7849 *lines = NULL;
7851 /* if no hook then just fill in the default port */
7853 if ( !*cmd ) {
7854 if (!(qlines = talloc_array( NULL, char*, 2 ))) {
7855 return WERR_NOMEM;
7857 if (!(qlines[0] = talloc_strdup(qlines, SAMBA_PRINTER_PORT_NAME ))) {
7858 TALLOC_FREE(qlines);
7859 return WERR_NOMEM;
7861 qlines[1] = NULL;
7862 numlines = 1;
7864 else {
7865 /* we have a valid enumport command */
7867 command = talloc_asprintf(ctx, "%s \"%d\"", cmd, 1);
7868 if (!command) {
7869 return WERR_NOMEM;
7872 DEBUG(10,("Running [%s]\n", command));
7873 ret = smbrun(command, &fd);
7874 DEBUG(10,("Returned [%d]\n", ret));
7875 TALLOC_FREE(command);
7876 if (ret != 0) {
7877 if (fd != -1) {
7878 close(fd);
7880 return WERR_ACCESS_DENIED;
7883 numlines = 0;
7884 qlines = fd_lines_load(fd, &numlines, 0, NULL);
7885 DEBUGADD(10,("Lines returned = [%d]\n", numlines));
7886 close(fd);
7889 *count = numlines;
7890 *lines = qlines;
7892 return WERR_OK;
7895 /****************************************************************************
7896 enumports level 1.
7897 ****************************************************************************/
7899 static WERROR enumports_level_1(TALLOC_CTX *mem_ctx,
7900 union spoolss_PortInfo **info_p,
7901 uint32_t *count)
7903 union spoolss_PortInfo *info = NULL;
7904 int i=0;
7905 WERROR result = WERR_OK;
7906 char **qlines = NULL;
7907 int numlines = 0;
7909 result = enumports_hook(talloc_tos(), &numlines, &qlines );
7910 if (!W_ERROR_IS_OK(result)) {
7911 goto out;
7914 if (numlines) {
7915 info = talloc_array(mem_ctx, union spoolss_PortInfo, numlines);
7916 if (!info) {
7917 DEBUG(10,("Returning WERR_NOMEM\n"));
7918 result = WERR_NOMEM;
7919 goto out;
7922 for (i=0; i<numlines; i++) {
7923 DEBUG(6,("Filling port number [%d] with port [%s]\n", i, qlines[i]));
7924 result = fill_port_1(info, &info[i].info1, qlines[i]);
7925 if (!W_ERROR_IS_OK(result)) {
7926 goto out;
7930 TALLOC_FREE(qlines);
7932 out:
7933 if (!W_ERROR_IS_OK(result)) {
7934 TALLOC_FREE(info);
7935 TALLOC_FREE(qlines);
7936 *count = 0;
7937 *info_p = NULL;
7938 return result;
7941 *info_p = info;
7942 *count = numlines;
7944 return WERR_OK;
7947 /****************************************************************************
7948 enumports level 2.
7949 ****************************************************************************/
7951 static WERROR enumports_level_2(TALLOC_CTX *mem_ctx,
7952 union spoolss_PortInfo **info_p,
7953 uint32_t *count)
7955 union spoolss_PortInfo *info = NULL;
7956 int i=0;
7957 WERROR result = WERR_OK;
7958 char **qlines = NULL;
7959 int numlines = 0;
7961 result = enumports_hook(talloc_tos(), &numlines, &qlines );
7962 if (!W_ERROR_IS_OK(result)) {
7963 goto out;
7966 if (numlines) {
7967 info = talloc_array(mem_ctx, union spoolss_PortInfo, numlines);
7968 if (!info) {
7969 DEBUG(10,("Returning WERR_NOMEM\n"));
7970 result = WERR_NOMEM;
7971 goto out;
7974 for (i=0; i<numlines; i++) {
7975 DEBUG(6,("Filling port number [%d] with port [%s]\n", i, qlines[i]));
7976 result = fill_port_2(info, &info[i].info2, qlines[i]);
7977 if (!W_ERROR_IS_OK(result)) {
7978 goto out;
7982 TALLOC_FREE(qlines);
7984 out:
7985 if (!W_ERROR_IS_OK(result)) {
7986 TALLOC_FREE(info);
7987 TALLOC_FREE(qlines);
7988 *count = 0;
7989 *info_p = NULL;
7990 return result;
7993 *info_p = info;
7994 *count = numlines;
7996 return WERR_OK;
7999 /****************************************************************
8000 _spoolss_EnumPorts
8001 ****************************************************************/
8003 WERROR _spoolss_EnumPorts(struct pipes_struct *p,
8004 struct spoolss_EnumPorts *r)
8006 WERROR result;
8008 /* that's an [in out] buffer */
8010 if (!r->in.buffer && (r->in.offered != 0)) {
8011 return WERR_INVALID_PARAM;
8014 DEBUG(4,("_spoolss_EnumPorts\n"));
8016 *r->out.count = 0;
8017 *r->out.needed = 0;
8018 *r->out.info = NULL;
8020 switch (r->in.level) {
8021 case 1:
8022 result = enumports_level_1(p->mem_ctx, r->out.info,
8023 r->out.count);
8024 break;
8025 case 2:
8026 result = enumports_level_2(p->mem_ctx, r->out.info,
8027 r->out.count);
8028 break;
8029 default:
8030 return WERR_UNKNOWN_LEVEL;
8033 if (!W_ERROR_IS_OK(result)) {
8034 return result;
8037 *r->out.needed = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
8038 spoolss_EnumPorts,
8039 *r->out.info, r->in.level,
8040 *r->out.count);
8041 *r->out.info = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
8042 *r->out.count = SPOOLSS_BUFFER_OK(*r->out.count, 0);
8044 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
8047 /****************************************************************************
8048 ****************************************************************************/
8050 static WERROR spoolss_addprinterex_level_2(struct pipes_struct *p,
8051 const char *server,
8052 struct spoolss_SetPrinterInfoCtr *info_ctr,
8053 struct spoolss_DeviceMode *devmode,
8054 struct security_descriptor *secdesc,
8055 struct spoolss_UserLevelCtr *user_ctr,
8056 struct policy_handle *handle)
8058 struct spoolss_SetPrinterInfo2 *info2 = info_ctr->info.info2;
8059 uint32_t info2_mask = SPOOLSS_PRINTER_INFO_ALL;
8060 int snum;
8061 WERROR err = WERR_OK;
8063 /* samba does not have a concept of local, non-shared printers yet, so
8064 * make sure we always setup sharename - gd */
8065 if ((info2->sharename == NULL || info2->sharename[0] == '\0') &&
8066 (info2->printername != NULL && info2->printername[0] != '\0')) {
8067 DEBUG(5, ("spoolss_addprinterex_level_2: "
8068 "no sharename has been set, setting printername %s as sharename\n",
8069 info2->printername));
8070 info2->sharename = info2->printername;
8073 /* check to see if the printer already exists */
8074 if ((snum = print_queue_snum(info2->sharename)) != -1) {
8075 DEBUG(5, ("spoolss_addprinterex_level_2: Attempted to add a printer named [%s] when one already existed!\n",
8076 info2->sharename));
8077 return WERR_PRINTER_ALREADY_EXISTS;
8080 if (!lp_force_printername(GLOBAL_SECTION_SNUM)) {
8081 if ((snum = print_queue_snum(info2->printername)) != -1) {
8082 DEBUG(5, ("spoolss_addprinterex_level_2: Attempted to add a printer named [%s] when one already existed!\n",
8083 info2->printername));
8084 return WERR_PRINTER_ALREADY_EXISTS;
8088 /* validate printer info struct */
8089 if (!info2->printername || strlen(info2->printername) == 0) {
8090 return WERR_INVALID_PRINTER_NAME;
8092 if (!info2->portname || strlen(info2->portname) == 0) {
8093 return WERR_UNKNOWN_PORT;
8095 if (!info2->drivername || strlen(info2->drivername) == 0) {
8096 return WERR_UNKNOWN_PRINTER_DRIVER;
8098 if (!info2->printprocessor || strlen(info2->printprocessor) == 0) {
8099 return WERR_UNKNOWN_PRINTPROCESSOR;
8102 /* FIXME!!! smbd should check to see if the driver is installed before
8103 trying to add a printer like this --jerry */
8105 if (*lp_addprinter_cmd(talloc_tos()) ) {
8106 char *raddr;
8108 raddr = tsocket_address_inet_addr_string(p->remote_address,
8109 p->mem_ctx);
8110 if (raddr == NULL) {
8111 return WERR_NOMEM;
8114 if ( !add_printer_hook(p->mem_ctx, p->session_info->security_token,
8115 info2, raddr,
8116 p->msg_ctx) ) {
8117 return WERR_ACCESS_DENIED;
8119 } else {
8120 DEBUG(0,("spoolss_addprinterex_level_2: add printer for printer %s called and no "
8121 "smb.conf parameter \"addprinter command\" is defined. This "
8122 "parameter must exist for this call to succeed\n",
8123 info2->sharename ));
8126 if ((snum = print_queue_snum(info2->sharename)) == -1) {
8127 return WERR_ACCESS_DENIED;
8130 /* you must be a printer admin to add a new printer */
8131 if (!print_access_check(p->session_info,
8132 p->msg_ctx,
8133 snum,
8134 PRINTER_ACCESS_ADMINISTER)) {
8135 return WERR_ACCESS_DENIED;
8139 * Do sanity check on the requested changes for Samba.
8142 if (!check_printer_ok(p->mem_ctx, info2, snum)) {
8143 return WERR_INVALID_PARAM;
8146 if (devmode == NULL) {
8147 info2_mask = ~SPOOLSS_PRINTER_INFO_DEVMODE;
8150 err = update_dsspooler(p->mem_ctx,
8151 get_session_info_system(),
8152 p->msg_ctx,
8154 info2,
8155 NULL);
8156 if (!W_ERROR_IS_OK(err)) {
8157 return err;
8160 err = winreg_update_printer_internal(p->mem_ctx,
8161 get_session_info_system(),
8162 p->msg_ctx,
8163 info2->sharename,
8164 info2_mask,
8165 info2,
8166 devmode,
8167 secdesc);
8168 if (!W_ERROR_IS_OK(err)) {
8169 return err;
8172 err = open_printer_hnd(p, handle, info2->printername, PRINTER_ACCESS_ADMINISTER);
8173 if (!W_ERROR_IS_OK(err)) {
8174 /* Handle open failed - remove addition. */
8175 ZERO_STRUCTP(handle);
8176 return err;
8179 return WERR_OK;
8182 /****************************************************************
8183 _spoolss_AddPrinterEx
8184 ****************************************************************/
8186 WERROR _spoolss_AddPrinterEx(struct pipes_struct *p,
8187 struct spoolss_AddPrinterEx *r)
8189 switch (r->in.info_ctr->level) {
8190 case 1:
8191 /* we don't handle yet */
8192 /* but I know what to do ... */
8193 return WERR_UNKNOWN_LEVEL;
8194 case 2:
8195 return spoolss_addprinterex_level_2(p, r->in.server,
8196 r->in.info_ctr,
8197 r->in.devmode_ctr->devmode,
8198 r->in.secdesc_ctr->sd,
8199 r->in.userlevel_ctr,
8200 r->out.handle);
8201 default:
8202 return WERR_UNKNOWN_LEVEL;
8206 /****************************************************************
8207 _spoolss_AddPrinter
8208 ****************************************************************/
8210 WERROR _spoolss_AddPrinter(struct pipes_struct *p,
8211 struct spoolss_AddPrinter *r)
8213 struct spoolss_AddPrinterEx a;
8214 struct spoolss_UserLevelCtr userlevel_ctr;
8216 ZERO_STRUCT(userlevel_ctr);
8218 userlevel_ctr.level = 1;
8220 a.in.server = r->in.server;
8221 a.in.info_ctr = r->in.info_ctr;
8222 a.in.devmode_ctr = r->in.devmode_ctr;
8223 a.in.secdesc_ctr = r->in.secdesc_ctr;
8224 a.in.userlevel_ctr = &userlevel_ctr;
8225 a.out.handle = r->out.handle;
8227 return _spoolss_AddPrinterEx(p, &a);
8230 /****************************************************************
8231 _spoolss_AddPrinterDriverEx
8232 ****************************************************************/
8234 WERROR _spoolss_AddPrinterDriverEx(struct pipes_struct *p,
8235 struct spoolss_AddPrinterDriverEx *r)
8237 WERROR err = WERR_OK;
8238 const char *driver_name = NULL;
8239 uint32_t version;
8240 const char *fn;
8242 switch (p->opnum) {
8243 case NDR_SPOOLSS_ADDPRINTERDRIVER:
8244 fn = "_spoolss_AddPrinterDriver";
8245 break;
8246 case NDR_SPOOLSS_ADDPRINTERDRIVEREX:
8247 fn = "_spoolss_AddPrinterDriverEx";
8248 break;
8249 default:
8250 return WERR_INVALID_PARAM;
8254 * we only support the semantics of AddPrinterDriver()
8255 * i.e. only copy files that are newer than existing ones
8258 if (r->in.flags == 0) {
8259 return WERR_INVALID_PARAM;
8262 if (r->in.flags != APD_COPY_NEW_FILES) {
8263 return WERR_ACCESS_DENIED;
8266 /* FIXME */
8267 if (r->in.info_ctr->level != 3 && r->in.info_ctr->level != 6) {
8268 /* Clever hack from Martin Zielinski <mz@seh.de>
8269 * to allow downgrade from level 8 (Vista).
8271 DEBUG(0,("%s: level %d not yet implemented\n", fn,
8272 r->in.info_ctr->level));
8273 return WERR_UNKNOWN_LEVEL;
8276 DEBUG(5,("Cleaning driver's information\n"));
8277 err = clean_up_driver_struct(p->mem_ctx, p->session_info, r->in.info_ctr);
8278 if (!W_ERROR_IS_OK(err))
8279 goto done;
8281 DEBUG(5,("Moving driver to final destination\n"));
8282 err = move_driver_to_download_area(p->session_info, r->in.info_ctr);
8283 if (!W_ERROR_IS_OK(err)) {
8284 goto done;
8287 err = winreg_add_driver_internal(p->mem_ctx,
8288 get_session_info_system(),
8289 p->msg_ctx,
8290 r->in.info_ctr,
8291 &driver_name,
8292 &version);
8293 if (!W_ERROR_IS_OK(err)) {
8294 goto done;
8298 * I think this is where he DrvUpgradePrinter() hook would be
8299 * be called in a driver's interface DLL on a Windows NT 4.0/2k
8300 * server. Right now, we just need to send ourselves a message
8301 * to update each printer bound to this driver. --jerry
8304 if (!srv_spoolss_drv_upgrade_printer(driver_name, p->msg_ctx)) {
8305 DEBUG(0,("%s: Failed to send message about upgrading driver [%s]!\n",
8306 fn, driver_name));
8309 done:
8310 return err;
8313 /****************************************************************
8314 _spoolss_AddPrinterDriver
8315 ****************************************************************/
8317 WERROR _spoolss_AddPrinterDriver(struct pipes_struct *p,
8318 struct spoolss_AddPrinterDriver *r)
8320 struct spoolss_AddPrinterDriverEx a;
8322 switch (r->in.info_ctr->level) {
8323 case 2:
8324 case 3:
8325 case 4:
8326 case 5:
8327 break;
8328 default:
8329 return WERR_UNKNOWN_LEVEL;
8332 a.in.servername = r->in.servername;
8333 a.in.info_ctr = r->in.info_ctr;
8334 a.in.flags = APD_COPY_NEW_FILES;
8336 return _spoolss_AddPrinterDriverEx(p, &a);
8339 /****************************************************************************
8340 ****************************************************************************/
8342 struct _spoolss_paths {
8343 int type;
8344 const char *share;
8345 const char *dir;
8348 enum { SPOOLSS_DRIVER_PATH, SPOOLSS_PRTPROCS_PATH };
8350 static const struct _spoolss_paths spoolss_paths[]= {
8351 { SPOOLSS_DRIVER_PATH, "print$", "DRIVERS" },
8352 { SPOOLSS_PRTPROCS_PATH, "prnproc$", "PRTPROCS" }
8355 static WERROR compose_spoolss_server_path(TALLOC_CTX *mem_ctx,
8356 const char *servername,
8357 const char *environment,
8358 int component,
8359 char **path)
8361 const char *pservername = NULL;
8362 const char *long_archi = SPOOLSS_ARCHITECTURE_NT_X86;
8363 const char *short_archi;
8365 *path = NULL;
8367 /* environment may be empty */
8368 if (environment && strlen(environment)) {
8369 long_archi = environment;
8372 /* servername may be empty */
8373 if (servername && strlen(servername)) {
8374 pservername = canon_servername(servername);
8376 if (!is_myname_or_ipaddr(pservername)) {
8377 return WERR_INVALID_PARAM;
8381 if (!(short_archi = get_short_archi(long_archi))) {
8382 return WERR_INVALID_ENVIRONMENT;
8385 switch (component) {
8386 case SPOOLSS_PRTPROCS_PATH:
8387 case SPOOLSS_DRIVER_PATH:
8388 if (pservername) {
8389 *path = talloc_asprintf(mem_ctx,
8390 "\\\\%s\\%s\\%s",
8391 pservername,
8392 spoolss_paths[component].share,
8393 short_archi);
8394 } else {
8395 *path = talloc_asprintf(mem_ctx, "%s\\%s\\%s",
8396 SPOOLSS_DEFAULT_SERVER_PATH,
8397 spoolss_paths[component].dir,
8398 short_archi);
8400 break;
8401 default:
8402 return WERR_INVALID_PARAM;
8405 if (!*path) {
8406 return WERR_NOMEM;
8409 return WERR_OK;
8412 /****************************************************************************
8413 ****************************************************************************/
8415 static WERROR getprinterdriverdir_level_1(TALLOC_CTX *mem_ctx,
8416 const char *servername,
8417 const char *environment,
8418 struct spoolss_DriverDirectoryInfo1 *r)
8420 WERROR werr;
8421 char *path = NULL;
8423 werr = compose_spoolss_server_path(mem_ctx,
8424 servername,
8425 environment,
8426 SPOOLSS_DRIVER_PATH,
8427 &path);
8428 if (!W_ERROR_IS_OK(werr)) {
8429 return werr;
8432 DEBUG(4,("printer driver directory: [%s]\n", path));
8434 r->directory_name = path;
8436 return WERR_OK;
8439 /****************************************************************
8440 _spoolss_GetPrinterDriverDirectory
8441 ****************************************************************/
8443 WERROR _spoolss_GetPrinterDriverDirectory(struct pipes_struct *p,
8444 struct spoolss_GetPrinterDriverDirectory *r)
8446 WERROR werror;
8448 /* that's an [in out] buffer */
8450 if (!r->in.buffer && (r->in.offered != 0)) {
8451 return WERR_INVALID_PARAM;
8454 DEBUG(5,("_spoolss_GetPrinterDriverDirectory: level %d\n",
8455 r->in.level));
8457 *r->out.needed = 0;
8459 /* r->in.level is ignored */
8461 werror = getprinterdriverdir_level_1(p->mem_ctx,
8462 r->in.server,
8463 r->in.environment,
8464 &r->out.info->info1);
8465 if (!W_ERROR_IS_OK(werror)) {
8466 TALLOC_FREE(r->out.info);
8467 return werror;
8470 *r->out.needed = SPOOLSS_BUFFER_UNION(spoolss_DriverDirectoryInfo,
8471 r->out.info, r->in.level);
8472 r->out.info = SPOOLSS_BUFFER_OK(r->out.info, NULL);
8474 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
8477 /****************************************************************
8478 _spoolss_EnumPrinterData
8479 ****************************************************************/
8481 WERROR _spoolss_EnumPrinterData(struct pipes_struct *p,
8482 struct spoolss_EnumPrinterData *r)
8484 WERROR result;
8485 struct spoolss_EnumPrinterDataEx r2;
8486 uint32_t count;
8487 struct spoolss_PrinterEnumValues *info, *val = NULL;
8488 uint32_t needed;
8490 r2.in.handle = r->in.handle;
8491 r2.in.key_name = "PrinterDriverData";
8492 r2.in.offered = 0;
8493 r2.out.count = &count;
8494 r2.out.info = &info;
8495 r2.out.needed = &needed;
8497 result = _spoolss_EnumPrinterDataEx(p, &r2);
8498 if (W_ERROR_EQUAL(result, WERR_MORE_DATA)) {
8499 r2.in.offered = needed;
8500 result = _spoolss_EnumPrinterDataEx(p, &r2);
8502 if (!W_ERROR_IS_OK(result)) {
8503 return result;
8507 * The NT machine wants to know the biggest size of value and data
8509 * cf: MSDN EnumPrinterData remark section
8512 if (!r->in.value_offered && !r->in.data_offered) {
8513 uint32_t biggest_valuesize = 0;
8514 uint32_t biggest_datasize = 0;
8515 int i, name_length;
8517 DEBUGADD(6,("Activating NT mega-hack to find sizes\n"));
8519 for (i=0; i<count; i++) {
8521 name_length = strlen(info[i].value_name);
8522 if (strlen(info[i].value_name) > biggest_valuesize) {
8523 biggest_valuesize = name_length;
8526 if (info[i].data_length > biggest_datasize) {
8527 biggest_datasize = info[i].data_length;
8530 DEBUG(6,("current values: [%d], [%d]\n", biggest_valuesize,
8531 biggest_datasize));
8534 /* the value is an UNICODE string but real_value_size is the length
8535 in bytes including the trailing 0 */
8537 *r->out.value_needed = 2 * (1 + biggest_valuesize);
8538 *r->out.data_needed = biggest_datasize;
8540 DEBUG(6,("final values: [%d], [%d]\n",
8541 *r->out.value_needed, *r->out.data_needed));
8543 return WERR_OK;
8546 if (r->in.enum_index < count) {
8547 val = &info[r->in.enum_index];
8550 if (val == NULL) {
8551 /* out_value should default to "" or else NT4 has
8552 problems unmarshalling the response */
8554 if (r->in.value_offered) {
8555 *r->out.value_needed = 1;
8556 r->out.value_name = talloc_strdup(r, "");
8557 if (!r->out.value_name) {
8558 return WERR_NOMEM;
8560 } else {
8561 r->out.value_name = NULL;
8562 *r->out.value_needed = 0;
8565 /* the data is counted in bytes */
8567 *r->out.data_needed = r->in.data_offered;
8569 result = WERR_NO_MORE_ITEMS;
8570 } else {
8572 * the value is:
8573 * - counted in bytes in the request
8574 * - counted in UNICODE chars in the max reply
8575 * - counted in bytes in the real size
8577 * take a pause *before* coding not *during* coding
8580 /* name */
8581 if (r->in.value_offered) {
8582 r->out.value_name = talloc_strdup(r, val->value_name);
8583 if (!r->out.value_name) {
8584 return WERR_NOMEM;
8586 *r->out.value_needed = val->value_name_len;
8587 } else {
8588 r->out.value_name = NULL;
8589 *r->out.value_needed = 0;
8592 /* type */
8594 *r->out.type = val->type;
8596 /* data - counted in bytes */
8599 * See the section "Dynamically Typed Query Parameters"
8600 * in MS-RPRN.
8603 if (r->out.data && val->data && val->data->data &&
8604 val->data_length && r->in.data_offered) {
8605 memcpy(r->out.data, val->data->data,
8606 MIN(val->data_length,r->in.data_offered));
8609 *r->out.data_needed = val->data_length;
8611 result = WERR_OK;
8614 return result;
8617 /****************************************************************
8618 _spoolss_SetPrinterData
8619 ****************************************************************/
8621 WERROR _spoolss_SetPrinterData(struct pipes_struct *p,
8622 struct spoolss_SetPrinterData *r)
8624 struct spoolss_SetPrinterDataEx r2;
8626 r2.in.handle = r->in.handle;
8627 r2.in.key_name = "PrinterDriverData";
8628 r2.in.value_name = r->in.value_name;
8629 r2.in.type = r->in.type;
8630 r2.in.data = r->in.data;
8631 r2.in.offered = r->in.offered;
8633 return _spoolss_SetPrinterDataEx(p, &r2);
8636 /****************************************************************
8637 _spoolss_ResetPrinter
8638 ****************************************************************/
8640 WERROR _spoolss_ResetPrinter(struct pipes_struct *p,
8641 struct spoolss_ResetPrinter *r)
8643 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
8644 int snum;
8646 DEBUG(5,("_spoolss_ResetPrinter\n"));
8649 * All we do is to check to see if the handle and queue is valid.
8650 * This call really doesn't mean anything to us because we only
8651 * support RAW printing. --jerry
8654 if (!Printer) {
8655 DEBUG(2,("_spoolss_ResetPrinter: Invalid handle (%s:%u:%u).\n",
8656 OUR_HANDLE(r->in.handle)));
8657 return WERR_BADFID;
8660 if (!get_printer_snum(p, r->in.handle, &snum, NULL))
8661 return WERR_BADFID;
8664 /* blindly return success */
8665 return WERR_OK;
8668 /****************************************************************
8669 _spoolss_DeletePrinterData
8670 ****************************************************************/
8672 WERROR _spoolss_DeletePrinterData(struct pipes_struct *p,
8673 struct spoolss_DeletePrinterData *r)
8675 struct spoolss_DeletePrinterDataEx r2;
8677 r2.in.handle = r->in.handle;
8678 r2.in.key_name = "PrinterDriverData";
8679 r2.in.value_name = r->in.value_name;
8681 return _spoolss_DeletePrinterDataEx(p, &r2);
8684 /****************************************************************
8685 _spoolss_AddForm
8686 ****************************************************************/
8688 WERROR _spoolss_AddForm(struct pipes_struct *p,
8689 struct spoolss_AddForm *r)
8691 struct spoolss_AddFormInfo1 *form = r->in.info.info1;
8692 int snum = -1;
8693 WERROR status = WERR_OK;
8694 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
8695 struct dcerpc_binding_handle *b;
8696 TALLOC_CTX *tmp_ctx = NULL;
8698 DEBUG(5,("_spoolss_AddForm\n"));
8700 if (!Printer) {
8701 DEBUG(2,("_spoolss_AddForm: Invalid handle (%s:%u:%u).\n",
8702 OUR_HANDLE(r->in.handle)));
8703 return WERR_BADFID;
8706 /* if the user is not root, doesn't have SE_PRINT_OPERATOR privilege,
8707 and not a printer admin, then fail */
8709 if ((p->session_info->unix_token->uid != sec_initial_uid()) &&
8710 !security_token_has_privilege(p->session_info->security_token,
8711 SEC_PRIV_PRINT_OPERATOR)) {
8712 DEBUG(2,("_spoolss_Addform: denied by insufficient permissions.\n"));
8713 return WERR_ACCESS_DENIED;
8716 switch (form->flags) {
8717 case SPOOLSS_FORM_USER:
8718 case SPOOLSS_FORM_BUILTIN:
8719 case SPOOLSS_FORM_PRINTER:
8720 break;
8721 default:
8722 return WERR_INVALID_PARAM;
8725 tmp_ctx = talloc_new(p->mem_ctx);
8726 if (!tmp_ctx) {
8727 return WERR_NOMEM;
8730 status = winreg_printer_binding_handle(tmp_ctx,
8731 get_session_info_system(),
8732 p->msg_ctx,
8733 &b);
8734 if (!W_ERROR_IS_OK(status)) {
8735 goto done;
8738 status = winreg_printer_addform1(tmp_ctx, b, form);
8739 if (!W_ERROR_IS_OK(status)) {
8740 goto done;
8744 * ChangeID must always be set if this is a printer
8746 if (Printer->printer_type == SPLHND_PRINTER) {
8747 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
8748 status = WERR_BADFID;
8749 goto done;
8752 status = winreg_printer_update_changeid(tmp_ctx, b,
8753 lp_const_servicename(snum));
8756 done:
8757 talloc_free(tmp_ctx);
8758 return status;
8761 /****************************************************************
8762 _spoolss_DeleteForm
8763 ****************************************************************/
8765 WERROR _spoolss_DeleteForm(struct pipes_struct *p,
8766 struct spoolss_DeleteForm *r)
8768 const char *form_name = r->in.form_name;
8769 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
8770 int snum = -1;
8771 WERROR status = WERR_OK;
8772 struct dcerpc_binding_handle *b;
8773 TALLOC_CTX *tmp_ctx = NULL;
8775 DEBUG(5,("_spoolss_DeleteForm\n"));
8777 if (!Printer) {
8778 DEBUG(2,("_spoolss_DeleteForm: Invalid handle (%s:%u:%u).\n",
8779 OUR_HANDLE(r->in.handle)));
8780 return WERR_BADFID;
8783 if ((p->session_info->unix_token->uid != sec_initial_uid()) &&
8784 !security_token_has_privilege(p->session_info->security_token,
8785 SEC_PRIV_PRINT_OPERATOR)) {
8786 DEBUG(2,("_spoolss_DeleteForm: denied by insufficient permissions.\n"));
8787 return WERR_ACCESS_DENIED;
8790 tmp_ctx = talloc_new(p->mem_ctx);
8791 if (!tmp_ctx) {
8792 return WERR_NOMEM;
8795 status = winreg_printer_binding_handle(tmp_ctx,
8796 get_session_info_system(),
8797 p->msg_ctx,
8798 &b);
8799 if (!W_ERROR_IS_OK(status)) {
8800 goto done;
8803 status = winreg_printer_deleteform1(tmp_ctx, b, form_name);
8804 if (!W_ERROR_IS_OK(status)) {
8805 goto done;
8809 * ChangeID must always be set if this is a printer
8811 if (Printer->printer_type == SPLHND_PRINTER) {
8812 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
8813 status = WERR_BADFID;
8814 goto done;
8817 status = winreg_printer_update_changeid(tmp_ctx, b,
8818 lp_const_servicename(snum));
8821 done:
8822 talloc_free(tmp_ctx);
8823 return status;
8826 /****************************************************************
8827 _spoolss_SetForm
8828 ****************************************************************/
8830 WERROR _spoolss_SetForm(struct pipes_struct *p,
8831 struct spoolss_SetForm *r)
8833 struct spoolss_AddFormInfo1 *form = r->in.info.info1;
8834 const char *form_name = r->in.form_name;
8835 int snum = -1;
8836 WERROR status = WERR_OK;
8837 struct dcerpc_binding_handle *b;
8838 TALLOC_CTX *tmp_ctx = NULL;
8840 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
8842 DEBUG(5,("_spoolss_SetForm\n"));
8844 if (!Printer) {
8845 DEBUG(2,("_spoolss_SetForm: Invalid handle (%s:%u:%u).\n",
8846 OUR_HANDLE(r->in.handle)));
8847 return WERR_BADFID;
8850 /* if the user is not root, doesn't have SE_PRINT_OPERATOR privilege,
8851 and not a printer admin, then fail */
8853 if ((p->session_info->unix_token->uid != sec_initial_uid()) &&
8854 !security_token_has_privilege(p->session_info->security_token,
8855 SEC_PRIV_PRINT_OPERATOR)) {
8856 DEBUG(2,("_spoolss_Setform: denied by insufficient permissions.\n"));
8857 return WERR_ACCESS_DENIED;
8860 tmp_ctx = talloc_new(p->mem_ctx);
8861 if (!tmp_ctx) {
8862 return WERR_NOMEM;
8865 status = winreg_printer_binding_handle(tmp_ctx,
8866 get_session_info_system(),
8867 p->msg_ctx,
8868 &b);
8869 if (!W_ERROR_IS_OK(status)) {
8870 goto done;
8873 status = winreg_printer_setform1(tmp_ctx, b,
8874 form_name,
8875 form);
8876 if (!W_ERROR_IS_OK(status)) {
8877 goto done;
8881 * ChangeID must always be set if this is a printer
8883 if (Printer->printer_type == SPLHND_PRINTER) {
8884 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
8885 status = WERR_BADFID;
8886 goto done;
8889 status = winreg_printer_update_changeid(tmp_ctx, b,
8890 lp_const_servicename(snum));
8893 done:
8894 talloc_free(tmp_ctx);
8895 return status;
8898 /****************************************************************************
8899 fill_print_processor1
8900 ****************************************************************************/
8902 static WERROR fill_print_processor1(TALLOC_CTX *mem_ctx,
8903 struct spoolss_PrintProcessorInfo1 *r,
8904 const char *print_processor_name)
8906 r->print_processor_name = talloc_strdup(mem_ctx, print_processor_name);
8907 W_ERROR_HAVE_NO_MEMORY(r->print_processor_name);
8909 return WERR_OK;
8912 /****************************************************************************
8913 enumprintprocessors level 1.
8914 ****************************************************************************/
8916 static WERROR enumprintprocessors_level_1(TALLOC_CTX *mem_ctx,
8917 union spoolss_PrintProcessorInfo **info_p,
8918 uint32_t *count)
8920 union spoolss_PrintProcessorInfo *info;
8921 WERROR result;
8923 info = talloc_array(mem_ctx, union spoolss_PrintProcessorInfo, 1);
8924 W_ERROR_HAVE_NO_MEMORY(info);
8926 *count = 1;
8928 result = fill_print_processor1(info, &info[0].info1, "winprint");
8929 if (!W_ERROR_IS_OK(result)) {
8930 goto out;
8933 out:
8934 if (!W_ERROR_IS_OK(result)) {
8935 TALLOC_FREE(info);
8936 *count = 0;
8937 return result;
8940 *info_p = info;
8942 return WERR_OK;
8945 /****************************************************************
8946 _spoolss_EnumPrintProcessors
8947 ****************************************************************/
8949 WERROR _spoolss_EnumPrintProcessors(struct pipes_struct *p,
8950 struct spoolss_EnumPrintProcessors *r)
8952 WERROR result;
8954 /* that's an [in out] buffer */
8956 if (!r->in.buffer && (r->in.offered != 0)) {
8957 return WERR_INVALID_PARAM;
8960 DEBUG(5,("_spoolss_EnumPrintProcessors\n"));
8963 * Enumerate the print processors ...
8965 * Just reply with "winprint", to keep NT happy
8966 * and I can use my nice printer checker.
8969 *r->out.count = 0;
8970 *r->out.needed = 0;
8971 *r->out.info = NULL;
8973 if (!get_short_archi(r->in.environment)) {
8974 return WERR_INVALID_ENVIRONMENT;
8977 switch (r->in.level) {
8978 case 1:
8979 result = enumprintprocessors_level_1(p->mem_ctx, r->out.info,
8980 r->out.count);
8981 break;
8982 default:
8983 return WERR_UNKNOWN_LEVEL;
8986 if (!W_ERROR_IS_OK(result)) {
8987 return result;
8990 *r->out.needed = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
8991 spoolss_EnumPrintProcessors,
8992 *r->out.info, r->in.level,
8993 *r->out.count);
8994 *r->out.info = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
8995 *r->out.count = SPOOLSS_BUFFER_OK(*r->out.count, 0);
8997 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
9000 /****************************************************************************
9001 fill_printprocdatatype1
9002 ****************************************************************************/
9004 static WERROR fill_printprocdatatype1(TALLOC_CTX *mem_ctx,
9005 struct spoolss_PrintProcDataTypesInfo1 *r,
9006 const char *name_array)
9008 r->name_array = talloc_strdup(mem_ctx, name_array);
9009 W_ERROR_HAVE_NO_MEMORY(r->name_array);
9011 return WERR_OK;
9014 /****************************************************************************
9015 enumprintprocdatatypes level 1.
9016 ****************************************************************************/
9018 static WERROR enumprintprocdatatypes_level_1(TALLOC_CTX *mem_ctx,
9019 union spoolss_PrintProcDataTypesInfo **info_p,
9020 uint32_t *count)
9022 WERROR result;
9023 union spoolss_PrintProcDataTypesInfo *info;
9025 info = talloc_array(mem_ctx, union spoolss_PrintProcDataTypesInfo, 1);
9026 W_ERROR_HAVE_NO_MEMORY(info);
9028 *count = 1;
9030 result = fill_printprocdatatype1(info, &info[0].info1, "RAW");
9031 if (!W_ERROR_IS_OK(result)) {
9032 goto out;
9035 out:
9036 if (!W_ERROR_IS_OK(result)) {
9037 TALLOC_FREE(info);
9038 *count = 0;
9039 return result;
9042 *info_p = info;
9044 return WERR_OK;
9047 /****************************************************************
9048 _spoolss_EnumPrintProcDataTypes
9049 ****************************************************************/
9051 WERROR _spoolss_EnumPrintProcDataTypes(struct pipes_struct *p,
9052 struct spoolss_EnumPrintProcDataTypes *r)
9054 WERROR result;
9056 /* that's an [in out] buffer */
9058 if (!r->in.buffer && (r->in.offered != 0)) {
9059 return WERR_INVALID_PARAM;
9062 DEBUG(5,("_spoolss_EnumPrintProcDataTypes\n"));
9064 *r->out.count = 0;
9065 *r->out.needed = 0;
9066 *r->out.info = NULL;
9068 if (r->in.print_processor_name == NULL ||
9069 !strequal(r->in.print_processor_name, "winprint")) {
9070 return WERR_UNKNOWN_PRINTPROCESSOR;
9073 switch (r->in.level) {
9074 case 1:
9075 result = enumprintprocdatatypes_level_1(p->mem_ctx, r->out.info,
9076 r->out.count);
9077 break;
9078 default:
9079 return WERR_UNKNOWN_LEVEL;
9082 if (!W_ERROR_IS_OK(result)) {
9083 return result;
9086 *r->out.needed = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
9087 spoolss_EnumPrintProcDataTypes,
9088 *r->out.info, r->in.level,
9089 *r->out.count);
9090 *r->out.info = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
9091 *r->out.count = SPOOLSS_BUFFER_OK(*r->out.count, 0);
9093 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
9096 /****************************************************************************
9097 fill_monitor_1
9098 ****************************************************************************/
9100 static WERROR fill_monitor_1(TALLOC_CTX *mem_ctx,
9101 struct spoolss_MonitorInfo1 *r,
9102 const char *monitor_name)
9104 r->monitor_name = talloc_strdup(mem_ctx, monitor_name);
9105 W_ERROR_HAVE_NO_MEMORY(r->monitor_name);
9107 return WERR_OK;
9110 /****************************************************************************
9111 fill_monitor_2
9112 ****************************************************************************/
9114 static WERROR fill_monitor_2(TALLOC_CTX *mem_ctx,
9115 struct spoolss_MonitorInfo2 *r,
9116 const char *monitor_name,
9117 const char *environment,
9118 const char *dll_name)
9120 r->monitor_name = talloc_strdup(mem_ctx, monitor_name);
9121 W_ERROR_HAVE_NO_MEMORY(r->monitor_name);
9122 r->environment = talloc_strdup(mem_ctx, environment);
9123 W_ERROR_HAVE_NO_MEMORY(r->environment);
9124 r->dll_name = talloc_strdup(mem_ctx, dll_name);
9125 W_ERROR_HAVE_NO_MEMORY(r->dll_name);
9127 return WERR_OK;
9130 /****************************************************************************
9131 enumprintmonitors level 1.
9132 ****************************************************************************/
9134 static WERROR enumprintmonitors_level_1(TALLOC_CTX *mem_ctx,
9135 union spoolss_MonitorInfo **info_p,
9136 uint32_t *count)
9138 union spoolss_MonitorInfo *info;
9139 WERROR result = WERR_OK;
9141 info = talloc_array(mem_ctx, union spoolss_MonitorInfo, 2);
9142 W_ERROR_HAVE_NO_MEMORY(info);
9144 *count = 2;
9146 result = fill_monitor_1(info, &info[0].info1,
9147 SPL_LOCAL_PORT);
9148 if (!W_ERROR_IS_OK(result)) {
9149 goto out;
9152 result = fill_monitor_1(info, &info[1].info1,
9153 SPL_TCPIP_PORT);
9154 if (!W_ERROR_IS_OK(result)) {
9155 goto out;
9158 out:
9159 if (!W_ERROR_IS_OK(result)) {
9160 TALLOC_FREE(info);
9161 *count = 0;
9162 return result;
9165 *info_p = info;
9167 return WERR_OK;
9170 /****************************************************************************
9171 enumprintmonitors level 2.
9172 ****************************************************************************/
9174 static WERROR enumprintmonitors_level_2(TALLOC_CTX *mem_ctx,
9175 union spoolss_MonitorInfo **info_p,
9176 uint32_t *count)
9178 union spoolss_MonitorInfo *info;
9179 WERROR result = WERR_OK;
9181 info = talloc_array(mem_ctx, union spoolss_MonitorInfo, 2);
9182 W_ERROR_HAVE_NO_MEMORY(info);
9184 *count = 2;
9186 result = fill_monitor_2(info, &info[0].info2,
9187 SPL_LOCAL_PORT,
9188 "Windows NT X86", /* FIXME */
9189 "localmon.dll");
9190 if (!W_ERROR_IS_OK(result)) {
9191 goto out;
9194 result = fill_monitor_2(info, &info[1].info2,
9195 SPL_TCPIP_PORT,
9196 "Windows NT X86", /* FIXME */
9197 "tcpmon.dll");
9198 if (!W_ERROR_IS_OK(result)) {
9199 goto out;
9202 out:
9203 if (!W_ERROR_IS_OK(result)) {
9204 TALLOC_FREE(info);
9205 *count = 0;
9206 return result;
9209 *info_p = info;
9211 return WERR_OK;
9214 /****************************************************************
9215 _spoolss_EnumMonitors
9216 ****************************************************************/
9218 WERROR _spoolss_EnumMonitors(struct pipes_struct *p,
9219 struct spoolss_EnumMonitors *r)
9221 WERROR result;
9223 /* that's an [in out] buffer */
9225 if (!r->in.buffer && (r->in.offered != 0)) {
9226 return WERR_INVALID_PARAM;
9229 DEBUG(5,("_spoolss_EnumMonitors\n"));
9232 * Enumerate the print monitors ...
9234 * Just reply with "Local Port", to keep NT happy
9235 * and I can use my nice printer checker.
9238 *r->out.count = 0;
9239 *r->out.needed = 0;
9240 *r->out.info = NULL;
9242 switch (r->in.level) {
9243 case 1:
9244 result = enumprintmonitors_level_1(p->mem_ctx, r->out.info,
9245 r->out.count);
9246 break;
9247 case 2:
9248 result = enumprintmonitors_level_2(p->mem_ctx, r->out.info,
9249 r->out.count);
9250 break;
9251 default:
9252 return WERR_UNKNOWN_LEVEL;
9255 if (!W_ERROR_IS_OK(result)) {
9256 return result;
9259 *r->out.needed = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
9260 spoolss_EnumMonitors,
9261 *r->out.info, r->in.level,
9262 *r->out.count);
9263 *r->out.info = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
9264 *r->out.count = SPOOLSS_BUFFER_OK(*r->out.count, 0);
9266 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
9269 /****************************************************************************
9270 ****************************************************************************/
9272 static WERROR getjob_level_1(TALLOC_CTX *mem_ctx,
9273 const print_queue_struct *queue,
9274 int count, int snum,
9275 struct spoolss_PrinterInfo2 *pinfo2,
9276 uint32_t jobid,
9277 struct spoolss_JobInfo1 *r)
9279 int i = 0;
9280 bool found = false;
9282 for (i=0; i<count; i++) {
9283 if (queue[i].sysjob == (int)jobid) {
9284 found = true;
9285 break;
9289 if (found == false) {
9290 /* NT treats not found as bad param... yet another bad choice */
9291 return WERR_INVALID_PARAM;
9294 return fill_job_info1(mem_ctx,
9296 &queue[i],
9298 snum,
9299 pinfo2);
9302 /****************************************************************************
9303 ****************************************************************************/
9305 static WERROR getjob_level_2(TALLOC_CTX *mem_ctx,
9306 const print_queue_struct *queue,
9307 int count, int snum,
9308 struct spoolss_PrinterInfo2 *pinfo2,
9309 uint32_t jobid,
9310 struct spoolss_JobInfo2 *r)
9312 int i = 0;
9313 bool found = false;
9314 struct spoolss_DeviceMode *devmode;
9315 WERROR result;
9317 for (i=0; i<count; i++) {
9318 if (queue[i].sysjob == (int)jobid) {
9319 found = true;
9320 break;
9324 if (found == false) {
9325 /* NT treats not found as bad param... yet another bad
9326 choice */
9327 return WERR_INVALID_PARAM;
9331 * if the print job does not have a DEVMODE associated with it,
9332 * just use the one for the printer. A NULL devicemode is not
9333 * a failure condition
9336 devmode = print_job_devmode(mem_ctx, lp_const_servicename(snum), jobid);
9337 if (!devmode) {
9338 result = spoolss_create_default_devmode(mem_ctx,
9339 pinfo2->printername,
9340 &devmode);
9341 if (!W_ERROR_IS_OK(result)) {
9342 DEBUG(3, ("Can't proceed w/o a devmode!"));
9343 return result;
9347 return fill_job_info2(mem_ctx,
9349 &queue[i],
9351 snum,
9352 pinfo2,
9353 devmode);
9356 /****************************************************************
9357 _spoolss_GetJob
9358 ****************************************************************/
9360 WERROR _spoolss_GetJob(struct pipes_struct *p,
9361 struct spoolss_GetJob *r)
9363 WERROR result = WERR_OK;
9364 struct spoolss_PrinterInfo2 *pinfo2 = NULL;
9365 int snum;
9366 int count;
9367 print_queue_struct *queue = NULL;
9368 print_status_struct prt_status;
9370 /* that's an [in out] buffer */
9372 if (!r->in.buffer && (r->in.offered != 0)) {
9373 return WERR_INVALID_PARAM;
9376 DEBUG(5,("_spoolss_GetJob\n"));
9378 *r->out.needed = 0;
9380 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
9381 return WERR_BADFID;
9384 result = winreg_get_printer_internal(p->mem_ctx,
9385 get_session_info_system(),
9386 p->msg_ctx,
9387 lp_const_servicename(snum),
9388 &pinfo2);
9389 if (!W_ERROR_IS_OK(result)) {
9390 return result;
9393 count = print_queue_status(p->msg_ctx, snum, &queue, &prt_status);
9395 DEBUGADD(4,("count:[%d], prt_status:[%d], [%s]\n",
9396 count, prt_status.status, prt_status.message));
9398 switch (r->in.level) {
9399 case 1:
9400 result = getjob_level_1(p->mem_ctx,
9401 queue, count, snum, pinfo2,
9402 r->in.job_id, &r->out.info->info1);
9403 break;
9404 case 2:
9405 result = getjob_level_2(p->mem_ctx,
9406 queue, count, snum, pinfo2,
9407 r->in.job_id, &r->out.info->info2);
9408 break;
9409 default:
9410 result = WERR_UNKNOWN_LEVEL;
9411 break;
9414 SAFE_FREE(queue);
9415 TALLOC_FREE(pinfo2);
9417 if (!W_ERROR_IS_OK(result)) {
9418 TALLOC_FREE(r->out.info);
9419 return result;
9422 *r->out.needed = SPOOLSS_BUFFER_UNION(spoolss_JobInfo, r->out.info,
9423 r->in.level);
9424 r->out.info = SPOOLSS_BUFFER_OK(r->out.info, NULL);
9426 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
9429 /****************************************************************
9430 _spoolss_GetPrinterDataEx
9431 ****************************************************************/
9433 WERROR _spoolss_GetPrinterDataEx(struct pipes_struct *p,
9434 struct spoolss_GetPrinterDataEx *r)
9437 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
9438 const char *printer;
9439 int snum = 0;
9440 WERROR result = WERR_OK;
9441 DATA_BLOB blob;
9442 enum winreg_Type val_type = REG_NONE;
9443 uint8_t *val_data = NULL;
9444 uint32_t val_size = 0;
9445 struct dcerpc_binding_handle *b;
9446 TALLOC_CTX *tmp_ctx;
9448 DEBUG(4,("_spoolss_GetPrinterDataEx\n"));
9450 DEBUG(10, ("_spoolss_GetPrinterDataEx: key => [%s], value => [%s]\n",
9451 r->in.key_name, r->in.value_name));
9453 /* in case of problem, return some default values */
9455 *r->out.needed = 0;
9456 *r->out.type = REG_NONE;
9458 tmp_ctx = talloc_new(p->mem_ctx);
9459 if (!tmp_ctx) {
9460 return WERR_NOMEM;
9463 if (!Printer) {
9464 DEBUG(2,("_spoolss_GetPrinterDataEx: Invalid handle (%s:%u:%u).\n",
9465 OUR_HANDLE(r->in.handle)));
9466 result = WERR_BADFID;
9467 goto done;
9470 /* check to see if the keyname is valid */
9471 if (!strlen(r->in.key_name)) {
9472 result = WERR_INVALID_PARAM;
9473 goto done;
9476 /* Is the handle to a printer or to the server? */
9478 if (Printer->printer_type == SPLHND_SERVER) {
9480 union spoolss_PrinterData data;
9482 result = getprinterdata_printer_server(tmp_ctx,
9483 r->in.value_name,
9484 r->out.type,
9485 &data);
9486 if (!W_ERROR_IS_OK(result)) {
9487 goto done;
9490 result = push_spoolss_PrinterData(tmp_ctx, &blob,
9491 *r->out.type, &data);
9492 if (!W_ERROR_IS_OK(result)) {
9493 goto done;
9496 *r->out.needed = blob.length;
9498 if (r->in.offered >= *r->out.needed) {
9499 memcpy(r->out.data, blob.data, blob.length);
9502 result = WERR_OK;
9503 goto done;
9506 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
9507 result = WERR_BADFID;
9508 goto done;
9510 printer = lp_const_servicename(snum);
9512 result = winreg_printer_binding_handle(tmp_ctx,
9513 get_session_info_system(),
9514 p->msg_ctx,
9515 &b);
9516 if (!W_ERROR_IS_OK(result)) {
9517 goto done;
9520 /* XP sends this and wants the ChangeID value from PRINTER_INFO_0 */
9521 if (strequal(r->in.key_name, SPOOL_PRINTERDATA_KEY) &&
9522 strequal(r->in.value_name, "ChangeId")) {
9523 *r->out.type = REG_DWORD;
9524 *r->out.needed = 4;
9525 if (r->in.offered >= *r->out.needed) {
9526 uint32_t changeid = 0;
9528 result = winreg_printer_get_changeid(tmp_ctx, b,
9529 printer,
9530 &changeid);
9531 if (!W_ERROR_IS_OK(result)) {
9532 goto done;
9535 SIVAL(r->out.data, 0, changeid);
9536 result = WERR_OK;
9538 goto done;
9541 result = winreg_get_printer_dataex(tmp_ctx, b,
9542 printer,
9543 r->in.key_name,
9544 r->in.value_name,
9545 &val_type,
9546 &val_data,
9547 &val_size);
9548 if (!W_ERROR_IS_OK(result)) {
9549 goto done;
9552 *r->out.needed = val_size;
9553 *r->out.type = val_type;
9555 if (r->in.offered >= *r->out.needed) {
9556 memcpy(r->out.data, val_data, val_size);
9559 done:
9560 /* NOTE: do not replace type when returning WERR_MORE_DATA */
9562 if (W_ERROR_IS_OK(result)) {
9563 result = SPOOLSS_BUFFER_OK(WERR_OK, WERR_MORE_DATA);
9566 talloc_free(tmp_ctx);
9567 return result;
9570 /****************************************************************
9571 _spoolss_SetPrinterDataEx
9572 ****************************************************************/
9574 WERROR _spoolss_SetPrinterDataEx(struct pipes_struct *p,
9575 struct spoolss_SetPrinterDataEx *r)
9577 struct spoolss_PrinterInfo2 *pinfo2 = NULL;
9578 int snum = 0;
9579 WERROR result = WERR_OK;
9580 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
9581 char *oid_string;
9582 struct dcerpc_binding_handle *b;
9583 TALLOC_CTX *tmp_ctx;
9585 DEBUG(4,("_spoolss_SetPrinterDataEx\n"));
9587 /* From MSDN documentation of SetPrinterDataEx: pass request to
9588 SetPrinterData if key is "PrinterDriverData" */
9590 if (!Printer) {
9591 DEBUG(2,("_spoolss_SetPrinterDataEx: Invalid handle (%s:%u:%u).\n",
9592 OUR_HANDLE(r->in.handle)));
9593 return WERR_BADFID;
9596 if (Printer->printer_type == SPLHND_SERVER) {
9597 DEBUG(10,("_spoolss_SetPrinterDataEx: "
9598 "Not implemented for server handles yet\n"));
9599 return WERR_INVALID_PARAM;
9602 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
9603 return WERR_BADFID;
9607 * Access check : NT returns "access denied" if you make a
9608 * SetPrinterData call without the necessary privildge.
9609 * we were originally returning OK if nothing changed
9610 * which made Win2k issue **a lot** of SetPrinterData
9611 * when connecting to a printer --jerry
9614 if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
9615 DEBUG(3, ("_spoolss_SetPrinterDataEx: "
9616 "change denied by handle access permissions\n"));
9617 return WERR_ACCESS_DENIED;
9620 tmp_ctx = talloc_new(p->mem_ctx);
9621 if (!tmp_ctx) {
9622 return WERR_NOMEM;
9625 result = winreg_printer_binding_handle(tmp_ctx,
9626 get_session_info_system(),
9627 p->msg_ctx,
9628 &b);
9629 if (!W_ERROR_IS_OK(result)) {
9630 goto done;
9633 result = winreg_get_printer(tmp_ctx, b,
9634 lp_servicename(talloc_tos(), snum),
9635 &pinfo2);
9636 if (!W_ERROR_IS_OK(result)) {
9637 goto done;
9640 /* check for OID in valuename */
9642 oid_string = strchr(r->in.value_name, ',');
9643 if (oid_string) {
9644 *oid_string = '\0';
9645 oid_string++;
9648 /* save the registry data */
9650 result = winreg_set_printer_dataex(tmp_ctx, b,
9651 pinfo2->sharename,
9652 r->in.key_name,
9653 r->in.value_name,
9654 r->in.type,
9655 r->in.data,
9656 r->in.offered);
9658 if (W_ERROR_IS_OK(result)) {
9659 /* save the OID if one was specified */
9660 if (oid_string) {
9661 char *str = talloc_asprintf(tmp_ctx, "%s\\%s",
9662 r->in.key_name, SPOOL_OID_KEY);
9663 if (!str) {
9664 result = WERR_NOMEM;
9665 goto done;
9669 * I'm not checking the status here on purpose. Don't know
9670 * if this is right, but I'm returning the status from the
9671 * previous set_printer_dataex() call. I have no idea if
9672 * this is right. --jerry
9674 winreg_set_printer_dataex(tmp_ctx, b,
9675 pinfo2->sharename,
9676 str,
9677 r->in.value_name,
9678 REG_SZ,
9679 (uint8_t *) oid_string,
9680 strlen(oid_string) + 1);
9683 result = winreg_printer_update_changeid(tmp_ctx, b,
9684 lp_const_servicename(snum));
9688 done:
9689 talloc_free(tmp_ctx);
9690 return result;
9693 /****************************************************************
9694 _spoolss_DeletePrinterDataEx
9695 ****************************************************************/
9697 WERROR _spoolss_DeletePrinterDataEx(struct pipes_struct *p,
9698 struct spoolss_DeletePrinterDataEx *r)
9700 const char *printer;
9701 int snum=0;
9702 WERROR status = WERR_OK;
9703 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
9705 DEBUG(5,("_spoolss_DeletePrinterDataEx\n"));
9707 if (!Printer) {
9708 DEBUG(2,("_spoolss_DeletePrinterDataEx: "
9709 "Invalid handle (%s:%u:%u).\n",
9710 OUR_HANDLE(r->in.handle)));
9711 return WERR_BADFID;
9714 if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
9715 DEBUG(3, ("_spoolss_DeletePrinterDataEx: "
9716 "printer properties change denied by handle\n"));
9717 return WERR_ACCESS_DENIED;
9720 if (!r->in.value_name || !r->in.key_name) {
9721 return WERR_NOMEM;
9724 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
9725 return WERR_BADFID;
9727 printer = lp_const_servicename(snum);
9729 status = winreg_delete_printer_dataex_internal(p->mem_ctx,
9730 get_session_info_system(),
9731 p->msg_ctx,
9732 printer,
9733 r->in.key_name,
9734 r->in.value_name);
9735 if (W_ERROR_IS_OK(status)) {
9736 status = winreg_printer_update_changeid_internal(p->mem_ctx,
9737 get_session_info_system(),
9738 p->msg_ctx,
9739 printer);
9742 return status;
9745 /****************************************************************
9746 _spoolss_EnumPrinterKey
9747 ****************************************************************/
9749 WERROR _spoolss_EnumPrinterKey(struct pipes_struct *p,
9750 struct spoolss_EnumPrinterKey *r)
9752 uint32_t num_keys;
9753 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
9754 int snum = 0;
9755 WERROR result = WERR_BADFILE;
9756 const char **array = NULL;
9757 DATA_BLOB blob;
9759 DEBUG(4,("_spoolss_EnumPrinterKey\n"));
9761 if (!Printer) {
9762 DEBUG(2,("_spoolss_EnumPrinterKey: Invalid handle (%s:%u:%u).\n",
9763 OUR_HANDLE(r->in.handle)));
9764 return WERR_BADFID;
9767 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
9768 return WERR_BADFID;
9771 result = winreg_enum_printer_key_internal(p->mem_ctx,
9772 get_session_info_system(),
9773 p->msg_ctx,
9774 lp_const_servicename(snum),
9775 r->in.key_name,
9776 &num_keys,
9777 &array);
9778 if (!W_ERROR_IS_OK(result)) {
9779 goto done;
9782 if (!push_reg_multi_sz(p->mem_ctx, &blob, array)) {
9783 result = WERR_NOMEM;
9784 goto done;
9787 *r->out._ndr_size = r->in.offered / 2;
9788 *r->out.needed = blob.length;
9790 if (r->in.offered < *r->out.needed) {
9791 result = WERR_MORE_DATA;
9792 } else {
9793 result = WERR_OK;
9794 r->out.key_buffer->string_array = array;
9797 done:
9798 if (!W_ERROR_IS_OK(result)) {
9799 TALLOC_FREE(array);
9800 if (!W_ERROR_EQUAL(result, WERR_MORE_DATA)) {
9801 *r->out.needed = 0;
9805 return result;
9808 /****************************************************************
9809 _spoolss_DeletePrinterKey
9810 ****************************************************************/
9812 WERROR _spoolss_DeletePrinterKey(struct pipes_struct *p,
9813 struct spoolss_DeletePrinterKey *r)
9815 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
9816 int snum=0;
9817 WERROR status;
9818 const char *printer;
9819 struct dcerpc_binding_handle *b;
9820 TALLOC_CTX *tmp_ctx;
9822 DEBUG(5,("_spoolss_DeletePrinterKey\n"));
9824 if (!Printer) {
9825 DEBUG(2,("_spoolss_DeletePrinterKey: Invalid handle (%s:%u:%u).\n",
9826 OUR_HANDLE(r->in.handle)));
9827 return WERR_BADFID;
9830 /* if keyname == NULL, return error */
9831 if ( !r->in.key_name )
9832 return WERR_INVALID_PARAM;
9834 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
9835 return WERR_BADFID;
9838 if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
9839 DEBUG(3, ("_spoolss_DeletePrinterKey: "
9840 "printer properties change denied by handle\n"));
9841 return WERR_ACCESS_DENIED;
9844 printer = lp_const_servicename(snum);
9846 tmp_ctx = talloc_new(p->mem_ctx);
9847 if (!tmp_ctx) {
9848 return WERR_NOMEM;
9851 status = winreg_printer_binding_handle(tmp_ctx,
9852 get_session_info_system(),
9853 p->msg_ctx,
9854 &b);
9855 if (!W_ERROR_IS_OK(status)) {
9856 goto done;
9859 /* delete the key and all subkeys */
9860 status = winreg_delete_printer_key(tmp_ctx, b,
9861 printer,
9862 r->in.key_name);
9863 if (W_ERROR_IS_OK(status)) {
9864 status = winreg_printer_update_changeid(tmp_ctx, b,
9865 printer);
9868 done:
9869 talloc_free(tmp_ctx);
9870 return status;
9873 /****************************************************************
9874 _spoolss_EnumPrinterDataEx
9875 ****************************************************************/
9877 WERROR _spoolss_EnumPrinterDataEx(struct pipes_struct *p,
9878 struct spoolss_EnumPrinterDataEx *r)
9880 uint32_t count = 0;
9881 struct spoolss_PrinterEnumValues *info = NULL;
9882 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
9883 int snum;
9884 WERROR result;
9886 DEBUG(4,("_spoolss_EnumPrinterDataEx\n"));
9888 *r->out.count = 0;
9889 *r->out.needed = 0;
9890 *r->out.info = NULL;
9892 if (!Printer) {
9893 DEBUG(2,("_spoolss_EnumPrinterDataEx: Invalid handle (%s:%u:%u1<).\n",
9894 OUR_HANDLE(r->in.handle)));
9895 return WERR_BADFID;
9899 * first check for a keyname of NULL or "". Win2k seems to send
9900 * this a lot and we should send back WERR_INVALID_PARAM
9901 * no need to spend time looking up the printer in this case.
9902 * --jerry
9905 if (!strlen(r->in.key_name)) {
9906 result = WERR_INVALID_PARAM;
9907 goto done;
9910 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
9911 return WERR_BADFID;
9914 /* now look for a match on the key name */
9915 result = winreg_enum_printer_dataex_internal(p->mem_ctx,
9916 get_session_info_system(),
9917 p->msg_ctx,
9918 lp_const_servicename(snum),
9919 r->in.key_name,
9920 &count,
9921 &info);
9922 if (!W_ERROR_IS_OK(result)) {
9923 goto done;
9926 #if 0 /* FIXME - gd */
9927 /* housekeeping information in the reply */
9929 /* Fix from Martin Zielinski <mz@seh.de> - ensure
9930 * the hand marshalled container size is a multiple
9931 * of 4 bytes for RPC alignment.
9934 if (needed % 4) {
9935 needed += 4-(needed % 4);
9937 #endif
9938 *r->out.count = count;
9939 *r->out.info = info;
9941 done:
9942 if (!W_ERROR_IS_OK(result)) {
9943 return result;
9946 *r->out.needed = SPOOLSS_BUFFER_ARRAY(p->mem_ctx,
9947 spoolss_EnumPrinterDataEx,
9948 *r->out.info,
9949 *r->out.count);
9950 *r->out.info = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
9951 *r->out.count = SPOOLSS_BUFFER_OK(*r->out.count, *r->out.count);
9953 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_MORE_DATA);
9956 /****************************************************************************
9957 ****************************************************************************/
9959 static WERROR getprintprocessordirectory_level_1(TALLOC_CTX *mem_ctx,
9960 const char *servername,
9961 const char *environment,
9962 struct spoolss_PrintProcessorDirectoryInfo1 *r)
9964 WERROR werr;
9965 char *path = NULL;
9967 werr = compose_spoolss_server_path(mem_ctx,
9968 servername,
9969 environment,
9970 SPOOLSS_PRTPROCS_PATH,
9971 &path);
9972 if (!W_ERROR_IS_OK(werr)) {
9973 return werr;
9976 DEBUG(4,("print processor directory: [%s]\n", path));
9978 r->directory_name = path;
9980 return WERR_OK;
9983 /****************************************************************
9984 _spoolss_GetPrintProcessorDirectory
9985 ****************************************************************/
9987 WERROR _spoolss_GetPrintProcessorDirectory(struct pipes_struct *p,
9988 struct spoolss_GetPrintProcessorDirectory *r)
9990 WERROR result;
9991 char *prnproc_share = NULL;
9992 bool prnproc_share_exists = false;
9993 int snum;
9995 /* that's an [in out] buffer */
9997 if (!r->in.buffer && (r->in.offered != 0)) {
9998 return WERR_INVALID_PARAM;
10001 DEBUG(5,("_spoolss_GetPrintProcessorDirectory: level %d\n",
10002 r->in.level));
10004 *r->out.needed = 0;
10006 /* r->in.level is ignored */
10008 /* We always should reply with a local print processor directory so that
10009 * users are not forced to have a [prnproc$] share on the Samba spoolss
10010 * server, if users decide to do so, lets announce it though - Guenther */
10012 snum = find_service(talloc_tos(), "prnproc$", &prnproc_share);
10013 if (!prnproc_share) {
10014 return WERR_NOMEM;
10016 if (snum != -1) {
10017 prnproc_share_exists = true;
10020 result = getprintprocessordirectory_level_1(p->mem_ctx,
10021 prnproc_share_exists ? r->in.server : NULL,
10022 r->in.environment,
10023 &r->out.info->info1);
10024 if (!W_ERROR_IS_OK(result)) {
10025 TALLOC_FREE(r->out.info);
10026 return result;
10029 *r->out.needed = SPOOLSS_BUFFER_UNION(spoolss_PrintProcessorDirectoryInfo,
10030 r->out.info, r->in.level);
10031 r->out.info = SPOOLSS_BUFFER_OK(r->out.info, NULL);
10033 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
10036 /*******************************************************************
10037 ********************************************************************/
10039 static bool push_monitorui_buf(TALLOC_CTX *mem_ctx, DATA_BLOB *buf,
10040 const char *dllname)
10042 enum ndr_err_code ndr_err;
10043 struct spoolss_MonitorUi ui;
10045 ui.dll_name = dllname;
10047 ndr_err = ndr_push_struct_blob(buf, mem_ctx, &ui,
10048 (ndr_push_flags_fn_t)ndr_push_spoolss_MonitorUi);
10049 if (NDR_ERR_CODE_IS_SUCCESS(ndr_err) && (DEBUGLEVEL >= 10)) {
10050 NDR_PRINT_DEBUG(spoolss_MonitorUi, &ui);
10052 return NDR_ERR_CODE_IS_SUCCESS(ndr_err);
10055 /*******************************************************************
10056 Streams the monitor UI DLL name in UNICODE
10057 *******************************************************************/
10059 static WERROR xcvtcp_monitorui(TALLOC_CTX *mem_ctx,
10060 struct security_token *token, DATA_BLOB *in,
10061 DATA_BLOB *out, uint32_t *needed)
10063 const char *dllname = "tcpmonui.dll";
10065 *needed = (strlen(dllname)+1) * 2;
10067 if (out->length < *needed) {
10068 return WERR_INSUFFICIENT_BUFFER;
10071 if (!push_monitorui_buf(mem_ctx, out, dllname)) {
10072 return WERR_NOMEM;
10075 return WERR_OK;
10078 /*******************************************************************
10079 ********************************************************************/
10081 static bool pull_port_data_1(TALLOC_CTX *mem_ctx,
10082 struct spoolss_PortData1 *port1,
10083 const DATA_BLOB *buf)
10085 enum ndr_err_code ndr_err;
10086 ndr_err = ndr_pull_struct_blob(buf, mem_ctx, port1,
10087 (ndr_pull_flags_fn_t)ndr_pull_spoolss_PortData1);
10088 if (NDR_ERR_CODE_IS_SUCCESS(ndr_err) && (DEBUGLEVEL >= 10)) {
10089 NDR_PRINT_DEBUG(spoolss_PortData1, port1);
10091 return NDR_ERR_CODE_IS_SUCCESS(ndr_err);
10094 /*******************************************************************
10095 ********************************************************************/
10097 static bool pull_port_data_2(TALLOC_CTX *mem_ctx,
10098 struct spoolss_PortData2 *port2,
10099 const DATA_BLOB *buf)
10101 enum ndr_err_code ndr_err;
10102 ndr_err = ndr_pull_struct_blob(buf, mem_ctx, port2,
10103 (ndr_pull_flags_fn_t)ndr_pull_spoolss_PortData2);
10104 if (NDR_ERR_CODE_IS_SUCCESS(ndr_err) && (DEBUGLEVEL >= 10)) {
10105 NDR_PRINT_DEBUG(spoolss_PortData2, port2);
10107 return NDR_ERR_CODE_IS_SUCCESS(ndr_err);
10110 /*******************************************************************
10111 Create a new TCP/IP port
10112 *******************************************************************/
10114 static WERROR xcvtcp_addport(TALLOC_CTX *mem_ctx,
10115 struct security_token *token, DATA_BLOB *in,
10116 DATA_BLOB *out, uint32_t *needed)
10118 struct spoolss_PortData1 port1;
10119 struct spoolss_PortData2 port2;
10120 char *device_uri = NULL;
10121 uint32_t version;
10123 const char *portname;
10124 const char *hostaddress;
10125 const char *queue;
10126 uint32_t port_number;
10127 uint32_t protocol;
10129 /* peek for spoolss_PortData version */
10131 if (!in || (in->length < (128 + 4))) {
10132 return WERR_GENERAL_FAILURE;
10135 version = IVAL(in->data, 128);
10137 switch (version) {
10138 case 1:
10139 ZERO_STRUCT(port1);
10141 if (!pull_port_data_1(mem_ctx, &port1, in)) {
10142 return WERR_NOMEM;
10145 portname = port1.portname;
10146 hostaddress = port1.hostaddress;
10147 queue = port1.queue;
10148 protocol = port1.protocol;
10149 port_number = port1.port_number;
10151 break;
10152 case 2:
10153 ZERO_STRUCT(port2);
10155 if (!pull_port_data_2(mem_ctx, &port2, in)) {
10156 return WERR_NOMEM;
10159 portname = port2.portname;
10160 hostaddress = port2.hostaddress;
10161 queue = port2.queue;
10162 protocol = port2.protocol;
10163 port_number = port2.port_number;
10165 break;
10166 default:
10167 DEBUG(1,("xcvtcp_addport: "
10168 "unknown version of port_data: %d\n", version));
10169 return WERR_UNKNOWN_PORT;
10172 /* create the device URI and call the add_port_hook() */
10174 switch (protocol) {
10175 case PROTOCOL_RAWTCP_TYPE:
10176 device_uri = talloc_asprintf(mem_ctx,
10177 "socket://%s:%d/", hostaddress,
10178 port_number);
10179 break;
10181 case PROTOCOL_LPR_TYPE:
10182 device_uri = talloc_asprintf(mem_ctx,
10183 "lpr://%s/%s", hostaddress, queue );
10184 break;
10186 default:
10187 return WERR_UNKNOWN_PORT;
10190 if (!device_uri) {
10191 return WERR_NOMEM;
10194 return add_port_hook(mem_ctx, token, portname, device_uri);
10197 /*******************************************************************
10198 *******************************************************************/
10200 struct xcv_api_table xcvtcp_cmds[] = {
10201 { "MonitorUI", xcvtcp_monitorui },
10202 { "AddPort", xcvtcp_addport},
10203 { NULL, NULL }
10206 static WERROR process_xcvtcp_command(TALLOC_CTX *mem_ctx,
10207 struct security_token *token, const char *command,
10208 DATA_BLOB *inbuf,
10209 DATA_BLOB *outbuf,
10210 uint32_t *needed )
10212 int i;
10214 DEBUG(10,("process_xcvtcp_command: Received command \"%s\"\n", command));
10216 for ( i=0; xcvtcp_cmds[i].name; i++ ) {
10217 if ( strcmp( command, xcvtcp_cmds[i].name ) == 0 )
10218 return xcvtcp_cmds[i].fn(mem_ctx, token, inbuf, outbuf, needed);
10221 return WERR_BADFUNC;
10224 /*******************************************************************
10225 *******************************************************************/
10226 #if 0 /* don't support management using the "Local Port" monitor */
10228 static WERROR xcvlocal_monitorui(TALLOC_CTX *mem_ctx,
10229 struct security_token *token, DATA_BLOB *in,
10230 DATA_BLOB *out, uint32_t *needed)
10232 const char *dllname = "localui.dll";
10234 *needed = (strlen(dllname)+1) * 2;
10236 if (out->length < *needed) {
10237 return WERR_INSUFFICIENT_BUFFER;
10240 if (!push_monitorui_buf(mem_ctx, out, dllname)) {
10241 return WERR_NOMEM;
10244 return WERR_OK;
10247 /*******************************************************************
10248 *******************************************************************/
10250 struct xcv_api_table xcvlocal_cmds[] = {
10251 { "MonitorUI", xcvlocal_monitorui },
10252 { NULL, NULL }
10254 #else
10255 struct xcv_api_table xcvlocal_cmds[] = {
10256 { NULL, NULL }
10258 #endif
10262 /*******************************************************************
10263 *******************************************************************/
10265 static WERROR process_xcvlocal_command(TALLOC_CTX *mem_ctx,
10266 struct security_token *token, const char *command,
10267 DATA_BLOB *inbuf, DATA_BLOB *outbuf,
10268 uint32_t *needed)
10270 int i;
10272 DEBUG(10,("process_xcvlocal_command: Received command \"%s\"\n", command));
10274 for ( i=0; xcvlocal_cmds[i].name; i++ ) {
10275 if ( strcmp( command, xcvlocal_cmds[i].name ) == 0 )
10276 return xcvlocal_cmds[i].fn(mem_ctx, token, inbuf, outbuf, needed);
10278 return WERR_BADFUNC;
10281 /****************************************************************
10282 _spoolss_XcvData
10283 ****************************************************************/
10285 WERROR _spoolss_XcvData(struct pipes_struct *p,
10286 struct spoolss_XcvData *r)
10288 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
10289 DATA_BLOB out_data = data_blob_null;
10290 WERROR werror;
10292 if (!Printer) {
10293 DEBUG(2,("_spoolss_XcvData: Invalid handle (%s:%u:%u).\n",
10294 OUR_HANDLE(r->in.handle)));
10295 return WERR_BADFID;
10298 /* Has to be a handle to the TCP/IP port monitor */
10300 if ( !(Printer->printer_type & (SPLHND_PORTMON_LOCAL|SPLHND_PORTMON_TCP)) ) {
10301 DEBUG(2,("_spoolss_XcvData: Call only valid for Port Monitors\n"));
10302 return WERR_BADFID;
10305 /* requires administrative access to the server */
10307 if ( !(Printer->access_granted & SERVER_ACCESS_ADMINISTER) ) {
10308 DEBUG(2,("_spoolss_XcvData: denied by handle permissions.\n"));
10309 return WERR_ACCESS_DENIED;
10312 /* Allocate the outgoing buffer */
10314 if (r->in.out_data_size) {
10315 out_data = data_blob_talloc_zero(p->mem_ctx, r->in.out_data_size);
10316 if (out_data.data == NULL) {
10317 return WERR_NOMEM;
10321 switch ( Printer->printer_type ) {
10322 case SPLHND_PORTMON_TCP:
10323 werror = process_xcvtcp_command(p->mem_ctx,
10324 p->session_info->security_token,
10325 r->in.function_name,
10326 &r->in.in_data, &out_data,
10327 r->out.needed);
10328 break;
10329 case SPLHND_PORTMON_LOCAL:
10330 werror = process_xcvlocal_command(p->mem_ctx,
10331 p->session_info->security_token,
10332 r->in.function_name,
10333 &r->in.in_data, &out_data,
10334 r->out.needed);
10335 break;
10336 default:
10337 werror = WERR_INVALID_PRINT_MONITOR;
10340 if (!W_ERROR_IS_OK(werror)) {
10341 return werror;
10344 *r->out.status_code = 0;
10346 if (r->out.out_data && out_data.data && r->in.out_data_size && out_data.length) {
10347 memcpy(r->out.out_data, out_data.data,
10348 MIN(r->in.out_data_size, out_data.length));
10351 return WERR_OK;
10354 /****************************************************************
10355 _spoolss_AddPrintProcessor
10356 ****************************************************************/
10358 WERROR _spoolss_AddPrintProcessor(struct pipes_struct *p,
10359 struct spoolss_AddPrintProcessor *r)
10361 /* for now, just indicate success and ignore the add. We'll
10362 automatically set the winprint processor for printer
10363 entries later. Used to debug the LexMark Optra S 1855 PCL
10364 driver --jerry */
10366 return WERR_OK;
10369 /****************************************************************
10370 _spoolss_AddPort
10371 ****************************************************************/
10373 WERROR _spoolss_AddPort(struct pipes_struct *p,
10374 struct spoolss_AddPort *r)
10376 /* do what w2k3 does */
10378 return WERR_NOT_SUPPORTED;
10381 /****************************************************************
10382 _spoolss_GetPrinterDriver
10383 ****************************************************************/
10385 WERROR _spoolss_GetPrinterDriver(struct pipes_struct *p,
10386 struct spoolss_GetPrinterDriver *r)
10388 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10389 return WERR_NOT_SUPPORTED;
10392 /****************************************************************
10393 _spoolss_ReadPrinter
10394 ****************************************************************/
10396 WERROR _spoolss_ReadPrinter(struct pipes_struct *p,
10397 struct spoolss_ReadPrinter *r)
10399 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10400 return WERR_NOT_SUPPORTED;
10403 /****************************************************************
10404 _spoolss_WaitForPrinterChange
10405 ****************************************************************/
10407 WERROR _spoolss_WaitForPrinterChange(struct pipes_struct *p,
10408 struct spoolss_WaitForPrinterChange *r)
10410 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10411 return WERR_NOT_SUPPORTED;
10414 /****************************************************************
10415 _spoolss_ConfigurePort
10416 ****************************************************************/
10418 WERROR _spoolss_ConfigurePort(struct pipes_struct *p,
10419 struct spoolss_ConfigurePort *r)
10421 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10422 return WERR_NOT_SUPPORTED;
10425 /****************************************************************
10426 _spoolss_DeletePort
10427 ****************************************************************/
10429 WERROR _spoolss_DeletePort(struct pipes_struct *p,
10430 struct spoolss_DeletePort *r)
10432 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10433 return WERR_NOT_SUPPORTED;
10436 /****************************************************************
10437 _spoolss_CreatePrinterIC
10438 ****************************************************************/
10440 WERROR _spoolss_CreatePrinterIC(struct pipes_struct *p,
10441 struct spoolss_CreatePrinterIC *r)
10443 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10444 return WERR_NOT_SUPPORTED;
10447 /****************************************************************
10448 _spoolss_PlayGDIScriptOnPrinterIC
10449 ****************************************************************/
10451 WERROR _spoolss_PlayGDIScriptOnPrinterIC(struct pipes_struct *p,
10452 struct spoolss_PlayGDIScriptOnPrinterIC *r)
10454 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10455 return WERR_NOT_SUPPORTED;
10458 /****************************************************************
10459 _spoolss_DeletePrinterIC
10460 ****************************************************************/
10462 WERROR _spoolss_DeletePrinterIC(struct pipes_struct *p,
10463 struct spoolss_DeletePrinterIC *r)
10465 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10466 return WERR_NOT_SUPPORTED;
10469 /****************************************************************
10470 _spoolss_AddPrinterConnection
10471 ****************************************************************/
10473 WERROR _spoolss_AddPrinterConnection(struct pipes_struct *p,
10474 struct spoolss_AddPrinterConnection *r)
10476 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10477 return WERR_NOT_SUPPORTED;
10480 /****************************************************************
10481 _spoolss_DeletePrinterConnection
10482 ****************************************************************/
10484 WERROR _spoolss_DeletePrinterConnection(struct pipes_struct *p,
10485 struct spoolss_DeletePrinterConnection *r)
10487 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10488 return WERR_NOT_SUPPORTED;
10491 /****************************************************************
10492 _spoolss_PrinterMessageBox
10493 ****************************************************************/
10495 WERROR _spoolss_PrinterMessageBox(struct pipes_struct *p,
10496 struct spoolss_PrinterMessageBox *r)
10498 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10499 return WERR_NOT_SUPPORTED;
10502 /****************************************************************
10503 _spoolss_AddMonitor
10504 ****************************************************************/
10506 WERROR _spoolss_AddMonitor(struct pipes_struct *p,
10507 struct spoolss_AddMonitor *r)
10509 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10510 return WERR_NOT_SUPPORTED;
10513 /****************************************************************
10514 _spoolss_DeleteMonitor
10515 ****************************************************************/
10517 WERROR _spoolss_DeleteMonitor(struct pipes_struct *p,
10518 struct spoolss_DeleteMonitor *r)
10520 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10521 return WERR_NOT_SUPPORTED;
10524 /****************************************************************
10525 _spoolss_DeletePrintProcessor
10526 ****************************************************************/
10528 WERROR _spoolss_DeletePrintProcessor(struct pipes_struct *p,
10529 struct spoolss_DeletePrintProcessor *r)
10531 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10532 return WERR_NOT_SUPPORTED;
10535 /****************************************************************
10536 _spoolss_AddPrintProvidor
10537 ****************************************************************/
10539 WERROR _spoolss_AddPrintProvidor(struct pipes_struct *p,
10540 struct spoolss_AddPrintProvidor *r)
10542 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10543 return WERR_NOT_SUPPORTED;
10546 /****************************************************************
10547 _spoolss_DeletePrintProvidor
10548 ****************************************************************/
10550 WERROR _spoolss_DeletePrintProvidor(struct pipes_struct *p,
10551 struct spoolss_DeletePrintProvidor *r)
10553 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10554 return WERR_NOT_SUPPORTED;
10557 /****************************************************************
10558 _spoolss_FindFirstPrinterChangeNotification
10559 ****************************************************************/
10561 WERROR _spoolss_FindFirstPrinterChangeNotification(struct pipes_struct *p,
10562 struct spoolss_FindFirstPrinterChangeNotification *r)
10564 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10565 return WERR_NOT_SUPPORTED;
10568 /****************************************************************
10569 _spoolss_FindNextPrinterChangeNotification
10570 ****************************************************************/
10572 WERROR _spoolss_FindNextPrinterChangeNotification(struct pipes_struct *p,
10573 struct spoolss_FindNextPrinterChangeNotification *r)
10575 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10576 return WERR_NOT_SUPPORTED;
10579 /****************************************************************
10580 _spoolss_RouterFindFirstPrinterChangeNotificationOld
10581 ****************************************************************/
10583 WERROR _spoolss_RouterFindFirstPrinterChangeNotificationOld(struct pipes_struct *p,
10584 struct spoolss_RouterFindFirstPrinterChangeNotificationOld *r)
10586 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10587 return WERR_NOT_SUPPORTED;
10590 /****************************************************************
10591 _spoolss_ReplyOpenPrinter
10592 ****************************************************************/
10594 WERROR _spoolss_ReplyOpenPrinter(struct pipes_struct *p,
10595 struct spoolss_ReplyOpenPrinter *r)
10597 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10598 return WERR_NOT_SUPPORTED;
10601 /****************************************************************
10602 _spoolss_RouterReplyPrinter
10603 ****************************************************************/
10605 WERROR _spoolss_RouterReplyPrinter(struct pipes_struct *p,
10606 struct spoolss_RouterReplyPrinter *r)
10608 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10609 return WERR_NOT_SUPPORTED;
10612 /****************************************************************
10613 _spoolss_ReplyClosePrinter
10614 ****************************************************************/
10616 WERROR _spoolss_ReplyClosePrinter(struct pipes_struct *p,
10617 struct spoolss_ReplyClosePrinter *r)
10619 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10620 return WERR_NOT_SUPPORTED;
10623 /****************************************************************
10624 _spoolss_AddPortEx
10625 ****************************************************************/
10627 WERROR _spoolss_AddPortEx(struct pipes_struct *p,
10628 struct spoolss_AddPortEx *r)
10630 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10631 return WERR_NOT_SUPPORTED;
10634 /****************************************************************
10635 _spoolss_RouterFindFirstPrinterChangeNotification
10636 ****************************************************************/
10638 WERROR _spoolss_RouterFindFirstPrinterChangeNotification(struct pipes_struct *p,
10639 struct spoolss_RouterFindFirstPrinterChangeNotification *r)
10641 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10642 return WERR_NOT_SUPPORTED;
10645 /****************************************************************
10646 _spoolss_SpoolerInit
10647 ****************************************************************/
10649 WERROR _spoolss_SpoolerInit(struct pipes_struct *p,
10650 struct spoolss_SpoolerInit *r)
10652 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10653 return WERR_NOT_SUPPORTED;
10656 /****************************************************************
10657 _spoolss_ResetPrinterEx
10658 ****************************************************************/
10660 WERROR _spoolss_ResetPrinterEx(struct pipes_struct *p,
10661 struct spoolss_ResetPrinterEx *r)
10663 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10664 return WERR_NOT_SUPPORTED;
10667 /****************************************************************
10668 _spoolss_RouterReplyPrinterEx
10669 ****************************************************************/
10671 WERROR _spoolss_RouterReplyPrinterEx(struct pipes_struct *p,
10672 struct spoolss_RouterReplyPrinterEx *r)
10674 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10675 return WERR_NOT_SUPPORTED;
10678 /****************************************************************
10679 _spoolss_44
10680 ****************************************************************/
10682 WERROR _spoolss_44(struct pipes_struct *p,
10683 struct spoolss_44 *r)
10685 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10686 return WERR_NOT_SUPPORTED;
10689 /****************************************************************
10690 _spoolss_SetPort
10691 ****************************************************************/
10693 WERROR _spoolss_SetPort(struct pipes_struct *p,
10694 struct spoolss_SetPort *r)
10696 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10697 return WERR_NOT_SUPPORTED;
10700 /****************************************************************
10701 _spoolss_4a
10702 ****************************************************************/
10704 WERROR _spoolss_4a(struct pipes_struct *p,
10705 struct spoolss_4a *r)
10707 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10708 return WERR_NOT_SUPPORTED;
10711 /****************************************************************
10712 _spoolss_4b
10713 ****************************************************************/
10715 WERROR _spoolss_4b(struct pipes_struct *p,
10716 struct spoolss_4b *r)
10718 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10719 return WERR_NOT_SUPPORTED;
10722 /****************************************************************
10723 _spoolss_4c
10724 ****************************************************************/
10726 WERROR _spoolss_4c(struct pipes_struct *p,
10727 struct spoolss_4c *r)
10729 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10730 return WERR_NOT_SUPPORTED;
10733 /****************************************************************
10734 _spoolss_53
10735 ****************************************************************/
10737 WERROR _spoolss_53(struct pipes_struct *p,
10738 struct spoolss_53 *r)
10740 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10741 return WERR_NOT_SUPPORTED;
10744 /****************************************************************
10745 _spoolss_AddPerMachineConnection
10746 ****************************************************************/
10748 WERROR _spoolss_AddPerMachineConnection(struct pipes_struct *p,
10749 struct spoolss_AddPerMachineConnection *r)
10751 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10752 return WERR_NOT_SUPPORTED;
10755 /****************************************************************
10756 _spoolss_DeletePerMachineConnection
10757 ****************************************************************/
10759 WERROR _spoolss_DeletePerMachineConnection(struct pipes_struct *p,
10760 struct spoolss_DeletePerMachineConnection *r)
10762 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10763 return WERR_NOT_SUPPORTED;
10766 /****************************************************************
10767 _spoolss_EnumPerMachineConnections
10768 ****************************************************************/
10770 WERROR _spoolss_EnumPerMachineConnections(struct pipes_struct *p,
10771 struct spoolss_EnumPerMachineConnections *r)
10773 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10774 return WERR_NOT_SUPPORTED;
10777 /****************************************************************
10778 _spoolss_5a
10779 ****************************************************************/
10781 WERROR _spoolss_5a(struct pipes_struct *p,
10782 struct spoolss_5a *r)
10784 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10785 return WERR_NOT_SUPPORTED;
10788 /****************************************************************
10789 _spoolss_5b
10790 ****************************************************************/
10792 WERROR _spoolss_5b(struct pipes_struct *p,
10793 struct spoolss_5b *r)
10795 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10796 return WERR_NOT_SUPPORTED;
10799 /****************************************************************
10800 _spoolss_5c
10801 ****************************************************************/
10803 WERROR _spoolss_5c(struct pipes_struct *p,
10804 struct spoolss_5c *r)
10806 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10807 return WERR_NOT_SUPPORTED;
10810 /****************************************************************
10811 _spoolss_5d
10812 ****************************************************************/
10814 WERROR _spoolss_5d(struct pipes_struct *p,
10815 struct spoolss_5d *r)
10817 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10818 return WERR_NOT_SUPPORTED;
10821 /****************************************************************
10822 _spoolss_5e
10823 ****************************************************************/
10825 WERROR _spoolss_5e(struct pipes_struct *p,
10826 struct spoolss_5e *r)
10828 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10829 return WERR_NOT_SUPPORTED;
10832 /****************************************************************
10833 _spoolss_5f
10834 ****************************************************************/
10836 WERROR _spoolss_5f(struct pipes_struct *p,
10837 struct spoolss_5f *r)
10839 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10840 return WERR_NOT_SUPPORTED;
10843 /****************************************************************
10844 _spoolss_60
10845 ****************************************************************/
10847 WERROR _spoolss_60(struct pipes_struct *p,
10848 struct spoolss_60 *r)
10850 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10851 return WERR_NOT_SUPPORTED;
10854 /****************************************************************
10855 _spoolss_61
10856 ****************************************************************/
10858 WERROR _spoolss_61(struct pipes_struct *p,
10859 struct spoolss_61 *r)
10861 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10862 return WERR_NOT_SUPPORTED;
10865 /****************************************************************
10866 _spoolss_62
10867 ****************************************************************/
10869 WERROR _spoolss_62(struct pipes_struct *p,
10870 struct spoolss_62 *r)
10872 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10873 return WERR_NOT_SUPPORTED;
10876 /****************************************************************
10877 _spoolss_63
10878 ****************************************************************/
10880 WERROR _spoolss_63(struct pipes_struct *p,
10881 struct spoolss_63 *r)
10883 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10884 return WERR_NOT_SUPPORTED;
10887 /****************************************************************
10888 _spoolss_64
10889 ****************************************************************/
10891 WERROR _spoolss_64(struct pipes_struct *p,
10892 struct spoolss_64 *r)
10894 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10895 return WERR_NOT_SUPPORTED;
10898 /****************************************************************
10899 _spoolss_65
10900 ****************************************************************/
10902 WERROR _spoolss_65(struct pipes_struct *p,
10903 struct spoolss_65 *r)
10905 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10906 return WERR_NOT_SUPPORTED;
10909 /****************************************************************
10910 _spoolss_GetCorePrinterDrivers
10911 ****************************************************************/
10913 WERROR _spoolss_GetCorePrinterDrivers(struct pipes_struct *p,
10914 struct spoolss_GetCorePrinterDrivers *r)
10916 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10917 return WERR_NOT_SUPPORTED;
10920 /****************************************************************
10921 _spoolss_67
10922 ****************************************************************/
10924 WERROR _spoolss_67(struct pipes_struct *p,
10925 struct spoolss_67 *r)
10927 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10928 return WERR_NOT_SUPPORTED;
10931 /****************************************************************
10932 _spoolss_GetPrinterDriverPackagePath
10933 ****************************************************************/
10935 WERROR _spoolss_GetPrinterDriverPackagePath(struct pipes_struct *p,
10936 struct spoolss_GetPrinterDriverPackagePath *r)
10938 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10939 return WERR_NOT_SUPPORTED;
10942 /****************************************************************
10943 _spoolss_69
10944 ****************************************************************/
10946 WERROR _spoolss_69(struct pipes_struct *p,
10947 struct spoolss_69 *r)
10949 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10950 return WERR_NOT_SUPPORTED;
10953 /****************************************************************
10954 _spoolss_6a
10955 ****************************************************************/
10957 WERROR _spoolss_6a(struct pipes_struct *p,
10958 struct spoolss_6a *r)
10960 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10961 return WERR_NOT_SUPPORTED;
10964 /****************************************************************
10965 _spoolss_6b
10966 ****************************************************************/
10968 WERROR _spoolss_6b(struct pipes_struct *p,
10969 struct spoolss_6b *r)
10971 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10972 return WERR_NOT_SUPPORTED;
10975 /****************************************************************
10976 _spoolss_6c
10977 ****************************************************************/
10979 WERROR _spoolss_6c(struct pipes_struct *p,
10980 struct spoolss_6c *r)
10982 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10983 return WERR_NOT_SUPPORTED;
10986 /****************************************************************
10987 _spoolss_6d
10988 ****************************************************************/
10990 WERROR _spoolss_6d(struct pipes_struct *p,
10991 struct spoolss_6d *r)
10993 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10994 return WERR_NOT_SUPPORTED;