s3-spoolss: remove (disabled) enum_all_printers_info_1_remote.
[Samba.git] / source3 / rpc_server / srv_spoolss_nt.c
bloba62b5290f421d3ea64c51da5ee9d5f41cbd0f4d6
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.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, see <http://www.gnu.org/licenses/>.
25 /* Since the SPOOLSS rpc routines are basically DOS 16-bit calls wrapped
26 up, all the errors returned are DOS errors, not NT status codes. */
28 #include "includes.h"
30 /* macros stolen from s4 spoolss server */
31 #define SPOOLSS_BUFFER_UNION(fn,ic,info,level) \
32 ((info)?ndr_size_##fn(info, level, ic, 0):0)
34 #define SPOOLSS_BUFFER_UNION_ARRAY(mem_ctx,fn,ic,info,level,count) \
35 ((info)?ndr_size_##fn##_info(mem_ctx, ic, level, count, info):0)
37 #define SPOOLSS_BUFFER_OK(val_true,val_false) ((r->in.offered >= *r->out.needed)?val_true:val_false)
40 extern userdom_struct current_user_info;
42 #undef DBGC_CLASS
43 #define DBGC_CLASS DBGC_RPC_SRV
45 #ifndef MAX_OPEN_PRINTER_EXS
46 #define MAX_OPEN_PRINTER_EXS 50
47 #endif
49 #define MAGIC_DISPLAY_FREQUENCY 0xfade2bad
50 #define PHANTOM_DEVMODE_KEY "_p_f_a_n_t_0_m_"
52 struct table_node {
53 const char *long_archi;
54 const char *short_archi;
55 int version;
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 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 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, POLICY_HND *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, POLICY_HND *hnd)
232 Printer_entry *find_printer = NULL;
234 if(!find_policy_by_hnd(p,hnd,(void **)(void *)&find_printer)) {
235 DEBUG(2,("find_printer_index_by_hnd: Printer handle not found: "));
236 return NULL;
239 return find_printer;
242 /****************************************************************************
243 Close printer index by handle.
244 ****************************************************************************/
246 static bool close_printer_handle(pipes_struct *p, POLICY_HND *hnd)
248 Printer_entry *Printer = find_printer_index_by_hnd(p, hnd);
250 if (!Printer) {
251 DEBUG(2,("close_printer_handle: Invalid handle (%s:%u:%u)\n", OUR_HANDLE(hnd)));
252 return False;
255 close_policy_hnd(p, hnd);
257 return True;
260 /****************************************************************************
261 Delete a printer given a handle.
262 ****************************************************************************/
264 WERROR delete_printer_hook(TALLOC_CTX *ctx, NT_USER_TOKEN *token, const char *sharename )
266 char *cmd = lp_deleteprinter_cmd();
267 char *command = NULL;
268 int ret;
269 SE_PRIV se_printop = SE_PRINT_OPERATOR;
270 bool is_print_op = False;
272 /* can't fail if we don't try */
274 if ( !*cmd )
275 return WERR_OK;
277 command = talloc_asprintf(ctx,
278 "%s \"%s\"",
279 cmd, sharename);
280 if (!command) {
281 return WERR_NOMEM;
283 if ( token )
284 is_print_op = user_has_privileges( token, &se_printop );
286 DEBUG(10,("Running [%s]\n", command));
288 /********** BEGIN SePrintOperatorPrivlege BLOCK **********/
290 if ( is_print_op )
291 become_root();
293 if ( (ret = smbrun(command, NULL)) == 0 ) {
294 /* Tell everyone we updated smb.conf. */
295 message_send_all(smbd_messaging_context(),
296 MSG_SMB_CONF_UPDATED, NULL, 0, NULL);
299 if ( is_print_op )
300 unbecome_root();
302 /********** END SePrintOperatorPrivlege BLOCK **********/
304 DEBUGADD(10,("returned [%d]\n", ret));
306 TALLOC_FREE(command);
308 if (ret != 0)
309 return WERR_BADFID; /* What to return here? */
311 /* go ahead and re-read the services immediately */
312 reload_services( False );
314 if ( lp_servicenumber( sharename ) < 0 )
315 return WERR_ACCESS_DENIED;
317 return WERR_OK;
320 /****************************************************************************
321 Delete a printer given a handle.
322 ****************************************************************************/
324 static WERROR delete_printer_handle(pipes_struct *p, POLICY_HND *hnd)
326 Printer_entry *Printer = find_printer_index_by_hnd(p, hnd);
328 if (!Printer) {
329 DEBUG(2,("delete_printer_handle: Invalid handle (%s:%u:%u)\n", OUR_HANDLE(hnd)));
330 return WERR_BADFID;
334 * It turns out that Windows allows delete printer on a handle
335 * opened by an admin user, then used on a pipe handle created
336 * by an anonymous user..... but they're working on security.... riiight !
337 * JRA.
340 if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
341 DEBUG(3, ("delete_printer_handle: denied by handle\n"));
342 return WERR_ACCESS_DENIED;
345 /* this does not need a become root since the access check has been
346 done on the handle already */
348 if (del_a_printer( Printer->sharename ) != 0) {
349 DEBUG(3,("Error deleting printer %s\n", Printer->sharename));
350 return WERR_BADFID;
353 return delete_printer_hook(p->mem_ctx, p->server_info->ptok,
354 Printer->sharename );
357 /****************************************************************************
358 Return the snum of a printer corresponding to an handle.
359 ****************************************************************************/
361 static bool get_printer_snum(pipes_struct *p, POLICY_HND *hnd, int *number,
362 struct share_params **params)
364 Printer_entry *Printer = find_printer_index_by_hnd(p, hnd);
366 if (!Printer) {
367 DEBUG(2,("get_printer_snum: Invalid handle (%s:%u:%u)\n", OUR_HANDLE(hnd)));
368 return False;
371 switch (Printer->printer_type) {
372 case SPLHND_PRINTER:
373 DEBUG(4,("short name:%s\n", Printer->sharename));
374 *number = print_queue_snum(Printer->sharename);
375 return (*number != -1);
376 case SPLHND_SERVER:
377 return False;
378 default:
379 return False;
383 /****************************************************************************
384 Set printer handle type.
385 Check if it's \\server or \\server\printer
386 ****************************************************************************/
388 static bool set_printer_hnd_printertype(Printer_entry *Printer, char *handlename)
390 DEBUG(3,("Setting printer type=%s\n", handlename));
392 if ( strlen(handlename) < 3 ) {
393 DEBUGADD(4,("A print server must have at least 1 char ! %s\n", handlename));
394 return False;
397 /* it's a print server */
398 if (*handlename=='\\' && *(handlename+1)=='\\' && !strchr_m(handlename+2, '\\')) {
399 DEBUGADD(4,("Printer is a print server\n"));
400 Printer->printer_type = SPLHND_SERVER;
402 /* it's a printer (set_printer_hnd_name() will handle port monitors */
403 else {
404 DEBUGADD(4,("Printer is a printer\n"));
405 Printer->printer_type = SPLHND_PRINTER;
408 return True;
411 /****************************************************************************
412 Set printer handle name.. Accept names like \\server, \\server\printer,
413 \\server\SHARE, & "\\server\,XcvMonitor Standard TCP/IP Port" See
414 the MSDN docs regarding OpenPrinter() for details on the XcvData() and
415 XcvDataPort() interface.
416 ****************************************************************************/
418 static bool set_printer_hnd_name(Printer_entry *Printer, char *handlename)
420 int snum;
421 int n_services=lp_numservices();
422 char *aprinter, *printername;
423 const char *servername;
424 fstring sname;
425 bool found=False;
426 NT_PRINTER_INFO_LEVEL *printer = NULL;
427 WERROR result;
429 DEBUG(4,("Setting printer name=%s (len=%lu)\n", handlename, (unsigned long)strlen(handlename)));
431 aprinter = handlename;
432 if ( *handlename == '\\' ) {
433 servername = canon_servername(handlename);
434 if ( (aprinter = strchr_m( servername, '\\' )) != NULL ) {
435 *aprinter = '\0';
436 aprinter++;
438 } else {
439 servername = "";
442 /* save the servername to fill in replies on this handle */
444 if ( !is_myname_or_ipaddr( servername ) )
445 return False;
447 fstrcpy( Printer->servername, servername );
449 if ( Printer->printer_type == SPLHND_SERVER )
450 return True;
452 if ( Printer->printer_type != SPLHND_PRINTER )
453 return False;
455 DEBUGADD(5, ("searching for [%s]\n", aprinter ));
457 /* check for the Port Monitor Interface */
459 if ( strequal( aprinter, SPL_XCV_MONITOR_TCPMON ) ) {
460 Printer->printer_type = SPLHND_PORTMON_TCP;
461 fstrcpy(sname, SPL_XCV_MONITOR_TCPMON);
462 found = True;
464 else if ( strequal( aprinter, SPL_XCV_MONITOR_LOCALMON ) ) {
465 Printer->printer_type = SPLHND_PORTMON_LOCAL;
466 fstrcpy(sname, SPL_XCV_MONITOR_LOCALMON);
467 found = True;
470 /* Search all sharenames first as this is easier than pulling
471 the printer_info_2 off of disk. Don't use find_service() since
472 that calls out to map_username() */
474 /* do another loop to look for printernames */
476 for (snum=0; !found && snum<n_services; snum++) {
478 /* no point going on if this is not a printer */
480 if ( !(lp_snum_ok(snum) && lp_print_ok(snum)) )
481 continue;
483 fstrcpy(sname, lp_servicename(snum));
484 if ( strequal( aprinter, sname ) ) {
485 found = True;
486 break;
489 /* no point looking up the printer object if
490 we aren't allowing printername != sharename */
492 if ( lp_force_printername(snum) )
493 continue;
495 fstrcpy(sname, lp_servicename(snum));
497 printer = NULL;
499 /* This call doesn't fill in the location or comment from
500 * a CUPS server for efficiency with large numbers of printers.
501 * JRA.
504 result = get_a_printer_search( NULL, &printer, 2, sname );
505 if ( !W_ERROR_IS_OK(result) ) {
506 DEBUG(0,("set_printer_hnd_name: failed to lookup printer [%s] -- result [%s]\n",
507 sname, win_errstr(result)));
508 continue;
511 /* printername is always returned as \\server\printername */
512 if ( !(printername = strchr_m(&printer->info_2->printername[2], '\\')) ) {
513 DEBUG(0,("set_printer_hnd_name: info2->printername in wrong format! [%s]\n",
514 printer->info_2->printername));
515 free_a_printer( &printer, 2);
516 continue;
519 printername++;
521 if ( strequal(printername, aprinter) ) {
522 free_a_printer( &printer, 2);
523 found = True;
524 break;
527 DEBUGADD(10, ("printername: %s\n", printername));
529 free_a_printer( &printer, 2);
532 free_a_printer( &printer, 2);
534 if ( !found ) {
535 DEBUGADD(4,("Printer not found\n"));
536 return False;
539 DEBUGADD(4,("set_printer_hnd_name: Printer found: %s -> %s\n", aprinter, sname));
541 fstrcpy(Printer->sharename, sname);
543 return True;
546 /****************************************************************************
547 Find first available printer slot. creates a printer handle for you.
548 ****************************************************************************/
550 static bool open_printer_hnd(pipes_struct *p, POLICY_HND *hnd, char *name, uint32 access_granted)
552 Printer_entry *new_printer;
554 DEBUG(10,("open_printer_hnd: name [%s]\n", name));
556 new_printer = TALLOC_ZERO_P(NULL, Printer_entry);
557 if (new_printer == NULL) {
558 return false;
560 talloc_set_destructor(new_printer, printer_entry_destructor);
562 if (!create_policy_hnd(p, hnd, new_printer)) {
563 TALLOC_FREE(new_printer);
564 return False;
567 /* Add to the internal list. */
568 DLIST_ADD(printers_list, new_printer);
570 new_printer->notify.option=NULL;
572 if (!set_printer_hnd_printertype(new_printer, name)) {
573 close_printer_handle(p, hnd);
574 return False;
577 if (!set_printer_hnd_name(new_printer, name)) {
578 close_printer_handle(p, hnd);
579 return False;
582 new_printer->access_granted = access_granted;
584 DEBUG(5, ("%d printer handles active\n", (int)p->pipe_handles->count ));
586 return True;
589 /***************************************************************************
590 check to see if the client motify handle is monitoring the notification
591 given by (notify_type, notify_field).
592 **************************************************************************/
594 static bool is_monitoring_event_flags(uint32 flags, uint16 notify_type,
595 uint16 notify_field)
597 return True;
600 static bool is_monitoring_event(Printer_entry *p, uint16 notify_type,
601 uint16 notify_field)
603 struct spoolss_NotifyOption *option = p->notify.option;
604 uint32 i, j;
607 * Flags should always be zero when the change notify
608 * is registered by the client's spooler. A user Win32 app
609 * might use the flags though instead of the NOTIFY_OPTION_INFO
610 * --jerry
613 if (!option) {
614 return False;
617 if (p->notify.flags)
618 return is_monitoring_event_flags(
619 p->notify.flags, notify_type, notify_field);
621 for (i = 0; i < option->count; i++) {
623 /* Check match for notify_type */
625 if (option->types[i].type != notify_type)
626 continue;
628 /* Check match for field */
630 for (j = 0; j < option->types[i].count; j++) {
631 if (option->types[i].fields[j] == notify_field) {
632 return True;
637 DEBUG(10, ("Open handle for \\\\%s\\%s is not monitoring 0x%02x/0x%02x\n",
638 p->servername, p->sharename, notify_type, notify_field));
640 return False;
643 #define SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(_data, _integer) \
644 _data->data.integer[0] = _integer; \
645 _data->data.integer[1] = 0;
648 #define SETUP_SPOOLSS_NOTIFY_DATA_STRING(_data, _p) \
649 _data->data.string.string = talloc_strdup(mem_ctx, _p); \
650 if (!_data->data.string.string) {\
651 _data->data.string.size = 0; \
653 _data->data.string.size = strlen_m_term(_p) * 2;
655 #define SETUP_SPOOLSS_NOTIFY_DATA_DEVMODE(_data, _devmode) \
656 _data->data.devmode.devmode = _devmode;
658 #define SETUP_SPOOLSS_NOTIFY_DATA_SECDESC(_data, _size, _sd) \
659 _data->data.sd.sd = dup_sec_desc(mem_ctx, _sd); \
660 if (!_data->data.sd.sd) { \
661 _data->data.sd.sd_size = 0; \
663 _data->data.sd.sd_size = _size;
665 static void init_systemtime_buffer(TALLOC_CTX *mem_ctx,
666 struct tm *t,
667 const char **pp,
668 uint32_t *plen)
670 struct spoolss_Time st;
671 uint32_t len = 16;
672 char *p;
674 if (!init_systemtime(&st, t)) {
675 return;
678 p = talloc_array(mem_ctx, char, len);
679 if (!p) {
680 return;
684 * Systemtime must be linearized as a set of UINT16's.
685 * Fix from Benjamin (Bj) Kuit bj@it.uts.edu.au
688 SSVAL(p, 0, st.year);
689 SSVAL(p, 2, st.month);
690 SSVAL(p, 4, st.day_of_week);
691 SSVAL(p, 6, st.day);
692 SSVAL(p, 8, st.hour);
693 SSVAL(p, 10, st.minute);
694 SSVAL(p, 12, st.second);
695 SSVAL(p, 14, st.millisecond);
697 *pp = p;
698 *plen = len;
701 /* Convert a notification message to a struct spoolss_Notify */
703 static void notify_one_value(struct spoolss_notify_msg *msg,
704 struct spoolss_Notify *data,
705 TALLOC_CTX *mem_ctx)
707 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, msg->notify.value[0]);
710 static void notify_string(struct spoolss_notify_msg *msg,
711 struct spoolss_Notify *data,
712 TALLOC_CTX *mem_ctx)
714 /* The length of the message includes the trailing \0 */
716 data->data.string.size = msg->len * 2;
717 data->data.string.string = talloc_strdup(mem_ctx, msg->notify.data);
718 if (!data->data.string.string) {
719 data->data.string.size = 0;
720 return;
724 static void notify_system_time(struct spoolss_notify_msg *msg,
725 struct spoolss_Notify *data,
726 TALLOC_CTX *mem_ctx)
728 data->data.string.string = NULL;
729 data->data.string.size = 0;
731 if (msg->len != sizeof(time_t)) {
732 DEBUG(5, ("notify_system_time: received wrong sized message (%d)\n",
733 msg->len));
734 return;
737 init_systemtime_buffer(mem_ctx, gmtime((time_t *)msg->notify.data),
738 &data->data.string.string,
739 &data->data.string.size);
742 struct notify2_message_table {
743 const char *name;
744 void (*fn)(struct spoolss_notify_msg *msg,
745 struct spoolss_Notify *data, TALLOC_CTX *mem_ctx);
748 static struct notify2_message_table printer_notify_table[] = {
749 /* 0x00 */ { "PRINTER_NOTIFY_SERVER_NAME", notify_string },
750 /* 0x01 */ { "PRINTER_NOTIFY_PRINTER_NAME", notify_string },
751 /* 0x02 */ { "PRINTER_NOTIFY_SHARE_NAME", notify_string },
752 /* 0x03 */ { "PRINTER_NOTIFY_PORT_NAME", notify_string },
753 /* 0x04 */ { "PRINTER_NOTIFY_DRIVER_NAME", notify_string },
754 /* 0x05 */ { "PRINTER_NOTIFY_COMMENT", notify_string },
755 /* 0x06 */ { "PRINTER_NOTIFY_LOCATION", notify_string },
756 /* 0x07 */ { "PRINTER_NOTIFY_DEVMODE", NULL },
757 /* 0x08 */ { "PRINTER_NOTIFY_SEPFILE", notify_string },
758 /* 0x09 */ { "PRINTER_NOTIFY_PRINT_PROCESSOR", notify_string },
759 /* 0x0a */ { "PRINTER_NOTIFY_PARAMETERS", NULL },
760 /* 0x0b */ { "PRINTER_NOTIFY_DATATYPE", notify_string },
761 /* 0x0c */ { "PRINTER_NOTIFY_SECURITY_DESCRIPTOR", NULL },
762 /* 0x0d */ { "PRINTER_NOTIFY_ATTRIBUTES", notify_one_value },
763 /* 0x0e */ { "PRINTER_NOTIFY_PRIORITY", notify_one_value },
764 /* 0x0f */ { "PRINTER_NOTIFY_DEFAULT_PRIORITY", NULL },
765 /* 0x10 */ { "PRINTER_NOTIFY_START_TIME", NULL },
766 /* 0x11 */ { "PRINTER_NOTIFY_UNTIL_TIME", NULL },
767 /* 0x12 */ { "PRINTER_NOTIFY_STATUS", notify_one_value },
770 static struct notify2_message_table job_notify_table[] = {
771 /* 0x00 */ { "JOB_NOTIFY_PRINTER_NAME", NULL },
772 /* 0x01 */ { "JOB_NOTIFY_MACHINE_NAME", NULL },
773 /* 0x02 */ { "JOB_NOTIFY_PORT_NAME", NULL },
774 /* 0x03 */ { "JOB_NOTIFY_USER_NAME", notify_string },
775 /* 0x04 */ { "JOB_NOTIFY_NOTIFY_NAME", NULL },
776 /* 0x05 */ { "JOB_NOTIFY_DATATYPE", NULL },
777 /* 0x06 */ { "JOB_NOTIFY_PRINT_PROCESSOR", NULL },
778 /* 0x07 */ { "JOB_NOTIFY_PARAMETERS", NULL },
779 /* 0x08 */ { "JOB_NOTIFY_DRIVER_NAME", NULL },
780 /* 0x09 */ { "JOB_NOTIFY_DEVMODE", NULL },
781 /* 0x0a */ { "JOB_NOTIFY_STATUS", notify_one_value },
782 /* 0x0b */ { "JOB_NOTIFY_STATUS_STRING", NULL },
783 /* 0x0c */ { "JOB_NOTIFY_SECURITY_DESCRIPTOR", NULL },
784 /* 0x0d */ { "JOB_NOTIFY_DOCUMENT", notify_string },
785 /* 0x0e */ { "JOB_NOTIFY_PRIORITY", NULL },
786 /* 0x0f */ { "JOB_NOTIFY_POSITION", NULL },
787 /* 0x10 */ { "JOB_NOTIFY_SUBMITTED", notify_system_time },
788 /* 0x11 */ { "JOB_NOTIFY_START_TIME", NULL },
789 /* 0x12 */ { "JOB_NOTIFY_UNTIL_TIME", NULL },
790 /* 0x13 */ { "JOB_NOTIFY_TIME", NULL },
791 /* 0x14 */ { "JOB_NOTIFY_TOTAL_PAGES", notify_one_value },
792 /* 0x15 */ { "JOB_NOTIFY_PAGES_PRINTED", NULL },
793 /* 0x16 */ { "JOB_NOTIFY_TOTAL_BYTES", notify_one_value },
794 /* 0x17 */ { "JOB_NOTIFY_BYTES_PRINTED", NULL },
798 /***********************************************************************
799 Allocate talloc context for container object
800 **********************************************************************/
802 static void notify_msg_ctr_init( SPOOLSS_NOTIFY_MSG_CTR *ctr )
804 if ( !ctr )
805 return;
807 ctr->ctx = talloc_init("notify_msg_ctr_init %p", ctr);
809 return;
812 /***********************************************************************
813 release all allocated memory and zero out structure
814 **********************************************************************/
816 static void notify_msg_ctr_destroy( SPOOLSS_NOTIFY_MSG_CTR *ctr )
818 if ( !ctr )
819 return;
821 if ( ctr->ctx )
822 talloc_destroy(ctr->ctx);
824 ZERO_STRUCTP(ctr);
826 return;
829 /***********************************************************************
830 **********************************************************************/
832 static TALLOC_CTX* notify_ctr_getctx( SPOOLSS_NOTIFY_MSG_CTR *ctr )
834 if ( !ctr )
835 return NULL;
837 return ctr->ctx;
840 /***********************************************************************
841 **********************************************************************/
843 static SPOOLSS_NOTIFY_MSG_GROUP* notify_ctr_getgroup( SPOOLSS_NOTIFY_MSG_CTR *ctr, uint32 idx )
845 if ( !ctr || !ctr->msg_groups )
846 return NULL;
848 if ( idx >= ctr->num_groups )
849 return NULL;
851 return &ctr->msg_groups[idx];
855 /***********************************************************************
856 How many groups of change messages do we have ?
857 **********************************************************************/
859 static int notify_msg_ctr_numgroups( SPOOLSS_NOTIFY_MSG_CTR *ctr )
861 if ( !ctr )
862 return 0;
864 return ctr->num_groups;
867 /***********************************************************************
868 Add a SPOOLSS_NOTIFY_MSG_CTR to the correct group
869 **********************************************************************/
871 static int notify_msg_ctr_addmsg( SPOOLSS_NOTIFY_MSG_CTR *ctr, SPOOLSS_NOTIFY_MSG *msg )
873 SPOOLSS_NOTIFY_MSG_GROUP *groups = NULL;
874 SPOOLSS_NOTIFY_MSG_GROUP *msg_grp = NULL;
875 SPOOLSS_NOTIFY_MSG *msg_list = NULL;
876 int i, new_slot;
878 if ( !ctr || !msg )
879 return 0;
881 /* loop over all groups looking for a matching printer name */
883 for ( i=0; i<ctr->num_groups; i++ ) {
884 if ( strcmp(ctr->msg_groups[i].printername, msg->printer) == 0 )
885 break;
888 /* add a new group? */
890 if ( i == ctr->num_groups ) {
891 ctr->num_groups++;
893 if ( !(groups = TALLOC_REALLOC_ARRAY( ctr->ctx, ctr->msg_groups, SPOOLSS_NOTIFY_MSG_GROUP, ctr->num_groups)) ) {
894 DEBUG(0,("notify_msg_ctr_addmsg: talloc_realloc() failed!\n"));
895 return 0;
897 ctr->msg_groups = groups;
899 /* clear the new entry and set the printer name */
901 ZERO_STRUCT( ctr->msg_groups[ctr->num_groups-1] );
902 fstrcpy( ctr->msg_groups[ctr->num_groups-1].printername, msg->printer );
905 /* add the change messages; 'i' is the correct index now regardless */
907 msg_grp = &ctr->msg_groups[i];
909 msg_grp->num_msgs++;
911 if ( !(msg_list = TALLOC_REALLOC_ARRAY( ctr->ctx, msg_grp->msgs, SPOOLSS_NOTIFY_MSG, msg_grp->num_msgs )) ) {
912 DEBUG(0,("notify_msg_ctr_addmsg: talloc_realloc() failed for new message [%d]!\n", msg_grp->num_msgs));
913 return 0;
915 msg_grp->msgs = msg_list;
917 new_slot = msg_grp->num_msgs-1;
918 memcpy( &msg_grp->msgs[new_slot], msg, sizeof(SPOOLSS_NOTIFY_MSG) );
920 /* need to allocate own copy of data */
922 if ( msg->len != 0 )
923 msg_grp->msgs[new_slot].notify.data = (char *)
924 TALLOC_MEMDUP( ctr->ctx, msg->notify.data, msg->len );
926 return ctr->num_groups;
929 /***********************************************************************
930 Send a change notication message on all handles which have a call
931 back registered
932 **********************************************************************/
934 static void send_notify2_changes( SPOOLSS_NOTIFY_MSG_CTR *ctr, uint32 idx )
936 Printer_entry *p;
937 TALLOC_CTX *mem_ctx = notify_ctr_getctx( ctr );
938 SPOOLSS_NOTIFY_MSG_GROUP *msg_group = notify_ctr_getgroup( ctr, idx );
939 SPOOLSS_NOTIFY_MSG *messages;
940 int sending_msg_count;
942 if ( !msg_group ) {
943 DEBUG(5,("send_notify2_changes() called with no msg group!\n"));
944 return;
947 messages = msg_group->msgs;
949 if ( !messages ) {
950 DEBUG(5,("send_notify2_changes() called with no messages!\n"));
951 return;
954 DEBUG(8,("send_notify2_changes: Enter...[%s]\n", msg_group->printername));
956 /* loop over all printers */
958 for (p = printers_list; p; p = p->next) {
959 struct spoolss_Notify *notifies;
960 uint32_t count = 0;
961 uint32_t id;
962 int i;
964 /* Is there notification on this handle? */
966 if ( !p->notify.client_connected )
967 continue;
969 DEBUG(10,("Client connected! [\\\\%s\\%s]\n", p->servername, p->sharename));
971 /* For this printer? Print servers always receive
972 notifications. */
974 if ( ( p->printer_type == SPLHND_PRINTER ) &&
975 ( !strequal(msg_group->printername, p->sharename) ) )
976 continue;
978 DEBUG(10,("Our printer\n"));
980 /* allocate the max entries possible */
982 notifies = TALLOC_ZERO_ARRAY(mem_ctx, struct spoolss_Notify, msg_group->num_msgs);
983 if (!notifies) {
984 return;
987 /* build the array of change notifications */
989 sending_msg_count = 0;
991 for ( i=0; i<msg_group->num_msgs; i++ ) {
992 SPOOLSS_NOTIFY_MSG *msg = &messages[i];
994 /* Are we monitoring this event? */
996 if (!is_monitoring_event(p, msg->type, msg->field))
997 continue;
999 sending_msg_count++;
1002 DEBUG(10,("process_notify2_message: Sending message type [0x%x] field [0x%2x] for printer [%s]\n",
1003 msg->type, msg->field, p->sharename));
1006 * if the is a printer notification handle and not a job notification
1007 * type, then set the id to 0. Other wise just use what was specified
1008 * in the message.
1010 * When registering change notification on a print server handle
1011 * we always need to send back the id (snum) matching the printer
1012 * for which the change took place. For change notify registered
1013 * on a printer handle, this does not matter and the id should be 0.
1015 * --jerry
1018 if ( ( p->printer_type == SPLHND_PRINTER ) && ( msg->type == PRINTER_NOTIFY_TYPE ) )
1019 id = 0;
1020 else
1021 id = msg->id;
1024 /* Convert unix jobid to smb jobid */
1026 if (msg->flags & SPOOLSS_NOTIFY_MSG_UNIX_JOBID) {
1027 id = sysjob_to_jobid(msg->id);
1029 if (id == -1) {
1030 DEBUG(3, ("no such unix jobid %d\n", msg->id));
1031 goto done;
1035 construct_info_data( &notifies[count], msg->type, msg->field, id );
1037 switch(msg->type) {
1038 case PRINTER_NOTIFY_TYPE:
1039 if ( printer_notify_table[msg->field].fn )
1040 printer_notify_table[msg->field].fn(msg, &notifies[count], mem_ctx);
1041 break;
1043 case JOB_NOTIFY_TYPE:
1044 if ( job_notify_table[msg->field].fn )
1045 job_notify_table[msg->field].fn(msg, &notifies[count], mem_ctx);
1046 break;
1048 default:
1049 DEBUG(5, ("Unknown notification type %d\n", msg->type));
1050 goto done;
1053 count++;
1056 if ( sending_msg_count ) {
1057 NTSTATUS status;
1058 WERROR werr;
1059 union spoolss_ReplyPrinterInfo info;
1060 struct spoolss_NotifyInfo info0;
1061 uint32_t reply_result;
1063 info0.version = 0x2;
1064 info0.flags = count ? 0x00020000 /* ??? */ : PRINTER_NOTIFY_INFO_DISCARDED;
1065 info0.count = count;
1066 info0.notifies = notifies;
1068 info.info0 = &info0;
1070 status = rpccli_spoolss_RouterReplyPrinterEx(notify_cli_pipe, mem_ctx,
1071 &p->notify.client_hnd,
1072 p->notify.change, /* color */
1073 p->notify.flags,
1074 &reply_result,
1075 0, /* reply_type, must be 0 */
1076 info,
1077 &werr);
1078 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(werr)) {
1079 DEBUG(1,("RouterReplyPrinterEx to client: %s failed: %s\n",
1080 notify_cli_pipe->srv_name_slash,
1081 win_errstr(werr)));
1083 switch (reply_result) {
1084 case 0:
1085 break;
1086 case PRINTER_NOTIFY_INFO_DISCARDED:
1087 case PRINTER_NOTIFY_INFO_DISCARDNOTED:
1088 case PRINTER_NOTIFY_INFO_COLOR_MISMATCH:
1089 break;
1090 default:
1091 break;
1096 done:
1097 DEBUG(8,("send_notify2_changes: Exit...\n"));
1098 return;
1101 /***********************************************************************
1102 **********************************************************************/
1104 static bool notify2_unpack_msg( SPOOLSS_NOTIFY_MSG *msg, struct timeval *tv, void *buf, size_t len )
1107 uint32 tv_sec, tv_usec;
1108 size_t offset = 0;
1110 /* Unpack message */
1112 offset += tdb_unpack((uint8 *)buf + offset, len - offset, "f",
1113 msg->printer);
1115 offset += tdb_unpack((uint8 *)buf + offset, len - offset, "ddddddd",
1116 &tv_sec, &tv_usec,
1117 &msg->type, &msg->field, &msg->id, &msg->len, &msg->flags);
1119 if (msg->len == 0)
1120 tdb_unpack((uint8 *)buf + offset, len - offset, "dd",
1121 &msg->notify.value[0], &msg->notify.value[1]);
1122 else
1123 tdb_unpack((uint8 *)buf + offset, len - offset, "B",
1124 &msg->len, &msg->notify.data);
1126 DEBUG(3, ("notify2_unpack_msg: got NOTIFY2 message for printer %s, jobid %u type %d, field 0x%02x, flags 0x%04x\n",
1127 msg->printer, (unsigned int)msg->id, msg->type, msg->field, msg->flags));
1129 tv->tv_sec = tv_sec;
1130 tv->tv_usec = tv_usec;
1132 if (msg->len == 0)
1133 DEBUG(3, ("notify2_unpack_msg: value1 = %d, value2 = %d\n", msg->notify.value[0],
1134 msg->notify.value[1]));
1135 else
1136 dump_data(3, (uint8 *)msg->notify.data, msg->len);
1138 return True;
1141 /********************************************************************
1142 Receive a notify2 message list
1143 ********************************************************************/
1145 static void receive_notify2_message_list(struct messaging_context *msg,
1146 void *private_data,
1147 uint32_t msg_type,
1148 struct server_id server_id,
1149 DATA_BLOB *data)
1151 size_t msg_count, i;
1152 char *buf = (char *)data->data;
1153 char *msg_ptr;
1154 size_t msg_len;
1155 SPOOLSS_NOTIFY_MSG notify;
1156 SPOOLSS_NOTIFY_MSG_CTR messages;
1157 int num_groups;
1159 if (data->length < 4) {
1160 DEBUG(0,("receive_notify2_message_list: bad message format (len < 4)!\n"));
1161 return;
1164 msg_count = IVAL(buf, 0);
1165 msg_ptr = buf + 4;
1167 DEBUG(5, ("receive_notify2_message_list: got %lu messages in list\n", (unsigned long)msg_count));
1169 if (msg_count == 0) {
1170 DEBUG(0,("receive_notify2_message_list: bad message format (msg_count == 0) !\n"));
1171 return;
1174 /* initialize the container */
1176 ZERO_STRUCT( messages );
1177 notify_msg_ctr_init( &messages );
1180 * build message groups for each printer identified
1181 * in a change_notify msg. Remember that a PCN message
1182 * includes the handle returned for the srv_spoolss_replyopenprinter()
1183 * call. Therefore messages are grouped according to printer handle.
1186 for ( i=0; i<msg_count; i++ ) {
1187 struct timeval msg_tv;
1189 if (msg_ptr + 4 - buf > data->length) {
1190 DEBUG(0,("receive_notify2_message_list: bad message format (len > buf_size) !\n"));
1191 return;
1194 msg_len = IVAL(msg_ptr,0);
1195 msg_ptr += 4;
1197 if (msg_ptr + msg_len - buf > data->length) {
1198 DEBUG(0,("receive_notify2_message_list: bad message format (bad len) !\n"));
1199 return;
1202 /* unpack messages */
1204 ZERO_STRUCT( notify );
1205 notify2_unpack_msg( &notify, &msg_tv, msg_ptr, msg_len );
1206 msg_ptr += msg_len;
1208 /* add to correct list in container */
1210 notify_msg_ctr_addmsg( &messages, &notify );
1212 /* free memory that might have been allocated by notify2_unpack_msg() */
1214 if ( notify.len != 0 )
1215 SAFE_FREE( notify.notify.data );
1218 /* process each group of messages */
1220 num_groups = notify_msg_ctr_numgroups( &messages );
1221 for ( i=0; i<num_groups; i++ )
1222 send_notify2_changes( &messages, i );
1225 /* cleanup */
1227 DEBUG(10,("receive_notify2_message_list: processed %u messages\n", (uint32)msg_count ));
1229 notify_msg_ctr_destroy( &messages );
1231 return;
1234 /********************************************************************
1235 Send a message to ourself about new driver being installed
1236 so we can upgrade the information for each printer bound to this
1237 driver
1238 ********************************************************************/
1240 static bool srv_spoolss_drv_upgrade_printer(char* drivername)
1242 int len = strlen(drivername);
1244 if (!len)
1245 return False;
1247 DEBUG(10,("srv_spoolss_drv_upgrade_printer: Sending message about driver upgrade [%s]\n",
1248 drivername));
1250 messaging_send_buf(smbd_messaging_context(), procid_self(),
1251 MSG_PRINTER_DRVUPGRADE,
1252 (uint8 *)drivername, len+1);
1254 return True;
1257 /**********************************************************************
1258 callback to receive a MSG_PRINTER_DRVUPGRADE message and interate
1259 over all printers, upgrading ones as necessary
1260 **********************************************************************/
1262 void do_drv_upgrade_printer(struct messaging_context *msg,
1263 void *private_data,
1264 uint32_t msg_type,
1265 struct server_id server_id,
1266 DATA_BLOB *data)
1268 fstring drivername;
1269 int snum;
1270 int n_services = lp_numservices();
1271 size_t len;
1273 len = MIN(data->length,sizeof(drivername)-1);
1274 strncpy(drivername, (const char *)data->data, len);
1276 DEBUG(10,("do_drv_upgrade_printer: Got message for new driver [%s]\n", drivername ));
1278 /* Iterate the printer list */
1280 for (snum=0; snum<n_services; snum++)
1282 if (lp_snum_ok(snum) && lp_print_ok(snum) )
1284 WERROR result;
1285 NT_PRINTER_INFO_LEVEL *printer = NULL;
1287 result = get_a_printer(NULL, &printer, 2, lp_const_servicename(snum));
1288 if (!W_ERROR_IS_OK(result))
1289 continue;
1291 if (printer && printer->info_2 && !strcmp(drivername, printer->info_2->drivername))
1293 DEBUG(6,("Updating printer [%s]\n", printer->info_2->printername));
1295 /* all we care about currently is the change_id */
1297 result = mod_a_printer(printer, 2);
1298 if (!W_ERROR_IS_OK(result)) {
1299 DEBUG(3,("do_drv_upgrade_printer: mod_a_printer() failed with status [%s]\n",
1300 win_errstr(result)));
1304 free_a_printer(&printer, 2);
1308 /* all done */
1311 /********************************************************************
1312 Update the cache for all printq's with a registered client
1313 connection
1314 ********************************************************************/
1316 void update_monitored_printq_cache( void )
1318 Printer_entry *printer = printers_list;
1319 int snum;
1321 /* loop through all printers and update the cache where
1322 client_connected == True */
1323 while ( printer )
1325 if ( (printer->printer_type == SPLHND_PRINTER)
1326 && printer->notify.client_connected )
1328 snum = print_queue_snum(printer->sharename);
1329 print_queue_status( snum, NULL, NULL );
1332 printer = printer->next;
1335 return;
1337 /********************************************************************
1338 Send a message to ourself about new driver being installed
1339 so we can upgrade the information for each printer bound to this
1340 driver
1341 ********************************************************************/
1343 static bool srv_spoolss_reset_printerdata(char* drivername)
1345 int len = strlen(drivername);
1347 if (!len)
1348 return False;
1350 DEBUG(10,("srv_spoolss_reset_printerdata: Sending message about resetting printerdata [%s]\n",
1351 drivername));
1353 messaging_send_buf(smbd_messaging_context(), procid_self(),
1354 MSG_PRINTERDATA_INIT_RESET,
1355 (uint8 *)drivername, len+1);
1357 return True;
1360 /**********************************************************************
1361 callback to receive a MSG_PRINTERDATA_INIT_RESET message and interate
1362 over all printers, resetting printer data as neessary
1363 **********************************************************************/
1365 void reset_all_printerdata(struct messaging_context *msg,
1366 void *private_data,
1367 uint32_t msg_type,
1368 struct server_id server_id,
1369 DATA_BLOB *data)
1371 fstring drivername;
1372 int snum;
1373 int n_services = lp_numservices();
1374 size_t len;
1376 len = MIN( data->length, sizeof(drivername)-1 );
1377 strncpy( drivername, (const char *)data->data, len );
1379 DEBUG(10,("reset_all_printerdata: Got message for new driver [%s]\n", drivername ));
1381 /* Iterate the printer list */
1383 for ( snum=0; snum<n_services; snum++ )
1385 if ( lp_snum_ok(snum) && lp_print_ok(snum) )
1387 WERROR result;
1388 NT_PRINTER_INFO_LEVEL *printer = NULL;
1390 result = get_a_printer( NULL, &printer, 2, lp_const_servicename(snum) );
1391 if ( !W_ERROR_IS_OK(result) )
1392 continue;
1395 * if the printer is bound to the driver,
1396 * then reset to the new driver initdata
1399 if ( printer && printer->info_2 && !strcmp(drivername, printer->info_2->drivername) )
1401 DEBUG(6,("reset_all_printerdata: Updating printer [%s]\n", printer->info_2->printername));
1403 if ( !set_driver_init(printer, 2) ) {
1404 DEBUG(5,("reset_all_printerdata: Error resetting printer data for printer [%s], driver [%s]!\n",
1405 printer->info_2->printername, printer->info_2->drivername));
1408 result = mod_a_printer( printer, 2 );
1409 if ( !W_ERROR_IS_OK(result) ) {
1410 DEBUG(3,("reset_all_printerdata: mod_a_printer() failed! (%s)\n",
1411 get_dos_error_msg(result)));
1415 free_a_printer( &printer, 2 );
1419 /* all done */
1421 return;
1424 /****************************************************************
1425 _spoolss_OpenPrinter
1426 ****************************************************************/
1428 WERROR _spoolss_OpenPrinter(pipes_struct *p,
1429 struct spoolss_OpenPrinter *r)
1431 struct spoolss_OpenPrinterEx e;
1432 WERROR werr;
1434 ZERO_STRUCT(e.in.userlevel);
1436 e.in.printername = r->in.printername;
1437 e.in.datatype = r->in.datatype;
1438 e.in.devmode_ctr = r->in.devmode_ctr;
1439 e.in.access_mask = r->in.access_mask;
1440 e.in.level = 0;
1442 e.out.handle = r->out.handle;
1444 werr = _spoolss_OpenPrinterEx(p, &e);
1446 if (W_ERROR_EQUAL(werr, WERR_INVALID_PARAM)) {
1447 /* OpenPrinterEx returns this for a bad
1448 * printer name. We must return WERR_INVALID_PRINTER_NAME
1449 * instead.
1451 werr = WERR_INVALID_PRINTER_NAME;
1454 return werr;
1457 /********************************************************************
1458 ********************************************************************/
1460 bool convert_devicemode(const char *printername,
1461 const struct spoolss_DeviceMode *devmode,
1462 NT_DEVICEMODE **pp_nt_devmode)
1464 NT_DEVICEMODE *nt_devmode = *pp_nt_devmode;
1467 * Ensure nt_devmode is a valid pointer
1468 * as we will be overwriting it.
1471 if (nt_devmode == NULL) {
1472 DEBUG(5, ("convert_devicemode: allocating a generic devmode\n"));
1473 if ((nt_devmode = construct_nt_devicemode(printername)) == NULL)
1474 return false;
1477 rpcstr_push(nt_devmode->devicename, devmode->devicename, 31, 0);
1478 rpcstr_push(nt_devmode->formname, devmode->formname, 31, 0);
1480 nt_devmode->specversion = devmode->specversion;
1481 nt_devmode->driverversion = devmode->driverversion;
1482 nt_devmode->size = devmode->size;
1483 nt_devmode->fields = devmode->fields;
1484 nt_devmode->orientation = devmode->orientation;
1485 nt_devmode->papersize = devmode->papersize;
1486 nt_devmode->paperlength = devmode->paperlength;
1487 nt_devmode->paperwidth = devmode->paperwidth;
1488 nt_devmode->scale = devmode->scale;
1489 nt_devmode->copies = devmode->copies;
1490 nt_devmode->defaultsource = devmode->defaultsource;
1491 nt_devmode->printquality = devmode->printquality;
1492 nt_devmode->color = devmode->color;
1493 nt_devmode->duplex = devmode->duplex;
1494 nt_devmode->yresolution = devmode->yresolution;
1495 nt_devmode->ttoption = devmode->ttoption;
1496 nt_devmode->collate = devmode->collate;
1498 nt_devmode->logpixels = devmode->logpixels;
1499 nt_devmode->bitsperpel = devmode->bitsperpel;
1500 nt_devmode->pelswidth = devmode->pelswidth;
1501 nt_devmode->pelsheight = devmode->pelsheight;
1502 nt_devmode->displayflags = devmode->displayflags;
1503 nt_devmode->displayfrequency = devmode->displayfrequency;
1504 nt_devmode->icmmethod = devmode->icmmethod;
1505 nt_devmode->icmintent = devmode->icmintent;
1506 nt_devmode->mediatype = devmode->mediatype;
1507 nt_devmode->dithertype = devmode->dithertype;
1508 nt_devmode->reserved1 = devmode->reserved1;
1509 nt_devmode->reserved2 = devmode->reserved2;
1510 nt_devmode->panningwidth = devmode->panningwidth;
1511 nt_devmode->panningheight = devmode->panningheight;
1514 * Only change private and driverextra if the incoming devmode
1515 * has a new one. JRA.
1518 if ((devmode->__driverextra_length != 0) && (devmode->driverextra_data.data != NULL)) {
1519 SAFE_FREE(nt_devmode->nt_dev_private);
1520 nt_devmode->driverextra = devmode->__driverextra_length;
1521 if((nt_devmode->nt_dev_private=SMB_MALLOC_ARRAY(uint8, nt_devmode->driverextra)) == NULL)
1522 return false;
1523 memcpy(nt_devmode->nt_dev_private, devmode->driverextra_data.data, nt_devmode->driverextra);
1526 *pp_nt_devmode = nt_devmode;
1528 return true;
1531 /****************************************************************
1532 _spoolss_OpenPrinterEx
1533 ****************************************************************/
1535 WERROR _spoolss_OpenPrinterEx(pipes_struct *p,
1536 struct spoolss_OpenPrinterEx *r)
1538 POLICY_HND *handle = r->out.handle;
1539 char *name = CONST_DISCARD(char *, r->in.printername);
1540 int snum;
1541 Printer_entry *Printer=NULL;
1543 if (!name) {
1544 return WERR_INVALID_PARAM;
1547 /* some sanity check because you can open a printer or a print server */
1548 /* aka: \\server\printer or \\server */
1550 DEBUGADD(3,("checking name: %s\n",name));
1552 if (!open_printer_hnd(p, handle, name, 0)) {
1553 ZERO_STRUCTP(r->out.handle);
1554 return WERR_INVALID_PARAM;
1557 Printer=find_printer_index_by_hnd(p, handle);
1558 if ( !Printer ) {
1559 DEBUG(0,("_spoolss_OpenPrinterEx: logic error. Can't find printer "
1560 "handle we created for printer %s\n", name ));
1561 close_printer_handle(p,handle);
1562 ZERO_STRUCTP(r->out.handle);
1563 return WERR_INVALID_PARAM;
1567 * First case: the user is opening the print server:
1569 * Disallow MS AddPrinterWizard if parameter disables it. A Win2k
1570 * client 1st tries an OpenPrinterEx with access==0, MUST be allowed.
1572 * Then both Win2k and WinNT clients try an OpenPrinterEx with
1573 * SERVER_ALL_ACCESS, which we allow only if the user is root (uid=0)
1574 * or if the user is listed in the smb.conf printer admin parameter.
1576 * Then they try OpenPrinterEx with SERVER_READ which we allow. This lets the
1577 * client view printer folder, but does not show the MSAPW.
1579 * Note: this test needs code to check access rights here too. Jeremy
1580 * could you look at this?
1582 * Second case: the user is opening a printer:
1583 * NT doesn't let us connect to a printer if the connecting user
1584 * doesn't have print permission.
1586 * Third case: user is opening a Port Monitor
1587 * access checks same as opening a handle to the print server.
1590 switch (Printer->printer_type )
1592 case SPLHND_SERVER:
1593 case SPLHND_PORTMON_TCP:
1594 case SPLHND_PORTMON_LOCAL:
1595 /* Printserver handles use global struct... */
1597 snum = -1;
1599 /* Map standard access rights to object specific access rights */
1601 se_map_standard(&r->in.access_mask,
1602 &printserver_std_mapping);
1604 /* Deny any object specific bits that don't apply to print
1605 servers (i.e printer and job specific bits) */
1607 r->in.access_mask &= SPECIFIC_RIGHTS_MASK;
1609 if (r->in.access_mask &
1610 ~(SERVER_ACCESS_ADMINISTER | SERVER_ACCESS_ENUMERATE)) {
1611 DEBUG(3, ("access DENIED for non-printserver bits\n"));
1612 close_printer_handle(p, handle);
1613 ZERO_STRUCTP(r->out.handle);
1614 return WERR_ACCESS_DENIED;
1617 /* Allow admin access */
1619 if ( r->in.access_mask & SERVER_ACCESS_ADMINISTER )
1621 SE_PRIV se_printop = SE_PRINT_OPERATOR;
1623 if (!lp_ms_add_printer_wizard()) {
1624 close_printer_handle(p, handle);
1625 ZERO_STRUCTP(r->out.handle);
1626 return WERR_ACCESS_DENIED;
1629 /* if the user is not root, doesn't have SE_PRINT_OPERATOR privilege,
1630 and not a printer admin, then fail */
1632 if ((p->server_info->utok.uid != 0) &&
1633 !user_has_privileges(p->server_info->ptok,
1634 &se_printop ) &&
1635 !token_contains_name_in_list(
1636 uidtoname(p->server_info->utok.uid),
1637 NULL, NULL,
1638 p->server_info->ptok,
1639 lp_printer_admin(snum))) {
1640 close_printer_handle(p, handle);
1641 ZERO_STRUCTP(r->out.handle);
1642 return WERR_ACCESS_DENIED;
1645 r->in.access_mask = SERVER_ACCESS_ADMINISTER;
1647 else
1649 r->in.access_mask = SERVER_ACCESS_ENUMERATE;
1652 DEBUG(4,("Setting print server access = %s\n", (r->in.access_mask == SERVER_ACCESS_ADMINISTER)
1653 ? "SERVER_ACCESS_ADMINISTER" : "SERVER_ACCESS_ENUMERATE" ));
1655 /* We fall through to return WERR_OK */
1656 break;
1658 case SPLHND_PRINTER:
1659 /* NT doesn't let us connect to a printer if the connecting user
1660 doesn't have print permission. */
1662 if (!get_printer_snum(p, handle, &snum, NULL)) {
1663 close_printer_handle(p, handle);
1664 ZERO_STRUCTP(r->out.handle);
1665 return WERR_BADFID;
1668 se_map_standard(&r->in.access_mask, &printer_std_mapping);
1670 /* map an empty access mask to the minimum access mask */
1671 if (r->in.access_mask == 0x0)
1672 r->in.access_mask = PRINTER_ACCESS_USE;
1675 * If we are not serving the printer driver for this printer,
1676 * map PRINTER_ACCESS_ADMINISTER to PRINTER_ACCESS_USE. This
1677 * will keep NT clients happy --jerry
1680 if (lp_use_client_driver(snum)
1681 && (r->in.access_mask & PRINTER_ACCESS_ADMINISTER))
1683 r->in.access_mask = PRINTER_ACCESS_USE;
1686 /* check smb.conf parameters and the the sec_desc */
1688 if ( !check_access(get_client_fd(), lp_hostsallow(snum), lp_hostsdeny(snum)) ) {
1689 DEBUG(3, ("access DENIED (hosts allow/deny) for printer open\n"));
1690 ZERO_STRUCTP(r->out.handle);
1691 return WERR_ACCESS_DENIED;
1694 if (!user_ok_token(uidtoname(p->server_info->utok.uid), NULL,
1695 p->server_info->ptok, snum) ||
1696 !print_access_check(p->server_info, snum,
1697 r->in.access_mask)) {
1698 DEBUG(3, ("access DENIED for printer open\n"));
1699 close_printer_handle(p, handle);
1700 ZERO_STRUCTP(r->out.handle);
1701 return WERR_ACCESS_DENIED;
1704 if ((r->in.access_mask & SPECIFIC_RIGHTS_MASK)& ~(PRINTER_ACCESS_ADMINISTER|PRINTER_ACCESS_USE)) {
1705 DEBUG(3, ("access DENIED for printer open - unknown bits\n"));
1706 close_printer_handle(p, handle);
1707 ZERO_STRUCTP(r->out.handle);
1708 return WERR_ACCESS_DENIED;
1711 if (r->in.access_mask & PRINTER_ACCESS_ADMINISTER)
1712 r->in.access_mask = PRINTER_ACCESS_ADMINISTER;
1713 else
1714 r->in.access_mask = PRINTER_ACCESS_USE;
1716 DEBUG(4,("Setting printer access = %s\n", (r->in.access_mask == PRINTER_ACCESS_ADMINISTER)
1717 ? "PRINTER_ACCESS_ADMINISTER" : "PRINTER_ACCESS_USE" ));
1719 break;
1721 default:
1722 /* sanity check to prevent programmer error */
1723 ZERO_STRUCTP(r->out.handle);
1724 return WERR_BADFID;
1727 Printer->access_granted = r->in.access_mask;
1730 * If the client sent a devmode in the OpenPrinter() call, then
1731 * save it here in case we get a job submission on this handle
1734 if ((Printer->printer_type != SPLHND_SERVER) &&
1735 r->in.devmode_ctr.devmode) {
1736 convert_devicemode(Printer->sharename,
1737 r->in.devmode_ctr.devmode,
1738 &Printer->nt_devmode);
1741 #if 0 /* JERRY -- I'm doubtful this is really effective */
1742 /* HACK ALERT!!! Sleep for 1/3 of a second to try trigger a LAN/WAN
1743 optimization in Windows 2000 clients --jerry */
1745 if ( (r->in.access_mask == PRINTER_ACCESS_ADMINISTER)
1746 && (RA_WIN2K == get_remote_arch()) )
1748 DEBUG(10,("_spoolss_OpenPrinterEx: Enabling LAN/WAN hack for Win2k clients.\n"));
1749 sys_usleep( 500000 );
1751 #endif
1753 return WERR_OK;
1756 /****************************************************************************
1757 ****************************************************************************/
1759 static bool printer_info2_to_nt_printer_info2(struct spoolss_SetPrinterInfo2 *r,
1760 NT_PRINTER_INFO_LEVEL_2 *d)
1762 DEBUG(7,("printer_info2_to_nt_printer_info2\n"));
1764 if (!r || !d) {
1765 return false;
1768 d->attributes = r->attributes;
1769 d->priority = r->priority;
1770 d->default_priority = r->defaultpriority;
1771 d->starttime = r->starttime;
1772 d->untiltime = r->untiltime;
1773 d->status = r->status;
1774 d->cjobs = r->cjobs;
1776 fstrcpy(d->servername, r->servername);
1777 fstrcpy(d->printername, r->printername);
1778 fstrcpy(d->sharename, r->sharename);
1779 fstrcpy(d->portname, r->portname);
1780 fstrcpy(d->drivername, r->drivername);
1781 slprintf(d->comment, sizeof(d->comment)-1, "%s", r->comment);
1782 fstrcpy(d->location, r->location);
1783 fstrcpy(d->sepfile, r->sepfile);
1784 fstrcpy(d->printprocessor, r->printprocessor);
1785 fstrcpy(d->datatype, r->datatype);
1786 fstrcpy(d->parameters, r->parameters);
1788 return true;
1791 /****************************************************************************
1792 ****************************************************************************/
1794 static bool convert_printer_info(struct spoolss_SetPrinterInfoCtr *info_ctr,
1795 NT_PRINTER_INFO_LEVEL *printer)
1797 bool ret;
1799 switch (info_ctr->level) {
1800 case 2:
1801 /* allocate memory if needed. Messy because
1802 convert_printer_info is used to update an existing
1803 printer or build a new one */
1805 if (!printer->info_2) {
1806 printer->info_2 = TALLOC_ZERO_P(printer, NT_PRINTER_INFO_LEVEL_2);
1807 if (!printer->info_2) {
1808 DEBUG(0,("convert_printer_info: "
1809 "talloc() failed!\n"));
1810 return false;
1814 ret = printer_info2_to_nt_printer_info2(info_ctr->info.info2,
1815 printer->info_2);
1816 printer->info_2->setuptime = time(NULL);
1817 return ret;
1820 return false;
1823 /*******************************************************************
1824 ********************************************************************/
1826 static bool string_array_to_fstring_array(const char **sarray, fstring **farray)
1828 int i;
1830 if (!sarray) {
1831 *farray = NULL;
1832 return true;
1835 *farray = SMB_MALLOC_ARRAY(fstring, 1);
1836 if (!*farray) {
1837 return false;
1840 for (i=0; sarray[i] != NULL; i++) {
1841 *farray = SMB_REALLOC_ARRAY(*farray, fstring, i+2);
1842 if (!*farray) {
1843 return false;
1845 fstrcpy((*farray)[i], sarray[i]);
1848 fstrcpy((*farray)[i], "");
1850 return true;
1853 /*******************************************************************
1854 ********************************************************************/
1856 static bool driver_info3_to_nt_driver_info3(struct spoolss_AddDriverInfo3 *r,
1857 NT_PRINTER_DRIVER_INFO_LEVEL_3 **p)
1859 NT_PRINTER_DRIVER_INFO_LEVEL_3 *d;
1861 DEBUG(7,("driver_info3_to_nt_driver_info3: Converting from UNICODE to ASCII\n"));
1863 if (*p == NULL) {
1864 *p = SMB_MALLOC_P(NT_PRINTER_DRIVER_INFO_LEVEL_3);
1865 if (*p == NULL) {
1866 return false;
1868 ZERO_STRUCTP(*p);
1871 d = *p;
1873 d->cversion = r->version;
1875 fstrcpy(d->name, r->driver_name);
1876 fstrcpy(d->environment, r->architecture);
1877 fstrcpy(d->driverpath, r->driver_path);
1878 fstrcpy(d->datafile, r->data_file);
1879 fstrcpy(d->configfile, r->config_file);
1880 fstrcpy(d->helpfile, r->help_file);
1881 fstrcpy(d->monitorname, r->monitor_name);
1882 fstrcpy(d->defaultdatatype, r->default_datatype);
1884 DEBUGADD(8,( "version: %d\n", d->cversion));
1885 DEBUGADD(8,( "name: %s\n", d->name));
1886 DEBUGADD(8,( "environment: %s\n", d->environment));
1887 DEBUGADD(8,( "driverpath: %s\n", d->driverpath));
1888 DEBUGADD(8,( "datafile: %s\n", d->datafile));
1889 DEBUGADD(8,( "configfile: %s\n", d->configfile));
1890 DEBUGADD(8,( "helpfile: %s\n", d->helpfile));
1891 DEBUGADD(8,( "monitorname: %s\n", d->monitorname));
1892 DEBUGADD(8,( "defaultdatatype: %s\n", d->defaultdatatype));
1894 if (r->dependent_files) {
1895 if (!string_array_to_fstring_array(r->dependent_files->string,
1896 &d->dependentfiles)) {
1897 SAFE_FREE(*p);
1898 return false;
1902 return true;
1905 /*******************************************************************
1906 ********************************************************************/
1908 static bool driver_info6_to_nt_driver_info6(struct spoolss_AddDriverInfo6 *r,
1909 NT_PRINTER_DRIVER_INFO_LEVEL_6 **p)
1911 NT_PRINTER_DRIVER_INFO_LEVEL_6 *d;
1913 DEBUG(7,("driver_info6_to_nt_driver_info6: Converting from UNICODE to ASCII\n"));
1915 if (*p == NULL) {
1916 *p = SMB_MALLOC_P(NT_PRINTER_DRIVER_INFO_LEVEL_6);
1917 if (*p == NULL) {
1918 return false;
1920 ZERO_STRUCTP(*p);
1923 d = *p;
1925 d->version = r->version;
1927 fstrcpy(d->name, r->driver_name);
1928 fstrcpy(d->environment, r->architecture);
1929 fstrcpy(d->driverpath, r->driver_path);
1930 fstrcpy(d->datafile, r->data_file);
1931 fstrcpy(d->configfile, r->config_file);
1932 fstrcpy(d->helpfile, r->help_file);
1933 fstrcpy(d->monitorname, r->monitor_name);
1934 fstrcpy(d->defaultdatatype, r->default_datatype);
1936 DEBUGADD(8,( "version: %d\n", d->version));
1937 DEBUGADD(8,( "name: %s\n", d->name));
1938 DEBUGADD(8,( "environment: %s\n", d->environment));
1939 DEBUGADD(8,( "driverpath: %s\n", d->driverpath));
1940 DEBUGADD(8,( "datafile: %s\n", d->datafile));
1941 DEBUGADD(8,( "configfile: %s\n", d->configfile));
1942 DEBUGADD(8,( "helpfile: %s\n", d->helpfile));
1943 DEBUGADD(8,( "monitorname: %s\n", d->monitorname));
1944 DEBUGADD(8,( "defaultdatatype: %s\n", d->defaultdatatype));
1946 if (r->dependent_files) {
1947 if (!string_array_to_fstring_array(r->dependent_files->string,
1948 &d->dependentfiles)) {
1949 goto error;
1953 if (r->previous_names) {
1954 if (!string_array_to_fstring_array(r->previous_names->string,
1955 &d->previousnames)) {
1956 goto error;
1960 return true;
1962 error:
1963 SAFE_FREE(*p);
1964 return false;
1967 /********************************************************************
1968 ********************************************************************/
1970 static bool convert_printer_driver_info(const struct spoolss_AddDriverInfoCtr *r,
1971 NT_PRINTER_DRIVER_INFO_LEVEL *printer,
1972 uint32_t level)
1974 switch (level) {
1975 case 3:
1976 printer->info_3 = NULL;
1977 if (!driver_info3_to_nt_driver_info3(r->info.info3, &printer->info_3)) {
1978 return false;
1980 break;
1981 case 6:
1982 printer->info_6 = NULL;
1983 if (!driver_info6_to_nt_driver_info6(r->info.info6, &printer->info_6)) {
1984 return false;
1986 break;
1987 default:
1988 return false;
1991 return true;
1994 /********************************************************************
1995 * _spoolss_enddocprinter_internal.
1996 ********************************************************************/
1998 static WERROR _spoolss_enddocprinter_internal(pipes_struct *p, POLICY_HND *handle)
2000 Printer_entry *Printer=find_printer_index_by_hnd(p, handle);
2001 int snum;
2003 if (!Printer) {
2004 DEBUG(2,("_spoolss_enddocprinter_internal: Invalid handle (%s:%u:%u)\n", OUR_HANDLE(handle)));
2005 return WERR_BADFID;
2008 if (!get_printer_snum(p, handle, &snum, NULL))
2009 return WERR_BADFID;
2011 Printer->document_started=False;
2012 print_job_end(snum, Printer->jobid,NORMAL_CLOSE);
2013 /* error codes unhandled so far ... */
2015 return WERR_OK;
2018 /****************************************************************
2019 _spoolss_ClosePrinter
2020 ****************************************************************/
2022 WERROR _spoolss_ClosePrinter(pipes_struct *p,
2023 struct spoolss_ClosePrinter *r)
2025 POLICY_HND *handle = r->in.handle;
2027 Printer_entry *Printer=find_printer_index_by_hnd(p, handle);
2029 if (Printer && Printer->document_started)
2030 _spoolss_enddocprinter_internal(p, handle); /* print job was not closed */
2032 if (!close_printer_handle(p, handle))
2033 return WERR_BADFID;
2035 /* clear the returned printer handle. Observed behavior
2036 from Win2k server. Don't think this really matters.
2037 Previous code just copied the value of the closed
2038 handle. --jerry */
2040 ZERO_STRUCTP(r->out.handle);
2042 return WERR_OK;
2045 /****************************************************************
2046 _spoolss_DeletePrinter
2047 ****************************************************************/
2049 WERROR _spoolss_DeletePrinter(pipes_struct *p,
2050 struct spoolss_DeletePrinter *r)
2052 POLICY_HND *handle = r->in.handle;
2053 Printer_entry *Printer=find_printer_index_by_hnd(p, handle);
2054 WERROR result;
2056 if (Printer && Printer->document_started)
2057 _spoolss_enddocprinter_internal(p, handle); /* print job was not closed */
2059 result = delete_printer_handle(p, handle);
2061 update_c_setprinter(False);
2063 return result;
2066 /*******************************************************************
2067 * static function to lookup the version id corresponding to an
2068 * long architecture string
2069 ******************************************************************/
2071 static int get_version_id (char * arch)
2073 int i;
2074 struct table_node archi_table[]= {
2076 {"Windows 4.0", "WIN40", 0 },
2077 {"Windows NT x86", "W32X86", 2 },
2078 {"Windows NT R4000", "W32MIPS", 2 },
2079 {"Windows NT Alpha_AXP", "W32ALPHA", 2 },
2080 {"Windows NT PowerPC", "W32PPC", 2 },
2081 {"Windows IA64", "IA64", 3 },
2082 {"Windows x64", "x64", 3 },
2083 {NULL, "", -1 }
2086 for (i=0; archi_table[i].long_archi != NULL; i++)
2088 if (strcmp(arch, archi_table[i].long_archi) == 0)
2089 return (archi_table[i].version);
2092 return -1;
2095 /****************************************************************
2096 _spoolss_DeletePrinterDriver
2097 ****************************************************************/
2099 WERROR _spoolss_DeletePrinterDriver(pipes_struct *p,
2100 struct spoolss_DeletePrinterDriver *r)
2102 char *driver;
2103 char *arch;
2104 NT_PRINTER_DRIVER_INFO_LEVEL info;
2105 NT_PRINTER_DRIVER_INFO_LEVEL info_win2k;
2106 int version;
2107 WERROR status;
2108 WERROR status_win2k = WERR_ACCESS_DENIED;
2109 SE_PRIV se_printop = SE_PRINT_OPERATOR;
2111 /* if the user is not root, doesn't have SE_PRINT_OPERATOR privilege,
2112 and not a printer admin, then fail */
2114 if ( (p->server_info->utok.uid != 0)
2115 && !user_has_privileges(p->server_info->ptok, &se_printop )
2116 && !token_contains_name_in_list(
2117 uidtoname(p->server_info->utok.uid), NULL,
2118 NULL, p->server_info->ptok,
2119 lp_printer_admin(-1)) )
2121 return WERR_ACCESS_DENIED;
2124 driver = CONST_DISCARD(char *, r->in.driver);
2125 arch = CONST_DISCARD(char *, r->in.architecture);
2127 /* check that we have a valid driver name first */
2129 if ((version=get_version_id(arch)) == -1)
2130 return WERR_INVALID_ENVIRONMENT;
2132 ZERO_STRUCT(info);
2133 ZERO_STRUCT(info_win2k);
2135 if (!W_ERROR_IS_OK(get_a_printer_driver(&info, 3, driver, arch, version)))
2137 /* try for Win2k driver if "Windows NT x86" */
2139 if ( version == 2 ) {
2140 version = 3;
2141 if (!W_ERROR_IS_OK(get_a_printer_driver(&info, 3, driver, arch, version))) {
2142 status = WERR_UNKNOWN_PRINTER_DRIVER;
2143 goto done;
2146 /* otherwise it was a failure */
2147 else {
2148 status = WERR_UNKNOWN_PRINTER_DRIVER;
2149 goto done;
2154 if (printer_driver_in_use(info.info_3)) {
2155 status = WERR_PRINTER_DRIVER_IN_USE;
2156 goto done;
2159 if ( version == 2 )
2161 if (W_ERROR_IS_OK(get_a_printer_driver(&info_win2k, 3, driver, arch, 3)))
2163 /* if we get to here, we now have 2 driver info structures to remove */
2164 /* remove the Win2k driver first*/
2166 status_win2k = delete_printer_driver(
2167 p, info_win2k.info_3, 3, False );
2168 free_a_printer_driver( info_win2k, 3 );
2170 /* this should not have failed---if it did, report to client */
2171 if ( !W_ERROR_IS_OK(status_win2k) )
2173 status = status_win2k;
2174 goto done;
2179 status = delete_printer_driver(p, info.info_3, version, False);
2181 /* if at least one of the deletes succeeded return OK */
2183 if ( W_ERROR_IS_OK(status) || W_ERROR_IS_OK(status_win2k) )
2184 status = WERR_OK;
2186 done:
2187 free_a_printer_driver( info, 3 );
2189 return status;
2192 /****************************************************************
2193 _spoolss_DeletePrinterDriverEx
2194 ****************************************************************/
2196 WERROR _spoolss_DeletePrinterDriverEx(pipes_struct *p,
2197 struct spoolss_DeletePrinterDriverEx *r)
2199 char *driver;
2200 char *arch;
2201 NT_PRINTER_DRIVER_INFO_LEVEL info;
2202 NT_PRINTER_DRIVER_INFO_LEVEL info_win2k;
2203 int version;
2204 uint32_t flags = r->in.delete_flags;
2205 bool delete_files;
2206 WERROR status;
2207 WERROR status_win2k = WERR_ACCESS_DENIED;
2208 SE_PRIV se_printop = SE_PRINT_OPERATOR;
2210 /* if the user is not root, doesn't have SE_PRINT_OPERATOR privilege,
2211 and not a printer admin, then fail */
2213 if ( (p->server_info->utok.uid != 0)
2214 && !user_has_privileges(p->server_info->ptok, &se_printop )
2215 && !token_contains_name_in_list(
2216 uidtoname(p->server_info->utok.uid), NULL, NULL,
2217 p->server_info->ptok, lp_printer_admin(-1)) )
2219 return WERR_ACCESS_DENIED;
2222 driver = CONST_DISCARD(char *, r->in.driver);
2223 arch = CONST_DISCARD(char *, r->in.architecture);
2225 /* check that we have a valid driver name first */
2226 if ((version=get_version_id(arch)) == -1) {
2227 /* this is what NT returns */
2228 return WERR_INVALID_ENVIRONMENT;
2231 if ( flags & DPD_DELETE_SPECIFIC_VERSION )
2232 version = r->in.version;
2234 ZERO_STRUCT(info);
2235 ZERO_STRUCT(info_win2k);
2237 status = get_a_printer_driver(&info, 3, driver, arch, version);
2239 if ( !W_ERROR_IS_OK(status) )
2242 * if the client asked for a specific version,
2243 * or this is something other than Windows NT x86,
2244 * then we've failed
2247 if ( (flags&DPD_DELETE_SPECIFIC_VERSION) || (version !=2) )
2248 goto done;
2250 /* try for Win2k driver if "Windows NT x86" */
2252 version = 3;
2253 if (!W_ERROR_IS_OK(get_a_printer_driver(&info, 3, driver, arch, version))) {
2254 status = WERR_UNKNOWN_PRINTER_DRIVER;
2255 goto done;
2259 if ( printer_driver_in_use(info.info_3) ) {
2260 status = WERR_PRINTER_DRIVER_IN_USE;
2261 goto done;
2265 * we have a couple of cases to consider.
2266 * (1) Are any files in use? If so and DPD_DELTE_ALL_FILE is set,
2267 * then the delete should fail if **any** files overlap with
2268 * other drivers
2269 * (2) If DPD_DELTE_UNUSED_FILES is sert, then delete all
2270 * non-overlapping files
2271 * (3) If neither DPD_DELTE_ALL_FILE nor DPD_DELTE_ALL_FILES
2272 * is set, the do not delete any files
2273 * Refer to MSDN docs on DeletePrinterDriverEx() for details.
2276 delete_files = flags & (DPD_DELETE_ALL_FILES|DPD_DELETE_UNUSED_FILES);
2278 /* fail if any files are in use and DPD_DELETE_ALL_FILES is set */
2280 if ( delete_files && printer_driver_files_in_use(info.info_3) & (flags&DPD_DELETE_ALL_FILES) ) {
2281 /* no idea of the correct error here */
2282 status = WERR_ACCESS_DENIED;
2283 goto done;
2287 /* also check for W32X86/3 if necessary; maybe we already have? */
2289 if ( (version == 2) && ((flags&DPD_DELETE_SPECIFIC_VERSION) != DPD_DELETE_SPECIFIC_VERSION) ) {
2290 if (W_ERROR_IS_OK(get_a_printer_driver(&info_win2k, 3, driver, arch, 3)))
2293 if ( delete_files && printer_driver_files_in_use(info_win2k.info_3) & (flags&DPD_DELETE_ALL_FILES) ) {
2294 /* no idea of the correct error here */
2295 free_a_printer_driver( info_win2k, 3 );
2296 status = WERR_ACCESS_DENIED;
2297 goto done;
2300 /* if we get to here, we now have 2 driver info structures to remove */
2301 /* remove the Win2k driver first*/
2303 status_win2k = delete_printer_driver(
2304 p, info_win2k.info_3, 3, delete_files);
2305 free_a_printer_driver( info_win2k, 3 );
2307 /* this should not have failed---if it did, report to client */
2309 if ( !W_ERROR_IS_OK(status_win2k) )
2310 goto done;
2314 status = delete_printer_driver(p, info.info_3, version, delete_files);
2316 if ( W_ERROR_IS_OK(status) || W_ERROR_IS_OK(status_win2k) )
2317 status = WERR_OK;
2318 done:
2319 free_a_printer_driver( info, 3 );
2321 return status;
2325 /****************************************************************************
2326 Internal routine for removing printerdata
2327 ***************************************************************************/
2329 static WERROR delete_printer_dataex( NT_PRINTER_INFO_LEVEL *printer, const char *key, const char *value )
2331 return delete_printer_data( printer->info_2, key, value );
2334 /****************************************************************************
2335 Internal routine for storing printerdata
2336 ***************************************************************************/
2338 WERROR set_printer_dataex( NT_PRINTER_INFO_LEVEL *printer, const char *key, const char *value,
2339 uint32 type, uint8 *data, int real_len )
2341 /* the registry objects enforce uniqueness based on value name */
2343 return add_printer_data( printer->info_2, key, value, type, data, real_len );
2346 /********************************************************************
2347 GetPrinterData on a printer server Handle.
2348 ********************************************************************/
2350 static WERROR getprinterdata_printer_server(TALLOC_CTX *mem_ctx,
2351 const char *value,
2352 enum winreg_Type *type,
2353 union spoolss_PrinterData *data)
2355 DEBUG(8,("getprinterdata_printer_server:%s\n", value));
2357 if (!StrCaseCmp(value, "W3SvcInstalled")) {
2358 *type = REG_DWORD;
2359 data->value = 0x00;
2360 return WERR_OK;
2363 if (!StrCaseCmp(value, "BeepEnabled")) {
2364 *type = REG_DWORD;
2365 data->value = 0x00;
2366 return WERR_OK;
2369 if (!StrCaseCmp(value, "EventLog")) {
2370 *type = REG_DWORD;
2371 /* formally was 0x1b */
2372 data->value = 0x00;
2373 return WERR_OK;
2376 if (!StrCaseCmp(value, "NetPopup")) {
2377 *type = REG_DWORD;
2378 data->value = 0x00;
2379 return WERR_OK;
2382 if (!StrCaseCmp(value, "MajorVersion")) {
2383 *type = REG_DWORD;
2385 /* Windows NT 4.0 seems to not allow uploading of drivers
2386 to a server that reports 0x3 as the MajorVersion.
2387 need to investigate more how Win2k gets around this .
2388 -- jerry */
2390 if (RA_WINNT == get_remote_arch()) {
2391 data->value = 0x02;
2392 } else {
2393 data->value = 0x03;
2396 return WERR_OK;
2399 if (!StrCaseCmp(value, "MinorVersion")) {
2400 *type = REG_DWORD;
2401 data->value = 0x00;
2402 return WERR_OK;
2405 /* REG_BINARY
2406 * uint32 size = 0x114
2407 * uint32 major = 5
2408 * uint32 minor = [0|1]
2409 * uint32 build = [2195|2600]
2410 * extra unicode string = e.g. "Service Pack 3"
2412 if (!StrCaseCmp(value, "OSVersion")) {
2413 DATA_BLOB blob;
2414 enum ndr_err_code ndr_err;
2415 struct spoolss_OSVersion os;
2417 os.major = 5; /* Windows 2000 == 5.0 */
2418 os.minor = 0;
2419 os.build = 2195; /* build */
2420 os.extra_string = ""; /* leave extra string empty */
2422 ndr_err = ndr_push_struct_blob(&blob, mem_ctx, NULL, &os,
2423 (ndr_push_flags_fn_t)ndr_push_spoolss_OSVersion);
2424 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2425 return WERR_GENERAL_FAILURE;
2428 *type = REG_BINARY;
2429 data->binary = blob;
2431 return WERR_OK;
2435 if (!StrCaseCmp(value, "DefaultSpoolDirectory")) {
2436 *type = REG_SZ;
2438 data->string = talloc_strdup(mem_ctx, "C:\\PRINTERS");
2439 W_ERROR_HAVE_NO_MEMORY(data->string);
2441 return WERR_OK;
2444 if (!StrCaseCmp(value, "Architecture")) {
2445 *type = REG_SZ;
2447 data->string = talloc_strdup(mem_ctx, "Windows NT x86");
2448 W_ERROR_HAVE_NO_MEMORY(data->string);
2450 return WERR_OK;
2453 if (!StrCaseCmp(value, "DsPresent")) {
2454 *type = REG_DWORD;
2456 /* only show the publish check box if we are a
2457 member of a AD domain */
2459 if (lp_security() == SEC_ADS) {
2460 data->value = 0x01;
2461 } else {
2462 data->value = 0x00;
2464 return WERR_OK;
2467 if (!StrCaseCmp(value, "DNSMachineName")) {
2468 const char *hostname = get_mydnsfullname();
2470 if (!hostname) {
2471 return WERR_BADFILE;
2474 *type = REG_SZ;
2475 data->string = talloc_strdup(mem_ctx, hostname);
2476 W_ERROR_HAVE_NO_MEMORY(data->string);
2478 return WERR_OK;
2481 return WERR_BADFILE;
2484 /****************************************************************
2485 _spoolss_GetPrinterData
2486 ****************************************************************/
2488 WERROR _spoolss_GetPrinterData(pipes_struct *p,
2489 struct spoolss_GetPrinterData *r)
2491 WERROR result;
2492 Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
2493 NT_PRINTER_INFO_LEVEL *printer = NULL;
2494 int snum = 0;
2497 * Reminder: when it's a string, the length is in BYTES
2498 * even if UNICODE is negociated.
2500 * JFM, 4/19/1999
2503 /* in case of problem, return some default values */
2505 *r->out.needed = 0;
2506 *r->out.type = 0;
2508 DEBUG(4,("_spoolss_GetPrinterData\n"));
2510 if (!Printer) {
2511 DEBUG(2,("_spoolss_GetPrinterData: Invalid handle (%s:%u:%u).\n",
2512 OUR_HANDLE(r->in.handle)));
2513 result = WERR_BADFID;
2514 goto done;
2517 if (Printer->printer_type == SPLHND_SERVER) {
2518 result = getprinterdata_printer_server(p->mem_ctx,
2519 r->in.value_name,
2520 r->out.type,
2521 r->out.data);
2522 } else {
2523 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
2524 result = WERR_BADFID;
2525 goto done;
2528 result = get_a_printer(Printer, &printer, 2, lp_servicename(snum));
2529 if (!W_ERROR_IS_OK(result)) {
2530 goto done;
2533 /* XP sends this and wants to change id value from the PRINTER_INFO_0 */
2535 if (strequal(r->in.value_name, "ChangeId")) {
2536 *r->out.type = REG_DWORD;
2537 r->out.data->value = printer->info_2->changeid;
2538 result = WERR_OK;
2539 } else {
2540 REGISTRY_VALUE *v;
2541 DATA_BLOB blob;
2543 v = get_printer_data(printer->info_2,
2544 SPOOL_PRINTERDATA_KEY,
2545 r->in.value_name);
2546 if (!v) {
2547 result = WERR_BADFILE;
2548 goto done;
2551 *r->out.type = v->type;
2553 blob = data_blob_const(v->data_p, v->size);
2555 result = pull_spoolss_PrinterData(p->mem_ctx, &blob,
2556 r->out.data,
2557 *r->out.type);
2561 done:
2562 /* cleanup & exit */
2564 if (printer) {
2565 free_a_printer(&printer, 2);
2568 if (!W_ERROR_IS_OK(result)) {
2569 return result;
2572 *r->out.needed = ndr_size_spoolss_PrinterData(r->out.data, *r->out.type, NULL, 0);
2573 *r->out.type = SPOOLSS_BUFFER_OK(*r->out.type, REG_NONE);
2574 r->out.data = SPOOLSS_BUFFER_OK(r->out.data, r->out.data);
2576 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_MORE_DATA);
2579 /*********************************************************
2580 Connect to the client machine.
2581 **********************************************************/
2583 static bool spoolss_connect_to_client(struct rpc_pipe_client **pp_pipe,
2584 struct sockaddr_storage *client_ss, const char *remote_machine)
2586 NTSTATUS ret;
2587 struct cli_state *the_cli;
2588 struct sockaddr_storage rm_addr;
2590 if ( is_zero_addr((struct sockaddr *)client_ss) ) {
2591 if ( !resolve_name( remote_machine, &rm_addr, 0x20) ) {
2592 DEBUG(2,("spoolss_connect_to_client: Can't resolve address for %s\n", remote_machine));
2593 return False;
2596 if (ismyaddr((struct sockaddr *)&rm_addr)) {
2597 DEBUG(0,("spoolss_connect_to_client: Machine %s is one of our addresses. Cannot add to ourselves.\n", remote_machine));
2598 return False;
2600 } else {
2601 char addr[INET6_ADDRSTRLEN];
2602 rm_addr = *client_ss;
2603 print_sockaddr(addr, sizeof(addr), &rm_addr);
2604 DEBUG(5,("spoolss_connect_to_client: Using address %s (no name resolution necessary)\n",
2605 addr));
2608 /* setup the connection */
2610 ret = cli_full_connection( &the_cli, global_myname(), remote_machine,
2611 &rm_addr, 0, "IPC$", "IPC",
2612 "", /* username */
2613 "", /* domain */
2614 "", /* password */
2615 0, lp_client_signing(), NULL );
2617 if ( !NT_STATUS_IS_OK( ret ) ) {
2618 DEBUG(2,("spoolss_connect_to_client: connection to [%s] failed!\n",
2619 remote_machine ));
2620 return False;
2623 if ( the_cli->protocol != PROTOCOL_NT1 ) {
2624 DEBUG(0,("spoolss_connect_to_client: machine %s didn't negotiate NT protocol.\n", remote_machine));
2625 cli_shutdown(the_cli);
2626 return False;
2630 * Ok - we have an anonymous connection to the IPC$ share.
2631 * Now start the NT Domain stuff :-).
2634 ret = cli_rpc_pipe_open_noauth(the_cli, &syntax_spoolss, pp_pipe);
2635 if (!NT_STATUS_IS_OK(ret)) {
2636 DEBUG(2,("spoolss_connect_to_client: unable to open the spoolss pipe on machine %s. Error was : %s.\n",
2637 remote_machine, nt_errstr(ret)));
2638 cli_shutdown(the_cli);
2639 return False;
2642 return True;
2645 /***************************************************************************
2646 Connect to the client.
2647 ****************************************************************************/
2649 static bool srv_spoolss_replyopenprinter(int snum, const char *printer,
2650 uint32 localprinter, uint32 type,
2651 POLICY_HND *handle, struct sockaddr_storage *client_ss)
2653 WERROR result;
2654 NTSTATUS status;
2657 * If it's the first connection, contact the client
2658 * and connect to the IPC$ share anonymously
2660 if (smb_connections==0) {
2661 fstring unix_printer;
2663 fstrcpy(unix_printer, printer+2); /* the +2 is to strip the leading 2 backslashs */
2665 if ( !spoolss_connect_to_client( &notify_cli_pipe, client_ss, unix_printer ))
2666 return False;
2668 messaging_register(smbd_messaging_context(), NULL,
2669 MSG_PRINTER_NOTIFY2,
2670 receive_notify2_message_list);
2671 /* Tell the connections db we're now interested in printer
2672 * notify messages. */
2673 register_message_flags( True, FLAG_MSG_PRINT_NOTIFY );
2677 * Tell the specific printing tdb we want messages for this printer
2678 * by registering our PID.
2681 if (!print_notify_register_pid(snum))
2682 DEBUG(0,("print_notify_register_pid: Failed to register our pid for printer %s\n", printer ));
2684 smb_connections++;
2686 status = rpccli_spoolss_ReplyOpenPrinter(notify_cli_pipe, talloc_tos(),
2687 printer,
2688 localprinter,
2689 type,
2691 NULL,
2692 handle,
2693 &result);
2694 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result))
2695 DEBUG(5,("srv_spoolss_reply_open_printer: Client RPC returned [%s]\n",
2696 win_errstr(result)));
2698 return (W_ERROR_IS_OK(result));
2701 /****************************************************************
2702 ****************************************************************/
2704 static struct spoolss_NotifyOption *dup_spoolss_NotifyOption(TALLOC_CTX *mem_ctx,
2705 const struct spoolss_NotifyOption *r)
2707 struct spoolss_NotifyOption *option;
2708 uint32_t i,k;
2710 if (!r) {
2711 return NULL;
2714 option = talloc_zero(mem_ctx, struct spoolss_NotifyOption);
2715 if (!option) {
2716 return NULL;
2719 *option = *r;
2721 if (!option->count) {
2722 return option;
2725 option->types = talloc_zero_array(option,
2726 struct spoolss_NotifyOptionType, option->count);
2727 if (!option->types) {
2728 talloc_free(option);
2729 return NULL;
2732 for (i=0; i < option->count; i++) {
2733 option->types[i] = r->types[i];
2735 if (option->types[i].count) {
2736 option->types[i].fields = talloc_zero_array(option,
2737 enum spoolss_Field, option->types[i].count);
2738 if (!option->types[i].fields) {
2739 talloc_free(option);
2740 return NULL;
2742 for (k=0; k<option->types[i].count; k++) {
2743 option->types[i].fields[k] =
2744 r->types[i].fields[k];
2749 return option;
2752 /****************************************************************
2753 * _spoolss_RemoteFindFirstPrinterChangeNotifyEx
2755 * before replying OK: status=0 a rpc call is made to the workstation
2756 * asking ReplyOpenPrinter
2758 * in fact ReplyOpenPrinter is the changenotify equivalent on the spoolss pipe
2759 * called from api_spoolss_rffpcnex
2760 ****************************************************************/
2762 WERROR _spoolss_RemoteFindFirstPrinterChangeNotifyEx(pipes_struct *p,
2763 struct spoolss_RemoteFindFirstPrinterChangeNotifyEx *r)
2765 POLICY_HND *handle = r->in.handle;
2766 int snum = -1;
2767 struct spoolss_NotifyOption *option = r->in.notify_options;
2768 struct sockaddr_storage client_ss;
2770 /* store the notify value in the printer struct */
2772 Printer_entry *Printer=find_printer_index_by_hnd(p, handle);
2774 if (!Printer) {
2775 DEBUG(2,("_spoolss_RemoteFindFirstPrinterChangeNotifyEx: "
2776 "Invalid handle (%s:%u:%u).\n", OUR_HANDLE(handle)));
2777 return WERR_BADFID;
2780 Printer->notify.flags = r->in.flags;
2781 Printer->notify.options = r->in.options;
2782 Printer->notify.printerlocal = r->in.printer_local;
2784 TALLOC_FREE(Printer->notify.option);
2785 Printer->notify.option = dup_spoolss_NotifyOption(Printer, option);
2787 fstrcpy(Printer->notify.localmachine, r->in.local_machine);
2789 /* Connect to the client machine and send a ReplyOpenPrinter */
2791 if ( Printer->printer_type == SPLHND_SERVER)
2792 snum = -1;
2793 else if ( (Printer->printer_type == SPLHND_PRINTER) &&
2794 !get_printer_snum(p, handle, &snum, NULL) )
2795 return WERR_BADFID;
2797 if (!interpret_string_addr(&client_ss, p->client_address,
2798 AI_NUMERICHOST)) {
2799 return WERR_SERVER_UNAVAILABLE;
2802 if(!srv_spoolss_replyopenprinter(snum, Printer->notify.localmachine,
2803 Printer->notify.printerlocal, 1,
2804 &Printer->notify.client_hnd, &client_ss))
2805 return WERR_SERVER_UNAVAILABLE;
2807 Printer->notify.client_connected=True;
2809 return WERR_OK;
2812 /*******************************************************************
2813 * fill a notify_info_data with the servername
2814 ********************************************************************/
2816 void spoolss_notify_server_name(int snum,
2817 struct spoolss_Notify *data,
2818 print_queue_struct *queue,
2819 NT_PRINTER_INFO_LEVEL *printer,
2820 TALLOC_CTX *mem_ctx)
2822 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, printer->info_2->servername);
2825 /*******************************************************************
2826 * fill a notify_info_data with the printername (not including the servername).
2827 ********************************************************************/
2829 void spoolss_notify_printer_name(int snum,
2830 struct spoolss_Notify *data,
2831 print_queue_struct *queue,
2832 NT_PRINTER_INFO_LEVEL *printer,
2833 TALLOC_CTX *mem_ctx)
2835 /* the notify name should not contain the \\server\ part */
2836 char *p = strrchr(printer->info_2->printername, '\\');
2838 if (!p) {
2839 p = printer->info_2->printername;
2840 } else {
2841 p++;
2844 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, p);
2847 /*******************************************************************
2848 * fill a notify_info_data with the servicename
2849 ********************************************************************/
2851 void spoolss_notify_share_name(int snum,
2852 struct spoolss_Notify *data,
2853 print_queue_struct *queue,
2854 NT_PRINTER_INFO_LEVEL *printer,
2855 TALLOC_CTX *mem_ctx)
2857 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, lp_servicename(snum));
2860 /*******************************************************************
2861 * fill a notify_info_data with the port name
2862 ********************************************************************/
2864 void spoolss_notify_port_name(int snum,
2865 struct spoolss_Notify *data,
2866 print_queue_struct *queue,
2867 NT_PRINTER_INFO_LEVEL *printer,
2868 TALLOC_CTX *mem_ctx)
2870 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, printer->info_2->portname);
2873 /*******************************************************************
2874 * fill a notify_info_data with the printername
2875 * but it doesn't exist, have to see what to do
2876 ********************************************************************/
2878 void spoolss_notify_driver_name(int snum,
2879 struct spoolss_Notify *data,
2880 print_queue_struct *queue,
2881 NT_PRINTER_INFO_LEVEL *printer,
2882 TALLOC_CTX *mem_ctx)
2884 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, printer->info_2->drivername);
2887 /*******************************************************************
2888 * fill a notify_info_data with the comment
2889 ********************************************************************/
2891 void spoolss_notify_comment(int snum,
2892 struct spoolss_Notify *data,
2893 print_queue_struct *queue,
2894 NT_PRINTER_INFO_LEVEL *printer,
2895 TALLOC_CTX *mem_ctx)
2897 char *p;
2899 if (*printer->info_2->comment == '\0') {
2900 p = lp_comment(snum);
2901 } else {
2902 p = printer->info_2->comment;
2905 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, printer->info_2->comment);
2908 /*******************************************************************
2909 * fill a notify_info_data with the comment
2910 * location = "Room 1, floor 2, building 3"
2911 ********************************************************************/
2913 void spoolss_notify_location(int snum,
2914 struct spoolss_Notify *data,
2915 print_queue_struct *queue,
2916 NT_PRINTER_INFO_LEVEL *printer,
2917 TALLOC_CTX *mem_ctx)
2919 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, printer->info_2->location);
2922 /*******************************************************************
2923 * fill a notify_info_data with the device mode
2924 * jfm:xxxx don't to it for know but that's a real problem !!!
2925 ********************************************************************/
2927 static void spoolss_notify_devmode(int snum,
2928 struct spoolss_Notify *data,
2929 print_queue_struct *queue,
2930 NT_PRINTER_INFO_LEVEL *printer,
2931 TALLOC_CTX *mem_ctx)
2933 /* for a dummy implementation we have to zero the fields */
2934 SETUP_SPOOLSS_NOTIFY_DATA_DEVMODE(data, NULL);
2937 /*******************************************************************
2938 * fill a notify_info_data with the separator file name
2939 ********************************************************************/
2941 void spoolss_notify_sepfile(int snum,
2942 struct spoolss_Notify *data,
2943 print_queue_struct *queue,
2944 NT_PRINTER_INFO_LEVEL *printer,
2945 TALLOC_CTX *mem_ctx)
2947 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, printer->info_2->sepfile);
2950 /*******************************************************************
2951 * fill a notify_info_data with the print processor
2952 * jfm:xxxx return always winprint to indicate we don't do anything to it
2953 ********************************************************************/
2955 void spoolss_notify_print_processor(int snum,
2956 struct spoolss_Notify *data,
2957 print_queue_struct *queue,
2958 NT_PRINTER_INFO_LEVEL *printer,
2959 TALLOC_CTX *mem_ctx)
2961 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, printer->info_2->printprocessor);
2964 /*******************************************************************
2965 * fill a notify_info_data with the print processor options
2966 * jfm:xxxx send an empty string
2967 ********************************************************************/
2969 void spoolss_notify_parameters(int snum,
2970 struct spoolss_Notify *data,
2971 print_queue_struct *queue,
2972 NT_PRINTER_INFO_LEVEL *printer,
2973 TALLOC_CTX *mem_ctx)
2975 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, printer->info_2->parameters);
2978 /*******************************************************************
2979 * fill a notify_info_data with the data type
2980 * jfm:xxxx always send RAW as data type
2981 ********************************************************************/
2983 void spoolss_notify_datatype(int snum,
2984 struct spoolss_Notify *data,
2985 print_queue_struct *queue,
2986 NT_PRINTER_INFO_LEVEL *printer,
2987 TALLOC_CTX *mem_ctx)
2989 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, printer->info_2->datatype);
2992 /*******************************************************************
2993 * fill a notify_info_data with the security descriptor
2994 * jfm:xxxx send an null pointer to say no security desc
2995 * have to implement security before !
2996 ********************************************************************/
2998 static void spoolss_notify_security_desc(int snum,
2999 struct spoolss_Notify *data,
3000 print_queue_struct *queue,
3001 NT_PRINTER_INFO_LEVEL *printer,
3002 TALLOC_CTX *mem_ctx)
3004 SETUP_SPOOLSS_NOTIFY_DATA_SECDESC(data,
3005 printer->info_2->secdesc_buf->sd_size,
3006 printer->info_2->secdesc_buf->sd);
3009 /*******************************************************************
3010 * fill a notify_info_data with the attributes
3011 * jfm:xxxx a samba printer is always shared
3012 ********************************************************************/
3014 void spoolss_notify_attributes(int snum,
3015 struct spoolss_Notify *data,
3016 print_queue_struct *queue,
3017 NT_PRINTER_INFO_LEVEL *printer,
3018 TALLOC_CTX *mem_ctx)
3020 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, printer->info_2->attributes);
3023 /*******************************************************************
3024 * fill a notify_info_data with the priority
3025 ********************************************************************/
3027 static void spoolss_notify_priority(int snum,
3028 struct spoolss_Notify *data,
3029 print_queue_struct *queue,
3030 NT_PRINTER_INFO_LEVEL *printer,
3031 TALLOC_CTX *mem_ctx)
3033 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, printer->info_2->priority);
3036 /*******************************************************************
3037 * fill a notify_info_data with the default priority
3038 ********************************************************************/
3040 static void spoolss_notify_default_priority(int snum,
3041 struct spoolss_Notify *data,
3042 print_queue_struct *queue,
3043 NT_PRINTER_INFO_LEVEL *printer,
3044 TALLOC_CTX *mem_ctx)
3046 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, printer->info_2->default_priority);
3049 /*******************************************************************
3050 * fill a notify_info_data with the start time
3051 ********************************************************************/
3053 static void spoolss_notify_start_time(int snum,
3054 struct spoolss_Notify *data,
3055 print_queue_struct *queue,
3056 NT_PRINTER_INFO_LEVEL *printer,
3057 TALLOC_CTX *mem_ctx)
3059 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, printer->info_2->starttime);
3062 /*******************************************************************
3063 * fill a notify_info_data with the until time
3064 ********************************************************************/
3066 static void spoolss_notify_until_time(int snum,
3067 struct spoolss_Notify *data,
3068 print_queue_struct *queue,
3069 NT_PRINTER_INFO_LEVEL *printer,
3070 TALLOC_CTX *mem_ctx)
3072 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, printer->info_2->untiltime);
3075 /*******************************************************************
3076 * fill a notify_info_data with the status
3077 ********************************************************************/
3079 static void spoolss_notify_status(int snum,
3080 struct spoolss_Notify *data,
3081 print_queue_struct *queue,
3082 NT_PRINTER_INFO_LEVEL *printer,
3083 TALLOC_CTX *mem_ctx)
3085 print_status_struct status;
3087 print_queue_length(snum, &status);
3088 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, status.status);
3091 /*******************************************************************
3092 * fill a notify_info_data with the number of jobs queued
3093 ********************************************************************/
3095 void spoolss_notify_cjobs(int snum,
3096 struct spoolss_Notify *data,
3097 print_queue_struct *queue,
3098 NT_PRINTER_INFO_LEVEL *printer,
3099 TALLOC_CTX *mem_ctx)
3101 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, print_queue_length(snum, NULL));
3104 /*******************************************************************
3105 * fill a notify_info_data with the average ppm
3106 ********************************************************************/
3108 static void spoolss_notify_average_ppm(int snum,
3109 struct spoolss_Notify *data,
3110 print_queue_struct *queue,
3111 NT_PRINTER_INFO_LEVEL *printer,
3112 TALLOC_CTX *mem_ctx)
3114 /* always respond 8 pages per minutes */
3115 /* a little hard ! */
3116 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, printer->info_2->averageppm);
3119 /*******************************************************************
3120 * fill a notify_info_data with username
3121 ********************************************************************/
3123 static void spoolss_notify_username(int snum,
3124 struct spoolss_Notify *data,
3125 print_queue_struct *queue,
3126 NT_PRINTER_INFO_LEVEL *printer,
3127 TALLOC_CTX *mem_ctx)
3129 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, queue->fs_user);
3132 /*******************************************************************
3133 * fill a notify_info_data with job status
3134 ********************************************************************/
3136 static void spoolss_notify_job_status(int snum,
3137 struct spoolss_Notify *data,
3138 print_queue_struct *queue,
3139 NT_PRINTER_INFO_LEVEL *printer,
3140 TALLOC_CTX *mem_ctx)
3142 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, nt_printj_status(queue->status));
3145 /*******************************************************************
3146 * fill a notify_info_data with job name
3147 ********************************************************************/
3149 static void spoolss_notify_job_name(int snum,
3150 struct spoolss_Notify *data,
3151 print_queue_struct *queue,
3152 NT_PRINTER_INFO_LEVEL *printer,
3153 TALLOC_CTX *mem_ctx)
3155 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, queue->fs_file);
3158 /*******************************************************************
3159 * fill a notify_info_data with job status
3160 ********************************************************************/
3162 static void spoolss_notify_job_status_string(int snum,
3163 struct spoolss_Notify *data,
3164 print_queue_struct *queue,
3165 NT_PRINTER_INFO_LEVEL *printer,
3166 TALLOC_CTX *mem_ctx)
3169 * Now we're returning job status codes we just return a "" here. JRA.
3172 const char *p = "";
3174 #if 0 /* NO LONGER NEEDED - JRA. 02/22/2001 */
3175 p = "unknown";
3177 switch (queue->status) {
3178 case LPQ_QUEUED:
3179 p = "Queued";
3180 break;
3181 case LPQ_PAUSED:
3182 p = ""; /* NT provides the paused string */
3183 break;
3184 case LPQ_SPOOLING:
3185 p = "Spooling";
3186 break;
3187 case LPQ_PRINTING:
3188 p = "Printing";
3189 break;
3191 #endif /* NO LONGER NEEDED. */
3193 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, p);
3196 /*******************************************************************
3197 * fill a notify_info_data with job time
3198 ********************************************************************/
3200 static void spoolss_notify_job_time(int snum,
3201 struct spoolss_Notify *data,
3202 print_queue_struct *queue,
3203 NT_PRINTER_INFO_LEVEL *printer,
3204 TALLOC_CTX *mem_ctx)
3206 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, 0);
3209 /*******************************************************************
3210 * fill a notify_info_data with job size
3211 ********************************************************************/
3213 static void spoolss_notify_job_size(int snum,
3214 struct spoolss_Notify *data,
3215 print_queue_struct *queue,
3216 NT_PRINTER_INFO_LEVEL *printer,
3217 TALLOC_CTX *mem_ctx)
3219 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, queue->size);
3222 /*******************************************************************
3223 * fill a notify_info_data with page info
3224 ********************************************************************/
3225 static void spoolss_notify_total_pages(int snum,
3226 struct spoolss_Notify *data,
3227 print_queue_struct *queue,
3228 NT_PRINTER_INFO_LEVEL *printer,
3229 TALLOC_CTX *mem_ctx)
3231 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, queue->page_count);
3234 /*******************************************************************
3235 * fill a notify_info_data with pages printed info.
3236 ********************************************************************/
3237 static void spoolss_notify_pages_printed(int snum,
3238 struct spoolss_Notify *data,
3239 print_queue_struct *queue,
3240 NT_PRINTER_INFO_LEVEL *printer,
3241 TALLOC_CTX *mem_ctx)
3243 /* Add code when back-end tracks this */
3244 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, 0);
3247 /*******************************************************************
3248 Fill a notify_info_data with job position.
3249 ********************************************************************/
3251 static void spoolss_notify_job_position(int snum,
3252 struct spoolss_Notify *data,
3253 print_queue_struct *queue,
3254 NT_PRINTER_INFO_LEVEL *printer,
3255 TALLOC_CTX *mem_ctx)
3257 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, queue->job);
3260 /*******************************************************************
3261 Fill a notify_info_data with submitted time.
3262 ********************************************************************/
3264 static void spoolss_notify_submitted_time(int snum,
3265 struct spoolss_Notify *data,
3266 print_queue_struct *queue,
3267 NT_PRINTER_INFO_LEVEL *printer,
3268 TALLOC_CTX *mem_ctx)
3270 data->data.string.string = NULL;
3271 data->data.string.size = 0;
3273 init_systemtime_buffer(mem_ctx, gmtime(&queue->time),
3274 &data->data.string.string,
3275 &data->data.string.size);
3279 struct s_notify_info_data_table
3281 enum spoolss_NotifyType type;
3282 enum spoolss_Field field;
3283 const char *name;
3284 enum spoolss_NotifyTable variable_type;
3285 void (*fn) (int snum, struct spoolss_Notify *data,
3286 print_queue_struct *queue,
3287 NT_PRINTER_INFO_LEVEL *printer, TALLOC_CTX *mem_ctx);
3290 /* A table describing the various print notification constants and
3291 whether the notification data is a pointer to a variable sized
3292 buffer, a one value uint32 or a two value uint32. */
3294 static const struct s_notify_info_data_table notify_info_data_table[] =
3296 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_SERVER_NAME, "PRINTER_NOTIFY_SERVER_NAME", NOTIFY_TABLE_STRING, spoolss_notify_server_name },
3297 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_PRINTER_NAME, "PRINTER_NOTIFY_PRINTER_NAME", NOTIFY_TABLE_STRING, spoolss_notify_printer_name },
3298 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_SHARE_NAME, "PRINTER_NOTIFY_SHARE_NAME", NOTIFY_TABLE_STRING, spoolss_notify_share_name },
3299 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_PORT_NAME, "PRINTER_NOTIFY_PORT_NAME", NOTIFY_TABLE_STRING, spoolss_notify_port_name },
3300 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_DRIVER_NAME, "PRINTER_NOTIFY_DRIVER_NAME", NOTIFY_TABLE_STRING, spoolss_notify_driver_name },
3301 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_COMMENT, "PRINTER_NOTIFY_COMMENT", NOTIFY_TABLE_STRING, spoolss_notify_comment },
3302 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_LOCATION, "PRINTER_NOTIFY_LOCATION", NOTIFY_TABLE_STRING, spoolss_notify_location },
3303 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_DEVMODE, "PRINTER_NOTIFY_DEVMODE", NOTIFY_TABLE_DEVMODE, spoolss_notify_devmode },
3304 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_SEPFILE, "PRINTER_NOTIFY_SEPFILE", NOTIFY_TABLE_STRING, spoolss_notify_sepfile },
3305 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_PRINT_PROCESSOR, "PRINTER_NOTIFY_PRINT_PROCESSOR", NOTIFY_TABLE_STRING, spoolss_notify_print_processor },
3306 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_PARAMETERS, "PRINTER_NOTIFY_PARAMETERS", NOTIFY_TABLE_STRING, spoolss_notify_parameters },
3307 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_DATATYPE, "PRINTER_NOTIFY_DATATYPE", NOTIFY_TABLE_STRING, spoolss_notify_datatype },
3308 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_SECURITY_DESCRIPTOR, "PRINTER_NOTIFY_SECURITY_DESCRIPTOR", NOTIFY_TABLE_SECURITYDESCRIPTOR, spoolss_notify_security_desc },
3309 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_ATTRIBUTES, "PRINTER_NOTIFY_ATTRIBUTES", NOTIFY_TABLE_DWORD, spoolss_notify_attributes },
3310 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_PRIORITY, "PRINTER_NOTIFY_PRIORITY", NOTIFY_TABLE_DWORD, spoolss_notify_priority },
3311 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_DEFAULT_PRIORITY, "PRINTER_NOTIFY_DEFAULT_PRIORITY", NOTIFY_TABLE_DWORD, spoolss_notify_default_priority },
3312 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_START_TIME, "PRINTER_NOTIFY_START_TIME", NOTIFY_TABLE_DWORD, spoolss_notify_start_time },
3313 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_UNTIL_TIME, "PRINTER_NOTIFY_UNTIL_TIME", NOTIFY_TABLE_DWORD, spoolss_notify_until_time },
3314 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_STATUS, "PRINTER_NOTIFY_STATUS", NOTIFY_TABLE_DWORD, spoolss_notify_status },
3315 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_STATUS_STRING, "PRINTER_NOTIFY_STATUS_STRING", NOTIFY_TABLE_STRING, NULL },
3316 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_CJOBS, "PRINTER_NOTIFY_CJOBS", NOTIFY_TABLE_DWORD, spoolss_notify_cjobs },
3317 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_AVERAGE_PPM, "PRINTER_NOTIFY_AVERAGE_PPM", NOTIFY_TABLE_DWORD, spoolss_notify_average_ppm },
3318 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_TOTAL_PAGES, "PRINTER_NOTIFY_TOTAL_PAGES", NOTIFY_TABLE_DWORD, NULL },
3319 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_PAGES_PRINTED, "PRINTER_NOTIFY_PAGES_PRINTED", NOTIFY_TABLE_DWORD, NULL },
3320 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_TOTAL_BYTES, "PRINTER_NOTIFY_TOTAL_BYTES", NOTIFY_TABLE_DWORD, NULL },
3321 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_BYTES_PRINTED, "PRINTER_NOTIFY_BYTES_PRINTED", NOTIFY_TABLE_DWORD, NULL },
3322 { JOB_NOTIFY_TYPE, JOB_NOTIFY_PRINTER_NAME, "JOB_NOTIFY_PRINTER_NAME", NOTIFY_TABLE_STRING, spoolss_notify_printer_name },
3323 { JOB_NOTIFY_TYPE, JOB_NOTIFY_MACHINE_NAME, "JOB_NOTIFY_MACHINE_NAME", NOTIFY_TABLE_STRING, spoolss_notify_server_name },
3324 { JOB_NOTIFY_TYPE, JOB_NOTIFY_PORT_NAME, "JOB_NOTIFY_PORT_NAME", NOTIFY_TABLE_STRING, spoolss_notify_port_name },
3325 { JOB_NOTIFY_TYPE, JOB_NOTIFY_USER_NAME, "JOB_NOTIFY_USER_NAME", NOTIFY_TABLE_STRING, spoolss_notify_username },
3326 { JOB_NOTIFY_TYPE, JOB_NOTIFY_NOTIFY_NAME, "JOB_NOTIFY_NOTIFY_NAME", NOTIFY_TABLE_STRING, spoolss_notify_username },
3327 { JOB_NOTIFY_TYPE, JOB_NOTIFY_DATATYPE, "JOB_NOTIFY_DATATYPE", NOTIFY_TABLE_STRING, spoolss_notify_datatype },
3328 { JOB_NOTIFY_TYPE, JOB_NOTIFY_PRINT_PROCESSOR, "JOB_NOTIFY_PRINT_PROCESSOR", NOTIFY_TABLE_STRING, spoolss_notify_print_processor },
3329 { JOB_NOTIFY_TYPE, JOB_NOTIFY_PARAMETERS, "JOB_NOTIFY_PARAMETERS", NOTIFY_TABLE_STRING, spoolss_notify_parameters },
3330 { JOB_NOTIFY_TYPE, JOB_NOTIFY_DRIVER_NAME, "JOB_NOTIFY_DRIVER_NAME", NOTIFY_TABLE_STRING, spoolss_notify_driver_name },
3331 { JOB_NOTIFY_TYPE, JOB_NOTIFY_DEVMODE, "JOB_NOTIFY_DEVMODE", NOTIFY_TABLE_DEVMODE, spoolss_notify_devmode },
3332 { JOB_NOTIFY_TYPE, JOB_NOTIFY_STATUS, "JOB_NOTIFY_STATUS", NOTIFY_TABLE_DWORD, spoolss_notify_job_status },
3333 { JOB_NOTIFY_TYPE, JOB_NOTIFY_STATUS_STRING, "JOB_NOTIFY_STATUS_STRING", NOTIFY_TABLE_STRING, spoolss_notify_job_status_string },
3334 { JOB_NOTIFY_TYPE, JOB_NOTIFY_SECURITY_DESCRIPTOR, "JOB_NOTIFY_SECURITY_DESCRIPTOR", NOTIFY_TABLE_SECURITYDESCRIPTOR, NULL },
3335 { JOB_NOTIFY_TYPE, JOB_NOTIFY_DOCUMENT, "JOB_NOTIFY_DOCUMENT", NOTIFY_TABLE_STRING, spoolss_notify_job_name },
3336 { JOB_NOTIFY_TYPE, JOB_NOTIFY_PRIORITY, "JOB_NOTIFY_PRIORITY", NOTIFY_TABLE_DWORD, spoolss_notify_priority },
3337 { JOB_NOTIFY_TYPE, JOB_NOTIFY_POSITION, "JOB_NOTIFY_POSITION", NOTIFY_TABLE_DWORD, spoolss_notify_job_position },
3338 { JOB_NOTIFY_TYPE, JOB_NOTIFY_SUBMITTED, "JOB_NOTIFY_SUBMITTED", NOTIFY_TABLE_TIME, spoolss_notify_submitted_time },
3339 { JOB_NOTIFY_TYPE, JOB_NOTIFY_START_TIME, "JOB_NOTIFY_START_TIME", NOTIFY_TABLE_DWORD, spoolss_notify_start_time },
3340 { JOB_NOTIFY_TYPE, JOB_NOTIFY_UNTIL_TIME, "JOB_NOTIFY_UNTIL_TIME", NOTIFY_TABLE_DWORD, spoolss_notify_until_time },
3341 { JOB_NOTIFY_TYPE, JOB_NOTIFY_TIME, "JOB_NOTIFY_TIME", NOTIFY_TABLE_DWORD, spoolss_notify_job_time },
3342 { JOB_NOTIFY_TYPE, JOB_NOTIFY_TOTAL_PAGES, "JOB_NOTIFY_TOTAL_PAGES", NOTIFY_TABLE_DWORD, spoolss_notify_total_pages },
3343 { JOB_NOTIFY_TYPE, JOB_NOTIFY_PAGES_PRINTED, "JOB_NOTIFY_PAGES_PRINTED", NOTIFY_TABLE_DWORD, spoolss_notify_pages_printed },
3344 { JOB_NOTIFY_TYPE, JOB_NOTIFY_TOTAL_BYTES, "JOB_NOTIFY_TOTAL_BYTES", NOTIFY_TABLE_DWORD, spoolss_notify_job_size },
3347 /*******************************************************************
3348 Return the variable_type of info_data structure.
3349 ********************************************************************/
3351 static uint32_t variable_type_of_notify_info_data(enum spoolss_NotifyType type,
3352 enum spoolss_Field field)
3354 int i=0;
3356 for (i = 0; i < ARRAY_SIZE(notify_info_data_table); i++) {
3357 if ( (notify_info_data_table[i].type == type) &&
3358 (notify_info_data_table[i].field == field) ) {
3359 return notify_info_data_table[i].variable_type;
3363 DEBUG(5, ("invalid notify data type %d/%d\n", type, field));
3365 return 0;
3368 /****************************************************************************
3369 ****************************************************************************/
3371 static bool search_notify(enum spoolss_NotifyType type,
3372 enum spoolss_Field field,
3373 int *value)
3375 int i;
3377 for (i = 0; i < ARRAY_SIZE(notify_info_data_table); i++) {
3378 if (notify_info_data_table[i].type == type &&
3379 notify_info_data_table[i].field == field &&
3380 notify_info_data_table[i].fn != NULL) {
3381 *value = i;
3382 return True;
3386 return False;
3389 /****************************************************************************
3390 ****************************************************************************/
3392 void construct_info_data(struct spoolss_Notify *info_data,
3393 enum spoolss_NotifyType type,
3394 enum spoolss_Field field,
3395 int id)
3397 info_data->type = type;
3398 info_data->field = field;
3399 info_data->variable_type = variable_type_of_notify_info_data(type, field);
3400 info_data->job_id = id;
3403 /*******************************************************************
3405 * fill a notify_info struct with info asked
3407 ********************************************************************/
3409 static bool construct_notify_printer_info(Printer_entry *print_hnd,
3410 struct spoolss_NotifyInfo *info,
3411 int snum,
3412 const struct spoolss_NotifyOptionType *option_type,
3413 uint32_t id,
3414 TALLOC_CTX *mem_ctx)
3416 int field_num,j;
3417 enum spoolss_NotifyType type;
3418 enum spoolss_Field field;
3420 struct spoolss_Notify *current_data;
3421 NT_PRINTER_INFO_LEVEL *printer = NULL;
3422 print_queue_struct *queue=NULL;
3424 type = option_type->type;
3426 DEBUG(4,("construct_notify_printer_info: Notify type: [%s], number of notify info: [%d] on printer: [%s]\n",
3427 (type == PRINTER_NOTIFY_TYPE ? "PRINTER_NOTIFY_TYPE" : "JOB_NOTIFY_TYPE"),
3428 option_type->count, lp_servicename(snum)));
3430 if (!W_ERROR_IS_OK(get_a_printer(print_hnd, &printer, 2, lp_const_servicename(snum))))
3431 return False;
3433 for(field_num=0; field_num < option_type->count; field_num++) {
3434 field = option_type->fields[field_num];
3436 DEBUG(4,("construct_notify_printer_info: notify [%d]: type [%x], field [%x]\n", field_num, type, field));
3438 if (!search_notify(type, field, &j) )
3439 continue;
3441 info->notifies = TALLOC_REALLOC_ARRAY(info, info->notifies,
3442 struct spoolss_Notify,
3443 info->count + 1);
3444 if (info->notifies == NULL) {
3445 DEBUG(2,("construct_notify_printer_info: failed to enlarge buffer info->data!\n"));
3446 free_a_printer(&printer, 2);
3447 return False;
3450 current_data = &info->notifies[info->count];
3452 construct_info_data(current_data, type, field, id);
3454 DEBUG(10,("construct_notify_printer_info: calling [%s] snum=%d printername=[%s])\n",
3455 notify_info_data_table[j].name, snum, printer->info_2->printername ));
3457 notify_info_data_table[j].fn(snum, current_data, queue,
3458 printer, mem_ctx);
3460 info->count++;
3463 free_a_printer(&printer, 2);
3464 return True;
3467 /*******************************************************************
3469 * fill a notify_info struct with info asked
3471 ********************************************************************/
3473 static bool construct_notify_jobs_info(print_queue_struct *queue,
3474 struct spoolss_NotifyInfo *info,
3475 NT_PRINTER_INFO_LEVEL *printer,
3476 int snum,
3477 const struct spoolss_NotifyOptionType *option_type,
3478 uint32_t id,
3479 TALLOC_CTX *mem_ctx)
3481 int field_num,j;
3482 enum spoolss_NotifyType type;
3483 enum spoolss_Field field;
3484 struct spoolss_Notify *current_data;
3486 DEBUG(4,("construct_notify_jobs_info\n"));
3488 type = option_type->type;
3490 DEBUGADD(4,("Notify type: [%s], number of notify info: [%d]\n",
3491 (type == PRINTER_NOTIFY_TYPE ? "PRINTER_NOTIFY_TYPE" : "JOB_NOTIFY_TYPE"),
3492 option_type->count));
3494 for(field_num=0; field_num<option_type->count; field_num++) {
3495 field = option_type->fields[field_num];
3497 if (!search_notify(type, field, &j) )
3498 continue;
3500 info->notifies = TALLOC_REALLOC_ARRAY(info, info->notifies,
3501 struct spoolss_Notify,
3502 info->count + 1);
3503 if (info->notifies == NULL) {
3504 DEBUG(2,("construct_notify_jobs_info: failed to enlarg buffer info->data!\n"));
3505 return False;
3508 current_data=&(info->notifies[info->count]);
3510 construct_info_data(current_data, type, field, id);
3511 notify_info_data_table[j].fn(snum, current_data, queue,
3512 printer, mem_ctx);
3513 info->count++;
3516 return True;
3520 * JFM: The enumeration is not that simple, it's even non obvious.
3522 * let's take an example: I want to monitor the PRINTER SERVER for
3523 * the printer's name and the number of jobs currently queued.
3524 * So in the NOTIFY_OPTION, I have one NOTIFY_OPTION_TYPE structure.
3525 * Its type is PRINTER_NOTIFY_TYPE and it has 2 fields NAME and CJOBS.
3527 * I have 3 printers on the back of my server.
3529 * Now the response is a NOTIFY_INFO structure, with 6 NOTIFY_INFO_DATA
3530 * structures.
3531 * Number Data Id
3532 * 1 printer 1 name 1
3533 * 2 printer 1 cjob 1
3534 * 3 printer 2 name 2
3535 * 4 printer 2 cjob 2
3536 * 5 printer 3 name 3
3537 * 6 printer 3 name 3
3539 * that's the print server case, the printer case is even worse.
3542 /*******************************************************************
3544 * enumerate all printers on the printserver
3545 * fill a notify_info struct with info asked
3547 ********************************************************************/
3549 static WERROR printserver_notify_info(pipes_struct *p, POLICY_HND *hnd,
3550 struct spoolss_NotifyInfo *info,
3551 TALLOC_CTX *mem_ctx)
3553 int snum;
3554 Printer_entry *Printer=find_printer_index_by_hnd(p, hnd);
3555 int n_services=lp_numservices();
3556 int i;
3557 struct spoolss_NotifyOption *option;
3558 struct spoolss_NotifyOptionType option_type;
3560 DEBUG(4,("printserver_notify_info\n"));
3562 if (!Printer)
3563 return WERR_BADFID;
3565 option = Printer->notify.option;
3567 info->version = 2;
3568 info->notifies = NULL;
3569 info->count = 0;
3571 /* a bug in xp sp2 rc2 causes it to send a fnpcn request without
3572 sending a ffpcn() request first */
3574 if ( !option )
3575 return WERR_BADFID;
3577 for (i=0; i<option->count; i++) {
3578 option_type = option->types[i];
3580 if (option_type.type != PRINTER_NOTIFY_TYPE)
3581 continue;
3583 for (snum=0; snum<n_services; snum++)
3585 if ( lp_browseable(snum) && lp_snum_ok(snum) && lp_print_ok(snum) )
3586 construct_notify_printer_info ( Printer, info, snum, &option_type, snum, mem_ctx );
3590 #if 0
3592 * Debugging information, don't delete.
3595 DEBUG(1,("dumping the NOTIFY_INFO\n"));
3596 DEBUGADD(1,("info->version:[%d], info->flags:[%d], info->count:[%d]\n", info->version, info->flags, info->count));
3597 DEBUGADD(1,("num\ttype\tfield\tres\tid\tsize\tenc_type\n"));
3599 for (i=0; i<info->count; i++) {
3600 DEBUGADD(1,("[%d]\t[%d]\t[%d]\t[%d]\t[%d]\t[%d]\t[%d]\n",
3601 i, info->data[i].type, info->data[i].field, info->data[i].reserved,
3602 info->data[i].id, info->data[i].size, info->data[i].enc_type));
3604 #endif
3606 return WERR_OK;
3609 /*******************************************************************
3611 * fill a notify_info struct with info asked
3613 ********************************************************************/
3615 static WERROR printer_notify_info(pipes_struct *p, POLICY_HND *hnd, struct spoolss_NotifyInfo *info,
3616 TALLOC_CTX *mem_ctx)
3618 int snum;
3619 Printer_entry *Printer=find_printer_index_by_hnd(p, hnd);
3620 int i;
3621 uint32 id;
3622 struct spoolss_NotifyOption *option;
3623 struct spoolss_NotifyOptionType option_type;
3624 int count,j;
3625 print_queue_struct *queue=NULL;
3626 print_status_struct status;
3628 DEBUG(4,("printer_notify_info\n"));
3630 if (!Printer)
3631 return WERR_BADFID;
3633 option = Printer->notify.option;
3634 id = 0x0;
3636 info->version = 2;
3637 info->notifies = NULL;
3638 info->count = 0;
3640 /* a bug in xp sp2 rc2 causes it to send a fnpcn request without
3641 sending a ffpcn() request first */
3643 if ( !option )
3644 return WERR_BADFID;
3646 get_printer_snum(p, hnd, &snum, NULL);
3648 for (i=0; i<option->count; i++) {
3649 option_type = option->types[i];
3651 switch (option_type.type) {
3652 case PRINTER_NOTIFY_TYPE:
3653 if(construct_notify_printer_info(Printer, info, snum,
3654 &option_type, id,
3655 mem_ctx))
3656 id--;
3657 break;
3659 case JOB_NOTIFY_TYPE: {
3660 NT_PRINTER_INFO_LEVEL *printer = NULL;
3662 count = print_queue_status(snum, &queue, &status);
3664 if (!W_ERROR_IS_OK(get_a_printer(Printer, &printer, 2, lp_const_servicename(snum))))
3665 goto done;
3667 for (j=0; j<count; j++) {
3668 construct_notify_jobs_info(&queue[j], info,
3669 printer, snum,
3670 &option_type,
3671 queue[j].job,
3672 mem_ctx);
3675 free_a_printer(&printer, 2);
3677 done:
3678 SAFE_FREE(queue);
3679 break;
3685 * Debugging information, don't delete.
3688 DEBUG(1,("dumping the NOTIFY_INFO\n"));
3689 DEBUGADD(1,("info->version:[%d], info->flags:[%d], info->count:[%d]\n", info->version, info->flags, info->count));
3690 DEBUGADD(1,("num\ttype\tfield\tres\tid\tsize\tenc_type\n"));
3692 for (i=0; i<info->count; i++) {
3693 DEBUGADD(1,("[%d]\t[%d]\t[%d]\t[%d]\t[%d]\t[%d]\t[%d]\n",
3694 i, info->data[i].type, info->data[i].field, info->data[i].reserved,
3695 info->data[i].id, info->data[i].size, info->data[i].enc_type));
3698 return WERR_OK;
3701 /****************************************************************
3702 _spoolss_RouterRefreshPrinterChangeNotify
3703 ****************************************************************/
3705 WERROR _spoolss_RouterRefreshPrinterChangeNotify(pipes_struct *p,
3706 struct spoolss_RouterRefreshPrinterChangeNotify *r)
3708 POLICY_HND *handle = r->in.handle;
3709 struct spoolss_NotifyInfo *info;
3711 Printer_entry *Printer=find_printer_index_by_hnd(p, handle);
3712 WERROR result = WERR_BADFID;
3714 /* we always have a spoolss_NotifyInfo struct */
3715 info = talloc_zero(p->mem_ctx, struct spoolss_NotifyInfo);
3716 if (!info) {
3717 result = WERR_NOMEM;
3718 goto done;
3721 *r->out.info = info;
3723 if (!Printer) {
3724 DEBUG(2,("_spoolss_RouterRefreshPrinterChangeNotify: "
3725 "Invalid handle (%s:%u:%u).\n", OUR_HANDLE(handle)));
3726 goto done;
3729 DEBUG(4,("Printer type %x\n",Printer->printer_type));
3732 * We are now using the change value, and
3733 * I should check for PRINTER_NOTIFY_OPTIONS_REFRESH but as
3734 * I don't have a global notification system, I'm sending back all the
3735 * informations even when _NOTHING_ has changed.
3738 /* We need to keep track of the change value to send back in
3739 RRPCN replies otherwise our updates are ignored. */
3741 Printer->notify.fnpcn = True;
3743 if (Printer->notify.client_connected) {
3744 DEBUG(10,("_spoolss_RouterRefreshPrinterChangeNotify: "
3745 "Saving change value in request [%x]\n",
3746 r->in.change_low));
3747 Printer->notify.change = r->in.change_low;
3750 /* just ignore the spoolss_NotifyOption */
3752 switch (Printer->printer_type) {
3753 case SPLHND_SERVER:
3754 result = printserver_notify_info(p, handle, info, p->mem_ctx);
3755 break;
3757 case SPLHND_PRINTER:
3758 result = printer_notify_info(p, handle, info, p->mem_ctx);
3759 break;
3762 Printer->notify.fnpcn = False;
3764 done:
3765 return result;
3768 /********************************************************************
3769 * construct_printer_info_0
3770 * fill a printer_info_0 struct
3771 ********************************************************************/
3773 static WERROR construct_printer_info0(TALLOC_CTX *mem_ctx,
3774 const NT_PRINTER_INFO_LEVEL *ntprinter,
3775 struct spoolss_PrinterInfo0 *r,
3776 int snum)
3778 int count;
3779 counter_printer_0 *session_counter;
3780 time_t setuptime;
3781 print_status_struct status;
3783 r->printername = talloc_strdup(mem_ctx, ntprinter->info_2->printername);
3784 W_ERROR_HAVE_NO_MEMORY(r->printername);
3786 r->servername = talloc_strdup(mem_ctx, ntprinter->info_2->servername);
3787 W_ERROR_HAVE_NO_MEMORY(r->servername);
3789 count = print_queue_length(snum, &status);
3791 /* check if we already have a counter for this printer */
3792 for (session_counter = counter_list; session_counter; session_counter = session_counter->next) {
3793 if (session_counter->snum == snum)
3794 break;
3797 /* it's the first time, add it to the list */
3798 if (session_counter == NULL) {
3799 session_counter = SMB_MALLOC_P(counter_printer_0);
3800 W_ERROR_HAVE_NO_MEMORY(session_counter);
3801 ZERO_STRUCTP(session_counter);
3802 session_counter->snum = snum;
3803 session_counter->counter = 0;
3804 DLIST_ADD(counter_list, session_counter);
3807 /* increment it */
3808 session_counter->counter++;
3810 r->cjobs = count;
3811 r->total_jobs = 0;
3812 r->total_bytes = 0;
3814 setuptime = (time_t)ntprinter->info_2->setuptime;
3816 init_systemtime(&r->time, gmtime(&setuptime));
3818 /* JFM:
3819 * the global_counter should be stored in a TDB as it's common to all the clients
3820 * and should be zeroed on samba startup
3822 r->global_counter = session_counter->counter;
3823 r->total_pages = 0;
3824 /* in 2.2 we reported ourselves as 0x0004 and 0x0565 */
3825 r->version = 0x0005; /* NT 5 */
3826 r->free_build = 0x0893; /* build 2195 */
3827 r->spooling = 0;
3828 r->max_spooling = 0;
3829 r->session_counter = session_counter->counter;
3830 r->num_error_out_of_paper = 0x0;
3831 r->num_error_not_ready = 0x0; /* number of print failure */
3832 r->job_error = 0x0;
3833 r->number_of_processors = 0x1;
3834 r->processor_type = PROCESSOR_INTEL_PENTIUM; /* 586 Pentium ? */
3835 r->high_part_total_bytes = 0x0;
3836 r->change_id = ntprinter->info_2->changeid; /* ChangeID in milliseconds*/
3837 r->last_error = WERR_OK;
3838 r->status = nt_printq_status(status.status);
3839 r->enumerate_network_printers = 0x0;
3840 r->c_setprinter = get_c_setprinter(); /* monotonically increasing sum of delta printer counts */
3841 r->processor_architecture = 0x0;
3842 r->processor_level = 0x6; /* 6 ???*/
3843 r->ref_ic = 0;
3844 r->reserved2 = 0;
3845 r->reserved3 = 0;
3847 return WERR_OK;
3850 /****************************************************************************
3851 Convert an NT_DEVICEMODE to a spoolss_DeviceMode structure. Both pointers
3852 should be valid upon entry
3853 ****************************************************************************/
3855 static WERROR convert_nt_devicemode(TALLOC_CTX *mem_ctx,
3856 struct spoolss_DeviceMode *r,
3857 const NT_DEVICEMODE *ntdevmode)
3859 if (!r || !ntdevmode) {
3860 return WERR_INVALID_PARAM;
3863 r->devicename = talloc_strdup(mem_ctx, ntdevmode->devicename);
3864 W_ERROR_HAVE_NO_MEMORY(r->devicename);
3866 r->specversion = ntdevmode->specversion;
3867 r->driverversion = ntdevmode->driverversion;
3868 r->size = ntdevmode->size;
3869 r->__driverextra_length = ntdevmode->driverextra;
3870 r->fields = ntdevmode->fields;
3872 r->orientation = ntdevmode->orientation;
3873 r->papersize = ntdevmode->papersize;
3874 r->paperlength = ntdevmode->paperlength;
3875 r->paperwidth = ntdevmode->paperwidth;
3876 r->scale = ntdevmode->scale;
3877 r->copies = ntdevmode->copies;
3878 r->defaultsource = ntdevmode->defaultsource;
3879 r->printquality = ntdevmode->printquality;
3880 r->color = ntdevmode->color;
3881 r->duplex = ntdevmode->duplex;
3882 r->yresolution = ntdevmode->yresolution;
3883 r->ttoption = ntdevmode->ttoption;
3884 r->collate = ntdevmode->collate;
3886 r->formname = talloc_strdup(mem_ctx, ntdevmode->formname);
3887 W_ERROR_HAVE_NO_MEMORY(r->formname);
3889 /* all 0 below are values that have not been set in the old parsing/copy
3890 * function, maybe they should... - gd */
3892 r->logpixels = 0;
3893 r->bitsperpel = 0;
3894 r->pelswidth = 0;
3895 r->pelsheight = 0;
3896 r->displayflags = 0;
3897 r->displayfrequency = 0;
3898 r->icmmethod = ntdevmode->icmmethod;
3899 r->icmintent = ntdevmode->icmintent;
3900 r->mediatype = ntdevmode->mediatype;
3901 r->dithertype = ntdevmode->dithertype;
3902 r->reserved1 = 0;
3903 r->reserved2 = 0;
3904 r->panningwidth = 0;
3905 r->panningheight = 0;
3907 if (ntdevmode->nt_dev_private != NULL) {
3908 r->driverextra_data = data_blob_talloc(mem_ctx,
3909 ntdevmode->nt_dev_private,
3910 ntdevmode->driverextra);
3911 W_ERROR_HAVE_NO_MEMORY(r->driverextra_data.data);
3914 return WERR_OK;
3918 /****************************************************************************
3919 Create a spoolss_DeviceMode struct. Returns talloced memory.
3920 ****************************************************************************/
3922 struct spoolss_DeviceMode *construct_dev_mode(TALLOC_CTX *mem_ctx,
3923 const char *servicename)
3925 WERROR result;
3926 NT_PRINTER_INFO_LEVEL *printer = NULL;
3927 struct spoolss_DeviceMode *devmode = NULL;
3929 DEBUG(7,("construct_dev_mode\n"));
3931 DEBUGADD(8,("getting printer characteristics\n"));
3933 if (!W_ERROR_IS_OK(get_a_printer(NULL, &printer, 2, servicename)))
3934 return NULL;
3936 if (!printer->info_2->devmode) {
3937 DEBUG(5, ("BONG! There was no device mode!\n"));
3938 goto done;
3941 devmode = TALLOC_ZERO_P(mem_ctx, struct spoolss_DeviceMode);
3942 if (!devmode) {
3943 DEBUG(2,("construct_dev_mode: talloc fail.\n"));
3944 goto done;
3947 DEBUGADD(8,("loading DEVICEMODE\n"));
3949 result = convert_nt_devicemode(mem_ctx, devmode, printer->info_2->devmode);
3950 if (!W_ERROR_IS_OK(result)) {
3951 TALLOC_FREE(devmode);
3954 done:
3955 free_a_printer(&printer,2);
3957 return devmode;
3960 /********************************************************************
3961 * construct_printer_info3
3962 * fill a spoolss_PrinterInfo3 struct
3963 ********************************************************************/
3965 static WERROR construct_printer_info3(TALLOC_CTX *mem_ctx,
3966 const NT_PRINTER_INFO_LEVEL *ntprinter,
3967 struct spoolss_PrinterInfo3 *r,
3968 int snum)
3970 /* These are the components of the SD we are returning. */
3972 if (ntprinter->info_2->secdesc_buf && ntprinter->info_2->secdesc_buf->sd_size != 0) {
3973 /* don't use talloc_steal() here unless you do a deep steal of all
3974 the SEC_DESC members */
3976 r->secdesc = dup_sec_desc(mem_ctx,
3977 ntprinter->info_2->secdesc_buf->sd);
3978 W_ERROR_HAVE_NO_MEMORY(r->secdesc);
3981 return WERR_OK;
3984 /********************************************************************
3985 * construct_printer_info4
3986 * fill a spoolss_PrinterInfo4 struct
3987 ********************************************************************/
3989 static WERROR construct_printer_info4(TALLOC_CTX *mem_ctx,
3990 const NT_PRINTER_INFO_LEVEL *ntprinter,
3991 struct spoolss_PrinterInfo4 *r,
3992 int snum)
3994 r->printername = talloc_strdup(mem_ctx, ntprinter->info_2->printername);
3995 W_ERROR_HAVE_NO_MEMORY(r->printername);
3996 r->servername = talloc_strdup(mem_ctx, ntprinter->info_2->servername);
3997 W_ERROR_HAVE_NO_MEMORY(r->servername);
3999 r->attributes = ntprinter->info_2->attributes;
4001 return WERR_OK;
4004 /********************************************************************
4005 * construct_printer_info5
4006 * fill a spoolss_PrinterInfo5 struct
4007 ********************************************************************/
4009 static WERROR construct_printer_info5(TALLOC_CTX *mem_ctx,
4010 const NT_PRINTER_INFO_LEVEL *ntprinter,
4011 struct spoolss_PrinterInfo5 *r,
4012 int snum)
4014 r->printername = talloc_strdup(mem_ctx, ntprinter->info_2->printername);
4015 W_ERROR_HAVE_NO_MEMORY(r->printername);
4016 r->portname = talloc_strdup(mem_ctx, ntprinter->info_2->portname);
4017 W_ERROR_HAVE_NO_MEMORY(r->portname);
4019 r->attributes = ntprinter->info_2->attributes;
4021 /* these two are not used by NT+ according to MSDN */
4023 r->device_not_selected_timeout = 0x0; /* have seen 0x3a98 */
4024 r->transmission_retry_timeout = 0x0; /* have seen 0xafc8 */
4026 return WERR_OK;
4029 /********************************************************************
4030 * construct_printer_info_6
4031 * fill a spoolss_PrinterInfo6 struct
4032 ********************************************************************/
4034 static WERROR construct_printer_info6(TALLOC_CTX *mem_ctx,
4035 const NT_PRINTER_INFO_LEVEL *ntprinter,
4036 struct spoolss_PrinterInfo6 *r,
4037 int snum)
4039 int count;
4040 print_status_struct status;
4042 count = print_queue_length(snum, &status);
4044 r->status = nt_printq_status(status.status);
4046 return WERR_OK;
4049 /********************************************************************
4050 * construct_printer_info7
4051 * fill a spoolss_PrinterInfo7 struct
4052 ********************************************************************/
4054 static WERROR construct_printer_info7(TALLOC_CTX *mem_ctx,
4055 Printer_entry *print_hnd,
4056 struct spoolss_PrinterInfo7 *r,
4057 int snum)
4059 struct GUID guid;
4061 if (is_printer_published(print_hnd, snum, &guid)) {
4062 r->guid = talloc_strdup_upper(mem_ctx, GUID_string2(mem_ctx, &guid));
4063 r->action = DSPRINT_PUBLISH;
4064 } else {
4065 r->guid = talloc_strdup(mem_ctx, "");
4066 r->action = DSPRINT_UNPUBLISH;
4068 W_ERROR_HAVE_NO_MEMORY(r->guid);
4070 return WERR_OK;
4073 /********************************************************************
4074 * construct_printer_info1
4075 * fill a spoolss_PrinterInfo1 struct
4076 ********************************************************************/
4078 static WERROR construct_printer_info1(TALLOC_CTX *mem_ctx,
4079 const NT_PRINTER_INFO_LEVEL *ntprinter,
4080 uint32_t flags,
4081 struct spoolss_PrinterInfo1 *r,
4082 int snum)
4084 char *chaine = NULL;
4085 r->flags = flags;
4087 if (*ntprinter->info_2->comment == '\0') {
4088 r->comment = talloc_strdup(mem_ctx, lp_comment(snum));
4089 chaine = talloc_asprintf(mem_ctx,
4090 "%s,%s,%s", ntprinter->info_2->printername,
4091 ntprinter->info_2->drivername, lp_comment(snum));
4092 } else {
4093 r->comment = talloc_strdup(mem_ctx, ntprinter->info_2->comment); /* saved comment */
4094 chaine = talloc_asprintf(mem_ctx,
4095 "%s,%s,%s", ntprinter->info_2->printername,
4096 ntprinter->info_2->drivername, ntprinter->info_2->comment);
4098 W_ERROR_HAVE_NO_MEMORY(chaine);
4099 W_ERROR_HAVE_NO_MEMORY(r->comment);
4101 r->description = talloc_strdup(mem_ctx, chaine);
4102 W_ERROR_HAVE_NO_MEMORY(r->description);
4103 r->name = talloc_strdup(mem_ctx, ntprinter->info_2->printername);
4104 W_ERROR_HAVE_NO_MEMORY(r->name);
4106 return WERR_OK;
4109 /********************************************************************
4110 * construct_printer_info2
4111 * fill a spoolss_PrinterInfo2 struct
4112 ********************************************************************/
4114 static WERROR construct_printer_info2(TALLOC_CTX *mem_ctx,
4115 const NT_PRINTER_INFO_LEVEL *ntprinter,
4116 struct spoolss_PrinterInfo2 *r,
4117 int snum)
4119 int count;
4121 print_status_struct status;
4123 count = print_queue_length(snum, &status);
4125 r->servername = talloc_strdup(mem_ctx, ntprinter->info_2->servername);
4126 W_ERROR_HAVE_NO_MEMORY(r->servername);
4127 r->printername = talloc_strdup(mem_ctx, ntprinter->info_2->printername);
4128 W_ERROR_HAVE_NO_MEMORY(r->printername);
4129 r->sharename = talloc_strdup(mem_ctx, lp_servicename(snum));
4130 W_ERROR_HAVE_NO_MEMORY(r->sharename);
4131 r->portname = talloc_strdup(mem_ctx, ntprinter->info_2->portname);
4132 W_ERROR_HAVE_NO_MEMORY(r->portname);
4133 r->drivername = talloc_strdup(mem_ctx, ntprinter->info_2->drivername);
4134 W_ERROR_HAVE_NO_MEMORY(r->drivername);
4136 if (*ntprinter->info_2->comment == '\0') {
4137 r->comment = talloc_strdup(mem_ctx, lp_comment(snum));
4138 } else {
4139 r->comment = talloc_strdup(mem_ctx, ntprinter->info_2->comment);
4141 W_ERROR_HAVE_NO_MEMORY(r->comment);
4143 r->location = talloc_strdup(mem_ctx, ntprinter->info_2->location);
4144 W_ERROR_HAVE_NO_MEMORY(r->location);
4145 r->sepfile = talloc_strdup(mem_ctx, ntprinter->info_2->sepfile);
4146 W_ERROR_HAVE_NO_MEMORY(r->sepfile);
4147 r->printprocessor = talloc_strdup(mem_ctx, ntprinter->info_2->printprocessor);
4148 W_ERROR_HAVE_NO_MEMORY(r->printprocessor);
4149 r->datatype = talloc_strdup(mem_ctx, ntprinter->info_2->datatype);
4150 W_ERROR_HAVE_NO_MEMORY(r->datatype);
4151 r->parameters = talloc_strdup(mem_ctx, ntprinter->info_2->parameters);
4152 W_ERROR_HAVE_NO_MEMORY(r->parameters);
4154 r->attributes = ntprinter->info_2->attributes;
4156 r->priority = ntprinter->info_2->priority;
4157 r->defaultpriority = ntprinter->info_2->default_priority;
4158 r->starttime = ntprinter->info_2->starttime;
4159 r->untiltime = ntprinter->info_2->untiltime;
4160 r->status = nt_printq_status(status.status);
4161 r->cjobs = count;
4162 r->averageppm = ntprinter->info_2->averageppm;
4164 r->devmode = construct_dev_mode(mem_ctx, lp_const_servicename(snum));
4165 if (!r->devmode) {
4166 DEBUG(8,("Returning NULL Devicemode!\n"));
4169 r->secdesc = NULL;
4171 if (ntprinter->info_2->secdesc_buf && ntprinter->info_2->secdesc_buf->sd_size != 0) {
4172 /* don't use talloc_steal() here unless you do a deep steal of all
4173 the SEC_DESC members */
4175 r->secdesc = dup_sec_desc(mem_ctx, ntprinter->info_2->secdesc_buf->sd);
4178 return WERR_OK;
4181 /********************************************************************
4182 ********************************************************************/
4184 static bool snum_is_shared_printer(int snum)
4186 return (lp_browseable(snum) && lp_snum_ok(snum) && lp_print_ok(snum));
4189 /********************************************************************
4190 Spoolss_enumprinters.
4191 ********************************************************************/
4193 static WERROR enum_all_printers_info_1(TALLOC_CTX *mem_ctx,
4194 uint32_t flags,
4195 union spoolss_PrinterInfo **info_p,
4196 uint32_t *count)
4198 int snum;
4199 int n_services = lp_numservices();
4200 union spoolss_PrinterInfo *info = NULL;
4201 WERROR result = WERR_OK;
4203 DEBUG(4,("enum_all_printers_info_1\n"));
4205 *count = 0;
4207 for (snum=0; snum<n_services; snum++) {
4209 NT_PRINTER_INFO_LEVEL *ntprinter = NULL;
4210 struct spoolss_PrinterInfo1 info1;
4212 if (!snum_is_shared_printer(snum)) {
4213 continue;
4216 DEBUG(4,("Found a printer in smb.conf: %s[%x]\n", lp_servicename(snum), snum));
4218 result = get_a_printer(NULL, &ntprinter, 2, lp_const_servicename(snum));
4219 if (!W_ERROR_IS_OK(result)) {
4220 continue;
4223 result = construct_printer_info1(info, ntprinter, flags, &info1, snum);
4224 free_a_printer(&ntprinter,2);
4225 if (!W_ERROR_IS_OK(result)) {
4226 continue;
4229 info = TALLOC_REALLOC_ARRAY(mem_ctx, info,
4230 union spoolss_PrinterInfo,
4231 *count + 1);
4232 if (!info) {
4233 DEBUG(2,("enum_all_printers_info_1: failed to enlarge printers buffer!\n"));
4234 result = WERR_NOMEM;
4235 goto out;
4238 DEBUG(4,("ReAlloced memory for [%d] PRINTER_INFO_1\n", *count));
4240 info[*count].info1 = info1;
4241 (*count)++;
4244 out:
4245 if (!W_ERROR_IS_OK(result)) {
4246 TALLOC_FREE(info);
4247 *count = 0;
4248 return result;
4251 *info_p = info;
4253 return WERR_OK;
4256 /********************************************************************
4257 enum_all_printers_info_1_local.
4258 *********************************************************************/
4260 static WERROR enum_all_printers_info_1_local(TALLOC_CTX *mem_ctx,
4261 union spoolss_PrinterInfo **info,
4262 uint32_t *count)
4264 DEBUG(4,("enum_all_printers_info_1_local\n"));
4266 return enum_all_printers_info_1(mem_ctx, PRINTER_ENUM_ICON8, info, count);
4269 /********************************************************************
4270 enum_all_printers_info_1_name.
4271 *********************************************************************/
4273 static WERROR enum_all_printers_info_1_name(TALLOC_CTX *mem_ctx,
4274 const char *name,
4275 union spoolss_PrinterInfo **info,
4276 uint32_t *count)
4278 const char *s = name;
4280 DEBUG(4,("enum_all_printers_info_1_name\n"));
4282 if ((name[0] == '\\') && (name[1] == '\\')) {
4283 s = name + 2;
4286 if (!is_myname_or_ipaddr(s)) {
4287 return WERR_INVALID_NAME;
4290 return enum_all_printers_info_1(mem_ctx, PRINTER_ENUM_ICON8, info, count);
4293 /********************************************************************
4294 enum_all_printers_info_1_network.
4295 *********************************************************************/
4297 static WERROR enum_all_printers_info_1_network(TALLOC_CTX *mem_ctx,
4298 const char *name,
4299 union spoolss_PrinterInfo **info,
4300 uint32_t *count)
4302 const char *s = name;
4304 DEBUG(4,("enum_all_printers_info_1_network\n"));
4306 /* If we respond to a enum_printers level 1 on our name with flags
4307 set to PRINTER_ENUM_REMOTE with a list of printers then these
4308 printers incorrectly appear in the APW browse list.
4309 Specifically the printers for the server appear at the workgroup
4310 level where all the other servers in the domain are
4311 listed. Windows responds to this call with a
4312 WERR_CAN_NOT_COMPLETE so we should do the same. */
4314 if (name[0] == '\\' && name[1] == '\\') {
4315 s = name + 2;
4318 if (is_myname_or_ipaddr(s)) {
4319 return WERR_CAN_NOT_COMPLETE;
4322 return enum_all_printers_info_1(mem_ctx, PRINTER_ENUM_NAME, info, count);
4325 /********************************************************************
4326 * api_spoolss_enumprinters
4328 * called from api_spoolss_enumprinters (see this to understand)
4329 ********************************************************************/
4331 static WERROR enum_all_printers_info_2(TALLOC_CTX *mem_ctx,
4332 union spoolss_PrinterInfo **info_p,
4333 uint32_t *count)
4335 int snum;
4336 int n_services = lp_numservices();
4337 union spoolss_PrinterInfo *info = NULL;
4338 WERROR result = WERR_OK;
4340 *count = 0;
4342 for (snum=0; snum<n_services; snum++) {
4344 struct spoolss_PrinterInfo2 info2;
4345 NT_PRINTER_INFO_LEVEL *ntprinter = NULL;
4347 if (!snum_is_shared_printer(snum)) {
4348 continue;
4351 DEBUG(4,("Found a printer in smb.conf: %s[%x]\n", lp_servicename(snum), snum));
4353 result = get_a_printer(NULL, &ntprinter, 2, lp_const_servicename(snum));
4354 if (!W_ERROR_IS_OK(result)) {
4355 continue;
4358 result = construct_printer_info2(info, ntprinter, &info2, snum);
4359 free_a_printer(&ntprinter, 2);
4360 if (!W_ERROR_IS_OK(result)) {
4361 continue;
4364 info = TALLOC_REALLOC_ARRAY(mem_ctx, info,
4365 union spoolss_PrinterInfo,
4366 *count + 1);
4367 if (!info) {
4368 DEBUG(2,("enum_all_printers_info_2: failed to enlarge printers buffer!\n"));
4369 result = WERR_NOMEM;
4370 goto out;
4373 DEBUG(4,("ReAlloced memory for [%d] PRINTER_INFO_2\n", *count + 1));
4375 info[*count].info2 = info2;
4377 (*count)++;
4380 out:
4381 if (!W_ERROR_IS_OK(result)) {
4382 TALLOC_FREE(info);
4383 *count = 0;
4384 return result;
4387 *info_p = info;
4389 return WERR_OK;
4392 /********************************************************************
4393 * handle enumeration of printers at level 1
4394 ********************************************************************/
4396 static WERROR enumprinters_level1(TALLOC_CTX *mem_ctx,
4397 uint32_t flags,
4398 const char *name,
4399 union spoolss_PrinterInfo **info,
4400 uint32_t *count)
4402 /* Not all the flags are equals */
4404 if (flags & PRINTER_ENUM_LOCAL) {
4405 return enum_all_printers_info_1_local(mem_ctx, info, count);
4408 if (flags & PRINTER_ENUM_NAME) {
4409 return enum_all_printers_info_1_name(mem_ctx, name, info, count);
4412 if (flags & PRINTER_ENUM_NETWORK) {
4413 return enum_all_printers_info_1_network(mem_ctx, name, info, count);
4416 return WERR_OK; /* NT4sp5 does that */
4419 /********************************************************************
4420 * handle enumeration of printers at level 2
4421 ********************************************************************/
4423 static WERROR enumprinters_level2(TALLOC_CTX *mem_ctx,
4424 uint32_t flags,
4425 const char *servername,
4426 union spoolss_PrinterInfo **info,
4427 uint32_t *count)
4429 if (flags & PRINTER_ENUM_LOCAL) {
4430 return enum_all_printers_info_2(mem_ctx, info, count);
4433 if (flags & PRINTER_ENUM_NAME) {
4434 if (!is_myname_or_ipaddr(canon_servername(servername))) {
4435 return WERR_INVALID_NAME;
4438 return enum_all_printers_info_2(mem_ctx, info, count);
4441 if (flags & PRINTER_ENUM_REMOTE) {
4442 return WERR_UNKNOWN_LEVEL;
4445 return WERR_OK;
4448 /********************************************************************
4449 * handle enumeration of printers at level 5
4450 ********************************************************************/
4452 static WERROR enumprinters_level5(TALLOC_CTX *mem_ctx,
4453 uint32_t flags,
4454 const char *servername,
4455 union spoolss_PrinterInfo **info,
4456 uint32_t *count)
4458 /* return enum_all_printers_info_5(mem_ctx, info, offered, needed, count);*/
4459 return WERR_OK;
4462 /****************************************************************
4463 _spoolss_EnumPrinters
4464 ****************************************************************/
4466 WERROR _spoolss_EnumPrinters(pipes_struct *p,
4467 struct spoolss_EnumPrinters *r)
4469 const char *name;
4470 WERROR result;
4472 /* that's an [in out] buffer */
4474 if (!r->in.buffer && (r->in.offered != 0)) {
4475 return WERR_INVALID_PARAM;
4478 DEBUG(4,("_spoolss_EnumPrinters\n"));
4480 *r->out.needed = 0;
4481 *r->out.count = 0;
4482 *r->out.info = NULL;
4485 * Level 1:
4486 * flags==PRINTER_ENUM_NAME
4487 * if name=="" then enumerates all printers
4488 * if name!="" then enumerate the printer
4489 * flags==PRINTER_ENUM_REMOTE
4490 * name is NULL, enumerate printers
4491 * Level 2: name!="" enumerates printers, name can't be NULL
4492 * Level 3: doesn't exist
4493 * Level 4: does a local registry lookup
4494 * Level 5: same as Level 2
4497 name = talloc_strdup_upper(p->mem_ctx, r->in.server);
4498 W_ERROR_HAVE_NO_MEMORY(name);
4500 switch (r->in.level) {
4501 case 1:
4502 result = enumprinters_level1(p->mem_ctx, r->in.flags, name,
4503 r->out.info, r->out.count);
4504 break;
4505 case 2:
4506 result = enumprinters_level2(p->mem_ctx, r->in.flags, name,
4507 r->out.info, r->out.count);
4508 break;
4509 case 5:
4510 result = enumprinters_level5(p->mem_ctx, r->in.flags, name,
4511 r->out.info, r->out.count);
4512 break;
4513 case 3:
4514 case 4:
4515 result = WERR_OK; /* ??? */
4516 break;
4517 default:
4518 return WERR_UNKNOWN_LEVEL;
4521 if (!W_ERROR_IS_OK(result)) {
4522 return result;
4525 *r->out.needed = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
4526 spoolss_EnumPrinters, NULL,
4527 *r->out.info, r->in.level,
4528 *r->out.count);
4529 *r->out.info = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
4530 *r->out.count = SPOOLSS_BUFFER_OK(*r->out.count, 0);
4532 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
4535 /****************************************************************
4536 _spoolss_GetPrinter
4537 ****************************************************************/
4539 WERROR _spoolss_GetPrinter(pipes_struct *p,
4540 struct spoolss_GetPrinter *r)
4542 Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
4543 NT_PRINTER_INFO_LEVEL *ntprinter = NULL;
4544 WERROR result = WERR_OK;
4546 int snum;
4548 /* that's an [in out] buffer */
4550 if (!r->in.buffer && (r->in.offered != 0)) {
4551 return WERR_INVALID_PARAM;
4554 *r->out.needed = 0;
4556 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
4557 return WERR_BADFID;
4560 result = get_a_printer(Printer, &ntprinter, 2,
4561 lp_const_servicename(snum));
4562 if (!W_ERROR_IS_OK(result)) {
4563 return result;
4566 switch (r->in.level) {
4567 case 0:
4568 result = construct_printer_info0(p->mem_ctx, ntprinter,
4569 &r->out.info->info0, snum);
4570 break;
4571 case 1:
4572 result = construct_printer_info1(p->mem_ctx, ntprinter,
4573 PRINTER_ENUM_ICON8,
4574 &r->out.info->info1, snum);
4575 break;
4576 case 2:
4577 result = construct_printer_info2(p->mem_ctx, ntprinter,
4578 &r->out.info->info2, snum);
4579 break;
4580 case 3:
4581 result = construct_printer_info3(p->mem_ctx, ntprinter,
4582 &r->out.info->info3, snum);
4583 break;
4584 case 4:
4585 result = construct_printer_info4(p->mem_ctx, ntprinter,
4586 &r->out.info->info4, snum);
4587 break;
4588 case 5:
4589 result = construct_printer_info5(p->mem_ctx, ntprinter,
4590 &r->out.info->info5, snum);
4591 break;
4592 case 6:
4593 result = construct_printer_info6(p->mem_ctx, ntprinter,
4594 &r->out.info->info6, snum);
4595 break;
4596 case 7:
4597 result = construct_printer_info7(p->mem_ctx, Printer,
4598 &r->out.info->info7, snum);
4599 break;
4600 default:
4601 result = WERR_UNKNOWN_LEVEL;
4602 break;
4605 free_a_printer(&ntprinter, 2);
4607 if (!W_ERROR_IS_OK(result)) {
4608 TALLOC_FREE(r->out.info);
4609 return result;
4612 *r->out.needed = SPOOLSS_BUFFER_UNION(spoolss_PrinterInfo, NULL,
4613 r->out.info, r->in.level);
4614 r->out.info = SPOOLSS_BUFFER_OK(r->out.info, NULL);
4616 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
4619 /********************************************************************
4620 ********************************************************************/
4622 static const char **string_array_from_driver_info(TALLOC_CTX *mem_ctx,
4623 fstring *fstring_array,
4624 const char *cservername)
4626 int i, num_strings = 0;
4627 const char **array = NULL;
4629 for (i=0; fstring_array && fstring_array[i][0] != '\0'; i++) {
4631 const char *str = talloc_asprintf(mem_ctx, "\\\\%s%s",
4632 cservername, fstring_array[i]);
4633 if (!str) {
4634 TALLOC_FREE(array);
4635 return NULL;
4639 if (!add_string_to_array(mem_ctx, str, &array, &num_strings)) {
4640 TALLOC_FREE(array);
4641 return NULL;
4645 if (i > 0) {
4646 ADD_TO_ARRAY(mem_ctx, const char *, NULL,
4647 &array, &num_strings);
4650 return array;
4653 /********************************************************************
4654 * fill a spoolss_DriverInfo1 struct
4655 ********************************************************************/
4657 static WERROR fill_printer_driver_info1(TALLOC_CTX *mem_ctx,
4658 struct spoolss_DriverInfo1 *r,
4659 const NT_PRINTER_DRIVER_INFO_LEVEL *driver,
4660 const char *servername,
4661 const char *architecture)
4663 r->driver_name = talloc_strdup(mem_ctx, driver->info_3->name);
4664 W_ERROR_HAVE_NO_MEMORY(r->driver_name);
4666 return WERR_OK;
4669 /********************************************************************
4670 * fill a spoolss_DriverInfo2 struct
4671 ********************************************************************/
4673 static WERROR fill_printer_driver_info2(TALLOC_CTX *mem_ctx,
4674 struct spoolss_DriverInfo2 *r,
4675 const NT_PRINTER_DRIVER_INFO_LEVEL *driver,
4676 const char *servername)
4679 const char *cservername = canon_servername(servername);
4681 r->version = driver->info_3->cversion;
4683 r->driver_name = talloc_strdup(mem_ctx, driver->info_3->name);
4684 W_ERROR_HAVE_NO_MEMORY(r->driver_name);
4685 r->architecture = talloc_strdup(mem_ctx, driver->info_3->environment);
4686 W_ERROR_HAVE_NO_MEMORY(r->architecture);
4688 if (strlen(driver->info_3->driverpath)) {
4689 r->driver_path = talloc_asprintf(mem_ctx, "\\\\%s%s",
4690 cservername, driver->info_3->driverpath);
4691 } else {
4692 r->driver_path = talloc_strdup(mem_ctx, "");
4694 W_ERROR_HAVE_NO_MEMORY(r->driver_path);
4696 if (strlen(driver->info_3->datafile)) {
4697 r->data_file = talloc_asprintf(mem_ctx, "\\\\%s%s",
4698 cservername, driver->info_3->datafile);
4699 } else {
4700 r->data_file = talloc_strdup(mem_ctx, "");
4702 W_ERROR_HAVE_NO_MEMORY(r->data_file);
4704 if (strlen(driver->info_3->configfile)) {
4705 r->config_file = talloc_asprintf(mem_ctx, "\\\\%s%s",
4706 cservername, driver->info_3->configfile);
4707 } else {
4708 r->config_file = talloc_strdup(mem_ctx, "");
4710 W_ERROR_HAVE_NO_MEMORY(r->config_file);
4712 return WERR_OK;
4715 /********************************************************************
4716 * fill a spoolss_DriverInfo3 struct
4717 ********************************************************************/
4719 static WERROR fill_printer_driver_info3(TALLOC_CTX *mem_ctx,
4720 struct spoolss_DriverInfo3 *r,
4721 const NT_PRINTER_DRIVER_INFO_LEVEL *driver,
4722 const char *servername)
4724 const char *cservername = canon_servername(servername);
4726 r->version = driver->info_3->cversion;
4728 r->driver_name = talloc_strdup(mem_ctx, driver->info_3->name);
4729 W_ERROR_HAVE_NO_MEMORY(r->driver_name);
4730 r->architecture = talloc_strdup(mem_ctx, driver->info_3->environment);
4731 W_ERROR_HAVE_NO_MEMORY(r->architecture);
4733 if (strlen(driver->info_3->driverpath)) {
4734 r->driver_path = talloc_asprintf(mem_ctx, "\\\\%s%s",
4735 cservername, driver->info_3->driverpath);
4736 } else {
4737 r->driver_path = talloc_strdup(mem_ctx, "");
4739 W_ERROR_HAVE_NO_MEMORY(r->driver_path);
4741 if (strlen(driver->info_3->datafile)) {
4742 r->data_file = talloc_asprintf(mem_ctx, "\\\\%s%s",
4743 cservername, driver->info_3->datafile);
4744 } else {
4745 r->data_file = talloc_strdup(mem_ctx, "");
4747 W_ERROR_HAVE_NO_MEMORY(r->data_file);
4749 if (strlen(driver->info_3->configfile)) {
4750 r->config_file = talloc_asprintf(mem_ctx, "\\\\%s%s",
4751 cservername, driver->info_3->configfile);
4752 } else {
4753 r->config_file = talloc_strdup(mem_ctx, "");
4755 W_ERROR_HAVE_NO_MEMORY(r->config_file);
4757 if (strlen(driver->info_3->helpfile)) {
4758 r->help_file = talloc_asprintf(mem_ctx, "\\\\%s%s",
4759 cservername, driver->info_3->helpfile);
4760 } else {
4761 r->help_file = talloc_strdup(mem_ctx, "");
4763 W_ERROR_HAVE_NO_MEMORY(r->config_file);
4765 r->monitor_name = talloc_strdup(mem_ctx, driver->info_3->monitorname);
4766 W_ERROR_HAVE_NO_MEMORY(r->monitor_name);
4767 r->default_datatype = talloc_strdup(mem_ctx, driver->info_3->defaultdatatype);
4768 W_ERROR_HAVE_NO_MEMORY(r->default_datatype);
4770 r->dependent_files = string_array_from_driver_info(mem_ctx,
4771 driver->info_3->dependentfiles,
4772 cservername);
4773 return WERR_OK;
4776 /********************************************************************
4777 * fill a spoolss_DriverInfo6 struct
4778 ********************************************************************/
4780 static WERROR fill_printer_driver_info6(TALLOC_CTX *mem_ctx,
4781 struct spoolss_DriverInfo6 *r,
4782 const NT_PRINTER_DRIVER_INFO_LEVEL *driver,
4783 const char *servername)
4785 const char *cservername = canon_servername(servername);
4787 r->version = driver->info_3->cversion;
4789 r->driver_name = talloc_strdup(mem_ctx, driver->info_3->name);
4790 W_ERROR_HAVE_NO_MEMORY(r->driver_name);
4791 r->architecture = talloc_strdup(mem_ctx, driver->info_3->environment);
4792 W_ERROR_HAVE_NO_MEMORY(r->architecture);
4794 if (strlen(driver->info_3->driverpath)) {
4795 r->driver_path = talloc_asprintf(mem_ctx, "\\\\%s%s",
4796 cservername, driver->info_3->driverpath);
4797 } else {
4798 r->driver_path = talloc_strdup(mem_ctx, "");
4800 W_ERROR_HAVE_NO_MEMORY(r->driver_path);
4802 if (strlen(driver->info_3->datafile)) {
4803 r->data_file = talloc_asprintf(mem_ctx, "\\\\%s%s",
4804 cservername, driver->info_3->datafile);
4805 } else {
4806 r->data_file = talloc_strdup(mem_ctx, "");
4808 W_ERROR_HAVE_NO_MEMORY(r->data_file);
4810 if (strlen(driver->info_3->configfile)) {
4811 r->config_file = talloc_asprintf(mem_ctx, "\\\\%s%s",
4812 cservername, driver->info_3->configfile);
4813 } else {
4814 r->config_file = talloc_strdup(mem_ctx, "");
4816 W_ERROR_HAVE_NO_MEMORY(r->config_file);
4818 if (strlen(driver->info_3->helpfile)) {
4819 r->help_file = talloc_asprintf(mem_ctx, "\\\\%s%s",
4820 cservername, driver->info_3->helpfile);
4821 } else {
4822 r->help_file = talloc_strdup(mem_ctx, "");
4824 W_ERROR_HAVE_NO_MEMORY(r->config_file);
4826 r->monitor_name = talloc_strdup(mem_ctx, driver->info_3->monitorname);
4827 W_ERROR_HAVE_NO_MEMORY(r->monitor_name);
4828 r->default_datatype = talloc_strdup(mem_ctx, driver->info_3->defaultdatatype);
4829 W_ERROR_HAVE_NO_MEMORY(r->default_datatype);
4831 r->dependent_files = string_array_from_driver_info(mem_ctx,
4832 driver->info_3->dependentfiles,
4833 cservername);
4834 r->previous_names = string_array_from_driver_info(mem_ctx,
4835 NULL,
4836 cservername);
4838 r->driver_date = 0;
4839 r->driver_version = 0;
4841 r->manufacturer_name = talloc_strdup(mem_ctx, "");
4842 W_ERROR_HAVE_NO_MEMORY(r->manufacturer_name);
4843 r->manufacturer_url = talloc_strdup(mem_ctx, "");
4844 W_ERROR_HAVE_NO_MEMORY(r->manufacturer_url);
4845 r->hardware_id = talloc_strdup(mem_ctx, "");
4846 W_ERROR_HAVE_NO_MEMORY(r->hardware_id);
4847 r->provider = talloc_strdup(mem_ctx, "");
4848 W_ERROR_HAVE_NO_MEMORY(r->provider);
4850 return WERR_OK;
4853 /********************************************************************
4854 * construct_printer_driver_info_1
4855 ********************************************************************/
4857 static WERROR construct_printer_driver_info_1(TALLOC_CTX *mem_ctx,
4858 struct spoolss_DriverInfo1 *r,
4859 int snum,
4860 const char *servername,
4861 const char *architecture,
4862 uint32_t version)
4864 NT_PRINTER_INFO_LEVEL *printer = NULL;
4865 NT_PRINTER_DRIVER_INFO_LEVEL driver;
4866 WERROR result;
4868 ZERO_STRUCT(driver);
4870 if (!W_ERROR_IS_OK(get_a_printer(NULL, &printer, 2, lp_const_servicename(snum))))
4871 return WERR_INVALID_PRINTER_NAME;
4873 if (!W_ERROR_IS_OK(get_a_printer_driver(&driver, 3, printer->info_2->drivername, architecture, version))) {
4874 free_a_printer(&printer, 2);
4875 return WERR_UNKNOWN_PRINTER_DRIVER;
4878 result = fill_printer_driver_info1(mem_ctx, r, &driver, servername, architecture);
4880 free_a_printer(&printer,2);
4882 return result;
4885 /********************************************************************
4886 * construct_printer_driver_info_2
4887 * fill a printer_info_2 struct
4888 ********************************************************************/
4890 static WERROR construct_printer_driver_info_2(TALLOC_CTX *mem_ctx,
4891 struct spoolss_DriverInfo2 *r,
4892 int snum,
4893 const char *servername,
4894 const char *architecture,
4895 uint32_t version)
4897 NT_PRINTER_INFO_LEVEL *printer = NULL;
4898 NT_PRINTER_DRIVER_INFO_LEVEL driver;
4899 WERROR result;
4901 ZERO_STRUCT(printer);
4902 ZERO_STRUCT(driver);
4904 if (!W_ERROR_IS_OK(get_a_printer(NULL, &printer, 2, lp_const_servicename(snum))))
4905 return WERR_INVALID_PRINTER_NAME;
4907 if (!W_ERROR_IS_OK(get_a_printer_driver(&driver, 3, printer->info_2->drivername, architecture, version))) {
4908 free_a_printer(&printer, 2);
4909 return WERR_UNKNOWN_PRINTER_DRIVER;
4912 result = fill_printer_driver_info2(mem_ctx, r, &driver, servername);
4914 free_a_printer(&printer,2);
4916 return result;
4919 /********************************************************************
4920 * construct_printer_info_3
4921 * fill a printer_info_3 struct
4922 ********************************************************************/
4924 static WERROR construct_printer_driver_info_3(TALLOC_CTX *mem_ctx,
4925 struct spoolss_DriverInfo3 *r,
4926 int snum,
4927 const char *servername,
4928 const char *architecture,
4929 uint32_t version)
4931 NT_PRINTER_INFO_LEVEL *printer = NULL;
4932 NT_PRINTER_DRIVER_INFO_LEVEL driver;
4933 WERROR status;
4934 ZERO_STRUCT(driver);
4936 status=get_a_printer(NULL, &printer, 2, lp_const_servicename(snum) );
4937 DEBUG(8,("construct_printer_driver_info_3: status: %s\n", win_errstr(status)));
4938 if (!W_ERROR_IS_OK(status))
4939 return WERR_INVALID_PRINTER_NAME;
4941 status=get_a_printer_driver(&driver, 3, printer->info_2->drivername, architecture, version);
4942 DEBUG(8,("construct_printer_driver_info_3: status: %s\n", win_errstr(status)));
4944 #if 0 /* JERRY */
4947 * I put this code in during testing. Helpful when commenting out the
4948 * support for DRIVER_INFO_6 in regards to win2k. Not needed in general
4949 * as win2k always queries the driver using an infor level of 6.
4950 * I've left it in (but ifdef'd out) because I'll probably
4951 * use it in experimentation again in the future. --jerry 22/01/2002
4954 if (!W_ERROR_IS_OK(status)) {
4956 * Is this a W2k client ?
4958 if (version == 3) {
4959 /* Yes - try again with a WinNT driver. */
4960 version = 2;
4961 status=get_a_printer_driver(&driver, 3, printer->info_2->drivername, architecture, version);
4962 DEBUG(8,("construct_printer_driver_info_3: status: %s\n", win_errstr(status)));
4964 #endif
4966 if (!W_ERROR_IS_OK(status)) {
4967 free_a_printer(&printer,2);
4968 return WERR_UNKNOWN_PRINTER_DRIVER;
4971 #if 0 /* JERRY */
4973 #endif
4976 status = fill_printer_driver_info3(mem_ctx, r, &driver, servername);
4978 free_a_printer(&printer,2);
4980 return status;
4983 /********************************************************************
4984 * construct_printer_info_6
4985 * fill a printer_info_6 struct
4986 ********************************************************************/
4988 static WERROR construct_printer_driver_info_6(TALLOC_CTX *mem_ctx,
4989 struct spoolss_DriverInfo6 *r,
4990 int snum,
4991 const char *servername,
4992 const char *architecture,
4993 uint32_t version)
4995 NT_PRINTER_INFO_LEVEL *printer = NULL;
4996 NT_PRINTER_DRIVER_INFO_LEVEL driver;
4997 WERROR status;
4999 ZERO_STRUCT(driver);
5001 status=get_a_printer(NULL, &printer, 2, lp_const_servicename(snum) );
5003 DEBUG(8,("construct_printer_driver_info_6: status: %s\n", win_errstr(status)));
5005 if (!W_ERROR_IS_OK(status))
5006 return WERR_INVALID_PRINTER_NAME;
5008 status = get_a_printer_driver(&driver, 3, printer->info_2->drivername, architecture, version);
5010 DEBUG(8,("construct_printer_driver_info_6: status: %s\n", win_errstr(status)));
5012 if (!W_ERROR_IS_OK(status))
5015 * Is this a W2k client ?
5018 if (version < 3) {
5019 free_a_printer(&printer,2);
5020 return WERR_UNKNOWN_PRINTER_DRIVER;
5023 /* Yes - try again with a WinNT driver. */
5024 version = 2;
5025 status=get_a_printer_driver(&driver, 3, printer->info_2->drivername, architecture, version);
5026 DEBUG(8,("construct_printer_driver_info_6: status: %s\n", win_errstr(status)));
5027 if (!W_ERROR_IS_OK(status)) {
5028 free_a_printer(&printer,2);
5029 return WERR_UNKNOWN_PRINTER_DRIVER;
5033 status = fill_printer_driver_info6(mem_ctx, r, &driver, servername);
5035 free_a_printer(&printer,2);
5036 free_a_printer_driver(driver, 3);
5038 return status;
5041 /****************************************************************
5042 _spoolss_GetPrinterDriver2
5043 ****************************************************************/
5045 WERROR _spoolss_GetPrinterDriver2(pipes_struct *p,
5046 struct spoolss_GetPrinterDriver2 *r)
5048 Printer_entry *printer;
5049 WERROR result;
5051 const char *servername;
5052 int snum;
5054 /* that's an [in out] buffer */
5056 if (!r->in.buffer && (r->in.offered != 0)) {
5057 return WERR_INVALID_PARAM;
5060 DEBUG(4,("_spoolss_GetPrinterDriver2\n"));
5062 if (!(printer = find_printer_index_by_hnd( p, r->in.handle))) {
5063 DEBUG(0,("_spoolss_GetPrinterDriver2: invalid printer handle!\n"));
5064 return WERR_INVALID_PRINTER_NAME;
5067 *r->out.needed = 0;
5068 *r->out.server_major_version = 0;
5069 *r->out.server_minor_version = 0;
5071 servername = get_server_name(printer);
5073 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
5074 return WERR_BADFID;
5077 switch (r->in.level) {
5078 case 1:
5079 result = construct_printer_driver_info_1(p->mem_ctx,
5080 &r->out.info->info1,
5081 snum,
5082 servername,
5083 r->in.architecture,
5084 r->in.client_major_version);
5085 break;
5086 case 2:
5087 result = construct_printer_driver_info_2(p->mem_ctx,
5088 &r->out.info->info2,
5089 snum,
5090 servername,
5091 r->in.architecture,
5092 r->in.client_major_version);
5093 break;
5094 case 3:
5095 result = construct_printer_driver_info_3(p->mem_ctx,
5096 &r->out.info->info3,
5097 snum,
5098 servername,
5099 r->in.architecture,
5100 r->in.client_major_version);
5101 break;
5102 case 6:
5103 result = construct_printer_driver_info_6(p->mem_ctx,
5104 &r->out.info->info6,
5105 snum,
5106 servername,
5107 r->in.architecture,
5108 r->in.client_major_version);
5109 break;
5110 default:
5111 #if 0 /* JERRY */
5112 case 101:
5113 /* apparently this call is the equivalent of
5114 EnumPrinterDataEx() for the DsDriver key */
5115 break;
5116 #endif
5117 result = WERR_UNKNOWN_LEVEL;
5118 break;
5121 if (!W_ERROR_IS_OK(result)) {
5122 TALLOC_FREE(r->out.info);
5123 return result;
5126 *r->out.needed = SPOOLSS_BUFFER_UNION(spoolss_DriverInfo, NULL,
5127 r->out.info, r->in.level);
5128 r->out.info = SPOOLSS_BUFFER_OK(r->out.info, NULL);
5130 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
5134 /****************************************************************
5135 _spoolss_StartPagePrinter
5136 ****************************************************************/
5138 WERROR _spoolss_StartPagePrinter(pipes_struct *p,
5139 struct spoolss_StartPagePrinter *r)
5141 POLICY_HND *handle = r->in.handle;
5143 Printer_entry *Printer = find_printer_index_by_hnd(p, handle);
5145 if (!Printer) {
5146 DEBUG(3,("_spoolss_StartPagePrinter: "
5147 "Error in startpageprinter printer handle\n"));
5148 return WERR_BADFID;
5151 Printer->page_started=True;
5152 return WERR_OK;
5155 /****************************************************************
5156 _spoolss_EndPagePrinter
5157 ****************************************************************/
5159 WERROR _spoolss_EndPagePrinter(pipes_struct *p,
5160 struct spoolss_EndPagePrinter *r)
5162 POLICY_HND *handle = r->in.handle;
5163 int snum;
5165 Printer_entry *Printer = find_printer_index_by_hnd(p, handle);
5167 if (!Printer) {
5168 DEBUG(2,("_spoolss_EndPagePrinter: Invalid handle (%s:%u:%u).\n",
5169 OUR_HANDLE(handle)));
5170 return WERR_BADFID;
5173 if (!get_printer_snum(p, handle, &snum, NULL))
5174 return WERR_BADFID;
5176 Printer->page_started=False;
5177 print_job_endpage(snum, Printer->jobid);
5179 return WERR_OK;
5182 /****************************************************************
5183 _spoolss_StartDocPrinter
5184 ****************************************************************/
5186 WERROR _spoolss_StartDocPrinter(pipes_struct *p,
5187 struct spoolss_StartDocPrinter *r)
5189 POLICY_HND *handle = r->in.handle;
5190 uint32_t *jobid = r->out.job_id;
5191 struct spoolss_DocumentInfo1 *info_1;
5192 int snum;
5193 Printer_entry *Printer = find_printer_index_by_hnd(p, handle);
5195 if (!Printer) {
5196 DEBUG(2,("_spoolss_StartDocPrinter: "
5197 "Invalid handle (%s:%u:%u)\n", OUR_HANDLE(handle)));
5198 return WERR_BADFID;
5201 if (r->in.level != 1) {
5202 return WERR_UNKNOWN_LEVEL;
5205 info_1 = r->in.info.info1;
5208 * a nice thing with NT is it doesn't listen to what you tell it.
5209 * when asked to send _only_ RAW datas, it tries to send datas
5210 * in EMF format.
5212 * So I add checks like in NT Server ...
5215 if (info_1->datatype) {
5216 if (strcmp(info_1->datatype, "RAW") != 0) {
5217 (*jobid)=0;
5218 return WERR_INVALID_DATATYPE;
5222 /* get the share number of the printer */
5223 if (!get_printer_snum(p, handle, &snum, NULL)) {
5224 return WERR_BADFID;
5227 Printer->jobid = print_job_start(p->server_info, snum,
5228 info_1->document_name,
5229 Printer->nt_devmode);
5231 /* An error occured in print_job_start() so return an appropriate
5232 NT error code. */
5234 if (Printer->jobid == -1) {
5235 return map_werror_from_unix(errno);
5238 Printer->document_started=True;
5239 (*jobid) = Printer->jobid;
5241 return WERR_OK;
5244 /****************************************************************
5245 _spoolss_EndDocPrinter
5246 ****************************************************************/
5248 WERROR _spoolss_EndDocPrinter(pipes_struct *p,
5249 struct spoolss_EndDocPrinter *r)
5251 POLICY_HND *handle = r->in.handle;
5253 return _spoolss_enddocprinter_internal(p, handle);
5256 /****************************************************************
5257 _spoolss_WritePrinter
5258 ****************************************************************/
5260 WERROR _spoolss_WritePrinter(pipes_struct *p,
5261 struct spoolss_WritePrinter *r)
5263 POLICY_HND *handle = r->in.handle;
5264 uint32 buffer_size = r->in._data_size;
5265 uint8 *buffer = r->in.data.data;
5266 uint32 *buffer_written = &r->in._data_size;
5267 int snum;
5268 Printer_entry *Printer = find_printer_index_by_hnd(p, handle);
5270 if (!Printer) {
5271 DEBUG(2,("_spoolss_WritePrinter: Invalid handle (%s:%u:%u)\n",
5272 OUR_HANDLE(handle)));
5273 *r->out.num_written = r->in._data_size;
5274 return WERR_BADFID;
5277 if (!get_printer_snum(p, handle, &snum, NULL))
5278 return WERR_BADFID;
5280 (*buffer_written) = (uint32)print_job_write(snum, Printer->jobid, (const char *)buffer,
5281 (SMB_OFF_T)-1, (size_t)buffer_size);
5282 if (*buffer_written == (uint32)-1) {
5283 *r->out.num_written = 0;
5284 if (errno == ENOSPC)
5285 return WERR_NO_SPOOL_SPACE;
5286 else
5287 return WERR_ACCESS_DENIED;
5290 *r->out.num_written = r->in._data_size;
5292 return WERR_OK;
5295 /********************************************************************
5296 * api_spoolss_getprinter
5297 * called from the spoolss dispatcher
5299 ********************************************************************/
5301 static WERROR control_printer(POLICY_HND *handle, uint32 command,
5302 pipes_struct *p)
5304 int snum;
5305 WERROR errcode = WERR_BADFUNC;
5306 Printer_entry *Printer = find_printer_index_by_hnd(p, handle);
5308 if (!Printer) {
5309 DEBUG(2,("control_printer: Invalid handle (%s:%u:%u)\n", OUR_HANDLE(handle)));
5310 return WERR_BADFID;
5313 if (!get_printer_snum(p, handle, &snum, NULL))
5314 return WERR_BADFID;
5316 switch (command) {
5317 case SPOOLSS_PRINTER_CONTROL_PAUSE:
5318 if (print_queue_pause(p->server_info, snum, &errcode)) {
5319 errcode = WERR_OK;
5321 break;
5322 case SPOOLSS_PRINTER_CONTROL_RESUME:
5323 case SPOOLSS_PRINTER_CONTROL_UNPAUSE:
5324 if (print_queue_resume(p->server_info, snum, &errcode)) {
5325 errcode = WERR_OK;
5327 break;
5328 case SPOOLSS_PRINTER_CONTROL_PURGE:
5329 if (print_queue_purge(p->server_info, snum, &errcode)) {
5330 errcode = WERR_OK;
5332 break;
5333 default:
5334 return WERR_UNKNOWN_LEVEL;
5337 return errcode;
5341 /****************************************************************
5342 _spoolss_AbortPrinter
5343 * From MSDN: "Deletes printer's spool file if printer is configured
5344 * for spooling"
5345 ****************************************************************/
5347 WERROR _spoolss_AbortPrinter(pipes_struct *p,
5348 struct spoolss_AbortPrinter *r)
5350 POLICY_HND *handle = r->in.handle;
5351 Printer_entry *Printer = find_printer_index_by_hnd(p, handle);
5352 int snum;
5353 WERROR errcode = WERR_OK;
5355 if (!Printer) {
5356 DEBUG(2,("_spoolss_AbortPrinter: Invalid handle (%s:%u:%u)\n",
5357 OUR_HANDLE(handle)));
5358 return WERR_BADFID;
5361 if (!get_printer_snum(p, handle, &snum, NULL))
5362 return WERR_BADFID;
5364 print_job_delete(p->server_info, snum, Printer->jobid, &errcode );
5366 return errcode;
5369 /********************************************************************
5370 * called by spoolss_api_setprinter
5371 * when updating a printer description
5372 ********************************************************************/
5374 static WERROR update_printer_sec(POLICY_HND *handle,
5375 pipes_struct *p, SEC_DESC_BUF *secdesc_ctr)
5377 SEC_DESC_BUF *new_secdesc_ctr = NULL, *old_secdesc_ctr = NULL;
5378 WERROR result;
5379 int snum;
5381 Printer_entry *Printer = find_printer_index_by_hnd(p, handle);
5383 if (!Printer || !get_printer_snum(p, handle, &snum, NULL)) {
5384 DEBUG(2,("update_printer_sec: Invalid handle (%s:%u:%u)\n",
5385 OUR_HANDLE(handle)));
5387 result = WERR_BADFID;
5388 goto done;
5391 if (!secdesc_ctr) {
5392 DEBUG(10,("update_printer_sec: secdesc_ctr is NULL !\n"));
5393 result = WERR_INVALID_PARAM;
5394 goto done;
5397 /* Check the user has permissions to change the security
5398 descriptor. By experimentation with two NT machines, the user
5399 requires Full Access to the printer to change security
5400 information. */
5402 if ( Printer->access_granted != PRINTER_ACCESS_ADMINISTER ) {
5403 DEBUG(4,("update_printer_sec: updated denied by printer permissions\n"));
5404 result = WERR_ACCESS_DENIED;
5405 goto done;
5408 /* NT seems to like setting the security descriptor even though
5409 nothing may have actually changed. */
5411 if ( !nt_printing_getsec(p->mem_ctx, Printer->sharename, &old_secdesc_ctr)) {
5412 DEBUG(2,("update_printer_sec: nt_printing_getsec() failed\n"));
5413 result = WERR_BADFID;
5414 goto done;
5417 if (DEBUGLEVEL >= 10) {
5418 SEC_ACL *the_acl;
5419 int i;
5421 the_acl = old_secdesc_ctr->sd->dacl;
5422 DEBUG(10, ("old_secdesc_ctr for %s has %d aces:\n",
5423 PRINTERNAME(snum), the_acl->num_aces));
5425 for (i = 0; i < the_acl->num_aces; i++) {
5426 DEBUG(10, ("%s 0x%08x\n", sid_string_dbg(
5427 &the_acl->aces[i].trustee),
5428 the_acl->aces[i].access_mask));
5431 the_acl = secdesc_ctr->sd->dacl;
5433 if (the_acl) {
5434 DEBUG(10, ("secdesc_ctr for %s has %d aces:\n",
5435 PRINTERNAME(snum), the_acl->num_aces));
5437 for (i = 0; i < the_acl->num_aces; i++) {
5438 DEBUG(10, ("%s 0x%08x\n", sid_string_dbg(
5439 &the_acl->aces[i].trustee),
5440 the_acl->aces[i].access_mask));
5442 } else {
5443 DEBUG(10, ("dacl for secdesc_ctr is NULL\n"));
5447 new_secdesc_ctr = sec_desc_merge(p->mem_ctx, secdesc_ctr, old_secdesc_ctr);
5448 if (!new_secdesc_ctr) {
5449 result = WERR_NOMEM;
5450 goto done;
5453 if (sec_desc_equal(new_secdesc_ctr->sd, old_secdesc_ctr->sd)) {
5454 result = WERR_OK;
5455 goto done;
5458 result = nt_printing_setsec(Printer->sharename, new_secdesc_ctr);
5460 done:
5462 return result;
5465 /********************************************************************
5466 Canonicalize printer info from a client
5468 ATTN: It does not matter what we set the servername to hear
5469 since we do the necessary work in get_a_printer() to set it to
5470 the correct value based on what the client sent in the
5471 _spoolss_open_printer_ex().
5472 ********************************************************************/
5474 static bool check_printer_ok(NT_PRINTER_INFO_LEVEL_2 *info, int snum)
5476 fstring printername;
5477 const char *p;
5479 DEBUG(5,("check_printer_ok: servername=%s printername=%s sharename=%s "
5480 "portname=%s drivername=%s comment=%s location=%s\n",
5481 info->servername, info->printername, info->sharename,
5482 info->portname, info->drivername, info->comment, info->location));
5484 /* we force some elements to "correct" values */
5485 slprintf(info->servername, sizeof(info->servername)-1, "\\\\%s", global_myname());
5486 fstrcpy(info->sharename, lp_servicename(snum));
5488 /* check to see if we allow printername != sharename */
5490 if ( lp_force_printername(snum) ) {
5491 slprintf(info->printername, sizeof(info->printername)-1, "\\\\%s\\%s",
5492 global_myname(), info->sharename );
5493 } else {
5495 /* make sure printername is in \\server\printername format */
5497 fstrcpy( printername, info->printername );
5498 p = printername;
5499 if ( printername[0] == '\\' && printername[1] == '\\' ) {
5500 if ( (p = strchr_m( &printername[2], '\\' )) != NULL )
5501 p++;
5504 slprintf(info->printername, sizeof(info->printername)-1, "\\\\%s\\%s",
5505 global_myname(), p );
5508 info->attributes |= PRINTER_ATTRIBUTE_SAMBA;
5509 info->attributes &= ~PRINTER_ATTRIBUTE_NOT_SAMBA;
5513 return True;
5516 /****************************************************************************
5517 ****************************************************************************/
5519 WERROR add_port_hook(TALLOC_CTX *ctx, NT_USER_TOKEN *token, const char *portname, const char *uri )
5521 char *cmd = lp_addport_cmd();
5522 char *command = NULL;
5523 int ret;
5524 SE_PRIV se_printop = SE_PRINT_OPERATOR;
5525 bool is_print_op = False;
5527 if ( !*cmd ) {
5528 return WERR_ACCESS_DENIED;
5531 command = talloc_asprintf(ctx,
5532 "%s \"%s\" \"%s\"", cmd, portname, uri );
5533 if (!command) {
5534 return WERR_NOMEM;
5537 if ( token )
5538 is_print_op = user_has_privileges( token, &se_printop );
5540 DEBUG(10,("Running [%s]\n", command));
5542 /********* BEGIN SePrintOperatorPrivilege **********/
5544 if ( is_print_op )
5545 become_root();
5547 ret = smbrun(command, NULL);
5549 if ( is_print_op )
5550 unbecome_root();
5552 /********* END SePrintOperatorPrivilege **********/
5554 DEBUGADD(10,("returned [%d]\n", ret));
5556 TALLOC_FREE(command);
5558 if ( ret != 0 ) {
5559 return WERR_ACCESS_DENIED;
5562 return WERR_OK;
5565 /****************************************************************************
5566 ****************************************************************************/
5568 bool add_printer_hook(TALLOC_CTX *ctx, NT_USER_TOKEN *token, NT_PRINTER_INFO_LEVEL *printer)
5570 char *cmd = lp_addprinter_cmd();
5571 char **qlines;
5572 char *command = NULL;
5573 int numlines;
5574 int ret;
5575 int fd;
5576 SE_PRIV se_printop = SE_PRINT_OPERATOR;
5577 bool is_print_op = False;
5578 char *remote_machine = talloc_strdup(ctx, "%m");
5580 if (!remote_machine) {
5581 return false;
5583 remote_machine = talloc_sub_basic(ctx,
5584 current_user_info.smb_name,
5585 current_user_info.domain,
5586 remote_machine);
5587 if (!remote_machine) {
5588 return false;
5591 command = talloc_asprintf(ctx,
5592 "%s \"%s\" \"%s\" \"%s\" \"%s\" \"%s\" \"%s\" \"%s\"",
5593 cmd, printer->info_2->printername, printer->info_2->sharename,
5594 printer->info_2->portname, printer->info_2->drivername,
5595 printer->info_2->location, printer->info_2->comment, remote_machine);
5596 if (!command) {
5597 return false;
5600 if ( token )
5601 is_print_op = user_has_privileges( token, &se_printop );
5603 DEBUG(10,("Running [%s]\n", command));
5605 /********* BEGIN SePrintOperatorPrivilege **********/
5607 if ( is_print_op )
5608 become_root();
5610 if ( (ret = smbrun(command, &fd)) == 0 ) {
5611 /* Tell everyone we updated smb.conf. */
5612 message_send_all(smbd_messaging_context(),
5613 MSG_SMB_CONF_UPDATED, NULL, 0, NULL);
5616 if ( is_print_op )
5617 unbecome_root();
5619 /********* END SePrintOperatorPrivilege **********/
5621 DEBUGADD(10,("returned [%d]\n", ret));
5623 TALLOC_FREE(command);
5624 TALLOC_FREE(remote_machine);
5626 if ( ret != 0 ) {
5627 if (fd != -1)
5628 close(fd);
5629 return False;
5632 /* reload our services immediately */
5633 reload_services( False );
5635 numlines = 0;
5636 /* Get lines and convert them back to dos-codepage */
5637 qlines = fd_lines_load(fd, &numlines, 0, NULL);
5638 DEBUGADD(10,("Lines returned = [%d]\n", numlines));
5639 close(fd);
5641 /* Set the portname to what the script says the portname should be. */
5642 /* but don't require anything to be return from the script exit a good error code */
5644 if (numlines) {
5645 /* Set the portname to what the script says the portname should be. */
5646 strncpy(printer->info_2->portname, qlines[0], sizeof(printer->info_2->portname));
5647 DEBUGADD(6,("Line[0] = [%s]\n", qlines[0]));
5650 TALLOC_FREE(qlines);
5651 return True;
5655 /********************************************************************
5656 * Called by spoolss_api_setprinter
5657 * when updating a printer description.
5658 ********************************************************************/
5660 static WERROR update_printer(pipes_struct *p, POLICY_HND *handle,
5661 struct spoolss_SetPrinterInfoCtr *info_ctr,
5662 struct spoolss_DeviceMode *devmode)
5664 int snum;
5665 NT_PRINTER_INFO_LEVEL *printer = NULL, *old_printer = NULL;
5666 Printer_entry *Printer = find_printer_index_by_hnd(p, handle);
5667 WERROR result;
5668 UNISTR2 buffer;
5669 fstring asc_buffer;
5671 DEBUG(8,("update_printer\n"));
5673 result = WERR_OK;
5675 if (!Printer) {
5676 result = WERR_BADFID;
5677 goto done;
5680 if (!get_printer_snum(p, handle, &snum, NULL)) {
5681 result = WERR_BADFID;
5682 goto done;
5685 if (!W_ERROR_IS_OK(get_a_printer(Printer, &printer, 2, lp_const_servicename(snum))) ||
5686 (!W_ERROR_IS_OK(get_a_printer(Printer, &old_printer, 2, lp_const_servicename(snum))))) {
5687 result = WERR_BADFID;
5688 goto done;
5691 DEBUGADD(8,("Converting info_2 struct\n"));
5694 * convert_printer_info converts the incoming
5695 * info from the client and overwrites the info
5696 * just read from the tdb in the pointer 'printer'.
5699 if (!convert_printer_info(info_ctr, printer)) {
5700 result = WERR_NOMEM;
5701 goto done;
5704 if (devmode) {
5705 /* we have a valid devmode
5706 convert it and link it*/
5708 DEBUGADD(8,("update_printer: Converting the devicemode struct\n"));
5709 if (!convert_devicemode(printer->info_2->printername, devmode,
5710 &printer->info_2->devmode)) {
5711 result = WERR_NOMEM;
5712 goto done;
5716 /* Do sanity check on the requested changes for Samba */
5718 if (!check_printer_ok(printer->info_2, snum)) {
5719 result = WERR_INVALID_PARAM;
5720 goto done;
5723 /* FIXME!!! If the driver has changed we really should verify that
5724 it is installed before doing much else --jerry */
5726 /* Check calling user has permission to update printer description */
5728 if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
5729 DEBUG(3, ("update_printer: printer property change denied by handle\n"));
5730 result = WERR_ACCESS_DENIED;
5731 goto done;
5734 /* Call addprinter hook */
5735 /* Check changes to see if this is really needed */
5737 if ( *lp_addprinter_cmd()
5738 && (!strequal(printer->info_2->drivername, old_printer->info_2->drivername)
5739 || !strequal(printer->info_2->comment, old_printer->info_2->comment)
5740 || !strequal(printer->info_2->portname, old_printer->info_2->portname)
5741 || !strequal(printer->info_2->location, old_printer->info_2->location)) )
5743 /* add_printer_hook() will call reload_services() */
5745 if ( !add_printer_hook(p->mem_ctx, p->server_info->ptok,
5746 printer) ) {
5747 result = WERR_ACCESS_DENIED;
5748 goto done;
5753 * When a *new* driver is bound to a printer, the drivername is used to
5754 * lookup previously saved driver initialization info, which is then
5755 * bound to the printer, simulating what happens in the Windows arch.
5757 if (!strequal(printer->info_2->drivername, old_printer->info_2->drivername))
5759 if (!set_driver_init(printer, 2))
5761 DEBUG(5,("update_printer: Error restoring driver initialization data for driver [%s]!\n",
5762 printer->info_2->drivername));
5765 DEBUG(10,("update_printer: changing driver [%s]! Sending event!\n",
5766 printer->info_2->drivername));
5768 notify_printer_driver(snum, printer->info_2->drivername);
5772 * flag which changes actually occured. This is a small subset of
5773 * all the possible changes. We also have to update things in the
5774 * DsSpooler key.
5777 if (!strequal(printer->info_2->comment, old_printer->info_2->comment)) {
5778 init_unistr2( &buffer, printer->info_2->comment, UNI_STR_TERMINATE);
5779 set_printer_dataex( printer, SPOOL_DSSPOOLER_KEY, "description",
5780 REG_SZ, (uint8*)buffer.buffer, buffer.uni_str_len*2 );
5782 notify_printer_comment(snum, printer->info_2->comment);
5785 if (!strequal(printer->info_2->sharename, old_printer->info_2->sharename)) {
5786 init_unistr2( &buffer, printer->info_2->sharename, UNI_STR_TERMINATE);
5787 set_printer_dataex( printer, SPOOL_DSSPOOLER_KEY, "shareName",
5788 REG_SZ, (uint8*)buffer.buffer, buffer.uni_str_len*2 );
5790 notify_printer_sharename(snum, printer->info_2->sharename);
5793 if (!strequal(printer->info_2->printername, old_printer->info_2->printername)) {
5794 char *pname;
5796 if ( (pname = strchr_m( printer->info_2->printername+2, '\\' )) != NULL )
5797 pname++;
5798 else
5799 pname = printer->info_2->printername;
5802 init_unistr2( &buffer, pname, UNI_STR_TERMINATE);
5803 set_printer_dataex( printer, SPOOL_DSSPOOLER_KEY, "printerName",
5804 REG_SZ, (uint8*)buffer.buffer, buffer.uni_str_len*2 );
5806 notify_printer_printername( snum, pname );
5809 if (!strequal(printer->info_2->portname, old_printer->info_2->portname)) {
5810 init_unistr2( &buffer, printer->info_2->portname, UNI_STR_TERMINATE);
5811 set_printer_dataex( printer, SPOOL_DSSPOOLER_KEY, "portName",
5812 REG_SZ, (uint8*)buffer.buffer, buffer.uni_str_len*2 );
5814 notify_printer_port(snum, printer->info_2->portname);
5817 if (!strequal(printer->info_2->location, old_printer->info_2->location)) {
5818 init_unistr2( &buffer, printer->info_2->location, UNI_STR_TERMINATE);
5819 set_printer_dataex( printer, SPOOL_DSSPOOLER_KEY, "location",
5820 REG_SZ, (uint8*)buffer.buffer, buffer.uni_str_len*2 );
5822 notify_printer_location(snum, printer->info_2->location);
5825 /* here we need to update some more DsSpooler keys */
5826 /* uNCName, serverName, shortServerName */
5828 init_unistr2( &buffer, global_myname(), UNI_STR_TERMINATE);
5829 set_printer_dataex( printer, SPOOL_DSSPOOLER_KEY, "serverName",
5830 REG_SZ, (uint8*)buffer.buffer, buffer.uni_str_len*2 );
5831 set_printer_dataex( printer, SPOOL_DSSPOOLER_KEY, "shortServerName",
5832 REG_SZ, (uint8*)buffer.buffer, buffer.uni_str_len*2 );
5834 slprintf( asc_buffer, sizeof(asc_buffer)-1, "\\\\%s\\%s",
5835 global_myname(), printer->info_2->sharename );
5836 init_unistr2( &buffer, asc_buffer, UNI_STR_TERMINATE);
5837 set_printer_dataex( printer, SPOOL_DSSPOOLER_KEY, "uNCName",
5838 REG_SZ, (uint8*)buffer.buffer, buffer.uni_str_len*2 );
5840 /* Update printer info */
5841 result = mod_a_printer(printer, 2);
5843 done:
5844 free_a_printer(&printer, 2);
5845 free_a_printer(&old_printer, 2);
5848 return result;
5851 /****************************************************************************
5852 ****************************************************************************/
5853 static WERROR publish_or_unpublish_printer(pipes_struct *p, POLICY_HND *handle,
5854 struct spoolss_SetPrinterInfo7 *info7)
5856 #ifdef HAVE_ADS
5857 int snum;
5858 Printer_entry *Printer;
5860 if ( lp_security() != SEC_ADS ) {
5861 return WERR_UNKNOWN_LEVEL;
5864 Printer = find_printer_index_by_hnd(p, handle);
5866 DEBUG(5,("publish_or_unpublish_printer, action = %d\n",info7->action));
5868 if (!Printer)
5869 return WERR_BADFID;
5871 if (!get_printer_snum(p, handle, &snum, NULL))
5872 return WERR_BADFID;
5874 nt_printer_publish(Printer, snum, info7->action);
5876 return WERR_OK;
5877 #else
5878 return WERR_UNKNOWN_LEVEL;
5879 #endif
5882 /****************************************************************
5883 _spoolss_SetPrinter
5884 ****************************************************************/
5886 WERROR _spoolss_SetPrinter(pipes_struct *p,
5887 struct spoolss_SetPrinter *r)
5889 POLICY_HND *handle = r->in.handle;
5890 WERROR result;
5892 Printer_entry *Printer = find_printer_index_by_hnd(p, handle);
5894 if (!Printer) {
5895 DEBUG(2,("_spoolss_SetPrinter: Invalid handle (%s:%u:%u)\n",
5896 OUR_HANDLE(handle)));
5897 return WERR_BADFID;
5900 /* check the level */
5901 switch (r->in.info_ctr->level) {
5902 case 0:
5903 return control_printer(handle, r->in.command, p);
5904 case 2:
5905 result = update_printer(p, handle,
5906 r->in.info_ctr,
5907 r->in.devmode_ctr->devmode);
5908 if (!W_ERROR_IS_OK(result))
5909 return result;
5910 if (r->in.secdesc_ctr->sd)
5911 result = update_printer_sec(handle, p,
5912 r->in.secdesc_ctr);
5913 return result;
5914 case 3:
5915 return update_printer_sec(handle, p,
5916 r->in.secdesc_ctr);
5917 case 7:
5918 return publish_or_unpublish_printer(p, handle,
5919 r->in.info_ctr->info.info7);
5920 default:
5921 return WERR_UNKNOWN_LEVEL;
5925 /****************************************************************
5926 _spoolss_FindClosePrinterNotify
5927 ****************************************************************/
5929 WERROR _spoolss_FindClosePrinterNotify(pipes_struct *p,
5930 struct spoolss_FindClosePrinterNotify *r)
5932 POLICY_HND *handle = r->in.handle;
5933 Printer_entry *Printer= find_printer_index_by_hnd(p, handle);
5935 if (!Printer) {
5936 DEBUG(2,("_spoolss_FindClosePrinterNotify: "
5937 "Invalid handle (%s:%u:%u)\n", OUR_HANDLE(handle)));
5938 return WERR_BADFID;
5941 if (Printer->notify.client_connected==True) {
5942 int snum = -1;
5944 if ( Printer->printer_type == SPLHND_SERVER)
5945 snum = -1;
5946 else if ( (Printer->printer_type == SPLHND_PRINTER) &&
5947 !get_printer_snum(p, handle, &snum, NULL) )
5948 return WERR_BADFID;
5950 srv_spoolss_replycloseprinter(snum, &Printer->notify.client_hnd);
5953 Printer->notify.flags=0;
5954 Printer->notify.options=0;
5955 Printer->notify.localmachine[0]='\0';
5956 Printer->notify.printerlocal=0;
5957 TALLOC_FREE(Printer->notify.option);
5958 Printer->notify.client_connected=False;
5960 return WERR_OK;
5963 /****************************************************************
5964 _spoolss_AddJob
5965 ****************************************************************/
5967 WERROR _spoolss_AddJob(pipes_struct *p,
5968 struct spoolss_AddJob *r)
5970 if (!r->in.buffer && (r->in.offered != 0)) {
5971 return WERR_INVALID_PARAM;
5974 /* this is what a NT server returns for AddJob. AddJob must fail on
5975 * non-local printers */
5977 if (r->in.level != 1) {
5978 return WERR_UNKNOWN_LEVEL;
5981 return WERR_INVALID_PARAM;
5984 /****************************************************************************
5985 fill_job_info1
5986 ****************************************************************************/
5988 static WERROR fill_job_info1(TALLOC_CTX *mem_ctx,
5989 struct spoolss_JobInfo1 *r,
5990 const print_queue_struct *queue,
5991 int position, int snum,
5992 const NT_PRINTER_INFO_LEVEL *ntprinter)
5994 struct tm *t;
5996 t = gmtime(&queue->time);
5998 r->job_id = queue->job;
6000 r->printer_name = talloc_strdup(mem_ctx, lp_servicename(snum));
6001 W_ERROR_HAVE_NO_MEMORY(r->printer_name);
6002 r->server_name = talloc_strdup(mem_ctx, ntprinter->info_2->servername);
6003 W_ERROR_HAVE_NO_MEMORY(r->server_name);
6004 r->user_name = talloc_strdup(mem_ctx, queue->fs_user);
6005 W_ERROR_HAVE_NO_MEMORY(r->user_name);
6006 r->document_name = talloc_strdup(mem_ctx, queue->fs_file);
6007 W_ERROR_HAVE_NO_MEMORY(r->document_name);
6008 r->data_type = talloc_strdup(mem_ctx, "RAW");
6009 W_ERROR_HAVE_NO_MEMORY(r->data_type);
6010 r->text_status = talloc_strdup(mem_ctx, "");
6011 W_ERROR_HAVE_NO_MEMORY(r->text_status);
6013 r->status = nt_printj_status(queue->status);
6014 r->priority = queue->priority;
6015 r->position = position;
6016 r->total_pages = queue->page_count;
6017 r->pages_printed = 0; /* ??? */
6019 init_systemtime(&r->submitted, t);
6021 return WERR_OK;
6024 /****************************************************************************
6025 fill_job_info2
6026 ****************************************************************************/
6028 static WERROR fill_job_info2(TALLOC_CTX *mem_ctx,
6029 struct spoolss_JobInfo2 *r,
6030 const print_queue_struct *queue,
6031 int position, int snum,
6032 const NT_PRINTER_INFO_LEVEL *ntprinter,
6033 struct spoolss_DeviceMode *devmode)
6035 struct tm *t;
6037 t = gmtime(&queue->time);
6039 r->job_id = queue->job;
6041 r->printer_name = talloc_strdup(mem_ctx, lp_servicename(snum));
6042 W_ERROR_HAVE_NO_MEMORY(r->printer_name);
6043 r->server_name = talloc_strdup(mem_ctx, ntprinter->info_2->servername);
6044 W_ERROR_HAVE_NO_MEMORY(r->server_name);
6045 r->user_name = talloc_strdup(mem_ctx, queue->fs_user);
6046 W_ERROR_HAVE_NO_MEMORY(r->user_name);
6047 r->document_name = talloc_strdup(mem_ctx, queue->fs_file);
6048 W_ERROR_HAVE_NO_MEMORY(r->document_name);
6049 r->notify_name = talloc_strdup(mem_ctx, queue->fs_user);
6050 W_ERROR_HAVE_NO_MEMORY(r->notify_name);
6051 r->data_type = talloc_strdup(mem_ctx, "RAW");
6052 W_ERROR_HAVE_NO_MEMORY(r->data_type);
6053 r->print_processor = talloc_strdup(mem_ctx, "winprint");
6054 W_ERROR_HAVE_NO_MEMORY(r->print_processor);
6055 r->parameters = talloc_strdup(mem_ctx, "");
6056 W_ERROR_HAVE_NO_MEMORY(r->parameters);
6057 r->driver_name = talloc_strdup(mem_ctx, ntprinter->info_2->drivername);
6058 W_ERROR_HAVE_NO_MEMORY(r->driver_name);
6060 r->devmode = devmode;
6062 r->text_status = talloc_strdup(mem_ctx, "");
6063 W_ERROR_HAVE_NO_MEMORY(r->text_status);
6065 r->secdesc = NULL;
6067 r->status = nt_printj_status(queue->status);
6068 r->priority = queue->priority;
6069 r->position = position;
6070 r->start_time = 0;
6071 r->until_time = 0;
6072 r->total_pages = queue->page_count;
6073 r->size = queue->size;
6074 init_systemtime(&r->submitted, t);
6075 r->time = 0;
6076 r->pages_printed = 0; /* ??? */
6078 return WERR_OK;
6081 /****************************************************************************
6082 Enumjobs at level 1.
6083 ****************************************************************************/
6085 static WERROR enumjobs_level1(TALLOC_CTX *mem_ctx,
6086 const print_queue_struct *queue,
6087 uint32_t num_queues, int snum,
6088 const NT_PRINTER_INFO_LEVEL *ntprinter,
6089 union spoolss_JobInfo **info_p,
6090 uint32_t *count)
6092 union spoolss_JobInfo *info;
6093 int i;
6094 WERROR result = WERR_OK;
6096 info = TALLOC_ARRAY(mem_ctx, union spoolss_JobInfo, num_queues);
6097 W_ERROR_HAVE_NO_MEMORY(info);
6099 *count = num_queues;
6101 for (i=0; i<*count; i++) {
6102 result = fill_job_info1(info,
6103 &info[i].info1,
6104 &queue[i],
6106 snum,
6107 ntprinter);
6108 if (!W_ERROR_IS_OK(result)) {
6109 goto out;
6113 out:
6114 if (!W_ERROR_IS_OK(result)) {
6115 TALLOC_FREE(info);
6116 *count = 0;
6117 return result;
6120 *info_p = info;
6122 return WERR_OK;
6125 /****************************************************************************
6126 Enumjobs at level 2.
6127 ****************************************************************************/
6129 static WERROR enumjobs_level2(TALLOC_CTX *mem_ctx,
6130 const print_queue_struct *queue,
6131 uint32_t num_queues, int snum,
6132 const NT_PRINTER_INFO_LEVEL *ntprinter,
6133 union spoolss_JobInfo **info_p,
6134 uint32_t *count)
6136 union spoolss_JobInfo *info;
6137 int i;
6138 WERROR result = WERR_OK;
6140 info = TALLOC_ARRAY(mem_ctx, union spoolss_JobInfo, num_queues);
6141 W_ERROR_HAVE_NO_MEMORY(info);
6143 *count = num_queues;
6145 for (i=0; i<*count; i++) {
6147 struct spoolss_DeviceMode *devmode;
6149 devmode = construct_dev_mode(info, lp_const_servicename(snum));
6150 if (!devmode) {
6151 result = WERR_NOMEM;
6152 goto out;
6155 result = fill_job_info2(info,
6156 &info[i].info2,
6157 &queue[i],
6159 snum,
6160 ntprinter,
6161 devmode);
6162 if (!W_ERROR_IS_OK(result)) {
6163 goto out;
6167 out:
6168 if (!W_ERROR_IS_OK(result)) {
6169 TALLOC_FREE(info);
6170 *count = 0;
6171 return result;
6174 *info_p = info;
6176 return WERR_OK;
6179 /****************************************************************
6180 _spoolss_EnumJobs
6181 ****************************************************************/
6183 WERROR _spoolss_EnumJobs(pipes_struct *p,
6184 struct spoolss_EnumJobs *r)
6186 WERROR result;
6187 NT_PRINTER_INFO_LEVEL *ntprinter = NULL;
6188 int snum;
6189 print_status_struct prt_status;
6190 print_queue_struct *queue = NULL;
6191 uint32_t count;
6193 /* that's an [in out] buffer */
6195 if (!r->in.buffer && (r->in.offered != 0)) {
6196 return WERR_INVALID_PARAM;
6199 DEBUG(4,("_spoolss_EnumJobs\n"));
6201 *r->out.needed = 0;
6202 *r->out.count = 0;
6203 *r->out.info = NULL;
6205 /* lookup the printer snum and tdb entry */
6207 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
6208 return WERR_BADFID;
6211 result = get_a_printer(NULL, &ntprinter, 2, lp_servicename(snum));
6212 if (!W_ERROR_IS_OK(result)) {
6213 return result;
6216 count = print_queue_status(snum, &queue, &prt_status);
6217 DEBUGADD(4,("count:[%d], status:[%d], [%s]\n",
6218 count, prt_status.status, prt_status.message));
6220 if (count == 0) {
6221 SAFE_FREE(queue);
6222 free_a_printer(&ntprinter, 2);
6223 return WERR_OK;
6226 switch (r->in.level) {
6227 case 1:
6228 result = enumjobs_level1(p->mem_ctx, queue, count, snum,
6229 ntprinter, r->out.info, r->out.count);
6230 break;
6231 case 2:
6232 result = enumjobs_level2(p->mem_ctx, queue, count, snum,
6233 ntprinter, r->out.info, r->out.count);
6234 break;
6235 default:
6236 result = WERR_UNKNOWN_LEVEL;
6237 break;
6240 SAFE_FREE(queue);
6241 free_a_printer(&ntprinter, 2);
6243 if (!W_ERROR_IS_OK(result)) {
6244 return result;
6247 *r->out.needed = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
6248 spoolss_EnumJobs, NULL,
6249 *r->out.info, r->in.level,
6250 *r->out.count);
6251 *r->out.info = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
6252 *r->out.count = SPOOLSS_BUFFER_OK(*r->out.count, 0);
6254 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
6257 /****************************************************************
6258 _spoolss_ScheduleJob
6259 ****************************************************************/
6261 WERROR _spoolss_ScheduleJob(pipes_struct *p,
6262 struct spoolss_ScheduleJob *r)
6264 return WERR_OK;
6267 /****************************************************************
6268 _spoolss_SetJob
6269 ****************************************************************/
6271 WERROR _spoolss_SetJob(pipes_struct *p,
6272 struct spoolss_SetJob *r)
6274 POLICY_HND *handle = r->in.handle;
6275 uint32 jobid = r->in.job_id;
6276 uint32 command = r->in.command;
6278 int snum;
6279 WERROR errcode = WERR_BADFUNC;
6281 if (!get_printer_snum(p, handle, &snum, NULL)) {
6282 return WERR_BADFID;
6285 if (!print_job_exists(lp_const_servicename(snum), jobid)) {
6286 return WERR_INVALID_PRINTER_NAME;
6289 switch (command) {
6290 case SPOOLSS_JOB_CONTROL_CANCEL:
6291 case SPOOLSS_JOB_CONTROL_DELETE:
6292 if (print_job_delete(p->server_info, snum, jobid, &errcode)) {
6293 errcode = WERR_OK;
6295 break;
6296 case SPOOLSS_JOB_CONTROL_PAUSE:
6297 if (print_job_pause(p->server_info, snum, jobid, &errcode)) {
6298 errcode = WERR_OK;
6300 break;
6301 case SPOOLSS_JOB_CONTROL_RESTART:
6302 case SPOOLSS_JOB_CONTROL_RESUME:
6303 if (print_job_resume(p->server_info, snum, jobid, &errcode)) {
6304 errcode = WERR_OK;
6306 break;
6307 default:
6308 return WERR_UNKNOWN_LEVEL;
6311 return errcode;
6314 /****************************************************************************
6315 Enumerates all printer drivers at level 1.
6316 ****************************************************************************/
6318 static WERROR enumprinterdrivers_level1(TALLOC_CTX *mem_ctx,
6319 const char *servername,
6320 const char *architecture,
6321 union spoolss_DriverInfo **info_p,
6322 uint32_t *count)
6324 int i;
6325 int ndrivers;
6326 uint32_t version;
6327 fstring *list = NULL;
6328 NT_PRINTER_DRIVER_INFO_LEVEL driver;
6329 union spoolss_DriverInfo *info = NULL;
6330 WERROR result = WERR_OK;
6332 *count = 0;
6334 for (version=0; version<DRIVER_MAX_VERSION; version++) {
6335 list = NULL;
6336 ndrivers = get_ntdrivers(&list, architecture, version);
6337 DEBUGADD(4,("we have:[%d] drivers in environment [%s] and version [%d]\n",
6338 ndrivers, architecture, version));
6340 if (ndrivers == -1) {
6341 result = WERR_NOMEM;
6342 goto out;
6345 if (ndrivers != 0) {
6346 info = TALLOC_REALLOC_ARRAY(mem_ctx, info,
6347 union spoolss_DriverInfo,
6348 *count + ndrivers);
6349 if (!info) {
6350 DEBUG(0,("enumprinterdrivers_level1: "
6351 "failed to enlarge driver info buffer!\n"));
6352 result = WERR_NOMEM;
6353 goto out;
6357 for (i=0; i<ndrivers; i++) {
6358 DEBUGADD(5,("\tdriver: [%s]\n", list[i]));
6359 ZERO_STRUCT(driver);
6360 result = get_a_printer_driver(&driver, 3, list[i],
6361 architecture, version);
6362 if (!W_ERROR_IS_OK(result)) {
6363 goto out;
6365 result = fill_printer_driver_info1(info, &info[*count+i].info1,
6366 &driver, servername,
6367 architecture);
6368 if (!W_ERROR_IS_OK(result)) {
6369 free_a_printer_driver(driver, 3);
6370 goto out;
6372 free_a_printer_driver(driver, 3);
6375 *count += ndrivers;
6376 SAFE_FREE(list);
6379 out:
6380 SAFE_FREE(list);
6382 if (!W_ERROR_IS_OK(result)) {
6383 TALLOC_FREE(info);
6384 *count = 0;
6385 return result;
6388 *info_p = info;
6390 return WERR_OK;
6393 /****************************************************************************
6394 Enumerates all printer drivers at level 2.
6395 ****************************************************************************/
6397 static WERROR enumprinterdrivers_level2(TALLOC_CTX *mem_ctx,
6398 const char *servername,
6399 const char *architecture,
6400 union spoolss_DriverInfo **info_p,
6401 uint32_t *count)
6403 int i;
6404 int ndrivers;
6405 uint32_t version;
6406 fstring *list = NULL;
6407 NT_PRINTER_DRIVER_INFO_LEVEL driver;
6408 union spoolss_DriverInfo *info = NULL;
6409 WERROR result = WERR_OK;
6411 *count = 0;
6413 for (version=0; version<DRIVER_MAX_VERSION; version++) {
6414 list = NULL;
6415 ndrivers = get_ntdrivers(&list, architecture, version);
6416 DEBUGADD(4,("we have:[%d] drivers in environment [%s] and version [%d]\n",
6417 ndrivers, architecture, version));
6419 if (ndrivers == -1) {
6420 result = WERR_NOMEM;
6421 goto out;
6424 if (ndrivers != 0) {
6425 info = TALLOC_REALLOC_ARRAY(mem_ctx, info,
6426 union spoolss_DriverInfo,
6427 *count + ndrivers);
6428 if (!info) {
6429 DEBUG(0,("enumprinterdrivers_level2: "
6430 "failed to enlarge driver info buffer!\n"));
6431 result = WERR_NOMEM;
6432 goto out;
6436 for (i=0; i<ndrivers; i++) {
6437 DEBUGADD(5,("\tdriver: [%s]\n", list[i]));
6438 ZERO_STRUCT(driver);
6439 result = get_a_printer_driver(&driver, 3, list[i],
6440 architecture, version);
6441 if (!W_ERROR_IS_OK(result)) {
6442 goto out;
6444 result = fill_printer_driver_info2(info, &info[*count+i].info2,
6445 &driver, servername);
6446 if (!W_ERROR_IS_OK(result)) {
6447 free_a_printer_driver(driver, 3);
6448 goto out;
6450 free_a_printer_driver(driver, 3);
6453 *count += ndrivers;
6454 SAFE_FREE(list);
6457 out:
6458 SAFE_FREE(list);
6460 if (!W_ERROR_IS_OK(result)) {
6461 TALLOC_FREE(info);
6462 *count = 0;
6463 return result;
6466 *info_p = info;
6468 return WERR_OK;
6471 /****************************************************************************
6472 Enumerates all printer drivers at level 3.
6473 ****************************************************************************/
6475 static WERROR enumprinterdrivers_level3(TALLOC_CTX *mem_ctx,
6476 const char *servername,
6477 const char *architecture,
6478 union spoolss_DriverInfo **info_p,
6479 uint32_t *count)
6481 int i;
6482 int ndrivers;
6483 uint32_t version;
6484 fstring *list = NULL;
6485 union spoolss_DriverInfo *info = NULL;
6486 NT_PRINTER_DRIVER_INFO_LEVEL driver;
6487 WERROR result = WERR_OK;
6489 *count = 0;
6491 for (version=0; version<DRIVER_MAX_VERSION; version++) {
6492 list = NULL;
6493 ndrivers = get_ntdrivers(&list, architecture, version);
6494 DEBUGADD(4,("we have:[%d] drivers in environment [%s] and version [%d]\n",
6495 ndrivers, architecture, version));
6497 if (ndrivers == -1) {
6498 result = WERR_NOMEM;
6499 goto out;
6502 if (ndrivers != 0) {
6503 info = TALLOC_REALLOC_ARRAY(mem_ctx, info,
6504 union spoolss_DriverInfo,
6505 *count + ndrivers);
6506 if (!info) {
6507 DEBUG(0,("enumprinterdrivers_level3: "
6508 "failed to enlarge driver info buffer!\n"));
6509 result = WERR_NOMEM;
6510 goto out;
6514 for (i=0; i<ndrivers; i++) {
6515 DEBUGADD(5,("\tdriver: [%s]\n", list[i]));
6516 ZERO_STRUCT(driver);
6517 result = get_a_printer_driver(&driver, 3, list[i],
6518 architecture, version);
6519 if (!W_ERROR_IS_OK(result)) {
6520 goto out;
6522 result = fill_printer_driver_info3(info, &info[*count+i].info3,
6523 &driver, servername);
6524 if (!W_ERROR_IS_OK(result)) {
6525 free_a_printer_driver(driver, 3);
6526 goto out;
6529 free_a_printer_driver(driver, 3);
6532 *count += ndrivers;
6533 SAFE_FREE(list);
6536 out:
6537 SAFE_FREE(list);
6539 if (!W_ERROR_IS_OK(result)) {
6540 TALLOC_FREE(info);
6541 *count = 0;
6542 return result;
6545 *info_p = info;
6547 return WERR_OK;
6550 /****************************************************************
6551 _spoolss_EnumPrinterDrivers
6552 ****************************************************************/
6554 WERROR _spoolss_EnumPrinterDrivers(pipes_struct *p,
6555 struct spoolss_EnumPrinterDrivers *r)
6557 const char *cservername;
6558 WERROR result;
6560 /* that's an [in out] buffer */
6562 if (!r->in.buffer && (r->in.offered != 0)) {
6563 return WERR_INVALID_PARAM;
6566 DEBUG(4,("_spoolss_EnumPrinterDrivers\n"));
6568 *r->out.needed = 0;
6569 *r->out.count = 0;
6570 *r->out.info = NULL;
6572 cservername = canon_servername(r->in.server);
6574 if (!is_myname_or_ipaddr(cservername)) {
6575 return WERR_UNKNOWN_PRINTER_DRIVER;
6578 switch (r->in.level) {
6579 case 1:
6580 result = enumprinterdrivers_level1(p->mem_ctx, cservername,
6581 r->in.environment,
6582 r->out.info, r->out.count);
6583 break;
6584 case 2:
6585 result = enumprinterdrivers_level2(p->mem_ctx, cservername,
6586 r->in.environment,
6587 r->out.info, r->out.count);
6588 break;
6589 case 3:
6590 result = enumprinterdrivers_level3(p->mem_ctx, cservername,
6591 r->in.environment,
6592 r->out.info, r->out.count);
6593 break;
6594 default:
6595 return WERR_UNKNOWN_LEVEL;
6598 if (!W_ERROR_IS_OK(result)) {
6599 return result;
6602 *r->out.needed = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
6603 spoolss_EnumPrinterDrivers, NULL,
6604 *r->out.info, r->in.level,
6605 *r->out.count);
6606 *r->out.info = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
6607 *r->out.count = SPOOLSS_BUFFER_OK(*r->out.count, 0);
6609 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
6612 /****************************************************************************
6613 ****************************************************************************/
6615 static WERROR fill_form_info_1(TALLOC_CTX *mem_ctx,
6616 struct spoolss_FormInfo1 *r,
6617 const nt_forms_struct *form)
6619 r->form_name = talloc_strdup(mem_ctx, form->name);
6620 W_ERROR_HAVE_NO_MEMORY(r->form_name);
6622 r->flags = form->flag;
6623 r->size.width = form->width;
6624 r->size.height = form->length;
6625 r->area.left = form->left;
6626 r->area.top = form->top;
6627 r->area.right = form->right;
6628 r->area.bottom = form->bottom;
6630 return WERR_OK;
6633 /****************************************************************
6634 spoolss_enumforms_level1
6635 ****************************************************************/
6637 static WERROR spoolss_enumforms_level1(TALLOC_CTX *mem_ctx,
6638 const nt_forms_struct *builtin_forms,
6639 uint32_t num_builtin_forms,
6640 const nt_forms_struct *user_forms,
6641 uint32_t num_user_forms,
6642 union spoolss_FormInfo **info_p,
6643 uint32_t *count)
6645 union spoolss_FormInfo *info;
6646 WERROR result = WERR_OK;
6647 int i;
6649 *count = num_builtin_forms + num_user_forms;
6651 info = TALLOC_ARRAY(mem_ctx, union spoolss_FormInfo, *count);
6652 W_ERROR_HAVE_NO_MEMORY(info);
6654 /* construct the list of form structures */
6655 for (i=0; i<num_builtin_forms; i++) {
6656 DEBUGADD(6,("Filling form number [%d]\n",i));
6657 result = fill_form_info_1(info, &info[i].info1,
6658 &builtin_forms[i]);
6659 if (!W_ERROR_IS_OK(result)) {
6660 goto out;
6664 for (; i<num_user_forms; i++) {
6665 DEBUGADD(6,("Filling form number [%d]\n",i));
6666 result = fill_form_info_1(info, &info[i].info1,
6667 &user_forms[i-num_builtin_forms]);
6668 if (!W_ERROR_IS_OK(result)) {
6669 goto out;
6673 out:
6674 if (!W_ERROR_IS_OK(result)) {
6675 TALLOC_FREE(info);
6676 *count = 0;
6677 return result;
6680 *info_p = info;
6682 return WERR_OK;
6685 /****************************************************************
6686 _spoolss_EnumForms
6687 ****************************************************************/
6689 WERROR _spoolss_EnumForms(pipes_struct *p,
6690 struct spoolss_EnumForms *r)
6692 WERROR result;
6693 nt_forms_struct *user_forms = NULL;
6694 nt_forms_struct *builtin_forms = NULL;
6695 uint32_t num_user_forms;
6696 uint32_t num_builtin_forms;
6698 *r->out.count = 0;
6699 *r->out.needed = 0;
6700 *r->out.info = NULL;
6702 /* that's an [in out] buffer */
6704 if (!r->in.buffer && (r->in.offered != 0) ) {
6705 return WERR_INVALID_PARAM;
6708 DEBUG(4,("_spoolss_EnumForms\n"));
6709 DEBUGADD(5,("Offered buffer size [%d]\n", r->in.offered));
6710 DEBUGADD(5,("Info level [%d]\n", r->in.level));
6712 num_builtin_forms = get_builtin_ntforms(&builtin_forms);
6713 DEBUGADD(5,("Number of builtin forms [%d]\n", num_builtin_forms));
6714 num_user_forms = get_ntforms(&user_forms);
6715 DEBUGADD(5,("Number of user forms [%d]\n", num_user_forms));
6717 if (num_user_forms + num_builtin_forms == 0) {
6718 SAFE_FREE(builtin_forms);
6719 SAFE_FREE(user_forms);
6720 return WERR_NO_MORE_ITEMS;
6723 switch (r->in.level) {
6724 case 1:
6725 result = spoolss_enumforms_level1(p->mem_ctx,
6726 builtin_forms,
6727 num_builtin_forms,
6728 user_forms,
6729 num_user_forms,
6730 r->out.info,
6731 r->out.count);
6732 break;
6733 default:
6734 result = WERR_UNKNOWN_LEVEL;
6735 break;
6738 SAFE_FREE(user_forms);
6739 SAFE_FREE(builtin_forms);
6741 if (!W_ERROR_IS_OK(result)) {
6742 return result;
6745 *r->out.needed = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
6746 spoolss_EnumForms, NULL,
6747 *r->out.info, r->in.level,
6748 *r->out.count);
6749 *r->out.info = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
6750 *r->out.count = SPOOLSS_BUFFER_OK(*r->out.count, 0);
6752 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
6755 /****************************************************************
6756 ****************************************************************/
6758 static WERROR find_form_byname(const char *name,
6759 nt_forms_struct *form)
6761 nt_forms_struct *list = NULL;
6762 int num_forms = 0, i = 0;
6764 if (get_a_builtin_ntform_by_string(name, form)) {
6765 return WERR_OK;
6768 num_forms = get_ntforms(&list);
6769 DEBUGADD(5,("Number of forms [%d]\n", num_forms));
6771 if (num_forms == 0) {
6772 return WERR_BADFID;
6775 /* Check if the requested name is in the list of form structures */
6776 for (i = 0; i < num_forms; i++) {
6778 DEBUG(4,("checking form %s (want %s)\n", list[i].name, name));
6780 if (strequal(name, list[i].name)) {
6781 DEBUGADD(6,("Found form %s number [%d]\n", name, i));
6782 *form = list[i];
6783 SAFE_FREE(list);
6784 return WERR_OK;
6788 SAFE_FREE(list);
6790 return WERR_BADFID;
6793 /****************************************************************
6794 _spoolss_GetForm
6795 ****************************************************************/
6797 WERROR _spoolss_GetForm(pipes_struct *p,
6798 struct spoolss_GetForm *r)
6800 WERROR result;
6801 nt_forms_struct form;
6803 /* that's an [in out] buffer */
6805 if (!r->in.buffer && (r->in.offered != 0)) {
6806 return WERR_INVALID_PARAM;
6809 DEBUG(4,("_spoolss_GetForm\n"));
6810 DEBUGADD(5,("Offered buffer size [%d]\n", r->in.offered));
6811 DEBUGADD(5,("Info level [%d]\n", r->in.level));
6813 result = find_form_byname(r->in.form_name, &form);
6814 if (!W_ERROR_IS_OK(result)) {
6815 TALLOC_FREE(r->out.info);
6816 return result;
6819 switch (r->in.level) {
6820 case 1:
6821 result = fill_form_info_1(p->mem_ctx,
6822 &r->out.info->info1,
6823 &form);
6824 break;
6826 default:
6827 result = WERR_UNKNOWN_LEVEL;
6828 break;
6831 if (!W_ERROR_IS_OK(result)) {
6832 TALLOC_FREE(r->out.info);
6833 return result;
6836 *r->out.needed = SPOOLSS_BUFFER_UNION(spoolss_FormInfo, NULL,
6837 r->out.info, r->in.level);
6838 r->out.info = SPOOLSS_BUFFER_OK(r->out.info, NULL);
6840 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
6843 /****************************************************************************
6844 ****************************************************************************/
6846 static WERROR fill_port_1(TALLOC_CTX *mem_ctx,
6847 struct spoolss_PortInfo1 *r,
6848 const char *name)
6850 r->port_name = talloc_strdup(mem_ctx, name);
6851 W_ERROR_HAVE_NO_MEMORY(r->port_name);
6853 return WERR_OK;
6856 /****************************************************************************
6857 TODO: This probably needs distinguish between TCP/IP and Local ports
6858 somehow.
6859 ****************************************************************************/
6861 static WERROR fill_port_2(TALLOC_CTX *mem_ctx,
6862 struct spoolss_PortInfo2 *r,
6863 const char *name)
6865 r->port_name = talloc_strdup(mem_ctx, name);
6866 W_ERROR_HAVE_NO_MEMORY(r->port_name);
6868 r->monitor_name = talloc_strdup(mem_ctx, "Local Monitor");
6869 W_ERROR_HAVE_NO_MEMORY(r->monitor_name);
6871 r->description = talloc_strdup(mem_ctx, SPL_LOCAL_PORT);
6872 W_ERROR_HAVE_NO_MEMORY(r->description);
6874 r->port_type = SPOOLSS_PORT_TYPE_WRITE;
6875 r->reserved = 0;
6877 return WERR_OK;
6881 /****************************************************************************
6882 wrapper around the enumer ports command
6883 ****************************************************************************/
6885 WERROR enumports_hook(TALLOC_CTX *ctx, int *count, char ***lines )
6887 char *cmd = lp_enumports_cmd();
6888 char **qlines = NULL;
6889 char *command = NULL;
6890 int numlines;
6891 int ret;
6892 int fd;
6894 *count = 0;
6895 *lines = NULL;
6897 /* if no hook then just fill in the default port */
6899 if ( !*cmd ) {
6900 if (!(qlines = TALLOC_ARRAY( NULL, char*, 2 ))) {
6901 return WERR_NOMEM;
6903 if (!(qlines[0] = talloc_strdup(qlines, SAMBA_PRINTER_PORT_NAME ))) {
6904 TALLOC_FREE(qlines);
6905 return WERR_NOMEM;
6907 qlines[1] = NULL;
6908 numlines = 1;
6910 else {
6911 /* we have a valid enumport command */
6913 command = talloc_asprintf(ctx, "%s \"%d\"", cmd, 1);
6914 if (!command) {
6915 return WERR_NOMEM;
6918 DEBUG(10,("Running [%s]\n", command));
6919 ret = smbrun(command, &fd);
6920 DEBUG(10,("Returned [%d]\n", ret));
6921 TALLOC_FREE(command);
6922 if (ret != 0) {
6923 if (fd != -1) {
6924 close(fd);
6926 return WERR_ACCESS_DENIED;
6929 numlines = 0;
6930 qlines = fd_lines_load(fd, &numlines, 0, NULL);
6931 DEBUGADD(10,("Lines returned = [%d]\n", numlines));
6932 close(fd);
6935 *count = numlines;
6936 *lines = qlines;
6938 return WERR_OK;
6941 /****************************************************************************
6942 enumports level 1.
6943 ****************************************************************************/
6945 static WERROR enumports_level_1(TALLOC_CTX *mem_ctx,
6946 union spoolss_PortInfo **info_p,
6947 uint32_t *count)
6949 union spoolss_PortInfo *info = NULL;
6950 int i=0;
6951 WERROR result = WERR_OK;
6952 char **qlines = NULL;
6953 int numlines = 0;
6955 result = enumports_hook(talloc_tos(), &numlines, &qlines );
6956 if (!W_ERROR_IS_OK(result)) {
6957 goto out;
6960 if (numlines) {
6961 info = TALLOC_ARRAY(mem_ctx, union spoolss_PortInfo, numlines);
6962 if (!info) {
6963 DEBUG(10,("Returning WERR_NOMEM\n"));
6964 result = WERR_NOMEM;
6965 goto out;
6968 for (i=0; i<numlines; i++) {
6969 DEBUG(6,("Filling port number [%d] with port [%s]\n", i, qlines[i]));
6970 result = fill_port_1(info, &info[i].info1, qlines[i]);
6971 if (!W_ERROR_IS_OK(result)) {
6972 goto out;
6976 TALLOC_FREE(qlines);
6978 out:
6979 if (!W_ERROR_IS_OK(result)) {
6980 TALLOC_FREE(info);
6981 TALLOC_FREE(qlines);
6982 *count = 0;
6983 *info_p = NULL;
6984 return result;
6987 *info_p = info;
6988 *count = numlines;
6990 return WERR_OK;
6993 /****************************************************************************
6994 enumports level 2.
6995 ****************************************************************************/
6997 static WERROR enumports_level_2(TALLOC_CTX *mem_ctx,
6998 union spoolss_PortInfo **info_p,
6999 uint32_t *count)
7001 union spoolss_PortInfo *info = NULL;
7002 int i=0;
7003 WERROR result = WERR_OK;
7004 char **qlines = NULL;
7005 int numlines = 0;
7007 result = enumports_hook(talloc_tos(), &numlines, &qlines );
7008 if (!W_ERROR_IS_OK(result)) {
7009 goto out;
7012 if (numlines) {
7013 info = TALLOC_ARRAY(mem_ctx, union spoolss_PortInfo, numlines);
7014 if (!info) {
7015 DEBUG(10,("Returning WERR_NOMEM\n"));
7016 result = WERR_NOMEM;
7017 goto out;
7020 for (i=0; i<numlines; i++) {
7021 DEBUG(6,("Filling port number [%d] with port [%s]\n", i, qlines[i]));
7022 result = fill_port_2(info, &info[i].info2, qlines[i]);
7023 if (!W_ERROR_IS_OK(result)) {
7024 goto out;
7028 TALLOC_FREE(qlines);
7030 out:
7031 if (!W_ERROR_IS_OK(result)) {
7032 TALLOC_FREE(info);
7033 TALLOC_FREE(qlines);
7034 *count = 0;
7035 *info_p = NULL;
7036 return result;
7039 *info_p = info;
7040 *count = numlines;
7042 return WERR_OK;
7045 /****************************************************************
7046 _spoolss_EnumPorts
7047 ****************************************************************/
7049 WERROR _spoolss_EnumPorts(pipes_struct *p,
7050 struct spoolss_EnumPorts *r)
7052 WERROR result;
7054 /* that's an [in out] buffer */
7056 if (!r->in.buffer && (r->in.offered != 0)) {
7057 return WERR_INVALID_PARAM;
7060 DEBUG(4,("_spoolss_EnumPorts\n"));
7062 *r->out.count = 0;
7063 *r->out.needed = 0;
7064 *r->out.info = NULL;
7066 switch (r->in.level) {
7067 case 1:
7068 result = enumports_level_1(p->mem_ctx, r->out.info,
7069 r->out.count);
7070 break;
7071 case 2:
7072 result = enumports_level_2(p->mem_ctx, r->out.info,
7073 r->out.count);
7074 break;
7075 default:
7076 return WERR_UNKNOWN_LEVEL;
7079 if (!W_ERROR_IS_OK(result)) {
7080 return result;
7083 *r->out.needed = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
7084 spoolss_EnumPorts, NULL,
7085 *r->out.info, r->in.level,
7086 *r->out.count);
7087 *r->out.info = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
7088 *r->out.count = SPOOLSS_BUFFER_OK(*r->out.count, 0);
7090 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
7093 /****************************************************************************
7094 ****************************************************************************/
7096 static WERROR spoolss_addprinterex_level_2(pipes_struct *p,
7097 const char *server,
7098 struct spoolss_SetPrinterInfoCtr *info_ctr,
7099 struct spoolss_DeviceMode *devmode,
7100 struct security_descriptor *sec_desc,
7101 struct spoolss_UserLevelCtr *user_ctr,
7102 POLICY_HND *handle)
7104 NT_PRINTER_INFO_LEVEL *printer = NULL;
7105 fstring name;
7106 int snum;
7107 WERROR err = WERR_OK;
7109 if ( !(printer = TALLOC_ZERO_P(NULL, NT_PRINTER_INFO_LEVEL)) ) {
7110 DEBUG(0,("spoolss_addprinterex_level_2: malloc fail.\n"));
7111 return WERR_NOMEM;
7114 /* convert from UNICODE to ASCII - this allocates the info_2 struct inside *printer.*/
7115 if (!convert_printer_info(info_ctr, printer)) {
7116 free_a_printer(&printer, 2);
7117 return WERR_NOMEM;
7120 /* check to see if the printer already exists */
7122 if ((snum = print_queue_snum(printer->info_2->sharename)) != -1) {
7123 DEBUG(5, ("spoolss_addprinterex_level_2: Attempted to add a printer named [%s] when one already existed!\n",
7124 printer->info_2->sharename));
7125 free_a_printer(&printer, 2);
7126 return WERR_PRINTER_ALREADY_EXISTS;
7129 /* FIXME!!! smbd should check to see if the driver is installed before
7130 trying to add a printer like this --jerry */
7132 if (*lp_addprinter_cmd() ) {
7133 if ( !add_printer_hook(p->mem_ctx, p->server_info->ptok,
7134 printer) ) {
7135 free_a_printer(&printer,2);
7136 return WERR_ACCESS_DENIED;
7138 } else {
7139 DEBUG(0,("spoolss_addprinterex_level_2: add printer for printer %s called and no"
7140 "smb.conf parameter \"addprinter command\" is defined. This"
7141 "parameter must exist for this call to succeed\n",
7142 printer->info_2->sharename ));
7145 /* use our primary netbios name since get_a_printer() will convert
7146 it to what the client expects on a case by case basis */
7148 slprintf(name, sizeof(name)-1, "\\\\%s\\%s", global_myname(),
7149 printer->info_2->sharename);
7152 if ((snum = print_queue_snum(printer->info_2->sharename)) == -1) {
7153 free_a_printer(&printer,2);
7154 return WERR_ACCESS_DENIED;
7157 /* you must be a printer admin to add a new printer */
7158 if (!print_access_check(NULL, snum, PRINTER_ACCESS_ADMINISTER)) {
7159 free_a_printer(&printer,2);
7160 return WERR_ACCESS_DENIED;
7164 * Do sanity check on the requested changes for Samba.
7167 if (!check_printer_ok(printer->info_2, snum)) {
7168 free_a_printer(&printer,2);
7169 return WERR_INVALID_PARAM;
7173 * When a printer is created, the drivername bound to the printer is used
7174 * to lookup previously saved driver initialization info, which is then
7175 * bound to the new printer, simulating what happens in the Windows arch.
7178 if (!devmode)
7180 set_driver_init(printer, 2);
7182 else
7184 /* A valid devmode was included, convert and link it
7186 DEBUGADD(10, ("spoolss_addprinterex_level_2: devmode included, converting\n"));
7188 if (!convert_devicemode(printer->info_2->printername, devmode,
7189 &printer->info_2->devmode)) {
7190 return WERR_NOMEM;
7194 /* write the ASCII on disk */
7195 err = mod_a_printer(printer, 2);
7196 if (!W_ERROR_IS_OK(err)) {
7197 free_a_printer(&printer,2);
7198 return err;
7201 if (!open_printer_hnd(p, handle, name, PRINTER_ACCESS_ADMINISTER)) {
7202 /* Handle open failed - remove addition. */
7203 del_a_printer(printer->info_2->sharename);
7204 free_a_printer(&printer,2);
7205 ZERO_STRUCTP(handle);
7206 return WERR_ACCESS_DENIED;
7209 update_c_setprinter(False);
7210 free_a_printer(&printer,2);
7212 return WERR_OK;
7215 /****************************************************************
7216 _spoolss_AddPrinterEx
7217 ****************************************************************/
7219 WERROR _spoolss_AddPrinterEx(pipes_struct *p,
7220 struct spoolss_AddPrinterEx *r)
7222 switch (r->in.info_ctr->level) {
7223 case 1:
7224 /* we don't handle yet */
7225 /* but I know what to do ... */
7226 return WERR_UNKNOWN_LEVEL;
7227 case 2:
7228 return spoolss_addprinterex_level_2(p, r->in.server,
7229 r->in.info_ctr,
7230 r->in.devmode_ctr->devmode,
7231 r->in.secdesc_ctr->sd,
7232 r->in.userlevel_ctr,
7233 r->out.handle);
7234 default:
7235 return WERR_UNKNOWN_LEVEL;
7239 /****************************************************************
7240 _spoolss_AddPrinterDriver
7241 ****************************************************************/
7243 WERROR _spoolss_AddPrinterDriver(pipes_struct *p,
7244 struct spoolss_AddPrinterDriver *r)
7246 uint32_t level = r->in.info_ctr->level;
7247 struct spoolss_AddDriverInfoCtr *info = r->in.info_ctr;
7248 WERROR err = WERR_OK;
7249 NT_PRINTER_DRIVER_INFO_LEVEL driver;
7250 fstring driver_name;
7251 uint32 version;
7252 const char *fn;
7254 switch (p->hdr_req.opnum) {
7255 case NDR_SPOOLSS_ADDPRINTERDRIVER:
7256 fn = "_spoolss_AddPrinterDriver";
7257 break;
7258 case NDR_SPOOLSS_ADDPRINTERDRIVEREX:
7259 fn = "_spoolss_AddPrinterDriverEx";
7260 break;
7261 default:
7262 return WERR_INVALID_PARAM;
7266 /* FIXME */
7267 if (level != 3 && level != 6) {
7268 /* Clever hack from Martin Zielinski <mz@seh.de>
7269 * to allow downgrade from level 8 (Vista).
7271 DEBUG(0,("%s: level %d not yet implemented\n", fn, level));
7272 return WERR_UNKNOWN_LEVEL;
7275 ZERO_STRUCT(driver);
7277 if (!convert_printer_driver_info(info, &driver, level)) {
7278 err = WERR_NOMEM;
7279 goto done;
7282 DEBUG(5,("Cleaning driver's information\n"));
7283 err = clean_up_driver_struct(p, driver, level);
7284 if (!W_ERROR_IS_OK(err))
7285 goto done;
7287 DEBUG(5,("Moving driver to final destination\n"));
7288 if( !W_ERROR_IS_OK(err = move_driver_to_download_area(p, driver, level,
7289 &err)) ) {
7290 goto done;
7293 if (add_a_printer_driver(driver, level)!=0) {
7294 err = WERR_ACCESS_DENIED;
7295 goto done;
7298 switch(level) {
7299 case 3:
7300 fstrcpy(driver_name,
7301 driver.info_3->name ? driver.info_3->name : "");
7302 break;
7303 case 6:
7304 fstrcpy(driver_name,
7305 driver.info_6->name ? driver.info_6->name : "");
7306 break;
7310 * I think this is where he DrvUpgradePrinter() hook would be
7311 * be called in a driver's interface DLL on a Windows NT 4.0/2k
7312 * server. Right now, we just need to send ourselves a message
7313 * to update each printer bound to this driver. --jerry
7316 if (!srv_spoolss_drv_upgrade_printer(driver_name)) {
7317 DEBUG(0,("%s: Failed to send message about upgrading driver [%s]!\n",
7318 fn, driver_name));
7322 * Based on the version (e.g. driver destination dir: 0=9x,2=Nt/2k,3=2k/Xp),
7323 * decide if the driver init data should be deleted. The rules are:
7324 * 1) never delete init data if it is a 9x driver, they don't use it anyway
7325 * 2) delete init data only if there is no 2k/Xp driver
7326 * 3) always delete init data
7327 * The generalized rule is always use init data from the highest order driver.
7328 * It is necessary to follow the driver install by an initialization step to
7329 * finish off this process.
7331 if (level == 3)
7332 version = driver.info_3->cversion;
7333 else if (level == 6)
7334 version = driver.info_6->version;
7335 else
7336 version = -1;
7337 switch (version) {
7339 * 9x printer driver - never delete init data
7341 case 0:
7342 DEBUG(10,("%s: init data not deleted for 9x driver [%s]\n",
7343 fn, driver_name));
7344 break;
7347 * Nt or 2k (compatiblity mode) printer driver - only delete init data if
7348 * there is no 2k/Xp driver init data for this driver name.
7350 case 2:
7352 NT_PRINTER_DRIVER_INFO_LEVEL driver1;
7354 if (!W_ERROR_IS_OK(get_a_printer_driver(&driver1, 3, driver_name, "Windows NT x86", 3))) {
7356 * No 2k/Xp driver found, delete init data (if any) for the new Nt driver.
7358 if (!del_driver_init(driver_name))
7359 DEBUG(6,("%s: del_driver_init(%s) Nt failed!\n",
7360 fn, driver_name));
7361 } else {
7363 * a 2k/Xp driver was found, don't delete init data because Nt driver will use it.
7365 free_a_printer_driver(driver1,3);
7366 DEBUG(10,("%s: init data not deleted for Nt driver [%s]\n",
7367 fn, driver_name));
7370 break;
7373 * 2k or Xp printer driver - always delete init data
7375 case 3:
7376 if (!del_driver_init(driver_name))
7377 DEBUG(6,("%s: del_driver_init(%s) 2k/Xp failed!\n",
7378 fn, driver_name));
7379 break;
7381 default:
7382 DEBUG(0,("%s: invalid level=%d\n", fn, level));
7383 break;
7387 done:
7388 free_a_printer_driver(driver, level);
7389 return err;
7392 /****************************************************************
7393 _spoolss_AddPrinterDriverEx
7394 ****************************************************************/
7396 WERROR _spoolss_AddPrinterDriverEx(pipes_struct *p,
7397 struct spoolss_AddPrinterDriverEx *r)
7399 struct spoolss_AddPrinterDriver a;
7402 * we only support the semantics of AddPrinterDriver()
7403 * i.e. only copy files that are newer than existing ones
7406 if (r->in.flags != APD_COPY_NEW_FILES) {
7407 return WERR_ACCESS_DENIED;
7410 a.in.servername = r->in.servername;
7411 a.in.info_ctr = r->in.info_ctr;
7413 return _spoolss_AddPrinterDriver(p, &a);
7416 /****************************************************************************
7417 ****************************************************************************/
7419 struct _spoolss_paths {
7420 int type;
7421 const char *share;
7422 const char *dir;
7425 enum { SPOOLSS_DRIVER_PATH, SPOOLSS_PRTPROCS_PATH };
7427 static const struct _spoolss_paths spoolss_paths[]= {
7428 { SPOOLSS_DRIVER_PATH, "print$", "DRIVERS" },
7429 { SPOOLSS_PRTPROCS_PATH, "prnproc$", "PRTPROCS" }
7432 static WERROR compose_spoolss_server_path(TALLOC_CTX *mem_ctx,
7433 const char *servername,
7434 const char *environment,
7435 int component,
7436 char **path)
7438 const char *pservername = NULL;
7439 const char *long_archi = SPOOLSS_ARCHITECTURE_NT_X86;
7440 const char *short_archi;
7442 *path = NULL;
7444 /* environment may be empty */
7445 if (environment && strlen(environment)) {
7446 long_archi = environment;
7449 /* servername may be empty */
7450 if (servername && strlen(servername)) {
7451 pservername = canon_servername(servername);
7453 if (!is_myname_or_ipaddr(pservername)) {
7454 return WERR_INVALID_PARAM;
7458 if (!(short_archi = get_short_archi(long_archi))) {
7459 return WERR_INVALID_ENVIRONMENT;
7462 switch (component) {
7463 case SPOOLSS_PRTPROCS_PATH:
7464 case SPOOLSS_DRIVER_PATH:
7465 if (pservername) {
7466 *path = talloc_asprintf(mem_ctx,
7467 "\\\\%s\\%s\\%s",
7468 pservername,
7469 spoolss_paths[component].share,
7470 short_archi);
7471 } else {
7472 *path = talloc_asprintf(mem_ctx, "%s\\%s\\%s",
7473 SPOOLSS_DEFAULT_SERVER_PATH,
7474 spoolss_paths[component].dir,
7475 short_archi);
7477 break;
7478 default:
7479 return WERR_INVALID_PARAM;
7482 if (!*path) {
7483 return WERR_NOMEM;
7486 return WERR_OK;
7489 /****************************************************************************
7490 ****************************************************************************/
7492 static WERROR getprinterdriverdir_level_1(TALLOC_CTX *mem_ctx,
7493 const char *servername,
7494 const char *environment,
7495 struct spoolss_DriverDirectoryInfo1 *r)
7497 WERROR werr;
7498 char *path = NULL;
7500 werr = compose_spoolss_server_path(mem_ctx,
7501 servername,
7502 environment,
7503 SPOOLSS_DRIVER_PATH,
7504 &path);
7505 if (!W_ERROR_IS_OK(werr)) {
7506 return werr;
7509 DEBUG(4,("printer driver directory: [%s]\n", path));
7511 r->directory_name = path;
7513 return WERR_OK;
7516 /****************************************************************
7517 _spoolss_GetPrinterDriverDirectory
7518 ****************************************************************/
7520 WERROR _spoolss_GetPrinterDriverDirectory(pipes_struct *p,
7521 struct spoolss_GetPrinterDriverDirectory *r)
7523 WERROR werror;
7525 /* that's an [in out] buffer */
7527 if (!r->in.buffer && (r->in.offered != 0)) {
7528 return WERR_INVALID_PARAM;
7531 DEBUG(5,("_spoolss_GetPrinterDriverDirectory: level %d\n",
7532 r->in.level));
7534 *r->out.needed = 0;
7536 /* r->in.level is ignored */
7538 werror = getprinterdriverdir_level_1(p->mem_ctx,
7539 r->in.server,
7540 r->in.environment,
7541 &r->out.info->info1);
7542 if (!W_ERROR_IS_OK(werror)) {
7543 TALLOC_FREE(r->out.info);
7544 return werror;
7547 *r->out.needed = SPOOLSS_BUFFER_UNION(spoolss_DriverDirectoryInfo, NULL,
7548 r->out.info, r->in.level);
7549 r->out.info = SPOOLSS_BUFFER_OK(r->out.info, NULL);
7551 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
7554 /****************************************************************
7555 _spoolss_EnumPrinterData
7556 ****************************************************************/
7558 WERROR _spoolss_EnumPrinterData(pipes_struct *p,
7559 struct spoolss_EnumPrinterData *r)
7561 NT_PRINTER_INFO_LEVEL *printer = NULL;
7562 Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
7563 int snum;
7564 WERROR result;
7565 REGISTRY_VALUE *val = NULL;
7566 NT_PRINTER_DATA *p_data;
7567 int i, key_index, num_values;
7568 int name_length;
7570 *r->out.value_needed = 0;
7571 *r->out.type = REG_NONE;
7572 *r->out.data_needed = 0;
7574 DEBUG(5,("_spoolss_EnumPrinterData\n"));
7576 if (!Printer) {
7577 DEBUG(2,("_spoolss_EnumPrinterData: Invalid handle (%s:%u:%u).\n",
7578 OUR_HANDLE(r->in.handle)));
7579 return WERR_BADFID;
7582 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
7583 return WERR_BADFID;
7586 result = get_a_printer(Printer, &printer, 2, lp_const_servicename(snum));
7587 if (!W_ERROR_IS_OK(result)) {
7588 return result;
7591 p_data = printer->info_2->data;
7592 key_index = lookup_printerkey( p_data, SPOOL_PRINTERDATA_KEY );
7594 result = WERR_OK;
7597 * The NT machine wants to know the biggest size of value and data
7599 * cf: MSDN EnumPrinterData remark section
7602 if (!r->in.value_offered && !r->in.data_offered && (key_index != -1)) {
7604 uint32_t biggest_valuesize = 0;
7605 uint32_t biggest_datasize = 0;
7607 DEBUGADD(6,("Activating NT mega-hack to find sizes\n"));
7609 num_values = regval_ctr_numvals( p_data->keys[key_index].values );
7611 for ( i=0; i<num_values; i++ )
7613 val = regval_ctr_specific_value( p_data->keys[key_index].values, i );
7615 name_length = strlen(val->valuename);
7616 if ( strlen(val->valuename) > biggest_valuesize )
7617 biggest_valuesize = name_length;
7619 if ( val->size > biggest_datasize )
7620 biggest_datasize = val->size;
7622 DEBUG(6,("current values: [%d], [%d]\n", biggest_valuesize,
7623 biggest_datasize));
7626 /* the value is an UNICODE string but real_value_size is the length
7627 in bytes including the trailing 0 */
7629 *r->out.value_needed = 2 * (1 + biggest_valuesize);
7630 *r->out.data_needed = biggest_datasize;
7632 DEBUG(6,("final values: [%d], [%d]\n",
7633 *r->out.value_needed, *r->out.data_needed));
7635 goto done;
7639 * the value len is wrong in NT sp3
7640 * that's the number of bytes not the number of unicode chars
7643 if (key_index != -1) {
7644 val = regval_ctr_specific_value(p_data->keys[key_index].values,
7645 r->in.enum_index);
7648 if (!val) {
7650 /* out_value should default to "" or else NT4 has
7651 problems unmarshalling the response */
7653 if (r->in.value_offered) {
7654 *r->out.value_needed = 1;
7655 r->out.value_name = talloc_strdup(r, "");
7656 if (!r->out.value_name) {
7657 result = WERR_NOMEM;
7658 goto done;
7660 } else {
7661 r->out.value_name = NULL;
7662 *r->out.value_needed = 0;
7665 /* the data is counted in bytes */
7667 *r->out.data_needed = r->in.data_offered;
7669 result = WERR_NO_MORE_ITEMS;
7670 } else {
7672 * the value is:
7673 * - counted in bytes in the request
7674 * - counted in UNICODE chars in the max reply
7675 * - counted in bytes in the real size
7677 * take a pause *before* coding not *during* coding
7680 /* name */
7681 if (r->in.value_offered) {
7682 r->out.value_name = talloc_strdup(r, regval_name(val));
7683 if (!r->out.value_name) {
7684 result = WERR_NOMEM;
7685 goto done;
7687 *r->out.value_needed = strlen_m(regval_name(val));
7688 } else {
7689 r->out.value_name = NULL;
7690 *r->out.value_needed = 0;
7693 /* type */
7695 *r->out.type = regval_type(val);
7697 /* data - counted in bytes */
7699 if (r->out.data && regval_size(val)) {
7700 memcpy(r->out.data, regval_data_p(val), regval_size(val));
7703 *r->out.data_needed = regval_size(val);
7706 done:
7707 free_a_printer(&printer, 2);
7708 return result;
7711 /****************************************************************
7712 _spoolss_SetPrinterData
7713 ****************************************************************/
7715 WERROR _spoolss_SetPrinterData(pipes_struct *p,
7716 struct spoolss_SetPrinterData *r)
7718 NT_PRINTER_INFO_LEVEL *printer = NULL;
7719 int snum=0;
7720 WERROR result = WERR_OK;
7721 Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
7722 DATA_BLOB blob;
7724 DEBUG(5,("_spoolss_SetPrinterData\n"));
7726 if (!Printer) {
7727 DEBUG(2,("_spoolss_SetPrinterData: Invalid handle (%s:%u:%u).\n",
7728 OUR_HANDLE(r->in.handle)));
7729 return WERR_BADFID;
7732 if (Printer->printer_type == SPLHND_SERVER) {
7733 DEBUG(10,("_spoolss_SetPrinterData: "
7734 "Not implemented for server handles yet\n"));
7735 return WERR_INVALID_PARAM;
7738 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
7739 return WERR_BADFID;
7743 * Access check : NT returns "access denied" if you make a
7744 * SetPrinterData call without the necessary privildge.
7745 * we were originally returning OK if nothing changed
7746 * which made Win2k issue **a lot** of SetPrinterData
7747 * when connecting to a printer --jerry
7750 if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
7751 DEBUG(3,("_spoolss_SetPrinterData: "
7752 "change denied by handle access permissions\n"));
7753 result = WERR_ACCESS_DENIED;
7754 goto done;
7757 result = get_a_printer(Printer, &printer, 2, lp_const_servicename(snum));
7758 if (!W_ERROR_IS_OK(result)) {
7759 return result;
7762 result = push_spoolss_PrinterData(p->mem_ctx, &blob,
7763 r->in.type, &r->in.data);
7764 if (!W_ERROR_IS_OK(result)) {
7765 goto done;
7769 * When client side code sets a magic printer data key, detect it and save
7770 * the current printer data and the magic key's data (its the DEVMODE) for
7771 * future printer/driver initializations.
7773 if ((r->in.type == REG_BINARY) && strequal(r->in.value_name, PHANTOM_DEVMODE_KEY)) {
7774 /* Set devmode and printer initialization info */
7775 result = save_driver_init(printer, 2, blob.data, blob.length);
7777 srv_spoolss_reset_printerdata(printer->info_2->drivername);
7779 goto done;
7782 result = set_printer_dataex(printer, SPOOL_PRINTERDATA_KEY,
7783 r->in.value_name, r->in.type,
7784 blob.data, blob.length);
7785 if (W_ERROR_IS_OK(result)) {
7786 result = mod_a_printer(printer, 2);
7789 done:
7790 free_a_printer(&printer, 2);
7792 return result;
7795 /****************************************************************
7796 _spoolss_ResetPrinter
7797 ****************************************************************/
7799 WERROR _spoolss_ResetPrinter(pipes_struct *p,
7800 struct spoolss_ResetPrinter *r)
7802 POLICY_HND *handle = r->in.handle;
7803 Printer_entry *Printer=find_printer_index_by_hnd(p, handle);
7804 int snum;
7806 DEBUG(5,("_spoolss_ResetPrinter\n"));
7809 * All we do is to check to see if the handle and queue is valid.
7810 * This call really doesn't mean anything to us because we only
7811 * support RAW printing. --jerry
7814 if (!Printer) {
7815 DEBUG(2,("_spoolss_ResetPrinter: Invalid handle (%s:%u:%u).\n",
7816 OUR_HANDLE(handle)));
7817 return WERR_BADFID;
7820 if (!get_printer_snum(p,handle, &snum, NULL))
7821 return WERR_BADFID;
7824 /* blindly return success */
7825 return WERR_OK;
7828 /****************************************************************
7829 _spoolss_DeletePrinterData
7830 ****************************************************************/
7832 WERROR _spoolss_DeletePrinterData(pipes_struct *p,
7833 struct spoolss_DeletePrinterData *r)
7835 POLICY_HND *handle = r->in.handle;
7836 NT_PRINTER_INFO_LEVEL *printer = NULL;
7837 int snum=0;
7838 WERROR status = WERR_OK;
7839 Printer_entry *Printer=find_printer_index_by_hnd(p, handle);
7841 DEBUG(5,("_spoolss_DeletePrinterData\n"));
7843 if (!Printer) {
7844 DEBUG(2,("_spoolss_DeletePrinterData: Invalid handle (%s:%u:%u).\n",
7845 OUR_HANDLE(handle)));
7846 return WERR_BADFID;
7849 if (!get_printer_snum(p, handle, &snum, NULL))
7850 return WERR_BADFID;
7852 if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
7853 DEBUG(3, ("_spoolss_DeletePrinterData: "
7854 "printer properties change denied by handle\n"));
7855 return WERR_ACCESS_DENIED;
7858 status = get_a_printer(Printer, &printer, 2, lp_const_servicename(snum));
7859 if (!W_ERROR_IS_OK(status))
7860 return status;
7862 if (!r->in.value_name) {
7863 free_a_printer(&printer, 2);
7864 return WERR_NOMEM;
7867 status = delete_printer_dataex( printer, SPOOL_PRINTERDATA_KEY,
7868 r->in.value_name );
7870 if ( W_ERROR_IS_OK(status) )
7871 mod_a_printer( printer, 2 );
7873 free_a_printer(&printer, 2);
7875 return status;
7878 /****************************************************************
7879 _spoolss_AddForm
7880 ****************************************************************/
7882 WERROR _spoolss_AddForm(pipes_struct *p,
7883 struct spoolss_AddForm *r)
7885 POLICY_HND *handle = r->in.handle;
7886 struct spoolss_AddFormInfo1 *form = r->in.info.info1;
7887 nt_forms_struct tmpForm;
7888 int snum;
7889 WERROR status = WERR_OK;
7890 NT_PRINTER_INFO_LEVEL *printer = NULL;
7892 int count=0;
7893 nt_forms_struct *list=NULL;
7894 Printer_entry *Printer = find_printer_index_by_hnd(p, handle);
7896 DEBUG(5,("_spoolss_AddForm\n"));
7898 if (!Printer) {
7899 DEBUG(2,("_spoolss_AddForm: Invalid handle (%s:%u:%u).\n",
7900 OUR_HANDLE(handle)));
7901 return WERR_BADFID;
7905 /* forms can be added on printer of on the print server handle */
7907 if ( Printer->printer_type == SPLHND_PRINTER )
7909 if (!get_printer_snum(p,handle, &snum, NULL))
7910 return WERR_BADFID;
7912 status = get_a_printer(Printer, &printer, 2, lp_const_servicename(snum));
7913 if (!W_ERROR_IS_OK(status))
7914 goto done;
7917 if ( !(Printer->access_granted & (PRINTER_ACCESS_ADMINISTER|SERVER_ACCESS_ADMINISTER)) ) {
7918 DEBUG(2,("_spoolss_addform: denied by handle permissions.\n"));
7919 status = WERR_ACCESS_DENIED;
7920 goto done;
7923 /* can't add if builtin */
7925 if (get_a_builtin_ntform_by_string(form->form_name, &tmpForm)) {
7926 status = WERR_FILE_EXISTS;
7927 goto done;
7930 count = get_ntforms(&list);
7932 if(!add_a_form(&list, form, &count)) {
7933 status = WERR_NOMEM;
7934 goto done;
7937 write_ntforms(&list, count);
7940 * ChangeID must always be set if this is a printer
7943 if ( Printer->printer_type == SPLHND_PRINTER )
7944 status = mod_a_printer(printer, 2);
7946 done:
7947 if ( printer )
7948 free_a_printer(&printer, 2);
7949 SAFE_FREE(list);
7951 return status;
7954 /****************************************************************
7955 _spoolss_DeleteForm
7956 ****************************************************************/
7958 WERROR _spoolss_DeleteForm(pipes_struct *p,
7959 struct spoolss_DeleteForm *r)
7961 POLICY_HND *handle = r->in.handle;
7962 const char *form_name = r->in.form_name;
7963 nt_forms_struct tmpForm;
7964 int count=0;
7965 nt_forms_struct *list=NULL;
7966 Printer_entry *Printer = find_printer_index_by_hnd(p, handle);
7967 int snum;
7968 WERROR status = WERR_OK;
7969 NT_PRINTER_INFO_LEVEL *printer = NULL;
7971 DEBUG(5,("_spoolss_DeleteForm\n"));
7973 if (!Printer) {
7974 DEBUG(2,("_spoolss_DeleteForm: Invalid handle (%s:%u:%u).\n",
7975 OUR_HANDLE(handle)));
7976 return WERR_BADFID;
7979 /* forms can be deleted on printer of on the print server handle */
7981 if ( Printer->printer_type == SPLHND_PRINTER )
7983 if (!get_printer_snum(p,handle, &snum, NULL))
7984 return WERR_BADFID;
7986 status = get_a_printer(Printer, &printer, 2, lp_const_servicename(snum));
7987 if (!W_ERROR_IS_OK(status))
7988 goto done;
7991 if ( !(Printer->access_granted & (PRINTER_ACCESS_ADMINISTER|SERVER_ACCESS_ADMINISTER)) ) {
7992 DEBUG(2,("_spoolss_DeleteForm: denied by handle permissions.\n"));
7993 status = WERR_ACCESS_DENIED;
7994 goto done;
7997 /* can't delete if builtin */
7999 if (get_a_builtin_ntform_by_string(form_name,&tmpForm)) {
8000 status = WERR_INVALID_PARAM;
8001 goto done;
8004 count = get_ntforms(&list);
8006 if ( !delete_a_form(&list, form_name, &count, &status ))
8007 goto done;
8010 * ChangeID must always be set if this is a printer
8013 if ( Printer->printer_type == SPLHND_PRINTER )
8014 status = mod_a_printer(printer, 2);
8016 done:
8017 if ( printer )
8018 free_a_printer(&printer, 2);
8019 SAFE_FREE(list);
8021 return status;
8024 /****************************************************************
8025 _spoolss_SetForm
8026 ****************************************************************/
8028 WERROR _spoolss_SetForm(pipes_struct *p,
8029 struct spoolss_SetForm *r)
8031 POLICY_HND *handle = r->in.handle;
8032 struct spoolss_AddFormInfo1 *form = r->in.info.info1;
8033 nt_forms_struct tmpForm;
8034 int snum;
8035 WERROR status = WERR_OK;
8036 NT_PRINTER_INFO_LEVEL *printer = NULL;
8038 int count=0;
8039 nt_forms_struct *list=NULL;
8040 Printer_entry *Printer = find_printer_index_by_hnd(p, handle);
8042 DEBUG(5,("_spoolss_SetForm\n"));
8044 if (!Printer) {
8045 DEBUG(2,("_spoolss_SetForm: Invalid handle (%s:%u:%u).\n",
8046 OUR_HANDLE(handle)));
8047 return WERR_BADFID;
8050 /* forms can be modified on printer of on the print server handle */
8052 if ( Printer->printer_type == SPLHND_PRINTER )
8054 if (!get_printer_snum(p,handle, &snum, NULL))
8055 return WERR_BADFID;
8057 status = get_a_printer(Printer, &printer, 2, lp_const_servicename(snum));
8058 if (!W_ERROR_IS_OK(status))
8059 goto done;
8062 if ( !(Printer->access_granted & (PRINTER_ACCESS_ADMINISTER|SERVER_ACCESS_ADMINISTER)) ) {
8063 DEBUG(2,("_spoolss_SetForm: denied by handle permissions\n"));
8064 status = WERR_ACCESS_DENIED;
8065 goto done;
8068 /* can't set if builtin */
8069 if (get_a_builtin_ntform_by_string(form->form_name, &tmpForm)) {
8070 status = WERR_INVALID_PARAM;
8071 goto done;
8074 count = get_ntforms(&list);
8075 update_a_form(&list, form, count);
8076 write_ntforms(&list, count);
8079 * ChangeID must always be set if this is a printer
8082 if ( Printer->printer_type == SPLHND_PRINTER )
8083 status = mod_a_printer(printer, 2);
8086 done:
8087 if ( printer )
8088 free_a_printer(&printer, 2);
8089 SAFE_FREE(list);
8091 return status;
8094 /****************************************************************************
8095 fill_print_processor1
8096 ****************************************************************************/
8098 static WERROR fill_print_processor1(TALLOC_CTX *mem_ctx,
8099 struct spoolss_PrintProcessorInfo1 *r,
8100 const char *print_processor_name)
8102 r->print_processor_name = talloc_strdup(mem_ctx, print_processor_name);
8103 W_ERROR_HAVE_NO_MEMORY(r->print_processor_name);
8105 return WERR_OK;
8108 /****************************************************************************
8109 enumprintprocessors level 1.
8110 ****************************************************************************/
8112 static WERROR enumprintprocessors_level_1(TALLOC_CTX *mem_ctx,
8113 union spoolss_PrintProcessorInfo **info_p,
8114 uint32_t *count)
8116 union spoolss_PrintProcessorInfo *info;
8117 WERROR result;
8119 info = TALLOC_ARRAY(mem_ctx, union spoolss_PrintProcessorInfo, 1);
8120 W_ERROR_HAVE_NO_MEMORY(info);
8122 *count = 1;
8124 result = fill_print_processor1(info, &info[0].info1, "winprint");
8125 if (!W_ERROR_IS_OK(result)) {
8126 goto out;
8129 out:
8130 if (!W_ERROR_IS_OK(result)) {
8131 TALLOC_FREE(info);
8132 *count = 0;
8133 return result;
8136 *info_p = info;
8138 return WERR_OK;
8141 /****************************************************************
8142 _spoolss_EnumPrintProcessors
8143 ****************************************************************/
8145 WERROR _spoolss_EnumPrintProcessors(pipes_struct *p,
8146 struct spoolss_EnumPrintProcessors *r)
8148 WERROR result;
8150 /* that's an [in out] buffer */
8152 if (!r->in.buffer && (r->in.offered != 0)) {
8153 return WERR_INVALID_PARAM;
8156 DEBUG(5,("_spoolss_EnumPrintProcessors\n"));
8159 * Enumerate the print processors ...
8161 * Just reply with "winprint", to keep NT happy
8162 * and I can use my nice printer checker.
8165 *r->out.count = 0;
8166 *r->out.needed = 0;
8167 *r->out.info = NULL;
8169 switch (r->in.level) {
8170 case 1:
8171 result = enumprintprocessors_level_1(p->mem_ctx, r->out.info,
8172 r->out.count);
8173 break;
8174 default:
8175 return WERR_UNKNOWN_LEVEL;
8178 if (!W_ERROR_IS_OK(result)) {
8179 return result;
8182 *r->out.needed = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
8183 spoolss_EnumPrintProcessors, NULL,
8184 *r->out.info, r->in.level,
8185 *r->out.count);
8186 *r->out.info = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
8187 *r->out.count = SPOOLSS_BUFFER_OK(*r->out.count, 0);
8189 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
8192 /****************************************************************************
8193 fill_printprocdatatype1
8194 ****************************************************************************/
8196 static WERROR fill_printprocdatatype1(TALLOC_CTX *mem_ctx,
8197 struct spoolss_PrintProcDataTypesInfo1 *r,
8198 const char *name_array)
8200 r->name_array = talloc_strdup(mem_ctx, name_array);
8201 W_ERROR_HAVE_NO_MEMORY(r->name_array);
8203 return WERR_OK;
8206 /****************************************************************************
8207 enumprintprocdatatypes level 1.
8208 ****************************************************************************/
8210 static WERROR enumprintprocdatatypes_level_1(TALLOC_CTX *mem_ctx,
8211 union spoolss_PrintProcDataTypesInfo **info_p,
8212 uint32_t *count)
8214 WERROR result;
8215 union spoolss_PrintProcDataTypesInfo *info;
8217 info = TALLOC_ARRAY(mem_ctx, union spoolss_PrintProcDataTypesInfo, 1);
8218 W_ERROR_HAVE_NO_MEMORY(info);
8220 *count = 1;
8222 result = fill_printprocdatatype1(info, &info[0].info1, "RAW");
8223 if (!W_ERROR_IS_OK(result)) {
8224 goto out;
8227 out:
8228 if (!W_ERROR_IS_OK(result)) {
8229 TALLOC_FREE(info);
8230 *count = 0;
8231 return result;
8234 *info_p = info;
8236 return WERR_OK;
8239 /****************************************************************
8240 _spoolss_EnumPrintProcDataTypes
8241 ****************************************************************/
8243 WERROR _spoolss_EnumPrintProcDataTypes(pipes_struct *p,
8244 struct spoolss_EnumPrintProcDataTypes *r)
8246 WERROR result;
8248 /* that's an [in out] buffer */
8250 if (!r->in.buffer && (r->in.offered != 0)) {
8251 return WERR_INVALID_PARAM;
8254 DEBUG(5,("_spoolss_EnumPrintProcDataTypes\n"));
8256 *r->out.count = 0;
8257 *r->out.needed = 0;
8258 *r->out.info = NULL;
8260 switch (r->in.level) {
8261 case 1:
8262 result = enumprintprocdatatypes_level_1(p->mem_ctx, r->out.info,
8263 r->out.count);
8264 break;
8265 default:
8266 return WERR_UNKNOWN_LEVEL;
8269 *r->out.needed = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
8270 spoolss_EnumPrintProcDataTypes, NULL,
8271 *r->out.info, r->in.level,
8272 *r->out.count);
8273 *r->out.info = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
8274 *r->out.count = SPOOLSS_BUFFER_OK(*r->out.count, 0);
8276 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
8279 /****************************************************************************
8280 fill_monitor_1
8281 ****************************************************************************/
8283 static WERROR fill_monitor_1(TALLOC_CTX *mem_ctx,
8284 struct spoolss_MonitorInfo1 *r,
8285 const char *monitor_name)
8287 r->monitor_name = talloc_strdup(mem_ctx, monitor_name);
8288 W_ERROR_HAVE_NO_MEMORY(r->monitor_name);
8290 return WERR_OK;
8293 /****************************************************************************
8294 fill_monitor_2
8295 ****************************************************************************/
8297 static WERROR fill_monitor_2(TALLOC_CTX *mem_ctx,
8298 struct spoolss_MonitorInfo2 *r,
8299 const char *monitor_name,
8300 const char *environment,
8301 const char *dll_name)
8303 r->monitor_name = talloc_strdup(mem_ctx, monitor_name);
8304 W_ERROR_HAVE_NO_MEMORY(r->monitor_name);
8305 r->environment = talloc_strdup(mem_ctx, environment);
8306 W_ERROR_HAVE_NO_MEMORY(r->environment);
8307 r->dll_name = talloc_strdup(mem_ctx, dll_name);
8308 W_ERROR_HAVE_NO_MEMORY(r->dll_name);
8310 return WERR_OK;
8313 /****************************************************************************
8314 enumprintmonitors level 1.
8315 ****************************************************************************/
8317 static WERROR enumprintmonitors_level_1(TALLOC_CTX *mem_ctx,
8318 union spoolss_MonitorInfo **info_p,
8319 uint32_t *count)
8321 union spoolss_MonitorInfo *info;
8322 WERROR result = WERR_OK;
8324 info = TALLOC_ARRAY(mem_ctx, union spoolss_MonitorInfo, 2);
8325 W_ERROR_HAVE_NO_MEMORY(info);
8327 *count = 2;
8329 result = fill_monitor_1(info, &info[0].info1,
8330 SPL_LOCAL_PORT);
8331 if (!W_ERROR_IS_OK(result)) {
8332 goto out;
8335 result = fill_monitor_1(info, &info[1].info1,
8336 SPL_TCPIP_PORT);
8337 if (!W_ERROR_IS_OK(result)) {
8338 goto out;
8341 out:
8342 if (!W_ERROR_IS_OK(result)) {
8343 TALLOC_FREE(info);
8344 *count = 0;
8345 return result;
8348 *info_p = info;
8350 return WERR_OK;
8353 /****************************************************************************
8354 enumprintmonitors level 2.
8355 ****************************************************************************/
8357 static WERROR enumprintmonitors_level_2(TALLOC_CTX *mem_ctx,
8358 union spoolss_MonitorInfo **info_p,
8359 uint32_t *count)
8361 union spoolss_MonitorInfo *info;
8362 WERROR result = WERR_OK;
8364 info = TALLOC_ARRAY(mem_ctx, union spoolss_MonitorInfo, 2);
8365 W_ERROR_HAVE_NO_MEMORY(info);
8367 *count = 2;
8369 result = fill_monitor_2(info, &info[0].info2,
8370 SPL_LOCAL_PORT,
8371 "Windows NT X86", /* FIXME */
8372 "localmon.dll");
8373 if (!W_ERROR_IS_OK(result)) {
8374 goto out;
8377 result = fill_monitor_2(info, &info[1].info2,
8378 SPL_TCPIP_PORT,
8379 "Windows NT X86", /* FIXME */
8380 "tcpmon.dll");
8381 if (!W_ERROR_IS_OK(result)) {
8382 goto out;
8385 out:
8386 if (!W_ERROR_IS_OK(result)) {
8387 TALLOC_FREE(info);
8388 *count = 0;
8389 return result;
8392 *info_p = info;
8394 return WERR_OK;
8397 /****************************************************************
8398 _spoolss_EnumMonitors
8399 ****************************************************************/
8401 WERROR _spoolss_EnumMonitors(pipes_struct *p,
8402 struct spoolss_EnumMonitors *r)
8404 WERROR result;
8406 /* that's an [in out] buffer */
8408 if (!r->in.buffer && (r->in.offered != 0)) {
8409 return WERR_INVALID_PARAM;
8412 DEBUG(5,("_spoolss_EnumMonitors\n"));
8415 * Enumerate the print monitors ...
8417 * Just reply with "Local Port", to keep NT happy
8418 * and I can use my nice printer checker.
8421 *r->out.count = 0;
8422 *r->out.needed = 0;
8423 *r->out.info = NULL;
8425 switch (r->in.level) {
8426 case 1:
8427 result = enumprintmonitors_level_1(p->mem_ctx, r->out.info,
8428 r->out.count);
8429 break;
8430 case 2:
8431 result = enumprintmonitors_level_2(p->mem_ctx, r->out.info,
8432 r->out.count);
8433 break;
8434 default:
8435 return WERR_UNKNOWN_LEVEL;
8438 if (!W_ERROR_IS_OK(result)) {
8439 return result;
8442 *r->out.needed = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
8443 spoolss_EnumMonitors, NULL,
8444 *r->out.info, r->in.level,
8445 *r->out.count);
8446 *r->out.info = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
8447 *r->out.count = SPOOLSS_BUFFER_OK(*r->out.count, 0);
8449 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
8452 /****************************************************************************
8453 ****************************************************************************/
8455 static WERROR getjob_level_1(TALLOC_CTX *mem_ctx,
8456 const print_queue_struct *queue,
8457 int count, int snum,
8458 const NT_PRINTER_INFO_LEVEL *ntprinter,
8459 uint32_t jobid,
8460 struct spoolss_JobInfo1 *r)
8462 int i = 0;
8463 bool found = false;
8465 for (i=0; i<count && found == false; i++) {
8466 if (queue[i].job == (int)jobid) {
8467 found = true;
8471 if (found == false) {
8472 /* NT treats not found as bad param... yet another bad choice */
8473 return WERR_INVALID_PARAM;
8476 return fill_job_info1(mem_ctx,
8478 &queue[i-1],
8480 snum,
8481 ntprinter);
8484 /****************************************************************************
8485 ****************************************************************************/
8487 static WERROR getjob_level_2(TALLOC_CTX *mem_ctx,
8488 const print_queue_struct *queue,
8489 int count, int snum,
8490 const NT_PRINTER_INFO_LEVEL *ntprinter,
8491 uint32_t jobid,
8492 struct spoolss_JobInfo2 *r)
8494 int i = 0;
8495 bool found = false;
8496 struct spoolss_DeviceMode *devmode;
8497 NT_DEVICEMODE *nt_devmode;
8498 WERROR result;
8500 for (i=0; i<count && found == false; i++) {
8501 if (queue[i].job == (int)jobid) {
8502 found = true;
8506 if (found == false) {
8507 /* NT treats not found as bad param... yet another bad
8508 choice */
8509 return WERR_INVALID_PARAM;
8513 * if the print job does not have a DEVMODE associated with it,
8514 * just use the one for the printer. A NULL devicemode is not
8515 * a failure condition
8518 nt_devmode = print_job_devmode(lp_const_servicename(snum), jobid);
8519 if (nt_devmode) {
8520 devmode = TALLOC_ZERO_P(mem_ctx, struct spoolss_DeviceMode);
8521 W_ERROR_HAVE_NO_MEMORY(devmode);
8522 result = convert_nt_devicemode(devmode, devmode, nt_devmode);
8523 if (!W_ERROR_IS_OK(result)) {
8524 return result;
8526 } else {
8527 devmode = construct_dev_mode(mem_ctx, lp_const_servicename(snum));
8528 W_ERROR_HAVE_NO_MEMORY(devmode);
8531 return fill_job_info2(mem_ctx,
8533 &queue[i-1],
8535 snum,
8536 ntprinter,
8537 devmode);
8540 /****************************************************************
8541 _spoolss_GetJob
8542 ****************************************************************/
8544 WERROR _spoolss_GetJob(pipes_struct *p,
8545 struct spoolss_GetJob *r)
8547 WERROR result = WERR_OK;
8548 NT_PRINTER_INFO_LEVEL *ntprinter = NULL;
8549 int snum;
8550 int count;
8551 print_queue_struct *queue = NULL;
8552 print_status_struct prt_status;
8554 /* that's an [in out] buffer */
8556 if (!r->in.buffer && (r->in.offered != 0)) {
8557 return WERR_INVALID_PARAM;
8560 DEBUG(5,("_spoolss_GetJob\n"));
8562 *r->out.needed = 0;
8564 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
8565 return WERR_BADFID;
8568 result = get_a_printer(NULL, &ntprinter, 2, lp_servicename(snum));
8569 if (!W_ERROR_IS_OK(result)) {
8570 return result;
8573 count = print_queue_status(snum, &queue, &prt_status);
8575 DEBUGADD(4,("count:[%d], prt_status:[%d], [%s]\n",
8576 count, prt_status.status, prt_status.message));
8578 switch (r->in.level) {
8579 case 1:
8580 result = getjob_level_1(p->mem_ctx,
8581 queue, count, snum, ntprinter,
8582 r->in.job_id, &r->out.info->info1);
8583 break;
8584 case 2:
8585 result = getjob_level_2(p->mem_ctx,
8586 queue, count, snum, ntprinter,
8587 r->in.job_id, &r->out.info->info2);
8588 break;
8589 default:
8590 result = WERR_UNKNOWN_LEVEL;
8591 break;
8594 SAFE_FREE(queue);
8595 free_a_printer(&ntprinter, 2);
8597 if (!W_ERROR_IS_OK(result)) {
8598 TALLOC_FREE(r->out.info);
8599 return result;
8602 *r->out.needed = SPOOLSS_BUFFER_UNION(spoolss_JobInfo, NULL,
8603 r->out.info, r->in.level);
8604 r->out.info = SPOOLSS_BUFFER_OK(r->out.info, NULL);
8606 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
8609 /****************************************************************
8610 _spoolss_GetPrinterDataEx
8612 From MSDN documentation of GetPrinterDataEx: pass request
8613 to GetPrinterData if key is "PrinterDriverData".
8614 ****************************************************************/
8616 WERROR _spoolss_GetPrinterDataEx(pipes_struct *p,
8617 struct spoolss_GetPrinterDataEx *r)
8620 Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
8621 REGISTRY_VALUE *val = NULL;
8622 NT_PRINTER_INFO_LEVEL *printer = NULL;
8623 int snum = 0;
8624 WERROR result = WERR_OK;
8626 DEBUG(4,("_spoolss_GetPrinterDataEx\n"));
8628 DEBUG(10, ("_spoolss_GetPrinterDataEx: key => [%s], value => [%s]\n",
8629 r->in.key_name, r->in.value_name));
8631 /* in case of problem, return some default values */
8633 *r->out.needed = 0;
8634 *r->out.type = REG_NONE;
8636 if (!Printer) {
8637 DEBUG(2,("_spoolss_GetPrinterDataEx: Invalid handle (%s:%u:%u).\n",
8638 OUR_HANDLE(r->in.handle)));
8639 result = WERR_BADFID;
8640 goto done;
8643 /* Is the handle to a printer or to the server? */
8645 if (Printer->printer_type == SPLHND_SERVER) {
8646 DEBUG(10,("_spoolss_GetPrinterDataEx: "
8647 "Not implemented for server handles yet\n"));
8648 result = WERR_INVALID_PARAM;
8649 goto done;
8652 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
8653 return WERR_BADFID;
8656 result = get_a_printer(Printer, &printer, 2, lp_servicename(snum));
8657 if (!W_ERROR_IS_OK(result)) {
8658 goto done;
8661 /* check to see if the keyname is valid */
8662 if (!strlen(r->in.key_name)) {
8663 result = WERR_INVALID_PARAM;
8664 goto done;
8667 if (lookup_printerkey(printer->info_2->data, r->in.key_name) == -1) {
8668 DEBUG(4,("_spoolss_GetPrinterDataEx: "
8669 "Invalid keyname [%s]\n", r->in.key_name ));
8670 result = WERR_BADFILE;
8671 goto done;
8674 /* When given a new keyname, we should just create it */
8676 val = get_printer_data(printer->info_2,
8677 r->in.key_name, r->in.value_name);
8678 if (!val) {
8679 result = WERR_BADFILE;
8680 goto done;
8683 *r->out.needed = regval_size(val);
8685 if (*r->out.needed > r->in.offered) {
8686 result = WERR_MORE_DATA;
8687 goto done;
8690 *r->out.type = regval_type(val);
8692 memcpy(r->out.buffer, regval_data_p(val), regval_size(val));
8694 done:
8695 if (printer) {
8696 free_a_printer(&printer, 2);
8699 return result;
8702 /****************************************************************
8703 _spoolss_SetPrinterDataEx
8704 ****************************************************************/
8706 WERROR _spoolss_SetPrinterDataEx(pipes_struct *p,
8707 struct spoolss_SetPrinterDataEx *r)
8709 NT_PRINTER_INFO_LEVEL *printer = NULL;
8710 int snum = 0;
8711 WERROR result = WERR_OK;
8712 Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
8713 char *oid_string;
8715 DEBUG(4,("_spoolss_SetPrinterDataEx\n"));
8717 /* From MSDN documentation of SetPrinterDataEx: pass request to
8718 SetPrinterData if key is "PrinterDriverData" */
8720 if (!Printer) {
8721 DEBUG(2,("_spoolss_SetPrinterDataEx: Invalid handle (%s:%u:%u).\n",
8722 OUR_HANDLE(r->in.handle)));
8723 return WERR_BADFID;
8726 if (Printer->printer_type == SPLHND_SERVER) {
8727 DEBUG(10,("_spoolss_SetPrinterDataEx: "
8728 "Not implemented for server handles yet\n"));
8729 return WERR_INVALID_PARAM;
8732 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
8733 return WERR_BADFID;
8737 * Access check : NT returns "access denied" if you make a
8738 * SetPrinterData call without the necessary privildge.
8739 * we were originally returning OK if nothing changed
8740 * which made Win2k issue **a lot** of SetPrinterData
8741 * when connecting to a printer --jerry
8744 if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
8745 DEBUG(3, ("_spoolss_SetPrinterDataEx: "
8746 "change denied by handle access permissions\n"));
8747 return WERR_ACCESS_DENIED;
8750 result = get_a_printer(Printer, &printer, 2, lp_servicename(snum));
8751 if (!W_ERROR_IS_OK(result)) {
8752 return result;
8755 /* check for OID in valuename */
8757 oid_string = strchr(r->in.value_name, ',');
8758 if (oid_string) {
8759 *oid_string = '\0';
8760 oid_string++;
8763 /* save the registry data */
8765 result = set_printer_dataex(printer, r->in.key_name, r->in.value_name,
8766 r->in.type, r->in.buffer, r->in.offered);
8768 if (W_ERROR_IS_OK(result)) {
8769 /* save the OID if one was specified */
8770 if (oid_string) {
8771 char *str = talloc_asprintf(p->mem_ctx, "%s\\%s",
8772 r->in.key_name, SPOOL_OID_KEY);
8773 if (!str) {
8774 result = WERR_NOMEM;
8775 goto done;
8779 * I'm not checking the status here on purpose. Don't know
8780 * if this is right, but I'm returning the status from the
8781 * previous set_printer_dataex() call. I have no idea if
8782 * this is right. --jerry
8785 set_printer_dataex(printer, str, r->in.value_name,
8786 REG_SZ, (uint8_t *)oid_string,
8787 strlen(oid_string)+1);
8790 result = mod_a_printer(printer, 2);
8793 done:
8794 free_a_printer(&printer, 2);
8796 return result;
8799 /****************************************************************
8800 _spoolss_DeletePrinterDataEx
8801 ****************************************************************/
8803 WERROR _spoolss_DeletePrinterDataEx(pipes_struct *p,
8804 struct spoolss_DeletePrinterDataEx *r)
8806 POLICY_HND *handle = r->in.handle;
8807 NT_PRINTER_INFO_LEVEL *printer = NULL;
8808 int snum=0;
8809 WERROR status = WERR_OK;
8810 Printer_entry *Printer=find_printer_index_by_hnd(p, handle);
8812 DEBUG(5,("_spoolss_DeletePrinterDataEx\n"));
8814 if (!Printer) {
8815 DEBUG(2,("_spoolss_DeletePrinterDataEx: "
8816 "Invalid handle (%s:%u:%u).\n", OUR_HANDLE(handle)));
8817 return WERR_BADFID;
8820 if (!get_printer_snum(p, handle, &snum, NULL))
8821 return WERR_BADFID;
8823 if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
8824 DEBUG(3, ("_spoolss_DeletePrinterDataEx: "
8825 "printer properties change denied by handle\n"));
8826 return WERR_ACCESS_DENIED;
8829 if (!r->in.value_name || !r->in.key_name) {
8830 return WERR_NOMEM;
8833 status = get_a_printer(Printer, &printer, 2, lp_const_servicename(snum));
8834 if (!W_ERROR_IS_OK(status))
8835 return status;
8837 status = delete_printer_dataex( printer, r->in.key_name, r->in.value_name );
8839 if ( W_ERROR_IS_OK(status) )
8840 mod_a_printer( printer, 2 );
8842 free_a_printer(&printer, 2);
8844 return status;
8847 /****************************************************************
8848 _spoolss_EnumPrinterKey
8849 ****************************************************************/
8851 WERROR _spoolss_EnumPrinterKey(pipes_struct *p,
8852 struct spoolss_EnumPrinterKey *r)
8854 fstring *keynames = NULL;
8855 int num_keys;
8856 Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
8857 NT_PRINTER_DATA *data;
8858 NT_PRINTER_INFO_LEVEL *printer = NULL;
8859 int snum = 0;
8860 WERROR result = WERR_BADFILE;
8861 int i;
8862 const char **array = NULL;
8865 DEBUG(4,("_spoolss_EnumPrinterKey\n"));
8867 if (!Printer) {
8868 DEBUG(2,("_spoolss_EnumPrinterKey: Invalid handle (%s:%u:%u).\n",
8869 OUR_HANDLE(r->in.handle)));
8870 return WERR_BADFID;
8873 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
8874 return WERR_BADFID;
8877 result = get_a_printer(Printer, &printer, 2, lp_const_servicename(snum));
8878 if (!W_ERROR_IS_OK(result)) {
8879 return result;
8882 /* get the list of subkey names */
8884 data = printer->info_2->data;
8886 num_keys = get_printer_subkeys(data, r->in.key_name, &keynames);
8887 if (num_keys == -1) {
8888 result = WERR_BADFILE;
8889 goto done;
8892 *r->out.needed = 4;
8894 array = talloc_zero_array(r->out.key_buffer, const char *, num_keys + 1);
8895 if (!array) {
8896 result = WERR_NOMEM;
8897 goto done;
8900 for (i=0; i < num_keys; i++) {
8901 array[i] = talloc_strdup(array, keynames[i]);
8902 if (!array[i]) {
8903 result = WERR_NOMEM;
8904 goto done;
8907 *r->out.needed += strlen_m_term(keynames[i]) * 2;
8910 if (r->in.offered < *r->out.needed) {
8911 result = WERR_MORE_DATA;
8912 goto done;
8915 result = WERR_OK;
8917 *r->out.key_buffer = array;
8919 done:
8920 if (!W_ERROR_IS_OK(result)) {
8921 TALLOC_FREE(array);
8922 ZERO_STRUCTP(r->out.key_buffer);
8925 free_a_printer(&printer, 2);
8926 SAFE_FREE(keynames);
8928 return result;
8931 /****************************************************************
8932 _spoolss_DeletePrinterKey
8933 ****************************************************************/
8935 WERROR _spoolss_DeletePrinterKey(pipes_struct *p,
8936 struct spoolss_DeletePrinterKey *r)
8938 POLICY_HND *handle = r->in.handle;
8939 Printer_entry *Printer = find_printer_index_by_hnd(p, handle);
8940 NT_PRINTER_INFO_LEVEL *printer = NULL;
8941 int snum=0;
8942 WERROR status;
8944 DEBUG(5,("_spoolss_DeletePrinterKey\n"));
8946 if (!Printer) {
8947 DEBUG(2,("_spoolss_DeletePrinterKey: Invalid handle (%s:%u:%u).\n",
8948 OUR_HANDLE(handle)));
8949 return WERR_BADFID;
8952 /* if keyname == NULL, return error */
8954 if ( !r->in.key_name )
8955 return WERR_INVALID_PARAM;
8957 if (!get_printer_snum(p, handle, &snum, NULL))
8958 return WERR_BADFID;
8960 if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
8961 DEBUG(3, ("_spoolss_DeletePrinterKey: "
8962 "printer properties change denied by handle\n"));
8963 return WERR_ACCESS_DENIED;
8966 status = get_a_printer(Printer, &printer, 2, lp_const_servicename(snum));
8967 if (!W_ERROR_IS_OK(status))
8968 return status;
8970 /* delete the key and all subneys */
8972 status = delete_all_printer_data( printer->info_2, r->in.key_name );
8974 if ( W_ERROR_IS_OK(status) )
8975 status = mod_a_printer(printer, 2);
8977 free_a_printer( &printer, 2 );
8979 return status;
8983 /********************************************************************
8984 * spoolss_enumprinterdataex
8985 ********************************************************************/
8987 WERROR _spoolss_enumprinterdataex(pipes_struct *p, SPOOL_Q_ENUMPRINTERDATAEX *q_u, SPOOL_R_ENUMPRINTERDATAEX *r_u)
8989 POLICY_HND *handle = &q_u->handle;
8990 uint32 in_size = q_u->size;
8991 uint32 num_entries,
8992 needed;
8993 NT_PRINTER_INFO_LEVEL *printer = NULL;
8994 PRINTER_ENUM_VALUES *enum_values = NULL;
8995 NT_PRINTER_DATA *p_data;
8996 fstring key;
8997 Printer_entry *Printer = find_printer_index_by_hnd(p, handle);
8998 int snum;
8999 WERROR result;
9000 int key_index;
9001 int i;
9002 REGISTRY_VALUE *val;
9003 char *value_name;
9004 uint32 data_len;
9007 DEBUG(4,("_spoolss_enumprinterdataex\n"));
9009 if (!Printer) {
9010 DEBUG(2,("_spoolss_enumprinterdataex: Invalid handle (%s:%u:%u1<).\n", OUR_HANDLE(handle)));
9011 return WERR_BADFID;
9015 * first check for a keyname of NULL or "". Win2k seems to send
9016 * this a lot and we should send back WERR_INVALID_PARAM
9017 * no need to spend time looking up the printer in this case.
9018 * --jerry
9021 unistr2_to_ascii(key, &q_u->key, sizeof(key));
9022 if ( !strlen(key) ) {
9023 result = WERR_INVALID_PARAM;
9024 goto done;
9027 /* get the printer off of disk */
9029 if (!get_printer_snum(p,handle, &snum, NULL))
9030 return WERR_BADFID;
9032 ZERO_STRUCT(printer);
9033 result = get_a_printer(Printer, &printer, 2, lp_const_servicename(snum));
9034 if (!W_ERROR_IS_OK(result))
9035 return result;
9037 /* now look for a match on the key name */
9039 p_data = printer->info_2->data;
9041 unistr2_to_ascii(key, &q_u->key, sizeof(key));
9042 if ( (key_index = lookup_printerkey( p_data, key)) == -1 )
9044 DEBUG(10,("_spoolss_enumprinterdataex: Unknown keyname [%s]\n", key));
9045 result = WERR_INVALID_PARAM;
9046 goto done;
9049 result = WERR_OK;
9050 needed = 0;
9052 /* allocate the memory for the array of pointers -- if necessary */
9054 num_entries = regval_ctr_numvals( p_data->keys[key_index].values );
9055 if ( num_entries )
9057 if ( (enum_values=TALLOC_ARRAY(p->mem_ctx, PRINTER_ENUM_VALUES, num_entries)) == NULL )
9059 DEBUG(0,("_spoolss_enumprinterdataex: talloc() failed to allocate memory for [%lu] bytes!\n",
9060 (unsigned long)num_entries*sizeof(PRINTER_ENUM_VALUES)));
9061 result = WERR_NOMEM;
9062 goto done;
9065 memset( enum_values, 0x0, num_entries*sizeof(PRINTER_ENUM_VALUES) );
9069 * loop through all params and build the array to pass
9070 * back to the client
9073 for ( i=0; i<num_entries; i++ )
9075 /* lookup the registry value */
9077 val = regval_ctr_specific_value( p_data->keys[key_index].values, i );
9078 DEBUG(10,("retrieved value number [%d] [%s]\n", i, regval_name(val) ));
9080 /* copy the data */
9082 value_name = regval_name( val );
9083 init_unistr( &enum_values[i].valuename, value_name );
9084 enum_values[i].value_len = (strlen(value_name)+1) * 2;
9085 enum_values[i].type = regval_type( val );
9087 data_len = regval_size( val );
9088 if ( data_len ) {
9089 if ( !(enum_values[i].data = (uint8 *)TALLOC_MEMDUP(p->mem_ctx, regval_data_p(val), data_len)) )
9091 DEBUG(0,("TALLOC_MEMDUP failed to allocate memory [data_len=%d] for data!\n",
9092 data_len ));
9093 result = WERR_NOMEM;
9094 goto done;
9097 enum_values[i].data_len = data_len;
9099 /* keep track of the size of the array in bytes */
9101 needed += spoolss_size_printer_enum_values(&enum_values[i]);
9104 /* housekeeping information in the reply */
9106 /* Fix from Martin Zielinski <mz@seh.de> - ensure
9107 * the hand marshalled container size is a multiple
9108 * of 4 bytes for RPC alignment.
9111 if (needed % 4) {
9112 needed += 4-(needed % 4);
9115 r_u->needed = needed;
9116 r_u->returned = num_entries;
9118 if (needed > in_size) {
9119 result = WERR_MORE_DATA;
9120 goto done;
9123 /* copy data into the reply */
9125 /* mz: Vista x64 returns 0x6f7 (The stub received bad data), if the
9126 response buffer size is != the offered buffer size
9128 r_u->ctr.size = r_u->needed;
9130 r_u->ctr.size = in_size;
9132 r_u->ctr.size_of_array = r_u->returned;
9133 r_u->ctr.values = enum_values;
9135 done:
9136 if ( printer )
9137 free_a_printer(&printer, 2);
9139 return result;
9142 /****************************************************************************
9143 ****************************************************************************/
9145 static WERROR getprintprocessordirectory_level_1(TALLOC_CTX *mem_ctx,
9146 const char *servername,
9147 const char *environment,
9148 struct spoolss_PrintProcessorDirectoryInfo1 *r)
9150 WERROR werr;
9151 char *path = NULL;
9153 werr = compose_spoolss_server_path(mem_ctx,
9154 servername,
9155 environment,
9156 SPOOLSS_PRTPROCS_PATH,
9157 &path);
9158 if (!W_ERROR_IS_OK(werr)) {
9159 return werr;
9162 DEBUG(4,("print processor directory: [%s]\n", path));
9164 r->directory_name = path;
9166 return WERR_OK;
9169 /****************************************************************
9170 _spoolss_GetPrintProcessorDirectory
9171 ****************************************************************/
9173 WERROR _spoolss_GetPrintProcessorDirectory(pipes_struct *p,
9174 struct spoolss_GetPrintProcessorDirectory *r)
9176 WERROR result;
9178 /* that's an [in out] buffer */
9180 if (!r->in.buffer && (r->in.offered != 0)) {
9181 return WERR_INVALID_PARAM;
9184 DEBUG(5,("_spoolss_GetPrintProcessorDirectory: level %d\n",
9185 r->in.level));
9187 *r->out.needed = 0;
9189 /* r->in.level is ignored */
9191 result = getprintprocessordirectory_level_1(p->mem_ctx,
9192 r->in.server,
9193 r->in.environment,
9194 &r->out.info->info1);
9195 if (!W_ERROR_IS_OK(result)) {
9196 TALLOC_FREE(r->out.info);
9197 return result;
9200 *r->out.needed = SPOOLSS_BUFFER_UNION(spoolss_PrintProcessorDirectoryInfo, NULL,
9201 r->out.info, r->in.level);
9202 r->out.info = SPOOLSS_BUFFER_OK(r->out.info, NULL);
9204 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
9207 /*******************************************************************
9208 ********************************************************************/
9210 static bool push_monitorui_buf(TALLOC_CTX *mem_ctx, DATA_BLOB *buf,
9211 const char *dllname)
9213 enum ndr_err_code ndr_err;
9214 struct spoolss_MonitorUi ui;
9216 ui.dll_name = dllname;
9218 ndr_err = ndr_push_struct_blob(buf, mem_ctx, NULL, &ui,
9219 (ndr_push_flags_fn_t)ndr_push_spoolss_MonitorUi);
9220 if (NDR_ERR_CODE_IS_SUCCESS(ndr_err) && (DEBUGLEVEL >= 10)) {
9221 NDR_PRINT_DEBUG(spoolss_MonitorUi, &ui);
9223 return NDR_ERR_CODE_IS_SUCCESS(ndr_err);
9226 /*******************************************************************
9227 Streams the monitor UI DLL name in UNICODE
9228 *******************************************************************/
9230 static WERROR xcvtcp_monitorui(TALLOC_CTX *mem_ctx,
9231 NT_USER_TOKEN *token, DATA_BLOB *in,
9232 DATA_BLOB *out, uint32_t *needed)
9234 const char *dllname = "tcpmonui.dll";
9236 *needed = (strlen(dllname)+1) * 2;
9238 if (out->length < *needed) {
9239 return WERR_INSUFFICIENT_BUFFER;
9242 if (!push_monitorui_buf(mem_ctx, out, dllname)) {
9243 return WERR_NOMEM;
9246 return WERR_OK;
9249 /*******************************************************************
9250 ********************************************************************/
9252 static bool pull_port_data_1(TALLOC_CTX *mem_ctx,
9253 struct spoolss_PortData1 *port1,
9254 const DATA_BLOB *buf)
9256 enum ndr_err_code ndr_err;
9257 ndr_err = ndr_pull_struct_blob(buf, mem_ctx, NULL, port1,
9258 (ndr_pull_flags_fn_t)ndr_pull_spoolss_PortData1);
9259 if (NDR_ERR_CODE_IS_SUCCESS(ndr_err) && (DEBUGLEVEL >= 10)) {
9260 NDR_PRINT_DEBUG(spoolss_PortData1, port1);
9262 return NDR_ERR_CODE_IS_SUCCESS(ndr_err);
9265 /*******************************************************************
9266 ********************************************************************/
9268 static bool pull_port_data_2(TALLOC_CTX *mem_ctx,
9269 struct spoolss_PortData2 *port2,
9270 const DATA_BLOB *buf)
9272 enum ndr_err_code ndr_err;
9273 ndr_err = ndr_pull_struct_blob(buf, mem_ctx, NULL, port2,
9274 (ndr_pull_flags_fn_t)ndr_pull_spoolss_PortData2);
9275 if (NDR_ERR_CODE_IS_SUCCESS(ndr_err) && (DEBUGLEVEL >= 10)) {
9276 NDR_PRINT_DEBUG(spoolss_PortData2, port2);
9278 return NDR_ERR_CODE_IS_SUCCESS(ndr_err);
9281 /*******************************************************************
9282 Create a new TCP/IP port
9283 *******************************************************************/
9285 static WERROR xcvtcp_addport(TALLOC_CTX *mem_ctx,
9286 NT_USER_TOKEN *token, DATA_BLOB *in,
9287 DATA_BLOB *out, uint32_t *needed)
9289 struct spoolss_PortData1 port1;
9290 struct spoolss_PortData2 port2;
9291 char *device_uri = NULL;
9292 uint32_t version;
9294 const char *portname;
9295 const char *hostaddress;
9296 const char *queue;
9297 uint32_t port_number;
9298 uint32_t protocol;
9300 /* peek for spoolss_PortData version */
9302 if (!in || (in->length < (128 + 4))) {
9303 return WERR_GENERAL_FAILURE;
9306 version = IVAL(in->data, 128);
9308 switch (version) {
9309 case 1:
9310 ZERO_STRUCT(port1);
9312 if (!pull_port_data_1(mem_ctx, &port1, in)) {
9313 return WERR_NOMEM;
9316 portname = port1.portname;
9317 hostaddress = port1.hostaddress;
9318 queue = port1.queue;
9319 protocol = port1.protocol;
9320 port_number = port1.port_number;
9322 break;
9323 case 2:
9324 ZERO_STRUCT(port2);
9326 if (!pull_port_data_2(mem_ctx, &port2, in)) {
9327 return WERR_NOMEM;
9330 portname = port2.portname;
9331 hostaddress = port2.hostaddress;
9332 queue = port2.queue;
9333 protocol = port2.protocol;
9334 port_number = port2.port_number;
9336 break;
9337 default:
9338 DEBUG(1,("xcvtcp_addport: "
9339 "unknown version of port_data: %d\n", version));
9340 return WERR_UNKNOWN_PORT;
9343 /* create the device URI and call the add_port_hook() */
9345 switch (protocol) {
9346 case PROTOCOL_RAWTCP_TYPE:
9347 device_uri = talloc_asprintf(mem_ctx,
9348 "socket://%s:%d/", hostaddress,
9349 port_number);
9350 break;
9352 case PROTOCOL_LPR_TYPE:
9353 device_uri = talloc_asprintf(mem_ctx,
9354 "lpr://%s/%s", hostaddress, queue );
9355 break;
9357 default:
9358 return WERR_UNKNOWN_PORT;
9361 if (!device_uri) {
9362 return WERR_NOMEM;
9365 return add_port_hook(mem_ctx, token, portname, device_uri);
9368 /*******************************************************************
9369 *******************************************************************/
9371 struct xcv_api_table xcvtcp_cmds[] = {
9372 { "MonitorUI", xcvtcp_monitorui },
9373 { "AddPort", xcvtcp_addport},
9374 { NULL, NULL }
9377 static WERROR process_xcvtcp_command(TALLOC_CTX *mem_ctx,
9378 NT_USER_TOKEN *token, const char *command,
9379 DATA_BLOB *inbuf,
9380 DATA_BLOB *outbuf,
9381 uint32_t *needed )
9383 int i;
9385 DEBUG(10,("process_xcvtcp_command: Received command \"%s\"\n", command));
9387 for ( i=0; xcvtcp_cmds[i].name; i++ ) {
9388 if ( strcmp( command, xcvtcp_cmds[i].name ) == 0 )
9389 return xcvtcp_cmds[i].fn(mem_ctx, token, inbuf, outbuf, needed);
9392 return WERR_BADFUNC;
9395 /*******************************************************************
9396 *******************************************************************/
9397 #if 0 /* don't support management using the "Local Port" monitor */
9399 static WERROR xcvlocal_monitorui(TALLOC_CTX *mem_ctx,
9400 NT_USER_TOKEN *token, DATA_BLOB *in,
9401 DATA_BLOB *out, uint32_t *needed)
9403 const char *dllname = "localui.dll";
9405 *needed = (strlen(dllname)+1) * 2;
9407 if (out->length < *needed) {
9408 return WERR_INSUFFICIENT_BUFFER;
9411 if (!push_monitorui_buf(mem_ctx, out, dllname)) {
9412 return WERR_NOMEM;
9415 return WERR_OK;
9418 /*******************************************************************
9419 *******************************************************************/
9421 struct xcv_api_table xcvlocal_cmds[] = {
9422 { "MonitorUI", xcvlocal_monitorui },
9423 { NULL, NULL }
9425 #else
9426 struct xcv_api_table xcvlocal_cmds[] = {
9427 { NULL, NULL }
9429 #endif
9433 /*******************************************************************
9434 *******************************************************************/
9436 static WERROR process_xcvlocal_command(TALLOC_CTX *mem_ctx,
9437 NT_USER_TOKEN *token, const char *command,
9438 DATA_BLOB *inbuf, DATA_BLOB *outbuf,
9439 uint32_t *needed)
9441 int i;
9443 DEBUG(10,("process_xcvlocal_command: Received command \"%s\"\n", command));
9445 for ( i=0; xcvlocal_cmds[i].name; i++ ) {
9446 if ( strcmp( command, xcvlocal_cmds[i].name ) == 0 )
9447 return xcvlocal_cmds[i].fn(mem_ctx, token, inbuf, outbuf, needed);
9449 return WERR_BADFUNC;
9452 /****************************************************************
9453 _spoolss_XcvData
9454 ****************************************************************/
9456 WERROR _spoolss_XcvData(pipes_struct *p,
9457 struct spoolss_XcvData *r)
9459 Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
9460 DATA_BLOB out_data = data_blob_null;
9461 WERROR werror;
9463 if (!Printer) {
9464 DEBUG(2,("_spoolss_XcvData: Invalid handle (%s:%u:%u).\n",
9465 OUR_HANDLE(r->in.handle)));
9466 return WERR_BADFID;
9469 /* Has to be a handle to the TCP/IP port monitor */
9471 if ( !(Printer->printer_type & (SPLHND_PORTMON_LOCAL|SPLHND_PORTMON_TCP)) ) {
9472 DEBUG(2,("_spoolss_XcvData: Call only valid for Port Monitors\n"));
9473 return WERR_BADFID;
9476 /* requires administrative access to the server */
9478 if ( !(Printer->access_granted & SERVER_ACCESS_ADMINISTER) ) {
9479 DEBUG(2,("_spoolss_XcvData: denied by handle permissions.\n"));
9480 return WERR_ACCESS_DENIED;
9483 /* Allocate the outgoing buffer */
9485 if (r->in.out_data_size) {
9486 out_data = data_blob_talloc_zero(p->mem_ctx, r->in.out_data_size);
9487 if (out_data.data == NULL) {
9488 return WERR_NOMEM;
9492 switch ( Printer->printer_type ) {
9493 case SPLHND_PORTMON_TCP:
9494 werror = process_xcvtcp_command(p->mem_ctx,
9495 p->server_info->ptok,
9496 r->in.function_name,
9497 &r->in.in_data, &out_data,
9498 r->out.needed);
9499 break;
9500 case SPLHND_PORTMON_LOCAL:
9501 werror = process_xcvlocal_command(p->mem_ctx,
9502 p->server_info->ptok,
9503 r->in.function_name,
9504 &r->in.in_data, &out_data,
9505 r->out.needed);
9506 break;
9507 default:
9508 werror = WERR_INVALID_PRINT_MONITOR;
9511 if (!W_ERROR_IS_OK(werror)) {
9512 return werror;
9515 *r->out.status_code = 0;
9517 memcpy(r->out.out_data, out_data.data, out_data.length);
9519 return WERR_OK;
9522 /****************************************************************
9523 _spoolss_AddPrintProcessor
9524 ****************************************************************/
9526 WERROR _spoolss_AddPrintProcessor(pipes_struct *p,
9527 struct spoolss_AddPrintProcessor *r)
9529 /* for now, just indicate success and ignore the add. We'll
9530 automatically set the winprint processor for printer
9531 entries later. Used to debug the LexMark Optra S 1855 PCL
9532 driver --jerry */
9534 return WERR_OK;
9537 /****************************************************************
9538 _spoolss_AddPrinter
9539 ****************************************************************/
9541 WERROR _spoolss_AddPrinter(pipes_struct *p,
9542 struct spoolss_AddPrinter *r)
9544 p->rng_fault_state = true;
9545 return WERR_NOT_SUPPORTED;
9548 /****************************************************************
9549 _spoolss_GetPrinterDriver
9550 ****************************************************************/
9552 WERROR _spoolss_GetPrinterDriver(pipes_struct *p,
9553 struct spoolss_GetPrinterDriver *r)
9555 p->rng_fault_state = true;
9556 return WERR_NOT_SUPPORTED;
9559 /****************************************************************
9560 _spoolss_ReadPrinter
9561 ****************************************************************/
9563 WERROR _spoolss_ReadPrinter(pipes_struct *p,
9564 struct spoolss_ReadPrinter *r)
9566 p->rng_fault_state = true;
9567 return WERR_NOT_SUPPORTED;
9570 /****************************************************************
9571 _spoolss_WaitForPrinterChange
9572 ****************************************************************/
9574 WERROR _spoolss_WaitForPrinterChange(pipes_struct *p,
9575 struct spoolss_WaitForPrinterChange *r)
9577 p->rng_fault_state = true;
9578 return WERR_NOT_SUPPORTED;
9581 /****************************************************************
9582 _spoolss_AddPort
9583 ****************************************************************/
9585 WERROR _spoolss_AddPort(pipes_struct *p,
9586 struct spoolss_AddPort *r)
9588 p->rng_fault_state = true;
9589 return WERR_NOT_SUPPORTED;
9592 /****************************************************************
9593 _spoolss_ConfigurePort
9594 ****************************************************************/
9596 WERROR _spoolss_ConfigurePort(pipes_struct *p,
9597 struct spoolss_ConfigurePort *r)
9599 p->rng_fault_state = true;
9600 return WERR_NOT_SUPPORTED;
9603 /****************************************************************
9604 _spoolss_DeletePort
9605 ****************************************************************/
9607 WERROR _spoolss_DeletePort(pipes_struct *p,
9608 struct spoolss_DeletePort *r)
9610 p->rng_fault_state = true;
9611 return WERR_NOT_SUPPORTED;
9614 /****************************************************************
9615 _spoolss_CreatePrinterIC
9616 ****************************************************************/
9618 WERROR _spoolss_CreatePrinterIC(pipes_struct *p,
9619 struct spoolss_CreatePrinterIC *r)
9621 p->rng_fault_state = true;
9622 return WERR_NOT_SUPPORTED;
9625 /****************************************************************
9626 _spoolss_PlayGDIScriptOnPrinterIC
9627 ****************************************************************/
9629 WERROR _spoolss_PlayGDIScriptOnPrinterIC(pipes_struct *p,
9630 struct spoolss_PlayGDIScriptOnPrinterIC *r)
9632 p->rng_fault_state = true;
9633 return WERR_NOT_SUPPORTED;
9636 /****************************************************************
9637 _spoolss_DeletePrinterIC
9638 ****************************************************************/
9640 WERROR _spoolss_DeletePrinterIC(pipes_struct *p,
9641 struct spoolss_DeletePrinterIC *r)
9643 p->rng_fault_state = true;
9644 return WERR_NOT_SUPPORTED;
9647 /****************************************************************
9648 _spoolss_AddPrinterConnection
9649 ****************************************************************/
9651 WERROR _spoolss_AddPrinterConnection(pipes_struct *p,
9652 struct spoolss_AddPrinterConnection *r)
9654 p->rng_fault_state = true;
9655 return WERR_NOT_SUPPORTED;
9658 /****************************************************************
9659 _spoolss_DeletePrinterConnection
9660 ****************************************************************/
9662 WERROR _spoolss_DeletePrinterConnection(pipes_struct *p,
9663 struct spoolss_DeletePrinterConnection *r)
9665 p->rng_fault_state = true;
9666 return WERR_NOT_SUPPORTED;
9669 /****************************************************************
9670 _spoolss_PrinterMessageBox
9671 ****************************************************************/
9673 WERROR _spoolss_PrinterMessageBox(pipes_struct *p,
9674 struct spoolss_PrinterMessageBox *r)
9676 p->rng_fault_state = true;
9677 return WERR_NOT_SUPPORTED;
9680 /****************************************************************
9681 _spoolss_AddMonitor
9682 ****************************************************************/
9684 WERROR _spoolss_AddMonitor(pipes_struct *p,
9685 struct spoolss_AddMonitor *r)
9687 p->rng_fault_state = true;
9688 return WERR_NOT_SUPPORTED;
9691 /****************************************************************
9692 _spoolss_DeleteMonitor
9693 ****************************************************************/
9695 WERROR _spoolss_DeleteMonitor(pipes_struct *p,
9696 struct spoolss_DeleteMonitor *r)
9698 p->rng_fault_state = true;
9699 return WERR_NOT_SUPPORTED;
9702 /****************************************************************
9703 _spoolss_DeletePrintProcessor
9704 ****************************************************************/
9706 WERROR _spoolss_DeletePrintProcessor(pipes_struct *p,
9707 struct spoolss_DeletePrintProcessor *r)
9709 p->rng_fault_state = true;
9710 return WERR_NOT_SUPPORTED;
9713 /****************************************************************
9714 _spoolss_AddPrintProvidor
9715 ****************************************************************/
9717 WERROR _spoolss_AddPrintProvidor(pipes_struct *p,
9718 struct spoolss_AddPrintProvidor *r)
9720 p->rng_fault_state = true;
9721 return WERR_NOT_SUPPORTED;
9724 /****************************************************************
9725 _spoolss_DeletePrintProvidor
9726 ****************************************************************/
9728 WERROR _spoolss_DeletePrintProvidor(pipes_struct *p,
9729 struct spoolss_DeletePrintProvidor *r)
9731 p->rng_fault_state = true;
9732 return WERR_NOT_SUPPORTED;
9735 /****************************************************************
9736 _spoolss_FindFirstPrinterChangeNotification
9737 ****************************************************************/
9739 WERROR _spoolss_FindFirstPrinterChangeNotification(pipes_struct *p,
9740 struct spoolss_FindFirstPrinterChangeNotification *r)
9742 p->rng_fault_state = true;
9743 return WERR_NOT_SUPPORTED;
9746 /****************************************************************
9747 _spoolss_FindNextPrinterChangeNotification
9748 ****************************************************************/
9750 WERROR _spoolss_FindNextPrinterChangeNotification(pipes_struct *p,
9751 struct spoolss_FindNextPrinterChangeNotification *r)
9753 p->rng_fault_state = true;
9754 return WERR_NOT_SUPPORTED;
9757 /****************************************************************
9758 _spoolss_RouterFindFirstPrinterChangeNotificationOld
9759 ****************************************************************/
9761 WERROR _spoolss_RouterFindFirstPrinterChangeNotificationOld(pipes_struct *p,
9762 struct spoolss_RouterFindFirstPrinterChangeNotificationOld *r)
9764 p->rng_fault_state = true;
9765 return WERR_NOT_SUPPORTED;
9768 /****************************************************************
9769 _spoolss_ReplyOpenPrinter
9770 ****************************************************************/
9772 WERROR _spoolss_ReplyOpenPrinter(pipes_struct *p,
9773 struct spoolss_ReplyOpenPrinter *r)
9775 p->rng_fault_state = true;
9776 return WERR_NOT_SUPPORTED;
9779 /****************************************************************
9780 _spoolss_RouterReplyPrinter
9781 ****************************************************************/
9783 WERROR _spoolss_RouterReplyPrinter(pipes_struct *p,
9784 struct spoolss_RouterReplyPrinter *r)
9786 p->rng_fault_state = true;
9787 return WERR_NOT_SUPPORTED;
9790 /****************************************************************
9791 _spoolss_ReplyClosePrinter
9792 ****************************************************************/
9794 WERROR _spoolss_ReplyClosePrinter(pipes_struct *p,
9795 struct spoolss_ReplyClosePrinter *r)
9797 p->rng_fault_state = true;
9798 return WERR_NOT_SUPPORTED;
9801 /****************************************************************
9802 _spoolss_AddPortEx
9803 ****************************************************************/
9805 WERROR _spoolss_AddPortEx(pipes_struct *p,
9806 struct spoolss_AddPortEx *r)
9808 p->rng_fault_state = true;
9809 return WERR_NOT_SUPPORTED;
9812 /****************************************************************
9813 _spoolss_RouterFindFirstPrinterChangeNotification
9814 ****************************************************************/
9816 WERROR _spoolss_RouterFindFirstPrinterChangeNotification(pipes_struct *p,
9817 struct spoolss_RouterFindFirstPrinterChangeNotification *r)
9819 p->rng_fault_state = true;
9820 return WERR_NOT_SUPPORTED;
9823 /****************************************************************
9824 _spoolss_SpoolerInit
9825 ****************************************************************/
9827 WERROR _spoolss_SpoolerInit(pipes_struct *p,
9828 struct spoolss_SpoolerInit *r)
9830 p->rng_fault_state = true;
9831 return WERR_NOT_SUPPORTED;
9834 /****************************************************************
9835 _spoolss_ResetPrinterEx
9836 ****************************************************************/
9838 WERROR _spoolss_ResetPrinterEx(pipes_struct *p,
9839 struct spoolss_ResetPrinterEx *r)
9841 p->rng_fault_state = true;
9842 return WERR_NOT_SUPPORTED;
9845 /****************************************************************
9846 _spoolss_RouterReplyPrinterEx
9847 ****************************************************************/
9849 WERROR _spoolss_RouterReplyPrinterEx(pipes_struct *p,
9850 struct spoolss_RouterReplyPrinterEx *r)
9852 p->rng_fault_state = true;
9853 return WERR_NOT_SUPPORTED;
9856 /****************************************************************
9857 _spoolss_44
9858 ****************************************************************/
9860 WERROR _spoolss_44(pipes_struct *p,
9861 struct spoolss_44 *r)
9863 p->rng_fault_state = true;
9864 return WERR_NOT_SUPPORTED;
9867 /****************************************************************
9868 _spoolss_47
9869 ****************************************************************/
9871 WERROR _spoolss_47(pipes_struct *p,
9872 struct spoolss_47 *r)
9874 p->rng_fault_state = true;
9875 return WERR_NOT_SUPPORTED;
9878 /****************************************************************
9879 _spoolss_4a
9880 ****************************************************************/
9882 WERROR _spoolss_4a(pipes_struct *p,
9883 struct spoolss_4a *r)
9885 p->rng_fault_state = true;
9886 return WERR_NOT_SUPPORTED;
9889 /****************************************************************
9890 _spoolss_4b
9891 ****************************************************************/
9893 WERROR _spoolss_4b(pipes_struct *p,
9894 struct spoolss_4b *r)
9896 p->rng_fault_state = true;
9897 return WERR_NOT_SUPPORTED;
9900 /****************************************************************
9901 _spoolss_4c
9902 ****************************************************************/
9904 WERROR _spoolss_4c(pipes_struct *p,
9905 struct spoolss_4c *r)
9907 p->rng_fault_state = true;
9908 return WERR_NOT_SUPPORTED;
9911 /****************************************************************
9912 _spoolss_EnumPrinterDataEx
9913 ****************************************************************/
9915 WERROR _spoolss_EnumPrinterDataEx(pipes_struct *p,
9916 struct spoolss_EnumPrinterDataEx *r)
9918 p->rng_fault_state = true;
9919 return WERR_NOT_SUPPORTED;
9922 /****************************************************************
9923 _spoolss_53
9924 ****************************************************************/
9926 WERROR _spoolss_53(pipes_struct *p,
9927 struct spoolss_53 *r)
9929 p->rng_fault_state = true;
9930 return WERR_NOT_SUPPORTED;
9933 /****************************************************************
9934 _spoolss_55
9935 ****************************************************************/
9937 WERROR _spoolss_55(pipes_struct *p,
9938 struct spoolss_55 *r)
9940 p->rng_fault_state = true;
9941 return WERR_NOT_SUPPORTED;
9944 /****************************************************************
9945 _spoolss_56
9946 ****************************************************************/
9948 WERROR _spoolss_56(pipes_struct *p,
9949 struct spoolss_56 *r)
9951 p->rng_fault_state = true;
9952 return WERR_NOT_SUPPORTED;
9955 /****************************************************************
9956 _spoolss_57
9957 ****************************************************************/
9959 WERROR _spoolss_57(pipes_struct *p,
9960 struct spoolss_57 *r)
9962 p->rng_fault_state = true;
9963 return WERR_NOT_SUPPORTED;
9966 /****************************************************************
9967 _spoolss_5a
9968 ****************************************************************/
9970 WERROR _spoolss_5a(pipes_struct *p,
9971 struct spoolss_5a *r)
9973 p->rng_fault_state = true;
9974 return WERR_NOT_SUPPORTED;
9977 /****************************************************************
9978 _spoolss_5b
9979 ****************************************************************/
9981 WERROR _spoolss_5b(pipes_struct *p,
9982 struct spoolss_5b *r)
9984 p->rng_fault_state = true;
9985 return WERR_NOT_SUPPORTED;
9988 /****************************************************************
9989 _spoolss_5c
9990 ****************************************************************/
9992 WERROR _spoolss_5c(pipes_struct *p,
9993 struct spoolss_5c *r)
9995 p->rng_fault_state = true;
9996 return WERR_NOT_SUPPORTED;
9999 /****************************************************************
10000 _spoolss_5d
10001 ****************************************************************/
10003 WERROR _spoolss_5d(pipes_struct *p,
10004 struct spoolss_5d *r)
10006 p->rng_fault_state = true;
10007 return WERR_NOT_SUPPORTED;
10010 /****************************************************************
10011 _spoolss_5e
10012 ****************************************************************/
10014 WERROR _spoolss_5e(pipes_struct *p,
10015 struct spoolss_5e *r)
10017 p->rng_fault_state = true;
10018 return WERR_NOT_SUPPORTED;
10021 /****************************************************************
10022 _spoolss_5f
10023 ****************************************************************/
10025 WERROR _spoolss_5f(pipes_struct *p,
10026 struct spoolss_5f *r)
10028 p->rng_fault_state = true;
10029 return WERR_NOT_SUPPORTED;