s3-printing: Fix Bug #7541, %D in "printer admin" causing smbd crash.
[Samba/bb.git] / source3 / rpc_server / srv_spoolss_nt.c
blob4b5385be26e974762e30b44cf91026b1e1238d30
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.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 3 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <http://www.gnu.org/licenses/>.
26 /* Since the SPOOLSS rpc routines are basically DOS 16-bit calls wrapped
27 up, all the errors returned are DOS errors, not NT status codes. */
29 #include "includes.h"
30 #include "../librpc/gen_ndr/srv_spoolss.h"
31 #include "../librpc/gen_ndr/cli_spoolss.h"
33 /* macros stolen from s4 spoolss server */
34 #define SPOOLSS_BUFFER_UNION(fn,ic,info,level) \
35 ((info)?ndr_size_##fn(info, level, ic, 0):0)
37 #define SPOOLSS_BUFFER_UNION_ARRAY(mem_ctx,fn,ic,info,level,count) \
38 ((info)?ndr_size_##fn##_info(mem_ctx, ic, level, count, info):0)
40 #define SPOOLSS_BUFFER_ARRAY(mem_ctx,fn,ic,info,count) \
41 ((info)?ndr_size_##fn##_info(mem_ctx, ic, count, info):0)
43 #define SPOOLSS_BUFFER_OK(val_true,val_false) ((r->in.offered >= *r->out.needed)?val_true:val_false)
46 extern userdom_struct current_user_info;
48 #undef DBGC_CLASS
49 #define DBGC_CLASS DBGC_RPC_SRV
51 #ifndef MAX_OPEN_PRINTER_EXS
52 #define MAX_OPEN_PRINTER_EXS 50
53 #endif
55 #define MAGIC_DISPLAY_FREQUENCY 0xfade2bad
56 #define PHANTOM_DEVMODE_KEY "_p_f_a_n_t_0_m_"
58 static Printer_entry *printers_list;
60 typedef struct _counter_printer_0 {
61 struct _counter_printer_0 *next;
62 struct _counter_printer_0 *prev;
64 int snum;
65 uint32_t counter;
66 } counter_printer_0;
68 static counter_printer_0 *counter_list;
70 static struct rpc_pipe_client *notify_cli_pipe; /* print notify back-channel pipe handle*/
71 static uint32_t smb_connections = 0;
74 /* in printing/nt_printing.c */
76 extern struct standard_mapping printer_std_mapping, printserver_std_mapping;
78 /* API table for Xcv Monitor functions */
80 struct xcv_api_table {
81 const char *name;
82 WERROR(*fn) (TALLOC_CTX *mem_ctx, NT_USER_TOKEN *token, DATA_BLOB *in, DATA_BLOB *out, uint32_t *needed);
85 /********************************************************************
86 * Canonicalize servername.
87 ********************************************************************/
89 static const char *canon_servername(const char *servername)
91 const char *pservername = servername;
92 while (*pservername == '\\') {
93 pservername++;
95 return pservername;
98 /* translate between internal status numbers and NT status numbers */
99 static int nt_printj_status(int v)
101 switch (v) {
102 case LPQ_QUEUED:
103 return 0;
104 case LPQ_PAUSED:
105 return JOB_STATUS_PAUSED;
106 case LPQ_SPOOLING:
107 return JOB_STATUS_SPOOLING;
108 case LPQ_PRINTING:
109 return JOB_STATUS_PRINTING;
110 case LPQ_ERROR:
111 return JOB_STATUS_ERROR;
112 case LPQ_DELETING:
113 return JOB_STATUS_DELETING;
114 case LPQ_OFFLINE:
115 return JOB_STATUS_OFFLINE;
116 case LPQ_PAPEROUT:
117 return JOB_STATUS_PAPEROUT;
118 case LPQ_PRINTED:
119 return JOB_STATUS_PRINTED;
120 case LPQ_DELETED:
121 return JOB_STATUS_DELETED;
122 case LPQ_BLOCKED:
123 return JOB_STATUS_BLOCKED_DEVQ;
124 case LPQ_USER_INTERVENTION:
125 return JOB_STATUS_USER_INTERVENTION;
127 return 0;
130 static int nt_printq_status(int v)
132 switch (v) {
133 case LPQ_PAUSED:
134 return PRINTER_STATUS_PAUSED;
135 case LPQ_QUEUED:
136 case LPQ_SPOOLING:
137 case LPQ_PRINTING:
138 return 0;
140 return 0;
143 /***************************************************************************
144 Disconnect from the client
145 ****************************************************************************/
147 static void srv_spoolss_replycloseprinter(int snum, struct policy_handle *handle)
149 WERROR result;
150 NTSTATUS status;
153 * Tell the specific printing tdb we no longer want messages for this printer
154 * by deregistering our PID.
157 if (!print_notify_deregister_pid(snum))
158 DEBUG(0,("print_notify_register_pid: Failed to register our pid for printer %s\n", lp_const_servicename(snum) ));
160 /* weird if the test succeds !!! */
161 if (smb_connections==0) {
162 DEBUG(0,("srv_spoolss_replycloseprinter:Trying to close non-existant notify backchannel !\n"));
163 return;
166 status = rpccli_spoolss_ReplyClosePrinter(notify_cli_pipe, talloc_tos(),
167 handle,
168 &result);
169 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result))
170 DEBUG(0,("srv_spoolss_replycloseprinter: reply_close_printer failed [%s].\n",
171 win_errstr(result)));
173 /* if it's the last connection, deconnect the IPC$ share */
174 if (smb_connections==1) {
176 cli_shutdown( rpc_pipe_np_smb_conn(notify_cli_pipe) );
177 notify_cli_pipe = NULL; /* The above call shuts downn the pipe also. */
179 messaging_deregister(smbd_messaging_context(),
180 MSG_PRINTER_NOTIFY2, NULL);
182 /* Tell the connections db we're no longer interested in
183 * printer notify messages. */
185 register_message_flags(false, FLAG_MSG_PRINT_NOTIFY);
188 smb_connections--;
191 /****************************************************************************
192 Functions to free a printer entry datastruct.
193 ****************************************************************************/
195 static int printer_entry_destructor(Printer_entry *Printer)
197 if (Printer->notify.client_connected == true) {
198 int snum = -1;
200 if ( Printer->printer_type == SPLHND_SERVER) {
201 snum = -1;
202 srv_spoolss_replycloseprinter(snum, &Printer->notify.client_hnd);
203 } else if (Printer->printer_type == SPLHND_PRINTER) {
204 snum = print_queue_snum(Printer->sharename);
205 if (snum != -1)
206 srv_spoolss_replycloseprinter(snum,
207 &Printer->notify.client_hnd);
211 Printer->notify.flags=0;
212 Printer->notify.options=0;
213 Printer->notify.localmachine[0]='\0';
214 Printer->notify.printerlocal=0;
215 TALLOC_FREE(Printer->notify.option);
216 Printer->notify.client_connected = false;
218 free_nt_devicemode( &Printer->nt_devmode );
219 free_a_printer( &Printer->printer_info, 2 );
221 /* Remove from the internal list. */
222 DLIST_REMOVE(printers_list, Printer);
223 return 0;
226 /****************************************************************************
227 find printer index by handle
228 ****************************************************************************/
230 static Printer_entry *find_printer_index_by_hnd(pipes_struct *p,
231 struct policy_handle *hnd)
233 Printer_entry *find_printer = NULL;
235 if(!find_policy_by_hnd(p,hnd,(void **)(void *)&find_printer)) {
236 DEBUG(2,("find_printer_index_by_hnd: Printer handle not found: "));
237 return NULL;
240 return find_printer;
243 /****************************************************************************
244 Close printer index by handle.
245 ****************************************************************************/
247 static bool close_printer_handle(pipes_struct *p, struct policy_handle *hnd)
249 Printer_entry *Printer = find_printer_index_by_hnd(p, hnd);
251 if (!Printer) {
252 DEBUG(2,("close_printer_handle: Invalid handle (%s:%u:%u)\n",
253 OUR_HANDLE(hnd)));
254 return false;
257 close_policy_hnd(p, hnd);
259 return true;
262 /****************************************************************************
263 Delete a printer given a handle.
264 ****************************************************************************/
266 static WERROR delete_printer_hook(TALLOC_CTX *ctx, NT_USER_TOKEN *token, const char *sharename)
268 char *cmd = lp_deleteprinter_cmd();
269 char *command = NULL;
270 int ret;
271 SE_PRIV se_printop = SE_PRINT_OPERATOR;
272 bool is_print_op = false;
274 /* can't fail if we don't try */
276 if ( !*cmd )
277 return WERR_OK;
279 command = talloc_asprintf(ctx,
280 "%s \"%s\"",
281 cmd, sharename);
282 if (!command) {
283 return WERR_NOMEM;
285 if ( token )
286 is_print_op = user_has_privileges( token, &se_printop );
288 DEBUG(10,("Running [%s]\n", command));
290 /********** BEGIN SePrintOperatorPrivlege BLOCK **********/
292 if ( is_print_op )
293 become_root();
295 if ( (ret = smbrun(command, NULL)) == 0 ) {
296 /* Tell everyone we updated smb.conf. */
297 message_send_all(smbd_messaging_context(),
298 MSG_SMB_CONF_UPDATED, NULL, 0, NULL);
301 if ( is_print_op )
302 unbecome_root();
304 /********** END SePrintOperatorPrivlege BLOCK **********/
306 DEBUGADD(10,("returned [%d]\n", ret));
308 TALLOC_FREE(command);
310 if (ret != 0)
311 return WERR_BADFID; /* What to return here? */
313 /* go ahead and re-read the services immediately */
314 become_root();
315 reload_services(false);
316 unbecome_root();
318 if ( lp_servicenumber( sharename ) < 0 )
319 return WERR_ACCESS_DENIED;
321 return WERR_OK;
324 /****************************************************************************
325 Delete a printer given a handle.
326 ****************************************************************************/
328 static WERROR delete_printer_handle(pipes_struct *p, struct policy_handle *hnd)
330 Printer_entry *Printer = find_printer_index_by_hnd(p, hnd);
332 if (!Printer) {
333 DEBUG(2,("delete_printer_handle: Invalid handle (%s:%u:%u)\n",
334 OUR_HANDLE(hnd)));
335 return WERR_BADFID;
339 * It turns out that Windows allows delete printer on a handle
340 * opened by an admin user, then used on a pipe handle created
341 * by an anonymous user..... but they're working on security.... riiight !
342 * JRA.
345 if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
346 DEBUG(3, ("delete_printer_handle: denied by handle\n"));
347 return WERR_ACCESS_DENIED;
350 /* this does not need a become root since the access check has been
351 done on the handle already */
353 if (del_a_printer( Printer->sharename ) != 0) {
354 DEBUG(3,("Error deleting printer %s\n", Printer->sharename));
355 return WERR_BADFID;
358 return delete_printer_hook(p->mem_ctx, p->server_info->ptok,
359 Printer->sharename );
362 /****************************************************************************
363 Return the snum of a printer corresponding to an handle.
364 ****************************************************************************/
366 static bool get_printer_snum(pipes_struct *p, struct policy_handle *hnd,
367 int *number, struct share_params **params)
369 Printer_entry *Printer = find_printer_index_by_hnd(p, hnd);
371 if (!Printer) {
372 DEBUG(2,("get_printer_snum: Invalid handle (%s:%u:%u)\n",
373 OUR_HANDLE(hnd)));
374 return false;
377 switch (Printer->printer_type) {
378 case SPLHND_PRINTER:
379 DEBUG(4,("short name:%s\n", Printer->sharename));
380 *number = print_queue_snum(Printer->sharename);
381 return (*number != -1);
382 case SPLHND_SERVER:
383 return false;
384 default:
385 return false;
389 /****************************************************************************
390 Set printer handle type.
391 Check if it's \\server or \\server\printer
392 ****************************************************************************/
394 static bool set_printer_hnd_printertype(Printer_entry *Printer, const char *handlename)
396 DEBUG(3,("Setting printer type=%s\n", handlename));
398 if ( strlen(handlename) < 3 ) {
399 DEBUGADD(4,("A print server must have at least 1 char ! %s\n", handlename));
400 return false;
403 /* it's a print server */
404 if (*handlename=='\\' && *(handlename+1)=='\\' && !strchr_m(handlename+2, '\\')) {
405 DEBUGADD(4,("Printer is a print server\n"));
406 Printer->printer_type = SPLHND_SERVER;
408 /* it's a printer (set_printer_hnd_name() will handle port monitors */
409 else {
410 DEBUGADD(4,("Printer is a printer\n"));
411 Printer->printer_type = SPLHND_PRINTER;
414 return true;
417 /****************************************************************************
418 Set printer handle name.. Accept names like \\server, \\server\printer,
419 \\server\SHARE, & "\\server\,XcvMonitor Standard TCP/IP Port" See
420 the MSDN docs regarding OpenPrinter() for details on the XcvData() and
421 XcvDataPort() interface.
422 ****************************************************************************/
424 static bool set_printer_hnd_name(Printer_entry *Printer, const char *handlename)
426 int snum;
427 int n_services=lp_numservices();
428 char *aprinter, *printername;
429 const char *servername;
430 fstring sname;
431 bool found = false;
432 NT_PRINTER_INFO_LEVEL *printer = NULL;
433 WERROR result;
435 DEBUG(4,("Setting printer name=%s (len=%lu)\n", handlename,
436 (unsigned long)strlen(handlename)));
438 aprinter = CONST_DISCARD(char *, handlename);
439 if ( *handlename == '\\' ) {
440 servername = canon_servername(handlename);
441 if ( (aprinter = strchr_m( servername, '\\' )) != NULL ) {
442 *aprinter = '\0';
443 aprinter++;
445 } else {
446 servername = global_myname();
449 /* save the servername to fill in replies on this handle */
451 if ( !is_myname_or_ipaddr( servername ) )
452 return false;
454 fstrcpy( Printer->servername, servername );
456 if ( Printer->printer_type == SPLHND_SERVER )
457 return true;
459 if ( Printer->printer_type != SPLHND_PRINTER )
460 return false;
462 DEBUGADD(5, ("searching for [%s]\n", aprinter ));
464 /* check for the Port Monitor Interface */
466 if ( strequal( aprinter, SPL_XCV_MONITOR_TCPMON ) ) {
467 Printer->printer_type = SPLHND_PORTMON_TCP;
468 fstrcpy(sname, SPL_XCV_MONITOR_TCPMON);
469 found = true;
471 else if ( strequal( aprinter, SPL_XCV_MONITOR_LOCALMON ) ) {
472 Printer->printer_type = SPLHND_PORTMON_LOCAL;
473 fstrcpy(sname, SPL_XCV_MONITOR_LOCALMON);
474 found = true;
477 /* Search all sharenames first as this is easier than pulling
478 the printer_info_2 off of disk. Don't use find_service() since
479 that calls out to map_username() */
481 /* do another loop to look for printernames */
483 for (snum=0; !found && snum<n_services; snum++) {
485 /* no point going on if this is not a printer */
487 if ( !(lp_snum_ok(snum) && lp_print_ok(snum)) )
488 continue;
490 fstrcpy(sname, lp_servicename(snum));
491 if ( strequal( aprinter, sname ) ) {
492 found = true;
493 break;
496 /* no point looking up the printer object if
497 we aren't allowing printername != sharename */
499 if ( lp_force_printername(snum) )
500 continue;
502 fstrcpy(sname, lp_servicename(snum));
504 printer = NULL;
506 /* This call doesn't fill in the location or comment from
507 * a CUPS server for efficiency with large numbers of printers.
508 * JRA.
511 result = get_a_printer_search( NULL, &printer, 2, sname );
512 if ( !W_ERROR_IS_OK(result) ) {
513 DEBUG(0,("set_printer_hnd_name: failed to lookup printer [%s] -- result [%s]\n",
514 sname, win_errstr(result)));
515 continue;
518 /* printername is always returned as \\server\printername */
519 if ( !(printername = strchr_m(&printer->info_2->printername[2], '\\')) ) {
520 DEBUG(0,("set_printer_hnd_name: info2->printername in wrong format! [%s]\n",
521 printer->info_2->printername));
522 free_a_printer( &printer, 2);
523 continue;
526 printername++;
528 if ( strequal(printername, aprinter) ) {
529 free_a_printer( &printer, 2);
530 found = true;
531 break;
534 DEBUGADD(10, ("printername: %s\n", printername));
536 free_a_printer( &printer, 2);
539 free_a_printer( &printer, 2);
541 if ( !found ) {
542 DEBUGADD(4,("Printer not found\n"));
543 return false;
546 DEBUGADD(4,("set_printer_hnd_name: Printer found: %s -> %s\n", aprinter, sname));
548 fstrcpy(Printer->sharename, sname);
550 return true;
553 /****************************************************************************
554 Find first available printer slot. creates a printer handle for you.
555 ****************************************************************************/
557 static bool open_printer_hnd(pipes_struct *p, struct policy_handle *hnd,
558 const char *name, uint32_t access_granted)
560 Printer_entry *new_printer;
562 DEBUG(10,("open_printer_hnd: name [%s]\n", name));
564 new_printer = TALLOC_ZERO_P(NULL, Printer_entry);
565 if (new_printer == NULL) {
566 return false;
568 talloc_set_destructor(new_printer, printer_entry_destructor);
570 if (!create_policy_hnd(p, hnd, new_printer)) {
571 TALLOC_FREE(new_printer);
572 return false;
575 /* Add to the internal list. */
576 DLIST_ADD(printers_list, new_printer);
578 new_printer->notify.option=NULL;
580 if (!set_printer_hnd_printertype(new_printer, name)) {
581 close_printer_handle(p, hnd);
582 return false;
585 if (!set_printer_hnd_name(new_printer, name)) {
586 close_printer_handle(p, hnd);
587 return false;
590 new_printer->access_granted = access_granted;
592 DEBUG(5, ("%d printer handles active\n",
593 (int)num_pipe_handles(p->pipe_handles)));
595 return true;
598 /***************************************************************************
599 check to see if the client motify handle is monitoring the notification
600 given by (notify_type, notify_field).
601 **************************************************************************/
603 static bool is_monitoring_event_flags(uint32_t flags, uint16_t notify_type,
604 uint16_t notify_field)
606 return true;
609 static bool is_monitoring_event(Printer_entry *p, uint16_t notify_type,
610 uint16_t notify_field)
612 struct spoolss_NotifyOption *option = p->notify.option;
613 uint32_t i, j;
616 * Flags should always be zero when the change notify
617 * is registered by the client's spooler. A user Win32 app
618 * might use the flags though instead of the NOTIFY_OPTION_INFO
619 * --jerry
622 if (!option) {
623 return false;
626 if (p->notify.flags)
627 return is_monitoring_event_flags(
628 p->notify.flags, notify_type, notify_field);
630 for (i = 0; i < option->count; i++) {
632 /* Check match for notify_type */
634 if (option->types[i].type != notify_type)
635 continue;
637 /* Check match for field */
639 for (j = 0; j < option->types[i].count; j++) {
640 if (option->types[i].fields[j].field == notify_field) {
641 return true;
646 DEBUG(10, ("Open handle for \\\\%s\\%s is not monitoring 0x%02x/0x%02x\n",
647 p->servername, p->sharename, notify_type, notify_field));
649 return false;
652 #define SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(_data, _integer) \
653 _data->data.integer[0] = _integer; \
654 _data->data.integer[1] = 0;
657 #define SETUP_SPOOLSS_NOTIFY_DATA_STRING(_data, _p) \
658 _data->data.string.string = talloc_strdup(mem_ctx, _p); \
659 if (!_data->data.string.string) {\
660 _data->data.string.size = 0; \
662 _data->data.string.size = strlen_m_term(_p) * 2;
664 #define SETUP_SPOOLSS_NOTIFY_DATA_DEVMODE(_data, _devmode) \
665 _data->data.devmode.devmode = _devmode;
667 #define SETUP_SPOOLSS_NOTIFY_DATA_SECDESC(_data, _size, _sd) \
668 _data->data.sd.sd = dup_sec_desc(mem_ctx, _sd); \
669 if (!_data->data.sd.sd) { \
670 _data->data.sd.sd_size = 0; \
672 _data->data.sd.sd_size = _size;
674 static void init_systemtime_buffer(TALLOC_CTX *mem_ctx,
675 struct tm *t,
676 const char **pp,
677 uint32_t *plen)
679 struct spoolss_Time st;
680 uint32_t len = 16;
681 char *p;
683 if (!init_systemtime(&st, t)) {
684 return;
687 p = talloc_array(mem_ctx, char, len);
688 if (!p) {
689 return;
693 * Systemtime must be linearized as a set of UINT16's.
694 * Fix from Benjamin (Bj) Kuit bj@it.uts.edu.au
697 SSVAL(p, 0, st.year);
698 SSVAL(p, 2, st.month);
699 SSVAL(p, 4, st.day_of_week);
700 SSVAL(p, 6, st.day);
701 SSVAL(p, 8, st.hour);
702 SSVAL(p, 10, st.minute);
703 SSVAL(p, 12, st.second);
704 SSVAL(p, 14, st.millisecond);
706 *pp = p;
707 *plen = len;
710 /* Convert a notification message to a struct spoolss_Notify */
712 static void notify_one_value(struct spoolss_notify_msg *msg,
713 struct spoolss_Notify *data,
714 TALLOC_CTX *mem_ctx)
716 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, msg->notify.value[0]);
719 static void notify_string(struct spoolss_notify_msg *msg,
720 struct spoolss_Notify *data,
721 TALLOC_CTX *mem_ctx)
723 /* The length of the message includes the trailing \0 */
725 data->data.string.size = msg->len * 2;
726 data->data.string.string = talloc_strdup(mem_ctx, msg->notify.data);
727 if (!data->data.string.string) {
728 data->data.string.size = 0;
729 return;
733 static void notify_system_time(struct spoolss_notify_msg *msg,
734 struct spoolss_Notify *data,
735 TALLOC_CTX *mem_ctx)
737 data->data.string.string = NULL;
738 data->data.string.size = 0;
740 if (msg->len != sizeof(time_t)) {
741 DEBUG(5, ("notify_system_time: received wrong sized message (%d)\n",
742 msg->len));
743 return;
746 init_systemtime_buffer(mem_ctx, gmtime((time_t *)msg->notify.data),
747 &data->data.string.string,
748 &data->data.string.size);
751 struct notify2_message_table {
752 const char *name;
753 void (*fn)(struct spoolss_notify_msg *msg,
754 struct spoolss_Notify *data, TALLOC_CTX *mem_ctx);
757 static struct notify2_message_table printer_notify_table[] = {
758 /* 0x00 */ { "PRINTER_NOTIFY_FIELD_SERVER_NAME", notify_string },
759 /* 0x01 */ { "PRINTER_NOTIFY_FIELD_PRINTER_NAME", notify_string },
760 /* 0x02 */ { "PRINTER_NOTIFY_FIELD_SHARE_NAME", notify_string },
761 /* 0x03 */ { "PRINTER_NOTIFY_FIELD_PORT_NAME", notify_string },
762 /* 0x04 */ { "PRINTER_NOTIFY_FIELD_DRIVER_NAME", notify_string },
763 /* 0x05 */ { "PRINTER_NOTIFY_FIELD_COMMENT", notify_string },
764 /* 0x06 */ { "PRINTER_NOTIFY_FIELD_LOCATION", notify_string },
765 /* 0x07 */ { "PRINTER_NOTIFY_FIELD_DEVMODE", NULL },
766 /* 0x08 */ { "PRINTER_NOTIFY_FIELD_SEPFILE", notify_string },
767 /* 0x09 */ { "PRINTER_NOTIFY_FIELD_PRINT_PROCESSOR", notify_string },
768 /* 0x0a */ { "PRINTER_NOTIFY_FIELD_PARAMETERS", NULL },
769 /* 0x0b */ { "PRINTER_NOTIFY_FIELD_DATATYPE", notify_string },
770 /* 0x0c */ { "PRINTER_NOTIFY_FIELD_SECURITY_DESCRIPTOR", NULL },
771 /* 0x0d */ { "PRINTER_NOTIFY_FIELD_ATTRIBUTES", notify_one_value },
772 /* 0x0e */ { "PRINTER_NOTIFY_FIELD_PRIORITY", notify_one_value },
773 /* 0x0f */ { "PRINTER_NOTIFY_FIELD_DEFAULT_PRIORITY", NULL },
774 /* 0x10 */ { "PRINTER_NOTIFY_FIELD_START_TIME", NULL },
775 /* 0x11 */ { "PRINTER_NOTIFY_FIELD_UNTIL_TIME", NULL },
776 /* 0x12 */ { "PRINTER_NOTIFY_FIELD_STATUS", notify_one_value },
779 static struct notify2_message_table job_notify_table[] = {
780 /* 0x00 */ { "JOB_NOTIFY_FIELD_PRINTER_NAME", NULL },
781 /* 0x01 */ { "JOB_NOTIFY_FIELD_MACHINE_NAME", NULL },
782 /* 0x02 */ { "JOB_NOTIFY_FIELD_PORT_NAME", NULL },
783 /* 0x03 */ { "JOB_NOTIFY_FIELD_USER_NAME", notify_string },
784 /* 0x04 */ { "JOB_NOTIFY_FIELD_NOTIFY_NAME", NULL },
785 /* 0x05 */ { "JOB_NOTIFY_FIELD_DATATYPE", NULL },
786 /* 0x06 */ { "JOB_NOTIFY_FIELD_PRINT_PROCESSOR", NULL },
787 /* 0x07 */ { "JOB_NOTIFY_FIELD_PARAMETERS", NULL },
788 /* 0x08 */ { "JOB_NOTIFY_FIELD_DRIVER_NAME", NULL },
789 /* 0x09 */ { "JOB_NOTIFY_FIELD_DEVMODE", NULL },
790 /* 0x0a */ { "JOB_NOTIFY_FIELD_STATUS", notify_one_value },
791 /* 0x0b */ { "JOB_NOTIFY_FIELD_STATUS_STRING", NULL },
792 /* 0x0c */ { "JOB_NOTIFY_FIELD_SECURITY_DESCRIPTOR", NULL },
793 /* 0x0d */ { "JOB_NOTIFY_FIELD_DOCUMENT", notify_string },
794 /* 0x0e */ { "JOB_NOTIFY_FIELD_PRIORITY", NULL },
795 /* 0x0f */ { "JOB_NOTIFY_FIELD_POSITION", NULL },
796 /* 0x10 */ { "JOB_NOTIFY_FIELD_SUBMITTED", notify_system_time },
797 /* 0x11 */ { "JOB_NOTIFY_FIELD_START_TIME", NULL },
798 /* 0x12 */ { "JOB_NOTIFY_FIELD_UNTIL_TIME", NULL },
799 /* 0x13 */ { "JOB_NOTIFY_FIELD_TIME", NULL },
800 /* 0x14 */ { "JOB_NOTIFY_FIELD_TOTAL_PAGES", notify_one_value },
801 /* 0x15 */ { "JOB_NOTIFY_FIELD_PAGES_PRINTED", NULL },
802 /* 0x16 */ { "JOB_NOTIFY_FIELD_TOTAL_BYTES", notify_one_value },
803 /* 0x17 */ { "JOB_NOTIFY_FIELD_BYTES_PRINTED", NULL },
807 /***********************************************************************
808 Allocate talloc context for container object
809 **********************************************************************/
811 static void notify_msg_ctr_init( SPOOLSS_NOTIFY_MSG_CTR *ctr )
813 if ( !ctr )
814 return;
816 ctr->ctx = talloc_init("notify_msg_ctr_init %p", ctr);
818 return;
821 /***********************************************************************
822 release all allocated memory and zero out structure
823 **********************************************************************/
825 static void notify_msg_ctr_destroy( SPOOLSS_NOTIFY_MSG_CTR *ctr )
827 if ( !ctr )
828 return;
830 if ( ctr->ctx )
831 talloc_destroy(ctr->ctx);
833 ZERO_STRUCTP(ctr);
835 return;
838 /***********************************************************************
839 **********************************************************************/
841 static TALLOC_CTX* notify_ctr_getctx( SPOOLSS_NOTIFY_MSG_CTR *ctr )
843 if ( !ctr )
844 return NULL;
846 return ctr->ctx;
849 /***********************************************************************
850 **********************************************************************/
852 static SPOOLSS_NOTIFY_MSG_GROUP* notify_ctr_getgroup( SPOOLSS_NOTIFY_MSG_CTR *ctr, uint32_t idx )
854 if ( !ctr || !ctr->msg_groups )
855 return NULL;
857 if ( idx >= ctr->num_groups )
858 return NULL;
860 return &ctr->msg_groups[idx];
864 /***********************************************************************
865 How many groups of change messages do we have ?
866 **********************************************************************/
868 static int notify_msg_ctr_numgroups( SPOOLSS_NOTIFY_MSG_CTR *ctr )
870 if ( !ctr )
871 return 0;
873 return ctr->num_groups;
876 /***********************************************************************
877 Add a SPOOLSS_NOTIFY_MSG_CTR to the correct group
878 **********************************************************************/
880 static int notify_msg_ctr_addmsg( SPOOLSS_NOTIFY_MSG_CTR *ctr, SPOOLSS_NOTIFY_MSG *msg )
882 SPOOLSS_NOTIFY_MSG_GROUP *groups = NULL;
883 SPOOLSS_NOTIFY_MSG_GROUP *msg_grp = NULL;
884 SPOOLSS_NOTIFY_MSG *msg_list = NULL;
885 int i, new_slot;
887 if ( !ctr || !msg )
888 return 0;
890 /* loop over all groups looking for a matching printer name */
892 for ( i=0; i<ctr->num_groups; i++ ) {
893 if ( strcmp(ctr->msg_groups[i].printername, msg->printer) == 0 )
894 break;
897 /* add a new group? */
899 if ( i == ctr->num_groups ) {
900 ctr->num_groups++;
902 if ( !(groups = TALLOC_REALLOC_ARRAY( ctr->ctx, ctr->msg_groups, SPOOLSS_NOTIFY_MSG_GROUP, ctr->num_groups)) ) {
903 DEBUG(0,("notify_msg_ctr_addmsg: talloc_realloc() failed!\n"));
904 return 0;
906 ctr->msg_groups = groups;
908 /* clear the new entry and set the printer name */
910 ZERO_STRUCT( ctr->msg_groups[ctr->num_groups-1] );
911 fstrcpy( ctr->msg_groups[ctr->num_groups-1].printername, msg->printer );
914 /* add the change messages; 'i' is the correct index now regardless */
916 msg_grp = &ctr->msg_groups[i];
918 msg_grp->num_msgs++;
920 if ( !(msg_list = TALLOC_REALLOC_ARRAY( ctr->ctx, msg_grp->msgs, SPOOLSS_NOTIFY_MSG, msg_grp->num_msgs )) ) {
921 DEBUG(0,("notify_msg_ctr_addmsg: talloc_realloc() failed for new message [%d]!\n", msg_grp->num_msgs));
922 return 0;
924 msg_grp->msgs = msg_list;
926 new_slot = msg_grp->num_msgs-1;
927 memcpy( &msg_grp->msgs[new_slot], msg, sizeof(SPOOLSS_NOTIFY_MSG) );
929 /* need to allocate own copy of data */
931 if ( msg->len != 0 )
932 msg_grp->msgs[new_slot].notify.data = (char *)
933 TALLOC_MEMDUP( ctr->ctx, msg->notify.data, msg->len );
935 return ctr->num_groups;
938 /***********************************************************************
939 Send a change notication message on all handles which have a call
940 back registered
941 **********************************************************************/
943 static void send_notify2_changes( SPOOLSS_NOTIFY_MSG_CTR *ctr, uint32_t idx )
945 Printer_entry *p;
946 TALLOC_CTX *mem_ctx = notify_ctr_getctx( ctr );
947 SPOOLSS_NOTIFY_MSG_GROUP *msg_group = notify_ctr_getgroup( ctr, idx );
948 SPOOLSS_NOTIFY_MSG *messages;
949 int sending_msg_count;
951 if ( !msg_group ) {
952 DEBUG(5,("send_notify2_changes() called with no msg group!\n"));
953 return;
956 messages = msg_group->msgs;
958 if ( !messages ) {
959 DEBUG(5,("send_notify2_changes() called with no messages!\n"));
960 return;
963 DEBUG(8,("send_notify2_changes: Enter...[%s]\n", msg_group->printername));
965 /* loop over all printers */
967 for (p = printers_list; p; p = p->next) {
968 struct spoolss_Notify *notifies;
969 uint32_t count = 0;
970 uint32_t id;
971 int i;
973 /* Is there notification on this handle? */
975 if ( !p->notify.client_connected )
976 continue;
978 DEBUG(10,("Client connected! [\\\\%s\\%s]\n", p->servername, p->sharename));
980 /* For this printer? Print servers always receive
981 notifications. */
983 if ( ( p->printer_type == SPLHND_PRINTER ) &&
984 ( !strequal(msg_group->printername, p->sharename) ) )
985 continue;
987 DEBUG(10,("Our printer\n"));
989 /* allocate the max entries possible */
991 notifies = TALLOC_ZERO_ARRAY(mem_ctx, struct spoolss_Notify, msg_group->num_msgs);
992 if (!notifies) {
993 return;
996 /* build the array of change notifications */
998 sending_msg_count = 0;
1000 for ( i=0; i<msg_group->num_msgs; i++ ) {
1001 SPOOLSS_NOTIFY_MSG *msg = &messages[i];
1003 /* Are we monitoring this event? */
1005 if (!is_monitoring_event(p, msg->type, msg->field))
1006 continue;
1008 sending_msg_count++;
1011 DEBUG(10,("process_notify2_message: Sending message type [0x%x] field [0x%2x] for printer [%s]\n",
1012 msg->type, msg->field, p->sharename));
1015 * if the is a printer notification handle and not a job notification
1016 * type, then set the id to 0. Other wise just use what was specified
1017 * in the message.
1019 * When registering change notification on a print server handle
1020 * we always need to send back the id (snum) matching the printer
1021 * for which the change took place. For change notify registered
1022 * on a printer handle, this does not matter and the id should be 0.
1024 * --jerry
1027 if ( ( p->printer_type == SPLHND_PRINTER ) && ( msg->type == PRINTER_NOTIFY_TYPE ) )
1028 id = 0;
1029 else
1030 id = msg->id;
1033 /* Convert unix jobid to smb jobid */
1035 if (msg->flags & SPOOLSS_NOTIFY_MSG_UNIX_JOBID) {
1036 id = sysjob_to_jobid(msg->id);
1038 if (id == -1) {
1039 DEBUG(3, ("no such unix jobid %d\n", msg->id));
1040 goto done;
1044 construct_info_data( &notifies[count], msg->type, msg->field, id );
1046 switch(msg->type) {
1047 case PRINTER_NOTIFY_TYPE:
1048 if ( printer_notify_table[msg->field].fn )
1049 printer_notify_table[msg->field].fn(msg, &notifies[count], mem_ctx);
1050 break;
1052 case JOB_NOTIFY_TYPE:
1053 if ( job_notify_table[msg->field].fn )
1054 job_notify_table[msg->field].fn(msg, &notifies[count], mem_ctx);
1055 break;
1057 default:
1058 DEBUG(5, ("Unknown notification type %d\n", msg->type));
1059 goto done;
1062 count++;
1065 if ( sending_msg_count ) {
1066 NTSTATUS status;
1067 WERROR werr;
1068 union spoolss_ReplyPrinterInfo info;
1069 struct spoolss_NotifyInfo info0;
1070 uint32_t reply_result;
1072 info0.version = 0x2;
1073 info0.flags = count ? 0x00020000 /* ??? */ : PRINTER_NOTIFY_INFO_DISCARDED;
1074 info0.count = count;
1075 info0.notifies = notifies;
1077 info.info0 = &info0;
1079 status = rpccli_spoolss_RouterReplyPrinterEx(notify_cli_pipe, mem_ctx,
1080 &p->notify.client_hnd,
1081 p->notify.change, /* color */
1082 p->notify.flags,
1083 &reply_result,
1084 0, /* reply_type, must be 0 */
1085 info,
1086 &werr);
1087 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(werr)) {
1088 DEBUG(1,("RouterReplyPrinterEx to client: %s failed: %s\n",
1089 notify_cli_pipe->srv_name_slash,
1090 win_errstr(werr)));
1092 switch (reply_result) {
1093 case 0:
1094 break;
1095 case PRINTER_NOTIFY_INFO_DISCARDED:
1096 case PRINTER_NOTIFY_INFO_DISCARDNOTED:
1097 case PRINTER_NOTIFY_INFO_COLOR_MISMATCH:
1098 break;
1099 default:
1100 break;
1105 done:
1106 DEBUG(8,("send_notify2_changes: Exit...\n"));
1107 return;
1110 /***********************************************************************
1111 **********************************************************************/
1113 static bool notify2_unpack_msg( SPOOLSS_NOTIFY_MSG *msg, struct timeval *tv, void *buf, size_t len )
1116 uint32_t tv_sec, tv_usec;
1117 size_t offset = 0;
1119 /* Unpack message */
1121 offset += tdb_unpack((uint8_t *)buf + offset, len - offset, "f",
1122 msg->printer);
1124 offset += tdb_unpack((uint8_t *)buf + offset, len - offset, "ddddddd",
1125 &tv_sec, &tv_usec,
1126 &msg->type, &msg->field, &msg->id, &msg->len, &msg->flags);
1128 if (msg->len == 0)
1129 tdb_unpack((uint8_t *)buf + offset, len - offset, "dd",
1130 &msg->notify.value[0], &msg->notify.value[1]);
1131 else
1132 tdb_unpack((uint8_t *)buf + offset, len - offset, "B",
1133 &msg->len, &msg->notify.data);
1135 DEBUG(3, ("notify2_unpack_msg: got NOTIFY2 message for printer %s, jobid %u type %d, field 0x%02x, flags 0x%04x\n",
1136 msg->printer, (unsigned int)msg->id, msg->type, msg->field, msg->flags));
1138 tv->tv_sec = tv_sec;
1139 tv->tv_usec = tv_usec;
1141 if (msg->len == 0)
1142 DEBUG(3, ("notify2_unpack_msg: value1 = %d, value2 = %d\n", msg->notify.value[0],
1143 msg->notify.value[1]));
1144 else
1145 dump_data(3, (uint8_t *)msg->notify.data, msg->len);
1147 return true;
1150 /********************************************************************
1151 Receive a notify2 message list
1152 ********************************************************************/
1154 static void receive_notify2_message_list(struct messaging_context *msg,
1155 void *private_data,
1156 uint32_t msg_type,
1157 struct server_id server_id,
1158 DATA_BLOB *data)
1160 size_t msg_count, i;
1161 char *buf = (char *)data->data;
1162 char *msg_ptr;
1163 size_t msg_len;
1164 SPOOLSS_NOTIFY_MSG notify;
1165 SPOOLSS_NOTIFY_MSG_CTR messages;
1166 int num_groups;
1168 if (data->length < 4) {
1169 DEBUG(0,("receive_notify2_message_list: bad message format (len < 4)!\n"));
1170 return;
1173 msg_count = IVAL(buf, 0);
1174 msg_ptr = buf + 4;
1176 DEBUG(5, ("receive_notify2_message_list: got %lu messages in list\n", (unsigned long)msg_count));
1178 if (msg_count == 0) {
1179 DEBUG(0,("receive_notify2_message_list: bad message format (msg_count == 0) !\n"));
1180 return;
1183 /* initialize the container */
1185 ZERO_STRUCT( messages );
1186 notify_msg_ctr_init( &messages );
1189 * build message groups for each printer identified
1190 * in a change_notify msg. Remember that a PCN message
1191 * includes the handle returned for the srv_spoolss_replyopenprinter()
1192 * call. Therefore messages are grouped according to printer handle.
1195 for ( i=0; i<msg_count; i++ ) {
1196 struct timeval msg_tv;
1198 if (msg_ptr + 4 - buf > data->length) {
1199 DEBUG(0,("receive_notify2_message_list: bad message format (len > buf_size) !\n"));
1200 return;
1203 msg_len = IVAL(msg_ptr,0);
1204 msg_ptr += 4;
1206 if (msg_ptr + msg_len - buf > data->length) {
1207 DEBUG(0,("receive_notify2_message_list: bad message format (bad len) !\n"));
1208 return;
1211 /* unpack messages */
1213 ZERO_STRUCT( notify );
1214 notify2_unpack_msg( &notify, &msg_tv, msg_ptr, msg_len );
1215 msg_ptr += msg_len;
1217 /* add to correct list in container */
1219 notify_msg_ctr_addmsg( &messages, &notify );
1221 /* free memory that might have been allocated by notify2_unpack_msg() */
1223 if ( notify.len != 0 )
1224 SAFE_FREE( notify.notify.data );
1227 /* process each group of messages */
1229 num_groups = notify_msg_ctr_numgroups( &messages );
1230 for ( i=0; i<num_groups; i++ )
1231 send_notify2_changes( &messages, i );
1234 /* cleanup */
1236 DEBUG(10,("receive_notify2_message_list: processed %u messages\n",
1237 (uint32_t)msg_count ));
1239 notify_msg_ctr_destroy( &messages );
1241 return;
1244 /********************************************************************
1245 Send a message to ourself about new driver being installed
1246 so we can upgrade the information for each printer bound to this
1247 driver
1248 ********************************************************************/
1250 static bool srv_spoolss_drv_upgrade_printer(const char *drivername)
1252 int len = strlen(drivername);
1254 if (!len)
1255 return false;
1257 DEBUG(10,("srv_spoolss_drv_upgrade_printer: Sending message about driver upgrade [%s]\n",
1258 drivername));
1260 messaging_send_buf(smbd_messaging_context(), procid_self(),
1261 MSG_PRINTER_DRVUPGRADE,
1262 (uint8_t *)drivername, len+1);
1264 return true;
1267 /**********************************************************************
1268 callback to receive a MSG_PRINTER_DRVUPGRADE message and interate
1269 over all printers, upgrading ones as necessary
1270 **********************************************************************/
1272 void do_drv_upgrade_printer(struct messaging_context *msg,
1273 void *private_data,
1274 uint32_t msg_type,
1275 struct server_id server_id,
1276 DATA_BLOB *data)
1278 fstring drivername;
1279 int snum;
1280 int n_services = lp_numservices();
1281 size_t len;
1283 len = MIN(data->length,sizeof(drivername)-1);
1284 strncpy(drivername, (const char *)data->data, len);
1286 DEBUG(10,("do_drv_upgrade_printer: Got message for new driver [%s]\n", drivername ));
1288 /* Iterate the printer list */
1290 for (snum=0; snum<n_services; snum++)
1292 if (lp_snum_ok(snum) && lp_print_ok(snum) )
1294 WERROR result;
1295 NT_PRINTER_INFO_LEVEL *printer = NULL;
1297 result = get_a_printer(NULL, &printer, 2, lp_const_servicename(snum));
1298 if (!W_ERROR_IS_OK(result))
1299 continue;
1301 if (printer && printer->info_2 && !strcmp(drivername, printer->info_2->drivername))
1303 DEBUG(6,("Updating printer [%s]\n", printer->info_2->printername));
1305 /* all we care about currently is the change_id */
1307 result = mod_a_printer(printer, 2);
1308 if (!W_ERROR_IS_OK(result)) {
1309 DEBUG(3,("do_drv_upgrade_printer: mod_a_printer() failed with status [%s]\n",
1310 win_errstr(result)));
1314 free_a_printer(&printer, 2);
1318 /* all done */
1321 /********************************************************************
1322 Update the cache for all printq's with a registered client
1323 connection
1324 ********************************************************************/
1326 void update_monitored_printq_cache( void )
1328 Printer_entry *printer = printers_list;
1329 int snum;
1331 /* loop through all printers and update the cache where
1332 client_connected == true */
1333 while ( printer )
1335 if ( (printer->printer_type == SPLHND_PRINTER)
1336 && printer->notify.client_connected )
1338 snum = print_queue_snum(printer->sharename);
1339 print_queue_status( snum, NULL, NULL );
1342 printer = printer->next;
1345 return;
1347 /********************************************************************
1348 Send a message to ourself about new driver being installed
1349 so we can upgrade the information for each printer bound to this
1350 driver
1351 ********************************************************************/
1353 static bool srv_spoolss_reset_printerdata(char* drivername)
1355 int len = strlen(drivername);
1357 if (!len)
1358 return false;
1360 DEBUG(10,("srv_spoolss_reset_printerdata: Sending message about resetting printerdata [%s]\n",
1361 drivername));
1363 messaging_send_buf(smbd_messaging_context(), procid_self(),
1364 MSG_PRINTERDATA_INIT_RESET,
1365 (uint8_t *)drivername, len+1);
1367 return true;
1370 /**********************************************************************
1371 callback to receive a MSG_PRINTERDATA_INIT_RESET message and interate
1372 over all printers, resetting printer data as neessary
1373 **********************************************************************/
1375 void reset_all_printerdata(struct messaging_context *msg,
1376 void *private_data,
1377 uint32_t msg_type,
1378 struct server_id server_id,
1379 DATA_BLOB *data)
1381 fstring drivername;
1382 int snum;
1383 int n_services = lp_numservices();
1384 size_t len;
1386 len = MIN( data->length, sizeof(drivername)-1 );
1387 strncpy( drivername, (const char *)data->data, len );
1389 DEBUG(10,("reset_all_printerdata: Got message for new driver [%s]\n", drivername ));
1391 /* Iterate the printer list */
1393 for ( snum=0; snum<n_services; snum++ )
1395 if ( lp_snum_ok(snum) && lp_print_ok(snum) )
1397 WERROR result;
1398 NT_PRINTER_INFO_LEVEL *printer = NULL;
1400 result = get_a_printer( NULL, &printer, 2, lp_const_servicename(snum) );
1401 if ( !W_ERROR_IS_OK(result) )
1402 continue;
1405 * if the printer is bound to the driver,
1406 * then reset to the new driver initdata
1409 if ( printer && printer->info_2 && !strcmp(drivername, printer->info_2->drivername) )
1411 DEBUG(6,("reset_all_printerdata: Updating printer [%s]\n", printer->info_2->printername));
1413 if ( !set_driver_init(printer, 2) ) {
1414 DEBUG(5,("reset_all_printerdata: Error resetting printer data for printer [%s], driver [%s]!\n",
1415 printer->info_2->printername, printer->info_2->drivername));
1418 result = mod_a_printer( printer, 2 );
1419 if ( !W_ERROR_IS_OK(result) ) {
1420 DEBUG(3,("reset_all_printerdata: mod_a_printer() failed! (%s)\n",
1421 get_dos_error_msg(result)));
1425 free_a_printer( &printer, 2 );
1429 /* all done */
1431 return;
1434 /****************************************************************
1435 _spoolss_OpenPrinter
1436 ****************************************************************/
1438 WERROR _spoolss_OpenPrinter(pipes_struct *p,
1439 struct spoolss_OpenPrinter *r)
1441 struct spoolss_OpenPrinterEx e;
1442 WERROR werr;
1444 ZERO_STRUCT(e.in.userlevel);
1446 e.in.printername = r->in.printername;
1447 e.in.datatype = r->in.datatype;
1448 e.in.devmode_ctr = r->in.devmode_ctr;
1449 e.in.access_mask = r->in.access_mask;
1450 e.in.level = 0;
1452 e.out.handle = r->out.handle;
1454 werr = _spoolss_OpenPrinterEx(p, &e);
1456 if (W_ERROR_EQUAL(werr, WERR_INVALID_PARAM)) {
1457 /* OpenPrinterEx returns this for a bad
1458 * printer name. We must return WERR_INVALID_PRINTER_NAME
1459 * instead.
1461 werr = WERR_INVALID_PRINTER_NAME;
1464 return werr;
1467 /********************************************************************
1468 ********************************************************************/
1470 bool convert_devicemode(const char *printername,
1471 const struct spoolss_DeviceMode *devmode,
1472 NT_DEVICEMODE **pp_nt_devmode)
1474 NT_DEVICEMODE *nt_devmode = *pp_nt_devmode;
1477 * Ensure nt_devmode is a valid pointer
1478 * as we will be overwriting it.
1481 if (nt_devmode == NULL) {
1482 DEBUG(5, ("convert_devicemode: allocating a generic devmode\n"));
1483 if ((nt_devmode = construct_nt_devicemode(printername)) == NULL)
1484 return false;
1487 fstrcpy(nt_devmode->devicename, devmode->devicename);
1488 fstrcpy(nt_devmode->formname, devmode->formname);
1490 nt_devmode->devicename[31] = '\0';
1491 nt_devmode->formname[31] = '\0';
1493 nt_devmode->specversion = devmode->specversion;
1494 nt_devmode->driverversion = devmode->driverversion;
1495 nt_devmode->size = devmode->size;
1496 nt_devmode->fields = devmode->fields;
1497 nt_devmode->orientation = devmode->orientation;
1498 nt_devmode->papersize = devmode->papersize;
1499 nt_devmode->paperlength = devmode->paperlength;
1500 nt_devmode->paperwidth = devmode->paperwidth;
1501 nt_devmode->scale = devmode->scale;
1502 nt_devmode->copies = devmode->copies;
1503 nt_devmode->defaultsource = devmode->defaultsource;
1504 nt_devmode->printquality = devmode->printquality;
1505 nt_devmode->color = devmode->color;
1506 nt_devmode->duplex = devmode->duplex;
1507 nt_devmode->yresolution = devmode->yresolution;
1508 nt_devmode->ttoption = devmode->ttoption;
1509 nt_devmode->collate = devmode->collate;
1511 nt_devmode->logpixels = devmode->logpixels;
1512 nt_devmode->bitsperpel = devmode->bitsperpel;
1513 nt_devmode->pelswidth = devmode->pelswidth;
1514 nt_devmode->pelsheight = devmode->pelsheight;
1515 nt_devmode->displayflags = devmode->displayflags;
1516 nt_devmode->displayfrequency = devmode->displayfrequency;
1517 nt_devmode->icmmethod = devmode->icmmethod;
1518 nt_devmode->icmintent = devmode->icmintent;
1519 nt_devmode->mediatype = devmode->mediatype;
1520 nt_devmode->dithertype = devmode->dithertype;
1521 nt_devmode->reserved1 = devmode->reserved1;
1522 nt_devmode->reserved2 = devmode->reserved2;
1523 nt_devmode->panningwidth = devmode->panningwidth;
1524 nt_devmode->panningheight = devmode->panningheight;
1527 * Only change private and driverextra if the incoming devmode
1528 * has a new one. JRA.
1531 if ((devmode->__driverextra_length != 0) && (devmode->driverextra_data.data != NULL)) {
1532 SAFE_FREE(nt_devmode->nt_dev_private);
1533 nt_devmode->driverextra = devmode->__driverextra_length;
1534 if((nt_devmode->nt_dev_private = SMB_MALLOC_ARRAY(uint8_t, nt_devmode->driverextra)) == NULL)
1535 return false;
1536 memcpy(nt_devmode->nt_dev_private, devmode->driverextra_data.data, nt_devmode->driverextra);
1539 *pp_nt_devmode = nt_devmode;
1541 return true;
1544 /****************************************************************
1545 _spoolss_OpenPrinterEx
1546 ****************************************************************/
1548 WERROR _spoolss_OpenPrinterEx(pipes_struct *p,
1549 struct spoolss_OpenPrinterEx *r)
1551 int snum;
1552 Printer_entry *Printer=NULL;
1554 if (!r->in.printername) {
1555 return WERR_INVALID_PARAM;
1558 /* some sanity check because you can open a printer or a print server */
1559 /* aka: \\server\printer or \\server */
1561 DEBUGADD(3,("checking name: %s\n", r->in.printername));
1563 if (!open_printer_hnd(p, r->out.handle, r->in.printername, 0)) {
1564 ZERO_STRUCTP(r->out.handle);
1565 return WERR_INVALID_PARAM;
1568 Printer = find_printer_index_by_hnd(p, r->out.handle);
1569 if ( !Printer ) {
1570 DEBUG(0,("_spoolss_OpenPrinterEx: logic error. Can't find printer "
1571 "handle we created for printer %s\n", r->in.printername));
1572 close_printer_handle(p, r->out.handle);
1573 ZERO_STRUCTP(r->out.handle);
1574 return WERR_INVALID_PARAM;
1578 * First case: the user is opening the print server:
1580 * Disallow MS AddPrinterWizard if parameter disables it. A Win2k
1581 * client 1st tries an OpenPrinterEx with access==0, MUST be allowed.
1583 * Then both Win2k and WinNT clients try an OpenPrinterEx with
1584 * SERVER_ALL_ACCESS, which we allow only if the user is root (uid=0)
1585 * or if the user is listed in the smb.conf printer admin parameter.
1587 * Then they try OpenPrinterEx with SERVER_READ which we allow. This lets the
1588 * client view printer folder, but does not show the MSAPW.
1590 * Note: this test needs code to check access rights here too. Jeremy
1591 * could you look at this?
1593 * Second case: the user is opening a printer:
1594 * NT doesn't let us connect to a printer if the connecting user
1595 * doesn't have print permission.
1597 * Third case: user is opening a Port Monitor
1598 * access checks same as opening a handle to the print server.
1601 switch (Printer->printer_type )
1603 case SPLHND_SERVER:
1604 case SPLHND_PORTMON_TCP:
1605 case SPLHND_PORTMON_LOCAL:
1606 /* Printserver handles use global struct... */
1608 snum = -1;
1610 /* Map standard access rights to object specific access rights */
1612 se_map_standard(&r->in.access_mask,
1613 &printserver_std_mapping);
1615 /* Deny any object specific bits that don't apply to print
1616 servers (i.e printer and job specific bits) */
1618 r->in.access_mask &= SPECIFIC_RIGHTS_MASK;
1620 if (r->in.access_mask &
1621 ~(SERVER_ACCESS_ADMINISTER | SERVER_ACCESS_ENUMERATE)) {
1622 DEBUG(3, ("access DENIED for non-printserver bits\n"));
1623 close_printer_handle(p, r->out.handle);
1624 ZERO_STRUCTP(r->out.handle);
1625 return WERR_ACCESS_DENIED;
1628 /* Allow admin access */
1630 if ( r->in.access_mask & SERVER_ACCESS_ADMINISTER )
1632 SE_PRIV se_printop = SE_PRINT_OPERATOR;
1634 if (!lp_ms_add_printer_wizard()) {
1635 close_printer_handle(p, r->out.handle);
1636 ZERO_STRUCTP(r->out.handle);
1637 return WERR_ACCESS_DENIED;
1640 /* if the user is not root, doesn't have SE_PRINT_OPERATOR privilege,
1641 and not a printer admin, then fail */
1643 if ((p->server_info->utok.uid != sec_initial_uid()) &&
1644 !user_has_privileges(p->server_info->ptok,
1645 &se_printop ) &&
1646 !token_contains_name_in_list(
1647 uidtoname(p->server_info->utok.uid),
1648 pdb_get_domain(p->server_info->sam_account),
1649 NULL,
1650 p->server_info->ptok,
1651 lp_printer_admin(snum))) {
1652 close_printer_handle(p, r->out.handle);
1653 ZERO_STRUCTP(r->out.handle);
1654 return WERR_ACCESS_DENIED;
1657 r->in.access_mask = SERVER_ACCESS_ADMINISTER;
1659 else
1661 r->in.access_mask = SERVER_ACCESS_ENUMERATE;
1664 DEBUG(4,("Setting print server access = %s\n", (r->in.access_mask == SERVER_ACCESS_ADMINISTER)
1665 ? "SERVER_ACCESS_ADMINISTER" : "SERVER_ACCESS_ENUMERATE" ));
1667 /* We fall through to return WERR_OK */
1668 break;
1670 case SPLHND_PRINTER:
1671 /* NT doesn't let us connect to a printer if the connecting user
1672 doesn't have print permission. */
1674 if (!get_printer_snum(p, r->out.handle, &snum, NULL)) {
1675 close_printer_handle(p, r->out.handle);
1676 ZERO_STRUCTP(r->out.handle);
1677 return WERR_BADFID;
1680 if (r->in.access_mask == SEC_FLAG_MAXIMUM_ALLOWED) {
1681 r->in.access_mask = PRINTER_ACCESS_ADMINISTER;
1684 se_map_standard(&r->in.access_mask, &printer_std_mapping);
1686 /* map an empty access mask to the minimum access mask */
1687 if (r->in.access_mask == 0x0)
1688 r->in.access_mask = PRINTER_ACCESS_USE;
1691 * If we are not serving the printer driver for this printer,
1692 * map PRINTER_ACCESS_ADMINISTER to PRINTER_ACCESS_USE. This
1693 * will keep NT clients happy --jerry
1696 if (lp_use_client_driver(snum)
1697 && (r->in.access_mask & PRINTER_ACCESS_ADMINISTER))
1699 r->in.access_mask = PRINTER_ACCESS_USE;
1702 /* check smb.conf parameters and the the sec_desc */
1704 if ( !check_access(get_client_fd(), lp_hostsallow(snum), lp_hostsdeny(snum)) ) {
1705 DEBUG(3, ("access DENIED (hosts allow/deny) for printer open\n"));
1706 ZERO_STRUCTP(r->out.handle);
1707 return WERR_ACCESS_DENIED;
1710 if (!user_ok_token(uidtoname(p->server_info->utok.uid), NULL,
1711 p->server_info->ptok, snum) ||
1712 !print_access_check(p->server_info, snum,
1713 r->in.access_mask)) {
1714 DEBUG(3, ("access DENIED for printer open\n"));
1715 close_printer_handle(p, r->out.handle);
1716 ZERO_STRUCTP(r->out.handle);
1717 return WERR_ACCESS_DENIED;
1720 if ((r->in.access_mask & SPECIFIC_RIGHTS_MASK)& ~(PRINTER_ACCESS_ADMINISTER|PRINTER_ACCESS_USE)) {
1721 DEBUG(3, ("access DENIED for printer open - unknown bits\n"));
1722 close_printer_handle(p, r->out.handle);
1723 ZERO_STRUCTP(r->out.handle);
1724 return WERR_ACCESS_DENIED;
1727 if (r->in.access_mask & PRINTER_ACCESS_ADMINISTER)
1728 r->in.access_mask = PRINTER_ACCESS_ADMINISTER;
1729 else
1730 r->in.access_mask = PRINTER_ACCESS_USE;
1732 DEBUG(4,("Setting printer access = %s\n", (r->in.access_mask == PRINTER_ACCESS_ADMINISTER)
1733 ? "PRINTER_ACCESS_ADMINISTER" : "PRINTER_ACCESS_USE" ));
1735 break;
1737 default:
1738 /* sanity check to prevent programmer error */
1739 ZERO_STRUCTP(r->out.handle);
1740 return WERR_BADFID;
1743 Printer->access_granted = r->in.access_mask;
1746 * If the client sent a devmode in the OpenPrinter() call, then
1747 * save it here in case we get a job submission on this handle
1750 if ((Printer->printer_type != SPLHND_SERVER) &&
1751 r->in.devmode_ctr.devmode) {
1752 convert_devicemode(Printer->sharename,
1753 r->in.devmode_ctr.devmode,
1754 &Printer->nt_devmode);
1757 #if 0 /* JERRY -- I'm doubtful this is really effective */
1758 /* HACK ALERT!!! Sleep for 1/3 of a second to try trigger a LAN/WAN
1759 optimization in Windows 2000 clients --jerry */
1761 if ( (r->in.access_mask == PRINTER_ACCESS_ADMINISTER)
1762 && (RA_WIN2K == get_remote_arch()) )
1764 DEBUG(10,("_spoolss_OpenPrinterEx: Enabling LAN/WAN hack for Win2k clients.\n"));
1765 sys_usleep( 500000 );
1767 #endif
1769 return WERR_OK;
1772 /****************************************************************************
1773 ****************************************************************************/
1775 static bool printer_info2_to_nt_printer_info2(struct spoolss_SetPrinterInfo2 *r,
1776 NT_PRINTER_INFO_LEVEL_2 *d)
1778 DEBUG(7,("printer_info2_to_nt_printer_info2\n"));
1780 if (!r || !d) {
1781 return false;
1784 d->attributes = r->attributes;
1785 d->priority = r->priority;
1786 d->default_priority = r->defaultpriority;
1787 d->starttime = r->starttime;
1788 d->untiltime = r->untiltime;
1789 d->status = r->status;
1790 d->cjobs = r->cjobs;
1792 fstrcpy(d->servername, r->servername);
1793 fstrcpy(d->printername, r->printername);
1794 fstrcpy(d->sharename, r->sharename);
1795 fstrcpy(d->portname, r->portname);
1796 fstrcpy(d->drivername, r->drivername);
1797 slprintf(d->comment, sizeof(d->comment)-1, "%s", r->comment);
1798 fstrcpy(d->location, r->location);
1799 fstrcpy(d->sepfile, r->sepfile);
1800 fstrcpy(d->printprocessor, r->printprocessor);
1801 fstrcpy(d->datatype, r->datatype);
1802 fstrcpy(d->parameters, r->parameters);
1804 return true;
1807 /****************************************************************************
1808 ****************************************************************************/
1810 static bool convert_printer_info(struct spoolss_SetPrinterInfoCtr *info_ctr,
1811 NT_PRINTER_INFO_LEVEL *printer)
1813 bool ret;
1815 switch (info_ctr->level) {
1816 case 2:
1817 /* allocate memory if needed. Messy because
1818 convert_printer_info is used to update an existing
1819 printer or build a new one */
1821 if (!printer->info_2) {
1822 printer->info_2 = TALLOC_ZERO_P(printer, NT_PRINTER_INFO_LEVEL_2);
1823 if (!printer->info_2) {
1824 DEBUG(0,("convert_printer_info: "
1825 "talloc() failed!\n"));
1826 return false;
1830 ret = printer_info2_to_nt_printer_info2(info_ctr->info.info2,
1831 printer->info_2);
1832 printer->info_2->setuptime = time(NULL);
1833 return ret;
1836 return false;
1839 /****************************************************************
1840 _spoolss_ClosePrinter
1841 ****************************************************************/
1843 WERROR _spoolss_ClosePrinter(pipes_struct *p,
1844 struct spoolss_ClosePrinter *r)
1846 Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
1848 if (Printer && Printer->document_started) {
1849 struct spoolss_EndDocPrinter e;
1851 e.in.handle = r->in.handle;
1853 _spoolss_EndDocPrinter(p, &e);
1856 if (!close_printer_handle(p, r->in.handle))
1857 return WERR_BADFID;
1859 /* clear the returned printer handle. Observed behavior
1860 from Win2k server. Don't think this really matters.
1861 Previous code just copied the value of the closed
1862 handle. --jerry */
1864 ZERO_STRUCTP(r->out.handle);
1866 return WERR_OK;
1869 /****************************************************************
1870 _spoolss_DeletePrinter
1871 ****************************************************************/
1873 WERROR _spoolss_DeletePrinter(pipes_struct *p,
1874 struct spoolss_DeletePrinter *r)
1876 Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
1877 WERROR result;
1879 if (Printer && Printer->document_started) {
1880 struct spoolss_EndDocPrinter e;
1882 e.in.handle = r->in.handle;
1884 _spoolss_EndDocPrinter(p, &e);
1887 result = delete_printer_handle(p, r->in.handle);
1889 update_c_setprinter(false);
1891 return result;
1894 /*******************************************************************
1895 * static function to lookup the version id corresponding to an
1896 * long architecture string
1897 ******************************************************************/
1899 static const struct print_architecture_table_node archi_table[]= {
1901 {"Windows 4.0", SPL_ARCH_WIN40, 0 },
1902 {"Windows NT x86", SPL_ARCH_W32X86, 2 },
1903 {"Windows NT R4000", SPL_ARCH_W32MIPS, 2 },
1904 {"Windows NT Alpha_AXP", SPL_ARCH_W32ALPHA, 2 },
1905 {"Windows NT PowerPC", SPL_ARCH_W32PPC, 2 },
1906 {"Windows IA64", SPL_ARCH_IA64, 3 },
1907 {"Windows x64", SPL_ARCH_X64, 3 },
1908 {NULL, "", -1 }
1911 static int get_version_id(const char *arch)
1913 int i;
1915 for (i=0; archi_table[i].long_archi != NULL; i++)
1917 if (strcmp(arch, archi_table[i].long_archi) == 0)
1918 return (archi_table[i].version);
1921 return -1;
1924 /****************************************************************
1925 _spoolss_DeletePrinterDriver
1926 ****************************************************************/
1928 WERROR _spoolss_DeletePrinterDriver(pipes_struct *p,
1929 struct spoolss_DeletePrinterDriver *r)
1932 struct spoolss_DriverInfo8 *info = NULL;
1933 struct spoolss_DriverInfo8 *info_win2k = NULL;
1934 int version;
1935 WERROR status;
1936 WERROR status_win2k = WERR_ACCESS_DENIED;
1937 SE_PRIV se_printop = SE_PRINT_OPERATOR;
1939 /* if the user is not root, doesn't have SE_PRINT_OPERATOR privilege,
1940 and not a printer admin, then fail */
1942 if ( (p->server_info->utok.uid != sec_initial_uid())
1943 && !user_has_privileges(p->server_info->ptok, &se_printop )
1944 && !token_contains_name_in_list(
1945 uidtoname(p->server_info->utok.uid),
1946 pdb_get_domain(p->server_info->sam_account),
1947 NULL,
1948 p->server_info->ptok,
1949 lp_printer_admin(-1)) )
1951 return WERR_ACCESS_DENIED;
1954 /* check that we have a valid driver name first */
1956 if ((version = get_version_id(r->in.architecture)) == -1)
1957 return WERR_INVALID_ENVIRONMENT;
1959 if (!W_ERROR_IS_OK(get_a_printer_driver(p->mem_ctx, &info, r->in.driver,
1960 r->in.architecture,
1961 version)))
1963 /* try for Win2k driver if "Windows NT x86" */
1965 if ( version == 2 ) {
1966 version = 3;
1967 if (!W_ERROR_IS_OK(get_a_printer_driver(p->mem_ctx,
1968 &info,
1969 r->in.driver,
1970 r->in.architecture,
1971 version))) {
1972 status = WERR_UNKNOWN_PRINTER_DRIVER;
1973 goto done;
1976 /* otherwise it was a failure */
1977 else {
1978 status = WERR_UNKNOWN_PRINTER_DRIVER;
1979 goto done;
1984 if (printer_driver_in_use(info)) {
1985 status = WERR_PRINTER_DRIVER_IN_USE;
1986 goto done;
1989 if ( version == 2 )
1991 if (W_ERROR_IS_OK(get_a_printer_driver(p->mem_ctx,
1992 &info_win2k,
1993 r->in.driver,
1994 r->in.architecture, 3)))
1996 /* if we get to here, we now have 2 driver info structures to remove */
1997 /* remove the Win2k driver first*/
1999 status_win2k = delete_printer_driver(
2000 p, info_win2k, 3, false);
2001 free_a_printer_driver(info_win2k);
2003 /* this should not have failed---if it did, report to client */
2004 if ( !W_ERROR_IS_OK(status_win2k) )
2006 status = status_win2k;
2007 goto done;
2012 status = delete_printer_driver(p, info, version, false);
2014 /* if at least one of the deletes succeeded return OK */
2016 if ( W_ERROR_IS_OK(status) || W_ERROR_IS_OK(status_win2k) )
2017 status = WERR_OK;
2019 done:
2020 free_a_printer_driver(info);
2022 return status;
2025 /****************************************************************
2026 _spoolss_DeletePrinterDriverEx
2027 ****************************************************************/
2029 WERROR _spoolss_DeletePrinterDriverEx(pipes_struct *p,
2030 struct spoolss_DeletePrinterDriverEx *r)
2032 struct spoolss_DriverInfo8 *info = NULL;
2033 struct spoolss_DriverInfo8 *info_win2k = NULL;
2034 int version;
2035 bool delete_files;
2036 WERROR status;
2037 WERROR status_win2k = WERR_ACCESS_DENIED;
2038 SE_PRIV se_printop = SE_PRINT_OPERATOR;
2040 /* if the user is not root, doesn't have SE_PRINT_OPERATOR privilege,
2041 and not a printer admin, then fail */
2043 if ( (p->server_info->utok.uid != sec_initial_uid())
2044 && !user_has_privileges(p->server_info->ptok, &se_printop )
2045 && !token_contains_name_in_list(
2046 uidtoname(p->server_info->utok.uid),
2047 pdb_get_domain(p->server_info->sam_account),
2048 NULL,
2049 p->server_info->ptok, lp_printer_admin(-1)) )
2051 return WERR_ACCESS_DENIED;
2054 /* check that we have a valid driver name first */
2055 if ((version = get_version_id(r->in.architecture)) == -1) {
2056 /* this is what NT returns */
2057 return WERR_INVALID_ENVIRONMENT;
2060 if (r->in.delete_flags & DPD_DELETE_SPECIFIC_VERSION)
2061 version = r->in.version;
2063 status = get_a_printer_driver(p->mem_ctx, &info, r->in.driver,
2064 r->in.architecture, version);
2066 if ( !W_ERROR_IS_OK(status) )
2069 * if the client asked for a specific version,
2070 * or this is something other than Windows NT x86,
2071 * then we've failed
2074 if ( (r->in.delete_flags & DPD_DELETE_SPECIFIC_VERSION) || (version !=2) )
2075 goto done;
2077 /* try for Win2k driver if "Windows NT x86" */
2079 version = 3;
2080 if (!W_ERROR_IS_OK(get_a_printer_driver(p->mem_ctx, &info, r->in.driver,
2081 r->in.architecture,
2082 version))) {
2083 status = WERR_UNKNOWN_PRINTER_DRIVER;
2084 goto done;
2088 if (printer_driver_in_use(info)) {
2089 status = WERR_PRINTER_DRIVER_IN_USE;
2090 goto done;
2094 * we have a couple of cases to consider.
2095 * (1) Are any files in use? If so and DPD_DELTE_ALL_FILE is set,
2096 * then the delete should fail if **any** files overlap with
2097 * other drivers
2098 * (2) If DPD_DELTE_UNUSED_FILES is sert, then delete all
2099 * non-overlapping files
2100 * (3) If neither DPD_DELTE_ALL_FILE nor DPD_DELTE_ALL_FILES
2101 * is set, the do not delete any files
2102 * Refer to MSDN docs on DeletePrinterDriverEx() for details.
2105 delete_files = r->in.delete_flags & (DPD_DELETE_ALL_FILES|DPD_DELETE_UNUSED_FILES);
2107 /* fail if any files are in use and DPD_DELETE_ALL_FILES is set */
2109 if (delete_files && printer_driver_files_in_use(info, info) & (r->in.delete_flags & DPD_DELETE_ALL_FILES)) {
2110 /* no idea of the correct error here */
2111 status = WERR_ACCESS_DENIED;
2112 goto done;
2116 /* also check for W32X86/3 if necessary; maybe we already have? */
2118 if ( (version == 2) && ((r->in.delete_flags & DPD_DELETE_SPECIFIC_VERSION) != DPD_DELETE_SPECIFIC_VERSION) ) {
2119 if (W_ERROR_IS_OK(get_a_printer_driver(p->mem_ctx, &info_win2k,
2120 r->in.driver,
2121 r->in.architecture, 3)))
2124 if (delete_files && printer_driver_files_in_use(info, info_win2k) & (r->in.delete_flags & DPD_DELETE_ALL_FILES) ) {
2125 /* no idea of the correct error here */
2126 free_a_printer_driver(info_win2k);
2127 status = WERR_ACCESS_DENIED;
2128 goto done;
2131 /* if we get to here, we now have 2 driver info structures to remove */
2132 /* remove the Win2k driver first*/
2134 status_win2k = delete_printer_driver(
2135 p, info_win2k, 3, delete_files);
2136 free_a_printer_driver(info_win2k);
2138 /* this should not have failed---if it did, report to client */
2140 if ( !W_ERROR_IS_OK(status_win2k) )
2141 goto done;
2145 status = delete_printer_driver(p, info, version, delete_files);
2147 if ( W_ERROR_IS_OK(status) || W_ERROR_IS_OK(status_win2k) )
2148 status = WERR_OK;
2149 done:
2150 free_a_printer_driver(info);
2152 return status;
2156 /****************************************************************************
2157 Internal routine for removing printerdata
2158 ***************************************************************************/
2160 static WERROR delete_printer_dataex( NT_PRINTER_INFO_LEVEL *printer, const char *key, const char *value )
2162 return delete_printer_data( printer->info_2, key, value );
2165 /****************************************************************************
2166 Internal routine for storing printerdata
2167 ***************************************************************************/
2169 WERROR set_printer_dataex(NT_PRINTER_INFO_LEVEL *printer,
2170 const char *key, const char *value,
2171 uint32_t type, uint8_t *data, int real_len)
2173 /* the registry objects enforce uniqueness based on value name */
2175 return add_printer_data( printer->info_2, key, value, type, data, real_len );
2178 /********************************************************************
2179 GetPrinterData on a printer server Handle.
2180 ********************************************************************/
2182 static WERROR getprinterdata_printer_server(TALLOC_CTX *mem_ctx,
2183 const char *value,
2184 enum winreg_Type *type,
2185 union spoolss_PrinterData *data)
2187 DEBUG(8,("getprinterdata_printer_server:%s\n", value));
2189 if (!StrCaseCmp(value, "W3SvcInstalled")) {
2190 *type = REG_DWORD;
2191 data->value = 0x00;
2192 return WERR_OK;
2195 if (!StrCaseCmp(value, "BeepEnabled")) {
2196 *type = REG_DWORD;
2197 data->value = 0x00;
2198 return WERR_OK;
2201 if (!StrCaseCmp(value, "EventLog")) {
2202 *type = REG_DWORD;
2203 /* formally was 0x1b */
2204 data->value = 0x00;
2205 return WERR_OK;
2208 if (!StrCaseCmp(value, "NetPopup")) {
2209 *type = REG_DWORD;
2210 data->value = 0x00;
2211 return WERR_OK;
2214 if (!StrCaseCmp(value, "MajorVersion")) {
2215 *type = REG_DWORD;
2217 /* Windows NT 4.0 seems to not allow uploading of drivers
2218 to a server that reports 0x3 as the MajorVersion.
2219 need to investigate more how Win2k gets around this .
2220 -- jerry */
2222 if (RA_WINNT == get_remote_arch()) {
2223 data->value = 0x02;
2224 } else {
2225 data->value = 0x03;
2228 return WERR_OK;
2231 if (!StrCaseCmp(value, "MinorVersion")) {
2232 *type = REG_DWORD;
2233 data->value = 0x00;
2234 return WERR_OK;
2237 /* REG_BINARY
2238 * uint32_t size = 0x114
2239 * uint32_t major = 5
2240 * uint32_t minor = [0|1]
2241 * uint32_t build = [2195|2600]
2242 * extra unicode string = e.g. "Service Pack 3"
2244 if (!StrCaseCmp(value, "OSVersion")) {
2245 DATA_BLOB blob;
2246 enum ndr_err_code ndr_err;
2247 struct spoolss_OSVersion os;
2249 os.major = 5; /* Windows 2000 == 5.0 */
2250 os.minor = 0;
2251 os.build = 2195; /* build */
2252 os.extra_string = ""; /* leave extra string empty */
2254 ndr_err = ndr_push_struct_blob(&blob, mem_ctx, NULL, &os,
2255 (ndr_push_flags_fn_t)ndr_push_spoolss_OSVersion);
2256 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2257 return WERR_GENERAL_FAILURE;
2260 *type = REG_BINARY;
2261 data->binary = blob;
2263 return WERR_OK;
2267 if (!StrCaseCmp(value, "DefaultSpoolDirectory")) {
2268 *type = REG_SZ;
2270 data->string = talloc_strdup(mem_ctx, "C:\\PRINTERS");
2271 W_ERROR_HAVE_NO_MEMORY(data->string);
2273 return WERR_OK;
2276 if (!StrCaseCmp(value, "Architecture")) {
2277 *type = REG_SZ;
2279 data->string = talloc_strdup(mem_ctx, "Windows NT x86");
2280 W_ERROR_HAVE_NO_MEMORY(data->string);
2282 return WERR_OK;
2285 if (!StrCaseCmp(value, "DsPresent")) {
2286 *type = REG_DWORD;
2288 /* only show the publish check box if we are a
2289 member of a AD domain */
2291 if (lp_security() == SEC_ADS) {
2292 data->value = 0x01;
2293 } else {
2294 data->value = 0x00;
2296 return WERR_OK;
2299 if (!StrCaseCmp(value, "DNSMachineName")) {
2300 const char *hostname = get_mydnsfullname();
2302 if (!hostname) {
2303 return WERR_BADFILE;
2306 *type = REG_SZ;
2307 data->string = talloc_strdup(mem_ctx, hostname);
2308 W_ERROR_HAVE_NO_MEMORY(data->string);
2310 return WERR_OK;
2313 *type = REG_NONE;
2315 return WERR_INVALID_PARAM;
2318 /****************************************************************
2319 _spoolss_GetPrinterData
2320 ****************************************************************/
2322 WERROR _spoolss_GetPrinterData(pipes_struct *p,
2323 struct spoolss_GetPrinterData *r)
2325 struct spoolss_GetPrinterDataEx r2;
2327 r2.in.handle = r->in.handle;
2328 r2.in.key_name = "PrinterDriverData";
2329 r2.in.value_name = r->in.value_name;
2330 r2.in.offered = r->in.offered;
2331 r2.out.type = r->out.type;
2332 r2.out.data = r->out.data;
2333 r2.out.needed = r->out.needed;
2335 return _spoolss_GetPrinterDataEx(p, &r2);
2338 /*********************************************************
2339 Connect to the client machine.
2340 **********************************************************/
2342 static bool spoolss_connect_to_client(struct rpc_pipe_client **pp_pipe,
2343 struct sockaddr_storage *client_ss, const char *remote_machine)
2345 NTSTATUS ret;
2346 struct cli_state *the_cli;
2347 struct sockaddr_storage rm_addr;
2349 if ( is_zero_addr((struct sockaddr *)client_ss) ) {
2350 if ( !resolve_name( remote_machine, &rm_addr, 0x20, false) ) {
2351 DEBUG(2,("spoolss_connect_to_client: Can't resolve address for %s\n", remote_machine));
2352 return false;
2355 if (ismyaddr((struct sockaddr *)&rm_addr)) {
2356 DEBUG(0,("spoolss_connect_to_client: Machine %s is one of our addresses. Cannot add to ourselves.\n", remote_machine));
2357 return false;
2359 } else {
2360 char addr[INET6_ADDRSTRLEN];
2361 rm_addr = *client_ss;
2362 print_sockaddr(addr, sizeof(addr), &rm_addr);
2363 DEBUG(5,("spoolss_connect_to_client: Using address %s (no name resolution necessary)\n",
2364 addr));
2367 /* setup the connection */
2369 ret = cli_full_connection( &the_cli, global_myname(), remote_machine,
2370 &rm_addr, 0, "IPC$", "IPC",
2371 "", /* username */
2372 "", /* domain */
2373 "", /* password */
2374 0, lp_client_signing(), NULL );
2376 if ( !NT_STATUS_IS_OK( ret ) ) {
2377 DEBUG(2,("spoolss_connect_to_client: connection to [%s] failed!\n",
2378 remote_machine ));
2379 return false;
2382 if ( the_cli->protocol != PROTOCOL_NT1 ) {
2383 DEBUG(0,("spoolss_connect_to_client: machine %s didn't negotiate NT protocol.\n", remote_machine));
2384 cli_shutdown(the_cli);
2385 return false;
2389 * Ok - we have an anonymous connection to the IPC$ share.
2390 * Now start the NT Domain stuff :-).
2393 ret = cli_rpc_pipe_open_noauth(the_cli, &ndr_table_spoolss.syntax_id, pp_pipe);
2394 if (!NT_STATUS_IS_OK(ret)) {
2395 DEBUG(2,("spoolss_connect_to_client: unable to open the spoolss pipe on machine %s. Error was : %s.\n",
2396 remote_machine, nt_errstr(ret)));
2397 cli_shutdown(the_cli);
2398 return false;
2401 return true;
2404 /***************************************************************************
2405 Connect to the client.
2406 ****************************************************************************/
2408 static bool srv_spoolss_replyopenprinter(int snum, const char *printer,
2409 uint32_t localprinter, uint32_t type,
2410 struct policy_handle *handle,
2411 struct sockaddr_storage *client_ss)
2413 WERROR result;
2414 NTSTATUS status;
2417 * If it's the first connection, contact the client
2418 * and connect to the IPC$ share anonymously
2420 if (smb_connections==0) {
2421 fstring unix_printer;
2423 fstrcpy(unix_printer, printer+2); /* the +2 is to strip the leading 2 backslashs */
2425 if ( !spoolss_connect_to_client( &notify_cli_pipe, client_ss, unix_printer ))
2426 return false;
2428 messaging_register(smbd_messaging_context(), NULL,
2429 MSG_PRINTER_NOTIFY2,
2430 receive_notify2_message_list);
2431 /* Tell the connections db we're now interested in printer
2432 * notify messages. */
2433 register_message_flags(true, FLAG_MSG_PRINT_NOTIFY);
2437 * Tell the specific printing tdb we want messages for this printer
2438 * by registering our PID.
2441 if (!print_notify_register_pid(snum))
2442 DEBUG(0,("print_notify_register_pid: Failed to register our pid for printer %s\n", printer ));
2444 smb_connections++;
2446 status = rpccli_spoolss_ReplyOpenPrinter(notify_cli_pipe, talloc_tos(),
2447 printer,
2448 localprinter,
2449 type,
2451 NULL,
2452 handle,
2453 &result);
2454 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result))
2455 DEBUG(5,("srv_spoolss_reply_open_printer: Client RPC returned [%s]\n",
2456 win_errstr(result)));
2458 return (W_ERROR_IS_OK(result));
2461 /****************************************************************
2462 ****************************************************************/
2464 static struct spoolss_NotifyOption *dup_spoolss_NotifyOption(TALLOC_CTX *mem_ctx,
2465 const struct spoolss_NotifyOption *r)
2467 struct spoolss_NotifyOption *option;
2468 uint32_t i,k;
2470 if (!r) {
2471 return NULL;
2474 option = talloc_zero(mem_ctx, struct spoolss_NotifyOption);
2475 if (!option) {
2476 return NULL;
2479 *option = *r;
2481 if (!option->count) {
2482 return option;
2485 option->types = talloc_zero_array(option,
2486 struct spoolss_NotifyOptionType, option->count);
2487 if (!option->types) {
2488 talloc_free(option);
2489 return NULL;
2492 for (i=0; i < option->count; i++) {
2493 option->types[i] = r->types[i];
2495 if (option->types[i].count) {
2496 option->types[i].fields = talloc_zero_array(option,
2497 union spoolss_Field, option->types[i].count);
2498 if (!option->types[i].fields) {
2499 talloc_free(option);
2500 return NULL;
2502 for (k=0; k<option->types[i].count; k++) {
2503 option->types[i].fields[k] =
2504 r->types[i].fields[k];
2509 return option;
2512 /****************************************************************
2513 * _spoolss_RemoteFindFirstPrinterChangeNotifyEx
2515 * before replying OK: status=0 a rpc call is made to the workstation
2516 * asking ReplyOpenPrinter
2518 * in fact ReplyOpenPrinter is the changenotify equivalent on the spoolss pipe
2519 * called from api_spoolss_rffpcnex
2520 ****************************************************************/
2522 WERROR _spoolss_RemoteFindFirstPrinterChangeNotifyEx(pipes_struct *p,
2523 struct spoolss_RemoteFindFirstPrinterChangeNotifyEx *r)
2525 int snum = -1;
2526 struct spoolss_NotifyOption *option = r->in.notify_options;
2527 struct sockaddr_storage client_ss;
2529 /* store the notify value in the printer struct */
2531 Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
2533 if (!Printer) {
2534 DEBUG(2,("_spoolss_RemoteFindFirstPrinterChangeNotifyEx: "
2535 "Invalid handle (%s:%u:%u).\n",
2536 OUR_HANDLE(r->in.handle)));
2537 return WERR_BADFID;
2540 Printer->notify.flags = r->in.flags;
2541 Printer->notify.options = r->in.options;
2542 Printer->notify.printerlocal = r->in.printer_local;
2544 TALLOC_FREE(Printer->notify.option);
2545 Printer->notify.option = dup_spoolss_NotifyOption(Printer, option);
2547 fstrcpy(Printer->notify.localmachine, r->in.local_machine);
2549 /* Connect to the client machine and send a ReplyOpenPrinter */
2551 if ( Printer->printer_type == SPLHND_SERVER)
2552 snum = -1;
2553 else if ( (Printer->printer_type == SPLHND_PRINTER) &&
2554 !get_printer_snum(p, r->in.handle, &snum, NULL) )
2555 return WERR_BADFID;
2557 if (!interpret_string_addr(&client_ss, p->client_address,
2558 AI_NUMERICHOST)) {
2559 return WERR_SERVER_UNAVAILABLE;
2562 if(!srv_spoolss_replyopenprinter(snum, Printer->notify.localmachine,
2563 Printer->notify.printerlocal, 1,
2564 &Printer->notify.client_hnd, &client_ss))
2565 return WERR_SERVER_UNAVAILABLE;
2567 Printer->notify.client_connected = true;
2569 return WERR_OK;
2572 /*******************************************************************
2573 * fill a notify_info_data with the servername
2574 ********************************************************************/
2576 void spoolss_notify_server_name(int snum,
2577 struct spoolss_Notify *data,
2578 print_queue_struct *queue,
2579 NT_PRINTER_INFO_LEVEL *printer,
2580 TALLOC_CTX *mem_ctx)
2582 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, printer->info_2->servername);
2585 /*******************************************************************
2586 * fill a notify_info_data with the printername (not including the servername).
2587 ********************************************************************/
2589 void spoolss_notify_printer_name(int snum,
2590 struct spoolss_Notify *data,
2591 print_queue_struct *queue,
2592 NT_PRINTER_INFO_LEVEL *printer,
2593 TALLOC_CTX *mem_ctx)
2595 /* the notify name should not contain the \\server\ part */
2596 char *p = strrchr(printer->info_2->printername, '\\');
2598 if (!p) {
2599 p = printer->info_2->printername;
2600 } else {
2601 p++;
2604 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, p);
2607 /*******************************************************************
2608 * fill a notify_info_data with the servicename
2609 ********************************************************************/
2611 void spoolss_notify_share_name(int snum,
2612 struct spoolss_Notify *data,
2613 print_queue_struct *queue,
2614 NT_PRINTER_INFO_LEVEL *printer,
2615 TALLOC_CTX *mem_ctx)
2617 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, lp_servicename(snum));
2620 /*******************************************************************
2621 * fill a notify_info_data with the port name
2622 ********************************************************************/
2624 void spoolss_notify_port_name(int snum,
2625 struct spoolss_Notify *data,
2626 print_queue_struct *queue,
2627 NT_PRINTER_INFO_LEVEL *printer,
2628 TALLOC_CTX *mem_ctx)
2630 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, printer->info_2->portname);
2633 /*******************************************************************
2634 * fill a notify_info_data with the printername
2635 * but it doesn't exist, have to see what to do
2636 ********************************************************************/
2638 void spoolss_notify_driver_name(int snum,
2639 struct spoolss_Notify *data,
2640 print_queue_struct *queue,
2641 NT_PRINTER_INFO_LEVEL *printer,
2642 TALLOC_CTX *mem_ctx)
2644 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, printer->info_2->drivername);
2647 /*******************************************************************
2648 * fill a notify_info_data with the comment
2649 ********************************************************************/
2651 void spoolss_notify_comment(int snum,
2652 struct spoolss_Notify *data,
2653 print_queue_struct *queue,
2654 NT_PRINTER_INFO_LEVEL *printer,
2655 TALLOC_CTX *mem_ctx)
2657 char *p;
2659 if (*printer->info_2->comment == '\0') {
2660 p = lp_comment(snum);
2661 } else {
2662 p = printer->info_2->comment;
2665 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, printer->info_2->comment);
2668 /*******************************************************************
2669 * fill a notify_info_data with the comment
2670 * location = "Room 1, floor 2, building 3"
2671 ********************************************************************/
2673 void spoolss_notify_location(int snum,
2674 struct spoolss_Notify *data,
2675 print_queue_struct *queue,
2676 NT_PRINTER_INFO_LEVEL *printer,
2677 TALLOC_CTX *mem_ctx)
2679 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, printer->info_2->location);
2682 /*******************************************************************
2683 * fill a notify_info_data with the device mode
2684 * jfm:xxxx don't to it for know but that's a real problem !!!
2685 ********************************************************************/
2687 static void spoolss_notify_devmode(int snum,
2688 struct spoolss_Notify *data,
2689 print_queue_struct *queue,
2690 NT_PRINTER_INFO_LEVEL *printer,
2691 TALLOC_CTX *mem_ctx)
2693 /* for a dummy implementation we have to zero the fields */
2694 SETUP_SPOOLSS_NOTIFY_DATA_DEVMODE(data, NULL);
2697 /*******************************************************************
2698 * fill a notify_info_data with the separator file name
2699 ********************************************************************/
2701 void spoolss_notify_sepfile(int snum,
2702 struct spoolss_Notify *data,
2703 print_queue_struct *queue,
2704 NT_PRINTER_INFO_LEVEL *printer,
2705 TALLOC_CTX *mem_ctx)
2707 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, printer->info_2->sepfile);
2710 /*******************************************************************
2711 * fill a notify_info_data with the print processor
2712 * jfm:xxxx return always winprint to indicate we don't do anything to it
2713 ********************************************************************/
2715 void spoolss_notify_print_processor(int snum,
2716 struct spoolss_Notify *data,
2717 print_queue_struct *queue,
2718 NT_PRINTER_INFO_LEVEL *printer,
2719 TALLOC_CTX *mem_ctx)
2721 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, printer->info_2->printprocessor);
2724 /*******************************************************************
2725 * fill a notify_info_data with the print processor options
2726 * jfm:xxxx send an empty string
2727 ********************************************************************/
2729 void spoolss_notify_parameters(int snum,
2730 struct spoolss_Notify *data,
2731 print_queue_struct *queue,
2732 NT_PRINTER_INFO_LEVEL *printer,
2733 TALLOC_CTX *mem_ctx)
2735 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, printer->info_2->parameters);
2738 /*******************************************************************
2739 * fill a notify_info_data with the data type
2740 * jfm:xxxx always send RAW as data type
2741 ********************************************************************/
2743 void spoolss_notify_datatype(int snum,
2744 struct spoolss_Notify *data,
2745 print_queue_struct *queue,
2746 NT_PRINTER_INFO_LEVEL *printer,
2747 TALLOC_CTX *mem_ctx)
2749 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, printer->info_2->datatype);
2752 /*******************************************************************
2753 * fill a notify_info_data with the security descriptor
2754 * jfm:xxxx send an null pointer to say no security desc
2755 * have to implement security before !
2756 ********************************************************************/
2758 static void spoolss_notify_security_desc(int snum,
2759 struct spoolss_Notify *data,
2760 print_queue_struct *queue,
2761 NT_PRINTER_INFO_LEVEL *printer,
2762 TALLOC_CTX *mem_ctx)
2764 SETUP_SPOOLSS_NOTIFY_DATA_SECDESC(data,
2765 printer->info_2->secdesc_buf->sd_size,
2766 printer->info_2->secdesc_buf->sd);
2769 /*******************************************************************
2770 * fill a notify_info_data with the attributes
2771 * jfm:xxxx a samba printer is always shared
2772 ********************************************************************/
2774 void spoolss_notify_attributes(int snum,
2775 struct spoolss_Notify *data,
2776 print_queue_struct *queue,
2777 NT_PRINTER_INFO_LEVEL *printer,
2778 TALLOC_CTX *mem_ctx)
2780 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, printer->info_2->attributes);
2783 /*******************************************************************
2784 * fill a notify_info_data with the priority
2785 ********************************************************************/
2787 static void spoolss_notify_priority(int snum,
2788 struct spoolss_Notify *data,
2789 print_queue_struct *queue,
2790 NT_PRINTER_INFO_LEVEL *printer,
2791 TALLOC_CTX *mem_ctx)
2793 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, printer->info_2->priority);
2796 /*******************************************************************
2797 * fill a notify_info_data with the default priority
2798 ********************************************************************/
2800 static void spoolss_notify_default_priority(int snum,
2801 struct spoolss_Notify *data,
2802 print_queue_struct *queue,
2803 NT_PRINTER_INFO_LEVEL *printer,
2804 TALLOC_CTX *mem_ctx)
2806 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, printer->info_2->default_priority);
2809 /*******************************************************************
2810 * fill a notify_info_data with the start time
2811 ********************************************************************/
2813 static void spoolss_notify_start_time(int snum,
2814 struct spoolss_Notify *data,
2815 print_queue_struct *queue,
2816 NT_PRINTER_INFO_LEVEL *printer,
2817 TALLOC_CTX *mem_ctx)
2819 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, printer->info_2->starttime);
2822 /*******************************************************************
2823 * fill a notify_info_data with the until time
2824 ********************************************************************/
2826 static void spoolss_notify_until_time(int snum,
2827 struct spoolss_Notify *data,
2828 print_queue_struct *queue,
2829 NT_PRINTER_INFO_LEVEL *printer,
2830 TALLOC_CTX *mem_ctx)
2832 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, printer->info_2->untiltime);
2835 /*******************************************************************
2836 * fill a notify_info_data with the status
2837 ********************************************************************/
2839 static void spoolss_notify_status(int snum,
2840 struct spoolss_Notify *data,
2841 print_queue_struct *queue,
2842 NT_PRINTER_INFO_LEVEL *printer,
2843 TALLOC_CTX *mem_ctx)
2845 print_status_struct status;
2847 print_queue_length(snum, &status);
2848 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, status.status);
2851 /*******************************************************************
2852 * fill a notify_info_data with the number of jobs queued
2853 ********************************************************************/
2855 void spoolss_notify_cjobs(int snum,
2856 struct spoolss_Notify *data,
2857 print_queue_struct *queue,
2858 NT_PRINTER_INFO_LEVEL *printer,
2859 TALLOC_CTX *mem_ctx)
2861 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, print_queue_length(snum, NULL));
2864 /*******************************************************************
2865 * fill a notify_info_data with the average ppm
2866 ********************************************************************/
2868 static void spoolss_notify_average_ppm(int snum,
2869 struct spoolss_Notify *data,
2870 print_queue_struct *queue,
2871 NT_PRINTER_INFO_LEVEL *printer,
2872 TALLOC_CTX *mem_ctx)
2874 /* always respond 8 pages per minutes */
2875 /* a little hard ! */
2876 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, printer->info_2->averageppm);
2879 /*******************************************************************
2880 * fill a notify_info_data with username
2881 ********************************************************************/
2883 static void spoolss_notify_username(int snum,
2884 struct spoolss_Notify *data,
2885 print_queue_struct *queue,
2886 NT_PRINTER_INFO_LEVEL *printer,
2887 TALLOC_CTX *mem_ctx)
2889 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, queue->fs_user);
2892 /*******************************************************************
2893 * fill a notify_info_data with job status
2894 ********************************************************************/
2896 static void spoolss_notify_job_status(int snum,
2897 struct spoolss_Notify *data,
2898 print_queue_struct *queue,
2899 NT_PRINTER_INFO_LEVEL *printer,
2900 TALLOC_CTX *mem_ctx)
2902 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, nt_printj_status(queue->status));
2905 /*******************************************************************
2906 * fill a notify_info_data with job name
2907 ********************************************************************/
2909 static void spoolss_notify_job_name(int snum,
2910 struct spoolss_Notify *data,
2911 print_queue_struct *queue,
2912 NT_PRINTER_INFO_LEVEL *printer,
2913 TALLOC_CTX *mem_ctx)
2915 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, queue->fs_file);
2918 /*******************************************************************
2919 * fill a notify_info_data with job status
2920 ********************************************************************/
2922 static void spoolss_notify_job_status_string(int snum,
2923 struct spoolss_Notify *data,
2924 print_queue_struct *queue,
2925 NT_PRINTER_INFO_LEVEL *printer,
2926 TALLOC_CTX *mem_ctx)
2929 * Now we're returning job status codes we just return a "" here. JRA.
2932 const char *p = "";
2934 #if 0 /* NO LONGER NEEDED - JRA. 02/22/2001 */
2935 p = "unknown";
2937 switch (queue->status) {
2938 case LPQ_QUEUED:
2939 p = "Queued";
2940 break;
2941 case LPQ_PAUSED:
2942 p = ""; /* NT provides the paused string */
2943 break;
2944 case LPQ_SPOOLING:
2945 p = "Spooling";
2946 break;
2947 case LPQ_PRINTING:
2948 p = "Printing";
2949 break;
2951 #endif /* NO LONGER NEEDED. */
2953 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, p);
2956 /*******************************************************************
2957 * fill a notify_info_data with job time
2958 ********************************************************************/
2960 static void spoolss_notify_job_time(int snum,
2961 struct spoolss_Notify *data,
2962 print_queue_struct *queue,
2963 NT_PRINTER_INFO_LEVEL *printer,
2964 TALLOC_CTX *mem_ctx)
2966 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, 0);
2969 /*******************************************************************
2970 * fill a notify_info_data with job size
2971 ********************************************************************/
2973 static void spoolss_notify_job_size(int snum,
2974 struct spoolss_Notify *data,
2975 print_queue_struct *queue,
2976 NT_PRINTER_INFO_LEVEL *printer,
2977 TALLOC_CTX *mem_ctx)
2979 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, queue->size);
2982 /*******************************************************************
2983 * fill a notify_info_data with page info
2984 ********************************************************************/
2985 static void spoolss_notify_total_pages(int snum,
2986 struct spoolss_Notify *data,
2987 print_queue_struct *queue,
2988 NT_PRINTER_INFO_LEVEL *printer,
2989 TALLOC_CTX *mem_ctx)
2991 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, queue->page_count);
2994 /*******************************************************************
2995 * fill a notify_info_data with pages printed info.
2996 ********************************************************************/
2997 static void spoolss_notify_pages_printed(int snum,
2998 struct spoolss_Notify *data,
2999 print_queue_struct *queue,
3000 NT_PRINTER_INFO_LEVEL *printer,
3001 TALLOC_CTX *mem_ctx)
3003 /* Add code when back-end tracks this */
3004 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, 0);
3007 /*******************************************************************
3008 Fill a notify_info_data with job position.
3009 ********************************************************************/
3011 static void spoolss_notify_job_position(int snum,
3012 struct spoolss_Notify *data,
3013 print_queue_struct *queue,
3014 NT_PRINTER_INFO_LEVEL *printer,
3015 TALLOC_CTX *mem_ctx)
3017 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, queue->job);
3020 /*******************************************************************
3021 Fill a notify_info_data with submitted time.
3022 ********************************************************************/
3024 static void spoolss_notify_submitted_time(int snum,
3025 struct spoolss_Notify *data,
3026 print_queue_struct *queue,
3027 NT_PRINTER_INFO_LEVEL *printer,
3028 TALLOC_CTX *mem_ctx)
3030 data->data.string.string = NULL;
3031 data->data.string.size = 0;
3033 init_systemtime_buffer(mem_ctx, gmtime(&queue->time),
3034 &data->data.string.string,
3035 &data->data.string.size);
3039 struct s_notify_info_data_table
3041 enum spoolss_NotifyType type;
3042 uint16_t field;
3043 const char *name;
3044 enum spoolss_NotifyTable variable_type;
3045 void (*fn) (int snum, struct spoolss_Notify *data,
3046 print_queue_struct *queue,
3047 NT_PRINTER_INFO_LEVEL *printer, TALLOC_CTX *mem_ctx);
3050 /* A table describing the various print notification constants and
3051 whether the notification data is a pointer to a variable sized
3052 buffer, a one value uint32_t or a two value uint32_t. */
3054 static const struct s_notify_info_data_table notify_info_data_table[] =
3056 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_SERVER_NAME, "PRINTER_NOTIFY_FIELD_SERVER_NAME", NOTIFY_TABLE_STRING, spoolss_notify_server_name },
3057 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_PRINTER_NAME, "PRINTER_NOTIFY_FIELD_PRINTER_NAME", NOTIFY_TABLE_STRING, spoolss_notify_printer_name },
3058 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_SHARE_NAME, "PRINTER_NOTIFY_FIELD_SHARE_NAME", NOTIFY_TABLE_STRING, spoolss_notify_share_name },
3059 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_PORT_NAME, "PRINTER_NOTIFY_FIELD_PORT_NAME", NOTIFY_TABLE_STRING, spoolss_notify_port_name },
3060 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_DRIVER_NAME, "PRINTER_NOTIFY_FIELD_DRIVER_NAME", NOTIFY_TABLE_STRING, spoolss_notify_driver_name },
3061 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_COMMENT, "PRINTER_NOTIFY_FIELD_COMMENT", NOTIFY_TABLE_STRING, spoolss_notify_comment },
3062 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_LOCATION, "PRINTER_NOTIFY_FIELD_LOCATION", NOTIFY_TABLE_STRING, spoolss_notify_location },
3063 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_DEVMODE, "PRINTER_NOTIFY_FIELD_DEVMODE", NOTIFY_TABLE_DEVMODE, spoolss_notify_devmode },
3064 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_SEPFILE, "PRINTER_NOTIFY_FIELD_SEPFILE", NOTIFY_TABLE_STRING, spoolss_notify_sepfile },
3065 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_PRINT_PROCESSOR, "PRINTER_NOTIFY_FIELD_PRINT_PROCESSOR", NOTIFY_TABLE_STRING, spoolss_notify_print_processor },
3066 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_PARAMETERS, "PRINTER_NOTIFY_FIELD_PARAMETERS", NOTIFY_TABLE_STRING, spoolss_notify_parameters },
3067 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_DATATYPE, "PRINTER_NOTIFY_FIELD_DATATYPE", NOTIFY_TABLE_STRING, spoolss_notify_datatype },
3068 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_SECURITY_DESCRIPTOR, "PRINTER_NOTIFY_FIELD_SECURITY_DESCRIPTOR", NOTIFY_TABLE_SECURITYDESCRIPTOR, spoolss_notify_security_desc },
3069 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_ATTRIBUTES, "PRINTER_NOTIFY_FIELD_ATTRIBUTES", NOTIFY_TABLE_DWORD, spoolss_notify_attributes },
3070 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_PRIORITY, "PRINTER_NOTIFY_FIELD_PRIORITY", NOTIFY_TABLE_DWORD, spoolss_notify_priority },
3071 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_DEFAULT_PRIORITY, "PRINTER_NOTIFY_FIELD_DEFAULT_PRIORITY", NOTIFY_TABLE_DWORD, spoolss_notify_default_priority },
3072 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_START_TIME, "PRINTER_NOTIFY_FIELD_START_TIME", NOTIFY_TABLE_DWORD, spoolss_notify_start_time },
3073 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_UNTIL_TIME, "PRINTER_NOTIFY_FIELD_UNTIL_TIME", NOTIFY_TABLE_DWORD, spoolss_notify_until_time },
3074 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_STATUS, "PRINTER_NOTIFY_FIELD_STATUS", NOTIFY_TABLE_DWORD, spoolss_notify_status },
3075 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_STATUS_STRING, "PRINTER_NOTIFY_FIELD_STATUS_STRING", NOTIFY_TABLE_STRING, NULL },
3076 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_CJOBS, "PRINTER_NOTIFY_FIELD_CJOBS", NOTIFY_TABLE_DWORD, spoolss_notify_cjobs },
3077 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_AVERAGE_PPM, "PRINTER_NOTIFY_FIELD_AVERAGE_PPM", NOTIFY_TABLE_DWORD, spoolss_notify_average_ppm },
3078 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_TOTAL_PAGES, "PRINTER_NOTIFY_FIELD_TOTAL_PAGES", NOTIFY_TABLE_DWORD, NULL },
3079 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_PAGES_PRINTED, "PRINTER_NOTIFY_FIELD_PAGES_PRINTED", NOTIFY_TABLE_DWORD, NULL },
3080 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_TOTAL_BYTES, "PRINTER_NOTIFY_FIELD_TOTAL_BYTES", NOTIFY_TABLE_DWORD, NULL },
3081 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_BYTES_PRINTED, "PRINTER_NOTIFY_FIELD_BYTES_PRINTED", NOTIFY_TABLE_DWORD, NULL },
3082 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_PRINTER_NAME, "JOB_NOTIFY_FIELD_PRINTER_NAME", NOTIFY_TABLE_STRING, spoolss_notify_printer_name },
3083 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_MACHINE_NAME, "JOB_NOTIFY_FIELD_MACHINE_NAME", NOTIFY_TABLE_STRING, spoolss_notify_server_name },
3084 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_PORT_NAME, "JOB_NOTIFY_FIELD_PORT_NAME", NOTIFY_TABLE_STRING, spoolss_notify_port_name },
3085 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_USER_NAME, "JOB_NOTIFY_FIELD_USER_NAME", NOTIFY_TABLE_STRING, spoolss_notify_username },
3086 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_NOTIFY_NAME, "JOB_NOTIFY_FIELD_NOTIFY_NAME", NOTIFY_TABLE_STRING, spoolss_notify_username },
3087 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_DATATYPE, "JOB_NOTIFY_FIELD_DATATYPE", NOTIFY_TABLE_STRING, spoolss_notify_datatype },
3088 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_PRINT_PROCESSOR, "JOB_NOTIFY_FIELD_PRINT_PROCESSOR", NOTIFY_TABLE_STRING, spoolss_notify_print_processor },
3089 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_PARAMETERS, "JOB_NOTIFY_FIELD_PARAMETERS", NOTIFY_TABLE_STRING, spoolss_notify_parameters },
3090 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_DRIVER_NAME, "JOB_NOTIFY_FIELD_DRIVER_NAME", NOTIFY_TABLE_STRING, spoolss_notify_driver_name },
3091 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_DEVMODE, "JOB_NOTIFY_FIELD_DEVMODE", NOTIFY_TABLE_DEVMODE, spoolss_notify_devmode },
3092 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_STATUS, "JOB_NOTIFY_FIELD_STATUS", NOTIFY_TABLE_DWORD, spoolss_notify_job_status },
3093 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_STATUS_STRING, "JOB_NOTIFY_FIELD_STATUS_STRING", NOTIFY_TABLE_STRING, spoolss_notify_job_status_string },
3094 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_SECURITY_DESCRIPTOR, "JOB_NOTIFY_FIELD_SECURITY_DESCRIPTOR", NOTIFY_TABLE_SECURITYDESCRIPTOR, NULL },
3095 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_DOCUMENT, "JOB_NOTIFY_FIELD_DOCUMENT", NOTIFY_TABLE_STRING, spoolss_notify_job_name },
3096 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_PRIORITY, "JOB_NOTIFY_FIELD_PRIORITY", NOTIFY_TABLE_DWORD, spoolss_notify_priority },
3097 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_POSITION, "JOB_NOTIFY_FIELD_POSITION", NOTIFY_TABLE_DWORD, spoolss_notify_job_position },
3098 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_SUBMITTED, "JOB_NOTIFY_FIELD_SUBMITTED", NOTIFY_TABLE_TIME, spoolss_notify_submitted_time },
3099 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_START_TIME, "JOB_NOTIFY_FIELD_START_TIME", NOTIFY_TABLE_DWORD, spoolss_notify_start_time },
3100 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_UNTIL_TIME, "JOB_NOTIFY_FIELD_UNTIL_TIME", NOTIFY_TABLE_DWORD, spoolss_notify_until_time },
3101 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_TIME, "JOB_NOTIFY_FIELD_TIME", NOTIFY_TABLE_DWORD, spoolss_notify_job_time },
3102 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_TOTAL_PAGES, "JOB_NOTIFY_FIELD_TOTAL_PAGES", NOTIFY_TABLE_DWORD, spoolss_notify_total_pages },
3103 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_PAGES_PRINTED, "JOB_NOTIFY_FIELD_PAGES_PRINTED", NOTIFY_TABLE_DWORD, spoolss_notify_pages_printed },
3104 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_TOTAL_BYTES, "JOB_NOTIFY_FIELD_TOTAL_BYTES", NOTIFY_TABLE_DWORD, spoolss_notify_job_size },
3107 /*******************************************************************
3108 Return the variable_type of info_data structure.
3109 ********************************************************************/
3111 static uint32_t variable_type_of_notify_info_data(enum spoolss_NotifyType type,
3112 uint16_t field)
3114 int i=0;
3116 for (i = 0; i < ARRAY_SIZE(notify_info_data_table); i++) {
3117 if ( (notify_info_data_table[i].type == type) &&
3118 (notify_info_data_table[i].field == field) ) {
3119 return notify_info_data_table[i].variable_type;
3123 DEBUG(5, ("invalid notify data type %d/%d\n", type, field));
3125 return 0;
3128 /****************************************************************************
3129 ****************************************************************************/
3131 static bool search_notify(enum spoolss_NotifyType type,
3132 uint16_t field,
3133 int *value)
3135 int i;
3137 for (i = 0; i < ARRAY_SIZE(notify_info_data_table); i++) {
3138 if (notify_info_data_table[i].type == type &&
3139 notify_info_data_table[i].field == field &&
3140 notify_info_data_table[i].fn != NULL) {
3141 *value = i;
3142 return true;
3146 return false;
3149 /****************************************************************************
3150 ****************************************************************************/
3152 void construct_info_data(struct spoolss_Notify *info_data,
3153 enum spoolss_NotifyType type,
3154 uint16_t field,
3155 int id)
3157 info_data->type = type;
3158 info_data->field.field = field;
3159 info_data->variable_type = variable_type_of_notify_info_data(type, field);
3160 info_data->job_id = id;
3163 /*******************************************************************
3165 * fill a notify_info struct with info asked
3167 ********************************************************************/
3169 static bool construct_notify_printer_info(Printer_entry *print_hnd,
3170 struct spoolss_NotifyInfo *info,
3171 int snum,
3172 const struct spoolss_NotifyOptionType *option_type,
3173 uint32_t id,
3174 TALLOC_CTX *mem_ctx)
3176 int field_num,j;
3177 enum spoolss_NotifyType type;
3178 uint16_t field;
3180 struct spoolss_Notify *current_data;
3181 NT_PRINTER_INFO_LEVEL *printer = NULL;
3182 print_queue_struct *queue=NULL;
3184 type = option_type->type;
3186 DEBUG(4,("construct_notify_printer_info: Notify type: [%s], number of notify info: [%d] on printer: [%s]\n",
3187 (type == PRINTER_NOTIFY_TYPE ? "PRINTER_NOTIFY_TYPE" : "JOB_NOTIFY_TYPE"),
3188 option_type->count, lp_servicename(snum)));
3190 if (!W_ERROR_IS_OK(get_a_printer(print_hnd, &printer, 2, lp_const_servicename(snum))))
3191 return false;
3193 for(field_num=0; field_num < option_type->count; field_num++) {
3194 field = option_type->fields[field_num].field;
3196 DEBUG(4,("construct_notify_printer_info: notify [%d]: type [%x], field [%x]\n", field_num, type, field));
3198 if (!search_notify(type, field, &j) )
3199 continue;
3201 info->notifies = TALLOC_REALLOC_ARRAY(info, info->notifies,
3202 struct spoolss_Notify,
3203 info->count + 1);
3204 if (info->notifies == NULL) {
3205 DEBUG(2,("construct_notify_printer_info: failed to enlarge buffer info->data!\n"));
3206 free_a_printer(&printer, 2);
3207 return false;
3210 current_data = &info->notifies[info->count];
3212 construct_info_data(current_data, type, field, id);
3214 DEBUG(10,("construct_notify_printer_info: calling [%s] snum=%d printername=[%s])\n",
3215 notify_info_data_table[j].name, snum, printer->info_2->printername ));
3217 notify_info_data_table[j].fn(snum, current_data, queue,
3218 printer, mem_ctx);
3220 info->count++;
3223 free_a_printer(&printer, 2);
3224 return true;
3227 /*******************************************************************
3229 * fill a notify_info struct with info asked
3231 ********************************************************************/
3233 static bool construct_notify_jobs_info(print_queue_struct *queue,
3234 struct spoolss_NotifyInfo *info,
3235 NT_PRINTER_INFO_LEVEL *printer,
3236 int snum,
3237 const struct spoolss_NotifyOptionType *option_type,
3238 uint32_t id,
3239 TALLOC_CTX *mem_ctx)
3241 int field_num,j;
3242 enum spoolss_NotifyType type;
3243 uint16_t field;
3244 struct spoolss_Notify *current_data;
3246 DEBUG(4,("construct_notify_jobs_info\n"));
3248 type = option_type->type;
3250 DEBUGADD(4,("Notify type: [%s], number of notify info: [%d]\n",
3251 (type == PRINTER_NOTIFY_TYPE ? "PRINTER_NOTIFY_TYPE" : "JOB_NOTIFY_TYPE"),
3252 option_type->count));
3254 for(field_num=0; field_num<option_type->count; field_num++) {
3255 field = option_type->fields[field_num].field;
3257 if (!search_notify(type, field, &j) )
3258 continue;
3260 info->notifies = TALLOC_REALLOC_ARRAY(info, info->notifies,
3261 struct spoolss_Notify,
3262 info->count + 1);
3263 if (info->notifies == NULL) {
3264 DEBUG(2,("construct_notify_jobs_info: failed to enlarg buffer info->data!\n"));
3265 return false;
3268 current_data=&(info->notifies[info->count]);
3270 construct_info_data(current_data, type, field, id);
3271 notify_info_data_table[j].fn(snum, current_data, queue,
3272 printer, mem_ctx);
3273 info->count++;
3276 return true;
3280 * JFM: The enumeration is not that simple, it's even non obvious.
3282 * let's take an example: I want to monitor the PRINTER SERVER for
3283 * the printer's name and the number of jobs currently queued.
3284 * So in the NOTIFY_OPTION, I have one NOTIFY_OPTION_TYPE structure.
3285 * Its type is PRINTER_NOTIFY_TYPE and it has 2 fields NAME and CJOBS.
3287 * I have 3 printers on the back of my server.
3289 * Now the response is a NOTIFY_INFO structure, with 6 NOTIFY_INFO_DATA
3290 * structures.
3291 * Number Data Id
3292 * 1 printer 1 name 1
3293 * 2 printer 1 cjob 1
3294 * 3 printer 2 name 2
3295 * 4 printer 2 cjob 2
3296 * 5 printer 3 name 3
3297 * 6 printer 3 name 3
3299 * that's the print server case, the printer case is even worse.
3302 /*******************************************************************
3304 * enumerate all printers on the printserver
3305 * fill a notify_info struct with info asked
3307 ********************************************************************/
3309 static WERROR printserver_notify_info(pipes_struct *p,
3310 struct policy_handle *hnd,
3311 struct spoolss_NotifyInfo *info,
3312 TALLOC_CTX *mem_ctx)
3314 int snum;
3315 Printer_entry *Printer = find_printer_index_by_hnd(p, hnd);
3316 int n_services=lp_numservices();
3317 int i;
3318 struct spoolss_NotifyOption *option;
3319 struct spoolss_NotifyOptionType option_type;
3321 DEBUG(4,("printserver_notify_info\n"));
3323 if (!Printer)
3324 return WERR_BADFID;
3326 option = Printer->notify.option;
3328 info->version = 2;
3329 info->notifies = NULL;
3330 info->count = 0;
3332 /* a bug in xp sp2 rc2 causes it to send a fnpcn request without
3333 sending a ffpcn() request first */
3335 if ( !option )
3336 return WERR_BADFID;
3338 for (i=0; i<option->count; i++) {
3339 option_type = option->types[i];
3341 if (option_type.type != PRINTER_NOTIFY_TYPE)
3342 continue;
3344 for (snum=0; snum<n_services; snum++)
3346 if ( lp_browseable(snum) && lp_snum_ok(snum) && lp_print_ok(snum) )
3347 construct_notify_printer_info ( Printer, info, snum, &option_type, snum, mem_ctx );
3351 #if 0
3353 * Debugging information, don't delete.
3356 DEBUG(1,("dumping the NOTIFY_INFO\n"));
3357 DEBUGADD(1,("info->version:[%d], info->flags:[%d], info->count:[%d]\n", info->version, info->flags, info->count));
3358 DEBUGADD(1,("num\ttype\tfield\tres\tid\tsize\tenc_type\n"));
3360 for (i=0; i<info->count; i++) {
3361 DEBUGADD(1,("[%d]\t[%d]\t[%d]\t[%d]\t[%d]\t[%d]\t[%d]\n",
3362 i, info->data[i].type, info->data[i].field, info->data[i].reserved,
3363 info->data[i].id, info->data[i].size, info->data[i].enc_type));
3365 #endif
3367 return WERR_OK;
3370 /*******************************************************************
3372 * fill a notify_info struct with info asked
3374 ********************************************************************/
3376 static WERROR printer_notify_info(pipes_struct *p, struct policy_handle *hnd,
3377 struct spoolss_NotifyInfo *info,
3378 TALLOC_CTX *mem_ctx)
3380 int snum;
3381 Printer_entry *Printer = find_printer_index_by_hnd(p, hnd);
3382 int i;
3383 uint32_t id;
3384 struct spoolss_NotifyOption *option;
3385 struct spoolss_NotifyOptionType option_type;
3386 int count,j;
3387 print_queue_struct *queue=NULL;
3388 print_status_struct status;
3390 DEBUG(4,("printer_notify_info\n"));
3392 if (!Printer)
3393 return WERR_BADFID;
3395 option = Printer->notify.option;
3396 id = 0x0;
3398 info->version = 2;
3399 info->notifies = NULL;
3400 info->count = 0;
3402 /* a bug in xp sp2 rc2 causes it to send a fnpcn request without
3403 sending a ffpcn() request first */
3405 if ( !option )
3406 return WERR_BADFID;
3408 get_printer_snum(p, hnd, &snum, NULL);
3410 for (i=0; i<option->count; i++) {
3411 option_type = option->types[i];
3413 switch (option_type.type) {
3414 case PRINTER_NOTIFY_TYPE:
3415 if(construct_notify_printer_info(Printer, info, snum,
3416 &option_type, id,
3417 mem_ctx))
3418 id--;
3419 break;
3421 case JOB_NOTIFY_TYPE: {
3422 NT_PRINTER_INFO_LEVEL *printer = NULL;
3424 count = print_queue_status(snum, &queue, &status);
3426 if (!W_ERROR_IS_OK(get_a_printer(Printer, &printer, 2, lp_const_servicename(snum))))
3427 goto done;
3429 for (j=0; j<count; j++) {
3430 construct_notify_jobs_info(&queue[j], info,
3431 printer, snum,
3432 &option_type,
3433 queue[j].job,
3434 mem_ctx);
3437 free_a_printer(&printer, 2);
3439 done:
3440 SAFE_FREE(queue);
3441 break;
3447 * Debugging information, don't delete.
3450 DEBUG(1,("dumping the NOTIFY_INFO\n"));
3451 DEBUGADD(1,("info->version:[%d], info->flags:[%d], info->count:[%d]\n", info->version, info->flags, info->count));
3452 DEBUGADD(1,("num\ttype\tfield\tres\tid\tsize\tenc_type\n"));
3454 for (i=0; i<info->count; i++) {
3455 DEBUGADD(1,("[%d]\t[%d]\t[%d]\t[%d]\t[%d]\t[%d]\t[%d]\n",
3456 i, info->data[i].type, info->data[i].field, info->data[i].reserved,
3457 info->data[i].id, info->data[i].size, info->data[i].enc_type));
3460 return WERR_OK;
3463 /****************************************************************
3464 _spoolss_RouterRefreshPrinterChangeNotify
3465 ****************************************************************/
3467 WERROR _spoolss_RouterRefreshPrinterChangeNotify(pipes_struct *p,
3468 struct spoolss_RouterRefreshPrinterChangeNotify *r)
3470 struct spoolss_NotifyInfo *info;
3472 Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
3473 WERROR result = WERR_BADFID;
3475 /* we always have a spoolss_NotifyInfo struct */
3476 info = talloc_zero(p->mem_ctx, struct spoolss_NotifyInfo);
3477 if (!info) {
3478 result = WERR_NOMEM;
3479 goto done;
3482 *r->out.info = info;
3484 if (!Printer) {
3485 DEBUG(2,("_spoolss_RouterRefreshPrinterChangeNotify: "
3486 "Invalid handle (%s:%u:%u).\n",
3487 OUR_HANDLE(r->in.handle)));
3488 goto done;
3491 DEBUG(4,("Printer type %x\n",Printer->printer_type));
3494 * We are now using the change value, and
3495 * I should check for PRINTER_NOTIFY_OPTIONS_REFRESH but as
3496 * I don't have a global notification system, I'm sending back all the
3497 * informations even when _NOTHING_ has changed.
3500 /* We need to keep track of the change value to send back in
3501 RRPCN replies otherwise our updates are ignored. */
3503 Printer->notify.fnpcn = true;
3505 if (Printer->notify.client_connected) {
3506 DEBUG(10,("_spoolss_RouterRefreshPrinterChangeNotify: "
3507 "Saving change value in request [%x]\n",
3508 r->in.change_low));
3509 Printer->notify.change = r->in.change_low;
3512 /* just ignore the spoolss_NotifyOption */
3514 switch (Printer->printer_type) {
3515 case SPLHND_SERVER:
3516 result = printserver_notify_info(p, r->in.handle,
3517 info, p->mem_ctx);
3518 break;
3520 case SPLHND_PRINTER:
3521 result = printer_notify_info(p, r->in.handle,
3522 info, p->mem_ctx);
3523 break;
3526 Printer->notify.fnpcn = false;
3528 done:
3529 return result;
3532 /********************************************************************
3533 * construct_printer_info_0
3534 * fill a printer_info_0 struct
3535 ********************************************************************/
3537 static WERROR construct_printer_info0(TALLOC_CTX *mem_ctx,
3538 const NT_PRINTER_INFO_LEVEL *ntprinter,
3539 struct spoolss_PrinterInfo0 *r,
3540 int snum)
3542 int count;
3543 counter_printer_0 *session_counter;
3544 time_t setuptime;
3545 print_status_struct status;
3547 r->printername = talloc_strdup(mem_ctx, ntprinter->info_2->printername);
3548 W_ERROR_HAVE_NO_MEMORY(r->printername);
3550 r->servername = talloc_strdup(mem_ctx, ntprinter->info_2->servername);
3551 W_ERROR_HAVE_NO_MEMORY(r->servername);
3553 count = print_queue_length(snum, &status);
3555 /* check if we already have a counter for this printer */
3556 for (session_counter = counter_list; session_counter; session_counter = session_counter->next) {
3557 if (session_counter->snum == snum)
3558 break;
3561 /* it's the first time, add it to the list */
3562 if (session_counter == NULL) {
3563 session_counter = SMB_MALLOC_P(counter_printer_0);
3564 W_ERROR_HAVE_NO_MEMORY(session_counter);
3565 ZERO_STRUCTP(session_counter);
3566 session_counter->snum = snum;
3567 session_counter->counter = 0;
3568 DLIST_ADD(counter_list, session_counter);
3571 /* increment it */
3572 session_counter->counter++;
3574 r->cjobs = count;
3575 r->total_jobs = 0;
3576 r->total_bytes = 0;
3578 setuptime = (time_t)ntprinter->info_2->setuptime;
3580 init_systemtime(&r->time, gmtime(&setuptime));
3582 /* JFM:
3583 * the global_counter should be stored in a TDB as it's common to all the clients
3584 * and should be zeroed on samba startup
3586 r->global_counter = session_counter->counter;
3587 r->total_pages = 0;
3588 /* in 2.2 we reported ourselves as 0x0004 and 0x0565 */
3589 r->version = 0x0005; /* NT 5 */
3590 r->free_build = 0x0893; /* build 2195 */
3591 r->spooling = 0;
3592 r->max_spooling = 0;
3593 r->session_counter = session_counter->counter;
3594 r->num_error_out_of_paper = 0x0;
3595 r->num_error_not_ready = 0x0; /* number of print failure */
3596 r->job_error = 0x0;
3597 r->number_of_processors = 0x1;
3598 r->processor_type = PROCESSOR_INTEL_PENTIUM; /* 586 Pentium ? */
3599 r->high_part_total_bytes = 0x0;
3600 r->change_id = ntprinter->info_2->changeid; /* ChangeID in milliseconds*/
3601 r->last_error = WERR_OK;
3602 r->status = nt_printq_status(status.status);
3603 r->enumerate_network_printers = 0x0;
3604 r->c_setprinter = get_c_setprinter(); /* monotonically increasing sum of delta printer counts */
3605 r->processor_architecture = 0x0;
3606 r->processor_level = 0x6; /* 6 ???*/
3607 r->ref_ic = 0;
3608 r->reserved2 = 0;
3609 r->reserved3 = 0;
3611 return WERR_OK;
3614 /****************************************************************************
3615 Convert an NT_DEVICEMODE to a spoolss_DeviceMode structure. Both pointers
3616 should be valid upon entry
3617 ****************************************************************************/
3619 static WERROR convert_nt_devicemode(TALLOC_CTX *mem_ctx,
3620 struct spoolss_DeviceMode *r,
3621 const NT_DEVICEMODE *ntdevmode)
3623 if (!r || !ntdevmode) {
3624 return WERR_INVALID_PARAM;
3627 r->devicename = talloc_strdup(mem_ctx, ntdevmode->devicename);
3628 W_ERROR_HAVE_NO_MEMORY(r->devicename);
3630 r->specversion = ntdevmode->specversion;
3631 r->driverversion = ntdevmode->driverversion;
3632 r->size = ntdevmode->size;
3633 r->__driverextra_length = ntdevmode->driverextra;
3634 r->fields = ntdevmode->fields;
3636 r->orientation = ntdevmode->orientation;
3637 r->papersize = ntdevmode->papersize;
3638 r->paperlength = ntdevmode->paperlength;
3639 r->paperwidth = ntdevmode->paperwidth;
3640 r->scale = ntdevmode->scale;
3641 r->copies = ntdevmode->copies;
3642 r->defaultsource = ntdevmode->defaultsource;
3643 r->printquality = ntdevmode->printquality;
3644 r->color = ntdevmode->color;
3645 r->duplex = ntdevmode->duplex;
3646 r->yresolution = ntdevmode->yresolution;
3647 r->ttoption = ntdevmode->ttoption;
3648 r->collate = ntdevmode->collate;
3650 r->formname = talloc_strdup(mem_ctx, ntdevmode->formname);
3651 W_ERROR_HAVE_NO_MEMORY(r->formname);
3653 /* all 0 below are values that have not been set in the old parsing/copy
3654 * function, maybe they should... - gd */
3656 r->logpixels = 0;
3657 r->bitsperpel = 0;
3658 r->pelswidth = 0;
3659 r->pelsheight = 0;
3660 r->displayflags = 0;
3661 r->displayfrequency = 0;
3662 r->icmmethod = ntdevmode->icmmethod;
3663 r->icmintent = ntdevmode->icmintent;
3664 r->mediatype = ntdevmode->mediatype;
3665 r->dithertype = ntdevmode->dithertype;
3666 r->reserved1 = 0;
3667 r->reserved2 = 0;
3668 r->panningwidth = 0;
3669 r->panningheight = 0;
3671 if (ntdevmode->nt_dev_private != NULL) {
3672 r->driverextra_data = data_blob_talloc(mem_ctx,
3673 ntdevmode->nt_dev_private,
3674 ntdevmode->driverextra);
3675 W_ERROR_HAVE_NO_MEMORY(r->driverextra_data.data);
3678 return WERR_OK;
3682 /****************************************************************************
3683 Create a spoolss_DeviceMode struct. Returns talloced memory.
3684 ****************************************************************************/
3686 struct spoolss_DeviceMode *construct_dev_mode(TALLOC_CTX *mem_ctx,
3687 const char *servicename)
3689 WERROR result;
3690 NT_PRINTER_INFO_LEVEL *printer = NULL;
3691 struct spoolss_DeviceMode *devmode = NULL;
3693 DEBUG(7,("construct_dev_mode\n"));
3695 DEBUGADD(8,("getting printer characteristics\n"));
3697 if (!W_ERROR_IS_OK(get_a_printer(NULL, &printer, 2, servicename)))
3698 return NULL;
3700 if (!printer->info_2->devmode) {
3701 DEBUG(5, ("BONG! There was no device mode!\n"));
3702 goto done;
3705 devmode = TALLOC_ZERO_P(mem_ctx, struct spoolss_DeviceMode);
3706 if (!devmode) {
3707 DEBUG(2,("construct_dev_mode: talloc fail.\n"));
3708 goto done;
3711 DEBUGADD(8,("loading DEVICEMODE\n"));
3713 result = convert_nt_devicemode(mem_ctx, devmode, printer->info_2->devmode);
3714 if (!W_ERROR_IS_OK(result)) {
3715 TALLOC_FREE(devmode);
3718 done:
3719 free_a_printer(&printer,2);
3721 return devmode;
3724 /********************************************************************
3725 * construct_printer_info1
3726 * fill a spoolss_PrinterInfo1 struct
3727 ********************************************************************/
3729 static WERROR construct_printer_info1(TALLOC_CTX *mem_ctx,
3730 const NT_PRINTER_INFO_LEVEL *ntprinter,
3731 uint32_t flags,
3732 struct spoolss_PrinterInfo1 *r,
3733 int snum)
3735 r->flags = flags;
3737 r->description = talloc_asprintf(mem_ctx, "%s,%s,%s",
3738 ntprinter->info_2->printername,
3739 ntprinter->info_2->drivername,
3740 ntprinter->info_2->location);
3741 W_ERROR_HAVE_NO_MEMORY(r->description);
3743 if (*ntprinter->info_2->comment == '\0') {
3744 r->comment = talloc_strdup(mem_ctx, lp_comment(snum));
3745 } else {
3746 r->comment = talloc_strdup(mem_ctx, ntprinter->info_2->comment); /* saved comment */
3748 W_ERROR_HAVE_NO_MEMORY(r->comment);
3750 r->name = talloc_strdup(mem_ctx, ntprinter->info_2->printername);
3751 W_ERROR_HAVE_NO_MEMORY(r->name);
3753 return WERR_OK;
3756 /********************************************************************
3757 * construct_printer_info2
3758 * fill a spoolss_PrinterInfo2 struct
3759 ********************************************************************/
3761 static WERROR construct_printer_info2(TALLOC_CTX *mem_ctx,
3762 const NT_PRINTER_INFO_LEVEL *ntprinter,
3763 struct spoolss_PrinterInfo2 *r,
3764 int snum)
3766 int count;
3768 print_status_struct status;
3770 count = print_queue_length(snum, &status);
3772 r->servername = talloc_strdup(mem_ctx, ntprinter->info_2->servername);
3773 W_ERROR_HAVE_NO_MEMORY(r->servername);
3774 r->printername = talloc_strdup(mem_ctx, ntprinter->info_2->printername);
3775 W_ERROR_HAVE_NO_MEMORY(r->printername);
3776 r->sharename = talloc_strdup(mem_ctx, lp_servicename(snum));
3777 W_ERROR_HAVE_NO_MEMORY(r->sharename);
3778 r->portname = talloc_strdup(mem_ctx, ntprinter->info_2->portname);
3779 W_ERROR_HAVE_NO_MEMORY(r->portname);
3780 r->drivername = talloc_strdup(mem_ctx, ntprinter->info_2->drivername);
3781 W_ERROR_HAVE_NO_MEMORY(r->drivername);
3783 if (*ntprinter->info_2->comment == '\0') {
3784 r->comment = talloc_strdup(mem_ctx, lp_comment(snum));
3785 } else {
3786 r->comment = talloc_strdup(mem_ctx, ntprinter->info_2->comment);
3788 W_ERROR_HAVE_NO_MEMORY(r->comment);
3790 r->location = talloc_strdup(mem_ctx, ntprinter->info_2->location);
3791 W_ERROR_HAVE_NO_MEMORY(r->location);
3792 r->sepfile = talloc_strdup(mem_ctx, ntprinter->info_2->sepfile);
3793 W_ERROR_HAVE_NO_MEMORY(r->sepfile);
3794 r->printprocessor = talloc_strdup(mem_ctx, ntprinter->info_2->printprocessor);
3795 W_ERROR_HAVE_NO_MEMORY(r->printprocessor);
3796 r->datatype = talloc_strdup(mem_ctx, ntprinter->info_2->datatype);
3797 W_ERROR_HAVE_NO_MEMORY(r->datatype);
3798 r->parameters = talloc_strdup(mem_ctx, ntprinter->info_2->parameters);
3799 W_ERROR_HAVE_NO_MEMORY(r->parameters);
3801 r->attributes = ntprinter->info_2->attributes;
3803 r->priority = ntprinter->info_2->priority;
3804 r->defaultpriority = ntprinter->info_2->default_priority;
3805 r->starttime = ntprinter->info_2->starttime;
3806 r->untiltime = ntprinter->info_2->untiltime;
3807 r->status = nt_printq_status(status.status);
3808 r->cjobs = count;
3809 r->averageppm = ntprinter->info_2->averageppm;
3811 r->devmode = construct_dev_mode(mem_ctx, lp_const_servicename(snum));
3812 if (!r->devmode) {
3813 DEBUG(8,("Returning NULL Devicemode!\n"));
3816 r->secdesc = NULL;
3818 if (ntprinter->info_2->secdesc_buf && ntprinter->info_2->secdesc_buf->sd_size != 0) {
3819 /* don't use talloc_steal() here unless you do a deep steal of all
3820 the SEC_DESC members */
3822 r->secdesc = dup_sec_desc(mem_ctx, ntprinter->info_2->secdesc_buf->sd);
3825 return WERR_OK;
3828 /********************************************************************
3829 * construct_printer_info3
3830 * fill a spoolss_PrinterInfo3 struct
3831 ********************************************************************/
3833 static WERROR construct_printer_info3(TALLOC_CTX *mem_ctx,
3834 const NT_PRINTER_INFO_LEVEL *ntprinter,
3835 struct spoolss_PrinterInfo3 *r,
3836 int snum)
3838 /* These are the components of the SD we are returning. */
3840 if (ntprinter->info_2->secdesc_buf && ntprinter->info_2->secdesc_buf->sd_size != 0) {
3841 /* don't use talloc_steal() here unless you do a deep steal of all
3842 the SEC_DESC members */
3844 r->secdesc = dup_sec_desc(mem_ctx,
3845 ntprinter->info_2->secdesc_buf->sd);
3846 W_ERROR_HAVE_NO_MEMORY(r->secdesc);
3849 return WERR_OK;
3852 /********************************************************************
3853 * construct_printer_info4
3854 * fill a spoolss_PrinterInfo4 struct
3855 ********************************************************************/
3857 static WERROR construct_printer_info4(TALLOC_CTX *mem_ctx,
3858 const NT_PRINTER_INFO_LEVEL *ntprinter,
3859 struct spoolss_PrinterInfo4 *r,
3860 int snum)
3862 r->printername = talloc_strdup(mem_ctx, ntprinter->info_2->printername);
3863 W_ERROR_HAVE_NO_MEMORY(r->printername);
3864 r->servername = talloc_strdup(mem_ctx, ntprinter->info_2->servername);
3865 W_ERROR_HAVE_NO_MEMORY(r->servername);
3867 r->attributes = ntprinter->info_2->attributes;
3869 return WERR_OK;
3872 /********************************************************************
3873 * construct_printer_info5
3874 * fill a spoolss_PrinterInfo5 struct
3875 ********************************************************************/
3877 static WERROR construct_printer_info5(TALLOC_CTX *mem_ctx,
3878 const NT_PRINTER_INFO_LEVEL *ntprinter,
3879 struct spoolss_PrinterInfo5 *r,
3880 int snum)
3882 r->printername = talloc_strdup(mem_ctx, ntprinter->info_2->printername);
3883 W_ERROR_HAVE_NO_MEMORY(r->printername);
3884 r->portname = talloc_strdup(mem_ctx, ntprinter->info_2->portname);
3885 W_ERROR_HAVE_NO_MEMORY(r->portname);
3887 r->attributes = ntprinter->info_2->attributes;
3889 /* these two are not used by NT+ according to MSDN */
3891 r->device_not_selected_timeout = 0x0; /* have seen 0x3a98 */
3892 r->transmission_retry_timeout = 0x0; /* have seen 0xafc8 */
3894 return WERR_OK;
3897 /********************************************************************
3898 * construct_printer_info_6
3899 * fill a spoolss_PrinterInfo6 struct
3900 ********************************************************************/
3902 static WERROR construct_printer_info6(TALLOC_CTX *mem_ctx,
3903 const NT_PRINTER_INFO_LEVEL *ntprinter,
3904 struct spoolss_PrinterInfo6 *r,
3905 int snum)
3907 int count;
3908 print_status_struct status;
3910 count = print_queue_length(snum, &status);
3912 r->status = nt_printq_status(status.status);
3914 return WERR_OK;
3917 /********************************************************************
3918 * construct_printer_info7
3919 * fill a spoolss_PrinterInfo7 struct
3920 ********************************************************************/
3922 static WERROR construct_printer_info7(TALLOC_CTX *mem_ctx,
3923 Printer_entry *print_hnd,
3924 struct spoolss_PrinterInfo7 *r,
3925 int snum)
3927 struct GUID guid;
3929 if (is_printer_published(print_hnd, snum, &guid)) {
3930 r->guid = talloc_strdup_upper(mem_ctx, GUID_string2(mem_ctx, &guid));
3931 r->action = DSPRINT_PUBLISH;
3932 } else {
3933 r->guid = talloc_strdup(mem_ctx, "");
3934 r->action = DSPRINT_UNPUBLISH;
3936 W_ERROR_HAVE_NO_MEMORY(r->guid);
3938 return WERR_OK;
3941 /********************************************************************
3942 * construct_printer_info8
3943 * fill a spoolss_PrinterInfo8 struct
3944 ********************************************************************/
3946 static WERROR construct_printer_info8(TALLOC_CTX *mem_ctx,
3947 const NT_PRINTER_INFO_LEVEL *ntprinter,
3948 struct spoolss_DeviceModeInfo *r,
3949 int snum)
3951 struct spoolss_DeviceMode *devmode;
3952 WERROR result;
3954 if (!ntprinter->info_2->devmode) {
3955 r->devmode = NULL;
3956 return WERR_OK;
3959 devmode = TALLOC_ZERO_P(mem_ctx, struct spoolss_DeviceMode);
3960 W_ERROR_HAVE_NO_MEMORY(devmode);
3962 result = convert_nt_devicemode(mem_ctx, devmode, ntprinter->info_2->devmode);
3963 if (!W_ERROR_IS_OK(result)) {
3964 TALLOC_FREE(devmode);
3965 return result;
3968 r->devmode = devmode;
3970 return WERR_OK;
3974 /********************************************************************
3975 ********************************************************************/
3977 static bool snum_is_shared_printer(int snum)
3979 return (lp_browseable(snum) && lp_snum_ok(snum) && lp_print_ok(snum));
3982 /********************************************************************
3983 Spoolss_enumprinters.
3984 ********************************************************************/
3986 static WERROR enum_all_printers_info_level(TALLOC_CTX *mem_ctx,
3987 uint32_t level,
3988 uint32_t flags,
3989 union spoolss_PrinterInfo **info_p,
3990 uint32_t *count_p)
3992 int snum;
3993 int n_services = lp_numservices();
3994 union spoolss_PrinterInfo *info = NULL;
3995 uint32_t count = 0;
3996 WERROR result = WERR_OK;
3998 *count_p = 0;
3999 *info_p = NULL;
4001 for (snum = 0; snum < n_services; snum++) {
4003 NT_PRINTER_INFO_LEVEL *ntprinter = NULL;
4005 if (!snum_is_shared_printer(snum)) {
4006 continue;
4009 DEBUG(4,("Found a printer in smb.conf: %s[%x]\n",
4010 lp_servicename(snum), snum));
4012 info = TALLOC_REALLOC_ARRAY(mem_ctx, info,
4013 union spoolss_PrinterInfo,
4014 count + 1);
4015 if (!info) {
4016 result = WERR_NOMEM;
4017 goto out;
4020 result = get_a_printer(NULL, &ntprinter, 2,
4021 lp_const_servicename(snum));
4022 if (!W_ERROR_IS_OK(result)) {
4023 goto out;
4026 switch (level) {
4027 case 0:
4028 result = construct_printer_info0(info, ntprinter,
4029 &info[count].info0, snum);
4030 break;
4031 case 1:
4032 result = construct_printer_info1(info, ntprinter, flags,
4033 &info[count].info1, snum);
4034 break;
4035 case 2:
4036 result = construct_printer_info2(info, ntprinter,
4037 &info[count].info2, snum);
4038 break;
4039 case 4:
4040 result = construct_printer_info4(info, ntprinter,
4041 &info[count].info4, snum);
4042 break;
4043 case 5:
4044 result = construct_printer_info5(info, ntprinter,
4045 &info[count].info5, snum);
4046 break;
4048 default:
4049 result = WERR_UNKNOWN_LEVEL;
4050 free_a_printer(&ntprinter, 2);
4051 goto out;
4054 free_a_printer(&ntprinter, 2);
4055 if (!W_ERROR_IS_OK(result)) {
4056 goto out;
4059 count++;
4062 *count_p = count;
4063 *info_p = info;
4065 out:
4066 if (!W_ERROR_IS_OK(result)) {
4067 TALLOC_FREE(info);
4068 return result;
4071 *info_p = info;
4073 return WERR_OK;
4076 /********************************************************************
4077 * handle enumeration of printers at level 0
4078 ********************************************************************/
4080 static WERROR enumprinters_level0(TALLOC_CTX *mem_ctx,
4081 uint32_t flags,
4082 const char *servername,
4083 union spoolss_PrinterInfo **info,
4084 uint32_t *count)
4086 DEBUG(4,("enum_all_printers_info_0\n"));
4088 return enum_all_printers_info_level(mem_ctx, 0, flags, info, count);
4092 /********************************************************************
4093 ********************************************************************/
4095 static WERROR enum_all_printers_info_1(TALLOC_CTX *mem_ctx,
4096 uint32_t flags,
4097 union spoolss_PrinterInfo **info,
4098 uint32_t *count)
4100 DEBUG(4,("enum_all_printers_info_1\n"));
4102 return enum_all_printers_info_level(mem_ctx, 1, flags, info, count);
4105 /********************************************************************
4106 enum_all_printers_info_1_local.
4107 *********************************************************************/
4109 static WERROR enum_all_printers_info_1_local(TALLOC_CTX *mem_ctx,
4110 union spoolss_PrinterInfo **info,
4111 uint32_t *count)
4113 DEBUG(4,("enum_all_printers_info_1_local\n"));
4115 return enum_all_printers_info_1(mem_ctx, PRINTER_ENUM_ICON8, info, count);
4118 /********************************************************************
4119 enum_all_printers_info_1_name.
4120 *********************************************************************/
4122 static WERROR enum_all_printers_info_1_name(TALLOC_CTX *mem_ctx,
4123 const char *name,
4124 union spoolss_PrinterInfo **info,
4125 uint32_t *count)
4127 const char *s = name;
4129 DEBUG(4,("enum_all_printers_info_1_name\n"));
4131 if ((name[0] == '\\') && (name[1] == '\\')) {
4132 s = name + 2;
4135 if (!is_myname_or_ipaddr(s)) {
4136 return WERR_INVALID_NAME;
4139 return enum_all_printers_info_1(mem_ctx, PRINTER_ENUM_ICON8, info, count);
4142 /********************************************************************
4143 enum_all_printers_info_1_network.
4144 *********************************************************************/
4146 static WERROR enum_all_printers_info_1_network(TALLOC_CTX *mem_ctx,
4147 const char *name,
4148 union spoolss_PrinterInfo **info,
4149 uint32_t *count)
4151 const char *s = name;
4153 DEBUG(4,("enum_all_printers_info_1_network\n"));
4155 /* If we respond to a enum_printers level 1 on our name with flags
4156 set to PRINTER_ENUM_REMOTE with a list of printers then these
4157 printers incorrectly appear in the APW browse list.
4158 Specifically the printers for the server appear at the workgroup
4159 level where all the other servers in the domain are
4160 listed. Windows responds to this call with a
4161 WERR_CAN_NOT_COMPLETE so we should do the same. */
4163 if (name[0] == '\\' && name[1] == '\\') {
4164 s = name + 2;
4167 if (is_myname_or_ipaddr(s)) {
4168 return WERR_CAN_NOT_COMPLETE;
4171 return enum_all_printers_info_1(mem_ctx, PRINTER_ENUM_NAME, info, count);
4174 /********************************************************************
4175 * api_spoolss_enumprinters
4177 * called from api_spoolss_enumprinters (see this to understand)
4178 ********************************************************************/
4180 static WERROR enum_all_printers_info_2(TALLOC_CTX *mem_ctx,
4181 union spoolss_PrinterInfo **info,
4182 uint32_t *count)
4184 DEBUG(4,("enum_all_printers_info_2\n"));
4186 return enum_all_printers_info_level(mem_ctx, 2, 0, info, count);
4189 /********************************************************************
4190 * handle enumeration of printers at level 1
4191 ********************************************************************/
4193 static WERROR enumprinters_level1(TALLOC_CTX *mem_ctx,
4194 uint32_t flags,
4195 const char *name,
4196 union spoolss_PrinterInfo **info,
4197 uint32_t *count)
4199 /* Not all the flags are equals */
4201 if (flags & PRINTER_ENUM_LOCAL) {
4202 return enum_all_printers_info_1_local(mem_ctx, info, count);
4205 if (flags & PRINTER_ENUM_NAME) {
4206 return enum_all_printers_info_1_name(mem_ctx, name, info, count);
4209 if (flags & PRINTER_ENUM_NETWORK) {
4210 return enum_all_printers_info_1_network(mem_ctx, name, info, count);
4213 return WERR_OK; /* NT4sp5 does that */
4216 /********************************************************************
4217 * handle enumeration of printers at level 2
4218 ********************************************************************/
4220 static WERROR enumprinters_level2(TALLOC_CTX *mem_ctx,
4221 uint32_t flags,
4222 const char *servername,
4223 union spoolss_PrinterInfo **info,
4224 uint32_t *count)
4226 if (flags & PRINTER_ENUM_LOCAL) {
4227 return enum_all_printers_info_2(mem_ctx, info, count);
4230 if (flags & PRINTER_ENUM_NAME) {
4231 if (!is_myname_or_ipaddr(canon_servername(servername))) {
4232 return WERR_INVALID_NAME;
4235 return enum_all_printers_info_2(mem_ctx, info, count);
4238 if (flags & PRINTER_ENUM_REMOTE) {
4239 return WERR_UNKNOWN_LEVEL;
4242 return WERR_OK;
4245 /********************************************************************
4246 * handle enumeration of printers at level 4
4247 ********************************************************************/
4249 static WERROR enumprinters_level4(TALLOC_CTX *mem_ctx,
4250 uint32_t flags,
4251 const char *servername,
4252 union spoolss_PrinterInfo **info,
4253 uint32_t *count)
4255 DEBUG(4,("enum_all_printers_info_4\n"));
4257 return enum_all_printers_info_level(mem_ctx, 4, flags, info, count);
4261 /********************************************************************
4262 * handle enumeration of printers at level 5
4263 ********************************************************************/
4265 static WERROR enumprinters_level5(TALLOC_CTX *mem_ctx,
4266 uint32_t flags,
4267 const char *servername,
4268 union spoolss_PrinterInfo **info,
4269 uint32_t *count)
4271 DEBUG(4,("enum_all_printers_info_5\n"));
4273 return enum_all_printers_info_level(mem_ctx, 5, flags, info, count);
4276 /****************************************************************
4277 _spoolss_EnumPrinters
4278 ****************************************************************/
4280 WERROR _spoolss_EnumPrinters(pipes_struct *p,
4281 struct spoolss_EnumPrinters *r)
4283 const char *name = NULL;
4284 WERROR result;
4286 /* that's an [in out] buffer */
4288 if (!r->in.buffer && (r->in.offered != 0)) {
4289 return WERR_INVALID_PARAM;
4292 DEBUG(4,("_spoolss_EnumPrinters\n"));
4294 *r->out.needed = 0;
4295 *r->out.count = 0;
4296 *r->out.info = NULL;
4299 * Level 1:
4300 * flags==PRINTER_ENUM_NAME
4301 * if name=="" then enumerates all printers
4302 * if name!="" then enumerate the printer
4303 * flags==PRINTER_ENUM_REMOTE
4304 * name is NULL, enumerate printers
4305 * Level 2: name!="" enumerates printers, name can't be NULL
4306 * Level 3: doesn't exist
4307 * Level 4: does a local registry lookup
4308 * Level 5: same as Level 2
4311 if (r->in.server) {
4312 name = talloc_strdup_upper(p->mem_ctx, r->in.server);
4313 W_ERROR_HAVE_NO_MEMORY(name);
4316 switch (r->in.level) {
4317 case 0:
4318 result = enumprinters_level0(p->mem_ctx, r->in.flags, name,
4319 r->out.info, r->out.count);
4320 break;
4321 case 1:
4322 result = enumprinters_level1(p->mem_ctx, r->in.flags, name,
4323 r->out.info, r->out.count);
4324 break;
4325 case 2:
4326 result = enumprinters_level2(p->mem_ctx, r->in.flags, name,
4327 r->out.info, r->out.count);
4328 break;
4329 case 4:
4330 result = enumprinters_level4(p->mem_ctx, r->in.flags, name,
4331 r->out.info, r->out.count);
4332 break;
4333 case 5:
4334 result = enumprinters_level5(p->mem_ctx, r->in.flags, name,
4335 r->out.info, r->out.count);
4336 break;
4337 default:
4338 return WERR_UNKNOWN_LEVEL;
4341 if (!W_ERROR_IS_OK(result)) {
4342 return result;
4345 *r->out.needed = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
4346 spoolss_EnumPrinters, NULL,
4347 *r->out.info, r->in.level,
4348 *r->out.count);
4349 *r->out.info = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
4350 *r->out.count = SPOOLSS_BUFFER_OK(*r->out.count, 0);
4352 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
4355 /****************************************************************
4356 _spoolss_GetPrinter
4357 ****************************************************************/
4359 WERROR _spoolss_GetPrinter(pipes_struct *p,
4360 struct spoolss_GetPrinter *r)
4362 Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
4363 NT_PRINTER_INFO_LEVEL *ntprinter = NULL;
4364 WERROR result = WERR_OK;
4366 int snum;
4368 /* that's an [in out] buffer */
4370 if (!r->in.buffer && (r->in.offered != 0)) {
4371 return WERR_INVALID_PARAM;
4374 *r->out.needed = 0;
4376 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
4377 return WERR_BADFID;
4380 result = get_a_printer(Printer, &ntprinter, 2,
4381 lp_const_servicename(snum));
4382 if (!W_ERROR_IS_OK(result)) {
4383 return result;
4386 switch (r->in.level) {
4387 case 0:
4388 result = construct_printer_info0(p->mem_ctx, ntprinter,
4389 &r->out.info->info0, snum);
4390 break;
4391 case 1:
4392 result = construct_printer_info1(p->mem_ctx, ntprinter,
4393 PRINTER_ENUM_ICON8,
4394 &r->out.info->info1, snum);
4395 break;
4396 case 2:
4397 result = construct_printer_info2(p->mem_ctx, ntprinter,
4398 &r->out.info->info2, snum);
4399 break;
4400 case 3:
4401 result = construct_printer_info3(p->mem_ctx, ntprinter,
4402 &r->out.info->info3, snum);
4403 break;
4404 case 4:
4405 result = construct_printer_info4(p->mem_ctx, ntprinter,
4406 &r->out.info->info4, snum);
4407 break;
4408 case 5:
4409 result = construct_printer_info5(p->mem_ctx, ntprinter,
4410 &r->out.info->info5, snum);
4411 break;
4412 case 6:
4413 result = construct_printer_info6(p->mem_ctx, ntprinter,
4414 &r->out.info->info6, snum);
4415 break;
4416 case 7:
4417 result = construct_printer_info7(p->mem_ctx, Printer,
4418 &r->out.info->info7, snum);
4419 break;
4420 case 8:
4421 result = construct_printer_info8(p->mem_ctx, ntprinter,
4422 &r->out.info->info8, snum);
4423 break;
4424 default:
4425 result = WERR_UNKNOWN_LEVEL;
4426 break;
4429 free_a_printer(&ntprinter, 2);
4431 if (!W_ERROR_IS_OK(result)) {
4432 TALLOC_FREE(r->out.info);
4433 return result;
4436 *r->out.needed = SPOOLSS_BUFFER_UNION(spoolss_PrinterInfo, NULL,
4437 r->out.info, r->in.level);
4438 r->out.info = SPOOLSS_BUFFER_OK(r->out.info, NULL);
4440 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
4443 /********************************************************************
4444 ********************************************************************/
4446 static const char **string_array_from_driver_info(TALLOC_CTX *mem_ctx,
4447 const char **string_array,
4448 const char *cservername)
4450 int i, num_strings = 0;
4451 const char **array = NULL;
4453 if (!string_array) {
4454 return NULL;
4457 for (i=0; string_array[i] && string_array[i][0] != '\0'; i++) {
4459 const char *str = talloc_asprintf(mem_ctx, "\\\\%s%s",
4460 cservername, string_array[i]);
4461 if (!str) {
4462 TALLOC_FREE(array);
4463 return NULL;
4467 if (!add_string_to_array(mem_ctx, str, &array, &num_strings)) {
4468 TALLOC_FREE(array);
4469 return NULL;
4473 if (i > 0) {
4474 ADD_TO_ARRAY(mem_ctx, const char *, NULL,
4475 &array, &num_strings);
4478 return array;
4481 #define FILL_DRIVER_STRING(mem_ctx, in, out) \
4482 do { \
4483 if (in && strlen(in)) { \
4484 out = talloc_strdup(mem_ctx, in); \
4485 W_ERROR_HAVE_NO_MEMORY(out); \
4486 } else { \
4487 out = NULL; \
4489 } while (0);
4491 #define FILL_DRIVER_UNC_STRING(mem_ctx, server, in, out) \
4492 do { \
4493 if (in && strlen(in)) { \
4494 out = talloc_asprintf(mem_ctx, "\\\\%s%s", server, in); \
4495 } else { \
4496 out = talloc_strdup(mem_ctx, ""); \
4498 W_ERROR_HAVE_NO_MEMORY(out); \
4499 } while (0);
4501 /********************************************************************
4502 * fill a spoolss_DriverInfo1 struct
4503 ********************************************************************/
4505 static WERROR fill_printer_driver_info1(TALLOC_CTX *mem_ctx,
4506 struct spoolss_DriverInfo1 *r,
4507 const struct spoolss_DriverInfo8 *driver,
4508 const char *servername)
4510 r->driver_name = talloc_strdup(mem_ctx, driver->driver_name);
4511 W_ERROR_HAVE_NO_MEMORY(r->driver_name);
4513 return WERR_OK;
4516 /********************************************************************
4517 * fill a spoolss_DriverInfo2 struct
4518 ********************************************************************/
4520 static WERROR fill_printer_driver_info2(TALLOC_CTX *mem_ctx,
4521 struct spoolss_DriverInfo2 *r,
4522 const struct spoolss_DriverInfo8 *driver,
4523 const char *servername)
4526 const char *cservername = canon_servername(servername);
4528 r->version = driver->version;
4530 r->driver_name = talloc_strdup(mem_ctx, driver->driver_name);
4531 W_ERROR_HAVE_NO_MEMORY(r->driver_name);
4532 r->architecture = talloc_strdup(mem_ctx, driver->architecture);
4533 W_ERROR_HAVE_NO_MEMORY(r->architecture);
4535 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4536 driver->driver_path,
4537 r->driver_path);
4539 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4540 driver->data_file,
4541 r->data_file);
4543 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4544 driver->config_file,
4545 r->config_file);
4547 return WERR_OK;
4550 /********************************************************************
4551 * fill a spoolss_DriverInfo3 struct
4552 ********************************************************************/
4554 static WERROR fill_printer_driver_info3(TALLOC_CTX *mem_ctx,
4555 struct spoolss_DriverInfo3 *r,
4556 const struct spoolss_DriverInfo8 *driver,
4557 const char *servername)
4559 const char *cservername = canon_servername(servername);
4561 r->version = driver->version;
4563 r->driver_name = talloc_strdup(mem_ctx, driver->driver_name);
4564 W_ERROR_HAVE_NO_MEMORY(r->driver_name);
4565 r->architecture = talloc_strdup(mem_ctx, driver->architecture);
4566 W_ERROR_HAVE_NO_MEMORY(r->architecture);
4568 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4569 driver->driver_path,
4570 r->driver_path);
4572 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4573 driver->data_file,
4574 r->data_file);
4576 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4577 driver->config_file,
4578 r->config_file);
4580 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4581 driver->help_file,
4582 r->help_file);
4584 FILL_DRIVER_STRING(mem_ctx,
4585 driver->monitor_name,
4586 r->monitor_name);
4588 FILL_DRIVER_STRING(mem_ctx,
4589 driver->default_datatype,
4590 r->default_datatype);
4592 r->dependent_files = string_array_from_driver_info(mem_ctx,
4593 driver->dependent_files,
4594 cservername);
4595 return WERR_OK;
4598 /********************************************************************
4599 * fill a spoolss_DriverInfo4 struct
4600 ********************************************************************/
4602 static WERROR fill_printer_driver_info4(TALLOC_CTX *mem_ctx,
4603 struct spoolss_DriverInfo4 *r,
4604 const struct spoolss_DriverInfo8 *driver,
4605 const char *servername)
4607 const char *cservername = canon_servername(servername);
4609 r->version = driver->version;
4611 r->driver_name = talloc_strdup(mem_ctx, driver->driver_name);
4612 W_ERROR_HAVE_NO_MEMORY(r->driver_name);
4613 r->architecture = talloc_strdup(mem_ctx, driver->architecture);
4614 W_ERROR_HAVE_NO_MEMORY(r->architecture);
4616 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4617 driver->driver_path,
4618 r->driver_path);
4620 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4621 driver->data_file,
4622 r->data_file);
4624 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4625 driver->config_file,
4626 r->config_file);
4628 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4629 driver->help_file,
4630 r->help_file);
4632 r->dependent_files = string_array_from_driver_info(mem_ctx,
4633 driver->dependent_files,
4634 cservername);
4636 FILL_DRIVER_STRING(mem_ctx,
4637 driver->monitor_name,
4638 r->monitor_name);
4640 FILL_DRIVER_STRING(mem_ctx,
4641 driver->default_datatype,
4642 r->default_datatype);
4644 r->previous_names = string_array_from_driver_info(mem_ctx,
4645 driver->previous_names,
4646 cservername);
4648 return WERR_OK;
4651 /********************************************************************
4652 * fill a spoolss_DriverInfo5 struct
4653 ********************************************************************/
4655 static WERROR fill_printer_driver_info5(TALLOC_CTX *mem_ctx,
4656 struct spoolss_DriverInfo5 *r,
4657 const struct spoolss_DriverInfo8 *driver,
4658 const char *servername)
4660 const char *cservername = canon_servername(servername);
4662 r->version = driver->version;
4664 r->driver_name = talloc_strdup(mem_ctx, driver->driver_name);
4665 W_ERROR_HAVE_NO_MEMORY(r->driver_name);
4666 r->architecture = talloc_strdup(mem_ctx, driver->architecture);
4667 W_ERROR_HAVE_NO_MEMORY(r->architecture);
4669 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4670 driver->driver_path,
4671 r->driver_path);
4673 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4674 driver->data_file,
4675 r->data_file);
4677 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4678 driver->config_file,
4679 r->config_file);
4681 r->driver_attributes = 0;
4682 r->config_version = 0;
4683 r->driver_version = 0;
4685 return WERR_OK;
4687 /********************************************************************
4688 * fill a spoolss_DriverInfo6 struct
4689 ********************************************************************/
4691 static WERROR fill_printer_driver_info6(TALLOC_CTX *mem_ctx,
4692 struct spoolss_DriverInfo6 *r,
4693 const struct spoolss_DriverInfo8 *driver,
4694 const char *servername)
4696 const char *cservername = canon_servername(servername);
4698 r->version = driver->version;
4700 r->driver_name = talloc_strdup(mem_ctx, driver->driver_name);
4701 W_ERROR_HAVE_NO_MEMORY(r->driver_name);
4702 r->architecture = talloc_strdup(mem_ctx, driver->architecture);
4703 W_ERROR_HAVE_NO_MEMORY(r->architecture);
4705 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4706 driver->driver_path,
4707 r->driver_path);
4709 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4710 driver->data_file,
4711 r->data_file);
4713 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4714 driver->config_file,
4715 r->config_file);
4717 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4718 driver->help_file,
4719 r->help_file);
4721 FILL_DRIVER_STRING(mem_ctx,
4722 driver->monitor_name,
4723 r->monitor_name);
4725 FILL_DRIVER_STRING(mem_ctx,
4726 driver->default_datatype,
4727 r->default_datatype);
4729 r->dependent_files = string_array_from_driver_info(mem_ctx,
4730 driver->dependent_files,
4731 cservername);
4732 r->previous_names = string_array_from_driver_info(mem_ctx,
4733 driver->previous_names,
4734 cservername);
4736 r->driver_date = driver->driver_date;
4737 r->driver_version = driver->driver_version;
4739 FILL_DRIVER_STRING(mem_ctx,
4740 driver->manufacturer_name,
4741 r->manufacturer_name);
4742 FILL_DRIVER_STRING(mem_ctx,
4743 driver->manufacturer_url,
4744 r->manufacturer_url);
4745 FILL_DRIVER_STRING(mem_ctx,
4746 driver->hardware_id,
4747 r->hardware_id);
4748 FILL_DRIVER_STRING(mem_ctx,
4749 driver->provider,
4750 r->provider);
4752 return WERR_OK;
4755 /********************************************************************
4756 * fill a spoolss_DriverInfo8 struct
4757 ********************************************************************/
4759 static WERROR fill_printer_driver_info8(TALLOC_CTX *mem_ctx,
4760 struct spoolss_DriverInfo8 *r,
4761 const struct spoolss_DriverInfo8 *driver,
4762 const char *servername)
4764 const char *cservername = canon_servername(servername);
4766 r->version = driver->version;
4768 r->driver_name = talloc_strdup(mem_ctx, driver->driver_name);
4769 W_ERROR_HAVE_NO_MEMORY(r->driver_name);
4770 r->architecture = talloc_strdup(mem_ctx, driver->architecture);
4771 W_ERROR_HAVE_NO_MEMORY(r->architecture);
4773 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4774 driver->driver_path,
4775 r->driver_path);
4777 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4778 driver->data_file,
4779 r->data_file);
4781 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4782 driver->config_file,
4783 r->config_file);
4785 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4786 driver->help_file,
4787 r->help_file);
4789 FILL_DRIVER_STRING(mem_ctx,
4790 driver->monitor_name,
4791 r->monitor_name);
4793 FILL_DRIVER_STRING(mem_ctx,
4794 driver->default_datatype,
4795 r->default_datatype);
4797 r->dependent_files = string_array_from_driver_info(mem_ctx,
4798 driver->dependent_files,
4799 cservername);
4800 r->previous_names = string_array_from_driver_info(mem_ctx,
4801 driver->previous_names,
4802 cservername);
4804 r->driver_date = driver->driver_date;
4805 r->driver_version = driver->driver_version;
4807 FILL_DRIVER_STRING(mem_ctx,
4808 driver->manufacturer_name,
4809 r->manufacturer_name);
4810 FILL_DRIVER_STRING(mem_ctx,
4811 driver->manufacturer_url,
4812 r->manufacturer_url);
4813 FILL_DRIVER_STRING(mem_ctx,
4814 driver->hardware_id,
4815 r->hardware_id);
4816 FILL_DRIVER_STRING(mem_ctx,
4817 driver->provider,
4818 r->provider);
4820 FILL_DRIVER_STRING(mem_ctx,
4821 driver->print_processor,
4822 r->print_processor);
4823 FILL_DRIVER_STRING(mem_ctx,
4824 driver->vendor_setup,
4825 r->vendor_setup);
4827 r->color_profiles = string_array_from_driver_info(mem_ctx,
4828 driver->color_profiles,
4829 cservername);
4831 FILL_DRIVER_STRING(mem_ctx,
4832 driver->inf_path,
4833 r->inf_path);
4835 r->printer_driver_attributes = driver->printer_driver_attributes;
4837 r->core_driver_dependencies = string_array_from_driver_info(mem_ctx,
4838 driver->core_driver_dependencies,
4839 cservername);
4841 r->min_inbox_driver_ver_date = driver->min_inbox_driver_ver_date;
4842 r->min_inbox_driver_ver_version = driver->min_inbox_driver_ver_version;
4844 return WERR_OK;
4847 #if 0 /* disabled until marshalling issues are resolved - gd */
4848 /********************************************************************
4849 ********************************************************************/
4851 static WERROR fill_spoolss_DriverFileInfo(TALLOC_CTX *mem_ctx,
4852 struct spoolss_DriverFileInfo *r,
4853 const char *cservername,
4854 const char *file_name,
4855 enum spoolss_DriverFileType file_type,
4856 uint32_t file_version)
4858 r->file_name = talloc_asprintf(mem_ctx, "\\\\%s%s",
4859 cservername, file_name);
4860 W_ERROR_HAVE_NO_MEMORY(r->file_name);
4861 r->file_type = file_type;
4862 r->file_version = file_version;
4864 return WERR_OK;
4867 /********************************************************************
4868 ********************************************************************/
4870 static WERROR spoolss_DriverFileInfo_from_driver(TALLOC_CTX *mem_ctx,
4871 const struct spoolss_DriverInfo8 *driver,
4872 const char *cservername,
4873 struct spoolss_DriverFileInfo **info_p,
4874 uint32_t *count_p)
4876 struct spoolss_DriverFileInfo *info = NULL;
4877 uint32_t count = 0;
4878 WERROR result;
4879 uint32_t i;
4881 *info_p = NULL;
4882 *count_p = 0;
4884 if (strlen(driver->driver_path)) {
4885 info = TALLOC_REALLOC_ARRAY(mem_ctx, info,
4886 struct spoolss_DriverFileInfo,
4887 count + 1);
4888 W_ERROR_HAVE_NO_MEMORY(info);
4889 result = fill_spoolss_DriverFileInfo(info,
4890 &info[count],
4891 cservername,
4892 driver->driver_path,
4893 SPOOLSS_DRIVER_FILE_TYPE_RENDERING,
4895 W_ERROR_NOT_OK_RETURN(result);
4896 count++;
4899 if (strlen(driver->config_file)) {
4900 info = TALLOC_REALLOC_ARRAY(mem_ctx, info,
4901 struct spoolss_DriverFileInfo,
4902 count + 1);
4903 W_ERROR_HAVE_NO_MEMORY(info);
4904 result = fill_spoolss_DriverFileInfo(info,
4905 &info[count],
4906 cservername,
4907 driver->config_file,
4908 SPOOLSS_DRIVER_FILE_TYPE_CONFIGURATION,
4910 W_ERROR_NOT_OK_RETURN(result);
4911 count++;
4914 if (strlen(driver->data_file)) {
4915 info = TALLOC_REALLOC_ARRAY(mem_ctx, info,
4916 struct spoolss_DriverFileInfo,
4917 count + 1);
4918 W_ERROR_HAVE_NO_MEMORY(info);
4919 result = fill_spoolss_DriverFileInfo(info,
4920 &info[count],
4921 cservername,
4922 driver->data_file,
4923 SPOOLSS_DRIVER_FILE_TYPE_DATA,
4925 W_ERROR_NOT_OK_RETURN(result);
4926 count++;
4929 if (strlen(driver->help_file)) {
4930 info = TALLOC_REALLOC_ARRAY(mem_ctx, info,
4931 struct spoolss_DriverFileInfo,
4932 count + 1);
4933 W_ERROR_HAVE_NO_MEMORY(info);
4934 result = fill_spoolss_DriverFileInfo(info,
4935 &info[count],
4936 cservername,
4937 driver->help_file,
4938 SPOOLSS_DRIVER_FILE_TYPE_HELP,
4940 W_ERROR_NOT_OK_RETURN(result);
4941 count++;
4944 for (i=0; driver->dependent_files[i] && driver->dependent_files[i][0] != '\0'; i++) {
4945 info = TALLOC_REALLOC_ARRAY(mem_ctx, info,
4946 struct spoolss_DriverFileInfo,
4947 count + 1);
4948 W_ERROR_HAVE_NO_MEMORY(info);
4949 result = fill_spoolss_DriverFileInfo(info,
4950 &info[count],
4951 cservername,
4952 driver->dependent_files[i],
4953 SPOOLSS_DRIVER_FILE_TYPE_OTHER,
4955 W_ERROR_NOT_OK_RETURN(result);
4956 count++;
4959 *info_p = info;
4960 *count_p = count;
4962 return WERR_OK;
4965 /********************************************************************
4966 * fill a spoolss_DriverInfo101 struct
4967 ********************************************************************/
4969 static WERROR fill_printer_driver_info101(TALLOC_CTX *mem_ctx,
4970 struct spoolss_DriverInfo101 *r,
4971 const struct spoolss_DriverInfo8 *driver,
4972 const char *servername)
4974 const char *cservername = canon_servername(servername);
4975 WERROR result;
4977 r->version = driver->version;
4979 r->driver_name = talloc_strdup(mem_ctx, driver->driver_name);
4980 W_ERROR_HAVE_NO_MEMORY(r->driver_name);
4981 r->architecture = talloc_strdup(mem_ctx, driver->architecture);
4982 W_ERROR_HAVE_NO_MEMORY(r->architecture);
4984 result = spoolss_DriverFileInfo_from_driver(mem_ctx, driver,
4985 cservername,
4986 &r->file_info,
4987 &r->file_count);
4988 if (!W_ERROR_IS_OK(result)) {
4989 return result;
4992 FILL_DRIVER_STRING(mem_ctx,
4993 driver->monitor_name,
4994 r->monitor_name);
4996 FILL_DRIVER_STRING(mem_ctx,
4997 driver->default_datatype,
4998 r->default_datatype);
5000 r->previous_names = string_array_from_driver_info(mem_ctx,
5001 driver->previous_names,
5002 cservername);
5003 r->driver_date = driver->driver_date;
5004 r->driver_version = driver->driver_version;
5006 FILL_DRIVER_STRING(mem_ctx,
5007 driver->manufacturer_name,
5008 r->manufacturer_name);
5009 FILL_DRIVER_STRING(mem_ctx,
5010 driver->manufacturer_url,
5011 r->manufacturer_url);
5012 FILL_DRIVER_STRING(mem_ctx,
5013 driver->hardware_id,
5014 r->hardware_id);
5015 FILL_DRIVER_STRING(mem_ctx,
5016 driver->provider,
5017 r->provider);
5019 return WERR_OK;
5021 #endif
5022 /********************************************************************
5023 ********************************************************************/
5025 static WERROR construct_printer_driver_info_level(TALLOC_CTX *mem_ctx,
5026 uint32_t level,
5027 union spoolss_DriverInfo *r,
5028 int snum,
5029 const char *servername,
5030 const char *architecture,
5031 uint32_t version)
5033 NT_PRINTER_INFO_LEVEL *printer = NULL;
5034 struct spoolss_DriverInfo8 *driver;
5035 WERROR result;
5037 result = get_a_printer(NULL, &printer, 2, lp_const_servicename(snum));
5039 DEBUG(8,("construct_printer_driver_info_level: status: %s\n",
5040 win_errstr(result)));
5042 if (!W_ERROR_IS_OK(result)) {
5043 return WERR_INVALID_PRINTER_NAME;
5046 result = get_a_printer_driver(mem_ctx, &driver, printer->info_2->drivername,
5047 architecture, version);
5049 DEBUG(8,("construct_printer_driver_info_level: status: %s\n",
5050 win_errstr(result)));
5052 if (!W_ERROR_IS_OK(result)) {
5054 * Is this a W2k client ?
5057 if (version < 3) {
5058 free_a_printer(&printer, 2);
5059 return WERR_UNKNOWN_PRINTER_DRIVER;
5062 /* Yes - try again with a WinNT driver. */
5063 version = 2;
5064 result = get_a_printer_driver(mem_ctx, &driver, printer->info_2->drivername,
5065 architecture, version);
5066 DEBUG(8,("construct_printer_driver_level: status: %s\n",
5067 win_errstr(result)));
5068 if (!W_ERROR_IS_OK(result)) {
5069 free_a_printer(&printer, 2);
5070 return WERR_UNKNOWN_PRINTER_DRIVER;
5074 switch (level) {
5075 case 1:
5076 result = fill_printer_driver_info1(mem_ctx, &r->info1, driver, servername);
5077 break;
5078 case 2:
5079 result = fill_printer_driver_info2(mem_ctx, &r->info2, driver, servername);
5080 break;
5081 case 3:
5082 result = fill_printer_driver_info3(mem_ctx, &r->info3, driver, servername);
5083 break;
5084 case 4:
5085 result = fill_printer_driver_info4(mem_ctx, &r->info4, driver, servername);
5086 break;
5087 case 5:
5088 result = fill_printer_driver_info5(mem_ctx, &r->info5, driver, servername);
5089 break;
5090 case 6:
5091 result = fill_printer_driver_info6(mem_ctx, &r->info6, driver, servername);
5092 break;
5093 case 8:
5094 result = fill_printer_driver_info8(mem_ctx, &r->info8, driver, servername);
5095 break;
5096 #if 0 /* disabled until marshalling issues are resolved - gd */
5097 case 101:
5098 result = fill_printer_driver_info101(mem_ctx, &r->info101, driver, servername);
5099 break;
5100 #endif
5101 default:
5102 result = WERR_UNKNOWN_LEVEL;
5103 break;
5106 free_a_printer(&printer, 2);
5107 free_a_printer_driver(driver);
5109 return result;
5112 /****************************************************************
5113 _spoolss_GetPrinterDriver2
5114 ****************************************************************/
5116 WERROR _spoolss_GetPrinterDriver2(pipes_struct *p,
5117 struct spoolss_GetPrinterDriver2 *r)
5119 Printer_entry *printer;
5120 WERROR result;
5122 const char *servername;
5123 int snum;
5125 /* that's an [in out] buffer */
5127 if (!r->in.buffer && (r->in.offered != 0)) {
5128 return WERR_INVALID_PARAM;
5131 DEBUG(4,("_spoolss_GetPrinterDriver2\n"));
5133 if (!(printer = find_printer_index_by_hnd(p, r->in.handle))) {
5134 DEBUG(0,("_spoolss_GetPrinterDriver2: invalid printer handle!\n"));
5135 return WERR_INVALID_PRINTER_NAME;
5138 *r->out.needed = 0;
5139 *r->out.server_major_version = 0;
5140 *r->out.server_minor_version = 0;
5142 servername = get_server_name(printer);
5144 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
5145 return WERR_BADFID;
5148 result = construct_printer_driver_info_level(p->mem_ctx, r->in.level,
5149 r->out.info, snum,
5150 servername,
5151 r->in.architecture,
5152 r->in.client_major_version);
5153 if (!W_ERROR_IS_OK(result)) {
5154 TALLOC_FREE(r->out.info);
5155 return result;
5158 *r->out.needed = SPOOLSS_BUFFER_UNION(spoolss_DriverInfo, NULL,
5159 r->out.info, r->in.level);
5160 r->out.info = SPOOLSS_BUFFER_OK(r->out.info, NULL);
5162 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
5166 /****************************************************************
5167 _spoolss_StartPagePrinter
5168 ****************************************************************/
5170 WERROR _spoolss_StartPagePrinter(pipes_struct *p,
5171 struct spoolss_StartPagePrinter *r)
5173 Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
5175 if (!Printer) {
5176 DEBUG(3,("_spoolss_StartPagePrinter: "
5177 "Error in startpageprinter printer handle\n"));
5178 return WERR_BADFID;
5181 Printer->page_started = true;
5182 return WERR_OK;
5185 /****************************************************************
5186 _spoolss_EndPagePrinter
5187 ****************************************************************/
5189 WERROR _spoolss_EndPagePrinter(pipes_struct *p,
5190 struct spoolss_EndPagePrinter *r)
5192 int snum;
5194 Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
5196 if (!Printer) {
5197 DEBUG(2,("_spoolss_EndPagePrinter: Invalid handle (%s:%u:%u).\n",
5198 OUR_HANDLE(r->in.handle)));
5199 return WERR_BADFID;
5202 if (!get_printer_snum(p, r->in.handle, &snum, NULL))
5203 return WERR_BADFID;
5205 Printer->page_started = false;
5206 print_job_endpage(snum, Printer->jobid);
5208 return WERR_OK;
5211 /****************************************************************
5212 _spoolss_StartDocPrinter
5213 ****************************************************************/
5215 WERROR _spoolss_StartDocPrinter(pipes_struct *p,
5216 struct spoolss_StartDocPrinter *r)
5218 struct spoolss_DocumentInfo1 *info_1;
5219 int snum;
5220 Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
5222 if (!Printer) {
5223 DEBUG(2,("_spoolss_StartDocPrinter: "
5224 "Invalid handle (%s:%u:%u)\n",
5225 OUR_HANDLE(r->in.handle)));
5226 return WERR_BADFID;
5229 if (r->in.level != 1) {
5230 return WERR_UNKNOWN_LEVEL;
5233 info_1 = r->in.info.info1;
5236 * a nice thing with NT is it doesn't listen to what you tell it.
5237 * when asked to send _only_ RAW datas, it tries to send datas
5238 * in EMF format.
5240 * So I add checks like in NT Server ...
5243 if (info_1->datatype) {
5244 if (strcmp(info_1->datatype, "RAW") != 0) {
5245 *r->out.job_id = 0;
5246 return WERR_INVALID_DATATYPE;
5250 /* get the share number of the printer */
5251 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
5252 return WERR_BADFID;
5255 Printer->jobid = print_job_start(p->server_info, snum,
5256 info_1->document_name,
5257 Printer->nt_devmode);
5259 /* An error occured in print_job_start() so return an appropriate
5260 NT error code. */
5262 if (Printer->jobid == -1) {
5263 return map_werror_from_unix(errno);
5266 Printer->document_started = true;
5267 *r->out.job_id = Printer->jobid;
5269 return WERR_OK;
5272 /****************************************************************
5273 _spoolss_EndDocPrinter
5274 ****************************************************************/
5276 WERROR _spoolss_EndDocPrinter(pipes_struct *p,
5277 struct spoolss_EndDocPrinter *r)
5279 Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
5280 int snum;
5282 if (!Printer) {
5283 DEBUG(2,("_spoolss_EndDocPrinter: Invalid handle (%s:%u:%u)\n",
5284 OUR_HANDLE(r->in.handle)));
5285 return WERR_BADFID;
5288 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
5289 return WERR_BADFID;
5292 Printer->document_started = false;
5293 print_job_end(snum, Printer->jobid, NORMAL_CLOSE);
5294 /* error codes unhandled so far ... */
5296 return WERR_OK;
5299 /****************************************************************
5300 _spoolss_WritePrinter
5301 ****************************************************************/
5303 WERROR _spoolss_WritePrinter(pipes_struct *p,
5304 struct spoolss_WritePrinter *r)
5306 uint32_t buffer_written;
5307 int snum;
5308 Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
5310 if (!Printer) {
5311 DEBUG(2,("_spoolss_WritePrinter: Invalid handle (%s:%u:%u)\n",
5312 OUR_HANDLE(r->in.handle)));
5313 *r->out.num_written = r->in._data_size;
5314 return WERR_BADFID;
5317 if (!get_printer_snum(p, r->in.handle, &snum, NULL))
5318 return WERR_BADFID;
5320 buffer_written = (uint32_t)print_job_write(snum, Printer->jobid,
5321 (const char *)r->in.data.data,
5322 (SMB_OFF_T)-1,
5323 (size_t)r->in._data_size);
5324 if (buffer_written == (uint32_t)-1) {
5325 *r->out.num_written = 0;
5326 if (errno == ENOSPC)
5327 return WERR_NO_SPOOL_SPACE;
5328 else
5329 return WERR_ACCESS_DENIED;
5332 *r->out.num_written = r->in._data_size;
5334 return WERR_OK;
5337 /********************************************************************
5338 * api_spoolss_getprinter
5339 * called from the spoolss dispatcher
5341 ********************************************************************/
5343 static WERROR control_printer(struct policy_handle *handle, uint32_t command,
5344 pipes_struct *p)
5346 int snum;
5347 WERROR errcode = WERR_BADFUNC;
5348 Printer_entry *Printer = find_printer_index_by_hnd(p, handle);
5350 if (!Printer) {
5351 DEBUG(2,("control_printer: Invalid handle (%s:%u:%u)\n",
5352 OUR_HANDLE(handle)));
5353 return WERR_BADFID;
5356 if (!get_printer_snum(p, handle, &snum, NULL))
5357 return WERR_BADFID;
5359 switch (command) {
5360 case SPOOLSS_PRINTER_CONTROL_PAUSE:
5361 errcode = print_queue_pause(p->server_info, snum);
5362 break;
5363 case SPOOLSS_PRINTER_CONTROL_RESUME:
5364 case SPOOLSS_PRINTER_CONTROL_UNPAUSE:
5365 errcode = print_queue_resume(p->server_info, snum);
5366 break;
5367 case SPOOLSS_PRINTER_CONTROL_PURGE:
5368 errcode = print_queue_purge(p->server_info, snum);
5369 break;
5370 default:
5371 return WERR_UNKNOWN_LEVEL;
5374 return errcode;
5378 /****************************************************************
5379 _spoolss_AbortPrinter
5380 * From MSDN: "Deletes printer's spool file if printer is configured
5381 * for spooling"
5382 ****************************************************************/
5384 WERROR _spoolss_AbortPrinter(pipes_struct *p,
5385 struct spoolss_AbortPrinter *r)
5387 Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
5388 int snum;
5389 WERROR errcode = WERR_OK;
5391 if (!Printer) {
5392 DEBUG(2,("_spoolss_AbortPrinter: Invalid handle (%s:%u:%u)\n",
5393 OUR_HANDLE(r->in.handle)));
5394 return WERR_BADFID;
5397 if (!get_printer_snum(p, r->in.handle, &snum, NULL))
5398 return WERR_BADFID;
5400 print_job_delete(p->server_info, snum, Printer->jobid, &errcode );
5402 return errcode;
5405 /********************************************************************
5406 * called by spoolss_api_setprinter
5407 * when updating a printer description
5408 ********************************************************************/
5410 static WERROR update_printer_sec(struct policy_handle *handle,
5411 pipes_struct *p, SEC_DESC_BUF *secdesc_ctr)
5413 SEC_DESC_BUF *new_secdesc_ctr = NULL, *old_secdesc_ctr = NULL;
5414 WERROR result;
5415 int snum;
5417 Printer_entry *Printer = find_printer_index_by_hnd(p, handle);
5419 if (!Printer || !get_printer_snum(p, handle, &snum, NULL)) {
5420 DEBUG(2,("update_printer_sec: Invalid handle (%s:%u:%u)\n",
5421 OUR_HANDLE(handle)));
5423 result = WERR_BADFID;
5424 goto done;
5427 if (!secdesc_ctr) {
5428 DEBUG(10,("update_printer_sec: secdesc_ctr is NULL !\n"));
5429 result = WERR_INVALID_PARAM;
5430 goto done;
5433 /* Check the user has permissions to change the security
5434 descriptor. By experimentation with two NT machines, the user
5435 requires Full Access to the printer to change security
5436 information. */
5438 if ( Printer->access_granted != PRINTER_ACCESS_ADMINISTER ) {
5439 DEBUG(4,("update_printer_sec: updated denied by printer permissions\n"));
5440 result = WERR_ACCESS_DENIED;
5441 goto done;
5444 /* NT seems to like setting the security descriptor even though
5445 nothing may have actually changed. */
5447 if ( !nt_printing_getsec(p->mem_ctx, Printer->sharename, &old_secdesc_ctr)) {
5448 DEBUG(2,("update_printer_sec: nt_printing_getsec() failed\n"));
5449 result = WERR_BADFID;
5450 goto done;
5453 if (DEBUGLEVEL >= 10) {
5454 SEC_ACL *the_acl;
5455 int i;
5457 the_acl = old_secdesc_ctr->sd->dacl;
5458 DEBUG(10, ("old_secdesc_ctr for %s has %d aces:\n",
5459 PRINTERNAME(snum), the_acl->num_aces));
5461 for (i = 0; i < the_acl->num_aces; i++) {
5462 DEBUG(10, ("%s 0x%08x\n", sid_string_dbg(
5463 &the_acl->aces[i].trustee),
5464 the_acl->aces[i].access_mask));
5467 the_acl = secdesc_ctr->sd->dacl;
5469 if (the_acl) {
5470 DEBUG(10, ("secdesc_ctr for %s has %d aces:\n",
5471 PRINTERNAME(snum), the_acl->num_aces));
5473 for (i = 0; i < the_acl->num_aces; i++) {
5474 DEBUG(10, ("%s 0x%08x\n", sid_string_dbg(
5475 &the_acl->aces[i].trustee),
5476 the_acl->aces[i].access_mask));
5478 } else {
5479 DEBUG(10, ("dacl for secdesc_ctr is NULL\n"));
5483 new_secdesc_ctr = sec_desc_merge(p->mem_ctx, secdesc_ctr, old_secdesc_ctr);
5484 if (!new_secdesc_ctr) {
5485 result = WERR_NOMEM;
5486 goto done;
5489 if (security_descriptor_equal(new_secdesc_ctr->sd, old_secdesc_ctr->sd)) {
5490 result = WERR_OK;
5491 goto done;
5494 result = nt_printing_setsec(Printer->sharename, new_secdesc_ctr);
5496 done:
5498 return result;
5501 /********************************************************************
5502 Canonicalize printer info from a client
5504 ATTN: It does not matter what we set the servername to hear
5505 since we do the necessary work in get_a_printer() to set it to
5506 the correct value based on what the client sent in the
5507 _spoolss_open_printer_ex().
5508 ********************************************************************/
5510 static bool check_printer_ok(NT_PRINTER_INFO_LEVEL_2 *info, int snum)
5512 fstring printername;
5513 const char *p;
5515 DEBUG(5,("check_printer_ok: servername=%s printername=%s sharename=%s "
5516 "portname=%s drivername=%s comment=%s location=%s\n",
5517 info->servername, info->printername, info->sharename,
5518 info->portname, info->drivername, info->comment, info->location));
5520 /* we force some elements to "correct" values */
5521 slprintf(info->servername, sizeof(info->servername)-1, "\\\\%s", global_myname());
5522 fstrcpy(info->sharename, lp_servicename(snum));
5524 /* check to see if we allow printername != sharename */
5526 if ( lp_force_printername(snum) ) {
5527 slprintf(info->printername, sizeof(info->printername)-1, "\\\\%s\\%s",
5528 global_myname(), info->sharename );
5529 } else {
5531 /* make sure printername is in \\server\printername format */
5533 fstrcpy( printername, info->printername );
5534 p = printername;
5535 if ( printername[0] == '\\' && printername[1] == '\\' ) {
5536 if ( (p = strchr_m( &printername[2], '\\' )) != NULL )
5537 p++;
5540 slprintf(info->printername, sizeof(info->printername)-1, "\\\\%s\\%s",
5541 global_myname(), p );
5544 info->attributes |= PRINTER_ATTRIBUTE_SAMBA;
5545 info->attributes &= ~PRINTER_ATTRIBUTE_NOT_SAMBA;
5549 return true;
5552 /****************************************************************************
5553 ****************************************************************************/
5555 static WERROR add_port_hook(TALLOC_CTX *ctx, NT_USER_TOKEN *token, const char *portname, const char *uri)
5557 char *cmd = lp_addport_cmd();
5558 char *command = NULL;
5559 int ret;
5560 SE_PRIV se_printop = SE_PRINT_OPERATOR;
5561 bool is_print_op = false;
5563 if ( !*cmd ) {
5564 return WERR_ACCESS_DENIED;
5567 command = talloc_asprintf(ctx,
5568 "%s \"%s\" \"%s\"", cmd, portname, uri );
5569 if (!command) {
5570 return WERR_NOMEM;
5573 if ( token )
5574 is_print_op = user_has_privileges( token, &se_printop );
5576 DEBUG(10,("Running [%s]\n", command));
5578 /********* BEGIN SePrintOperatorPrivilege **********/
5580 if ( is_print_op )
5581 become_root();
5583 ret = smbrun(command, NULL);
5585 if ( is_print_op )
5586 unbecome_root();
5588 /********* END SePrintOperatorPrivilege **********/
5590 DEBUGADD(10,("returned [%d]\n", ret));
5592 TALLOC_FREE(command);
5594 if ( ret != 0 ) {
5595 return WERR_ACCESS_DENIED;
5598 return WERR_OK;
5601 /****************************************************************************
5602 ****************************************************************************/
5604 bool add_printer_hook(TALLOC_CTX *ctx, NT_USER_TOKEN *token, NT_PRINTER_INFO_LEVEL *printer)
5606 char *cmd = lp_addprinter_cmd();
5607 char **qlines;
5608 char *command = NULL;
5609 int numlines;
5610 int ret;
5611 int fd;
5612 SE_PRIV se_printop = SE_PRINT_OPERATOR;
5613 bool is_print_op = false;
5614 char *remote_machine = talloc_strdup(ctx, "%m");
5616 if (!remote_machine) {
5617 return false;
5619 remote_machine = talloc_sub_basic(ctx,
5620 current_user_info.smb_name,
5621 current_user_info.domain,
5622 remote_machine);
5623 if (!remote_machine) {
5624 return false;
5627 command = talloc_asprintf(ctx,
5628 "%s \"%s\" \"%s\" \"%s\" \"%s\" \"%s\" \"%s\" \"%s\"",
5629 cmd, printer->info_2->printername, printer->info_2->sharename,
5630 printer->info_2->portname, printer->info_2->drivername,
5631 printer->info_2->location, printer->info_2->comment, remote_machine);
5632 if (!command) {
5633 return false;
5636 if ( token )
5637 is_print_op = user_has_privileges( token, &se_printop );
5639 DEBUG(10,("Running [%s]\n", command));
5641 /********* BEGIN SePrintOperatorPrivilege **********/
5643 if ( is_print_op )
5644 become_root();
5646 if ( (ret = smbrun(command, &fd)) == 0 ) {
5647 /* Tell everyone we updated smb.conf. */
5648 message_send_all(smbd_messaging_context(),
5649 MSG_SMB_CONF_UPDATED, NULL, 0, NULL);
5652 if ( is_print_op )
5653 unbecome_root();
5655 /********* END SePrintOperatorPrivilege **********/
5657 DEBUGADD(10,("returned [%d]\n", ret));
5659 TALLOC_FREE(command);
5660 TALLOC_FREE(remote_machine);
5662 if ( ret != 0 ) {
5663 if (fd != -1)
5664 close(fd);
5665 return false;
5668 /* reload our services immediately */
5669 become_root();
5670 reload_services(false);
5671 unbecome_root();
5673 numlines = 0;
5674 /* Get lines and convert them back to dos-codepage */
5675 qlines = fd_lines_load(fd, &numlines, 0, NULL);
5676 DEBUGADD(10,("Lines returned = [%d]\n", numlines));
5677 close(fd);
5679 /* Set the portname to what the script says the portname should be. */
5680 /* but don't require anything to be return from the script exit a good error code */
5682 if (numlines) {
5683 /* Set the portname to what the script says the portname should be. */
5684 strncpy(printer->info_2->portname, qlines[0], sizeof(printer->info_2->portname));
5685 DEBUGADD(6,("Line[0] = [%s]\n", qlines[0]));
5688 TALLOC_FREE(qlines);
5689 return true;
5693 /********************************************************************
5694 * Called by spoolss_api_setprinter
5695 * when updating a printer description.
5696 ********************************************************************/
5698 static WERROR update_printer(pipes_struct *p, struct policy_handle *handle,
5699 struct spoolss_SetPrinterInfoCtr *info_ctr,
5700 struct spoolss_DeviceMode *devmode)
5702 int snum;
5703 NT_PRINTER_INFO_LEVEL *printer = NULL, *old_printer = NULL;
5704 Printer_entry *Printer = find_printer_index_by_hnd(p, handle);
5705 WERROR result;
5706 DATA_BLOB buffer;
5707 fstring asc_buffer;
5709 DEBUG(8,("update_printer\n"));
5711 result = WERR_OK;
5713 if (!Printer) {
5714 result = WERR_BADFID;
5715 goto done;
5718 if (!get_printer_snum(p, handle, &snum, NULL)) {
5719 result = WERR_BADFID;
5720 goto done;
5723 if (!W_ERROR_IS_OK(get_a_printer(Printer, &printer, 2, lp_const_servicename(snum))) ||
5724 (!W_ERROR_IS_OK(get_a_printer(Printer, &old_printer, 2, lp_const_servicename(snum))))) {
5725 result = WERR_BADFID;
5726 goto done;
5729 DEBUGADD(8,("Converting info_2 struct\n"));
5732 * convert_printer_info converts the incoming
5733 * info from the client and overwrites the info
5734 * just read from the tdb in the pointer 'printer'.
5737 if (!convert_printer_info(info_ctr, printer)) {
5738 result = WERR_NOMEM;
5739 goto done;
5742 if (devmode) {
5743 /* we have a valid devmode
5744 convert it and link it*/
5746 DEBUGADD(8,("update_printer: Converting the devicemode struct\n"));
5747 if (!convert_devicemode(printer->info_2->printername, devmode,
5748 &printer->info_2->devmode)) {
5749 result = WERR_NOMEM;
5750 goto done;
5754 /* Do sanity check on the requested changes for Samba */
5756 if (!check_printer_ok(printer->info_2, snum)) {
5757 result = WERR_INVALID_PARAM;
5758 goto done;
5761 /* FIXME!!! If the driver has changed we really should verify that
5762 it is installed before doing much else --jerry */
5764 /* Check calling user has permission to update printer description */
5766 if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
5767 DEBUG(3, ("update_printer: printer property change denied by handle\n"));
5768 result = WERR_ACCESS_DENIED;
5769 goto done;
5772 /* Call addprinter hook */
5773 /* Check changes to see if this is really needed */
5775 if ( *lp_addprinter_cmd()
5776 && (!strequal(printer->info_2->drivername, old_printer->info_2->drivername)
5777 || !strequal(printer->info_2->comment, old_printer->info_2->comment)
5778 || !strequal(printer->info_2->portname, old_printer->info_2->portname)
5779 || !strequal(printer->info_2->location, old_printer->info_2->location)) )
5781 /* add_printer_hook() will call reload_services() */
5783 if ( !add_printer_hook(p->mem_ctx, p->server_info->ptok,
5784 printer) ) {
5785 result = WERR_ACCESS_DENIED;
5786 goto done;
5791 * When a *new* driver is bound to a printer, the drivername is used to
5792 * lookup previously saved driver initialization info, which is then
5793 * bound to the printer, simulating what happens in the Windows arch.
5795 if (!strequal(printer->info_2->drivername, old_printer->info_2->drivername))
5797 if (!set_driver_init(printer, 2))
5799 DEBUG(5,("update_printer: Error restoring driver initialization data for driver [%s]!\n",
5800 printer->info_2->drivername));
5803 DEBUG(10,("update_printer: changing driver [%s]! Sending event!\n",
5804 printer->info_2->drivername));
5806 notify_printer_driver(snum, printer->info_2->drivername);
5810 * flag which changes actually occured. This is a small subset of
5811 * all the possible changes. We also have to update things in the
5812 * DsSpooler key.
5815 if (!strequal(printer->info_2->comment, old_printer->info_2->comment)) {
5816 push_reg_sz(talloc_tos(), &buffer, printer->info_2->comment);
5817 set_printer_dataex( printer, SPOOL_DSSPOOLER_KEY, "description",
5818 REG_SZ, buffer.data, buffer.length);
5820 notify_printer_comment(snum, printer->info_2->comment);
5823 if (!strequal(printer->info_2->sharename, old_printer->info_2->sharename)) {
5824 push_reg_sz(talloc_tos(), &buffer, printer->info_2->sharename);
5825 set_printer_dataex( printer, SPOOL_DSSPOOLER_KEY, "shareName",
5826 REG_SZ, buffer.data, buffer.length);
5828 notify_printer_sharename(snum, printer->info_2->sharename);
5831 if (!strequal(printer->info_2->printername, old_printer->info_2->printername)) {
5832 char *pname;
5834 if ( (pname = strchr_m( printer->info_2->printername+2, '\\' )) != NULL )
5835 pname++;
5836 else
5837 pname = printer->info_2->printername;
5840 push_reg_sz(talloc_tos(), &buffer, pname);
5841 set_printer_dataex( printer, SPOOL_DSSPOOLER_KEY, "printerName",
5842 REG_SZ, buffer.data, buffer.length);
5844 notify_printer_printername( snum, pname );
5847 if (!strequal(printer->info_2->portname, old_printer->info_2->portname)) {
5848 push_reg_sz(talloc_tos(), &buffer, printer->info_2->portname);
5849 set_printer_dataex( printer, SPOOL_DSSPOOLER_KEY, "portName",
5850 REG_SZ, buffer.data, buffer.length);
5852 notify_printer_port(snum, printer->info_2->portname);
5855 if (!strequal(printer->info_2->location, old_printer->info_2->location)) {
5856 push_reg_sz(talloc_tos(), &buffer, printer->info_2->location);
5857 set_printer_dataex( printer, SPOOL_DSSPOOLER_KEY, "location",
5858 REG_SZ, buffer.data, buffer.length);
5860 notify_printer_location(snum, printer->info_2->location);
5863 /* here we need to update some more DsSpooler keys */
5864 /* uNCName, serverName, shortServerName */
5866 push_reg_sz(talloc_tos(), &buffer, global_myname());
5867 set_printer_dataex( printer, SPOOL_DSSPOOLER_KEY, "serverName",
5868 REG_SZ, buffer.data, buffer.length);
5869 set_printer_dataex( printer, SPOOL_DSSPOOLER_KEY, "shortServerName",
5870 REG_SZ, buffer.data, buffer.length);
5872 slprintf( asc_buffer, sizeof(asc_buffer)-1, "\\\\%s\\%s",
5873 global_myname(), printer->info_2->sharename );
5874 push_reg_sz(talloc_tos(), &buffer, asc_buffer);
5875 set_printer_dataex( printer, SPOOL_DSSPOOLER_KEY, "uNCName",
5876 REG_SZ, buffer.data, buffer.length);
5878 /* Update printer info */
5879 result = mod_a_printer(printer, 2);
5881 done:
5882 free_a_printer(&printer, 2);
5883 free_a_printer(&old_printer, 2);
5886 return result;
5889 /****************************************************************************
5890 ****************************************************************************/
5891 static WERROR publish_or_unpublish_printer(pipes_struct *p,
5892 struct policy_handle *handle,
5893 struct spoolss_SetPrinterInfo7 *info7)
5895 #ifdef HAVE_ADS
5896 int snum;
5897 Printer_entry *Printer;
5899 if ( lp_security() != SEC_ADS ) {
5900 return WERR_UNKNOWN_LEVEL;
5903 Printer = find_printer_index_by_hnd(p, handle);
5905 DEBUG(5,("publish_or_unpublish_printer, action = %d\n",info7->action));
5907 if (!Printer)
5908 return WERR_BADFID;
5910 if (!get_printer_snum(p, handle, &snum, NULL))
5911 return WERR_BADFID;
5913 nt_printer_publish(Printer, snum, info7->action);
5915 return WERR_OK;
5916 #else
5917 return WERR_UNKNOWN_LEVEL;
5918 #endif
5921 /****************************************************************
5922 _spoolss_SetPrinter
5923 ****************************************************************/
5925 WERROR _spoolss_SetPrinter(pipes_struct *p,
5926 struct spoolss_SetPrinter *r)
5928 WERROR result;
5930 Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
5932 if (!Printer) {
5933 DEBUG(2,("_spoolss_SetPrinter: Invalid handle (%s:%u:%u)\n",
5934 OUR_HANDLE(r->in.handle)));
5935 return WERR_BADFID;
5938 /* check the level */
5939 switch (r->in.info_ctr->level) {
5940 case 0:
5941 return control_printer(r->in.handle, r->in.command, p);
5942 case 2:
5943 result = update_printer(p, r->in.handle,
5944 r->in.info_ctr,
5945 r->in.devmode_ctr->devmode);
5946 if (!W_ERROR_IS_OK(result))
5947 return result;
5948 if (r->in.secdesc_ctr->sd)
5949 result = update_printer_sec(r->in.handle, p,
5950 r->in.secdesc_ctr);
5951 return result;
5952 case 3:
5953 return update_printer_sec(r->in.handle, p,
5954 r->in.secdesc_ctr);
5955 case 7:
5956 return publish_or_unpublish_printer(p, r->in.handle,
5957 r->in.info_ctr->info.info7);
5958 default:
5959 return WERR_UNKNOWN_LEVEL;
5963 /****************************************************************
5964 _spoolss_FindClosePrinterNotify
5965 ****************************************************************/
5967 WERROR _spoolss_FindClosePrinterNotify(pipes_struct *p,
5968 struct spoolss_FindClosePrinterNotify *r)
5970 Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
5972 if (!Printer) {
5973 DEBUG(2,("_spoolss_FindClosePrinterNotify: "
5974 "Invalid handle (%s:%u:%u)\n", OUR_HANDLE(r->in.handle)));
5975 return WERR_BADFID;
5978 if (Printer->notify.client_connected == true) {
5979 int snum = -1;
5981 if ( Printer->printer_type == SPLHND_SERVER)
5982 snum = -1;
5983 else if ( (Printer->printer_type == SPLHND_PRINTER) &&
5984 !get_printer_snum(p, r->in.handle, &snum, NULL) )
5985 return WERR_BADFID;
5987 srv_spoolss_replycloseprinter(snum, &Printer->notify.client_hnd);
5990 Printer->notify.flags=0;
5991 Printer->notify.options=0;
5992 Printer->notify.localmachine[0]='\0';
5993 Printer->notify.printerlocal=0;
5994 TALLOC_FREE(Printer->notify.option);
5995 Printer->notify.client_connected = false;
5997 return WERR_OK;
6000 /****************************************************************
6001 _spoolss_AddJob
6002 ****************************************************************/
6004 WERROR _spoolss_AddJob(pipes_struct *p,
6005 struct spoolss_AddJob *r)
6007 if (!r->in.buffer && (r->in.offered != 0)) {
6008 return WERR_INVALID_PARAM;
6011 /* this is what a NT server returns for AddJob. AddJob must fail on
6012 * non-local printers */
6014 if (r->in.level != 1) {
6015 return WERR_UNKNOWN_LEVEL;
6018 return WERR_INVALID_PARAM;
6021 /****************************************************************************
6022 fill_job_info1
6023 ****************************************************************************/
6025 static WERROR fill_job_info1(TALLOC_CTX *mem_ctx,
6026 struct spoolss_JobInfo1 *r,
6027 const print_queue_struct *queue,
6028 int position, int snum,
6029 const NT_PRINTER_INFO_LEVEL *ntprinter)
6031 struct tm *t;
6033 t = gmtime(&queue->time);
6035 r->job_id = queue->job;
6037 r->printer_name = talloc_strdup(mem_ctx, lp_servicename(snum));
6038 W_ERROR_HAVE_NO_MEMORY(r->printer_name);
6039 r->server_name = talloc_strdup(mem_ctx, ntprinter->info_2->servername);
6040 W_ERROR_HAVE_NO_MEMORY(r->server_name);
6041 r->user_name = talloc_strdup(mem_ctx, queue->fs_user);
6042 W_ERROR_HAVE_NO_MEMORY(r->user_name);
6043 r->document_name = talloc_strdup(mem_ctx, queue->fs_file);
6044 W_ERROR_HAVE_NO_MEMORY(r->document_name);
6045 r->data_type = talloc_strdup(mem_ctx, "RAW");
6046 W_ERROR_HAVE_NO_MEMORY(r->data_type);
6047 r->text_status = talloc_strdup(mem_ctx, "");
6048 W_ERROR_HAVE_NO_MEMORY(r->text_status);
6050 r->status = nt_printj_status(queue->status);
6051 r->priority = queue->priority;
6052 r->position = position;
6053 r->total_pages = queue->page_count;
6054 r->pages_printed = 0; /* ??? */
6056 init_systemtime(&r->submitted, t);
6058 return WERR_OK;
6061 /****************************************************************************
6062 fill_job_info2
6063 ****************************************************************************/
6065 static WERROR fill_job_info2(TALLOC_CTX *mem_ctx,
6066 struct spoolss_JobInfo2 *r,
6067 const print_queue_struct *queue,
6068 int position, int snum,
6069 const NT_PRINTER_INFO_LEVEL *ntprinter,
6070 struct spoolss_DeviceMode *devmode)
6072 struct tm *t;
6074 t = gmtime(&queue->time);
6076 r->job_id = queue->job;
6078 r->printer_name = talloc_strdup(mem_ctx, lp_servicename(snum));
6079 W_ERROR_HAVE_NO_MEMORY(r->printer_name);
6080 r->server_name = talloc_strdup(mem_ctx, ntprinter->info_2->servername);
6081 W_ERROR_HAVE_NO_MEMORY(r->server_name);
6082 r->user_name = talloc_strdup(mem_ctx, queue->fs_user);
6083 W_ERROR_HAVE_NO_MEMORY(r->user_name);
6084 r->document_name = talloc_strdup(mem_ctx, queue->fs_file);
6085 W_ERROR_HAVE_NO_MEMORY(r->document_name);
6086 r->notify_name = talloc_strdup(mem_ctx, queue->fs_user);
6087 W_ERROR_HAVE_NO_MEMORY(r->notify_name);
6088 r->data_type = talloc_strdup(mem_ctx, "RAW");
6089 W_ERROR_HAVE_NO_MEMORY(r->data_type);
6090 r->print_processor = talloc_strdup(mem_ctx, "winprint");
6091 W_ERROR_HAVE_NO_MEMORY(r->print_processor);
6092 r->parameters = talloc_strdup(mem_ctx, "");
6093 W_ERROR_HAVE_NO_MEMORY(r->parameters);
6094 r->driver_name = talloc_strdup(mem_ctx, ntprinter->info_2->drivername);
6095 W_ERROR_HAVE_NO_MEMORY(r->driver_name);
6097 r->devmode = devmode;
6099 r->text_status = talloc_strdup(mem_ctx, "");
6100 W_ERROR_HAVE_NO_MEMORY(r->text_status);
6102 r->secdesc = NULL;
6104 r->status = nt_printj_status(queue->status);
6105 r->priority = queue->priority;
6106 r->position = position;
6107 r->start_time = 0;
6108 r->until_time = 0;
6109 r->total_pages = queue->page_count;
6110 r->size = queue->size;
6111 init_systemtime(&r->submitted, t);
6112 r->time = 0;
6113 r->pages_printed = 0; /* ??? */
6115 return WERR_OK;
6118 /****************************************************************************
6119 fill_job_info3
6120 ****************************************************************************/
6122 static WERROR fill_job_info3(TALLOC_CTX *mem_ctx,
6123 struct spoolss_JobInfo3 *r,
6124 const print_queue_struct *queue,
6125 const print_queue_struct *next_queue,
6126 int position, int snum,
6127 const NT_PRINTER_INFO_LEVEL *ntprinter)
6129 r->job_id = queue->job;
6130 r->next_job_id = 0;
6131 if (next_queue) {
6132 r->next_job_id = next_queue->job;
6134 r->reserved = 0;
6136 return WERR_OK;
6139 /****************************************************************************
6140 Enumjobs at level 1.
6141 ****************************************************************************/
6143 static WERROR enumjobs_level1(TALLOC_CTX *mem_ctx,
6144 const print_queue_struct *queue,
6145 uint32_t num_queues, int snum,
6146 const NT_PRINTER_INFO_LEVEL *ntprinter,
6147 union spoolss_JobInfo **info_p,
6148 uint32_t *count)
6150 union spoolss_JobInfo *info;
6151 int i;
6152 WERROR result = WERR_OK;
6154 info = TALLOC_ARRAY(mem_ctx, union spoolss_JobInfo, num_queues);
6155 W_ERROR_HAVE_NO_MEMORY(info);
6157 *count = num_queues;
6159 for (i=0; i<*count; i++) {
6160 result = fill_job_info1(info,
6161 &info[i].info1,
6162 &queue[i],
6164 snum,
6165 ntprinter);
6166 if (!W_ERROR_IS_OK(result)) {
6167 goto out;
6171 out:
6172 if (!W_ERROR_IS_OK(result)) {
6173 TALLOC_FREE(info);
6174 *count = 0;
6175 return result;
6178 *info_p = info;
6180 return WERR_OK;
6183 /****************************************************************************
6184 Enumjobs at level 2.
6185 ****************************************************************************/
6187 static WERROR enumjobs_level2(TALLOC_CTX *mem_ctx,
6188 const print_queue_struct *queue,
6189 uint32_t num_queues, int snum,
6190 const NT_PRINTER_INFO_LEVEL *ntprinter,
6191 union spoolss_JobInfo **info_p,
6192 uint32_t *count)
6194 union spoolss_JobInfo *info;
6195 int i;
6196 WERROR result = WERR_OK;
6198 info = TALLOC_ARRAY(mem_ctx, union spoolss_JobInfo, num_queues);
6199 W_ERROR_HAVE_NO_MEMORY(info);
6201 *count = num_queues;
6203 for (i=0; i<*count; i++) {
6205 struct spoolss_DeviceMode *devmode;
6207 devmode = construct_dev_mode(info, lp_const_servicename(snum));
6208 if (!devmode) {
6209 result = WERR_NOMEM;
6210 goto out;
6213 result = fill_job_info2(info,
6214 &info[i].info2,
6215 &queue[i],
6217 snum,
6218 ntprinter,
6219 devmode);
6220 if (!W_ERROR_IS_OK(result)) {
6221 goto out;
6225 out:
6226 if (!W_ERROR_IS_OK(result)) {
6227 TALLOC_FREE(info);
6228 *count = 0;
6229 return result;
6232 *info_p = info;
6234 return WERR_OK;
6237 /****************************************************************************
6238 Enumjobs at level 3.
6239 ****************************************************************************/
6241 static WERROR enumjobs_level3(TALLOC_CTX *mem_ctx,
6242 const print_queue_struct *queue,
6243 uint32_t num_queues, int snum,
6244 const NT_PRINTER_INFO_LEVEL *ntprinter,
6245 union spoolss_JobInfo **info_p,
6246 uint32_t *count)
6248 union spoolss_JobInfo *info;
6249 int i;
6250 WERROR result = WERR_OK;
6252 info = TALLOC_ARRAY(mem_ctx, union spoolss_JobInfo, num_queues);
6253 W_ERROR_HAVE_NO_MEMORY(info);
6255 *count = num_queues;
6257 for (i=0; i<*count; i++) {
6258 const print_queue_struct *next_queue = NULL;
6260 if (i+1 < *count) {
6261 next_queue = &queue[i+1];
6264 result = fill_job_info3(info,
6265 &info[i].info3,
6266 &queue[i],
6267 next_queue,
6269 snum,
6270 ntprinter);
6271 if (!W_ERROR_IS_OK(result)) {
6272 goto out;
6276 out:
6277 if (!W_ERROR_IS_OK(result)) {
6278 TALLOC_FREE(info);
6279 *count = 0;
6280 return result;
6283 *info_p = info;
6285 return WERR_OK;
6288 /****************************************************************
6289 _spoolss_EnumJobs
6290 ****************************************************************/
6292 WERROR _spoolss_EnumJobs(pipes_struct *p,
6293 struct spoolss_EnumJobs *r)
6295 WERROR result;
6296 NT_PRINTER_INFO_LEVEL *ntprinter = NULL;
6297 int snum;
6298 print_status_struct prt_status;
6299 print_queue_struct *queue = NULL;
6300 uint32_t count;
6302 /* that's an [in out] buffer */
6304 if (!r->in.buffer && (r->in.offered != 0)) {
6305 return WERR_INVALID_PARAM;
6308 DEBUG(4,("_spoolss_EnumJobs\n"));
6310 *r->out.needed = 0;
6311 *r->out.count = 0;
6312 *r->out.info = NULL;
6314 /* lookup the printer snum and tdb entry */
6316 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
6317 return WERR_BADFID;
6320 result = get_a_printer(NULL, &ntprinter, 2, lp_servicename(snum));
6321 if (!W_ERROR_IS_OK(result)) {
6322 return result;
6325 count = print_queue_status(snum, &queue, &prt_status);
6326 DEBUGADD(4,("count:[%d], status:[%d], [%s]\n",
6327 count, prt_status.status, prt_status.message));
6329 if (count == 0) {
6330 SAFE_FREE(queue);
6331 free_a_printer(&ntprinter, 2);
6332 return WERR_OK;
6335 switch (r->in.level) {
6336 case 1:
6337 result = enumjobs_level1(p->mem_ctx, queue, count, snum,
6338 ntprinter, r->out.info, r->out.count);
6339 break;
6340 case 2:
6341 result = enumjobs_level2(p->mem_ctx, queue, count, snum,
6342 ntprinter, r->out.info, r->out.count);
6343 break;
6344 case 3:
6345 result = enumjobs_level3(p->mem_ctx, queue, count, snum,
6346 ntprinter, r->out.info, r->out.count);
6347 break;
6348 default:
6349 result = WERR_UNKNOWN_LEVEL;
6350 break;
6353 SAFE_FREE(queue);
6354 free_a_printer(&ntprinter, 2);
6356 if (!W_ERROR_IS_OK(result)) {
6357 return result;
6360 *r->out.needed = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
6361 spoolss_EnumJobs, NULL,
6362 *r->out.info, r->in.level,
6363 *r->out.count);
6364 *r->out.info = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
6365 *r->out.count = SPOOLSS_BUFFER_OK(*r->out.count, 0);
6367 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
6370 /****************************************************************
6371 _spoolss_ScheduleJob
6372 ****************************************************************/
6374 WERROR _spoolss_ScheduleJob(pipes_struct *p,
6375 struct spoolss_ScheduleJob *r)
6377 return WERR_OK;
6380 /****************************************************************
6381 _spoolss_SetJob
6382 ****************************************************************/
6384 WERROR _spoolss_SetJob(pipes_struct *p,
6385 struct spoolss_SetJob *r)
6387 int snum;
6388 WERROR errcode = WERR_BADFUNC;
6390 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
6391 return WERR_BADFID;
6394 if (!print_job_exists(lp_const_servicename(snum), r->in.job_id)) {
6395 return WERR_INVALID_PRINTER_NAME;
6398 switch (r->in.command) {
6399 case SPOOLSS_JOB_CONTROL_CANCEL:
6400 case SPOOLSS_JOB_CONTROL_DELETE:
6401 if (print_job_delete(p->server_info, snum, r->in.job_id, &errcode)) {
6402 errcode = WERR_OK;
6404 break;
6405 case SPOOLSS_JOB_CONTROL_PAUSE:
6406 if (print_job_pause(p->server_info, snum, r->in.job_id, &errcode)) {
6407 errcode = WERR_OK;
6409 break;
6410 case SPOOLSS_JOB_CONTROL_RESTART:
6411 case SPOOLSS_JOB_CONTROL_RESUME:
6412 if (print_job_resume(p->server_info, snum, r->in.job_id, &errcode)) {
6413 errcode = WERR_OK;
6415 break;
6416 default:
6417 return WERR_UNKNOWN_LEVEL;
6420 return errcode;
6423 /****************************************************************************
6424 Enumerates all printer drivers by level and architecture.
6425 ****************************************************************************/
6427 static WERROR enumprinterdrivers_level_by_architecture(TALLOC_CTX *mem_ctx,
6428 const char *servername,
6429 const char *architecture,
6430 uint32_t level,
6431 union spoolss_DriverInfo **info_p,
6432 uint32_t *count_p)
6434 int i;
6435 int ndrivers;
6436 uint32_t version;
6437 fstring *list = NULL;
6438 struct spoolss_DriverInfo8 *driver;
6439 union spoolss_DriverInfo *info = NULL;
6440 uint32_t count = 0;
6441 WERROR result = WERR_OK;
6443 *count_p = 0;
6444 *info_p = NULL;
6446 for (version=0; version<DRIVER_MAX_VERSION; version++) {
6447 list = NULL;
6448 ndrivers = get_ntdrivers(&list, architecture, version);
6449 DEBUGADD(4,("we have:[%d] drivers in environment [%s] and version [%d]\n",
6450 ndrivers, architecture, version));
6452 if (ndrivers == -1) {
6453 result = WERR_NOMEM;
6454 goto out;
6457 if (ndrivers != 0) {
6458 info = TALLOC_REALLOC_ARRAY(mem_ctx, info,
6459 union spoolss_DriverInfo,
6460 count + ndrivers);
6461 if (!info) {
6462 DEBUG(0,("enumprinterdrivers_level_by_architecture: "
6463 "failed to enlarge driver info buffer!\n"));
6464 result = WERR_NOMEM;
6465 goto out;
6469 for (i=0; i<ndrivers; i++) {
6470 DEBUGADD(5,("\tdriver: [%s]\n", list[i]));
6471 ZERO_STRUCT(driver);
6472 result = get_a_printer_driver(mem_ctx, &driver, list[i],
6473 architecture, version);
6474 if (!W_ERROR_IS_OK(result)) {
6475 goto out;
6478 switch (level) {
6479 case 1:
6480 result = fill_printer_driver_info1(info, &info[count+i].info1,
6481 driver, servername);
6482 break;
6483 case 2:
6484 result = fill_printer_driver_info2(info, &info[count+i].info2,
6485 driver, servername);
6486 break;
6487 case 3:
6488 result = fill_printer_driver_info3(info, &info[count+i].info3,
6489 driver, servername);
6490 break;
6491 case 4:
6492 result = fill_printer_driver_info4(info, &info[count+i].info4,
6493 driver, servername);
6494 break;
6495 case 5:
6496 result = fill_printer_driver_info5(info, &info[count+i].info5,
6497 driver, servername);
6498 break;
6499 case 6:
6500 result = fill_printer_driver_info6(info, &info[count+i].info6,
6501 driver, servername);
6502 break;
6503 case 8:
6504 result = fill_printer_driver_info8(info, &info[count+i].info8,
6505 driver, servername);
6506 break;
6507 default:
6508 result = WERR_UNKNOWN_LEVEL;
6509 break;
6512 free_a_printer_driver(driver);
6514 if (!W_ERROR_IS_OK(result)) {
6515 goto out;
6519 count += ndrivers;
6520 SAFE_FREE(list);
6523 out:
6524 SAFE_FREE(list);
6526 if (!W_ERROR_IS_OK(result)) {
6527 TALLOC_FREE(info);
6528 return result;
6531 *info_p = info;
6532 *count_p = count;
6534 return WERR_OK;
6537 /****************************************************************************
6538 Enumerates all printer drivers by level.
6539 ****************************************************************************/
6541 static WERROR enumprinterdrivers_level(TALLOC_CTX *mem_ctx,
6542 const char *servername,
6543 const char *architecture,
6544 uint32_t level,
6545 union spoolss_DriverInfo **info_p,
6546 uint32_t *count_p)
6548 uint32_t a,i;
6549 WERROR result = WERR_OK;
6551 if (strequal(architecture, SPOOLSS_ARCHITECTURE_ALL)) {
6553 for (a=0; archi_table[a].long_archi != NULL; a++) {
6555 union spoolss_DriverInfo *info = NULL;
6556 uint32_t count = 0;
6558 result = enumprinterdrivers_level_by_architecture(mem_ctx,
6559 servername,
6560 archi_table[a].long_archi,
6561 level,
6562 &info,
6563 &count);
6564 if (!W_ERROR_IS_OK(result)) {
6565 continue;
6568 for (i=0; i < count; i++) {
6569 ADD_TO_ARRAY(mem_ctx, union spoolss_DriverInfo,
6570 info[i], info_p, count_p);
6574 return result;
6577 return enumprinterdrivers_level_by_architecture(mem_ctx,
6578 servername,
6579 architecture,
6580 level,
6581 info_p,
6582 count_p);
6585 /****************************************************************
6586 _spoolss_EnumPrinterDrivers
6587 ****************************************************************/
6589 WERROR _spoolss_EnumPrinterDrivers(pipes_struct *p,
6590 struct spoolss_EnumPrinterDrivers *r)
6592 const char *cservername;
6593 WERROR result;
6595 /* that's an [in out] buffer */
6597 if (!r->in.buffer && (r->in.offered != 0)) {
6598 return WERR_INVALID_PARAM;
6601 DEBUG(4,("_spoolss_EnumPrinterDrivers\n"));
6603 *r->out.needed = 0;
6604 *r->out.count = 0;
6605 *r->out.info = NULL;
6607 cservername = canon_servername(r->in.server);
6609 if (!is_myname_or_ipaddr(cservername)) {
6610 return WERR_UNKNOWN_PRINTER_DRIVER;
6613 result = enumprinterdrivers_level(p->mem_ctx, cservername,
6614 r->in.environment,
6615 r->in.level,
6616 r->out.info,
6617 r->out.count);
6618 if (!W_ERROR_IS_OK(result)) {
6619 return result;
6622 *r->out.needed = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
6623 spoolss_EnumPrinterDrivers, NULL,
6624 *r->out.info, r->in.level,
6625 *r->out.count);
6626 *r->out.info = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
6627 *r->out.count = SPOOLSS_BUFFER_OK(*r->out.count, 0);
6629 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
6632 /****************************************************************************
6633 ****************************************************************************/
6635 static WERROR fill_form_info_1(TALLOC_CTX *mem_ctx,
6636 struct spoolss_FormInfo1 *r,
6637 const nt_forms_struct *form)
6639 r->form_name = talloc_strdup(mem_ctx, form->name);
6640 W_ERROR_HAVE_NO_MEMORY(r->form_name);
6642 r->flags = form->flag;
6643 r->size.width = form->width;
6644 r->size.height = form->length;
6645 r->area.left = form->left;
6646 r->area.top = form->top;
6647 r->area.right = form->right;
6648 r->area.bottom = form->bottom;
6650 return WERR_OK;
6653 /****************************************************************
6654 spoolss_enumforms_level1
6655 ****************************************************************/
6657 static WERROR spoolss_enumforms_level1(TALLOC_CTX *mem_ctx,
6658 const nt_forms_struct *builtin_forms,
6659 uint32_t num_builtin_forms,
6660 const nt_forms_struct *user_forms,
6661 uint32_t num_user_forms,
6662 union spoolss_FormInfo **info_p,
6663 uint32_t *count)
6665 union spoolss_FormInfo *info;
6666 WERROR result = WERR_OK;
6667 int i;
6669 *count = num_builtin_forms + num_user_forms;
6671 info = TALLOC_ARRAY(mem_ctx, union spoolss_FormInfo, *count);
6672 W_ERROR_HAVE_NO_MEMORY(info);
6674 /* construct the list of form structures */
6675 for (i=0; i<num_builtin_forms; i++) {
6676 DEBUGADD(6,("Filling builtin form number [%d]\n",i));
6677 result = fill_form_info_1(info, &info[i].info1,
6678 &builtin_forms[i]);
6679 if (!W_ERROR_IS_OK(result)) {
6680 goto out;
6684 for (i=0; i<num_user_forms; i++) {
6685 DEBUGADD(6,("Filling user form number [%d]\n",i));
6686 result = fill_form_info_1(info, &info[i+num_builtin_forms].info1,
6687 &user_forms[i]);
6688 if (!W_ERROR_IS_OK(result)) {
6689 goto out;
6693 out:
6694 if (!W_ERROR_IS_OK(result)) {
6695 TALLOC_FREE(info);
6696 *count = 0;
6697 return result;
6700 *info_p = info;
6702 return WERR_OK;
6705 /****************************************************************
6706 _spoolss_EnumForms
6707 ****************************************************************/
6709 WERROR _spoolss_EnumForms(pipes_struct *p,
6710 struct spoolss_EnumForms *r)
6712 WERROR result;
6713 nt_forms_struct *user_forms = NULL;
6714 nt_forms_struct *builtin_forms = NULL;
6715 uint32_t num_user_forms;
6716 uint32_t num_builtin_forms;
6718 *r->out.count = 0;
6719 *r->out.needed = 0;
6720 *r->out.info = NULL;
6722 /* that's an [in out] buffer */
6724 if (!r->in.buffer && (r->in.offered != 0) ) {
6725 return WERR_INVALID_PARAM;
6728 DEBUG(4,("_spoolss_EnumForms\n"));
6729 DEBUGADD(5,("Offered buffer size [%d]\n", r->in.offered));
6730 DEBUGADD(5,("Info level [%d]\n", r->in.level));
6732 num_builtin_forms = get_builtin_ntforms(&builtin_forms);
6733 DEBUGADD(5,("Number of builtin forms [%d]\n", num_builtin_forms));
6734 num_user_forms = get_ntforms(&user_forms);
6735 DEBUGADD(5,("Number of user forms [%d]\n", num_user_forms));
6737 if (num_user_forms + num_builtin_forms == 0) {
6738 SAFE_FREE(builtin_forms);
6739 SAFE_FREE(user_forms);
6740 return WERR_NO_MORE_ITEMS;
6743 switch (r->in.level) {
6744 case 1:
6745 result = spoolss_enumforms_level1(p->mem_ctx,
6746 builtin_forms,
6747 num_builtin_forms,
6748 user_forms,
6749 num_user_forms,
6750 r->out.info,
6751 r->out.count);
6752 break;
6753 default:
6754 result = WERR_UNKNOWN_LEVEL;
6755 break;
6758 SAFE_FREE(user_forms);
6759 SAFE_FREE(builtin_forms);
6761 if (!W_ERROR_IS_OK(result)) {
6762 return result;
6765 *r->out.needed = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
6766 spoolss_EnumForms, NULL,
6767 *r->out.info, r->in.level,
6768 *r->out.count);
6769 *r->out.info = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
6770 *r->out.count = SPOOLSS_BUFFER_OK(*r->out.count, 0);
6772 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
6775 /****************************************************************
6776 ****************************************************************/
6778 static WERROR find_form_byname(const char *name,
6779 nt_forms_struct *form)
6781 nt_forms_struct *list = NULL;
6782 int num_forms = 0, i = 0;
6784 if (get_a_builtin_ntform_by_string(name, form)) {
6785 return WERR_OK;
6788 num_forms = get_ntforms(&list);
6789 DEBUGADD(5,("Number of forms [%d]\n", num_forms));
6791 if (num_forms == 0) {
6792 return WERR_BADFID;
6795 /* Check if the requested name is in the list of form structures */
6796 for (i = 0; i < num_forms; i++) {
6798 DEBUG(4,("checking form %s (want %s)\n", list[i].name, name));
6800 if (strequal(name, list[i].name)) {
6801 DEBUGADD(6,("Found form %s number [%d]\n", name, i));
6802 *form = list[i];
6803 SAFE_FREE(list);
6804 return WERR_OK;
6808 SAFE_FREE(list);
6810 return WERR_BADFID;
6813 /****************************************************************
6814 _spoolss_GetForm
6815 ****************************************************************/
6817 WERROR _spoolss_GetForm(pipes_struct *p,
6818 struct spoolss_GetForm *r)
6820 WERROR result;
6821 nt_forms_struct form;
6823 /* that's an [in out] buffer */
6825 if (!r->in.buffer && (r->in.offered != 0)) {
6826 return WERR_INVALID_PARAM;
6829 DEBUG(4,("_spoolss_GetForm\n"));
6830 DEBUGADD(5,("Offered buffer size [%d]\n", r->in.offered));
6831 DEBUGADD(5,("Info level [%d]\n", r->in.level));
6833 result = find_form_byname(r->in.form_name, &form);
6834 if (!W_ERROR_IS_OK(result)) {
6835 TALLOC_FREE(r->out.info);
6836 return result;
6839 switch (r->in.level) {
6840 case 1:
6841 result = fill_form_info_1(p->mem_ctx,
6842 &r->out.info->info1,
6843 &form);
6844 break;
6846 default:
6847 result = WERR_UNKNOWN_LEVEL;
6848 break;
6851 if (!W_ERROR_IS_OK(result)) {
6852 TALLOC_FREE(r->out.info);
6853 return result;
6856 *r->out.needed = SPOOLSS_BUFFER_UNION(spoolss_FormInfo, NULL,
6857 r->out.info, r->in.level);
6858 r->out.info = SPOOLSS_BUFFER_OK(r->out.info, NULL);
6860 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
6863 /****************************************************************************
6864 ****************************************************************************/
6866 static WERROR fill_port_1(TALLOC_CTX *mem_ctx,
6867 struct spoolss_PortInfo1 *r,
6868 const char *name)
6870 r->port_name = talloc_strdup(mem_ctx, name);
6871 W_ERROR_HAVE_NO_MEMORY(r->port_name);
6873 return WERR_OK;
6876 /****************************************************************************
6877 TODO: This probably needs distinguish between TCP/IP and Local ports
6878 somehow.
6879 ****************************************************************************/
6881 static WERROR fill_port_2(TALLOC_CTX *mem_ctx,
6882 struct spoolss_PortInfo2 *r,
6883 const char *name)
6885 r->port_name = talloc_strdup(mem_ctx, name);
6886 W_ERROR_HAVE_NO_MEMORY(r->port_name);
6888 r->monitor_name = talloc_strdup(mem_ctx, "Local Monitor");
6889 W_ERROR_HAVE_NO_MEMORY(r->monitor_name);
6891 r->description = talloc_strdup(mem_ctx, SPL_LOCAL_PORT);
6892 W_ERROR_HAVE_NO_MEMORY(r->description);
6894 r->port_type = SPOOLSS_PORT_TYPE_WRITE;
6895 r->reserved = 0;
6897 return WERR_OK;
6901 /****************************************************************************
6902 wrapper around the enumer ports command
6903 ****************************************************************************/
6905 static WERROR enumports_hook(TALLOC_CTX *ctx, int *count, char ***lines)
6907 char *cmd = lp_enumports_cmd();
6908 char **qlines = NULL;
6909 char *command = NULL;
6910 int numlines;
6911 int ret;
6912 int fd;
6914 *count = 0;
6915 *lines = NULL;
6917 /* if no hook then just fill in the default port */
6919 if ( !*cmd ) {
6920 if (!(qlines = TALLOC_ARRAY( NULL, char*, 2 ))) {
6921 return WERR_NOMEM;
6923 if (!(qlines[0] = talloc_strdup(qlines, SAMBA_PRINTER_PORT_NAME ))) {
6924 TALLOC_FREE(qlines);
6925 return WERR_NOMEM;
6927 qlines[1] = NULL;
6928 numlines = 1;
6930 else {
6931 /* we have a valid enumport command */
6933 command = talloc_asprintf(ctx, "%s \"%d\"", cmd, 1);
6934 if (!command) {
6935 return WERR_NOMEM;
6938 DEBUG(10,("Running [%s]\n", command));
6939 ret = smbrun(command, &fd);
6940 DEBUG(10,("Returned [%d]\n", ret));
6941 TALLOC_FREE(command);
6942 if (ret != 0) {
6943 if (fd != -1) {
6944 close(fd);
6946 return WERR_ACCESS_DENIED;
6949 numlines = 0;
6950 qlines = fd_lines_load(fd, &numlines, 0, NULL);
6951 DEBUGADD(10,("Lines returned = [%d]\n", numlines));
6952 close(fd);
6955 *count = numlines;
6956 *lines = qlines;
6958 return WERR_OK;
6961 /****************************************************************************
6962 enumports level 1.
6963 ****************************************************************************/
6965 static WERROR enumports_level_1(TALLOC_CTX *mem_ctx,
6966 union spoolss_PortInfo **info_p,
6967 uint32_t *count)
6969 union spoolss_PortInfo *info = NULL;
6970 int i=0;
6971 WERROR result = WERR_OK;
6972 char **qlines = NULL;
6973 int numlines = 0;
6975 result = enumports_hook(talloc_tos(), &numlines, &qlines );
6976 if (!W_ERROR_IS_OK(result)) {
6977 goto out;
6980 if (numlines) {
6981 info = TALLOC_ARRAY(mem_ctx, union spoolss_PortInfo, numlines);
6982 if (!info) {
6983 DEBUG(10,("Returning WERR_NOMEM\n"));
6984 result = WERR_NOMEM;
6985 goto out;
6988 for (i=0; i<numlines; i++) {
6989 DEBUG(6,("Filling port number [%d] with port [%s]\n", i, qlines[i]));
6990 result = fill_port_1(info, &info[i].info1, qlines[i]);
6991 if (!W_ERROR_IS_OK(result)) {
6992 goto out;
6996 TALLOC_FREE(qlines);
6998 out:
6999 if (!W_ERROR_IS_OK(result)) {
7000 TALLOC_FREE(info);
7001 TALLOC_FREE(qlines);
7002 *count = 0;
7003 *info_p = NULL;
7004 return result;
7007 *info_p = info;
7008 *count = numlines;
7010 return WERR_OK;
7013 /****************************************************************************
7014 enumports level 2.
7015 ****************************************************************************/
7017 static WERROR enumports_level_2(TALLOC_CTX *mem_ctx,
7018 union spoolss_PortInfo **info_p,
7019 uint32_t *count)
7021 union spoolss_PortInfo *info = NULL;
7022 int i=0;
7023 WERROR result = WERR_OK;
7024 char **qlines = NULL;
7025 int numlines = 0;
7027 result = enumports_hook(talloc_tos(), &numlines, &qlines );
7028 if (!W_ERROR_IS_OK(result)) {
7029 goto out;
7032 if (numlines) {
7033 info = TALLOC_ARRAY(mem_ctx, union spoolss_PortInfo, numlines);
7034 if (!info) {
7035 DEBUG(10,("Returning WERR_NOMEM\n"));
7036 result = WERR_NOMEM;
7037 goto out;
7040 for (i=0; i<numlines; i++) {
7041 DEBUG(6,("Filling port number [%d] with port [%s]\n", i, qlines[i]));
7042 result = fill_port_2(info, &info[i].info2, qlines[i]);
7043 if (!W_ERROR_IS_OK(result)) {
7044 goto out;
7048 TALLOC_FREE(qlines);
7050 out:
7051 if (!W_ERROR_IS_OK(result)) {
7052 TALLOC_FREE(info);
7053 TALLOC_FREE(qlines);
7054 *count = 0;
7055 *info_p = NULL;
7056 return result;
7059 *info_p = info;
7060 *count = numlines;
7062 return WERR_OK;
7065 /****************************************************************
7066 _spoolss_EnumPorts
7067 ****************************************************************/
7069 WERROR _spoolss_EnumPorts(pipes_struct *p,
7070 struct spoolss_EnumPorts *r)
7072 WERROR result;
7074 /* that's an [in out] buffer */
7076 if (!r->in.buffer && (r->in.offered != 0)) {
7077 return WERR_INVALID_PARAM;
7080 DEBUG(4,("_spoolss_EnumPorts\n"));
7082 *r->out.count = 0;
7083 *r->out.needed = 0;
7084 *r->out.info = NULL;
7086 switch (r->in.level) {
7087 case 1:
7088 result = enumports_level_1(p->mem_ctx, r->out.info,
7089 r->out.count);
7090 break;
7091 case 2:
7092 result = enumports_level_2(p->mem_ctx, r->out.info,
7093 r->out.count);
7094 break;
7095 default:
7096 return WERR_UNKNOWN_LEVEL;
7099 if (!W_ERROR_IS_OK(result)) {
7100 return result;
7103 *r->out.needed = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
7104 spoolss_EnumPorts, NULL,
7105 *r->out.info, r->in.level,
7106 *r->out.count);
7107 *r->out.info = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
7108 *r->out.count = SPOOLSS_BUFFER_OK(*r->out.count, 0);
7110 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
7113 /****************************************************************************
7114 ****************************************************************************/
7116 static WERROR spoolss_addprinterex_level_2(pipes_struct *p,
7117 const char *server,
7118 struct spoolss_SetPrinterInfoCtr *info_ctr,
7119 struct spoolss_DeviceMode *devmode,
7120 struct security_descriptor *sec_desc,
7121 struct spoolss_UserLevelCtr *user_ctr,
7122 struct policy_handle *handle)
7124 NT_PRINTER_INFO_LEVEL *printer = NULL;
7125 fstring name;
7126 int snum;
7127 WERROR err = WERR_OK;
7129 if ( !(printer = TALLOC_ZERO_P(NULL, NT_PRINTER_INFO_LEVEL)) ) {
7130 DEBUG(0,("spoolss_addprinterex_level_2: malloc fail.\n"));
7131 return WERR_NOMEM;
7134 /* convert from UNICODE to ASCII - this allocates the info_2 struct inside *printer.*/
7135 if (!convert_printer_info(info_ctr, printer)) {
7136 free_a_printer(&printer, 2);
7137 return WERR_NOMEM;
7140 /* check to see if the printer already exists */
7142 if ((snum = print_queue_snum(printer->info_2->sharename)) != -1) {
7143 DEBUG(5, ("spoolss_addprinterex_level_2: Attempted to add a printer named [%s] when one already existed!\n",
7144 printer->info_2->sharename));
7145 free_a_printer(&printer, 2);
7146 return WERR_PRINTER_ALREADY_EXISTS;
7149 /* FIXME!!! smbd should check to see if the driver is installed before
7150 trying to add a printer like this --jerry */
7152 if (*lp_addprinter_cmd() ) {
7153 if ( !add_printer_hook(p->mem_ctx, p->server_info->ptok,
7154 printer) ) {
7155 free_a_printer(&printer,2);
7156 return WERR_ACCESS_DENIED;
7158 } else {
7159 DEBUG(0,("spoolss_addprinterex_level_2: add printer for printer %s called and no"
7160 "smb.conf parameter \"addprinter command\" is defined. This"
7161 "parameter must exist for this call to succeed\n",
7162 printer->info_2->sharename ));
7165 /* use our primary netbios name since get_a_printer() will convert
7166 it to what the client expects on a case by case basis */
7168 slprintf(name, sizeof(name)-1, "\\\\%s\\%s", global_myname(),
7169 printer->info_2->sharename);
7172 if ((snum = print_queue_snum(printer->info_2->sharename)) == -1) {
7173 free_a_printer(&printer,2);
7174 return WERR_ACCESS_DENIED;
7177 /* you must be a printer admin to add a new printer */
7178 if (!print_access_check(p->server_info, snum, PRINTER_ACCESS_ADMINISTER)) {
7179 free_a_printer(&printer,2);
7180 return WERR_ACCESS_DENIED;
7184 * Do sanity check on the requested changes for Samba.
7187 if (!check_printer_ok(printer->info_2, snum)) {
7188 free_a_printer(&printer,2);
7189 return WERR_INVALID_PARAM;
7193 * When a printer is created, the drivername bound to the printer is used
7194 * to lookup previously saved driver initialization info, which is then
7195 * bound to the new printer, simulating what happens in the Windows arch.
7198 if (!devmode)
7200 set_driver_init(printer, 2);
7202 else
7204 /* A valid devmode was included, convert and link it
7206 DEBUGADD(10, ("spoolss_addprinterex_level_2: devmode included, converting\n"));
7208 if (!convert_devicemode(printer->info_2->printername, devmode,
7209 &printer->info_2->devmode)) {
7210 return WERR_NOMEM;
7214 /* write the ASCII on disk */
7215 err = mod_a_printer(printer, 2);
7216 if (!W_ERROR_IS_OK(err)) {
7217 free_a_printer(&printer,2);
7218 return err;
7221 if (!open_printer_hnd(p, handle, name, PRINTER_ACCESS_ADMINISTER)) {
7222 /* Handle open failed - remove addition. */
7223 del_a_printer(printer->info_2->sharename);
7224 free_a_printer(&printer,2);
7225 ZERO_STRUCTP(handle);
7226 return WERR_ACCESS_DENIED;
7229 update_c_setprinter(false);
7230 free_a_printer(&printer,2);
7232 return WERR_OK;
7235 /****************************************************************
7236 _spoolss_AddPrinterEx
7237 ****************************************************************/
7239 WERROR _spoolss_AddPrinterEx(pipes_struct *p,
7240 struct spoolss_AddPrinterEx *r)
7242 switch (r->in.info_ctr->level) {
7243 case 1:
7244 /* we don't handle yet */
7245 /* but I know what to do ... */
7246 return WERR_UNKNOWN_LEVEL;
7247 case 2:
7248 return spoolss_addprinterex_level_2(p, r->in.server,
7249 r->in.info_ctr,
7250 r->in.devmode_ctr->devmode,
7251 r->in.secdesc_ctr->sd,
7252 r->in.userlevel_ctr,
7253 r->out.handle);
7254 default:
7255 return WERR_UNKNOWN_LEVEL;
7259 /****************************************************************
7260 _spoolss_AddPrinter
7261 ****************************************************************/
7263 WERROR _spoolss_AddPrinter(pipes_struct *p,
7264 struct spoolss_AddPrinter *r)
7266 struct spoolss_AddPrinterEx a;
7267 struct spoolss_UserLevelCtr userlevel_ctr;
7269 ZERO_STRUCT(userlevel_ctr);
7271 userlevel_ctr.level = 1;
7273 a.in.server = r->in.server;
7274 a.in.info_ctr = r->in.info_ctr;
7275 a.in.devmode_ctr = r->in.devmode_ctr;
7276 a.in.secdesc_ctr = r->in.secdesc_ctr;
7277 a.in.userlevel_ctr = &userlevel_ctr;
7278 a.out.handle = r->out.handle;
7280 return _spoolss_AddPrinterEx(p, &a);
7283 /****************************************************************
7284 _spoolss_AddPrinterDriver
7285 ****************************************************************/
7287 WERROR _spoolss_AddPrinterDriver(pipes_struct *p,
7288 struct spoolss_AddPrinterDriver *r)
7290 WERROR err = WERR_OK;
7291 char *driver_name = NULL;
7292 uint32_t version;
7293 const char *fn;
7295 switch (p->hdr_req.opnum) {
7296 case NDR_SPOOLSS_ADDPRINTERDRIVER:
7297 fn = "_spoolss_AddPrinterDriver";
7298 break;
7299 case NDR_SPOOLSS_ADDPRINTERDRIVEREX:
7300 fn = "_spoolss_AddPrinterDriverEx";
7301 break;
7302 default:
7303 return WERR_INVALID_PARAM;
7307 /* FIXME */
7308 if (r->in.info_ctr->level != 3 && r->in.info_ctr->level != 6) {
7309 /* Clever hack from Martin Zielinski <mz@seh.de>
7310 * to allow downgrade from level 8 (Vista).
7312 DEBUG(0,("%s: level %d not yet implemented\n", fn,
7313 r->in.info_ctr->level));
7314 return WERR_UNKNOWN_LEVEL;
7317 DEBUG(5,("Cleaning driver's information\n"));
7318 err = clean_up_driver_struct(p, r->in.info_ctr);
7319 if (!W_ERROR_IS_OK(err))
7320 goto done;
7322 DEBUG(5,("Moving driver to final destination\n"));
7323 if( !W_ERROR_IS_OK(err = move_driver_to_download_area(p, r->in.info_ctr,
7324 &err)) ) {
7325 goto done;
7328 if (add_a_printer_driver(p->mem_ctx, r->in.info_ctr, &driver_name, &version)!=0) {
7329 err = WERR_ACCESS_DENIED;
7330 goto done;
7334 * I think this is where he DrvUpgradePrinter() hook would be
7335 * be called in a driver's interface DLL on a Windows NT 4.0/2k
7336 * server. Right now, we just need to send ourselves a message
7337 * to update each printer bound to this driver. --jerry
7340 if (!srv_spoolss_drv_upgrade_printer(driver_name)) {
7341 DEBUG(0,("%s: Failed to send message about upgrading driver [%s]!\n",
7342 fn, driver_name));
7346 * Based on the version (e.g. driver destination dir: 0=9x,2=Nt/2k,3=2k/Xp),
7347 * decide if the driver init data should be deleted. The rules are:
7348 * 1) never delete init data if it is a 9x driver, they don't use it anyway
7349 * 2) delete init data only if there is no 2k/Xp driver
7350 * 3) always delete init data
7351 * The generalized rule is always use init data from the highest order driver.
7352 * It is necessary to follow the driver install by an initialization step to
7353 * finish off this process.
7356 switch (version) {
7358 * 9x printer driver - never delete init data
7360 case 0:
7361 DEBUG(10,("%s: init data not deleted for 9x driver [%s]\n",
7362 fn, driver_name));
7363 break;
7366 * Nt or 2k (compatiblity mode) printer driver - only delete init data if
7367 * there is no 2k/Xp driver init data for this driver name.
7369 case 2:
7371 struct spoolss_DriverInfo8 *driver1;
7373 if (!W_ERROR_IS_OK(get_a_printer_driver(p->mem_ctx, &driver1, driver_name, "Windows NT x86", 3))) {
7375 * No 2k/Xp driver found, delete init data (if any) for the new Nt driver.
7377 if (!del_driver_init(driver_name))
7378 DEBUG(6,("%s: del_driver_init(%s) Nt failed!\n",
7379 fn, driver_name));
7380 } else {
7382 * a 2k/Xp driver was found, don't delete init data because Nt driver will use it.
7384 free_a_printer_driver(driver1);
7385 DEBUG(10,("%s: init data not deleted for Nt driver [%s]\n",
7386 fn, driver_name));
7389 break;
7392 * 2k or Xp printer driver - always delete init data
7394 case 3:
7395 if (!del_driver_init(driver_name))
7396 DEBUG(6,("%s: del_driver_init(%s) 2k/Xp failed!\n",
7397 fn, driver_name));
7398 break;
7400 default:
7401 DEBUG(0,("%s: invalid level=%d\n", fn,
7402 r->in.info_ctr->level));
7403 break;
7407 done:
7408 return err;
7411 /****************************************************************
7412 _spoolss_AddPrinterDriverEx
7413 ****************************************************************/
7415 WERROR _spoolss_AddPrinterDriverEx(pipes_struct *p,
7416 struct spoolss_AddPrinterDriverEx *r)
7418 struct spoolss_AddPrinterDriver a;
7421 * we only support the semantics of AddPrinterDriver()
7422 * i.e. only copy files that are newer than existing ones
7425 if (r->in.flags == 0) {
7426 return WERR_INVALID_PARAM;
7429 if (r->in.flags != APD_COPY_NEW_FILES) {
7430 return WERR_ACCESS_DENIED;
7433 a.in.servername = r->in.servername;
7434 a.in.info_ctr = r->in.info_ctr;
7436 return _spoolss_AddPrinterDriver(p, &a);
7439 /****************************************************************************
7440 ****************************************************************************/
7442 struct _spoolss_paths {
7443 int type;
7444 const char *share;
7445 const char *dir;
7448 enum { SPOOLSS_DRIVER_PATH, SPOOLSS_PRTPROCS_PATH };
7450 static const struct _spoolss_paths spoolss_paths[]= {
7451 { SPOOLSS_DRIVER_PATH, "print$", "DRIVERS" },
7452 { SPOOLSS_PRTPROCS_PATH, "prnproc$", "PRTPROCS" }
7455 static WERROR compose_spoolss_server_path(TALLOC_CTX *mem_ctx,
7456 const char *servername,
7457 const char *environment,
7458 int component,
7459 char **path)
7461 const char *pservername = NULL;
7462 const char *long_archi = SPOOLSS_ARCHITECTURE_NT_X86;
7463 const char *short_archi;
7465 *path = NULL;
7467 /* environment may be empty */
7468 if (environment && strlen(environment)) {
7469 long_archi = environment;
7472 /* servername may be empty */
7473 if (servername && strlen(servername)) {
7474 pservername = canon_servername(servername);
7476 if (!is_myname_or_ipaddr(pservername)) {
7477 return WERR_INVALID_PARAM;
7481 if (!(short_archi = get_short_archi(long_archi))) {
7482 return WERR_INVALID_ENVIRONMENT;
7485 switch (component) {
7486 case SPOOLSS_PRTPROCS_PATH:
7487 case SPOOLSS_DRIVER_PATH:
7488 if (pservername) {
7489 *path = talloc_asprintf(mem_ctx,
7490 "\\\\%s\\%s\\%s",
7491 pservername,
7492 spoolss_paths[component].share,
7493 short_archi);
7494 } else {
7495 *path = talloc_asprintf(mem_ctx, "%s\\%s\\%s",
7496 SPOOLSS_DEFAULT_SERVER_PATH,
7497 spoolss_paths[component].dir,
7498 short_archi);
7500 break;
7501 default:
7502 return WERR_INVALID_PARAM;
7505 if (!*path) {
7506 return WERR_NOMEM;
7509 return WERR_OK;
7512 /****************************************************************************
7513 ****************************************************************************/
7515 static WERROR getprinterdriverdir_level_1(TALLOC_CTX *mem_ctx,
7516 const char *servername,
7517 const char *environment,
7518 struct spoolss_DriverDirectoryInfo1 *r)
7520 WERROR werr;
7521 char *path = NULL;
7523 werr = compose_spoolss_server_path(mem_ctx,
7524 servername,
7525 environment,
7526 SPOOLSS_DRIVER_PATH,
7527 &path);
7528 if (!W_ERROR_IS_OK(werr)) {
7529 return werr;
7532 DEBUG(4,("printer driver directory: [%s]\n", path));
7534 r->directory_name = path;
7536 return WERR_OK;
7539 /****************************************************************
7540 _spoolss_GetPrinterDriverDirectory
7541 ****************************************************************/
7543 WERROR _spoolss_GetPrinterDriverDirectory(pipes_struct *p,
7544 struct spoolss_GetPrinterDriverDirectory *r)
7546 WERROR werror;
7548 /* that's an [in out] buffer */
7550 if (!r->in.buffer && (r->in.offered != 0)) {
7551 return WERR_INVALID_PARAM;
7554 DEBUG(5,("_spoolss_GetPrinterDriverDirectory: level %d\n",
7555 r->in.level));
7557 *r->out.needed = 0;
7559 /* r->in.level is ignored */
7561 werror = getprinterdriverdir_level_1(p->mem_ctx,
7562 r->in.server,
7563 r->in.environment,
7564 &r->out.info->info1);
7565 if (!W_ERROR_IS_OK(werror)) {
7566 TALLOC_FREE(r->out.info);
7567 return werror;
7570 *r->out.needed = SPOOLSS_BUFFER_UNION(spoolss_DriverDirectoryInfo, NULL,
7571 r->out.info, r->in.level);
7572 r->out.info = SPOOLSS_BUFFER_OK(r->out.info, NULL);
7574 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
7577 /****************************************************************
7578 _spoolss_EnumPrinterData
7579 ****************************************************************/
7581 WERROR _spoolss_EnumPrinterData(pipes_struct *p,
7582 struct spoolss_EnumPrinterData *r)
7584 NT_PRINTER_INFO_LEVEL *printer = NULL;
7585 Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
7586 int snum;
7587 WERROR result;
7588 struct regval_blob *val = NULL;
7589 NT_PRINTER_DATA *p_data;
7590 int i, key_index, num_values;
7591 int name_length;
7593 *r->out.value_needed = 0;
7594 *r->out.type = REG_NONE;
7595 *r->out.data_needed = 0;
7597 DEBUG(5,("_spoolss_EnumPrinterData\n"));
7599 if (!Printer) {
7600 DEBUG(2,("_spoolss_EnumPrinterData: Invalid handle (%s:%u:%u).\n",
7601 OUR_HANDLE(r->in.handle)));
7602 return WERR_BADFID;
7605 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
7606 return WERR_BADFID;
7609 result = get_a_printer(Printer, &printer, 2, lp_const_servicename(snum));
7610 if (!W_ERROR_IS_OK(result)) {
7611 return result;
7614 p_data = printer->info_2->data;
7615 key_index = lookup_printerkey( p_data, SPOOL_PRINTERDATA_KEY );
7617 result = WERR_OK;
7620 * The NT machine wants to know the biggest size of value and data
7622 * cf: MSDN EnumPrinterData remark section
7625 if (!r->in.value_offered && !r->in.data_offered && (key_index != -1)) {
7627 uint32_t biggest_valuesize = 0;
7628 uint32_t biggest_datasize = 0;
7630 DEBUGADD(6,("Activating NT mega-hack to find sizes\n"));
7632 num_values = regval_ctr_numvals( p_data->keys[key_index].values );
7634 for ( i=0; i<num_values; i++ )
7636 val = regval_ctr_specific_value( p_data->keys[key_index].values, i );
7638 name_length = strlen(val->valuename);
7639 if ( strlen(val->valuename) > biggest_valuesize )
7640 biggest_valuesize = name_length;
7642 if ( val->size > biggest_datasize )
7643 biggest_datasize = val->size;
7645 DEBUG(6,("current values: [%d], [%d]\n", biggest_valuesize,
7646 biggest_datasize));
7649 /* the value is an UNICODE string but real_value_size is the length
7650 in bytes including the trailing 0 */
7652 *r->out.value_needed = 2 * (1 + biggest_valuesize);
7653 *r->out.data_needed = biggest_datasize;
7655 DEBUG(6,("final values: [%d], [%d]\n",
7656 *r->out.value_needed, *r->out.data_needed));
7658 goto done;
7662 * the value len is wrong in NT sp3
7663 * that's the number of bytes not the number of unicode chars
7666 if (key_index != -1) {
7667 val = regval_ctr_specific_value(p_data->keys[key_index].values,
7668 r->in.enum_index);
7671 if (!val) {
7673 /* out_value should default to "" or else NT4 has
7674 problems unmarshalling the response */
7676 if (r->in.value_offered) {
7677 *r->out.value_needed = 1;
7678 r->out.value_name = talloc_strdup(r, "");
7679 if (!r->out.value_name) {
7680 result = WERR_NOMEM;
7681 goto done;
7683 } else {
7684 r->out.value_name = NULL;
7685 *r->out.value_needed = 0;
7688 /* the data is counted in bytes */
7690 *r->out.data_needed = r->in.data_offered;
7692 result = WERR_NO_MORE_ITEMS;
7693 } else {
7695 * the value is:
7696 * - counted in bytes in the request
7697 * - counted in UNICODE chars in the max reply
7698 * - counted in bytes in the real size
7700 * take a pause *before* coding not *during* coding
7703 /* name */
7704 if (r->in.value_offered) {
7705 r->out.value_name = talloc_strdup(r, regval_name(val));
7706 if (!r->out.value_name) {
7707 result = WERR_NOMEM;
7708 goto done;
7710 *r->out.value_needed = strlen_m_term(regval_name(val)) * 2;
7711 } else {
7712 r->out.value_name = NULL;
7713 *r->out.value_needed = 0;
7716 /* type */
7718 *r->out.type = regval_type(val);
7720 /* data - counted in bytes */
7723 * See the section "Dynamically Typed Query Parameters"
7724 * in MS-RPRN.
7727 if (r->out.data && regval_data_p(val) &&
7728 regval_size(val) && r->in.data_offered) {
7729 memcpy(r->out.data, regval_data_p(val),
7730 MIN(regval_size(val),r->in.data_offered));
7733 *r->out.data_needed = regval_size(val);
7736 done:
7737 free_a_printer(&printer, 2);
7738 return result;
7741 /****************************************************************
7742 _spoolss_SetPrinterData
7743 ****************************************************************/
7745 WERROR _spoolss_SetPrinterData(pipes_struct *p,
7746 struct spoolss_SetPrinterData *r)
7748 struct spoolss_SetPrinterDataEx r2;
7750 r2.in.handle = r->in.handle;
7751 r2.in.key_name = "PrinterDriverData";
7752 r2.in.value_name = r->in.value_name;
7753 r2.in.type = r->in.type;
7754 r2.in.data = r->in.data;
7755 r2.in.offered = r->in.offered;
7757 return _spoolss_SetPrinterDataEx(p, &r2);
7760 /****************************************************************
7761 _spoolss_ResetPrinter
7762 ****************************************************************/
7764 WERROR _spoolss_ResetPrinter(pipes_struct *p,
7765 struct spoolss_ResetPrinter *r)
7767 Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
7768 int snum;
7770 DEBUG(5,("_spoolss_ResetPrinter\n"));
7773 * All we do is to check to see if the handle and queue is valid.
7774 * This call really doesn't mean anything to us because we only
7775 * support RAW printing. --jerry
7778 if (!Printer) {
7779 DEBUG(2,("_spoolss_ResetPrinter: Invalid handle (%s:%u:%u).\n",
7780 OUR_HANDLE(r->in.handle)));
7781 return WERR_BADFID;
7784 if (!get_printer_snum(p, r->in.handle, &snum, NULL))
7785 return WERR_BADFID;
7788 /* blindly return success */
7789 return WERR_OK;
7792 /****************************************************************
7793 _spoolss_DeletePrinterData
7794 ****************************************************************/
7796 WERROR _spoolss_DeletePrinterData(pipes_struct *p,
7797 struct spoolss_DeletePrinterData *r)
7799 struct spoolss_DeletePrinterDataEx r2;
7801 r2.in.handle = r->in.handle;
7802 r2.in.key_name = "PrinterDriverData";
7803 r2.in.value_name = r->in.value_name;
7805 return _spoolss_DeletePrinterDataEx(p, &r2);
7808 /****************************************************************
7809 _spoolss_AddForm
7810 ****************************************************************/
7812 WERROR _spoolss_AddForm(pipes_struct *p,
7813 struct spoolss_AddForm *r)
7815 struct spoolss_AddFormInfo1 *form = r->in.info.info1;
7816 nt_forms_struct tmpForm;
7817 int snum = -1;
7818 WERROR status = WERR_OK;
7819 NT_PRINTER_INFO_LEVEL *printer = NULL;
7820 SE_PRIV se_printop = SE_PRINT_OPERATOR;
7822 int count=0;
7823 nt_forms_struct *list=NULL;
7824 Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
7826 DEBUG(5,("_spoolss_AddForm\n"));
7828 if (!Printer) {
7829 DEBUG(2,("_spoolss_AddForm: Invalid handle (%s:%u:%u).\n",
7830 OUR_HANDLE(r->in.handle)));
7831 return WERR_BADFID;
7835 /* forms can be added on printer of on the print server handle */
7837 if ( Printer->printer_type == SPLHND_PRINTER )
7839 if (!get_printer_snum(p, r->in.handle, &snum, NULL))
7840 return WERR_BADFID;
7842 status = get_a_printer(Printer, &printer, 2, lp_const_servicename(snum));
7843 if (!W_ERROR_IS_OK(status))
7844 goto done;
7847 /* if the user is not root, doesn't have SE_PRINT_OPERATOR privilege,
7848 and not a printer admin, then fail */
7850 if ((p->server_info->utok.uid != sec_initial_uid()) &&
7851 !user_has_privileges(p->server_info->ptok, &se_printop) &&
7852 !token_contains_name_in_list(uidtoname(p->server_info->utok.uid),
7853 pdb_get_domain(p->server_info->sam_account),
7854 NULL,
7855 p->server_info->ptok,
7856 lp_printer_admin(snum))) {
7857 DEBUG(2,("_spoolss_Addform: denied by insufficient permissions.\n"));
7858 return WERR_ACCESS_DENIED;
7861 /* can't add if builtin */
7863 if (get_a_builtin_ntform_by_string(form->form_name, &tmpForm)) {
7864 status = WERR_FILE_EXISTS;
7865 goto done;
7868 count = get_ntforms(&list);
7870 if(!add_a_form(&list, form, &count)) {
7871 status = WERR_NOMEM;
7872 goto done;
7875 become_root();
7876 write_ntforms(&list, count);
7877 unbecome_root();
7880 * ChangeID must always be set if this is a printer
7883 if ( Printer->printer_type == SPLHND_PRINTER )
7884 status = mod_a_printer(printer, 2);
7886 done:
7887 if ( printer )
7888 free_a_printer(&printer, 2);
7889 SAFE_FREE(list);
7891 return status;
7894 /****************************************************************
7895 _spoolss_DeleteForm
7896 ****************************************************************/
7898 WERROR _spoolss_DeleteForm(pipes_struct *p,
7899 struct spoolss_DeleteForm *r)
7901 const char *form_name = r->in.form_name;
7902 nt_forms_struct tmpForm;
7903 int count=0;
7904 nt_forms_struct *list=NULL;
7905 Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
7906 int snum = -1;
7907 WERROR status = WERR_OK;
7908 NT_PRINTER_INFO_LEVEL *printer = NULL;
7909 SE_PRIV se_printop = SE_PRINT_OPERATOR;
7910 bool ret = false;
7912 DEBUG(5,("_spoolss_DeleteForm\n"));
7914 if (!Printer) {
7915 DEBUG(2,("_spoolss_DeleteForm: Invalid handle (%s:%u:%u).\n",
7916 OUR_HANDLE(r->in.handle)));
7917 return WERR_BADFID;
7920 /* forms can be deleted on printer of on the print server handle */
7922 if ( Printer->printer_type == SPLHND_PRINTER )
7924 if (!get_printer_snum(p, r->in.handle, &snum, NULL))
7925 return WERR_BADFID;
7927 status = get_a_printer(Printer, &printer, 2, lp_const_servicename(snum));
7928 if (!W_ERROR_IS_OK(status))
7929 goto done;
7932 if ((p->server_info->utok.uid != sec_initial_uid()) &&
7933 !user_has_privileges(p->server_info->ptok, &se_printop) &&
7934 !token_contains_name_in_list(uidtoname(p->server_info->utok.uid),
7935 pdb_get_domain(p->server_info->sam_account),
7936 NULL,
7937 p->server_info->ptok,
7938 lp_printer_admin(snum))) {
7939 DEBUG(2,("_spoolss_DeleteForm: denied by insufficient permissions.\n"));
7940 return WERR_ACCESS_DENIED;
7944 /* can't delete if builtin */
7946 if (get_a_builtin_ntform_by_string(form_name,&tmpForm)) {
7947 status = WERR_INVALID_PARAM;
7948 goto done;
7951 count = get_ntforms(&list);
7953 become_root();
7954 ret = delete_a_form(&list, form_name, &count, &status);
7955 unbecome_root();
7956 if (ret == false) {
7957 goto done;
7961 * ChangeID must always be set if this is a printer
7964 if ( Printer->printer_type == SPLHND_PRINTER )
7965 status = mod_a_printer(printer, 2);
7967 done:
7968 if ( printer )
7969 free_a_printer(&printer, 2);
7970 SAFE_FREE(list);
7972 return status;
7975 /****************************************************************
7976 _spoolss_SetForm
7977 ****************************************************************/
7979 WERROR _spoolss_SetForm(pipes_struct *p,
7980 struct spoolss_SetForm *r)
7982 struct spoolss_AddFormInfo1 *form = r->in.info.info1;
7983 nt_forms_struct tmpForm;
7984 int snum = -1;
7985 WERROR status = WERR_OK;
7986 NT_PRINTER_INFO_LEVEL *printer = NULL;
7987 SE_PRIV se_printop = SE_PRINT_OPERATOR;
7989 int count=0;
7990 nt_forms_struct *list=NULL;
7991 Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
7993 DEBUG(5,("_spoolss_SetForm\n"));
7995 if (!Printer) {
7996 DEBUG(2,("_spoolss_SetForm: Invalid handle (%s:%u:%u).\n",
7997 OUR_HANDLE(r->in.handle)));
7998 return WERR_BADFID;
8001 /* forms can be modified on printer of on the print server handle */
8003 if ( Printer->printer_type == SPLHND_PRINTER )
8005 if (!get_printer_snum(p, r->in.handle, &snum, NULL))
8006 return WERR_BADFID;
8008 status = get_a_printer(Printer, &printer, 2, lp_const_servicename(snum));
8009 if (!W_ERROR_IS_OK(status))
8010 goto done;
8013 /* if the user is not root, doesn't have SE_PRINT_OPERATOR privilege,
8014 and not a printer admin, then fail */
8016 if ((p->server_info->utok.uid != sec_initial_uid()) &&
8017 !user_has_privileges(p->server_info->ptok, &se_printop) &&
8018 !token_contains_name_in_list(uidtoname(p->server_info->utok.uid),
8019 pdb_get_domain(p->server_info->sam_account),
8020 NULL,
8021 p->server_info->ptok,
8022 lp_printer_admin(snum))) {
8023 DEBUG(2,("_spoolss_Setform: denied by insufficient permissions.\n"));
8024 return WERR_ACCESS_DENIED;
8027 /* can't set if builtin */
8028 if (get_a_builtin_ntform_by_string(form->form_name, &tmpForm)) {
8029 status = WERR_INVALID_PARAM;
8030 goto done;
8033 count = get_ntforms(&list);
8034 update_a_form(&list, form, count);
8035 become_root();
8036 write_ntforms(&list, count);
8037 unbecome_root();
8040 * ChangeID must always be set if this is a printer
8043 if ( Printer->printer_type == SPLHND_PRINTER )
8044 status = mod_a_printer(printer, 2);
8047 done:
8048 if ( printer )
8049 free_a_printer(&printer, 2);
8050 SAFE_FREE(list);
8052 return status;
8055 /****************************************************************************
8056 fill_print_processor1
8057 ****************************************************************************/
8059 static WERROR fill_print_processor1(TALLOC_CTX *mem_ctx,
8060 struct spoolss_PrintProcessorInfo1 *r,
8061 const char *print_processor_name)
8063 r->print_processor_name = talloc_strdup(mem_ctx, print_processor_name);
8064 W_ERROR_HAVE_NO_MEMORY(r->print_processor_name);
8066 return WERR_OK;
8069 /****************************************************************************
8070 enumprintprocessors level 1.
8071 ****************************************************************************/
8073 static WERROR enumprintprocessors_level_1(TALLOC_CTX *mem_ctx,
8074 union spoolss_PrintProcessorInfo **info_p,
8075 uint32_t *count)
8077 union spoolss_PrintProcessorInfo *info;
8078 WERROR result;
8080 info = TALLOC_ARRAY(mem_ctx, union spoolss_PrintProcessorInfo, 1);
8081 W_ERROR_HAVE_NO_MEMORY(info);
8083 *count = 1;
8085 result = fill_print_processor1(info, &info[0].info1, "winprint");
8086 if (!W_ERROR_IS_OK(result)) {
8087 goto out;
8090 out:
8091 if (!W_ERROR_IS_OK(result)) {
8092 TALLOC_FREE(info);
8093 *count = 0;
8094 return result;
8097 *info_p = info;
8099 return WERR_OK;
8102 /****************************************************************
8103 _spoolss_EnumPrintProcessors
8104 ****************************************************************/
8106 WERROR _spoolss_EnumPrintProcessors(pipes_struct *p,
8107 struct spoolss_EnumPrintProcessors *r)
8109 WERROR result;
8111 /* that's an [in out] buffer */
8113 if (!r->in.buffer && (r->in.offered != 0)) {
8114 return WERR_INVALID_PARAM;
8117 DEBUG(5,("_spoolss_EnumPrintProcessors\n"));
8120 * Enumerate the print processors ...
8122 * Just reply with "winprint", to keep NT happy
8123 * and I can use my nice printer checker.
8126 *r->out.count = 0;
8127 *r->out.needed = 0;
8128 *r->out.info = NULL;
8130 switch (r->in.level) {
8131 case 1:
8132 result = enumprintprocessors_level_1(p->mem_ctx, r->out.info,
8133 r->out.count);
8134 break;
8135 default:
8136 return WERR_UNKNOWN_LEVEL;
8139 if (!W_ERROR_IS_OK(result)) {
8140 return result;
8143 *r->out.needed = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
8144 spoolss_EnumPrintProcessors, NULL,
8145 *r->out.info, r->in.level,
8146 *r->out.count);
8147 *r->out.info = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
8148 *r->out.count = SPOOLSS_BUFFER_OK(*r->out.count, 0);
8150 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
8153 /****************************************************************************
8154 fill_printprocdatatype1
8155 ****************************************************************************/
8157 static WERROR fill_printprocdatatype1(TALLOC_CTX *mem_ctx,
8158 struct spoolss_PrintProcDataTypesInfo1 *r,
8159 const char *name_array)
8161 r->name_array = talloc_strdup(mem_ctx, name_array);
8162 W_ERROR_HAVE_NO_MEMORY(r->name_array);
8164 return WERR_OK;
8167 /****************************************************************************
8168 enumprintprocdatatypes level 1.
8169 ****************************************************************************/
8171 static WERROR enumprintprocdatatypes_level_1(TALLOC_CTX *mem_ctx,
8172 union spoolss_PrintProcDataTypesInfo **info_p,
8173 uint32_t *count)
8175 WERROR result;
8176 union spoolss_PrintProcDataTypesInfo *info;
8178 info = TALLOC_ARRAY(mem_ctx, union spoolss_PrintProcDataTypesInfo, 1);
8179 W_ERROR_HAVE_NO_MEMORY(info);
8181 *count = 1;
8183 result = fill_printprocdatatype1(info, &info[0].info1, "RAW");
8184 if (!W_ERROR_IS_OK(result)) {
8185 goto out;
8188 out:
8189 if (!W_ERROR_IS_OK(result)) {
8190 TALLOC_FREE(info);
8191 *count = 0;
8192 return result;
8195 *info_p = info;
8197 return WERR_OK;
8200 /****************************************************************
8201 _spoolss_EnumPrintProcDataTypes
8202 ****************************************************************/
8204 WERROR _spoolss_EnumPrintProcDataTypes(pipes_struct *p,
8205 struct spoolss_EnumPrintProcDataTypes *r)
8207 WERROR result;
8209 /* that's an [in out] buffer */
8211 if (!r->in.buffer && (r->in.offered != 0)) {
8212 return WERR_INVALID_PARAM;
8215 DEBUG(5,("_spoolss_EnumPrintProcDataTypes\n"));
8217 *r->out.count = 0;
8218 *r->out.needed = 0;
8219 *r->out.info = NULL;
8221 switch (r->in.level) {
8222 case 1:
8223 result = enumprintprocdatatypes_level_1(p->mem_ctx, r->out.info,
8224 r->out.count);
8225 break;
8226 default:
8227 return WERR_UNKNOWN_LEVEL;
8230 *r->out.needed = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
8231 spoolss_EnumPrintProcDataTypes, NULL,
8232 *r->out.info, r->in.level,
8233 *r->out.count);
8234 *r->out.info = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
8235 *r->out.count = SPOOLSS_BUFFER_OK(*r->out.count, 0);
8237 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
8240 /****************************************************************************
8241 fill_monitor_1
8242 ****************************************************************************/
8244 static WERROR fill_monitor_1(TALLOC_CTX *mem_ctx,
8245 struct spoolss_MonitorInfo1 *r,
8246 const char *monitor_name)
8248 r->monitor_name = talloc_strdup(mem_ctx, monitor_name);
8249 W_ERROR_HAVE_NO_MEMORY(r->monitor_name);
8251 return WERR_OK;
8254 /****************************************************************************
8255 fill_monitor_2
8256 ****************************************************************************/
8258 static WERROR fill_monitor_2(TALLOC_CTX *mem_ctx,
8259 struct spoolss_MonitorInfo2 *r,
8260 const char *monitor_name,
8261 const char *environment,
8262 const char *dll_name)
8264 r->monitor_name = talloc_strdup(mem_ctx, monitor_name);
8265 W_ERROR_HAVE_NO_MEMORY(r->monitor_name);
8266 r->environment = talloc_strdup(mem_ctx, environment);
8267 W_ERROR_HAVE_NO_MEMORY(r->environment);
8268 r->dll_name = talloc_strdup(mem_ctx, dll_name);
8269 W_ERROR_HAVE_NO_MEMORY(r->dll_name);
8271 return WERR_OK;
8274 /****************************************************************************
8275 enumprintmonitors level 1.
8276 ****************************************************************************/
8278 static WERROR enumprintmonitors_level_1(TALLOC_CTX *mem_ctx,
8279 union spoolss_MonitorInfo **info_p,
8280 uint32_t *count)
8282 union spoolss_MonitorInfo *info;
8283 WERROR result = WERR_OK;
8285 info = TALLOC_ARRAY(mem_ctx, union spoolss_MonitorInfo, 2);
8286 W_ERROR_HAVE_NO_MEMORY(info);
8288 *count = 2;
8290 result = fill_monitor_1(info, &info[0].info1,
8291 SPL_LOCAL_PORT);
8292 if (!W_ERROR_IS_OK(result)) {
8293 goto out;
8296 result = fill_monitor_1(info, &info[1].info1,
8297 SPL_TCPIP_PORT);
8298 if (!W_ERROR_IS_OK(result)) {
8299 goto out;
8302 out:
8303 if (!W_ERROR_IS_OK(result)) {
8304 TALLOC_FREE(info);
8305 *count = 0;
8306 return result;
8309 *info_p = info;
8311 return WERR_OK;
8314 /****************************************************************************
8315 enumprintmonitors level 2.
8316 ****************************************************************************/
8318 static WERROR enumprintmonitors_level_2(TALLOC_CTX *mem_ctx,
8319 union spoolss_MonitorInfo **info_p,
8320 uint32_t *count)
8322 union spoolss_MonitorInfo *info;
8323 WERROR result = WERR_OK;
8325 info = TALLOC_ARRAY(mem_ctx, union spoolss_MonitorInfo, 2);
8326 W_ERROR_HAVE_NO_MEMORY(info);
8328 *count = 2;
8330 result = fill_monitor_2(info, &info[0].info2,
8331 SPL_LOCAL_PORT,
8332 "Windows NT X86", /* FIXME */
8333 "localmon.dll");
8334 if (!W_ERROR_IS_OK(result)) {
8335 goto out;
8338 result = fill_monitor_2(info, &info[1].info2,
8339 SPL_TCPIP_PORT,
8340 "Windows NT X86", /* FIXME */
8341 "tcpmon.dll");
8342 if (!W_ERROR_IS_OK(result)) {
8343 goto out;
8346 out:
8347 if (!W_ERROR_IS_OK(result)) {
8348 TALLOC_FREE(info);
8349 *count = 0;
8350 return result;
8353 *info_p = info;
8355 return WERR_OK;
8358 /****************************************************************
8359 _spoolss_EnumMonitors
8360 ****************************************************************/
8362 WERROR _spoolss_EnumMonitors(pipes_struct *p,
8363 struct spoolss_EnumMonitors *r)
8365 WERROR result;
8367 /* that's an [in out] buffer */
8369 if (!r->in.buffer && (r->in.offered != 0)) {
8370 return WERR_INVALID_PARAM;
8373 DEBUG(5,("_spoolss_EnumMonitors\n"));
8376 * Enumerate the print monitors ...
8378 * Just reply with "Local Port", to keep NT happy
8379 * and I can use my nice printer checker.
8382 *r->out.count = 0;
8383 *r->out.needed = 0;
8384 *r->out.info = NULL;
8386 switch (r->in.level) {
8387 case 1:
8388 result = enumprintmonitors_level_1(p->mem_ctx, r->out.info,
8389 r->out.count);
8390 break;
8391 case 2:
8392 result = enumprintmonitors_level_2(p->mem_ctx, r->out.info,
8393 r->out.count);
8394 break;
8395 default:
8396 return WERR_UNKNOWN_LEVEL;
8399 if (!W_ERROR_IS_OK(result)) {
8400 return result;
8403 *r->out.needed = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
8404 spoolss_EnumMonitors, NULL,
8405 *r->out.info, r->in.level,
8406 *r->out.count);
8407 *r->out.info = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
8408 *r->out.count = SPOOLSS_BUFFER_OK(*r->out.count, 0);
8410 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
8413 /****************************************************************************
8414 ****************************************************************************/
8416 static WERROR getjob_level_1(TALLOC_CTX *mem_ctx,
8417 const print_queue_struct *queue,
8418 int count, int snum,
8419 const NT_PRINTER_INFO_LEVEL *ntprinter,
8420 uint32_t jobid,
8421 struct spoolss_JobInfo1 *r)
8423 int i = 0;
8424 bool found = false;
8426 for (i=0; i<count && found == false; i++) {
8427 if (queue[i].job == (int)jobid) {
8428 found = true;
8432 if (found == false) {
8433 /* NT treats not found as bad param... yet another bad choice */
8434 return WERR_INVALID_PARAM;
8437 return fill_job_info1(mem_ctx,
8439 &queue[i-1],
8441 snum,
8442 ntprinter);
8445 /****************************************************************************
8446 ****************************************************************************/
8448 static WERROR getjob_level_2(TALLOC_CTX *mem_ctx,
8449 const print_queue_struct *queue,
8450 int count, int snum,
8451 const NT_PRINTER_INFO_LEVEL *ntprinter,
8452 uint32_t jobid,
8453 struct spoolss_JobInfo2 *r)
8455 int i = 0;
8456 bool found = false;
8457 struct spoolss_DeviceMode *devmode;
8458 NT_DEVICEMODE *nt_devmode;
8459 WERROR result;
8461 for (i=0; i<count && found == false; i++) {
8462 if (queue[i].job == (int)jobid) {
8463 found = true;
8467 if (found == false) {
8468 /* NT treats not found as bad param... yet another bad
8469 choice */
8470 return WERR_INVALID_PARAM;
8474 * if the print job does not have a DEVMODE associated with it,
8475 * just use the one for the printer. A NULL devicemode is not
8476 * a failure condition
8479 nt_devmode = print_job_devmode(lp_const_servicename(snum), jobid);
8480 if (nt_devmode) {
8481 devmode = TALLOC_ZERO_P(mem_ctx, struct spoolss_DeviceMode);
8482 W_ERROR_HAVE_NO_MEMORY(devmode);
8483 result = convert_nt_devicemode(devmode, devmode, nt_devmode);
8484 if (!W_ERROR_IS_OK(result)) {
8485 return result;
8487 } else {
8488 devmode = construct_dev_mode(mem_ctx, lp_const_servicename(snum));
8489 W_ERROR_HAVE_NO_MEMORY(devmode);
8492 return fill_job_info2(mem_ctx,
8494 &queue[i-1],
8496 snum,
8497 ntprinter,
8498 devmode);
8501 /****************************************************************
8502 _spoolss_GetJob
8503 ****************************************************************/
8505 WERROR _spoolss_GetJob(pipes_struct *p,
8506 struct spoolss_GetJob *r)
8508 WERROR result = WERR_OK;
8509 NT_PRINTER_INFO_LEVEL *ntprinter = NULL;
8510 int snum;
8511 int count;
8512 print_queue_struct *queue = NULL;
8513 print_status_struct prt_status;
8515 /* that's an [in out] buffer */
8517 if (!r->in.buffer && (r->in.offered != 0)) {
8518 return WERR_INVALID_PARAM;
8521 DEBUG(5,("_spoolss_GetJob\n"));
8523 *r->out.needed = 0;
8525 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
8526 return WERR_BADFID;
8529 result = get_a_printer(NULL, &ntprinter, 2, lp_servicename(snum));
8530 if (!W_ERROR_IS_OK(result)) {
8531 return result;
8534 count = print_queue_status(snum, &queue, &prt_status);
8536 DEBUGADD(4,("count:[%d], prt_status:[%d], [%s]\n",
8537 count, prt_status.status, prt_status.message));
8539 switch (r->in.level) {
8540 case 1:
8541 result = getjob_level_1(p->mem_ctx,
8542 queue, count, snum, ntprinter,
8543 r->in.job_id, &r->out.info->info1);
8544 break;
8545 case 2:
8546 result = getjob_level_2(p->mem_ctx,
8547 queue, count, snum, ntprinter,
8548 r->in.job_id, &r->out.info->info2);
8549 break;
8550 default:
8551 result = WERR_UNKNOWN_LEVEL;
8552 break;
8555 SAFE_FREE(queue);
8556 free_a_printer(&ntprinter, 2);
8558 if (!W_ERROR_IS_OK(result)) {
8559 TALLOC_FREE(r->out.info);
8560 return result;
8563 *r->out.needed = SPOOLSS_BUFFER_UNION(spoolss_JobInfo, NULL,
8564 r->out.info, r->in.level);
8565 r->out.info = SPOOLSS_BUFFER_OK(r->out.info, NULL);
8567 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
8570 /****************************************************************
8571 _spoolss_GetPrinterDataEx
8572 ****************************************************************/
8574 WERROR _spoolss_GetPrinterDataEx(pipes_struct *p,
8575 struct spoolss_GetPrinterDataEx *r)
8578 Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
8579 struct regval_blob *val = NULL;
8580 NT_PRINTER_INFO_LEVEL *printer = NULL;
8581 int snum = 0;
8582 WERROR result = WERR_OK;
8583 DATA_BLOB blob;
8585 DEBUG(4,("_spoolss_GetPrinterDataEx\n"));
8587 DEBUG(10, ("_spoolss_GetPrinterDataEx: key => [%s], value => [%s]\n",
8588 r->in.key_name, r->in.value_name));
8590 /* in case of problem, return some default values */
8592 *r->out.needed = 0;
8593 *r->out.type = REG_NONE;
8595 if (!Printer) {
8596 DEBUG(2,("_spoolss_GetPrinterDataEx: Invalid handle (%s:%u:%u).\n",
8597 OUR_HANDLE(r->in.handle)));
8598 result = WERR_BADFID;
8599 goto done;
8602 /* Is the handle to a printer or to the server? */
8604 if (Printer->printer_type == SPLHND_SERVER) {
8606 union spoolss_PrinterData data;
8608 result = getprinterdata_printer_server(p->mem_ctx,
8609 r->in.value_name,
8610 r->out.type,
8611 &data);
8612 if (!W_ERROR_IS_OK(result)) {
8613 goto done;
8616 result = push_spoolss_PrinterData(p->mem_ctx, &blob,
8617 *r->out.type, &data);
8618 if (!W_ERROR_IS_OK(result)) {
8619 goto done;
8622 *r->out.needed = blob.length;
8624 if (r->in.offered >= *r->out.needed) {
8625 memcpy(r->out.data, blob.data, blob.length);
8628 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_MORE_DATA);
8631 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
8632 result = WERR_BADFID;
8633 goto done;
8636 result = get_a_printer(Printer, &printer, 2, lp_servicename(snum));
8637 if (!W_ERROR_IS_OK(result)) {
8638 goto done;
8641 /* check to see if the keyname is valid */
8642 if (!strlen(r->in.key_name)) {
8643 result = WERR_INVALID_PARAM;
8644 goto done;
8647 /* XP sends this and wants to change id value from the PRINTER_INFO_0 */
8649 if (strequal(r->in.key_name, SPOOL_PRINTERDATA_KEY) &&
8650 strequal(r->in.value_name, "ChangeId")) {
8651 *r->out.type = REG_DWORD;
8652 *r->out.needed = 4;
8653 if (r->in.offered >= *r->out.needed) {
8654 SIVAL(r->out.data, 0, printer->info_2->changeid);
8655 result = WERR_OK;
8657 goto done;
8660 if (lookup_printerkey(printer->info_2->data, r->in.key_name) == -1) {
8661 DEBUG(4,("_spoolss_GetPrinterDataEx: "
8662 "Invalid keyname [%s]\n", r->in.key_name ));
8663 result = WERR_BADFILE;
8664 goto done;
8667 val = get_printer_data(printer->info_2,
8668 r->in.key_name, r->in.value_name);
8669 if (!val) {
8670 result = WERR_BADFILE;
8671 goto done;
8674 *r->out.needed = regval_size(val);
8675 *r->out.type = regval_type(val);
8677 if (r->in.offered >= *r->out.needed) {
8678 memcpy(r->out.data, regval_data_p(val), regval_size(val));
8680 done:
8681 if (printer) {
8682 free_a_printer(&printer, 2);
8685 if (!W_ERROR_IS_OK(result)) {
8686 return result;
8689 *r->out.type = SPOOLSS_BUFFER_OK(*r->out.type, REG_NONE);
8690 r->out.data = SPOOLSS_BUFFER_OK(r->out.data, r->out.data);
8692 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_MORE_DATA);
8695 /****************************************************************
8696 _spoolss_SetPrinterDataEx
8697 ****************************************************************/
8699 WERROR _spoolss_SetPrinterDataEx(pipes_struct *p,
8700 struct spoolss_SetPrinterDataEx *r)
8702 NT_PRINTER_INFO_LEVEL *printer = NULL;
8703 int snum = 0;
8704 WERROR result = WERR_OK;
8705 Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
8706 char *oid_string;
8708 DEBUG(4,("_spoolss_SetPrinterDataEx\n"));
8710 /* From MSDN documentation of SetPrinterDataEx: pass request to
8711 SetPrinterData if key is "PrinterDriverData" */
8713 if (!Printer) {
8714 DEBUG(2,("_spoolss_SetPrinterDataEx: Invalid handle (%s:%u:%u).\n",
8715 OUR_HANDLE(r->in.handle)));
8716 return WERR_BADFID;
8719 if (Printer->printer_type == SPLHND_SERVER) {
8720 DEBUG(10,("_spoolss_SetPrinterDataEx: "
8721 "Not implemented for server handles yet\n"));
8722 return WERR_INVALID_PARAM;
8725 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
8726 return WERR_BADFID;
8730 * Access check : NT returns "access denied" if you make a
8731 * SetPrinterData call without the necessary privildge.
8732 * we were originally returning OK if nothing changed
8733 * which made Win2k issue **a lot** of SetPrinterData
8734 * when connecting to a printer --jerry
8737 if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
8738 DEBUG(3, ("_spoolss_SetPrinterDataEx: "
8739 "change denied by handle access permissions\n"));
8740 return WERR_ACCESS_DENIED;
8743 result = get_a_printer(Printer, &printer, 2, lp_servicename(snum));
8744 if (!W_ERROR_IS_OK(result)) {
8745 return result;
8748 /* check for OID in valuename */
8750 oid_string = strchr(r->in.value_name, ',');
8751 if (oid_string) {
8752 *oid_string = '\0';
8753 oid_string++;
8757 * When client side code sets a magic printer data key, detect it and save
8758 * the current printer data and the magic key's data (its the DEVMODE) for
8759 * future printer/driver initializations.
8761 if ((r->in.type == REG_BINARY) && strequal(r->in.value_name, PHANTOM_DEVMODE_KEY)) {
8762 /* Set devmode and printer initialization info */
8763 result = save_driver_init(printer, 2, r->in.data, r->in.offered);
8765 srv_spoolss_reset_printerdata(printer->info_2->drivername);
8767 goto done;
8770 /* save the registry data */
8772 result = set_printer_dataex(printer, r->in.key_name, r->in.value_name,
8773 r->in.type, r->in.data, r->in.offered);
8775 if (W_ERROR_IS_OK(result)) {
8776 /* save the OID if one was specified */
8777 if (oid_string) {
8778 char *str = talloc_asprintf(p->mem_ctx, "%s\\%s",
8779 r->in.key_name, SPOOL_OID_KEY);
8780 if (!str) {
8781 result = WERR_NOMEM;
8782 goto done;
8786 * I'm not checking the status here on purpose. Don't know
8787 * if this is right, but I'm returning the status from the
8788 * previous set_printer_dataex() call. I have no idea if
8789 * this is right. --jerry
8792 set_printer_dataex(printer, str, r->in.value_name,
8793 REG_SZ, (uint8_t *)oid_string,
8794 strlen(oid_string)+1);
8797 result = mod_a_printer(printer, 2);
8800 done:
8801 free_a_printer(&printer, 2);
8803 return result;
8806 /****************************************************************
8807 _spoolss_DeletePrinterDataEx
8808 ****************************************************************/
8810 WERROR _spoolss_DeletePrinterDataEx(pipes_struct *p,
8811 struct spoolss_DeletePrinterDataEx *r)
8813 NT_PRINTER_INFO_LEVEL *printer = NULL;
8814 int snum=0;
8815 WERROR status = WERR_OK;
8816 Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
8818 DEBUG(5,("_spoolss_DeletePrinterDataEx\n"));
8820 if (!Printer) {
8821 DEBUG(2,("_spoolss_DeletePrinterDataEx: "
8822 "Invalid handle (%s:%u:%u).\n",
8823 OUR_HANDLE(r->in.handle)));
8824 return WERR_BADFID;
8827 if (!get_printer_snum(p, r->in.handle, &snum, NULL))
8828 return WERR_BADFID;
8830 if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
8831 DEBUG(3, ("_spoolss_DeletePrinterDataEx: "
8832 "printer properties change denied by handle\n"));
8833 return WERR_ACCESS_DENIED;
8836 if (!r->in.value_name || !r->in.key_name) {
8837 return WERR_NOMEM;
8840 status = get_a_printer(Printer, &printer, 2, lp_const_servicename(snum));
8841 if (!W_ERROR_IS_OK(status))
8842 return status;
8844 status = delete_printer_dataex( printer, r->in.key_name, r->in.value_name );
8846 if ( W_ERROR_IS_OK(status) )
8847 mod_a_printer( printer, 2 );
8849 free_a_printer(&printer, 2);
8851 return status;
8854 /****************************************************************
8855 _spoolss_EnumPrinterKey
8856 ****************************************************************/
8858 WERROR _spoolss_EnumPrinterKey(pipes_struct *p,
8859 struct spoolss_EnumPrinterKey *r)
8861 fstring *keynames = NULL;
8862 int num_keys;
8863 Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
8864 NT_PRINTER_DATA *data;
8865 NT_PRINTER_INFO_LEVEL *printer = NULL;
8866 int snum = 0;
8867 WERROR result = WERR_BADFILE;
8868 int i;
8869 const char **array = NULL;
8870 DATA_BLOB blob;
8872 DEBUG(4,("_spoolss_EnumPrinterKey\n"));
8874 if (!Printer) {
8875 DEBUG(2,("_spoolss_EnumPrinterKey: Invalid handle (%s:%u:%u).\n",
8876 OUR_HANDLE(r->in.handle)));
8877 return WERR_BADFID;
8880 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
8881 return WERR_BADFID;
8884 result = get_a_printer(Printer, &printer, 2, lp_const_servicename(snum));
8885 if (!W_ERROR_IS_OK(result)) {
8886 return result;
8889 /* get the list of subkey names */
8891 data = printer->info_2->data;
8893 num_keys = get_printer_subkeys(data, r->in.key_name, &keynames);
8894 if (num_keys == -1) {
8895 result = WERR_BADFILE;
8896 goto done;
8899 array = talloc_zero_array(r->out.key_buffer, const char *, num_keys + 2);
8900 if (!array) {
8901 result = WERR_NOMEM;
8902 goto done;
8905 if (!num_keys) {
8906 array[0] = talloc_strdup(array, "");
8907 if (!array[0]) {
8908 result = WERR_NOMEM;
8909 goto done;
8913 for (i=0; i < num_keys; i++) {
8915 DEBUG(10,("_spoolss_EnumPrinterKey: adding keyname: %s\n",
8916 keynames[i]));
8918 array[i] = talloc_strdup(array, keynames[i]);
8919 if (!array[i]) {
8920 result = WERR_NOMEM;
8921 goto done;
8925 if (!push_reg_multi_sz(p->mem_ctx, &blob, array)) {
8926 result = WERR_NOMEM;
8927 goto done;
8930 *r->out._ndr_size = r->in.offered / 2;
8931 *r->out.needed = blob.length;
8933 if (r->in.offered < *r->out.needed) {
8934 result = WERR_MORE_DATA;
8935 } else {
8936 result = WERR_OK;
8937 r->out.key_buffer->string_array = array;
8940 done:
8941 if (!W_ERROR_IS_OK(result)) {
8942 TALLOC_FREE(array);
8943 if (!W_ERROR_EQUAL(result, WERR_MORE_DATA)) {
8944 *r->out.needed = 0;
8948 free_a_printer(&printer, 2);
8949 SAFE_FREE(keynames);
8951 return result;
8954 /****************************************************************
8955 _spoolss_DeletePrinterKey
8956 ****************************************************************/
8958 WERROR _spoolss_DeletePrinterKey(pipes_struct *p,
8959 struct spoolss_DeletePrinterKey *r)
8961 Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
8962 NT_PRINTER_INFO_LEVEL *printer = NULL;
8963 int snum=0;
8964 WERROR status;
8966 DEBUG(5,("_spoolss_DeletePrinterKey\n"));
8968 if (!Printer) {
8969 DEBUG(2,("_spoolss_DeletePrinterKey: Invalid handle (%s:%u:%u).\n",
8970 OUR_HANDLE(r->in.handle)));
8971 return WERR_BADFID;
8974 /* if keyname == NULL, return error */
8976 if ( !r->in.key_name )
8977 return WERR_INVALID_PARAM;
8979 if (!get_printer_snum(p, r->in.handle, &snum, NULL))
8980 return WERR_BADFID;
8982 if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
8983 DEBUG(3, ("_spoolss_DeletePrinterKey: "
8984 "printer properties change denied by handle\n"));
8985 return WERR_ACCESS_DENIED;
8988 status = get_a_printer(Printer, &printer, 2, lp_const_servicename(snum));
8989 if (!W_ERROR_IS_OK(status))
8990 return status;
8992 /* delete the key and all subneys */
8994 status = delete_all_printer_data( printer->info_2, r->in.key_name );
8996 if ( W_ERROR_IS_OK(status) )
8997 status = mod_a_printer(printer, 2);
8999 free_a_printer( &printer, 2 );
9001 return status;
9004 /****************************************************************
9005 ****************************************************************/
9007 static WERROR registry_value_to_printer_enum_value(TALLOC_CTX *mem_ctx,
9008 struct regval_blob *v,
9009 struct spoolss_PrinterEnumValues *r)
9011 r->data = TALLOC_ZERO_P(mem_ctx, DATA_BLOB);
9012 W_ERROR_HAVE_NO_MEMORY(r->data);
9014 r->value_name = talloc_strdup(mem_ctx, regval_name(v));
9015 W_ERROR_HAVE_NO_MEMORY(r->value_name);
9017 r->type = regval_type(v);
9018 r->data_length = regval_size(v);
9020 if (r->data_length) {
9021 *r->data = data_blob_talloc(r->data, regval_data_p(v), regval_size(v));
9024 return WERR_OK;
9027 /****************************************************************
9028 _spoolss_EnumPrinterDataEx
9029 ****************************************************************/
9031 WERROR _spoolss_EnumPrinterDataEx(pipes_struct *p,
9032 struct spoolss_EnumPrinterDataEx *r)
9034 uint32_t count = 0;
9035 NT_PRINTER_INFO_LEVEL *printer = NULL;
9036 struct spoolss_PrinterEnumValues *info = NULL;
9037 NT_PRINTER_DATA *p_data;
9038 Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
9039 int snum;
9040 WERROR result;
9041 int key_index;
9042 int i;
9044 DEBUG(4,("_spoolss_EnumPrinterDataEx\n"));
9046 *r->out.count = 0;
9047 *r->out.needed = 0;
9048 *r->out.info = NULL;
9050 if (!Printer) {
9051 DEBUG(2,("_spoolss_EnumPrinterDataEx: Invalid handle (%s:%u:%u1<).\n",
9052 OUR_HANDLE(r->in.handle)));
9053 return WERR_BADFID;
9057 * first check for a keyname of NULL or "". Win2k seems to send
9058 * this a lot and we should send back WERR_INVALID_PARAM
9059 * no need to spend time looking up the printer in this case.
9060 * --jerry
9063 if (!strlen(r->in.key_name)) {
9064 result = WERR_INVALID_PARAM;
9065 goto done;
9068 /* get the printer off of disk */
9070 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
9071 return WERR_BADFID;
9074 ZERO_STRUCT(printer);
9075 result = get_a_printer(Printer, &printer, 2, lp_const_servicename(snum));
9076 if (!W_ERROR_IS_OK(result)) {
9077 return result;
9080 /* now look for a match on the key name */
9082 p_data = printer->info_2->data;
9084 key_index = lookup_printerkey(p_data, r->in.key_name);
9085 if (key_index == -1) {
9086 DEBUG(10,("_spoolss_EnumPrinterDataEx: Unknown keyname [%s]\n",
9087 r->in.key_name));
9088 result = WERR_INVALID_PARAM;
9089 goto done;
9092 /* allocate the memory for the array of pointers -- if necessary */
9094 count = regval_ctr_numvals(p_data->keys[key_index].values);
9095 if (!count) {
9096 result = WERR_OK; /* ??? */
9097 goto done;
9100 info = TALLOC_ZERO_ARRAY(p->mem_ctx,
9101 struct spoolss_PrinterEnumValues,
9102 count);
9103 if (!info) {
9104 DEBUG(0,("_spoolss_EnumPrinterDataEx: talloc() failed\n"));
9105 result = WERR_NOMEM;
9106 goto done;
9110 * loop through all params and build the array to pass
9111 * back to the client
9114 for (i=0; i < count; i++) {
9116 struct regval_blob *val;
9118 /* lookup the registry value */
9120 val = regval_ctr_specific_value(p_data->keys[key_index].values, i);
9122 DEBUG(10,("retrieved value number [%d] [%s]\n", i, regval_name(val)));
9124 /* copy the data */
9126 result = registry_value_to_printer_enum_value(info, val, &info[i]);
9127 if (!W_ERROR_IS_OK(result)) {
9128 goto done;
9132 #if 0 /* FIXME - gd */
9133 /* housekeeping information in the reply */
9135 /* Fix from Martin Zielinski <mz@seh.de> - ensure
9136 * the hand marshalled container size is a multiple
9137 * of 4 bytes for RPC alignment.
9140 if (needed % 4) {
9141 needed += 4-(needed % 4);
9143 #endif
9144 *r->out.count = count;
9145 *r->out.info = info;
9147 done:
9149 if (printer) {
9150 free_a_printer(&printer, 2);
9153 if (!W_ERROR_IS_OK(result)) {
9154 return result;
9157 *r->out.needed = SPOOLSS_BUFFER_ARRAY(p->mem_ctx,
9158 spoolss_EnumPrinterDataEx, NULL,
9159 *r->out.info,
9160 *r->out.count);
9161 *r->out.info = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
9162 *r->out.count = SPOOLSS_BUFFER_OK(*r->out.count, *r->out.count);
9164 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_MORE_DATA);
9167 /****************************************************************************
9168 ****************************************************************************/
9170 static WERROR getprintprocessordirectory_level_1(TALLOC_CTX *mem_ctx,
9171 const char *servername,
9172 const char *environment,
9173 struct spoolss_PrintProcessorDirectoryInfo1 *r)
9175 WERROR werr;
9176 char *path = NULL;
9178 werr = compose_spoolss_server_path(mem_ctx,
9179 servername,
9180 environment,
9181 SPOOLSS_PRTPROCS_PATH,
9182 &path);
9183 if (!W_ERROR_IS_OK(werr)) {
9184 return werr;
9187 DEBUG(4,("print processor directory: [%s]\n", path));
9189 r->directory_name = path;
9191 return WERR_OK;
9194 /****************************************************************
9195 _spoolss_GetPrintProcessorDirectory
9196 ****************************************************************/
9198 WERROR _spoolss_GetPrintProcessorDirectory(pipes_struct *p,
9199 struct spoolss_GetPrintProcessorDirectory *r)
9201 WERROR result;
9203 /* that's an [in out] buffer */
9205 if (!r->in.buffer && (r->in.offered != 0)) {
9206 return WERR_INVALID_PARAM;
9209 DEBUG(5,("_spoolss_GetPrintProcessorDirectory: level %d\n",
9210 r->in.level));
9212 *r->out.needed = 0;
9214 /* r->in.level is ignored */
9216 /* We always should reply with a local print processor directory so that
9217 * users are not forced to have a [prnproc$] share on the Samba spoolss
9218 * server - Guenther */
9220 result = getprintprocessordirectory_level_1(p->mem_ctx,
9221 NULL, /* r->in.server */
9222 r->in.environment,
9223 &r->out.info->info1);
9224 if (!W_ERROR_IS_OK(result)) {
9225 TALLOC_FREE(r->out.info);
9226 return result;
9229 *r->out.needed = SPOOLSS_BUFFER_UNION(spoolss_PrintProcessorDirectoryInfo, NULL,
9230 r->out.info, r->in.level);
9231 r->out.info = SPOOLSS_BUFFER_OK(r->out.info, NULL);
9233 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
9236 /*******************************************************************
9237 ********************************************************************/
9239 static bool push_monitorui_buf(TALLOC_CTX *mem_ctx, DATA_BLOB *buf,
9240 const char *dllname)
9242 enum ndr_err_code ndr_err;
9243 struct spoolss_MonitorUi ui;
9245 ui.dll_name = dllname;
9247 ndr_err = ndr_push_struct_blob(buf, mem_ctx, NULL, &ui,
9248 (ndr_push_flags_fn_t)ndr_push_spoolss_MonitorUi);
9249 if (NDR_ERR_CODE_IS_SUCCESS(ndr_err) && (DEBUGLEVEL >= 10)) {
9250 NDR_PRINT_DEBUG(spoolss_MonitorUi, &ui);
9252 return NDR_ERR_CODE_IS_SUCCESS(ndr_err);
9255 /*******************************************************************
9256 Streams the monitor UI DLL name in UNICODE
9257 *******************************************************************/
9259 static WERROR xcvtcp_monitorui(TALLOC_CTX *mem_ctx,
9260 NT_USER_TOKEN *token, DATA_BLOB *in,
9261 DATA_BLOB *out, uint32_t *needed)
9263 const char *dllname = "tcpmonui.dll";
9265 *needed = (strlen(dllname)+1) * 2;
9267 if (out->length < *needed) {
9268 return WERR_INSUFFICIENT_BUFFER;
9271 if (!push_monitorui_buf(mem_ctx, out, dllname)) {
9272 return WERR_NOMEM;
9275 return WERR_OK;
9278 /*******************************************************************
9279 ********************************************************************/
9281 static bool pull_port_data_1(TALLOC_CTX *mem_ctx,
9282 struct spoolss_PortData1 *port1,
9283 const DATA_BLOB *buf)
9285 enum ndr_err_code ndr_err;
9286 ndr_err = ndr_pull_struct_blob(buf, mem_ctx, NULL, port1,
9287 (ndr_pull_flags_fn_t)ndr_pull_spoolss_PortData1);
9288 if (NDR_ERR_CODE_IS_SUCCESS(ndr_err) && (DEBUGLEVEL >= 10)) {
9289 NDR_PRINT_DEBUG(spoolss_PortData1, port1);
9291 return NDR_ERR_CODE_IS_SUCCESS(ndr_err);
9294 /*******************************************************************
9295 ********************************************************************/
9297 static bool pull_port_data_2(TALLOC_CTX *mem_ctx,
9298 struct spoolss_PortData2 *port2,
9299 const DATA_BLOB *buf)
9301 enum ndr_err_code ndr_err;
9302 ndr_err = ndr_pull_struct_blob(buf, mem_ctx, NULL, port2,
9303 (ndr_pull_flags_fn_t)ndr_pull_spoolss_PortData2);
9304 if (NDR_ERR_CODE_IS_SUCCESS(ndr_err) && (DEBUGLEVEL >= 10)) {
9305 NDR_PRINT_DEBUG(spoolss_PortData2, port2);
9307 return NDR_ERR_CODE_IS_SUCCESS(ndr_err);
9310 /*******************************************************************
9311 Create a new TCP/IP port
9312 *******************************************************************/
9314 static WERROR xcvtcp_addport(TALLOC_CTX *mem_ctx,
9315 NT_USER_TOKEN *token, DATA_BLOB *in,
9316 DATA_BLOB *out, uint32_t *needed)
9318 struct spoolss_PortData1 port1;
9319 struct spoolss_PortData2 port2;
9320 char *device_uri = NULL;
9321 uint32_t version;
9323 const char *portname;
9324 const char *hostaddress;
9325 const char *queue;
9326 uint32_t port_number;
9327 uint32_t protocol;
9329 /* peek for spoolss_PortData version */
9331 if (!in || (in->length < (128 + 4))) {
9332 return WERR_GENERAL_FAILURE;
9335 version = IVAL(in->data, 128);
9337 switch (version) {
9338 case 1:
9339 ZERO_STRUCT(port1);
9341 if (!pull_port_data_1(mem_ctx, &port1, in)) {
9342 return WERR_NOMEM;
9345 portname = port1.portname;
9346 hostaddress = port1.hostaddress;
9347 queue = port1.queue;
9348 protocol = port1.protocol;
9349 port_number = port1.port_number;
9351 break;
9352 case 2:
9353 ZERO_STRUCT(port2);
9355 if (!pull_port_data_2(mem_ctx, &port2, in)) {
9356 return WERR_NOMEM;
9359 portname = port2.portname;
9360 hostaddress = port2.hostaddress;
9361 queue = port2.queue;
9362 protocol = port2.protocol;
9363 port_number = port2.port_number;
9365 break;
9366 default:
9367 DEBUG(1,("xcvtcp_addport: "
9368 "unknown version of port_data: %d\n", version));
9369 return WERR_UNKNOWN_PORT;
9372 /* create the device URI and call the add_port_hook() */
9374 switch (protocol) {
9375 case PROTOCOL_RAWTCP_TYPE:
9376 device_uri = talloc_asprintf(mem_ctx,
9377 "socket://%s:%d/", hostaddress,
9378 port_number);
9379 break;
9381 case PROTOCOL_LPR_TYPE:
9382 device_uri = talloc_asprintf(mem_ctx,
9383 "lpr://%s/%s", hostaddress, queue );
9384 break;
9386 default:
9387 return WERR_UNKNOWN_PORT;
9390 if (!device_uri) {
9391 return WERR_NOMEM;
9394 return add_port_hook(mem_ctx, token, portname, device_uri);
9397 /*******************************************************************
9398 *******************************************************************/
9400 struct xcv_api_table xcvtcp_cmds[] = {
9401 { "MonitorUI", xcvtcp_monitorui },
9402 { "AddPort", xcvtcp_addport},
9403 { NULL, NULL }
9406 static WERROR process_xcvtcp_command(TALLOC_CTX *mem_ctx,
9407 NT_USER_TOKEN *token, const char *command,
9408 DATA_BLOB *inbuf,
9409 DATA_BLOB *outbuf,
9410 uint32_t *needed )
9412 int i;
9414 DEBUG(10,("process_xcvtcp_command: Received command \"%s\"\n", command));
9416 for ( i=0; xcvtcp_cmds[i].name; i++ ) {
9417 if ( strcmp( command, xcvtcp_cmds[i].name ) == 0 )
9418 return xcvtcp_cmds[i].fn(mem_ctx, token, inbuf, outbuf, needed);
9421 return WERR_BADFUNC;
9424 /*******************************************************************
9425 *******************************************************************/
9426 #if 0 /* don't support management using the "Local Port" monitor */
9428 static WERROR xcvlocal_monitorui(TALLOC_CTX *mem_ctx,
9429 NT_USER_TOKEN *token, DATA_BLOB *in,
9430 DATA_BLOB *out, uint32_t *needed)
9432 const char *dllname = "localui.dll";
9434 *needed = (strlen(dllname)+1) * 2;
9436 if (out->length < *needed) {
9437 return WERR_INSUFFICIENT_BUFFER;
9440 if (!push_monitorui_buf(mem_ctx, out, dllname)) {
9441 return WERR_NOMEM;
9444 return WERR_OK;
9447 /*******************************************************************
9448 *******************************************************************/
9450 struct xcv_api_table xcvlocal_cmds[] = {
9451 { "MonitorUI", xcvlocal_monitorui },
9452 { NULL, NULL }
9454 #else
9455 struct xcv_api_table xcvlocal_cmds[] = {
9456 { NULL, NULL }
9458 #endif
9462 /*******************************************************************
9463 *******************************************************************/
9465 static WERROR process_xcvlocal_command(TALLOC_CTX *mem_ctx,
9466 NT_USER_TOKEN *token, const char *command,
9467 DATA_BLOB *inbuf, DATA_BLOB *outbuf,
9468 uint32_t *needed)
9470 int i;
9472 DEBUG(10,("process_xcvlocal_command: Received command \"%s\"\n", command));
9474 for ( i=0; xcvlocal_cmds[i].name; i++ ) {
9475 if ( strcmp( command, xcvlocal_cmds[i].name ) == 0 )
9476 return xcvlocal_cmds[i].fn(mem_ctx, token, inbuf, outbuf, needed);
9478 return WERR_BADFUNC;
9481 /****************************************************************
9482 _spoolss_XcvData
9483 ****************************************************************/
9485 WERROR _spoolss_XcvData(pipes_struct *p,
9486 struct spoolss_XcvData *r)
9488 Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
9489 DATA_BLOB out_data = data_blob_null;
9490 WERROR werror;
9492 if (!Printer) {
9493 DEBUG(2,("_spoolss_XcvData: Invalid handle (%s:%u:%u).\n",
9494 OUR_HANDLE(r->in.handle)));
9495 return WERR_BADFID;
9498 /* Has to be a handle to the TCP/IP port monitor */
9500 if ( !(Printer->printer_type & (SPLHND_PORTMON_LOCAL|SPLHND_PORTMON_TCP)) ) {
9501 DEBUG(2,("_spoolss_XcvData: Call only valid for Port Monitors\n"));
9502 return WERR_BADFID;
9505 /* requires administrative access to the server */
9507 if ( !(Printer->access_granted & SERVER_ACCESS_ADMINISTER) ) {
9508 DEBUG(2,("_spoolss_XcvData: denied by handle permissions.\n"));
9509 return WERR_ACCESS_DENIED;
9512 /* Allocate the outgoing buffer */
9514 if (r->in.out_data_size) {
9515 out_data = data_blob_talloc_zero(p->mem_ctx, r->in.out_data_size);
9516 if (out_data.data == NULL) {
9517 return WERR_NOMEM;
9521 switch ( Printer->printer_type ) {
9522 case SPLHND_PORTMON_TCP:
9523 werror = process_xcvtcp_command(p->mem_ctx,
9524 p->server_info->ptok,
9525 r->in.function_name,
9526 &r->in.in_data, &out_data,
9527 r->out.needed);
9528 break;
9529 case SPLHND_PORTMON_LOCAL:
9530 werror = process_xcvlocal_command(p->mem_ctx,
9531 p->server_info->ptok,
9532 r->in.function_name,
9533 &r->in.in_data, &out_data,
9534 r->out.needed);
9535 break;
9536 default:
9537 werror = WERR_INVALID_PRINT_MONITOR;
9540 if (!W_ERROR_IS_OK(werror)) {
9541 return werror;
9544 *r->out.status_code = 0;
9546 if (r->out.out_data && out_data.data && r->in.out_data_size && out_data.length) {
9547 memcpy(r->out.out_data, out_data.data,
9548 MIN(r->in.out_data_size, out_data.length));
9551 return WERR_OK;
9554 /****************************************************************
9555 _spoolss_AddPrintProcessor
9556 ****************************************************************/
9558 WERROR _spoolss_AddPrintProcessor(pipes_struct *p,
9559 struct spoolss_AddPrintProcessor *r)
9561 /* for now, just indicate success and ignore the add. We'll
9562 automatically set the winprint processor for printer
9563 entries later. Used to debug the LexMark Optra S 1855 PCL
9564 driver --jerry */
9566 return WERR_OK;
9569 /****************************************************************
9570 _spoolss_AddPort
9571 ****************************************************************/
9573 WERROR _spoolss_AddPort(pipes_struct *p,
9574 struct spoolss_AddPort *r)
9576 /* do what w2k3 does */
9578 return WERR_NOT_SUPPORTED;
9581 /****************************************************************
9582 _spoolss_GetPrinterDriver
9583 ****************************************************************/
9585 WERROR _spoolss_GetPrinterDriver(pipes_struct *p,
9586 struct spoolss_GetPrinterDriver *r)
9588 p->rng_fault_state = true;
9589 return WERR_NOT_SUPPORTED;
9592 /****************************************************************
9593 _spoolss_ReadPrinter
9594 ****************************************************************/
9596 WERROR _spoolss_ReadPrinter(pipes_struct *p,
9597 struct spoolss_ReadPrinter *r)
9599 p->rng_fault_state = true;
9600 return WERR_NOT_SUPPORTED;
9603 /****************************************************************
9604 _spoolss_WaitForPrinterChange
9605 ****************************************************************/
9607 WERROR _spoolss_WaitForPrinterChange(pipes_struct *p,
9608 struct spoolss_WaitForPrinterChange *r)
9610 p->rng_fault_state = true;
9611 return WERR_NOT_SUPPORTED;
9614 /****************************************************************
9615 _spoolss_ConfigurePort
9616 ****************************************************************/
9618 WERROR _spoolss_ConfigurePort(pipes_struct *p,
9619 struct spoolss_ConfigurePort *r)
9621 p->rng_fault_state = true;
9622 return WERR_NOT_SUPPORTED;
9625 /****************************************************************
9626 _spoolss_DeletePort
9627 ****************************************************************/
9629 WERROR _spoolss_DeletePort(pipes_struct *p,
9630 struct spoolss_DeletePort *r)
9632 p->rng_fault_state = true;
9633 return WERR_NOT_SUPPORTED;
9636 /****************************************************************
9637 _spoolss_CreatePrinterIC
9638 ****************************************************************/
9640 WERROR _spoolss_CreatePrinterIC(pipes_struct *p,
9641 struct spoolss_CreatePrinterIC *r)
9643 p->rng_fault_state = true;
9644 return WERR_NOT_SUPPORTED;
9647 /****************************************************************
9648 _spoolss_PlayGDIScriptOnPrinterIC
9649 ****************************************************************/
9651 WERROR _spoolss_PlayGDIScriptOnPrinterIC(pipes_struct *p,
9652 struct spoolss_PlayGDIScriptOnPrinterIC *r)
9654 p->rng_fault_state = true;
9655 return WERR_NOT_SUPPORTED;
9658 /****************************************************************
9659 _spoolss_DeletePrinterIC
9660 ****************************************************************/
9662 WERROR _spoolss_DeletePrinterIC(pipes_struct *p,
9663 struct spoolss_DeletePrinterIC *r)
9665 p->rng_fault_state = true;
9666 return WERR_NOT_SUPPORTED;
9669 /****************************************************************
9670 _spoolss_AddPrinterConnection
9671 ****************************************************************/
9673 WERROR _spoolss_AddPrinterConnection(pipes_struct *p,
9674 struct spoolss_AddPrinterConnection *r)
9676 p->rng_fault_state = true;
9677 return WERR_NOT_SUPPORTED;
9680 /****************************************************************
9681 _spoolss_DeletePrinterConnection
9682 ****************************************************************/
9684 WERROR _spoolss_DeletePrinterConnection(pipes_struct *p,
9685 struct spoolss_DeletePrinterConnection *r)
9687 p->rng_fault_state = true;
9688 return WERR_NOT_SUPPORTED;
9691 /****************************************************************
9692 _spoolss_PrinterMessageBox
9693 ****************************************************************/
9695 WERROR _spoolss_PrinterMessageBox(pipes_struct *p,
9696 struct spoolss_PrinterMessageBox *r)
9698 p->rng_fault_state = true;
9699 return WERR_NOT_SUPPORTED;
9702 /****************************************************************
9703 _spoolss_AddMonitor
9704 ****************************************************************/
9706 WERROR _spoolss_AddMonitor(pipes_struct *p,
9707 struct spoolss_AddMonitor *r)
9709 p->rng_fault_state = true;
9710 return WERR_NOT_SUPPORTED;
9713 /****************************************************************
9714 _spoolss_DeleteMonitor
9715 ****************************************************************/
9717 WERROR _spoolss_DeleteMonitor(pipes_struct *p,
9718 struct spoolss_DeleteMonitor *r)
9720 p->rng_fault_state = true;
9721 return WERR_NOT_SUPPORTED;
9724 /****************************************************************
9725 _spoolss_DeletePrintProcessor
9726 ****************************************************************/
9728 WERROR _spoolss_DeletePrintProcessor(pipes_struct *p,
9729 struct spoolss_DeletePrintProcessor *r)
9731 p->rng_fault_state = true;
9732 return WERR_NOT_SUPPORTED;
9735 /****************************************************************
9736 _spoolss_AddPrintProvidor
9737 ****************************************************************/
9739 WERROR _spoolss_AddPrintProvidor(pipes_struct *p,
9740 struct spoolss_AddPrintProvidor *r)
9742 p->rng_fault_state = true;
9743 return WERR_NOT_SUPPORTED;
9746 /****************************************************************
9747 _spoolss_DeletePrintProvidor
9748 ****************************************************************/
9750 WERROR _spoolss_DeletePrintProvidor(pipes_struct *p,
9751 struct spoolss_DeletePrintProvidor *r)
9753 p->rng_fault_state = true;
9754 return WERR_NOT_SUPPORTED;
9757 /****************************************************************
9758 _spoolss_FindFirstPrinterChangeNotification
9759 ****************************************************************/
9761 WERROR _spoolss_FindFirstPrinterChangeNotification(pipes_struct *p,
9762 struct spoolss_FindFirstPrinterChangeNotification *r)
9764 p->rng_fault_state = true;
9765 return WERR_NOT_SUPPORTED;
9768 /****************************************************************
9769 _spoolss_FindNextPrinterChangeNotification
9770 ****************************************************************/
9772 WERROR _spoolss_FindNextPrinterChangeNotification(pipes_struct *p,
9773 struct spoolss_FindNextPrinterChangeNotification *r)
9775 p->rng_fault_state = true;
9776 return WERR_NOT_SUPPORTED;
9779 /****************************************************************
9780 _spoolss_RouterFindFirstPrinterChangeNotificationOld
9781 ****************************************************************/
9783 WERROR _spoolss_RouterFindFirstPrinterChangeNotificationOld(pipes_struct *p,
9784 struct spoolss_RouterFindFirstPrinterChangeNotificationOld *r)
9786 p->rng_fault_state = true;
9787 return WERR_NOT_SUPPORTED;
9790 /****************************************************************
9791 _spoolss_ReplyOpenPrinter
9792 ****************************************************************/
9794 WERROR _spoolss_ReplyOpenPrinter(pipes_struct *p,
9795 struct spoolss_ReplyOpenPrinter *r)
9797 p->rng_fault_state = true;
9798 return WERR_NOT_SUPPORTED;
9801 /****************************************************************
9802 _spoolss_RouterReplyPrinter
9803 ****************************************************************/
9805 WERROR _spoolss_RouterReplyPrinter(pipes_struct *p,
9806 struct spoolss_RouterReplyPrinter *r)
9808 p->rng_fault_state = true;
9809 return WERR_NOT_SUPPORTED;
9812 /****************************************************************
9813 _spoolss_ReplyClosePrinter
9814 ****************************************************************/
9816 WERROR _spoolss_ReplyClosePrinter(pipes_struct *p,
9817 struct spoolss_ReplyClosePrinter *r)
9819 p->rng_fault_state = true;
9820 return WERR_NOT_SUPPORTED;
9823 /****************************************************************
9824 _spoolss_AddPortEx
9825 ****************************************************************/
9827 WERROR _spoolss_AddPortEx(pipes_struct *p,
9828 struct spoolss_AddPortEx *r)
9830 p->rng_fault_state = true;
9831 return WERR_NOT_SUPPORTED;
9834 /****************************************************************
9835 _spoolss_RouterFindFirstPrinterChangeNotification
9836 ****************************************************************/
9838 WERROR _spoolss_RouterFindFirstPrinterChangeNotification(pipes_struct *p,
9839 struct spoolss_RouterFindFirstPrinterChangeNotification *r)
9841 p->rng_fault_state = true;
9842 return WERR_NOT_SUPPORTED;
9845 /****************************************************************
9846 _spoolss_SpoolerInit
9847 ****************************************************************/
9849 WERROR _spoolss_SpoolerInit(pipes_struct *p,
9850 struct spoolss_SpoolerInit *r)
9852 p->rng_fault_state = true;
9853 return WERR_NOT_SUPPORTED;
9856 /****************************************************************
9857 _spoolss_ResetPrinterEx
9858 ****************************************************************/
9860 WERROR _spoolss_ResetPrinterEx(pipes_struct *p,
9861 struct spoolss_ResetPrinterEx *r)
9863 p->rng_fault_state = true;
9864 return WERR_NOT_SUPPORTED;
9867 /****************************************************************
9868 _spoolss_RouterReplyPrinterEx
9869 ****************************************************************/
9871 WERROR _spoolss_RouterReplyPrinterEx(pipes_struct *p,
9872 struct spoolss_RouterReplyPrinterEx *r)
9874 p->rng_fault_state = true;
9875 return WERR_NOT_SUPPORTED;
9878 /****************************************************************
9879 _spoolss_44
9880 ****************************************************************/
9882 WERROR _spoolss_44(pipes_struct *p,
9883 struct spoolss_44 *r)
9885 p->rng_fault_state = true;
9886 return WERR_NOT_SUPPORTED;
9889 /****************************************************************
9890 _spoolss_47
9891 ****************************************************************/
9893 WERROR _spoolss_47(pipes_struct *p,
9894 struct spoolss_47 *r)
9896 p->rng_fault_state = true;
9897 return WERR_NOT_SUPPORTED;
9900 /****************************************************************
9901 _spoolss_4a
9902 ****************************************************************/
9904 WERROR _spoolss_4a(pipes_struct *p,
9905 struct spoolss_4a *r)
9907 p->rng_fault_state = true;
9908 return WERR_NOT_SUPPORTED;
9911 /****************************************************************
9912 _spoolss_4b
9913 ****************************************************************/
9915 WERROR _spoolss_4b(pipes_struct *p,
9916 struct spoolss_4b *r)
9918 p->rng_fault_state = true;
9919 return WERR_NOT_SUPPORTED;
9922 /****************************************************************
9923 _spoolss_4c
9924 ****************************************************************/
9926 WERROR _spoolss_4c(pipes_struct *p,
9927 struct spoolss_4c *r)
9929 p->rng_fault_state = true;
9930 return WERR_NOT_SUPPORTED;
9933 /****************************************************************
9934 _spoolss_53
9935 ****************************************************************/
9937 WERROR _spoolss_53(pipes_struct *p,
9938 struct spoolss_53 *r)
9940 p->rng_fault_state = true;
9941 return WERR_NOT_SUPPORTED;
9944 /****************************************************************
9945 _spoolss_55
9946 ****************************************************************/
9948 WERROR _spoolss_55(pipes_struct *p,
9949 struct spoolss_55 *r)
9951 p->rng_fault_state = true;
9952 return WERR_NOT_SUPPORTED;
9955 /****************************************************************
9956 _spoolss_56
9957 ****************************************************************/
9959 WERROR _spoolss_56(pipes_struct *p,
9960 struct spoolss_56 *r)
9962 p->rng_fault_state = true;
9963 return WERR_NOT_SUPPORTED;
9966 /****************************************************************
9967 _spoolss_57
9968 ****************************************************************/
9970 WERROR _spoolss_57(pipes_struct *p,
9971 struct spoolss_57 *r)
9973 p->rng_fault_state = true;
9974 return WERR_NOT_SUPPORTED;
9977 /****************************************************************
9978 _spoolss_5a
9979 ****************************************************************/
9981 WERROR _spoolss_5a(pipes_struct *p,
9982 struct spoolss_5a *r)
9984 p->rng_fault_state = true;
9985 return WERR_NOT_SUPPORTED;
9988 /****************************************************************
9989 _spoolss_5b
9990 ****************************************************************/
9992 WERROR _spoolss_5b(pipes_struct *p,
9993 struct spoolss_5b *r)
9995 p->rng_fault_state = true;
9996 return WERR_NOT_SUPPORTED;
9999 /****************************************************************
10000 _spoolss_5c
10001 ****************************************************************/
10003 WERROR _spoolss_5c(pipes_struct *p,
10004 struct spoolss_5c *r)
10006 p->rng_fault_state = true;
10007 return WERR_NOT_SUPPORTED;
10010 /****************************************************************
10011 _spoolss_5d
10012 ****************************************************************/
10014 WERROR _spoolss_5d(pipes_struct *p,
10015 struct spoolss_5d *r)
10017 p->rng_fault_state = true;
10018 return WERR_NOT_SUPPORTED;
10021 /****************************************************************
10022 _spoolss_5e
10023 ****************************************************************/
10025 WERROR _spoolss_5e(pipes_struct *p,
10026 struct spoolss_5e *r)
10028 p->rng_fault_state = true;
10029 return WERR_NOT_SUPPORTED;
10032 /****************************************************************
10033 _spoolss_5f
10034 ****************************************************************/
10036 WERROR _spoolss_5f(pipes_struct *p,
10037 struct spoolss_5f *r)
10039 p->rng_fault_state = true;
10040 return WERR_NOT_SUPPORTED;
10043 /****************************************************************
10044 _spoolss_60
10045 ****************************************************************/
10047 WERROR _spoolss_60(pipes_struct *p,
10048 struct spoolss_60 *r)
10050 p->rng_fault_state = true;
10051 return WERR_NOT_SUPPORTED;
10054 /****************************************************************
10055 _spoolss_61
10056 ****************************************************************/
10058 WERROR _spoolss_61(pipes_struct *p,
10059 struct spoolss_61 *r)
10061 p->rng_fault_state = true;
10062 return WERR_NOT_SUPPORTED;
10065 /****************************************************************
10066 _spoolss_62
10067 ****************************************************************/
10069 WERROR _spoolss_62(pipes_struct *p,
10070 struct spoolss_62 *r)
10072 p->rng_fault_state = true;
10073 return WERR_NOT_SUPPORTED;
10076 /****************************************************************
10077 _spoolss_63
10078 ****************************************************************/
10080 WERROR _spoolss_63(pipes_struct *p,
10081 struct spoolss_63 *r)
10083 p->rng_fault_state = true;
10084 return WERR_NOT_SUPPORTED;
10087 /****************************************************************
10088 _spoolss_64
10089 ****************************************************************/
10091 WERROR _spoolss_64(pipes_struct *p,
10092 struct spoolss_64 *r)
10094 p->rng_fault_state = true;
10095 return WERR_NOT_SUPPORTED;
10098 /****************************************************************
10099 _spoolss_65
10100 ****************************************************************/
10102 WERROR _spoolss_65(pipes_struct *p,
10103 struct spoolss_65 *r)
10105 p->rng_fault_state = true;
10106 return WERR_NOT_SUPPORTED;
10109 /****************************************************************
10110 _spoolss_GetCorePrinterDrivers
10111 ****************************************************************/
10113 WERROR _spoolss_GetCorePrinterDrivers(pipes_struct *p,
10114 struct spoolss_GetCorePrinterDrivers *r)
10116 p->rng_fault_state = true;
10117 return WERR_NOT_SUPPORTED;
10120 /****************************************************************
10121 _spoolss_67
10122 ****************************************************************/
10124 WERROR _spoolss_67(pipes_struct *p,
10125 struct spoolss_67 *r)
10127 p->rng_fault_state = true;
10128 return WERR_NOT_SUPPORTED;
10131 /****************************************************************
10132 _spoolss_GetPrinterDriverPackagePath
10133 ****************************************************************/
10135 WERROR _spoolss_GetPrinterDriverPackagePath(pipes_struct *p,
10136 struct spoolss_GetPrinterDriverPackagePath *r)
10138 p->rng_fault_state = true;
10139 return WERR_NOT_SUPPORTED;
10142 /****************************************************************
10143 _spoolss_69
10144 ****************************************************************/
10146 WERROR _spoolss_69(pipes_struct *p,
10147 struct spoolss_69 *r)
10149 p->rng_fault_state = true;
10150 return WERR_NOT_SUPPORTED;
10153 /****************************************************************
10154 _spoolss_6a
10155 ****************************************************************/
10157 WERROR _spoolss_6a(pipes_struct *p,
10158 struct spoolss_6a *r)
10160 p->rng_fault_state = true;
10161 return WERR_NOT_SUPPORTED;
10164 /****************************************************************
10165 _spoolss_6b
10166 ****************************************************************/
10168 WERROR _spoolss_6b(pipes_struct *p,
10169 struct spoolss_6b *r)
10171 p->rng_fault_state = true;
10172 return WERR_NOT_SUPPORTED;
10175 /****************************************************************
10176 _spoolss_6c
10177 ****************************************************************/
10179 WERROR _spoolss_6c(pipes_struct *p,
10180 struct spoolss_6c *r)
10182 p->rng_fault_state = true;
10183 return WERR_NOT_SUPPORTED;
10186 /****************************************************************
10187 _spoolss_6d
10188 ****************************************************************/
10190 WERROR _spoolss_6d(pipes_struct *p,
10191 struct spoolss_6d *r)
10193 p->rng_fault_state = true;
10194 return WERR_NOT_SUPPORTED;