s3-spoolss: remove unused architecture from fill_printer_driver_info1().
[Samba.git] / source3 / rpc_server / srv_spoolss_nt.c
blob13c3710a9aa5d2a6908dac26943352ca1e6a05cc
1 /*
2 * Unix SMB/CIFS implementation.
3 * RPC Pipe client / server routines
4 * Copyright (C) Andrew Tridgell 1992-2000,
5 * Copyright (C) Luke Kenneth Casson Leighton 1996-2000,
6 * Copyright (C) Jean François Micouleau 1998-2000,
7 * Copyright (C) Jeremy Allison 2001-2002,
8 * Copyright (C) Gerald Carter 2000-2004,
9 * Copyright (C) Tim Potter 2001-2002.
10 * Copyright (C) Guenther Deschner 2009.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 3 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <http://www.gnu.org/licenses/>.
26 /* Since the SPOOLSS rpc routines are basically DOS 16-bit calls wrapped
27 up, all the errors returned are DOS errors, not NT status codes. */
29 #include "includes.h"
30 #include "../librpc/gen_ndr/srv_spoolss.h"
31 #include "../librpc/gen_ndr/cli_spoolss.h"
33 /* macros stolen from s4 spoolss server */
34 #define SPOOLSS_BUFFER_UNION(fn,ic,info,level) \
35 ((info)?ndr_size_##fn(info, level, ic, 0):0)
37 #define SPOOLSS_BUFFER_UNION_ARRAY(mem_ctx,fn,ic,info,level,count) \
38 ((info)?ndr_size_##fn##_info(mem_ctx, ic, level, count, info):0)
40 #define SPOOLSS_BUFFER_ARRAY(mem_ctx,fn,ic,info,count) \
41 ((info)?ndr_size_##fn##_info(mem_ctx, ic, count, info):0)
43 #define SPOOLSS_BUFFER_OK(val_true,val_false) ((r->in.offered >= *r->out.needed)?val_true:val_false)
46 extern userdom_struct current_user_info;
48 #undef DBGC_CLASS
49 #define DBGC_CLASS DBGC_RPC_SRV
51 #ifndef MAX_OPEN_PRINTER_EXS
52 #define MAX_OPEN_PRINTER_EXS 50
53 #endif
55 #define MAGIC_DISPLAY_FREQUENCY 0xfade2bad
56 #define PHANTOM_DEVMODE_KEY "_p_f_a_n_t_0_m_"
58 static Printer_entry *printers_list;
60 typedef struct _counter_printer_0 {
61 struct _counter_printer_0 *next;
62 struct _counter_printer_0 *prev;
64 int snum;
65 uint32_t counter;
66 } counter_printer_0;
68 static counter_printer_0 *counter_list;
70 static struct rpc_pipe_client *notify_cli_pipe; /* print notify back-channel pipe handle*/
71 static uint32_t smb_connections = 0;
74 /* in printing/nt_printing.c */
76 extern struct standard_mapping printer_std_mapping, printserver_std_mapping;
78 /* API table for Xcv Monitor functions */
80 struct xcv_api_table {
81 const char *name;
82 WERROR(*fn) (TALLOC_CTX *mem_ctx, NT_USER_TOKEN *token, DATA_BLOB *in, DATA_BLOB *out, uint32_t *needed);
85 /********************************************************************
86 * Canonicalize servername.
87 ********************************************************************/
89 static const char *canon_servername(const char *servername)
91 const char *pservername = servername;
92 while (*pservername == '\\') {
93 pservername++;
95 return pservername;
98 /* translate between internal status numbers and NT status numbers */
99 static int nt_printj_status(int v)
101 switch (v) {
102 case LPQ_QUEUED:
103 return 0;
104 case LPQ_PAUSED:
105 return JOB_STATUS_PAUSED;
106 case LPQ_SPOOLING:
107 return JOB_STATUS_SPOOLING;
108 case LPQ_PRINTING:
109 return JOB_STATUS_PRINTING;
110 case LPQ_ERROR:
111 return JOB_STATUS_ERROR;
112 case LPQ_DELETING:
113 return JOB_STATUS_DELETING;
114 case LPQ_OFFLINE:
115 return JOB_STATUS_OFFLINE;
116 case LPQ_PAPEROUT:
117 return JOB_STATUS_PAPEROUT;
118 case LPQ_PRINTED:
119 return JOB_STATUS_PRINTED;
120 case LPQ_DELETED:
121 return JOB_STATUS_DELETED;
122 case LPQ_BLOCKED:
123 return JOB_STATUS_BLOCKED_DEVQ;
124 case LPQ_USER_INTERVENTION:
125 return JOB_STATUS_USER_INTERVENTION;
127 return 0;
130 static int nt_printq_status(int v)
132 switch (v) {
133 case LPQ_PAUSED:
134 return PRINTER_STATUS_PAUSED;
135 case LPQ_QUEUED:
136 case LPQ_SPOOLING:
137 case LPQ_PRINTING:
138 return 0;
140 return 0;
143 /***************************************************************************
144 Disconnect from the client
145 ****************************************************************************/
147 static void srv_spoolss_replycloseprinter(int snum, struct policy_handle *handle)
149 WERROR result;
150 NTSTATUS status;
153 * Tell the specific printing tdb we no longer want messages for this printer
154 * by deregistering our PID.
157 if (!print_notify_deregister_pid(snum))
158 DEBUG(0,("print_notify_register_pid: Failed to register our pid for printer %s\n", lp_const_servicename(snum) ));
160 /* weird if the test succeds !!! */
161 if (smb_connections==0) {
162 DEBUG(0,("srv_spoolss_replycloseprinter:Trying to close non-existant notify backchannel !\n"));
163 return;
166 status = rpccli_spoolss_ReplyClosePrinter(notify_cli_pipe, talloc_tos(),
167 handle,
168 &result);
169 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result))
170 DEBUG(0,("srv_spoolss_replycloseprinter: reply_close_printer failed [%s].\n",
171 win_errstr(result)));
173 /* if it's the last connection, deconnect the IPC$ share */
174 if (smb_connections==1) {
176 cli_shutdown( rpc_pipe_np_smb_conn(notify_cli_pipe) );
177 notify_cli_pipe = NULL; /* The above call shuts downn the pipe also. */
179 messaging_deregister(smbd_messaging_context(),
180 MSG_PRINTER_NOTIFY2, NULL);
182 /* Tell the connections db we're no longer interested in
183 * printer notify messages. */
185 register_message_flags(false, FLAG_MSG_PRINT_NOTIFY);
188 smb_connections--;
191 /****************************************************************************
192 Functions to free a printer entry datastruct.
193 ****************************************************************************/
195 static int printer_entry_destructor(Printer_entry *Printer)
197 if (Printer->notify.client_connected == true) {
198 int snum = -1;
200 if ( Printer->printer_type == SPLHND_SERVER) {
201 snum = -1;
202 srv_spoolss_replycloseprinter(snum, &Printer->notify.client_hnd);
203 } else if (Printer->printer_type == SPLHND_PRINTER) {
204 snum = print_queue_snum(Printer->sharename);
205 if (snum != -1)
206 srv_spoolss_replycloseprinter(snum,
207 &Printer->notify.client_hnd);
211 Printer->notify.flags=0;
212 Printer->notify.options=0;
213 Printer->notify.localmachine[0]='\0';
214 Printer->notify.printerlocal=0;
215 TALLOC_FREE(Printer->notify.option);
216 Printer->notify.client_connected = false;
218 free_nt_devicemode( &Printer->nt_devmode );
219 free_a_printer( &Printer->printer_info, 2 );
221 /* Remove from the internal list. */
222 DLIST_REMOVE(printers_list, Printer);
223 return 0;
226 /****************************************************************************
227 find printer index by handle
228 ****************************************************************************/
230 static Printer_entry *find_printer_index_by_hnd(pipes_struct *p,
231 struct policy_handle *hnd)
233 Printer_entry *find_printer = NULL;
235 if(!find_policy_by_hnd(p,hnd,(void **)(void *)&find_printer)) {
236 DEBUG(2,("find_printer_index_by_hnd: Printer handle not found: "));
237 return NULL;
240 return find_printer;
243 /****************************************************************************
244 Close printer index by handle.
245 ****************************************************************************/
247 static bool close_printer_handle(pipes_struct *p, struct policy_handle *hnd)
249 Printer_entry *Printer = find_printer_index_by_hnd(p, hnd);
251 if (!Printer) {
252 DEBUG(2,("close_printer_handle: Invalid handle (%s:%u:%u)\n",
253 OUR_HANDLE(hnd)));
254 return false;
257 close_policy_hnd(p, hnd);
259 return true;
262 /****************************************************************************
263 Delete a printer given a handle.
264 ****************************************************************************/
266 static WERROR delete_printer_hook(TALLOC_CTX *ctx, NT_USER_TOKEN *token, const char *sharename)
268 char *cmd = lp_deleteprinter_cmd();
269 char *command = NULL;
270 int ret;
271 SE_PRIV se_printop = SE_PRINT_OPERATOR;
272 bool is_print_op = false;
274 /* can't fail if we don't try */
276 if ( !*cmd )
277 return WERR_OK;
279 command = talloc_asprintf(ctx,
280 "%s \"%s\"",
281 cmd, sharename);
282 if (!command) {
283 return WERR_NOMEM;
285 if ( token )
286 is_print_op = user_has_privileges( token, &se_printop );
288 DEBUG(10,("Running [%s]\n", command));
290 /********** BEGIN SePrintOperatorPrivlege BLOCK **********/
292 if ( is_print_op )
293 become_root();
295 if ( (ret = smbrun(command, NULL)) == 0 ) {
296 /* Tell everyone we updated smb.conf. */
297 message_send_all(smbd_messaging_context(),
298 MSG_SMB_CONF_UPDATED, NULL, 0, NULL);
301 if ( is_print_op )
302 unbecome_root();
304 /********** END SePrintOperatorPrivlege BLOCK **********/
306 DEBUGADD(10,("returned [%d]\n", ret));
308 TALLOC_FREE(command);
310 if (ret != 0)
311 return WERR_BADFID; /* What to return here? */
313 /* go ahead and re-read the services immediately */
314 become_root();
315 reload_services(false);
316 unbecome_root();
318 if ( lp_servicenumber( sharename ) < 0 )
319 return WERR_ACCESS_DENIED;
321 return WERR_OK;
324 /****************************************************************************
325 Delete a printer given a handle.
326 ****************************************************************************/
328 static WERROR delete_printer_handle(pipes_struct *p, struct policy_handle *hnd)
330 Printer_entry *Printer = find_printer_index_by_hnd(p, hnd);
332 if (!Printer) {
333 DEBUG(2,("delete_printer_handle: Invalid handle (%s:%u:%u)\n",
334 OUR_HANDLE(hnd)));
335 return WERR_BADFID;
339 * It turns out that Windows allows delete printer on a handle
340 * opened by an admin user, then used on a pipe handle created
341 * by an anonymous user..... but they're working on security.... riiight !
342 * JRA.
345 if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
346 DEBUG(3, ("delete_printer_handle: denied by handle\n"));
347 return WERR_ACCESS_DENIED;
350 /* this does not need a become root since the access check has been
351 done on the handle already */
353 if (del_a_printer( Printer->sharename ) != 0) {
354 DEBUG(3,("Error deleting printer %s\n", Printer->sharename));
355 return WERR_BADFID;
358 return delete_printer_hook(p->mem_ctx, p->server_info->ptok,
359 Printer->sharename );
362 /****************************************************************************
363 Return the snum of a printer corresponding to an handle.
364 ****************************************************************************/
366 static bool get_printer_snum(pipes_struct *p, struct policy_handle *hnd,
367 int *number, struct share_params **params)
369 Printer_entry *Printer = find_printer_index_by_hnd(p, hnd);
371 if (!Printer) {
372 DEBUG(2,("get_printer_snum: Invalid handle (%s:%u:%u)\n",
373 OUR_HANDLE(hnd)));
374 return false;
377 switch (Printer->printer_type) {
378 case SPLHND_PRINTER:
379 DEBUG(4,("short name:%s\n", Printer->sharename));
380 *number = print_queue_snum(Printer->sharename);
381 return (*number != -1);
382 case SPLHND_SERVER:
383 return false;
384 default:
385 return false;
389 /****************************************************************************
390 Set printer handle type.
391 Check if it's \\server or \\server\printer
392 ****************************************************************************/
394 static bool set_printer_hnd_printertype(Printer_entry *Printer, const char *handlename)
396 DEBUG(3,("Setting printer type=%s\n", handlename));
398 if ( strlen(handlename) < 3 ) {
399 DEBUGADD(4,("A print server must have at least 1 char ! %s\n", handlename));
400 return false;
403 /* it's a print server */
404 if (*handlename=='\\' && *(handlename+1)=='\\' && !strchr_m(handlename+2, '\\')) {
405 DEBUGADD(4,("Printer is a print server\n"));
406 Printer->printer_type = SPLHND_SERVER;
408 /* it's a printer (set_printer_hnd_name() will handle port monitors */
409 else {
410 DEBUGADD(4,("Printer is a printer\n"));
411 Printer->printer_type = SPLHND_PRINTER;
414 return true;
417 /****************************************************************************
418 Set printer handle name.. Accept names like \\server, \\server\printer,
419 \\server\SHARE, & "\\server\,XcvMonitor Standard TCP/IP Port" See
420 the MSDN docs regarding OpenPrinter() for details on the XcvData() and
421 XcvDataPort() interface.
422 ****************************************************************************/
424 static bool set_printer_hnd_name(Printer_entry *Printer, const char *handlename)
426 int snum;
427 int n_services=lp_numservices();
428 char *aprinter, *printername;
429 const char *servername;
430 fstring sname;
431 bool found = false;
432 NT_PRINTER_INFO_LEVEL *printer = NULL;
433 WERROR result;
435 DEBUG(4,("Setting printer name=%s (len=%lu)\n", handlename,
436 (unsigned long)strlen(handlename)));
438 aprinter = CONST_DISCARD(char *, handlename);
439 if ( *handlename == '\\' ) {
440 servername = canon_servername(handlename);
441 if ( (aprinter = strchr_m( servername, '\\' )) != NULL ) {
442 *aprinter = '\0';
443 aprinter++;
445 } else {
446 servername = global_myname();
449 /* save the servername to fill in replies on this handle */
451 if ( !is_myname_or_ipaddr( servername ) )
452 return false;
454 fstrcpy( Printer->servername, servername );
456 if ( Printer->printer_type == SPLHND_SERVER )
457 return true;
459 if ( Printer->printer_type != SPLHND_PRINTER )
460 return false;
462 DEBUGADD(5, ("searching for [%s]\n", aprinter ));
464 /* check for the Port Monitor Interface */
466 if ( strequal( aprinter, SPL_XCV_MONITOR_TCPMON ) ) {
467 Printer->printer_type = SPLHND_PORTMON_TCP;
468 fstrcpy(sname, SPL_XCV_MONITOR_TCPMON);
469 found = true;
471 else if ( strequal( aprinter, SPL_XCV_MONITOR_LOCALMON ) ) {
472 Printer->printer_type = SPLHND_PORTMON_LOCAL;
473 fstrcpy(sname, SPL_XCV_MONITOR_LOCALMON);
474 found = true;
477 /* Search all sharenames first as this is easier than pulling
478 the printer_info_2 off of disk. Don't use find_service() since
479 that calls out to map_username() */
481 /* do another loop to look for printernames */
483 for (snum=0; !found && snum<n_services; snum++) {
485 /* no point going on if this is not a printer */
487 if ( !(lp_snum_ok(snum) && lp_print_ok(snum)) )
488 continue;
490 fstrcpy(sname, lp_servicename(snum));
491 if ( strequal( aprinter, sname ) ) {
492 found = true;
493 break;
496 /* no point looking up the printer object if
497 we aren't allowing printername != sharename */
499 if ( lp_force_printername(snum) )
500 continue;
502 fstrcpy(sname, lp_servicename(snum));
504 printer = NULL;
506 /* This call doesn't fill in the location or comment from
507 * a CUPS server for efficiency with large numbers of printers.
508 * JRA.
511 result = get_a_printer_search( NULL, &printer, 2, sname );
512 if ( !W_ERROR_IS_OK(result) ) {
513 DEBUG(0,("set_printer_hnd_name: failed to lookup printer [%s] -- result [%s]\n",
514 sname, win_errstr(result)));
515 continue;
518 /* printername is always returned as \\server\printername */
519 if ( !(printername = strchr_m(&printer->info_2->printername[2], '\\')) ) {
520 DEBUG(0,("set_printer_hnd_name: info2->printername in wrong format! [%s]\n",
521 printer->info_2->printername));
522 free_a_printer( &printer, 2);
523 continue;
526 printername++;
528 if ( strequal(printername, aprinter) ) {
529 free_a_printer( &printer, 2);
530 found = true;
531 break;
534 DEBUGADD(10, ("printername: %s\n", printername));
536 free_a_printer( &printer, 2);
539 free_a_printer( &printer, 2);
541 if ( !found ) {
542 DEBUGADD(4,("Printer not found\n"));
543 return false;
546 DEBUGADD(4,("set_printer_hnd_name: Printer found: %s -> %s\n", aprinter, sname));
548 fstrcpy(Printer->sharename, sname);
550 return true;
553 /****************************************************************************
554 Find first available printer slot. creates a printer handle for you.
555 ****************************************************************************/
557 static bool open_printer_hnd(pipes_struct *p, struct policy_handle *hnd,
558 const char *name, uint32_t access_granted)
560 Printer_entry *new_printer;
562 DEBUG(10,("open_printer_hnd: name [%s]\n", name));
564 new_printer = TALLOC_ZERO_P(NULL, Printer_entry);
565 if (new_printer == NULL) {
566 return false;
568 talloc_set_destructor(new_printer, printer_entry_destructor);
570 if (!create_policy_hnd(p, hnd, new_printer)) {
571 TALLOC_FREE(new_printer);
572 return false;
575 /* Add to the internal list. */
576 DLIST_ADD(printers_list, new_printer);
578 new_printer->notify.option=NULL;
580 if (!set_printer_hnd_printertype(new_printer, name)) {
581 close_printer_handle(p, hnd);
582 return false;
585 if (!set_printer_hnd_name(new_printer, name)) {
586 close_printer_handle(p, hnd);
587 return false;
590 new_printer->access_granted = access_granted;
592 DEBUG(5, ("%d printer handles active\n",
593 (int)num_pipe_handles(p->pipe_handles)));
595 return true;
598 /***************************************************************************
599 check to see if the client motify handle is monitoring the notification
600 given by (notify_type, notify_field).
601 **************************************************************************/
603 static bool is_monitoring_event_flags(uint32_t flags, uint16_t notify_type,
604 uint16_t notify_field)
606 return true;
609 static bool is_monitoring_event(Printer_entry *p, uint16_t notify_type,
610 uint16_t notify_field)
612 struct spoolss_NotifyOption *option = p->notify.option;
613 uint32_t i, j;
616 * Flags should always be zero when the change notify
617 * is registered by the client's spooler. A user Win32 app
618 * might use the flags though instead of the NOTIFY_OPTION_INFO
619 * --jerry
622 if (!option) {
623 return false;
626 if (p->notify.flags)
627 return is_monitoring_event_flags(
628 p->notify.flags, notify_type, notify_field);
630 for (i = 0; i < option->count; i++) {
632 /* Check match for notify_type */
634 if (option->types[i].type != notify_type)
635 continue;
637 /* Check match for field */
639 for (j = 0; j < option->types[i].count; j++) {
640 if (option->types[i].fields[j].field == notify_field) {
641 return true;
646 DEBUG(10, ("Open handle for \\\\%s\\%s is not monitoring 0x%02x/0x%02x\n",
647 p->servername, p->sharename, notify_type, notify_field));
649 return false;
652 #define SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(_data, _integer) \
653 _data->data.integer[0] = _integer; \
654 _data->data.integer[1] = 0;
657 #define SETUP_SPOOLSS_NOTIFY_DATA_STRING(_data, _p) \
658 _data->data.string.string = talloc_strdup(mem_ctx, _p); \
659 if (!_data->data.string.string) {\
660 _data->data.string.size = 0; \
662 _data->data.string.size = strlen_m_term(_p) * 2;
664 #define SETUP_SPOOLSS_NOTIFY_DATA_DEVMODE(_data, _devmode) \
665 _data->data.devmode.devmode = _devmode;
667 #define SETUP_SPOOLSS_NOTIFY_DATA_SECDESC(_data, _size, _sd) \
668 _data->data.sd.sd = dup_sec_desc(mem_ctx, _sd); \
669 if (!_data->data.sd.sd) { \
670 _data->data.sd.sd_size = 0; \
672 _data->data.sd.sd_size = _size;
674 static void init_systemtime_buffer(TALLOC_CTX *mem_ctx,
675 struct tm *t,
676 const char **pp,
677 uint32_t *plen)
679 struct spoolss_Time st;
680 uint32_t len = 16;
681 char *p;
683 if (!init_systemtime(&st, t)) {
684 return;
687 p = talloc_array(mem_ctx, char, len);
688 if (!p) {
689 return;
693 * Systemtime must be linearized as a set of UINT16's.
694 * Fix from Benjamin (Bj) Kuit bj@it.uts.edu.au
697 SSVAL(p, 0, st.year);
698 SSVAL(p, 2, st.month);
699 SSVAL(p, 4, st.day_of_week);
700 SSVAL(p, 6, st.day);
701 SSVAL(p, 8, st.hour);
702 SSVAL(p, 10, st.minute);
703 SSVAL(p, 12, st.second);
704 SSVAL(p, 14, st.millisecond);
706 *pp = p;
707 *plen = len;
710 /* Convert a notification message to a struct spoolss_Notify */
712 static void notify_one_value(struct spoolss_notify_msg *msg,
713 struct spoolss_Notify *data,
714 TALLOC_CTX *mem_ctx)
716 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, msg->notify.value[0]);
719 static void notify_string(struct spoolss_notify_msg *msg,
720 struct spoolss_Notify *data,
721 TALLOC_CTX *mem_ctx)
723 /* The length of the message includes the trailing \0 */
725 data->data.string.size = msg->len * 2;
726 data->data.string.string = talloc_strdup(mem_ctx, msg->notify.data);
727 if (!data->data.string.string) {
728 data->data.string.size = 0;
729 return;
733 static void notify_system_time(struct spoolss_notify_msg *msg,
734 struct spoolss_Notify *data,
735 TALLOC_CTX *mem_ctx)
737 data->data.string.string = NULL;
738 data->data.string.size = 0;
740 if (msg->len != sizeof(time_t)) {
741 DEBUG(5, ("notify_system_time: received wrong sized message (%d)\n",
742 msg->len));
743 return;
746 init_systemtime_buffer(mem_ctx, gmtime((time_t *)msg->notify.data),
747 &data->data.string.string,
748 &data->data.string.size);
751 struct notify2_message_table {
752 const char *name;
753 void (*fn)(struct spoolss_notify_msg *msg,
754 struct spoolss_Notify *data, TALLOC_CTX *mem_ctx);
757 static struct notify2_message_table printer_notify_table[] = {
758 /* 0x00 */ { "PRINTER_NOTIFY_FIELD_SERVER_NAME", notify_string },
759 /* 0x01 */ { "PRINTER_NOTIFY_FIELD_PRINTER_NAME", notify_string },
760 /* 0x02 */ { "PRINTER_NOTIFY_FIELD_SHARE_NAME", notify_string },
761 /* 0x03 */ { "PRINTER_NOTIFY_FIELD_PORT_NAME", notify_string },
762 /* 0x04 */ { "PRINTER_NOTIFY_FIELD_DRIVER_NAME", notify_string },
763 /* 0x05 */ { "PRINTER_NOTIFY_FIELD_COMMENT", notify_string },
764 /* 0x06 */ { "PRINTER_NOTIFY_FIELD_LOCATION", notify_string },
765 /* 0x07 */ { "PRINTER_NOTIFY_FIELD_DEVMODE", NULL },
766 /* 0x08 */ { "PRINTER_NOTIFY_FIELD_SEPFILE", notify_string },
767 /* 0x09 */ { "PRINTER_NOTIFY_FIELD_PRINT_PROCESSOR", notify_string },
768 /* 0x0a */ { "PRINTER_NOTIFY_FIELD_PARAMETERS", NULL },
769 /* 0x0b */ { "PRINTER_NOTIFY_FIELD_DATATYPE", notify_string },
770 /* 0x0c */ { "PRINTER_NOTIFY_FIELD_SECURITY_DESCRIPTOR", NULL },
771 /* 0x0d */ { "PRINTER_NOTIFY_FIELD_ATTRIBUTES", notify_one_value },
772 /* 0x0e */ { "PRINTER_NOTIFY_FIELD_PRIORITY", notify_one_value },
773 /* 0x0f */ { "PRINTER_NOTIFY_FIELD_DEFAULT_PRIORITY", NULL },
774 /* 0x10 */ { "PRINTER_NOTIFY_FIELD_START_TIME", NULL },
775 /* 0x11 */ { "PRINTER_NOTIFY_FIELD_UNTIL_TIME", NULL },
776 /* 0x12 */ { "PRINTER_NOTIFY_FIELD_STATUS", notify_one_value },
779 static struct notify2_message_table job_notify_table[] = {
780 /* 0x00 */ { "JOB_NOTIFY_FIELD_PRINTER_NAME", NULL },
781 /* 0x01 */ { "JOB_NOTIFY_FIELD_MACHINE_NAME", NULL },
782 /* 0x02 */ { "JOB_NOTIFY_FIELD_PORT_NAME", NULL },
783 /* 0x03 */ { "JOB_NOTIFY_FIELD_USER_NAME", notify_string },
784 /* 0x04 */ { "JOB_NOTIFY_FIELD_NOTIFY_NAME", NULL },
785 /* 0x05 */ { "JOB_NOTIFY_FIELD_DATATYPE", NULL },
786 /* 0x06 */ { "JOB_NOTIFY_FIELD_PRINT_PROCESSOR", NULL },
787 /* 0x07 */ { "JOB_NOTIFY_FIELD_PARAMETERS", NULL },
788 /* 0x08 */ { "JOB_NOTIFY_FIELD_DRIVER_NAME", NULL },
789 /* 0x09 */ { "JOB_NOTIFY_FIELD_DEVMODE", NULL },
790 /* 0x0a */ { "JOB_NOTIFY_FIELD_STATUS", notify_one_value },
791 /* 0x0b */ { "JOB_NOTIFY_FIELD_STATUS_STRING", NULL },
792 /* 0x0c */ { "JOB_NOTIFY_FIELD_SECURITY_DESCRIPTOR", NULL },
793 /* 0x0d */ { "JOB_NOTIFY_FIELD_DOCUMENT", notify_string },
794 /* 0x0e */ { "JOB_NOTIFY_FIELD_PRIORITY", NULL },
795 /* 0x0f */ { "JOB_NOTIFY_FIELD_POSITION", NULL },
796 /* 0x10 */ { "JOB_NOTIFY_FIELD_SUBMITTED", notify_system_time },
797 /* 0x11 */ { "JOB_NOTIFY_FIELD_START_TIME", NULL },
798 /* 0x12 */ { "JOB_NOTIFY_FIELD_UNTIL_TIME", NULL },
799 /* 0x13 */ { "JOB_NOTIFY_FIELD_TIME", NULL },
800 /* 0x14 */ { "JOB_NOTIFY_FIELD_TOTAL_PAGES", notify_one_value },
801 /* 0x15 */ { "JOB_NOTIFY_FIELD_PAGES_PRINTED", NULL },
802 /* 0x16 */ { "JOB_NOTIFY_FIELD_TOTAL_BYTES", notify_one_value },
803 /* 0x17 */ { "JOB_NOTIFY_FIELD_BYTES_PRINTED", NULL },
807 /***********************************************************************
808 Allocate talloc context for container object
809 **********************************************************************/
811 static void notify_msg_ctr_init( SPOOLSS_NOTIFY_MSG_CTR *ctr )
813 if ( !ctr )
814 return;
816 ctr->ctx = talloc_init("notify_msg_ctr_init %p", ctr);
818 return;
821 /***********************************************************************
822 release all allocated memory and zero out structure
823 **********************************************************************/
825 static void notify_msg_ctr_destroy( SPOOLSS_NOTIFY_MSG_CTR *ctr )
827 if ( !ctr )
828 return;
830 if ( ctr->ctx )
831 talloc_destroy(ctr->ctx);
833 ZERO_STRUCTP(ctr);
835 return;
838 /***********************************************************************
839 **********************************************************************/
841 static TALLOC_CTX* notify_ctr_getctx( SPOOLSS_NOTIFY_MSG_CTR *ctr )
843 if ( !ctr )
844 return NULL;
846 return ctr->ctx;
849 /***********************************************************************
850 **********************************************************************/
852 static SPOOLSS_NOTIFY_MSG_GROUP* notify_ctr_getgroup( SPOOLSS_NOTIFY_MSG_CTR *ctr, uint32_t idx )
854 if ( !ctr || !ctr->msg_groups )
855 return NULL;
857 if ( idx >= ctr->num_groups )
858 return NULL;
860 return &ctr->msg_groups[idx];
864 /***********************************************************************
865 How many groups of change messages do we have ?
866 **********************************************************************/
868 static int notify_msg_ctr_numgroups( SPOOLSS_NOTIFY_MSG_CTR *ctr )
870 if ( !ctr )
871 return 0;
873 return ctr->num_groups;
876 /***********************************************************************
877 Add a SPOOLSS_NOTIFY_MSG_CTR to the correct group
878 **********************************************************************/
880 static int notify_msg_ctr_addmsg( SPOOLSS_NOTIFY_MSG_CTR *ctr, SPOOLSS_NOTIFY_MSG *msg )
882 SPOOLSS_NOTIFY_MSG_GROUP *groups = NULL;
883 SPOOLSS_NOTIFY_MSG_GROUP *msg_grp = NULL;
884 SPOOLSS_NOTIFY_MSG *msg_list = NULL;
885 int i, new_slot;
887 if ( !ctr || !msg )
888 return 0;
890 /* loop over all groups looking for a matching printer name */
892 for ( i=0; i<ctr->num_groups; i++ ) {
893 if ( strcmp(ctr->msg_groups[i].printername, msg->printer) == 0 )
894 break;
897 /* add a new group? */
899 if ( i == ctr->num_groups ) {
900 ctr->num_groups++;
902 if ( !(groups = TALLOC_REALLOC_ARRAY( ctr->ctx, ctr->msg_groups, SPOOLSS_NOTIFY_MSG_GROUP, ctr->num_groups)) ) {
903 DEBUG(0,("notify_msg_ctr_addmsg: talloc_realloc() failed!\n"));
904 return 0;
906 ctr->msg_groups = groups;
908 /* clear the new entry and set the printer name */
910 ZERO_STRUCT( ctr->msg_groups[ctr->num_groups-1] );
911 fstrcpy( ctr->msg_groups[ctr->num_groups-1].printername, msg->printer );
914 /* add the change messages; 'i' is the correct index now regardless */
916 msg_grp = &ctr->msg_groups[i];
918 msg_grp->num_msgs++;
920 if ( !(msg_list = TALLOC_REALLOC_ARRAY( ctr->ctx, msg_grp->msgs, SPOOLSS_NOTIFY_MSG, msg_grp->num_msgs )) ) {
921 DEBUG(0,("notify_msg_ctr_addmsg: talloc_realloc() failed for new message [%d]!\n", msg_grp->num_msgs));
922 return 0;
924 msg_grp->msgs = msg_list;
926 new_slot = msg_grp->num_msgs-1;
927 memcpy( &msg_grp->msgs[new_slot], msg, sizeof(SPOOLSS_NOTIFY_MSG) );
929 /* need to allocate own copy of data */
931 if ( msg->len != 0 )
932 msg_grp->msgs[new_slot].notify.data = (char *)
933 TALLOC_MEMDUP( ctr->ctx, msg->notify.data, msg->len );
935 return ctr->num_groups;
938 /***********************************************************************
939 Send a change notication message on all handles which have a call
940 back registered
941 **********************************************************************/
943 static void send_notify2_changes( SPOOLSS_NOTIFY_MSG_CTR *ctr, uint32_t idx )
945 Printer_entry *p;
946 TALLOC_CTX *mem_ctx = notify_ctr_getctx( ctr );
947 SPOOLSS_NOTIFY_MSG_GROUP *msg_group = notify_ctr_getgroup( ctr, idx );
948 SPOOLSS_NOTIFY_MSG *messages;
949 int sending_msg_count;
951 if ( !msg_group ) {
952 DEBUG(5,("send_notify2_changes() called with no msg group!\n"));
953 return;
956 messages = msg_group->msgs;
958 if ( !messages ) {
959 DEBUG(5,("send_notify2_changes() called with no messages!\n"));
960 return;
963 DEBUG(8,("send_notify2_changes: Enter...[%s]\n", msg_group->printername));
965 /* loop over all printers */
967 for (p = printers_list; p; p = p->next) {
968 struct spoolss_Notify *notifies;
969 uint32_t count = 0;
970 uint32_t id;
971 int i;
973 /* Is there notification on this handle? */
975 if ( !p->notify.client_connected )
976 continue;
978 DEBUG(10,("Client connected! [\\\\%s\\%s]\n", p->servername, p->sharename));
980 /* For this printer? Print servers always receive
981 notifications. */
983 if ( ( p->printer_type == SPLHND_PRINTER ) &&
984 ( !strequal(msg_group->printername, p->sharename) ) )
985 continue;
987 DEBUG(10,("Our printer\n"));
989 /* allocate the max entries possible */
991 notifies = TALLOC_ZERO_ARRAY(mem_ctx, struct spoolss_Notify, msg_group->num_msgs);
992 if (!notifies) {
993 return;
996 /* build the array of change notifications */
998 sending_msg_count = 0;
1000 for ( i=0; i<msg_group->num_msgs; i++ ) {
1001 SPOOLSS_NOTIFY_MSG *msg = &messages[i];
1003 /* Are we monitoring this event? */
1005 if (!is_monitoring_event(p, msg->type, msg->field))
1006 continue;
1008 sending_msg_count++;
1011 DEBUG(10,("process_notify2_message: Sending message type [0x%x] field [0x%2x] for printer [%s]\n",
1012 msg->type, msg->field, p->sharename));
1015 * if the is a printer notification handle and not a job notification
1016 * type, then set the id to 0. Other wise just use what was specified
1017 * in the message.
1019 * When registering change notification on a print server handle
1020 * we always need to send back the id (snum) matching the printer
1021 * for which the change took place. For change notify registered
1022 * on a printer handle, this does not matter and the id should be 0.
1024 * --jerry
1027 if ( ( p->printer_type == SPLHND_PRINTER ) && ( msg->type == PRINTER_NOTIFY_TYPE ) )
1028 id = 0;
1029 else
1030 id = msg->id;
1033 /* Convert unix jobid to smb jobid */
1035 if (msg->flags & SPOOLSS_NOTIFY_MSG_UNIX_JOBID) {
1036 id = sysjob_to_jobid(msg->id);
1038 if (id == -1) {
1039 DEBUG(3, ("no such unix jobid %d\n", msg->id));
1040 goto done;
1044 construct_info_data( &notifies[count], msg->type, msg->field, id );
1046 switch(msg->type) {
1047 case PRINTER_NOTIFY_TYPE:
1048 if ( printer_notify_table[msg->field].fn )
1049 printer_notify_table[msg->field].fn(msg, &notifies[count], mem_ctx);
1050 break;
1052 case JOB_NOTIFY_TYPE:
1053 if ( job_notify_table[msg->field].fn )
1054 job_notify_table[msg->field].fn(msg, &notifies[count], mem_ctx);
1055 break;
1057 default:
1058 DEBUG(5, ("Unknown notification type %d\n", msg->type));
1059 goto done;
1062 count++;
1065 if ( sending_msg_count ) {
1066 NTSTATUS status;
1067 WERROR werr;
1068 union spoolss_ReplyPrinterInfo info;
1069 struct spoolss_NotifyInfo info0;
1070 uint32_t reply_result;
1072 info0.version = 0x2;
1073 info0.flags = count ? 0x00020000 /* ??? */ : PRINTER_NOTIFY_INFO_DISCARDED;
1074 info0.count = count;
1075 info0.notifies = notifies;
1077 info.info0 = &info0;
1079 status = rpccli_spoolss_RouterReplyPrinterEx(notify_cli_pipe, mem_ctx,
1080 &p->notify.client_hnd,
1081 p->notify.change, /* color */
1082 p->notify.flags,
1083 &reply_result,
1084 0, /* reply_type, must be 0 */
1085 info,
1086 &werr);
1087 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(werr)) {
1088 DEBUG(1,("RouterReplyPrinterEx to client: %s failed: %s\n",
1089 notify_cli_pipe->srv_name_slash,
1090 win_errstr(werr)));
1092 switch (reply_result) {
1093 case 0:
1094 break;
1095 case PRINTER_NOTIFY_INFO_DISCARDED:
1096 case PRINTER_NOTIFY_INFO_DISCARDNOTED:
1097 case PRINTER_NOTIFY_INFO_COLOR_MISMATCH:
1098 break;
1099 default:
1100 break;
1105 done:
1106 DEBUG(8,("send_notify2_changes: Exit...\n"));
1107 return;
1110 /***********************************************************************
1111 **********************************************************************/
1113 static bool notify2_unpack_msg( SPOOLSS_NOTIFY_MSG *msg, struct timeval *tv, void *buf, size_t len )
1116 uint32_t tv_sec, tv_usec;
1117 size_t offset = 0;
1119 /* Unpack message */
1121 offset += tdb_unpack((uint8_t *)buf + offset, len - offset, "f",
1122 msg->printer);
1124 offset += tdb_unpack((uint8_t *)buf + offset, len - offset, "ddddddd",
1125 &tv_sec, &tv_usec,
1126 &msg->type, &msg->field, &msg->id, &msg->len, &msg->flags);
1128 if (msg->len == 0)
1129 tdb_unpack((uint8_t *)buf + offset, len - offset, "dd",
1130 &msg->notify.value[0], &msg->notify.value[1]);
1131 else
1132 tdb_unpack((uint8_t *)buf + offset, len - offset, "B",
1133 &msg->len, &msg->notify.data);
1135 DEBUG(3, ("notify2_unpack_msg: got NOTIFY2 message for printer %s, jobid %u type %d, field 0x%02x, flags 0x%04x\n",
1136 msg->printer, (unsigned int)msg->id, msg->type, msg->field, msg->flags));
1138 tv->tv_sec = tv_sec;
1139 tv->tv_usec = tv_usec;
1141 if (msg->len == 0)
1142 DEBUG(3, ("notify2_unpack_msg: value1 = %d, value2 = %d\n", msg->notify.value[0],
1143 msg->notify.value[1]));
1144 else
1145 dump_data(3, (uint8_t *)msg->notify.data, msg->len);
1147 return true;
1150 /********************************************************************
1151 Receive a notify2 message list
1152 ********************************************************************/
1154 static void receive_notify2_message_list(struct messaging_context *msg,
1155 void *private_data,
1156 uint32_t msg_type,
1157 struct server_id server_id,
1158 DATA_BLOB *data)
1160 size_t msg_count, i;
1161 char *buf = (char *)data->data;
1162 char *msg_ptr;
1163 size_t msg_len;
1164 SPOOLSS_NOTIFY_MSG notify;
1165 SPOOLSS_NOTIFY_MSG_CTR messages;
1166 int num_groups;
1168 if (data->length < 4) {
1169 DEBUG(0,("receive_notify2_message_list: bad message format (len < 4)!\n"));
1170 return;
1173 msg_count = IVAL(buf, 0);
1174 msg_ptr = buf + 4;
1176 DEBUG(5, ("receive_notify2_message_list: got %lu messages in list\n", (unsigned long)msg_count));
1178 if (msg_count == 0) {
1179 DEBUG(0,("receive_notify2_message_list: bad message format (msg_count == 0) !\n"));
1180 return;
1183 /* initialize the container */
1185 ZERO_STRUCT( messages );
1186 notify_msg_ctr_init( &messages );
1189 * build message groups for each printer identified
1190 * in a change_notify msg. Remember that a PCN message
1191 * includes the handle returned for the srv_spoolss_replyopenprinter()
1192 * call. Therefore messages are grouped according to printer handle.
1195 for ( i=0; i<msg_count; i++ ) {
1196 struct timeval msg_tv;
1198 if (msg_ptr + 4 - buf > data->length) {
1199 DEBUG(0,("receive_notify2_message_list: bad message format (len > buf_size) !\n"));
1200 return;
1203 msg_len = IVAL(msg_ptr,0);
1204 msg_ptr += 4;
1206 if (msg_ptr + msg_len - buf > data->length) {
1207 DEBUG(0,("receive_notify2_message_list: bad message format (bad len) !\n"));
1208 return;
1211 /* unpack messages */
1213 ZERO_STRUCT( notify );
1214 notify2_unpack_msg( &notify, &msg_tv, msg_ptr, msg_len );
1215 msg_ptr += msg_len;
1217 /* add to correct list in container */
1219 notify_msg_ctr_addmsg( &messages, &notify );
1221 /* free memory that might have been allocated by notify2_unpack_msg() */
1223 if ( notify.len != 0 )
1224 SAFE_FREE( notify.notify.data );
1227 /* process each group of messages */
1229 num_groups = notify_msg_ctr_numgroups( &messages );
1230 for ( i=0; i<num_groups; i++ )
1231 send_notify2_changes( &messages, i );
1234 /* cleanup */
1236 DEBUG(10,("receive_notify2_message_list: processed %u messages\n",
1237 (uint32_t)msg_count ));
1239 notify_msg_ctr_destroy( &messages );
1241 return;
1244 /********************************************************************
1245 Send a message to ourself about new driver being installed
1246 so we can upgrade the information for each printer bound to this
1247 driver
1248 ********************************************************************/
1250 static bool srv_spoolss_drv_upgrade_printer(const char *drivername)
1252 int len = strlen(drivername);
1254 if (!len)
1255 return false;
1257 DEBUG(10,("srv_spoolss_drv_upgrade_printer: Sending message about driver upgrade [%s]\n",
1258 drivername));
1260 messaging_send_buf(smbd_messaging_context(), procid_self(),
1261 MSG_PRINTER_DRVUPGRADE,
1262 (uint8_t *)drivername, len+1);
1264 return true;
1267 /**********************************************************************
1268 callback to receive a MSG_PRINTER_DRVUPGRADE message and interate
1269 over all printers, upgrading ones as necessary
1270 **********************************************************************/
1272 void do_drv_upgrade_printer(struct messaging_context *msg,
1273 void *private_data,
1274 uint32_t msg_type,
1275 struct server_id server_id,
1276 DATA_BLOB *data)
1278 fstring drivername;
1279 int snum;
1280 int n_services = lp_numservices();
1281 size_t len;
1283 len = MIN(data->length,sizeof(drivername)-1);
1284 strncpy(drivername, (const char *)data->data, len);
1286 DEBUG(10,("do_drv_upgrade_printer: Got message for new driver [%s]\n", drivername ));
1288 /* Iterate the printer list */
1290 for (snum=0; snum<n_services; snum++)
1292 if (lp_snum_ok(snum) && lp_print_ok(snum) )
1294 WERROR result;
1295 NT_PRINTER_INFO_LEVEL *printer = NULL;
1297 result = get_a_printer(NULL, &printer, 2, lp_const_servicename(snum));
1298 if (!W_ERROR_IS_OK(result))
1299 continue;
1301 if (printer && printer->info_2 && !strcmp(drivername, printer->info_2->drivername))
1303 DEBUG(6,("Updating printer [%s]\n", printer->info_2->printername));
1305 /* all we care about currently is the change_id */
1307 result = mod_a_printer(printer, 2);
1308 if (!W_ERROR_IS_OK(result)) {
1309 DEBUG(3,("do_drv_upgrade_printer: mod_a_printer() failed with status [%s]\n",
1310 win_errstr(result)));
1314 free_a_printer(&printer, 2);
1318 /* all done */
1321 /********************************************************************
1322 Update the cache for all printq's with a registered client
1323 connection
1324 ********************************************************************/
1326 void update_monitored_printq_cache( void )
1328 Printer_entry *printer = printers_list;
1329 int snum;
1331 /* loop through all printers and update the cache where
1332 client_connected == true */
1333 while ( printer )
1335 if ( (printer->printer_type == SPLHND_PRINTER)
1336 && printer->notify.client_connected )
1338 snum = print_queue_snum(printer->sharename);
1339 print_queue_status( snum, NULL, NULL );
1342 printer = printer->next;
1345 return;
1347 /********************************************************************
1348 Send a message to ourself about new driver being installed
1349 so we can upgrade the information for each printer bound to this
1350 driver
1351 ********************************************************************/
1353 static bool srv_spoolss_reset_printerdata(char* drivername)
1355 int len = strlen(drivername);
1357 if (!len)
1358 return false;
1360 DEBUG(10,("srv_spoolss_reset_printerdata: Sending message about resetting printerdata [%s]\n",
1361 drivername));
1363 messaging_send_buf(smbd_messaging_context(), procid_self(),
1364 MSG_PRINTERDATA_INIT_RESET,
1365 (uint8_t *)drivername, len+1);
1367 return true;
1370 /**********************************************************************
1371 callback to receive a MSG_PRINTERDATA_INIT_RESET message and interate
1372 over all printers, resetting printer data as neessary
1373 **********************************************************************/
1375 void reset_all_printerdata(struct messaging_context *msg,
1376 void *private_data,
1377 uint32_t msg_type,
1378 struct server_id server_id,
1379 DATA_BLOB *data)
1381 fstring drivername;
1382 int snum;
1383 int n_services = lp_numservices();
1384 size_t len;
1386 len = MIN( data->length, sizeof(drivername)-1 );
1387 strncpy( drivername, (const char *)data->data, len );
1389 DEBUG(10,("reset_all_printerdata: Got message for new driver [%s]\n", drivername ));
1391 /* Iterate the printer list */
1393 for ( snum=0; snum<n_services; snum++ )
1395 if ( lp_snum_ok(snum) && lp_print_ok(snum) )
1397 WERROR result;
1398 NT_PRINTER_INFO_LEVEL *printer = NULL;
1400 result = get_a_printer( NULL, &printer, 2, lp_const_servicename(snum) );
1401 if ( !W_ERROR_IS_OK(result) )
1402 continue;
1405 * if the printer is bound to the driver,
1406 * then reset to the new driver initdata
1409 if ( printer && printer->info_2 && !strcmp(drivername, printer->info_2->drivername) )
1411 DEBUG(6,("reset_all_printerdata: Updating printer [%s]\n", printer->info_2->printername));
1413 if ( !set_driver_init(printer, 2) ) {
1414 DEBUG(5,("reset_all_printerdata: Error resetting printer data for printer [%s], driver [%s]!\n",
1415 printer->info_2->printername, printer->info_2->drivername));
1418 result = mod_a_printer( printer, 2 );
1419 if ( !W_ERROR_IS_OK(result) ) {
1420 DEBUG(3,("reset_all_printerdata: mod_a_printer() failed! (%s)\n",
1421 get_dos_error_msg(result)));
1425 free_a_printer( &printer, 2 );
1429 /* all done */
1431 return;
1434 /****************************************************************
1435 _spoolss_OpenPrinter
1436 ****************************************************************/
1438 WERROR _spoolss_OpenPrinter(pipes_struct *p,
1439 struct spoolss_OpenPrinter *r)
1441 struct spoolss_OpenPrinterEx e;
1442 WERROR werr;
1444 ZERO_STRUCT(e.in.userlevel);
1446 e.in.printername = r->in.printername;
1447 e.in.datatype = r->in.datatype;
1448 e.in.devmode_ctr = r->in.devmode_ctr;
1449 e.in.access_mask = r->in.access_mask;
1450 e.in.level = 0;
1452 e.out.handle = r->out.handle;
1454 werr = _spoolss_OpenPrinterEx(p, &e);
1456 if (W_ERROR_EQUAL(werr, WERR_INVALID_PARAM)) {
1457 /* OpenPrinterEx returns this for a bad
1458 * printer name. We must return WERR_INVALID_PRINTER_NAME
1459 * instead.
1461 werr = WERR_INVALID_PRINTER_NAME;
1464 return werr;
1467 /********************************************************************
1468 ********************************************************************/
1470 bool convert_devicemode(const char *printername,
1471 const struct spoolss_DeviceMode *devmode,
1472 NT_DEVICEMODE **pp_nt_devmode)
1474 NT_DEVICEMODE *nt_devmode = *pp_nt_devmode;
1477 * Ensure nt_devmode is a valid pointer
1478 * as we will be overwriting it.
1481 if (nt_devmode == NULL) {
1482 DEBUG(5, ("convert_devicemode: allocating a generic devmode\n"));
1483 if ((nt_devmode = construct_nt_devicemode(printername)) == NULL)
1484 return false;
1487 fstrcpy(nt_devmode->devicename, devmode->devicename);
1488 fstrcpy(nt_devmode->formname, devmode->formname);
1490 nt_devmode->devicename[31] = '\0';
1491 nt_devmode->formname[31] = '\0';
1493 nt_devmode->specversion = devmode->specversion;
1494 nt_devmode->driverversion = devmode->driverversion;
1495 nt_devmode->size = devmode->size;
1496 nt_devmode->fields = devmode->fields;
1497 nt_devmode->orientation = devmode->orientation;
1498 nt_devmode->papersize = devmode->papersize;
1499 nt_devmode->paperlength = devmode->paperlength;
1500 nt_devmode->paperwidth = devmode->paperwidth;
1501 nt_devmode->scale = devmode->scale;
1502 nt_devmode->copies = devmode->copies;
1503 nt_devmode->defaultsource = devmode->defaultsource;
1504 nt_devmode->printquality = devmode->printquality;
1505 nt_devmode->color = devmode->color;
1506 nt_devmode->duplex = devmode->duplex;
1507 nt_devmode->yresolution = devmode->yresolution;
1508 nt_devmode->ttoption = devmode->ttoption;
1509 nt_devmode->collate = devmode->collate;
1511 nt_devmode->logpixels = devmode->logpixels;
1512 nt_devmode->bitsperpel = devmode->bitsperpel;
1513 nt_devmode->pelswidth = devmode->pelswidth;
1514 nt_devmode->pelsheight = devmode->pelsheight;
1515 nt_devmode->displayflags = devmode->displayflags;
1516 nt_devmode->displayfrequency = devmode->displayfrequency;
1517 nt_devmode->icmmethod = devmode->icmmethod;
1518 nt_devmode->icmintent = devmode->icmintent;
1519 nt_devmode->mediatype = devmode->mediatype;
1520 nt_devmode->dithertype = devmode->dithertype;
1521 nt_devmode->reserved1 = devmode->reserved1;
1522 nt_devmode->reserved2 = devmode->reserved2;
1523 nt_devmode->panningwidth = devmode->panningwidth;
1524 nt_devmode->panningheight = devmode->panningheight;
1527 * Only change private and driverextra if the incoming devmode
1528 * has a new one. JRA.
1531 if ((devmode->__driverextra_length != 0) && (devmode->driverextra_data.data != NULL)) {
1532 SAFE_FREE(nt_devmode->nt_dev_private);
1533 nt_devmode->driverextra = devmode->__driverextra_length;
1534 if((nt_devmode->nt_dev_private = SMB_MALLOC_ARRAY(uint8_t, nt_devmode->driverextra)) == NULL)
1535 return false;
1536 memcpy(nt_devmode->nt_dev_private, devmode->driverextra_data.data, nt_devmode->driverextra);
1539 *pp_nt_devmode = nt_devmode;
1541 return true;
1544 /****************************************************************
1545 _spoolss_OpenPrinterEx
1546 ****************************************************************/
1548 WERROR _spoolss_OpenPrinterEx(pipes_struct *p,
1549 struct spoolss_OpenPrinterEx *r)
1551 int snum;
1552 Printer_entry *Printer=NULL;
1554 if (!r->in.printername) {
1555 return WERR_INVALID_PARAM;
1558 /* some sanity check because you can open a printer or a print server */
1559 /* aka: \\server\printer or \\server */
1561 DEBUGADD(3,("checking name: %s\n", r->in.printername));
1563 if (!open_printer_hnd(p, r->out.handle, r->in.printername, 0)) {
1564 ZERO_STRUCTP(r->out.handle);
1565 return WERR_INVALID_PARAM;
1568 Printer = find_printer_index_by_hnd(p, r->out.handle);
1569 if ( !Printer ) {
1570 DEBUG(0,("_spoolss_OpenPrinterEx: logic error. Can't find printer "
1571 "handle we created for printer %s\n", r->in.printername));
1572 close_printer_handle(p, r->out.handle);
1573 ZERO_STRUCTP(r->out.handle);
1574 return WERR_INVALID_PARAM;
1578 * First case: the user is opening the print server:
1580 * Disallow MS AddPrinterWizard if parameter disables it. A Win2k
1581 * client 1st tries an OpenPrinterEx with access==0, MUST be allowed.
1583 * Then both Win2k and WinNT clients try an OpenPrinterEx with
1584 * SERVER_ALL_ACCESS, which we allow only if the user is root (uid=0)
1585 * or if the user is listed in the smb.conf printer admin parameter.
1587 * Then they try OpenPrinterEx with SERVER_READ which we allow. This lets the
1588 * client view printer folder, but does not show the MSAPW.
1590 * Note: this test needs code to check access rights here too. Jeremy
1591 * could you look at this?
1593 * Second case: the user is opening a printer:
1594 * NT doesn't let us connect to a printer if the connecting user
1595 * doesn't have print permission.
1597 * Third case: user is opening a Port Monitor
1598 * access checks same as opening a handle to the print server.
1601 switch (Printer->printer_type )
1603 case SPLHND_SERVER:
1604 case SPLHND_PORTMON_TCP:
1605 case SPLHND_PORTMON_LOCAL:
1606 /* Printserver handles use global struct... */
1608 snum = -1;
1610 /* Map standard access rights to object specific access rights */
1612 se_map_standard(&r->in.access_mask,
1613 &printserver_std_mapping);
1615 /* Deny any object specific bits that don't apply to print
1616 servers (i.e printer and job specific bits) */
1618 r->in.access_mask &= SPECIFIC_RIGHTS_MASK;
1620 if (r->in.access_mask &
1621 ~(SERVER_ACCESS_ADMINISTER | SERVER_ACCESS_ENUMERATE)) {
1622 DEBUG(3, ("access DENIED for non-printserver bits\n"));
1623 close_printer_handle(p, r->out.handle);
1624 ZERO_STRUCTP(r->out.handle);
1625 return WERR_ACCESS_DENIED;
1628 /* Allow admin access */
1630 if ( r->in.access_mask & SERVER_ACCESS_ADMINISTER )
1632 SE_PRIV se_printop = SE_PRINT_OPERATOR;
1634 if (!lp_ms_add_printer_wizard()) {
1635 close_printer_handle(p, r->out.handle);
1636 ZERO_STRUCTP(r->out.handle);
1637 return WERR_ACCESS_DENIED;
1640 /* if the user is not root, doesn't have SE_PRINT_OPERATOR privilege,
1641 and not a printer admin, then fail */
1643 if ((p->server_info->utok.uid != sec_initial_uid()) &&
1644 !user_has_privileges(p->server_info->ptok,
1645 &se_printop ) &&
1646 !token_contains_name_in_list(
1647 uidtoname(p->server_info->utok.uid),
1648 NULL, NULL,
1649 p->server_info->ptok,
1650 lp_printer_admin(snum))) {
1651 close_printer_handle(p, r->out.handle);
1652 ZERO_STRUCTP(r->out.handle);
1653 return WERR_ACCESS_DENIED;
1656 r->in.access_mask = SERVER_ACCESS_ADMINISTER;
1658 else
1660 r->in.access_mask = SERVER_ACCESS_ENUMERATE;
1663 DEBUG(4,("Setting print server access = %s\n", (r->in.access_mask == SERVER_ACCESS_ADMINISTER)
1664 ? "SERVER_ACCESS_ADMINISTER" : "SERVER_ACCESS_ENUMERATE" ));
1666 /* We fall through to return WERR_OK */
1667 break;
1669 case SPLHND_PRINTER:
1670 /* NT doesn't let us connect to a printer if the connecting user
1671 doesn't have print permission. */
1673 if (!get_printer_snum(p, r->out.handle, &snum, NULL)) {
1674 close_printer_handle(p, r->out.handle);
1675 ZERO_STRUCTP(r->out.handle);
1676 return WERR_BADFID;
1679 if (r->in.access_mask == SEC_FLAG_MAXIMUM_ALLOWED) {
1680 r->in.access_mask = PRINTER_ACCESS_ADMINISTER;
1683 se_map_standard(&r->in.access_mask, &printer_std_mapping);
1685 /* map an empty access mask to the minimum access mask */
1686 if (r->in.access_mask == 0x0)
1687 r->in.access_mask = PRINTER_ACCESS_USE;
1690 * If we are not serving the printer driver for this printer,
1691 * map PRINTER_ACCESS_ADMINISTER to PRINTER_ACCESS_USE. This
1692 * will keep NT clients happy --jerry
1695 if (lp_use_client_driver(snum)
1696 && (r->in.access_mask & PRINTER_ACCESS_ADMINISTER))
1698 r->in.access_mask = PRINTER_ACCESS_USE;
1701 /* check smb.conf parameters and the the sec_desc */
1703 if ( !check_access(get_client_fd(), lp_hostsallow(snum), lp_hostsdeny(snum)) ) {
1704 DEBUG(3, ("access DENIED (hosts allow/deny) for printer open\n"));
1705 ZERO_STRUCTP(r->out.handle);
1706 return WERR_ACCESS_DENIED;
1709 if (!user_ok_token(uidtoname(p->server_info->utok.uid), NULL,
1710 p->server_info->ptok, snum) ||
1711 !print_access_check(p->server_info, snum,
1712 r->in.access_mask)) {
1713 DEBUG(3, ("access DENIED for printer open\n"));
1714 close_printer_handle(p, r->out.handle);
1715 ZERO_STRUCTP(r->out.handle);
1716 return WERR_ACCESS_DENIED;
1719 if ((r->in.access_mask & SPECIFIC_RIGHTS_MASK)& ~(PRINTER_ACCESS_ADMINISTER|PRINTER_ACCESS_USE)) {
1720 DEBUG(3, ("access DENIED for printer open - unknown bits\n"));
1721 close_printer_handle(p, r->out.handle);
1722 ZERO_STRUCTP(r->out.handle);
1723 return WERR_ACCESS_DENIED;
1726 if (r->in.access_mask & PRINTER_ACCESS_ADMINISTER)
1727 r->in.access_mask = PRINTER_ACCESS_ADMINISTER;
1728 else
1729 r->in.access_mask = PRINTER_ACCESS_USE;
1731 DEBUG(4,("Setting printer access = %s\n", (r->in.access_mask == PRINTER_ACCESS_ADMINISTER)
1732 ? "PRINTER_ACCESS_ADMINISTER" : "PRINTER_ACCESS_USE" ));
1734 break;
1736 default:
1737 /* sanity check to prevent programmer error */
1738 ZERO_STRUCTP(r->out.handle);
1739 return WERR_BADFID;
1742 Printer->access_granted = r->in.access_mask;
1745 * If the client sent a devmode in the OpenPrinter() call, then
1746 * save it here in case we get a job submission on this handle
1749 if ((Printer->printer_type != SPLHND_SERVER) &&
1750 r->in.devmode_ctr.devmode) {
1751 convert_devicemode(Printer->sharename,
1752 r->in.devmode_ctr.devmode,
1753 &Printer->nt_devmode);
1756 #if 0 /* JERRY -- I'm doubtful this is really effective */
1757 /* HACK ALERT!!! Sleep for 1/3 of a second to try trigger a LAN/WAN
1758 optimization in Windows 2000 clients --jerry */
1760 if ( (r->in.access_mask == PRINTER_ACCESS_ADMINISTER)
1761 && (RA_WIN2K == get_remote_arch()) )
1763 DEBUG(10,("_spoolss_OpenPrinterEx: Enabling LAN/WAN hack for Win2k clients.\n"));
1764 sys_usleep( 500000 );
1766 #endif
1768 return WERR_OK;
1771 /****************************************************************************
1772 ****************************************************************************/
1774 static bool printer_info2_to_nt_printer_info2(struct spoolss_SetPrinterInfo2 *r,
1775 NT_PRINTER_INFO_LEVEL_2 *d)
1777 DEBUG(7,("printer_info2_to_nt_printer_info2\n"));
1779 if (!r || !d) {
1780 return false;
1783 d->attributes = r->attributes;
1784 d->priority = r->priority;
1785 d->default_priority = r->defaultpriority;
1786 d->starttime = r->starttime;
1787 d->untiltime = r->untiltime;
1788 d->status = r->status;
1789 d->cjobs = r->cjobs;
1791 fstrcpy(d->servername, r->servername);
1792 fstrcpy(d->printername, r->printername);
1793 fstrcpy(d->sharename, r->sharename);
1794 fstrcpy(d->portname, r->portname);
1795 fstrcpy(d->drivername, r->drivername);
1796 slprintf(d->comment, sizeof(d->comment)-1, "%s", r->comment);
1797 fstrcpy(d->location, r->location);
1798 fstrcpy(d->sepfile, r->sepfile);
1799 fstrcpy(d->printprocessor, r->printprocessor);
1800 fstrcpy(d->datatype, r->datatype);
1801 fstrcpy(d->parameters, r->parameters);
1803 return true;
1806 /****************************************************************************
1807 ****************************************************************************/
1809 static bool convert_printer_info(struct spoolss_SetPrinterInfoCtr *info_ctr,
1810 NT_PRINTER_INFO_LEVEL *printer)
1812 bool ret;
1814 switch (info_ctr->level) {
1815 case 2:
1816 /* allocate memory if needed. Messy because
1817 convert_printer_info is used to update an existing
1818 printer or build a new one */
1820 if (!printer->info_2) {
1821 printer->info_2 = TALLOC_ZERO_P(printer, NT_PRINTER_INFO_LEVEL_2);
1822 if (!printer->info_2) {
1823 DEBUG(0,("convert_printer_info: "
1824 "talloc() failed!\n"));
1825 return false;
1829 ret = printer_info2_to_nt_printer_info2(info_ctr->info.info2,
1830 printer->info_2);
1831 printer->info_2->setuptime = time(NULL);
1832 return ret;
1835 return false;
1838 /****************************************************************
1839 _spoolss_ClosePrinter
1840 ****************************************************************/
1842 WERROR _spoolss_ClosePrinter(pipes_struct *p,
1843 struct spoolss_ClosePrinter *r)
1845 Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
1847 if (Printer && Printer->document_started) {
1848 struct spoolss_EndDocPrinter e;
1850 e.in.handle = r->in.handle;
1852 _spoolss_EndDocPrinter(p, &e);
1855 if (!close_printer_handle(p, r->in.handle))
1856 return WERR_BADFID;
1858 /* clear the returned printer handle. Observed behavior
1859 from Win2k server. Don't think this really matters.
1860 Previous code just copied the value of the closed
1861 handle. --jerry */
1863 ZERO_STRUCTP(r->out.handle);
1865 return WERR_OK;
1868 /****************************************************************
1869 _spoolss_DeletePrinter
1870 ****************************************************************/
1872 WERROR _spoolss_DeletePrinter(pipes_struct *p,
1873 struct spoolss_DeletePrinter *r)
1875 Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
1876 WERROR result;
1878 if (Printer && Printer->document_started) {
1879 struct spoolss_EndDocPrinter e;
1881 e.in.handle = r->in.handle;
1883 _spoolss_EndDocPrinter(p, &e);
1886 result = delete_printer_handle(p, r->in.handle);
1888 update_c_setprinter(false);
1890 return result;
1893 /*******************************************************************
1894 * static function to lookup the version id corresponding to an
1895 * long architecture string
1896 ******************************************************************/
1898 static const struct print_architecture_table_node archi_table[]= {
1900 {"Windows 4.0", SPL_ARCH_WIN40, 0 },
1901 {"Windows NT x86", SPL_ARCH_W32X86, 2 },
1902 {"Windows NT R4000", SPL_ARCH_W32MIPS, 2 },
1903 {"Windows NT Alpha_AXP", SPL_ARCH_W32ALPHA, 2 },
1904 {"Windows NT PowerPC", SPL_ARCH_W32PPC, 2 },
1905 {"Windows IA64", SPL_ARCH_IA64, 3 },
1906 {"Windows x64", SPL_ARCH_X64, 3 },
1907 {NULL, "", -1 }
1910 static int get_version_id(const char *arch)
1912 int i;
1914 for (i=0; archi_table[i].long_archi != NULL; i++)
1916 if (strcmp(arch, archi_table[i].long_archi) == 0)
1917 return (archi_table[i].version);
1920 return -1;
1923 /****************************************************************
1924 _spoolss_DeletePrinterDriver
1925 ****************************************************************/
1927 WERROR _spoolss_DeletePrinterDriver(pipes_struct *p,
1928 struct spoolss_DeletePrinterDriver *r)
1931 struct spoolss_DriverInfo8 *info = NULL;
1932 struct spoolss_DriverInfo8 *info_win2k = NULL;
1933 int version;
1934 WERROR status;
1935 WERROR status_win2k = WERR_ACCESS_DENIED;
1936 SE_PRIV se_printop = SE_PRINT_OPERATOR;
1938 /* if the user is not root, doesn't have SE_PRINT_OPERATOR privilege,
1939 and not a printer admin, then fail */
1941 if ( (p->server_info->utok.uid != sec_initial_uid())
1942 && !user_has_privileges(p->server_info->ptok, &se_printop )
1943 && !token_contains_name_in_list(
1944 uidtoname(p->server_info->utok.uid), NULL,
1945 NULL, p->server_info->ptok,
1946 lp_printer_admin(-1)) )
1948 return WERR_ACCESS_DENIED;
1951 /* check that we have a valid driver name first */
1953 if ((version = get_version_id(r->in.architecture)) == -1)
1954 return WERR_INVALID_ENVIRONMENT;
1956 if (!W_ERROR_IS_OK(get_a_printer_driver(p->mem_ctx, &info, r->in.driver,
1957 r->in.architecture,
1958 version)))
1960 /* try for Win2k driver if "Windows NT x86" */
1962 if ( version == 2 ) {
1963 version = 3;
1964 if (!W_ERROR_IS_OK(get_a_printer_driver(p->mem_ctx,
1965 &info,
1966 r->in.driver,
1967 r->in.architecture,
1968 version))) {
1969 status = WERR_UNKNOWN_PRINTER_DRIVER;
1970 goto done;
1973 /* otherwise it was a failure */
1974 else {
1975 status = WERR_UNKNOWN_PRINTER_DRIVER;
1976 goto done;
1981 if (printer_driver_in_use(info)) {
1982 status = WERR_PRINTER_DRIVER_IN_USE;
1983 goto done;
1986 if ( version == 2 )
1988 if (W_ERROR_IS_OK(get_a_printer_driver(p->mem_ctx,
1989 &info_win2k,
1990 r->in.driver,
1991 r->in.architecture, 3)))
1993 /* if we get to here, we now have 2 driver info structures to remove */
1994 /* remove the Win2k driver first*/
1996 status_win2k = delete_printer_driver(
1997 p, info_win2k, 3, false);
1998 free_a_printer_driver(info_win2k);
2000 /* this should not have failed---if it did, report to client */
2001 if ( !W_ERROR_IS_OK(status_win2k) )
2003 status = status_win2k;
2004 goto done;
2009 status = delete_printer_driver(p, info, version, false);
2011 /* if at least one of the deletes succeeded return OK */
2013 if ( W_ERROR_IS_OK(status) || W_ERROR_IS_OK(status_win2k) )
2014 status = WERR_OK;
2016 done:
2017 free_a_printer_driver(info);
2019 return status;
2022 /****************************************************************
2023 _spoolss_DeletePrinterDriverEx
2024 ****************************************************************/
2026 WERROR _spoolss_DeletePrinterDriverEx(pipes_struct *p,
2027 struct spoolss_DeletePrinterDriverEx *r)
2029 struct spoolss_DriverInfo8 *info = NULL;
2030 struct spoolss_DriverInfo8 *info_win2k = NULL;
2031 int version;
2032 bool delete_files;
2033 WERROR status;
2034 WERROR status_win2k = WERR_ACCESS_DENIED;
2035 SE_PRIV se_printop = SE_PRINT_OPERATOR;
2037 /* if the user is not root, doesn't have SE_PRINT_OPERATOR privilege,
2038 and not a printer admin, then fail */
2040 if ( (p->server_info->utok.uid != sec_initial_uid())
2041 && !user_has_privileges(p->server_info->ptok, &se_printop )
2042 && !token_contains_name_in_list(
2043 uidtoname(p->server_info->utok.uid), NULL, NULL,
2044 p->server_info->ptok, lp_printer_admin(-1)) )
2046 return WERR_ACCESS_DENIED;
2049 /* check that we have a valid driver name first */
2050 if ((version = get_version_id(r->in.architecture)) == -1) {
2051 /* this is what NT returns */
2052 return WERR_INVALID_ENVIRONMENT;
2055 if (r->in.delete_flags & DPD_DELETE_SPECIFIC_VERSION)
2056 version = r->in.version;
2058 status = get_a_printer_driver(p->mem_ctx, &info, r->in.driver,
2059 r->in.architecture, version);
2061 if ( !W_ERROR_IS_OK(status) )
2064 * if the client asked for a specific version,
2065 * or this is something other than Windows NT x86,
2066 * then we've failed
2069 if ( (r->in.delete_flags & DPD_DELETE_SPECIFIC_VERSION) || (version !=2) )
2070 goto done;
2072 /* try for Win2k driver if "Windows NT x86" */
2074 version = 3;
2075 if (!W_ERROR_IS_OK(get_a_printer_driver(p->mem_ctx, &info, r->in.driver,
2076 r->in.architecture,
2077 version))) {
2078 status = WERR_UNKNOWN_PRINTER_DRIVER;
2079 goto done;
2083 if (printer_driver_in_use(info)) {
2084 status = WERR_PRINTER_DRIVER_IN_USE;
2085 goto done;
2089 * we have a couple of cases to consider.
2090 * (1) Are any files in use? If so and DPD_DELTE_ALL_FILE is set,
2091 * then the delete should fail if **any** files overlap with
2092 * other drivers
2093 * (2) If DPD_DELTE_UNUSED_FILES is sert, then delete all
2094 * non-overlapping files
2095 * (3) If neither DPD_DELTE_ALL_FILE nor DPD_DELTE_ALL_FILES
2096 * is set, the do not delete any files
2097 * Refer to MSDN docs on DeletePrinterDriverEx() for details.
2100 delete_files = r->in.delete_flags & (DPD_DELETE_ALL_FILES|DPD_DELETE_UNUSED_FILES);
2102 /* fail if any files are in use and DPD_DELETE_ALL_FILES is set */
2104 if (delete_files && printer_driver_files_in_use(info, info) & (r->in.delete_flags & DPD_DELETE_ALL_FILES)) {
2105 /* no idea of the correct error here */
2106 status = WERR_ACCESS_DENIED;
2107 goto done;
2111 /* also check for W32X86/3 if necessary; maybe we already have? */
2113 if ( (version == 2) && ((r->in.delete_flags & DPD_DELETE_SPECIFIC_VERSION) != DPD_DELETE_SPECIFIC_VERSION) ) {
2114 if (W_ERROR_IS_OK(get_a_printer_driver(p->mem_ctx, &info_win2k,
2115 r->in.driver,
2116 r->in.architecture, 3)))
2119 if (delete_files && printer_driver_files_in_use(info, info_win2k) & (r->in.delete_flags & DPD_DELETE_ALL_FILES) ) {
2120 /* no idea of the correct error here */
2121 free_a_printer_driver(info_win2k);
2122 status = WERR_ACCESS_DENIED;
2123 goto done;
2126 /* if we get to here, we now have 2 driver info structures to remove */
2127 /* remove the Win2k driver first*/
2129 status_win2k = delete_printer_driver(
2130 p, info_win2k, 3, delete_files);
2131 free_a_printer_driver(info_win2k);
2133 /* this should not have failed---if it did, report to client */
2135 if ( !W_ERROR_IS_OK(status_win2k) )
2136 goto done;
2140 status = delete_printer_driver(p, info, version, delete_files);
2142 if ( W_ERROR_IS_OK(status) || W_ERROR_IS_OK(status_win2k) )
2143 status = WERR_OK;
2144 done:
2145 free_a_printer_driver(info);
2147 return status;
2151 /****************************************************************************
2152 Internal routine for removing printerdata
2153 ***************************************************************************/
2155 static WERROR delete_printer_dataex( NT_PRINTER_INFO_LEVEL *printer, const char *key, const char *value )
2157 return delete_printer_data( printer->info_2, key, value );
2160 /****************************************************************************
2161 Internal routine for storing printerdata
2162 ***************************************************************************/
2164 WERROR set_printer_dataex(NT_PRINTER_INFO_LEVEL *printer,
2165 const char *key, const char *value,
2166 uint32_t type, uint8_t *data, int real_len)
2168 /* the registry objects enforce uniqueness based on value name */
2170 return add_printer_data( printer->info_2, key, value, type, data, real_len );
2173 /********************************************************************
2174 GetPrinterData on a printer server Handle.
2175 ********************************************************************/
2177 static WERROR getprinterdata_printer_server(TALLOC_CTX *mem_ctx,
2178 const char *value,
2179 enum winreg_Type *type,
2180 union spoolss_PrinterData *data)
2182 DEBUG(8,("getprinterdata_printer_server:%s\n", value));
2184 if (!StrCaseCmp(value, "W3SvcInstalled")) {
2185 *type = REG_DWORD;
2186 data->value = 0x00;
2187 return WERR_OK;
2190 if (!StrCaseCmp(value, "BeepEnabled")) {
2191 *type = REG_DWORD;
2192 data->value = 0x00;
2193 return WERR_OK;
2196 if (!StrCaseCmp(value, "EventLog")) {
2197 *type = REG_DWORD;
2198 /* formally was 0x1b */
2199 data->value = 0x00;
2200 return WERR_OK;
2203 if (!StrCaseCmp(value, "NetPopup")) {
2204 *type = REG_DWORD;
2205 data->value = 0x00;
2206 return WERR_OK;
2209 if (!StrCaseCmp(value, "MajorVersion")) {
2210 *type = REG_DWORD;
2212 /* Windows NT 4.0 seems to not allow uploading of drivers
2213 to a server that reports 0x3 as the MajorVersion.
2214 need to investigate more how Win2k gets around this .
2215 -- jerry */
2217 if (RA_WINNT == get_remote_arch()) {
2218 data->value = 0x02;
2219 } else {
2220 data->value = 0x03;
2223 return WERR_OK;
2226 if (!StrCaseCmp(value, "MinorVersion")) {
2227 *type = REG_DWORD;
2228 data->value = 0x00;
2229 return WERR_OK;
2232 /* REG_BINARY
2233 * uint32_t size = 0x114
2234 * uint32_t major = 5
2235 * uint32_t minor = [0|1]
2236 * uint32_t build = [2195|2600]
2237 * extra unicode string = e.g. "Service Pack 3"
2239 if (!StrCaseCmp(value, "OSVersion")) {
2240 DATA_BLOB blob;
2241 enum ndr_err_code ndr_err;
2242 struct spoolss_OSVersion os;
2244 os.major = 5; /* Windows 2000 == 5.0 */
2245 os.minor = 0;
2246 os.build = 2195; /* build */
2247 os.extra_string = ""; /* leave extra string empty */
2249 ndr_err = ndr_push_struct_blob(&blob, mem_ctx, NULL, &os,
2250 (ndr_push_flags_fn_t)ndr_push_spoolss_OSVersion);
2251 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2252 return WERR_GENERAL_FAILURE;
2255 *type = REG_BINARY;
2256 data->binary = blob;
2258 return WERR_OK;
2262 if (!StrCaseCmp(value, "DefaultSpoolDirectory")) {
2263 *type = REG_SZ;
2265 data->string = talloc_strdup(mem_ctx, "C:\\PRINTERS");
2266 W_ERROR_HAVE_NO_MEMORY(data->string);
2268 return WERR_OK;
2271 if (!StrCaseCmp(value, "Architecture")) {
2272 *type = REG_SZ;
2274 data->string = talloc_strdup(mem_ctx, "Windows NT x86");
2275 W_ERROR_HAVE_NO_MEMORY(data->string);
2277 return WERR_OK;
2280 if (!StrCaseCmp(value, "DsPresent")) {
2281 *type = REG_DWORD;
2283 /* only show the publish check box if we are a
2284 member of a AD domain */
2286 if (lp_security() == SEC_ADS) {
2287 data->value = 0x01;
2288 } else {
2289 data->value = 0x00;
2291 return WERR_OK;
2294 if (!StrCaseCmp(value, "DNSMachineName")) {
2295 const char *hostname = get_mydnsfullname();
2297 if (!hostname) {
2298 return WERR_BADFILE;
2301 *type = REG_SZ;
2302 data->string = talloc_strdup(mem_ctx, hostname);
2303 W_ERROR_HAVE_NO_MEMORY(data->string);
2305 return WERR_OK;
2308 *type = REG_NONE;
2310 return WERR_INVALID_PARAM;
2313 /****************************************************************
2314 _spoolss_GetPrinterData
2315 ****************************************************************/
2317 WERROR _spoolss_GetPrinterData(pipes_struct *p,
2318 struct spoolss_GetPrinterData *r)
2320 struct spoolss_GetPrinterDataEx r2;
2322 r2.in.handle = r->in.handle;
2323 r2.in.key_name = "PrinterDriverData";
2324 r2.in.value_name = r->in.value_name;
2325 r2.in.offered = r->in.offered;
2326 r2.out.type = r->out.type;
2327 r2.out.data = r->out.data;
2328 r2.out.needed = r->out.needed;
2330 return _spoolss_GetPrinterDataEx(p, &r2);
2333 /*********************************************************
2334 Connect to the client machine.
2335 **********************************************************/
2337 static bool spoolss_connect_to_client(struct rpc_pipe_client **pp_pipe,
2338 struct sockaddr_storage *client_ss, const char *remote_machine)
2340 NTSTATUS ret;
2341 struct cli_state *the_cli;
2342 struct sockaddr_storage rm_addr;
2344 if ( is_zero_addr((struct sockaddr *)client_ss) ) {
2345 if ( !resolve_name( remote_machine, &rm_addr, 0x20, false) ) {
2346 DEBUG(2,("spoolss_connect_to_client: Can't resolve address for %s\n", remote_machine));
2347 return false;
2350 if (ismyaddr((struct sockaddr *)&rm_addr)) {
2351 DEBUG(0,("spoolss_connect_to_client: Machine %s is one of our addresses. Cannot add to ourselves.\n", remote_machine));
2352 return false;
2354 } else {
2355 char addr[INET6_ADDRSTRLEN];
2356 rm_addr = *client_ss;
2357 print_sockaddr(addr, sizeof(addr), &rm_addr);
2358 DEBUG(5,("spoolss_connect_to_client: Using address %s (no name resolution necessary)\n",
2359 addr));
2362 /* setup the connection */
2364 ret = cli_full_connection( &the_cli, global_myname(), remote_machine,
2365 &rm_addr, 0, "IPC$", "IPC",
2366 "", /* username */
2367 "", /* domain */
2368 "", /* password */
2369 0, lp_client_signing(), NULL );
2371 if ( !NT_STATUS_IS_OK( ret ) ) {
2372 DEBUG(2,("spoolss_connect_to_client: connection to [%s] failed!\n",
2373 remote_machine ));
2374 return false;
2377 if ( the_cli->protocol != PROTOCOL_NT1 ) {
2378 DEBUG(0,("spoolss_connect_to_client: machine %s didn't negotiate NT protocol.\n", remote_machine));
2379 cli_shutdown(the_cli);
2380 return false;
2384 * Ok - we have an anonymous connection to the IPC$ share.
2385 * Now start the NT Domain stuff :-).
2388 ret = cli_rpc_pipe_open_noauth(the_cli, &ndr_table_spoolss.syntax_id, pp_pipe);
2389 if (!NT_STATUS_IS_OK(ret)) {
2390 DEBUG(2,("spoolss_connect_to_client: unable to open the spoolss pipe on machine %s. Error was : %s.\n",
2391 remote_machine, nt_errstr(ret)));
2392 cli_shutdown(the_cli);
2393 return false;
2396 return true;
2399 /***************************************************************************
2400 Connect to the client.
2401 ****************************************************************************/
2403 static bool srv_spoolss_replyopenprinter(int snum, const char *printer,
2404 uint32_t localprinter, uint32_t type,
2405 struct policy_handle *handle,
2406 struct sockaddr_storage *client_ss)
2408 WERROR result;
2409 NTSTATUS status;
2412 * If it's the first connection, contact the client
2413 * and connect to the IPC$ share anonymously
2415 if (smb_connections==0) {
2416 fstring unix_printer;
2418 fstrcpy(unix_printer, printer+2); /* the +2 is to strip the leading 2 backslashs */
2420 if ( !spoolss_connect_to_client( &notify_cli_pipe, client_ss, unix_printer ))
2421 return false;
2423 messaging_register(smbd_messaging_context(), NULL,
2424 MSG_PRINTER_NOTIFY2,
2425 receive_notify2_message_list);
2426 /* Tell the connections db we're now interested in printer
2427 * notify messages. */
2428 register_message_flags(true, FLAG_MSG_PRINT_NOTIFY);
2432 * Tell the specific printing tdb we want messages for this printer
2433 * by registering our PID.
2436 if (!print_notify_register_pid(snum))
2437 DEBUG(0,("print_notify_register_pid: Failed to register our pid for printer %s\n", printer ));
2439 smb_connections++;
2441 status = rpccli_spoolss_ReplyOpenPrinter(notify_cli_pipe, talloc_tos(),
2442 printer,
2443 localprinter,
2444 type,
2446 NULL,
2447 handle,
2448 &result);
2449 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result))
2450 DEBUG(5,("srv_spoolss_reply_open_printer: Client RPC returned [%s]\n",
2451 win_errstr(result)));
2453 return (W_ERROR_IS_OK(result));
2456 /****************************************************************
2457 ****************************************************************/
2459 static struct spoolss_NotifyOption *dup_spoolss_NotifyOption(TALLOC_CTX *mem_ctx,
2460 const struct spoolss_NotifyOption *r)
2462 struct spoolss_NotifyOption *option;
2463 uint32_t i,k;
2465 if (!r) {
2466 return NULL;
2469 option = talloc_zero(mem_ctx, struct spoolss_NotifyOption);
2470 if (!option) {
2471 return NULL;
2474 *option = *r;
2476 if (!option->count) {
2477 return option;
2480 option->types = talloc_zero_array(option,
2481 struct spoolss_NotifyOptionType, option->count);
2482 if (!option->types) {
2483 talloc_free(option);
2484 return NULL;
2487 for (i=0; i < option->count; i++) {
2488 option->types[i] = r->types[i];
2490 if (option->types[i].count) {
2491 option->types[i].fields = talloc_zero_array(option,
2492 union spoolss_Field, option->types[i].count);
2493 if (!option->types[i].fields) {
2494 talloc_free(option);
2495 return NULL;
2497 for (k=0; k<option->types[i].count; k++) {
2498 option->types[i].fields[k] =
2499 r->types[i].fields[k];
2504 return option;
2507 /****************************************************************
2508 * _spoolss_RemoteFindFirstPrinterChangeNotifyEx
2510 * before replying OK: status=0 a rpc call is made to the workstation
2511 * asking ReplyOpenPrinter
2513 * in fact ReplyOpenPrinter is the changenotify equivalent on the spoolss pipe
2514 * called from api_spoolss_rffpcnex
2515 ****************************************************************/
2517 WERROR _spoolss_RemoteFindFirstPrinterChangeNotifyEx(pipes_struct *p,
2518 struct spoolss_RemoteFindFirstPrinterChangeNotifyEx *r)
2520 int snum = -1;
2521 struct spoolss_NotifyOption *option = r->in.notify_options;
2522 struct sockaddr_storage client_ss;
2524 /* store the notify value in the printer struct */
2526 Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
2528 if (!Printer) {
2529 DEBUG(2,("_spoolss_RemoteFindFirstPrinterChangeNotifyEx: "
2530 "Invalid handle (%s:%u:%u).\n",
2531 OUR_HANDLE(r->in.handle)));
2532 return WERR_BADFID;
2535 Printer->notify.flags = r->in.flags;
2536 Printer->notify.options = r->in.options;
2537 Printer->notify.printerlocal = r->in.printer_local;
2539 TALLOC_FREE(Printer->notify.option);
2540 Printer->notify.option = dup_spoolss_NotifyOption(Printer, option);
2542 fstrcpy(Printer->notify.localmachine, r->in.local_machine);
2544 /* Connect to the client machine and send a ReplyOpenPrinter */
2546 if ( Printer->printer_type == SPLHND_SERVER)
2547 snum = -1;
2548 else if ( (Printer->printer_type == SPLHND_PRINTER) &&
2549 !get_printer_snum(p, r->in.handle, &snum, NULL) )
2550 return WERR_BADFID;
2552 if (!interpret_string_addr(&client_ss, p->client_address,
2553 AI_NUMERICHOST)) {
2554 return WERR_SERVER_UNAVAILABLE;
2557 if(!srv_spoolss_replyopenprinter(snum, Printer->notify.localmachine,
2558 Printer->notify.printerlocal, 1,
2559 &Printer->notify.client_hnd, &client_ss))
2560 return WERR_SERVER_UNAVAILABLE;
2562 Printer->notify.client_connected = true;
2564 return WERR_OK;
2567 /*******************************************************************
2568 * fill a notify_info_data with the servername
2569 ********************************************************************/
2571 void spoolss_notify_server_name(int snum,
2572 struct spoolss_Notify *data,
2573 print_queue_struct *queue,
2574 NT_PRINTER_INFO_LEVEL *printer,
2575 TALLOC_CTX *mem_ctx)
2577 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, printer->info_2->servername);
2580 /*******************************************************************
2581 * fill a notify_info_data with the printername (not including the servername).
2582 ********************************************************************/
2584 void spoolss_notify_printer_name(int snum,
2585 struct spoolss_Notify *data,
2586 print_queue_struct *queue,
2587 NT_PRINTER_INFO_LEVEL *printer,
2588 TALLOC_CTX *mem_ctx)
2590 /* the notify name should not contain the \\server\ part */
2591 char *p = strrchr(printer->info_2->printername, '\\');
2593 if (!p) {
2594 p = printer->info_2->printername;
2595 } else {
2596 p++;
2599 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, p);
2602 /*******************************************************************
2603 * fill a notify_info_data with the servicename
2604 ********************************************************************/
2606 void spoolss_notify_share_name(int snum,
2607 struct spoolss_Notify *data,
2608 print_queue_struct *queue,
2609 NT_PRINTER_INFO_LEVEL *printer,
2610 TALLOC_CTX *mem_ctx)
2612 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, lp_servicename(snum));
2615 /*******************************************************************
2616 * fill a notify_info_data with the port name
2617 ********************************************************************/
2619 void spoolss_notify_port_name(int snum,
2620 struct spoolss_Notify *data,
2621 print_queue_struct *queue,
2622 NT_PRINTER_INFO_LEVEL *printer,
2623 TALLOC_CTX *mem_ctx)
2625 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, printer->info_2->portname);
2628 /*******************************************************************
2629 * fill a notify_info_data with the printername
2630 * but it doesn't exist, have to see what to do
2631 ********************************************************************/
2633 void spoolss_notify_driver_name(int snum,
2634 struct spoolss_Notify *data,
2635 print_queue_struct *queue,
2636 NT_PRINTER_INFO_LEVEL *printer,
2637 TALLOC_CTX *mem_ctx)
2639 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, printer->info_2->drivername);
2642 /*******************************************************************
2643 * fill a notify_info_data with the comment
2644 ********************************************************************/
2646 void spoolss_notify_comment(int snum,
2647 struct spoolss_Notify *data,
2648 print_queue_struct *queue,
2649 NT_PRINTER_INFO_LEVEL *printer,
2650 TALLOC_CTX *mem_ctx)
2652 char *p;
2654 if (*printer->info_2->comment == '\0') {
2655 p = lp_comment(snum);
2656 } else {
2657 p = printer->info_2->comment;
2660 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, printer->info_2->comment);
2663 /*******************************************************************
2664 * fill a notify_info_data with the comment
2665 * location = "Room 1, floor 2, building 3"
2666 ********************************************************************/
2668 void spoolss_notify_location(int snum,
2669 struct spoolss_Notify *data,
2670 print_queue_struct *queue,
2671 NT_PRINTER_INFO_LEVEL *printer,
2672 TALLOC_CTX *mem_ctx)
2674 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, printer->info_2->location);
2677 /*******************************************************************
2678 * fill a notify_info_data with the device mode
2679 * jfm:xxxx don't to it for know but that's a real problem !!!
2680 ********************************************************************/
2682 static void spoolss_notify_devmode(int snum,
2683 struct spoolss_Notify *data,
2684 print_queue_struct *queue,
2685 NT_PRINTER_INFO_LEVEL *printer,
2686 TALLOC_CTX *mem_ctx)
2688 /* for a dummy implementation we have to zero the fields */
2689 SETUP_SPOOLSS_NOTIFY_DATA_DEVMODE(data, NULL);
2692 /*******************************************************************
2693 * fill a notify_info_data with the separator file name
2694 ********************************************************************/
2696 void spoolss_notify_sepfile(int snum,
2697 struct spoolss_Notify *data,
2698 print_queue_struct *queue,
2699 NT_PRINTER_INFO_LEVEL *printer,
2700 TALLOC_CTX *mem_ctx)
2702 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, printer->info_2->sepfile);
2705 /*******************************************************************
2706 * fill a notify_info_data with the print processor
2707 * jfm:xxxx return always winprint to indicate we don't do anything to it
2708 ********************************************************************/
2710 void spoolss_notify_print_processor(int snum,
2711 struct spoolss_Notify *data,
2712 print_queue_struct *queue,
2713 NT_PRINTER_INFO_LEVEL *printer,
2714 TALLOC_CTX *mem_ctx)
2716 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, printer->info_2->printprocessor);
2719 /*******************************************************************
2720 * fill a notify_info_data with the print processor options
2721 * jfm:xxxx send an empty string
2722 ********************************************************************/
2724 void spoolss_notify_parameters(int snum,
2725 struct spoolss_Notify *data,
2726 print_queue_struct *queue,
2727 NT_PRINTER_INFO_LEVEL *printer,
2728 TALLOC_CTX *mem_ctx)
2730 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, printer->info_2->parameters);
2733 /*******************************************************************
2734 * fill a notify_info_data with the data type
2735 * jfm:xxxx always send RAW as data type
2736 ********************************************************************/
2738 void spoolss_notify_datatype(int snum,
2739 struct spoolss_Notify *data,
2740 print_queue_struct *queue,
2741 NT_PRINTER_INFO_LEVEL *printer,
2742 TALLOC_CTX *mem_ctx)
2744 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, printer->info_2->datatype);
2747 /*******************************************************************
2748 * fill a notify_info_data with the security descriptor
2749 * jfm:xxxx send an null pointer to say no security desc
2750 * have to implement security before !
2751 ********************************************************************/
2753 static void spoolss_notify_security_desc(int snum,
2754 struct spoolss_Notify *data,
2755 print_queue_struct *queue,
2756 NT_PRINTER_INFO_LEVEL *printer,
2757 TALLOC_CTX *mem_ctx)
2759 SETUP_SPOOLSS_NOTIFY_DATA_SECDESC(data,
2760 printer->info_2->secdesc_buf->sd_size,
2761 printer->info_2->secdesc_buf->sd);
2764 /*******************************************************************
2765 * fill a notify_info_data with the attributes
2766 * jfm:xxxx a samba printer is always shared
2767 ********************************************************************/
2769 void spoolss_notify_attributes(int snum,
2770 struct spoolss_Notify *data,
2771 print_queue_struct *queue,
2772 NT_PRINTER_INFO_LEVEL *printer,
2773 TALLOC_CTX *mem_ctx)
2775 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, printer->info_2->attributes);
2778 /*******************************************************************
2779 * fill a notify_info_data with the priority
2780 ********************************************************************/
2782 static void spoolss_notify_priority(int snum,
2783 struct spoolss_Notify *data,
2784 print_queue_struct *queue,
2785 NT_PRINTER_INFO_LEVEL *printer,
2786 TALLOC_CTX *mem_ctx)
2788 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, printer->info_2->priority);
2791 /*******************************************************************
2792 * fill a notify_info_data with the default priority
2793 ********************************************************************/
2795 static void spoolss_notify_default_priority(int snum,
2796 struct spoolss_Notify *data,
2797 print_queue_struct *queue,
2798 NT_PRINTER_INFO_LEVEL *printer,
2799 TALLOC_CTX *mem_ctx)
2801 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, printer->info_2->default_priority);
2804 /*******************************************************************
2805 * fill a notify_info_data with the start time
2806 ********************************************************************/
2808 static void spoolss_notify_start_time(int snum,
2809 struct spoolss_Notify *data,
2810 print_queue_struct *queue,
2811 NT_PRINTER_INFO_LEVEL *printer,
2812 TALLOC_CTX *mem_ctx)
2814 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, printer->info_2->starttime);
2817 /*******************************************************************
2818 * fill a notify_info_data with the until time
2819 ********************************************************************/
2821 static void spoolss_notify_until_time(int snum,
2822 struct spoolss_Notify *data,
2823 print_queue_struct *queue,
2824 NT_PRINTER_INFO_LEVEL *printer,
2825 TALLOC_CTX *mem_ctx)
2827 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, printer->info_2->untiltime);
2830 /*******************************************************************
2831 * fill a notify_info_data with the status
2832 ********************************************************************/
2834 static void spoolss_notify_status(int snum,
2835 struct spoolss_Notify *data,
2836 print_queue_struct *queue,
2837 NT_PRINTER_INFO_LEVEL *printer,
2838 TALLOC_CTX *mem_ctx)
2840 print_status_struct status;
2842 print_queue_length(snum, &status);
2843 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, status.status);
2846 /*******************************************************************
2847 * fill a notify_info_data with the number of jobs queued
2848 ********************************************************************/
2850 void spoolss_notify_cjobs(int snum,
2851 struct spoolss_Notify *data,
2852 print_queue_struct *queue,
2853 NT_PRINTER_INFO_LEVEL *printer,
2854 TALLOC_CTX *mem_ctx)
2856 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, print_queue_length(snum, NULL));
2859 /*******************************************************************
2860 * fill a notify_info_data with the average ppm
2861 ********************************************************************/
2863 static void spoolss_notify_average_ppm(int snum,
2864 struct spoolss_Notify *data,
2865 print_queue_struct *queue,
2866 NT_PRINTER_INFO_LEVEL *printer,
2867 TALLOC_CTX *mem_ctx)
2869 /* always respond 8 pages per minutes */
2870 /* a little hard ! */
2871 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, printer->info_2->averageppm);
2874 /*******************************************************************
2875 * fill a notify_info_data with username
2876 ********************************************************************/
2878 static void spoolss_notify_username(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, queue->fs_user);
2887 /*******************************************************************
2888 * fill a notify_info_data with job status
2889 ********************************************************************/
2891 static void spoolss_notify_job_status(int snum,
2892 struct spoolss_Notify *data,
2893 print_queue_struct *queue,
2894 NT_PRINTER_INFO_LEVEL *printer,
2895 TALLOC_CTX *mem_ctx)
2897 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, nt_printj_status(queue->status));
2900 /*******************************************************************
2901 * fill a notify_info_data with job name
2902 ********************************************************************/
2904 static void spoolss_notify_job_name(int snum,
2905 struct spoolss_Notify *data,
2906 print_queue_struct *queue,
2907 NT_PRINTER_INFO_LEVEL *printer,
2908 TALLOC_CTX *mem_ctx)
2910 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, queue->fs_file);
2913 /*******************************************************************
2914 * fill a notify_info_data with job status
2915 ********************************************************************/
2917 static void spoolss_notify_job_status_string(int snum,
2918 struct spoolss_Notify *data,
2919 print_queue_struct *queue,
2920 NT_PRINTER_INFO_LEVEL *printer,
2921 TALLOC_CTX *mem_ctx)
2924 * Now we're returning job status codes we just return a "" here. JRA.
2927 const char *p = "";
2929 #if 0 /* NO LONGER NEEDED - JRA. 02/22/2001 */
2930 p = "unknown";
2932 switch (queue->status) {
2933 case LPQ_QUEUED:
2934 p = "Queued";
2935 break;
2936 case LPQ_PAUSED:
2937 p = ""; /* NT provides the paused string */
2938 break;
2939 case LPQ_SPOOLING:
2940 p = "Spooling";
2941 break;
2942 case LPQ_PRINTING:
2943 p = "Printing";
2944 break;
2946 #endif /* NO LONGER NEEDED. */
2948 SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, p);
2951 /*******************************************************************
2952 * fill a notify_info_data with job time
2953 ********************************************************************/
2955 static void spoolss_notify_job_time(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_INTEGER(data, 0);
2964 /*******************************************************************
2965 * fill a notify_info_data with job size
2966 ********************************************************************/
2968 static void spoolss_notify_job_size(int snum,
2969 struct spoolss_Notify *data,
2970 print_queue_struct *queue,
2971 NT_PRINTER_INFO_LEVEL *printer,
2972 TALLOC_CTX *mem_ctx)
2974 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, queue->size);
2977 /*******************************************************************
2978 * fill a notify_info_data with page info
2979 ********************************************************************/
2980 static void spoolss_notify_total_pages(int snum,
2981 struct spoolss_Notify *data,
2982 print_queue_struct *queue,
2983 NT_PRINTER_INFO_LEVEL *printer,
2984 TALLOC_CTX *mem_ctx)
2986 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, queue->page_count);
2989 /*******************************************************************
2990 * fill a notify_info_data with pages printed info.
2991 ********************************************************************/
2992 static void spoolss_notify_pages_printed(int snum,
2993 struct spoolss_Notify *data,
2994 print_queue_struct *queue,
2995 NT_PRINTER_INFO_LEVEL *printer,
2996 TALLOC_CTX *mem_ctx)
2998 /* Add code when back-end tracks this */
2999 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, 0);
3002 /*******************************************************************
3003 Fill a notify_info_data with job position.
3004 ********************************************************************/
3006 static void spoolss_notify_job_position(int snum,
3007 struct spoolss_Notify *data,
3008 print_queue_struct *queue,
3009 NT_PRINTER_INFO_LEVEL *printer,
3010 TALLOC_CTX *mem_ctx)
3012 SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, queue->job);
3015 /*******************************************************************
3016 Fill a notify_info_data with submitted time.
3017 ********************************************************************/
3019 static void spoolss_notify_submitted_time(int snum,
3020 struct spoolss_Notify *data,
3021 print_queue_struct *queue,
3022 NT_PRINTER_INFO_LEVEL *printer,
3023 TALLOC_CTX *mem_ctx)
3025 data->data.string.string = NULL;
3026 data->data.string.size = 0;
3028 init_systemtime_buffer(mem_ctx, gmtime(&queue->time),
3029 &data->data.string.string,
3030 &data->data.string.size);
3034 struct s_notify_info_data_table
3036 enum spoolss_NotifyType type;
3037 uint16_t field;
3038 const char *name;
3039 enum spoolss_NotifyTable variable_type;
3040 void (*fn) (int snum, struct spoolss_Notify *data,
3041 print_queue_struct *queue,
3042 NT_PRINTER_INFO_LEVEL *printer, TALLOC_CTX *mem_ctx);
3045 /* A table describing the various print notification constants and
3046 whether the notification data is a pointer to a variable sized
3047 buffer, a one value uint32_t or a two value uint32_t. */
3049 static const struct s_notify_info_data_table notify_info_data_table[] =
3051 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_SERVER_NAME, "PRINTER_NOTIFY_FIELD_SERVER_NAME", NOTIFY_TABLE_STRING, spoolss_notify_server_name },
3052 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_PRINTER_NAME, "PRINTER_NOTIFY_FIELD_PRINTER_NAME", NOTIFY_TABLE_STRING, spoolss_notify_printer_name },
3053 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_SHARE_NAME, "PRINTER_NOTIFY_FIELD_SHARE_NAME", NOTIFY_TABLE_STRING, spoolss_notify_share_name },
3054 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_PORT_NAME, "PRINTER_NOTIFY_FIELD_PORT_NAME", NOTIFY_TABLE_STRING, spoolss_notify_port_name },
3055 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_DRIVER_NAME, "PRINTER_NOTIFY_FIELD_DRIVER_NAME", NOTIFY_TABLE_STRING, spoolss_notify_driver_name },
3056 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_COMMENT, "PRINTER_NOTIFY_FIELD_COMMENT", NOTIFY_TABLE_STRING, spoolss_notify_comment },
3057 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_LOCATION, "PRINTER_NOTIFY_FIELD_LOCATION", NOTIFY_TABLE_STRING, spoolss_notify_location },
3058 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_DEVMODE, "PRINTER_NOTIFY_FIELD_DEVMODE", NOTIFY_TABLE_DEVMODE, spoolss_notify_devmode },
3059 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_SEPFILE, "PRINTER_NOTIFY_FIELD_SEPFILE", NOTIFY_TABLE_STRING, spoolss_notify_sepfile },
3060 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_PRINT_PROCESSOR, "PRINTER_NOTIFY_FIELD_PRINT_PROCESSOR", NOTIFY_TABLE_STRING, spoolss_notify_print_processor },
3061 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_PARAMETERS, "PRINTER_NOTIFY_FIELD_PARAMETERS", NOTIFY_TABLE_STRING, spoolss_notify_parameters },
3062 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_DATATYPE, "PRINTER_NOTIFY_FIELD_DATATYPE", NOTIFY_TABLE_STRING, spoolss_notify_datatype },
3063 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_SECURITY_DESCRIPTOR, "PRINTER_NOTIFY_FIELD_SECURITY_DESCRIPTOR", NOTIFY_TABLE_SECURITYDESCRIPTOR, spoolss_notify_security_desc },
3064 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_ATTRIBUTES, "PRINTER_NOTIFY_FIELD_ATTRIBUTES", NOTIFY_TABLE_DWORD, spoolss_notify_attributes },
3065 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_PRIORITY, "PRINTER_NOTIFY_FIELD_PRIORITY", NOTIFY_TABLE_DWORD, spoolss_notify_priority },
3066 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_DEFAULT_PRIORITY, "PRINTER_NOTIFY_FIELD_DEFAULT_PRIORITY", NOTIFY_TABLE_DWORD, spoolss_notify_default_priority },
3067 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_START_TIME, "PRINTER_NOTIFY_FIELD_START_TIME", NOTIFY_TABLE_DWORD, spoolss_notify_start_time },
3068 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_UNTIL_TIME, "PRINTER_NOTIFY_FIELD_UNTIL_TIME", NOTIFY_TABLE_DWORD, spoolss_notify_until_time },
3069 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_STATUS, "PRINTER_NOTIFY_FIELD_STATUS", NOTIFY_TABLE_DWORD, spoolss_notify_status },
3070 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_STATUS_STRING, "PRINTER_NOTIFY_FIELD_STATUS_STRING", NOTIFY_TABLE_STRING, NULL },
3071 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_CJOBS, "PRINTER_NOTIFY_FIELD_CJOBS", NOTIFY_TABLE_DWORD, spoolss_notify_cjobs },
3072 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_AVERAGE_PPM, "PRINTER_NOTIFY_FIELD_AVERAGE_PPM", NOTIFY_TABLE_DWORD, spoolss_notify_average_ppm },
3073 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_TOTAL_PAGES, "PRINTER_NOTIFY_FIELD_TOTAL_PAGES", NOTIFY_TABLE_DWORD, NULL },
3074 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_PAGES_PRINTED, "PRINTER_NOTIFY_FIELD_PAGES_PRINTED", NOTIFY_TABLE_DWORD, NULL },
3075 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_TOTAL_BYTES, "PRINTER_NOTIFY_FIELD_TOTAL_BYTES", NOTIFY_TABLE_DWORD, NULL },
3076 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_BYTES_PRINTED, "PRINTER_NOTIFY_FIELD_BYTES_PRINTED", NOTIFY_TABLE_DWORD, NULL },
3077 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_PRINTER_NAME, "JOB_NOTIFY_FIELD_PRINTER_NAME", NOTIFY_TABLE_STRING, spoolss_notify_printer_name },
3078 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_MACHINE_NAME, "JOB_NOTIFY_FIELD_MACHINE_NAME", NOTIFY_TABLE_STRING, spoolss_notify_server_name },
3079 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_PORT_NAME, "JOB_NOTIFY_FIELD_PORT_NAME", NOTIFY_TABLE_STRING, spoolss_notify_port_name },
3080 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_USER_NAME, "JOB_NOTIFY_FIELD_USER_NAME", NOTIFY_TABLE_STRING, spoolss_notify_username },
3081 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_NOTIFY_NAME, "JOB_NOTIFY_FIELD_NOTIFY_NAME", NOTIFY_TABLE_STRING, spoolss_notify_username },
3082 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_DATATYPE, "JOB_NOTIFY_FIELD_DATATYPE", NOTIFY_TABLE_STRING, spoolss_notify_datatype },
3083 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_PRINT_PROCESSOR, "JOB_NOTIFY_FIELD_PRINT_PROCESSOR", NOTIFY_TABLE_STRING, spoolss_notify_print_processor },
3084 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_PARAMETERS, "JOB_NOTIFY_FIELD_PARAMETERS", NOTIFY_TABLE_STRING, spoolss_notify_parameters },
3085 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_DRIVER_NAME, "JOB_NOTIFY_FIELD_DRIVER_NAME", NOTIFY_TABLE_STRING, spoolss_notify_driver_name },
3086 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_DEVMODE, "JOB_NOTIFY_FIELD_DEVMODE", NOTIFY_TABLE_DEVMODE, spoolss_notify_devmode },
3087 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_STATUS, "JOB_NOTIFY_FIELD_STATUS", NOTIFY_TABLE_DWORD, spoolss_notify_job_status },
3088 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_STATUS_STRING, "JOB_NOTIFY_FIELD_STATUS_STRING", NOTIFY_TABLE_STRING, spoolss_notify_job_status_string },
3089 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_SECURITY_DESCRIPTOR, "JOB_NOTIFY_FIELD_SECURITY_DESCRIPTOR", NOTIFY_TABLE_SECURITYDESCRIPTOR, NULL },
3090 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_DOCUMENT, "JOB_NOTIFY_FIELD_DOCUMENT", NOTIFY_TABLE_STRING, spoolss_notify_job_name },
3091 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_PRIORITY, "JOB_NOTIFY_FIELD_PRIORITY", NOTIFY_TABLE_DWORD, spoolss_notify_priority },
3092 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_POSITION, "JOB_NOTIFY_FIELD_POSITION", NOTIFY_TABLE_DWORD, spoolss_notify_job_position },
3093 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_SUBMITTED, "JOB_NOTIFY_FIELD_SUBMITTED", NOTIFY_TABLE_TIME, spoolss_notify_submitted_time },
3094 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_START_TIME, "JOB_NOTIFY_FIELD_START_TIME", NOTIFY_TABLE_DWORD, spoolss_notify_start_time },
3095 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_UNTIL_TIME, "JOB_NOTIFY_FIELD_UNTIL_TIME", NOTIFY_TABLE_DWORD, spoolss_notify_until_time },
3096 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_TIME, "JOB_NOTIFY_FIELD_TIME", NOTIFY_TABLE_DWORD, spoolss_notify_job_time },
3097 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_TOTAL_PAGES, "JOB_NOTIFY_FIELD_TOTAL_PAGES", NOTIFY_TABLE_DWORD, spoolss_notify_total_pages },
3098 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_PAGES_PRINTED, "JOB_NOTIFY_FIELD_PAGES_PRINTED", NOTIFY_TABLE_DWORD, spoolss_notify_pages_printed },
3099 { JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_TOTAL_BYTES, "JOB_NOTIFY_FIELD_TOTAL_BYTES", NOTIFY_TABLE_DWORD, spoolss_notify_job_size },
3102 /*******************************************************************
3103 Return the variable_type of info_data structure.
3104 ********************************************************************/
3106 static uint32_t variable_type_of_notify_info_data(enum spoolss_NotifyType type,
3107 uint16_t field)
3109 int i=0;
3111 for (i = 0; i < ARRAY_SIZE(notify_info_data_table); i++) {
3112 if ( (notify_info_data_table[i].type == type) &&
3113 (notify_info_data_table[i].field == field) ) {
3114 return notify_info_data_table[i].variable_type;
3118 DEBUG(5, ("invalid notify data type %d/%d\n", type, field));
3120 return 0;
3123 /****************************************************************************
3124 ****************************************************************************/
3126 static bool search_notify(enum spoolss_NotifyType type,
3127 uint16_t field,
3128 int *value)
3130 int i;
3132 for (i = 0; i < ARRAY_SIZE(notify_info_data_table); i++) {
3133 if (notify_info_data_table[i].type == type &&
3134 notify_info_data_table[i].field == field &&
3135 notify_info_data_table[i].fn != NULL) {
3136 *value = i;
3137 return true;
3141 return false;
3144 /****************************************************************************
3145 ****************************************************************************/
3147 void construct_info_data(struct spoolss_Notify *info_data,
3148 enum spoolss_NotifyType type,
3149 uint16_t field,
3150 int id)
3152 info_data->type = type;
3153 info_data->field.field = field;
3154 info_data->variable_type = variable_type_of_notify_info_data(type, field);
3155 info_data->job_id = id;
3158 /*******************************************************************
3160 * fill a notify_info struct with info asked
3162 ********************************************************************/
3164 static bool construct_notify_printer_info(Printer_entry *print_hnd,
3165 struct spoolss_NotifyInfo *info,
3166 int snum,
3167 const struct spoolss_NotifyOptionType *option_type,
3168 uint32_t id,
3169 TALLOC_CTX *mem_ctx)
3171 int field_num,j;
3172 enum spoolss_NotifyType type;
3173 uint16_t field;
3175 struct spoolss_Notify *current_data;
3176 NT_PRINTER_INFO_LEVEL *printer = NULL;
3177 print_queue_struct *queue=NULL;
3179 type = option_type->type;
3181 DEBUG(4,("construct_notify_printer_info: Notify type: [%s], number of notify info: [%d] on printer: [%s]\n",
3182 (type == PRINTER_NOTIFY_TYPE ? "PRINTER_NOTIFY_TYPE" : "JOB_NOTIFY_TYPE"),
3183 option_type->count, lp_servicename(snum)));
3185 if (!W_ERROR_IS_OK(get_a_printer(print_hnd, &printer, 2, lp_const_servicename(snum))))
3186 return false;
3188 for(field_num=0; field_num < option_type->count; field_num++) {
3189 field = option_type->fields[field_num].field;
3191 DEBUG(4,("construct_notify_printer_info: notify [%d]: type [%x], field [%x]\n", field_num, type, field));
3193 if (!search_notify(type, field, &j) )
3194 continue;
3196 info->notifies = TALLOC_REALLOC_ARRAY(info, info->notifies,
3197 struct spoolss_Notify,
3198 info->count + 1);
3199 if (info->notifies == NULL) {
3200 DEBUG(2,("construct_notify_printer_info: failed to enlarge buffer info->data!\n"));
3201 free_a_printer(&printer, 2);
3202 return false;
3205 current_data = &info->notifies[info->count];
3207 construct_info_data(current_data, type, field, id);
3209 DEBUG(10,("construct_notify_printer_info: calling [%s] snum=%d printername=[%s])\n",
3210 notify_info_data_table[j].name, snum, printer->info_2->printername ));
3212 notify_info_data_table[j].fn(snum, current_data, queue,
3213 printer, mem_ctx);
3215 info->count++;
3218 free_a_printer(&printer, 2);
3219 return true;
3222 /*******************************************************************
3224 * fill a notify_info struct with info asked
3226 ********************************************************************/
3228 static bool construct_notify_jobs_info(print_queue_struct *queue,
3229 struct spoolss_NotifyInfo *info,
3230 NT_PRINTER_INFO_LEVEL *printer,
3231 int snum,
3232 const struct spoolss_NotifyOptionType *option_type,
3233 uint32_t id,
3234 TALLOC_CTX *mem_ctx)
3236 int field_num,j;
3237 enum spoolss_NotifyType type;
3238 uint16_t field;
3239 struct spoolss_Notify *current_data;
3241 DEBUG(4,("construct_notify_jobs_info\n"));
3243 type = option_type->type;
3245 DEBUGADD(4,("Notify type: [%s], number of notify info: [%d]\n",
3246 (type == PRINTER_NOTIFY_TYPE ? "PRINTER_NOTIFY_TYPE" : "JOB_NOTIFY_TYPE"),
3247 option_type->count));
3249 for(field_num=0; field_num<option_type->count; field_num++) {
3250 field = option_type->fields[field_num].field;
3252 if (!search_notify(type, field, &j) )
3253 continue;
3255 info->notifies = TALLOC_REALLOC_ARRAY(info, info->notifies,
3256 struct spoolss_Notify,
3257 info->count + 1);
3258 if (info->notifies == NULL) {
3259 DEBUG(2,("construct_notify_jobs_info: failed to enlarg buffer info->data!\n"));
3260 return false;
3263 current_data=&(info->notifies[info->count]);
3265 construct_info_data(current_data, type, field, id);
3266 notify_info_data_table[j].fn(snum, current_data, queue,
3267 printer, mem_ctx);
3268 info->count++;
3271 return true;
3275 * JFM: The enumeration is not that simple, it's even non obvious.
3277 * let's take an example: I want to monitor the PRINTER SERVER for
3278 * the printer's name and the number of jobs currently queued.
3279 * So in the NOTIFY_OPTION, I have one NOTIFY_OPTION_TYPE structure.
3280 * Its type is PRINTER_NOTIFY_TYPE and it has 2 fields NAME and CJOBS.
3282 * I have 3 printers on the back of my server.
3284 * Now the response is a NOTIFY_INFO structure, with 6 NOTIFY_INFO_DATA
3285 * structures.
3286 * Number Data Id
3287 * 1 printer 1 name 1
3288 * 2 printer 1 cjob 1
3289 * 3 printer 2 name 2
3290 * 4 printer 2 cjob 2
3291 * 5 printer 3 name 3
3292 * 6 printer 3 name 3
3294 * that's the print server case, the printer case is even worse.
3297 /*******************************************************************
3299 * enumerate all printers on the printserver
3300 * fill a notify_info struct with info asked
3302 ********************************************************************/
3304 static WERROR printserver_notify_info(pipes_struct *p,
3305 struct policy_handle *hnd,
3306 struct spoolss_NotifyInfo *info,
3307 TALLOC_CTX *mem_ctx)
3309 int snum;
3310 Printer_entry *Printer = find_printer_index_by_hnd(p, hnd);
3311 int n_services=lp_numservices();
3312 int i;
3313 struct spoolss_NotifyOption *option;
3314 struct spoolss_NotifyOptionType option_type;
3316 DEBUG(4,("printserver_notify_info\n"));
3318 if (!Printer)
3319 return WERR_BADFID;
3321 option = Printer->notify.option;
3323 info->version = 2;
3324 info->notifies = NULL;
3325 info->count = 0;
3327 /* a bug in xp sp2 rc2 causes it to send a fnpcn request without
3328 sending a ffpcn() request first */
3330 if ( !option )
3331 return WERR_BADFID;
3333 for (i=0; i<option->count; i++) {
3334 option_type = option->types[i];
3336 if (option_type.type != PRINTER_NOTIFY_TYPE)
3337 continue;
3339 for (snum=0; snum<n_services; snum++)
3341 if ( lp_browseable(snum) && lp_snum_ok(snum) && lp_print_ok(snum) )
3342 construct_notify_printer_info ( Printer, info, snum, &option_type, snum, mem_ctx );
3346 #if 0
3348 * Debugging information, don't delete.
3351 DEBUG(1,("dumping the NOTIFY_INFO\n"));
3352 DEBUGADD(1,("info->version:[%d], info->flags:[%d], info->count:[%d]\n", info->version, info->flags, info->count));
3353 DEBUGADD(1,("num\ttype\tfield\tres\tid\tsize\tenc_type\n"));
3355 for (i=0; i<info->count; i++) {
3356 DEBUGADD(1,("[%d]\t[%d]\t[%d]\t[%d]\t[%d]\t[%d]\t[%d]\n",
3357 i, info->data[i].type, info->data[i].field, info->data[i].reserved,
3358 info->data[i].id, info->data[i].size, info->data[i].enc_type));
3360 #endif
3362 return WERR_OK;
3365 /*******************************************************************
3367 * fill a notify_info struct with info asked
3369 ********************************************************************/
3371 static WERROR printer_notify_info(pipes_struct *p, struct policy_handle *hnd,
3372 struct spoolss_NotifyInfo *info,
3373 TALLOC_CTX *mem_ctx)
3375 int snum;
3376 Printer_entry *Printer = find_printer_index_by_hnd(p, hnd);
3377 int i;
3378 uint32_t id;
3379 struct spoolss_NotifyOption *option;
3380 struct spoolss_NotifyOptionType option_type;
3381 int count,j;
3382 print_queue_struct *queue=NULL;
3383 print_status_struct status;
3385 DEBUG(4,("printer_notify_info\n"));
3387 if (!Printer)
3388 return WERR_BADFID;
3390 option = Printer->notify.option;
3391 id = 0x0;
3393 info->version = 2;
3394 info->notifies = NULL;
3395 info->count = 0;
3397 /* a bug in xp sp2 rc2 causes it to send a fnpcn request without
3398 sending a ffpcn() request first */
3400 if ( !option )
3401 return WERR_BADFID;
3403 get_printer_snum(p, hnd, &snum, NULL);
3405 for (i=0; i<option->count; i++) {
3406 option_type = option->types[i];
3408 switch (option_type.type) {
3409 case PRINTER_NOTIFY_TYPE:
3410 if(construct_notify_printer_info(Printer, info, snum,
3411 &option_type, id,
3412 mem_ctx))
3413 id--;
3414 break;
3416 case JOB_NOTIFY_TYPE: {
3417 NT_PRINTER_INFO_LEVEL *printer = NULL;
3419 count = print_queue_status(snum, &queue, &status);
3421 if (!W_ERROR_IS_OK(get_a_printer(Printer, &printer, 2, lp_const_servicename(snum))))
3422 goto done;
3424 for (j=0; j<count; j++) {
3425 construct_notify_jobs_info(&queue[j], info,
3426 printer, snum,
3427 &option_type,
3428 queue[j].job,
3429 mem_ctx);
3432 free_a_printer(&printer, 2);
3434 done:
3435 SAFE_FREE(queue);
3436 break;
3442 * Debugging information, don't delete.
3445 DEBUG(1,("dumping the NOTIFY_INFO\n"));
3446 DEBUGADD(1,("info->version:[%d], info->flags:[%d], info->count:[%d]\n", info->version, info->flags, info->count));
3447 DEBUGADD(1,("num\ttype\tfield\tres\tid\tsize\tenc_type\n"));
3449 for (i=0; i<info->count; i++) {
3450 DEBUGADD(1,("[%d]\t[%d]\t[%d]\t[%d]\t[%d]\t[%d]\t[%d]\n",
3451 i, info->data[i].type, info->data[i].field, info->data[i].reserved,
3452 info->data[i].id, info->data[i].size, info->data[i].enc_type));
3455 return WERR_OK;
3458 /****************************************************************
3459 _spoolss_RouterRefreshPrinterChangeNotify
3460 ****************************************************************/
3462 WERROR _spoolss_RouterRefreshPrinterChangeNotify(pipes_struct *p,
3463 struct spoolss_RouterRefreshPrinterChangeNotify *r)
3465 struct spoolss_NotifyInfo *info;
3467 Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
3468 WERROR result = WERR_BADFID;
3470 /* we always have a spoolss_NotifyInfo struct */
3471 info = talloc_zero(p->mem_ctx, struct spoolss_NotifyInfo);
3472 if (!info) {
3473 result = WERR_NOMEM;
3474 goto done;
3477 *r->out.info = info;
3479 if (!Printer) {
3480 DEBUG(2,("_spoolss_RouterRefreshPrinterChangeNotify: "
3481 "Invalid handle (%s:%u:%u).\n",
3482 OUR_HANDLE(r->in.handle)));
3483 goto done;
3486 DEBUG(4,("Printer type %x\n",Printer->printer_type));
3489 * We are now using the change value, and
3490 * I should check for PRINTER_NOTIFY_OPTIONS_REFRESH but as
3491 * I don't have a global notification system, I'm sending back all the
3492 * informations even when _NOTHING_ has changed.
3495 /* We need to keep track of the change value to send back in
3496 RRPCN replies otherwise our updates are ignored. */
3498 Printer->notify.fnpcn = true;
3500 if (Printer->notify.client_connected) {
3501 DEBUG(10,("_spoolss_RouterRefreshPrinterChangeNotify: "
3502 "Saving change value in request [%x]\n",
3503 r->in.change_low));
3504 Printer->notify.change = r->in.change_low;
3507 /* just ignore the spoolss_NotifyOption */
3509 switch (Printer->printer_type) {
3510 case SPLHND_SERVER:
3511 result = printserver_notify_info(p, r->in.handle,
3512 info, p->mem_ctx);
3513 break;
3515 case SPLHND_PRINTER:
3516 result = printer_notify_info(p, r->in.handle,
3517 info, p->mem_ctx);
3518 break;
3521 Printer->notify.fnpcn = false;
3523 done:
3524 return result;
3527 /********************************************************************
3528 * construct_printer_info_0
3529 * fill a printer_info_0 struct
3530 ********************************************************************/
3532 static WERROR construct_printer_info0(TALLOC_CTX *mem_ctx,
3533 const NT_PRINTER_INFO_LEVEL *ntprinter,
3534 struct spoolss_PrinterInfo0 *r,
3535 int snum)
3537 int count;
3538 counter_printer_0 *session_counter;
3539 time_t setuptime;
3540 print_status_struct status;
3542 r->printername = talloc_strdup(mem_ctx, ntprinter->info_2->printername);
3543 W_ERROR_HAVE_NO_MEMORY(r->printername);
3545 r->servername = talloc_strdup(mem_ctx, ntprinter->info_2->servername);
3546 W_ERROR_HAVE_NO_MEMORY(r->servername);
3548 count = print_queue_length(snum, &status);
3550 /* check if we already have a counter for this printer */
3551 for (session_counter = counter_list; session_counter; session_counter = session_counter->next) {
3552 if (session_counter->snum == snum)
3553 break;
3556 /* it's the first time, add it to the list */
3557 if (session_counter == NULL) {
3558 session_counter = SMB_MALLOC_P(counter_printer_0);
3559 W_ERROR_HAVE_NO_MEMORY(session_counter);
3560 ZERO_STRUCTP(session_counter);
3561 session_counter->snum = snum;
3562 session_counter->counter = 0;
3563 DLIST_ADD(counter_list, session_counter);
3566 /* increment it */
3567 session_counter->counter++;
3569 r->cjobs = count;
3570 r->total_jobs = 0;
3571 r->total_bytes = 0;
3573 setuptime = (time_t)ntprinter->info_2->setuptime;
3575 init_systemtime(&r->time, gmtime(&setuptime));
3577 /* JFM:
3578 * the global_counter should be stored in a TDB as it's common to all the clients
3579 * and should be zeroed on samba startup
3581 r->global_counter = session_counter->counter;
3582 r->total_pages = 0;
3583 /* in 2.2 we reported ourselves as 0x0004 and 0x0565 */
3584 r->version = 0x0005; /* NT 5 */
3585 r->free_build = 0x0893; /* build 2195 */
3586 r->spooling = 0;
3587 r->max_spooling = 0;
3588 r->session_counter = session_counter->counter;
3589 r->num_error_out_of_paper = 0x0;
3590 r->num_error_not_ready = 0x0; /* number of print failure */
3591 r->job_error = 0x0;
3592 r->number_of_processors = 0x1;
3593 r->processor_type = PROCESSOR_INTEL_PENTIUM; /* 586 Pentium ? */
3594 r->high_part_total_bytes = 0x0;
3595 r->change_id = ntprinter->info_2->changeid; /* ChangeID in milliseconds*/
3596 r->last_error = WERR_OK;
3597 r->status = nt_printq_status(status.status);
3598 r->enumerate_network_printers = 0x0;
3599 r->c_setprinter = get_c_setprinter(); /* monotonically increasing sum of delta printer counts */
3600 r->processor_architecture = 0x0;
3601 r->processor_level = 0x6; /* 6 ???*/
3602 r->ref_ic = 0;
3603 r->reserved2 = 0;
3604 r->reserved3 = 0;
3606 return WERR_OK;
3609 /****************************************************************************
3610 Convert an NT_DEVICEMODE to a spoolss_DeviceMode structure. Both pointers
3611 should be valid upon entry
3612 ****************************************************************************/
3614 static WERROR convert_nt_devicemode(TALLOC_CTX *mem_ctx,
3615 struct spoolss_DeviceMode *r,
3616 const NT_DEVICEMODE *ntdevmode)
3618 if (!r || !ntdevmode) {
3619 return WERR_INVALID_PARAM;
3622 r->devicename = talloc_strdup(mem_ctx, ntdevmode->devicename);
3623 W_ERROR_HAVE_NO_MEMORY(r->devicename);
3625 r->specversion = ntdevmode->specversion;
3626 r->driverversion = ntdevmode->driverversion;
3627 r->size = ntdevmode->size;
3628 r->__driverextra_length = ntdevmode->driverextra;
3629 r->fields = ntdevmode->fields;
3631 r->orientation = ntdevmode->orientation;
3632 r->papersize = ntdevmode->papersize;
3633 r->paperlength = ntdevmode->paperlength;
3634 r->paperwidth = ntdevmode->paperwidth;
3635 r->scale = ntdevmode->scale;
3636 r->copies = ntdevmode->copies;
3637 r->defaultsource = ntdevmode->defaultsource;
3638 r->printquality = ntdevmode->printquality;
3639 r->color = ntdevmode->color;
3640 r->duplex = ntdevmode->duplex;
3641 r->yresolution = ntdevmode->yresolution;
3642 r->ttoption = ntdevmode->ttoption;
3643 r->collate = ntdevmode->collate;
3645 r->formname = talloc_strdup(mem_ctx, ntdevmode->formname);
3646 W_ERROR_HAVE_NO_MEMORY(r->formname);
3648 /* all 0 below are values that have not been set in the old parsing/copy
3649 * function, maybe they should... - gd */
3651 r->logpixels = 0;
3652 r->bitsperpel = 0;
3653 r->pelswidth = 0;
3654 r->pelsheight = 0;
3655 r->displayflags = 0;
3656 r->displayfrequency = 0;
3657 r->icmmethod = ntdevmode->icmmethod;
3658 r->icmintent = ntdevmode->icmintent;
3659 r->mediatype = ntdevmode->mediatype;
3660 r->dithertype = ntdevmode->dithertype;
3661 r->reserved1 = 0;
3662 r->reserved2 = 0;
3663 r->panningwidth = 0;
3664 r->panningheight = 0;
3666 if (ntdevmode->nt_dev_private != NULL) {
3667 r->driverextra_data = data_blob_talloc(mem_ctx,
3668 ntdevmode->nt_dev_private,
3669 ntdevmode->driverextra);
3670 W_ERROR_HAVE_NO_MEMORY(r->driverextra_data.data);
3673 return WERR_OK;
3677 /****************************************************************************
3678 Create a spoolss_DeviceMode struct. Returns talloced memory.
3679 ****************************************************************************/
3681 struct spoolss_DeviceMode *construct_dev_mode(TALLOC_CTX *mem_ctx,
3682 const char *servicename)
3684 WERROR result;
3685 NT_PRINTER_INFO_LEVEL *printer = NULL;
3686 struct spoolss_DeviceMode *devmode = NULL;
3688 DEBUG(7,("construct_dev_mode\n"));
3690 DEBUGADD(8,("getting printer characteristics\n"));
3692 if (!W_ERROR_IS_OK(get_a_printer(NULL, &printer, 2, servicename)))
3693 return NULL;
3695 if (!printer->info_2->devmode) {
3696 DEBUG(5, ("BONG! There was no device mode!\n"));
3697 goto done;
3700 devmode = TALLOC_ZERO_P(mem_ctx, struct spoolss_DeviceMode);
3701 if (!devmode) {
3702 DEBUG(2,("construct_dev_mode: talloc fail.\n"));
3703 goto done;
3706 DEBUGADD(8,("loading DEVICEMODE\n"));
3708 result = convert_nt_devicemode(mem_ctx, devmode, printer->info_2->devmode);
3709 if (!W_ERROR_IS_OK(result)) {
3710 TALLOC_FREE(devmode);
3713 done:
3714 free_a_printer(&printer,2);
3716 return devmode;
3719 /********************************************************************
3720 * construct_printer_info1
3721 * fill a spoolss_PrinterInfo1 struct
3722 ********************************************************************/
3724 static WERROR construct_printer_info1(TALLOC_CTX *mem_ctx,
3725 const NT_PRINTER_INFO_LEVEL *ntprinter,
3726 uint32_t flags,
3727 struct spoolss_PrinterInfo1 *r,
3728 int snum)
3730 r->flags = flags;
3732 r->description = talloc_asprintf(mem_ctx, "%s,%s,%s",
3733 ntprinter->info_2->printername,
3734 ntprinter->info_2->drivername,
3735 ntprinter->info_2->location);
3736 W_ERROR_HAVE_NO_MEMORY(r->description);
3738 if (*ntprinter->info_2->comment == '\0') {
3739 r->comment = talloc_strdup(mem_ctx, lp_comment(snum));
3740 } else {
3741 r->comment = talloc_strdup(mem_ctx, ntprinter->info_2->comment); /* saved comment */
3743 W_ERROR_HAVE_NO_MEMORY(r->comment);
3745 r->name = talloc_strdup(mem_ctx, ntprinter->info_2->printername);
3746 W_ERROR_HAVE_NO_MEMORY(r->name);
3748 return WERR_OK;
3751 /********************************************************************
3752 * construct_printer_info2
3753 * fill a spoolss_PrinterInfo2 struct
3754 ********************************************************************/
3756 static WERROR construct_printer_info2(TALLOC_CTX *mem_ctx,
3757 const NT_PRINTER_INFO_LEVEL *ntprinter,
3758 struct spoolss_PrinterInfo2 *r,
3759 int snum)
3761 int count;
3763 print_status_struct status;
3765 count = print_queue_length(snum, &status);
3767 r->servername = talloc_strdup(mem_ctx, ntprinter->info_2->servername);
3768 W_ERROR_HAVE_NO_MEMORY(r->servername);
3769 r->printername = talloc_strdup(mem_ctx, ntprinter->info_2->printername);
3770 W_ERROR_HAVE_NO_MEMORY(r->printername);
3771 r->sharename = talloc_strdup(mem_ctx, lp_servicename(snum));
3772 W_ERROR_HAVE_NO_MEMORY(r->sharename);
3773 r->portname = talloc_strdup(mem_ctx, ntprinter->info_2->portname);
3774 W_ERROR_HAVE_NO_MEMORY(r->portname);
3775 r->drivername = talloc_strdup(mem_ctx, ntprinter->info_2->drivername);
3776 W_ERROR_HAVE_NO_MEMORY(r->drivername);
3778 if (*ntprinter->info_2->comment == '\0') {
3779 r->comment = talloc_strdup(mem_ctx, lp_comment(snum));
3780 } else {
3781 r->comment = talloc_strdup(mem_ctx, ntprinter->info_2->comment);
3783 W_ERROR_HAVE_NO_MEMORY(r->comment);
3785 r->location = talloc_strdup(mem_ctx, ntprinter->info_2->location);
3786 W_ERROR_HAVE_NO_MEMORY(r->location);
3787 r->sepfile = talloc_strdup(mem_ctx, ntprinter->info_2->sepfile);
3788 W_ERROR_HAVE_NO_MEMORY(r->sepfile);
3789 r->printprocessor = talloc_strdup(mem_ctx, ntprinter->info_2->printprocessor);
3790 W_ERROR_HAVE_NO_MEMORY(r->printprocessor);
3791 r->datatype = talloc_strdup(mem_ctx, ntprinter->info_2->datatype);
3792 W_ERROR_HAVE_NO_MEMORY(r->datatype);
3793 r->parameters = talloc_strdup(mem_ctx, ntprinter->info_2->parameters);
3794 W_ERROR_HAVE_NO_MEMORY(r->parameters);
3796 r->attributes = ntprinter->info_2->attributes;
3798 r->priority = ntprinter->info_2->priority;
3799 r->defaultpriority = ntprinter->info_2->default_priority;
3800 r->starttime = ntprinter->info_2->starttime;
3801 r->untiltime = ntprinter->info_2->untiltime;
3802 r->status = nt_printq_status(status.status);
3803 r->cjobs = count;
3804 r->averageppm = ntprinter->info_2->averageppm;
3806 r->devmode = construct_dev_mode(mem_ctx, lp_const_servicename(snum));
3807 if (!r->devmode) {
3808 DEBUG(8,("Returning NULL Devicemode!\n"));
3811 r->secdesc = NULL;
3813 if (ntprinter->info_2->secdesc_buf && ntprinter->info_2->secdesc_buf->sd_size != 0) {
3814 /* don't use talloc_steal() here unless you do a deep steal of all
3815 the SEC_DESC members */
3817 r->secdesc = dup_sec_desc(mem_ctx, ntprinter->info_2->secdesc_buf->sd);
3820 return WERR_OK;
3823 /********************************************************************
3824 * construct_printer_info3
3825 * fill a spoolss_PrinterInfo3 struct
3826 ********************************************************************/
3828 static WERROR construct_printer_info3(TALLOC_CTX *mem_ctx,
3829 const NT_PRINTER_INFO_LEVEL *ntprinter,
3830 struct spoolss_PrinterInfo3 *r,
3831 int snum)
3833 /* These are the components of the SD we are returning. */
3835 if (ntprinter->info_2->secdesc_buf && ntprinter->info_2->secdesc_buf->sd_size != 0) {
3836 /* don't use talloc_steal() here unless you do a deep steal of all
3837 the SEC_DESC members */
3839 r->secdesc = dup_sec_desc(mem_ctx,
3840 ntprinter->info_2->secdesc_buf->sd);
3841 W_ERROR_HAVE_NO_MEMORY(r->secdesc);
3844 return WERR_OK;
3847 /********************************************************************
3848 * construct_printer_info4
3849 * fill a spoolss_PrinterInfo4 struct
3850 ********************************************************************/
3852 static WERROR construct_printer_info4(TALLOC_CTX *mem_ctx,
3853 const NT_PRINTER_INFO_LEVEL *ntprinter,
3854 struct spoolss_PrinterInfo4 *r,
3855 int snum)
3857 r->printername = talloc_strdup(mem_ctx, ntprinter->info_2->printername);
3858 W_ERROR_HAVE_NO_MEMORY(r->printername);
3859 r->servername = talloc_strdup(mem_ctx, ntprinter->info_2->servername);
3860 W_ERROR_HAVE_NO_MEMORY(r->servername);
3862 r->attributes = ntprinter->info_2->attributes;
3864 return WERR_OK;
3867 /********************************************************************
3868 * construct_printer_info5
3869 * fill a spoolss_PrinterInfo5 struct
3870 ********************************************************************/
3872 static WERROR construct_printer_info5(TALLOC_CTX *mem_ctx,
3873 const NT_PRINTER_INFO_LEVEL *ntprinter,
3874 struct spoolss_PrinterInfo5 *r,
3875 int snum)
3877 r->printername = talloc_strdup(mem_ctx, ntprinter->info_2->printername);
3878 W_ERROR_HAVE_NO_MEMORY(r->printername);
3879 r->portname = talloc_strdup(mem_ctx, ntprinter->info_2->portname);
3880 W_ERROR_HAVE_NO_MEMORY(r->portname);
3882 r->attributes = ntprinter->info_2->attributes;
3884 /* these two are not used by NT+ according to MSDN */
3886 r->device_not_selected_timeout = 0x0; /* have seen 0x3a98 */
3887 r->transmission_retry_timeout = 0x0; /* have seen 0xafc8 */
3889 return WERR_OK;
3892 /********************************************************************
3893 * construct_printer_info_6
3894 * fill a spoolss_PrinterInfo6 struct
3895 ********************************************************************/
3897 static WERROR construct_printer_info6(TALLOC_CTX *mem_ctx,
3898 const NT_PRINTER_INFO_LEVEL *ntprinter,
3899 struct spoolss_PrinterInfo6 *r,
3900 int snum)
3902 int count;
3903 print_status_struct status;
3905 count = print_queue_length(snum, &status);
3907 r->status = nt_printq_status(status.status);
3909 return WERR_OK;
3912 /********************************************************************
3913 * construct_printer_info7
3914 * fill a spoolss_PrinterInfo7 struct
3915 ********************************************************************/
3917 static WERROR construct_printer_info7(TALLOC_CTX *mem_ctx,
3918 Printer_entry *print_hnd,
3919 struct spoolss_PrinterInfo7 *r,
3920 int snum)
3922 struct GUID guid;
3924 if (is_printer_published(print_hnd, snum, &guid)) {
3925 r->guid = talloc_strdup_upper(mem_ctx, GUID_string2(mem_ctx, &guid));
3926 r->action = DSPRINT_PUBLISH;
3927 } else {
3928 r->guid = talloc_strdup(mem_ctx, "");
3929 r->action = DSPRINT_UNPUBLISH;
3931 W_ERROR_HAVE_NO_MEMORY(r->guid);
3933 return WERR_OK;
3936 /********************************************************************
3937 * construct_printer_info8
3938 * fill a spoolss_PrinterInfo8 struct
3939 ********************************************************************/
3941 static WERROR construct_printer_info8(TALLOC_CTX *mem_ctx,
3942 const NT_PRINTER_INFO_LEVEL *ntprinter,
3943 struct spoolss_DeviceModeInfo *r,
3944 int snum)
3946 struct spoolss_DeviceMode *devmode;
3947 WERROR result;
3949 if (!ntprinter->info_2->devmode) {
3950 r->devmode = NULL;
3951 return WERR_OK;
3954 devmode = TALLOC_ZERO_P(mem_ctx, struct spoolss_DeviceMode);
3955 W_ERROR_HAVE_NO_MEMORY(devmode);
3957 result = convert_nt_devicemode(mem_ctx, devmode, ntprinter->info_2->devmode);
3958 if (!W_ERROR_IS_OK(result)) {
3959 TALLOC_FREE(devmode);
3960 return result;
3963 r->devmode = devmode;
3965 return WERR_OK;
3969 /********************************************************************
3970 ********************************************************************/
3972 static bool snum_is_shared_printer(int snum)
3974 return (lp_browseable(snum) && lp_snum_ok(snum) && lp_print_ok(snum));
3977 /********************************************************************
3978 Spoolss_enumprinters.
3979 ********************************************************************/
3981 static WERROR enum_all_printers_info_level(TALLOC_CTX *mem_ctx,
3982 uint32_t level,
3983 uint32_t flags,
3984 union spoolss_PrinterInfo **info_p,
3985 uint32_t *count_p)
3987 int snum;
3988 int n_services = lp_numservices();
3989 union spoolss_PrinterInfo *info = NULL;
3990 uint32_t count = 0;
3991 WERROR result = WERR_OK;
3993 *count_p = 0;
3994 *info_p = NULL;
3996 for (snum = 0; snum < n_services; snum++) {
3998 NT_PRINTER_INFO_LEVEL *ntprinter = NULL;
4000 if (!snum_is_shared_printer(snum)) {
4001 continue;
4004 DEBUG(4,("Found a printer in smb.conf: %s[%x]\n",
4005 lp_servicename(snum), snum));
4007 info = TALLOC_REALLOC_ARRAY(mem_ctx, info,
4008 union spoolss_PrinterInfo,
4009 count + 1);
4010 if (!info) {
4011 result = WERR_NOMEM;
4012 goto out;
4015 result = get_a_printer(NULL, &ntprinter, 2,
4016 lp_const_servicename(snum));
4017 if (!W_ERROR_IS_OK(result)) {
4018 goto out;
4021 switch (level) {
4022 case 0:
4023 result = construct_printer_info0(info, ntprinter,
4024 &info[count].info0, snum);
4025 break;
4026 case 1:
4027 result = construct_printer_info1(info, ntprinter, flags,
4028 &info[count].info1, snum);
4029 break;
4030 case 2:
4031 result = construct_printer_info2(info, ntprinter,
4032 &info[count].info2, snum);
4033 break;
4034 case 4:
4035 result = construct_printer_info4(info, ntprinter,
4036 &info[count].info4, snum);
4037 break;
4038 case 5:
4039 result = construct_printer_info5(info, ntprinter,
4040 &info[count].info5, snum);
4041 break;
4043 default:
4044 result = WERR_UNKNOWN_LEVEL;
4045 free_a_printer(&ntprinter, 2);
4046 goto out;
4049 free_a_printer(&ntprinter, 2);
4050 if (!W_ERROR_IS_OK(result)) {
4051 goto out;
4054 count++;
4057 *count_p = count;
4058 *info_p = info;
4060 out:
4061 if (!W_ERROR_IS_OK(result)) {
4062 TALLOC_FREE(info);
4063 return result;
4066 *info_p = info;
4068 return WERR_OK;
4071 /********************************************************************
4072 * handle enumeration of printers at level 0
4073 ********************************************************************/
4075 static WERROR enumprinters_level0(TALLOC_CTX *mem_ctx,
4076 uint32_t flags,
4077 const char *servername,
4078 union spoolss_PrinterInfo **info,
4079 uint32_t *count)
4081 DEBUG(4,("enum_all_printers_info_0\n"));
4083 return enum_all_printers_info_level(mem_ctx, 0, flags, info, count);
4087 /********************************************************************
4088 ********************************************************************/
4090 static WERROR enum_all_printers_info_1(TALLOC_CTX *mem_ctx,
4091 uint32_t flags,
4092 union spoolss_PrinterInfo **info,
4093 uint32_t *count)
4095 DEBUG(4,("enum_all_printers_info_1\n"));
4097 return enum_all_printers_info_level(mem_ctx, 1, flags, info, count);
4100 /********************************************************************
4101 enum_all_printers_info_1_local.
4102 *********************************************************************/
4104 static WERROR enum_all_printers_info_1_local(TALLOC_CTX *mem_ctx,
4105 union spoolss_PrinterInfo **info,
4106 uint32_t *count)
4108 DEBUG(4,("enum_all_printers_info_1_local\n"));
4110 return enum_all_printers_info_1(mem_ctx, PRINTER_ENUM_ICON8, info, count);
4113 /********************************************************************
4114 enum_all_printers_info_1_name.
4115 *********************************************************************/
4117 static WERROR enum_all_printers_info_1_name(TALLOC_CTX *mem_ctx,
4118 const char *name,
4119 union spoolss_PrinterInfo **info,
4120 uint32_t *count)
4122 const char *s = name;
4124 DEBUG(4,("enum_all_printers_info_1_name\n"));
4126 if ((name[0] == '\\') && (name[1] == '\\')) {
4127 s = name + 2;
4130 if (!is_myname_or_ipaddr(s)) {
4131 return WERR_INVALID_NAME;
4134 return enum_all_printers_info_1(mem_ctx, PRINTER_ENUM_ICON8, info, count);
4137 /********************************************************************
4138 enum_all_printers_info_1_network.
4139 *********************************************************************/
4141 static WERROR enum_all_printers_info_1_network(TALLOC_CTX *mem_ctx,
4142 const char *name,
4143 union spoolss_PrinterInfo **info,
4144 uint32_t *count)
4146 const char *s = name;
4148 DEBUG(4,("enum_all_printers_info_1_network\n"));
4150 /* If we respond to a enum_printers level 1 on our name with flags
4151 set to PRINTER_ENUM_REMOTE with a list of printers then these
4152 printers incorrectly appear in the APW browse list.
4153 Specifically the printers for the server appear at the workgroup
4154 level where all the other servers in the domain are
4155 listed. Windows responds to this call with a
4156 WERR_CAN_NOT_COMPLETE so we should do the same. */
4158 if (name[0] == '\\' && name[1] == '\\') {
4159 s = name + 2;
4162 if (is_myname_or_ipaddr(s)) {
4163 return WERR_CAN_NOT_COMPLETE;
4166 return enum_all_printers_info_1(mem_ctx, PRINTER_ENUM_NAME, info, count);
4169 /********************************************************************
4170 * api_spoolss_enumprinters
4172 * called from api_spoolss_enumprinters (see this to understand)
4173 ********************************************************************/
4175 static WERROR enum_all_printers_info_2(TALLOC_CTX *mem_ctx,
4176 union spoolss_PrinterInfo **info,
4177 uint32_t *count)
4179 DEBUG(4,("enum_all_printers_info_2\n"));
4181 return enum_all_printers_info_level(mem_ctx, 2, 0, info, count);
4184 /********************************************************************
4185 * handle enumeration of printers at level 1
4186 ********************************************************************/
4188 static WERROR enumprinters_level1(TALLOC_CTX *mem_ctx,
4189 uint32_t flags,
4190 const char *name,
4191 union spoolss_PrinterInfo **info,
4192 uint32_t *count)
4194 /* Not all the flags are equals */
4196 if (flags & PRINTER_ENUM_LOCAL) {
4197 return enum_all_printers_info_1_local(mem_ctx, info, count);
4200 if (flags & PRINTER_ENUM_NAME) {
4201 return enum_all_printers_info_1_name(mem_ctx, name, info, count);
4204 if (flags & PRINTER_ENUM_NETWORK) {
4205 return enum_all_printers_info_1_network(mem_ctx, name, info, count);
4208 return WERR_OK; /* NT4sp5 does that */
4211 /********************************************************************
4212 * handle enumeration of printers at level 2
4213 ********************************************************************/
4215 static WERROR enumprinters_level2(TALLOC_CTX *mem_ctx,
4216 uint32_t flags,
4217 const char *servername,
4218 union spoolss_PrinterInfo **info,
4219 uint32_t *count)
4221 if (flags & PRINTER_ENUM_LOCAL) {
4222 return enum_all_printers_info_2(mem_ctx, info, count);
4225 if (flags & PRINTER_ENUM_NAME) {
4226 if (!is_myname_or_ipaddr(canon_servername(servername))) {
4227 return WERR_INVALID_NAME;
4230 return enum_all_printers_info_2(mem_ctx, info, count);
4233 if (flags & PRINTER_ENUM_REMOTE) {
4234 return WERR_UNKNOWN_LEVEL;
4237 return WERR_OK;
4240 /********************************************************************
4241 * handle enumeration of printers at level 4
4242 ********************************************************************/
4244 static WERROR enumprinters_level4(TALLOC_CTX *mem_ctx,
4245 uint32_t flags,
4246 const char *servername,
4247 union spoolss_PrinterInfo **info,
4248 uint32_t *count)
4250 DEBUG(4,("enum_all_printers_info_4\n"));
4252 return enum_all_printers_info_level(mem_ctx, 4, flags, info, count);
4256 /********************************************************************
4257 * handle enumeration of printers at level 5
4258 ********************************************************************/
4260 static WERROR enumprinters_level5(TALLOC_CTX *mem_ctx,
4261 uint32_t flags,
4262 const char *servername,
4263 union spoolss_PrinterInfo **info,
4264 uint32_t *count)
4266 DEBUG(4,("enum_all_printers_info_5\n"));
4268 return enum_all_printers_info_level(mem_ctx, 5, flags, info, count);
4271 /****************************************************************
4272 _spoolss_EnumPrinters
4273 ****************************************************************/
4275 WERROR _spoolss_EnumPrinters(pipes_struct *p,
4276 struct spoolss_EnumPrinters *r)
4278 const char *name;
4279 WERROR result;
4281 /* that's an [in out] buffer */
4283 if (!r->in.buffer && (r->in.offered != 0)) {
4284 return WERR_INVALID_PARAM;
4287 DEBUG(4,("_spoolss_EnumPrinters\n"));
4289 *r->out.needed = 0;
4290 *r->out.count = 0;
4291 *r->out.info = NULL;
4294 * Level 1:
4295 * flags==PRINTER_ENUM_NAME
4296 * if name=="" then enumerates all printers
4297 * if name!="" then enumerate the printer
4298 * flags==PRINTER_ENUM_REMOTE
4299 * name is NULL, enumerate printers
4300 * Level 2: name!="" enumerates printers, name can't be NULL
4301 * Level 3: doesn't exist
4302 * Level 4: does a local registry lookup
4303 * Level 5: same as Level 2
4306 name = talloc_strdup_upper(p->mem_ctx, r->in.server);
4307 W_ERROR_HAVE_NO_MEMORY(name);
4309 switch (r->in.level) {
4310 case 0:
4311 result = enumprinters_level0(p->mem_ctx, r->in.flags, name,
4312 r->out.info, r->out.count);
4313 break;
4314 case 1:
4315 result = enumprinters_level1(p->mem_ctx, r->in.flags, name,
4316 r->out.info, r->out.count);
4317 break;
4318 case 2:
4319 result = enumprinters_level2(p->mem_ctx, r->in.flags, name,
4320 r->out.info, r->out.count);
4321 break;
4322 case 4:
4323 result = enumprinters_level4(p->mem_ctx, r->in.flags, name,
4324 r->out.info, r->out.count);
4325 break;
4326 case 5:
4327 result = enumprinters_level5(p->mem_ctx, r->in.flags, name,
4328 r->out.info, r->out.count);
4329 break;
4330 default:
4331 return WERR_UNKNOWN_LEVEL;
4334 if (!W_ERROR_IS_OK(result)) {
4335 return result;
4338 *r->out.needed = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
4339 spoolss_EnumPrinters, NULL,
4340 *r->out.info, r->in.level,
4341 *r->out.count);
4342 *r->out.info = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
4343 *r->out.count = SPOOLSS_BUFFER_OK(*r->out.count, 0);
4345 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
4348 /****************************************************************
4349 _spoolss_GetPrinter
4350 ****************************************************************/
4352 WERROR _spoolss_GetPrinter(pipes_struct *p,
4353 struct spoolss_GetPrinter *r)
4355 Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
4356 NT_PRINTER_INFO_LEVEL *ntprinter = NULL;
4357 WERROR result = WERR_OK;
4359 int snum;
4361 /* that's an [in out] buffer */
4363 if (!r->in.buffer && (r->in.offered != 0)) {
4364 return WERR_INVALID_PARAM;
4367 *r->out.needed = 0;
4369 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
4370 return WERR_BADFID;
4373 result = get_a_printer(Printer, &ntprinter, 2,
4374 lp_const_servicename(snum));
4375 if (!W_ERROR_IS_OK(result)) {
4376 return result;
4379 switch (r->in.level) {
4380 case 0:
4381 result = construct_printer_info0(p->mem_ctx, ntprinter,
4382 &r->out.info->info0, snum);
4383 break;
4384 case 1:
4385 result = construct_printer_info1(p->mem_ctx, ntprinter,
4386 PRINTER_ENUM_ICON8,
4387 &r->out.info->info1, snum);
4388 break;
4389 case 2:
4390 result = construct_printer_info2(p->mem_ctx, ntprinter,
4391 &r->out.info->info2, snum);
4392 break;
4393 case 3:
4394 result = construct_printer_info3(p->mem_ctx, ntprinter,
4395 &r->out.info->info3, snum);
4396 break;
4397 case 4:
4398 result = construct_printer_info4(p->mem_ctx, ntprinter,
4399 &r->out.info->info4, snum);
4400 break;
4401 case 5:
4402 result = construct_printer_info5(p->mem_ctx, ntprinter,
4403 &r->out.info->info5, snum);
4404 break;
4405 case 6:
4406 result = construct_printer_info6(p->mem_ctx, ntprinter,
4407 &r->out.info->info6, snum);
4408 break;
4409 case 7:
4410 result = construct_printer_info7(p->mem_ctx, Printer,
4411 &r->out.info->info7, snum);
4412 break;
4413 case 8:
4414 result = construct_printer_info8(p->mem_ctx, ntprinter,
4415 &r->out.info->info8, snum);
4416 break;
4417 default:
4418 result = WERR_UNKNOWN_LEVEL;
4419 break;
4422 free_a_printer(&ntprinter, 2);
4424 if (!W_ERROR_IS_OK(result)) {
4425 TALLOC_FREE(r->out.info);
4426 return result;
4429 *r->out.needed = SPOOLSS_BUFFER_UNION(spoolss_PrinterInfo, NULL,
4430 r->out.info, r->in.level);
4431 r->out.info = SPOOLSS_BUFFER_OK(r->out.info, NULL);
4433 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
4436 /********************************************************************
4437 ********************************************************************/
4439 static const char **string_array_from_driver_info(TALLOC_CTX *mem_ctx,
4440 const char **string_array,
4441 const char *cservername)
4443 int i, num_strings = 0;
4444 const char **array = NULL;
4446 if (!string_array) {
4447 return NULL;
4450 for (i=0; string_array[i] && string_array[i][0] != '\0'; i++) {
4452 const char *str = talloc_asprintf(mem_ctx, "\\\\%s%s",
4453 cservername, string_array[i]);
4454 if (!str) {
4455 TALLOC_FREE(array);
4456 return NULL;
4460 if (!add_string_to_array(mem_ctx, str, &array, &num_strings)) {
4461 TALLOC_FREE(array);
4462 return NULL;
4466 if (i > 0) {
4467 ADD_TO_ARRAY(mem_ctx, const char *, NULL,
4468 &array, &num_strings);
4471 return array;
4474 #define FILL_DRIVER_STRING(mem_ctx, in, out) \
4475 do { \
4476 if (in && strlen(in)) { \
4477 out = talloc_strdup(mem_ctx, in); \
4478 W_ERROR_HAVE_NO_MEMORY(out); \
4479 } else { \
4480 out = NULL; \
4482 } while (0);
4484 #define FILL_DRIVER_UNC_STRING(mem_ctx, server, in, out) \
4485 do { \
4486 if (in && strlen(in)) { \
4487 out = talloc_asprintf(mem_ctx, "\\\\%s%s", server, in); \
4488 } else { \
4489 out = talloc_strdup(mem_ctx, ""); \
4491 W_ERROR_HAVE_NO_MEMORY(out); \
4492 } while (0);
4494 /********************************************************************
4495 * fill a spoolss_DriverInfo1 struct
4496 ********************************************************************/
4498 static WERROR fill_printer_driver_info1(TALLOC_CTX *mem_ctx,
4499 struct spoolss_DriverInfo1 *r,
4500 const struct spoolss_DriverInfo8 *driver,
4501 const char *servername)
4503 r->driver_name = talloc_strdup(mem_ctx, driver->driver_name);
4504 W_ERROR_HAVE_NO_MEMORY(r->driver_name);
4506 return WERR_OK;
4509 /********************************************************************
4510 * fill a spoolss_DriverInfo2 struct
4511 ********************************************************************/
4513 static WERROR fill_printer_driver_info2(TALLOC_CTX *mem_ctx,
4514 struct spoolss_DriverInfo2 *r,
4515 const struct spoolss_DriverInfo8 *driver,
4516 const char *servername)
4519 const char *cservername = canon_servername(servername);
4521 r->version = driver->version;
4523 r->driver_name = talloc_strdup(mem_ctx, driver->driver_name);
4524 W_ERROR_HAVE_NO_MEMORY(r->driver_name);
4525 r->architecture = talloc_strdup(mem_ctx, driver->architecture);
4526 W_ERROR_HAVE_NO_MEMORY(r->architecture);
4528 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4529 driver->driver_path,
4530 r->driver_path);
4532 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4533 driver->data_file,
4534 r->data_file);
4536 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4537 driver->config_file,
4538 r->config_file);
4540 return WERR_OK;
4543 /********************************************************************
4544 * fill a spoolss_DriverInfo3 struct
4545 ********************************************************************/
4547 static WERROR fill_printer_driver_info3(TALLOC_CTX *mem_ctx,
4548 struct spoolss_DriverInfo3 *r,
4549 const struct spoolss_DriverInfo8 *driver,
4550 const char *servername)
4552 const char *cservername = canon_servername(servername);
4554 r->version = driver->version;
4556 r->driver_name = talloc_strdup(mem_ctx, driver->driver_name);
4557 W_ERROR_HAVE_NO_MEMORY(r->driver_name);
4558 r->architecture = talloc_strdup(mem_ctx, driver->architecture);
4559 W_ERROR_HAVE_NO_MEMORY(r->architecture);
4561 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4562 driver->driver_path,
4563 r->driver_path);
4565 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4566 driver->data_file,
4567 r->data_file);
4569 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4570 driver->config_file,
4571 r->config_file);
4573 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4574 driver->help_file,
4575 r->help_file);
4577 FILL_DRIVER_STRING(mem_ctx,
4578 driver->monitor_name,
4579 r->monitor_name);
4581 FILL_DRIVER_STRING(mem_ctx,
4582 driver->default_datatype,
4583 r->default_datatype);
4585 r->dependent_files = string_array_from_driver_info(mem_ctx,
4586 driver->dependent_files,
4587 cservername);
4588 return WERR_OK;
4591 /********************************************************************
4592 * fill a spoolss_DriverInfo4 struct
4593 ********************************************************************/
4595 static WERROR fill_printer_driver_info4(TALLOC_CTX *mem_ctx,
4596 struct spoolss_DriverInfo4 *r,
4597 const struct spoolss_DriverInfo8 *driver,
4598 const char *servername)
4600 const char *cservername = canon_servername(servername);
4602 r->version = driver->version;
4604 r->driver_name = talloc_strdup(mem_ctx, driver->driver_name);
4605 W_ERROR_HAVE_NO_MEMORY(r->driver_name);
4606 r->architecture = talloc_strdup(mem_ctx, driver->architecture);
4607 W_ERROR_HAVE_NO_MEMORY(r->architecture);
4609 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4610 driver->driver_path,
4611 r->driver_path);
4613 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4614 driver->data_file,
4615 r->data_file);
4617 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4618 driver->config_file,
4619 r->config_file);
4621 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4622 driver->help_file,
4623 r->help_file);
4625 r->dependent_files = string_array_from_driver_info(mem_ctx,
4626 driver->dependent_files,
4627 cservername);
4629 FILL_DRIVER_STRING(mem_ctx,
4630 driver->monitor_name,
4631 r->monitor_name);
4633 FILL_DRIVER_STRING(mem_ctx,
4634 driver->default_datatype,
4635 r->default_datatype);
4637 r->previous_names = string_array_from_driver_info(mem_ctx,
4638 driver->previous_names,
4639 cservername);
4641 return WERR_OK;
4644 /********************************************************************
4645 * fill a spoolss_DriverInfo5 struct
4646 ********************************************************************/
4648 static WERROR fill_printer_driver_info5(TALLOC_CTX *mem_ctx,
4649 struct spoolss_DriverInfo5 *r,
4650 const struct spoolss_DriverInfo8 *driver,
4651 const char *servername)
4653 const char *cservername = canon_servername(servername);
4655 r->version = driver->version;
4657 r->driver_name = talloc_strdup(mem_ctx, driver->driver_name);
4658 W_ERROR_HAVE_NO_MEMORY(r->driver_name);
4659 r->architecture = talloc_strdup(mem_ctx, driver->architecture);
4660 W_ERROR_HAVE_NO_MEMORY(r->architecture);
4662 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4663 driver->driver_path,
4664 r->driver_path);
4666 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4667 driver->data_file,
4668 r->data_file);
4670 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4671 driver->config_file,
4672 r->config_file);
4674 r->driver_attributes = 0;
4675 r->config_version = 0;
4676 r->driver_version = 0;
4678 return WERR_OK;
4680 /********************************************************************
4681 * fill a spoolss_DriverInfo6 struct
4682 ********************************************************************/
4684 static WERROR fill_printer_driver_info6(TALLOC_CTX *mem_ctx,
4685 struct spoolss_DriverInfo6 *r,
4686 const struct spoolss_DriverInfo8 *driver,
4687 const char *servername)
4689 const char *cservername = canon_servername(servername);
4691 r->version = driver->version;
4693 r->driver_name = talloc_strdup(mem_ctx, driver->driver_name);
4694 W_ERROR_HAVE_NO_MEMORY(r->driver_name);
4695 r->architecture = talloc_strdup(mem_ctx, driver->architecture);
4696 W_ERROR_HAVE_NO_MEMORY(r->architecture);
4698 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4699 driver->driver_path,
4700 r->driver_path);
4702 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4703 driver->data_file,
4704 r->data_file);
4706 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4707 driver->config_file,
4708 r->config_file);
4710 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4711 driver->help_file,
4712 r->help_file);
4714 FILL_DRIVER_STRING(mem_ctx,
4715 driver->monitor_name,
4716 r->monitor_name);
4718 FILL_DRIVER_STRING(mem_ctx,
4719 driver->default_datatype,
4720 r->default_datatype);
4722 r->dependent_files = string_array_from_driver_info(mem_ctx,
4723 driver->dependent_files,
4724 cservername);
4725 r->previous_names = string_array_from_driver_info(mem_ctx,
4726 driver->previous_names,
4727 cservername);
4729 r->driver_date = driver->driver_date;
4730 r->driver_version = driver->driver_version;
4732 FILL_DRIVER_STRING(mem_ctx,
4733 driver->manufacturer_name,
4734 r->manufacturer_name);
4735 FILL_DRIVER_STRING(mem_ctx,
4736 driver->manufacturer_url,
4737 r->manufacturer_url);
4738 FILL_DRIVER_STRING(mem_ctx,
4739 driver->hardware_id,
4740 r->hardware_id);
4741 FILL_DRIVER_STRING(mem_ctx,
4742 driver->provider,
4743 r->provider);
4745 return WERR_OK;
4748 /********************************************************************
4749 * fill a spoolss_DriverInfo8 struct
4750 ********************************************************************/
4752 static WERROR fill_printer_driver_info8(TALLOC_CTX *mem_ctx,
4753 struct spoolss_DriverInfo8 *r,
4754 const struct spoolss_DriverInfo8 *driver,
4755 const char *servername)
4757 const char *cservername = canon_servername(servername);
4759 r->version = driver->version;
4761 r->driver_name = talloc_strdup(mem_ctx, driver->driver_name);
4762 W_ERROR_HAVE_NO_MEMORY(r->driver_name);
4763 r->architecture = talloc_strdup(mem_ctx, driver->architecture);
4764 W_ERROR_HAVE_NO_MEMORY(r->architecture);
4766 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4767 driver->driver_path,
4768 r->driver_path);
4770 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4771 driver->data_file,
4772 r->data_file);
4774 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4775 driver->config_file,
4776 r->config_file);
4778 FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4779 driver->help_file,
4780 r->help_file);
4782 FILL_DRIVER_STRING(mem_ctx,
4783 driver->monitor_name,
4784 r->monitor_name);
4786 FILL_DRIVER_STRING(mem_ctx,
4787 driver->default_datatype,
4788 r->default_datatype);
4790 r->dependent_files = string_array_from_driver_info(mem_ctx,
4791 driver->dependent_files,
4792 cservername);
4793 r->previous_names = string_array_from_driver_info(mem_ctx,
4794 driver->previous_names,
4795 cservername);
4797 r->driver_date = driver->driver_date;
4798 r->driver_version = driver->driver_version;
4800 FILL_DRIVER_STRING(mem_ctx,
4801 driver->manufacturer_name,
4802 r->manufacturer_name);
4803 FILL_DRIVER_STRING(mem_ctx,
4804 driver->manufacturer_url,
4805 r->manufacturer_url);
4806 FILL_DRIVER_STRING(mem_ctx,
4807 driver->hardware_id,
4808 r->hardware_id);
4809 FILL_DRIVER_STRING(mem_ctx,
4810 driver->provider,
4811 r->provider);
4813 FILL_DRIVER_STRING(mem_ctx,
4814 driver->print_processor,
4815 r->print_processor);
4816 FILL_DRIVER_STRING(mem_ctx,
4817 driver->vendor_setup,
4818 r->vendor_setup);
4820 r->color_profiles = string_array_from_driver_info(mem_ctx,
4821 driver->color_profiles,
4822 cservername);
4824 FILL_DRIVER_STRING(mem_ctx,
4825 driver->inf_path,
4826 r->inf_path);
4828 r->printer_driver_attributes = driver->printer_driver_attributes;
4830 r->core_driver_dependencies = string_array_from_driver_info(mem_ctx,
4831 driver->core_driver_dependencies,
4832 cservername);
4834 r->min_inbox_driver_ver_date = driver->min_inbox_driver_ver_date;
4835 r->min_inbox_driver_ver_version = driver->min_inbox_driver_ver_version;
4837 return WERR_OK;
4839 /********************************************************************
4840 ********************************************************************/
4842 static WERROR fill_spoolss_DriverFileInfo(TALLOC_CTX *mem_ctx,
4843 struct spoolss_DriverFileInfo *r,
4844 const char *cservername,
4845 const char *file_name,
4846 enum spoolss_DriverFileType file_type,
4847 uint32_t file_version)
4849 r->file_name = talloc_asprintf(mem_ctx, "\\\\%s%s",
4850 cservername, file_name);
4851 W_ERROR_HAVE_NO_MEMORY(r->file_name);
4852 r->file_type = file_type;
4853 r->file_version = file_version;
4855 return WERR_OK;
4858 /********************************************************************
4859 ********************************************************************/
4861 static WERROR spoolss_DriverFileInfo_from_driver(TALLOC_CTX *mem_ctx,
4862 const struct spoolss_DriverInfo8 *driver,
4863 const char *cservername,
4864 struct spoolss_DriverFileInfo **info_p,
4865 uint32_t *count_p)
4867 struct spoolss_DriverFileInfo *info = NULL;
4868 uint32_t count = 0;
4869 WERROR result;
4870 uint32_t i;
4872 *info_p = NULL;
4873 *count_p = 0;
4875 if (strlen(driver->driver_path)) {
4876 info = TALLOC_REALLOC_ARRAY(mem_ctx, info,
4877 struct spoolss_DriverFileInfo,
4878 count + 1);
4879 W_ERROR_HAVE_NO_MEMORY(info);
4880 result = fill_spoolss_DriverFileInfo(info,
4881 &info[count],
4882 cservername,
4883 driver->driver_path,
4884 SPOOLSS_DRIVER_FILE_TYPE_RENDERING,
4886 W_ERROR_NOT_OK_RETURN(result);
4887 count++;
4890 if (strlen(driver->config_file)) {
4891 info = TALLOC_REALLOC_ARRAY(mem_ctx, info,
4892 struct spoolss_DriverFileInfo,
4893 count + 1);
4894 W_ERROR_HAVE_NO_MEMORY(info);
4895 result = fill_spoolss_DriverFileInfo(info,
4896 &info[count],
4897 cservername,
4898 driver->config_file,
4899 SPOOLSS_DRIVER_FILE_TYPE_CONFIGURATION,
4901 W_ERROR_NOT_OK_RETURN(result);
4902 count++;
4905 if (strlen(driver->data_file)) {
4906 info = TALLOC_REALLOC_ARRAY(mem_ctx, info,
4907 struct spoolss_DriverFileInfo,
4908 count + 1);
4909 W_ERROR_HAVE_NO_MEMORY(info);
4910 result = fill_spoolss_DriverFileInfo(info,
4911 &info[count],
4912 cservername,
4913 driver->data_file,
4914 SPOOLSS_DRIVER_FILE_TYPE_DATA,
4916 W_ERROR_NOT_OK_RETURN(result);
4917 count++;
4920 if (strlen(driver->help_file)) {
4921 info = TALLOC_REALLOC_ARRAY(mem_ctx, info,
4922 struct spoolss_DriverFileInfo,
4923 count + 1);
4924 W_ERROR_HAVE_NO_MEMORY(info);
4925 result = fill_spoolss_DriverFileInfo(info,
4926 &info[count],
4927 cservername,
4928 driver->help_file,
4929 SPOOLSS_DRIVER_FILE_TYPE_HELP,
4931 W_ERROR_NOT_OK_RETURN(result);
4932 count++;
4935 for (i=0; driver->dependent_files[i] && driver->dependent_files[i][0] != '\0'; i++) {
4936 info = TALLOC_REALLOC_ARRAY(mem_ctx, info,
4937 struct spoolss_DriverFileInfo,
4938 count + 1);
4939 W_ERROR_HAVE_NO_MEMORY(info);
4940 result = fill_spoolss_DriverFileInfo(info,
4941 &info[count],
4942 cservername,
4943 driver->dependent_files[i],
4944 SPOOLSS_DRIVER_FILE_TYPE_OTHER,
4946 W_ERROR_NOT_OK_RETURN(result);
4947 count++;
4950 *info_p = info;
4951 *count_p = count;
4953 return WERR_OK;
4956 /********************************************************************
4957 * fill a spoolss_DriverInfo101 sttruct
4958 ********************************************************************/
4960 static WERROR fill_printer_driver_info101(TALLOC_CTX *mem_ctx,
4961 struct spoolss_DriverInfo101 *r,
4962 const struct spoolss_DriverInfo8 *driver,
4963 const char *servername)
4965 const char *cservername = canon_servername(servername);
4966 WERROR result;
4968 r->version = driver->version;
4970 r->driver_name = talloc_strdup(mem_ctx, driver->driver_name);
4971 W_ERROR_HAVE_NO_MEMORY(r->driver_name);
4972 r->architecture = talloc_strdup(mem_ctx, driver->architecture);
4973 W_ERROR_HAVE_NO_MEMORY(r->architecture);
4975 result = spoolss_DriverFileInfo_from_driver(mem_ctx, driver,
4976 cservername,
4977 &r->file_info,
4978 &r->file_count);
4979 if (!W_ERROR_IS_OK(result)) {
4980 return result;
4983 FILL_DRIVER_STRING(mem_ctx,
4984 driver->monitor_name,
4985 r->monitor_name);
4987 FILL_DRIVER_STRING(mem_ctx,
4988 driver->default_datatype,
4989 r->default_datatype);
4991 r->previous_names = string_array_from_driver_info(mem_ctx,
4992 driver->previous_names,
4993 cservername);
4994 r->driver_date = driver->driver_date;
4995 r->driver_version = driver->driver_version;
4997 FILL_DRIVER_STRING(mem_ctx,
4998 driver->manufacturer_name,
4999 r->manufacturer_name);
5000 FILL_DRIVER_STRING(mem_ctx,
5001 driver->manufacturer_url,
5002 r->manufacturer_url);
5003 FILL_DRIVER_STRING(mem_ctx,
5004 driver->hardware_id,
5005 r->hardware_id);
5006 FILL_DRIVER_STRING(mem_ctx,
5007 driver->provider,
5008 r->provider);
5010 return WERR_OK;
5013 /********************************************************************
5014 ********************************************************************/
5016 static WERROR construct_printer_driver_info_level(TALLOC_CTX *mem_ctx,
5017 uint32_t level,
5018 union spoolss_DriverInfo *r,
5019 int snum,
5020 const char *servername,
5021 const char *architecture,
5022 uint32_t version)
5024 NT_PRINTER_INFO_LEVEL *printer = NULL;
5025 struct spoolss_DriverInfo8 *driver;
5026 WERROR result;
5028 result = get_a_printer(NULL, &printer, 2, lp_const_servicename(snum));
5030 DEBUG(8,("construct_printer_driver_info_level: status: %s\n",
5031 win_errstr(result)));
5033 if (!W_ERROR_IS_OK(result)) {
5034 return WERR_INVALID_PRINTER_NAME;
5037 result = get_a_printer_driver(mem_ctx, &driver, printer->info_2->drivername,
5038 architecture, version);
5040 DEBUG(8,("construct_printer_driver_info_level: status: %s\n",
5041 win_errstr(result)));
5043 if (!W_ERROR_IS_OK(result)) {
5045 * Is this a W2k client ?
5048 if (version < 3) {
5049 free_a_printer(&printer, 2);
5050 return WERR_UNKNOWN_PRINTER_DRIVER;
5053 /* Yes - try again with a WinNT driver. */
5054 version = 2;
5055 result = get_a_printer_driver(mem_ctx, &driver, printer->info_2->drivername,
5056 architecture, version);
5057 DEBUG(8,("construct_printer_driver_level: status: %s\n",
5058 win_errstr(result)));
5059 if (!W_ERROR_IS_OK(result)) {
5060 free_a_printer(&printer, 2);
5061 return WERR_UNKNOWN_PRINTER_DRIVER;
5065 switch (level) {
5066 case 1:
5067 result = fill_printer_driver_info1(mem_ctx, &r->info1, driver, servername);
5068 break;
5069 case 2:
5070 result = fill_printer_driver_info2(mem_ctx, &r->info2, driver, servername);
5071 break;
5072 case 3:
5073 result = fill_printer_driver_info3(mem_ctx, &r->info3, driver, servername);
5074 break;
5075 case 4:
5076 result = fill_printer_driver_info4(mem_ctx, &r->info4, driver, servername);
5077 break;
5078 case 5:
5079 result = fill_printer_driver_info5(mem_ctx, &r->info5, driver, servername);
5080 break;
5081 case 6:
5082 result = fill_printer_driver_info6(mem_ctx, &r->info6, driver, servername);
5083 break;
5084 case 8:
5085 result = fill_printer_driver_info8(mem_ctx, &r->info8, driver, servername);
5086 break;
5087 case 101:
5088 result = fill_printer_driver_info101(mem_ctx, &r->info101, driver, servername);
5089 break;
5090 default:
5091 result = WERR_UNKNOWN_LEVEL;
5092 break;
5095 free_a_printer(&printer, 2);
5096 free_a_printer_driver(driver);
5098 return result;
5101 /****************************************************************
5102 _spoolss_GetPrinterDriver2
5103 ****************************************************************/
5105 WERROR _spoolss_GetPrinterDriver2(pipes_struct *p,
5106 struct spoolss_GetPrinterDriver2 *r)
5108 Printer_entry *printer;
5109 WERROR result;
5111 const char *servername;
5112 int snum;
5114 /* that's an [in out] buffer */
5116 if (!r->in.buffer && (r->in.offered != 0)) {
5117 return WERR_INVALID_PARAM;
5120 DEBUG(4,("_spoolss_GetPrinterDriver2\n"));
5122 if (!(printer = find_printer_index_by_hnd(p, r->in.handle))) {
5123 DEBUG(0,("_spoolss_GetPrinterDriver2: invalid printer handle!\n"));
5124 return WERR_INVALID_PRINTER_NAME;
5127 *r->out.needed = 0;
5128 *r->out.server_major_version = 0;
5129 *r->out.server_minor_version = 0;
5131 servername = get_server_name(printer);
5133 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
5134 return WERR_BADFID;
5137 result = construct_printer_driver_info_level(p->mem_ctx, r->in.level,
5138 r->out.info, snum,
5139 servername,
5140 r->in.architecture,
5141 r->in.client_major_version);
5142 if (!W_ERROR_IS_OK(result)) {
5143 TALLOC_FREE(r->out.info);
5144 return result;
5147 *r->out.needed = SPOOLSS_BUFFER_UNION(spoolss_DriverInfo, NULL,
5148 r->out.info, r->in.level);
5149 r->out.info = SPOOLSS_BUFFER_OK(r->out.info, NULL);
5151 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
5155 /****************************************************************
5156 _spoolss_StartPagePrinter
5157 ****************************************************************/
5159 WERROR _spoolss_StartPagePrinter(pipes_struct *p,
5160 struct spoolss_StartPagePrinter *r)
5162 Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
5164 if (!Printer) {
5165 DEBUG(3,("_spoolss_StartPagePrinter: "
5166 "Error in startpageprinter printer handle\n"));
5167 return WERR_BADFID;
5170 Printer->page_started = true;
5171 return WERR_OK;
5174 /****************************************************************
5175 _spoolss_EndPagePrinter
5176 ****************************************************************/
5178 WERROR _spoolss_EndPagePrinter(pipes_struct *p,
5179 struct spoolss_EndPagePrinter *r)
5181 int snum;
5183 Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
5185 if (!Printer) {
5186 DEBUG(2,("_spoolss_EndPagePrinter: Invalid handle (%s:%u:%u).\n",
5187 OUR_HANDLE(r->in.handle)));
5188 return WERR_BADFID;
5191 if (!get_printer_snum(p, r->in.handle, &snum, NULL))
5192 return WERR_BADFID;
5194 Printer->page_started = false;
5195 print_job_endpage(snum, Printer->jobid);
5197 return WERR_OK;
5200 /****************************************************************
5201 _spoolss_StartDocPrinter
5202 ****************************************************************/
5204 WERROR _spoolss_StartDocPrinter(pipes_struct *p,
5205 struct spoolss_StartDocPrinter *r)
5207 struct spoolss_DocumentInfo1 *info_1;
5208 int snum;
5209 Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
5211 if (!Printer) {
5212 DEBUG(2,("_spoolss_StartDocPrinter: "
5213 "Invalid handle (%s:%u:%u)\n",
5214 OUR_HANDLE(r->in.handle)));
5215 return WERR_BADFID;
5218 if (r->in.level != 1) {
5219 return WERR_UNKNOWN_LEVEL;
5222 info_1 = r->in.info.info1;
5225 * a nice thing with NT is it doesn't listen to what you tell it.
5226 * when asked to send _only_ RAW datas, it tries to send datas
5227 * in EMF format.
5229 * So I add checks like in NT Server ...
5232 if (info_1->datatype) {
5233 if (strcmp(info_1->datatype, "RAW") != 0) {
5234 *r->out.job_id = 0;
5235 return WERR_INVALID_DATATYPE;
5239 /* get the share number of the printer */
5240 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
5241 return WERR_BADFID;
5244 Printer->jobid = print_job_start(p->server_info, snum,
5245 info_1->document_name,
5246 Printer->nt_devmode);
5248 /* An error occured in print_job_start() so return an appropriate
5249 NT error code. */
5251 if (Printer->jobid == -1) {
5252 return map_werror_from_unix(errno);
5255 Printer->document_started = true;
5256 *r->out.job_id = Printer->jobid;
5258 return WERR_OK;
5261 /****************************************************************
5262 _spoolss_EndDocPrinter
5263 ****************************************************************/
5265 WERROR _spoolss_EndDocPrinter(pipes_struct *p,
5266 struct spoolss_EndDocPrinter *r)
5268 Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
5269 int snum;
5271 if (!Printer) {
5272 DEBUG(2,("_spoolss_EndDocPrinter: Invalid handle (%s:%u:%u)\n",
5273 OUR_HANDLE(r->in.handle)));
5274 return WERR_BADFID;
5277 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
5278 return WERR_BADFID;
5281 Printer->document_started = false;
5282 print_job_end(snum, Printer->jobid, NORMAL_CLOSE);
5283 /* error codes unhandled so far ... */
5285 return WERR_OK;
5288 /****************************************************************
5289 _spoolss_WritePrinter
5290 ****************************************************************/
5292 WERROR _spoolss_WritePrinter(pipes_struct *p,
5293 struct spoolss_WritePrinter *r)
5295 uint32_t buffer_written;
5296 int snum;
5297 Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
5299 if (!Printer) {
5300 DEBUG(2,("_spoolss_WritePrinter: Invalid handle (%s:%u:%u)\n",
5301 OUR_HANDLE(r->in.handle)));
5302 *r->out.num_written = r->in._data_size;
5303 return WERR_BADFID;
5306 if (!get_printer_snum(p, r->in.handle, &snum, NULL))
5307 return WERR_BADFID;
5309 buffer_written = (uint32_t)print_job_write(snum, Printer->jobid,
5310 (const char *)r->in.data.data,
5311 (SMB_OFF_T)-1,
5312 (size_t)r->in._data_size);
5313 if (buffer_written == (uint32_t)-1) {
5314 *r->out.num_written = 0;
5315 if (errno == ENOSPC)
5316 return WERR_NO_SPOOL_SPACE;
5317 else
5318 return WERR_ACCESS_DENIED;
5321 *r->out.num_written = r->in._data_size;
5323 return WERR_OK;
5326 /********************************************************************
5327 * api_spoolss_getprinter
5328 * called from the spoolss dispatcher
5330 ********************************************************************/
5332 static WERROR control_printer(struct policy_handle *handle, uint32_t command,
5333 pipes_struct *p)
5335 int snum;
5336 WERROR errcode = WERR_BADFUNC;
5337 Printer_entry *Printer = find_printer_index_by_hnd(p, handle);
5339 if (!Printer) {
5340 DEBUG(2,("control_printer: Invalid handle (%s:%u:%u)\n",
5341 OUR_HANDLE(handle)));
5342 return WERR_BADFID;
5345 if (!get_printer_snum(p, handle, &snum, NULL))
5346 return WERR_BADFID;
5348 switch (command) {
5349 case SPOOLSS_PRINTER_CONTROL_PAUSE:
5350 errcode = print_queue_pause(p->server_info, snum);
5351 break;
5352 case SPOOLSS_PRINTER_CONTROL_RESUME:
5353 case SPOOLSS_PRINTER_CONTROL_UNPAUSE:
5354 errcode = print_queue_resume(p->server_info, snum);
5355 break;
5356 case SPOOLSS_PRINTER_CONTROL_PURGE:
5357 errcode = print_queue_purge(p->server_info, snum);
5358 break;
5359 default:
5360 return WERR_UNKNOWN_LEVEL;
5363 return errcode;
5367 /****************************************************************
5368 _spoolss_AbortPrinter
5369 * From MSDN: "Deletes printer's spool file if printer is configured
5370 * for spooling"
5371 ****************************************************************/
5373 WERROR _spoolss_AbortPrinter(pipes_struct *p,
5374 struct spoolss_AbortPrinter *r)
5376 Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
5377 int snum;
5378 WERROR errcode = WERR_OK;
5380 if (!Printer) {
5381 DEBUG(2,("_spoolss_AbortPrinter: Invalid handle (%s:%u:%u)\n",
5382 OUR_HANDLE(r->in.handle)));
5383 return WERR_BADFID;
5386 if (!get_printer_snum(p, r->in.handle, &snum, NULL))
5387 return WERR_BADFID;
5389 print_job_delete(p->server_info, snum, Printer->jobid, &errcode );
5391 return errcode;
5394 /********************************************************************
5395 * called by spoolss_api_setprinter
5396 * when updating a printer description
5397 ********************************************************************/
5399 static WERROR update_printer_sec(struct policy_handle *handle,
5400 pipes_struct *p, SEC_DESC_BUF *secdesc_ctr)
5402 SEC_DESC_BUF *new_secdesc_ctr = NULL, *old_secdesc_ctr = NULL;
5403 WERROR result;
5404 int snum;
5406 Printer_entry *Printer = find_printer_index_by_hnd(p, handle);
5408 if (!Printer || !get_printer_snum(p, handle, &snum, NULL)) {
5409 DEBUG(2,("update_printer_sec: Invalid handle (%s:%u:%u)\n",
5410 OUR_HANDLE(handle)));
5412 result = WERR_BADFID;
5413 goto done;
5416 if (!secdesc_ctr) {
5417 DEBUG(10,("update_printer_sec: secdesc_ctr is NULL !\n"));
5418 result = WERR_INVALID_PARAM;
5419 goto done;
5422 /* Check the user has permissions to change the security
5423 descriptor. By experimentation with two NT machines, the user
5424 requires Full Access to the printer to change security
5425 information. */
5427 if ( Printer->access_granted != PRINTER_ACCESS_ADMINISTER ) {
5428 DEBUG(4,("update_printer_sec: updated denied by printer permissions\n"));
5429 result = WERR_ACCESS_DENIED;
5430 goto done;
5433 /* NT seems to like setting the security descriptor even though
5434 nothing may have actually changed. */
5436 if ( !nt_printing_getsec(p->mem_ctx, Printer->sharename, &old_secdesc_ctr)) {
5437 DEBUG(2,("update_printer_sec: nt_printing_getsec() failed\n"));
5438 result = WERR_BADFID;
5439 goto done;
5442 if (DEBUGLEVEL >= 10) {
5443 SEC_ACL *the_acl;
5444 int i;
5446 the_acl = old_secdesc_ctr->sd->dacl;
5447 DEBUG(10, ("old_secdesc_ctr for %s has %d aces:\n",
5448 PRINTERNAME(snum), the_acl->num_aces));
5450 for (i = 0; i < the_acl->num_aces; i++) {
5451 DEBUG(10, ("%s 0x%08x\n", sid_string_dbg(
5452 &the_acl->aces[i].trustee),
5453 the_acl->aces[i].access_mask));
5456 the_acl = secdesc_ctr->sd->dacl;
5458 if (the_acl) {
5459 DEBUG(10, ("secdesc_ctr for %s has %d aces:\n",
5460 PRINTERNAME(snum), the_acl->num_aces));
5462 for (i = 0; i < the_acl->num_aces; i++) {
5463 DEBUG(10, ("%s 0x%08x\n", sid_string_dbg(
5464 &the_acl->aces[i].trustee),
5465 the_acl->aces[i].access_mask));
5467 } else {
5468 DEBUG(10, ("dacl for secdesc_ctr is NULL\n"));
5472 new_secdesc_ctr = sec_desc_merge(p->mem_ctx, secdesc_ctr, old_secdesc_ctr);
5473 if (!new_secdesc_ctr) {
5474 result = WERR_NOMEM;
5475 goto done;
5478 if (security_descriptor_equal(new_secdesc_ctr->sd, old_secdesc_ctr->sd)) {
5479 result = WERR_OK;
5480 goto done;
5483 result = nt_printing_setsec(Printer->sharename, new_secdesc_ctr);
5485 done:
5487 return result;
5490 /********************************************************************
5491 Canonicalize printer info from a client
5493 ATTN: It does not matter what we set the servername to hear
5494 since we do the necessary work in get_a_printer() to set it to
5495 the correct value based on what the client sent in the
5496 _spoolss_open_printer_ex().
5497 ********************************************************************/
5499 static bool check_printer_ok(NT_PRINTER_INFO_LEVEL_2 *info, int snum)
5501 fstring printername;
5502 const char *p;
5504 DEBUG(5,("check_printer_ok: servername=%s printername=%s sharename=%s "
5505 "portname=%s drivername=%s comment=%s location=%s\n",
5506 info->servername, info->printername, info->sharename,
5507 info->portname, info->drivername, info->comment, info->location));
5509 /* we force some elements to "correct" values */
5510 slprintf(info->servername, sizeof(info->servername)-1, "\\\\%s", global_myname());
5511 fstrcpy(info->sharename, lp_servicename(snum));
5513 /* check to see if we allow printername != sharename */
5515 if ( lp_force_printername(snum) ) {
5516 slprintf(info->printername, sizeof(info->printername)-1, "\\\\%s\\%s",
5517 global_myname(), info->sharename );
5518 } else {
5520 /* make sure printername is in \\server\printername format */
5522 fstrcpy( printername, info->printername );
5523 p = printername;
5524 if ( printername[0] == '\\' && printername[1] == '\\' ) {
5525 if ( (p = strchr_m( &printername[2], '\\' )) != NULL )
5526 p++;
5529 slprintf(info->printername, sizeof(info->printername)-1, "\\\\%s\\%s",
5530 global_myname(), p );
5533 info->attributes |= PRINTER_ATTRIBUTE_SAMBA;
5534 info->attributes &= ~PRINTER_ATTRIBUTE_NOT_SAMBA;
5538 return true;
5541 /****************************************************************************
5542 ****************************************************************************/
5544 static WERROR add_port_hook(TALLOC_CTX *ctx, NT_USER_TOKEN *token, const char *portname, const char *uri)
5546 char *cmd = lp_addport_cmd();
5547 char *command = NULL;
5548 int ret;
5549 SE_PRIV se_printop = SE_PRINT_OPERATOR;
5550 bool is_print_op = false;
5552 if ( !*cmd ) {
5553 return WERR_ACCESS_DENIED;
5556 command = talloc_asprintf(ctx,
5557 "%s \"%s\" \"%s\"", cmd, portname, uri );
5558 if (!command) {
5559 return WERR_NOMEM;
5562 if ( token )
5563 is_print_op = user_has_privileges( token, &se_printop );
5565 DEBUG(10,("Running [%s]\n", command));
5567 /********* BEGIN SePrintOperatorPrivilege **********/
5569 if ( is_print_op )
5570 become_root();
5572 ret = smbrun(command, NULL);
5574 if ( is_print_op )
5575 unbecome_root();
5577 /********* END SePrintOperatorPrivilege **********/
5579 DEBUGADD(10,("returned [%d]\n", ret));
5581 TALLOC_FREE(command);
5583 if ( ret != 0 ) {
5584 return WERR_ACCESS_DENIED;
5587 return WERR_OK;
5590 /****************************************************************************
5591 ****************************************************************************/
5593 bool add_printer_hook(TALLOC_CTX *ctx, NT_USER_TOKEN *token, NT_PRINTER_INFO_LEVEL *printer)
5595 char *cmd = lp_addprinter_cmd();
5596 char **qlines;
5597 char *command = NULL;
5598 int numlines;
5599 int ret;
5600 int fd;
5601 SE_PRIV se_printop = SE_PRINT_OPERATOR;
5602 bool is_print_op = false;
5603 char *remote_machine = talloc_strdup(ctx, "%m");
5605 if (!remote_machine) {
5606 return false;
5608 remote_machine = talloc_sub_basic(ctx,
5609 current_user_info.smb_name,
5610 current_user_info.domain,
5611 remote_machine);
5612 if (!remote_machine) {
5613 return false;
5616 command = talloc_asprintf(ctx,
5617 "%s \"%s\" \"%s\" \"%s\" \"%s\" \"%s\" \"%s\" \"%s\"",
5618 cmd, printer->info_2->printername, printer->info_2->sharename,
5619 printer->info_2->portname, printer->info_2->drivername,
5620 printer->info_2->location, printer->info_2->comment, remote_machine);
5621 if (!command) {
5622 return false;
5625 if ( token )
5626 is_print_op = user_has_privileges( token, &se_printop );
5628 DEBUG(10,("Running [%s]\n", command));
5630 /********* BEGIN SePrintOperatorPrivilege **********/
5632 if ( is_print_op )
5633 become_root();
5635 if ( (ret = smbrun(command, &fd)) == 0 ) {
5636 /* Tell everyone we updated smb.conf. */
5637 message_send_all(smbd_messaging_context(),
5638 MSG_SMB_CONF_UPDATED, NULL, 0, NULL);
5641 if ( is_print_op )
5642 unbecome_root();
5644 /********* END SePrintOperatorPrivilege **********/
5646 DEBUGADD(10,("returned [%d]\n", ret));
5648 TALLOC_FREE(command);
5649 TALLOC_FREE(remote_machine);
5651 if ( ret != 0 ) {
5652 if (fd != -1)
5653 close(fd);
5654 return false;
5657 /* reload our services immediately */
5658 become_root();
5659 reload_services(false);
5660 unbecome_root();
5662 numlines = 0;
5663 /* Get lines and convert them back to dos-codepage */
5664 qlines = fd_lines_load(fd, &numlines, 0, NULL);
5665 DEBUGADD(10,("Lines returned = [%d]\n", numlines));
5666 close(fd);
5668 /* Set the portname to what the script says the portname should be. */
5669 /* but don't require anything to be return from the script exit a good error code */
5671 if (numlines) {
5672 /* Set the portname to what the script says the portname should be. */
5673 strncpy(printer->info_2->portname, qlines[0], sizeof(printer->info_2->portname));
5674 DEBUGADD(6,("Line[0] = [%s]\n", qlines[0]));
5677 TALLOC_FREE(qlines);
5678 return true;
5682 /********************************************************************
5683 * Called by spoolss_api_setprinter
5684 * when updating a printer description.
5685 ********************************************************************/
5687 static WERROR update_printer(pipes_struct *p, struct policy_handle *handle,
5688 struct spoolss_SetPrinterInfoCtr *info_ctr,
5689 struct spoolss_DeviceMode *devmode)
5691 int snum;
5692 NT_PRINTER_INFO_LEVEL *printer = NULL, *old_printer = NULL;
5693 Printer_entry *Printer = find_printer_index_by_hnd(p, handle);
5694 WERROR result;
5695 DATA_BLOB buffer;
5696 fstring asc_buffer;
5698 DEBUG(8,("update_printer\n"));
5700 result = WERR_OK;
5702 if (!Printer) {
5703 result = WERR_BADFID;
5704 goto done;
5707 if (!get_printer_snum(p, handle, &snum, NULL)) {
5708 result = WERR_BADFID;
5709 goto done;
5712 if (!W_ERROR_IS_OK(get_a_printer(Printer, &printer, 2, lp_const_servicename(snum))) ||
5713 (!W_ERROR_IS_OK(get_a_printer(Printer, &old_printer, 2, lp_const_servicename(snum))))) {
5714 result = WERR_BADFID;
5715 goto done;
5718 DEBUGADD(8,("Converting info_2 struct\n"));
5721 * convert_printer_info converts the incoming
5722 * info from the client and overwrites the info
5723 * just read from the tdb in the pointer 'printer'.
5726 if (!convert_printer_info(info_ctr, printer)) {
5727 result = WERR_NOMEM;
5728 goto done;
5731 if (devmode) {
5732 /* we have a valid devmode
5733 convert it and link it*/
5735 DEBUGADD(8,("update_printer: Converting the devicemode struct\n"));
5736 if (!convert_devicemode(printer->info_2->printername, devmode,
5737 &printer->info_2->devmode)) {
5738 result = WERR_NOMEM;
5739 goto done;
5743 /* Do sanity check on the requested changes for Samba */
5745 if (!check_printer_ok(printer->info_2, snum)) {
5746 result = WERR_INVALID_PARAM;
5747 goto done;
5750 /* FIXME!!! If the driver has changed we really should verify that
5751 it is installed before doing much else --jerry */
5753 /* Check calling user has permission to update printer description */
5755 if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
5756 DEBUG(3, ("update_printer: printer property change denied by handle\n"));
5757 result = WERR_ACCESS_DENIED;
5758 goto done;
5761 /* Call addprinter hook */
5762 /* Check changes to see if this is really needed */
5764 if ( *lp_addprinter_cmd()
5765 && (!strequal(printer->info_2->drivername, old_printer->info_2->drivername)
5766 || !strequal(printer->info_2->comment, old_printer->info_2->comment)
5767 || !strequal(printer->info_2->portname, old_printer->info_2->portname)
5768 || !strequal(printer->info_2->location, old_printer->info_2->location)) )
5770 /* add_printer_hook() will call reload_services() */
5772 if ( !add_printer_hook(p->mem_ctx, p->server_info->ptok,
5773 printer) ) {
5774 result = WERR_ACCESS_DENIED;
5775 goto done;
5780 * When a *new* driver is bound to a printer, the drivername is used to
5781 * lookup previously saved driver initialization info, which is then
5782 * bound to the printer, simulating what happens in the Windows arch.
5784 if (!strequal(printer->info_2->drivername, old_printer->info_2->drivername))
5786 if (!set_driver_init(printer, 2))
5788 DEBUG(5,("update_printer: Error restoring driver initialization data for driver [%s]!\n",
5789 printer->info_2->drivername));
5792 DEBUG(10,("update_printer: changing driver [%s]! Sending event!\n",
5793 printer->info_2->drivername));
5795 notify_printer_driver(snum, printer->info_2->drivername);
5799 * flag which changes actually occured. This is a small subset of
5800 * all the possible changes. We also have to update things in the
5801 * DsSpooler key.
5804 if (!strequal(printer->info_2->comment, old_printer->info_2->comment)) {
5805 push_reg_sz(talloc_tos(), &buffer, printer->info_2->comment);
5806 set_printer_dataex( printer, SPOOL_DSSPOOLER_KEY, "description",
5807 REG_SZ, buffer.data, buffer.length);
5809 notify_printer_comment(snum, printer->info_2->comment);
5812 if (!strequal(printer->info_2->sharename, old_printer->info_2->sharename)) {
5813 push_reg_sz(talloc_tos(), &buffer, printer->info_2->sharename);
5814 set_printer_dataex( printer, SPOOL_DSSPOOLER_KEY, "shareName",
5815 REG_SZ, buffer.data, buffer.length);
5817 notify_printer_sharename(snum, printer->info_2->sharename);
5820 if (!strequal(printer->info_2->printername, old_printer->info_2->printername)) {
5821 char *pname;
5823 if ( (pname = strchr_m( printer->info_2->printername+2, '\\' )) != NULL )
5824 pname++;
5825 else
5826 pname = printer->info_2->printername;
5829 push_reg_sz(talloc_tos(), &buffer, pname);
5830 set_printer_dataex( printer, SPOOL_DSSPOOLER_KEY, "printerName",
5831 REG_SZ, buffer.data, buffer.length);
5833 notify_printer_printername( snum, pname );
5836 if (!strequal(printer->info_2->portname, old_printer->info_2->portname)) {
5837 push_reg_sz(talloc_tos(), &buffer, printer->info_2->portname);
5838 set_printer_dataex( printer, SPOOL_DSSPOOLER_KEY, "portName",
5839 REG_SZ, buffer.data, buffer.length);
5841 notify_printer_port(snum, printer->info_2->portname);
5844 if (!strequal(printer->info_2->location, old_printer->info_2->location)) {
5845 push_reg_sz(talloc_tos(), &buffer, printer->info_2->location);
5846 set_printer_dataex( printer, SPOOL_DSSPOOLER_KEY, "location",
5847 REG_SZ, buffer.data, buffer.length);
5849 notify_printer_location(snum, printer->info_2->location);
5852 /* here we need to update some more DsSpooler keys */
5853 /* uNCName, serverName, shortServerName */
5855 push_reg_sz(talloc_tos(), &buffer, global_myname());
5856 set_printer_dataex( printer, SPOOL_DSSPOOLER_KEY, "serverName",
5857 REG_SZ, buffer.data, buffer.length);
5858 set_printer_dataex( printer, SPOOL_DSSPOOLER_KEY, "shortServerName",
5859 REG_SZ, buffer.data, buffer.length);
5861 slprintf( asc_buffer, sizeof(asc_buffer)-1, "\\\\%s\\%s",
5862 global_myname(), printer->info_2->sharename );
5863 push_reg_sz(talloc_tos(), &buffer, asc_buffer);
5864 set_printer_dataex( printer, SPOOL_DSSPOOLER_KEY, "uNCName",
5865 REG_SZ, buffer.data, buffer.length);
5867 /* Update printer info */
5868 result = mod_a_printer(printer, 2);
5870 done:
5871 free_a_printer(&printer, 2);
5872 free_a_printer(&old_printer, 2);
5875 return result;
5878 /****************************************************************************
5879 ****************************************************************************/
5880 static WERROR publish_or_unpublish_printer(pipes_struct *p,
5881 struct policy_handle *handle,
5882 struct spoolss_SetPrinterInfo7 *info7)
5884 #ifdef HAVE_ADS
5885 int snum;
5886 Printer_entry *Printer;
5888 if ( lp_security() != SEC_ADS ) {
5889 return WERR_UNKNOWN_LEVEL;
5892 Printer = find_printer_index_by_hnd(p, handle);
5894 DEBUG(5,("publish_or_unpublish_printer, action = %d\n",info7->action));
5896 if (!Printer)
5897 return WERR_BADFID;
5899 if (!get_printer_snum(p, handle, &snum, NULL))
5900 return WERR_BADFID;
5902 nt_printer_publish(Printer, snum, info7->action);
5904 return WERR_OK;
5905 #else
5906 return WERR_UNKNOWN_LEVEL;
5907 #endif
5910 /****************************************************************
5911 _spoolss_SetPrinter
5912 ****************************************************************/
5914 WERROR _spoolss_SetPrinter(pipes_struct *p,
5915 struct spoolss_SetPrinter *r)
5917 WERROR result;
5919 Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
5921 if (!Printer) {
5922 DEBUG(2,("_spoolss_SetPrinter: Invalid handle (%s:%u:%u)\n",
5923 OUR_HANDLE(r->in.handle)));
5924 return WERR_BADFID;
5927 /* check the level */
5928 switch (r->in.info_ctr->level) {
5929 case 0:
5930 return control_printer(r->in.handle, r->in.command, p);
5931 case 2:
5932 result = update_printer(p, r->in.handle,
5933 r->in.info_ctr,
5934 r->in.devmode_ctr->devmode);
5935 if (!W_ERROR_IS_OK(result))
5936 return result;
5937 if (r->in.secdesc_ctr->sd)
5938 result = update_printer_sec(r->in.handle, p,
5939 r->in.secdesc_ctr);
5940 return result;
5941 case 3:
5942 return update_printer_sec(r->in.handle, p,
5943 r->in.secdesc_ctr);
5944 case 7:
5945 return publish_or_unpublish_printer(p, r->in.handle,
5946 r->in.info_ctr->info.info7);
5947 default:
5948 return WERR_UNKNOWN_LEVEL;
5952 /****************************************************************
5953 _spoolss_FindClosePrinterNotify
5954 ****************************************************************/
5956 WERROR _spoolss_FindClosePrinterNotify(pipes_struct *p,
5957 struct spoolss_FindClosePrinterNotify *r)
5959 Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
5961 if (!Printer) {
5962 DEBUG(2,("_spoolss_FindClosePrinterNotify: "
5963 "Invalid handle (%s:%u:%u)\n", OUR_HANDLE(r->in.handle)));
5964 return WERR_BADFID;
5967 if (Printer->notify.client_connected == true) {
5968 int snum = -1;
5970 if ( Printer->printer_type == SPLHND_SERVER)
5971 snum = -1;
5972 else if ( (Printer->printer_type == SPLHND_PRINTER) &&
5973 !get_printer_snum(p, r->in.handle, &snum, NULL) )
5974 return WERR_BADFID;
5976 srv_spoolss_replycloseprinter(snum, &Printer->notify.client_hnd);
5979 Printer->notify.flags=0;
5980 Printer->notify.options=0;
5981 Printer->notify.localmachine[0]='\0';
5982 Printer->notify.printerlocal=0;
5983 TALLOC_FREE(Printer->notify.option);
5984 Printer->notify.client_connected = false;
5986 return WERR_OK;
5989 /****************************************************************
5990 _spoolss_AddJob
5991 ****************************************************************/
5993 WERROR _spoolss_AddJob(pipes_struct *p,
5994 struct spoolss_AddJob *r)
5996 if (!r->in.buffer && (r->in.offered != 0)) {
5997 return WERR_INVALID_PARAM;
6000 /* this is what a NT server returns for AddJob. AddJob must fail on
6001 * non-local printers */
6003 if (r->in.level != 1) {
6004 return WERR_UNKNOWN_LEVEL;
6007 return WERR_INVALID_PARAM;
6010 /****************************************************************************
6011 fill_job_info1
6012 ****************************************************************************/
6014 static WERROR fill_job_info1(TALLOC_CTX *mem_ctx,
6015 struct spoolss_JobInfo1 *r,
6016 const print_queue_struct *queue,
6017 int position, int snum,
6018 const NT_PRINTER_INFO_LEVEL *ntprinter)
6020 struct tm *t;
6022 t = gmtime(&queue->time);
6024 r->job_id = queue->job;
6026 r->printer_name = talloc_strdup(mem_ctx, lp_servicename(snum));
6027 W_ERROR_HAVE_NO_MEMORY(r->printer_name);
6028 r->server_name = talloc_strdup(mem_ctx, ntprinter->info_2->servername);
6029 W_ERROR_HAVE_NO_MEMORY(r->server_name);
6030 r->user_name = talloc_strdup(mem_ctx, queue->fs_user);
6031 W_ERROR_HAVE_NO_MEMORY(r->user_name);
6032 r->document_name = talloc_strdup(mem_ctx, queue->fs_file);
6033 W_ERROR_HAVE_NO_MEMORY(r->document_name);
6034 r->data_type = talloc_strdup(mem_ctx, "RAW");
6035 W_ERROR_HAVE_NO_MEMORY(r->data_type);
6036 r->text_status = talloc_strdup(mem_ctx, "");
6037 W_ERROR_HAVE_NO_MEMORY(r->text_status);
6039 r->status = nt_printj_status(queue->status);
6040 r->priority = queue->priority;
6041 r->position = position;
6042 r->total_pages = queue->page_count;
6043 r->pages_printed = 0; /* ??? */
6045 init_systemtime(&r->submitted, t);
6047 return WERR_OK;
6050 /****************************************************************************
6051 fill_job_info2
6052 ****************************************************************************/
6054 static WERROR fill_job_info2(TALLOC_CTX *mem_ctx,
6055 struct spoolss_JobInfo2 *r,
6056 const print_queue_struct *queue,
6057 int position, int snum,
6058 const NT_PRINTER_INFO_LEVEL *ntprinter,
6059 struct spoolss_DeviceMode *devmode)
6061 struct tm *t;
6063 t = gmtime(&queue->time);
6065 r->job_id = queue->job;
6067 r->printer_name = talloc_strdup(mem_ctx, lp_servicename(snum));
6068 W_ERROR_HAVE_NO_MEMORY(r->printer_name);
6069 r->server_name = talloc_strdup(mem_ctx, ntprinter->info_2->servername);
6070 W_ERROR_HAVE_NO_MEMORY(r->server_name);
6071 r->user_name = talloc_strdup(mem_ctx, queue->fs_user);
6072 W_ERROR_HAVE_NO_MEMORY(r->user_name);
6073 r->document_name = talloc_strdup(mem_ctx, queue->fs_file);
6074 W_ERROR_HAVE_NO_MEMORY(r->document_name);
6075 r->notify_name = talloc_strdup(mem_ctx, queue->fs_user);
6076 W_ERROR_HAVE_NO_MEMORY(r->notify_name);
6077 r->data_type = talloc_strdup(mem_ctx, "RAW");
6078 W_ERROR_HAVE_NO_MEMORY(r->data_type);
6079 r->print_processor = talloc_strdup(mem_ctx, "winprint");
6080 W_ERROR_HAVE_NO_MEMORY(r->print_processor);
6081 r->parameters = talloc_strdup(mem_ctx, "");
6082 W_ERROR_HAVE_NO_MEMORY(r->parameters);
6083 r->driver_name = talloc_strdup(mem_ctx, ntprinter->info_2->drivername);
6084 W_ERROR_HAVE_NO_MEMORY(r->driver_name);
6086 r->devmode = devmode;
6088 r->text_status = talloc_strdup(mem_ctx, "");
6089 W_ERROR_HAVE_NO_MEMORY(r->text_status);
6091 r->secdesc = NULL;
6093 r->status = nt_printj_status(queue->status);
6094 r->priority = queue->priority;
6095 r->position = position;
6096 r->start_time = 0;
6097 r->until_time = 0;
6098 r->total_pages = queue->page_count;
6099 r->size = queue->size;
6100 init_systemtime(&r->submitted, t);
6101 r->time = 0;
6102 r->pages_printed = 0; /* ??? */
6104 return WERR_OK;
6107 /****************************************************************************
6108 Enumjobs at level 1.
6109 ****************************************************************************/
6111 static WERROR enumjobs_level1(TALLOC_CTX *mem_ctx,
6112 const print_queue_struct *queue,
6113 uint32_t num_queues, int snum,
6114 const NT_PRINTER_INFO_LEVEL *ntprinter,
6115 union spoolss_JobInfo **info_p,
6116 uint32_t *count)
6118 union spoolss_JobInfo *info;
6119 int i;
6120 WERROR result = WERR_OK;
6122 info = TALLOC_ARRAY(mem_ctx, union spoolss_JobInfo, num_queues);
6123 W_ERROR_HAVE_NO_MEMORY(info);
6125 *count = num_queues;
6127 for (i=0; i<*count; i++) {
6128 result = fill_job_info1(info,
6129 &info[i].info1,
6130 &queue[i],
6132 snum,
6133 ntprinter);
6134 if (!W_ERROR_IS_OK(result)) {
6135 goto out;
6139 out:
6140 if (!W_ERROR_IS_OK(result)) {
6141 TALLOC_FREE(info);
6142 *count = 0;
6143 return result;
6146 *info_p = info;
6148 return WERR_OK;
6151 /****************************************************************************
6152 Enumjobs at level 2.
6153 ****************************************************************************/
6155 static WERROR enumjobs_level2(TALLOC_CTX *mem_ctx,
6156 const print_queue_struct *queue,
6157 uint32_t num_queues, int snum,
6158 const NT_PRINTER_INFO_LEVEL *ntprinter,
6159 union spoolss_JobInfo **info_p,
6160 uint32_t *count)
6162 union spoolss_JobInfo *info;
6163 int i;
6164 WERROR result = WERR_OK;
6166 info = TALLOC_ARRAY(mem_ctx, union spoolss_JobInfo, num_queues);
6167 W_ERROR_HAVE_NO_MEMORY(info);
6169 *count = num_queues;
6171 for (i=0; i<*count; i++) {
6173 struct spoolss_DeviceMode *devmode;
6175 devmode = construct_dev_mode(info, lp_const_servicename(snum));
6176 if (!devmode) {
6177 result = WERR_NOMEM;
6178 goto out;
6181 result = fill_job_info2(info,
6182 &info[i].info2,
6183 &queue[i],
6185 snum,
6186 ntprinter,
6187 devmode);
6188 if (!W_ERROR_IS_OK(result)) {
6189 goto out;
6193 out:
6194 if (!W_ERROR_IS_OK(result)) {
6195 TALLOC_FREE(info);
6196 *count = 0;
6197 return result;
6200 *info_p = info;
6202 return WERR_OK;
6205 /****************************************************************
6206 _spoolss_EnumJobs
6207 ****************************************************************/
6209 WERROR _spoolss_EnumJobs(pipes_struct *p,
6210 struct spoolss_EnumJobs *r)
6212 WERROR result;
6213 NT_PRINTER_INFO_LEVEL *ntprinter = NULL;
6214 int snum;
6215 print_status_struct prt_status;
6216 print_queue_struct *queue = NULL;
6217 uint32_t count;
6219 /* that's an [in out] buffer */
6221 if (!r->in.buffer && (r->in.offered != 0)) {
6222 return WERR_INVALID_PARAM;
6225 DEBUG(4,("_spoolss_EnumJobs\n"));
6227 *r->out.needed = 0;
6228 *r->out.count = 0;
6229 *r->out.info = NULL;
6231 /* lookup the printer snum and tdb entry */
6233 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
6234 return WERR_BADFID;
6237 result = get_a_printer(NULL, &ntprinter, 2, lp_servicename(snum));
6238 if (!W_ERROR_IS_OK(result)) {
6239 return result;
6242 count = print_queue_status(snum, &queue, &prt_status);
6243 DEBUGADD(4,("count:[%d], status:[%d], [%s]\n",
6244 count, prt_status.status, prt_status.message));
6246 if (count == 0) {
6247 SAFE_FREE(queue);
6248 free_a_printer(&ntprinter, 2);
6249 return WERR_OK;
6252 switch (r->in.level) {
6253 case 1:
6254 result = enumjobs_level1(p->mem_ctx, queue, count, snum,
6255 ntprinter, r->out.info, r->out.count);
6256 break;
6257 case 2:
6258 result = enumjobs_level2(p->mem_ctx, queue, count, snum,
6259 ntprinter, r->out.info, r->out.count);
6260 break;
6261 default:
6262 result = WERR_UNKNOWN_LEVEL;
6263 break;
6266 SAFE_FREE(queue);
6267 free_a_printer(&ntprinter, 2);
6269 if (!W_ERROR_IS_OK(result)) {
6270 return result;
6273 *r->out.needed = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
6274 spoolss_EnumJobs, NULL,
6275 *r->out.info, r->in.level,
6276 *r->out.count);
6277 *r->out.info = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
6278 *r->out.count = SPOOLSS_BUFFER_OK(*r->out.count, 0);
6280 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
6283 /****************************************************************
6284 _spoolss_ScheduleJob
6285 ****************************************************************/
6287 WERROR _spoolss_ScheduleJob(pipes_struct *p,
6288 struct spoolss_ScheduleJob *r)
6290 return WERR_OK;
6293 /****************************************************************
6294 _spoolss_SetJob
6295 ****************************************************************/
6297 WERROR _spoolss_SetJob(pipes_struct *p,
6298 struct spoolss_SetJob *r)
6300 int snum;
6301 WERROR errcode = WERR_BADFUNC;
6303 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
6304 return WERR_BADFID;
6307 if (!print_job_exists(lp_const_servicename(snum), r->in.job_id)) {
6308 return WERR_INVALID_PRINTER_NAME;
6311 switch (r->in.command) {
6312 case SPOOLSS_JOB_CONTROL_CANCEL:
6313 case SPOOLSS_JOB_CONTROL_DELETE:
6314 if (print_job_delete(p->server_info, snum, r->in.job_id, &errcode)) {
6315 errcode = WERR_OK;
6317 break;
6318 case SPOOLSS_JOB_CONTROL_PAUSE:
6319 if (print_job_pause(p->server_info, snum, r->in.job_id, &errcode)) {
6320 errcode = WERR_OK;
6322 break;
6323 case SPOOLSS_JOB_CONTROL_RESTART:
6324 case SPOOLSS_JOB_CONTROL_RESUME:
6325 if (print_job_resume(p->server_info, snum, r->in.job_id, &errcode)) {
6326 errcode = WERR_OK;
6328 break;
6329 default:
6330 return WERR_UNKNOWN_LEVEL;
6333 return errcode;
6336 /****************************************************************************
6337 Enumerates all printer drivers by level and architecture.
6338 ****************************************************************************/
6340 static WERROR enumprinterdrivers_level_by_architecture(TALLOC_CTX *mem_ctx,
6341 const char *servername,
6342 const char *architecture,
6343 uint32_t level,
6344 union spoolss_DriverInfo **info_p,
6345 uint32_t *count_p)
6347 int i;
6348 int ndrivers;
6349 uint32_t version;
6350 fstring *list = NULL;
6351 struct spoolss_DriverInfo8 *driver;
6352 union spoolss_DriverInfo *info = NULL;
6353 uint32_t count = 0;
6354 WERROR result = WERR_OK;
6356 *count_p = 0;
6357 *info_p = NULL;
6359 for (version=0; version<DRIVER_MAX_VERSION; version++) {
6360 list = NULL;
6361 ndrivers = get_ntdrivers(&list, architecture, version);
6362 DEBUGADD(4,("we have:[%d] drivers in environment [%s] and version [%d]\n",
6363 ndrivers, architecture, version));
6365 if (ndrivers == -1) {
6366 result = WERR_NOMEM;
6367 goto out;
6370 if (ndrivers != 0) {
6371 info = TALLOC_REALLOC_ARRAY(mem_ctx, info,
6372 union spoolss_DriverInfo,
6373 count + ndrivers);
6374 if (!info) {
6375 DEBUG(0,("enumprinterdrivers_level1: "
6376 "failed to enlarge driver info buffer!\n"));
6377 result = WERR_NOMEM;
6378 goto out;
6382 for (i=0; i<ndrivers; i++) {
6383 DEBUGADD(5,("\tdriver: [%s]\n", list[i]));
6384 ZERO_STRUCT(driver);
6385 result = get_a_printer_driver(mem_ctx, &driver, list[i],
6386 architecture, version);
6387 if (!W_ERROR_IS_OK(result)) {
6388 goto out;
6391 switch (level) {
6392 case 1:
6393 result = fill_printer_driver_info1(info, &info[count+i].info1,
6394 driver, servername);
6395 break;
6396 case 2:
6397 result = fill_printer_driver_info2(info, &info[count+i].info2,
6398 driver, servername);
6399 break;
6400 case 3:
6401 result = fill_printer_driver_info3(info, &info[count+i].info3,
6402 driver, servername);
6403 break;
6404 case 4:
6405 result = fill_printer_driver_info4(info, &info[count+i].info4,
6406 driver, servername);
6407 break;
6408 case 5:
6409 result = fill_printer_driver_info5(info, &info[count+i].info5,
6410 driver, servername);
6411 break;
6412 case 6:
6413 result = fill_printer_driver_info6(info, &info[count+i].info6,
6414 driver, servername);
6415 break;
6416 case 8:
6417 result = fill_printer_driver_info8(info, &info[count+i].info8,
6418 driver, servername);
6419 break;
6420 default:
6421 result = WERR_UNKNOWN_LEVEL;
6422 break;
6425 free_a_printer_driver(driver);
6427 if (!W_ERROR_IS_OK(result)) {
6428 goto out;
6432 count += ndrivers;
6433 SAFE_FREE(list);
6436 out:
6437 SAFE_FREE(list);
6439 if (!W_ERROR_IS_OK(result)) {
6440 TALLOC_FREE(info);
6441 return result;
6444 *info_p = info;
6445 *count_p = count;
6447 return WERR_OK;
6450 /****************************************************************************
6451 Enumerates all printer drivers by level.
6452 ****************************************************************************/
6454 static WERROR enumprinterdrivers_level(TALLOC_CTX *mem_ctx,
6455 const char *servername,
6456 const char *architecture,
6457 uint32_t level,
6458 union spoolss_DriverInfo **info_p,
6459 uint32_t *count_p)
6461 uint32_t a,i;
6462 WERROR result = WERR_OK;
6464 if (strequal(architecture, "all")) {
6466 for (a=0; archi_table[a].long_archi != NULL; a++) {
6468 union spoolss_DriverInfo *info = NULL;
6469 uint32_t count = 0;
6471 result = enumprinterdrivers_level_by_architecture(mem_ctx,
6472 servername,
6473 archi_table[a].long_archi,
6474 level,
6475 &info,
6476 &count);
6477 if (!W_ERROR_IS_OK(result)) {
6478 continue;
6481 for (i=0; i < count; i++) {
6482 ADD_TO_ARRAY(mem_ctx, union spoolss_DriverInfo,
6483 info[i], info_p, count_p);
6487 return result;
6490 return enumprinterdrivers_level_by_architecture(mem_ctx,
6491 servername,
6492 architecture,
6493 level,
6494 info_p,
6495 count_p);
6498 /****************************************************************************
6499 Enumerates all printer drivers at level 1.
6500 ****************************************************************************/
6502 static WERROR enumprinterdrivers_level1(TALLOC_CTX *mem_ctx,
6503 const char *servername,
6504 const char *architecture,
6505 union spoolss_DriverInfo **info_p,
6506 uint32_t *count)
6508 return enumprinterdrivers_level(mem_ctx, servername, architecture, 1,
6509 info_p, count);
6512 /****************************************************************************
6513 Enumerates all printer drivers at level 2.
6514 ****************************************************************************/
6516 static WERROR enumprinterdrivers_level2(TALLOC_CTX *mem_ctx,
6517 const char *servername,
6518 const char *architecture,
6519 union spoolss_DriverInfo **info_p,
6520 uint32_t *count)
6522 return enumprinterdrivers_level(mem_ctx, servername, architecture, 2,
6523 info_p, count);
6526 /****************************************************************************
6527 Enumerates all printer drivers at level 3.
6528 ****************************************************************************/
6530 static WERROR enumprinterdrivers_level3(TALLOC_CTX *mem_ctx,
6531 const char *servername,
6532 const char *architecture,
6533 union spoolss_DriverInfo **info_p,
6534 uint32_t *count)
6536 return enumprinterdrivers_level(mem_ctx, servername, architecture, 3,
6537 info_p, count);
6540 /****************************************************************************
6541 Enumerates all printer drivers at level 4.
6542 ****************************************************************************/
6544 static WERROR enumprinterdrivers_level4(TALLOC_CTX *mem_ctx,
6545 const char *servername,
6546 const char *architecture,
6547 union spoolss_DriverInfo **info_p,
6548 uint32_t *count)
6550 return enumprinterdrivers_level(mem_ctx, servername, architecture, 4,
6551 info_p, count);
6554 /****************************************************************************
6555 Enumerates all printer drivers at level 5.
6556 ****************************************************************************/
6558 static WERROR enumprinterdrivers_level5(TALLOC_CTX *mem_ctx,
6559 const char *servername,
6560 const char *architecture,
6561 union spoolss_DriverInfo **info_p,
6562 uint32_t *count)
6564 return enumprinterdrivers_level(mem_ctx, servername, architecture, 5,
6565 info_p, count);
6568 /****************************************************************************
6569 Enumerates all printer drivers at level 6.
6570 ****************************************************************************/
6572 static WERROR enumprinterdrivers_level6(TALLOC_CTX *mem_ctx,
6573 const char *servername,
6574 const char *architecture,
6575 union spoolss_DriverInfo **info_p,
6576 uint32_t *count)
6578 return enumprinterdrivers_level(mem_ctx, servername, architecture, 6,
6579 info_p, count);
6582 /****************************************************************************
6583 Enumerates all printer drivers at level 8.
6584 ****************************************************************************/
6586 static WERROR enumprinterdrivers_level8(TALLOC_CTX *mem_ctx,
6587 const char *servername,
6588 const char *architecture,
6589 union spoolss_DriverInfo **info_p,
6590 uint32_t *count)
6592 return enumprinterdrivers_level(mem_ctx, servername, architecture, 8,
6593 info_p, count);
6596 /****************************************************************
6597 _spoolss_EnumPrinterDrivers
6598 ****************************************************************/
6600 WERROR _spoolss_EnumPrinterDrivers(pipes_struct *p,
6601 struct spoolss_EnumPrinterDrivers *r)
6603 const char *cservername;
6604 WERROR result;
6606 /* that's an [in out] buffer */
6608 if (!r->in.buffer && (r->in.offered != 0)) {
6609 return WERR_INVALID_PARAM;
6612 DEBUG(4,("_spoolss_EnumPrinterDrivers\n"));
6614 *r->out.needed = 0;
6615 *r->out.count = 0;
6616 *r->out.info = NULL;
6618 cservername = canon_servername(r->in.server);
6620 if (!is_myname_or_ipaddr(cservername)) {
6621 return WERR_UNKNOWN_PRINTER_DRIVER;
6624 switch (r->in.level) {
6625 case 1:
6626 result = enumprinterdrivers_level1(p->mem_ctx, cservername,
6627 r->in.environment,
6628 r->out.info, r->out.count);
6629 break;
6630 case 2:
6631 result = enumprinterdrivers_level2(p->mem_ctx, cservername,
6632 r->in.environment,
6633 r->out.info, r->out.count);
6634 break;
6635 case 3:
6636 result = enumprinterdrivers_level3(p->mem_ctx, cservername,
6637 r->in.environment,
6638 r->out.info, r->out.count);
6639 break;
6640 case 4:
6641 result = enumprinterdrivers_level4(p->mem_ctx, cservername,
6642 r->in.environment,
6643 r->out.info, r->out.count);
6644 break;
6645 case 5:
6646 result = enumprinterdrivers_level5(p->mem_ctx, cservername,
6647 r->in.environment,
6648 r->out.info, r->out.count);
6649 break;
6650 case 6:
6651 result = enumprinterdrivers_level6(p->mem_ctx, cservername,
6652 r->in.environment,
6653 r->out.info, r->out.count);
6654 break;
6655 case 8:
6656 result = enumprinterdrivers_level8(p->mem_ctx, cservername,
6657 r->in.environment,
6658 r->out.info, r->out.count);
6659 break;
6660 default:
6661 return WERR_UNKNOWN_LEVEL;
6664 if (!W_ERROR_IS_OK(result)) {
6665 return result;
6668 *r->out.needed = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
6669 spoolss_EnumPrinterDrivers, NULL,
6670 *r->out.info, r->in.level,
6671 *r->out.count);
6672 *r->out.info = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
6673 *r->out.count = SPOOLSS_BUFFER_OK(*r->out.count, 0);
6675 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
6678 /****************************************************************************
6679 ****************************************************************************/
6681 static WERROR fill_form_info_1(TALLOC_CTX *mem_ctx,
6682 struct spoolss_FormInfo1 *r,
6683 const nt_forms_struct *form)
6685 r->form_name = talloc_strdup(mem_ctx, form->name);
6686 W_ERROR_HAVE_NO_MEMORY(r->form_name);
6688 r->flags = form->flag;
6689 r->size.width = form->width;
6690 r->size.height = form->length;
6691 r->area.left = form->left;
6692 r->area.top = form->top;
6693 r->area.right = form->right;
6694 r->area.bottom = form->bottom;
6696 return WERR_OK;
6699 /****************************************************************
6700 spoolss_enumforms_level1
6701 ****************************************************************/
6703 static WERROR spoolss_enumforms_level1(TALLOC_CTX *mem_ctx,
6704 const nt_forms_struct *builtin_forms,
6705 uint32_t num_builtin_forms,
6706 const nt_forms_struct *user_forms,
6707 uint32_t num_user_forms,
6708 union spoolss_FormInfo **info_p,
6709 uint32_t *count)
6711 union spoolss_FormInfo *info;
6712 WERROR result = WERR_OK;
6713 int i;
6715 *count = num_builtin_forms + num_user_forms;
6717 info = TALLOC_ARRAY(mem_ctx, union spoolss_FormInfo, *count);
6718 W_ERROR_HAVE_NO_MEMORY(info);
6720 /* construct the list of form structures */
6721 for (i=0; i<num_builtin_forms; i++) {
6722 DEBUGADD(6,("Filling builtin form number [%d]\n",i));
6723 result = fill_form_info_1(info, &info[i].info1,
6724 &builtin_forms[i]);
6725 if (!W_ERROR_IS_OK(result)) {
6726 goto out;
6730 for (i=0; i<num_user_forms; i++) {
6731 DEBUGADD(6,("Filling user form number [%d]\n",i));
6732 result = fill_form_info_1(info, &info[i+num_builtin_forms].info1,
6733 &user_forms[i]);
6734 if (!W_ERROR_IS_OK(result)) {
6735 goto out;
6739 out:
6740 if (!W_ERROR_IS_OK(result)) {
6741 TALLOC_FREE(info);
6742 *count = 0;
6743 return result;
6746 *info_p = info;
6748 return WERR_OK;
6751 /****************************************************************
6752 _spoolss_EnumForms
6753 ****************************************************************/
6755 WERROR _spoolss_EnumForms(pipes_struct *p,
6756 struct spoolss_EnumForms *r)
6758 WERROR result;
6759 nt_forms_struct *user_forms = NULL;
6760 nt_forms_struct *builtin_forms = NULL;
6761 uint32_t num_user_forms;
6762 uint32_t num_builtin_forms;
6764 *r->out.count = 0;
6765 *r->out.needed = 0;
6766 *r->out.info = NULL;
6768 /* that's an [in out] buffer */
6770 if (!r->in.buffer && (r->in.offered != 0) ) {
6771 return WERR_INVALID_PARAM;
6774 DEBUG(4,("_spoolss_EnumForms\n"));
6775 DEBUGADD(5,("Offered buffer size [%d]\n", r->in.offered));
6776 DEBUGADD(5,("Info level [%d]\n", r->in.level));
6778 num_builtin_forms = get_builtin_ntforms(&builtin_forms);
6779 DEBUGADD(5,("Number of builtin forms [%d]\n", num_builtin_forms));
6780 num_user_forms = get_ntforms(&user_forms);
6781 DEBUGADD(5,("Number of user forms [%d]\n", num_user_forms));
6783 if (num_user_forms + num_builtin_forms == 0) {
6784 SAFE_FREE(builtin_forms);
6785 SAFE_FREE(user_forms);
6786 return WERR_NO_MORE_ITEMS;
6789 switch (r->in.level) {
6790 case 1:
6791 result = spoolss_enumforms_level1(p->mem_ctx,
6792 builtin_forms,
6793 num_builtin_forms,
6794 user_forms,
6795 num_user_forms,
6796 r->out.info,
6797 r->out.count);
6798 break;
6799 default:
6800 result = WERR_UNKNOWN_LEVEL;
6801 break;
6804 SAFE_FREE(user_forms);
6805 SAFE_FREE(builtin_forms);
6807 if (!W_ERROR_IS_OK(result)) {
6808 return result;
6811 *r->out.needed = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
6812 spoolss_EnumForms, NULL,
6813 *r->out.info, r->in.level,
6814 *r->out.count);
6815 *r->out.info = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
6816 *r->out.count = SPOOLSS_BUFFER_OK(*r->out.count, 0);
6818 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
6821 /****************************************************************
6822 ****************************************************************/
6824 static WERROR find_form_byname(const char *name,
6825 nt_forms_struct *form)
6827 nt_forms_struct *list = NULL;
6828 int num_forms = 0, i = 0;
6830 if (get_a_builtin_ntform_by_string(name, form)) {
6831 return WERR_OK;
6834 num_forms = get_ntforms(&list);
6835 DEBUGADD(5,("Number of forms [%d]\n", num_forms));
6837 if (num_forms == 0) {
6838 return WERR_BADFID;
6841 /* Check if the requested name is in the list of form structures */
6842 for (i = 0; i < num_forms; i++) {
6844 DEBUG(4,("checking form %s (want %s)\n", list[i].name, name));
6846 if (strequal(name, list[i].name)) {
6847 DEBUGADD(6,("Found form %s number [%d]\n", name, i));
6848 *form = list[i];
6849 SAFE_FREE(list);
6850 return WERR_OK;
6854 SAFE_FREE(list);
6856 return WERR_BADFID;
6859 /****************************************************************
6860 _spoolss_GetForm
6861 ****************************************************************/
6863 WERROR _spoolss_GetForm(pipes_struct *p,
6864 struct spoolss_GetForm *r)
6866 WERROR result;
6867 nt_forms_struct form;
6869 /* that's an [in out] buffer */
6871 if (!r->in.buffer && (r->in.offered != 0)) {
6872 return WERR_INVALID_PARAM;
6875 DEBUG(4,("_spoolss_GetForm\n"));
6876 DEBUGADD(5,("Offered buffer size [%d]\n", r->in.offered));
6877 DEBUGADD(5,("Info level [%d]\n", r->in.level));
6879 result = find_form_byname(r->in.form_name, &form);
6880 if (!W_ERROR_IS_OK(result)) {
6881 TALLOC_FREE(r->out.info);
6882 return result;
6885 switch (r->in.level) {
6886 case 1:
6887 result = fill_form_info_1(p->mem_ctx,
6888 &r->out.info->info1,
6889 &form);
6890 break;
6892 default:
6893 result = WERR_UNKNOWN_LEVEL;
6894 break;
6897 if (!W_ERROR_IS_OK(result)) {
6898 TALLOC_FREE(r->out.info);
6899 return result;
6902 *r->out.needed = SPOOLSS_BUFFER_UNION(spoolss_FormInfo, NULL,
6903 r->out.info, r->in.level);
6904 r->out.info = SPOOLSS_BUFFER_OK(r->out.info, NULL);
6906 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
6909 /****************************************************************************
6910 ****************************************************************************/
6912 static WERROR fill_port_1(TALLOC_CTX *mem_ctx,
6913 struct spoolss_PortInfo1 *r,
6914 const char *name)
6916 r->port_name = talloc_strdup(mem_ctx, name);
6917 W_ERROR_HAVE_NO_MEMORY(r->port_name);
6919 return WERR_OK;
6922 /****************************************************************************
6923 TODO: This probably needs distinguish between TCP/IP and Local ports
6924 somehow.
6925 ****************************************************************************/
6927 static WERROR fill_port_2(TALLOC_CTX *mem_ctx,
6928 struct spoolss_PortInfo2 *r,
6929 const char *name)
6931 r->port_name = talloc_strdup(mem_ctx, name);
6932 W_ERROR_HAVE_NO_MEMORY(r->port_name);
6934 r->monitor_name = talloc_strdup(mem_ctx, "Local Monitor");
6935 W_ERROR_HAVE_NO_MEMORY(r->monitor_name);
6937 r->description = talloc_strdup(mem_ctx, SPL_LOCAL_PORT);
6938 W_ERROR_HAVE_NO_MEMORY(r->description);
6940 r->port_type = SPOOLSS_PORT_TYPE_WRITE;
6941 r->reserved = 0;
6943 return WERR_OK;
6947 /****************************************************************************
6948 wrapper around the enumer ports command
6949 ****************************************************************************/
6951 static WERROR enumports_hook(TALLOC_CTX *ctx, int *count, char ***lines)
6953 char *cmd = lp_enumports_cmd();
6954 char **qlines = NULL;
6955 char *command = NULL;
6956 int numlines;
6957 int ret;
6958 int fd;
6960 *count = 0;
6961 *lines = NULL;
6963 /* if no hook then just fill in the default port */
6965 if ( !*cmd ) {
6966 if (!(qlines = TALLOC_ARRAY( NULL, char*, 2 ))) {
6967 return WERR_NOMEM;
6969 if (!(qlines[0] = talloc_strdup(qlines, SAMBA_PRINTER_PORT_NAME ))) {
6970 TALLOC_FREE(qlines);
6971 return WERR_NOMEM;
6973 qlines[1] = NULL;
6974 numlines = 1;
6976 else {
6977 /* we have a valid enumport command */
6979 command = talloc_asprintf(ctx, "%s \"%d\"", cmd, 1);
6980 if (!command) {
6981 return WERR_NOMEM;
6984 DEBUG(10,("Running [%s]\n", command));
6985 ret = smbrun(command, &fd);
6986 DEBUG(10,("Returned [%d]\n", ret));
6987 TALLOC_FREE(command);
6988 if (ret != 0) {
6989 if (fd != -1) {
6990 close(fd);
6992 return WERR_ACCESS_DENIED;
6995 numlines = 0;
6996 qlines = fd_lines_load(fd, &numlines, 0, NULL);
6997 DEBUGADD(10,("Lines returned = [%d]\n", numlines));
6998 close(fd);
7001 *count = numlines;
7002 *lines = qlines;
7004 return WERR_OK;
7007 /****************************************************************************
7008 enumports level 1.
7009 ****************************************************************************/
7011 static WERROR enumports_level_1(TALLOC_CTX *mem_ctx,
7012 union spoolss_PortInfo **info_p,
7013 uint32_t *count)
7015 union spoolss_PortInfo *info = NULL;
7016 int i=0;
7017 WERROR result = WERR_OK;
7018 char **qlines = NULL;
7019 int numlines = 0;
7021 result = enumports_hook(talloc_tos(), &numlines, &qlines );
7022 if (!W_ERROR_IS_OK(result)) {
7023 goto out;
7026 if (numlines) {
7027 info = TALLOC_ARRAY(mem_ctx, union spoolss_PortInfo, numlines);
7028 if (!info) {
7029 DEBUG(10,("Returning WERR_NOMEM\n"));
7030 result = WERR_NOMEM;
7031 goto out;
7034 for (i=0; i<numlines; i++) {
7035 DEBUG(6,("Filling port number [%d] with port [%s]\n", i, qlines[i]));
7036 result = fill_port_1(info, &info[i].info1, qlines[i]);
7037 if (!W_ERROR_IS_OK(result)) {
7038 goto out;
7042 TALLOC_FREE(qlines);
7044 out:
7045 if (!W_ERROR_IS_OK(result)) {
7046 TALLOC_FREE(info);
7047 TALLOC_FREE(qlines);
7048 *count = 0;
7049 *info_p = NULL;
7050 return result;
7053 *info_p = info;
7054 *count = numlines;
7056 return WERR_OK;
7059 /****************************************************************************
7060 enumports level 2.
7061 ****************************************************************************/
7063 static WERROR enumports_level_2(TALLOC_CTX *mem_ctx,
7064 union spoolss_PortInfo **info_p,
7065 uint32_t *count)
7067 union spoolss_PortInfo *info = NULL;
7068 int i=0;
7069 WERROR result = WERR_OK;
7070 char **qlines = NULL;
7071 int numlines = 0;
7073 result = enumports_hook(talloc_tos(), &numlines, &qlines );
7074 if (!W_ERROR_IS_OK(result)) {
7075 goto out;
7078 if (numlines) {
7079 info = TALLOC_ARRAY(mem_ctx, union spoolss_PortInfo, numlines);
7080 if (!info) {
7081 DEBUG(10,("Returning WERR_NOMEM\n"));
7082 result = WERR_NOMEM;
7083 goto out;
7086 for (i=0; i<numlines; i++) {
7087 DEBUG(6,("Filling port number [%d] with port [%s]\n", i, qlines[i]));
7088 result = fill_port_2(info, &info[i].info2, qlines[i]);
7089 if (!W_ERROR_IS_OK(result)) {
7090 goto out;
7094 TALLOC_FREE(qlines);
7096 out:
7097 if (!W_ERROR_IS_OK(result)) {
7098 TALLOC_FREE(info);
7099 TALLOC_FREE(qlines);
7100 *count = 0;
7101 *info_p = NULL;
7102 return result;
7105 *info_p = info;
7106 *count = numlines;
7108 return WERR_OK;
7111 /****************************************************************
7112 _spoolss_EnumPorts
7113 ****************************************************************/
7115 WERROR _spoolss_EnumPorts(pipes_struct *p,
7116 struct spoolss_EnumPorts *r)
7118 WERROR result;
7120 /* that's an [in out] buffer */
7122 if (!r->in.buffer && (r->in.offered != 0)) {
7123 return WERR_INVALID_PARAM;
7126 DEBUG(4,("_spoolss_EnumPorts\n"));
7128 *r->out.count = 0;
7129 *r->out.needed = 0;
7130 *r->out.info = NULL;
7132 switch (r->in.level) {
7133 case 1:
7134 result = enumports_level_1(p->mem_ctx, r->out.info,
7135 r->out.count);
7136 break;
7137 case 2:
7138 result = enumports_level_2(p->mem_ctx, r->out.info,
7139 r->out.count);
7140 break;
7141 default:
7142 return WERR_UNKNOWN_LEVEL;
7145 if (!W_ERROR_IS_OK(result)) {
7146 return result;
7149 *r->out.needed = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
7150 spoolss_EnumPorts, NULL,
7151 *r->out.info, r->in.level,
7152 *r->out.count);
7153 *r->out.info = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
7154 *r->out.count = SPOOLSS_BUFFER_OK(*r->out.count, 0);
7156 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
7159 /****************************************************************************
7160 ****************************************************************************/
7162 static WERROR spoolss_addprinterex_level_2(pipes_struct *p,
7163 const char *server,
7164 struct spoolss_SetPrinterInfoCtr *info_ctr,
7165 struct spoolss_DeviceMode *devmode,
7166 struct security_descriptor *sec_desc,
7167 struct spoolss_UserLevelCtr *user_ctr,
7168 struct policy_handle *handle)
7170 NT_PRINTER_INFO_LEVEL *printer = NULL;
7171 fstring name;
7172 int snum;
7173 WERROR err = WERR_OK;
7175 if ( !(printer = TALLOC_ZERO_P(NULL, NT_PRINTER_INFO_LEVEL)) ) {
7176 DEBUG(0,("spoolss_addprinterex_level_2: malloc fail.\n"));
7177 return WERR_NOMEM;
7180 /* convert from UNICODE to ASCII - this allocates the info_2 struct inside *printer.*/
7181 if (!convert_printer_info(info_ctr, printer)) {
7182 free_a_printer(&printer, 2);
7183 return WERR_NOMEM;
7186 /* check to see if the printer already exists */
7188 if ((snum = print_queue_snum(printer->info_2->sharename)) != -1) {
7189 DEBUG(5, ("spoolss_addprinterex_level_2: Attempted to add a printer named [%s] when one already existed!\n",
7190 printer->info_2->sharename));
7191 free_a_printer(&printer, 2);
7192 return WERR_PRINTER_ALREADY_EXISTS;
7195 /* FIXME!!! smbd should check to see if the driver is installed before
7196 trying to add a printer like this --jerry */
7198 if (*lp_addprinter_cmd() ) {
7199 if ( !add_printer_hook(p->mem_ctx, p->server_info->ptok,
7200 printer) ) {
7201 free_a_printer(&printer,2);
7202 return WERR_ACCESS_DENIED;
7204 } else {
7205 DEBUG(0,("spoolss_addprinterex_level_2: add printer for printer %s called and no"
7206 "smb.conf parameter \"addprinter command\" is defined. This"
7207 "parameter must exist for this call to succeed\n",
7208 printer->info_2->sharename ));
7211 /* use our primary netbios name since get_a_printer() will convert
7212 it to what the client expects on a case by case basis */
7214 slprintf(name, sizeof(name)-1, "\\\\%s\\%s", global_myname(),
7215 printer->info_2->sharename);
7218 if ((snum = print_queue_snum(printer->info_2->sharename)) == -1) {
7219 free_a_printer(&printer,2);
7220 return WERR_ACCESS_DENIED;
7223 /* you must be a printer admin to add a new printer */
7224 if (!print_access_check(p->server_info, snum, PRINTER_ACCESS_ADMINISTER)) {
7225 free_a_printer(&printer,2);
7226 return WERR_ACCESS_DENIED;
7230 * Do sanity check on the requested changes for Samba.
7233 if (!check_printer_ok(printer->info_2, snum)) {
7234 free_a_printer(&printer,2);
7235 return WERR_INVALID_PARAM;
7239 * When a printer is created, the drivername bound to the printer is used
7240 * to lookup previously saved driver initialization info, which is then
7241 * bound to the new printer, simulating what happens in the Windows arch.
7244 if (!devmode)
7246 set_driver_init(printer, 2);
7248 else
7250 /* A valid devmode was included, convert and link it
7252 DEBUGADD(10, ("spoolss_addprinterex_level_2: devmode included, converting\n"));
7254 if (!convert_devicemode(printer->info_2->printername, devmode,
7255 &printer->info_2->devmode)) {
7256 return WERR_NOMEM;
7260 /* write the ASCII on disk */
7261 err = mod_a_printer(printer, 2);
7262 if (!W_ERROR_IS_OK(err)) {
7263 free_a_printer(&printer,2);
7264 return err;
7267 if (!open_printer_hnd(p, handle, name, PRINTER_ACCESS_ADMINISTER)) {
7268 /* Handle open failed - remove addition. */
7269 del_a_printer(printer->info_2->sharename);
7270 free_a_printer(&printer,2);
7271 ZERO_STRUCTP(handle);
7272 return WERR_ACCESS_DENIED;
7275 update_c_setprinter(false);
7276 free_a_printer(&printer,2);
7278 return WERR_OK;
7281 /****************************************************************
7282 _spoolss_AddPrinterEx
7283 ****************************************************************/
7285 WERROR _spoolss_AddPrinterEx(pipes_struct *p,
7286 struct spoolss_AddPrinterEx *r)
7288 switch (r->in.info_ctr->level) {
7289 case 1:
7290 /* we don't handle yet */
7291 /* but I know what to do ... */
7292 return WERR_UNKNOWN_LEVEL;
7293 case 2:
7294 return spoolss_addprinterex_level_2(p, r->in.server,
7295 r->in.info_ctr,
7296 r->in.devmode_ctr->devmode,
7297 r->in.secdesc_ctr->sd,
7298 r->in.userlevel_ctr,
7299 r->out.handle);
7300 default:
7301 return WERR_UNKNOWN_LEVEL;
7305 /****************************************************************
7306 _spoolss_AddPrinter
7307 ****************************************************************/
7309 WERROR _spoolss_AddPrinter(pipes_struct *p,
7310 struct spoolss_AddPrinter *r)
7312 struct spoolss_AddPrinterEx a;
7313 struct spoolss_UserLevelCtr userlevel_ctr;
7315 ZERO_STRUCT(userlevel_ctr);
7317 userlevel_ctr.level = 1;
7319 a.in.server = r->in.server;
7320 a.in.info_ctr = r->in.info_ctr;
7321 a.in.devmode_ctr = r->in.devmode_ctr;
7322 a.in.secdesc_ctr = r->in.secdesc_ctr;
7323 a.in.userlevel_ctr = &userlevel_ctr;
7324 a.out.handle = r->out.handle;
7326 return _spoolss_AddPrinterEx(p, &a);
7329 /****************************************************************
7330 _spoolss_AddPrinterDriver
7331 ****************************************************************/
7333 WERROR _spoolss_AddPrinterDriver(pipes_struct *p,
7334 struct spoolss_AddPrinterDriver *r)
7336 WERROR err = WERR_OK;
7337 char *driver_name = NULL;
7338 uint32_t version;
7339 const char *fn;
7341 switch (p->hdr_req.opnum) {
7342 case NDR_SPOOLSS_ADDPRINTERDRIVER:
7343 fn = "_spoolss_AddPrinterDriver";
7344 break;
7345 case NDR_SPOOLSS_ADDPRINTERDRIVEREX:
7346 fn = "_spoolss_AddPrinterDriverEx";
7347 break;
7348 default:
7349 return WERR_INVALID_PARAM;
7353 /* FIXME */
7354 if (r->in.info_ctr->level != 3 && r->in.info_ctr->level != 6) {
7355 /* Clever hack from Martin Zielinski <mz@seh.de>
7356 * to allow downgrade from level 8 (Vista).
7358 DEBUG(0,("%s: level %d not yet implemented\n", fn,
7359 r->in.info_ctr->level));
7360 return WERR_UNKNOWN_LEVEL;
7363 DEBUG(5,("Cleaning driver's information\n"));
7364 err = clean_up_driver_struct(p, r->in.info_ctr);
7365 if (!W_ERROR_IS_OK(err))
7366 goto done;
7368 DEBUG(5,("Moving driver to final destination\n"));
7369 if( !W_ERROR_IS_OK(err = move_driver_to_download_area(p, r->in.info_ctr,
7370 &err)) ) {
7371 goto done;
7374 if (add_a_printer_driver(p->mem_ctx, r->in.info_ctr, &driver_name, &version)!=0) {
7375 err = WERR_ACCESS_DENIED;
7376 goto done;
7380 * I think this is where he DrvUpgradePrinter() hook would be
7381 * be called in a driver's interface DLL on a Windows NT 4.0/2k
7382 * server. Right now, we just need to send ourselves a message
7383 * to update each printer bound to this driver. --jerry
7386 if (!srv_spoolss_drv_upgrade_printer(driver_name)) {
7387 DEBUG(0,("%s: Failed to send message about upgrading driver [%s]!\n",
7388 fn, driver_name));
7392 * Based on the version (e.g. driver destination dir: 0=9x,2=Nt/2k,3=2k/Xp),
7393 * decide if the driver init data should be deleted. The rules are:
7394 * 1) never delete init data if it is a 9x driver, they don't use it anyway
7395 * 2) delete init data only if there is no 2k/Xp driver
7396 * 3) always delete init data
7397 * The generalized rule is always use init data from the highest order driver.
7398 * It is necessary to follow the driver install by an initialization step to
7399 * finish off this process.
7402 switch (version) {
7404 * 9x printer driver - never delete init data
7406 case 0:
7407 DEBUG(10,("%s: init data not deleted for 9x driver [%s]\n",
7408 fn, driver_name));
7409 break;
7412 * Nt or 2k (compatiblity mode) printer driver - only delete init data if
7413 * there is no 2k/Xp driver init data for this driver name.
7415 case 2:
7417 struct spoolss_DriverInfo8 *driver1;
7419 if (!W_ERROR_IS_OK(get_a_printer_driver(p->mem_ctx, &driver1, driver_name, "Windows NT x86", 3))) {
7421 * No 2k/Xp driver found, delete init data (if any) for the new Nt driver.
7423 if (!del_driver_init(driver_name))
7424 DEBUG(6,("%s: del_driver_init(%s) Nt failed!\n",
7425 fn, driver_name));
7426 } else {
7428 * a 2k/Xp driver was found, don't delete init data because Nt driver will use it.
7430 free_a_printer_driver(driver1);
7431 DEBUG(10,("%s: init data not deleted for Nt driver [%s]\n",
7432 fn, driver_name));
7435 break;
7438 * 2k or Xp printer driver - always delete init data
7440 case 3:
7441 if (!del_driver_init(driver_name))
7442 DEBUG(6,("%s: del_driver_init(%s) 2k/Xp failed!\n",
7443 fn, driver_name));
7444 break;
7446 default:
7447 DEBUG(0,("%s: invalid level=%d\n", fn,
7448 r->in.info_ctr->level));
7449 break;
7453 done:
7454 return err;
7457 /****************************************************************
7458 _spoolss_AddPrinterDriverEx
7459 ****************************************************************/
7461 WERROR _spoolss_AddPrinterDriverEx(pipes_struct *p,
7462 struct spoolss_AddPrinterDriverEx *r)
7464 struct spoolss_AddPrinterDriver a;
7467 * we only support the semantics of AddPrinterDriver()
7468 * i.e. only copy files that are newer than existing ones
7471 if (r->in.flags != APD_COPY_NEW_FILES) {
7472 return WERR_ACCESS_DENIED;
7475 a.in.servername = r->in.servername;
7476 a.in.info_ctr = r->in.info_ctr;
7478 return _spoolss_AddPrinterDriver(p, &a);
7481 /****************************************************************************
7482 ****************************************************************************/
7484 struct _spoolss_paths {
7485 int type;
7486 const char *share;
7487 const char *dir;
7490 enum { SPOOLSS_DRIVER_PATH, SPOOLSS_PRTPROCS_PATH };
7492 static const struct _spoolss_paths spoolss_paths[]= {
7493 { SPOOLSS_DRIVER_PATH, "print$", "DRIVERS" },
7494 { SPOOLSS_PRTPROCS_PATH, "prnproc$", "PRTPROCS" }
7497 static WERROR compose_spoolss_server_path(TALLOC_CTX *mem_ctx,
7498 const char *servername,
7499 const char *environment,
7500 int component,
7501 char **path)
7503 const char *pservername = NULL;
7504 const char *long_archi = SPOOLSS_ARCHITECTURE_NT_X86;
7505 const char *short_archi;
7507 *path = NULL;
7509 /* environment may be empty */
7510 if (environment && strlen(environment)) {
7511 long_archi = environment;
7514 /* servername may be empty */
7515 if (servername && strlen(servername)) {
7516 pservername = canon_servername(servername);
7518 if (!is_myname_or_ipaddr(pservername)) {
7519 return WERR_INVALID_PARAM;
7523 if (!(short_archi = get_short_archi(long_archi))) {
7524 return WERR_INVALID_ENVIRONMENT;
7527 switch (component) {
7528 case SPOOLSS_PRTPROCS_PATH:
7529 case SPOOLSS_DRIVER_PATH:
7530 if (pservername) {
7531 *path = talloc_asprintf(mem_ctx,
7532 "\\\\%s\\%s\\%s",
7533 pservername,
7534 spoolss_paths[component].share,
7535 short_archi);
7536 } else {
7537 *path = talloc_asprintf(mem_ctx, "%s\\%s\\%s",
7538 SPOOLSS_DEFAULT_SERVER_PATH,
7539 spoolss_paths[component].dir,
7540 short_archi);
7542 break;
7543 default:
7544 return WERR_INVALID_PARAM;
7547 if (!*path) {
7548 return WERR_NOMEM;
7551 return WERR_OK;
7554 /****************************************************************************
7555 ****************************************************************************/
7557 static WERROR getprinterdriverdir_level_1(TALLOC_CTX *mem_ctx,
7558 const char *servername,
7559 const char *environment,
7560 struct spoolss_DriverDirectoryInfo1 *r)
7562 WERROR werr;
7563 char *path = NULL;
7565 werr = compose_spoolss_server_path(mem_ctx,
7566 servername,
7567 environment,
7568 SPOOLSS_DRIVER_PATH,
7569 &path);
7570 if (!W_ERROR_IS_OK(werr)) {
7571 return werr;
7574 DEBUG(4,("printer driver directory: [%s]\n", path));
7576 r->directory_name = path;
7578 return WERR_OK;
7581 /****************************************************************
7582 _spoolss_GetPrinterDriverDirectory
7583 ****************************************************************/
7585 WERROR _spoolss_GetPrinterDriverDirectory(pipes_struct *p,
7586 struct spoolss_GetPrinterDriverDirectory *r)
7588 WERROR werror;
7590 /* that's an [in out] buffer */
7592 if (!r->in.buffer && (r->in.offered != 0)) {
7593 return WERR_INVALID_PARAM;
7596 DEBUG(5,("_spoolss_GetPrinterDriverDirectory: level %d\n",
7597 r->in.level));
7599 *r->out.needed = 0;
7601 /* r->in.level is ignored */
7603 werror = getprinterdriverdir_level_1(p->mem_ctx,
7604 r->in.server,
7605 r->in.environment,
7606 &r->out.info->info1);
7607 if (!W_ERROR_IS_OK(werror)) {
7608 TALLOC_FREE(r->out.info);
7609 return werror;
7612 *r->out.needed = SPOOLSS_BUFFER_UNION(spoolss_DriverDirectoryInfo, NULL,
7613 r->out.info, r->in.level);
7614 r->out.info = SPOOLSS_BUFFER_OK(r->out.info, NULL);
7616 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
7619 /****************************************************************
7620 _spoolss_EnumPrinterData
7621 ****************************************************************/
7623 WERROR _spoolss_EnumPrinterData(pipes_struct *p,
7624 struct spoolss_EnumPrinterData *r)
7626 NT_PRINTER_INFO_LEVEL *printer = NULL;
7627 Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
7628 int snum;
7629 WERROR result;
7630 struct regval_blob *val = NULL;
7631 NT_PRINTER_DATA *p_data;
7632 int i, key_index, num_values;
7633 int name_length;
7635 *r->out.value_needed = 0;
7636 *r->out.type = REG_NONE;
7637 *r->out.data_needed = 0;
7639 DEBUG(5,("_spoolss_EnumPrinterData\n"));
7641 if (!Printer) {
7642 DEBUG(2,("_spoolss_EnumPrinterData: Invalid handle (%s:%u:%u).\n",
7643 OUR_HANDLE(r->in.handle)));
7644 return WERR_BADFID;
7647 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
7648 return WERR_BADFID;
7651 result = get_a_printer(Printer, &printer, 2, lp_const_servicename(snum));
7652 if (!W_ERROR_IS_OK(result)) {
7653 return result;
7656 p_data = printer->info_2->data;
7657 key_index = lookup_printerkey( p_data, SPOOL_PRINTERDATA_KEY );
7659 result = WERR_OK;
7662 * The NT machine wants to know the biggest size of value and data
7664 * cf: MSDN EnumPrinterData remark section
7667 if (!r->in.value_offered && !r->in.data_offered && (key_index != -1)) {
7669 uint32_t biggest_valuesize = 0;
7670 uint32_t biggest_datasize = 0;
7672 DEBUGADD(6,("Activating NT mega-hack to find sizes\n"));
7674 num_values = regval_ctr_numvals( p_data->keys[key_index].values );
7676 for ( i=0; i<num_values; i++ )
7678 val = regval_ctr_specific_value( p_data->keys[key_index].values, i );
7680 name_length = strlen(val->valuename);
7681 if ( strlen(val->valuename) > biggest_valuesize )
7682 biggest_valuesize = name_length;
7684 if ( val->size > biggest_datasize )
7685 biggest_datasize = val->size;
7687 DEBUG(6,("current values: [%d], [%d]\n", biggest_valuesize,
7688 biggest_datasize));
7691 /* the value is an UNICODE string but real_value_size is the length
7692 in bytes including the trailing 0 */
7694 *r->out.value_needed = 2 * (1 + biggest_valuesize);
7695 *r->out.data_needed = biggest_datasize;
7697 DEBUG(6,("final values: [%d], [%d]\n",
7698 *r->out.value_needed, *r->out.data_needed));
7700 goto done;
7704 * the value len is wrong in NT sp3
7705 * that's the number of bytes not the number of unicode chars
7708 if (key_index != -1) {
7709 val = regval_ctr_specific_value(p_data->keys[key_index].values,
7710 r->in.enum_index);
7713 if (!val) {
7715 /* out_value should default to "" or else NT4 has
7716 problems unmarshalling the response */
7718 if (r->in.value_offered) {
7719 *r->out.value_needed = 1;
7720 r->out.value_name = talloc_strdup(r, "");
7721 if (!r->out.value_name) {
7722 result = WERR_NOMEM;
7723 goto done;
7725 } else {
7726 r->out.value_name = NULL;
7727 *r->out.value_needed = 0;
7730 /* the data is counted in bytes */
7732 *r->out.data_needed = r->in.data_offered;
7734 result = WERR_NO_MORE_ITEMS;
7735 } else {
7737 * the value is:
7738 * - counted in bytes in the request
7739 * - counted in UNICODE chars in the max reply
7740 * - counted in bytes in the real size
7742 * take a pause *before* coding not *during* coding
7745 /* name */
7746 if (r->in.value_offered) {
7747 r->out.value_name = talloc_strdup(r, regval_name(val));
7748 if (!r->out.value_name) {
7749 result = WERR_NOMEM;
7750 goto done;
7752 *r->out.value_needed = strlen_m(regval_name(val));
7753 } else {
7754 r->out.value_name = NULL;
7755 *r->out.value_needed = 0;
7758 /* type */
7760 *r->out.type = regval_type(val);
7762 /* data - counted in bytes */
7764 if (r->out.data && regval_size(val)) {
7765 memcpy(r->out.data, regval_data_p(val), regval_size(val));
7768 *r->out.data_needed = regval_size(val);
7771 done:
7772 free_a_printer(&printer, 2);
7773 return result;
7776 /****************************************************************
7777 _spoolss_SetPrinterData
7778 ****************************************************************/
7780 WERROR _spoolss_SetPrinterData(pipes_struct *p,
7781 struct spoolss_SetPrinterData *r)
7783 struct spoolss_SetPrinterDataEx r2;
7785 r2.in.handle = r->in.handle;
7786 r2.in.key_name = "PrinterDriverData";
7787 r2.in.value_name = r->in.value_name;
7788 r2.in.type = r->in.type;
7789 r2.in.data = r->in.data;
7790 r2.in._offered = r->in._offered;
7792 return _spoolss_SetPrinterDataEx(p, &r2);
7795 /****************************************************************
7796 _spoolss_ResetPrinter
7797 ****************************************************************/
7799 WERROR _spoolss_ResetPrinter(pipes_struct *p,
7800 struct spoolss_ResetPrinter *r)
7802 Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
7803 int snum;
7805 DEBUG(5,("_spoolss_ResetPrinter\n"));
7808 * All we do is to check to see if the handle and queue is valid.
7809 * This call really doesn't mean anything to us because we only
7810 * support RAW printing. --jerry
7813 if (!Printer) {
7814 DEBUG(2,("_spoolss_ResetPrinter: Invalid handle (%s:%u:%u).\n",
7815 OUR_HANDLE(r->in.handle)));
7816 return WERR_BADFID;
7819 if (!get_printer_snum(p, r->in.handle, &snum, NULL))
7820 return WERR_BADFID;
7823 /* blindly return success */
7824 return WERR_OK;
7827 /****************************************************************
7828 _spoolss_DeletePrinterData
7829 ****************************************************************/
7831 WERROR _spoolss_DeletePrinterData(pipes_struct *p,
7832 struct spoolss_DeletePrinterData *r)
7834 struct spoolss_DeletePrinterDataEx r2;
7836 r2.in.handle = r->in.handle;
7837 r2.in.key_name = "PrinterDriverData";
7838 r2.in.value_name = r->in.value_name;
7840 return _spoolss_DeletePrinterDataEx(p, &r2);
7843 /****************************************************************
7844 _spoolss_AddForm
7845 ****************************************************************/
7847 WERROR _spoolss_AddForm(pipes_struct *p,
7848 struct spoolss_AddForm *r)
7850 struct spoolss_AddFormInfo1 *form = r->in.info.info1;
7851 nt_forms_struct tmpForm;
7852 int snum = -1;
7853 WERROR status = WERR_OK;
7854 NT_PRINTER_INFO_LEVEL *printer = NULL;
7855 SE_PRIV se_printop = SE_PRINT_OPERATOR;
7857 int count=0;
7858 nt_forms_struct *list=NULL;
7859 Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
7861 DEBUG(5,("_spoolss_AddForm\n"));
7863 if (!Printer) {
7864 DEBUG(2,("_spoolss_AddForm: Invalid handle (%s:%u:%u).\n",
7865 OUR_HANDLE(r->in.handle)));
7866 return WERR_BADFID;
7870 /* forms can be added on printer of on the print server handle */
7872 if ( Printer->printer_type == SPLHND_PRINTER )
7874 if (!get_printer_snum(p, r->in.handle, &snum, NULL))
7875 return WERR_BADFID;
7877 status = get_a_printer(Printer, &printer, 2, lp_const_servicename(snum));
7878 if (!W_ERROR_IS_OK(status))
7879 goto done;
7882 /* if the user is not root, doesn't have SE_PRINT_OPERATOR privilege,
7883 and not a printer admin, then fail */
7885 if ((p->server_info->utok.uid != sec_initial_uid()) &&
7886 !user_has_privileges(p->server_info->ptok, &se_printop) &&
7887 !token_contains_name_in_list(uidtoname(p->server_info->utok.uid),
7888 NULL, NULL,
7889 p->server_info->ptok,
7890 lp_printer_admin(snum))) {
7891 DEBUG(2,("_spoolss_Addform: denied by insufficient permissions.\n"));
7892 return WERR_ACCESS_DENIED;
7895 /* can't add if builtin */
7897 if (get_a_builtin_ntform_by_string(form->form_name, &tmpForm)) {
7898 status = WERR_FILE_EXISTS;
7899 goto done;
7902 count = get_ntforms(&list);
7904 if(!add_a_form(&list, form, &count)) {
7905 status = WERR_NOMEM;
7906 goto done;
7909 become_root();
7910 write_ntforms(&list, count);
7911 unbecome_root();
7914 * ChangeID must always be set if this is a printer
7917 if ( Printer->printer_type == SPLHND_PRINTER )
7918 status = mod_a_printer(printer, 2);
7920 done:
7921 if ( printer )
7922 free_a_printer(&printer, 2);
7923 SAFE_FREE(list);
7925 return status;
7928 /****************************************************************
7929 _spoolss_DeleteForm
7930 ****************************************************************/
7932 WERROR _spoolss_DeleteForm(pipes_struct *p,
7933 struct spoolss_DeleteForm *r)
7935 const char *form_name = r->in.form_name;
7936 nt_forms_struct tmpForm;
7937 int count=0;
7938 nt_forms_struct *list=NULL;
7939 Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
7940 int snum = -1;
7941 WERROR status = WERR_OK;
7942 NT_PRINTER_INFO_LEVEL *printer = NULL;
7943 SE_PRIV se_printop = SE_PRINT_OPERATOR;
7944 bool ret = false;
7946 DEBUG(5,("_spoolss_DeleteForm\n"));
7948 if (!Printer) {
7949 DEBUG(2,("_spoolss_DeleteForm: Invalid handle (%s:%u:%u).\n",
7950 OUR_HANDLE(r->in.handle)));
7951 return WERR_BADFID;
7954 /* forms can be deleted on printer of on the print server handle */
7956 if ( Printer->printer_type == SPLHND_PRINTER )
7958 if (!get_printer_snum(p, r->in.handle, &snum, NULL))
7959 return WERR_BADFID;
7961 status = get_a_printer(Printer, &printer, 2, lp_const_servicename(snum));
7962 if (!W_ERROR_IS_OK(status))
7963 goto done;
7966 if ((p->server_info->utok.uid != sec_initial_uid()) &&
7967 !user_has_privileges(p->server_info->ptok, &se_printop) &&
7968 !token_contains_name_in_list(uidtoname(p->server_info->utok.uid),
7969 NULL, NULL,
7970 p->server_info->ptok,
7971 lp_printer_admin(snum))) {
7972 DEBUG(2,("_spoolss_DeleteForm: denied by insufficient permissions.\n"));
7973 return WERR_ACCESS_DENIED;
7977 /* can't delete if builtin */
7979 if (get_a_builtin_ntform_by_string(form_name,&tmpForm)) {
7980 status = WERR_INVALID_PARAM;
7981 goto done;
7984 count = get_ntforms(&list);
7986 become_root();
7987 ret = delete_a_form(&list, form_name, &count, &status);
7988 unbecome_root();
7989 if (ret == false) {
7990 goto done;
7994 * ChangeID must always be set if this is a printer
7997 if ( Printer->printer_type == SPLHND_PRINTER )
7998 status = mod_a_printer(printer, 2);
8000 done:
8001 if ( printer )
8002 free_a_printer(&printer, 2);
8003 SAFE_FREE(list);
8005 return status;
8008 /****************************************************************
8009 _spoolss_SetForm
8010 ****************************************************************/
8012 WERROR _spoolss_SetForm(pipes_struct *p,
8013 struct spoolss_SetForm *r)
8015 struct spoolss_AddFormInfo1 *form = r->in.info.info1;
8016 nt_forms_struct tmpForm;
8017 int snum = -1;
8018 WERROR status = WERR_OK;
8019 NT_PRINTER_INFO_LEVEL *printer = NULL;
8020 SE_PRIV se_printop = SE_PRINT_OPERATOR;
8022 int count=0;
8023 nt_forms_struct *list=NULL;
8024 Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
8026 DEBUG(5,("_spoolss_SetForm\n"));
8028 if (!Printer) {
8029 DEBUG(2,("_spoolss_SetForm: Invalid handle (%s:%u:%u).\n",
8030 OUR_HANDLE(r->in.handle)));
8031 return WERR_BADFID;
8034 /* forms can be modified on printer of on the print server handle */
8036 if ( Printer->printer_type == SPLHND_PRINTER )
8038 if (!get_printer_snum(p, r->in.handle, &snum, NULL))
8039 return WERR_BADFID;
8041 status = get_a_printer(Printer, &printer, 2, lp_const_servicename(snum));
8042 if (!W_ERROR_IS_OK(status))
8043 goto done;
8046 /* if the user is not root, doesn't have SE_PRINT_OPERATOR privilege,
8047 and not a printer admin, then fail */
8049 if ((p->server_info->utok.uid != sec_initial_uid()) &&
8050 !user_has_privileges(p->server_info->ptok, &se_printop) &&
8051 !token_contains_name_in_list(uidtoname(p->server_info->utok.uid),
8052 NULL, NULL,
8053 p->server_info->ptok,
8054 lp_printer_admin(snum))) {
8055 DEBUG(2,("_spoolss_Setform: denied by insufficient permissions.\n"));
8056 return WERR_ACCESS_DENIED;
8059 /* can't set if builtin */
8060 if (get_a_builtin_ntform_by_string(form->form_name, &tmpForm)) {
8061 status = WERR_INVALID_PARAM;
8062 goto done;
8065 count = get_ntforms(&list);
8066 update_a_form(&list, form, count);
8067 become_root();
8068 write_ntforms(&list, count);
8069 unbecome_root();
8072 * ChangeID must always be set if this is a printer
8075 if ( Printer->printer_type == SPLHND_PRINTER )
8076 status = mod_a_printer(printer, 2);
8079 done:
8080 if ( printer )
8081 free_a_printer(&printer, 2);
8082 SAFE_FREE(list);
8084 return status;
8087 /****************************************************************************
8088 fill_print_processor1
8089 ****************************************************************************/
8091 static WERROR fill_print_processor1(TALLOC_CTX *mem_ctx,
8092 struct spoolss_PrintProcessorInfo1 *r,
8093 const char *print_processor_name)
8095 r->print_processor_name = talloc_strdup(mem_ctx, print_processor_name);
8096 W_ERROR_HAVE_NO_MEMORY(r->print_processor_name);
8098 return WERR_OK;
8101 /****************************************************************************
8102 enumprintprocessors level 1.
8103 ****************************************************************************/
8105 static WERROR enumprintprocessors_level_1(TALLOC_CTX *mem_ctx,
8106 union spoolss_PrintProcessorInfo **info_p,
8107 uint32_t *count)
8109 union spoolss_PrintProcessorInfo *info;
8110 WERROR result;
8112 info = TALLOC_ARRAY(mem_ctx, union spoolss_PrintProcessorInfo, 1);
8113 W_ERROR_HAVE_NO_MEMORY(info);
8115 *count = 1;
8117 result = fill_print_processor1(info, &info[0].info1, "winprint");
8118 if (!W_ERROR_IS_OK(result)) {
8119 goto out;
8122 out:
8123 if (!W_ERROR_IS_OK(result)) {
8124 TALLOC_FREE(info);
8125 *count = 0;
8126 return result;
8129 *info_p = info;
8131 return WERR_OK;
8134 /****************************************************************
8135 _spoolss_EnumPrintProcessors
8136 ****************************************************************/
8138 WERROR _spoolss_EnumPrintProcessors(pipes_struct *p,
8139 struct spoolss_EnumPrintProcessors *r)
8141 WERROR result;
8143 /* that's an [in out] buffer */
8145 if (!r->in.buffer && (r->in.offered != 0)) {
8146 return WERR_INVALID_PARAM;
8149 DEBUG(5,("_spoolss_EnumPrintProcessors\n"));
8152 * Enumerate the print processors ...
8154 * Just reply with "winprint", to keep NT happy
8155 * and I can use my nice printer checker.
8158 *r->out.count = 0;
8159 *r->out.needed = 0;
8160 *r->out.info = NULL;
8162 switch (r->in.level) {
8163 case 1:
8164 result = enumprintprocessors_level_1(p->mem_ctx, r->out.info,
8165 r->out.count);
8166 break;
8167 default:
8168 return WERR_UNKNOWN_LEVEL;
8171 if (!W_ERROR_IS_OK(result)) {
8172 return result;
8175 *r->out.needed = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
8176 spoolss_EnumPrintProcessors, NULL,
8177 *r->out.info, r->in.level,
8178 *r->out.count);
8179 *r->out.info = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
8180 *r->out.count = SPOOLSS_BUFFER_OK(*r->out.count, 0);
8182 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
8185 /****************************************************************************
8186 fill_printprocdatatype1
8187 ****************************************************************************/
8189 static WERROR fill_printprocdatatype1(TALLOC_CTX *mem_ctx,
8190 struct spoolss_PrintProcDataTypesInfo1 *r,
8191 const char *name_array)
8193 r->name_array = talloc_strdup(mem_ctx, name_array);
8194 W_ERROR_HAVE_NO_MEMORY(r->name_array);
8196 return WERR_OK;
8199 /****************************************************************************
8200 enumprintprocdatatypes level 1.
8201 ****************************************************************************/
8203 static WERROR enumprintprocdatatypes_level_1(TALLOC_CTX *mem_ctx,
8204 union spoolss_PrintProcDataTypesInfo **info_p,
8205 uint32_t *count)
8207 WERROR result;
8208 union spoolss_PrintProcDataTypesInfo *info;
8210 info = TALLOC_ARRAY(mem_ctx, union spoolss_PrintProcDataTypesInfo, 1);
8211 W_ERROR_HAVE_NO_MEMORY(info);
8213 *count = 1;
8215 result = fill_printprocdatatype1(info, &info[0].info1, "RAW");
8216 if (!W_ERROR_IS_OK(result)) {
8217 goto out;
8220 out:
8221 if (!W_ERROR_IS_OK(result)) {
8222 TALLOC_FREE(info);
8223 *count = 0;
8224 return result;
8227 *info_p = info;
8229 return WERR_OK;
8232 /****************************************************************
8233 _spoolss_EnumPrintProcDataTypes
8234 ****************************************************************/
8236 WERROR _spoolss_EnumPrintProcDataTypes(pipes_struct *p,
8237 struct spoolss_EnumPrintProcDataTypes *r)
8239 WERROR result;
8241 /* that's an [in out] buffer */
8243 if (!r->in.buffer && (r->in.offered != 0)) {
8244 return WERR_INVALID_PARAM;
8247 DEBUG(5,("_spoolss_EnumPrintProcDataTypes\n"));
8249 *r->out.count = 0;
8250 *r->out.needed = 0;
8251 *r->out.info = NULL;
8253 switch (r->in.level) {
8254 case 1:
8255 result = enumprintprocdatatypes_level_1(p->mem_ctx, r->out.info,
8256 r->out.count);
8257 break;
8258 default:
8259 return WERR_UNKNOWN_LEVEL;
8262 *r->out.needed = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
8263 spoolss_EnumPrintProcDataTypes, NULL,
8264 *r->out.info, r->in.level,
8265 *r->out.count);
8266 *r->out.info = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
8267 *r->out.count = SPOOLSS_BUFFER_OK(*r->out.count, 0);
8269 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
8272 /****************************************************************************
8273 fill_monitor_1
8274 ****************************************************************************/
8276 static WERROR fill_monitor_1(TALLOC_CTX *mem_ctx,
8277 struct spoolss_MonitorInfo1 *r,
8278 const char *monitor_name)
8280 r->monitor_name = talloc_strdup(mem_ctx, monitor_name);
8281 W_ERROR_HAVE_NO_MEMORY(r->monitor_name);
8283 return WERR_OK;
8286 /****************************************************************************
8287 fill_monitor_2
8288 ****************************************************************************/
8290 static WERROR fill_monitor_2(TALLOC_CTX *mem_ctx,
8291 struct spoolss_MonitorInfo2 *r,
8292 const char *monitor_name,
8293 const char *environment,
8294 const char *dll_name)
8296 r->monitor_name = talloc_strdup(mem_ctx, monitor_name);
8297 W_ERROR_HAVE_NO_MEMORY(r->monitor_name);
8298 r->environment = talloc_strdup(mem_ctx, environment);
8299 W_ERROR_HAVE_NO_MEMORY(r->environment);
8300 r->dll_name = talloc_strdup(mem_ctx, dll_name);
8301 W_ERROR_HAVE_NO_MEMORY(r->dll_name);
8303 return WERR_OK;
8306 /****************************************************************************
8307 enumprintmonitors level 1.
8308 ****************************************************************************/
8310 static WERROR enumprintmonitors_level_1(TALLOC_CTX *mem_ctx,
8311 union spoolss_MonitorInfo **info_p,
8312 uint32_t *count)
8314 union spoolss_MonitorInfo *info;
8315 WERROR result = WERR_OK;
8317 info = TALLOC_ARRAY(mem_ctx, union spoolss_MonitorInfo, 2);
8318 W_ERROR_HAVE_NO_MEMORY(info);
8320 *count = 2;
8322 result = fill_monitor_1(info, &info[0].info1,
8323 SPL_LOCAL_PORT);
8324 if (!W_ERROR_IS_OK(result)) {
8325 goto out;
8328 result = fill_monitor_1(info, &info[1].info1,
8329 SPL_TCPIP_PORT);
8330 if (!W_ERROR_IS_OK(result)) {
8331 goto out;
8334 out:
8335 if (!W_ERROR_IS_OK(result)) {
8336 TALLOC_FREE(info);
8337 *count = 0;
8338 return result;
8341 *info_p = info;
8343 return WERR_OK;
8346 /****************************************************************************
8347 enumprintmonitors level 2.
8348 ****************************************************************************/
8350 static WERROR enumprintmonitors_level_2(TALLOC_CTX *mem_ctx,
8351 union spoolss_MonitorInfo **info_p,
8352 uint32_t *count)
8354 union spoolss_MonitorInfo *info;
8355 WERROR result = WERR_OK;
8357 info = TALLOC_ARRAY(mem_ctx, union spoolss_MonitorInfo, 2);
8358 W_ERROR_HAVE_NO_MEMORY(info);
8360 *count = 2;
8362 result = fill_monitor_2(info, &info[0].info2,
8363 SPL_LOCAL_PORT,
8364 "Windows NT X86", /* FIXME */
8365 "localmon.dll");
8366 if (!W_ERROR_IS_OK(result)) {
8367 goto out;
8370 result = fill_monitor_2(info, &info[1].info2,
8371 SPL_TCPIP_PORT,
8372 "Windows NT X86", /* FIXME */
8373 "tcpmon.dll");
8374 if (!W_ERROR_IS_OK(result)) {
8375 goto out;
8378 out:
8379 if (!W_ERROR_IS_OK(result)) {
8380 TALLOC_FREE(info);
8381 *count = 0;
8382 return result;
8385 *info_p = info;
8387 return WERR_OK;
8390 /****************************************************************
8391 _spoolss_EnumMonitors
8392 ****************************************************************/
8394 WERROR _spoolss_EnumMonitors(pipes_struct *p,
8395 struct spoolss_EnumMonitors *r)
8397 WERROR result;
8399 /* that's an [in out] buffer */
8401 if (!r->in.buffer && (r->in.offered != 0)) {
8402 return WERR_INVALID_PARAM;
8405 DEBUG(5,("_spoolss_EnumMonitors\n"));
8408 * Enumerate the print monitors ...
8410 * Just reply with "Local Port", to keep NT happy
8411 * and I can use my nice printer checker.
8414 *r->out.count = 0;
8415 *r->out.needed = 0;
8416 *r->out.info = NULL;
8418 switch (r->in.level) {
8419 case 1:
8420 result = enumprintmonitors_level_1(p->mem_ctx, r->out.info,
8421 r->out.count);
8422 break;
8423 case 2:
8424 result = enumprintmonitors_level_2(p->mem_ctx, r->out.info,
8425 r->out.count);
8426 break;
8427 default:
8428 return WERR_UNKNOWN_LEVEL;
8431 if (!W_ERROR_IS_OK(result)) {
8432 return result;
8435 *r->out.needed = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
8436 spoolss_EnumMonitors, NULL,
8437 *r->out.info, r->in.level,
8438 *r->out.count);
8439 *r->out.info = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
8440 *r->out.count = SPOOLSS_BUFFER_OK(*r->out.count, 0);
8442 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
8445 /****************************************************************************
8446 ****************************************************************************/
8448 static WERROR getjob_level_1(TALLOC_CTX *mem_ctx,
8449 const print_queue_struct *queue,
8450 int count, int snum,
8451 const NT_PRINTER_INFO_LEVEL *ntprinter,
8452 uint32_t jobid,
8453 struct spoolss_JobInfo1 *r)
8455 int i = 0;
8456 bool found = false;
8458 for (i=0; i<count && found == false; i++) {
8459 if (queue[i].job == (int)jobid) {
8460 found = true;
8464 if (found == false) {
8465 /* NT treats not found as bad param... yet another bad choice */
8466 return WERR_INVALID_PARAM;
8469 return fill_job_info1(mem_ctx,
8471 &queue[i-1],
8473 snum,
8474 ntprinter);
8477 /****************************************************************************
8478 ****************************************************************************/
8480 static WERROR getjob_level_2(TALLOC_CTX *mem_ctx,
8481 const print_queue_struct *queue,
8482 int count, int snum,
8483 const NT_PRINTER_INFO_LEVEL *ntprinter,
8484 uint32_t jobid,
8485 struct spoolss_JobInfo2 *r)
8487 int i = 0;
8488 bool found = false;
8489 struct spoolss_DeviceMode *devmode;
8490 NT_DEVICEMODE *nt_devmode;
8491 WERROR result;
8493 for (i=0; i<count && found == false; i++) {
8494 if (queue[i].job == (int)jobid) {
8495 found = true;
8499 if (found == false) {
8500 /* NT treats not found as bad param... yet another bad
8501 choice */
8502 return WERR_INVALID_PARAM;
8506 * if the print job does not have a DEVMODE associated with it,
8507 * just use the one for the printer. A NULL devicemode is not
8508 * a failure condition
8511 nt_devmode = print_job_devmode(lp_const_servicename(snum), jobid);
8512 if (nt_devmode) {
8513 devmode = TALLOC_ZERO_P(mem_ctx, struct spoolss_DeviceMode);
8514 W_ERROR_HAVE_NO_MEMORY(devmode);
8515 result = convert_nt_devicemode(devmode, devmode, nt_devmode);
8516 if (!W_ERROR_IS_OK(result)) {
8517 return result;
8519 } else {
8520 devmode = construct_dev_mode(mem_ctx, lp_const_servicename(snum));
8521 W_ERROR_HAVE_NO_MEMORY(devmode);
8524 return fill_job_info2(mem_ctx,
8526 &queue[i-1],
8528 snum,
8529 ntprinter,
8530 devmode);
8533 /****************************************************************
8534 _spoolss_GetJob
8535 ****************************************************************/
8537 WERROR _spoolss_GetJob(pipes_struct *p,
8538 struct spoolss_GetJob *r)
8540 WERROR result = WERR_OK;
8541 NT_PRINTER_INFO_LEVEL *ntprinter = NULL;
8542 int snum;
8543 int count;
8544 print_queue_struct *queue = NULL;
8545 print_status_struct prt_status;
8547 /* that's an [in out] buffer */
8549 if (!r->in.buffer && (r->in.offered != 0)) {
8550 return WERR_INVALID_PARAM;
8553 DEBUG(5,("_spoolss_GetJob\n"));
8555 *r->out.needed = 0;
8557 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
8558 return WERR_BADFID;
8561 result = get_a_printer(NULL, &ntprinter, 2, lp_servicename(snum));
8562 if (!W_ERROR_IS_OK(result)) {
8563 return result;
8566 count = print_queue_status(snum, &queue, &prt_status);
8568 DEBUGADD(4,("count:[%d], prt_status:[%d], [%s]\n",
8569 count, prt_status.status, prt_status.message));
8571 switch (r->in.level) {
8572 case 1:
8573 result = getjob_level_1(p->mem_ctx,
8574 queue, count, snum, ntprinter,
8575 r->in.job_id, &r->out.info->info1);
8576 break;
8577 case 2:
8578 result = getjob_level_2(p->mem_ctx,
8579 queue, count, snum, ntprinter,
8580 r->in.job_id, &r->out.info->info2);
8581 break;
8582 default:
8583 result = WERR_UNKNOWN_LEVEL;
8584 break;
8587 SAFE_FREE(queue);
8588 free_a_printer(&ntprinter, 2);
8590 if (!W_ERROR_IS_OK(result)) {
8591 TALLOC_FREE(r->out.info);
8592 return result;
8595 *r->out.needed = SPOOLSS_BUFFER_UNION(spoolss_JobInfo, NULL,
8596 r->out.info, r->in.level);
8597 r->out.info = SPOOLSS_BUFFER_OK(r->out.info, NULL);
8599 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
8602 /****************************************************************
8603 _spoolss_GetPrinterDataEx
8604 ****************************************************************/
8606 WERROR _spoolss_GetPrinterDataEx(pipes_struct *p,
8607 struct spoolss_GetPrinterDataEx *r)
8610 Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
8611 struct regval_blob *val = NULL;
8612 NT_PRINTER_INFO_LEVEL *printer = NULL;
8613 int snum = 0;
8614 WERROR result = WERR_OK;
8615 DATA_BLOB blob;
8617 DEBUG(4,("_spoolss_GetPrinterDataEx\n"));
8619 DEBUG(10, ("_spoolss_GetPrinterDataEx: key => [%s], value => [%s]\n",
8620 r->in.key_name, r->in.value_name));
8622 /* in case of problem, return some default values */
8624 *r->out.needed = 0;
8625 *r->out.type = REG_NONE;
8627 if (!Printer) {
8628 DEBUG(2,("_spoolss_GetPrinterDataEx: Invalid handle (%s:%u:%u).\n",
8629 OUR_HANDLE(r->in.handle)));
8630 result = WERR_BADFID;
8631 goto done;
8634 /* Is the handle to a printer or to the server? */
8636 if (Printer->printer_type == SPLHND_SERVER) {
8638 result = getprinterdata_printer_server(p->mem_ctx,
8639 r->in.value_name,
8640 r->out.type,
8641 r->out.data);
8642 goto done;
8645 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
8646 result = WERR_BADFID;
8647 goto done;
8650 result = get_a_printer(Printer, &printer, 2, lp_servicename(snum));
8651 if (!W_ERROR_IS_OK(result)) {
8652 goto done;
8655 /* check to see if the keyname is valid */
8656 if (!strlen(r->in.key_name)) {
8657 result = WERR_INVALID_PARAM;
8658 goto done;
8661 /* XP sends this and wants to change id value from the PRINTER_INFO_0 */
8663 if (strequal(r->in.key_name, SPOOL_PRINTERDATA_KEY) &&
8664 strequal(r->in.value_name, "ChangeId")) {
8665 *r->out.type = REG_DWORD;
8666 *r->out.needed = 4;
8667 r->out.data->value = printer->info_2->changeid;
8668 result = WERR_OK;
8669 goto done;
8672 if (lookup_printerkey(printer->info_2->data, r->in.key_name) == -1) {
8673 DEBUG(4,("_spoolss_GetPrinterDataEx: "
8674 "Invalid keyname [%s]\n", r->in.key_name ));
8675 result = WERR_BADFILE;
8676 goto done;
8679 val = get_printer_data(printer->info_2,
8680 r->in.key_name, r->in.value_name);
8681 if (!val) {
8682 result = WERR_BADFILE;
8683 goto done;
8686 *r->out.needed = regval_size(val);
8687 *r->out.type = regval_type(val);
8689 blob = data_blob_const(regval_data_p(val), regval_size(val));
8691 result = pull_spoolss_PrinterData(p->mem_ctx, &blob,
8692 r->out.data,
8693 *r->out.type);
8695 done:
8696 if (printer) {
8697 free_a_printer(&printer, 2);
8700 if (!W_ERROR_IS_OK(result)) {
8701 return result;
8704 *r->out.needed = ndr_size_spoolss_PrinterData(r->out.data, *r->out.type, NULL, 0);
8705 *r->out.type = SPOOLSS_BUFFER_OK(*r->out.type, REG_NONE);
8706 r->out.data = SPOOLSS_BUFFER_OK(r->out.data, r->out.data);
8708 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_MORE_DATA);
8711 /****************************************************************
8712 _spoolss_SetPrinterDataEx
8713 ****************************************************************/
8715 WERROR _spoolss_SetPrinterDataEx(pipes_struct *p,
8716 struct spoolss_SetPrinterDataEx *r)
8718 NT_PRINTER_INFO_LEVEL *printer = NULL;
8719 int snum = 0;
8720 WERROR result = WERR_OK;
8721 Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
8722 char *oid_string;
8723 DATA_BLOB blob;
8725 DEBUG(4,("_spoolss_SetPrinterDataEx\n"));
8727 /* From MSDN documentation of SetPrinterDataEx: pass request to
8728 SetPrinterData if key is "PrinterDriverData" */
8730 if (!Printer) {
8731 DEBUG(2,("_spoolss_SetPrinterDataEx: Invalid handle (%s:%u:%u).\n",
8732 OUR_HANDLE(r->in.handle)));
8733 return WERR_BADFID;
8736 if (Printer->printer_type == SPLHND_SERVER) {
8737 DEBUG(10,("_spoolss_SetPrinterDataEx: "
8738 "Not implemented for server handles yet\n"));
8739 return WERR_INVALID_PARAM;
8742 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
8743 return WERR_BADFID;
8747 * Access check : NT returns "access denied" if you make a
8748 * SetPrinterData call without the necessary privildge.
8749 * we were originally returning OK if nothing changed
8750 * which made Win2k issue **a lot** of SetPrinterData
8751 * when connecting to a printer --jerry
8754 if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
8755 DEBUG(3, ("_spoolss_SetPrinterDataEx: "
8756 "change denied by handle access permissions\n"));
8757 return WERR_ACCESS_DENIED;
8760 result = get_a_printer(Printer, &printer, 2, lp_servicename(snum));
8761 if (!W_ERROR_IS_OK(result)) {
8762 return result;
8765 /* check for OID in valuename */
8767 oid_string = strchr(r->in.value_name, ',');
8768 if (oid_string) {
8769 *oid_string = '\0';
8770 oid_string++;
8773 result = push_spoolss_PrinterData(p->mem_ctx, &blob,
8774 r->in.type, &r->in.data);
8775 if (!W_ERROR_IS_OK(result)) {
8776 goto done;
8780 * When client side code sets a magic printer data key, detect it and save
8781 * the current printer data and the magic key's data (its the DEVMODE) for
8782 * future printer/driver initializations.
8784 if ((r->in.type == REG_BINARY) && strequal(r->in.value_name, PHANTOM_DEVMODE_KEY)) {
8785 /* Set devmode and printer initialization info */
8786 result = save_driver_init(printer, 2, blob.data, blob.length);
8788 srv_spoolss_reset_printerdata(printer->info_2->drivername);
8790 goto done;
8793 /* save the registry data */
8795 result = set_printer_dataex(printer, r->in.key_name, r->in.value_name,
8796 r->in.type, blob.data, blob.length);
8798 if (W_ERROR_IS_OK(result)) {
8799 /* save the OID if one was specified */
8800 if (oid_string) {
8801 char *str = talloc_asprintf(p->mem_ctx, "%s\\%s",
8802 r->in.key_name, SPOOL_OID_KEY);
8803 if (!str) {
8804 result = WERR_NOMEM;
8805 goto done;
8809 * I'm not checking the status here on purpose. Don't know
8810 * if this is right, but I'm returning the status from the
8811 * previous set_printer_dataex() call. I have no idea if
8812 * this is right. --jerry
8815 set_printer_dataex(printer, str, r->in.value_name,
8816 REG_SZ, (uint8_t *)oid_string,
8817 strlen(oid_string)+1);
8820 result = mod_a_printer(printer, 2);
8823 done:
8824 free_a_printer(&printer, 2);
8826 return result;
8829 /****************************************************************
8830 _spoolss_DeletePrinterDataEx
8831 ****************************************************************/
8833 WERROR _spoolss_DeletePrinterDataEx(pipes_struct *p,
8834 struct spoolss_DeletePrinterDataEx *r)
8836 NT_PRINTER_INFO_LEVEL *printer = NULL;
8837 int snum=0;
8838 WERROR status = WERR_OK;
8839 Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
8841 DEBUG(5,("_spoolss_DeletePrinterDataEx\n"));
8843 if (!Printer) {
8844 DEBUG(2,("_spoolss_DeletePrinterDataEx: "
8845 "Invalid handle (%s:%u:%u).\n",
8846 OUR_HANDLE(r->in.handle)));
8847 return WERR_BADFID;
8850 if (!get_printer_snum(p, r->in.handle, &snum, NULL))
8851 return WERR_BADFID;
8853 if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
8854 DEBUG(3, ("_spoolss_DeletePrinterDataEx: "
8855 "printer properties change denied by handle\n"));
8856 return WERR_ACCESS_DENIED;
8859 if (!r->in.value_name || !r->in.key_name) {
8860 return WERR_NOMEM;
8863 status = get_a_printer(Printer, &printer, 2, lp_const_servicename(snum));
8864 if (!W_ERROR_IS_OK(status))
8865 return status;
8867 status = delete_printer_dataex( printer, r->in.key_name, r->in.value_name );
8869 if ( W_ERROR_IS_OK(status) )
8870 mod_a_printer( printer, 2 );
8872 free_a_printer(&printer, 2);
8874 return status;
8877 /****************************************************************
8878 _spoolss_EnumPrinterKey
8879 ****************************************************************/
8881 WERROR _spoolss_EnumPrinterKey(pipes_struct *p,
8882 struct spoolss_EnumPrinterKey *r)
8884 fstring *keynames = NULL;
8885 int num_keys;
8886 Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
8887 NT_PRINTER_DATA *data;
8888 NT_PRINTER_INFO_LEVEL *printer = NULL;
8889 int snum = 0;
8890 WERROR result = WERR_BADFILE;
8891 int i;
8892 const char **array = NULL;
8893 DATA_BLOB blob;
8895 DEBUG(4,("_spoolss_EnumPrinterKey\n"));
8897 if (!Printer) {
8898 DEBUG(2,("_spoolss_EnumPrinterKey: Invalid handle (%s:%u:%u).\n",
8899 OUR_HANDLE(r->in.handle)));
8900 return WERR_BADFID;
8903 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
8904 return WERR_BADFID;
8907 result = get_a_printer(Printer, &printer, 2, lp_const_servicename(snum));
8908 if (!W_ERROR_IS_OK(result)) {
8909 return result;
8912 /* get the list of subkey names */
8914 data = printer->info_2->data;
8916 num_keys = get_printer_subkeys(data, r->in.key_name, &keynames);
8917 if (num_keys == -1) {
8918 result = WERR_BADFILE;
8919 goto done;
8922 array = talloc_zero_array(r->out.key_buffer, const char *, num_keys + 1);
8923 if (!array) {
8924 result = WERR_NOMEM;
8925 goto done;
8928 for (i=0; i < num_keys; i++) {
8930 DEBUG(10,("_spoolss_EnumPrinterKey: adding keyname: %s\n",
8931 keynames[i]));
8933 array[i] = talloc_strdup(array, keynames[i]);
8934 if (!array[i]) {
8935 result = WERR_NOMEM;
8936 goto done;
8940 if (!push_reg_multi_sz(p->mem_ctx, &blob, array)) {
8941 result = WERR_NOMEM;
8942 goto done;
8945 *r->out.needed = blob.length;
8947 if (r->in.offered < *r->out.needed) {
8948 result = WERR_MORE_DATA;
8949 } else {
8950 result = WERR_OK;
8951 r->out.key_buffer->string = array;
8954 done:
8955 if (!W_ERROR_IS_OK(result)) {
8956 TALLOC_FREE(array);
8957 if (!W_ERROR_EQUAL(result, WERR_MORE_DATA)) {
8958 *r->out.needed = 0;
8962 free_a_printer(&printer, 2);
8963 SAFE_FREE(keynames);
8965 return result;
8968 /****************************************************************
8969 _spoolss_DeletePrinterKey
8970 ****************************************************************/
8972 WERROR _spoolss_DeletePrinterKey(pipes_struct *p,
8973 struct spoolss_DeletePrinterKey *r)
8975 Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
8976 NT_PRINTER_INFO_LEVEL *printer = NULL;
8977 int snum=0;
8978 WERROR status;
8980 DEBUG(5,("_spoolss_DeletePrinterKey\n"));
8982 if (!Printer) {
8983 DEBUG(2,("_spoolss_DeletePrinterKey: Invalid handle (%s:%u:%u).\n",
8984 OUR_HANDLE(r->in.handle)));
8985 return WERR_BADFID;
8988 /* if keyname == NULL, return error */
8990 if ( !r->in.key_name )
8991 return WERR_INVALID_PARAM;
8993 if (!get_printer_snum(p, r->in.handle, &snum, NULL))
8994 return WERR_BADFID;
8996 if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
8997 DEBUG(3, ("_spoolss_DeletePrinterKey: "
8998 "printer properties change denied by handle\n"));
8999 return WERR_ACCESS_DENIED;
9002 status = get_a_printer(Printer, &printer, 2, lp_const_servicename(snum));
9003 if (!W_ERROR_IS_OK(status))
9004 return status;
9006 /* delete the key and all subneys */
9008 status = delete_all_printer_data( printer->info_2, r->in.key_name );
9010 if ( W_ERROR_IS_OK(status) )
9011 status = mod_a_printer(printer, 2);
9013 free_a_printer( &printer, 2 );
9015 return status;
9018 /****************************************************************
9019 ****************************************************************/
9021 static WERROR registry_value_to_printer_enum_value(TALLOC_CTX *mem_ctx,
9022 struct regval_blob *v,
9023 struct spoolss_PrinterEnumValues *r)
9025 WERROR result;
9027 r->data = TALLOC_ZERO_P(mem_ctx, union spoolss_PrinterData);
9028 W_ERROR_HAVE_NO_MEMORY(r->data);
9030 r->value_name = talloc_strdup(mem_ctx, regval_name(v));
9031 W_ERROR_HAVE_NO_MEMORY(r->value_name);
9033 r->type = regval_type(v);
9034 r->data_length = regval_size(v);
9036 if (r->data_length) {
9037 DATA_BLOB blob = data_blob_const(regval_data_p(v),
9038 regval_size(v));
9039 result = pull_spoolss_PrinterData(mem_ctx, &blob,
9040 r->data,
9041 r->type);
9042 if (!W_ERROR_IS_OK(result)) {
9043 return result;
9047 return WERR_OK;
9050 /****************************************************************
9051 _spoolss_EnumPrinterDataEx
9052 ****************************************************************/
9054 WERROR _spoolss_EnumPrinterDataEx(pipes_struct *p,
9055 struct spoolss_EnumPrinterDataEx *r)
9057 uint32_t count = 0;
9058 NT_PRINTER_INFO_LEVEL *printer = NULL;
9059 struct spoolss_PrinterEnumValues *info = NULL;
9060 NT_PRINTER_DATA *p_data;
9061 Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
9062 int snum;
9063 WERROR result;
9064 int key_index;
9065 int i;
9067 DEBUG(4,("_spoolss_EnumPrinterDataEx\n"));
9069 *r->out.count = 0;
9070 *r->out.needed = 0;
9071 *r->out.info = NULL;
9073 if (!Printer) {
9074 DEBUG(2,("_spoolss_EnumPrinterDataEx: Invalid handle (%s:%u:%u1<).\n",
9075 OUR_HANDLE(r->in.handle)));
9076 return WERR_BADFID;
9080 * first check for a keyname of NULL or "". Win2k seems to send
9081 * this a lot and we should send back WERR_INVALID_PARAM
9082 * no need to spend time looking up the printer in this case.
9083 * --jerry
9086 if (!strlen(r->in.key_name)) {
9087 result = WERR_INVALID_PARAM;
9088 goto done;
9091 /* get the printer off of disk */
9093 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
9094 return WERR_BADFID;
9097 ZERO_STRUCT(printer);
9098 result = get_a_printer(Printer, &printer, 2, lp_const_servicename(snum));
9099 if (!W_ERROR_IS_OK(result)) {
9100 return result;
9103 /* now look for a match on the key name */
9105 p_data = printer->info_2->data;
9107 key_index = lookup_printerkey(p_data, r->in.key_name);
9108 if (key_index == -1) {
9109 DEBUG(10,("_spoolss_EnumPrinterDataEx: Unknown keyname [%s]\n",
9110 r->in.key_name));
9111 result = WERR_INVALID_PARAM;
9112 goto done;
9115 /* allocate the memory for the array of pointers -- if necessary */
9117 count = regval_ctr_numvals(p_data->keys[key_index].values);
9118 if (!count) {
9119 result = WERR_OK; /* ??? */
9120 goto done;
9123 info = TALLOC_ZERO_ARRAY(p->mem_ctx,
9124 struct spoolss_PrinterEnumValues,
9125 count);
9126 if (!info) {
9127 DEBUG(0,("_spoolss_EnumPrinterDataEx: talloc() failed\n"));
9128 result = WERR_NOMEM;
9129 goto done;
9133 * loop through all params and build the array to pass
9134 * back to the client
9137 for (i=0; i < count; i++) {
9139 struct regval_blob *val;
9141 /* lookup the registry value */
9143 val = regval_ctr_specific_value(p_data->keys[key_index].values, i);
9145 DEBUG(10,("retrieved value number [%d] [%s]\n", i, regval_name(val)));
9147 /* copy the data */
9149 result = registry_value_to_printer_enum_value(info, val, &info[i]);
9150 if (!W_ERROR_IS_OK(result)) {
9151 goto done;
9155 #if 0 /* FIXME - gd */
9156 /* housekeeping information in the reply */
9158 /* Fix from Martin Zielinski <mz@seh.de> - ensure
9159 * the hand marshalled container size is a multiple
9160 * of 4 bytes for RPC alignment.
9163 if (needed % 4) {
9164 needed += 4-(needed % 4);
9166 #endif
9167 *r->out.count = count;
9168 *r->out.info = info;
9170 done:
9172 if (printer) {
9173 free_a_printer(&printer, 2);
9176 if (!W_ERROR_IS_OK(result)) {
9177 return result;
9180 *r->out.needed = SPOOLSS_BUFFER_ARRAY(p->mem_ctx,
9181 spoolss_EnumPrinterDataEx, NULL,
9182 *r->out.info,
9183 *r->out.count);
9184 *r->out.info = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
9185 *r->out.count = SPOOLSS_BUFFER_OK(*r->out.count, *r->out.count);
9187 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_MORE_DATA);
9190 /****************************************************************************
9191 ****************************************************************************/
9193 static WERROR getprintprocessordirectory_level_1(TALLOC_CTX *mem_ctx,
9194 const char *servername,
9195 const char *environment,
9196 struct spoolss_PrintProcessorDirectoryInfo1 *r)
9198 WERROR werr;
9199 char *path = NULL;
9201 werr = compose_spoolss_server_path(mem_ctx,
9202 servername,
9203 environment,
9204 SPOOLSS_PRTPROCS_PATH,
9205 &path);
9206 if (!W_ERROR_IS_OK(werr)) {
9207 return werr;
9210 DEBUG(4,("print processor directory: [%s]\n", path));
9212 r->directory_name = path;
9214 return WERR_OK;
9217 /****************************************************************
9218 _spoolss_GetPrintProcessorDirectory
9219 ****************************************************************/
9221 WERROR _spoolss_GetPrintProcessorDirectory(pipes_struct *p,
9222 struct spoolss_GetPrintProcessorDirectory *r)
9224 WERROR result;
9226 /* that's an [in out] buffer */
9228 if (!r->in.buffer && (r->in.offered != 0)) {
9229 return WERR_INVALID_PARAM;
9232 DEBUG(5,("_spoolss_GetPrintProcessorDirectory: level %d\n",
9233 r->in.level));
9235 *r->out.needed = 0;
9237 /* r->in.level is ignored */
9239 /* We always should reply with a local print processor directory so that
9240 * users are not forced to have a [prnproc$] share on the Samba spoolss
9241 * server - Guenther */
9243 result = getprintprocessordirectory_level_1(p->mem_ctx,
9244 NULL, /* r->in.server */
9245 r->in.environment,
9246 &r->out.info->info1);
9247 if (!W_ERROR_IS_OK(result)) {
9248 TALLOC_FREE(r->out.info);
9249 return result;
9252 *r->out.needed = SPOOLSS_BUFFER_UNION(spoolss_PrintProcessorDirectoryInfo, NULL,
9253 r->out.info, r->in.level);
9254 r->out.info = SPOOLSS_BUFFER_OK(r->out.info, NULL);
9256 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
9259 /*******************************************************************
9260 ********************************************************************/
9262 static bool push_monitorui_buf(TALLOC_CTX *mem_ctx, DATA_BLOB *buf,
9263 const char *dllname)
9265 enum ndr_err_code ndr_err;
9266 struct spoolss_MonitorUi ui;
9268 ui.dll_name = dllname;
9270 ndr_err = ndr_push_struct_blob(buf, mem_ctx, NULL, &ui,
9271 (ndr_push_flags_fn_t)ndr_push_spoolss_MonitorUi);
9272 if (NDR_ERR_CODE_IS_SUCCESS(ndr_err) && (DEBUGLEVEL >= 10)) {
9273 NDR_PRINT_DEBUG(spoolss_MonitorUi, &ui);
9275 return NDR_ERR_CODE_IS_SUCCESS(ndr_err);
9278 /*******************************************************************
9279 Streams the monitor UI DLL name in UNICODE
9280 *******************************************************************/
9282 static WERROR xcvtcp_monitorui(TALLOC_CTX *mem_ctx,
9283 NT_USER_TOKEN *token, DATA_BLOB *in,
9284 DATA_BLOB *out, uint32_t *needed)
9286 const char *dllname = "tcpmonui.dll";
9288 *needed = (strlen(dllname)+1) * 2;
9290 if (out->length < *needed) {
9291 return WERR_INSUFFICIENT_BUFFER;
9294 if (!push_monitorui_buf(mem_ctx, out, dllname)) {
9295 return WERR_NOMEM;
9298 return WERR_OK;
9301 /*******************************************************************
9302 ********************************************************************/
9304 static bool pull_port_data_1(TALLOC_CTX *mem_ctx,
9305 struct spoolss_PortData1 *port1,
9306 const DATA_BLOB *buf)
9308 enum ndr_err_code ndr_err;
9309 ndr_err = ndr_pull_struct_blob(buf, mem_ctx, NULL, port1,
9310 (ndr_pull_flags_fn_t)ndr_pull_spoolss_PortData1);
9311 if (NDR_ERR_CODE_IS_SUCCESS(ndr_err) && (DEBUGLEVEL >= 10)) {
9312 NDR_PRINT_DEBUG(spoolss_PortData1, port1);
9314 return NDR_ERR_CODE_IS_SUCCESS(ndr_err);
9317 /*******************************************************************
9318 ********************************************************************/
9320 static bool pull_port_data_2(TALLOC_CTX *mem_ctx,
9321 struct spoolss_PortData2 *port2,
9322 const DATA_BLOB *buf)
9324 enum ndr_err_code ndr_err;
9325 ndr_err = ndr_pull_struct_blob(buf, mem_ctx, NULL, port2,
9326 (ndr_pull_flags_fn_t)ndr_pull_spoolss_PortData2);
9327 if (NDR_ERR_CODE_IS_SUCCESS(ndr_err) && (DEBUGLEVEL >= 10)) {
9328 NDR_PRINT_DEBUG(spoolss_PortData2, port2);
9330 return NDR_ERR_CODE_IS_SUCCESS(ndr_err);
9333 /*******************************************************************
9334 Create a new TCP/IP port
9335 *******************************************************************/
9337 static WERROR xcvtcp_addport(TALLOC_CTX *mem_ctx,
9338 NT_USER_TOKEN *token, DATA_BLOB *in,
9339 DATA_BLOB *out, uint32_t *needed)
9341 struct spoolss_PortData1 port1;
9342 struct spoolss_PortData2 port2;
9343 char *device_uri = NULL;
9344 uint32_t version;
9346 const char *portname;
9347 const char *hostaddress;
9348 const char *queue;
9349 uint32_t port_number;
9350 uint32_t protocol;
9352 /* peek for spoolss_PortData version */
9354 if (!in || (in->length < (128 + 4))) {
9355 return WERR_GENERAL_FAILURE;
9358 version = IVAL(in->data, 128);
9360 switch (version) {
9361 case 1:
9362 ZERO_STRUCT(port1);
9364 if (!pull_port_data_1(mem_ctx, &port1, in)) {
9365 return WERR_NOMEM;
9368 portname = port1.portname;
9369 hostaddress = port1.hostaddress;
9370 queue = port1.queue;
9371 protocol = port1.protocol;
9372 port_number = port1.port_number;
9374 break;
9375 case 2:
9376 ZERO_STRUCT(port2);
9378 if (!pull_port_data_2(mem_ctx, &port2, in)) {
9379 return WERR_NOMEM;
9382 portname = port2.portname;
9383 hostaddress = port2.hostaddress;
9384 queue = port2.queue;
9385 protocol = port2.protocol;
9386 port_number = port2.port_number;
9388 break;
9389 default:
9390 DEBUG(1,("xcvtcp_addport: "
9391 "unknown version of port_data: %d\n", version));
9392 return WERR_UNKNOWN_PORT;
9395 /* create the device URI and call the add_port_hook() */
9397 switch (protocol) {
9398 case PROTOCOL_RAWTCP_TYPE:
9399 device_uri = talloc_asprintf(mem_ctx,
9400 "socket://%s:%d/", hostaddress,
9401 port_number);
9402 break;
9404 case PROTOCOL_LPR_TYPE:
9405 device_uri = talloc_asprintf(mem_ctx,
9406 "lpr://%s/%s", hostaddress, queue );
9407 break;
9409 default:
9410 return WERR_UNKNOWN_PORT;
9413 if (!device_uri) {
9414 return WERR_NOMEM;
9417 return add_port_hook(mem_ctx, token, portname, device_uri);
9420 /*******************************************************************
9421 *******************************************************************/
9423 struct xcv_api_table xcvtcp_cmds[] = {
9424 { "MonitorUI", xcvtcp_monitorui },
9425 { "AddPort", xcvtcp_addport},
9426 { NULL, NULL }
9429 static WERROR process_xcvtcp_command(TALLOC_CTX *mem_ctx,
9430 NT_USER_TOKEN *token, const char *command,
9431 DATA_BLOB *inbuf,
9432 DATA_BLOB *outbuf,
9433 uint32_t *needed )
9435 int i;
9437 DEBUG(10,("process_xcvtcp_command: Received command \"%s\"\n", command));
9439 for ( i=0; xcvtcp_cmds[i].name; i++ ) {
9440 if ( strcmp( command, xcvtcp_cmds[i].name ) == 0 )
9441 return xcvtcp_cmds[i].fn(mem_ctx, token, inbuf, outbuf, needed);
9444 return WERR_BADFUNC;
9447 /*******************************************************************
9448 *******************************************************************/
9449 #if 0 /* don't support management using the "Local Port" monitor */
9451 static WERROR xcvlocal_monitorui(TALLOC_CTX *mem_ctx,
9452 NT_USER_TOKEN *token, DATA_BLOB *in,
9453 DATA_BLOB *out, uint32_t *needed)
9455 const char *dllname = "localui.dll";
9457 *needed = (strlen(dllname)+1) * 2;
9459 if (out->length < *needed) {
9460 return WERR_INSUFFICIENT_BUFFER;
9463 if (!push_monitorui_buf(mem_ctx, out, dllname)) {
9464 return WERR_NOMEM;
9467 return WERR_OK;
9470 /*******************************************************************
9471 *******************************************************************/
9473 struct xcv_api_table xcvlocal_cmds[] = {
9474 { "MonitorUI", xcvlocal_monitorui },
9475 { NULL, NULL }
9477 #else
9478 struct xcv_api_table xcvlocal_cmds[] = {
9479 { NULL, NULL }
9481 #endif
9485 /*******************************************************************
9486 *******************************************************************/
9488 static WERROR process_xcvlocal_command(TALLOC_CTX *mem_ctx,
9489 NT_USER_TOKEN *token, const char *command,
9490 DATA_BLOB *inbuf, DATA_BLOB *outbuf,
9491 uint32_t *needed)
9493 int i;
9495 DEBUG(10,("process_xcvlocal_command: Received command \"%s\"\n", command));
9497 for ( i=0; xcvlocal_cmds[i].name; i++ ) {
9498 if ( strcmp( command, xcvlocal_cmds[i].name ) == 0 )
9499 return xcvlocal_cmds[i].fn(mem_ctx, token, inbuf, outbuf, needed);
9501 return WERR_BADFUNC;
9504 /****************************************************************
9505 _spoolss_XcvData
9506 ****************************************************************/
9508 WERROR _spoolss_XcvData(pipes_struct *p,
9509 struct spoolss_XcvData *r)
9511 Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
9512 DATA_BLOB out_data = data_blob_null;
9513 WERROR werror;
9515 if (!Printer) {
9516 DEBUG(2,("_spoolss_XcvData: Invalid handle (%s:%u:%u).\n",
9517 OUR_HANDLE(r->in.handle)));
9518 return WERR_BADFID;
9521 /* Has to be a handle to the TCP/IP port monitor */
9523 if ( !(Printer->printer_type & (SPLHND_PORTMON_LOCAL|SPLHND_PORTMON_TCP)) ) {
9524 DEBUG(2,("_spoolss_XcvData: Call only valid for Port Monitors\n"));
9525 return WERR_BADFID;
9528 /* requires administrative access to the server */
9530 if ( !(Printer->access_granted & SERVER_ACCESS_ADMINISTER) ) {
9531 DEBUG(2,("_spoolss_XcvData: denied by handle permissions.\n"));
9532 return WERR_ACCESS_DENIED;
9535 /* Allocate the outgoing buffer */
9537 if (r->in.out_data_size) {
9538 out_data = data_blob_talloc_zero(p->mem_ctx, r->in.out_data_size);
9539 if (out_data.data == NULL) {
9540 return WERR_NOMEM;
9544 switch ( Printer->printer_type ) {
9545 case SPLHND_PORTMON_TCP:
9546 werror = process_xcvtcp_command(p->mem_ctx,
9547 p->server_info->ptok,
9548 r->in.function_name,
9549 &r->in.in_data, &out_data,
9550 r->out.needed);
9551 break;
9552 case SPLHND_PORTMON_LOCAL:
9553 werror = process_xcvlocal_command(p->mem_ctx,
9554 p->server_info->ptok,
9555 r->in.function_name,
9556 &r->in.in_data, &out_data,
9557 r->out.needed);
9558 break;
9559 default:
9560 werror = WERR_INVALID_PRINT_MONITOR;
9563 if (!W_ERROR_IS_OK(werror)) {
9564 return werror;
9567 *r->out.status_code = 0;
9569 memcpy(r->out.out_data, out_data.data, out_data.length);
9571 return WERR_OK;
9574 /****************************************************************
9575 _spoolss_AddPrintProcessor
9576 ****************************************************************/
9578 WERROR _spoolss_AddPrintProcessor(pipes_struct *p,
9579 struct spoolss_AddPrintProcessor *r)
9581 /* for now, just indicate success and ignore the add. We'll
9582 automatically set the winprint processor for printer
9583 entries later. Used to debug the LexMark Optra S 1855 PCL
9584 driver --jerry */
9586 return WERR_OK;
9589 /****************************************************************
9590 _spoolss_AddPort
9591 ****************************************************************/
9593 WERROR _spoolss_AddPort(pipes_struct *p,
9594 struct spoolss_AddPort *r)
9596 /* do what w2k3 does */
9598 return WERR_NOT_SUPPORTED;
9601 /****************************************************************
9602 _spoolss_GetPrinterDriver
9603 ****************************************************************/
9605 WERROR _spoolss_GetPrinterDriver(pipes_struct *p,
9606 struct spoolss_GetPrinterDriver *r)
9608 p->rng_fault_state = true;
9609 return WERR_NOT_SUPPORTED;
9612 /****************************************************************
9613 _spoolss_ReadPrinter
9614 ****************************************************************/
9616 WERROR _spoolss_ReadPrinter(pipes_struct *p,
9617 struct spoolss_ReadPrinter *r)
9619 p->rng_fault_state = true;
9620 return WERR_NOT_SUPPORTED;
9623 /****************************************************************
9624 _spoolss_WaitForPrinterChange
9625 ****************************************************************/
9627 WERROR _spoolss_WaitForPrinterChange(pipes_struct *p,
9628 struct spoolss_WaitForPrinterChange *r)
9630 p->rng_fault_state = true;
9631 return WERR_NOT_SUPPORTED;
9634 /****************************************************************
9635 _spoolss_ConfigurePort
9636 ****************************************************************/
9638 WERROR _spoolss_ConfigurePort(pipes_struct *p,
9639 struct spoolss_ConfigurePort *r)
9641 p->rng_fault_state = true;
9642 return WERR_NOT_SUPPORTED;
9645 /****************************************************************
9646 _spoolss_DeletePort
9647 ****************************************************************/
9649 WERROR _spoolss_DeletePort(pipes_struct *p,
9650 struct spoolss_DeletePort *r)
9652 p->rng_fault_state = true;
9653 return WERR_NOT_SUPPORTED;
9656 /****************************************************************
9657 _spoolss_CreatePrinterIC
9658 ****************************************************************/
9660 WERROR _spoolss_CreatePrinterIC(pipes_struct *p,
9661 struct spoolss_CreatePrinterIC *r)
9663 p->rng_fault_state = true;
9664 return WERR_NOT_SUPPORTED;
9667 /****************************************************************
9668 _spoolss_PlayGDIScriptOnPrinterIC
9669 ****************************************************************/
9671 WERROR _spoolss_PlayGDIScriptOnPrinterIC(pipes_struct *p,
9672 struct spoolss_PlayGDIScriptOnPrinterIC *r)
9674 p->rng_fault_state = true;
9675 return WERR_NOT_SUPPORTED;
9678 /****************************************************************
9679 _spoolss_DeletePrinterIC
9680 ****************************************************************/
9682 WERROR _spoolss_DeletePrinterIC(pipes_struct *p,
9683 struct spoolss_DeletePrinterIC *r)
9685 p->rng_fault_state = true;
9686 return WERR_NOT_SUPPORTED;
9689 /****************************************************************
9690 _spoolss_AddPrinterConnection
9691 ****************************************************************/
9693 WERROR _spoolss_AddPrinterConnection(pipes_struct *p,
9694 struct spoolss_AddPrinterConnection *r)
9696 p->rng_fault_state = true;
9697 return WERR_NOT_SUPPORTED;
9700 /****************************************************************
9701 _spoolss_DeletePrinterConnection
9702 ****************************************************************/
9704 WERROR _spoolss_DeletePrinterConnection(pipes_struct *p,
9705 struct spoolss_DeletePrinterConnection *r)
9707 p->rng_fault_state = true;
9708 return WERR_NOT_SUPPORTED;
9711 /****************************************************************
9712 _spoolss_PrinterMessageBox
9713 ****************************************************************/
9715 WERROR _spoolss_PrinterMessageBox(pipes_struct *p,
9716 struct spoolss_PrinterMessageBox *r)
9718 p->rng_fault_state = true;
9719 return WERR_NOT_SUPPORTED;
9722 /****************************************************************
9723 _spoolss_AddMonitor
9724 ****************************************************************/
9726 WERROR _spoolss_AddMonitor(pipes_struct *p,
9727 struct spoolss_AddMonitor *r)
9729 p->rng_fault_state = true;
9730 return WERR_NOT_SUPPORTED;
9733 /****************************************************************
9734 _spoolss_DeleteMonitor
9735 ****************************************************************/
9737 WERROR _spoolss_DeleteMonitor(pipes_struct *p,
9738 struct spoolss_DeleteMonitor *r)
9740 p->rng_fault_state = true;
9741 return WERR_NOT_SUPPORTED;
9744 /****************************************************************
9745 _spoolss_DeletePrintProcessor
9746 ****************************************************************/
9748 WERROR _spoolss_DeletePrintProcessor(pipes_struct *p,
9749 struct spoolss_DeletePrintProcessor *r)
9751 p->rng_fault_state = true;
9752 return WERR_NOT_SUPPORTED;
9755 /****************************************************************
9756 _spoolss_AddPrintProvidor
9757 ****************************************************************/
9759 WERROR _spoolss_AddPrintProvidor(pipes_struct *p,
9760 struct spoolss_AddPrintProvidor *r)
9762 p->rng_fault_state = true;
9763 return WERR_NOT_SUPPORTED;
9766 /****************************************************************
9767 _spoolss_DeletePrintProvidor
9768 ****************************************************************/
9770 WERROR _spoolss_DeletePrintProvidor(pipes_struct *p,
9771 struct spoolss_DeletePrintProvidor *r)
9773 p->rng_fault_state = true;
9774 return WERR_NOT_SUPPORTED;
9777 /****************************************************************
9778 _spoolss_FindFirstPrinterChangeNotification
9779 ****************************************************************/
9781 WERROR _spoolss_FindFirstPrinterChangeNotification(pipes_struct *p,
9782 struct spoolss_FindFirstPrinterChangeNotification *r)
9784 p->rng_fault_state = true;
9785 return WERR_NOT_SUPPORTED;
9788 /****************************************************************
9789 _spoolss_FindNextPrinterChangeNotification
9790 ****************************************************************/
9792 WERROR _spoolss_FindNextPrinterChangeNotification(pipes_struct *p,
9793 struct spoolss_FindNextPrinterChangeNotification *r)
9795 p->rng_fault_state = true;
9796 return WERR_NOT_SUPPORTED;
9799 /****************************************************************
9800 _spoolss_RouterFindFirstPrinterChangeNotificationOld
9801 ****************************************************************/
9803 WERROR _spoolss_RouterFindFirstPrinterChangeNotificationOld(pipes_struct *p,
9804 struct spoolss_RouterFindFirstPrinterChangeNotificationOld *r)
9806 p->rng_fault_state = true;
9807 return WERR_NOT_SUPPORTED;
9810 /****************************************************************
9811 _spoolss_ReplyOpenPrinter
9812 ****************************************************************/
9814 WERROR _spoolss_ReplyOpenPrinter(pipes_struct *p,
9815 struct spoolss_ReplyOpenPrinter *r)
9817 p->rng_fault_state = true;
9818 return WERR_NOT_SUPPORTED;
9821 /****************************************************************
9822 _spoolss_RouterReplyPrinter
9823 ****************************************************************/
9825 WERROR _spoolss_RouterReplyPrinter(pipes_struct *p,
9826 struct spoolss_RouterReplyPrinter *r)
9828 p->rng_fault_state = true;
9829 return WERR_NOT_SUPPORTED;
9832 /****************************************************************
9833 _spoolss_ReplyClosePrinter
9834 ****************************************************************/
9836 WERROR _spoolss_ReplyClosePrinter(pipes_struct *p,
9837 struct spoolss_ReplyClosePrinter *r)
9839 p->rng_fault_state = true;
9840 return WERR_NOT_SUPPORTED;
9843 /****************************************************************
9844 _spoolss_AddPortEx
9845 ****************************************************************/
9847 WERROR _spoolss_AddPortEx(pipes_struct *p,
9848 struct spoolss_AddPortEx *r)
9850 p->rng_fault_state = true;
9851 return WERR_NOT_SUPPORTED;
9854 /****************************************************************
9855 _spoolss_RouterFindFirstPrinterChangeNotification
9856 ****************************************************************/
9858 WERROR _spoolss_RouterFindFirstPrinterChangeNotification(pipes_struct *p,
9859 struct spoolss_RouterFindFirstPrinterChangeNotification *r)
9861 p->rng_fault_state = true;
9862 return WERR_NOT_SUPPORTED;
9865 /****************************************************************
9866 _spoolss_SpoolerInit
9867 ****************************************************************/
9869 WERROR _spoolss_SpoolerInit(pipes_struct *p,
9870 struct spoolss_SpoolerInit *r)
9872 p->rng_fault_state = true;
9873 return WERR_NOT_SUPPORTED;
9876 /****************************************************************
9877 _spoolss_ResetPrinterEx
9878 ****************************************************************/
9880 WERROR _spoolss_ResetPrinterEx(pipes_struct *p,
9881 struct spoolss_ResetPrinterEx *r)
9883 p->rng_fault_state = true;
9884 return WERR_NOT_SUPPORTED;
9887 /****************************************************************
9888 _spoolss_RouterReplyPrinterEx
9889 ****************************************************************/
9891 WERROR _spoolss_RouterReplyPrinterEx(pipes_struct *p,
9892 struct spoolss_RouterReplyPrinterEx *r)
9894 p->rng_fault_state = true;
9895 return WERR_NOT_SUPPORTED;
9898 /****************************************************************
9899 _spoolss_44
9900 ****************************************************************/
9902 WERROR _spoolss_44(pipes_struct *p,
9903 struct spoolss_44 *r)
9905 p->rng_fault_state = true;
9906 return WERR_NOT_SUPPORTED;
9909 /****************************************************************
9910 _spoolss_47
9911 ****************************************************************/
9913 WERROR _spoolss_47(pipes_struct *p,
9914 struct spoolss_47 *r)
9916 p->rng_fault_state = true;
9917 return WERR_NOT_SUPPORTED;
9920 /****************************************************************
9921 _spoolss_4a
9922 ****************************************************************/
9924 WERROR _spoolss_4a(pipes_struct *p,
9925 struct spoolss_4a *r)
9927 p->rng_fault_state = true;
9928 return WERR_NOT_SUPPORTED;
9931 /****************************************************************
9932 _spoolss_4b
9933 ****************************************************************/
9935 WERROR _spoolss_4b(pipes_struct *p,
9936 struct spoolss_4b *r)
9938 p->rng_fault_state = true;
9939 return WERR_NOT_SUPPORTED;
9942 /****************************************************************
9943 _spoolss_4c
9944 ****************************************************************/
9946 WERROR _spoolss_4c(pipes_struct *p,
9947 struct spoolss_4c *r)
9949 p->rng_fault_state = true;
9950 return WERR_NOT_SUPPORTED;
9953 /****************************************************************
9954 _spoolss_53
9955 ****************************************************************/
9957 WERROR _spoolss_53(pipes_struct *p,
9958 struct spoolss_53 *r)
9960 p->rng_fault_state = true;
9961 return WERR_NOT_SUPPORTED;
9964 /****************************************************************
9965 _spoolss_55
9966 ****************************************************************/
9968 WERROR _spoolss_55(pipes_struct *p,
9969 struct spoolss_55 *r)
9971 p->rng_fault_state = true;
9972 return WERR_NOT_SUPPORTED;
9975 /****************************************************************
9976 _spoolss_56
9977 ****************************************************************/
9979 WERROR _spoolss_56(pipes_struct *p,
9980 struct spoolss_56 *r)
9982 p->rng_fault_state = true;
9983 return WERR_NOT_SUPPORTED;
9986 /****************************************************************
9987 _spoolss_57
9988 ****************************************************************/
9990 WERROR _spoolss_57(pipes_struct *p,
9991 struct spoolss_57 *r)
9993 p->rng_fault_state = true;
9994 return WERR_NOT_SUPPORTED;
9997 /****************************************************************
9998 _spoolss_5a
9999 ****************************************************************/
10001 WERROR _spoolss_5a(pipes_struct *p,
10002 struct spoolss_5a *r)
10004 p->rng_fault_state = true;
10005 return WERR_NOT_SUPPORTED;
10008 /****************************************************************
10009 _spoolss_5b
10010 ****************************************************************/
10012 WERROR _spoolss_5b(pipes_struct *p,
10013 struct spoolss_5b *r)
10015 p->rng_fault_state = true;
10016 return WERR_NOT_SUPPORTED;
10019 /****************************************************************
10020 _spoolss_5c
10021 ****************************************************************/
10023 WERROR _spoolss_5c(pipes_struct *p,
10024 struct spoolss_5c *r)
10026 p->rng_fault_state = true;
10027 return WERR_NOT_SUPPORTED;
10030 /****************************************************************
10031 _spoolss_5d
10032 ****************************************************************/
10034 WERROR _spoolss_5d(pipes_struct *p,
10035 struct spoolss_5d *r)
10037 p->rng_fault_state = true;
10038 return WERR_NOT_SUPPORTED;
10041 /****************************************************************
10042 _spoolss_5e
10043 ****************************************************************/
10045 WERROR _spoolss_5e(pipes_struct *p,
10046 struct spoolss_5e *r)
10048 p->rng_fault_state = true;
10049 return WERR_NOT_SUPPORTED;
10052 /****************************************************************
10053 _spoolss_5f
10054 ****************************************************************/
10056 WERROR _spoolss_5f(pipes_struct *p,
10057 struct spoolss_5f *r)
10059 p->rng_fault_state = true;
10060 return WERR_NOT_SUPPORTED;
10063 /****************************************************************
10064 _spoolss_60
10065 ****************************************************************/
10067 WERROR _spoolss_60(pipes_struct *p,
10068 struct spoolss_60 *r)
10070 p->rng_fault_state = true;
10071 return WERR_NOT_SUPPORTED;
10074 /****************************************************************
10075 _spoolss_61
10076 ****************************************************************/
10078 WERROR _spoolss_61(pipes_struct *p,
10079 struct spoolss_61 *r)
10081 p->rng_fault_state = true;
10082 return WERR_NOT_SUPPORTED;
10085 /****************************************************************
10086 _spoolss_62
10087 ****************************************************************/
10089 WERROR _spoolss_62(pipes_struct *p,
10090 struct spoolss_62 *r)
10092 p->rng_fault_state = true;
10093 return WERR_NOT_SUPPORTED;
10096 /****************************************************************
10097 _spoolss_63
10098 ****************************************************************/
10100 WERROR _spoolss_63(pipes_struct *p,
10101 struct spoolss_63 *r)
10103 p->rng_fault_state = true;
10104 return WERR_NOT_SUPPORTED;
10107 /****************************************************************
10108 _spoolss_64
10109 ****************************************************************/
10111 WERROR _spoolss_64(pipes_struct *p,
10112 struct spoolss_64 *r)
10114 p->rng_fault_state = true;
10115 return WERR_NOT_SUPPORTED;
10118 /****************************************************************
10119 _spoolss_65
10120 ****************************************************************/
10122 WERROR _spoolss_65(pipes_struct *p,
10123 struct spoolss_65 *r)
10125 p->rng_fault_state = true;
10126 return WERR_NOT_SUPPORTED;
10129 /****************************************************************
10130 _spoolss_GetCorePrinterDrivers
10131 ****************************************************************/
10133 WERROR _spoolss_GetCorePrinterDrivers(pipes_struct *p,
10134 struct spoolss_GetCorePrinterDrivers *r)
10136 p->rng_fault_state = true;
10137 return WERR_NOT_SUPPORTED;
10140 /****************************************************************
10141 _spoolss_67
10142 ****************************************************************/
10144 WERROR _spoolss_67(pipes_struct *p,
10145 struct spoolss_67 *r)
10147 p->rng_fault_state = true;
10148 return WERR_NOT_SUPPORTED;
10151 /****************************************************************
10152 _spoolss_GetPrinterDriverPackagePath
10153 ****************************************************************/
10155 WERROR _spoolss_GetPrinterDriverPackagePath(pipes_struct *p,
10156 struct spoolss_GetPrinterDriverPackagePath *r)
10158 p->rng_fault_state = true;
10159 return WERR_NOT_SUPPORTED;
10162 /****************************************************************
10163 _spoolss_69
10164 ****************************************************************/
10166 WERROR _spoolss_69(pipes_struct *p,
10167 struct spoolss_69 *r)
10169 p->rng_fault_state = true;
10170 return WERR_NOT_SUPPORTED;
10173 /****************************************************************
10174 _spoolss_6a
10175 ****************************************************************/
10177 WERROR _spoolss_6a(pipes_struct *p,
10178 struct spoolss_6a *r)
10180 p->rng_fault_state = true;
10181 return WERR_NOT_SUPPORTED;
10184 /****************************************************************
10185 _spoolss_6b
10186 ****************************************************************/
10188 WERROR _spoolss_6b(pipes_struct *p,
10189 struct spoolss_6b *r)
10191 p->rng_fault_state = true;
10192 return WERR_NOT_SUPPORTED;
10195 /****************************************************************
10196 _spoolss_6c
10197 ****************************************************************/
10199 WERROR _spoolss_6c(pipes_struct *p,
10200 struct spoolss_6c *r)
10202 p->rng_fault_state = true;
10203 return WERR_NOT_SUPPORTED;
10206 /****************************************************************
10207 _spoolss_6d
10208 ****************************************************************/
10210 WERROR _spoolss_6d(pipes_struct *p,
10211 struct spoolss_6d *r)
10213 p->rng_fault_state = true;
10214 return WERR_NOT_SUPPORTED;