BUG 9474: Downgrade v4 printer driver requests to v3.
[Samba/gebeck_regimport.git] / source3 / rpc_server / spoolss / srv_spoolss_nt.c
blob9601ce6a62e70a8ccdc797ea06a5cafee6663284
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 struct spoolss_UserLevel1 level1;
1636 WERROR werr;
1638 ZERO_STRUCT(level1);
1640 e.in.printername = r->in.printername;
1641 e.in.datatype = r->in.datatype;
1642 e.in.devmode_ctr = r->in.devmode_ctr;
1643 e.in.access_mask = r->in.access_mask;
1644 e.in.userlevel_ctr.level = 1;
1645 e.in.userlevel_ctr.user_info.level1 = &level1;
1647 e.out.handle = r->out.handle;
1649 werr = _spoolss_OpenPrinterEx(p, &e);
1651 if (W_ERROR_EQUAL(werr, WERR_INVALID_PARAM)) {
1652 /* OpenPrinterEx returns this for a bad
1653 * printer name. We must return WERR_INVALID_PRINTER_NAME
1654 * instead.
1656 werr = WERR_INVALID_PRINTER_NAME;
1659 return werr;
1662 static WERROR copy_devicemode(TALLOC_CTX *mem_ctx,
1663 struct spoolss_DeviceMode *orig,
1664 struct spoolss_DeviceMode **dest)
1666 struct spoolss_DeviceMode *dm;
1668 dm = talloc(mem_ctx, struct spoolss_DeviceMode);
1669 if (!dm) {
1670 return WERR_NOMEM;
1673 /* copy all values, then duplicate strings and structs */
1674 *dm = *orig;
1676 dm->devicename = talloc_strdup(dm, orig->devicename);
1677 if (!dm->devicename) {
1678 return WERR_NOMEM;
1680 dm->formname = talloc_strdup(dm, orig->formname);
1681 if (!dm->formname) {
1682 return WERR_NOMEM;
1684 if (orig->driverextra_data.data) {
1685 dm->driverextra_data.data =
1686 (uint8_t *) talloc_memdup(dm, orig->driverextra_data.data,
1687 orig->driverextra_data.length);
1688 if (!dm->driverextra_data.data) {
1689 return WERR_NOMEM;
1693 *dest = dm;
1694 return WERR_OK;
1697 /****************************************************************
1698 _spoolss_OpenPrinterEx
1699 ****************************************************************/
1701 WERROR _spoolss_OpenPrinterEx(struct pipes_struct *p,
1702 struct spoolss_OpenPrinterEx *r)
1704 int snum;
1705 char *raddr;
1706 char *rhost;
1707 struct printer_handle *Printer=NULL;
1708 WERROR result;
1709 int rc;
1711 if (!r->in.printername) {
1712 return WERR_INVALID_PARAM;
1715 if (!*r->in.printername) {
1716 return WERR_INVALID_PARAM;
1719 if (r->in.userlevel_ctr.level > 3) {
1720 return WERR_INVALID_PARAM;
1722 if ((r->in.userlevel_ctr.level == 1 && !r->in.userlevel_ctr.user_info.level1) ||
1723 (r->in.userlevel_ctr.level == 2 && !r->in.userlevel_ctr.user_info.level2) ||
1724 (r->in.userlevel_ctr.level == 3 && !r->in.userlevel_ctr.user_info.level3)) {
1725 return WERR_INVALID_PARAM;
1728 /* some sanity check because you can open a printer or a print server */
1729 /* aka: \\server\printer or \\server */
1731 DEBUGADD(3,("checking name: %s\n", r->in.printername));
1733 result = open_printer_hnd(p, r->out.handle, r->in.printername, 0);
1734 if (!W_ERROR_IS_OK(result)) {
1735 DEBUG(0,("_spoolss_OpenPrinterEx: Cannot open a printer handle "
1736 "for printer %s\n", r->in.printername));
1737 ZERO_STRUCTP(r->out.handle);
1738 return result;
1741 Printer = find_printer_index_by_hnd(p, r->out.handle);
1742 if ( !Printer ) {
1743 DEBUG(0,("_spoolss_OpenPrinterEx: logic error. Can't find printer "
1744 "handle we created for printer %s\n", r->in.printername));
1745 close_printer_handle(p, r->out.handle);
1746 ZERO_STRUCTP(r->out.handle);
1747 return WERR_INVALID_PARAM;
1751 * First case: the user is opening the print server:
1753 * Disallow MS AddPrinterWizard if parameter disables it. A Win2k
1754 * client 1st tries an OpenPrinterEx with access==0, MUST be allowed.
1756 * Then both Win2k and WinNT clients try an OpenPrinterEx with
1757 * SERVER_ALL_ACCESS, which we allow only if the user is root (uid=0)
1758 * or if the user is listed in the smb.conf printer admin parameter.
1760 * Then they try OpenPrinterEx with SERVER_READ which we allow. This lets the
1761 * client view printer folder, but does not show the MSAPW.
1763 * Note: this test needs code to check access rights here too. Jeremy
1764 * could you look at this?
1766 * Second case: the user is opening a printer:
1767 * NT doesn't let us connect to a printer if the connecting user
1768 * doesn't have print permission.
1770 * Third case: user is opening a Port Monitor
1771 * access checks same as opening a handle to the print server.
1774 switch (Printer->printer_type )
1776 case SPLHND_SERVER:
1777 case SPLHND_PORTMON_TCP:
1778 case SPLHND_PORTMON_LOCAL:
1779 /* Printserver handles use global struct... */
1781 snum = -1;
1783 /* Map standard access rights to object specific access rights */
1785 se_map_standard(&r->in.access_mask,
1786 &printserver_std_mapping);
1788 /* Deny any object specific bits that don't apply to print
1789 servers (i.e printer and job specific bits) */
1791 r->in.access_mask &= SEC_MASK_SPECIFIC;
1793 if (r->in.access_mask &
1794 ~(SERVER_ACCESS_ADMINISTER | SERVER_ACCESS_ENUMERATE)) {
1795 DEBUG(3, ("access DENIED for non-printserver bits\n"));
1796 close_printer_handle(p, r->out.handle);
1797 ZERO_STRUCTP(r->out.handle);
1798 return WERR_ACCESS_DENIED;
1801 /* Allow admin access */
1803 if ( r->in.access_mask & SERVER_ACCESS_ADMINISTER )
1805 if (!lp_ms_add_printer_wizard()) {
1806 close_printer_handle(p, r->out.handle);
1807 ZERO_STRUCTP(r->out.handle);
1808 return WERR_ACCESS_DENIED;
1811 /* if the user is not root, doesn't have SE_PRINT_OPERATOR privilege,
1812 and not a printer admin, then fail */
1814 if ((p->session_info->unix_token->uid != sec_initial_uid()) &&
1815 !security_token_has_privilege(p->session_info->security_token, SEC_PRIV_PRINT_OPERATOR) &&
1816 !nt_token_check_sid(&global_sid_Builtin_Print_Operators,
1817 p->session_info->security_token)) {
1818 close_printer_handle(p, r->out.handle);
1819 ZERO_STRUCTP(r->out.handle);
1820 DEBUG(3,("access DENIED as user is not root, "
1821 "has no printoperator privilege, "
1822 "not a member of the printoperator builtin group and "
1823 "is not in printer admin list"));
1824 return WERR_ACCESS_DENIED;
1827 r->in.access_mask = SERVER_ACCESS_ADMINISTER;
1829 else
1831 r->in.access_mask = SERVER_ACCESS_ENUMERATE;
1834 DEBUG(4,("Setting print server access = %s\n", (r->in.access_mask == SERVER_ACCESS_ADMINISTER)
1835 ? "SERVER_ACCESS_ADMINISTER" : "SERVER_ACCESS_ENUMERATE" ));
1837 /* We fall through to return WERR_OK */
1838 break;
1840 case SPLHND_PRINTER:
1841 /* NT doesn't let us connect to a printer if the connecting user
1842 doesn't have print permission. */
1844 if (!get_printer_snum(p, r->out.handle, &snum, NULL)) {
1845 close_printer_handle(p, r->out.handle);
1846 ZERO_STRUCTP(r->out.handle);
1847 return WERR_BADFID;
1850 if (r->in.access_mask == SEC_FLAG_MAXIMUM_ALLOWED) {
1851 r->in.access_mask = PRINTER_ACCESS_ADMINISTER;
1854 se_map_standard(&r->in.access_mask, &printer_std_mapping);
1856 /* map an empty access mask to the minimum access mask */
1857 if (r->in.access_mask == 0x0)
1858 r->in.access_mask = PRINTER_ACCESS_USE;
1861 * If we are not serving the printer driver for this printer,
1862 * map PRINTER_ACCESS_ADMINISTER to PRINTER_ACCESS_USE. This
1863 * will keep NT clients happy --jerry
1866 if (lp_use_client_driver(snum)
1867 && (r->in.access_mask & PRINTER_ACCESS_ADMINISTER))
1869 r->in.access_mask = PRINTER_ACCESS_USE;
1872 /* check smb.conf parameters and the the sec_desc */
1873 raddr = tsocket_address_inet_addr_string(p->remote_address,
1874 p->mem_ctx);
1875 if (raddr == NULL) {
1876 return WERR_NOMEM;
1879 rc = get_remote_hostname(p->remote_address,
1880 &rhost,
1881 p->mem_ctx);
1882 if (rc < 0) {
1883 return WERR_NOMEM;
1885 if (strequal(rhost, "UNKNOWN")) {
1886 rhost = raddr;
1889 if (!allow_access(lp_hostsdeny(snum), lp_hostsallow(snum),
1890 rhost, raddr)) {
1891 DEBUG(3, ("access DENIED (hosts allow/deny) for printer open\n"));
1892 ZERO_STRUCTP(r->out.handle);
1893 return WERR_ACCESS_DENIED;
1896 if (!user_ok_token(uidtoname(p->session_info->unix_token->uid), NULL,
1897 p->session_info->security_token, snum) ||
1898 !print_access_check(p->session_info,
1899 p->msg_ctx,
1900 snum,
1901 r->in.access_mask)) {
1902 DEBUG(3, ("access DENIED for printer open\n"));
1903 close_printer_handle(p, r->out.handle);
1904 ZERO_STRUCTP(r->out.handle);
1905 return WERR_ACCESS_DENIED;
1908 if ((r->in.access_mask & SEC_MASK_SPECIFIC)& ~(PRINTER_ACCESS_ADMINISTER|PRINTER_ACCESS_USE)) {
1909 DEBUG(3, ("access DENIED for printer open - unknown bits\n"));
1910 close_printer_handle(p, r->out.handle);
1911 ZERO_STRUCTP(r->out.handle);
1912 return WERR_ACCESS_DENIED;
1915 if (r->in.access_mask & PRINTER_ACCESS_ADMINISTER)
1916 r->in.access_mask = PRINTER_ACCESS_ADMINISTER;
1917 else
1918 r->in.access_mask = PRINTER_ACCESS_USE;
1920 DEBUG(4,("Setting printer access = %s\n", (r->in.access_mask == PRINTER_ACCESS_ADMINISTER)
1921 ? "PRINTER_ACCESS_ADMINISTER" : "PRINTER_ACCESS_USE" ));
1923 winreg_create_printer_internal(p->mem_ctx,
1924 get_session_info_system(),
1925 p->msg_ctx,
1926 lp_const_servicename(snum));
1928 break;
1930 default:
1931 /* sanity check to prevent programmer error */
1932 ZERO_STRUCTP(r->out.handle);
1933 return WERR_BADFID;
1936 Printer->access_granted = r->in.access_mask;
1939 * If the client sent a devmode in the OpenPrinter() call, then
1940 * save it here in case we get a job submission on this handle
1943 if ((Printer->printer_type != SPLHND_SERVER)
1944 && (r->in.devmode_ctr.devmode != NULL)) {
1945 copy_devicemode(NULL, r->in.devmode_ctr.devmode,
1946 &Printer->devmode);
1949 return WERR_OK;
1952 /****************************************************************
1953 _spoolss_ClosePrinter
1954 ****************************************************************/
1956 WERROR _spoolss_ClosePrinter(struct pipes_struct *p,
1957 struct spoolss_ClosePrinter *r)
1959 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
1961 if (Printer && Printer->document_started) {
1962 struct spoolss_EndDocPrinter e;
1964 e.in.handle = r->in.handle;
1966 _spoolss_EndDocPrinter(p, &e);
1969 if (!close_printer_handle(p, r->in.handle))
1970 return WERR_BADFID;
1972 /* clear the returned printer handle. Observed behavior
1973 from Win2k server. Don't think this really matters.
1974 Previous code just copied the value of the closed
1975 handle. --jerry */
1977 ZERO_STRUCTP(r->out.handle);
1979 return WERR_OK;
1982 /****************************************************************
1983 _spoolss_DeletePrinter
1984 ****************************************************************/
1986 WERROR _spoolss_DeletePrinter(struct pipes_struct *p,
1987 struct spoolss_DeletePrinter *r)
1989 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
1990 WERROR result;
1991 int snum;
1993 if (Printer && Printer->document_started) {
1994 struct spoolss_EndDocPrinter e;
1996 e.in.handle = r->in.handle;
1998 _spoolss_EndDocPrinter(p, &e);
2001 if (get_printer_snum(p, r->in.handle, &snum, NULL)) {
2002 winreg_delete_printer_key_internal(p->mem_ctx,
2003 get_session_info_system(),
2004 p->msg_ctx,
2005 lp_const_servicename(snum),
2006 "");
2009 result = delete_printer_handle(p, r->in.handle);
2011 return result;
2014 /*******************************************************************
2015 * static function to lookup the version id corresponding to an
2016 * long architecture string
2017 ******************************************************************/
2019 static const struct print_architecture_table_node archi_table[]= {
2021 {"Windows 4.0", SPL_ARCH_WIN40, 0 },
2022 {"Windows NT x86", SPL_ARCH_W32X86, 2 },
2023 {"Windows NT R4000", SPL_ARCH_W32MIPS, 2 },
2024 {"Windows NT Alpha_AXP", SPL_ARCH_W32ALPHA, 2 },
2025 {"Windows NT PowerPC", SPL_ARCH_W32PPC, 2 },
2026 {"Windows IA64", SPL_ARCH_IA64, 3 },
2027 {"Windows x64", SPL_ARCH_X64, 3 },
2028 {NULL, "", -1 }
2031 static const int drv_cversion[] = {SPOOLSS_DRIVER_VERSION_9X,
2032 SPOOLSS_DRIVER_VERSION_NT35,
2033 SPOOLSS_DRIVER_VERSION_NT4,
2034 SPOOLSS_DRIVER_VERSION_200X,
2035 -1};
2037 static int get_version_id(const char *arch)
2039 int i;
2041 for (i=0; archi_table[i].long_archi != NULL; i++)
2043 if (strcmp(arch, archi_table[i].long_archi) == 0)
2044 return (archi_table[i].version);
2047 return -1;
2050 /****************************************************************
2051 _spoolss_DeletePrinterDriver
2052 ****************************************************************/
2054 WERROR _spoolss_DeletePrinterDriver(struct pipes_struct *p,
2055 struct spoolss_DeletePrinterDriver *r)
2058 struct spoolss_DriverInfo8 *info = NULL;
2059 int version;
2060 WERROR status;
2061 struct dcerpc_binding_handle *b;
2062 TALLOC_CTX *tmp_ctx = NULL;
2063 int i;
2064 bool found;
2066 /* if the user is not root, doesn't have SE_PRINT_OPERATOR privilege,
2067 and not a printer admin, then fail */
2069 if ((p->session_info->unix_token->uid != sec_initial_uid()) &&
2070 !security_token_has_privilege(p->session_info->security_token,
2071 SEC_PRIV_PRINT_OPERATOR)) {
2072 return WERR_ACCESS_DENIED;
2075 /* check that we have a valid driver name first */
2077 if ((version = get_version_id(r->in.architecture)) == -1) {
2078 return WERR_INVALID_ENVIRONMENT;
2081 tmp_ctx = talloc_new(p->mem_ctx);
2082 if (!tmp_ctx) {
2083 return WERR_NOMEM;
2086 status = winreg_printer_binding_handle(tmp_ctx,
2087 get_session_info_system(),
2088 p->msg_ctx,
2089 &b);
2090 if (!W_ERROR_IS_OK(status)) {
2091 goto done;
2094 for (found = false, i = 0; drv_cversion[i] >= 0; i++) {
2095 status = winreg_get_driver(tmp_ctx, b,
2096 r->in.architecture, r->in.driver,
2097 drv_cversion[i], &info);
2098 if (!W_ERROR_IS_OK(status)) {
2099 DEBUG(5, ("skipping del of driver with version %d\n",
2100 drv_cversion[i]));
2101 continue;
2103 found = true;
2105 if (printer_driver_in_use(tmp_ctx, b, info)) {
2106 status = WERR_PRINTER_DRIVER_IN_USE;
2107 goto done;
2110 status = winreg_del_driver(tmp_ctx, b, info, drv_cversion[i]);
2111 if (!W_ERROR_IS_OK(status)) {
2112 DEBUG(0, ("failed del of driver with version %d\n",
2113 drv_cversion[i]));
2114 goto done;
2117 if (found == false) {
2118 DEBUG(0, ("driver %s not found for deletion\n", r->in.driver));
2119 status = WERR_UNKNOWN_PRINTER_DRIVER;
2120 } else {
2121 status = WERR_OK;
2124 done:
2125 talloc_free(tmp_ctx);
2127 return status;
2130 static WERROR spoolss_dpd_version(TALLOC_CTX *mem_ctx,
2131 struct pipes_struct *p,
2132 struct spoolss_DeletePrinterDriverEx *r,
2133 struct dcerpc_binding_handle *b,
2134 struct spoolss_DriverInfo8 *info)
2136 WERROR status;
2137 bool delete_files;
2139 if (printer_driver_in_use(mem_ctx, b, info)) {
2140 status = WERR_PRINTER_DRIVER_IN_USE;
2141 goto done;
2145 * we have a couple of cases to consider.
2146 * (1) Are any files in use? If so and DPD_DELETE_ALL_FILES is set,
2147 * then the delete should fail if **any** files overlap with
2148 * other drivers
2149 * (2) If DPD_DELETE_UNUSED_FILES is set, then delete all
2150 * non-overlapping files
2151 * (3) If neither DPD_DELETE_ALL_FILES nor DPD_DELETE_UNUSED_FILES
2152 * are set, then do not delete any files
2153 * Refer to MSDN docs on DeletePrinterDriverEx() for details.
2156 delete_files = r->in.delete_flags
2157 & (DPD_DELETE_ALL_FILES | DPD_DELETE_UNUSED_FILES);
2160 if (delete_files) {
2161 bool in_use = printer_driver_files_in_use(mem_ctx, b, info);
2162 if (in_use && (r->in.delete_flags & DPD_DELETE_ALL_FILES)) {
2163 status = WERR_PRINTER_DRIVER_IN_USE;
2164 goto done;
2167 * printer_driver_files_in_use() has trimmed overlapping files
2168 * from info so they are not removed on DPD_DELETE_UNUSED_FILES
2173 status = winreg_del_driver(mem_ctx, b, info, info->version);
2174 if (!W_ERROR_IS_OK(status)) {
2175 goto done;
2179 * now delete any associated files if delete_files is
2180 * true. Even if this part failes, we return succes
2181 * because the driver doesn not exist any more
2183 if (delete_files) {
2184 delete_driver_files(p->session_info, info);
2187 done:
2188 return status;
2191 /****************************************************************
2192 _spoolss_DeletePrinterDriverEx
2193 ****************************************************************/
2195 WERROR _spoolss_DeletePrinterDriverEx(struct pipes_struct *p,
2196 struct spoolss_DeletePrinterDriverEx *r)
2198 struct spoolss_DriverInfo8 *info = NULL;
2199 WERROR status;
2200 struct dcerpc_binding_handle *b;
2201 TALLOC_CTX *tmp_ctx = NULL;
2202 int i;
2203 bool found;
2205 /* if the user is not root, doesn't have SE_PRINT_OPERATOR privilege,
2206 and not a printer admin, then fail */
2208 if ((p->session_info->unix_token->uid != sec_initial_uid()) &&
2209 !security_token_has_privilege(p->session_info->security_token,
2210 SEC_PRIV_PRINT_OPERATOR)) {
2211 return WERR_ACCESS_DENIED;
2214 /* check that we have a valid driver name first */
2215 if (get_version_id(r->in.architecture) == -1) {
2216 /* this is what NT returns */
2217 return WERR_INVALID_ENVIRONMENT;
2220 tmp_ctx = talloc_new(p->mem_ctx);
2221 if (!tmp_ctx) {
2222 return WERR_NOMEM;
2225 status = winreg_printer_binding_handle(tmp_ctx,
2226 get_session_info_system(),
2227 p->msg_ctx,
2228 &b);
2229 if (!W_ERROR_IS_OK(status)) {
2230 goto done;
2233 for (found = false, i = 0; drv_cversion[i] >= 0; i++) {
2234 if ((r->in.delete_flags & DPD_DELETE_SPECIFIC_VERSION)
2235 && (drv_cversion[i] != r->in.version)) {
2236 continue;
2239 /* check if a driver with this version exists before delete */
2240 status = winreg_get_driver(tmp_ctx, b,
2241 r->in.architecture, r->in.driver,
2242 drv_cversion[i], &info);
2243 if (!W_ERROR_IS_OK(status)) {
2244 DEBUG(5, ("skipping del of driver with version %d\n",
2245 drv_cversion[i]));
2246 continue;
2248 found = true;
2250 status = spoolss_dpd_version(tmp_ctx, p, r, b, info);
2251 if (!W_ERROR_IS_OK(status)) {
2252 DEBUG(0, ("failed to delete driver with version %d\n",
2253 drv_cversion[i]));
2254 goto done;
2257 if (found == false) {
2258 DEBUG(0, ("driver %s not found for deletion\n", r->in.driver));
2259 status = WERR_UNKNOWN_PRINTER_DRIVER;
2260 } else {
2261 status = WERR_OK;
2264 done:
2265 talloc_free(tmp_ctx);
2266 return status;
2270 /********************************************************************
2271 GetPrinterData on a printer server Handle.
2272 ********************************************************************/
2274 static WERROR getprinterdata_printer_server(TALLOC_CTX *mem_ctx,
2275 const char *value,
2276 enum winreg_Type *type,
2277 union spoolss_PrinterData *data)
2279 DEBUG(8,("getprinterdata_printer_server:%s\n", value));
2281 if (!strcasecmp_m(value, "W3SvcInstalled")) {
2282 *type = REG_DWORD;
2283 SIVAL(&data->value, 0, 0x00);
2284 return WERR_OK;
2287 if (!strcasecmp_m(value, "BeepEnabled")) {
2288 *type = REG_DWORD;
2289 SIVAL(&data->value, 0, 0x00);
2290 return WERR_OK;
2293 if (!strcasecmp_m(value, "EventLog")) {
2294 *type = REG_DWORD;
2295 /* formally was 0x1b */
2296 SIVAL(&data->value, 0, 0x00);
2297 return WERR_OK;
2300 if (!strcasecmp_m(value, "NetPopup")) {
2301 *type = REG_DWORD;
2302 SIVAL(&data->value, 0, 0x00);
2303 return WERR_OK;
2306 if (!strcasecmp_m(value, "MajorVersion")) {
2307 *type = REG_DWORD;
2309 /* Windows NT 4.0 seems to not allow uploading of drivers
2310 to a server that reports 0x3 as the MajorVersion.
2311 need to investigate more how Win2k gets around this .
2312 -- jerry */
2314 if (RA_WINNT == get_remote_arch()) {
2315 SIVAL(&data->value, 0, 0x02);
2316 } else {
2317 SIVAL(&data->value, 0, 0x03);
2320 return WERR_OK;
2323 if (!strcasecmp_m(value, "MinorVersion")) {
2324 *type = REG_DWORD;
2325 SIVAL(&data->value, 0, 0x00);
2326 return WERR_OK;
2329 /* REG_BINARY
2330 * uint32_t size = 0x114
2331 * uint32_t major = 5
2332 * uint32_t minor = [0|1]
2333 * uint32_t build = [2195|2600]
2334 * extra unicode string = e.g. "Service Pack 3"
2336 if (!strcasecmp_m(value, "OSVersion")) {
2337 DATA_BLOB blob;
2338 enum ndr_err_code ndr_err;
2339 struct spoolss_OSVersion os;
2341 os.major = 5; /* Windows 2000 == 5.0 */
2342 os.minor = 0;
2343 os.build = 2195; /* build */
2344 os.extra_string = ""; /* leave extra string empty */
2346 ndr_err = ndr_push_struct_blob(&blob, mem_ctx, &os,
2347 (ndr_push_flags_fn_t)ndr_push_spoolss_OSVersion);
2348 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2349 return WERR_GENERAL_FAILURE;
2352 *type = REG_BINARY;
2353 data->binary = blob;
2355 return WERR_OK;
2359 if (!strcasecmp_m(value, "DefaultSpoolDirectory")) {
2360 *type = REG_SZ;
2362 data->string = talloc_strdup(mem_ctx, "C:\\PRINTERS");
2363 W_ERROR_HAVE_NO_MEMORY(data->string);
2365 return WERR_OK;
2368 if (!strcasecmp_m(value, "Architecture")) {
2369 *type = REG_SZ;
2370 data->string = talloc_strdup(mem_ctx,
2371 lp_parm_const_string(GLOBAL_SECTION_SNUM, "spoolss", "architecture", SPOOLSS_ARCHITECTURE_NT_X86));
2372 W_ERROR_HAVE_NO_MEMORY(data->string);
2374 return WERR_OK;
2377 if (!strcasecmp_m(value, "DsPresent")) {
2378 *type = REG_DWORD;
2380 /* only show the publish check box if we are a
2381 member of a AD domain */
2383 if (lp_security() == SEC_ADS) {
2384 SIVAL(&data->value, 0, 0x01);
2385 } else {
2386 SIVAL(&data->value, 0, 0x00);
2388 return WERR_OK;
2391 if (!strcasecmp_m(value, "DNSMachineName")) {
2392 const char *hostname = get_mydnsfullname();
2394 if (!hostname) {
2395 return WERR_BADFILE;
2398 *type = REG_SZ;
2399 data->string = talloc_strdup(mem_ctx, hostname);
2400 W_ERROR_HAVE_NO_MEMORY(data->string);
2402 return WERR_OK;
2405 *type = REG_NONE;
2407 return WERR_INVALID_PARAM;
2410 /****************************************************************
2411 _spoolss_GetPrinterData
2412 ****************************************************************/
2414 WERROR _spoolss_GetPrinterData(struct pipes_struct *p,
2415 struct spoolss_GetPrinterData *r)
2417 struct spoolss_GetPrinterDataEx r2;
2419 r2.in.handle = r->in.handle;
2420 r2.in.key_name = "PrinterDriverData";
2421 r2.in.value_name = r->in.value_name;
2422 r2.in.offered = r->in.offered;
2423 r2.out.type = r->out.type;
2424 r2.out.data = r->out.data;
2425 r2.out.needed = r->out.needed;
2427 return _spoolss_GetPrinterDataEx(p, &r2);
2430 /*********************************************************
2431 Connect to the client machine.
2432 **********************************************************/
2434 static bool spoolss_connect_to_client(struct rpc_pipe_client **pp_pipe,
2435 struct sockaddr_storage *client_ss, const char *remote_machine)
2437 NTSTATUS ret;
2438 struct cli_state *the_cli;
2439 struct sockaddr_storage rm_addr;
2440 char addr[INET6_ADDRSTRLEN];
2442 if ( is_zero_addr(client_ss) ) {
2443 DEBUG(2,("spoolss_connect_to_client: resolving %s\n",
2444 remote_machine));
2445 if ( !resolve_name( remote_machine, &rm_addr, 0x20, false) ) {
2446 DEBUG(2,("spoolss_connect_to_client: Can't resolve address for %s\n", remote_machine));
2447 return false;
2449 print_sockaddr(addr, sizeof(addr), &rm_addr);
2450 } else {
2451 rm_addr = *client_ss;
2452 print_sockaddr(addr, sizeof(addr), &rm_addr);
2453 DEBUG(5,("spoolss_connect_to_client: Using address %s (no name resolution necessary)\n",
2454 addr));
2457 if (ismyaddr((struct sockaddr *)(void *)&rm_addr)) {
2458 DEBUG(0,("spoolss_connect_to_client: Machine %s is one of our addresses. Cannot add to ourselves.\n",
2459 addr));
2460 return false;
2463 /* setup the connection */
2464 ret = cli_full_connection( &the_cli, lp_netbios_name(), remote_machine,
2465 &rm_addr, 0, "IPC$", "IPC",
2466 "", /* username */
2467 "", /* domain */
2468 "", /* password */
2469 0, lp_client_signing());
2471 if ( !NT_STATUS_IS_OK( ret ) ) {
2472 DEBUG(2,("spoolss_connect_to_client: connection to [%s] failed!\n",
2473 remote_machine ));
2474 return false;
2477 if ( smbXcli_conn_protocol(the_cli->conn) != PROTOCOL_NT1 ) {
2478 DEBUG(0,("spoolss_connect_to_client: machine %s didn't negotiate NT protocol.\n", remote_machine));
2479 cli_shutdown(the_cli);
2480 return false;
2484 * Ok - we have an anonymous connection to the IPC$ share.
2485 * Now start the NT Domain stuff :-).
2488 ret = cli_rpc_pipe_open_noauth(the_cli, &ndr_table_spoolss.syntax_id, pp_pipe);
2489 if (!NT_STATUS_IS_OK(ret)) {
2490 DEBUG(2,("spoolss_connect_to_client: unable to open the spoolss pipe on machine %s. Error was : %s.\n",
2491 remote_machine, nt_errstr(ret)));
2492 cli_shutdown(the_cli);
2493 return false;
2496 return true;
2499 /***************************************************************************
2500 Connect to the client.
2501 ****************************************************************************/
2503 static bool srv_spoolss_replyopenprinter(int snum, const char *printer,
2504 uint32_t localprinter,
2505 enum winreg_Type type,
2506 struct policy_handle *handle,
2507 struct notify_back_channel **_chan,
2508 struct sockaddr_storage *client_ss,
2509 struct messaging_context *msg_ctx)
2511 WERROR result;
2512 NTSTATUS status;
2513 struct notify_back_channel *chan;
2515 for (chan = back_channels; chan; chan = chan->next) {
2516 if (memcmp(&chan->client_address, client_ss,
2517 sizeof(struct sockaddr_storage)) == 0) {
2518 break;
2523 * If it's the first connection, contact the client
2524 * and connect to the IPC$ share anonymously
2526 if (!chan) {
2527 fstring unix_printer;
2529 /* the +2 is to strip the leading 2 backslashs */
2530 fstrcpy(unix_printer, printer + 2);
2532 chan = talloc_zero(NULL, struct notify_back_channel);
2533 if (!chan) {
2534 return false;
2536 chan->client_address = *client_ss;
2538 if (!spoolss_connect_to_client(&chan->cli_pipe, client_ss, unix_printer)) {
2539 TALLOC_FREE(chan);
2540 return false;
2543 DLIST_ADD(back_channels, chan);
2545 messaging_register(msg_ctx, NULL, MSG_PRINTER_NOTIFY2,
2546 receive_notify2_message_list);
2549 if (chan->cli_pipe == NULL ||
2550 chan->cli_pipe->binding_handle == NULL) {
2551 DEBUG(0, ("srv_spoolss_replyopenprinter: error - "
2552 "NULL %s for printer %s\n",
2553 chan->cli_pipe == NULL ?
2554 "chan->cli_pipe" : "chan->cli_pipe->binding_handle",
2555 printer));
2556 return false;
2560 * Tell the specific printing tdb we want messages for this printer
2561 * by registering our PID.
2564 if (!print_notify_register_pid(snum)) {
2565 DEBUG(0, ("Failed to register our pid for printer %s\n",
2566 printer));
2569 status = dcerpc_spoolss_ReplyOpenPrinter(chan->cli_pipe->binding_handle,
2570 talloc_tos(),
2571 printer,
2572 localprinter,
2573 type,
2575 NULL,
2576 handle,
2577 &result);
2578 if (!NT_STATUS_IS_OK(status)) {
2579 DEBUG(5, ("dcerpc_spoolss_ReplyOpenPrinter returned [%s]\n", nt_errstr(status)));
2580 result = ntstatus_to_werror(status);
2581 } else if (!W_ERROR_IS_OK(result)) {
2582 DEBUG(5, ("ReplyOpenPrinter returned [%s]\n", win_errstr(result)));
2585 chan->active_connections++;
2586 *_chan = chan;
2588 return (W_ERROR_IS_OK(result));
2591 /****************************************************************
2592 ****************************************************************/
2594 static struct spoolss_NotifyOption *dup_spoolss_NotifyOption(TALLOC_CTX *mem_ctx,
2595 const struct spoolss_NotifyOption *r)
2597 struct spoolss_NotifyOption *option;
2598 uint32_t i,k;
2600 if (!r) {
2601 return NULL;
2604 option = talloc_zero(mem_ctx, struct spoolss_NotifyOption);
2605 if (!option) {
2606 return NULL;
2609 *option = *r;
2611 if (!option->count) {
2612 return option;
2615 option->types = talloc_zero_array(option,
2616 struct spoolss_NotifyOptionType, option->count);
2617 if (!option->types) {
2618 talloc_free(option);
2619 return NULL;
2622 for (i=0; i < option->count; i++) {
2623 option->types[i] = r->types[i];
2625 if (option->types[i].count) {
2626 option->types[i].fields = talloc_zero_array(option,
2627 union spoolss_Field, option->types[i].count);
2628 if (!option->types[i].fields) {
2629 talloc_free(option);
2630 return NULL;
2632 for (k=0; k<option->types[i].count; k++) {
2633 option->types[i].fields[k] =
2634 r->types[i].fields[k];
2639 return option;
2642 /****************************************************************
2643 * _spoolss_RemoteFindFirstPrinterChangeNotifyEx
2645 * before replying OK: status=0 a rpc call is made to the workstation
2646 * asking ReplyOpenPrinter
2648 * in fact ReplyOpenPrinter is the changenotify equivalent on the spoolss pipe
2649 * called from api_spoolss_rffpcnex
2650 ****************************************************************/
2652 WERROR _spoolss_RemoteFindFirstPrinterChangeNotifyEx(struct pipes_struct *p,
2653 struct spoolss_RemoteFindFirstPrinterChangeNotifyEx *r)
2655 int snum = -1;
2656 struct spoolss_NotifyOption *option = r->in.notify_options;
2657 struct sockaddr_storage client_ss;
2658 ssize_t client_len;
2660 /* store the notify value in the printer struct */
2662 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
2664 if (!Printer) {
2665 DEBUG(2,("_spoolss_RemoteFindFirstPrinterChangeNotifyEx: "
2666 "Invalid handle (%s:%u:%u).\n",
2667 OUR_HANDLE(r->in.handle)));
2668 return WERR_BADFID;
2671 Printer->notify.flags = r->in.flags;
2672 Printer->notify.options = r->in.options;
2673 Printer->notify.printerlocal = r->in.printer_local;
2674 Printer->notify.msg_ctx = p->msg_ctx;
2676 TALLOC_FREE(Printer->notify.option);
2677 Printer->notify.option = dup_spoolss_NotifyOption(Printer, option);
2679 fstrcpy(Printer->notify.localmachine, r->in.local_machine);
2681 /* Connect to the client machine and send a ReplyOpenPrinter */
2683 if ( Printer->printer_type == SPLHND_SERVER)
2684 snum = -1;
2685 else if ( (Printer->printer_type == SPLHND_PRINTER) &&
2686 !get_printer_snum(p, r->in.handle, &snum, NULL) )
2687 return WERR_BADFID;
2689 DEBUG(10,("_spoolss_RemoteFindFirstPrinterChangeNotifyEx: "
2690 "remote_address is %s\n",
2691 tsocket_address_string(p->remote_address, p->mem_ctx)));
2693 if (!lp_print_notify_backchannel(snum)) {
2694 DEBUG(10, ("_spoolss_RemoteFindFirstPrinterChangeNotifyEx: "
2695 "backchannel disabled\n"));
2696 return WERR_SERVER_UNAVAILABLE;
2699 client_len = tsocket_address_bsd_sockaddr(p->remote_address,
2700 (struct sockaddr *) &client_ss,
2701 sizeof(struct sockaddr_storage));
2702 if (client_len < 0) {
2703 return WERR_NOMEM;
2706 if(!srv_spoolss_replyopenprinter(snum, Printer->notify.localmachine,
2707 Printer->notify.printerlocal, REG_SZ,
2708 &Printer->notify.cli_hnd,
2709 &Printer->notify.cli_chan,
2710 &client_ss, p->msg_ctx)) {
2711 return WERR_SERVER_UNAVAILABLE;
2714 return WERR_OK;
2717 /*******************************************************************
2718 * fill a notify_info_data with the servername
2719 ********************************************************************/
2721 static void spoolss_notify_server_name(struct messaging_context *msg_ctx,
2722 int snum,
2723 struct spoolss_Notify *data,
2724 print_queue_struct *queue,
2725 struct spoolss_PrinterInfo2 *pinfo2,
2726 TALLOC_CTX *mem_ctx)
2728 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, pinfo2->servername);
2731 /*******************************************************************
2732 * fill a notify_info_data with the printername (not including the servername).
2733 ********************************************************************/
2735 static void spoolss_notify_printer_name(struct messaging_context *msg_ctx,
2736 int snum,
2737 struct spoolss_Notify *data,
2738 print_queue_struct *queue,
2739 struct spoolss_PrinterInfo2 *pinfo2,
2740 TALLOC_CTX *mem_ctx)
2742 /* the notify name should not contain the \\server\ part */
2743 const char *p = strrchr(pinfo2->printername, '\\');
2745 if (!p) {
2746 p = pinfo2->printername;
2747 } else {
2748 p++;
2751 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, p);
2754 /*******************************************************************
2755 * fill a notify_info_data with the servicename
2756 ********************************************************************/
2758 static void spoolss_notify_share_name(struct messaging_context *msg_ctx,
2759 int snum,
2760 struct spoolss_Notify *data,
2761 print_queue_struct *queue,
2762 struct spoolss_PrinterInfo2 *pinfo2,
2763 TALLOC_CTX *mem_ctx)
2765 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, lp_servicename(talloc_tos(), snum));
2768 /*******************************************************************
2769 * fill a notify_info_data with the port name
2770 ********************************************************************/
2772 static void spoolss_notify_port_name(struct messaging_context *msg_ctx,
2773 int snum,
2774 struct spoolss_Notify *data,
2775 print_queue_struct *queue,
2776 struct spoolss_PrinterInfo2 *pinfo2,
2777 TALLOC_CTX *mem_ctx)
2779 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, pinfo2->portname);
2782 /*******************************************************************
2783 * fill a notify_info_data with the printername
2784 * but it doesn't exist, have to see what to do
2785 ********************************************************************/
2787 static void spoolss_notify_driver_name(struct messaging_context *msg_ctx,
2788 int snum,
2789 struct spoolss_Notify *data,
2790 print_queue_struct *queue,
2791 struct spoolss_PrinterInfo2 *pinfo2,
2792 TALLOC_CTX *mem_ctx)
2794 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, pinfo2->drivername);
2797 /*******************************************************************
2798 * fill a notify_info_data with the comment
2799 ********************************************************************/
2801 static void spoolss_notify_comment(struct messaging_context *msg_ctx,
2802 int snum,
2803 struct spoolss_Notify *data,
2804 print_queue_struct *queue,
2805 struct spoolss_PrinterInfo2 *pinfo2,
2806 TALLOC_CTX *mem_ctx)
2808 const char *p;
2810 if (*pinfo2->comment == '\0') {
2811 p = lp_comment(talloc_tos(), snum);
2812 } else {
2813 p = pinfo2->comment;
2816 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, p);
2819 /*******************************************************************
2820 * fill a notify_info_data with the comment
2821 * location = "Room 1, floor 2, building 3"
2822 ********************************************************************/
2824 static void spoolss_notify_location(struct messaging_context *msg_ctx,
2825 int snum,
2826 struct spoolss_Notify *data,
2827 print_queue_struct *queue,
2828 struct spoolss_PrinterInfo2 *pinfo2,
2829 TALLOC_CTX *mem_ctx)
2831 const char *loc = pinfo2->location;
2832 NTSTATUS status;
2834 status = printer_list_get_printer(mem_ctx,
2835 pinfo2->sharename,
2836 NULL,
2837 &loc,
2838 NULL);
2839 if (NT_STATUS_IS_OK(status)) {
2840 if (loc == NULL) {
2841 loc = pinfo2->location;
2845 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, loc);
2848 /*******************************************************************
2849 * fill a notify_info_data with the device mode
2850 * jfm:xxxx don't to it for know but that's a real problem !!!
2851 ********************************************************************/
2853 static void spoolss_notify_devmode(struct messaging_context *msg_ctx,
2854 int snum,
2855 struct spoolss_Notify *data,
2856 print_queue_struct *queue,
2857 struct spoolss_PrinterInfo2 *pinfo2,
2858 TALLOC_CTX *mem_ctx)
2860 /* for a dummy implementation we have to zero the fields */
2861 SETUP_SPOOLSS_NOTIFY_DATA_DEVMODE(data, NULL);
2864 /*******************************************************************
2865 * fill a notify_info_data with the separator file name
2866 ********************************************************************/
2868 static void spoolss_notify_sepfile(struct messaging_context *msg_ctx,
2869 int snum,
2870 struct spoolss_Notify *data,
2871 print_queue_struct *queue,
2872 struct spoolss_PrinterInfo2 *pinfo2,
2873 TALLOC_CTX *mem_ctx)
2875 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, pinfo2->sepfile);
2878 /*******************************************************************
2879 * fill a notify_info_data with the print processor
2880 * jfm:xxxx return always winprint to indicate we don't do anything to it
2881 ********************************************************************/
2883 static void spoolss_notify_print_processor(struct messaging_context *msg_ctx,
2884 int snum,
2885 struct spoolss_Notify *data,
2886 print_queue_struct *queue,
2887 struct spoolss_PrinterInfo2 *pinfo2,
2888 TALLOC_CTX *mem_ctx)
2890 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, pinfo2->printprocessor);
2893 /*******************************************************************
2894 * fill a notify_info_data with the print processor options
2895 * jfm:xxxx send an empty string
2896 ********************************************************************/
2898 static void spoolss_notify_parameters(struct messaging_context *msg_ctx,
2899 int snum,
2900 struct spoolss_Notify *data,
2901 print_queue_struct *queue,
2902 struct spoolss_PrinterInfo2 *pinfo2,
2903 TALLOC_CTX *mem_ctx)
2905 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, pinfo2->parameters);
2908 /*******************************************************************
2909 * fill a notify_info_data with the data type
2910 * jfm:xxxx always send RAW as data type
2911 ********************************************************************/
2913 static void spoolss_notify_datatype(struct messaging_context *msg_ctx,
2914 int snum,
2915 struct spoolss_Notify *data,
2916 print_queue_struct *queue,
2917 struct spoolss_PrinterInfo2 *pinfo2,
2918 TALLOC_CTX *mem_ctx)
2920 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, pinfo2->datatype);
2923 /*******************************************************************
2924 * fill a notify_info_data with the security descriptor
2925 * jfm:xxxx send an null pointer to say no security desc
2926 * have to implement security before !
2927 ********************************************************************/
2929 static void spoolss_notify_security_desc(struct messaging_context *msg_ctx,
2930 int snum,
2931 struct spoolss_Notify *data,
2932 print_queue_struct *queue,
2933 struct spoolss_PrinterInfo2 *pinfo2,
2934 TALLOC_CTX *mem_ctx)
2936 SETUP_SPOOLSS_NOTIFY_DATA_SECDESC(data, pinfo2->secdesc);
2939 /*******************************************************************
2940 * fill a notify_info_data with the attributes
2941 * jfm:xxxx a samba printer is always shared
2942 ********************************************************************/
2944 static void spoolss_notify_attributes(struct messaging_context *msg_ctx,
2945 int snum,
2946 struct spoolss_Notify *data,
2947 print_queue_struct *queue,
2948 struct spoolss_PrinterInfo2 *pinfo2,
2949 TALLOC_CTX *mem_ctx)
2951 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, pinfo2->attributes);
2954 /*******************************************************************
2955 * fill a notify_info_data with the priority
2956 ********************************************************************/
2958 static void spoolss_notify_priority(struct messaging_context *msg_ctx,
2959 int snum,
2960 struct spoolss_Notify *data,
2961 print_queue_struct *queue,
2962 struct spoolss_PrinterInfo2 *pinfo2,
2963 TALLOC_CTX *mem_ctx)
2965 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, pinfo2->priority);
2968 /*******************************************************************
2969 * fill a notify_info_data with the default priority
2970 ********************************************************************/
2972 static void spoolss_notify_default_priority(struct messaging_context *msg_ctx,
2973 int snum,
2974 struct spoolss_Notify *data,
2975 print_queue_struct *queue,
2976 struct spoolss_PrinterInfo2 *pinfo2,
2977 TALLOC_CTX *mem_ctx)
2979 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, pinfo2->defaultpriority);
2982 /*******************************************************************
2983 * fill a notify_info_data with the start time
2984 ********************************************************************/
2986 static void spoolss_notify_start_time(struct messaging_context *msg_ctx,
2987 int snum,
2988 struct spoolss_Notify *data,
2989 print_queue_struct *queue,
2990 struct spoolss_PrinterInfo2 *pinfo2,
2991 TALLOC_CTX *mem_ctx)
2993 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, pinfo2->starttime);
2996 /*******************************************************************
2997 * fill a notify_info_data with the until time
2998 ********************************************************************/
3000 static void spoolss_notify_until_time(struct messaging_context *msg_ctx,
3001 int snum,
3002 struct spoolss_Notify *data,
3003 print_queue_struct *queue,
3004 struct spoolss_PrinterInfo2 *pinfo2,
3005 TALLOC_CTX *mem_ctx)
3007 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, pinfo2->untiltime);
3010 /*******************************************************************
3011 * fill a notify_info_data with the status
3012 ********************************************************************/
3014 static void spoolss_notify_status(struct messaging_context *msg_ctx,
3015 int snum,
3016 struct spoolss_Notify *data,
3017 print_queue_struct *queue,
3018 struct spoolss_PrinterInfo2 *pinfo2,
3019 TALLOC_CTX *mem_ctx)
3021 print_status_struct status;
3023 print_queue_length(msg_ctx, snum, &status);
3024 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, status.status);
3027 /*******************************************************************
3028 * fill a notify_info_data with the number of jobs queued
3029 ********************************************************************/
3031 static void spoolss_notify_cjobs(struct messaging_context *msg_ctx,
3032 int snum,
3033 struct spoolss_Notify *data,
3034 print_queue_struct *queue,
3035 struct spoolss_PrinterInfo2 *pinfo2,
3036 TALLOC_CTX *mem_ctx)
3038 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(
3039 data, print_queue_length(msg_ctx, snum, NULL));
3042 /*******************************************************************
3043 * fill a notify_info_data with the average ppm
3044 ********************************************************************/
3046 static void spoolss_notify_average_ppm(struct messaging_context *msg_ctx,
3047 int snum,
3048 struct spoolss_Notify *data,
3049 print_queue_struct *queue,
3050 struct spoolss_PrinterInfo2 *pinfo2,
3051 TALLOC_CTX *mem_ctx)
3053 /* always respond 8 pages per minutes */
3054 /* a little hard ! */
3055 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, pinfo2->averageppm);
3058 /*******************************************************************
3059 * fill a notify_info_data with username
3060 ********************************************************************/
3062 static void spoolss_notify_username(struct messaging_context *msg_ctx,
3063 int snum,
3064 struct spoolss_Notify *data,
3065 print_queue_struct *queue,
3066 struct spoolss_PrinterInfo2 *pinfo2,
3067 TALLOC_CTX *mem_ctx)
3069 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, queue->fs_user);
3072 /*******************************************************************
3073 * fill a notify_info_data with job status
3074 ********************************************************************/
3076 static void spoolss_notify_job_status(struct messaging_context *msg_ctx,
3077 int snum,
3078 struct spoolss_Notify *data,
3079 print_queue_struct *queue,
3080 struct spoolss_PrinterInfo2 *pinfo2,
3081 TALLOC_CTX *mem_ctx)
3083 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, nt_printj_status(queue->status));
3086 /*******************************************************************
3087 * fill a notify_info_data with job name
3088 ********************************************************************/
3090 static void spoolss_notify_job_name(struct messaging_context *msg_ctx,
3091 int snum,
3092 struct spoolss_Notify *data,
3093 print_queue_struct *queue,
3094 struct spoolss_PrinterInfo2 *pinfo2,
3095 TALLOC_CTX *mem_ctx)
3097 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, queue->fs_file);
3100 /*******************************************************************
3101 * fill a notify_info_data with job status
3102 ********************************************************************/
3104 static void spoolss_notify_job_status_string(struct messaging_context *msg_ctx,
3105 int snum,
3106 struct spoolss_Notify *data,
3107 print_queue_struct *queue,
3108 struct spoolss_PrinterInfo2 *pinfo2,
3109 TALLOC_CTX *mem_ctx)
3112 * Now we're returning job status codes we just return a "" here. JRA.
3115 const char *p = "";
3117 #if 0 /* NO LONGER NEEDED - JRA. 02/22/2001 */
3118 p = "unknown";
3120 switch (queue->status) {
3121 case LPQ_QUEUED:
3122 p = "Queued";
3123 break;
3124 case LPQ_PAUSED:
3125 p = ""; /* NT provides the paused string */
3126 break;
3127 case LPQ_SPOOLING:
3128 p = "Spooling";
3129 break;
3130 case LPQ_PRINTING:
3131 p = "Printing";
3132 break;
3134 #endif /* NO LONGER NEEDED. */
3136 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, p);
3139 /*******************************************************************
3140 * fill a notify_info_data with job time
3141 ********************************************************************/
3143 static void spoolss_notify_job_time(struct messaging_context *msg_ctx,
3144 int snum,
3145 struct spoolss_Notify *data,
3146 print_queue_struct *queue,
3147 struct spoolss_PrinterInfo2 *pinfo2,
3148 TALLOC_CTX *mem_ctx)
3150 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, 0);
3153 /*******************************************************************
3154 * fill a notify_info_data with job size
3155 ********************************************************************/
3157 static void spoolss_notify_job_size(struct messaging_context *msg_ctx,
3158 int snum,
3159 struct spoolss_Notify *data,
3160 print_queue_struct *queue,
3161 struct spoolss_PrinterInfo2 *pinfo2,
3162 TALLOC_CTX *mem_ctx)
3164 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, queue->size);
3167 /*******************************************************************
3168 * fill a notify_info_data with page info
3169 ********************************************************************/
3170 static void spoolss_notify_total_pages(struct messaging_context *msg_ctx,
3171 int snum,
3172 struct spoolss_Notify *data,
3173 print_queue_struct *queue,
3174 struct spoolss_PrinterInfo2 *pinfo2,
3175 TALLOC_CTX *mem_ctx)
3177 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, queue->page_count);
3180 /*******************************************************************
3181 * fill a notify_info_data with pages printed info.
3182 ********************************************************************/
3183 static void spoolss_notify_pages_printed(struct messaging_context *msg_ctx,
3184 int snum,
3185 struct spoolss_Notify *data,
3186 print_queue_struct *queue,
3187 struct spoolss_PrinterInfo2 *pinfo2,
3188 TALLOC_CTX *mem_ctx)
3190 /* Add code when back-end tracks this */
3191 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, 0);
3194 /*******************************************************************
3195 Fill a notify_info_data with job position.
3196 ********************************************************************/
3198 static void spoolss_notify_job_position(struct messaging_context *msg_ctx,
3199 int snum,
3200 struct spoolss_Notify *data,
3201 print_queue_struct *queue,
3202 struct spoolss_PrinterInfo2 *pinfo2,
3203 TALLOC_CTX *mem_ctx)
3205 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, queue->sysjob);
3208 /*******************************************************************
3209 Fill a notify_info_data with submitted time.
3210 ********************************************************************/
3212 static void spoolss_notify_submitted_time(struct messaging_context *msg_ctx,
3213 int snum,
3214 struct spoolss_Notify *data,
3215 print_queue_struct *queue,
3216 struct spoolss_PrinterInfo2 *pinfo2,
3217 TALLOC_CTX *mem_ctx)
3219 data->data.string.string = NULL;
3220 data->data.string.size = 0;
3222 init_systemtime_buffer(mem_ctx, gmtime(&queue->time),
3223 &data->data.string.string,
3224 &data->data.string.size);
3228 struct s_notify_info_data_table
3230 enum spoolss_NotifyType type;
3231 uint16_t field;
3232 const char *name;
3233 enum spoolss_NotifyTable variable_type;
3234 void (*fn) (struct messaging_context *msg_ctx,
3235 int snum, struct spoolss_Notify *data,
3236 print_queue_struct *queue,
3237 struct spoolss_PrinterInfo2 *pinfo2,
3238 TALLOC_CTX *mem_ctx);
3241 /* A table describing the various print notification constants and
3242 whether the notification data is a pointer to a variable sized
3243 buffer, a one value uint32_t or a two value uint32_t. */
3245 static const struct s_notify_info_data_table notify_info_data_table[] =
3247 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_SERVER_NAME, "PRINTER_NOTIFY_FIELD_SERVER_NAME", NOTIFY_TABLE_STRING, spoolss_notify_server_name },
3248 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_PRINTER_NAME, "PRINTER_NOTIFY_FIELD_PRINTER_NAME", NOTIFY_TABLE_STRING, spoolss_notify_printer_name },
3249 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_SHARE_NAME, "PRINTER_NOTIFY_FIELD_SHARE_NAME", NOTIFY_TABLE_STRING, spoolss_notify_share_name },
3250 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_PORT_NAME, "PRINTER_NOTIFY_FIELD_PORT_NAME", NOTIFY_TABLE_STRING, spoolss_notify_port_name },
3251 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_DRIVER_NAME, "PRINTER_NOTIFY_FIELD_DRIVER_NAME", NOTIFY_TABLE_STRING, spoolss_notify_driver_name },
3252 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_COMMENT, "PRINTER_NOTIFY_FIELD_COMMENT", NOTIFY_TABLE_STRING, spoolss_notify_comment },
3253 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_LOCATION, "PRINTER_NOTIFY_FIELD_LOCATION", NOTIFY_TABLE_STRING, spoolss_notify_location },
3254 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_DEVMODE, "PRINTER_NOTIFY_FIELD_DEVMODE", NOTIFY_TABLE_DEVMODE, spoolss_notify_devmode },
3255 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_SEPFILE, "PRINTER_NOTIFY_FIELD_SEPFILE", NOTIFY_TABLE_STRING, spoolss_notify_sepfile },
3256 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_PRINT_PROCESSOR, "PRINTER_NOTIFY_FIELD_PRINT_PROCESSOR", NOTIFY_TABLE_STRING, spoolss_notify_print_processor },
3257 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_PARAMETERS, "PRINTER_NOTIFY_FIELD_PARAMETERS", NOTIFY_TABLE_STRING, spoolss_notify_parameters },
3258 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_DATATYPE, "PRINTER_NOTIFY_FIELD_DATATYPE", NOTIFY_TABLE_STRING, spoolss_notify_datatype },
3259 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_SECURITY_DESCRIPTOR, "PRINTER_NOTIFY_FIELD_SECURITY_DESCRIPTOR", NOTIFY_TABLE_SECURITYDESCRIPTOR, spoolss_notify_security_desc },
3260 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_ATTRIBUTES, "PRINTER_NOTIFY_FIELD_ATTRIBUTES", NOTIFY_TABLE_DWORD, spoolss_notify_attributes },
3261 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_PRIORITY, "PRINTER_NOTIFY_FIELD_PRIORITY", NOTIFY_TABLE_DWORD, spoolss_notify_priority },
3262 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_DEFAULT_PRIORITY, "PRINTER_NOTIFY_FIELD_DEFAULT_PRIORITY", NOTIFY_TABLE_DWORD, spoolss_notify_default_priority },
3263 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_START_TIME, "PRINTER_NOTIFY_FIELD_START_TIME", NOTIFY_TABLE_DWORD, spoolss_notify_start_time },
3264 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_UNTIL_TIME, "PRINTER_NOTIFY_FIELD_UNTIL_TIME", NOTIFY_TABLE_DWORD, spoolss_notify_until_time },
3265 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_STATUS, "PRINTER_NOTIFY_FIELD_STATUS", NOTIFY_TABLE_DWORD, spoolss_notify_status },
3266 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_STATUS_STRING, "PRINTER_NOTIFY_FIELD_STATUS_STRING", NOTIFY_TABLE_STRING, NULL },
3267 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_CJOBS, "PRINTER_NOTIFY_FIELD_CJOBS", NOTIFY_TABLE_DWORD, spoolss_notify_cjobs },
3268 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_AVERAGE_PPM, "PRINTER_NOTIFY_FIELD_AVERAGE_PPM", NOTIFY_TABLE_DWORD, spoolss_notify_average_ppm },
3269 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_TOTAL_PAGES, "PRINTER_NOTIFY_FIELD_TOTAL_PAGES", NOTIFY_TABLE_DWORD, NULL },
3270 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_PAGES_PRINTED, "PRINTER_NOTIFY_FIELD_PAGES_PRINTED", NOTIFY_TABLE_DWORD, NULL },
3271 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_TOTAL_BYTES, "PRINTER_NOTIFY_FIELD_TOTAL_BYTES", NOTIFY_TABLE_DWORD, NULL },
3272 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_BYTES_PRINTED, "PRINTER_NOTIFY_FIELD_BYTES_PRINTED", NOTIFY_TABLE_DWORD, NULL },
3273 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_PRINTER_NAME, "JOB_NOTIFY_FIELD_PRINTER_NAME", NOTIFY_TABLE_STRING, spoolss_notify_printer_name },
3274 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_MACHINE_NAME, "JOB_NOTIFY_FIELD_MACHINE_NAME", NOTIFY_TABLE_STRING, spoolss_notify_server_name },
3275 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_PORT_NAME, "JOB_NOTIFY_FIELD_PORT_NAME", NOTIFY_TABLE_STRING, spoolss_notify_port_name },
3276 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_USER_NAME, "JOB_NOTIFY_FIELD_USER_NAME", NOTIFY_TABLE_STRING, spoolss_notify_username },
3277 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_NOTIFY_NAME, "JOB_NOTIFY_FIELD_NOTIFY_NAME", NOTIFY_TABLE_STRING, spoolss_notify_username },
3278 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_DATATYPE, "JOB_NOTIFY_FIELD_DATATYPE", NOTIFY_TABLE_STRING, spoolss_notify_datatype },
3279 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_PRINT_PROCESSOR, "JOB_NOTIFY_FIELD_PRINT_PROCESSOR", NOTIFY_TABLE_STRING, spoolss_notify_print_processor },
3280 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_PARAMETERS, "JOB_NOTIFY_FIELD_PARAMETERS", NOTIFY_TABLE_STRING, spoolss_notify_parameters },
3281 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_DRIVER_NAME, "JOB_NOTIFY_FIELD_DRIVER_NAME", NOTIFY_TABLE_STRING, spoolss_notify_driver_name },
3282 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_DEVMODE, "JOB_NOTIFY_FIELD_DEVMODE", NOTIFY_TABLE_DEVMODE, spoolss_notify_devmode },
3283 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_STATUS, "JOB_NOTIFY_FIELD_STATUS", NOTIFY_TABLE_DWORD, spoolss_notify_job_status },
3284 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_STATUS_STRING, "JOB_NOTIFY_FIELD_STATUS_STRING", NOTIFY_TABLE_STRING, spoolss_notify_job_status_string },
3285 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_SECURITY_DESCRIPTOR, "JOB_NOTIFY_FIELD_SECURITY_DESCRIPTOR", NOTIFY_TABLE_SECURITYDESCRIPTOR, NULL },
3286 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_DOCUMENT, "JOB_NOTIFY_FIELD_DOCUMENT", NOTIFY_TABLE_STRING, spoolss_notify_job_name },
3287 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_PRIORITY, "JOB_NOTIFY_FIELD_PRIORITY", NOTIFY_TABLE_DWORD, spoolss_notify_priority },
3288 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_POSITION, "JOB_NOTIFY_FIELD_POSITION", NOTIFY_TABLE_DWORD, spoolss_notify_job_position },
3289 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_SUBMITTED, "JOB_NOTIFY_FIELD_SUBMITTED", NOTIFY_TABLE_TIME, spoolss_notify_submitted_time },
3290 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_START_TIME, "JOB_NOTIFY_FIELD_START_TIME", NOTIFY_TABLE_DWORD, spoolss_notify_start_time },
3291 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_UNTIL_TIME, "JOB_NOTIFY_FIELD_UNTIL_TIME", NOTIFY_TABLE_DWORD, spoolss_notify_until_time },
3292 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_TIME, "JOB_NOTIFY_FIELD_TIME", NOTIFY_TABLE_DWORD, spoolss_notify_job_time },
3293 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_TOTAL_PAGES, "JOB_NOTIFY_FIELD_TOTAL_PAGES", NOTIFY_TABLE_DWORD, spoolss_notify_total_pages },
3294 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_PAGES_PRINTED, "JOB_NOTIFY_FIELD_PAGES_PRINTED", NOTIFY_TABLE_DWORD, spoolss_notify_pages_printed },
3295 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_TOTAL_BYTES, "JOB_NOTIFY_FIELD_TOTAL_BYTES", NOTIFY_TABLE_DWORD, spoolss_notify_job_size },
3298 /*******************************************************************
3299 Return the variable_type of info_data structure.
3300 ********************************************************************/
3302 static enum spoolss_NotifyTable variable_type_of_notify_info_data(enum spoolss_NotifyType type,
3303 uint16_t field)
3305 int i=0;
3307 for (i = 0; i < ARRAY_SIZE(notify_info_data_table); i++) {
3308 if ( (notify_info_data_table[i].type == type) &&
3309 (notify_info_data_table[i].field == field) ) {
3310 return notify_info_data_table[i].variable_type;
3314 DEBUG(5, ("invalid notify data type %d/%d\n", type, field));
3316 return (enum spoolss_NotifyTable) 0;
3319 /****************************************************************************
3320 ****************************************************************************/
3322 static bool search_notify(enum spoolss_NotifyType type,
3323 uint16_t field,
3324 int *value)
3326 int i;
3328 for (i = 0; i < ARRAY_SIZE(notify_info_data_table); i++) {
3329 if (notify_info_data_table[i].type == type &&
3330 notify_info_data_table[i].field == field &&
3331 notify_info_data_table[i].fn != NULL) {
3332 *value = i;
3333 return true;
3337 return false;
3340 /****************************************************************************
3341 ****************************************************************************/
3343 static void construct_info_data(struct spoolss_Notify *info_data,
3344 enum spoolss_NotifyType type,
3345 uint16_t field, int id)
3347 info_data->type = type;
3348 info_data->field.field = field;
3349 info_data->variable_type = variable_type_of_notify_info_data(type, field);
3350 info_data->job_id = id;
3353 /*******************************************************************
3355 * fill a notify_info struct with info asked
3357 ********************************************************************/
3359 static bool construct_notify_printer_info(struct messaging_context *msg_ctx,
3360 struct printer_handle *print_hnd,
3361 struct spoolss_NotifyInfo *info,
3362 struct spoolss_PrinterInfo2 *pinfo2,
3363 int snum,
3364 const struct spoolss_NotifyOptionType *option_type,
3365 uint32_t id,
3366 TALLOC_CTX *mem_ctx)
3368 int field_num,j;
3369 enum spoolss_NotifyType type;
3370 uint16_t field;
3372 struct spoolss_Notify *current_data;
3374 type = option_type->type;
3376 DEBUG(4,("construct_notify_printer_info: Notify type: [%s], number of notify info: [%d] on printer: [%s]\n",
3377 (type == PRINTER_NOTIFY_TYPE ? "PRINTER_NOTIFY_TYPE" : "JOB_NOTIFY_TYPE"),
3378 option_type->count, lp_servicename(talloc_tos(), snum)));
3380 for(field_num=0; field_num < option_type->count; field_num++) {
3381 field = option_type->fields[field_num].field;
3383 DEBUG(4,("construct_notify_printer_info: notify [%d]: type [%x], field [%x]\n", field_num, type, field));
3385 if (!search_notify(type, field, &j) )
3386 continue;
3388 info->notifies = talloc_realloc(info, info->notifies,
3389 struct spoolss_Notify,
3390 info->count + 1);
3391 if (info->notifies == NULL) {
3392 DEBUG(2,("construct_notify_printer_info: failed to enlarge buffer info->data!\n"));
3393 return false;
3396 current_data = &info->notifies[info->count];
3398 construct_info_data(current_data, type, field, id);
3400 DEBUG(10, ("construct_notify_printer_info: "
3401 "calling [%s] snum=%d printername=[%s])\n",
3402 notify_info_data_table[j].name, snum,
3403 pinfo2->printername));
3405 notify_info_data_table[j].fn(msg_ctx, snum, current_data,
3406 NULL, pinfo2, mem_ctx);
3408 info->count++;
3411 return true;
3414 /*******************************************************************
3416 * fill a notify_info struct with info asked
3418 ********************************************************************/
3420 static bool construct_notify_jobs_info(struct messaging_context *msg_ctx,
3421 print_queue_struct *queue,
3422 struct spoolss_NotifyInfo *info,
3423 struct spoolss_PrinterInfo2 *pinfo2,
3424 int snum,
3425 const struct spoolss_NotifyOptionType *option_type,
3426 uint32_t id,
3427 TALLOC_CTX *mem_ctx)
3429 int field_num,j;
3430 enum spoolss_NotifyType type;
3431 uint16_t field;
3432 struct spoolss_Notify *current_data;
3434 DEBUG(4,("construct_notify_jobs_info\n"));
3436 type = option_type->type;
3438 DEBUGADD(4,("Notify type: [%s], number of notify info: [%d]\n",
3439 (type == PRINTER_NOTIFY_TYPE ? "PRINTER_NOTIFY_TYPE" : "JOB_NOTIFY_TYPE"),
3440 option_type->count));
3442 for(field_num=0; field_num<option_type->count; field_num++) {
3443 field = option_type->fields[field_num].field;
3445 if (!search_notify(type, field, &j) )
3446 continue;
3448 info->notifies = talloc_realloc(info, info->notifies,
3449 struct spoolss_Notify,
3450 info->count + 1);
3451 if (info->notifies == NULL) {
3452 DEBUG(2,("construct_notify_jobs_info: failed to enlarg buffer info->data!\n"));
3453 return false;
3456 current_data=&(info->notifies[info->count]);
3458 construct_info_data(current_data, type, field, id);
3459 notify_info_data_table[j].fn(msg_ctx, snum, current_data,
3460 queue, pinfo2, mem_ctx);
3461 info->count++;
3464 return true;
3468 * JFM: The enumeration is not that simple, it's even non obvious.
3470 * let's take an example: I want to monitor the PRINTER SERVER for
3471 * the printer's name and the number of jobs currently queued.
3472 * So in the NOTIFY_OPTION, I have one NOTIFY_OPTION_TYPE structure.
3473 * Its type is PRINTER_NOTIFY_TYPE and it has 2 fields NAME and CJOBS.
3475 * I have 3 printers on the back of my server.
3477 * Now the response is a NOTIFY_INFO structure, with 6 NOTIFY_INFO_DATA
3478 * structures.
3479 * Number Data Id
3480 * 1 printer 1 name 1
3481 * 2 printer 1 cjob 1
3482 * 3 printer 2 name 2
3483 * 4 printer 2 cjob 2
3484 * 5 printer 3 name 3
3485 * 6 printer 3 name 3
3487 * that's the print server case, the printer case is even worse.
3490 /*******************************************************************
3492 * enumerate all printers on the printserver
3493 * fill a notify_info struct with info asked
3495 ********************************************************************/
3497 static WERROR printserver_notify_info(struct pipes_struct *p,
3498 struct policy_handle *hnd,
3499 struct spoolss_NotifyInfo *info,
3500 TALLOC_CTX *mem_ctx)
3502 int snum;
3503 struct printer_handle *Printer = find_printer_index_by_hnd(p, hnd);
3504 int n_services=lp_numservices();
3505 int i;
3506 struct spoolss_NotifyOption *option;
3507 struct spoolss_NotifyOptionType option_type;
3508 struct spoolss_PrinterInfo2 *pinfo2 = NULL;
3509 WERROR result;
3511 DEBUG(4,("printserver_notify_info\n"));
3513 if (!Printer)
3514 return WERR_BADFID;
3516 option = Printer->notify.option;
3518 info->version = 2;
3519 info->notifies = NULL;
3520 info->count = 0;
3522 /* a bug in xp sp2 rc2 causes it to send a fnpcn request without
3523 sending a ffpcn() request first */
3525 if ( !option )
3526 return WERR_BADFID;
3528 for (i=0; i<option->count; i++) {
3529 option_type = option->types[i];
3531 if (option_type.type != PRINTER_NOTIFY_TYPE)
3532 continue;
3534 for (snum = 0; snum < n_services; snum++) {
3535 if (!lp_browseable(snum) ||
3536 !lp_snum_ok(snum) ||
3537 !lp_print_ok(snum)) {
3538 continue; /* skip */
3541 /* Maybe we should use the SYSTEM session_info here... */
3542 result = winreg_get_printer_internal(mem_ctx,
3543 get_session_info_system(),
3544 p->msg_ctx,
3545 lp_servicename(talloc_tos(), snum),
3546 &pinfo2);
3547 if (!W_ERROR_IS_OK(result)) {
3548 DEBUG(4, ("printserver_notify_info: "
3549 "Failed to get printer [%s]\n",
3550 lp_servicename(talloc_tos(), snum)));
3551 continue;
3555 construct_notify_printer_info(p->msg_ctx,
3556 Printer, info,
3557 pinfo2, snum,
3558 &option_type, snum,
3559 mem_ctx);
3561 TALLOC_FREE(pinfo2);
3565 #if 0
3567 * Debugging information, don't delete.
3570 DEBUG(1,("dumping the NOTIFY_INFO\n"));
3571 DEBUGADD(1,("info->version:[%d], info->flags:[%d], info->count:[%d]\n", info->version, info->flags, info->count));
3572 DEBUGADD(1,("num\ttype\tfield\tres\tid\tsize\tenc_type\n"));
3574 for (i=0; i<info->count; i++) {
3575 DEBUGADD(1,("[%d]\t[%d]\t[%d]\t[%d]\t[%d]\t[%d]\t[%d]\n",
3576 i, info->data[i].type, info->data[i].field, info->data[i].reserved,
3577 info->data[i].id, info->data[i].size, info->data[i].enc_type));
3579 #endif
3581 return WERR_OK;
3584 /*******************************************************************
3586 * fill a notify_info struct with info asked
3588 ********************************************************************/
3590 static WERROR printer_notify_info(struct pipes_struct *p,
3591 struct policy_handle *hnd,
3592 struct spoolss_NotifyInfo *info,
3593 TALLOC_CTX *mem_ctx)
3595 int snum;
3596 struct printer_handle *Printer = find_printer_index_by_hnd(p, hnd);
3597 int i;
3598 uint32_t id;
3599 struct spoolss_NotifyOption *option;
3600 struct spoolss_NotifyOptionType option_type;
3601 int count,j;
3602 print_queue_struct *queue=NULL;
3603 print_status_struct status;
3604 struct spoolss_PrinterInfo2 *pinfo2 = NULL;
3605 WERROR result;
3607 DEBUG(4,("printer_notify_info\n"));
3609 if (!Printer)
3610 return WERR_BADFID;
3612 option = Printer->notify.option;
3613 id = 0x0;
3615 info->version = 2;
3616 info->notifies = NULL;
3617 info->count = 0;
3619 /* a bug in xp sp2 rc2 causes it to send a fnpcn request without
3620 sending a ffpcn() request first */
3622 if ( !option )
3623 return WERR_BADFID;
3625 if (!get_printer_snum(p, hnd, &snum, NULL)) {
3626 return WERR_BADFID;
3629 /* Maybe we should use the SYSTEM session_info here... */
3630 result = winreg_get_printer_internal(mem_ctx,
3631 get_session_info_system(),
3632 p->msg_ctx,
3633 lp_servicename(talloc_tos(), snum), &pinfo2);
3634 if (!W_ERROR_IS_OK(result)) {
3635 return WERR_BADFID;
3639 * When sending a PRINTER_NOTIFY_FIELD_SERVER_NAME we should send the
3640 * correct servername.
3642 pinfo2->servername = talloc_strdup(pinfo2, Printer->servername);
3643 if (pinfo2->servername == NULL) {
3644 return WERR_NOMEM;
3647 for (i=0; i<option->count; i++) {
3648 option_type = option->types[i];
3650 switch (option_type.type) {
3651 case PRINTER_NOTIFY_TYPE:
3652 if (construct_notify_printer_info(p->msg_ctx,
3653 Printer, info,
3654 pinfo2, snum,
3655 &option_type, id,
3656 mem_ctx)) {
3657 id--;
3659 break;
3661 case JOB_NOTIFY_TYPE:
3663 count = print_queue_status(p->msg_ctx, snum, &queue,
3664 &status);
3666 for (j=0; j<count; j++) {
3667 construct_notify_jobs_info(p->msg_ctx,
3668 &queue[j], info,
3669 pinfo2, snum,
3670 &option_type,
3671 queue[j].sysjob,
3672 mem_ctx);
3675 SAFE_FREE(queue);
3676 break;
3681 * Debugging information, don't delete.
3684 DEBUG(1,("dumping the NOTIFY_INFO\n"));
3685 DEBUGADD(1,("info->version:[%d], info->flags:[%d], info->count:[%d]\n", info->version, info->flags, info->count));
3686 DEBUGADD(1,("num\ttype\tfield\tres\tid\tsize\tenc_type\n"));
3688 for (i=0; i<info->count; i++) {
3689 DEBUGADD(1,("[%d]\t[%d]\t[%d]\t[%d]\t[%d]\t[%d]\t[%d]\n",
3690 i, info->data[i].type, info->data[i].field, info->data[i].reserved,
3691 info->data[i].id, info->data[i].size, info->data[i].enc_type));
3695 talloc_free(pinfo2);
3696 return WERR_OK;
3699 /****************************************************************
3700 _spoolss_RouterRefreshPrinterChangeNotify
3701 ****************************************************************/
3703 WERROR _spoolss_RouterRefreshPrinterChangeNotify(struct pipes_struct *p,
3704 struct spoolss_RouterRefreshPrinterChangeNotify *r)
3706 struct spoolss_NotifyInfo *info;
3708 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
3709 WERROR result = WERR_BADFID;
3711 /* we always have a spoolss_NotifyInfo struct */
3712 info = talloc_zero(p->mem_ctx, struct spoolss_NotifyInfo);
3713 if (!info) {
3714 result = WERR_NOMEM;
3715 goto done;
3718 *r->out.info = info;
3720 if (!Printer) {
3721 DEBUG(2,("_spoolss_RouterRefreshPrinterChangeNotify: "
3722 "Invalid handle (%s:%u:%u).\n",
3723 OUR_HANDLE(r->in.handle)));
3724 goto done;
3727 DEBUG(4,("Printer type %x\n",Printer->printer_type));
3730 * We are now using the change value, and
3731 * I should check for PRINTER_NOTIFY_OPTIONS_REFRESH but as
3732 * I don't have a global notification system, I'm sending back all the
3733 * information even when _NOTHING_ has changed.
3736 /* We need to keep track of the change value to send back in
3737 RRPCN replies otherwise our updates are ignored. */
3739 Printer->notify.fnpcn = true;
3741 if (Printer->notify.cli_chan != NULL &&
3742 Printer->notify.cli_chan->active_connections > 0) {
3743 DEBUG(10,("_spoolss_RouterRefreshPrinterChangeNotify: "
3744 "Saving change value in request [%x]\n",
3745 r->in.change_low));
3746 Printer->notify.change = r->in.change_low;
3749 /* just ignore the spoolss_NotifyOption */
3751 switch (Printer->printer_type) {
3752 case SPLHND_SERVER:
3753 result = printserver_notify_info(p, r->in.handle,
3754 info, p->mem_ctx);
3755 break;
3757 case SPLHND_PRINTER:
3758 result = printer_notify_info(p, r->in.handle,
3759 info, p->mem_ctx);
3760 break;
3763 Printer->notify.fnpcn = false;
3765 done:
3766 return result;
3769 /********************************************************************
3770 ********************************************************************/
3772 static WERROR create_printername(TALLOC_CTX *mem_ctx,
3773 const char *servername,
3774 const char *printername,
3775 const char **printername_p)
3777 /* FIXME: add lp_force_printername() */
3779 if (servername == NULL) {
3780 *printername_p = talloc_strdup(mem_ctx, printername);
3781 W_ERROR_HAVE_NO_MEMORY(*printername_p);
3782 return WERR_OK;
3785 if (servername[0] == '\\' && servername[1] == '\\') {
3786 servername += 2;
3789 *printername_p = talloc_asprintf(mem_ctx, "\\\\%s\\%s", servername, printername);
3790 W_ERROR_HAVE_NO_MEMORY(*printername_p);
3792 return WERR_OK;
3795 /********************************************************************
3796 ********************************************************************/
3798 static void compose_devicemode_devicename(struct spoolss_DeviceMode *dm,
3799 const char *printername)
3801 if (dm == NULL) {
3802 return;
3805 dm->devicename = talloc_strndup(dm, printername,
3806 MIN(strlen(printername), 31));
3809 /********************************************************************
3810 * construct_printer_info_0
3811 * fill a printer_info_0 struct
3812 ********************************************************************/
3814 static WERROR construct_printer_info0(TALLOC_CTX *mem_ctx,
3815 const struct auth_session_info *session_info,
3816 struct messaging_context *msg_ctx,
3817 struct spoolss_PrinterInfo2 *info2,
3818 const char *servername,
3819 struct spoolss_PrinterInfo0 *r,
3820 int snum)
3822 int count;
3823 struct printer_session_counter *session_counter;
3824 struct timeval setuptime;
3825 print_status_struct status;
3826 WERROR result;
3828 result = create_printername(mem_ctx, servername, info2->printername, &r->printername);
3829 if (!W_ERROR_IS_OK(result)) {
3830 return result;
3833 if (servername) {
3834 r->servername = talloc_strdup(mem_ctx, servername);
3835 W_ERROR_HAVE_NO_MEMORY(r->servername);
3836 } else {
3837 r->servername = NULL;
3840 count = print_queue_length(msg_ctx, snum, &status);
3842 /* check if we already have a counter for this printer */
3843 for (session_counter = counter_list; session_counter; session_counter = session_counter->next) {
3844 if (session_counter->snum == snum)
3845 break;
3848 /* it's the first time, add it to the list */
3849 if (session_counter == NULL) {
3850 session_counter = talloc_zero(counter_list, struct printer_session_counter);
3851 W_ERROR_HAVE_NO_MEMORY(session_counter);
3852 session_counter->snum = snum;
3853 session_counter->counter = 0;
3854 DLIST_ADD(counter_list, session_counter);
3857 /* increment it */
3858 session_counter->counter++;
3860 r->cjobs = count;
3861 r->total_jobs = 0;
3862 r->total_bytes = 0;
3864 get_startup_time(&setuptime);
3865 init_systemtime(&r->time, gmtime(&setuptime.tv_sec));
3867 /* JFM:
3868 * the global_counter should be stored in a TDB as it's common to all the clients
3869 * and should be zeroed on samba startup
3871 r->global_counter = session_counter->counter;
3872 r->total_pages = 0;
3873 /* in 2.2 we reported ourselves as 0x0004 and 0x0565 */
3874 SSVAL(&r->version, 0, 0x0005); /* NT 5 */
3875 SSVAL(&r->version, 2, 0x0893); /* build 2195 */
3876 r->free_build = SPOOLSS_RELEASE_BUILD;
3877 r->spooling = 0;
3878 r->max_spooling = 0;
3879 r->session_counter = session_counter->counter;
3880 r->num_error_out_of_paper = 0x0;
3881 r->num_error_not_ready = 0x0; /* number of print failure */
3882 r->job_error = 0x0;
3883 r->number_of_processors = 0x1;
3884 r->processor_type = PROCESSOR_INTEL_PENTIUM; /* 586 Pentium ? */
3885 r->high_part_total_bytes = 0x0;
3887 /* ChangeID in milliseconds*/
3888 winreg_printer_get_changeid_internal(mem_ctx, session_info, msg_ctx,
3889 info2->sharename, &r->change_id);
3891 r->last_error = WERR_OK;
3892 r->status = nt_printq_status(status.status);
3893 r->enumerate_network_printers = 0x0;
3894 r->c_setprinter = 0x0;
3895 r->processor_architecture = PROCESSOR_ARCHITECTURE_INTEL;
3896 r->processor_level = 0x6; /* 6 ???*/
3897 r->ref_ic = 0;
3898 r->reserved2 = 0;
3899 r->reserved3 = 0;
3901 return WERR_OK;
3905 /********************************************************************
3906 * construct_printer_info1
3907 * fill a spoolss_PrinterInfo1 struct
3908 ********************************************************************/
3910 static WERROR construct_printer_info1(TALLOC_CTX *mem_ctx,
3911 const struct spoolss_PrinterInfo2 *info2,
3912 uint32_t flags,
3913 const char *servername,
3914 struct spoolss_PrinterInfo1 *r,
3915 int snum)
3917 WERROR result;
3919 r->flags = flags;
3921 if (info2->comment == NULL || info2->comment[0] == '\0') {
3922 r->comment = lp_comment(mem_ctx, snum);
3923 } else {
3924 r->comment = talloc_strdup(mem_ctx, info2->comment); /* saved comment */
3926 W_ERROR_HAVE_NO_MEMORY(r->comment);
3928 result = create_printername(mem_ctx, servername, info2->printername, &r->name);
3929 if (!W_ERROR_IS_OK(result)) {
3930 return result;
3933 r->description = talloc_asprintf(mem_ctx, "%s,%s,%s",
3934 r->name,
3935 info2->drivername,
3936 r->comment);
3937 W_ERROR_HAVE_NO_MEMORY(r->description);
3939 return WERR_OK;
3942 /********************************************************************
3943 * construct_printer_info2
3944 * fill a spoolss_PrinterInfo2 struct
3945 ********************************************************************/
3947 static WERROR construct_printer_info2(TALLOC_CTX *mem_ctx,
3948 struct messaging_context *msg_ctx,
3949 const struct spoolss_PrinterInfo2 *info2,
3950 const char *servername,
3951 struct spoolss_PrinterInfo2 *r,
3952 int snum)
3954 int count;
3955 print_status_struct status;
3956 WERROR result;
3958 count = print_queue_length(msg_ctx, snum, &status);
3960 if (servername) {
3961 r->servername = talloc_strdup(mem_ctx, servername);
3962 W_ERROR_HAVE_NO_MEMORY(r->servername);
3963 } else {
3964 r->servername = NULL;
3967 result = create_printername(mem_ctx, servername, info2->printername, &r->printername);
3968 if (!W_ERROR_IS_OK(result)) {
3969 return result;
3972 r->sharename = lp_servicename(mem_ctx, snum);
3973 W_ERROR_HAVE_NO_MEMORY(r->sharename);
3974 r->portname = talloc_strdup(mem_ctx, info2->portname);
3975 W_ERROR_HAVE_NO_MEMORY(r->portname);
3976 r->drivername = talloc_strdup(mem_ctx, info2->drivername);
3977 W_ERROR_HAVE_NO_MEMORY(r->drivername);
3979 if (info2->comment[0] == '\0') {
3980 r->comment = lp_comment(mem_ctx, snum);
3981 } else {
3982 r->comment = talloc_strdup(mem_ctx, info2->comment);
3984 W_ERROR_HAVE_NO_MEMORY(r->comment);
3986 r->location = talloc_strdup(mem_ctx, info2->location);
3987 if (info2->location[0] == '\0') {
3988 const char *loc = NULL;
3989 NTSTATUS nt_status;
3991 nt_status = printer_list_get_printer(mem_ctx,
3992 info2->sharename,
3993 NULL,
3994 &loc,
3995 NULL);
3996 if (NT_STATUS_IS_OK(nt_status)) {
3997 if (loc != NULL) {
3998 r->location = talloc_strdup(mem_ctx, loc);
4002 W_ERROR_HAVE_NO_MEMORY(r->location);
4004 r->sepfile = talloc_strdup(mem_ctx, info2->sepfile);
4005 W_ERROR_HAVE_NO_MEMORY(r->sepfile);
4006 r->printprocessor = talloc_strdup(mem_ctx, info2->printprocessor);
4007 W_ERROR_HAVE_NO_MEMORY(r->printprocessor);
4008 r->datatype = talloc_strdup(mem_ctx, info2->datatype);
4009 W_ERROR_HAVE_NO_MEMORY(r->datatype);
4010 r->parameters = talloc_strdup(mem_ctx, info2->parameters);
4011 W_ERROR_HAVE_NO_MEMORY(r->parameters);
4013 r->attributes = info2->attributes;
4015 r->priority = info2->priority;
4016 r->defaultpriority = info2->defaultpriority;
4017 r->starttime = info2->starttime;
4018 r->untiltime = info2->untiltime;
4019 r->status = nt_printq_status(status.status);
4020 r->cjobs = count;
4021 r->averageppm = info2->averageppm;
4023 if (info2->devmode != NULL) {
4024 result = copy_devicemode(mem_ctx,
4025 info2->devmode,
4026 &r->devmode);
4027 if (!W_ERROR_IS_OK(result)) {
4028 return result;
4030 } else if (lp_default_devmode(snum)) {
4031 result = spoolss_create_default_devmode(mem_ctx,
4032 info2->printername,
4033 &r->devmode);
4034 if (!W_ERROR_IS_OK(result)) {
4035 return result;
4037 } else {
4038 r->devmode = NULL;
4039 DEBUG(8,("Returning NULL Devicemode!\n"));
4042 compose_devicemode_devicename(r->devmode, r->printername);
4044 r->secdesc = NULL;
4046 if (info2->secdesc != NULL) {
4047 /* don't use talloc_steal() here unless you do a deep steal of all
4048 the SEC_DESC members */
4050 r->secdesc = dup_sec_desc(mem_ctx, info2->secdesc);
4053 return WERR_OK;
4056 /********************************************************************
4057 * construct_printer_info3
4058 * fill a spoolss_PrinterInfo3 struct
4059 ********************************************************************/
4061 static WERROR construct_printer_info3(TALLOC_CTX *mem_ctx,
4062 const struct spoolss_PrinterInfo2 *info2,
4063 const char *servername,
4064 struct spoolss_PrinterInfo3 *r,
4065 int snum)
4067 /* These are the components of the SD we are returning. */
4069 if (info2->secdesc != NULL) {
4070 /* don't use talloc_steal() here unless you do a deep steal of all
4071 the SEC_DESC members */
4073 r->secdesc = dup_sec_desc(mem_ctx, info2->secdesc);
4074 W_ERROR_HAVE_NO_MEMORY(r->secdesc);
4077 return WERR_OK;
4080 /********************************************************************
4081 * construct_printer_info4
4082 * fill a spoolss_PrinterInfo4 struct
4083 ********************************************************************/
4085 static WERROR construct_printer_info4(TALLOC_CTX *mem_ctx,
4086 const struct spoolss_PrinterInfo2 *info2,
4087 const char *servername,
4088 struct spoolss_PrinterInfo4 *r,
4089 int snum)
4091 WERROR result;
4093 result = create_printername(mem_ctx, servername, info2->printername, &r->printername);
4094 if (!W_ERROR_IS_OK(result)) {
4095 return result;
4098 if (servername) {
4099 r->servername = talloc_strdup(mem_ctx, servername);
4100 W_ERROR_HAVE_NO_MEMORY(r->servername);
4101 } else {
4102 r->servername = NULL;
4105 r->attributes = info2->attributes;
4107 return WERR_OK;
4110 /********************************************************************
4111 * construct_printer_info5
4112 * fill a spoolss_PrinterInfo5 struct
4113 ********************************************************************/
4115 static WERROR construct_printer_info5(TALLOC_CTX *mem_ctx,
4116 const struct spoolss_PrinterInfo2 *info2,
4117 const char *servername,
4118 struct spoolss_PrinterInfo5 *r,
4119 int snum)
4121 WERROR result;
4123 result = create_printername(mem_ctx, servername, info2->printername, &r->printername);
4124 if (!W_ERROR_IS_OK(result)) {
4125 return result;
4128 r->portname = talloc_strdup(mem_ctx, info2->portname);
4129 W_ERROR_HAVE_NO_MEMORY(r->portname);
4131 r->attributes = info2->attributes;
4133 /* these two are not used by NT+ according to MSDN */
4134 r->device_not_selected_timeout = 0x0; /* have seen 0x3a98 */
4135 r->transmission_retry_timeout = 0x0; /* have seen 0xafc8 */
4137 return WERR_OK;
4140 /********************************************************************
4141 * construct_printer_info_6
4142 * fill a spoolss_PrinterInfo6 struct
4143 ********************************************************************/
4145 static WERROR construct_printer_info6(TALLOC_CTX *mem_ctx,
4146 struct messaging_context *msg_ctx,
4147 const struct spoolss_PrinterInfo2 *info2,
4148 const char *servername,
4149 struct spoolss_PrinterInfo6 *r,
4150 int snum)
4152 print_status_struct status;
4154 print_queue_length(msg_ctx, snum, &status);
4156 r->status = nt_printq_status(status.status);
4158 return WERR_OK;
4161 /********************************************************************
4162 * construct_printer_info7
4163 * fill a spoolss_PrinterInfo7 struct
4164 ********************************************************************/
4166 static WERROR construct_printer_info7(TALLOC_CTX *mem_ctx,
4167 struct messaging_context *msg_ctx,
4168 const char *servername,
4169 struct spoolss_PrinterInfo7 *r,
4170 int snum)
4172 const struct auth_session_info *session_info = get_session_info_system();
4173 struct GUID guid;
4175 if (is_printer_published(mem_ctx, session_info, msg_ctx,
4176 servername,
4177 lp_servicename(talloc_tos(), snum), &guid, NULL)) {
4178 r->guid = talloc_strdup_upper(mem_ctx, GUID_string2(mem_ctx, &guid));
4179 r->action = DSPRINT_PUBLISH;
4180 } else {
4181 r->guid = talloc_strdup(mem_ctx, "");
4182 r->action = DSPRINT_UNPUBLISH;
4184 W_ERROR_HAVE_NO_MEMORY(r->guid);
4186 return WERR_OK;
4189 /********************************************************************
4190 * construct_printer_info8
4191 * fill a spoolss_PrinterInfo8 struct
4192 ********************************************************************/
4194 static WERROR construct_printer_info8(TALLOC_CTX *mem_ctx,
4195 const struct spoolss_PrinterInfo2 *info2,
4196 const char *servername,
4197 struct spoolss_DeviceModeInfo *r,
4198 int snum)
4200 WERROR result;
4201 const char *printername;
4203 result = create_printername(mem_ctx, servername, info2->printername, &printername);
4204 if (!W_ERROR_IS_OK(result)) {
4205 return result;
4208 if (info2->devmode != NULL) {
4209 result = copy_devicemode(mem_ctx,
4210 info2->devmode,
4211 &r->devmode);
4212 if (!W_ERROR_IS_OK(result)) {
4213 return result;
4215 } else if (lp_default_devmode(snum)) {
4216 result = spoolss_create_default_devmode(mem_ctx,
4217 info2->printername,
4218 &r->devmode);
4219 if (!W_ERROR_IS_OK(result)) {
4220 return result;
4222 } else {
4223 r->devmode = NULL;
4224 DEBUG(8,("Returning NULL Devicemode!\n"));
4227 compose_devicemode_devicename(r->devmode, printername);
4229 return WERR_OK;
4233 /********************************************************************
4234 ********************************************************************/
4236 static bool snum_is_shared_printer(int snum)
4238 return (lp_browseable(snum) && lp_snum_ok(snum) && lp_print_ok(snum));
4241 /********************************************************************
4242 Spoolss_enumprinters.
4243 ********************************************************************/
4245 static WERROR enum_all_printers_info_level(TALLOC_CTX *mem_ctx,
4246 const struct auth_session_info *session_info,
4247 struct messaging_context *msg_ctx,
4248 const char *servername,
4249 uint32_t level,
4250 uint32_t flags,
4251 union spoolss_PrinterInfo **info_p,
4252 uint32_t *count_p)
4254 int snum;
4255 int n_services = lp_numservices();
4256 union spoolss_PrinterInfo *info = NULL;
4257 uint32_t count = 0;
4258 WERROR result = WERR_OK;
4259 struct dcerpc_binding_handle *b = NULL;
4260 TALLOC_CTX *tmp_ctx = NULL;
4262 tmp_ctx = talloc_new(mem_ctx);
4263 if (!tmp_ctx) {
4264 return WERR_NOMEM;
4267 *count_p = 0;
4268 *info_p = NULL;
4270 for (snum = 0; snum < n_services; snum++) {
4272 const char *printer;
4273 struct spoolss_PrinterInfo2 *info2;
4275 if (!snum_is_shared_printer(snum)) {
4276 continue;
4279 printer = lp_const_servicename(snum);
4281 DEBUG(4,("Found a printer in smb.conf: %s[%x]\n",
4282 printer, snum));
4284 if (b == NULL) {
4285 result = winreg_printer_binding_handle(tmp_ctx,
4286 session_info,
4287 msg_ctx,
4288 &b);
4289 if (!W_ERROR_IS_OK(result)) {
4290 goto out;
4294 result = winreg_create_printer(tmp_ctx, b,
4295 printer);
4296 if (!W_ERROR_IS_OK(result)) {
4297 goto out;
4300 info = talloc_realloc(tmp_ctx, info,
4301 union spoolss_PrinterInfo,
4302 count + 1);
4303 if (!info) {
4304 result = WERR_NOMEM;
4305 goto out;
4308 result = winreg_get_printer(tmp_ctx, b,
4309 printer, &info2);
4310 if (!W_ERROR_IS_OK(result)) {
4311 goto out;
4314 switch (level) {
4315 case 0:
4316 result = construct_printer_info0(info, session_info,
4317 msg_ctx, info2,
4318 servername,
4319 &info[count].info0, snum);
4320 break;
4321 case 1:
4322 result = construct_printer_info1(info, info2, flags,
4323 servername,
4324 &info[count].info1, snum);
4325 break;
4326 case 2:
4327 result = construct_printer_info2(info, msg_ctx, info2,
4328 servername,
4329 &info[count].info2, snum);
4330 break;
4331 case 4:
4332 result = construct_printer_info4(info, info2,
4333 servername,
4334 &info[count].info4, snum);
4335 break;
4336 case 5:
4337 result = construct_printer_info5(info, info2,
4338 servername,
4339 &info[count].info5, snum);
4340 break;
4342 default:
4343 result = WERR_UNKNOWN_LEVEL;
4344 goto out;
4347 if (!W_ERROR_IS_OK(result)) {
4348 goto out;
4351 count++;
4354 out:
4355 if (W_ERROR_IS_OK(result)) {
4356 *info_p = talloc_move(mem_ctx, &info);
4357 *count_p = count;
4360 talloc_free(tmp_ctx);
4362 return result;
4365 /********************************************************************
4366 * handle enumeration of printers at level 0
4367 ********************************************************************/
4369 static WERROR enumprinters_level0(TALLOC_CTX *mem_ctx,
4370 const struct auth_session_info *session_info,
4371 struct messaging_context *msg_ctx,
4372 uint32_t flags,
4373 const char *servername,
4374 union spoolss_PrinterInfo **info,
4375 uint32_t *count)
4377 DEBUG(4,("enum_all_printers_info_0\n"));
4379 return enum_all_printers_info_level(mem_ctx, session_info, msg_ctx,
4380 servername, 0, flags, info, count);
4384 /********************************************************************
4385 ********************************************************************/
4387 static WERROR enum_all_printers_info_1(TALLOC_CTX *mem_ctx,
4388 const struct auth_session_info *session_info,
4389 struct messaging_context *msg_ctx,
4390 const char *servername,
4391 uint32_t flags,
4392 union spoolss_PrinterInfo **info,
4393 uint32_t *count)
4395 DEBUG(4,("enum_all_printers_info_1\n"));
4397 return enum_all_printers_info_level(mem_ctx, session_info, msg_ctx,
4398 servername, 1, flags, info, count);
4401 /********************************************************************
4402 enum_all_printers_info_1_local.
4403 *********************************************************************/
4405 static WERROR enum_all_printers_info_1_local(TALLOC_CTX *mem_ctx,
4406 const struct auth_session_info *session_info,
4407 struct messaging_context *msg_ctx,
4408 const char *servername,
4409 union spoolss_PrinterInfo **info,
4410 uint32_t *count)
4412 DEBUG(4,("enum_all_printers_info_1_local\n"));
4414 return enum_all_printers_info_1(mem_ctx, session_info, msg_ctx,
4415 servername, PRINTER_ENUM_ICON8, info, count);
4418 /********************************************************************
4419 enum_all_printers_info_1_name.
4420 *********************************************************************/
4422 static WERROR enum_all_printers_info_1_name(TALLOC_CTX *mem_ctx,
4423 const struct auth_session_info *session_info,
4424 struct messaging_context *msg_ctx,
4425 const char *servername,
4426 union spoolss_PrinterInfo **info,
4427 uint32_t *count)
4429 const char *s = servername;
4431 DEBUG(4,("enum_all_printers_info_1_name\n"));
4433 if (servername != NULL &&
4434 (servername[0] == '\\') && (servername[1] == '\\')) {
4435 s = servername + 2;
4438 if (!is_myname_or_ipaddr(s)) {
4439 return WERR_INVALID_NAME;
4442 return enum_all_printers_info_1(mem_ctx, session_info, msg_ctx,
4443 servername, PRINTER_ENUM_ICON8, info, count);
4446 /********************************************************************
4447 enum_all_printers_info_1_network.
4448 *********************************************************************/
4450 static WERROR enum_all_printers_info_1_network(TALLOC_CTX *mem_ctx,
4451 const struct auth_session_info *session_info,
4452 struct messaging_context *msg_ctx,
4453 const char *servername,
4454 union spoolss_PrinterInfo **info,
4455 uint32_t *count)
4457 const char *s = servername;
4459 DEBUG(4,("enum_all_printers_info_1_network\n"));
4461 /* If we respond to a enum_printers level 1 on our name with flags
4462 set to PRINTER_ENUM_REMOTE with a list of printers then these
4463 printers incorrectly appear in the APW browse list.
4464 Specifically the printers for the server appear at the workgroup
4465 level where all the other servers in the domain are
4466 listed. Windows responds to this call with a
4467 WERR_CAN_NOT_COMPLETE so we should do the same. */
4469 if (servername != NULL &&
4470 (servername[0] == '\\') && (servername[1] == '\\')) {
4471 s = servername + 2;
4474 if (is_myname_or_ipaddr(s)) {
4475 return WERR_CAN_NOT_COMPLETE;
4478 return enum_all_printers_info_1(mem_ctx, session_info, msg_ctx,
4479 servername, PRINTER_ENUM_NAME, info, count);
4482 /********************************************************************
4483 * api_spoolss_enumprinters
4485 * called from api_spoolss_enumprinters (see this to understand)
4486 ********************************************************************/
4488 static WERROR enum_all_printers_info_2(TALLOC_CTX *mem_ctx,
4489 const struct auth_session_info *session_info,
4490 struct messaging_context *msg_ctx,
4491 const char *servername,
4492 union spoolss_PrinterInfo **info,
4493 uint32_t *count)
4495 DEBUG(4,("enum_all_printers_info_2\n"));
4497 return enum_all_printers_info_level(mem_ctx, session_info, msg_ctx,
4498 servername, 2, 0, info, count);
4501 /********************************************************************
4502 * handle enumeration of printers at level 1
4503 ********************************************************************/
4505 static WERROR enumprinters_level1(TALLOC_CTX *mem_ctx,
4506 const struct auth_session_info *session_info,
4507 struct messaging_context *msg_ctx,
4508 uint32_t flags,
4509 const char *servername,
4510 union spoolss_PrinterInfo **info,
4511 uint32_t *count)
4513 /* Not all the flags are equals */
4515 if (flags & PRINTER_ENUM_LOCAL) {
4516 return enum_all_printers_info_1_local(mem_ctx, session_info,
4517 msg_ctx, servername, info, count);
4520 if (flags & PRINTER_ENUM_NAME) {
4521 return enum_all_printers_info_1_name(mem_ctx, session_info,
4522 msg_ctx, servername, info,
4523 count);
4526 if (flags & PRINTER_ENUM_NETWORK) {
4527 return enum_all_printers_info_1_network(mem_ctx, session_info,
4528 msg_ctx, servername, info,
4529 count);
4532 return WERR_OK; /* NT4sp5 does that */
4535 /********************************************************************
4536 * handle enumeration of printers at level 2
4537 ********************************************************************/
4539 static WERROR enumprinters_level2(TALLOC_CTX *mem_ctx,
4540 const struct auth_session_info *session_info,
4541 struct messaging_context *msg_ctx,
4542 uint32_t flags,
4543 const char *servername,
4544 union spoolss_PrinterInfo **info,
4545 uint32_t *count)
4547 if (flags & PRINTER_ENUM_LOCAL) {
4549 return enum_all_printers_info_2(mem_ctx, session_info, msg_ctx,
4550 servername,
4551 info, count);
4554 if (flags & PRINTER_ENUM_NAME) {
4555 if (servername && !is_myname_or_ipaddr(canon_servername(servername))) {
4556 return WERR_INVALID_NAME;
4559 return enum_all_printers_info_2(mem_ctx, session_info, msg_ctx,
4560 servername,
4561 info, count);
4564 if (flags & PRINTER_ENUM_REMOTE) {
4565 return WERR_UNKNOWN_LEVEL;
4568 return WERR_OK;
4571 /********************************************************************
4572 * handle enumeration of printers at level 4
4573 ********************************************************************/
4575 static WERROR enumprinters_level4(TALLOC_CTX *mem_ctx,
4576 const struct auth_session_info *session_info,
4577 struct messaging_context *msg_ctx,
4578 uint32_t flags,
4579 const char *servername,
4580 union spoolss_PrinterInfo **info,
4581 uint32_t *count)
4583 DEBUG(4,("enum_all_printers_info_4\n"));
4585 return enum_all_printers_info_level(mem_ctx, session_info, msg_ctx,
4586 servername, 4, flags, info, count);
4590 /********************************************************************
4591 * handle enumeration of printers at level 5
4592 ********************************************************************/
4594 static WERROR enumprinters_level5(TALLOC_CTX *mem_ctx,
4595 const struct auth_session_info *session_info,
4596 struct messaging_context *msg_ctx,
4597 uint32_t flags,
4598 const char *servername,
4599 union spoolss_PrinterInfo **info,
4600 uint32_t *count)
4602 DEBUG(4,("enum_all_printers_info_5\n"));
4604 return enum_all_printers_info_level(mem_ctx, session_info, msg_ctx,
4605 servername, 5, flags, info, count);
4608 /****************************************************************
4609 _spoolss_EnumPrinters
4610 ****************************************************************/
4612 WERROR _spoolss_EnumPrinters(struct pipes_struct *p,
4613 struct spoolss_EnumPrinters *r)
4615 const struct auth_session_info *session_info = get_session_info_system();
4616 WERROR result;
4618 /* that's an [in out] buffer */
4620 if (!r->in.buffer && (r->in.offered != 0)) {
4621 return WERR_INVALID_PARAM;
4624 DEBUG(4,("_spoolss_EnumPrinters\n"));
4626 *r->out.needed = 0;
4627 *r->out.count = 0;
4628 *r->out.info = NULL;
4631 * Level 1:
4632 * flags==PRINTER_ENUM_NAME
4633 * if name=="" then enumerates all printers
4634 * if name!="" then enumerate the printer
4635 * flags==PRINTER_ENUM_REMOTE
4636 * name is NULL, enumerate printers
4637 * Level 2: name!="" enumerates printers, name can't be NULL
4638 * Level 3: doesn't exist
4639 * Level 4: does a local registry lookup
4640 * Level 5: same as Level 2
4643 if (r->in.server && r->in.server[0] == '\0') {
4644 r->in.server = NULL;
4647 switch (r->in.level) {
4648 case 0:
4649 result = enumprinters_level0(p->mem_ctx, session_info,
4650 p->msg_ctx, r->in.flags,
4651 r->in.server,
4652 r->out.info, r->out.count);
4653 break;
4654 case 1:
4655 result = enumprinters_level1(p->mem_ctx, session_info,
4656 p->msg_ctx, r->in.flags,
4657 r->in.server,
4658 r->out.info, r->out.count);
4659 break;
4660 case 2:
4661 result = enumprinters_level2(p->mem_ctx, session_info,
4662 p->msg_ctx, r->in.flags,
4663 r->in.server,
4664 r->out.info, r->out.count);
4665 break;
4666 case 4:
4667 result = enumprinters_level4(p->mem_ctx, session_info,
4668 p->msg_ctx, r->in.flags,
4669 r->in.server,
4670 r->out.info, r->out.count);
4671 break;
4672 case 5:
4673 result = enumprinters_level5(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 default:
4679 return WERR_UNKNOWN_LEVEL;
4682 if (!W_ERROR_IS_OK(result)) {
4683 return result;
4686 *r->out.needed = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
4687 spoolss_EnumPrinters,
4688 *r->out.info, r->in.level,
4689 *r->out.count);
4690 *r->out.info = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
4691 *r->out.count = SPOOLSS_BUFFER_OK(*r->out.count, 0);
4693 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
4696 /****************************************************************
4697 _spoolss_GetPrinter
4698 ****************************************************************/
4700 WERROR _spoolss_GetPrinter(struct pipes_struct *p,
4701 struct spoolss_GetPrinter *r)
4703 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
4704 struct spoolss_PrinterInfo2 *info2 = NULL;
4705 WERROR result = WERR_OK;
4706 int snum;
4708 /* that's an [in out] buffer */
4710 if (!r->in.buffer && (r->in.offered != 0)) {
4711 return WERR_INVALID_PARAM;
4714 *r->out.needed = 0;
4716 if (Printer == NULL) {
4717 return WERR_BADFID;
4720 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
4721 return WERR_BADFID;
4724 result = winreg_get_printer_internal(p->mem_ctx,
4725 get_session_info_system(),
4726 p->msg_ctx,
4727 lp_const_servicename(snum),
4728 &info2);
4729 if (!W_ERROR_IS_OK(result)) {
4730 goto out;
4733 switch (r->in.level) {
4734 case 0:
4735 result = construct_printer_info0(p->mem_ctx,
4736 get_session_info_system(),
4737 p->msg_ctx,
4738 info2,
4739 Printer->servername,
4740 &r->out.info->info0,
4741 snum);
4742 break;
4743 case 1:
4744 result = construct_printer_info1(p->mem_ctx, info2,
4745 PRINTER_ENUM_ICON8,
4746 Printer->servername,
4747 &r->out.info->info1, snum);
4748 break;
4749 case 2:
4750 result = construct_printer_info2(p->mem_ctx, p->msg_ctx, info2,
4751 Printer->servername,
4752 &r->out.info->info2, snum);
4753 break;
4754 case 3:
4755 result = construct_printer_info3(p->mem_ctx, info2,
4756 Printer->servername,
4757 &r->out.info->info3, snum);
4758 break;
4759 case 4:
4760 result = construct_printer_info4(p->mem_ctx, info2,
4761 Printer->servername,
4762 &r->out.info->info4, snum);
4763 break;
4764 case 5:
4765 result = construct_printer_info5(p->mem_ctx, info2,
4766 Printer->servername,
4767 &r->out.info->info5, snum);
4768 break;
4769 case 6:
4770 result = construct_printer_info6(p->mem_ctx, p->msg_ctx, info2,
4771 Printer->servername,
4772 &r->out.info->info6, snum);
4773 break;
4774 case 7:
4775 result = construct_printer_info7(p->mem_ctx, p->msg_ctx,
4776 Printer->servername,
4777 &r->out.info->info7, snum);
4778 break;
4779 case 8:
4780 result = construct_printer_info8(p->mem_ctx, info2,
4781 Printer->servername,
4782 &r->out.info->info8, snum);
4783 break;
4784 default:
4785 result = WERR_UNKNOWN_LEVEL;
4786 break;
4788 TALLOC_FREE(info2);
4790 out:
4791 if (!W_ERROR_IS_OK(result)) {
4792 DEBUG(0, ("_spoolss_GetPrinter: failed to construct printer info level %d - %s\n",
4793 r->in.level, win_errstr(result)));
4794 TALLOC_FREE(r->out.info);
4795 return result;
4798 *r->out.needed = SPOOLSS_BUFFER_UNION(spoolss_PrinterInfo,
4799 r->out.info, r->in.level);
4800 r->out.info = SPOOLSS_BUFFER_OK(r->out.info, NULL);
4802 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
4805 /********************************************************************
4806 ********************************************************************/
4808 #define FILL_DRIVER_STRING(mem_ctx, in, out) \
4809 do { \
4810 if (in && strlen(in)) { \
4811 out = talloc_strdup(mem_ctx, in); \
4812 } else { \
4813 out = talloc_strdup(mem_ctx, ""); \
4815 W_ERROR_HAVE_NO_MEMORY(out); \
4816 } while (0);
4818 #define FILL_DRIVER_UNC_STRING(mem_ctx, server, arch, ver, in, out) \
4819 do { \
4820 if (in && strlen(in)) { \
4821 out = talloc_asprintf(mem_ctx, "\\\\%s\\print$\\%s\\%d\\%s", server, get_short_archi(arch), ver, in); \
4822 } else { \
4823 out = talloc_strdup(mem_ctx, ""); \
4825 W_ERROR_HAVE_NO_MEMORY(out); \
4826 } while (0);
4828 static WERROR string_array_from_driver_info(TALLOC_CTX *mem_ctx,
4829 const char **string_array,
4830 const char ***presult,
4831 const char *cservername,
4832 const char *arch,
4833 int version)
4835 int i, num_strings = 0;
4836 const char **array = NULL;
4838 if (string_array == NULL) {
4839 return WERR_INVALID_PARAMETER;
4842 for (i=0; string_array[i] && string_array[i][0] != '\0'; i++) {
4843 const char *str = NULL;
4845 if (cservername == NULL || arch == NULL) {
4846 FILL_DRIVER_STRING(mem_ctx, string_array[i], str);
4847 } else {
4848 FILL_DRIVER_UNC_STRING(mem_ctx, cservername, arch, version, string_array[i], str);
4851 if (!add_string_to_array(mem_ctx, str, &array, &num_strings)) {
4852 TALLOC_FREE(array);
4853 return WERR_NOMEM;
4857 if (i > 0) {
4858 ADD_TO_ARRAY(mem_ctx, const char *, NULL,
4859 &array, &num_strings);
4862 if (presult) {
4863 *presult = array;
4866 return WERR_OK;
4869 /********************************************************************
4870 * fill a spoolss_DriverInfo1 struct
4871 ********************************************************************/
4873 static WERROR fill_printer_driver_info1(TALLOC_CTX *mem_ctx,
4874 struct spoolss_DriverInfo1 *r,
4875 const struct spoolss_DriverInfo8 *driver,
4876 const char *servername)
4878 r->driver_name = talloc_strdup(mem_ctx, driver->driver_name);
4879 W_ERROR_HAVE_NO_MEMORY(r->driver_name);
4881 return WERR_OK;
4884 /********************************************************************
4885 * fill a spoolss_DriverInfo2 struct
4886 ********************************************************************/
4888 static WERROR fill_printer_driver_info2(TALLOC_CTX *mem_ctx,
4889 struct spoolss_DriverInfo2 *r,
4890 const struct spoolss_DriverInfo8 *driver,
4891 const char *servername)
4894 const char *cservername = canon_servername(servername);
4896 r->version = driver->version;
4898 r->driver_name = talloc_strdup(mem_ctx, driver->driver_name);
4899 W_ERROR_HAVE_NO_MEMORY(r->driver_name);
4900 r->architecture = talloc_strdup(mem_ctx, driver->architecture);
4901 W_ERROR_HAVE_NO_MEMORY(r->architecture);
4903 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4904 driver->architecture,
4905 driver->version,
4906 driver->driver_path,
4907 r->driver_path);
4909 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4910 driver->architecture,
4911 driver->version,
4912 driver->data_file,
4913 r->data_file);
4915 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4916 driver->architecture,
4917 driver->version,
4918 driver->config_file,
4919 r->config_file);
4921 return WERR_OK;
4924 /********************************************************************
4925 * fill a spoolss_DriverInfo3 struct
4926 ********************************************************************/
4928 static WERROR fill_printer_driver_info3(TALLOC_CTX *mem_ctx,
4929 struct spoolss_DriverInfo3 *r,
4930 const struct spoolss_DriverInfo8 *driver,
4931 const char *servername)
4933 const char *cservername = canon_servername(servername);
4935 r->version = driver->version;
4937 r->driver_name = talloc_strdup(mem_ctx, driver->driver_name);
4938 W_ERROR_HAVE_NO_MEMORY(r->driver_name);
4939 r->architecture = talloc_strdup(mem_ctx, driver->architecture);
4940 W_ERROR_HAVE_NO_MEMORY(r->architecture);
4942 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4943 driver->architecture,
4944 driver->version,
4945 driver->driver_path,
4946 r->driver_path);
4948 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4949 driver->architecture,
4950 driver->version,
4951 driver->data_file,
4952 r->data_file);
4954 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4955 driver->architecture,
4956 driver->version,
4957 driver->config_file,
4958 r->config_file);
4960 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4961 driver->architecture,
4962 driver->version,
4963 driver->help_file,
4964 r->help_file);
4966 FILL_DRIVER_STRING(mem_ctx,
4967 driver->monitor_name,
4968 r->monitor_name);
4970 FILL_DRIVER_STRING(mem_ctx,
4971 driver->default_datatype,
4972 r->default_datatype);
4974 return string_array_from_driver_info(mem_ctx,
4975 driver->dependent_files,
4976 &r->dependent_files,
4977 cservername,
4978 driver->architecture,
4979 driver->version);
4982 /********************************************************************
4983 * fill a spoolss_DriverInfo4 struct
4984 ********************************************************************/
4986 static WERROR fill_printer_driver_info4(TALLOC_CTX *mem_ctx,
4987 struct spoolss_DriverInfo4 *r,
4988 const struct spoolss_DriverInfo8 *driver,
4989 const char *servername)
4991 const char *cservername = canon_servername(servername);
4992 WERROR result;
4994 r->version = driver->version;
4996 r->driver_name = talloc_strdup(mem_ctx, driver->driver_name);
4997 W_ERROR_HAVE_NO_MEMORY(r->driver_name);
4998 r->architecture = talloc_strdup(mem_ctx, driver->architecture);
4999 W_ERROR_HAVE_NO_MEMORY(r->architecture);
5001 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
5002 driver->architecture,
5003 driver->version,
5004 driver->driver_path,
5005 r->driver_path);
5007 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
5008 driver->architecture,
5009 driver->version,
5010 driver->data_file,
5011 r->data_file);
5013 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
5014 driver->architecture,
5015 driver->version,
5016 driver->config_file,
5017 r->config_file);
5019 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
5020 driver->architecture,
5021 driver->version,
5022 driver->help_file,
5023 r->help_file);
5025 result = string_array_from_driver_info(mem_ctx,
5026 driver->dependent_files,
5027 &r->dependent_files,
5028 cservername,
5029 driver->architecture,
5030 driver->version);
5031 if (!W_ERROR_IS_OK(result)) {
5032 return result;
5035 FILL_DRIVER_STRING(mem_ctx,
5036 driver->monitor_name,
5037 r->monitor_name);
5039 FILL_DRIVER_STRING(mem_ctx,
5040 driver->default_datatype,
5041 r->default_datatype);
5044 result = string_array_from_driver_info(mem_ctx,
5045 driver->previous_names,
5046 &r->previous_names,
5047 NULL, NULL, 0);
5049 return result;
5052 /********************************************************************
5053 * fill a spoolss_DriverInfo5 struct
5054 ********************************************************************/
5056 static WERROR fill_printer_driver_info5(TALLOC_CTX *mem_ctx,
5057 struct spoolss_DriverInfo5 *r,
5058 const struct spoolss_DriverInfo8 *driver,
5059 const char *servername)
5061 const char *cservername = canon_servername(servername);
5063 r->version = driver->version;
5065 r->driver_name = talloc_strdup(mem_ctx, driver->driver_name);
5066 W_ERROR_HAVE_NO_MEMORY(r->driver_name);
5067 r->architecture = talloc_strdup(mem_ctx, driver->architecture);
5068 W_ERROR_HAVE_NO_MEMORY(r->architecture);
5070 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
5071 driver->architecture,
5072 driver->version,
5073 driver->driver_path,
5074 r->driver_path);
5076 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
5077 driver->architecture,
5078 driver->version,
5079 driver->data_file,
5080 r->data_file);
5082 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
5083 driver->architecture,
5084 driver->version,
5085 driver->config_file,
5086 r->config_file);
5088 r->driver_attributes = 0;
5089 r->config_version = 0;
5090 r->driver_version = 0;
5092 return WERR_OK;
5094 /********************************************************************
5095 * fill a spoolss_DriverInfo6 struct
5096 ********************************************************************/
5098 static WERROR fill_printer_driver_info6(TALLOC_CTX *mem_ctx,
5099 struct spoolss_DriverInfo6 *r,
5100 const struct spoolss_DriverInfo8 *driver,
5101 const char *servername)
5103 const char *cservername = canon_servername(servername);
5104 WERROR result;
5106 r->version = driver->version;
5108 r->driver_name = talloc_strdup(mem_ctx, driver->driver_name);
5109 W_ERROR_HAVE_NO_MEMORY(r->driver_name);
5110 r->architecture = talloc_strdup(mem_ctx, driver->architecture);
5111 W_ERROR_HAVE_NO_MEMORY(r->architecture);
5113 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
5114 driver->architecture,
5115 driver->version,
5116 driver->driver_path,
5117 r->driver_path);
5119 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
5120 driver->architecture,
5121 driver->version,
5122 driver->data_file,
5123 r->data_file);
5125 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
5126 driver->architecture,
5127 driver->version,
5128 driver->config_file,
5129 r->config_file);
5131 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
5132 driver->architecture,
5133 driver->version,
5134 driver->help_file,
5135 r->help_file);
5137 FILL_DRIVER_STRING(mem_ctx,
5138 driver->monitor_name,
5139 r->monitor_name);
5141 FILL_DRIVER_STRING(mem_ctx,
5142 driver->default_datatype,
5143 r->default_datatype);
5145 result = string_array_from_driver_info(mem_ctx,
5146 driver->dependent_files,
5147 &r->dependent_files,
5148 cservername,
5149 driver->architecture,
5150 driver->version);
5151 if (!W_ERROR_IS_OK(result)) {
5152 return result;
5155 result = string_array_from_driver_info(mem_ctx,
5156 driver->previous_names,
5157 &r->previous_names,
5158 NULL, NULL, 0);
5159 if (!W_ERROR_IS_OK(result)) {
5160 return result;
5163 r->driver_date = driver->driver_date;
5164 r->driver_version = driver->driver_version;
5166 FILL_DRIVER_STRING(mem_ctx,
5167 driver->manufacturer_name,
5168 r->manufacturer_name);
5169 FILL_DRIVER_STRING(mem_ctx,
5170 driver->manufacturer_url,
5171 r->manufacturer_url);
5172 FILL_DRIVER_STRING(mem_ctx,
5173 driver->hardware_id,
5174 r->hardware_id);
5175 FILL_DRIVER_STRING(mem_ctx,
5176 driver->provider,
5177 r->provider);
5179 return WERR_OK;
5182 /********************************************************************
5183 * fill a spoolss_DriverInfo8 struct
5184 ********************************************************************/
5186 static WERROR fill_printer_driver_info8(TALLOC_CTX *mem_ctx,
5187 struct spoolss_DriverInfo8 *r,
5188 const struct spoolss_DriverInfo8 *driver,
5189 const char *servername)
5191 const char *cservername = canon_servername(servername);
5192 WERROR result;
5194 r->version = driver->version;
5196 r->driver_name = talloc_strdup(mem_ctx, driver->driver_name);
5197 W_ERROR_HAVE_NO_MEMORY(r->driver_name);
5198 r->architecture = talloc_strdup(mem_ctx, driver->architecture);
5199 W_ERROR_HAVE_NO_MEMORY(r->architecture);
5201 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
5202 driver->architecture,
5203 driver->version,
5204 driver->driver_path,
5205 r->driver_path);
5207 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
5208 driver->architecture,
5209 driver->version,
5210 driver->data_file,
5211 r->data_file);
5213 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
5214 driver->architecture,
5215 driver->version,
5216 driver->config_file,
5217 r->config_file);
5219 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
5220 driver->architecture,
5221 driver->version,
5222 driver->help_file,
5223 r->help_file);
5225 FILL_DRIVER_STRING(mem_ctx,
5226 driver->monitor_name,
5227 r->monitor_name);
5229 FILL_DRIVER_STRING(mem_ctx,
5230 driver->default_datatype,
5231 r->default_datatype);
5233 result = string_array_from_driver_info(mem_ctx,
5234 driver->dependent_files,
5235 &r->dependent_files,
5236 cservername,
5237 driver->architecture,
5238 driver->version);
5239 if (!W_ERROR_IS_OK(result)) {
5240 return result;
5243 result = string_array_from_driver_info(mem_ctx,
5244 driver->previous_names,
5245 &r->previous_names,
5246 NULL, NULL, 0);
5247 if (!W_ERROR_IS_OK(result)) {
5248 return result;
5251 r->driver_date = driver->driver_date;
5252 r->driver_version = driver->driver_version;
5254 FILL_DRIVER_STRING(mem_ctx,
5255 driver->manufacturer_name,
5256 r->manufacturer_name);
5257 FILL_DRIVER_STRING(mem_ctx,
5258 driver->manufacturer_url,
5259 r->manufacturer_url);
5260 FILL_DRIVER_STRING(mem_ctx,
5261 driver->hardware_id,
5262 r->hardware_id);
5263 FILL_DRIVER_STRING(mem_ctx,
5264 driver->provider,
5265 r->provider);
5267 FILL_DRIVER_STRING(mem_ctx,
5268 driver->print_processor,
5269 r->print_processor);
5270 FILL_DRIVER_STRING(mem_ctx,
5271 driver->vendor_setup,
5272 r->vendor_setup);
5274 result = string_array_from_driver_info(mem_ctx,
5275 driver->color_profiles,
5276 &r->color_profiles,
5277 NULL, NULL, 0);
5278 if (!W_ERROR_IS_OK(result)) {
5279 return result;
5282 FILL_DRIVER_STRING(mem_ctx,
5283 driver->inf_path,
5284 r->inf_path);
5286 r->printer_driver_attributes = driver->printer_driver_attributes;
5288 result = string_array_from_driver_info(mem_ctx,
5289 driver->core_driver_dependencies,
5290 &r->core_driver_dependencies,
5291 NULL, NULL, 0);
5292 if (!W_ERROR_IS_OK(result)) {
5293 return result;
5296 r->min_inbox_driver_ver_date = driver->min_inbox_driver_ver_date;
5297 r->min_inbox_driver_ver_version = driver->min_inbox_driver_ver_version;
5299 return WERR_OK;
5302 #if 0 /* disabled until marshalling issues are resolved - gd */
5303 /********************************************************************
5304 ********************************************************************/
5306 static WERROR fill_spoolss_DriverFileInfo(TALLOC_CTX *mem_ctx,
5307 struct spoolss_DriverFileInfo *r,
5308 const char *cservername,
5309 const char *file_name,
5310 enum spoolss_DriverFileType file_type,
5311 uint32_t file_version)
5313 r->file_name = talloc_asprintf(mem_ctx, "\\\\%s%s",
5314 cservername, file_name);
5315 W_ERROR_HAVE_NO_MEMORY(r->file_name);
5316 r->file_type = file_type;
5317 r->file_version = file_version;
5319 return WERR_OK;
5322 /********************************************************************
5323 ********************************************************************/
5325 static WERROR spoolss_DriverFileInfo_from_driver(TALLOC_CTX *mem_ctx,
5326 const struct spoolss_DriverInfo8 *driver,
5327 const char *cservername,
5328 struct spoolss_DriverFileInfo **info_p,
5329 uint32_t *count_p)
5331 struct spoolss_DriverFileInfo *info = NULL;
5332 uint32_t count = 0;
5333 WERROR result;
5334 uint32_t i;
5336 *info_p = NULL;
5337 *count_p = 0;
5339 if (strlen(driver->driver_path)) {
5340 info = talloc_realloc(mem_ctx, info,
5341 struct spoolss_DriverFileInfo,
5342 count + 1);
5343 W_ERROR_HAVE_NO_MEMORY(info);
5344 result = fill_spoolss_DriverFileInfo(info,
5345 &info[count],
5346 cservername,
5347 driver->driver_path,
5348 SPOOLSS_DRIVER_FILE_TYPE_RENDERING,
5350 W_ERROR_NOT_OK_RETURN(result);
5351 count++;
5354 if (strlen(driver->config_file)) {
5355 info = talloc_realloc(mem_ctx, info,
5356 struct spoolss_DriverFileInfo,
5357 count + 1);
5358 W_ERROR_HAVE_NO_MEMORY(info);
5359 result = fill_spoolss_DriverFileInfo(info,
5360 &info[count],
5361 cservername,
5362 driver->config_file,
5363 SPOOLSS_DRIVER_FILE_TYPE_CONFIGURATION,
5365 W_ERROR_NOT_OK_RETURN(result);
5366 count++;
5369 if (strlen(driver->data_file)) {
5370 info = talloc_realloc(mem_ctx, info,
5371 struct spoolss_DriverFileInfo,
5372 count + 1);
5373 W_ERROR_HAVE_NO_MEMORY(info);
5374 result = fill_spoolss_DriverFileInfo(info,
5375 &info[count],
5376 cservername,
5377 driver->data_file,
5378 SPOOLSS_DRIVER_FILE_TYPE_DATA,
5380 W_ERROR_NOT_OK_RETURN(result);
5381 count++;
5384 if (strlen(driver->help_file)) {
5385 info = talloc_realloc(mem_ctx, info,
5386 struct spoolss_DriverFileInfo,
5387 count + 1);
5388 W_ERROR_HAVE_NO_MEMORY(info);
5389 result = fill_spoolss_DriverFileInfo(info,
5390 &info[count],
5391 cservername,
5392 driver->help_file,
5393 SPOOLSS_DRIVER_FILE_TYPE_HELP,
5395 W_ERROR_NOT_OK_RETURN(result);
5396 count++;
5399 for (i=0; driver->dependent_files[i] && driver->dependent_files[i][0] != '\0'; i++) {
5400 info = talloc_realloc(mem_ctx, info,
5401 struct spoolss_DriverFileInfo,
5402 count + 1);
5403 W_ERROR_HAVE_NO_MEMORY(info);
5404 result = fill_spoolss_DriverFileInfo(info,
5405 &info[count],
5406 cservername,
5407 driver->dependent_files[i],
5408 SPOOLSS_DRIVER_FILE_TYPE_OTHER,
5410 W_ERROR_NOT_OK_RETURN(result);
5411 count++;
5414 *info_p = info;
5415 *count_p = count;
5417 return WERR_OK;
5420 /********************************************************************
5421 * fill a spoolss_DriverInfo101 struct
5422 ********************************************************************/
5424 static WERROR fill_printer_driver_info101(TALLOC_CTX *mem_ctx,
5425 struct spoolss_DriverInfo101 *r,
5426 const struct spoolss_DriverInfo8 *driver,
5427 const char *servername)
5429 const char *cservername = canon_servername(servername);
5430 WERROR result;
5432 r->version = driver->version;
5434 r->driver_name = talloc_strdup(mem_ctx, driver->driver_name);
5435 W_ERROR_HAVE_NO_MEMORY(r->driver_name);
5436 r->architecture = talloc_strdup(mem_ctx, driver->architecture);
5437 W_ERROR_HAVE_NO_MEMORY(r->architecture);
5439 result = spoolss_DriverFileInfo_from_driver(mem_ctx, driver,
5440 cservername,
5441 &r->file_info,
5442 &r->file_count);
5443 if (!W_ERROR_IS_OK(result)) {
5444 return result;
5447 FILL_DRIVER_STRING(mem_ctx,
5448 driver->monitor_name,
5449 r->monitor_name);
5451 FILL_DRIVER_STRING(mem_ctx,
5452 driver->default_datatype,
5453 r->default_datatype);
5455 result = string_array_from_driver_info(mem_ctx,
5456 driver->previous_names,
5457 &r->previous_names,
5458 NULL, NULL, 0);
5459 if (!W_ERROR_IS_OK(result)) {
5460 return result;
5463 r->driver_date = driver->driver_date;
5464 r->driver_version = driver->driver_version;
5466 FILL_DRIVER_STRING(mem_ctx,
5467 driver->manufacturer_name,
5468 r->manufacturer_name);
5469 FILL_DRIVER_STRING(mem_ctx,
5470 driver->manufacturer_url,
5471 r->manufacturer_url);
5472 FILL_DRIVER_STRING(mem_ctx,
5473 driver->hardware_id,
5474 r->hardware_id);
5475 FILL_DRIVER_STRING(mem_ctx,
5476 driver->provider,
5477 r->provider);
5479 return WERR_OK;
5481 #endif
5482 /********************************************************************
5483 ********************************************************************/
5485 static WERROR construct_printer_driver_info_level(TALLOC_CTX *mem_ctx,
5486 const struct auth_session_info *session_info,
5487 struct messaging_context *msg_ctx,
5488 uint32_t level,
5489 union spoolss_DriverInfo *r,
5490 int snum,
5491 const char *servername,
5492 const char *architecture,
5493 uint32_t version)
5495 struct spoolss_PrinterInfo2 *pinfo2 = NULL;
5496 struct spoolss_DriverInfo8 *driver;
5497 WERROR result;
5498 struct dcerpc_binding_handle *b;
5499 TALLOC_CTX *tmp_ctx = NULL;
5501 if (level == 101) {
5502 return WERR_UNKNOWN_LEVEL;
5505 tmp_ctx = talloc_new(mem_ctx);
5506 if (!tmp_ctx) {
5507 return WERR_NOMEM;
5510 result = winreg_printer_binding_handle(tmp_ctx,
5511 session_info,
5512 msg_ctx,
5513 &b);
5514 if (!W_ERROR_IS_OK(result)) {
5515 goto done;
5518 result = winreg_get_printer(tmp_ctx, b,
5519 lp_const_servicename(snum),
5520 &pinfo2);
5522 DEBUG(8,("construct_printer_driver_info_level: status: %s\n",
5523 win_errstr(result)));
5525 if (!W_ERROR_IS_OK(result)) {
5526 result = WERR_INVALID_PRINTER_NAME;
5527 goto done;
5530 result = winreg_get_driver(tmp_ctx, b,
5531 architecture,
5532 pinfo2->drivername, version, &driver);
5534 DEBUG(8,("construct_printer_driver_info_level: status: %s\n",
5535 win_errstr(result)));
5537 if (!W_ERROR_IS_OK(result)) {
5539 * Is this a W2k client ?
5542 if (version < 3) {
5543 result = WERR_UNKNOWN_PRINTER_DRIVER;
5544 goto done;
5547 /* Yes - try again with a WinNT driver. */
5548 version = 2;
5549 result = winreg_get_driver(tmp_ctx, b,
5550 architecture,
5551 pinfo2->drivername,
5552 version, &driver);
5553 DEBUG(8,("construct_printer_driver_level: status: %s\n",
5554 win_errstr(result)));
5555 if (!W_ERROR_IS_OK(result)) {
5556 result = WERR_UNKNOWN_PRINTER_DRIVER;
5557 goto done;
5561 /* these are allocated on mem_ctx and not tmp_ctx because they are
5562 * the 'return value' and need to utlive this call */
5563 switch (level) {
5564 case 1:
5565 result = fill_printer_driver_info1(mem_ctx, &r->info1, driver, servername);
5566 break;
5567 case 2:
5568 result = fill_printer_driver_info2(mem_ctx, &r->info2, driver, servername);
5569 break;
5570 case 3:
5571 result = fill_printer_driver_info3(mem_ctx, &r->info3, driver, servername);
5572 break;
5573 case 4:
5574 result = fill_printer_driver_info4(mem_ctx, &r->info4, driver, servername);
5575 break;
5576 case 5:
5577 result = fill_printer_driver_info5(mem_ctx, &r->info5, driver, servername);
5578 break;
5579 case 6:
5580 result = fill_printer_driver_info6(mem_ctx, &r->info6, driver, servername);
5581 break;
5582 case 8:
5583 result = fill_printer_driver_info8(mem_ctx, &r->info8, driver, servername);
5584 break;
5585 #if 0 /* disabled until marshalling issues are resolved - gd */
5586 case 101:
5587 result = fill_printer_driver_info101(mem_ctx, &r->info101, driver, servername);
5588 break;
5589 #endif
5590 default:
5591 result = WERR_UNKNOWN_LEVEL;
5592 break;
5595 done:
5596 talloc_free(tmp_ctx);
5597 return result;
5600 /****************************************************************
5601 _spoolss_GetPrinterDriver2
5602 ****************************************************************/
5604 WERROR _spoolss_GetPrinterDriver2(struct pipes_struct *p,
5605 struct spoolss_GetPrinterDriver2 *r)
5607 struct printer_handle *printer;
5608 WERROR result;
5609 uint32_t version = r->in.client_major_version;
5611 int snum;
5613 /* that's an [in out] buffer */
5615 if (!r->in.buffer && (r->in.offered != 0)) {
5616 return WERR_INVALID_PARAM;
5619 DEBUG(4,("_spoolss_GetPrinterDriver2\n"));
5621 if (!(printer = find_printer_index_by_hnd(p, r->in.handle))) {
5622 DEBUG(0,("_spoolss_GetPrinterDriver2: invalid printer handle!\n"));
5623 return WERR_INVALID_PRINTER_NAME;
5626 *r->out.needed = 0;
5627 *r->out.server_major_version = 0;
5628 *r->out.server_minor_version = 0;
5630 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
5631 return WERR_BADFID;
5634 if (r->in.client_major_version == SPOOLSS_DRIVER_VERSION_2012) {
5635 DEBUG(3,("_spoolss_GetPrinterDriver2: v4 driver requested, "
5636 "downgrading to v3\n"));
5637 version = SPOOLSS_DRIVER_VERSION_200X;
5640 result = construct_printer_driver_info_level(p->mem_ctx,
5641 get_session_info_system(),
5642 p->msg_ctx,
5643 r->in.level, r->out.info,
5644 snum, printer->servername,
5645 r->in.architecture,
5646 version);
5647 if (!W_ERROR_IS_OK(result)) {
5648 TALLOC_FREE(r->out.info);
5649 return result;
5652 *r->out.needed = SPOOLSS_BUFFER_UNION(spoolss_DriverInfo,
5653 r->out.info, r->in.level);
5654 r->out.info = SPOOLSS_BUFFER_OK(r->out.info, NULL);
5656 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
5660 /****************************************************************
5661 _spoolss_StartPagePrinter
5662 ****************************************************************/
5664 WERROR _spoolss_StartPagePrinter(struct pipes_struct *p,
5665 struct spoolss_StartPagePrinter *r)
5667 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
5669 if (!Printer) {
5670 DEBUG(3,("_spoolss_StartPagePrinter: "
5671 "Error in startpageprinter printer handle\n"));
5672 return WERR_BADFID;
5675 Printer->page_started = true;
5676 return WERR_OK;
5679 /****************************************************************
5680 _spoolss_EndPagePrinter
5681 ****************************************************************/
5683 WERROR _spoolss_EndPagePrinter(struct pipes_struct *p,
5684 struct spoolss_EndPagePrinter *r)
5686 int snum;
5688 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
5690 if (!Printer) {
5691 DEBUG(2,("_spoolss_EndPagePrinter: Invalid handle (%s:%u:%u).\n",
5692 OUR_HANDLE(r->in.handle)));
5693 return WERR_BADFID;
5696 if (!get_printer_snum(p, r->in.handle, &snum, NULL))
5697 return WERR_BADFID;
5699 Printer->page_started = false;
5700 print_job_endpage(p->msg_ctx, snum, Printer->jobid);
5702 return WERR_OK;
5705 /****************************************************************
5706 _spoolss_StartDocPrinter
5707 ****************************************************************/
5709 WERROR _spoolss_StartDocPrinter(struct pipes_struct *p,
5710 struct spoolss_StartDocPrinter *r)
5712 struct spoolss_DocumentInfo1 *info_1;
5713 int snum;
5714 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
5715 WERROR werr;
5716 char *rhost;
5717 int rc;
5719 if (!Printer) {
5720 DEBUG(2,("_spoolss_StartDocPrinter: "
5721 "Invalid handle (%s:%u:%u)\n",
5722 OUR_HANDLE(r->in.handle)));
5723 return WERR_BADFID;
5726 if (Printer->jobid) {
5727 DEBUG(2, ("_spoolss_StartDocPrinter: "
5728 "StartDocPrinter called twice! "
5729 "(existing jobid = %d)\n", Printer->jobid));
5730 return WERR_INVALID_HANDLE;
5733 if (r->in.level != 1) {
5734 return WERR_UNKNOWN_LEVEL;
5737 info_1 = r->in.info.info1;
5740 * a nice thing with NT is it doesn't listen to what you tell it.
5741 * when asked to send _only_ RAW datas, it tries to send datas
5742 * in EMF format.
5744 * So I add checks like in NT Server ...
5747 if (info_1->datatype) {
5748 if (strcmp(info_1->datatype, "RAW") != 0) {
5749 *r->out.job_id = 0;
5750 return WERR_INVALID_DATATYPE;
5754 /* get the share number of the printer */
5755 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
5756 return WERR_BADFID;
5759 rc = get_remote_hostname(p->remote_address,
5760 &rhost,
5761 p->mem_ctx);
5762 if (rc < 0) {
5763 return WERR_NOMEM;
5765 if (strequal(rhost,"UNKNOWN")) {
5766 rhost = tsocket_address_inet_addr_string(p->remote_address,
5767 p->mem_ctx);
5768 if (rhost == NULL) {
5769 return WERR_NOMEM;
5773 werr = print_job_start(p->session_info,
5774 p->msg_ctx,
5775 rhost,
5776 snum,
5777 info_1->document_name,
5778 info_1->output_file,
5779 Printer->devmode,
5780 &Printer->jobid);
5782 /* An error occured in print_job_start() so return an appropriate
5783 NT error code. */
5785 if (!W_ERROR_IS_OK(werr)) {
5786 return werr;
5789 Printer->document_started = true;
5790 *r->out.job_id = Printer->jobid;
5792 return WERR_OK;
5795 /****************************************************************
5796 _spoolss_EndDocPrinter
5797 ****************************************************************/
5799 WERROR _spoolss_EndDocPrinter(struct pipes_struct *p,
5800 struct spoolss_EndDocPrinter *r)
5802 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
5803 NTSTATUS status;
5804 int snum;
5806 if (!Printer) {
5807 DEBUG(2,("_spoolss_EndDocPrinter: Invalid handle (%s:%u:%u)\n",
5808 OUR_HANDLE(r->in.handle)));
5809 return WERR_BADFID;
5812 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
5813 return WERR_BADFID;
5816 Printer->document_started = false;
5817 status = print_job_end(p->msg_ctx, snum, Printer->jobid, NORMAL_CLOSE);
5818 if (!NT_STATUS_IS_OK(status)) {
5819 DEBUG(2, ("_spoolss_EndDocPrinter: "
5820 "print_job_end failed [%s]\n",
5821 nt_errstr(status)));
5824 Printer->jobid = 0;
5825 return ntstatus_to_werror(status);
5828 /****************************************************************
5829 _spoolss_WritePrinter
5830 ****************************************************************/
5832 WERROR _spoolss_WritePrinter(struct pipes_struct *p,
5833 struct spoolss_WritePrinter *r)
5835 ssize_t buffer_written;
5836 int snum;
5837 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
5839 if (!Printer) {
5840 DEBUG(2,("_spoolss_WritePrinter: Invalid handle (%s:%u:%u)\n",
5841 OUR_HANDLE(r->in.handle)));
5842 *r->out.num_written = r->in._data_size;
5843 return WERR_BADFID;
5846 if (!get_printer_snum(p, r->in.handle, &snum, NULL))
5847 return WERR_BADFID;
5849 /* print_job_write takes care of checking for PJOB_SMBD_SPOOLING */
5850 buffer_written = print_job_write(server_event_context(),p->msg_ctx,
5851 snum, Printer->jobid,
5852 (const char *)r->in.data.data,
5853 (size_t)r->in._data_size);
5854 if (buffer_written == (ssize_t)-1) {
5855 *r->out.num_written = 0;
5856 if (errno == ENOSPC)
5857 return WERR_NO_SPOOL_SPACE;
5858 else
5859 return WERR_ACCESS_DENIED;
5862 *r->out.num_written = r->in._data_size;
5864 return WERR_OK;
5867 /********************************************************************
5868 * api_spoolss_getprinter
5869 * called from the spoolss dispatcher
5871 ********************************************************************/
5873 static WERROR control_printer(struct policy_handle *handle, uint32_t command,
5874 struct pipes_struct *p)
5876 const struct auth_session_info *session_info = p->session_info;
5877 int snum;
5878 WERROR errcode = WERR_BADFUNC;
5879 struct printer_handle *Printer = find_printer_index_by_hnd(p, handle);
5881 if (!Printer) {
5882 DEBUG(2,("control_printer: Invalid handle (%s:%u:%u)\n",
5883 OUR_HANDLE(handle)));
5884 return WERR_BADFID;
5887 if (!get_printer_snum(p, handle, &snum, NULL))
5888 return WERR_BADFID;
5890 switch (command) {
5891 case SPOOLSS_PRINTER_CONTROL_PAUSE:
5892 errcode = print_queue_pause(session_info, p->msg_ctx, snum);
5893 break;
5894 case SPOOLSS_PRINTER_CONTROL_RESUME:
5895 case SPOOLSS_PRINTER_CONTROL_UNPAUSE:
5896 errcode = print_queue_resume(session_info, p->msg_ctx, snum);
5897 break;
5898 case SPOOLSS_PRINTER_CONTROL_PURGE:
5899 errcode = print_queue_purge(session_info, p->msg_ctx, snum);
5900 break;
5901 default:
5902 return WERR_UNKNOWN_LEVEL;
5905 return errcode;
5909 /****************************************************************
5910 _spoolss_AbortPrinter
5911 * From MSDN: "Deletes printer's spool file if printer is configured
5912 * for spooling"
5913 ****************************************************************/
5915 WERROR _spoolss_AbortPrinter(struct pipes_struct *p,
5916 struct spoolss_AbortPrinter *r)
5918 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
5919 int snum;
5920 WERROR errcode = WERR_OK;
5922 if (!Printer) {
5923 DEBUG(2,("_spoolss_AbortPrinter: Invalid handle (%s:%u:%u)\n",
5924 OUR_HANDLE(r->in.handle)));
5925 return WERR_BADFID;
5928 if (!get_printer_snum(p, r->in.handle, &snum, NULL))
5929 return WERR_BADFID;
5931 if (!Printer->document_started) {
5932 return WERR_SPL_NO_STARTDOC;
5935 errcode = print_job_delete(p->session_info,
5936 p->msg_ctx,
5937 snum,
5938 Printer->jobid);
5940 return errcode;
5943 /********************************************************************
5944 * called by spoolss_api_setprinter
5945 * when updating a printer description
5946 ********************************************************************/
5948 static WERROR update_printer_sec(struct policy_handle *handle,
5949 struct pipes_struct *p,
5950 struct sec_desc_buf *secdesc_ctr)
5952 struct spoolss_security_descriptor *new_secdesc = NULL;
5953 struct spoolss_security_descriptor *old_secdesc = NULL;
5954 const char *printer;
5955 WERROR result;
5956 int snum;
5957 struct printer_handle *Printer = find_printer_index_by_hnd(p, handle);
5958 struct dcerpc_binding_handle *b;
5959 TALLOC_CTX *tmp_ctx = NULL;
5961 if (!Printer || !get_printer_snum(p, handle, &snum, NULL)) {
5962 DEBUG(2,("update_printer_sec: Invalid handle (%s:%u:%u)\n",
5963 OUR_HANDLE(handle)));
5965 result = WERR_BADFID;
5966 goto done;
5969 if (secdesc_ctr == NULL) {
5970 DEBUG(10,("update_printer_sec: secdesc_ctr is NULL !\n"));
5971 result = WERR_INVALID_PARAM;
5972 goto done;
5974 printer = lp_const_servicename(snum);
5976 /* Check the user has permissions to change the security
5977 descriptor. By experimentation with two NT machines, the user
5978 requires Full Access to the printer to change security
5979 information. */
5981 if ( Printer->access_granted != PRINTER_ACCESS_ADMINISTER ) {
5982 DEBUG(4,("update_printer_sec: updated denied by printer permissions\n"));
5983 result = WERR_ACCESS_DENIED;
5984 goto done;
5987 tmp_ctx = talloc_new(p->mem_ctx);
5988 if (!tmp_ctx) {
5989 return WERR_NOMEM;
5992 result = winreg_printer_binding_handle(tmp_ctx,
5993 get_session_info_system(),
5994 p->msg_ctx,
5995 &b);
5996 if (!W_ERROR_IS_OK(result)) {
5997 goto done;
6000 /* NT seems to like setting the security descriptor even though
6001 nothing may have actually changed. */
6002 result = winreg_get_printer_secdesc(tmp_ctx, b,
6003 printer,
6004 &old_secdesc);
6005 if (!W_ERROR_IS_OK(result)) {
6006 DEBUG(2,("update_printer_sec: winreg_get_printer_secdesc_internal() failed\n"));
6007 result = WERR_BADFID;
6008 goto done;
6011 if (DEBUGLEVEL >= 10) {
6012 struct security_acl *the_acl;
6013 int i;
6015 the_acl = old_secdesc->dacl;
6016 DEBUG(10, ("old_secdesc_ctr for %s has %d aces:\n",
6017 printer, the_acl->num_aces));
6019 for (i = 0; i < the_acl->num_aces; i++) {
6020 DEBUG(10, ("%s 0x%08x\n", sid_string_dbg(
6021 &the_acl->aces[i].trustee),
6022 the_acl->aces[i].access_mask));
6025 the_acl = secdesc_ctr->sd->dacl;
6027 if (the_acl) {
6028 DEBUG(10, ("secdesc_ctr for %s has %d aces:\n",
6029 printer, the_acl->num_aces));
6031 for (i = 0; i < the_acl->num_aces; i++) {
6032 DEBUG(10, ("%s 0x%08x\n", sid_string_dbg(
6033 &the_acl->aces[i].trustee),
6034 the_acl->aces[i].access_mask));
6036 } else {
6037 DEBUG(10, ("dacl for secdesc_ctr is NULL\n"));
6041 new_secdesc = sec_desc_merge(tmp_ctx, secdesc_ctr->sd, old_secdesc);
6042 if (new_secdesc == NULL) {
6043 result = WERR_NOMEM;
6044 goto done;
6047 if (security_descriptor_equal(new_secdesc, old_secdesc)) {
6048 result = WERR_OK;
6049 goto done;
6052 result = winreg_set_printer_secdesc(tmp_ctx, b,
6053 printer,
6054 new_secdesc);
6056 done:
6057 talloc_free(tmp_ctx);
6058 return result;
6061 /********************************************************************
6062 Canonicalize printer info from a client
6063 ********************************************************************/
6065 static bool check_printer_ok(TALLOC_CTX *mem_ctx,
6066 struct spoolss_SetPrinterInfo2 *info2,
6067 int snum)
6069 fstring printername;
6070 const char *p;
6072 DEBUG(5,("check_printer_ok: servername=%s printername=%s sharename=%s "
6073 "portname=%s drivername=%s comment=%s location=%s\n",
6074 info2->servername, info2->printername, info2->sharename,
6075 info2->portname, info2->drivername, info2->comment,
6076 info2->location));
6078 /* we force some elements to "correct" values */
6079 info2->servername = talloc_asprintf(mem_ctx, "\\\\%s", lp_netbios_name());
6080 if (info2->servername == NULL) {
6081 return false;
6083 info2->sharename = talloc_strdup(mem_ctx, lp_const_servicename(snum));
6084 if (info2->sharename == NULL) {
6085 return false;
6088 /* check to see if we allow printername != sharename */
6089 if (lp_force_printername(snum)) {
6090 info2->printername = talloc_asprintf(mem_ctx, "\\\\%s\\%s",
6091 lp_netbios_name(), info2->sharename);
6092 } else {
6093 /* make sure printername is in \\server\printername format */
6094 fstrcpy(printername, info2->printername);
6095 p = printername;
6096 if ( printername[0] == '\\' && printername[1] == '\\' ) {
6097 if ( (p = strchr_m( &printername[2], '\\' )) != NULL )
6098 p++;
6101 info2->printername = talloc_asprintf(mem_ctx, "\\\\%s\\%s",
6102 lp_netbios_name(), p);
6104 if (info2->printername == NULL) {
6105 return false;
6108 info2->attributes |= PRINTER_ATTRIBUTE_SAMBA;
6109 info2->attributes &= ~PRINTER_ATTRIBUTE_NOT_SAMBA;
6111 return true;
6114 /****************************************************************************
6115 ****************************************************************************/
6117 static WERROR add_port_hook(TALLOC_CTX *ctx, struct security_token *token, const char *portname, const char *uri)
6119 char *cmd = lp_addport_cmd(talloc_tos());
6120 char *command = NULL;
6121 int ret;
6122 bool is_print_op = false;
6124 if ( !*cmd ) {
6125 return WERR_ACCESS_DENIED;
6128 command = talloc_asprintf(ctx,
6129 "%s \"%s\" \"%s\"", cmd, portname, uri );
6130 if (!command) {
6131 return WERR_NOMEM;
6134 if ( token )
6135 is_print_op = security_token_has_privilege(token, SEC_PRIV_PRINT_OPERATOR);
6137 DEBUG(10,("Running [%s]\n", command));
6139 /********* BEGIN SePrintOperatorPrivilege **********/
6141 if ( is_print_op )
6142 become_root();
6144 ret = smbrun(command, NULL);
6146 if ( is_print_op )
6147 unbecome_root();
6149 /********* END SePrintOperatorPrivilege **********/
6151 DEBUGADD(10,("returned [%d]\n", ret));
6153 TALLOC_FREE(command);
6155 if ( ret != 0 ) {
6156 return WERR_ACCESS_DENIED;
6159 return WERR_OK;
6162 /****************************************************************************
6163 ****************************************************************************/
6165 static bool spoolss_conn_snum_used(struct smbd_server_connection *sconn,
6166 int snum)
6169 * As we do not know if we are embedded in the file server process
6170 * or not, we have to pretend that all shares are in use.
6172 return true;
6175 static bool add_printer_hook(TALLOC_CTX *ctx, struct security_token *token,
6176 struct spoolss_SetPrinterInfo2 *info2,
6177 const char *remote_machine,
6178 struct messaging_context *msg_ctx)
6180 char *cmd = lp_addprinter_cmd(talloc_tos());
6181 char **qlines;
6182 char *command = NULL;
6183 int numlines;
6184 int ret;
6185 int fd;
6186 bool is_print_op = false;
6188 if (!remote_machine) {
6189 return false;
6192 command = talloc_asprintf(ctx,
6193 "%s \"%s\" \"%s\" \"%s\" \"%s\" \"%s\" \"%s\" \"%s\"",
6194 cmd, info2->printername, info2->sharename,
6195 info2->portname, info2->drivername,
6196 info2->location, info2->comment, remote_machine);
6197 if (!command) {
6198 return false;
6201 if ( token )
6202 is_print_op = security_token_has_privilege(token, SEC_PRIV_PRINT_OPERATOR);
6204 DEBUG(10,("Running [%s]\n", command));
6206 /********* BEGIN SePrintOperatorPrivilege **********/
6208 if ( is_print_op )
6209 become_root();
6211 if ( (ret = smbrun(command, &fd)) == 0 ) {
6212 /* Tell everyone we updated smb.conf. */
6213 message_send_all(msg_ctx, MSG_SMB_CONF_UPDATED, NULL, 0, NULL);
6216 if ( is_print_op )
6217 unbecome_root();
6219 /********* END SePrintOperatorPrivilege **********/
6221 DEBUGADD(10,("returned [%d]\n", ret));
6223 TALLOC_FREE(command);
6225 if ( ret != 0 ) {
6226 if (fd != -1)
6227 close(fd);
6228 return false;
6231 /* reload our services immediately */
6232 become_root();
6233 reload_services(NULL, spoolss_conn_snum_used, false);
6234 unbecome_root();
6236 numlines = 0;
6237 /* Get lines and convert them back to dos-codepage */
6238 qlines = fd_lines_load(fd, &numlines, 0, NULL);
6239 DEBUGADD(10,("Lines returned = [%d]\n", numlines));
6240 close(fd);
6242 /* Set the portname to what the script says the portname should be. */
6243 /* but don't require anything to be return from the script exit a good error code */
6245 if (numlines) {
6246 /* Set the portname to what the script says the portname should be. */
6247 info2->portname = talloc_strdup(ctx, qlines[0]);
6248 DEBUGADD(6,("Line[0] = [%s]\n", qlines[0]));
6251 TALLOC_FREE(qlines);
6252 return true;
6255 static WERROR update_dsspooler(TALLOC_CTX *mem_ctx,
6256 const struct auth_session_info *session_info,
6257 struct messaging_context *msg_ctx,
6258 int snum,
6259 struct spoolss_SetPrinterInfo2 *printer,
6260 struct spoolss_PrinterInfo2 *old_printer)
6262 bool force_update = (old_printer == NULL);
6263 const char *dnsdomname;
6264 const char *longname;
6265 const char *uncname;
6266 const char *spooling;
6267 DATA_BLOB buffer;
6268 WERROR result = WERR_OK;
6269 struct dcerpc_binding_handle *b;
6270 TALLOC_CTX *tmp_ctx;
6271 bool ok;
6273 tmp_ctx = talloc_new(mem_ctx);
6274 if (!tmp_ctx) {
6275 return WERR_NOMEM;
6278 result = winreg_printer_binding_handle(tmp_ctx,
6279 session_info,
6280 msg_ctx,
6281 &b);
6282 if (!W_ERROR_IS_OK(result)) {
6283 goto done;
6286 if (printer->drivername != NULL &&
6287 (force_update ||
6288 !strequal(printer->drivername, old_printer->drivername))) {
6289 ok = push_reg_sz(tmp_ctx, &buffer, printer->drivername);
6290 if (!ok) {
6291 DEBUG(0, ("%s data corrupted\n", SPOOL_REG_DRIVERNAME));
6292 result = WERR_INVALID_DATA;
6293 goto done;
6295 result = winreg_set_printer_dataex(tmp_ctx, b,
6296 printer->sharename,
6297 SPOOL_DSSPOOLER_KEY,
6298 SPOOL_REG_DRIVERNAME,
6299 REG_SZ,
6300 buffer.data,
6301 buffer.length);
6302 if (!W_ERROR_IS_OK(result)) {
6303 DEBUG(0, ("Failed to set %s\n", SPOOL_REG_DRIVERNAME));
6304 goto done;
6307 if (!force_update) {
6308 DEBUG(10,("update_printer: changing driver [%s]! Sending event!\n",
6309 printer->drivername));
6311 notify_printer_driver(server_event_context(), msg_ctx,
6312 snum, printer->drivername ?
6313 printer->drivername : "");
6317 if (printer->comment != NULL &&
6318 (force_update ||
6319 !strequal(printer->comment, old_printer->comment))) {
6320 ok = push_reg_sz(tmp_ctx, &buffer, printer->comment);
6321 if (!ok) {
6322 DEBUG(0, ("comment data corrupted\n"));
6323 result = WERR_INVALID_DATA;
6324 goto done;
6326 result = winreg_set_printer_dataex(tmp_ctx, b,
6327 printer->sharename,
6328 SPOOL_DSSPOOLER_KEY,
6329 SPOOL_REG_DESCRIPTION,
6330 REG_SZ,
6331 buffer.data,
6332 buffer.length);
6333 if (!W_ERROR_IS_OK(result)) {
6334 DEBUG(0, ("Failed to set %s\n", SPOOL_REG_DESCRIPTION));
6335 goto done;
6338 if (!force_update) {
6339 notify_printer_comment(server_event_context(), msg_ctx,
6340 snum, printer->comment ?
6341 printer->comment : "");
6345 if (printer->sharename != NULL &&
6346 (force_update ||
6347 !strequal(printer->sharename, old_printer->sharename))) {
6348 ok = push_reg_sz(tmp_ctx, &buffer, printer->sharename);
6349 if (!ok) {
6350 DEBUG(0, ("sharename data corrupted\n"));
6351 result = WERR_INVALID_DATA;
6352 goto done;
6354 result = winreg_set_printer_dataex(tmp_ctx, b,
6355 printer->sharename,
6356 SPOOL_DSSPOOLER_KEY,
6357 SPOOL_REG_PRINTSHARENAME,
6358 REG_SZ,
6359 buffer.data,
6360 buffer.length);
6361 if (!W_ERROR_IS_OK(result)) {
6362 DEBUG(0, ("Failed to set %s\n", SPOOL_REG_PRINTSHARENAME));
6363 goto done;
6366 if (!force_update) {
6367 notify_printer_sharename(server_event_context(),
6368 msg_ctx,
6369 snum, printer->sharename ?
6370 printer->sharename : "");
6374 if (printer->printername != NULL &&
6375 (force_update ||
6376 !strequal(printer->printername, old_printer->printername))) {
6377 const char *p;
6379 p = strrchr(printer->printername, '\\' );
6380 if (p != NULL) {
6381 p++;
6382 } else {
6383 p = printer->printername;
6386 ok = push_reg_sz(tmp_ctx, &buffer, p);
6387 if (!ok) {
6388 DEBUG(0, ("printername data corrupted\n"));
6389 result = WERR_INVALID_DATA;
6390 goto done;
6392 result = winreg_set_printer_dataex(tmp_ctx, b,
6393 printer->sharename,
6394 SPOOL_DSSPOOLER_KEY,
6395 SPOOL_REG_PRINTERNAME,
6396 REG_SZ,
6397 buffer.data,
6398 buffer.length);
6399 if (!W_ERROR_IS_OK(result)) {
6400 DEBUG(0, ("Failed to set %s\n", SPOOL_REG_PRINTSHARENAME));
6401 goto done;
6404 if (!force_update) {
6405 notify_printer_printername(server_event_context(),
6406 msg_ctx, snum, p ? p : "");
6410 if (printer->portname != NULL &&
6411 (force_update ||
6412 !strequal(printer->portname, old_printer->portname))) {
6413 ok = push_reg_sz(tmp_ctx, &buffer, printer->portname);
6414 if (!ok) {
6415 DEBUG(0, ("portname data corrupted\n"));
6416 result = WERR_INVALID_DATA;
6417 goto done;
6419 result = winreg_set_printer_dataex(tmp_ctx, b,
6420 printer->sharename,
6421 SPOOL_DSSPOOLER_KEY,
6422 SPOOL_REG_PORTNAME,
6423 REG_SZ,
6424 buffer.data,
6425 buffer.length);
6426 if (!W_ERROR_IS_OK(result)) {
6427 DEBUG(0, ("Failed to set %s\n", SPOOL_REG_PORTNAME));
6428 goto done;
6431 if (!force_update) {
6432 notify_printer_port(server_event_context(),
6433 msg_ctx, snum, printer->portname ?
6434 printer->portname : "");
6438 if (printer->location != NULL &&
6439 (force_update ||
6440 !strequal(printer->location, old_printer->location))) {
6441 ok = push_reg_sz(tmp_ctx, &buffer, printer->location);
6442 if (!ok) {
6443 DEBUG(0, ("location data corrupted\n"));
6444 result = WERR_INVALID_DATA;
6445 goto done;
6447 result = winreg_set_printer_dataex(tmp_ctx, b,
6448 printer->sharename,
6449 SPOOL_DSSPOOLER_KEY,
6450 SPOOL_REG_LOCATION,
6451 REG_SZ,
6452 buffer.data,
6453 buffer.length);
6454 if (!W_ERROR_IS_OK(result)) {
6455 DEBUG(0, ("Failed to set %s\n", SPOOL_REG_LOCATION));
6456 goto done;
6459 if (!force_update) {
6460 notify_printer_location(server_event_context(),
6461 msg_ctx, snum,
6462 printer->location ?
6463 printer->location : "");
6467 if (printer->sepfile != NULL &&
6468 (force_update ||
6469 !strequal(printer->sepfile, old_printer->sepfile))) {
6470 ok = push_reg_sz(tmp_ctx, &buffer, printer->sepfile);
6471 if (!ok) {
6472 DEBUG(0, ("sepfile data corrupted\n"));
6473 result = WERR_INVALID_DATA;
6474 goto done;
6476 result = winreg_set_printer_dataex(tmp_ctx, b,
6477 printer->sharename,
6478 SPOOL_DSSPOOLER_KEY,
6479 SPOOL_REG_PRINTSEPARATORFILE,
6480 REG_SZ,
6481 buffer.data,
6482 buffer.length);
6483 if (!W_ERROR_IS_OK(result)) {
6484 DEBUG(0, ("Failed to set %s\n", SPOOL_REG_PRINTSEPARATORFILE));
6485 goto done;
6488 if (!force_update) {
6489 notify_printer_sepfile(server_event_context(),
6490 msg_ctx, snum,
6491 printer->sepfile ?
6492 printer->sepfile : "");
6496 if (printer->starttime != 0 &&
6497 (force_update ||
6498 printer->starttime != old_printer->starttime)) {
6499 buffer = data_blob_talloc(tmp_ctx, NULL, 4);
6500 SIVAL(buffer.data, 0, printer->starttime);
6501 result = winreg_set_printer_dataex(tmp_ctx, b,
6502 printer->sharename,
6503 SPOOL_DSSPOOLER_KEY,
6504 SPOOL_REG_PRINTSTARTTIME,
6505 REG_DWORD,
6506 buffer.data,
6507 buffer.length);
6508 if (!W_ERROR_IS_OK(result)) {
6509 DEBUG(0, ("Failed to set %s\n", SPOOL_REG_PRINTSTARTTIME));
6510 goto done;
6514 if (printer->untiltime != 0 &&
6515 (force_update ||
6516 printer->untiltime != old_printer->untiltime)) {
6517 buffer = data_blob_talloc(tmp_ctx, NULL, 4);
6518 SIVAL(buffer.data, 0, printer->untiltime);
6519 result = winreg_set_printer_dataex(tmp_ctx, b,
6520 printer->sharename,
6521 SPOOL_DSSPOOLER_KEY,
6522 SPOOL_REG_PRINTENDTIME,
6523 REG_DWORD,
6524 buffer.data,
6525 buffer.length);
6526 if (!W_ERROR_IS_OK(result)) {
6527 DEBUG(0, ("Failed to set %s\n", SPOOL_REG_PRINTENDTIME));
6528 goto done;
6532 if (force_update || printer->priority != old_printer->priority) {
6533 buffer = data_blob_talloc(tmp_ctx, NULL, 4);
6534 SIVAL(buffer.data, 0, printer->priority);
6535 result = winreg_set_printer_dataex(tmp_ctx, b,
6536 printer->sharename,
6537 SPOOL_DSSPOOLER_KEY,
6538 SPOOL_REG_PRIORITY,
6539 REG_DWORD,
6540 buffer.data,
6541 buffer.length);
6542 if (!W_ERROR_IS_OK(result)) {
6543 DEBUG(0, ("Failed to set %s\n", SPOOL_REG_PRINTENDTIME));
6544 goto done;
6548 if (force_update || printer->attributes != old_printer->attributes) {
6549 buffer = data_blob_talloc(tmp_ctx, NULL, 4);
6550 SIVAL(buffer.data, 0, (printer->attributes &
6551 PRINTER_ATTRIBUTE_KEEPPRINTEDJOBS));
6552 result = winreg_set_printer_dataex(tmp_ctx, b,
6553 printer->sharename,
6554 SPOOL_DSSPOOLER_KEY,
6555 SPOOL_REG_PRINTKEEPPRINTEDJOBS,
6556 REG_DWORD,
6557 buffer.data,
6558 buffer.length);
6559 if (!W_ERROR_IS_OK(result)) {
6560 DEBUG(0, ("Failed to set %s\n", SPOOL_REG_PRINTENDTIME));
6561 goto done;
6564 switch (printer->attributes & 0x3) {
6565 case 0:
6566 spooling = SPOOL_REGVAL_PRINTWHILESPOOLING;
6567 break;
6568 case 1:
6569 spooling = SPOOL_REGVAL_PRINTAFTERSPOOLED;
6570 break;
6571 case 2:
6572 spooling = SPOOL_REGVAL_PRINTDIRECT;
6573 break;
6574 default:
6575 spooling = "unknown";
6577 ok = push_reg_sz(tmp_ctx, &buffer, spooling);
6578 if (!ok) {
6579 DEBUG(0, ("printSpooling data corrupted\n"));
6580 result = WERR_INVALID_DATA;
6581 goto done;
6583 winreg_set_printer_dataex(tmp_ctx, b,
6584 printer->sharename,
6585 SPOOL_DSSPOOLER_KEY,
6586 SPOOL_REG_PRINTSPOOLING,
6587 REG_SZ,
6588 buffer.data,
6589 buffer.length);
6592 ok = push_reg_sz(tmp_ctx, &buffer, lp_netbios_name());
6593 if (!ok) {
6594 DEBUG(0, ("shortServerName data corrupted\n"));
6595 result = WERR_INVALID_DATA;
6596 goto done;
6598 result = winreg_set_printer_dataex(tmp_ctx, b,
6599 printer->sharename,
6600 SPOOL_DSSPOOLER_KEY,
6601 SPOOL_REG_SHORTSERVERNAME,
6602 REG_SZ,
6603 buffer.data,
6604 buffer.length);
6605 if (!W_ERROR_IS_OK(result)) {
6606 DEBUG(0, ("Failed to set %s\n", SPOOL_REG_SHORTSERVERNAME));
6607 goto done;
6610 dnsdomname = get_mydnsfullname();
6611 if (dnsdomname != NULL && dnsdomname[0] != '\0') {
6612 longname = talloc_strdup(tmp_ctx, dnsdomname);
6613 } else {
6614 longname = talloc_strdup(tmp_ctx, lp_netbios_name());
6616 if (longname == NULL) {
6617 result = WERR_NOMEM;
6618 goto done;
6621 ok = push_reg_sz(tmp_ctx, &buffer, longname);
6622 if (!ok) {
6623 DEBUG(0, ("longname data corrupted\n"));
6624 result = WERR_INVALID_DATA;
6625 goto done;
6627 result = winreg_set_printer_dataex(tmp_ctx, b,
6628 printer->sharename,
6629 SPOOL_DSSPOOLER_KEY,
6630 SPOOL_REG_SERVERNAME,
6631 REG_SZ,
6632 buffer.data,
6633 buffer.length);
6634 if (!W_ERROR_IS_OK(result)) {
6635 DEBUG(0, ("Failed to set %s\n", SPOOL_REG_SERVERNAME));
6636 goto done;
6639 uncname = talloc_asprintf(tmp_ctx, "\\\\%s\\%s",
6640 lp_netbios_name(), printer->sharename);
6641 ok = push_reg_sz(tmp_ctx, &buffer, uncname);
6642 if (!ok) {
6643 DEBUG(0, ("uncName data corrupted\n"));
6644 result = WERR_INVALID_DATA;
6645 goto done;
6647 result = winreg_set_printer_dataex(tmp_ctx, b,
6648 printer->sharename,
6649 SPOOL_DSSPOOLER_KEY,
6650 SPOOL_REG_UNCNAME,
6651 REG_SZ,
6652 buffer.data,
6653 buffer.length);
6654 if (!W_ERROR_IS_OK(result)) {
6655 DEBUG(0, ("Failed to set %s\n", SPOOL_REG_UNCNAME));
6656 goto done;
6659 done:
6660 talloc_free(tmp_ctx);
6661 return result;
6664 /********************************************************************
6665 * Called by spoolss_api_setprinter
6666 * when updating a printer description.
6667 ********************************************************************/
6669 static WERROR update_printer(struct pipes_struct *p,
6670 struct policy_handle *handle,
6671 struct spoolss_SetPrinterInfoCtr *info_ctr,
6672 struct spoolss_DeviceMode *devmode)
6674 uint32_t printer_mask = SPOOLSS_PRINTER_INFO_ALL;
6675 struct spoolss_SetPrinterInfo2 *printer = info_ctr->info.info2;
6676 struct spoolss_PrinterInfo2 *old_printer;
6677 struct printer_handle *Printer = find_printer_index_by_hnd(p, handle);
6678 int snum;
6679 WERROR result = WERR_OK;
6680 TALLOC_CTX *tmp_ctx;
6681 struct dcerpc_binding_handle *b;
6683 DEBUG(8,("update_printer\n"));
6685 tmp_ctx = talloc_new(p->mem_ctx);
6686 if (tmp_ctx == NULL) {
6687 return WERR_NOMEM;
6690 if (!Printer) {
6691 result = WERR_BADFID;
6692 goto done;
6695 if (!get_printer_snum(p, handle, &snum, NULL)) {
6696 result = WERR_BADFID;
6697 goto done;
6700 result = winreg_printer_binding_handle(tmp_ctx,
6701 get_session_info_system(),
6702 p->msg_ctx,
6703 &b);
6704 if (!W_ERROR_IS_OK(result)) {
6705 goto done;
6708 result = winreg_get_printer(tmp_ctx, b,
6709 lp_const_servicename(snum),
6710 &old_printer);
6711 if (!W_ERROR_IS_OK(result)) {
6712 result = WERR_BADFID;
6713 goto done;
6716 /* Do sanity check on the requested changes for Samba */
6717 if (!check_printer_ok(tmp_ctx, printer, snum)) {
6718 result = WERR_INVALID_PARAM;
6719 goto done;
6722 /* FIXME!!! If the driver has changed we really should verify that
6723 it is installed before doing much else --jerry */
6725 /* Check calling user has permission to update printer description */
6726 if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
6727 DEBUG(3, ("update_printer: printer property change denied by handle\n"));
6728 result = WERR_ACCESS_DENIED;
6729 goto done;
6732 /* Call addprinter hook */
6733 /* Check changes to see if this is really needed */
6735 if (*lp_addprinter_cmd(talloc_tos()) &&
6736 (!strequal(printer->drivername, old_printer->drivername) ||
6737 !strequal(printer->comment, old_printer->comment) ||
6738 !strequal(printer->portname, old_printer->portname) ||
6739 !strequal(printer->location, old_printer->location)) )
6741 char *raddr;
6743 raddr = tsocket_address_inet_addr_string(p->remote_address,
6744 p->mem_ctx);
6745 if (raddr == NULL) {
6746 return WERR_NOMEM;
6749 /* add_printer_hook() will call reload_services() */
6750 if (!add_printer_hook(tmp_ctx, p->session_info->security_token,
6751 printer, raddr,
6752 p->msg_ctx)) {
6753 result = WERR_ACCESS_DENIED;
6754 goto done;
6758 result = update_dsspooler(tmp_ctx,
6759 get_session_info_system(),
6760 p->msg_ctx,
6761 snum,
6762 printer,
6763 old_printer);
6764 if (!W_ERROR_IS_OK(result)) {
6765 goto done;
6768 printer_mask &= ~SPOOLSS_PRINTER_INFO_SECDESC;
6770 if (devmode == NULL) {
6771 printer_mask &= ~SPOOLSS_PRINTER_INFO_DEVMODE;
6773 result = winreg_update_printer(tmp_ctx, b,
6774 printer->sharename,
6775 printer_mask,
6776 printer,
6777 devmode,
6778 NULL);
6780 done:
6781 talloc_free(tmp_ctx);
6783 return result;
6786 /****************************************************************************
6787 ****************************************************************************/
6788 static WERROR publish_or_unpublish_printer(struct pipes_struct *p,
6789 struct policy_handle *handle,
6790 struct spoolss_SetPrinterInfo7 *info7)
6792 #ifdef HAVE_ADS
6793 struct spoolss_PrinterInfo2 *pinfo2 = NULL;
6794 WERROR result;
6795 int snum;
6796 struct printer_handle *Printer;
6798 if ( lp_security() != SEC_ADS ) {
6799 return WERR_UNKNOWN_LEVEL;
6802 Printer = find_printer_index_by_hnd(p, handle);
6804 DEBUG(5,("publish_or_unpublish_printer, action = %d\n",info7->action));
6806 if (!Printer)
6807 return WERR_BADFID;
6809 if (!get_printer_snum(p, handle, &snum, NULL))
6810 return WERR_BADFID;
6812 result = winreg_get_printer_internal(p->mem_ctx,
6813 get_session_info_system(),
6814 p->msg_ctx,
6815 lp_servicename(talloc_tos(), snum),
6816 &pinfo2);
6817 if (!W_ERROR_IS_OK(result)) {
6818 return WERR_BADFID;
6821 nt_printer_publish(pinfo2,
6822 get_session_info_system(),
6823 p->msg_ctx,
6824 pinfo2,
6825 info7->action);
6827 TALLOC_FREE(pinfo2);
6828 return WERR_OK;
6829 #else
6830 return WERR_UNKNOWN_LEVEL;
6831 #endif
6834 /********************************************************************
6835 ********************************************************************/
6837 static WERROR update_printer_devmode(struct pipes_struct *p,
6838 struct policy_handle *handle,
6839 struct spoolss_DeviceMode *devmode)
6841 int snum;
6842 struct printer_handle *Printer = find_printer_index_by_hnd(p, handle);
6843 uint32_t info2_mask = SPOOLSS_PRINTER_INFO_DEVMODE;
6845 DEBUG(8,("update_printer_devmode\n"));
6847 if (!Printer) {
6848 return WERR_BADFID;
6851 if (!get_printer_snum(p, handle, &snum, NULL)) {
6852 return WERR_BADFID;
6855 /* Check calling user has permission to update printer description */
6856 if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
6857 DEBUG(3, ("update_printer: printer property change denied by handle\n"));
6858 return WERR_ACCESS_DENIED;
6861 return winreg_update_printer_internal(p->mem_ctx,
6862 get_session_info_system(),
6863 p->msg_ctx,
6864 lp_const_servicename(snum),
6865 info2_mask,
6866 NULL,
6867 devmode,
6868 NULL);
6872 /****************************************************************
6873 _spoolss_SetPrinter
6874 ****************************************************************/
6876 WERROR _spoolss_SetPrinter(struct pipes_struct *p,
6877 struct spoolss_SetPrinter *r)
6879 WERROR result;
6881 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
6883 if (!Printer) {
6884 DEBUG(2,("_spoolss_SetPrinter: Invalid handle (%s:%u:%u)\n",
6885 OUR_HANDLE(r->in.handle)));
6886 return WERR_BADFID;
6889 /* check the level */
6890 switch (r->in.info_ctr->level) {
6891 case 0:
6892 return control_printer(r->in.handle, r->in.command, p);
6893 case 2:
6894 result = update_printer(p, r->in.handle,
6895 r->in.info_ctr,
6896 r->in.devmode_ctr->devmode);
6897 if (!W_ERROR_IS_OK(result))
6898 return result;
6899 if (r->in.secdesc_ctr->sd)
6900 result = update_printer_sec(r->in.handle, p,
6901 r->in.secdesc_ctr);
6902 return result;
6903 case 3:
6904 return update_printer_sec(r->in.handle, p,
6905 r->in.secdesc_ctr);
6906 case 7:
6907 return publish_or_unpublish_printer(p, r->in.handle,
6908 r->in.info_ctr->info.info7);
6909 case 8:
6910 return update_printer_devmode(p, r->in.handle,
6911 r->in.devmode_ctr->devmode);
6912 default:
6913 return WERR_UNKNOWN_LEVEL;
6917 /****************************************************************
6918 _spoolss_FindClosePrinterNotify
6919 ****************************************************************/
6921 WERROR _spoolss_FindClosePrinterNotify(struct pipes_struct *p,
6922 struct spoolss_FindClosePrinterNotify *r)
6924 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
6926 if (!Printer) {
6927 DEBUG(2,("_spoolss_FindClosePrinterNotify: "
6928 "Invalid handle (%s:%u:%u)\n", OUR_HANDLE(r->in.handle)));
6929 return WERR_BADFID;
6932 if (Printer->notify.cli_chan != NULL &&
6933 Printer->notify.cli_chan->active_connections > 0) {
6934 int snum = -1;
6936 if (Printer->printer_type == SPLHND_PRINTER) {
6937 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
6938 return WERR_BADFID;
6942 srv_spoolss_replycloseprinter(snum, Printer);
6945 Printer->notify.flags=0;
6946 Printer->notify.options=0;
6947 Printer->notify.localmachine[0]='\0';
6948 Printer->notify.printerlocal=0;
6949 TALLOC_FREE(Printer->notify.option);
6951 return WERR_OK;
6954 /****************************************************************
6955 _spoolss_AddJob
6956 ****************************************************************/
6958 WERROR _spoolss_AddJob(struct pipes_struct *p,
6959 struct spoolss_AddJob *r)
6961 if (!r->in.buffer && (r->in.offered != 0)) {
6962 return WERR_INVALID_PARAM;
6965 /* this is what a NT server returns for AddJob. AddJob must fail on
6966 * non-local printers */
6968 if (r->in.level != 1) {
6969 return WERR_UNKNOWN_LEVEL;
6972 return WERR_INVALID_PARAM;
6975 /****************************************************************************
6976 fill_job_info1
6977 ****************************************************************************/
6979 static WERROR fill_job_info1(TALLOC_CTX *mem_ctx,
6980 struct spoolss_JobInfo1 *r,
6981 const print_queue_struct *queue,
6982 int position, int snum,
6983 struct spoolss_PrinterInfo2 *pinfo2)
6985 struct tm *t;
6987 t = gmtime(&queue->time);
6989 r->job_id = queue->sysjob;
6991 r->printer_name = lp_servicename(mem_ctx, snum);
6992 W_ERROR_HAVE_NO_MEMORY(r->printer_name);
6993 r->server_name = talloc_strdup(mem_ctx, pinfo2->servername);
6994 W_ERROR_HAVE_NO_MEMORY(r->server_name);
6995 r->user_name = talloc_strdup(mem_ctx, queue->fs_user);
6996 W_ERROR_HAVE_NO_MEMORY(r->user_name);
6997 r->document_name = talloc_strdup(mem_ctx, queue->fs_file);
6998 W_ERROR_HAVE_NO_MEMORY(r->document_name);
6999 r->data_type = talloc_strdup(mem_ctx, "RAW");
7000 W_ERROR_HAVE_NO_MEMORY(r->data_type);
7001 r->text_status = talloc_strdup(mem_ctx, "");
7002 W_ERROR_HAVE_NO_MEMORY(r->text_status);
7004 r->status = nt_printj_status(queue->status);
7005 r->priority = queue->priority;
7006 r->position = position;
7007 r->total_pages = queue->page_count;
7008 r->pages_printed = 0; /* ??? */
7010 init_systemtime(&r->submitted, t);
7012 return WERR_OK;
7015 /****************************************************************************
7016 fill_job_info2
7017 ****************************************************************************/
7019 static WERROR fill_job_info2(TALLOC_CTX *mem_ctx,
7020 struct spoolss_JobInfo2 *r,
7021 const print_queue_struct *queue,
7022 int position, int snum,
7023 struct spoolss_PrinterInfo2 *pinfo2,
7024 struct spoolss_DeviceMode *devmode)
7026 struct tm *t;
7028 t = gmtime(&queue->time);
7030 r->job_id = queue->sysjob;
7032 r->printer_name = lp_servicename(mem_ctx, snum);
7033 W_ERROR_HAVE_NO_MEMORY(r->printer_name);
7034 r->server_name = talloc_strdup(mem_ctx, pinfo2->servername);
7035 W_ERROR_HAVE_NO_MEMORY(r->server_name);
7036 r->user_name = talloc_strdup(mem_ctx, queue->fs_user);
7037 W_ERROR_HAVE_NO_MEMORY(r->user_name);
7038 r->document_name = talloc_strdup(mem_ctx, queue->fs_file);
7039 W_ERROR_HAVE_NO_MEMORY(r->document_name);
7040 r->notify_name = talloc_strdup(mem_ctx, queue->fs_user);
7041 W_ERROR_HAVE_NO_MEMORY(r->notify_name);
7042 r->data_type = talloc_strdup(mem_ctx, "RAW");
7043 W_ERROR_HAVE_NO_MEMORY(r->data_type);
7044 r->print_processor = talloc_strdup(mem_ctx, "winprint");
7045 W_ERROR_HAVE_NO_MEMORY(r->print_processor);
7046 r->parameters = talloc_strdup(mem_ctx, "");
7047 W_ERROR_HAVE_NO_MEMORY(r->parameters);
7048 r->driver_name = talloc_strdup(mem_ctx, pinfo2->drivername);
7049 W_ERROR_HAVE_NO_MEMORY(r->driver_name);
7051 r->devmode = devmode;
7053 r->text_status = talloc_strdup(mem_ctx, "");
7054 W_ERROR_HAVE_NO_MEMORY(r->text_status);
7056 r->secdesc = NULL;
7058 r->status = nt_printj_status(queue->status);
7059 r->priority = queue->priority;
7060 r->position = position;
7061 r->start_time = 0;
7062 r->until_time = 0;
7063 r->total_pages = queue->page_count;
7064 r->size = queue->size;
7065 init_systemtime(&r->submitted, t);
7066 r->time = 0;
7067 r->pages_printed = 0; /* ??? */
7069 return WERR_OK;
7072 /****************************************************************************
7073 fill_job_info3
7074 ****************************************************************************/
7076 static WERROR fill_job_info3(TALLOC_CTX *mem_ctx,
7077 struct spoolss_JobInfo3 *r,
7078 const print_queue_struct *queue,
7079 const print_queue_struct *next_queue,
7080 int position, int snum,
7081 struct spoolss_PrinterInfo2 *pinfo2)
7083 r->job_id = queue->sysjob;
7084 r->next_job_id = 0;
7085 if (next_queue) {
7086 r->next_job_id = next_queue->sysjob;
7088 r->reserved = 0;
7090 return WERR_OK;
7093 /****************************************************************************
7094 Enumjobs at level 1.
7095 ****************************************************************************/
7097 static WERROR enumjobs_level1(TALLOC_CTX *mem_ctx,
7098 const print_queue_struct *queue,
7099 uint32_t num_queues, int snum,
7100 struct spoolss_PrinterInfo2 *pinfo2,
7101 union spoolss_JobInfo **info_p,
7102 uint32_t *count)
7104 union spoolss_JobInfo *info;
7105 int i;
7106 WERROR result = WERR_OK;
7108 info = talloc_array(mem_ctx, union spoolss_JobInfo, num_queues);
7109 W_ERROR_HAVE_NO_MEMORY(info);
7111 *count = num_queues;
7113 for (i=0; i<*count; i++) {
7114 result = fill_job_info1(info,
7115 &info[i].info1,
7116 &queue[i],
7118 snum,
7119 pinfo2);
7120 if (!W_ERROR_IS_OK(result)) {
7121 goto out;
7125 out:
7126 if (!W_ERROR_IS_OK(result)) {
7127 TALLOC_FREE(info);
7128 *count = 0;
7129 return result;
7132 *info_p = info;
7134 return WERR_OK;
7137 /****************************************************************************
7138 Enumjobs at level 2.
7139 ****************************************************************************/
7141 static WERROR enumjobs_level2(TALLOC_CTX *mem_ctx,
7142 const print_queue_struct *queue,
7143 uint32_t num_queues, int snum,
7144 struct spoolss_PrinterInfo2 *pinfo2,
7145 union spoolss_JobInfo **info_p,
7146 uint32_t *count)
7148 union spoolss_JobInfo *info;
7149 int i;
7150 WERROR result = WERR_OK;
7152 info = talloc_array(mem_ctx, union spoolss_JobInfo, num_queues);
7153 W_ERROR_HAVE_NO_MEMORY(info);
7155 *count = num_queues;
7157 for (i=0; i<*count; i++) {
7158 struct spoolss_DeviceMode *devmode;
7160 result = spoolss_create_default_devmode(info,
7161 pinfo2->printername,
7162 &devmode);
7163 if (!W_ERROR_IS_OK(result)) {
7164 DEBUG(3, ("Can't proceed w/o a devmode!"));
7165 goto out;
7168 result = fill_job_info2(info,
7169 &info[i].info2,
7170 &queue[i],
7172 snum,
7173 pinfo2,
7174 devmode);
7175 if (!W_ERROR_IS_OK(result)) {
7176 goto out;
7180 out:
7181 if (!W_ERROR_IS_OK(result)) {
7182 TALLOC_FREE(info);
7183 *count = 0;
7184 return result;
7187 *info_p = info;
7189 return WERR_OK;
7192 /****************************************************************************
7193 Enumjobs at level 3.
7194 ****************************************************************************/
7196 static WERROR enumjobs_level3(TALLOC_CTX *mem_ctx,
7197 const print_queue_struct *queue,
7198 uint32_t num_queues, int snum,
7199 struct spoolss_PrinterInfo2 *pinfo2,
7200 union spoolss_JobInfo **info_p,
7201 uint32_t *count)
7203 union spoolss_JobInfo *info;
7204 int i;
7205 WERROR result = WERR_OK;
7207 info = talloc_array(mem_ctx, union spoolss_JobInfo, num_queues);
7208 W_ERROR_HAVE_NO_MEMORY(info);
7210 *count = num_queues;
7212 for (i=0; i<*count; i++) {
7213 const print_queue_struct *next_queue = NULL;
7215 if (i+1 < *count) {
7216 next_queue = &queue[i+1];
7219 result = fill_job_info3(info,
7220 &info[i].info3,
7221 &queue[i],
7222 next_queue,
7224 snum,
7225 pinfo2);
7226 if (!W_ERROR_IS_OK(result)) {
7227 goto out;
7231 out:
7232 if (!W_ERROR_IS_OK(result)) {
7233 TALLOC_FREE(info);
7234 *count = 0;
7235 return result;
7238 *info_p = info;
7240 return WERR_OK;
7243 /****************************************************************
7244 _spoolss_EnumJobs
7245 ****************************************************************/
7247 WERROR _spoolss_EnumJobs(struct pipes_struct *p,
7248 struct spoolss_EnumJobs *r)
7250 WERROR result;
7251 struct spoolss_PrinterInfo2 *pinfo2 = NULL;
7252 int snum;
7253 print_status_struct prt_status;
7254 print_queue_struct *queue = NULL;
7255 uint32_t count;
7257 /* that's an [in out] buffer */
7259 if (!r->in.buffer && (r->in.offered != 0)) {
7260 return WERR_INVALID_PARAM;
7263 DEBUG(4,("_spoolss_EnumJobs\n"));
7265 *r->out.needed = 0;
7266 *r->out.count = 0;
7267 *r->out.info = NULL;
7269 /* lookup the printer snum and tdb entry */
7271 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
7272 return WERR_BADFID;
7275 result = winreg_get_printer_internal(p->mem_ctx,
7276 get_session_info_system(),
7277 p->msg_ctx,
7278 lp_const_servicename(snum),
7279 &pinfo2);
7280 if (!W_ERROR_IS_OK(result)) {
7281 return result;
7284 count = print_queue_status(p->msg_ctx, snum, &queue, &prt_status);
7285 DEBUGADD(4,("count:[%d], status:[%d], [%s]\n",
7286 count, prt_status.status, prt_status.message));
7288 if (count == 0) {
7289 SAFE_FREE(queue);
7290 TALLOC_FREE(pinfo2);
7291 return WERR_OK;
7294 switch (r->in.level) {
7295 case 1:
7296 result = enumjobs_level1(p->mem_ctx, queue, count, snum,
7297 pinfo2, r->out.info, r->out.count);
7298 break;
7299 case 2:
7300 result = enumjobs_level2(p->mem_ctx, queue, count, snum,
7301 pinfo2, r->out.info, r->out.count);
7302 break;
7303 case 3:
7304 result = enumjobs_level3(p->mem_ctx, queue, count, snum,
7305 pinfo2, r->out.info, r->out.count);
7306 break;
7307 default:
7308 result = WERR_UNKNOWN_LEVEL;
7309 break;
7312 SAFE_FREE(queue);
7313 TALLOC_FREE(pinfo2);
7315 if (!W_ERROR_IS_OK(result)) {
7316 return result;
7319 *r->out.needed = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
7320 spoolss_EnumJobs,
7321 *r->out.info, r->in.level,
7322 *r->out.count);
7323 *r->out.info = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
7324 *r->out.count = SPOOLSS_BUFFER_OK(*r->out.count, 0);
7326 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
7329 /****************************************************************
7330 _spoolss_ScheduleJob
7331 ****************************************************************/
7333 WERROR _spoolss_ScheduleJob(struct pipes_struct *p,
7334 struct spoolss_ScheduleJob *r)
7336 return WERR_OK;
7339 /****************************************************************
7340 ****************************************************************/
7342 static WERROR spoolss_setjob_1(TALLOC_CTX *mem_ctx,
7343 struct messaging_context *msg_ctx,
7344 const char *printer_name,
7345 uint32_t job_id,
7346 struct spoolss_SetJobInfo1 *r)
7348 char *old_doc_name;
7350 if (!print_job_get_name(mem_ctx, printer_name, job_id, &old_doc_name)) {
7351 return WERR_BADFID;
7354 if (strequal(old_doc_name, r->document_name)) {
7355 return WERR_OK;
7358 if (!print_job_set_name(server_event_context(), msg_ctx,
7359 printer_name, job_id, r->document_name)) {
7360 return WERR_BADFID;
7363 return WERR_OK;
7366 /****************************************************************
7367 _spoolss_SetJob
7368 ****************************************************************/
7370 WERROR _spoolss_SetJob(struct pipes_struct *p,
7371 struct spoolss_SetJob *r)
7373 const struct auth_session_info *session_info = p->session_info;
7374 int snum;
7375 WERROR errcode = WERR_BADFUNC;
7377 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
7378 return WERR_BADFID;
7381 if (!print_job_exists(lp_const_servicename(snum), r->in.job_id)) {
7382 return WERR_INVALID_PRINTER_NAME;
7385 switch (r->in.command) {
7386 case SPOOLSS_JOB_CONTROL_CANCEL:
7387 case SPOOLSS_JOB_CONTROL_DELETE:
7388 errcode = print_job_delete(session_info, p->msg_ctx,
7389 snum, r->in.job_id);
7390 if (W_ERROR_EQUAL(errcode, WERR_PRINTER_HAS_JOBS_QUEUED)) {
7391 errcode = WERR_OK;
7393 break;
7394 case SPOOLSS_JOB_CONTROL_PAUSE:
7395 errcode = print_job_pause(session_info, p->msg_ctx,
7396 snum, r->in.job_id);
7397 break;
7398 case SPOOLSS_JOB_CONTROL_RESTART:
7399 case SPOOLSS_JOB_CONTROL_RESUME:
7400 errcode = print_job_resume(session_info, p->msg_ctx,
7401 snum, r->in.job_id);
7402 break;
7403 case 0:
7404 errcode = WERR_OK;
7405 break;
7406 default:
7407 return WERR_UNKNOWN_LEVEL;
7410 if (!W_ERROR_IS_OK(errcode)) {
7411 return errcode;
7414 if (r->in.ctr == NULL) {
7415 return errcode;
7418 switch (r->in.ctr->level) {
7419 case 1:
7420 errcode = spoolss_setjob_1(p->mem_ctx, p->msg_ctx,
7421 lp_const_servicename(snum),
7422 r->in.job_id,
7423 r->in.ctr->info.info1);
7424 break;
7425 case 2:
7426 case 3:
7427 case 4:
7428 default:
7429 return WERR_UNKNOWN_LEVEL;
7432 return errcode;
7435 /****************************************************************************
7436 Enumerates all printer drivers by level and architecture.
7437 ****************************************************************************/
7439 static WERROR enumprinterdrivers_level_by_architecture(TALLOC_CTX *mem_ctx,
7440 const struct auth_session_info *session_info,
7441 struct messaging_context *msg_ctx,
7442 const char *servername,
7443 const char *architecture,
7444 uint32_t level,
7445 union spoolss_DriverInfo **info_p,
7446 uint32_t *count_p)
7448 int i;
7449 uint32_t version;
7450 struct spoolss_DriverInfo8 *driver;
7451 union spoolss_DriverInfo *info = NULL;
7452 uint32_t count = 0;
7453 WERROR result = WERR_OK;
7454 uint32_t num_drivers;
7455 const char **drivers;
7456 struct dcerpc_binding_handle *b;
7457 TALLOC_CTX *tmp_ctx = NULL;
7459 *count_p = 0;
7460 *info_p = NULL;
7462 tmp_ctx = talloc_new(mem_ctx);
7463 if (!tmp_ctx) {
7464 return WERR_NOMEM;
7467 result = winreg_printer_binding_handle(tmp_ctx,
7468 session_info,
7469 msg_ctx,
7470 &b);
7471 if (!W_ERROR_IS_OK(result)) {
7472 goto out;
7475 for (version=0; version<DRIVER_MAX_VERSION; version++) {
7476 result = winreg_get_driver_list(tmp_ctx, b,
7477 architecture, version,
7478 &num_drivers, &drivers);
7479 if (!W_ERROR_IS_OK(result)) {
7480 goto out;
7482 DEBUG(4, ("we have:[%d] drivers in environment"
7483 " [%s] and version [%d]\n",
7484 num_drivers, architecture, version));
7486 if (num_drivers != 0) {
7487 info = talloc_realloc(tmp_ctx, info,
7488 union spoolss_DriverInfo,
7489 count + num_drivers);
7490 if (!info) {
7491 DEBUG(0,("enumprinterdrivers_level_by_architecture: "
7492 "failed to enlarge driver info buffer!\n"));
7493 result = WERR_NOMEM;
7494 goto out;
7498 for (i = 0; i < num_drivers; i++) {
7499 DEBUG(5, ("\tdriver: [%s]\n", drivers[i]));
7501 result = winreg_get_driver(tmp_ctx, b,
7502 architecture, drivers[i],
7503 version, &driver);
7504 if (!W_ERROR_IS_OK(result)) {
7505 goto out;
7508 switch (level) {
7509 case 1:
7510 result = fill_printer_driver_info1(info, &info[count+i].info1,
7511 driver, servername);
7512 break;
7513 case 2:
7514 result = fill_printer_driver_info2(info, &info[count+i].info2,
7515 driver, servername);
7516 break;
7517 case 3:
7518 result = fill_printer_driver_info3(info, &info[count+i].info3,
7519 driver, servername);
7520 break;
7521 case 4:
7522 result = fill_printer_driver_info4(info, &info[count+i].info4,
7523 driver, servername);
7524 break;
7525 case 5:
7526 result = fill_printer_driver_info5(info, &info[count+i].info5,
7527 driver, servername);
7528 break;
7529 case 6:
7530 result = fill_printer_driver_info6(info, &info[count+i].info6,
7531 driver, servername);
7532 break;
7533 case 8:
7534 result = fill_printer_driver_info8(info, &info[count+i].info8,
7535 driver, servername);
7536 break;
7537 default:
7538 result = WERR_UNKNOWN_LEVEL;
7539 break;
7542 TALLOC_FREE(driver);
7544 if (!W_ERROR_IS_OK(result)) {
7545 goto out;
7549 count += num_drivers;
7550 TALLOC_FREE(drivers);
7553 out:
7554 if (W_ERROR_IS_OK(result)) {
7555 *info_p = talloc_move(mem_ctx, &info);
7556 *count_p = count;
7559 talloc_free(tmp_ctx);
7560 return result;
7563 /****************************************************************************
7564 Enumerates all printer drivers by level.
7565 ****************************************************************************/
7567 static WERROR enumprinterdrivers_level(TALLOC_CTX *mem_ctx,
7568 const struct auth_session_info *session_info,
7569 struct messaging_context *msg_ctx,
7570 const char *servername,
7571 const char *architecture,
7572 uint32_t level,
7573 union spoolss_DriverInfo **info_p,
7574 uint32_t *count_p)
7576 uint32_t a,i;
7577 WERROR result = WERR_OK;
7579 if (strequal(architecture, SPOOLSS_ARCHITECTURE_ALL)) {
7581 for (a=0; archi_table[a].long_archi != NULL; a++) {
7583 union spoolss_DriverInfo *info = NULL;
7584 uint32_t count = 0;
7586 result = enumprinterdrivers_level_by_architecture(mem_ctx,
7587 session_info,
7588 msg_ctx,
7589 servername,
7590 archi_table[a].long_archi,
7591 level,
7592 &info,
7593 &count);
7594 if (!W_ERROR_IS_OK(result)) {
7595 continue;
7598 for (i=0; i < count; i++) {
7599 ADD_TO_ARRAY(mem_ctx, union spoolss_DriverInfo,
7600 info[i], info_p, count_p);
7604 return result;
7607 return enumprinterdrivers_level_by_architecture(mem_ctx,
7608 session_info,
7609 msg_ctx,
7610 servername,
7611 architecture,
7612 level,
7613 info_p,
7614 count_p);
7617 /****************************************************************
7618 _spoolss_EnumPrinterDrivers
7619 ****************************************************************/
7621 WERROR _spoolss_EnumPrinterDrivers(struct pipes_struct *p,
7622 struct spoolss_EnumPrinterDrivers *r)
7624 const char *cservername;
7625 WERROR result;
7627 /* that's an [in out] buffer */
7629 if (!r->in.buffer && (r->in.offered != 0)) {
7630 return WERR_INVALID_PARAM;
7633 DEBUG(4,("_spoolss_EnumPrinterDrivers\n"));
7635 *r->out.needed = 0;
7636 *r->out.count = 0;
7637 *r->out.info = NULL;
7639 cservername = canon_servername(r->in.server);
7641 if (!is_myname_or_ipaddr(cservername)) {
7642 return WERR_UNKNOWN_PRINTER_DRIVER;
7645 result = enumprinterdrivers_level(p->mem_ctx,
7646 get_session_info_system(),
7647 p->msg_ctx,
7648 cservername,
7649 r->in.environment,
7650 r->in.level,
7651 r->out.info,
7652 r->out.count);
7653 if (!W_ERROR_IS_OK(result)) {
7654 return result;
7657 *r->out.needed = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
7658 spoolss_EnumPrinterDrivers,
7659 *r->out.info, r->in.level,
7660 *r->out.count);
7661 *r->out.info = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
7662 *r->out.count = SPOOLSS_BUFFER_OK(*r->out.count, 0);
7664 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
7667 /****************************************************************
7668 _spoolss_EnumForms
7669 ****************************************************************/
7671 WERROR _spoolss_EnumForms(struct pipes_struct *p,
7672 struct spoolss_EnumForms *r)
7674 WERROR result;
7676 *r->out.count = 0;
7677 *r->out.needed = 0;
7678 *r->out.info = NULL;
7680 /* that's an [in out] buffer */
7682 if (!r->in.buffer && (r->in.offered != 0) ) {
7683 return WERR_INVALID_PARAM;
7686 DEBUG(4,("_spoolss_EnumForms\n"));
7687 DEBUGADD(5,("Offered buffer size [%d]\n", r->in.offered));
7688 DEBUGADD(5,("Info level [%d]\n", r->in.level));
7690 switch (r->in.level) {
7691 case 1:
7692 result = winreg_printer_enumforms1_internal(p->mem_ctx,
7693 get_session_info_system(),
7694 p->msg_ctx,
7695 r->out.count,
7696 r->out.info);
7697 break;
7698 default:
7699 result = WERR_UNKNOWN_LEVEL;
7700 break;
7703 if (!W_ERROR_IS_OK(result)) {
7704 return result;
7707 if (*r->out.count == 0) {
7708 return WERR_NO_MORE_ITEMS;
7711 *r->out.needed = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
7712 spoolss_EnumForms,
7713 *r->out.info, r->in.level,
7714 *r->out.count);
7715 *r->out.info = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
7716 *r->out.count = SPOOLSS_BUFFER_OK(*r->out.count, 0);
7718 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
7721 /****************************************************************
7722 _spoolss_GetForm
7723 ****************************************************************/
7725 WERROR _spoolss_GetForm(struct pipes_struct *p,
7726 struct spoolss_GetForm *r)
7728 WERROR result;
7730 /* that's an [in out] buffer */
7732 if (!r->in.buffer && (r->in.offered != 0)) {
7733 return WERR_INVALID_PARAM;
7736 DEBUG(4,("_spoolss_GetForm\n"));
7737 DEBUGADD(5,("Offered buffer size [%d]\n", r->in.offered));
7738 DEBUGADD(5,("Info level [%d]\n", r->in.level));
7740 switch (r->in.level) {
7741 case 1:
7742 result = winreg_printer_getform1_internal(p->mem_ctx,
7743 get_session_info_system(),
7744 p->msg_ctx,
7745 r->in.form_name,
7746 &r->out.info->info1);
7747 break;
7748 default:
7749 result = WERR_UNKNOWN_LEVEL;
7750 break;
7753 if (!W_ERROR_IS_OK(result)) {
7754 TALLOC_FREE(r->out.info);
7755 return result;
7758 *r->out.needed = SPOOLSS_BUFFER_UNION(spoolss_FormInfo,
7759 r->out.info, r->in.level);
7760 r->out.info = SPOOLSS_BUFFER_OK(r->out.info, NULL);
7762 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
7765 /****************************************************************************
7766 ****************************************************************************/
7768 static WERROR fill_port_1(TALLOC_CTX *mem_ctx,
7769 struct spoolss_PortInfo1 *r,
7770 const char *name)
7772 r->port_name = talloc_strdup(mem_ctx, name);
7773 W_ERROR_HAVE_NO_MEMORY(r->port_name);
7775 return WERR_OK;
7778 /****************************************************************************
7779 TODO: This probably needs distinguish between TCP/IP and Local ports
7780 somehow.
7781 ****************************************************************************/
7783 static WERROR fill_port_2(TALLOC_CTX *mem_ctx,
7784 struct spoolss_PortInfo2 *r,
7785 const char *name)
7787 r->port_name = talloc_strdup(mem_ctx, name);
7788 W_ERROR_HAVE_NO_MEMORY(r->port_name);
7790 r->monitor_name = talloc_strdup(mem_ctx, "Local Monitor");
7791 W_ERROR_HAVE_NO_MEMORY(r->monitor_name);
7793 r->description = talloc_strdup(mem_ctx, SPL_LOCAL_PORT);
7794 W_ERROR_HAVE_NO_MEMORY(r->description);
7796 r->port_type = SPOOLSS_PORT_TYPE_WRITE;
7797 r->reserved = 0;
7799 return WERR_OK;
7803 /****************************************************************************
7804 wrapper around the enumer ports command
7805 ****************************************************************************/
7807 static WERROR enumports_hook(TALLOC_CTX *ctx, int *count, char ***lines)
7809 char *cmd = lp_enumports_cmd(talloc_tos());
7810 char **qlines = NULL;
7811 char *command = NULL;
7812 int numlines;
7813 int ret;
7814 int fd;
7816 *count = 0;
7817 *lines = NULL;
7819 /* if no hook then just fill in the default port */
7821 if ( !*cmd ) {
7822 if (!(qlines = talloc_array( NULL, char*, 2 ))) {
7823 return WERR_NOMEM;
7825 if (!(qlines[0] = talloc_strdup(qlines, SAMBA_PRINTER_PORT_NAME ))) {
7826 TALLOC_FREE(qlines);
7827 return WERR_NOMEM;
7829 qlines[1] = NULL;
7830 numlines = 1;
7832 else {
7833 /* we have a valid enumport command */
7835 command = talloc_asprintf(ctx, "%s \"%d\"", cmd, 1);
7836 if (!command) {
7837 return WERR_NOMEM;
7840 DEBUG(10,("Running [%s]\n", command));
7841 ret = smbrun(command, &fd);
7842 DEBUG(10,("Returned [%d]\n", ret));
7843 TALLOC_FREE(command);
7844 if (ret != 0) {
7845 if (fd != -1) {
7846 close(fd);
7848 return WERR_ACCESS_DENIED;
7851 numlines = 0;
7852 qlines = fd_lines_load(fd, &numlines, 0, NULL);
7853 DEBUGADD(10,("Lines returned = [%d]\n", numlines));
7854 close(fd);
7857 *count = numlines;
7858 *lines = qlines;
7860 return WERR_OK;
7863 /****************************************************************************
7864 enumports level 1.
7865 ****************************************************************************/
7867 static WERROR enumports_level_1(TALLOC_CTX *mem_ctx,
7868 union spoolss_PortInfo **info_p,
7869 uint32_t *count)
7871 union spoolss_PortInfo *info = NULL;
7872 int i=0;
7873 WERROR result = WERR_OK;
7874 char **qlines = NULL;
7875 int numlines = 0;
7877 result = enumports_hook(talloc_tos(), &numlines, &qlines );
7878 if (!W_ERROR_IS_OK(result)) {
7879 goto out;
7882 if (numlines) {
7883 info = talloc_array(mem_ctx, union spoolss_PortInfo, numlines);
7884 if (!info) {
7885 DEBUG(10,("Returning WERR_NOMEM\n"));
7886 result = WERR_NOMEM;
7887 goto out;
7890 for (i=0; i<numlines; i++) {
7891 DEBUG(6,("Filling port number [%d] with port [%s]\n", i, qlines[i]));
7892 result = fill_port_1(info, &info[i].info1, qlines[i]);
7893 if (!W_ERROR_IS_OK(result)) {
7894 goto out;
7898 TALLOC_FREE(qlines);
7900 out:
7901 if (!W_ERROR_IS_OK(result)) {
7902 TALLOC_FREE(info);
7903 TALLOC_FREE(qlines);
7904 *count = 0;
7905 *info_p = NULL;
7906 return result;
7909 *info_p = info;
7910 *count = numlines;
7912 return WERR_OK;
7915 /****************************************************************************
7916 enumports level 2.
7917 ****************************************************************************/
7919 static WERROR enumports_level_2(TALLOC_CTX *mem_ctx,
7920 union spoolss_PortInfo **info_p,
7921 uint32_t *count)
7923 union spoolss_PortInfo *info = NULL;
7924 int i=0;
7925 WERROR result = WERR_OK;
7926 char **qlines = NULL;
7927 int numlines = 0;
7929 result = enumports_hook(talloc_tos(), &numlines, &qlines );
7930 if (!W_ERROR_IS_OK(result)) {
7931 goto out;
7934 if (numlines) {
7935 info = talloc_array(mem_ctx, union spoolss_PortInfo, numlines);
7936 if (!info) {
7937 DEBUG(10,("Returning WERR_NOMEM\n"));
7938 result = WERR_NOMEM;
7939 goto out;
7942 for (i=0; i<numlines; i++) {
7943 DEBUG(6,("Filling port number [%d] with port [%s]\n", i, qlines[i]));
7944 result = fill_port_2(info, &info[i].info2, qlines[i]);
7945 if (!W_ERROR_IS_OK(result)) {
7946 goto out;
7950 TALLOC_FREE(qlines);
7952 out:
7953 if (!W_ERROR_IS_OK(result)) {
7954 TALLOC_FREE(info);
7955 TALLOC_FREE(qlines);
7956 *count = 0;
7957 *info_p = NULL;
7958 return result;
7961 *info_p = info;
7962 *count = numlines;
7964 return WERR_OK;
7967 /****************************************************************
7968 _spoolss_EnumPorts
7969 ****************************************************************/
7971 WERROR _spoolss_EnumPorts(struct pipes_struct *p,
7972 struct spoolss_EnumPorts *r)
7974 WERROR result;
7976 /* that's an [in out] buffer */
7978 if (!r->in.buffer && (r->in.offered != 0)) {
7979 return WERR_INVALID_PARAM;
7982 DEBUG(4,("_spoolss_EnumPorts\n"));
7984 *r->out.count = 0;
7985 *r->out.needed = 0;
7986 *r->out.info = NULL;
7988 switch (r->in.level) {
7989 case 1:
7990 result = enumports_level_1(p->mem_ctx, r->out.info,
7991 r->out.count);
7992 break;
7993 case 2:
7994 result = enumports_level_2(p->mem_ctx, r->out.info,
7995 r->out.count);
7996 break;
7997 default:
7998 return WERR_UNKNOWN_LEVEL;
8001 if (!W_ERROR_IS_OK(result)) {
8002 return result;
8005 *r->out.needed = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
8006 spoolss_EnumPorts,
8007 *r->out.info, r->in.level,
8008 *r->out.count);
8009 *r->out.info = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
8010 *r->out.count = SPOOLSS_BUFFER_OK(*r->out.count, 0);
8012 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
8015 /****************************************************************************
8016 ****************************************************************************/
8018 static WERROR spoolss_addprinterex_level_2(struct pipes_struct *p,
8019 const char *server,
8020 struct spoolss_SetPrinterInfoCtr *info_ctr,
8021 struct spoolss_DeviceMode *devmode,
8022 struct security_descriptor *secdesc,
8023 struct spoolss_UserLevelCtr *user_ctr,
8024 struct policy_handle *handle)
8026 struct spoolss_SetPrinterInfo2 *info2 = info_ctr->info.info2;
8027 uint32_t info2_mask = SPOOLSS_PRINTER_INFO_ALL;
8028 int snum;
8029 WERROR err = WERR_OK;
8031 /* samba does not have a concept of local, non-shared printers yet, so
8032 * make sure we always setup sharename - gd */
8033 if ((info2->sharename == NULL || info2->sharename[0] == '\0') &&
8034 (info2->printername != NULL && info2->printername[0] != '\0')) {
8035 DEBUG(5, ("spoolss_addprinterex_level_2: "
8036 "no sharename has been set, setting printername %s as sharename\n",
8037 info2->printername));
8038 info2->sharename = info2->printername;
8041 /* check to see if the printer already exists */
8042 if ((snum = print_queue_snum(info2->sharename)) != -1) {
8043 DEBUG(5, ("spoolss_addprinterex_level_2: Attempted to add a printer named [%s] when one already existed!\n",
8044 info2->sharename));
8045 return WERR_PRINTER_ALREADY_EXISTS;
8048 if (!lp_force_printername(GLOBAL_SECTION_SNUM)) {
8049 if ((snum = print_queue_snum(info2->printername)) != -1) {
8050 DEBUG(5, ("spoolss_addprinterex_level_2: Attempted to add a printer named [%s] when one already existed!\n",
8051 info2->printername));
8052 return WERR_PRINTER_ALREADY_EXISTS;
8056 /* validate printer info struct */
8057 if (!info2->printername || strlen(info2->printername) == 0) {
8058 return WERR_INVALID_PRINTER_NAME;
8060 if (!info2->portname || strlen(info2->portname) == 0) {
8061 return WERR_UNKNOWN_PORT;
8063 if (!info2->drivername || strlen(info2->drivername) == 0) {
8064 return WERR_UNKNOWN_PRINTER_DRIVER;
8066 if (!info2->printprocessor || strlen(info2->printprocessor) == 0) {
8067 return WERR_UNKNOWN_PRINTPROCESSOR;
8070 /* FIXME!!! smbd should check to see if the driver is installed before
8071 trying to add a printer like this --jerry */
8073 if (*lp_addprinter_cmd(talloc_tos()) ) {
8074 char *raddr;
8076 raddr = tsocket_address_inet_addr_string(p->remote_address,
8077 p->mem_ctx);
8078 if (raddr == NULL) {
8079 return WERR_NOMEM;
8082 if ( !add_printer_hook(p->mem_ctx, p->session_info->security_token,
8083 info2, raddr,
8084 p->msg_ctx) ) {
8085 return WERR_ACCESS_DENIED;
8087 } else {
8088 DEBUG(0,("spoolss_addprinterex_level_2: add printer for printer %s called and no "
8089 "smb.conf parameter \"addprinter command\" is defined. This "
8090 "parameter must exist for this call to succeed\n",
8091 info2->sharename ));
8094 if ((snum = print_queue_snum(info2->sharename)) == -1) {
8095 return WERR_ACCESS_DENIED;
8098 /* you must be a printer admin to add a new printer */
8099 if (!print_access_check(p->session_info,
8100 p->msg_ctx,
8101 snum,
8102 PRINTER_ACCESS_ADMINISTER)) {
8103 return WERR_ACCESS_DENIED;
8107 * Do sanity check on the requested changes for Samba.
8110 if (!check_printer_ok(p->mem_ctx, info2, snum)) {
8111 return WERR_INVALID_PARAM;
8114 if (devmode == NULL) {
8115 info2_mask = ~SPOOLSS_PRINTER_INFO_DEVMODE;
8118 err = update_dsspooler(p->mem_ctx,
8119 get_session_info_system(),
8120 p->msg_ctx,
8122 info2,
8123 NULL);
8124 if (!W_ERROR_IS_OK(err)) {
8125 return err;
8128 err = winreg_update_printer_internal(p->mem_ctx,
8129 get_session_info_system(),
8130 p->msg_ctx,
8131 info2->sharename,
8132 info2_mask,
8133 info2,
8134 devmode,
8135 secdesc);
8136 if (!W_ERROR_IS_OK(err)) {
8137 return err;
8140 err = open_printer_hnd(p, handle, info2->printername, PRINTER_ACCESS_ADMINISTER);
8141 if (!W_ERROR_IS_OK(err)) {
8142 /* Handle open failed - remove addition. */
8143 ZERO_STRUCTP(handle);
8144 return err;
8147 return WERR_OK;
8150 /****************************************************************
8151 _spoolss_AddPrinterEx
8152 ****************************************************************/
8154 WERROR _spoolss_AddPrinterEx(struct pipes_struct *p,
8155 struct spoolss_AddPrinterEx *r)
8157 switch (r->in.info_ctr->level) {
8158 case 1:
8159 /* we don't handle yet */
8160 /* but I know what to do ... */
8161 return WERR_UNKNOWN_LEVEL;
8162 case 2:
8163 return spoolss_addprinterex_level_2(p, r->in.server,
8164 r->in.info_ctr,
8165 r->in.devmode_ctr->devmode,
8166 r->in.secdesc_ctr->sd,
8167 r->in.userlevel_ctr,
8168 r->out.handle);
8169 default:
8170 return WERR_UNKNOWN_LEVEL;
8174 /****************************************************************
8175 _spoolss_AddPrinter
8176 ****************************************************************/
8178 WERROR _spoolss_AddPrinter(struct pipes_struct *p,
8179 struct spoolss_AddPrinter *r)
8181 struct spoolss_AddPrinterEx a;
8182 struct spoolss_UserLevelCtr userlevel_ctr;
8184 ZERO_STRUCT(userlevel_ctr);
8186 userlevel_ctr.level = 1;
8188 a.in.server = r->in.server;
8189 a.in.info_ctr = r->in.info_ctr;
8190 a.in.devmode_ctr = r->in.devmode_ctr;
8191 a.in.secdesc_ctr = r->in.secdesc_ctr;
8192 a.in.userlevel_ctr = &userlevel_ctr;
8193 a.out.handle = r->out.handle;
8195 return _spoolss_AddPrinterEx(p, &a);
8198 /****************************************************************
8199 _spoolss_AddPrinterDriverEx
8200 ****************************************************************/
8202 WERROR _spoolss_AddPrinterDriverEx(struct pipes_struct *p,
8203 struct spoolss_AddPrinterDriverEx *r)
8205 WERROR err = WERR_OK;
8206 const char *driver_name = NULL;
8207 uint32_t version;
8208 const char *fn;
8210 switch (p->opnum) {
8211 case NDR_SPOOLSS_ADDPRINTERDRIVER:
8212 fn = "_spoolss_AddPrinterDriver";
8213 break;
8214 case NDR_SPOOLSS_ADDPRINTERDRIVEREX:
8215 fn = "_spoolss_AddPrinterDriverEx";
8216 break;
8217 default:
8218 return WERR_INVALID_PARAM;
8222 * we only support the semantics of AddPrinterDriver()
8223 * i.e. only copy files that are newer than existing ones
8226 if (r->in.flags == 0) {
8227 return WERR_INVALID_PARAM;
8230 if (r->in.flags != APD_COPY_NEW_FILES) {
8231 return WERR_ACCESS_DENIED;
8234 /* FIXME */
8235 if (r->in.info_ctr->level != 3 && r->in.info_ctr->level != 6) {
8236 /* Clever hack from Martin Zielinski <mz@seh.de>
8237 * to allow downgrade from level 8 (Vista).
8239 DEBUG(0,("%s: level %d not yet implemented\n", fn,
8240 r->in.info_ctr->level));
8241 return WERR_UNKNOWN_LEVEL;
8244 DEBUG(5,("Cleaning driver's information\n"));
8245 err = clean_up_driver_struct(p->mem_ctx, p->session_info, r->in.info_ctr);
8246 if (!W_ERROR_IS_OK(err))
8247 goto done;
8249 DEBUG(5,("Moving driver to final destination\n"));
8250 err = move_driver_to_download_area(p->session_info, r->in.info_ctr);
8251 if (!W_ERROR_IS_OK(err)) {
8252 goto done;
8255 err = winreg_add_driver_internal(p->mem_ctx,
8256 get_session_info_system(),
8257 p->msg_ctx,
8258 r->in.info_ctr,
8259 &driver_name,
8260 &version);
8261 if (!W_ERROR_IS_OK(err)) {
8262 goto done;
8266 * I think this is where he DrvUpgradePrinter() hook would be
8267 * be called in a driver's interface DLL on a Windows NT 4.0/2k
8268 * server. Right now, we just need to send ourselves a message
8269 * to update each printer bound to this driver. --jerry
8272 if (!srv_spoolss_drv_upgrade_printer(driver_name, p->msg_ctx)) {
8273 DEBUG(0,("%s: Failed to send message about upgrading driver [%s]!\n",
8274 fn, driver_name));
8277 done:
8278 return err;
8281 /****************************************************************
8282 _spoolss_AddPrinterDriver
8283 ****************************************************************/
8285 WERROR _spoolss_AddPrinterDriver(struct pipes_struct *p,
8286 struct spoolss_AddPrinterDriver *r)
8288 struct spoolss_AddPrinterDriverEx a;
8290 switch (r->in.info_ctr->level) {
8291 case 2:
8292 case 3:
8293 case 4:
8294 case 5:
8295 break;
8296 default:
8297 return WERR_UNKNOWN_LEVEL;
8300 a.in.servername = r->in.servername;
8301 a.in.info_ctr = r->in.info_ctr;
8302 a.in.flags = APD_COPY_NEW_FILES;
8304 return _spoolss_AddPrinterDriverEx(p, &a);
8307 /****************************************************************************
8308 ****************************************************************************/
8310 struct _spoolss_paths {
8311 int type;
8312 const char *share;
8313 const char *dir;
8316 enum { SPOOLSS_DRIVER_PATH, SPOOLSS_PRTPROCS_PATH };
8318 static const struct _spoolss_paths spoolss_paths[]= {
8319 { SPOOLSS_DRIVER_PATH, "print$", "DRIVERS" },
8320 { SPOOLSS_PRTPROCS_PATH, "prnproc$", "PRTPROCS" }
8323 static WERROR compose_spoolss_server_path(TALLOC_CTX *mem_ctx,
8324 const char *servername,
8325 const char *environment,
8326 int component,
8327 char **path)
8329 const char *pservername = NULL;
8330 const char *long_archi;
8331 const char *short_archi;
8333 *path = NULL;
8335 /* environment may be empty */
8336 if (environment && strlen(environment)) {
8337 long_archi = environment;
8338 } else {
8339 long_archi = lp_parm_const_string(GLOBAL_SECTION_SNUM,
8340 "spoolss", "architecture",
8341 SPOOLSS_ARCHITECTURE_NT_X86);
8344 /* servername may be empty */
8345 if (servername && strlen(servername)) {
8346 pservername = canon_servername(servername);
8348 if (!is_myname_or_ipaddr(pservername)) {
8349 return WERR_INVALID_PARAM;
8353 if (!(short_archi = get_short_archi(long_archi))) {
8354 return WERR_INVALID_ENVIRONMENT;
8357 switch (component) {
8358 case SPOOLSS_PRTPROCS_PATH:
8359 case SPOOLSS_DRIVER_PATH:
8360 if (pservername) {
8361 *path = talloc_asprintf(mem_ctx,
8362 "\\\\%s\\%s\\%s",
8363 pservername,
8364 spoolss_paths[component].share,
8365 short_archi);
8366 } else {
8367 *path = talloc_asprintf(mem_ctx, "%s\\%s\\%s",
8368 SPOOLSS_DEFAULT_SERVER_PATH,
8369 spoolss_paths[component].dir,
8370 short_archi);
8372 break;
8373 default:
8374 return WERR_INVALID_PARAM;
8377 if (!*path) {
8378 return WERR_NOMEM;
8381 return WERR_OK;
8384 /****************************************************************************
8385 ****************************************************************************/
8387 static WERROR getprinterdriverdir_level_1(TALLOC_CTX *mem_ctx,
8388 const char *servername,
8389 const char *environment,
8390 struct spoolss_DriverDirectoryInfo1 *r)
8392 WERROR werr;
8393 char *path = NULL;
8395 werr = compose_spoolss_server_path(mem_ctx,
8396 servername,
8397 environment,
8398 SPOOLSS_DRIVER_PATH,
8399 &path);
8400 if (!W_ERROR_IS_OK(werr)) {
8401 return werr;
8404 DEBUG(4,("printer driver directory: [%s]\n", path));
8406 r->directory_name = path;
8408 return WERR_OK;
8411 /****************************************************************
8412 _spoolss_GetPrinterDriverDirectory
8413 ****************************************************************/
8415 WERROR _spoolss_GetPrinterDriverDirectory(struct pipes_struct *p,
8416 struct spoolss_GetPrinterDriverDirectory *r)
8418 WERROR werror;
8420 /* that's an [in out] buffer */
8422 if (!r->in.buffer && (r->in.offered != 0)) {
8423 return WERR_INVALID_PARAM;
8426 DEBUG(5,("_spoolss_GetPrinterDriverDirectory: level %d\n",
8427 r->in.level));
8429 *r->out.needed = 0;
8431 /* r->in.level is ignored */
8433 werror = getprinterdriverdir_level_1(p->mem_ctx,
8434 r->in.server,
8435 r->in.environment,
8436 &r->out.info->info1);
8437 if (!W_ERROR_IS_OK(werror)) {
8438 TALLOC_FREE(r->out.info);
8439 return werror;
8442 *r->out.needed = SPOOLSS_BUFFER_UNION(spoolss_DriverDirectoryInfo,
8443 r->out.info, r->in.level);
8444 r->out.info = SPOOLSS_BUFFER_OK(r->out.info, NULL);
8446 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
8449 /****************************************************************
8450 _spoolss_EnumPrinterData
8451 ****************************************************************/
8453 WERROR _spoolss_EnumPrinterData(struct pipes_struct *p,
8454 struct spoolss_EnumPrinterData *r)
8456 WERROR result;
8457 struct spoolss_EnumPrinterDataEx r2;
8458 uint32_t count;
8459 struct spoolss_PrinterEnumValues *info, *val = NULL;
8460 uint32_t needed;
8462 r2.in.handle = r->in.handle;
8463 r2.in.key_name = "PrinterDriverData";
8464 r2.in.offered = 0;
8465 r2.out.count = &count;
8466 r2.out.info = &info;
8467 r2.out.needed = &needed;
8469 result = _spoolss_EnumPrinterDataEx(p, &r2);
8470 if (W_ERROR_EQUAL(result, WERR_MORE_DATA)) {
8471 r2.in.offered = needed;
8472 result = _spoolss_EnumPrinterDataEx(p, &r2);
8474 if (!W_ERROR_IS_OK(result)) {
8475 return result;
8479 * The NT machine wants to know the biggest size of value and data
8481 * cf: MSDN EnumPrinterData remark section
8484 if (!r->in.value_offered && !r->in.data_offered) {
8485 uint32_t biggest_valuesize = 0;
8486 uint32_t biggest_datasize = 0;
8487 int i, name_length;
8489 DEBUGADD(6,("Activating NT mega-hack to find sizes\n"));
8491 for (i=0; i<count; i++) {
8493 name_length = strlen(info[i].value_name);
8494 if (strlen(info[i].value_name) > biggest_valuesize) {
8495 biggest_valuesize = name_length;
8498 if (info[i].data_length > biggest_datasize) {
8499 biggest_datasize = info[i].data_length;
8502 DEBUG(6,("current values: [%d], [%d]\n", biggest_valuesize,
8503 biggest_datasize));
8506 /* the value is an UNICODE string but real_value_size is the length
8507 in bytes including the trailing 0 */
8509 *r->out.value_needed = 2 * (1 + biggest_valuesize);
8510 *r->out.data_needed = biggest_datasize;
8512 DEBUG(6,("final values: [%d], [%d]\n",
8513 *r->out.value_needed, *r->out.data_needed));
8515 return WERR_OK;
8518 if (r->in.enum_index < count) {
8519 val = &info[r->in.enum_index];
8522 if (val == NULL) {
8523 /* out_value should default to "" or else NT4 has
8524 problems unmarshalling the response */
8526 if (r->in.value_offered) {
8527 *r->out.value_needed = 1;
8528 r->out.value_name = talloc_strdup(r, "");
8529 if (!r->out.value_name) {
8530 return WERR_NOMEM;
8532 } else {
8533 r->out.value_name = NULL;
8534 *r->out.value_needed = 0;
8537 /* the data is counted in bytes */
8539 *r->out.data_needed = r->in.data_offered;
8541 result = WERR_NO_MORE_ITEMS;
8542 } else {
8544 * the value is:
8545 * - counted in bytes in the request
8546 * - counted in UNICODE chars in the max reply
8547 * - counted in bytes in the real size
8549 * take a pause *before* coding not *during* coding
8552 /* name */
8553 if (r->in.value_offered) {
8554 r->out.value_name = talloc_strdup(r, val->value_name);
8555 if (!r->out.value_name) {
8556 return WERR_NOMEM;
8558 *r->out.value_needed = val->value_name_len;
8559 } else {
8560 r->out.value_name = NULL;
8561 *r->out.value_needed = 0;
8564 /* type */
8566 *r->out.type = val->type;
8568 /* data - counted in bytes */
8571 * See the section "Dynamically Typed Query Parameters"
8572 * in MS-RPRN.
8575 if (r->out.data && val->data && val->data->data &&
8576 val->data_length && r->in.data_offered) {
8577 memcpy(r->out.data, val->data->data,
8578 MIN(val->data_length,r->in.data_offered));
8581 *r->out.data_needed = val->data_length;
8583 result = WERR_OK;
8586 return result;
8589 /****************************************************************
8590 _spoolss_SetPrinterData
8591 ****************************************************************/
8593 WERROR _spoolss_SetPrinterData(struct pipes_struct *p,
8594 struct spoolss_SetPrinterData *r)
8596 struct spoolss_SetPrinterDataEx r2;
8598 r2.in.handle = r->in.handle;
8599 r2.in.key_name = "PrinterDriverData";
8600 r2.in.value_name = r->in.value_name;
8601 r2.in.type = r->in.type;
8602 r2.in.data = r->in.data;
8603 r2.in.offered = r->in.offered;
8605 return _spoolss_SetPrinterDataEx(p, &r2);
8608 /****************************************************************
8609 _spoolss_ResetPrinter
8610 ****************************************************************/
8612 WERROR _spoolss_ResetPrinter(struct pipes_struct *p,
8613 struct spoolss_ResetPrinter *r)
8615 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
8616 int snum;
8618 DEBUG(5,("_spoolss_ResetPrinter\n"));
8621 * All we do is to check to see if the handle and queue is valid.
8622 * This call really doesn't mean anything to us because we only
8623 * support RAW printing. --jerry
8626 if (!Printer) {
8627 DEBUG(2,("_spoolss_ResetPrinter: Invalid handle (%s:%u:%u).\n",
8628 OUR_HANDLE(r->in.handle)));
8629 return WERR_BADFID;
8632 if (!get_printer_snum(p, r->in.handle, &snum, NULL))
8633 return WERR_BADFID;
8636 /* blindly return success */
8637 return WERR_OK;
8640 /****************************************************************
8641 _spoolss_DeletePrinterData
8642 ****************************************************************/
8644 WERROR _spoolss_DeletePrinterData(struct pipes_struct *p,
8645 struct spoolss_DeletePrinterData *r)
8647 struct spoolss_DeletePrinterDataEx r2;
8649 r2.in.handle = r->in.handle;
8650 r2.in.key_name = "PrinterDriverData";
8651 r2.in.value_name = r->in.value_name;
8653 return _spoolss_DeletePrinterDataEx(p, &r2);
8656 /****************************************************************
8657 _spoolss_AddForm
8658 ****************************************************************/
8660 WERROR _spoolss_AddForm(struct pipes_struct *p,
8661 struct spoolss_AddForm *r)
8663 struct spoolss_AddFormInfo1 *form;
8664 int snum = -1;
8665 WERROR status = WERR_OK;
8666 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
8667 struct dcerpc_binding_handle *b;
8668 TALLOC_CTX *tmp_ctx = NULL;
8670 DEBUG(5,("_spoolss_AddForm\n"));
8672 if (!Printer) {
8673 DEBUG(2,("_spoolss_AddForm: Invalid handle (%s:%u:%u).\n",
8674 OUR_HANDLE(r->in.handle)));
8675 return WERR_BADFID;
8678 /* if the user is not root, doesn't have SE_PRINT_OPERATOR privilege,
8679 and not a printer admin, then fail */
8681 if ((p->session_info->unix_token->uid != sec_initial_uid()) &&
8682 !security_token_has_privilege(p->session_info->security_token,
8683 SEC_PRIV_PRINT_OPERATOR)) {
8684 DEBUG(2,("_spoolss_Addform: denied by insufficient permissions.\n"));
8685 return WERR_ACCESS_DENIED;
8688 if (r->in.info_ctr->level != 1) {
8689 return WERR_INVALID_LEVEL;
8692 form = r->in.info_ctr->info.info1;
8693 if (!form) {
8694 return WERR_INVALID_PARAM;
8697 switch (form->flags) {
8698 case SPOOLSS_FORM_USER:
8699 case SPOOLSS_FORM_BUILTIN:
8700 case SPOOLSS_FORM_PRINTER:
8701 break;
8702 default:
8703 return WERR_INVALID_PARAM;
8706 tmp_ctx = talloc_new(p->mem_ctx);
8707 if (!tmp_ctx) {
8708 return WERR_NOMEM;
8711 status = winreg_printer_binding_handle(tmp_ctx,
8712 get_session_info_system(),
8713 p->msg_ctx,
8714 &b);
8715 if (!W_ERROR_IS_OK(status)) {
8716 goto done;
8719 status = winreg_printer_addform1(tmp_ctx, b, form);
8720 if (!W_ERROR_IS_OK(status)) {
8721 goto done;
8725 * ChangeID must always be set if this is a printer
8727 if (Printer->printer_type == SPLHND_PRINTER) {
8728 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
8729 status = WERR_BADFID;
8730 goto done;
8733 status = winreg_printer_update_changeid(tmp_ctx, b,
8734 lp_const_servicename(snum));
8737 done:
8738 talloc_free(tmp_ctx);
8739 return status;
8742 /****************************************************************
8743 _spoolss_DeleteForm
8744 ****************************************************************/
8746 WERROR _spoolss_DeleteForm(struct pipes_struct *p,
8747 struct spoolss_DeleteForm *r)
8749 const char *form_name = r->in.form_name;
8750 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
8751 int snum = -1;
8752 WERROR status = WERR_OK;
8753 struct dcerpc_binding_handle *b;
8754 TALLOC_CTX *tmp_ctx = NULL;
8756 DEBUG(5,("_spoolss_DeleteForm\n"));
8758 if (!Printer) {
8759 DEBUG(2,("_spoolss_DeleteForm: Invalid handle (%s:%u:%u).\n",
8760 OUR_HANDLE(r->in.handle)));
8761 return WERR_BADFID;
8764 if ((p->session_info->unix_token->uid != sec_initial_uid()) &&
8765 !security_token_has_privilege(p->session_info->security_token,
8766 SEC_PRIV_PRINT_OPERATOR)) {
8767 DEBUG(2,("_spoolss_DeleteForm: denied by insufficient permissions.\n"));
8768 return WERR_ACCESS_DENIED;
8771 tmp_ctx = talloc_new(p->mem_ctx);
8772 if (!tmp_ctx) {
8773 return WERR_NOMEM;
8776 status = winreg_printer_binding_handle(tmp_ctx,
8777 get_session_info_system(),
8778 p->msg_ctx,
8779 &b);
8780 if (!W_ERROR_IS_OK(status)) {
8781 goto done;
8784 status = winreg_printer_deleteform1(tmp_ctx, b, form_name);
8785 if (!W_ERROR_IS_OK(status)) {
8786 goto done;
8790 * ChangeID must always be set if this is a printer
8792 if (Printer->printer_type == SPLHND_PRINTER) {
8793 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
8794 status = WERR_BADFID;
8795 goto done;
8798 status = winreg_printer_update_changeid(tmp_ctx, b,
8799 lp_const_servicename(snum));
8802 done:
8803 talloc_free(tmp_ctx);
8804 return status;
8807 /****************************************************************
8808 _spoolss_SetForm
8809 ****************************************************************/
8811 WERROR _spoolss_SetForm(struct pipes_struct *p,
8812 struct spoolss_SetForm *r)
8814 struct spoolss_AddFormInfo1 *form;
8815 const char *form_name = r->in.form_name;
8816 int snum = -1;
8817 WERROR status = WERR_OK;
8818 struct dcerpc_binding_handle *b;
8819 TALLOC_CTX *tmp_ctx = NULL;
8821 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
8823 DEBUG(5,("_spoolss_SetForm\n"));
8825 if (!Printer) {
8826 DEBUG(2,("_spoolss_SetForm: Invalid handle (%s:%u:%u).\n",
8827 OUR_HANDLE(r->in.handle)));
8828 return WERR_BADFID;
8831 /* if the user is not root, doesn't have SE_PRINT_OPERATOR privilege,
8832 and not a printer admin, then fail */
8834 if ((p->session_info->unix_token->uid != sec_initial_uid()) &&
8835 !security_token_has_privilege(p->session_info->security_token,
8836 SEC_PRIV_PRINT_OPERATOR)) {
8837 DEBUG(2,("_spoolss_Setform: denied by insufficient permissions.\n"));
8838 return WERR_ACCESS_DENIED;
8841 if (r->in.info_ctr->level != 1) {
8842 return WERR_INVALID_LEVEL;
8845 form = r->in.info_ctr->info.info1;
8846 if (!form) {
8847 return WERR_INVALID_PARAM;
8850 tmp_ctx = talloc_new(p->mem_ctx);
8851 if (!tmp_ctx) {
8852 return WERR_NOMEM;
8855 status = winreg_printer_binding_handle(tmp_ctx,
8856 get_session_info_system(),
8857 p->msg_ctx,
8858 &b);
8859 if (!W_ERROR_IS_OK(status)) {
8860 goto done;
8863 status = winreg_printer_setform1(tmp_ctx, b,
8864 form_name,
8865 form);
8866 if (!W_ERROR_IS_OK(status)) {
8867 goto done;
8871 * ChangeID must always be set if this is a printer
8873 if (Printer->printer_type == SPLHND_PRINTER) {
8874 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
8875 status = WERR_BADFID;
8876 goto done;
8879 status = winreg_printer_update_changeid(tmp_ctx, b,
8880 lp_const_servicename(snum));
8883 done:
8884 talloc_free(tmp_ctx);
8885 return status;
8888 /****************************************************************************
8889 fill_print_processor1
8890 ****************************************************************************/
8892 static WERROR fill_print_processor1(TALLOC_CTX *mem_ctx,
8893 struct spoolss_PrintProcessorInfo1 *r,
8894 const char *print_processor_name)
8896 r->print_processor_name = talloc_strdup(mem_ctx, print_processor_name);
8897 W_ERROR_HAVE_NO_MEMORY(r->print_processor_name);
8899 return WERR_OK;
8902 /****************************************************************************
8903 enumprintprocessors level 1.
8904 ****************************************************************************/
8906 static WERROR enumprintprocessors_level_1(TALLOC_CTX *mem_ctx,
8907 union spoolss_PrintProcessorInfo **info_p,
8908 uint32_t *count)
8910 union spoolss_PrintProcessorInfo *info;
8911 WERROR result;
8913 info = talloc_array(mem_ctx, union spoolss_PrintProcessorInfo, 1);
8914 W_ERROR_HAVE_NO_MEMORY(info);
8916 *count = 1;
8918 result = fill_print_processor1(info, &info[0].info1, "winprint");
8919 if (!W_ERROR_IS_OK(result)) {
8920 goto out;
8923 out:
8924 if (!W_ERROR_IS_OK(result)) {
8925 TALLOC_FREE(info);
8926 *count = 0;
8927 return result;
8930 *info_p = info;
8932 return WERR_OK;
8935 /****************************************************************
8936 _spoolss_EnumPrintProcessors
8937 ****************************************************************/
8939 WERROR _spoolss_EnumPrintProcessors(struct pipes_struct *p,
8940 struct spoolss_EnumPrintProcessors *r)
8942 WERROR result;
8944 /* that's an [in out] buffer */
8946 if (!r->in.buffer && (r->in.offered != 0)) {
8947 return WERR_INVALID_PARAM;
8950 DEBUG(5,("_spoolss_EnumPrintProcessors\n"));
8953 * Enumerate the print processors ...
8955 * Just reply with "winprint", to keep NT happy
8956 * and I can use my nice printer checker.
8959 *r->out.count = 0;
8960 *r->out.needed = 0;
8961 *r->out.info = NULL;
8963 if (!get_short_archi(r->in.environment)) {
8964 return WERR_INVALID_ENVIRONMENT;
8967 switch (r->in.level) {
8968 case 1:
8969 result = enumprintprocessors_level_1(p->mem_ctx, r->out.info,
8970 r->out.count);
8971 break;
8972 default:
8973 return WERR_UNKNOWN_LEVEL;
8976 if (!W_ERROR_IS_OK(result)) {
8977 return result;
8980 *r->out.needed = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
8981 spoolss_EnumPrintProcessors,
8982 *r->out.info, r->in.level,
8983 *r->out.count);
8984 *r->out.info = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
8985 *r->out.count = SPOOLSS_BUFFER_OK(*r->out.count, 0);
8987 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
8990 /****************************************************************************
8991 fill_printprocdatatype1
8992 ****************************************************************************/
8994 static WERROR fill_printprocdatatype1(TALLOC_CTX *mem_ctx,
8995 struct spoolss_PrintProcDataTypesInfo1 *r,
8996 const char *name_array)
8998 r->name_array = talloc_strdup(mem_ctx, name_array);
8999 W_ERROR_HAVE_NO_MEMORY(r->name_array);
9001 return WERR_OK;
9004 /****************************************************************************
9005 enumprintprocdatatypes level 1.
9006 ****************************************************************************/
9008 static WERROR enumprintprocdatatypes_level_1(TALLOC_CTX *mem_ctx,
9009 union spoolss_PrintProcDataTypesInfo **info_p,
9010 uint32_t *count)
9012 WERROR result;
9013 union spoolss_PrintProcDataTypesInfo *info;
9015 info = talloc_array(mem_ctx, union spoolss_PrintProcDataTypesInfo, 1);
9016 W_ERROR_HAVE_NO_MEMORY(info);
9018 *count = 1;
9020 result = fill_printprocdatatype1(info, &info[0].info1, "RAW");
9021 if (!W_ERROR_IS_OK(result)) {
9022 goto out;
9025 out:
9026 if (!W_ERROR_IS_OK(result)) {
9027 TALLOC_FREE(info);
9028 *count = 0;
9029 return result;
9032 *info_p = info;
9034 return WERR_OK;
9037 /****************************************************************
9038 _spoolss_EnumPrintProcDataTypes
9039 ****************************************************************/
9041 WERROR _spoolss_EnumPrintProcDataTypes(struct pipes_struct *p,
9042 struct spoolss_EnumPrintProcDataTypes *r)
9044 WERROR result;
9046 /* that's an [in out] buffer */
9048 if (!r->in.buffer && (r->in.offered != 0)) {
9049 return WERR_INVALID_PARAM;
9052 DEBUG(5,("_spoolss_EnumPrintProcDataTypes\n"));
9054 *r->out.count = 0;
9055 *r->out.needed = 0;
9056 *r->out.info = NULL;
9058 if (r->in.print_processor_name == NULL ||
9059 !strequal(r->in.print_processor_name, "winprint")) {
9060 return WERR_UNKNOWN_PRINTPROCESSOR;
9063 switch (r->in.level) {
9064 case 1:
9065 result = enumprintprocdatatypes_level_1(p->mem_ctx, r->out.info,
9066 r->out.count);
9067 break;
9068 default:
9069 return WERR_UNKNOWN_LEVEL;
9072 if (!W_ERROR_IS_OK(result)) {
9073 return result;
9076 *r->out.needed = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
9077 spoolss_EnumPrintProcDataTypes,
9078 *r->out.info, r->in.level,
9079 *r->out.count);
9080 *r->out.info = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
9081 *r->out.count = SPOOLSS_BUFFER_OK(*r->out.count, 0);
9083 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
9086 /****************************************************************************
9087 fill_monitor_1
9088 ****************************************************************************/
9090 static WERROR fill_monitor_1(TALLOC_CTX *mem_ctx,
9091 struct spoolss_MonitorInfo1 *r,
9092 const char *monitor_name)
9094 r->monitor_name = talloc_strdup(mem_ctx, monitor_name);
9095 W_ERROR_HAVE_NO_MEMORY(r->monitor_name);
9097 return WERR_OK;
9100 /****************************************************************************
9101 fill_monitor_2
9102 ****************************************************************************/
9104 static WERROR fill_monitor_2(TALLOC_CTX *mem_ctx,
9105 struct spoolss_MonitorInfo2 *r,
9106 const char *monitor_name,
9107 const char *environment,
9108 const char *dll_name)
9110 r->monitor_name = talloc_strdup(mem_ctx, monitor_name);
9111 W_ERROR_HAVE_NO_MEMORY(r->monitor_name);
9112 r->environment = talloc_strdup(mem_ctx, environment);
9113 W_ERROR_HAVE_NO_MEMORY(r->environment);
9114 r->dll_name = talloc_strdup(mem_ctx, dll_name);
9115 W_ERROR_HAVE_NO_MEMORY(r->dll_name);
9117 return WERR_OK;
9120 /****************************************************************************
9121 enumprintmonitors level 1.
9122 ****************************************************************************/
9124 static WERROR enumprintmonitors_level_1(TALLOC_CTX *mem_ctx,
9125 union spoolss_MonitorInfo **info_p,
9126 uint32_t *count)
9128 union spoolss_MonitorInfo *info;
9129 WERROR result = WERR_OK;
9131 info = talloc_array(mem_ctx, union spoolss_MonitorInfo, 2);
9132 W_ERROR_HAVE_NO_MEMORY(info);
9134 *count = 2;
9136 result = fill_monitor_1(info, &info[0].info1,
9137 SPL_LOCAL_PORT);
9138 if (!W_ERROR_IS_OK(result)) {
9139 goto out;
9142 result = fill_monitor_1(info, &info[1].info1,
9143 SPL_TCPIP_PORT);
9144 if (!W_ERROR_IS_OK(result)) {
9145 goto out;
9148 out:
9149 if (!W_ERROR_IS_OK(result)) {
9150 TALLOC_FREE(info);
9151 *count = 0;
9152 return result;
9155 *info_p = info;
9157 return WERR_OK;
9160 /****************************************************************************
9161 enumprintmonitors level 2.
9162 ****************************************************************************/
9164 static WERROR enumprintmonitors_level_2(TALLOC_CTX *mem_ctx,
9165 union spoolss_MonitorInfo **info_p,
9166 uint32_t *count)
9168 union spoolss_MonitorInfo *info;
9169 WERROR result = WERR_OK;
9171 info = talloc_array(mem_ctx, union spoolss_MonitorInfo, 2);
9172 W_ERROR_HAVE_NO_MEMORY(info);
9174 *count = 2;
9176 result = fill_monitor_2(info, &info[0].info2,
9177 SPL_LOCAL_PORT,
9178 "Windows NT X86", /* FIXME */
9179 "localmon.dll");
9180 if (!W_ERROR_IS_OK(result)) {
9181 goto out;
9184 result = fill_monitor_2(info, &info[1].info2,
9185 SPL_TCPIP_PORT,
9186 "Windows NT X86", /* FIXME */
9187 "tcpmon.dll");
9188 if (!W_ERROR_IS_OK(result)) {
9189 goto out;
9192 out:
9193 if (!W_ERROR_IS_OK(result)) {
9194 TALLOC_FREE(info);
9195 *count = 0;
9196 return result;
9199 *info_p = info;
9201 return WERR_OK;
9204 /****************************************************************
9205 _spoolss_EnumMonitors
9206 ****************************************************************/
9208 WERROR _spoolss_EnumMonitors(struct pipes_struct *p,
9209 struct spoolss_EnumMonitors *r)
9211 WERROR result;
9213 /* that's an [in out] buffer */
9215 if (!r->in.buffer && (r->in.offered != 0)) {
9216 return WERR_INVALID_PARAM;
9219 DEBUG(5,("_spoolss_EnumMonitors\n"));
9222 * Enumerate the print monitors ...
9224 * Just reply with "Local Port", to keep NT happy
9225 * and I can use my nice printer checker.
9228 *r->out.count = 0;
9229 *r->out.needed = 0;
9230 *r->out.info = NULL;
9232 switch (r->in.level) {
9233 case 1:
9234 result = enumprintmonitors_level_1(p->mem_ctx, r->out.info,
9235 r->out.count);
9236 break;
9237 case 2:
9238 result = enumprintmonitors_level_2(p->mem_ctx, r->out.info,
9239 r->out.count);
9240 break;
9241 default:
9242 return WERR_UNKNOWN_LEVEL;
9245 if (!W_ERROR_IS_OK(result)) {
9246 return result;
9249 *r->out.needed = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
9250 spoolss_EnumMonitors,
9251 *r->out.info, r->in.level,
9252 *r->out.count);
9253 *r->out.info = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
9254 *r->out.count = SPOOLSS_BUFFER_OK(*r->out.count, 0);
9256 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
9259 /****************************************************************************
9260 ****************************************************************************/
9262 static WERROR getjob_level_1(TALLOC_CTX *mem_ctx,
9263 const print_queue_struct *queue,
9264 int count, int snum,
9265 struct spoolss_PrinterInfo2 *pinfo2,
9266 uint32_t jobid,
9267 struct spoolss_JobInfo1 *r)
9269 int i = 0;
9270 bool found = false;
9272 for (i=0; i<count; i++) {
9273 if (queue[i].sysjob == (int)jobid) {
9274 found = true;
9275 break;
9279 if (found == false) {
9280 /* NT treats not found as bad param... yet another bad choice */
9281 return WERR_INVALID_PARAM;
9284 return fill_job_info1(mem_ctx,
9286 &queue[i],
9288 snum,
9289 pinfo2);
9292 /****************************************************************************
9293 ****************************************************************************/
9295 static WERROR getjob_level_2(TALLOC_CTX *mem_ctx,
9296 const print_queue_struct *queue,
9297 int count, int snum,
9298 struct spoolss_PrinterInfo2 *pinfo2,
9299 uint32_t jobid,
9300 struct spoolss_JobInfo2 *r)
9302 int i = 0;
9303 bool found = false;
9304 struct spoolss_DeviceMode *devmode;
9305 WERROR result;
9307 for (i=0; i<count; i++) {
9308 if (queue[i].sysjob == (int)jobid) {
9309 found = true;
9310 break;
9314 if (found == false) {
9315 /* NT treats not found as bad param... yet another bad
9316 choice */
9317 return WERR_INVALID_PARAM;
9321 * if the print job does not have a DEVMODE associated with it,
9322 * just use the one for the printer. A NULL devicemode is not
9323 * a failure condition
9326 devmode = print_job_devmode(mem_ctx, lp_const_servicename(snum), jobid);
9327 if (!devmode) {
9328 result = spoolss_create_default_devmode(mem_ctx,
9329 pinfo2->printername,
9330 &devmode);
9331 if (!W_ERROR_IS_OK(result)) {
9332 DEBUG(3, ("Can't proceed w/o a devmode!"));
9333 return result;
9337 return fill_job_info2(mem_ctx,
9339 &queue[i],
9341 snum,
9342 pinfo2,
9343 devmode);
9346 /****************************************************************
9347 _spoolss_GetJob
9348 ****************************************************************/
9350 WERROR _spoolss_GetJob(struct pipes_struct *p,
9351 struct spoolss_GetJob *r)
9353 WERROR result = WERR_OK;
9354 struct spoolss_PrinterInfo2 *pinfo2 = NULL;
9355 int snum;
9356 int count;
9357 print_queue_struct *queue = NULL;
9358 print_status_struct prt_status;
9360 /* that's an [in out] buffer */
9362 if (!r->in.buffer && (r->in.offered != 0)) {
9363 return WERR_INVALID_PARAM;
9366 DEBUG(5,("_spoolss_GetJob\n"));
9368 *r->out.needed = 0;
9370 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
9371 return WERR_BADFID;
9374 result = winreg_get_printer_internal(p->mem_ctx,
9375 get_session_info_system(),
9376 p->msg_ctx,
9377 lp_const_servicename(snum),
9378 &pinfo2);
9379 if (!W_ERROR_IS_OK(result)) {
9380 return result;
9383 count = print_queue_status(p->msg_ctx, snum, &queue, &prt_status);
9385 DEBUGADD(4,("count:[%d], prt_status:[%d], [%s]\n",
9386 count, prt_status.status, prt_status.message));
9388 switch (r->in.level) {
9389 case 1:
9390 result = getjob_level_1(p->mem_ctx,
9391 queue, count, snum, pinfo2,
9392 r->in.job_id, &r->out.info->info1);
9393 break;
9394 case 2:
9395 result = getjob_level_2(p->mem_ctx,
9396 queue, count, snum, pinfo2,
9397 r->in.job_id, &r->out.info->info2);
9398 break;
9399 default:
9400 result = WERR_UNKNOWN_LEVEL;
9401 break;
9404 SAFE_FREE(queue);
9405 TALLOC_FREE(pinfo2);
9407 if (!W_ERROR_IS_OK(result)) {
9408 TALLOC_FREE(r->out.info);
9409 return result;
9412 *r->out.needed = SPOOLSS_BUFFER_UNION(spoolss_JobInfo, r->out.info,
9413 r->in.level);
9414 r->out.info = SPOOLSS_BUFFER_OK(r->out.info, NULL);
9416 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
9419 /****************************************************************
9420 _spoolss_GetPrinterDataEx
9421 ****************************************************************/
9423 WERROR _spoolss_GetPrinterDataEx(struct pipes_struct *p,
9424 struct spoolss_GetPrinterDataEx *r)
9427 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
9428 const char *printer;
9429 int snum = 0;
9430 WERROR result = WERR_OK;
9431 DATA_BLOB blob;
9432 enum winreg_Type val_type = REG_NONE;
9433 uint8_t *val_data = NULL;
9434 uint32_t val_size = 0;
9435 struct dcerpc_binding_handle *b;
9436 TALLOC_CTX *tmp_ctx;
9438 DEBUG(4,("_spoolss_GetPrinterDataEx\n"));
9440 DEBUG(10, ("_spoolss_GetPrinterDataEx: key => [%s], value => [%s]\n",
9441 r->in.key_name, r->in.value_name));
9443 /* in case of problem, return some default values */
9445 *r->out.needed = 0;
9446 *r->out.type = REG_NONE;
9448 tmp_ctx = talloc_new(p->mem_ctx);
9449 if (!tmp_ctx) {
9450 return WERR_NOMEM;
9453 if (!Printer) {
9454 DEBUG(2,("_spoolss_GetPrinterDataEx: Invalid handle (%s:%u:%u).\n",
9455 OUR_HANDLE(r->in.handle)));
9456 result = WERR_BADFID;
9457 goto done;
9460 /* check to see if the keyname is valid */
9461 if (!strlen(r->in.key_name)) {
9462 result = WERR_INVALID_PARAM;
9463 goto done;
9466 /* Is the handle to a printer or to the server? */
9468 if (Printer->printer_type == SPLHND_SERVER) {
9470 union spoolss_PrinterData data;
9472 result = getprinterdata_printer_server(tmp_ctx,
9473 r->in.value_name,
9474 r->out.type,
9475 &data);
9476 if (!W_ERROR_IS_OK(result)) {
9477 goto done;
9480 result = push_spoolss_PrinterData(tmp_ctx, &blob,
9481 *r->out.type, &data);
9482 if (!W_ERROR_IS_OK(result)) {
9483 goto done;
9486 *r->out.needed = blob.length;
9488 if (r->in.offered >= *r->out.needed) {
9489 memcpy(r->out.data, blob.data, blob.length);
9492 result = WERR_OK;
9493 goto done;
9496 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
9497 result = WERR_BADFID;
9498 goto done;
9500 printer = lp_const_servicename(snum);
9502 result = winreg_printer_binding_handle(tmp_ctx,
9503 get_session_info_system(),
9504 p->msg_ctx,
9505 &b);
9506 if (!W_ERROR_IS_OK(result)) {
9507 goto done;
9510 /* XP sends this and wants the ChangeID value from PRINTER_INFO_0 */
9511 if (strequal(r->in.key_name, SPOOL_PRINTERDATA_KEY) &&
9512 strequal(r->in.value_name, "ChangeId")) {
9513 *r->out.type = REG_DWORD;
9514 *r->out.needed = 4;
9515 if (r->in.offered >= *r->out.needed) {
9516 uint32_t changeid = 0;
9518 result = winreg_printer_get_changeid(tmp_ctx, b,
9519 printer,
9520 &changeid);
9521 if (!W_ERROR_IS_OK(result)) {
9522 goto done;
9525 SIVAL(r->out.data, 0, changeid);
9526 result = WERR_OK;
9528 goto done;
9531 result = winreg_get_printer_dataex(tmp_ctx, b,
9532 printer,
9533 r->in.key_name,
9534 r->in.value_name,
9535 &val_type,
9536 &val_data,
9537 &val_size);
9538 if (!W_ERROR_IS_OK(result)) {
9539 goto done;
9542 *r->out.needed = val_size;
9543 *r->out.type = val_type;
9545 if (r->in.offered >= *r->out.needed) {
9546 memcpy(r->out.data, val_data, val_size);
9549 done:
9550 /* NOTE: do not replace type when returning WERR_MORE_DATA */
9552 if (W_ERROR_IS_OK(result)) {
9553 result = SPOOLSS_BUFFER_OK(WERR_OK, WERR_MORE_DATA);
9556 talloc_free(tmp_ctx);
9557 return result;
9560 /****************************************************************
9561 _spoolss_SetPrinterDataEx
9562 ****************************************************************/
9564 WERROR _spoolss_SetPrinterDataEx(struct pipes_struct *p,
9565 struct spoolss_SetPrinterDataEx *r)
9567 struct spoolss_PrinterInfo2 *pinfo2 = NULL;
9568 int snum = 0;
9569 WERROR result = WERR_OK;
9570 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
9571 char *oid_string;
9572 struct dcerpc_binding_handle *b;
9573 TALLOC_CTX *tmp_ctx;
9575 DEBUG(4,("_spoolss_SetPrinterDataEx\n"));
9577 /* From MSDN documentation of SetPrinterDataEx: pass request to
9578 SetPrinterData if key is "PrinterDriverData" */
9580 if (!Printer) {
9581 DEBUG(2,("_spoolss_SetPrinterDataEx: Invalid handle (%s:%u:%u).\n",
9582 OUR_HANDLE(r->in.handle)));
9583 return WERR_BADFID;
9586 if (Printer->printer_type == SPLHND_SERVER) {
9587 DEBUG(10,("_spoolss_SetPrinterDataEx: "
9588 "Not implemented for server handles yet\n"));
9589 return WERR_INVALID_PARAM;
9592 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
9593 return WERR_BADFID;
9597 * Access check : NT returns "access denied" if you make a
9598 * SetPrinterData call without the necessary privildge.
9599 * we were originally returning OK if nothing changed
9600 * which made Win2k issue **a lot** of SetPrinterData
9601 * when connecting to a printer --jerry
9604 if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
9605 DEBUG(3, ("_spoolss_SetPrinterDataEx: "
9606 "change denied by handle access permissions\n"));
9607 return WERR_ACCESS_DENIED;
9610 tmp_ctx = talloc_new(p->mem_ctx);
9611 if (!tmp_ctx) {
9612 return WERR_NOMEM;
9615 result = winreg_printer_binding_handle(tmp_ctx,
9616 get_session_info_system(),
9617 p->msg_ctx,
9618 &b);
9619 if (!W_ERROR_IS_OK(result)) {
9620 goto done;
9623 result = winreg_get_printer(tmp_ctx, b,
9624 lp_servicename(talloc_tos(), snum),
9625 &pinfo2);
9626 if (!W_ERROR_IS_OK(result)) {
9627 goto done;
9630 /* check for OID in valuename */
9632 oid_string = strchr(r->in.value_name, ',');
9633 if (oid_string) {
9634 *oid_string = '\0';
9635 oid_string++;
9638 /* save the registry data */
9640 result = winreg_set_printer_dataex(tmp_ctx, b,
9641 pinfo2->sharename,
9642 r->in.key_name,
9643 r->in.value_name,
9644 r->in.type,
9645 r->in.data,
9646 r->in.offered);
9648 if (W_ERROR_IS_OK(result)) {
9649 /* save the OID if one was specified */
9650 if (oid_string) {
9651 char *str = talloc_asprintf(tmp_ctx, "%s\\%s",
9652 r->in.key_name, SPOOL_OID_KEY);
9653 if (!str) {
9654 result = WERR_NOMEM;
9655 goto done;
9659 * I'm not checking the status here on purpose. Don't know
9660 * if this is right, but I'm returning the status from the
9661 * previous set_printer_dataex() call. I have no idea if
9662 * this is right. --jerry
9664 winreg_set_printer_dataex(tmp_ctx, b,
9665 pinfo2->sharename,
9666 str,
9667 r->in.value_name,
9668 REG_SZ,
9669 (uint8_t *) oid_string,
9670 strlen(oid_string) + 1);
9673 result = winreg_printer_update_changeid(tmp_ctx, b,
9674 lp_const_servicename(snum));
9678 done:
9679 talloc_free(tmp_ctx);
9680 return result;
9683 /****************************************************************
9684 _spoolss_DeletePrinterDataEx
9685 ****************************************************************/
9687 WERROR _spoolss_DeletePrinterDataEx(struct pipes_struct *p,
9688 struct spoolss_DeletePrinterDataEx *r)
9690 const char *printer;
9691 int snum=0;
9692 WERROR status = WERR_OK;
9693 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
9695 DEBUG(5,("_spoolss_DeletePrinterDataEx\n"));
9697 if (!Printer) {
9698 DEBUG(2,("_spoolss_DeletePrinterDataEx: "
9699 "Invalid handle (%s:%u:%u).\n",
9700 OUR_HANDLE(r->in.handle)));
9701 return WERR_BADFID;
9704 if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
9705 DEBUG(3, ("_spoolss_DeletePrinterDataEx: "
9706 "printer properties change denied by handle\n"));
9707 return WERR_ACCESS_DENIED;
9710 if (!r->in.value_name || !r->in.key_name) {
9711 return WERR_NOMEM;
9714 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
9715 return WERR_BADFID;
9717 printer = lp_const_servicename(snum);
9719 status = winreg_delete_printer_dataex_internal(p->mem_ctx,
9720 get_session_info_system(),
9721 p->msg_ctx,
9722 printer,
9723 r->in.key_name,
9724 r->in.value_name);
9725 if (W_ERROR_IS_OK(status)) {
9726 status = winreg_printer_update_changeid_internal(p->mem_ctx,
9727 get_session_info_system(),
9728 p->msg_ctx,
9729 printer);
9732 return status;
9735 /****************************************************************
9736 _spoolss_EnumPrinterKey
9737 ****************************************************************/
9739 WERROR _spoolss_EnumPrinterKey(struct pipes_struct *p,
9740 struct spoolss_EnumPrinterKey *r)
9742 uint32_t num_keys;
9743 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
9744 int snum = 0;
9745 WERROR result = WERR_BADFILE;
9746 const char **array = NULL;
9747 DATA_BLOB blob;
9749 DEBUG(4,("_spoolss_EnumPrinterKey\n"));
9751 if (!Printer) {
9752 DEBUG(2,("_spoolss_EnumPrinterKey: Invalid handle (%s:%u:%u).\n",
9753 OUR_HANDLE(r->in.handle)));
9754 return WERR_BADFID;
9757 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
9758 return WERR_BADFID;
9761 result = winreg_enum_printer_key_internal(p->mem_ctx,
9762 get_session_info_system(),
9763 p->msg_ctx,
9764 lp_const_servicename(snum),
9765 r->in.key_name,
9766 &num_keys,
9767 &array);
9768 if (!W_ERROR_IS_OK(result)) {
9769 goto done;
9772 if (!push_reg_multi_sz(p->mem_ctx, &blob, array)) {
9773 result = WERR_NOMEM;
9774 goto done;
9777 *r->out._ndr_size = r->in.offered / 2;
9778 *r->out.needed = blob.length;
9780 if (r->in.offered < *r->out.needed) {
9781 result = WERR_MORE_DATA;
9782 } else {
9783 result = WERR_OK;
9784 r->out.key_buffer->string_array = array;
9787 done:
9788 if (!W_ERROR_IS_OK(result)) {
9789 TALLOC_FREE(array);
9790 if (!W_ERROR_EQUAL(result, WERR_MORE_DATA)) {
9791 *r->out.needed = 0;
9795 return result;
9798 /****************************************************************
9799 _spoolss_DeletePrinterKey
9800 ****************************************************************/
9802 WERROR _spoolss_DeletePrinterKey(struct pipes_struct *p,
9803 struct spoolss_DeletePrinterKey *r)
9805 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
9806 int snum=0;
9807 WERROR status;
9808 const char *printer;
9809 struct dcerpc_binding_handle *b;
9810 TALLOC_CTX *tmp_ctx;
9812 DEBUG(5,("_spoolss_DeletePrinterKey\n"));
9814 if (!Printer) {
9815 DEBUG(2,("_spoolss_DeletePrinterKey: Invalid handle (%s:%u:%u).\n",
9816 OUR_HANDLE(r->in.handle)));
9817 return WERR_BADFID;
9820 /* if keyname == NULL, return error */
9821 if ( !r->in.key_name )
9822 return WERR_INVALID_PARAM;
9824 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
9825 return WERR_BADFID;
9828 if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
9829 DEBUG(3, ("_spoolss_DeletePrinterKey: "
9830 "printer properties change denied by handle\n"));
9831 return WERR_ACCESS_DENIED;
9834 printer = lp_const_servicename(snum);
9836 tmp_ctx = talloc_new(p->mem_ctx);
9837 if (!tmp_ctx) {
9838 return WERR_NOMEM;
9841 status = winreg_printer_binding_handle(tmp_ctx,
9842 get_session_info_system(),
9843 p->msg_ctx,
9844 &b);
9845 if (!W_ERROR_IS_OK(status)) {
9846 goto done;
9849 /* delete the key and all subkeys */
9850 status = winreg_delete_printer_key(tmp_ctx, b,
9851 printer,
9852 r->in.key_name);
9853 if (W_ERROR_IS_OK(status)) {
9854 status = winreg_printer_update_changeid(tmp_ctx, b,
9855 printer);
9858 done:
9859 talloc_free(tmp_ctx);
9860 return status;
9863 /****************************************************************
9864 _spoolss_EnumPrinterDataEx
9865 ****************************************************************/
9867 WERROR _spoolss_EnumPrinterDataEx(struct pipes_struct *p,
9868 struct spoolss_EnumPrinterDataEx *r)
9870 uint32_t count = 0;
9871 struct spoolss_PrinterEnumValues *info = NULL;
9872 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
9873 int snum;
9874 WERROR result;
9876 DEBUG(4,("_spoolss_EnumPrinterDataEx\n"));
9878 *r->out.count = 0;
9879 *r->out.needed = 0;
9880 *r->out.info = NULL;
9882 if (!Printer) {
9883 DEBUG(2,("_spoolss_EnumPrinterDataEx: Invalid handle (%s:%u:%u1<).\n",
9884 OUR_HANDLE(r->in.handle)));
9885 return WERR_BADFID;
9889 * first check for a keyname of NULL or "". Win2k seems to send
9890 * this a lot and we should send back WERR_INVALID_PARAM
9891 * no need to spend time looking up the printer in this case.
9892 * --jerry
9895 if (!strlen(r->in.key_name)) {
9896 result = WERR_INVALID_PARAM;
9897 goto done;
9900 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
9901 return WERR_BADFID;
9904 /* now look for a match on the key name */
9905 result = winreg_enum_printer_dataex_internal(p->mem_ctx,
9906 get_session_info_system(),
9907 p->msg_ctx,
9908 lp_const_servicename(snum),
9909 r->in.key_name,
9910 &count,
9911 &info);
9912 if (!W_ERROR_IS_OK(result)) {
9913 goto done;
9916 #if 0 /* FIXME - gd */
9917 /* housekeeping information in the reply */
9919 /* Fix from Martin Zielinski <mz@seh.de> - ensure
9920 * the hand marshalled container size is a multiple
9921 * of 4 bytes for RPC alignment.
9924 if (needed % 4) {
9925 needed += 4-(needed % 4);
9927 #endif
9928 *r->out.count = count;
9929 *r->out.info = info;
9931 done:
9932 if (!W_ERROR_IS_OK(result)) {
9933 return result;
9936 *r->out.needed = SPOOLSS_BUFFER_ARRAY(p->mem_ctx,
9937 spoolss_EnumPrinterDataEx,
9938 *r->out.info,
9939 *r->out.count);
9940 *r->out.info = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
9941 *r->out.count = SPOOLSS_BUFFER_OK(*r->out.count, *r->out.count);
9943 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_MORE_DATA);
9946 /****************************************************************************
9947 ****************************************************************************/
9949 static WERROR getprintprocessordirectory_level_1(TALLOC_CTX *mem_ctx,
9950 const char *servername,
9951 const char *environment,
9952 struct spoolss_PrintProcessorDirectoryInfo1 *r)
9954 WERROR werr;
9955 char *path = NULL;
9957 werr = compose_spoolss_server_path(mem_ctx,
9958 servername,
9959 environment,
9960 SPOOLSS_PRTPROCS_PATH,
9961 &path);
9962 if (!W_ERROR_IS_OK(werr)) {
9963 return werr;
9966 DEBUG(4,("print processor directory: [%s]\n", path));
9968 r->directory_name = path;
9970 return WERR_OK;
9973 /****************************************************************
9974 _spoolss_GetPrintProcessorDirectory
9975 ****************************************************************/
9977 WERROR _spoolss_GetPrintProcessorDirectory(struct pipes_struct *p,
9978 struct spoolss_GetPrintProcessorDirectory *r)
9980 WERROR result;
9981 char *prnproc_share = NULL;
9982 bool prnproc_share_exists = false;
9983 int snum;
9985 /* that's an [in out] buffer */
9987 if (!r->in.buffer && (r->in.offered != 0)) {
9988 return WERR_INVALID_PARAM;
9991 DEBUG(5,("_spoolss_GetPrintProcessorDirectory: level %d\n",
9992 r->in.level));
9994 *r->out.needed = 0;
9996 /* r->in.level is ignored */
9998 /* We always should reply with a local print processor directory so that
9999 * users are not forced to have a [prnproc$] share on the Samba spoolss
10000 * server, if users decide to do so, lets announce it though - Guenther */
10002 snum = find_service(talloc_tos(), "prnproc$", &prnproc_share);
10003 if (!prnproc_share) {
10004 return WERR_NOMEM;
10006 if (snum != -1) {
10007 prnproc_share_exists = true;
10010 result = getprintprocessordirectory_level_1(p->mem_ctx,
10011 prnproc_share_exists ? r->in.server : NULL,
10012 r->in.environment,
10013 &r->out.info->info1);
10014 if (!W_ERROR_IS_OK(result)) {
10015 TALLOC_FREE(r->out.info);
10016 return result;
10019 *r->out.needed = SPOOLSS_BUFFER_UNION(spoolss_PrintProcessorDirectoryInfo,
10020 r->out.info, r->in.level);
10021 r->out.info = SPOOLSS_BUFFER_OK(r->out.info, NULL);
10023 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
10026 /*******************************************************************
10027 ********************************************************************/
10029 static bool push_monitorui_buf(TALLOC_CTX *mem_ctx, DATA_BLOB *buf,
10030 const char *dllname)
10032 enum ndr_err_code ndr_err;
10033 struct spoolss_MonitorUi ui;
10035 ui.dll_name = dllname;
10037 ndr_err = ndr_push_struct_blob(buf, mem_ctx, &ui,
10038 (ndr_push_flags_fn_t)ndr_push_spoolss_MonitorUi);
10039 if (NDR_ERR_CODE_IS_SUCCESS(ndr_err) && (DEBUGLEVEL >= 10)) {
10040 NDR_PRINT_DEBUG(spoolss_MonitorUi, &ui);
10042 return NDR_ERR_CODE_IS_SUCCESS(ndr_err);
10045 /*******************************************************************
10046 Streams the monitor UI DLL name in UNICODE
10047 *******************************************************************/
10049 static WERROR xcvtcp_monitorui(TALLOC_CTX *mem_ctx,
10050 struct security_token *token, DATA_BLOB *in,
10051 DATA_BLOB *out, uint32_t *needed)
10053 const char *dllname = "tcpmonui.dll";
10055 *needed = (strlen(dllname)+1) * 2;
10057 if (out->length < *needed) {
10058 return WERR_INSUFFICIENT_BUFFER;
10061 if (!push_monitorui_buf(mem_ctx, out, dllname)) {
10062 return WERR_NOMEM;
10065 return WERR_OK;
10068 /*******************************************************************
10069 ********************************************************************/
10071 static bool pull_port_data_1(TALLOC_CTX *mem_ctx,
10072 struct spoolss_PortData1 *port1,
10073 const DATA_BLOB *buf)
10075 enum ndr_err_code ndr_err;
10076 ndr_err = ndr_pull_struct_blob(buf, mem_ctx, port1,
10077 (ndr_pull_flags_fn_t)ndr_pull_spoolss_PortData1);
10078 if (NDR_ERR_CODE_IS_SUCCESS(ndr_err) && (DEBUGLEVEL >= 10)) {
10079 NDR_PRINT_DEBUG(spoolss_PortData1, port1);
10081 return NDR_ERR_CODE_IS_SUCCESS(ndr_err);
10084 /*******************************************************************
10085 ********************************************************************/
10087 static bool pull_port_data_2(TALLOC_CTX *mem_ctx,
10088 struct spoolss_PortData2 *port2,
10089 const DATA_BLOB *buf)
10091 enum ndr_err_code ndr_err;
10092 ndr_err = ndr_pull_struct_blob(buf, mem_ctx, port2,
10093 (ndr_pull_flags_fn_t)ndr_pull_spoolss_PortData2);
10094 if (NDR_ERR_CODE_IS_SUCCESS(ndr_err) && (DEBUGLEVEL >= 10)) {
10095 NDR_PRINT_DEBUG(spoolss_PortData2, port2);
10097 return NDR_ERR_CODE_IS_SUCCESS(ndr_err);
10100 /*******************************************************************
10101 Create a new TCP/IP port
10102 *******************************************************************/
10104 static WERROR xcvtcp_addport(TALLOC_CTX *mem_ctx,
10105 struct security_token *token, DATA_BLOB *in,
10106 DATA_BLOB *out, uint32_t *needed)
10108 struct spoolss_PortData1 port1;
10109 struct spoolss_PortData2 port2;
10110 char *device_uri = NULL;
10111 uint32_t version;
10113 const char *portname;
10114 const char *hostaddress;
10115 const char *queue;
10116 uint32_t port_number;
10117 uint32_t protocol;
10119 /* peek for spoolss_PortData version */
10121 if (!in || (in->length < (128 + 4))) {
10122 return WERR_GENERAL_FAILURE;
10125 version = IVAL(in->data, 128);
10127 switch (version) {
10128 case 1:
10129 ZERO_STRUCT(port1);
10131 if (!pull_port_data_1(mem_ctx, &port1, in)) {
10132 return WERR_NOMEM;
10135 portname = port1.portname;
10136 hostaddress = port1.hostaddress;
10137 queue = port1.queue;
10138 protocol = port1.protocol;
10139 port_number = port1.port_number;
10141 break;
10142 case 2:
10143 ZERO_STRUCT(port2);
10145 if (!pull_port_data_2(mem_ctx, &port2, in)) {
10146 return WERR_NOMEM;
10149 portname = port2.portname;
10150 hostaddress = port2.hostaddress;
10151 queue = port2.queue;
10152 protocol = port2.protocol;
10153 port_number = port2.port_number;
10155 break;
10156 default:
10157 DEBUG(1,("xcvtcp_addport: "
10158 "unknown version of port_data: %d\n", version));
10159 return WERR_UNKNOWN_PORT;
10162 /* create the device URI and call the add_port_hook() */
10164 switch (protocol) {
10165 case PROTOCOL_RAWTCP_TYPE:
10166 device_uri = talloc_asprintf(mem_ctx,
10167 "socket://%s:%d/", hostaddress,
10168 port_number);
10169 break;
10171 case PROTOCOL_LPR_TYPE:
10172 device_uri = talloc_asprintf(mem_ctx,
10173 "lpr://%s/%s", hostaddress, queue );
10174 break;
10176 default:
10177 return WERR_UNKNOWN_PORT;
10180 if (!device_uri) {
10181 return WERR_NOMEM;
10184 return add_port_hook(mem_ctx, token, portname, device_uri);
10187 /*******************************************************************
10188 *******************************************************************/
10190 struct xcv_api_table xcvtcp_cmds[] = {
10191 { "MonitorUI", xcvtcp_monitorui },
10192 { "AddPort", xcvtcp_addport},
10193 { NULL, NULL }
10196 static WERROR process_xcvtcp_command(TALLOC_CTX *mem_ctx,
10197 struct security_token *token, const char *command,
10198 DATA_BLOB *inbuf,
10199 DATA_BLOB *outbuf,
10200 uint32_t *needed )
10202 int i;
10204 DEBUG(10,("process_xcvtcp_command: Received command \"%s\"\n", command));
10206 for ( i=0; xcvtcp_cmds[i].name; i++ ) {
10207 if ( strcmp( command, xcvtcp_cmds[i].name ) == 0 )
10208 return xcvtcp_cmds[i].fn(mem_ctx, token, inbuf, outbuf, needed);
10211 return WERR_BADFUNC;
10214 /*******************************************************************
10215 *******************************************************************/
10216 #if 0 /* don't support management using the "Local Port" monitor */
10218 static WERROR xcvlocal_monitorui(TALLOC_CTX *mem_ctx,
10219 struct security_token *token, DATA_BLOB *in,
10220 DATA_BLOB *out, uint32_t *needed)
10222 const char *dllname = "localui.dll";
10224 *needed = (strlen(dllname)+1) * 2;
10226 if (out->length < *needed) {
10227 return WERR_INSUFFICIENT_BUFFER;
10230 if (!push_monitorui_buf(mem_ctx, out, dllname)) {
10231 return WERR_NOMEM;
10234 return WERR_OK;
10237 /*******************************************************************
10238 *******************************************************************/
10240 struct xcv_api_table xcvlocal_cmds[] = {
10241 { "MonitorUI", xcvlocal_monitorui },
10242 { NULL, NULL }
10244 #else
10245 struct xcv_api_table xcvlocal_cmds[] = {
10246 { NULL, NULL }
10248 #endif
10252 /*******************************************************************
10253 *******************************************************************/
10255 static WERROR process_xcvlocal_command(TALLOC_CTX *mem_ctx,
10256 struct security_token *token, const char *command,
10257 DATA_BLOB *inbuf, DATA_BLOB *outbuf,
10258 uint32_t *needed)
10260 int i;
10262 DEBUG(10,("process_xcvlocal_command: Received command \"%s\"\n", command));
10264 for ( i=0; xcvlocal_cmds[i].name; i++ ) {
10265 if ( strcmp( command, xcvlocal_cmds[i].name ) == 0 )
10266 return xcvlocal_cmds[i].fn(mem_ctx, token, inbuf, outbuf, needed);
10268 return WERR_BADFUNC;
10271 /****************************************************************
10272 _spoolss_XcvData
10273 ****************************************************************/
10275 WERROR _spoolss_XcvData(struct pipes_struct *p,
10276 struct spoolss_XcvData *r)
10278 struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
10279 DATA_BLOB out_data = data_blob_null;
10280 WERROR werror;
10282 if (!Printer) {
10283 DEBUG(2,("_spoolss_XcvData: Invalid handle (%s:%u:%u).\n",
10284 OUR_HANDLE(r->in.handle)));
10285 return WERR_BADFID;
10288 /* Has to be a handle to the TCP/IP port monitor */
10290 if ( !(Printer->printer_type & (SPLHND_PORTMON_LOCAL|SPLHND_PORTMON_TCP)) ) {
10291 DEBUG(2,("_spoolss_XcvData: Call only valid for Port Monitors\n"));
10292 return WERR_BADFID;
10295 /* requires administrative access to the server */
10297 if ( !(Printer->access_granted & SERVER_ACCESS_ADMINISTER) ) {
10298 DEBUG(2,("_spoolss_XcvData: denied by handle permissions.\n"));
10299 return WERR_ACCESS_DENIED;
10302 /* Allocate the outgoing buffer */
10304 if (r->in.out_data_size) {
10305 out_data = data_blob_talloc_zero(p->mem_ctx, r->in.out_data_size);
10306 if (out_data.data == NULL) {
10307 return WERR_NOMEM;
10311 switch ( Printer->printer_type ) {
10312 case SPLHND_PORTMON_TCP:
10313 werror = process_xcvtcp_command(p->mem_ctx,
10314 p->session_info->security_token,
10315 r->in.function_name,
10316 &r->in.in_data, &out_data,
10317 r->out.needed);
10318 break;
10319 case SPLHND_PORTMON_LOCAL:
10320 werror = process_xcvlocal_command(p->mem_ctx,
10321 p->session_info->security_token,
10322 r->in.function_name,
10323 &r->in.in_data, &out_data,
10324 r->out.needed);
10325 break;
10326 default:
10327 werror = WERR_INVALID_PRINT_MONITOR;
10330 if (!W_ERROR_IS_OK(werror)) {
10331 return werror;
10334 *r->out.status_code = 0;
10336 if (r->out.out_data && out_data.data && r->in.out_data_size && out_data.length) {
10337 memcpy(r->out.out_data, out_data.data,
10338 MIN(r->in.out_data_size, out_data.length));
10341 return WERR_OK;
10344 /****************************************************************
10345 _spoolss_AddPrintProcessor
10346 ****************************************************************/
10348 WERROR _spoolss_AddPrintProcessor(struct pipes_struct *p,
10349 struct spoolss_AddPrintProcessor *r)
10351 /* for now, just indicate success and ignore the add. We'll
10352 automatically set the winprint processor for printer
10353 entries later. Used to debug the LexMark Optra S 1855 PCL
10354 driver --jerry */
10356 return WERR_OK;
10359 /****************************************************************
10360 _spoolss_AddPort
10361 ****************************************************************/
10363 WERROR _spoolss_AddPort(struct pipes_struct *p,
10364 struct spoolss_AddPort *r)
10366 /* do what w2k3 does */
10368 return WERR_NOT_SUPPORTED;
10371 /****************************************************************
10372 _spoolss_GetPrinterDriver
10373 ****************************************************************/
10375 WERROR _spoolss_GetPrinterDriver(struct pipes_struct *p,
10376 struct spoolss_GetPrinterDriver *r)
10378 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10379 return WERR_NOT_SUPPORTED;
10382 /****************************************************************
10383 _spoolss_ReadPrinter
10384 ****************************************************************/
10386 WERROR _spoolss_ReadPrinter(struct pipes_struct *p,
10387 struct spoolss_ReadPrinter *r)
10389 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10390 return WERR_NOT_SUPPORTED;
10393 /****************************************************************
10394 _spoolss_WaitForPrinterChange
10395 ****************************************************************/
10397 WERROR _spoolss_WaitForPrinterChange(struct pipes_struct *p,
10398 struct spoolss_WaitForPrinterChange *r)
10400 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10401 return WERR_NOT_SUPPORTED;
10404 /****************************************************************
10405 _spoolss_ConfigurePort
10406 ****************************************************************/
10408 WERROR _spoolss_ConfigurePort(struct pipes_struct *p,
10409 struct spoolss_ConfigurePort *r)
10411 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10412 return WERR_NOT_SUPPORTED;
10415 /****************************************************************
10416 _spoolss_DeletePort
10417 ****************************************************************/
10419 WERROR _spoolss_DeletePort(struct pipes_struct *p,
10420 struct spoolss_DeletePort *r)
10422 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10423 return WERR_NOT_SUPPORTED;
10426 /****************************************************************
10427 _spoolss_CreatePrinterIC
10428 ****************************************************************/
10430 WERROR _spoolss_CreatePrinterIC(struct pipes_struct *p,
10431 struct spoolss_CreatePrinterIC *r)
10433 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10434 return WERR_NOT_SUPPORTED;
10437 /****************************************************************
10438 _spoolss_PlayGDIScriptOnPrinterIC
10439 ****************************************************************/
10441 WERROR _spoolss_PlayGDIScriptOnPrinterIC(struct pipes_struct *p,
10442 struct spoolss_PlayGDIScriptOnPrinterIC *r)
10444 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10445 return WERR_NOT_SUPPORTED;
10448 /****************************************************************
10449 _spoolss_DeletePrinterIC
10450 ****************************************************************/
10452 WERROR _spoolss_DeletePrinterIC(struct pipes_struct *p,
10453 struct spoolss_DeletePrinterIC *r)
10455 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10456 return WERR_NOT_SUPPORTED;
10459 /****************************************************************
10460 _spoolss_AddPrinterConnection
10461 ****************************************************************/
10463 WERROR _spoolss_AddPrinterConnection(struct pipes_struct *p,
10464 struct spoolss_AddPrinterConnection *r)
10466 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10467 return WERR_NOT_SUPPORTED;
10470 /****************************************************************
10471 _spoolss_DeletePrinterConnection
10472 ****************************************************************/
10474 WERROR _spoolss_DeletePrinterConnection(struct pipes_struct *p,
10475 struct spoolss_DeletePrinterConnection *r)
10477 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10478 return WERR_NOT_SUPPORTED;
10481 /****************************************************************
10482 _spoolss_PrinterMessageBox
10483 ****************************************************************/
10485 WERROR _spoolss_PrinterMessageBox(struct pipes_struct *p,
10486 struct spoolss_PrinterMessageBox *r)
10488 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10489 return WERR_NOT_SUPPORTED;
10492 /****************************************************************
10493 _spoolss_AddMonitor
10494 ****************************************************************/
10496 WERROR _spoolss_AddMonitor(struct pipes_struct *p,
10497 struct spoolss_AddMonitor *r)
10499 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10500 return WERR_NOT_SUPPORTED;
10503 /****************************************************************
10504 _spoolss_DeleteMonitor
10505 ****************************************************************/
10507 WERROR _spoolss_DeleteMonitor(struct pipes_struct *p,
10508 struct spoolss_DeleteMonitor *r)
10510 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10511 return WERR_NOT_SUPPORTED;
10514 /****************************************************************
10515 _spoolss_DeletePrintProcessor
10516 ****************************************************************/
10518 WERROR _spoolss_DeletePrintProcessor(struct pipes_struct *p,
10519 struct spoolss_DeletePrintProcessor *r)
10521 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10522 return WERR_NOT_SUPPORTED;
10525 /****************************************************************
10526 _spoolss_AddPrintProvidor
10527 ****************************************************************/
10529 WERROR _spoolss_AddPrintProvidor(struct pipes_struct *p,
10530 struct spoolss_AddPrintProvidor *r)
10532 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10533 return WERR_NOT_SUPPORTED;
10536 /****************************************************************
10537 _spoolss_DeletePrintProvidor
10538 ****************************************************************/
10540 WERROR _spoolss_DeletePrintProvidor(struct pipes_struct *p,
10541 struct spoolss_DeletePrintProvidor *r)
10543 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10544 return WERR_NOT_SUPPORTED;
10547 /****************************************************************
10548 _spoolss_FindFirstPrinterChangeNotification
10549 ****************************************************************/
10551 WERROR _spoolss_FindFirstPrinterChangeNotification(struct pipes_struct *p,
10552 struct spoolss_FindFirstPrinterChangeNotification *r)
10554 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10555 return WERR_NOT_SUPPORTED;
10558 /****************************************************************
10559 _spoolss_FindNextPrinterChangeNotification
10560 ****************************************************************/
10562 WERROR _spoolss_FindNextPrinterChangeNotification(struct pipes_struct *p,
10563 struct spoolss_FindNextPrinterChangeNotification *r)
10565 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10566 return WERR_NOT_SUPPORTED;
10569 /****************************************************************
10570 _spoolss_RouterFindFirstPrinterChangeNotificationOld
10571 ****************************************************************/
10573 WERROR _spoolss_RouterFindFirstPrinterChangeNotificationOld(struct pipes_struct *p,
10574 struct spoolss_RouterFindFirstPrinterChangeNotificationOld *r)
10576 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10577 return WERR_NOT_SUPPORTED;
10580 /****************************************************************
10581 _spoolss_ReplyOpenPrinter
10582 ****************************************************************/
10584 WERROR _spoolss_ReplyOpenPrinter(struct pipes_struct *p,
10585 struct spoolss_ReplyOpenPrinter *r)
10587 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10588 return WERR_NOT_SUPPORTED;
10591 /****************************************************************
10592 _spoolss_RouterReplyPrinter
10593 ****************************************************************/
10595 WERROR _spoolss_RouterReplyPrinter(struct pipes_struct *p,
10596 struct spoolss_RouterReplyPrinter *r)
10598 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10599 return WERR_NOT_SUPPORTED;
10602 /****************************************************************
10603 _spoolss_ReplyClosePrinter
10604 ****************************************************************/
10606 WERROR _spoolss_ReplyClosePrinter(struct pipes_struct *p,
10607 struct spoolss_ReplyClosePrinter *r)
10609 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10610 return WERR_NOT_SUPPORTED;
10613 /****************************************************************
10614 _spoolss_AddPortEx
10615 ****************************************************************/
10617 WERROR _spoolss_AddPortEx(struct pipes_struct *p,
10618 struct spoolss_AddPortEx *r)
10620 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10621 return WERR_NOT_SUPPORTED;
10624 /****************************************************************
10625 _spoolss_RouterFindFirstPrinterChangeNotification
10626 ****************************************************************/
10628 WERROR _spoolss_RouterFindFirstPrinterChangeNotification(struct pipes_struct *p,
10629 struct spoolss_RouterFindFirstPrinterChangeNotification *r)
10631 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10632 return WERR_NOT_SUPPORTED;
10635 /****************************************************************
10636 _spoolss_SpoolerInit
10637 ****************************************************************/
10639 WERROR _spoolss_SpoolerInit(struct pipes_struct *p,
10640 struct spoolss_SpoolerInit *r)
10642 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10643 return WERR_NOT_SUPPORTED;
10646 /****************************************************************
10647 _spoolss_ResetPrinterEx
10648 ****************************************************************/
10650 WERROR _spoolss_ResetPrinterEx(struct pipes_struct *p,
10651 struct spoolss_ResetPrinterEx *r)
10653 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10654 return WERR_NOT_SUPPORTED;
10657 /****************************************************************
10658 _spoolss_RouterReplyPrinterEx
10659 ****************************************************************/
10661 WERROR _spoolss_RouterReplyPrinterEx(struct pipes_struct *p,
10662 struct spoolss_RouterReplyPrinterEx *r)
10664 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10665 return WERR_NOT_SUPPORTED;
10668 /****************************************************************
10669 _spoolss_44
10670 ****************************************************************/
10672 WERROR _spoolss_44(struct pipes_struct *p,
10673 struct spoolss_44 *r)
10675 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10676 return WERR_NOT_SUPPORTED;
10679 /****************************************************************
10680 _spoolss_SetPort
10681 ****************************************************************/
10683 WERROR _spoolss_SetPort(struct pipes_struct *p,
10684 struct spoolss_SetPort *r)
10686 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10687 return WERR_NOT_SUPPORTED;
10690 /****************************************************************
10691 _spoolss_4a
10692 ****************************************************************/
10694 WERROR _spoolss_4a(struct pipes_struct *p,
10695 struct spoolss_4a *r)
10697 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10698 return WERR_NOT_SUPPORTED;
10701 /****************************************************************
10702 _spoolss_4b
10703 ****************************************************************/
10705 WERROR _spoolss_4b(struct pipes_struct *p,
10706 struct spoolss_4b *r)
10708 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10709 return WERR_NOT_SUPPORTED;
10712 /****************************************************************
10713 _spoolss_4c
10714 ****************************************************************/
10716 WERROR _spoolss_4c(struct pipes_struct *p,
10717 struct spoolss_4c *r)
10719 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10720 return WERR_NOT_SUPPORTED;
10723 /****************************************************************
10724 _spoolss_53
10725 ****************************************************************/
10727 WERROR _spoolss_53(struct pipes_struct *p,
10728 struct spoolss_53 *r)
10730 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10731 return WERR_NOT_SUPPORTED;
10734 /****************************************************************
10735 _spoolss_AddPerMachineConnection
10736 ****************************************************************/
10738 WERROR _spoolss_AddPerMachineConnection(struct pipes_struct *p,
10739 struct spoolss_AddPerMachineConnection *r)
10741 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10742 return WERR_NOT_SUPPORTED;
10745 /****************************************************************
10746 _spoolss_DeletePerMachineConnection
10747 ****************************************************************/
10749 WERROR _spoolss_DeletePerMachineConnection(struct pipes_struct *p,
10750 struct spoolss_DeletePerMachineConnection *r)
10752 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10753 return WERR_NOT_SUPPORTED;
10756 /****************************************************************
10757 _spoolss_EnumPerMachineConnections
10758 ****************************************************************/
10760 WERROR _spoolss_EnumPerMachineConnections(struct pipes_struct *p,
10761 struct spoolss_EnumPerMachineConnections *r)
10763 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10764 return WERR_NOT_SUPPORTED;
10767 /****************************************************************
10768 _spoolss_5a
10769 ****************************************************************/
10771 WERROR _spoolss_5a(struct pipes_struct *p,
10772 struct spoolss_5a *r)
10774 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10775 return WERR_NOT_SUPPORTED;
10778 /****************************************************************
10779 _spoolss_5b
10780 ****************************************************************/
10782 WERROR _spoolss_5b(struct pipes_struct *p,
10783 struct spoolss_5b *r)
10785 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10786 return WERR_NOT_SUPPORTED;
10789 /****************************************************************
10790 _spoolss_5c
10791 ****************************************************************/
10793 WERROR _spoolss_5c(struct pipes_struct *p,
10794 struct spoolss_5c *r)
10796 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10797 return WERR_NOT_SUPPORTED;
10800 /****************************************************************
10801 _spoolss_5d
10802 ****************************************************************/
10804 WERROR _spoolss_5d(struct pipes_struct *p,
10805 struct spoolss_5d *r)
10807 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10808 return WERR_NOT_SUPPORTED;
10811 /****************************************************************
10812 _spoolss_5e
10813 ****************************************************************/
10815 WERROR _spoolss_5e(struct pipes_struct *p,
10816 struct spoolss_5e *r)
10818 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10819 return WERR_NOT_SUPPORTED;
10822 /****************************************************************
10823 _spoolss_5f
10824 ****************************************************************/
10826 WERROR _spoolss_5f(struct pipes_struct *p,
10827 struct spoolss_5f *r)
10829 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10830 return WERR_NOT_SUPPORTED;
10833 /****************************************************************
10834 _spoolss_60
10835 ****************************************************************/
10837 WERROR _spoolss_60(struct pipes_struct *p,
10838 struct spoolss_60 *r)
10840 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10841 return WERR_NOT_SUPPORTED;
10844 /****************************************************************
10845 _spoolss_61
10846 ****************************************************************/
10848 WERROR _spoolss_61(struct pipes_struct *p,
10849 struct spoolss_61 *r)
10851 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10852 return WERR_NOT_SUPPORTED;
10855 /****************************************************************
10856 _spoolss_62
10857 ****************************************************************/
10859 WERROR _spoolss_62(struct pipes_struct *p,
10860 struct spoolss_62 *r)
10862 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10863 return WERR_NOT_SUPPORTED;
10866 /****************************************************************
10867 _spoolss_63
10868 ****************************************************************/
10870 WERROR _spoolss_63(struct pipes_struct *p,
10871 struct spoolss_63 *r)
10873 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10874 return WERR_NOT_SUPPORTED;
10877 /****************************************************************
10878 _spoolss_64
10879 ****************************************************************/
10881 WERROR _spoolss_64(struct pipes_struct *p,
10882 struct spoolss_64 *r)
10884 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10885 return WERR_NOT_SUPPORTED;
10888 /****************************************************************
10889 _spoolss_65
10890 ****************************************************************/
10892 WERROR _spoolss_65(struct pipes_struct *p,
10893 struct spoolss_65 *r)
10895 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10896 return WERR_NOT_SUPPORTED;
10899 /****************************************************************
10900 _spoolss_GetCorePrinterDrivers
10901 ****************************************************************/
10903 WERROR _spoolss_GetCorePrinterDrivers(struct pipes_struct *p,
10904 struct spoolss_GetCorePrinterDrivers *r)
10906 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10907 return WERR_NOT_SUPPORTED;
10910 /****************************************************************
10911 _spoolss_67
10912 ****************************************************************/
10914 WERROR _spoolss_67(struct pipes_struct *p,
10915 struct spoolss_67 *r)
10917 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10918 return WERR_NOT_SUPPORTED;
10921 /****************************************************************
10922 _spoolss_GetPrinterDriverPackagePath
10923 ****************************************************************/
10925 WERROR _spoolss_GetPrinterDriverPackagePath(struct pipes_struct *p,
10926 struct spoolss_GetPrinterDriverPackagePath *r)
10928 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10929 return WERR_NOT_SUPPORTED;
10932 /****************************************************************
10933 _spoolss_69
10934 ****************************************************************/
10936 WERROR _spoolss_69(struct pipes_struct *p,
10937 struct spoolss_69 *r)
10939 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10940 return WERR_NOT_SUPPORTED;
10943 /****************************************************************
10944 _spoolss_6a
10945 ****************************************************************/
10947 WERROR _spoolss_6a(struct pipes_struct *p,
10948 struct spoolss_6a *r)
10950 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10951 return WERR_NOT_SUPPORTED;
10954 /****************************************************************
10955 _spoolss_6b
10956 ****************************************************************/
10958 WERROR _spoolss_6b(struct pipes_struct *p,
10959 struct spoolss_6b *r)
10961 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10962 return WERR_NOT_SUPPORTED;
10965 /****************************************************************
10966 _spoolss_6c
10967 ****************************************************************/
10969 WERROR _spoolss_6c(struct pipes_struct *p,
10970 struct spoolss_6c *r)
10972 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10973 return WERR_NOT_SUPPORTED;
10976 /****************************************************************
10977 _spoolss_6d
10978 ****************************************************************/
10980 WERROR _spoolss_6d(struct pipes_struct *p,
10981 struct spoolss_6d *r)
10983 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10984 return WERR_NOT_SUPPORTED;
10987 /****************************************************************
10988 _spoolss_RpcGetJobNamedPropertyValue
10989 ****************************************************************/
10991 WERROR _spoolss_RpcGetJobNamedPropertyValue(struct pipes_struct *p,
10992 struct spoolss_RpcGetJobNamedPropertyValue *r)
10994 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
10995 return WERR_NOT_SUPPORTED;
10998 /****************************************************************
10999 _spoolss_RpcSetJobNamedProperty
11000 ****************************************************************/
11002 WERROR _spoolss_RpcSetJobNamedProperty(struct pipes_struct *p,
11003 struct spoolss_RpcSetJobNamedProperty *r)
11005 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
11006 return WERR_NOT_SUPPORTED;
11009 /****************************************************************
11010 _spoolss_RpcDeleteJobNamedProperty
11011 ****************************************************************/
11013 WERROR _spoolss_RpcDeleteJobNamedProperty(struct pipes_struct *p,
11014 struct spoolss_RpcDeleteJobNamedProperty *r)
11016 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
11017 return WERR_NOT_SUPPORTED;
11020 /****************************************************************
11021 _spoolss_RpcEnumJobNamedProperties
11022 ****************************************************************/
11024 WERROR _spoolss_RpcEnumJobNamedProperties(struct pipes_struct *p,
11025 struct spoolss_RpcEnumJobNamedProperties *r)
11027 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
11028 return WERR_NOT_SUPPORTED;