2 * Unix SMB/CIFS implementation.
3 * RPC Pipe client / server routines
4 * Copyright (C) Andrew Tridgell 1992-2000,
5 * Copyright (C) Luke Kenneth Casson Leighton 1996-2000,
6 * Copyright (C) Jean François Micouleau 1998-2000,
7 * Copyright (C) Jeremy Allison 2001-2002,
8 * Copyright (C) Gerald Carter 2000-2004,
9 * Copyright (C) Tim Potter 2001-2002.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, see <http://www.gnu.org/licenses/>.
25 /* Since the SPOOLSS rpc routines are basically DOS 16-bit calls wrapped
26 up, all the errors returned are DOS errors, not NT status codes. */
30 extern userdom_struct current_user_info;
33 #define DBGC_CLASS DBGC_RPC_SRV
35 #ifndef MAX_OPEN_PRINTER_EXS
36 #define MAX_OPEN_PRINTER_EXS 50
39 #define MAGIC_DISPLAY_FREQUENCY 0xfade2bad
40 #define PHANTOM_DEVMODE_KEY "_p_f_a_n_t_0_m_"
43 const char *long_archi;
44 const char *short_archi;
48 static Printer_entry *printers_list;
50 typedef struct _counter_printer_0 {
51 struct _counter_printer_0 *next;
52 struct _counter_printer_0 *prev;
58 static counter_printer_0 *counter_list;
60 static struct rpc_pipe_client *notify_cli_pipe; /* print notify back-channel pipe handle*/
61 static uint32 smb_connections=0;
64 /* in printing/nt_printing.c */
66 extern struct standard_mapping printer_std_mapping, printserver_std_mapping;
68 /* API table for Xcv Monitor functions */
70 struct xcv_api_table {
72 WERROR(*fn) (NT_USER_TOKEN *token, RPC_BUFFER *in, RPC_BUFFER *out, uint32 *needed);
75 /********************************************************************
76 * Canonicalize servername.
77 ********************************************************************/
79 static const char *canon_servername(const char *servername)
81 const char *pservername = servername;
82 while (*pservername == '\\') {
88 /* translate between internal status numbers and NT status numbers */
89 static int nt_printj_status(int v)
95 return JOB_STATUS_PAUSED;
97 return JOB_STATUS_SPOOLING;
99 return JOB_STATUS_PRINTING;
101 return JOB_STATUS_ERROR;
103 return JOB_STATUS_DELETING;
105 return JOB_STATUS_OFFLINE;
107 return JOB_STATUS_PAPEROUT;
109 return JOB_STATUS_PRINTED;
111 return JOB_STATUS_DELETED;
113 return JOB_STATUS_BLOCKED;
114 case LPQ_USER_INTERVENTION:
115 return JOB_STATUS_USER_INTERVENTION;
120 static int nt_printq_status(int v)
124 return PRINTER_STATUS_PAUSED;
133 /****************************************************************************
134 Functions to handle SPOOL_NOTIFY_OPTION struct stored in Printer_entry.
135 ****************************************************************************/
137 static void free_spool_notify_option(SPOOL_NOTIFY_OPTION **pp)
142 SAFE_FREE((*pp)->ctr.type);
146 /***************************************************************************
147 Disconnect from the client
148 ****************************************************************************/
150 static void srv_spoolss_replycloseprinter(int snum, POLICY_HND *handle)
155 * Tell the specific printing tdb we no longer want messages for this printer
156 * by deregistering our PID.
159 if (!print_notify_deregister_pid(snum))
160 DEBUG(0,("print_notify_register_pid: Failed to register our pid for printer %s\n", lp_const_servicename(snum) ));
162 /* weird if the test succeds !!! */
163 if (smb_connections==0) {
164 DEBUG(0,("srv_spoolss_replycloseprinter:Trying to close non-existant notify backchannel !\n"));
168 result = rpccli_spoolss_reply_close_printer(notify_cli_pipe,
172 if (!W_ERROR_IS_OK(result))
173 DEBUG(0,("srv_spoolss_replycloseprinter: reply_close_printer failed [%s].\n",
174 win_errstr(result)));
176 /* if it's the last connection, deconnect the IPC$ share */
177 if (smb_connections==1) {
179 cli_shutdown( rpc_pipe_np_smb_conn(notify_cli_pipe) );
180 notify_cli_pipe = NULL; /* The above call shuts downn the pipe also. */
182 messaging_deregister(smbd_messaging_context(),
183 MSG_PRINTER_NOTIFY2, NULL);
185 /* Tell the connections db we're no longer interested in
186 * printer notify messages. */
188 register_message_flags( False, FLAG_MSG_PRINT_NOTIFY );
194 /****************************************************************************
195 Functions to free a printer entry datastruct.
196 ****************************************************************************/
198 static int printer_entry_destructor(Printer_entry *Printer)
200 if (Printer->notify.client_connected==True) {
203 if ( Printer->printer_type == SPLHND_SERVER) {
205 srv_spoolss_replycloseprinter(snum, &Printer->notify.client_hnd);
206 } else if (Printer->printer_type == SPLHND_PRINTER) {
207 snum = print_queue_snum(Printer->sharename);
209 srv_spoolss_replycloseprinter(snum,
210 &Printer->notify.client_hnd);
214 Printer->notify.flags=0;
215 Printer->notify.options=0;
216 Printer->notify.localmachine[0]='\0';
217 Printer->notify.printerlocal=0;
218 free_spool_notify_option(&Printer->notify.option);
219 Printer->notify.option=NULL;
220 Printer->notify.client_connected=False;
222 free_nt_devicemode( &Printer->nt_devmode );
223 free_a_printer( &Printer->printer_info, 2 );
225 /* Remove from the internal list. */
226 DLIST_REMOVE(printers_list, Printer);
230 /****************************************************************************
231 Functions to duplicate a SPOOL_NOTIFY_OPTION struct stored in Printer_entry.
232 ****************************************************************************/
234 static SPOOL_NOTIFY_OPTION *dup_spool_notify_option(SPOOL_NOTIFY_OPTION *sp)
236 SPOOL_NOTIFY_OPTION *new_sp = NULL;
241 new_sp = SMB_MALLOC_P(SPOOL_NOTIFY_OPTION);
248 new_sp->ctr.type = (SPOOL_NOTIFY_OPTION_TYPE *)memdup(sp->ctr.type, sizeof(SPOOL_NOTIFY_OPTION_TYPE) * sp->ctr.count);
250 if (!new_sp->ctr.type) {
259 /****************************************************************************
260 find printer index by handle
261 ****************************************************************************/
263 static Printer_entry *find_printer_index_by_hnd(pipes_struct *p, POLICY_HND *hnd)
265 Printer_entry *find_printer = NULL;
267 if(!find_policy_by_hnd(p,hnd,(void **)(void *)&find_printer)) {
268 DEBUG(2,("find_printer_index_by_hnd: Printer handle not found: "));
275 /****************************************************************************
276 Close printer index by handle.
277 ****************************************************************************/
279 static bool close_printer_handle(pipes_struct *p, POLICY_HND *hnd)
281 Printer_entry *Printer = find_printer_index_by_hnd(p, hnd);
284 DEBUG(2,("close_printer_handle: Invalid handle (%s:%u:%u)\n", OUR_HANDLE(hnd)));
288 close_policy_hnd(p, hnd);
293 /****************************************************************************
294 Delete a printer given a handle.
295 ****************************************************************************/
297 WERROR delete_printer_hook(TALLOC_CTX *ctx, NT_USER_TOKEN *token, const char *sharename )
299 char *cmd = lp_deleteprinter_cmd();
300 char *command = NULL;
302 SE_PRIV se_printop = SE_PRINT_OPERATOR;
303 bool is_print_op = False;
305 /* can't fail if we don't try */
310 command = talloc_asprintf(ctx,
317 is_print_op = user_has_privileges( token, &se_printop );
319 DEBUG(10,("Running [%s]\n", command));
321 /********** BEGIN SePrintOperatorPrivlege BLOCK **********/
326 if ( (ret = smbrun(command, NULL)) == 0 ) {
327 /* Tell everyone we updated smb.conf. */
328 message_send_all(smbd_messaging_context(),
329 MSG_SMB_CONF_UPDATED, NULL, 0, NULL);
335 /********** END SePrintOperatorPrivlege BLOCK **********/
337 DEBUGADD(10,("returned [%d]\n", ret));
339 TALLOC_FREE(command);
342 return WERR_BADFID; /* What to return here? */
344 /* go ahead and re-read the services immediately */
345 reload_services( False );
347 if ( lp_servicenumber( sharename ) < 0 )
348 return WERR_ACCESS_DENIED;
353 /****************************************************************************
354 Delete a printer given a handle.
355 ****************************************************************************/
357 static WERROR delete_printer_handle(pipes_struct *p, POLICY_HND *hnd)
359 Printer_entry *Printer = find_printer_index_by_hnd(p, hnd);
362 DEBUG(2,("delete_printer_handle: Invalid handle (%s:%u:%u)\n", OUR_HANDLE(hnd)));
367 * It turns out that Windows allows delete printer on a handle
368 * opened by an admin user, then used on a pipe handle created
369 * by an anonymous user..... but they're working on security.... riiight !
373 if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
374 DEBUG(3, ("delete_printer_handle: denied by handle\n"));
375 return WERR_ACCESS_DENIED;
378 /* this does not need a become root since the access check has been
379 done on the handle already */
381 if (del_a_printer( Printer->sharename ) != 0) {
382 DEBUG(3,("Error deleting printer %s\n", Printer->sharename));
386 return delete_printer_hook(p->mem_ctx, p->server_info->ptok,
387 Printer->sharename );
390 /****************************************************************************
391 Return the snum of a printer corresponding to an handle.
392 ****************************************************************************/
394 static bool get_printer_snum(pipes_struct *p, POLICY_HND *hnd, int *number,
395 struct share_params **params)
397 Printer_entry *Printer = find_printer_index_by_hnd(p, hnd);
400 DEBUG(2,("get_printer_snum: Invalid handle (%s:%u:%u)\n", OUR_HANDLE(hnd)));
404 switch (Printer->printer_type) {
406 DEBUG(4,("short name:%s\n", Printer->sharename));
407 *number = print_queue_snum(Printer->sharename);
408 return (*number != -1);
416 /****************************************************************************
417 Set printer handle type.
418 Check if it's \\server or \\server\printer
419 ****************************************************************************/
421 static bool set_printer_hnd_printertype(Printer_entry *Printer, char *handlename)
423 DEBUG(3,("Setting printer type=%s\n", handlename));
425 if ( strlen(handlename) < 3 ) {
426 DEBUGADD(4,("A print server must have at least 1 char ! %s\n", handlename));
430 /* it's a print server */
431 if (*handlename=='\\' && *(handlename+1)=='\\' && !strchr_m(handlename+2, '\\')) {
432 DEBUGADD(4,("Printer is a print server\n"));
433 Printer->printer_type = SPLHND_SERVER;
435 /* it's a printer (set_printer_hnd_name() will handle port monitors */
437 DEBUGADD(4,("Printer is a printer\n"));
438 Printer->printer_type = SPLHND_PRINTER;
444 /****************************************************************************
445 Set printer handle name.. Accept names like \\server, \\server\printer,
446 \\server\SHARE, & "\\server\,XcvMonitor Standard TCP/IP Port" See
447 the MSDN docs regarding OpenPrinter() for details on the XcvData() and
448 XcvDataPort() interface.
449 ****************************************************************************/
451 static bool set_printer_hnd_name(Printer_entry *Printer, char *handlename)
454 int n_services=lp_numservices();
455 char *aprinter, *printername;
456 const char *servername;
459 NT_PRINTER_INFO_LEVEL *printer = NULL;
462 DEBUG(4,("Setting printer name=%s (len=%lu)\n", handlename, (unsigned long)strlen(handlename)));
464 aprinter = handlename;
465 if ( *handlename == '\\' ) {
466 servername = canon_servername(handlename);
467 if ( (aprinter = strchr_m( servername, '\\' )) != NULL ) {
475 /* save the servername to fill in replies on this handle */
477 if ( !is_myname_or_ipaddr( servername ) )
480 fstrcpy( Printer->servername, servername );
482 if ( Printer->printer_type == SPLHND_SERVER )
485 if ( Printer->printer_type != SPLHND_PRINTER )
488 DEBUGADD(5, ("searching for [%s]\n", aprinter ));
490 /* check for the Port Monitor Interface */
492 if ( strequal( aprinter, SPL_XCV_MONITOR_TCPMON ) ) {
493 Printer->printer_type = SPLHND_PORTMON_TCP;
494 fstrcpy(sname, SPL_XCV_MONITOR_TCPMON);
497 else if ( strequal( aprinter, SPL_XCV_MONITOR_LOCALMON ) ) {
498 Printer->printer_type = SPLHND_PORTMON_LOCAL;
499 fstrcpy(sname, SPL_XCV_MONITOR_LOCALMON);
503 /* Search all sharenames first as this is easier than pulling
504 the printer_info_2 off of disk. Don't use find_service() since
505 that calls out to map_username() */
507 /* do another loop to look for printernames */
509 for (snum=0; !found && snum<n_services; snum++) {
511 /* no point going on if this is not a printer */
513 if ( !(lp_snum_ok(snum) && lp_print_ok(snum)) )
516 fstrcpy(sname, lp_servicename(snum));
517 if ( strequal( aprinter, sname ) ) {
522 /* no point looking up the printer object if
523 we aren't allowing printername != sharename */
525 if ( lp_force_printername(snum) )
528 fstrcpy(sname, lp_servicename(snum));
532 /* This call doesn't fill in the location or comment from
533 * a CUPS server for efficiency with large numbers of printers.
537 result = get_a_printer_search( NULL, &printer, 2, sname );
538 if ( !W_ERROR_IS_OK(result) ) {
539 DEBUG(0,("set_printer_hnd_name: failed to lookup printer [%s] -- result [%s]\n",
540 sname, win_errstr(result)));
544 /* printername is always returned as \\server\printername */
545 if ( !(printername = strchr_m(&printer->info_2->printername[2], '\\')) ) {
546 DEBUG(0,("set_printer_hnd_name: info2->printername in wrong format! [%s]\n",
547 printer->info_2->printername));
548 free_a_printer( &printer, 2);
554 if ( strequal(printername, aprinter) ) {
555 free_a_printer( &printer, 2);
560 DEBUGADD(10, ("printername: %s\n", printername));
562 free_a_printer( &printer, 2);
565 free_a_printer( &printer, 2);
568 DEBUGADD(4,("Printer not found\n"));
572 DEBUGADD(4,("set_printer_hnd_name: Printer found: %s -> %s\n", aprinter, sname));
574 fstrcpy(Printer->sharename, sname);
579 /****************************************************************************
580 Find first available printer slot. creates a printer handle for you.
581 ****************************************************************************/
583 static bool open_printer_hnd(pipes_struct *p, POLICY_HND *hnd, char *name, uint32 access_granted)
585 Printer_entry *new_printer;
587 DEBUG(10,("open_printer_hnd: name [%s]\n", name));
589 new_printer = TALLOC_ZERO_P(NULL, Printer_entry);
590 if (new_printer == NULL) {
593 talloc_set_destructor(new_printer, printer_entry_destructor);
595 if (!create_policy_hnd(p, hnd, new_printer)) {
596 TALLOC_FREE(new_printer);
600 /* Add to the internal list. */
601 DLIST_ADD(printers_list, new_printer);
603 new_printer->notify.option=NULL;
605 if (!set_printer_hnd_printertype(new_printer, name)) {
606 close_printer_handle(p, hnd);
610 if (!set_printer_hnd_name(new_printer, name)) {
611 close_printer_handle(p, hnd);
615 new_printer->access_granted = access_granted;
617 DEBUG(5, ("%d printer handles active\n", (int)p->pipe_handles->count ));
622 /***************************************************************************
623 check to see if the client motify handle is monitoring the notification
624 given by (notify_type, notify_field).
625 **************************************************************************/
627 static bool is_monitoring_event_flags(uint32 flags, uint16 notify_type,
633 static bool is_monitoring_event(Printer_entry *p, uint16 notify_type,
636 SPOOL_NOTIFY_OPTION *option = p->notify.option;
640 * Flags should always be zero when the change notify
641 * is registered by the client's spooler. A user Win32 app
642 * might use the flags though instead of the NOTIFY_OPTION_INFO
651 return is_monitoring_event_flags(
652 p->notify.flags, notify_type, notify_field);
654 for (i = 0; i < option->count; i++) {
656 /* Check match for notify_type */
658 if (option->ctr.type[i].type != notify_type)
661 /* Check match for field */
663 for (j = 0; j < option->ctr.type[i].count; j++) {
664 if (option->ctr.type[i].fields[j] == notify_field) {
670 DEBUG(10, ("Open handle for \\\\%s\\%s is not monitoring 0x%02x/0x%02x\n",
671 p->servername, p->sharename, notify_type, notify_field));
676 /* Convert a notification message to a SPOOL_NOTIFY_INFO_DATA struct */
678 static void notify_one_value(struct spoolss_notify_msg *msg,
679 SPOOL_NOTIFY_INFO_DATA *data,
682 data->notify_data.value[0] = msg->notify.value[0];
683 data->notify_data.value[1] = 0;
686 static void notify_string(struct spoolss_notify_msg *msg,
687 SPOOL_NOTIFY_INFO_DATA *data,
692 /* The length of the message includes the trailing \0 */
694 init_unistr2(&unistr, msg->notify.data, UNI_STR_TERMINATE);
696 data->notify_data.data.length = msg->len * 2;
697 data->notify_data.data.string = TALLOC_ARRAY(mem_ctx, uint16, msg->len);
699 if (!data->notify_data.data.string) {
700 data->notify_data.data.length = 0;
704 memcpy(data->notify_data.data.string, unistr.buffer, msg->len * 2);
707 static void notify_system_time(struct spoolss_notify_msg *msg,
708 SPOOL_NOTIFY_INFO_DATA *data,
714 if (msg->len != sizeof(time_t)) {
715 DEBUG(5, ("notify_system_time: received wrong sized message (%d)\n",
720 if (!prs_init(&ps, RPC_MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL)) {
721 DEBUG(5, ("notify_system_time: prs_init() failed\n"));
725 if (!make_systemtime(&systime, gmtime((time_t *)msg->notify.data))) {
726 DEBUG(5, ("notify_system_time: unable to make systemtime\n"));
731 if (!spoolss_io_system_time("", &ps, 0, &systime)) {
736 data->notify_data.data.length = prs_offset(&ps);
737 if (prs_offset(&ps)) {
738 data->notify_data.data.string = (uint16 *)
739 TALLOC(mem_ctx, prs_offset(&ps));
740 if (!data->notify_data.data.string) {
744 prs_copy_all_data_out((char *)data->notify_data.data.string, &ps);
746 data->notify_data.data.string = NULL;
752 struct notify2_message_table {
754 void (*fn)(struct spoolss_notify_msg *msg,
755 SPOOL_NOTIFY_INFO_DATA *data, TALLOC_CTX *mem_ctx);
758 static struct notify2_message_table printer_notify_table[] = {
759 /* 0x00 */ { "PRINTER_NOTIFY_SERVER_NAME", notify_string },
760 /* 0x01 */ { "PRINTER_NOTIFY_PRINTER_NAME", notify_string },
761 /* 0x02 */ { "PRINTER_NOTIFY_SHARE_NAME", notify_string },
762 /* 0x03 */ { "PRINTER_NOTIFY_PORT_NAME", notify_string },
763 /* 0x04 */ { "PRINTER_NOTIFY_DRIVER_NAME", notify_string },
764 /* 0x05 */ { "PRINTER_NOTIFY_COMMENT", notify_string },
765 /* 0x06 */ { "PRINTER_NOTIFY_LOCATION", notify_string },
766 /* 0x07 */ { "PRINTER_NOTIFY_DEVMODE", NULL },
767 /* 0x08 */ { "PRINTER_NOTIFY_SEPFILE", notify_string },
768 /* 0x09 */ { "PRINTER_NOTIFY_PRINT_PROCESSOR", notify_string },
769 /* 0x0a */ { "PRINTER_NOTIFY_PARAMETERS", NULL },
770 /* 0x0b */ { "PRINTER_NOTIFY_DATATYPE", notify_string },
771 /* 0x0c */ { "PRINTER_NOTIFY_SECURITY_DESCRIPTOR", NULL },
772 /* 0x0d */ { "PRINTER_NOTIFY_ATTRIBUTES", notify_one_value },
773 /* 0x0e */ { "PRINTER_NOTIFY_PRIORITY", notify_one_value },
774 /* 0x0f */ { "PRINTER_NOTIFY_DEFAULT_PRIORITY", NULL },
775 /* 0x10 */ { "PRINTER_NOTIFY_START_TIME", NULL },
776 /* 0x11 */ { "PRINTER_NOTIFY_UNTIL_TIME", NULL },
777 /* 0x12 */ { "PRINTER_NOTIFY_STATUS", notify_one_value },
780 static struct notify2_message_table job_notify_table[] = {
781 /* 0x00 */ { "JOB_NOTIFY_PRINTER_NAME", NULL },
782 /* 0x01 */ { "JOB_NOTIFY_MACHINE_NAME", NULL },
783 /* 0x02 */ { "JOB_NOTIFY_PORT_NAME", NULL },
784 /* 0x03 */ { "JOB_NOTIFY_USER_NAME", notify_string },
785 /* 0x04 */ { "JOB_NOTIFY_NOTIFY_NAME", NULL },
786 /* 0x05 */ { "JOB_NOTIFY_DATATYPE", NULL },
787 /* 0x06 */ { "JOB_NOTIFY_PRINT_PROCESSOR", NULL },
788 /* 0x07 */ { "JOB_NOTIFY_PARAMETERS", NULL },
789 /* 0x08 */ { "JOB_NOTIFY_DRIVER_NAME", NULL },
790 /* 0x09 */ { "JOB_NOTIFY_DEVMODE", NULL },
791 /* 0x0a */ { "JOB_NOTIFY_STATUS", notify_one_value },
792 /* 0x0b */ { "JOB_NOTIFY_STATUS_STRING", NULL },
793 /* 0x0c */ { "JOB_NOTIFY_SECURITY_DESCRIPTOR", NULL },
794 /* 0x0d */ { "JOB_NOTIFY_DOCUMENT", notify_string },
795 /* 0x0e */ { "JOB_NOTIFY_PRIORITY", NULL },
796 /* 0x0f */ { "JOB_NOTIFY_POSITION", NULL },
797 /* 0x10 */ { "JOB_NOTIFY_SUBMITTED", notify_system_time },
798 /* 0x11 */ { "JOB_NOTIFY_START_TIME", NULL },
799 /* 0x12 */ { "JOB_NOTIFY_UNTIL_TIME", NULL },
800 /* 0x13 */ { "JOB_NOTIFY_TIME", NULL },
801 /* 0x14 */ { "JOB_NOTIFY_TOTAL_PAGES", notify_one_value },
802 /* 0x15 */ { "JOB_NOTIFY_PAGES_PRINTED", NULL },
803 /* 0x16 */ { "JOB_NOTIFY_TOTAL_BYTES", notify_one_value },
804 /* 0x17 */ { "JOB_NOTIFY_BYTES_PRINTED", NULL },
808 /***********************************************************************
809 Allocate talloc context for container object
810 **********************************************************************/
812 static void notify_msg_ctr_init( SPOOLSS_NOTIFY_MSG_CTR *ctr )
817 ctr->ctx = talloc_init("notify_msg_ctr_init %p", ctr);
822 /***********************************************************************
823 release all allocated memory and zero out structure
824 **********************************************************************/
826 static void notify_msg_ctr_destroy( SPOOLSS_NOTIFY_MSG_CTR *ctr )
832 talloc_destroy(ctr->ctx);
839 /***********************************************************************
840 **********************************************************************/
842 static TALLOC_CTX* notify_ctr_getctx( SPOOLSS_NOTIFY_MSG_CTR *ctr )
850 /***********************************************************************
851 **********************************************************************/
853 static SPOOLSS_NOTIFY_MSG_GROUP* notify_ctr_getgroup( SPOOLSS_NOTIFY_MSG_CTR *ctr, uint32 idx )
855 if ( !ctr || !ctr->msg_groups )
858 if ( idx >= ctr->num_groups )
861 return &ctr->msg_groups[idx];
865 /***********************************************************************
866 How many groups of change messages do we have ?
867 **********************************************************************/
869 static int notify_msg_ctr_numgroups( SPOOLSS_NOTIFY_MSG_CTR *ctr )
874 return ctr->num_groups;
877 /***********************************************************************
878 Add a SPOOLSS_NOTIFY_MSG_CTR to the correct group
879 **********************************************************************/
881 static int notify_msg_ctr_addmsg( SPOOLSS_NOTIFY_MSG_CTR *ctr, SPOOLSS_NOTIFY_MSG *msg )
883 SPOOLSS_NOTIFY_MSG_GROUP *groups = NULL;
884 SPOOLSS_NOTIFY_MSG_GROUP *msg_grp = NULL;
885 SPOOLSS_NOTIFY_MSG *msg_list = NULL;
891 /* loop over all groups looking for a matching printer name */
893 for ( i=0; i<ctr->num_groups; i++ ) {
894 if ( strcmp(ctr->msg_groups[i].printername, msg->printer) == 0 )
898 /* add a new group? */
900 if ( i == ctr->num_groups ) {
903 if ( !(groups = TALLOC_REALLOC_ARRAY( ctr->ctx, ctr->msg_groups, SPOOLSS_NOTIFY_MSG_GROUP, ctr->num_groups)) ) {
904 DEBUG(0,("notify_msg_ctr_addmsg: talloc_realloc() failed!\n"));
907 ctr->msg_groups = groups;
909 /* clear the new entry and set the printer name */
911 ZERO_STRUCT( ctr->msg_groups[ctr->num_groups-1] );
912 fstrcpy( ctr->msg_groups[ctr->num_groups-1].printername, msg->printer );
915 /* add the change messages; 'i' is the correct index now regardless */
917 msg_grp = &ctr->msg_groups[i];
921 if ( !(msg_list = TALLOC_REALLOC_ARRAY( ctr->ctx, msg_grp->msgs, SPOOLSS_NOTIFY_MSG, msg_grp->num_msgs )) ) {
922 DEBUG(0,("notify_msg_ctr_addmsg: talloc_realloc() failed for new message [%d]!\n", msg_grp->num_msgs));
925 msg_grp->msgs = msg_list;
927 new_slot = msg_grp->num_msgs-1;
928 memcpy( &msg_grp->msgs[new_slot], msg, sizeof(SPOOLSS_NOTIFY_MSG) );
930 /* need to allocate own copy of data */
933 msg_grp->msgs[new_slot].notify.data = (char *)
934 TALLOC_MEMDUP( ctr->ctx, msg->notify.data, msg->len );
936 return ctr->num_groups;
939 /***********************************************************************
940 Send a change notication message on all handles which have a call
942 **********************************************************************/
944 static void send_notify2_changes( SPOOLSS_NOTIFY_MSG_CTR *ctr, uint32 idx )
947 TALLOC_CTX *mem_ctx = notify_ctr_getctx( ctr );
948 SPOOLSS_NOTIFY_MSG_GROUP *msg_group = notify_ctr_getgroup( ctr, idx );
949 SPOOLSS_NOTIFY_MSG *messages;
950 int sending_msg_count;
953 DEBUG(5,("send_notify2_changes() called with no msg group!\n"));
957 messages = msg_group->msgs;
960 DEBUG(5,("send_notify2_changes() called with no messages!\n"));
964 DEBUG(8,("send_notify2_changes: Enter...[%s]\n", msg_group->printername));
966 /* loop over all printers */
968 for (p = printers_list; p; p = p->next) {
969 SPOOL_NOTIFY_INFO_DATA *data;
974 /* Is there notification on this handle? */
976 if ( !p->notify.client_connected )
979 DEBUG(10,("Client connected! [\\\\%s\\%s]\n", p->servername, p->sharename));
981 /* For this printer? Print servers always receive
984 if ( ( p->printer_type == SPLHND_PRINTER ) &&
985 ( !strequal(msg_group->printername, p->sharename) ) )
988 DEBUG(10,("Our printer\n"));
990 /* allocate the max entries possible */
992 data = TALLOC_ARRAY( mem_ctx, SPOOL_NOTIFY_INFO_DATA, msg_group->num_msgs);
999 /* build the array of change notifications */
1001 sending_msg_count = 0;
1003 for ( i=0; i<msg_group->num_msgs; i++ ) {
1004 SPOOLSS_NOTIFY_MSG *msg = &messages[i];
1006 /* Are we monitoring this event? */
1008 if (!is_monitoring_event(p, msg->type, msg->field))
1011 sending_msg_count++;
1014 DEBUG(10,("process_notify2_message: Sending message type [0x%x] field [0x%2x] for printer [%s]\n",
1015 msg->type, msg->field, p->sharename));
1018 * if the is a printer notification handle and not a job notification
1019 * type, then set the id to 0. Other wise just use what was specified
1022 * When registering change notification on a print server handle
1023 * we always need to send back the id (snum) matching the printer
1024 * for which the change took place. For change notify registered
1025 * on a printer handle, this does not matter and the id should be 0.
1030 if ( ( p->printer_type == SPLHND_PRINTER ) && ( msg->type == PRINTER_NOTIFY_TYPE ) )
1036 /* Convert unix jobid to smb jobid */
1038 if (msg->flags & SPOOLSS_NOTIFY_MSG_UNIX_JOBID) {
1039 id = sysjob_to_jobid(msg->id);
1042 DEBUG(3, ("no such unix jobid %d\n", msg->id));
1047 construct_info_data( &data[data_len], msg->type, msg->field, id );
1050 case PRINTER_NOTIFY_TYPE:
1051 if ( printer_notify_table[msg->field].fn )
1052 printer_notify_table[msg->field].fn(msg, &data[data_len], mem_ctx);
1055 case JOB_NOTIFY_TYPE:
1056 if ( job_notify_table[msg->field].fn )
1057 job_notify_table[msg->field].fn(msg, &data[data_len], mem_ctx);
1061 DEBUG(5, ("Unknown notification type %d\n", msg->type));
1068 if ( sending_msg_count ) {
1069 rpccli_spoolss_rrpcn( notify_cli_pipe, mem_ctx, &p->notify.client_hnd,
1070 data_len, data, p->notify.change, 0 );
1075 DEBUG(8,("send_notify2_changes: Exit...\n"));
1079 /***********************************************************************
1080 **********************************************************************/
1082 static bool notify2_unpack_msg( SPOOLSS_NOTIFY_MSG *msg, struct timeval *tv, void *buf, size_t len )
1085 uint32 tv_sec, tv_usec;
1088 /* Unpack message */
1090 offset += tdb_unpack((uint8 *)buf + offset, len - offset, "f",
1093 offset += tdb_unpack((uint8 *)buf + offset, len - offset, "ddddddd",
1095 &msg->type, &msg->field, &msg->id, &msg->len, &msg->flags);
1098 tdb_unpack((uint8 *)buf + offset, len - offset, "dd",
1099 &msg->notify.value[0], &msg->notify.value[1]);
1101 tdb_unpack((uint8 *)buf + offset, len - offset, "B",
1102 &msg->len, &msg->notify.data);
1104 DEBUG(3, ("notify2_unpack_msg: got NOTIFY2 message for printer %s, jobid %u type %d, field 0x%02x, flags 0x%04x\n",
1105 msg->printer, (unsigned int)msg->id, msg->type, msg->field, msg->flags));
1107 tv->tv_sec = tv_sec;
1108 tv->tv_usec = tv_usec;
1111 DEBUG(3, ("notify2_unpack_msg: value1 = %d, value2 = %d\n", msg->notify.value[0],
1112 msg->notify.value[1]));
1114 dump_data(3, (uint8 *)msg->notify.data, msg->len);
1119 /********************************************************************
1120 Receive a notify2 message list
1121 ********************************************************************/
1123 static void receive_notify2_message_list(struct messaging_context *msg,
1126 struct server_id server_id,
1129 size_t msg_count, i;
1130 char *buf = (char *)data->data;
1133 SPOOLSS_NOTIFY_MSG notify;
1134 SPOOLSS_NOTIFY_MSG_CTR messages;
1137 if (data->length < 4) {
1138 DEBUG(0,("receive_notify2_message_list: bad message format (len < 4)!\n"));
1142 msg_count = IVAL(buf, 0);
1145 DEBUG(5, ("receive_notify2_message_list: got %lu messages in list\n", (unsigned long)msg_count));
1147 if (msg_count == 0) {
1148 DEBUG(0,("receive_notify2_message_list: bad message format (msg_count == 0) !\n"));
1152 /* initialize the container */
1154 ZERO_STRUCT( messages );
1155 notify_msg_ctr_init( &messages );
1158 * build message groups for each printer identified
1159 * in a change_notify msg. Remember that a PCN message
1160 * includes the handle returned for the srv_spoolss_replyopenprinter()
1161 * call. Therefore messages are grouped according to printer handle.
1164 for ( i=0; i<msg_count; i++ ) {
1165 struct timeval msg_tv;
1167 if (msg_ptr + 4 - buf > data->length) {
1168 DEBUG(0,("receive_notify2_message_list: bad message format (len > buf_size) !\n"));
1172 msg_len = IVAL(msg_ptr,0);
1175 if (msg_ptr + msg_len - buf > data->length) {
1176 DEBUG(0,("receive_notify2_message_list: bad message format (bad len) !\n"));
1180 /* unpack messages */
1182 ZERO_STRUCT( notify );
1183 notify2_unpack_msg( ¬ify, &msg_tv, msg_ptr, msg_len );
1186 /* add to correct list in container */
1188 notify_msg_ctr_addmsg( &messages, ¬ify );
1190 /* free memory that might have been allocated by notify2_unpack_msg() */
1192 if ( notify.len != 0 )
1193 SAFE_FREE( notify.notify.data );
1196 /* process each group of messages */
1198 num_groups = notify_msg_ctr_numgroups( &messages );
1199 for ( i=0; i<num_groups; i++ )
1200 send_notify2_changes( &messages, i );
1205 DEBUG(10,("receive_notify2_message_list: processed %u messages\n", (uint32)msg_count ));
1207 notify_msg_ctr_destroy( &messages );
1212 /********************************************************************
1213 Send a message to ourself about new driver being installed
1214 so we can upgrade the information for each printer bound to this
1216 ********************************************************************/
1218 static bool srv_spoolss_drv_upgrade_printer(char* drivername)
1220 int len = strlen(drivername);
1225 DEBUG(10,("srv_spoolss_drv_upgrade_printer: Sending message about driver upgrade [%s]\n",
1228 messaging_send_buf(smbd_messaging_context(), procid_self(),
1229 MSG_PRINTER_DRVUPGRADE,
1230 (uint8 *)drivername, len+1);
1235 /**********************************************************************
1236 callback to receive a MSG_PRINTER_DRVUPGRADE message and interate
1237 over all printers, upgrading ones as necessary
1238 **********************************************************************/
1240 void do_drv_upgrade_printer(struct messaging_context *msg,
1243 struct server_id server_id,
1248 int n_services = lp_numservices();
1251 len = MIN(data->length,sizeof(drivername)-1);
1252 strncpy(drivername, (const char *)data->data, len);
1254 DEBUG(10,("do_drv_upgrade_printer: Got message for new driver [%s]\n", drivername ));
1256 /* Iterate the printer list */
1258 for (snum=0; snum<n_services; snum++)
1260 if (lp_snum_ok(snum) && lp_print_ok(snum) )
1263 NT_PRINTER_INFO_LEVEL *printer = NULL;
1265 result = get_a_printer(NULL, &printer, 2, lp_const_servicename(snum));
1266 if (!W_ERROR_IS_OK(result))
1269 if (printer && printer->info_2 && !strcmp(drivername, printer->info_2->drivername))
1271 DEBUG(6,("Updating printer [%s]\n", printer->info_2->printername));
1273 /* all we care about currently is the change_id */
1275 result = mod_a_printer(printer, 2);
1276 if (!W_ERROR_IS_OK(result)) {
1277 DEBUG(3,("do_drv_upgrade_printer: mod_a_printer() failed with status [%s]\n",
1278 win_errstr(result)));
1282 free_a_printer(&printer, 2);
1289 /********************************************************************
1290 Update the cache for all printq's with a registered client
1292 ********************************************************************/
1294 void update_monitored_printq_cache( void )
1296 Printer_entry *printer = printers_list;
1299 /* loop through all printers and update the cache where
1300 client_connected == True */
1303 if ( (printer->printer_type == SPLHND_PRINTER)
1304 && printer->notify.client_connected )
1306 snum = print_queue_snum(printer->sharename);
1307 print_queue_status( snum, NULL, NULL );
1310 printer = printer->next;
1315 /********************************************************************
1316 Send a message to ourself about new driver being installed
1317 so we can upgrade the information for each printer bound to this
1319 ********************************************************************/
1321 static bool srv_spoolss_reset_printerdata(char* drivername)
1323 int len = strlen(drivername);
1328 DEBUG(10,("srv_spoolss_reset_printerdata: Sending message about resetting printerdata [%s]\n",
1331 messaging_send_buf(smbd_messaging_context(), procid_self(),
1332 MSG_PRINTERDATA_INIT_RESET,
1333 (uint8 *)drivername, len+1);
1338 /**********************************************************************
1339 callback to receive a MSG_PRINTERDATA_INIT_RESET message and interate
1340 over all printers, resetting printer data as neessary
1341 **********************************************************************/
1343 void reset_all_printerdata(struct messaging_context *msg,
1346 struct server_id server_id,
1351 int n_services = lp_numservices();
1354 len = MIN( data->length, sizeof(drivername)-1 );
1355 strncpy( drivername, (const char *)data->data, len );
1357 DEBUG(10,("reset_all_printerdata: Got message for new driver [%s]\n", drivername ));
1359 /* Iterate the printer list */
1361 for ( snum=0; snum<n_services; snum++ )
1363 if ( lp_snum_ok(snum) && lp_print_ok(snum) )
1366 NT_PRINTER_INFO_LEVEL *printer = NULL;
1368 result = get_a_printer( NULL, &printer, 2, lp_const_servicename(snum) );
1369 if ( !W_ERROR_IS_OK(result) )
1373 * if the printer is bound to the driver,
1374 * then reset to the new driver initdata
1377 if ( printer && printer->info_2 && !strcmp(drivername, printer->info_2->drivername) )
1379 DEBUG(6,("reset_all_printerdata: Updating printer [%s]\n", printer->info_2->printername));
1381 if ( !set_driver_init(printer, 2) ) {
1382 DEBUG(5,("reset_all_printerdata: Error resetting printer data for printer [%s], driver [%s]!\n",
1383 printer->info_2->printername, printer->info_2->drivername));
1386 result = mod_a_printer( printer, 2 );
1387 if ( !W_ERROR_IS_OK(result) ) {
1388 DEBUG(3,("reset_all_printerdata: mod_a_printer() failed! (%s)\n",
1389 get_dos_error_msg(result)));
1393 free_a_printer( &printer, 2 );
1402 /********************************************************************
1403 Copy routines used by convert_to_openprinterex()
1404 *******************************************************************/
1406 static DEVICEMODE* dup_devicemode(TALLOC_CTX *ctx, DEVICEMODE *devmode)
1414 DEBUG (8,("dup_devmode\n"));
1416 /* bulk copy first */
1418 d = (DEVICEMODE *)TALLOC_MEMDUP(ctx, devmode, sizeof(DEVICEMODE));
1422 /* dup the pointer members separately */
1424 len = unistrlen(devmode->devicename.buffer);
1426 d->devicename.buffer = TALLOC_ARRAY(ctx, uint16, len);
1427 if (!d->devicename.buffer) {
1430 if (unistrcpy(d->devicename.buffer, devmode->devicename.buffer) != len)
1435 len = unistrlen(devmode->formname.buffer);
1437 d->formname.buffer = TALLOC_ARRAY(ctx, uint16, len);
1438 if (!d->formname.buffer) {
1441 if (unistrcpy(d->formname.buffer, devmode->formname.buffer) != len)
1445 if (devmode->driverextra) {
1446 d->dev_private = (uint8 *)TALLOC_MEMDUP(ctx, devmode->dev_private,
1447 devmode->driverextra);
1448 if (!d->dev_private) {
1452 d->dev_private = NULL;
1457 static void copy_devmode_ctr(TALLOC_CTX *ctx, DEVMODE_CTR *new_ctr, DEVMODE_CTR *ctr)
1459 if (!new_ctr || !ctr)
1462 DEBUG(8,("copy_devmode_ctr\n"));
1464 new_ctr->size = ctr->size;
1465 new_ctr->devmode_ptr = ctr->devmode_ptr;
1467 if(ctr->devmode_ptr)
1468 new_ctr->devmode = dup_devicemode(ctx, ctr->devmode);
1471 static void copy_printer_default(TALLOC_CTX *ctx, PRINTER_DEFAULT *new_def, PRINTER_DEFAULT *def)
1473 if (!new_def || !def)
1476 DEBUG(8,("copy_printer_defaults\n"));
1478 new_def->datatype_ptr = def->datatype_ptr;
1480 if (def->datatype_ptr)
1481 copy_unistr2(&new_def->datatype, &def->datatype);
1483 copy_devmode_ctr(ctx, &new_def->devmode_cont, &def->devmode_cont);
1485 new_def->access_required = def->access_required;
1488 /********************************************************************
1489 * Convert a SPOOL_Q_OPEN_PRINTER structure to a
1490 * SPOOL_Q_OPEN_PRINTER_EX structure
1491 ********************************************************************/
1493 static WERROR convert_to_openprinterex(TALLOC_CTX *ctx, SPOOL_Q_OPEN_PRINTER_EX *q_u_ex, SPOOL_Q_OPEN_PRINTER *q_u)
1495 if (!q_u_ex || !q_u)
1498 DEBUG(8,("convert_to_openprinterex\n"));
1500 if ( q_u->printername ) {
1501 q_u_ex->printername = TALLOC_ZERO_P( ctx, UNISTR2 );
1502 if (q_u_ex->printername == NULL)
1504 copy_unistr2(q_u_ex->printername, q_u->printername);
1507 copy_printer_default(ctx, &q_u_ex->printer_default, &q_u->printer_default);
1512 /********************************************************************
1513 * spoolss_open_printer
1515 * called from the spoolss dispatcher
1516 ********************************************************************/
1518 WERROR _spoolss_open_printer(pipes_struct *p, SPOOL_Q_OPEN_PRINTER *q_u, SPOOL_R_OPEN_PRINTER *r_u)
1520 SPOOL_Q_OPEN_PRINTER_EX q_u_ex;
1521 SPOOL_R_OPEN_PRINTER_EX r_u_ex;
1526 ZERO_STRUCT(q_u_ex);
1527 ZERO_STRUCT(r_u_ex);
1529 /* convert the OpenPrinter() call to OpenPrinterEx() */
1531 r_u_ex.status = convert_to_openprinterex(p->mem_ctx, &q_u_ex, q_u);
1532 if (!W_ERROR_IS_OK(r_u_ex.status))
1533 return r_u_ex.status;
1535 r_u_ex.status = _spoolss_open_printer_ex(p, &q_u_ex, &r_u_ex);
1537 /* convert back to OpenPrinter() */
1539 memcpy(r_u, &r_u_ex, sizeof(*r_u));
1541 if (W_ERROR_EQUAL(r_u->status, WERR_INVALID_PARAM)) {
1542 /* OpenPrinterEx returns this for a bad
1543 * printer name. We must return WERR_INVALID_PRINTER_NAME
1546 r_u->status = WERR_INVALID_PRINTER_NAME;
1551 /********************************************************************
1552 ********************************************************************/
1554 WERROR _spoolss_open_printer_ex( pipes_struct *p, SPOOL_Q_OPEN_PRINTER_EX *q_u, SPOOL_R_OPEN_PRINTER_EX *r_u)
1556 PRINTER_DEFAULT *printer_default = &q_u->printer_default;
1557 POLICY_HND *handle = &r_u->handle;
1561 Printer_entry *Printer=NULL;
1563 if (!q_u->printername) {
1564 return WERR_INVALID_PARAM;
1567 /* some sanity check because you can open a printer or a print server */
1568 /* aka: \\server\printer or \\server */
1570 unistr2_to_ascii(name, q_u->printername, sizeof(name));
1572 DEBUGADD(3,("checking name: %s\n",name));
1574 if (!open_printer_hnd(p, handle, name, 0)) {
1575 return WERR_INVALID_PARAM;
1578 Printer=find_printer_index_by_hnd(p, handle);
1580 DEBUG(0,(" _spoolss_open_printer_ex: logic error. Can't find printer "
1581 "handle we created for printer %s\n", name ));
1582 close_printer_handle(p,handle);
1583 return WERR_INVALID_PARAM;
1587 * First case: the user is opening the print server:
1589 * Disallow MS AddPrinterWizard if parameter disables it. A Win2k
1590 * client 1st tries an OpenPrinterEx with access==0, MUST be allowed.
1592 * Then both Win2k and WinNT clients try an OpenPrinterEx with
1593 * SERVER_ALL_ACCESS, which we allow only if the user is root (uid=0)
1594 * or if the user is listed in the smb.conf printer admin parameter.
1596 * Then they try OpenPrinterEx with SERVER_READ which we allow. This lets the
1597 * client view printer folder, but does not show the MSAPW.
1599 * Note: this test needs code to check access rights here too. Jeremy
1600 * could you look at this?
1602 * Second case: the user is opening a printer:
1603 * NT doesn't let us connect to a printer if the connecting user
1604 * doesn't have print permission.
1606 * Third case: user is opening a Port Monitor
1607 * access checks same as opening a handle to the print server.
1610 switch (Printer->printer_type )
1613 case SPLHND_PORTMON_TCP:
1614 case SPLHND_PORTMON_LOCAL:
1615 /* Printserver handles use global struct... */
1619 /* Map standard access rights to object specific access rights */
1621 se_map_standard(&printer_default->access_required,
1622 &printserver_std_mapping);
1624 /* Deny any object specific bits that don't apply to print
1625 servers (i.e printer and job specific bits) */
1627 printer_default->access_required &= SPECIFIC_RIGHTS_MASK;
1629 if (printer_default->access_required &
1630 ~(SERVER_ACCESS_ADMINISTER | SERVER_ACCESS_ENUMERATE)) {
1631 DEBUG(3, ("access DENIED for non-printserver bits\n"));
1632 close_printer_handle(p, handle);
1633 return WERR_ACCESS_DENIED;
1636 /* Allow admin access */
1638 if ( printer_default->access_required & SERVER_ACCESS_ADMINISTER )
1640 SE_PRIV se_printop = SE_PRINT_OPERATOR;
1642 if (!lp_ms_add_printer_wizard()) {
1643 close_printer_handle(p, handle);
1644 return WERR_ACCESS_DENIED;
1647 /* if the user is not root, doesn't have SE_PRINT_OPERATOR privilege,
1648 and not a printer admin, then fail */
1650 if ((p->server_info->utok.uid != 0) &&
1651 !user_has_privileges(p->server_info->ptok,
1653 !token_contains_name_in_list(
1654 uidtoname(p->server_info->utok.uid),
1656 p->server_info->ptok,
1657 lp_printer_admin(snum))) {
1658 close_printer_handle(p, handle);
1659 return WERR_ACCESS_DENIED;
1662 printer_default->access_required = SERVER_ACCESS_ADMINISTER;
1666 printer_default->access_required = SERVER_ACCESS_ENUMERATE;
1669 DEBUG(4,("Setting print server access = %s\n", (printer_default->access_required == SERVER_ACCESS_ADMINISTER)
1670 ? "SERVER_ACCESS_ADMINISTER" : "SERVER_ACCESS_ENUMERATE" ));
1672 /* We fall through to return WERR_OK */
1675 case SPLHND_PRINTER:
1676 /* NT doesn't let us connect to a printer if the connecting user
1677 doesn't have print permission. */
1679 if (!get_printer_snum(p, handle, &snum, NULL)) {
1680 close_printer_handle(p, handle);
1684 se_map_standard(&printer_default->access_required, &printer_std_mapping);
1686 /* map an empty access mask to the minimum access mask */
1687 if (printer_default->access_required == 0x0)
1688 printer_default->access_required = PRINTER_ACCESS_USE;
1691 * If we are not serving the printer driver for this printer,
1692 * map PRINTER_ACCESS_ADMINISTER to PRINTER_ACCESS_USE. This
1693 * will keep NT clients happy --jerry
1696 if (lp_use_client_driver(snum)
1697 && (printer_default->access_required & PRINTER_ACCESS_ADMINISTER))
1699 printer_default->access_required = PRINTER_ACCESS_USE;
1702 /* check smb.conf parameters and the the sec_desc */
1704 if ( !check_access(smbd_server_fd(), lp_hostsallow(snum), lp_hostsdeny(snum)) ) {
1705 DEBUG(3, ("access DENIED (hosts allow/deny) for printer open\n"));
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 printer_default->access_required)) {
1713 DEBUG(3, ("access DENIED for printer open\n"));
1714 close_printer_handle(p, handle);
1715 return WERR_ACCESS_DENIED;
1718 if ((printer_default->access_required & SPECIFIC_RIGHTS_MASK)& ~(PRINTER_ACCESS_ADMINISTER|PRINTER_ACCESS_USE)) {
1719 DEBUG(3, ("access DENIED for printer open - unknown bits\n"));
1720 close_printer_handle(p, handle);
1721 return WERR_ACCESS_DENIED;
1724 if (printer_default->access_required & PRINTER_ACCESS_ADMINISTER)
1725 printer_default->access_required = PRINTER_ACCESS_ADMINISTER;
1727 printer_default->access_required = PRINTER_ACCESS_USE;
1729 DEBUG(4,("Setting printer access = %s\n", (printer_default->access_required == PRINTER_ACCESS_ADMINISTER)
1730 ? "PRINTER_ACCESS_ADMINISTER" : "PRINTER_ACCESS_USE" ));
1735 /* sanity check to prevent programmer error */
1739 Printer->access_granted = printer_default->access_required;
1742 * If the client sent a devmode in the OpenPrinter() call, then
1743 * save it here in case we get a job submission on this handle
1746 if ( (Printer->printer_type != SPLHND_SERVER)
1747 && q_u->printer_default.devmode_cont.devmode_ptr )
1749 convert_devicemode( Printer->sharename, q_u->printer_default.devmode_cont.devmode,
1750 &Printer->nt_devmode );
1753 #if 0 /* JERRY -- I'm doubtful this is really effective */
1754 /* HACK ALERT!!! Sleep for 1/3 of a second to try trigger a LAN/WAN
1755 optimization in Windows 2000 clients --jerry */
1757 if ( (printer_default->access_required == PRINTER_ACCESS_ADMINISTER)
1758 && (RA_WIN2K == get_remote_arch()) )
1760 DEBUG(10,("_spoolss_open_printer_ex: Enabling LAN/WAN hack for Win2k clients.\n"));
1761 sys_usleep( 500000 );
1768 /****************************************************************************
1769 ****************************************************************************/
1771 static bool convert_printer_info(const SPOOL_PRINTER_INFO_LEVEL *uni,
1772 NT_PRINTER_INFO_LEVEL *printer, uint32 level)
1778 /* allocate memory if needed. Messy because
1779 convert_printer_info is used to update an existing
1780 printer or build a new one */
1782 if ( !printer->info_2 ) {
1783 printer->info_2 = TALLOC_ZERO_P( printer, NT_PRINTER_INFO_LEVEL_2 );
1784 if ( !printer->info_2 ) {
1785 DEBUG(0,("convert_printer_info: talloc() failed!\n"));
1790 ret = uni_2_asc_printer_info_2(uni->info_2, printer->info_2);
1791 printer->info_2->setuptime = time(NULL);
1799 static bool convert_printer_driver_info(const SPOOL_PRINTER_DRIVER_INFO_LEVEL *uni,
1800 NT_PRINTER_DRIVER_INFO_LEVEL *printer, uint32 level)
1806 printer->info_3=NULL;
1807 if (!uni_2_asc_printer_driver_3(uni->info_3, &printer->info_3))
1811 printer->info_6=NULL;
1812 if (!uni_2_asc_printer_driver_6(uni->info_6, &printer->info_6))
1822 bool convert_devicemode(const char *printername, const DEVICEMODE *devmode,
1823 NT_DEVICEMODE **pp_nt_devmode)
1825 NT_DEVICEMODE *nt_devmode = *pp_nt_devmode;
1828 * Ensure nt_devmode is a valid pointer
1829 * as we will be overwriting it.
1832 if (nt_devmode == NULL) {
1833 DEBUG(5, ("convert_devicemode: allocating a generic devmode\n"));
1834 if ((nt_devmode = construct_nt_devicemode(printername)) == NULL)
1838 rpcstr_pull(nt_devmode->devicename,devmode->devicename.buffer, 31, -1, 0);
1839 rpcstr_pull(nt_devmode->formname,devmode->formname.buffer, 31, -1, 0);
1841 nt_devmode->specversion=devmode->specversion;
1842 nt_devmode->driverversion=devmode->driverversion;
1843 nt_devmode->size=devmode->size;
1844 nt_devmode->fields=devmode->fields;
1845 nt_devmode->orientation=devmode->orientation;
1846 nt_devmode->papersize=devmode->papersize;
1847 nt_devmode->paperlength=devmode->paperlength;
1848 nt_devmode->paperwidth=devmode->paperwidth;
1849 nt_devmode->scale=devmode->scale;
1850 nt_devmode->copies=devmode->copies;
1851 nt_devmode->defaultsource=devmode->defaultsource;
1852 nt_devmode->printquality=devmode->printquality;
1853 nt_devmode->color=devmode->color;
1854 nt_devmode->duplex=devmode->duplex;
1855 nt_devmode->yresolution=devmode->yresolution;
1856 nt_devmode->ttoption=devmode->ttoption;
1857 nt_devmode->collate=devmode->collate;
1859 nt_devmode->logpixels=devmode->logpixels;
1860 nt_devmode->bitsperpel=devmode->bitsperpel;
1861 nt_devmode->pelswidth=devmode->pelswidth;
1862 nt_devmode->pelsheight=devmode->pelsheight;
1863 nt_devmode->displayflags=devmode->displayflags;
1864 nt_devmode->displayfrequency=devmode->displayfrequency;
1865 nt_devmode->icmmethod=devmode->icmmethod;
1866 nt_devmode->icmintent=devmode->icmintent;
1867 nt_devmode->mediatype=devmode->mediatype;
1868 nt_devmode->dithertype=devmode->dithertype;
1869 nt_devmode->reserved1=devmode->reserved1;
1870 nt_devmode->reserved2=devmode->reserved2;
1871 nt_devmode->panningwidth=devmode->panningwidth;
1872 nt_devmode->panningheight=devmode->panningheight;
1875 * Only change private and driverextra if the incoming devmode
1876 * has a new one. JRA.
1879 if ((devmode->driverextra != 0) && (devmode->dev_private != NULL)) {
1880 SAFE_FREE(nt_devmode->nt_dev_private);
1881 nt_devmode->driverextra=devmode->driverextra;
1882 if((nt_devmode->nt_dev_private=SMB_MALLOC_ARRAY(uint8, nt_devmode->driverextra)) == NULL)
1884 memcpy(nt_devmode->nt_dev_private, devmode->dev_private, nt_devmode->driverextra);
1887 *pp_nt_devmode = nt_devmode;
1892 /********************************************************************
1893 * _spoolss_enddocprinter_internal.
1894 ********************************************************************/
1896 static WERROR _spoolss_enddocprinter_internal(pipes_struct *p, POLICY_HND *handle)
1898 Printer_entry *Printer=find_printer_index_by_hnd(p, handle);
1902 DEBUG(2,("_spoolss_enddocprinter_internal: Invalid handle (%s:%u:%u)\n", OUR_HANDLE(handle)));
1906 if (!get_printer_snum(p, handle, &snum, NULL))
1909 Printer->document_started=False;
1910 print_job_end(snum, Printer->jobid,NORMAL_CLOSE);
1911 /* error codes unhandled so far ... */
1916 /********************************************************************
1917 * api_spoolss_closeprinter
1918 ********************************************************************/
1920 WERROR _spoolss_closeprinter(pipes_struct *p, SPOOL_Q_CLOSEPRINTER *q_u, SPOOL_R_CLOSEPRINTER *r_u)
1922 POLICY_HND *handle = &q_u->handle;
1924 Printer_entry *Printer=find_printer_index_by_hnd(p, handle);
1926 if (Printer && Printer->document_started)
1927 _spoolss_enddocprinter_internal(p, handle); /* print job was not closed */
1929 if (!close_printer_handle(p, handle))
1932 /* clear the returned printer handle. Observed behavior
1933 from Win2k server. Don't think this really matters.
1934 Previous code just copied the value of the closed
1937 memset(&r_u->handle, '\0', sizeof(r_u->handle));
1942 /********************************************************************
1943 * api_spoolss_deleteprinter
1945 ********************************************************************/
1947 WERROR _spoolss_deleteprinter(pipes_struct *p, SPOOL_Q_DELETEPRINTER *q_u, SPOOL_R_DELETEPRINTER *r_u)
1949 POLICY_HND *handle = &q_u->handle;
1950 Printer_entry *Printer=find_printer_index_by_hnd(p, handle);
1953 if (Printer && Printer->document_started)
1954 _spoolss_enddocprinter_internal(p, handle); /* print job was not closed */
1956 memcpy(&r_u->handle, &q_u->handle, sizeof(r_u->handle));
1958 result = delete_printer_handle(p, handle);
1960 update_c_setprinter(False);
1965 /*******************************************************************
1966 * static function to lookup the version id corresponding to an
1967 * long architecture string
1968 ******************************************************************/
1970 static int get_version_id (char * arch)
1973 struct table_node archi_table[]= {
1975 {"Windows 4.0", "WIN40", 0 },
1976 {"Windows NT x86", "W32X86", 2 },
1977 {"Windows NT R4000", "W32MIPS", 2 },
1978 {"Windows NT Alpha_AXP", "W32ALPHA", 2 },
1979 {"Windows NT PowerPC", "W32PPC", 2 },
1980 {"Windows IA64", "IA64", 3 },
1981 {"Windows x64", "x64", 3 },
1985 for (i=0; archi_table[i].long_archi != NULL; i++)
1987 if (strcmp(arch, archi_table[i].long_archi) == 0)
1988 return (archi_table[i].version);
1994 /********************************************************************
1995 * _spoolss_deleteprinterdriver
1996 ********************************************************************/
1998 WERROR _spoolss_deleteprinterdriver(pipes_struct *p, SPOOL_Q_DELETEPRINTERDRIVER *q_u, SPOOL_R_DELETEPRINTERDRIVER *r_u)
2002 NT_PRINTER_DRIVER_INFO_LEVEL info;
2003 NT_PRINTER_DRIVER_INFO_LEVEL info_win2k;
2006 WERROR status_win2k = WERR_ACCESS_DENIED;
2007 SE_PRIV se_printop = SE_PRINT_OPERATOR;
2009 /* if the user is not root, doesn't have SE_PRINT_OPERATOR privilege,
2010 and not a printer admin, then fail */
2012 if ( (p->server_info->utok.uid != 0)
2013 && !user_has_privileges(p->server_info->ptok, &se_printop )
2014 && !token_contains_name_in_list(
2015 uidtoname(p->server_info->utok.uid), NULL,
2016 NULL, p->server_info->ptok,
2017 lp_printer_admin(-1)) )
2019 return WERR_ACCESS_DENIED;
2022 unistr2_to_ascii(driver, &q_u->driver, sizeof(driver));
2023 unistr2_to_ascii(arch, &q_u->arch, sizeof(arch));
2025 /* check that we have a valid driver name first */
2027 if ((version=get_version_id(arch)) == -1)
2028 return WERR_INVALID_ENVIRONMENT;
2031 ZERO_STRUCT(info_win2k);
2033 if (!W_ERROR_IS_OK(get_a_printer_driver(&info, 3, driver, arch, version)))
2035 /* try for Win2k driver if "Windows NT x86" */
2037 if ( version == 2 ) {
2039 if (!W_ERROR_IS_OK(get_a_printer_driver(&info, 3, driver, arch, version))) {
2040 status = WERR_UNKNOWN_PRINTER_DRIVER;
2044 /* otherwise it was a failure */
2046 status = WERR_UNKNOWN_PRINTER_DRIVER;
2052 if (printer_driver_in_use(info.info_3)) {
2053 status = WERR_PRINTER_DRIVER_IN_USE;
2059 if (W_ERROR_IS_OK(get_a_printer_driver(&info_win2k, 3, driver, arch, 3)))
2061 /* if we get to here, we now have 2 driver info structures to remove */
2062 /* remove the Win2k driver first*/
2064 status_win2k = delete_printer_driver(
2065 p, info_win2k.info_3, 3, False );
2066 free_a_printer_driver( info_win2k, 3 );
2068 /* this should not have failed---if it did, report to client */
2069 if ( !W_ERROR_IS_OK(status_win2k) )
2071 status = status_win2k;
2077 status = delete_printer_driver(p, info.info_3, version, False);
2079 /* if at least one of the deletes succeeded return OK */
2081 if ( W_ERROR_IS_OK(status) || W_ERROR_IS_OK(status_win2k) )
2085 free_a_printer_driver( info, 3 );
2090 /********************************************************************
2091 * spoolss_deleteprinterdriverex
2092 ********************************************************************/
2094 WERROR _spoolss_deleteprinterdriverex(pipes_struct *p, SPOOL_Q_DELETEPRINTERDRIVEREX *q_u, SPOOL_R_DELETEPRINTERDRIVEREX *r_u)
2098 NT_PRINTER_DRIVER_INFO_LEVEL info;
2099 NT_PRINTER_DRIVER_INFO_LEVEL info_win2k;
2101 uint32 flags = q_u->delete_flags;
2104 WERROR status_win2k = WERR_ACCESS_DENIED;
2105 SE_PRIV se_printop = SE_PRINT_OPERATOR;
2107 /* if the user is not root, doesn't have SE_PRINT_OPERATOR privilege,
2108 and not a printer admin, then fail */
2110 if ( (p->server_info->utok.uid != 0)
2111 && !user_has_privileges(p->server_info->ptok, &se_printop )
2112 && !token_contains_name_in_list(
2113 uidtoname(p->server_info->utok.uid), NULL, NULL,
2114 p->server_info->ptok, lp_printer_admin(-1)) )
2116 return WERR_ACCESS_DENIED;
2119 unistr2_to_ascii(driver, &q_u->driver, sizeof(driver));
2120 unistr2_to_ascii(arch, &q_u->arch, sizeof(arch));
2122 /* check that we have a valid driver name first */
2123 if ((version=get_version_id(arch)) == -1) {
2124 /* this is what NT returns */
2125 return WERR_INVALID_ENVIRONMENT;
2128 if ( flags & DPD_DELETE_SPECIFIC_VERSION )
2129 version = q_u->version;
2132 ZERO_STRUCT(info_win2k);
2134 status = get_a_printer_driver(&info, 3, driver, arch, version);
2136 if ( !W_ERROR_IS_OK(status) )
2139 * if the client asked for a specific version,
2140 * or this is something other than Windows NT x86,
2144 if ( (flags&DPD_DELETE_SPECIFIC_VERSION) || (version !=2) )
2147 /* try for Win2k driver if "Windows NT x86" */
2150 if (!W_ERROR_IS_OK(get_a_printer_driver(&info, 3, driver, arch, version))) {
2151 status = WERR_UNKNOWN_PRINTER_DRIVER;
2156 if ( printer_driver_in_use(info.info_3) ) {
2157 status = WERR_PRINTER_DRIVER_IN_USE;
2162 * we have a couple of cases to consider.
2163 * (1) Are any files in use? If so and DPD_DELTE_ALL_FILE is set,
2164 * then the delete should fail if **any** files overlap with
2166 * (2) If DPD_DELTE_UNUSED_FILES is sert, then delete all
2167 * non-overlapping files
2168 * (3) If neither DPD_DELTE_ALL_FILE nor DPD_DELTE_ALL_FILES
2169 * is set, the do not delete any files
2170 * Refer to MSDN docs on DeletePrinterDriverEx() for details.
2173 delete_files = flags & (DPD_DELETE_ALL_FILES|DPD_DELETE_UNUSED_FILES);
2175 /* fail if any files are in use and DPD_DELETE_ALL_FILES is set */
2177 if ( delete_files && printer_driver_files_in_use(info.info_3) & (flags&DPD_DELETE_ALL_FILES) ) {
2178 /* no idea of the correct error here */
2179 status = WERR_ACCESS_DENIED;
2184 /* also check for W32X86/3 if necessary; maybe we already have? */
2186 if ( (version == 2) && ((flags&DPD_DELETE_SPECIFIC_VERSION) != DPD_DELETE_SPECIFIC_VERSION) ) {
2187 if (W_ERROR_IS_OK(get_a_printer_driver(&info_win2k, 3, driver, arch, 3)))
2190 if ( delete_files && printer_driver_files_in_use(info_win2k.info_3) & (flags&DPD_DELETE_ALL_FILES) ) {
2191 /* no idea of the correct error here */
2192 free_a_printer_driver( info_win2k, 3 );
2193 status = WERR_ACCESS_DENIED;
2197 /* if we get to here, we now have 2 driver info structures to remove */
2198 /* remove the Win2k driver first*/
2200 status_win2k = delete_printer_driver(
2201 p, info_win2k.info_3, 3, delete_files);
2202 free_a_printer_driver( info_win2k, 3 );
2204 /* this should not have failed---if it did, report to client */
2206 if ( !W_ERROR_IS_OK(status_win2k) )
2211 status = delete_printer_driver(p, info.info_3, version, delete_files);
2213 if ( W_ERROR_IS_OK(status) || W_ERROR_IS_OK(status_win2k) )
2216 free_a_printer_driver( info, 3 );
2222 /****************************************************************************
2223 Internal routine for retreiving printerdata
2224 ***************************************************************************/
2226 static WERROR get_printer_dataex( TALLOC_CTX *ctx, NT_PRINTER_INFO_LEVEL *printer,
2227 const char *key, const char *value, uint32 *type, uint8 **data,
2228 uint32 *needed, uint32 in_size )
2230 REGISTRY_VALUE *val;
2234 if ( !(val = get_printer_data( printer->info_2, key, value)) )
2235 return WERR_BADFILE;
2237 *type = regval_type( val );
2239 DEBUG(5,("get_printer_dataex: allocating %d\n", in_size));
2241 size = regval_size( val );
2243 /* copy the min(in_size, len) */
2246 data_len = (size > in_size) ? in_size : size*sizeof(uint8);
2248 /* special case for 0 length values */
2250 if ( (*data = (uint8 *)TALLOC_MEMDUP(ctx, regval_data_p(val), data_len)) == NULL )
2254 if ( (*data = (uint8 *)TALLOC_ZERO(ctx, in_size)) == NULL )
2263 DEBUG(5,("get_printer_dataex: copy done\n"));
2268 /****************************************************************************
2269 Internal routine for removing printerdata
2270 ***************************************************************************/
2272 static WERROR delete_printer_dataex( NT_PRINTER_INFO_LEVEL *printer, const char *key, const char *value )
2274 return delete_printer_data( printer->info_2, key, value );
2277 /****************************************************************************
2278 Internal routine for storing printerdata
2279 ***************************************************************************/
2281 WERROR set_printer_dataex( NT_PRINTER_INFO_LEVEL *printer, const char *key, const char *value,
2282 uint32 type, uint8 *data, int real_len )
2284 /* the registry objects enforce uniqueness based on value name */
2286 return add_printer_data( printer->info_2, key, value, type, data, real_len );
2289 /********************************************************************
2290 GetPrinterData on a printer server Handle.
2291 ********************************************************************/
2293 static WERROR getprinterdata_printer_server(TALLOC_CTX *ctx, fstring value, uint32 *type, uint8 **data, uint32 *needed, uint32 in_size)
2297 DEBUG(8,("getprinterdata_printer_server:%s\n", value));
2299 if (!StrCaseCmp(value, "W3SvcInstalled")) {
2301 if ( !(*data = TALLOC_ARRAY(ctx, uint8, sizeof(uint32) )) )
2303 SIVAL(*data, 0, 0x00);
2308 if (!StrCaseCmp(value, "BeepEnabled")) {
2310 if ( !(*data = TALLOC_ARRAY(ctx, uint8, sizeof(uint32) )) )
2312 SIVAL(*data, 0, 0x00);
2317 if (!StrCaseCmp(value, "EventLog")) {
2319 if ( !(*data = TALLOC_ARRAY(ctx, uint8, sizeof(uint32) )) )
2321 /* formally was 0x1b */
2322 SIVAL(*data, 0, 0x0);
2327 if (!StrCaseCmp(value, "NetPopup")) {
2329 if ( !(*data = TALLOC_ARRAY(ctx, uint8, sizeof(uint32) )) )
2331 SIVAL(*data, 0, 0x00);
2336 if (!StrCaseCmp(value, "MajorVersion")) {
2338 if ( !(*data = TALLOC_ARRAY(ctx, uint8, sizeof(uint32) )) )
2341 /* Windows NT 4.0 seems to not allow uploading of drivers
2342 to a server that reports 0x3 as the MajorVersion.
2343 need to investigate more how Win2k gets around this .
2346 if ( RA_WINNT == get_remote_arch() )
2355 if (!StrCaseCmp(value, "MinorVersion")) {
2357 if ( !(*data = TALLOC_ARRAY(ctx, uint8, sizeof(uint32) )) )
2365 * uint32 size = 0x114
2367 * uint32 minor = [0|1]
2368 * uint32 build = [2195|2600]
2369 * extra unicode string = e.g. "Service Pack 3"
2371 if (!StrCaseCmp(value, "OSVersion")) {
2375 if ( !(*data = TALLOC_ZERO_ARRAY(ctx, uint8, (*needed > in_size) ? *needed:in_size )) )
2378 SIVAL(*data, 0, *needed); /* size */
2379 SIVAL(*data, 4, 5); /* Windows 2000 == 5.0 */
2381 SIVAL(*data, 12, 2195); /* build */
2383 /* leave extra string empty */
2389 if (!StrCaseCmp(value, "DefaultSpoolDirectory")) {
2390 const char *string="C:\\PRINTERS";
2392 *needed = 2*(strlen(string)+1);
2393 if((*data = (uint8 *)TALLOC(ctx, (*needed > in_size) ? *needed:in_size )) == NULL)
2395 memset(*data, 0, (*needed > in_size) ? *needed:in_size);
2397 /* it's done by hand ready to go on the wire */
2398 for (i=0; i<strlen(string); i++) {
2399 (*data)[2*i]=string[i];
2400 (*data)[2*i+1]='\0';
2405 if (!StrCaseCmp(value, "Architecture")) {
2406 const char *string="Windows NT x86";
2408 *needed = 2*(strlen(string)+1);
2409 if((*data = (uint8 *)TALLOC(ctx, (*needed > in_size) ? *needed:in_size )) == NULL)
2411 memset(*data, 0, (*needed > in_size) ? *needed:in_size);
2412 for (i=0; i<strlen(string); i++) {
2413 (*data)[2*i]=string[i];
2414 (*data)[2*i+1]='\0';
2419 if (!StrCaseCmp(value, "DsPresent")) {
2421 if ( !(*data = TALLOC_ARRAY(ctx, uint8, sizeof(uint32) )) )
2424 /* only show the publish check box if we are a
2425 memeber of a AD domain */
2427 if ( lp_security() == SEC_ADS )
2428 SIVAL(*data, 0, 0x01);
2430 SIVAL(*data, 0, 0x00);
2436 if (!StrCaseCmp(value, "DNSMachineName")) {
2437 const char *hostname = get_mydnsfullname();
2440 return WERR_BADFILE;
2442 *needed = 2*(strlen(hostname)+1);
2443 if((*data = (uint8 *)TALLOC(ctx, (*needed > in_size) ? *needed:in_size )) == NULL)
2445 memset(*data, 0, (*needed > in_size) ? *needed:in_size);
2446 for (i=0; i<strlen(hostname); i++) {
2447 (*data)[2*i]=hostname[i];
2448 (*data)[2*i+1]='\0';
2454 return WERR_BADFILE;
2457 /********************************************************************
2458 * spoolss_getprinterdata
2459 ********************************************************************/
2461 WERROR _spoolss_getprinterdata(pipes_struct *p, SPOOL_Q_GETPRINTERDATA *q_u, SPOOL_R_GETPRINTERDATA *r_u)
2463 POLICY_HND *handle = &q_u->handle;
2464 UNISTR2 *valuename = &q_u->valuename;
2465 uint32 in_size = q_u->size;
2466 uint32 *type = &r_u->type;
2467 uint32 *out_size = &r_u->size;
2468 uint8 **data = &r_u->data;
2469 uint32 *needed = &r_u->needed;
2472 Printer_entry *Printer = find_printer_index_by_hnd(p, handle);
2473 NT_PRINTER_INFO_LEVEL *printer = NULL;
2477 * Reminder: when it's a string, the length is in BYTES
2478 * even if UNICODE is negociated.
2483 *out_size = in_size;
2485 /* in case of problem, return some default values */
2490 DEBUG(4,("_spoolss_getprinterdata\n"));
2493 DEBUG(2,("_spoolss_getprinterdata: Invalid handle (%s:%u:%u).\n", OUR_HANDLE(handle)));
2494 status = WERR_BADFID;
2498 unistr2_to_ascii(value, valuename, sizeof(value));
2500 if ( Printer->printer_type == SPLHND_SERVER )
2501 status = getprinterdata_printer_server( p->mem_ctx, value, type, data, needed, *out_size );
2504 if ( !get_printer_snum(p,handle, &snum, NULL) ) {
2505 status = WERR_BADFID;
2509 status = get_a_printer(Printer, &printer, 2, lp_servicename(snum));
2510 if ( !W_ERROR_IS_OK(status) )
2513 /* XP sends this and wants to change id value from the PRINTER_INFO_0 */
2515 if ( strequal(value, "ChangeId") ) {
2517 *needed = sizeof(uint32);
2518 if ( (*data = (uint8*)TALLOC(p->mem_ctx, sizeof(uint32))) == NULL) {
2519 status = WERR_NOMEM;
2522 SIVAL( *data, 0, printer->info_2->changeid );
2526 status = get_printer_dataex( p->mem_ctx, printer, SPOOL_PRINTERDATA_KEY, value, type, data, needed, *out_size );
2529 if (*needed > *out_size)
2530 status = WERR_MORE_DATA;
2533 if ( !W_ERROR_IS_OK(status) )
2535 DEBUG(5, ("error %d: allocating %d\n", W_ERROR_V(status),*out_size));
2537 /* reply this param doesn't exist */
2540 if((*data=(uint8 *)TALLOC_ZERO_ARRAY(p->mem_ctx, uint8, *out_size)) == NULL) {
2542 free_a_printer( &printer, 2 );
2550 /* cleanup & exit */
2553 free_a_printer( &printer, 2 );
2558 /*********************************************************
2559 Connect to the client machine.
2560 **********************************************************/
2562 static bool spoolss_connect_to_client(struct rpc_pipe_client **pp_pipe,
2563 struct sockaddr_storage *client_ss, const char *remote_machine)
2566 struct cli_state *the_cli;
2567 struct sockaddr_storage rm_addr;
2569 if ( is_zero_addr((struct sockaddr *)client_ss) ) {
2570 if ( !resolve_name( remote_machine, &rm_addr, 0x20) ) {
2571 DEBUG(2,("spoolss_connect_to_client: Can't resolve address for %s\n", remote_machine));
2575 if (ismyaddr((struct sockaddr *)&rm_addr)) {
2576 DEBUG(0,("spoolss_connect_to_client: Machine %s is one of our addresses. Cannot add to ourselves.\n", remote_machine));
2580 char addr[INET6_ADDRSTRLEN];
2581 rm_addr = *client_ss;
2582 print_sockaddr(addr, sizeof(addr), &rm_addr);
2583 DEBUG(5,("spoolss_connect_to_client: Using address %s (no name resolution necessary)\n",
2587 /* setup the connection */
2589 ret = cli_full_connection( &the_cli, global_myname(), remote_machine,
2590 &rm_addr, 0, "IPC$", "IPC",
2594 0, lp_client_signing(), NULL );
2596 if ( !NT_STATUS_IS_OK( ret ) ) {
2597 DEBUG(2,("spoolss_connect_to_client: connection to [%s] failed!\n",
2602 if ( the_cli->protocol != PROTOCOL_NT1 ) {
2603 DEBUG(0,("spoolss_connect_to_client: machine %s didn't negotiate NT protocol.\n", remote_machine));
2604 cli_shutdown(the_cli);
2609 * Ok - we have an anonymous connection to the IPC$ share.
2610 * Now start the NT Domain stuff :-).
2613 ret = cli_rpc_pipe_open_noauth(the_cli, &syntax_spoolss, pp_pipe);
2614 if (!NT_STATUS_IS_OK(ret)) {
2615 DEBUG(2,("spoolss_connect_to_client: unable to open the spoolss pipe on machine %s. Error was : %s.\n",
2616 remote_machine, nt_errstr(ret)));
2617 cli_shutdown(the_cli);
2624 /***************************************************************************
2625 Connect to the client.
2626 ****************************************************************************/
2628 static bool srv_spoolss_replyopenprinter(int snum, const char *printer,
2629 uint32 localprinter, uint32 type,
2630 POLICY_HND *handle, struct sockaddr_storage *client_ss)
2635 * If it's the first connection, contact the client
2636 * and connect to the IPC$ share anonymously
2638 if (smb_connections==0) {
2639 fstring unix_printer;
2641 fstrcpy(unix_printer, printer+2); /* the +2 is to strip the leading 2 backslashs */
2643 if ( !spoolss_connect_to_client( ¬ify_cli_pipe, client_ss, unix_printer ))
2646 messaging_register(smbd_messaging_context(), NULL,
2647 MSG_PRINTER_NOTIFY2,
2648 receive_notify2_message_list);
2649 /* Tell the connections db we're now interested in printer
2650 * notify messages. */
2651 register_message_flags( True, FLAG_MSG_PRINT_NOTIFY );
2655 * Tell the specific printing tdb we want messages for this printer
2656 * by registering our PID.
2659 if (!print_notify_register_pid(snum))
2660 DEBUG(0,("print_notify_register_pid: Failed to register our pid for printer %s\n", printer ));
2664 result = rpccli_spoolss_reply_open_printer(notify_cli_pipe,
2671 if (!W_ERROR_IS_OK(result))
2672 DEBUG(5,("srv_spoolss_reply_open_printer: Client RPC returned [%s]\n",
2673 win_errstr(result)));
2675 return (W_ERROR_IS_OK(result));
2678 /********************************************************************
2680 * ReplyFindFirstPrinterChangeNotifyEx
2682 * before replying OK: status=0 a rpc call is made to the workstation
2683 * asking ReplyOpenPrinter
2685 * in fact ReplyOpenPrinter is the changenotify equivalent on the spoolss pipe
2686 * called from api_spoolss_rffpcnex
2687 ********************************************************************/
2689 WERROR _spoolss_rffpcnex(pipes_struct *p, SPOOL_Q_RFFPCNEX *q_u, SPOOL_R_RFFPCNEX *r_u)
2691 POLICY_HND *handle = &q_u->handle;
2692 uint32 flags = q_u->flags;
2693 uint32 options = q_u->options;
2694 UNISTR2 *localmachine = &q_u->localmachine;
2695 uint32 printerlocal = q_u->printerlocal;
2697 SPOOL_NOTIFY_OPTION *option = q_u->option;
2698 struct sockaddr_storage client_ss;
2700 /* store the notify value in the printer struct */
2702 Printer_entry *Printer=find_printer_index_by_hnd(p, handle);
2705 DEBUG(2,("_spoolss_rffpcnex: Invalid handle (%s:%u:%u).\n", OUR_HANDLE(handle)));
2709 Printer->notify.flags=flags;
2710 Printer->notify.options=options;
2711 Printer->notify.printerlocal=printerlocal;
2713 if (Printer->notify.option)
2714 free_spool_notify_option(&Printer->notify.option);
2716 Printer->notify.option=dup_spool_notify_option(option);
2718 unistr2_to_ascii(Printer->notify.localmachine, localmachine,
2719 sizeof(Printer->notify.localmachine));
2721 /* Connect to the client machine and send a ReplyOpenPrinter */
2723 if ( Printer->printer_type == SPLHND_SERVER)
2725 else if ( (Printer->printer_type == SPLHND_PRINTER) &&
2726 !get_printer_snum(p, handle, &snum, NULL) )
2729 if (!interpret_string_addr(&client_ss, p->client_address,
2731 return WERR_SERVER_UNAVAILABLE;
2734 if(!srv_spoolss_replyopenprinter(snum, Printer->notify.localmachine,
2735 Printer->notify.printerlocal, 1,
2736 &Printer->notify.client_hnd, &client_ss))
2737 return WERR_SERVER_UNAVAILABLE;
2739 Printer->notify.client_connected=True;
2744 /*******************************************************************
2745 * fill a notify_info_data with the servername
2746 ********************************************************************/
2748 void spoolss_notify_server_name(int snum,
2749 SPOOL_NOTIFY_INFO_DATA *data,
2750 print_queue_struct *queue,
2751 NT_PRINTER_INFO_LEVEL *printer,
2752 TALLOC_CTX *mem_ctx)
2754 smb_ucs2_t *temp = NULL;
2757 len = rpcstr_push_talloc(mem_ctx, &temp, printer->info_2->servername);
2758 if (len == (uint32)-1) {
2762 data->notify_data.data.length = len;
2764 data->notify_data.data.string = (uint16 *)temp;
2766 data->notify_data.data.string = NULL;
2770 /*******************************************************************
2771 * fill a notify_info_data with the printername (not including the servername).
2772 ********************************************************************/
2774 void spoolss_notify_printer_name(int snum,
2775 SPOOL_NOTIFY_INFO_DATA *data,
2776 print_queue_struct *queue,
2777 NT_PRINTER_INFO_LEVEL *printer,
2778 TALLOC_CTX *mem_ctx)
2780 smb_ucs2_t *temp = NULL;
2783 /* the notify name should not contain the \\server\ part */
2784 char *p = strrchr(printer->info_2->printername, '\\');
2787 p = printer->info_2->printername;
2792 len = rpcstr_push_talloc(mem_ctx, &temp, p);
2793 if (len == (uint32)-1) {
2797 data->notify_data.data.length = len;
2799 data->notify_data.data.string = (uint16 *)temp;
2801 data->notify_data.data.string = NULL;
2805 /*******************************************************************
2806 * fill a notify_info_data with the servicename
2807 ********************************************************************/
2809 void spoolss_notify_share_name(int snum,
2810 SPOOL_NOTIFY_INFO_DATA *data,
2811 print_queue_struct *queue,
2812 NT_PRINTER_INFO_LEVEL *printer,
2813 TALLOC_CTX *mem_ctx)
2815 smb_ucs2_t *temp = NULL;
2818 len = rpcstr_push_talloc(mem_ctx, &temp, lp_servicename(snum));
2819 if (len == (uint32)-1) {
2823 data->notify_data.data.length = len;
2825 data->notify_data.data.string = (uint16 *)temp;
2827 data->notify_data.data.string = NULL;
2832 /*******************************************************************
2833 * fill a notify_info_data with the port name
2834 ********************************************************************/
2836 void spoolss_notify_port_name(int snum,
2837 SPOOL_NOTIFY_INFO_DATA *data,
2838 print_queue_struct *queue,
2839 NT_PRINTER_INFO_LEVEL *printer,
2840 TALLOC_CTX *mem_ctx)
2842 smb_ucs2_t *temp = NULL;
2845 /* even if it's strange, that's consistant in all the code */
2847 len = rpcstr_push_talloc(mem_ctx, &temp, printer->info_2->portname);
2848 if (len == (uint32)-1) {
2852 data->notify_data.data.length = len;
2854 data->notify_data.data.string = (uint16 *)temp;
2856 data->notify_data.data.string = NULL;
2860 /*******************************************************************
2861 * fill a notify_info_data with the printername
2862 * but it doesn't exist, have to see what to do
2863 ********************************************************************/
2865 void spoolss_notify_driver_name(int snum,
2866 SPOOL_NOTIFY_INFO_DATA *data,
2867 print_queue_struct *queue,
2868 NT_PRINTER_INFO_LEVEL *printer,
2869 TALLOC_CTX *mem_ctx)
2871 smb_ucs2_t *temp = NULL;
2874 len = rpcstr_push_talloc(mem_ctx, &temp, printer->info_2->drivername);
2875 if (len == (uint32)-1) {
2879 data->notify_data.data.length = len;
2881 data->notify_data.data.string = (uint16 *)temp;
2883 data->notify_data.data.string = NULL;
2887 /*******************************************************************
2888 * fill a notify_info_data with the comment
2889 ********************************************************************/
2891 void spoolss_notify_comment(int snum,
2892 SPOOL_NOTIFY_INFO_DATA *data,
2893 print_queue_struct *queue,
2894 NT_PRINTER_INFO_LEVEL *printer,
2895 TALLOC_CTX *mem_ctx)
2897 smb_ucs2_t *temp = NULL;
2900 if (*printer->info_2->comment == '\0')
2901 len = rpcstr_push_talloc(mem_ctx, &temp, lp_comment(snum));
2903 len = rpcstr_push_talloc(mem_ctx, &temp, printer->info_2->comment);
2905 if (len == (uint32)-1) {
2908 data->notify_data.data.length = len;
2910 data->notify_data.data.string = (uint16 *)temp;
2912 data->notify_data.data.string = NULL;
2916 /*******************************************************************
2917 * fill a notify_info_data with the comment
2918 * location = "Room 1, floor 2, building 3"
2919 ********************************************************************/
2921 void spoolss_notify_location(int snum,
2922 SPOOL_NOTIFY_INFO_DATA *data,
2923 print_queue_struct *queue,
2924 NT_PRINTER_INFO_LEVEL *printer,
2925 TALLOC_CTX *mem_ctx)
2927 smb_ucs2_t *temp = NULL;
2930 len = rpcstr_push_talloc(mem_ctx, &temp, printer->info_2->location);
2931 if (len == (uint32)-1) {
2935 data->notify_data.data.length = len;
2937 data->notify_data.data.string = (uint16 *)temp;
2939 data->notify_data.data.string = NULL;
2943 /*******************************************************************
2944 * fill a notify_info_data with the device mode
2945 * jfm:xxxx don't to it for know but that's a real problem !!!
2946 ********************************************************************/
2948 static void spoolss_notify_devmode(int snum,
2949 SPOOL_NOTIFY_INFO_DATA *data,
2950 print_queue_struct *queue,
2951 NT_PRINTER_INFO_LEVEL *printer,
2952 TALLOC_CTX *mem_ctx)
2954 /* for a dummy implementation we have to zero the fields */
2955 data->notify_data.data.length = 0;
2956 data->notify_data.data.string = NULL;
2959 /*******************************************************************
2960 * fill a notify_info_data with the separator file name
2961 ********************************************************************/
2963 void spoolss_notify_sepfile(int snum,
2964 SPOOL_NOTIFY_INFO_DATA *data,
2965 print_queue_struct *queue,
2966 NT_PRINTER_INFO_LEVEL *printer,
2967 TALLOC_CTX *mem_ctx)
2969 smb_ucs2_t *temp = NULL;
2972 len = rpcstr_push_talloc(mem_ctx, &temp, printer->info_2->sepfile);
2973 if (len == (uint32)-1) {
2977 data->notify_data.data.length = len;
2979 data->notify_data.data.string = (uint16 *)temp;
2981 data->notify_data.data.string = NULL;
2985 /*******************************************************************
2986 * fill a notify_info_data with the print processor
2987 * jfm:xxxx return always winprint to indicate we don't do anything to it
2988 ********************************************************************/
2990 void spoolss_notify_print_processor(int snum,
2991 SPOOL_NOTIFY_INFO_DATA *data,
2992 print_queue_struct *queue,
2993 NT_PRINTER_INFO_LEVEL *printer,
2994 TALLOC_CTX *mem_ctx)
2996 smb_ucs2_t *temp = NULL;
2999 len = rpcstr_push_talloc(mem_ctx, &temp, printer->info_2->printprocessor);
3000 if (len == (uint32)-1) {
3004 data->notify_data.data.length = len;
3006 data->notify_data.data.string = (uint16 *)temp;
3008 data->notify_data.data.string = NULL;
3012 /*******************************************************************
3013 * fill a notify_info_data with the print processor options
3014 * jfm:xxxx send an empty string
3015 ********************************************************************/
3017 void spoolss_notify_parameters(int snum,
3018 SPOOL_NOTIFY_INFO_DATA *data,
3019 print_queue_struct *queue,
3020 NT_PRINTER_INFO_LEVEL *printer,
3021 TALLOC_CTX *mem_ctx)
3023 smb_ucs2_t *temp = NULL;
3026 len = rpcstr_push_talloc(mem_ctx, &temp, printer->info_2->parameters);
3027 if (len == (uint32)-1) {
3031 data->notify_data.data.length = len;
3033 data->notify_data.data.string = (uint16 *)temp;
3035 data->notify_data.data.string = NULL;
3039 /*******************************************************************
3040 * fill a notify_info_data with the data type
3041 * jfm:xxxx always send RAW as data type
3042 ********************************************************************/
3044 void spoolss_notify_datatype(int snum,
3045 SPOOL_NOTIFY_INFO_DATA *data,
3046 print_queue_struct *queue,
3047 NT_PRINTER_INFO_LEVEL *printer,
3048 TALLOC_CTX *mem_ctx)
3050 smb_ucs2_t *temp = NULL;
3053 len = rpcstr_push_talloc(mem_ctx, &temp, printer->info_2->datatype);
3054 if (len == (uint32)-1) {
3058 data->notify_data.data.length = len;
3060 data->notify_data.data.string = (uint16 *)temp;
3062 data->notify_data.data.string = NULL;
3066 /*******************************************************************
3067 * fill a notify_info_data with the security descriptor
3068 * jfm:xxxx send an null pointer to say no security desc
3069 * have to implement security before !
3070 ********************************************************************/
3072 static void spoolss_notify_security_desc(int snum,
3073 SPOOL_NOTIFY_INFO_DATA *data,
3074 print_queue_struct *queue,
3075 NT_PRINTER_INFO_LEVEL *printer,
3076 TALLOC_CTX *mem_ctx)
3078 data->notify_data.sd.size = printer->info_2->secdesc_buf->sd_size;
3079 data->notify_data.sd.desc = dup_sec_desc( mem_ctx, printer->info_2->secdesc_buf->sd ) ;
3082 /*******************************************************************
3083 * fill a notify_info_data with the attributes
3084 * jfm:xxxx a samba printer is always shared
3085 ********************************************************************/
3087 void spoolss_notify_attributes(int snum,
3088 SPOOL_NOTIFY_INFO_DATA *data,
3089 print_queue_struct *queue,
3090 NT_PRINTER_INFO_LEVEL *printer,
3091 TALLOC_CTX *mem_ctx)
3093 data->notify_data.value[0] = printer->info_2->attributes;
3094 data->notify_data.value[1] = 0;
3097 /*******************************************************************
3098 * fill a notify_info_data with the priority
3099 ********************************************************************/
3101 static void spoolss_notify_priority(int snum,
3102 SPOOL_NOTIFY_INFO_DATA *data,
3103 print_queue_struct *queue,
3104 NT_PRINTER_INFO_LEVEL *printer,
3105 TALLOC_CTX *mem_ctx)
3107 data->notify_data.value[0] = printer->info_2->priority;
3108 data->notify_data.value[1] = 0;
3111 /*******************************************************************
3112 * fill a notify_info_data with the default priority
3113 ********************************************************************/
3115 static void spoolss_notify_default_priority(int snum,
3116 SPOOL_NOTIFY_INFO_DATA *data,
3117 print_queue_struct *queue,
3118 NT_PRINTER_INFO_LEVEL *printer,
3119 TALLOC_CTX *mem_ctx)
3121 data->notify_data.value[0] = printer->info_2->default_priority;
3122 data->notify_data.value[1] = 0;
3125 /*******************************************************************
3126 * fill a notify_info_data with the start time
3127 ********************************************************************/
3129 static void spoolss_notify_start_time(int snum,
3130 SPOOL_NOTIFY_INFO_DATA *data,
3131 print_queue_struct *queue,
3132 NT_PRINTER_INFO_LEVEL *printer,
3133 TALLOC_CTX *mem_ctx)
3135 data->notify_data.value[0] = printer->info_2->starttime;
3136 data->notify_data.value[1] = 0;
3139 /*******************************************************************
3140 * fill a notify_info_data with the until time
3141 ********************************************************************/
3143 static void spoolss_notify_until_time(int snum,
3144 SPOOL_NOTIFY_INFO_DATA *data,
3145 print_queue_struct *queue,
3146 NT_PRINTER_INFO_LEVEL *printer,
3147 TALLOC_CTX *mem_ctx)
3149 data->notify_data.value[0] = printer->info_2->untiltime;
3150 data->notify_data.value[1] = 0;
3153 /*******************************************************************
3154 * fill a notify_info_data with the status
3155 ********************************************************************/
3157 static void spoolss_notify_status(int snum,
3158 SPOOL_NOTIFY_INFO_DATA *data,
3159 print_queue_struct *queue,
3160 NT_PRINTER_INFO_LEVEL *printer,
3161 TALLOC_CTX *mem_ctx)
3163 print_status_struct status;
3165 print_queue_length(snum, &status);
3166 data->notify_data.value[0]=(uint32) status.status;
3167 data->notify_data.value[1] = 0;
3170 /*******************************************************************
3171 * fill a notify_info_data with the number of jobs queued
3172 ********************************************************************/
3174 void spoolss_notify_cjobs(int snum,
3175 SPOOL_NOTIFY_INFO_DATA *data,
3176 print_queue_struct *queue,
3177 NT_PRINTER_INFO_LEVEL *printer,
3178 TALLOC_CTX *mem_ctx)
3180 data->notify_data.value[0] = print_queue_length(snum, NULL);
3181 data->notify_data.value[1] = 0;
3184 /*******************************************************************
3185 * fill a notify_info_data with the average ppm
3186 ********************************************************************/
3188 static void spoolss_notify_average_ppm(int snum,
3189 SPOOL_NOTIFY_INFO_DATA *data,
3190 print_queue_struct *queue,
3191 NT_PRINTER_INFO_LEVEL *printer,
3192 TALLOC_CTX *mem_ctx)
3194 /* always respond 8 pages per minutes */
3195 /* a little hard ! */
3196 data->notify_data.value[0] = printer->info_2->averageppm;
3197 data->notify_data.value[1] = 0;
3200 /*******************************************************************
3201 * fill a notify_info_data with username
3202 ********************************************************************/
3204 static void spoolss_notify_username(int snum,
3205 SPOOL_NOTIFY_INFO_DATA *data,
3206 print_queue_struct *queue,
3207 NT_PRINTER_INFO_LEVEL *printer,
3208 TALLOC_CTX *mem_ctx)
3210 smb_ucs2_t *temp = NULL;
3213 len = rpcstr_push_talloc(mem_ctx, &temp, queue->fs_user);
3214 if (len == (uint32)-1) {
3218 data->notify_data.data.length = len;
3220 data->notify_data.data.string = (uint16 *)temp;
3222 data->notify_data.data.string = NULL;
3226 /*******************************************************************
3227 * fill a notify_info_data with job status
3228 ********************************************************************/
3230 static void spoolss_notify_job_status(int snum,
3231 SPOOL_NOTIFY_INFO_DATA *data,
3232 print_queue_struct *queue,
3233 NT_PRINTER_INFO_LEVEL *printer,
3234 TALLOC_CTX *mem_ctx)
3236 data->notify_data.value[0]=nt_printj_status(queue->status);
3237 data->notify_data.value[1] = 0;
3240 /*******************************************************************
3241 * fill a notify_info_data with job name
3242 ********************************************************************/
3244 static void spoolss_notify_job_name(int snum,
3245 SPOOL_NOTIFY_INFO_DATA *data,
3246 print_queue_struct *queue,
3247 NT_PRINTER_INFO_LEVEL *printer,
3248 TALLOC_CTX *mem_ctx)
3250 smb_ucs2_t *temp = NULL;
3253 len = rpcstr_push_talloc(mem_ctx, &temp, queue->fs_file);
3254 if (len == (uint32)-1) {
3258 data->notify_data.data.length = len;
3260 data->notify_data.data.string = (uint16 *)temp;
3262 data->notify_data.data.string = NULL;
3266 /*******************************************************************
3267 * fill a notify_info_data with job status
3268 ********************************************************************/
3270 static void spoolss_notify_job_status_string(int snum,
3271 SPOOL_NOTIFY_INFO_DATA *data,
3272 print_queue_struct *queue,
3273 NT_PRINTER_INFO_LEVEL *printer,
3274 TALLOC_CTX *mem_ctx)
3277 * Now we're returning job status codes we just return a "" here. JRA.
3281 smb_ucs2_t *temp = NULL;
3284 #if 0 /* NO LONGER NEEDED - JRA. 02/22/2001 */
3287 switch (queue->status) {
3292 p = ""; /* NT provides the paused string */
3301 #endif /* NO LONGER NEEDED. */
3303 len = rpcstr_push_talloc(mem_ctx, &temp, p);
3304 if (len == (uint32)-1) {
3308 data->notify_data.data.length = len;
3310 data->notify_data.data.string = (uint16 *)temp;
3312 data->notify_data.data.string = NULL;
3316 /*******************************************************************
3317 * fill a notify_info_data with job time
3318 ********************************************************************/
3320 static void spoolss_notify_job_time(int snum,
3321 SPOOL_NOTIFY_INFO_DATA *data,
3322 print_queue_struct *queue,
3323 NT_PRINTER_INFO_LEVEL *printer,
3324 TALLOC_CTX *mem_ctx)
3326 data->notify_data.value[0]=0x0;
3327 data->notify_data.value[1]=0;
3330 /*******************************************************************
3331 * fill a notify_info_data with job size
3332 ********************************************************************/
3334 static void spoolss_notify_job_size(int snum,
3335 SPOOL_NOTIFY_INFO_DATA *data,
3336 print_queue_struct *queue,
3337 NT_PRINTER_INFO_LEVEL *printer,
3338 TALLOC_CTX *mem_ctx)
3340 data->notify_data.value[0]=queue->size;
3341 data->notify_data.value[1]=0;
3344 /*******************************************************************
3345 * fill a notify_info_data with page info
3346 ********************************************************************/
3347 static void spoolss_notify_total_pages(int snum,
3348 SPOOL_NOTIFY_INFO_DATA *data,
3349 print_queue_struct *queue,
3350 NT_PRINTER_INFO_LEVEL *printer,
3351 TALLOC_CTX *mem_ctx)
3353 data->notify_data.value[0]=queue->page_count;
3354 data->notify_data.value[1]=0;
3357 /*******************************************************************
3358 * fill a notify_info_data with pages printed info.
3359 ********************************************************************/
3360 static void spoolss_notify_pages_printed(int snum,
3361 SPOOL_NOTIFY_INFO_DATA *data,
3362 print_queue_struct *queue,
3363 NT_PRINTER_INFO_LEVEL *printer,
3364 TALLOC_CTX *mem_ctx)
3366 data->notify_data.value[0]=0; /* Add code when back-end tracks this */
3367 data->notify_data.value[1]=0;
3370 /*******************************************************************
3371 Fill a notify_info_data with job position.
3372 ********************************************************************/
3374 static void spoolss_notify_job_position(int snum,
3375 SPOOL_NOTIFY_INFO_DATA *data,
3376 print_queue_struct *queue,
3377 NT_PRINTER_INFO_LEVEL *printer,
3378 TALLOC_CTX *mem_ctx)
3380 data->notify_data.value[0]=queue->job;
3381 data->notify_data.value[1]=0;
3384 /*******************************************************************
3385 Fill a notify_info_data with submitted time.
3386 ********************************************************************/
3388 static void spoolss_notify_submitted_time(int snum,
3389 SPOOL_NOTIFY_INFO_DATA *data,
3390 print_queue_struct *queue,
3391 NT_PRINTER_INFO_LEVEL *printer,
3392 TALLOC_CTX *mem_ctx)
3399 t=gmtime(&queue->time);
3401 len = sizeof(SYSTEMTIME);
3403 data->notify_data.data.length = len;
3404 data->notify_data.data.string = (uint16 *)TALLOC(mem_ctx, len);
3406 if (!data->notify_data.data.string) {
3407 data->notify_data.data.length = 0;
3411 make_systemtime(&st, t);
3414 * Systemtime must be linearized as a set of UINT16's.
3415 * Fix from Benjamin (Bj) Kuit bj@it.uts.edu.au
3418 p = (char *)data->notify_data.data.string;
3419 SSVAL(p, 0, st.year);
3420 SSVAL(p, 2, st.month);
3421 SSVAL(p, 4, st.dayofweek);
3422 SSVAL(p, 6, st.day);
3423 SSVAL(p, 8, st.hour);
3424 SSVAL(p, 10, st.minute);
3425 SSVAL(p, 12, st.second);
3426 SSVAL(p, 14, st.milliseconds);
3429 struct s_notify_info_data_table
3435 void (*fn) (int snum, SPOOL_NOTIFY_INFO_DATA *data,
3436 print_queue_struct *queue,
3437 NT_PRINTER_INFO_LEVEL *printer, TALLOC_CTX *mem_ctx);
3440 /* A table describing the various print notification constants and
3441 whether the notification data is a pointer to a variable sized
3442 buffer, a one value uint32 or a two value uint32. */
3444 static const struct s_notify_info_data_table notify_info_data_table[] =
3446 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_SERVER_NAME, "PRINTER_NOTIFY_SERVER_NAME", NOTIFY_STRING, spoolss_notify_server_name },
3447 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_PRINTER_NAME, "PRINTER_NOTIFY_PRINTER_NAME", NOTIFY_STRING, spoolss_notify_printer_name },
3448 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_SHARE_NAME, "PRINTER_NOTIFY_SHARE_NAME", NOTIFY_STRING, spoolss_notify_share_name },
3449 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_PORT_NAME, "PRINTER_NOTIFY_PORT_NAME", NOTIFY_STRING, spoolss_notify_port_name },
3450 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_DRIVER_NAME, "PRINTER_NOTIFY_DRIVER_NAME", NOTIFY_STRING, spoolss_notify_driver_name },
3451 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_COMMENT, "PRINTER_NOTIFY_COMMENT", NOTIFY_STRING, spoolss_notify_comment },
3452 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_LOCATION, "PRINTER_NOTIFY_LOCATION", NOTIFY_STRING, spoolss_notify_location },
3453 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_DEVMODE, "PRINTER_NOTIFY_DEVMODE", NOTIFY_POINTER, spoolss_notify_devmode },
3454 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_SEPFILE, "PRINTER_NOTIFY_SEPFILE", NOTIFY_STRING, spoolss_notify_sepfile },
3455 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_PRINT_PROCESSOR, "PRINTER_NOTIFY_PRINT_PROCESSOR", NOTIFY_STRING, spoolss_notify_print_processor },
3456 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_PARAMETERS, "PRINTER_NOTIFY_PARAMETERS", NOTIFY_STRING, spoolss_notify_parameters },
3457 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_DATATYPE, "PRINTER_NOTIFY_DATATYPE", NOTIFY_STRING, spoolss_notify_datatype },
3458 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_SECURITY_DESCRIPTOR, "PRINTER_NOTIFY_SECURITY_DESCRIPTOR", NOTIFY_SECDESC, spoolss_notify_security_desc },
3459 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_ATTRIBUTES, "PRINTER_NOTIFY_ATTRIBUTES", NOTIFY_ONE_VALUE, spoolss_notify_attributes },
3460 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_PRIORITY, "PRINTER_NOTIFY_PRIORITY", NOTIFY_ONE_VALUE, spoolss_notify_priority },
3461 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_DEFAULT_PRIORITY, "PRINTER_NOTIFY_DEFAULT_PRIORITY", NOTIFY_ONE_VALUE, spoolss_notify_default_priority },
3462 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_START_TIME, "PRINTER_NOTIFY_START_TIME", NOTIFY_ONE_VALUE, spoolss_notify_start_time },
3463 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_UNTIL_TIME, "PRINTER_NOTIFY_UNTIL_TIME", NOTIFY_ONE_VALUE, spoolss_notify_until_time },
3464 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_STATUS, "PRINTER_NOTIFY_STATUS", NOTIFY_ONE_VALUE, spoolss_notify_status },
3465 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_STATUS_STRING, "PRINTER_NOTIFY_STATUS_STRING", NOTIFY_POINTER, NULL },
3466 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_CJOBS, "PRINTER_NOTIFY_CJOBS", NOTIFY_ONE_VALUE, spoolss_notify_cjobs },
3467 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_AVERAGE_PPM, "PRINTER_NOTIFY_AVERAGE_PPM", NOTIFY_ONE_VALUE, spoolss_notify_average_ppm },
3468 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_TOTAL_PAGES, "PRINTER_NOTIFY_TOTAL_PAGES", NOTIFY_POINTER, NULL },
3469 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_PAGES_PRINTED, "PRINTER_NOTIFY_PAGES_PRINTED", NOTIFY_POINTER, NULL },
3470 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_TOTAL_BYTES, "PRINTER_NOTIFY_TOTAL_BYTES", NOTIFY_POINTER, NULL },
3471 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_BYTES_PRINTED, "PRINTER_NOTIFY_BYTES_PRINTED", NOTIFY_POINTER, NULL },
3472 { JOB_NOTIFY_TYPE, JOB_NOTIFY_PRINTER_NAME, "JOB_NOTIFY_PRINTER_NAME", NOTIFY_STRING, spoolss_notify_printer_name },
3473 { JOB_NOTIFY_TYPE, JOB_NOTIFY_MACHINE_NAME, "JOB_NOTIFY_MACHINE_NAME", NOTIFY_STRING, spoolss_notify_server_name },
3474 { JOB_NOTIFY_TYPE, JOB_NOTIFY_PORT_NAME, "JOB_NOTIFY_PORT_NAME", NOTIFY_STRING, spoolss_notify_port_name },
3475 { JOB_NOTIFY_TYPE, JOB_NOTIFY_USER_NAME, "JOB_NOTIFY_USER_NAME", NOTIFY_STRING, spoolss_notify_username },
3476 { JOB_NOTIFY_TYPE, JOB_NOTIFY_NOTIFY_NAME, "JOB_NOTIFY_NOTIFY_NAME", NOTIFY_STRING, spoolss_notify_username },
3477 { JOB_NOTIFY_TYPE, JOB_NOTIFY_DATATYPE, "JOB_NOTIFY_DATATYPE", NOTIFY_STRING, spoolss_notify_datatype },
3478 { JOB_NOTIFY_TYPE, JOB_NOTIFY_PRINT_PROCESSOR, "JOB_NOTIFY_PRINT_PROCESSOR", NOTIFY_STRING, spoolss_notify_print_processor },
3479 { JOB_NOTIFY_TYPE, JOB_NOTIFY_PARAMETERS, "JOB_NOTIFY_PARAMETERS", NOTIFY_STRING, spoolss_notify_parameters },
3480 { JOB_NOTIFY_TYPE, JOB_NOTIFY_DRIVER_NAME, "JOB_NOTIFY_DRIVER_NAME", NOTIFY_STRING, spoolss_notify_driver_name },
3481 { JOB_NOTIFY_TYPE, JOB_NOTIFY_DEVMODE, "JOB_NOTIFY_DEVMODE", NOTIFY_POINTER, spoolss_notify_devmode },
3482 { JOB_NOTIFY_TYPE, JOB_NOTIFY_STATUS, "JOB_NOTIFY_STATUS", NOTIFY_ONE_VALUE, spoolss_notify_job_status },
3483 { JOB_NOTIFY_TYPE, JOB_NOTIFY_STATUS_STRING, "JOB_NOTIFY_STATUS_STRING", NOTIFY_STRING, spoolss_notify_job_status_string },
3484 { JOB_NOTIFY_TYPE, JOB_NOTIFY_SECURITY_DESCRIPTOR, "JOB_NOTIFY_SECURITY_DESCRIPTOR", NOTIFY_POINTER, NULL },
3485 { JOB_NOTIFY_TYPE, JOB_NOTIFY_DOCUMENT, "JOB_NOTIFY_DOCUMENT", NOTIFY_STRING, spoolss_notify_job_name },
3486 { JOB_NOTIFY_TYPE, JOB_NOTIFY_PRIORITY, "JOB_NOTIFY_PRIORITY", NOTIFY_ONE_VALUE, spoolss_notify_priority },
3487 { JOB_NOTIFY_TYPE, JOB_NOTIFY_POSITION, "JOB_NOTIFY_POSITION", NOTIFY_ONE_VALUE, spoolss_notify_job_position },
3488 { JOB_NOTIFY_TYPE, JOB_NOTIFY_SUBMITTED, "JOB_NOTIFY_SUBMITTED", NOTIFY_POINTER, spoolss_notify_submitted_time },
3489 { JOB_NOTIFY_TYPE, JOB_NOTIFY_START_TIME, "JOB_NOTIFY_START_TIME", NOTIFY_ONE_VALUE, spoolss_notify_start_time },
3490 { JOB_NOTIFY_TYPE, JOB_NOTIFY_UNTIL_TIME, "JOB_NOTIFY_UNTIL_TIME", NOTIFY_ONE_VALUE, spoolss_notify_until_time },
3491 { JOB_NOTIFY_TYPE, JOB_NOTIFY_TIME, "JOB_NOTIFY_TIME", NOTIFY_ONE_VALUE, spoolss_notify_job_time },
3492 { JOB_NOTIFY_TYPE, JOB_NOTIFY_TOTAL_PAGES, "JOB_NOTIFY_TOTAL_PAGES", NOTIFY_ONE_VALUE, spoolss_notify_total_pages },
3493 { JOB_NOTIFY_TYPE, JOB_NOTIFY_PAGES_PRINTED, "JOB_NOTIFY_PAGES_PRINTED", NOTIFY_ONE_VALUE, spoolss_notify_pages_printed },
3494 { JOB_NOTIFY_TYPE, JOB_NOTIFY_TOTAL_BYTES, "JOB_NOTIFY_TOTAL_BYTES", NOTIFY_ONE_VALUE, spoolss_notify_job_size },
3495 { PRINT_TABLE_END, 0x0, NULL, 0x0, NULL },
3498 /*******************************************************************
3499 Return the size of info_data structure.
3500 ********************************************************************/
3502 static uint32 size_of_notify_info_data(uint16 type, uint16 field)
3506 for (i = 0; i < (sizeof(notify_info_data_table)/sizeof(struct s_notify_info_data_table)); i++) {
3507 if ( (notify_info_data_table[i].type == type)
3508 && (notify_info_data_table[i].field == field) ) {
3509 switch(notify_info_data_table[i].size) {
3510 case NOTIFY_ONE_VALUE:
3511 case NOTIFY_TWO_VALUE:
3516 /* The only pointer notify data I have seen on
3517 the wire is the submitted time and this has
3518 the notify size set to 4. -tpot */
3520 case NOTIFY_POINTER:
3523 case NOTIFY_SECDESC:
3529 DEBUG(5, ("invalid notify data type %d/%d\n", type, field));
3534 /*******************************************************************
3535 Return the type of notify_info_data.
3536 ********************************************************************/
3538 static uint32 type_of_notify_info_data(uint16 type, uint16 field)
3542 for (i = 0; i < (sizeof(notify_info_data_table)/sizeof(struct s_notify_info_data_table)); i++) {
3543 if (notify_info_data_table[i].type == type &&
3544 notify_info_data_table[i].field == field)
3545 return notify_info_data_table[i].size;
3551 /****************************************************************************
3552 ****************************************************************************/
3554 static bool search_notify(uint16 type, uint16 field, int *value)
3558 for (i = 0; notify_info_data_table[i].type != PRINT_TABLE_END; i++) {
3559 if (notify_info_data_table[i].type == type &&
3560 notify_info_data_table[i].field == field &&
3561 notify_info_data_table[i].fn != NULL) {
3570 /****************************************************************************
3571 ****************************************************************************/
3573 void construct_info_data(SPOOL_NOTIFY_INFO_DATA *info_data, uint16 type, uint16 field, int id)
3575 info_data->type = type;
3576 info_data->field = field;
3577 info_data->reserved = 0;
3579 info_data->size = size_of_notify_info_data(type, field);
3580 info_data->enc_type = type_of_notify_info_data(type, field);
3585 /*******************************************************************
3587 * fill a notify_info struct with info asked
3589 ********************************************************************/
3591 static bool construct_notify_printer_info(Printer_entry *print_hnd, SPOOL_NOTIFY_INFO *info, int
3592 snum, SPOOL_NOTIFY_OPTION_TYPE
3593 *option_type, uint32 id,
3594 TALLOC_CTX *mem_ctx)
3600 SPOOL_NOTIFY_INFO_DATA *current_data;
3601 NT_PRINTER_INFO_LEVEL *printer = NULL;
3602 print_queue_struct *queue=NULL;
3604 type=option_type->type;
3606 DEBUG(4,("construct_notify_printer_info: Notify type: [%s], number of notify info: [%d] on printer: [%s]\n",
3607 (option_type->type==PRINTER_NOTIFY_TYPE?"PRINTER_NOTIFY_TYPE":"JOB_NOTIFY_TYPE"),
3608 option_type->count, lp_servicename(snum)));
3610 if (!W_ERROR_IS_OK(get_a_printer(print_hnd, &printer, 2, lp_const_servicename(snum))))
3613 for(field_num=0; field_num<option_type->count; field_num++) {
3614 field = option_type->fields[field_num];
3616 DEBUG(4,("construct_notify_printer_info: notify [%d]: type [%x], field [%x]\n", field_num, type, field));
3618 if (!search_notify(type, field, &j) )
3621 if((info->data=SMB_REALLOC_ARRAY(info->data, SPOOL_NOTIFY_INFO_DATA, info->count+1)) == NULL) {
3622 DEBUG(2,("construct_notify_printer_info: failed to enlarge buffer info->data!\n"));
3623 free_a_printer(&printer, 2);
3627 current_data = &info->data[info->count];
3629 construct_info_data(current_data, type, field, id);
3631 DEBUG(10,("construct_notify_printer_info: calling [%s] snum=%d printername=[%s])\n",
3632 notify_info_data_table[j].name, snum, printer->info_2->printername ));
3634 notify_info_data_table[j].fn(snum, current_data, queue,
3640 free_a_printer(&printer, 2);
3644 /*******************************************************************
3646 * fill a notify_info struct with info asked
3648 ********************************************************************/
3650 static bool construct_notify_jobs_info(print_queue_struct *queue,
3651 SPOOL_NOTIFY_INFO *info,
3652 NT_PRINTER_INFO_LEVEL *printer,
3653 int snum, SPOOL_NOTIFY_OPTION_TYPE
3654 *option_type, uint32 id,
3655 TALLOC_CTX *mem_ctx)
3661 SPOOL_NOTIFY_INFO_DATA *current_data;
3663 DEBUG(4,("construct_notify_jobs_info\n"));
3665 type = option_type->type;
3667 DEBUGADD(4,("Notify type: [%s], number of notify info: [%d]\n",
3668 (option_type->type==PRINTER_NOTIFY_TYPE?"PRINTER_NOTIFY_TYPE":"JOB_NOTIFY_TYPE"),
3669 option_type->count));
3671 for(field_num=0; field_num<option_type->count; field_num++) {
3672 field = option_type->fields[field_num];
3674 if (!search_notify(type, field, &j) )
3677 if((info->data=SMB_REALLOC_ARRAY(info->data, SPOOL_NOTIFY_INFO_DATA, info->count+1)) == NULL) {
3678 DEBUG(2,("construct_notify_jobs_info: failed to enlarg buffer info->data!\n"));
3682 current_data=&(info->data[info->count]);
3684 construct_info_data(current_data, type, field, id);
3685 notify_info_data_table[j].fn(snum, current_data, queue,
3694 * JFM: The enumeration is not that simple, it's even non obvious.
3696 * let's take an example: I want to monitor the PRINTER SERVER for
3697 * the printer's name and the number of jobs currently queued.
3698 * So in the NOTIFY_OPTION, I have one NOTIFY_OPTION_TYPE structure.
3699 * Its type is PRINTER_NOTIFY_TYPE and it has 2 fields NAME and CJOBS.
3701 * I have 3 printers on the back of my server.
3703 * Now the response is a NOTIFY_INFO structure, with 6 NOTIFY_INFO_DATA
3706 * 1 printer 1 name 1
3707 * 2 printer 1 cjob 1
3708 * 3 printer 2 name 2
3709 * 4 printer 2 cjob 2
3710 * 5 printer 3 name 3
3711 * 6 printer 3 name 3
3713 * that's the print server case, the printer case is even worse.
3716 /*******************************************************************
3718 * enumerate all printers on the printserver
3719 * fill a notify_info struct with info asked
3721 ********************************************************************/
3723 static WERROR printserver_notify_info(pipes_struct *p, POLICY_HND *hnd,
3724 SPOOL_NOTIFY_INFO *info,
3725 TALLOC_CTX *mem_ctx)
3728 Printer_entry *Printer=find_printer_index_by_hnd(p, hnd);
3729 int n_services=lp_numservices();
3731 SPOOL_NOTIFY_OPTION *option;
3732 SPOOL_NOTIFY_OPTION_TYPE *option_type;
3734 DEBUG(4,("printserver_notify_info\n"));
3739 option=Printer->notify.option;
3744 /* a bug in xp sp2 rc2 causes it to send a fnpcn request without
3745 sending a ffpcn() request first */
3750 for (i=0; i<option->count; i++) {
3751 option_type=&(option->ctr.type[i]);
3753 if (option_type->type!=PRINTER_NOTIFY_TYPE)
3756 for (snum=0; snum<n_services; snum++)
3758 if ( lp_browseable(snum) && lp_snum_ok(snum) && lp_print_ok(snum) )
3759 construct_notify_printer_info ( Printer, info, snum, option_type, snum, mem_ctx );
3765 * Debugging information, don't delete.
3768 DEBUG(1,("dumping the NOTIFY_INFO\n"));
3769 DEBUGADD(1,("info->version:[%d], info->flags:[%d], info->count:[%d]\n", info->version, info->flags, info->count));
3770 DEBUGADD(1,("num\ttype\tfield\tres\tid\tsize\tenc_type\n"));
3772 for (i=0; i<info->count; i++) {
3773 DEBUGADD(1,("[%d]\t[%d]\t[%d]\t[%d]\t[%d]\t[%d]\t[%d]\n",
3774 i, info->data[i].type, info->data[i].field, info->data[i].reserved,
3775 info->data[i].id, info->data[i].size, info->data[i].enc_type));
3782 /*******************************************************************
3784 * fill a notify_info struct with info asked
3786 ********************************************************************/
3788 static WERROR printer_notify_info(pipes_struct *p, POLICY_HND *hnd, SPOOL_NOTIFY_INFO *info,
3789 TALLOC_CTX *mem_ctx)
3792 Printer_entry *Printer=find_printer_index_by_hnd(p, hnd);
3795 SPOOL_NOTIFY_OPTION *option;
3796 SPOOL_NOTIFY_OPTION_TYPE *option_type;
3798 print_queue_struct *queue=NULL;
3799 print_status_struct status;
3801 DEBUG(4,("printer_notify_info\n"));
3806 option=Printer->notify.option;
3812 /* a bug in xp sp2 rc2 causes it to send a fnpcn request without
3813 sending a ffpcn() request first */
3818 get_printer_snum(p, hnd, &snum, NULL);
3820 for (i=0; i<option->count; i++) {
3821 option_type=&option->ctr.type[i];
3823 switch ( option_type->type ) {
3824 case PRINTER_NOTIFY_TYPE:
3825 if(construct_notify_printer_info(Printer, info, snum,
3831 case JOB_NOTIFY_TYPE: {
3832 NT_PRINTER_INFO_LEVEL *printer = NULL;
3834 count = print_queue_status(snum, &queue, &status);
3836 if (!W_ERROR_IS_OK(get_a_printer(Printer, &printer, 2, lp_const_servicename(snum))))
3839 for (j=0; j<count; j++) {
3840 construct_notify_jobs_info(&queue[j], info,
3847 free_a_printer(&printer, 2);
3857 * Debugging information, don't delete.
3860 DEBUG(1,("dumping the NOTIFY_INFO\n"));
3861 DEBUGADD(1,("info->version:[%d], info->flags:[%d], info->count:[%d]\n", info->version, info->flags, info->count));
3862 DEBUGADD(1,("num\ttype\tfield\tres\tid\tsize\tenc_type\n"));
3864 for (i=0; i<info->count; i++) {
3865 DEBUGADD(1,("[%d]\t[%d]\t[%d]\t[%d]\t[%d]\t[%d]\t[%d]\n",
3866 i, info->data[i].type, info->data[i].field, info->data[i].reserved,
3867 info->data[i].id, info->data[i].size, info->data[i].enc_type));
3873 /********************************************************************
3875 ********************************************************************/
3877 WERROR _spoolss_rfnpcnex( pipes_struct *p, SPOOL_Q_RFNPCNEX *q_u, SPOOL_R_RFNPCNEX *r_u)
3879 POLICY_HND *handle = &q_u->handle;
3880 SPOOL_NOTIFY_INFO *info = &r_u->info;
3882 Printer_entry *Printer=find_printer_index_by_hnd(p, handle);
3883 WERROR result = WERR_BADFID;
3885 /* we always have a NOTIFY_INFO struct */
3889 DEBUG(2,("_spoolss_rfnpcnex: Invalid handle (%s:%u:%u).\n",
3890 OUR_HANDLE(handle)));
3894 DEBUG(4,("Printer type %x\n",Printer->printer_type));
3897 * We are now using the change value, and
3898 * I should check for PRINTER_NOTIFY_OPTIONS_REFRESH but as
3899 * I don't have a global notification system, I'm sending back all the
3900 * informations even when _NOTHING_ has changed.
3903 /* We need to keep track of the change value to send back in
3904 RRPCN replies otherwise our updates are ignored. */
3906 Printer->notify.fnpcn = True;
3908 if (Printer->notify.client_connected) {
3909 DEBUG(10,("_spoolss_rfnpcnex: Saving change value in request [%x]\n", q_u->change));
3910 Printer->notify.change = q_u->change;
3913 /* just ignore the SPOOL_NOTIFY_OPTION */
3915 switch (Printer->printer_type) {
3917 result = printserver_notify_info(p, handle, info, p->mem_ctx);
3920 case SPLHND_PRINTER:
3921 result = printer_notify_info(p, handle, info, p->mem_ctx);
3925 Printer->notify.fnpcn = False;
3931 /********************************************************************
3932 * construct_printer_info_0
3933 * fill a printer_info_0 struct
3934 ********************************************************************/
3936 static bool construct_printer_info_0(Printer_entry *print_hnd, PRINTER_INFO_0 *printer, int snum)
3938 char *chaine = NULL;
3940 NT_PRINTER_INFO_LEVEL *ntprinter = NULL;
3941 counter_printer_0 *session_counter;
3942 uint32 global_counter;
3945 print_status_struct status;
3946 TALLOC_CTX *ctx = talloc_tos();
3948 if (!W_ERROR_IS_OK(get_a_printer(print_hnd, &ntprinter, 2, lp_const_servicename(snum))))
3951 init_unistr(&printer->printername, ntprinter->info_2->printername);
3953 chaine = talloc_asprintf(ctx, "\\\\%s", get_server_name(print_hnd));
3955 free_a_printer(&ntprinter,2);
3959 count = print_queue_length(snum, &status);
3961 /* check if we already have a counter for this printer */
3962 for(session_counter = counter_list; session_counter; session_counter = session_counter->next) {
3963 if (session_counter->snum == snum)
3967 init_unistr(&printer->servername, chaine);
3969 /* it's the first time, add it to the list */
3970 if (session_counter==NULL) {
3971 if((session_counter=SMB_MALLOC_P(counter_printer_0)) == NULL) {
3972 free_a_printer(&ntprinter, 2);
3975 ZERO_STRUCTP(session_counter);
3976 session_counter->snum=snum;
3977 session_counter->counter=0;
3978 DLIST_ADD(counter_list, session_counter);
3982 session_counter->counter++;
3985 * the global_counter should be stored in a TDB as it's common to all the clients
3986 * and should be zeroed on samba startup
3988 global_counter=session_counter->counter;
3989 printer->cjobs = count;
3990 printer->total_jobs = 0;
3991 printer->total_bytes = 0;
3993 setuptime = (time_t)ntprinter->info_2->setuptime;
3994 t=gmtime(&setuptime);
3996 printer->year = t->tm_year+1900;
3997 printer->month = t->tm_mon+1;
3998 printer->dayofweek = t->tm_wday;
3999 printer->day = t->tm_mday;
4000 printer->hour = t->tm_hour;
4001 printer->minute = t->tm_min;
4002 printer->second = t->tm_sec;
4003 printer->milliseconds = 0;
4005 printer->global_counter = global_counter;
4006 printer->total_pages = 0;
4008 /* in 2.2 we reported ourselves as 0x0004 and 0x0565 */
4009 printer->major_version = 0x0005; /* NT 5 */
4010 printer->build_version = 0x0893; /* build 2195 */
4012 printer->unknown7 = 0x1;
4013 printer->unknown8 = 0x0;
4014 printer->unknown9 = 0x0;
4015 printer->session_counter = session_counter->counter;
4016 printer->unknown11 = 0x0;
4017 printer->printer_errors = 0x0; /* number of print failure */
4018 printer->unknown13 = 0x0;
4019 printer->unknown14 = 0x1;
4020 printer->unknown15 = 0x024a; /* 586 Pentium ? */
4021 printer->unknown16 = 0x0;
4022 printer->change_id = ntprinter->info_2->changeid; /* ChangeID in milliseconds*/
4023 printer->unknown18 = 0x0;
4024 printer->status = nt_printq_status(status.status);
4025 printer->unknown20 = 0x0;
4026 printer->c_setprinter = get_c_setprinter(); /* monotonically increasing sum of delta printer counts */
4027 printer->unknown22 = 0x0;
4028 printer->unknown23 = 0x6; /* 6 ???*/
4029 printer->unknown24 = 0; /* unknown 24 to 26 are always 0 */
4030 printer->unknown25 = 0;
4031 printer->unknown26 = 0;
4032 printer->unknown27 = 0;
4033 printer->unknown28 = 0;
4034 printer->unknown29 = 0;
4036 free_a_printer(&ntprinter,2);
4040 /********************************************************************
4041 * construct_printer_info_1
4042 * fill a printer_info_1 struct
4043 ********************************************************************/
4044 static bool construct_printer_info_1(Printer_entry *print_hnd, uint32 flags, PRINTER_INFO_1 *printer, int snum)
4046 char *chaine = NULL;
4047 NT_PRINTER_INFO_LEVEL *ntprinter = NULL;
4048 TALLOC_CTX *ctx = talloc_tos();
4050 if (!W_ERROR_IS_OK(get_a_printer(print_hnd, &ntprinter, 2, lp_const_servicename(snum))))
4053 printer->flags=flags;
4055 if (*ntprinter->info_2->comment == '\0') {
4056 init_unistr(&printer->comment, lp_comment(snum));
4057 chaine = talloc_asprintf(ctx,
4058 "%s,%s,%s", ntprinter->info_2->printername,
4059 ntprinter->info_2->drivername, lp_comment(snum));
4062 init_unistr(&printer->comment, ntprinter->info_2->comment); /* saved comment. */
4063 chaine = talloc_asprintf(ctx,
4064 "%s,%s,%s", ntprinter->info_2->printername,
4065 ntprinter->info_2->drivername, ntprinter->info_2->comment);
4069 free_a_printer(&ntprinter,2);
4073 init_unistr(&printer->description, chaine);
4074 init_unistr(&printer->name, ntprinter->info_2->printername);
4076 free_a_printer(&ntprinter,2);
4081 /****************************************************************************
4082 Free a DEVMODE struct.
4083 ****************************************************************************/
4085 static void free_dev_mode(DEVICEMODE *dev)
4090 SAFE_FREE(dev->dev_private);
4095 /****************************************************************************
4096 Convert an NT_DEVICEMODE to a DEVICEMODE structure. Both pointers
4097 should be valid upon entry
4098 ****************************************************************************/
4100 static bool convert_nt_devicemode( DEVICEMODE *devmode, NT_DEVICEMODE *ntdevmode )
4102 if ( !devmode || !ntdevmode )
4105 init_unistr(&devmode->devicename, ntdevmode->devicename);
4107 init_unistr(&devmode->formname, ntdevmode->formname);
4109 devmode->specversion = ntdevmode->specversion;
4110 devmode->driverversion = ntdevmode->driverversion;
4111 devmode->size = ntdevmode->size;
4112 devmode->driverextra = ntdevmode->driverextra;
4113 devmode->fields = ntdevmode->fields;
4115 devmode->orientation = ntdevmode->orientation;
4116 devmode->papersize = ntdevmode->papersize;
4117 devmode->paperlength = ntdevmode->paperlength;
4118 devmode->paperwidth = ntdevmode->paperwidth;
4119 devmode->scale = ntdevmode->scale;
4120 devmode->copies = ntdevmode->copies;
4121 devmode->defaultsource = ntdevmode->defaultsource;
4122 devmode->printquality = ntdevmode->printquality;
4123 devmode->color = ntdevmode->color;
4124 devmode->duplex = ntdevmode->duplex;
4125 devmode->yresolution = ntdevmode->yresolution;
4126 devmode->ttoption = ntdevmode->ttoption;
4127 devmode->collate = ntdevmode->collate;
4128 devmode->icmmethod = ntdevmode->icmmethod;
4129 devmode->icmintent = ntdevmode->icmintent;
4130 devmode->mediatype = ntdevmode->mediatype;
4131 devmode->dithertype = ntdevmode->dithertype;
4133 if (ntdevmode->nt_dev_private != NULL) {
4134 if ((devmode->dev_private=(uint8 *)memdup(ntdevmode->nt_dev_private, ntdevmode->driverextra)) == NULL)
4141 /****************************************************************************
4142 Create a DEVMODE struct. Returns malloced memory.
4143 ****************************************************************************/
4145 DEVICEMODE *construct_dev_mode(const char *servicename)
4147 NT_PRINTER_INFO_LEVEL *printer = NULL;
4148 DEVICEMODE *devmode = NULL;
4150 DEBUG(7,("construct_dev_mode\n"));
4152 DEBUGADD(8,("getting printer characteristics\n"));
4154 if (!W_ERROR_IS_OK(get_a_printer(NULL, &printer, 2, servicename)))
4157 if ( !printer->info_2->devmode ) {
4158 DEBUG(5, ("BONG! There was no device mode!\n"));
4162 if ((devmode = SMB_MALLOC_P(DEVICEMODE)) == NULL) {
4163 DEBUG(2,("construct_dev_mode: malloc fail.\n"));
4167 ZERO_STRUCTP(devmode);
4169 DEBUGADD(8,("loading DEVICEMODE\n"));
4171 if ( !convert_nt_devicemode( devmode, printer->info_2->devmode ) ) {
4172 free_dev_mode( devmode );
4177 free_a_printer(&printer,2);
4182 /********************************************************************
4183 * construct_printer_info_2
4184 * fill a printer_info_2 struct
4185 ********************************************************************/
4187 static bool construct_printer_info_2(Printer_entry *print_hnd, PRINTER_INFO_2 *printer, int snum)
4190 NT_PRINTER_INFO_LEVEL *ntprinter = NULL;
4192 print_status_struct status;
4194 if (!W_ERROR_IS_OK(get_a_printer(print_hnd, &ntprinter, 2, lp_const_servicename(snum))))
4197 count = print_queue_length(snum, &status);
4199 init_unistr(&printer->servername, ntprinter->info_2->servername); /* servername*/
4200 init_unistr(&printer->printername, ntprinter->info_2->printername); /* printername*/
4201 init_unistr(&printer->sharename, lp_servicename(snum)); /* sharename */
4202 init_unistr(&printer->portname, ntprinter->info_2->portname); /* port */
4203 init_unistr(&printer->drivername, ntprinter->info_2->drivername); /* drivername */
4205 if (*ntprinter->info_2->comment == '\0')
4206 init_unistr(&printer->comment, lp_comment(snum)); /* comment */
4208 init_unistr(&printer->comment, ntprinter->info_2->comment); /* saved comment. */
4210 init_unistr(&printer->location, ntprinter->info_2->location); /* location */
4211 init_unistr(&printer->sepfile, ntprinter->info_2->sepfile); /* separator file */
4212 init_unistr(&printer->printprocessor, ntprinter->info_2->printprocessor);/* print processor */
4213 init_unistr(&printer->datatype, ntprinter->info_2->datatype); /* datatype */
4214 init_unistr(&printer->parameters, ntprinter->info_2->parameters); /* parameters (of print processor) */
4216 printer->attributes = ntprinter->info_2->attributes;
4218 printer->priority = ntprinter->info_2->priority; /* priority */
4219 printer->defaultpriority = ntprinter->info_2->default_priority; /* default priority */
4220 printer->starttime = ntprinter->info_2->starttime; /* starttime */
4221 printer->untiltime = ntprinter->info_2->untiltime; /* untiltime */
4222 printer->status = nt_printq_status(status.status); /* status */
4223 printer->cjobs = count; /* jobs */
4224 printer->averageppm = ntprinter->info_2->averageppm; /* average pages per minute */
4226 if ( !(printer->devmode = construct_dev_mode(
4227 lp_const_servicename(snum))) )
4228 DEBUG(8, ("Returning NULL Devicemode!\n"));
4230 printer->secdesc = NULL;
4232 if ( ntprinter->info_2->secdesc_buf
4233 && ntprinter->info_2->secdesc_buf->sd_size != 0 )
4235 /* don't use talloc_steal() here unless you do a deep steal of all
4236 the SEC_DESC members */
4238 printer->secdesc = dup_sec_desc( talloc_tos(),
4239 ntprinter->info_2->secdesc_buf->sd );
4242 free_a_printer(&ntprinter, 2);
4247 /********************************************************************
4248 * construct_printer_info_3
4249 * fill a printer_info_3 struct
4250 ********************************************************************/
4252 static bool construct_printer_info_3(Printer_entry *print_hnd, PRINTER_INFO_3 **pp_printer, int snum)
4254 NT_PRINTER_INFO_LEVEL *ntprinter = NULL;
4255 PRINTER_INFO_3 *printer = NULL;
4257 if (!W_ERROR_IS_OK(get_a_printer(print_hnd, &ntprinter, 2, lp_const_servicename(snum))))
4261 if ((printer = SMB_MALLOC_P(PRINTER_INFO_3)) == NULL) {
4262 DEBUG(2,("construct_printer_info_3: malloc fail.\n"));
4263 free_a_printer(&ntprinter, 2);
4267 ZERO_STRUCTP(printer);
4269 /* These are the components of the SD we are returning. */
4271 if (ntprinter->info_2->secdesc_buf && ntprinter->info_2->secdesc_buf->sd_size != 0) {
4272 /* don't use talloc_steal() here unless you do a deep steal of all
4273 the SEC_DESC members */
4275 printer->secdesc = dup_sec_desc( talloc_tos(),
4276 ntprinter->info_2->secdesc_buf->sd );
4279 free_a_printer(&ntprinter, 2);
4281 *pp_printer = printer;
4285 /********************************************************************
4286 * construct_printer_info_4
4287 * fill a printer_info_4 struct
4288 ********************************************************************/
4290 static bool construct_printer_info_4(Printer_entry *print_hnd, PRINTER_INFO_4 *printer, int snum)
4292 NT_PRINTER_INFO_LEVEL *ntprinter = NULL;
4294 if (!W_ERROR_IS_OK(get_a_printer(print_hnd, &ntprinter, 2, lp_const_servicename(snum))))
4297 init_unistr(&printer->printername, ntprinter->info_2->printername); /* printername*/
4298 init_unistr(&printer->servername, ntprinter->info_2->servername); /* servername*/
4299 printer->attributes = ntprinter->info_2->attributes;
4301 free_a_printer(&ntprinter, 2);
4305 /********************************************************************
4306 * construct_printer_info_5
4307 * fill a printer_info_5 struct
4308 ********************************************************************/
4310 static bool construct_printer_info_5(Printer_entry *print_hnd, PRINTER_INFO_5 *printer, int snum)
4312 NT_PRINTER_INFO_LEVEL *ntprinter = NULL;
4314 if (!W_ERROR_IS_OK(get_a_printer(print_hnd, &ntprinter, 2, lp_const_servicename(snum))))
4317 init_unistr(&printer->printername, ntprinter->info_2->printername);
4318 init_unistr(&printer->portname, ntprinter->info_2->portname);
4319 printer->attributes = ntprinter->info_2->attributes;
4321 /* these two are not used by NT+ according to MSDN */
4323 printer->device_not_selected_timeout = 0x0; /* have seen 0x3a98 */
4324 printer->transmission_retry_timeout = 0x0; /* have seen 0xafc8 */
4326 free_a_printer(&ntprinter, 2);
4331 /********************************************************************
4332 * construct_printer_info_6
4333 * fill a printer_info_6 struct
4334 ********************************************************************/
4336 static bool construct_printer_info_6(Printer_entry *print_hnd,
4337 PRINTER_INFO_6 *printer,
4340 NT_PRINTER_INFO_LEVEL *ntprinter = NULL;
4342 print_status_struct status;
4344 if (!W_ERROR_IS_OK(get_a_printer(print_hnd, &ntprinter, 2,
4345 lp_const_servicename(snum))))
4348 count = print_queue_length(snum, &status);
4350 printer->status = nt_printq_status(status.status);
4352 free_a_printer(&ntprinter, 2);
4357 /********************************************************************
4358 * construct_printer_info_7
4359 * fill a printer_info_7 struct
4360 ********************************************************************/
4362 static bool construct_printer_info_7(Printer_entry *print_hnd, PRINTER_INFO_7 *printer, int snum)
4364 char *guid_str = NULL;
4367 if (is_printer_published(print_hnd, snum, &guid)) {
4368 if (asprintf(&guid_str, "{%s}",
4369 GUID_string(talloc_tos(), &guid)) == -1) {
4372 strupper_m(guid_str);
4373 init_unistr(&printer->guid, guid_str);
4374 SAFE_FREE(guid_str);
4375 printer->action = SPOOL_DS_PUBLISH;
4377 init_unistr(&printer->guid, "");
4378 printer->action = SPOOL_DS_UNPUBLISH;
4384 /********************************************************************
4385 Spoolss_enumprinters.
4386 ********************************************************************/
4388 static WERROR enum_all_printers_info_1(uint32 flags, RPC_BUFFER *buffer, uint32 offered, uint32 *needed, uint32 *returned)
4392 int n_services=lp_numservices();
4393 PRINTER_INFO_1 *printers=NULL;
4394 PRINTER_INFO_1 current_prt;
4395 WERROR result = WERR_OK;
4397 DEBUG(4,("enum_all_printers_info_1\n"));
4399 for (snum=0; snum<n_services; snum++) {
4400 if (lp_browseable(snum) && lp_snum_ok(snum) && lp_print_ok(snum) ) {
4401 DEBUG(4,("Found a printer in smb.conf: %s[%x]\n", lp_servicename(snum), snum));
4403 if (construct_printer_info_1(NULL, flags, ¤t_prt, snum)) {
4404 if((printers=SMB_REALLOC_ARRAY(printers, PRINTER_INFO_1, *returned +1)) == NULL) {
4405 DEBUG(2,("enum_all_printers_info_1: failed to enlarge printers buffer!\n"));
4409 DEBUG(4,("ReAlloced memory for [%d] PRINTER_INFO_1\n", *returned));
4411 memcpy(&printers[*returned], ¤t_prt, sizeof(PRINTER_INFO_1));
4417 /* check the required size. */
4418 for (i=0; i<*returned; i++)
4419 (*needed) += spoolss_size_printer_info_1(&printers[i]);
4421 if (*needed > offered) {
4422 result = WERR_INSUFFICIENT_BUFFER;
4426 if (!rpcbuf_alloc_size(buffer, *needed)) {
4427 result = WERR_NOMEM;
4431 /* fill the buffer with the structures */
4432 for (i=0; i<*returned; i++)
4433 smb_io_printer_info_1("", buffer, &printers[i], 0);
4438 SAFE_FREE(printers);
4440 if ( !W_ERROR_IS_OK(result) )
4446 /********************************************************************
4447 enum_all_printers_info_1_local.
4448 *********************************************************************/
4450 static WERROR enum_all_printers_info_1_local(RPC_BUFFER *buffer, uint32 offered, uint32 *needed, uint32 *returned)
4452 DEBUG(4,("enum_all_printers_info_1_local\n"));
4454 return enum_all_printers_info_1(PRINTER_ENUM_ICON8, buffer, offered, needed, returned);
4457 /********************************************************************
4458 enum_all_printers_info_1_name.
4459 *********************************************************************/
4461 static WERROR enum_all_printers_info_1_name(fstring name, RPC_BUFFER *buffer, uint32 offered, uint32 *needed, uint32 *returned)
4465 DEBUG(4,("enum_all_printers_info_1_name\n"));
4467 if ((name[0] == '\\') && (name[1] == '\\'))
4470 if (is_myname_or_ipaddr(s)) {
4471 return enum_all_printers_info_1(PRINTER_ENUM_ICON8, buffer, offered, needed, returned);
4474 return WERR_INVALID_NAME;
4477 #if 0 /* JERRY -- disabled for now. Don't think this is used, tested, or correct */
4478 /********************************************************************
4479 enum_all_printers_info_1_remote.
4480 *********************************************************************/
4482 static WERROR enum_all_printers_info_1_remote(fstring name, RPC_BUFFER *buffer, uint32 offered, uint32 *needed, uint32 *returned)
4484 PRINTER_INFO_1 *printer;
4485 fstring printername;
4488 DEBUG(4,("enum_all_printers_info_1_remote\n"));
4489 WERROR result = WERR_OK;
4491 /* JFM: currently it's more a place holder than anything else.
4492 * In the spooler world there is a notion of server registration.
4493 * the print servers are registered on the PDC (in the same domain)
4495 * We should have a TDB here. The registration is done thru an
4496 * undocumented RPC call.
4499 if((printer=SMB_MALLOC_P(PRINTER_INFO_1)) == NULL)
4504 slprintf(printername, sizeof(printername)-1,"Windows NT Remote Printers!!\\\\%s", name);
4505 slprintf(desc, sizeof(desc)-1,"%s", name);
4506 slprintf(comment, sizeof(comment)-1, "Logged on Domain");
4508 init_unistr(&printer->description, desc);
4509 init_unistr(&printer->name, printername);
4510 init_unistr(&printer->comment, comment);
4511 printer->flags=PRINTER_ENUM_ICON3|PRINTER_ENUM_CONTAINER;
4513 /* check the required size. */
4514 *needed += spoolss_size_printer_info_1(printer);
4516 if (*needed > offered) {
4517 result = WERR_INSUFFICIENT_BUFFER;
4521 if (!rpcbuf_alloc_size(buffer, *needed)) {
4522 result = WERR_NOMEM;
4526 /* fill the buffer with the structures */
4527 smb_io_printer_info_1("", buffer, printer, 0);
4533 if ( !W_ERROR_IS_OK(result) )
4541 /********************************************************************
4542 enum_all_printers_info_1_network.
4543 *********************************************************************/
4545 static WERROR enum_all_printers_info_1_network(fstring name, RPC_BUFFER *buffer, uint32 offered, uint32 *needed, uint32 *returned)
4549 DEBUG(4,("enum_all_printers_info_1_network\n"));
4551 /* If we respond to a enum_printers level 1 on our name with flags
4552 set to PRINTER_ENUM_REMOTE with a list of printers then these
4553 printers incorrectly appear in the APW browse list.
4554 Specifically the printers for the server appear at the workgroup
4555 level where all the other servers in the domain are
4556 listed. Windows responds to this call with a
4557 WERR_CAN_NOT_COMPLETE so we should do the same. */
4559 if (name[0] == '\\' && name[1] == '\\')
4562 if (is_myname_or_ipaddr(s))
4563 return WERR_CAN_NOT_COMPLETE;
4565 return enum_all_printers_info_1(PRINTER_ENUM_UNKNOWN_8, buffer, offered, needed, returned);
4568 /********************************************************************
4569 * api_spoolss_enumprinters
4571 * called from api_spoolss_enumprinters (see this to understand)
4572 ********************************************************************/
4574 static WERROR enum_all_printers_info_2(RPC_BUFFER *buffer, uint32 offered, uint32 *needed, uint32 *returned)
4578 int n_services=lp_numservices();
4579 PRINTER_INFO_2 *printers=NULL;
4580 PRINTER_INFO_2 current_prt;
4581 WERROR result = WERR_OK;
4585 for (snum=0; snum<n_services; snum++) {
4586 if (lp_browseable(snum) && lp_snum_ok(snum) && lp_print_ok(snum) ) {
4587 DEBUG(4,("Found a printer in smb.conf: %s[%x]\n", lp_servicename(snum), snum));
4589 if (construct_printer_info_2(NULL, ¤t_prt, snum)) {
4590 if ( !(printers=SMB_REALLOC_ARRAY(printers, PRINTER_INFO_2, *returned +1)) ) {
4591 DEBUG(2,("enum_all_printers_info_2: failed to enlarge printers buffer!\n"));
4596 DEBUG(4,("ReAlloced memory for [%d] PRINTER_INFO_2\n", *returned + 1));
4598 memcpy(&printers[*returned], ¤t_prt, sizeof(PRINTER_INFO_2));
4605 /* check the required size. */
4606 for (i=0; i<*returned; i++)
4607 (*needed) += spoolss_size_printer_info_2(&printers[i]);
4609 if (*needed > offered) {
4610 result = WERR_INSUFFICIENT_BUFFER;
4614 if (!rpcbuf_alloc_size(buffer, *needed)) {
4615 result = WERR_NOMEM;
4619 /* fill the buffer with the structures */
4620 for (i=0; i<*returned; i++)
4621 smb_io_printer_info_2("", buffer, &(printers[i]), 0);
4626 for (i=0; i<*returned; i++)
4627 free_devmode(printers[i].devmode);
4629 SAFE_FREE(printers);
4631 if ( !W_ERROR_IS_OK(result) )
4637 /********************************************************************
4638 * handle enumeration of printers at level 1
4639 ********************************************************************/
4641 static WERROR enumprinters_level1( uint32 flags, fstring name,
4642 RPC_BUFFER *buffer, uint32 offered,
4643 uint32 *needed, uint32 *returned)
4645 /* Not all the flags are equals */
4647 if (flags & PRINTER_ENUM_LOCAL)
4648 return enum_all_printers_info_1_local(buffer, offered, needed, returned);
4650 if (flags & PRINTER_ENUM_NAME)
4651 return enum_all_printers_info_1_name(name, buffer, offered, needed, returned);
4653 #if 0 /* JERRY - disabled for now */
4654 if (flags & PRINTER_ENUM_REMOTE)
4655 return enum_all_printers_info_1_remote(name, buffer, offered, needed, returned);
4658 if (flags & PRINTER_ENUM_NETWORK)
4659 return enum_all_printers_info_1_network(name, buffer, offered, needed, returned);
4661 return WERR_OK; /* NT4sp5 does that */
4664 /********************************************************************
4665 * handle enumeration of printers at level 2
4666 ********************************************************************/
4668 static WERROR enumprinters_level2( uint32 flags, const char *servername,
4669 RPC_BUFFER *buffer, uint32 offered,
4670 uint32 *needed, uint32 *returned)
4672 if (flags & PRINTER_ENUM_LOCAL) {
4673 return enum_all_printers_info_2(buffer, offered, needed, returned);
4676 if (flags & PRINTER_ENUM_NAME) {
4677 if (is_myname_or_ipaddr(canon_servername(servername)))
4678 return enum_all_printers_info_2(buffer, offered, needed, returned);
4680 return WERR_INVALID_NAME;
4683 if (flags & PRINTER_ENUM_REMOTE)
4684 return WERR_UNKNOWN_LEVEL;
4689 /********************************************************************
4690 * handle enumeration of printers at level 5
4691 ********************************************************************/
4693 static WERROR enumprinters_level5( uint32 flags, const char *servername,
4694 RPC_BUFFER *buffer, uint32 offered,
4695 uint32 *needed, uint32 *returned)
4697 /* return enum_all_printers_info_5(buffer, offered, needed, returned);*/
4701 /********************************************************************
4702 * api_spoolss_enumprinters
4704 * called from api_spoolss_enumprinters (see this to understand)
4705 ********************************************************************/
4707 WERROR _spoolss_enumprinters( pipes_struct *p, SPOOL_Q_ENUMPRINTERS *q_u, SPOOL_R_ENUMPRINTERS *r_u)
4709 uint32 flags = q_u->flags;
4710 UNISTR2 *servername = &q_u->servername;
4711 uint32 level = q_u->level;
4712 RPC_BUFFER *buffer = NULL;
4713 uint32 offered = q_u->offered;
4714 uint32 *needed = &r_u->needed;
4715 uint32 *returned = &r_u->returned;
4719 /* that's an [in out] buffer */
4721 if (!q_u->buffer && (offered!=0)) {
4722 return WERR_INVALID_PARAM;
4725 rpcbuf_move(q_u->buffer, &r_u->buffer);
4726 buffer = r_u->buffer;
4728 DEBUG(4,("_spoolss_enumprinters\n"));
4735 * flags==PRINTER_ENUM_NAME
4736 * if name=="" then enumerates all printers
4737 * if name!="" then enumerate the printer
4738 * flags==PRINTER_ENUM_REMOTE
4739 * name is NULL, enumerate printers
4740 * Level 2: name!="" enumerates printers, name can't be NULL
4741 * Level 3: doesn't exist
4742 * Level 4: does a local registry lookup
4743 * Level 5: same as Level 2
4746 unistr2_to_ascii(name, servername, sizeof(name));
4751 return enumprinters_level1(flags, name, buffer, offered, needed, returned);
4753 return enumprinters_level2(flags, name, buffer, offered, needed, returned);
4755 return enumprinters_level5(flags, name, buffer, offered, needed, returned);
4760 return WERR_UNKNOWN_LEVEL;
4763 /****************************************************************************
4764 ****************************************************************************/
4766 static WERROR getprinter_level_0(Printer_entry *print_hnd, int snum, RPC_BUFFER *buffer, uint32 offered, uint32 *needed)
4768 PRINTER_INFO_0 *printer=NULL;
4769 WERROR result = WERR_OK;
4771 if((printer=SMB_MALLOC_P(PRINTER_INFO_0)) == NULL)
4774 construct_printer_info_0(print_hnd, printer, snum);
4776 /* check the required size. */
4777 *needed += spoolss_size_printer_info_0(printer);
4779 if (*needed > offered) {
4780 result = WERR_INSUFFICIENT_BUFFER;
4784 if (!rpcbuf_alloc_size(buffer, *needed)) {
4785 result = WERR_NOMEM;
4789 /* fill the buffer with the structures */
4790 smb_io_printer_info_0("", buffer, printer, 0);
4800 /****************************************************************************
4801 ****************************************************************************/
4803 static WERROR getprinter_level_1(Printer_entry *print_hnd, int snum, RPC_BUFFER *buffer, uint32 offered, uint32 *needed)
4805 PRINTER_INFO_1 *printer=NULL;
4806 WERROR result = WERR_OK;
4808 if((printer=SMB_MALLOC_P(PRINTER_INFO_1)) == NULL)
4811 construct_printer_info_1(print_hnd, PRINTER_ENUM_ICON8, printer, snum);
4813 /* check the required size. */
4814 *needed += spoolss_size_printer_info_1(printer);
4816 if (*needed > offered) {
4817 result = WERR_INSUFFICIENT_BUFFER;
4821 if (!rpcbuf_alloc_size(buffer, *needed)) {
4822 result = WERR_NOMEM;
4826 /* fill the buffer with the structures */
4827 smb_io_printer_info_1("", buffer, printer, 0);
4836 /****************************************************************************
4837 ****************************************************************************/
4839 static WERROR getprinter_level_2(Printer_entry *print_hnd, int snum, RPC_BUFFER *buffer, uint32 offered, uint32 *needed)
4841 PRINTER_INFO_2 *printer=NULL;
4842 WERROR result = WERR_OK;
4844 if((printer=SMB_MALLOC_P(PRINTER_INFO_2))==NULL)
4847 construct_printer_info_2(print_hnd, printer, snum);
4849 /* check the required size. */
4850 *needed += spoolss_size_printer_info_2(printer);
4852 if (*needed > offered) {
4853 result = WERR_INSUFFICIENT_BUFFER;
4857 if (!rpcbuf_alloc_size(buffer, *needed)) {
4858 result = WERR_NOMEM;
4862 /* fill the buffer with the structures */
4863 if (!smb_io_printer_info_2("", buffer, printer, 0))
4864 result = WERR_NOMEM;
4868 free_printer_info_2(printer);
4873 /****************************************************************************
4874 ****************************************************************************/
4876 static WERROR getprinter_level_3(Printer_entry *print_hnd, int snum, RPC_BUFFER *buffer, uint32 offered, uint32 *needed)
4878 PRINTER_INFO_3 *printer=NULL;
4879 WERROR result = WERR_OK;
4881 if (!construct_printer_info_3(print_hnd, &printer, snum))
4884 /* check the required size. */
4885 *needed += spoolss_size_printer_info_3(printer);
4887 if (*needed > offered) {
4888 result = WERR_INSUFFICIENT_BUFFER;
4892 if (!rpcbuf_alloc_size(buffer, *needed)) {
4893 result = WERR_NOMEM;
4897 /* fill the buffer with the structures */
4898 smb_io_printer_info_3("", buffer, printer, 0);
4902 free_printer_info_3(printer);
4907 /****************************************************************************
4908 ****************************************************************************/
4910 static WERROR getprinter_level_4(Printer_entry *print_hnd, int snum, RPC_BUFFER *buffer, uint32 offered, uint32 *needed)
4912 PRINTER_INFO_4 *printer=NULL;
4913 WERROR result = WERR_OK;
4915 if((printer=SMB_MALLOC_P(PRINTER_INFO_4))==NULL)
4918 if (!construct_printer_info_4(print_hnd, printer, snum)) {
4923 /* check the required size. */
4924 *needed += spoolss_size_printer_info_4(printer);
4926 if (*needed > offered) {
4927 result = WERR_INSUFFICIENT_BUFFER;
4931 if (!rpcbuf_alloc_size(buffer, *needed)) {
4932 result = WERR_NOMEM;
4936 /* fill the buffer with the structures */
4937 smb_io_printer_info_4("", buffer, printer, 0);
4941 free_printer_info_4(printer);
4946 /****************************************************************************
4947 ****************************************************************************/
4949 static WERROR getprinter_level_5(Printer_entry *print_hnd, int snum, RPC_BUFFER *buffer, uint32 offered, uint32 *needed)
4951 PRINTER_INFO_5 *printer=NULL;
4952 WERROR result = WERR_OK;
4954 if((printer=SMB_MALLOC_P(PRINTER_INFO_5))==NULL)
4957 if (!construct_printer_info_5(print_hnd, printer, snum)) {
4958 free_printer_info_5(printer);
4962 /* check the required size. */
4963 *needed += spoolss_size_printer_info_5(printer);
4965 if (*needed > offered) {
4966 result = WERR_INSUFFICIENT_BUFFER;
4970 if (!rpcbuf_alloc_size(buffer, *needed)) {
4971 result = WERR_NOMEM;
4975 /* fill the buffer with the structures */
4976 smb_io_printer_info_5("", buffer, printer, 0);
4980 free_printer_info_5(printer);
4985 static WERROR getprinter_level_6(Printer_entry *print_hnd,
4987 RPC_BUFFER *buffer, uint32 offered,
4990 PRINTER_INFO_6 *printer;
4991 WERROR result = WERR_OK;
4993 if ((printer = SMB_MALLOC_P(PRINTER_INFO_6)) == NULL) {
4997 if (!construct_printer_info_6(print_hnd, printer, snum)) {
4998 free_printer_info_6(printer);
5002 /* check the required size. */
5003 *needed += spoolss_size_printer_info_6(printer);
5005 if (*needed > offered) {
5006 result = WERR_INSUFFICIENT_BUFFER;
5010 if (!rpcbuf_alloc_size(buffer, *needed)) {
5011 result = WERR_NOMEM;
5015 /* fill the buffer with the structures */
5016 smb_io_printer_info_6("", buffer, printer, 0);
5020 free_printer_info_6(printer);
5025 static WERROR getprinter_level_7(Printer_entry *print_hnd, int snum, RPC_BUFFER *buffer, uint32 offered, uint32 *needed)
5027 PRINTER_INFO_7 *printer=NULL;
5028 WERROR result = WERR_OK;
5030 if((printer=SMB_MALLOC_P(PRINTER_INFO_7))==NULL)
5033 if (!construct_printer_info_7(print_hnd, printer, snum)) {
5034 result = WERR_NOMEM;
5038 /* check the required size. */
5039 *needed += spoolss_size_printer_info_7(printer);
5041 if (*needed > offered) {
5042 result = WERR_INSUFFICIENT_BUFFER;
5046 if (!rpcbuf_alloc_size(buffer, *needed)) {
5047 result = WERR_NOMEM;
5052 /* fill the buffer with the structures */
5053 smb_io_printer_info_7("", buffer, printer, 0);
5057 free_printer_info_7(printer);
5062 /****************************************************************************
5063 ****************************************************************************/
5065 WERROR _spoolss_getprinter(pipes_struct *p, SPOOL_Q_GETPRINTER *q_u, SPOOL_R_GETPRINTER *r_u)
5067 POLICY_HND *handle = &q_u->handle;
5068 uint32 level = q_u->level;
5069 RPC_BUFFER *buffer = NULL;
5070 uint32 offered = q_u->offered;
5071 uint32 *needed = &r_u->needed;
5072 Printer_entry *Printer=find_printer_index_by_hnd(p, handle);
5076 /* that's an [in out] buffer */
5078 if (!q_u->buffer && (offered!=0)) {
5079 return WERR_INVALID_PARAM;
5082 rpcbuf_move(q_u->buffer, &r_u->buffer);
5083 buffer = r_u->buffer;
5087 if (!get_printer_snum(p, handle, &snum, NULL))
5092 return getprinter_level_0(Printer, snum, buffer, offered, needed);
5094 return getprinter_level_1(Printer, snum, buffer, offered, needed);
5096 return getprinter_level_2(Printer, snum, buffer, offered, needed);
5098 return getprinter_level_3(Printer, snum, buffer, offered, needed);
5100 return getprinter_level_4(Printer, snum, buffer, offered, needed);
5102 return getprinter_level_5(Printer, snum, buffer, offered, needed);
5104 return getprinter_level_6(Printer, snum, buffer, offered, needed);
5106 return getprinter_level_7(Printer, snum, buffer, offered, needed);
5108 return WERR_UNKNOWN_LEVEL;
5111 /********************************************************************
5112 * fill a DRIVER_INFO_1 struct
5113 ********************************************************************/
5115 static void fill_printer_driver_info_1(DRIVER_INFO_1 *info, NT_PRINTER_DRIVER_INFO_LEVEL driver, const char *servername, fstring architecture)
5117 init_unistr( &info->name, driver.info_3->name);
5120 /********************************************************************
5121 * construct_printer_driver_info_1
5122 ********************************************************************/
5124 static WERROR construct_printer_driver_info_1(DRIVER_INFO_1 *info, int snum, const char *servername, fstring architecture, uint32 version)
5126 NT_PRINTER_INFO_LEVEL *printer = NULL;
5127 NT_PRINTER_DRIVER_INFO_LEVEL driver;
5129 ZERO_STRUCT(driver);
5131 if (!W_ERROR_IS_OK(get_a_printer(NULL, &printer, 2, lp_const_servicename(snum))))
5132 return WERR_INVALID_PRINTER_NAME;
5134 if (!W_ERROR_IS_OK(get_a_printer_driver(&driver, 3, printer->info_2->drivername, architecture, version))) {
5135 free_a_printer(&printer, 2);
5136 return WERR_UNKNOWN_PRINTER_DRIVER;
5139 fill_printer_driver_info_1(info, driver, servername, architecture);
5141 free_a_printer(&printer,2);
5146 /********************************************************************
5147 * construct_printer_driver_info_2
5148 * fill a printer_info_2 struct
5149 ********************************************************************/
5151 static void fill_printer_driver_info_2(DRIVER_INFO_2 *info, NT_PRINTER_DRIVER_INFO_LEVEL driver, const char *servername)
5153 TALLOC_CTX *ctx = talloc_tos();
5155 const char *cservername = canon_servername(servername);
5157 info->version=driver.info_3->cversion;
5159 init_unistr( &info->name, driver.info_3->name );
5160 init_unistr( &info->architecture, driver.info_3->environment );
5162 if (strlen(driver.info_3->driverpath)) {
5163 temp = talloc_asprintf(ctx,
5166 driver.info_3->driverpath);
5167 init_unistr( &info->driverpath, temp );
5169 init_unistr( &info->driverpath, "" );
5173 if (strlen(driver.info_3->datafile)) {
5174 temp = talloc_asprintf(ctx,
5177 driver.info_3->datafile);
5178 init_unistr( &info->datafile, temp );
5180 init_unistr( &info->datafile, "" );
5183 if (strlen(driver.info_3->configfile)) {
5184 temp = talloc_asprintf(ctx,
5187 driver.info_3->configfile);
5188 init_unistr( &info->configfile, temp );
5190 init_unistr( &info->configfile, "" );
5193 /********************************************************************
5194 * construct_printer_driver_info_2
5195 * fill a printer_info_2 struct
5196 ********************************************************************/
5198 static WERROR construct_printer_driver_info_2(DRIVER_INFO_2 *info, int snum, const char *servername, fstring architecture, uint32 version)
5200 NT_PRINTER_INFO_LEVEL *printer = NULL;
5201 NT_PRINTER_DRIVER_INFO_LEVEL driver;
5203 ZERO_STRUCT(printer);
5204 ZERO_STRUCT(driver);
5206 if (!W_ERROR_IS_OK(get_a_printer(NULL, &printer, 2, lp_const_servicename(snum))))
5207 return WERR_INVALID_PRINTER_NAME;
5209 if (!W_ERROR_IS_OK(get_a_printer_driver(&driver, 3, printer->info_2->drivername, architecture, version))) {
5210 free_a_printer(&printer, 2);
5211 return WERR_UNKNOWN_PRINTER_DRIVER;
5214 fill_printer_driver_info_2(info, driver, servername);
5216 free_a_printer(&printer,2);
5221 /********************************************************************
5222 * copy a strings array and convert to UNICODE
5224 * convert an array of ascii string to a UNICODE string
5225 ********************************************************************/
5227 static uint32 init_unistr_array(uint16 **uni_array, fstring *char_array, const char *servername)
5233 TALLOC_CTX *ctx = talloc_tos();
5235 DEBUG(6,("init_unistr_array\n"));
5239 if ( !char_array ) {
5244 v = ""; /* hack to handle null lists */
5247 /* hack to allow this to be used in places other than when generating
5248 the list of dependent files */
5252 line = talloc_asprintf(ctx,
5254 canon_servername(servername),
5257 line = talloc_strdup(ctx, v);
5261 SAFE_FREE(*uni_array);
5264 DEBUGADD(6,("%d:%s:%lu\n", i, line, (unsigned long)strlen(line)));
5266 /* add one extra unit16 for the second terminating NULL */
5268 if ( (*uni_array=SMB_REALLOC_ARRAY(*uni_array, uint16, j+1+strlen(line)+2)) == NULL ) {
5269 DEBUG(2,("init_unistr_array: Realloc error\n" ));
5276 j += (rpcstr_push((*uni_array+j), line, sizeof(uint16)*strlen(line)+2, STR_TERMINATE) / sizeof(uint16));
5281 /* special case for ""; we need to add both NULL's here */
5283 (*uni_array)[j++]=0x0000;
5284 (*uni_array)[j]=0x0000;
5287 DEBUGADD(6,("last one:done\n"));
5289 /* return size of array in uint16's */
5294 /********************************************************************
5295 * construct_printer_info_3
5296 * fill a printer_info_3 struct
5297 ********************************************************************/
5299 static void fill_printer_driver_info_3(DRIVER_INFO_3 *info, NT_PRINTER_DRIVER_INFO_LEVEL driver, const char *servername)
5302 TALLOC_CTX *ctx = talloc_tos();
5303 const char *cservername = canon_servername(servername);
5307 info->version=driver.info_3->cversion;
5309 init_unistr( &info->name, driver.info_3->name );
5310 init_unistr( &info->architecture, driver.info_3->environment );
5312 if (strlen(driver.info_3->driverpath)) {
5313 temp = talloc_asprintf(ctx,
5316 driver.info_3->driverpath);
5317 init_unistr( &info->driverpath, temp );
5319 init_unistr( &info->driverpath, "" );
5322 if (strlen(driver.info_3->datafile)) {
5323 temp = talloc_asprintf(ctx,
5326 driver.info_3->datafile);
5327 init_unistr( &info->datafile, temp );
5329 init_unistr( &info->datafile, "" );
5332 if (strlen(driver.info_3->configfile)) {
5333 temp = talloc_asprintf(ctx,
5336 driver.info_3->configfile);
5337 init_unistr( &info->configfile, temp );
5339 init_unistr( &info->configfile, "" );
5342 if (strlen(driver.info_3->helpfile)) {
5343 temp = talloc_asprintf(ctx,
5346 driver.info_3->helpfile);
5347 init_unistr( &info->helpfile, temp );
5349 init_unistr( &info->helpfile, "" );
5352 init_unistr( &info->monitorname, driver.info_3->monitorname );
5353 init_unistr( &info->defaultdatatype, driver.info_3->defaultdatatype );
5355 info->dependentfiles=NULL;
5356 init_unistr_array(&info->dependentfiles, driver.info_3->dependentfiles, cservername);
5359 /********************************************************************
5360 * construct_printer_info_3
5361 * fill a printer_info_3 struct
5362 ********************************************************************/
5364 static WERROR construct_printer_driver_info_3(DRIVER_INFO_3 *info, int snum, const char *servername, fstring architecture, uint32 version)
5366 NT_PRINTER_INFO_LEVEL *printer = NULL;
5367 NT_PRINTER_DRIVER_INFO_LEVEL driver;
5369 ZERO_STRUCT(driver);
5371 status=get_a_printer(NULL, &printer, 2, lp_const_servicename(snum) );
5372 DEBUG(8,("construct_printer_driver_info_3: status: %s\n", win_errstr(status)));
5373 if (!W_ERROR_IS_OK(status))
5374 return WERR_INVALID_PRINTER_NAME;
5376 status=get_a_printer_driver(&driver, 3, printer->info_2->drivername, architecture, version);
5377 DEBUG(8,("construct_printer_driver_info_3: status: %s\n", win_errstr(status)));
5382 * I put this code in during testing. Helpful when commenting out the
5383 * support for DRIVER_INFO_6 in regards to win2k. Not needed in general
5384 * as win2k always queries the driver using an infor level of 6.
5385 * I've left it in (but ifdef'd out) because I'll probably
5386 * use it in experimentation again in the future. --jerry 22/01/2002
5389 if (!W_ERROR_IS_OK(status)) {
5391 * Is this a W2k client ?
5394 /* Yes - try again with a WinNT driver. */
5396 status=get_a_printer_driver(&driver, 3, printer->info_2->drivername, architecture, version);
5397 DEBUG(8,("construct_printer_driver_info_3: status: %s\n", win_errstr(status)));
5401 if (!W_ERROR_IS_OK(status)) {
5402 free_a_printer(&printer,2);
5403 return WERR_UNKNOWN_PRINTER_DRIVER;
5411 fill_printer_driver_info_3(info, driver, servername);
5413 free_a_printer(&printer,2);
5418 /********************************************************************
5419 * construct_printer_info_6
5420 * fill a printer_info_6 struct - we know that driver is really level 3. This sucks. JRA.
5421 ********************************************************************/
5423 static void fill_printer_driver_info_6(DRIVER_INFO_6 *info, NT_PRINTER_DRIVER_INFO_LEVEL driver, const char *servername)
5427 TALLOC_CTX *ctx = talloc_tos();
5428 const char *cservername = canon_servername(servername);
5431 memset(&nullstr, '\0', sizeof(fstring));
5433 info->version=driver.info_3->cversion;
5435 init_unistr( &info->name, driver.info_3->name );
5436 init_unistr( &info->architecture, driver.info_3->environment );
5438 if (strlen(driver.info_3->driverpath)) {
5439 temp = talloc_asprintf(ctx,
5442 driver.info_3->driverpath);
5443 init_unistr( &info->driverpath, temp );
5445 init_unistr( &info->driverpath, "" );
5448 if (strlen(driver.info_3->datafile)) {
5449 temp = talloc_asprintf(ctx,
5452 driver.info_3->datafile);
5453 init_unistr( &info->datafile, temp );
5455 init_unistr( &info->datafile, "" );
5458 if (strlen(driver.info_3->configfile)) {
5459 temp = talloc_asprintf(ctx,
5462 driver.info_3->configfile);
5463 init_unistr( &info->configfile, temp );
5465 init_unistr( &info->configfile, "" );
5468 if (strlen(driver.info_3->helpfile)) {
5469 temp = talloc_asprintf(ctx,
5472 driver.info_3->helpfile);
5473 init_unistr( &info->helpfile, temp );
5475 init_unistr( &info->helpfile, "" );
5478 init_unistr( &info->monitorname, driver.info_3->monitorname );
5479 init_unistr( &info->defaultdatatype, driver.info_3->defaultdatatype );
5481 info->dependentfiles = NULL;
5482 init_unistr_array( &info->dependentfiles, driver.info_3->dependentfiles, servername );
5484 info->previousdrivernames=NULL;
5485 init_unistr_array(&info->previousdrivernames, &nullstr, servername);
5487 info->driver_date=0;
5490 info->driver_version_low=0;
5491 info->driver_version_high=0;
5493 init_unistr( &info->mfgname, "");
5494 init_unistr( &info->oem_url, "");
5495 init_unistr( &info->hardware_id, "");
5496 init_unistr( &info->provider, "");
5499 /********************************************************************
5500 * construct_printer_info_6
5501 * fill a printer_info_6 struct
5502 ********************************************************************/
5504 static WERROR construct_printer_driver_info_6(DRIVER_INFO_6 *info, int snum,
5505 const char *servername, fstring architecture, uint32 version)
5507 NT_PRINTER_INFO_LEVEL *printer = NULL;
5508 NT_PRINTER_DRIVER_INFO_LEVEL driver;
5511 ZERO_STRUCT(driver);
5513 status=get_a_printer(NULL, &printer, 2, lp_const_servicename(snum) );
5515 DEBUG(8,("construct_printer_driver_info_6: status: %s\n", win_errstr(status)));
5517 if (!W_ERROR_IS_OK(status))
5518 return WERR_INVALID_PRINTER_NAME;
5520 status = get_a_printer_driver(&driver, 3, printer->info_2->drivername, architecture, version);
5522 DEBUG(8,("construct_printer_driver_info_6: status: %s\n", win_errstr(status)));
5524 if (!W_ERROR_IS_OK(status))
5527 * Is this a W2k client ?
5531 free_a_printer(&printer,2);
5532 return WERR_UNKNOWN_PRINTER_DRIVER;
5535 /* Yes - try again with a WinNT driver. */
5537 status=get_a_printer_driver(&driver, 3, printer->info_2->drivername, architecture, version);
5538 DEBUG(8,("construct_printer_driver_info_6: status: %s\n", win_errstr(status)));
5539 if (!W_ERROR_IS_OK(status)) {
5540 free_a_printer(&printer,2);
5541 return WERR_UNKNOWN_PRINTER_DRIVER;
5545 fill_printer_driver_info_6(info, driver, servername);
5547 free_a_printer(&printer,2);
5548 free_a_printer_driver(driver, 3);
5553 /****************************************************************************
5554 ****************************************************************************/
5556 static void free_printer_driver_info_3(DRIVER_INFO_3 *info)
5558 SAFE_FREE(info->dependentfiles);
5561 /****************************************************************************
5562 ****************************************************************************/
5564 static void free_printer_driver_info_6(DRIVER_INFO_6 *info)
5566 SAFE_FREE(info->dependentfiles);
5569 /****************************************************************************
5570 ****************************************************************************/
5572 static WERROR getprinterdriver2_level1(const char *servername, fstring architecture, uint32 version, int snum, RPC_BUFFER *buffer, uint32 offered, uint32 *needed)
5574 DRIVER_INFO_1 *info=NULL;
5577 if((info=SMB_MALLOC_P(DRIVER_INFO_1)) == NULL)
5580 result = construct_printer_driver_info_1(info, snum, servername, architecture, version);
5581 if (!W_ERROR_IS_OK(result))
5584 /* check the required size. */
5585 *needed += spoolss_size_printer_driver_info_1(info);
5587 if (*needed > offered) {
5588 result = WERR_INSUFFICIENT_BUFFER;
5592 if (!rpcbuf_alloc_size(buffer, *needed)) {
5593 result = WERR_NOMEM;
5597 /* fill the buffer with the structures */
5598 smb_io_printer_driver_info_1("", buffer, info, 0);
5607 /****************************************************************************
5608 ****************************************************************************/
5610 static WERROR getprinterdriver2_level2(const char *servername, fstring architecture, uint32 version, int snum, RPC_BUFFER *buffer, uint32 offered, uint32 *needed)
5612 DRIVER_INFO_2 *info=NULL;
5615 if((info=SMB_MALLOC_P(DRIVER_INFO_2)) == NULL)
5618 result = construct_printer_driver_info_2(info, snum, servername, architecture, version);
5619 if (!W_ERROR_IS_OK(result))
5622 /* check the required size. */
5623 *needed += spoolss_size_printer_driver_info_2(info);
5625 if (*needed > offered) {
5626 result = WERR_INSUFFICIENT_BUFFER;
5630 if (!rpcbuf_alloc_size(buffer, *needed)) {
5631 result = WERR_NOMEM;
5635 /* fill the buffer with the structures */
5636 smb_io_printer_driver_info_2("", buffer, info, 0);
5645 /****************************************************************************
5646 ****************************************************************************/
5648 static WERROR getprinterdriver2_level3(const char *servername, fstring architecture, uint32 version, int snum, RPC_BUFFER *buffer, uint32 offered, uint32 *needed)
5655 result = construct_printer_driver_info_3(&info, snum, servername, architecture, version);
5656 if (!W_ERROR_IS_OK(result))
5659 /* check the required size. */
5660 *needed += spoolss_size_printer_driver_info_3(&info);
5662 if (*needed > offered) {
5663 result = WERR_INSUFFICIENT_BUFFER;
5667 if (!rpcbuf_alloc_size(buffer, *needed)) {
5668 result = WERR_NOMEM;
5672 /* fill the buffer with the structures */
5673 smb_io_printer_driver_info_3("", buffer, &info, 0);
5676 free_printer_driver_info_3(&info);
5681 /****************************************************************************
5682 ****************************************************************************/
5684 static WERROR getprinterdriver2_level6(const char *servername, fstring architecture, uint32 version, int snum, RPC_BUFFER *buffer, uint32 offered, uint32 *needed)
5691 result = construct_printer_driver_info_6(&info, snum, servername, architecture, version);
5692 if (!W_ERROR_IS_OK(result))
5695 /* check the required size. */
5696 *needed += spoolss_size_printer_driver_info_6(&info);
5698 if (*needed > offered) {
5699 result = WERR_INSUFFICIENT_BUFFER;
5703 if (!rpcbuf_alloc_size(buffer, *needed)) {
5704 result = WERR_NOMEM;
5708 /* fill the buffer with the structures */
5709 smb_io_printer_driver_info_6("", buffer, &info, 0);
5712 free_printer_driver_info_6(&info);
5717 /****************************************************************************
5718 ****************************************************************************/
5720 WERROR _spoolss_getprinterdriver2(pipes_struct *p, SPOOL_Q_GETPRINTERDRIVER2 *q_u, SPOOL_R_GETPRINTERDRIVER2 *r_u)
5722 POLICY_HND *handle = &q_u->handle;
5723 UNISTR2 *uni_arch = &q_u->architecture;
5724 uint32 level = q_u->level;
5725 uint32 clientmajorversion = q_u->clientmajorversion;
5726 RPC_BUFFER *buffer = NULL;
5727 uint32 offered = q_u->offered;
5728 uint32 *needed = &r_u->needed;
5729 uint32 *servermajorversion = &r_u->servermajorversion;
5730 uint32 *serverminorversion = &r_u->serverminorversion;
5731 Printer_entry *printer;
5734 fstring architecture;
5737 /* that's an [in out] buffer */
5739 if (!q_u->buffer && (offered!=0)) {
5740 return WERR_INVALID_PARAM;
5743 rpcbuf_move(q_u->buffer, &r_u->buffer);
5744 buffer = r_u->buffer;
5746 DEBUG(4,("_spoolss_getprinterdriver2\n"));
5748 if ( !(printer = find_printer_index_by_hnd( p, handle )) ) {
5749 DEBUG(0,("_spoolss_getprinterdriver2: invalid printer handle!\n"));
5750 return WERR_INVALID_PRINTER_NAME;
5754 *servermajorversion = 0;
5755 *serverminorversion = 0;
5757 fstrcpy(servername, get_server_name( printer ));
5758 unistr2_to_ascii(architecture, uni_arch, sizeof(architecture));
5760 if (!get_printer_snum(p, handle, &snum, NULL))
5765 return getprinterdriver2_level1(servername, architecture, clientmajorversion, snum, buffer, offered, needed);
5767 return getprinterdriver2_level2(servername, architecture, clientmajorversion, snum, buffer, offered, needed);
5769 return getprinterdriver2_level3(servername, architecture, clientmajorversion, snum, buffer, offered, needed);
5771 return getprinterdriver2_level6(servername, architecture, clientmajorversion, snum, buffer, offered, needed);
5774 /* apparently this call is the equivalent of
5775 EnumPrinterDataEx() for the DsDriver key */
5780 return WERR_UNKNOWN_LEVEL;
5783 /****************************************************************************
5784 ****************************************************************************/
5786 WERROR _spoolss_startpageprinter(pipes_struct *p, SPOOL_Q_STARTPAGEPRINTER *q_u, SPOOL_R_STARTPAGEPRINTER *r_u)
5788 POLICY_HND *handle = &q_u->handle;
5790 Printer_entry *Printer = find_printer_index_by_hnd(p, handle);
5793 DEBUG(3,("Error in startpageprinter printer handle\n"));
5797 Printer->page_started=True;
5801 /****************************************************************************
5802 ****************************************************************************/
5804 WERROR _spoolss_endpageprinter(pipes_struct *p, SPOOL_Q_ENDPAGEPRINTER *q_u, SPOOL_R_ENDPAGEPRINTER *r_u)
5806 POLICY_HND *handle = &q_u->handle;
5809 Printer_entry *Printer = find_printer_index_by_hnd(p, handle);
5812 DEBUG(2,("_spoolss_endpageprinter: Invalid handle (%s:%u:%u).\n",OUR_HANDLE(handle)));
5816 if (!get_printer_snum(p, handle, &snum, NULL))
5819 Printer->page_started=False;
5820 print_job_endpage(snum, Printer->jobid);
5825 /********************************************************************
5826 * api_spoolss_getprinter
5827 * called from the spoolss dispatcher
5829 ********************************************************************/
5831 WERROR _spoolss_startdocprinter(pipes_struct *p, SPOOL_Q_STARTDOCPRINTER *q_u, SPOOL_R_STARTDOCPRINTER *r_u)
5833 POLICY_HND *handle = &q_u->handle;
5834 DOC_INFO *docinfo = &q_u->doc_info_container.docinfo;
5835 uint32 *jobid = &r_u->jobid;
5836 TALLOC_CTX *ctx = p->mem_ctx;
5837 DOC_INFO_1 *info_1 = &docinfo->doc_info_1;
5839 char *jobname = NULL;
5841 Printer_entry *Printer = find_printer_index_by_hnd(p, handle);
5844 DEBUG(2,("_spoolss_startdocprinter: Invalid handle (%s:%u:%u)\n", OUR_HANDLE(handle)));
5849 * a nice thing with NT is it doesn't listen to what you tell it.
5850 * when asked to send _only_ RAW datas, it tries to send datas
5853 * So I add checks like in NT Server ...
5856 if (info_1->p_datatype != 0) {
5857 unistr2_to_ascii(datatype, &info_1->datatype, sizeof(datatype));
5858 if (strcmp(datatype, "RAW") != 0) {
5860 return WERR_INVALID_DATATYPE;
5864 /* get the share number of the printer */
5865 if (!get_printer_snum(p, handle, &snum, NULL)) {
5869 jobname = unistr2_to_ascii_talloc(ctx, &info_1->docname);
5871 Printer->jobid = print_job_start(p->server_info, snum, jobname,
5872 Printer->nt_devmode);
5874 /* An error occured in print_job_start() so return an appropriate
5877 if (Printer->jobid == -1) {
5878 return map_werror_from_unix(errno);
5881 Printer->document_started=True;
5882 (*jobid) = Printer->jobid;
5887 /********************************************************************
5888 * api_spoolss_getprinter
5889 * called from the spoolss dispatcher
5891 ********************************************************************/
5893 WERROR _spoolss_enddocprinter(pipes_struct *p, SPOOL_Q_ENDDOCPRINTER *q_u, SPOOL_R_ENDDOCPRINTER *r_u)
5895 POLICY_HND *handle = &q_u->handle;
5897 return _spoolss_enddocprinter_internal(p, handle);
5900 /****************************************************************************
5901 ****************************************************************************/
5903 WERROR _spoolss_writeprinter(pipes_struct *p, SPOOL_Q_WRITEPRINTER *q_u, SPOOL_R_WRITEPRINTER *r_u)
5905 POLICY_HND *handle = &q_u->handle;
5906 uint32 buffer_size = q_u->buffer_size;
5907 uint8 *buffer = q_u->buffer;
5908 uint32 *buffer_written = &q_u->buffer_size2;
5910 Printer_entry *Printer = find_printer_index_by_hnd(p, handle);
5913 DEBUG(2,("_spoolss_writeprinter: Invalid handle (%s:%u:%u)\n",OUR_HANDLE(handle)));
5914 r_u->buffer_written = q_u->buffer_size2;
5918 if (!get_printer_snum(p, handle, &snum, NULL))
5921 (*buffer_written) = (uint32)print_job_write(snum, Printer->jobid, (const char *)buffer,
5922 (SMB_OFF_T)-1, (size_t)buffer_size);
5923 if (*buffer_written == (uint32)-1) {
5924 r_u->buffer_written = 0;
5925 if (errno == ENOSPC)
5926 return WERR_NO_SPOOL_SPACE;
5928 return WERR_ACCESS_DENIED;
5931 r_u->buffer_written = q_u->buffer_size2;
5936 /********************************************************************
5937 * api_spoolss_getprinter
5938 * called from the spoolss dispatcher
5940 ********************************************************************/
5942 static WERROR control_printer(POLICY_HND *handle, uint32 command,
5946 WERROR errcode = WERR_BADFUNC;
5947 Printer_entry *Printer = find_printer_index_by_hnd(p, handle);
5950 DEBUG(2,("control_printer: Invalid handle (%s:%u:%u)\n", OUR_HANDLE(handle)));
5954 if (!get_printer_snum(p, handle, &snum, NULL))
5958 case PRINTER_CONTROL_PAUSE:
5959 if (print_queue_pause(p->server_info, snum, &errcode)) {
5963 case PRINTER_CONTROL_RESUME:
5964 case PRINTER_CONTROL_UNPAUSE:
5965 if (print_queue_resume(p->server_info, snum, &errcode)) {
5969 case PRINTER_CONTROL_PURGE:
5970 if (print_queue_purge(p->server_info, snum, &errcode)) {
5975 return WERR_UNKNOWN_LEVEL;
5981 /********************************************************************
5982 * api_spoolss_abortprinter
5983 * From MSDN: "Deletes printer's spool file if printer is configured
5985 ********************************************************************/
5987 WERROR _spoolss_abortprinter(pipes_struct *p, SPOOL_Q_ABORTPRINTER *q_u, SPOOL_R_ABORTPRINTER *r_u)
5989 POLICY_HND *handle = &q_u->handle;
5990 Printer_entry *Printer = find_printer_index_by_hnd(p, handle);
5992 WERROR errcode = WERR_OK;
5995 DEBUG(2,("_spoolss_abortprinter: Invalid handle (%s:%u:%u)\n",OUR_HANDLE(handle)));
5999 if (!get_printer_snum(p, handle, &snum, NULL))
6002 print_job_delete(p->server_info, snum, Printer->jobid, &errcode );
6007 /********************************************************************
6008 * called by spoolss_api_setprinter
6009 * when updating a printer description
6010 ********************************************************************/
6012 static WERROR update_printer_sec(POLICY_HND *handle, uint32 level,
6013 const SPOOL_PRINTER_INFO_LEVEL *info,
6014 pipes_struct *p, SEC_DESC_BUF *secdesc_ctr)
6016 SEC_DESC_BUF *new_secdesc_ctr = NULL, *old_secdesc_ctr = NULL;
6020 Printer_entry *Printer = find_printer_index_by_hnd(p, handle);
6022 if (!Printer || !get_printer_snum(p, handle, &snum, NULL)) {
6023 DEBUG(2,("update_printer_sec: Invalid handle (%s:%u:%u)\n",
6024 OUR_HANDLE(handle)));
6026 result = WERR_BADFID;
6031 DEBUG(10,("update_printer_sec: secdesc_ctr is NULL !\n"));
6032 result = WERR_INVALID_PARAM;
6036 /* Check the user has permissions to change the security
6037 descriptor. By experimentation with two NT machines, the user
6038 requires Full Access to the printer to change security
6041 if ( Printer->access_granted != PRINTER_ACCESS_ADMINISTER ) {
6042 DEBUG(4,("update_printer_sec: updated denied by printer permissions\n"));
6043 result = WERR_ACCESS_DENIED;
6047 /* NT seems to like setting the security descriptor even though
6048 nothing may have actually changed. */
6050 if ( !nt_printing_getsec(p->mem_ctx, Printer->sharename, &old_secdesc_ctr)) {
6051 DEBUG(2,("update_printer_sec: nt_printing_getsec() failed\n"));
6052 result = WERR_BADFID;
6056 if (DEBUGLEVEL >= 10) {
6060 the_acl = old_secdesc_ctr->sd->dacl;
6061 DEBUG(10, ("old_secdesc_ctr for %s has %d aces:\n",
6062 PRINTERNAME(snum), the_acl->num_aces));
6064 for (i = 0; i < the_acl->num_aces; i++) {
6065 DEBUG(10, ("%s 0x%08x\n", sid_string_dbg(
6066 &the_acl->aces[i].trustee),
6067 the_acl->aces[i].access_mask));
6070 the_acl = secdesc_ctr->sd->dacl;
6073 DEBUG(10, ("secdesc_ctr for %s has %d aces:\n",
6074 PRINTERNAME(snum), the_acl->num_aces));
6076 for (i = 0; i < the_acl->num_aces; i++) {
6077 DEBUG(10, ("%s 0x%08x\n", sid_string_dbg(
6078 &the_acl->aces[i].trustee),
6079 the_acl->aces[i].access_mask));
6082 DEBUG(10, ("dacl for secdesc_ctr is NULL\n"));
6086 new_secdesc_ctr = sec_desc_merge(p->mem_ctx, secdesc_ctr, old_secdesc_ctr);
6087 if (!new_secdesc_ctr) {
6088 result = WERR_NOMEM;
6092 if (sec_desc_equal(new_secdesc_ctr->sd, old_secdesc_ctr->sd)) {
6097 result = nt_printing_setsec(Printer->sharename, new_secdesc_ctr);
6104 /********************************************************************
6105 Canonicalize printer info from a client
6107 ATTN: It does not matter what we set the servername to hear
6108 since we do the necessary work in get_a_printer() to set it to
6109 the correct value based on what the client sent in the
6110 _spoolss_open_printer_ex().
6111 ********************************************************************/
6113 static bool check_printer_ok(NT_PRINTER_INFO_LEVEL_2 *info, int snum)
6115 fstring printername;
6118 DEBUG(5,("check_printer_ok: servername=%s printername=%s sharename=%s "
6119 "portname=%s drivername=%s comment=%s location=%s\n",
6120 info->servername, info->printername, info->sharename,
6121 info->portname, info->drivername, info->comment, info->location));
6123 /* we force some elements to "correct" values */
6124 slprintf(info->servername, sizeof(info->servername)-1, "\\\\%s", global_myname());
6125 fstrcpy(info->sharename, lp_servicename(snum));
6127 /* check to see if we allow printername != sharename */
6129 if ( lp_force_printername(snum) ) {
6130 slprintf(info->printername, sizeof(info->printername)-1, "\\\\%s\\%s",
6131 global_myname(), info->sharename );
6134 /* make sure printername is in \\server\printername format */
6136 fstrcpy( printername, info->printername );
6138 if ( printername[0] == '\\' && printername[1] == '\\' ) {
6139 if ( (p = strchr_m( &printername[2], '\\' )) != NULL )
6143 slprintf(info->printername, sizeof(info->printername)-1, "\\\\%s\\%s",
6144 global_myname(), p );
6147 info->attributes |= PRINTER_ATTRIBUTE_SAMBA;
6148 info->attributes &= ~PRINTER_ATTRIBUTE_NOT_SAMBA;
6155 /****************************************************************************
6156 ****************************************************************************/
6158 WERROR add_port_hook(TALLOC_CTX *ctx, NT_USER_TOKEN *token, const char *portname, const char *uri )
6160 char *cmd = lp_addport_cmd();
6161 char *command = NULL;
6163 SE_PRIV se_printop = SE_PRINT_OPERATOR;
6164 bool is_print_op = False;
6167 return WERR_ACCESS_DENIED;
6170 command = talloc_asprintf(ctx,
6171 "%s \"%s\" \"%s\"", cmd, portname, uri );
6177 is_print_op = user_has_privileges( token, &se_printop );
6179 DEBUG(10,("Running [%s]\n", command));
6181 /********* BEGIN SePrintOperatorPrivilege **********/
6186 ret = smbrun(command, NULL);
6191 /********* END SePrintOperatorPrivilege **********/
6193 DEBUGADD(10,("returned [%d]\n", ret));
6195 TALLOC_FREE(command);
6198 return WERR_ACCESS_DENIED;
6204 /****************************************************************************
6205 ****************************************************************************/
6207 bool add_printer_hook(TALLOC_CTX *ctx, NT_USER_TOKEN *token, NT_PRINTER_INFO_LEVEL *printer)
6209 char *cmd = lp_addprinter_cmd();
6211 char *command = NULL;
6215 SE_PRIV se_printop = SE_PRINT_OPERATOR;
6216 bool is_print_op = False;
6217 char *remote_machine = talloc_strdup(ctx, "%m");
6219 if (!remote_machine) {
6222 remote_machine = talloc_sub_basic(ctx,
6223 current_user_info.smb_name,
6224 current_user_info.domain,
6226 if (!remote_machine) {
6230 command = talloc_asprintf(ctx,
6231 "%s \"%s\" \"%s\" \"%s\" \"%s\" \"%s\" \"%s\" \"%s\"",
6232 cmd, printer->info_2->printername, printer->info_2->sharename,
6233 printer->info_2->portname, printer->info_2->drivername,
6234 printer->info_2->location, printer->info_2->comment, remote_machine);
6240 is_print_op = user_has_privileges( token, &se_printop );
6242 DEBUG(10,("Running [%s]\n", command));
6244 /********* BEGIN SePrintOperatorPrivilege **********/
6249 if ( (ret = smbrun(command, &fd)) == 0 ) {
6250 /* Tell everyone we updated smb.conf. */
6251 message_send_all(smbd_messaging_context(),
6252 MSG_SMB_CONF_UPDATED, NULL, 0, NULL);
6258 /********* END SePrintOperatorPrivilege **********/
6260 DEBUGADD(10,("returned [%d]\n", ret));
6262 TALLOC_FREE(command);
6263 TALLOC_FREE(remote_machine);
6271 /* reload our services immediately */
6272 reload_services( False );
6275 /* Get lines and convert them back to dos-codepage */
6276 qlines = fd_lines_load(fd, &numlines, 0, NULL);
6277 DEBUGADD(10,("Lines returned = [%d]\n", numlines));
6280 /* Set the portname to what the script says the portname should be. */
6281 /* but don't require anything to be return from the script exit a good error code */
6284 /* Set the portname to what the script says the portname should be. */
6285 strncpy(printer->info_2->portname, qlines[0], sizeof(printer->info_2->portname));
6286 DEBUGADD(6,("Line[0] = [%s]\n", qlines[0]));
6289 TALLOC_FREE(qlines);
6294 /********************************************************************
6295 * Called by spoolss_api_setprinter
6296 * when updating a printer description.
6297 ********************************************************************/
6299 static WERROR update_printer(pipes_struct *p, POLICY_HND *handle, uint32 level,
6300 const SPOOL_PRINTER_INFO_LEVEL *info,
6301 DEVICEMODE *devmode)
6304 NT_PRINTER_INFO_LEVEL *printer = NULL, *old_printer = NULL;
6305 Printer_entry *Printer = find_printer_index_by_hnd(p, handle);
6310 DEBUG(8,("update_printer\n"));
6315 result = WERR_BADFID;
6319 if (!get_printer_snum(p, handle, &snum, NULL)) {
6320 result = WERR_BADFID;
6324 if (!W_ERROR_IS_OK(get_a_printer(Printer, &printer, 2, lp_const_servicename(snum))) ||
6325 (!W_ERROR_IS_OK(get_a_printer(Printer, &old_printer, 2, lp_const_servicename(snum))))) {
6326 result = WERR_BADFID;
6330 DEBUGADD(8,("Converting info_2 struct\n"));
6333 * convert_printer_info converts the incoming
6334 * info from the client and overwrites the info
6335 * just read from the tdb in the pointer 'printer'.
6338 if (!convert_printer_info(info, printer, level)) {
6339 result = WERR_NOMEM;
6344 /* we have a valid devmode
6345 convert it and link it*/
6347 DEBUGADD(8,("update_printer: Converting the devicemode struct\n"));
6348 if (!convert_devicemode(printer->info_2->printername, devmode,
6349 &printer->info_2->devmode)) {
6350 result = WERR_NOMEM;
6355 /* Do sanity check on the requested changes for Samba */
6357 if (!check_printer_ok(printer->info_2, snum)) {
6358 result = WERR_INVALID_PARAM;
6362 /* FIXME!!! If the driver has changed we really should verify that
6363 it is installed before doing much else --jerry */
6365 /* Check calling user has permission to update printer description */
6367 if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
6368 DEBUG(3, ("update_printer: printer property change denied by handle\n"));
6369 result = WERR_ACCESS_DENIED;
6373 /* Call addprinter hook */
6374 /* Check changes to see if this is really needed */
6376 if ( *lp_addprinter_cmd()
6377 && (!strequal(printer->info_2->drivername, old_printer->info_2->drivername)
6378 || !strequal(printer->info_2->comment, old_printer->info_2->comment)
6379 || !strequal(printer->info_2->portname, old_printer->info_2->portname)
6380 || !strequal(printer->info_2->location, old_printer->info_2->location)) )
6382 /* add_printer_hook() will call reload_services() */
6384 if ( !add_printer_hook(p->mem_ctx, p->server_info->ptok,
6386 result = WERR_ACCESS_DENIED;
6392 * When a *new* driver is bound to a printer, the drivername is used to
6393 * lookup previously saved driver initialization info, which is then
6394 * bound to the printer, simulating what happens in the Windows arch.
6396 if (!strequal(printer->info_2->drivername, old_printer->info_2->drivername))
6398 if (!set_driver_init(printer, 2))
6400 DEBUG(5,("update_printer: Error restoring driver initialization data for driver [%s]!\n",
6401 printer->info_2->drivername));
6404 DEBUG(10,("update_printer: changing driver [%s]! Sending event!\n",
6405 printer->info_2->drivername));
6407 notify_printer_driver(snum, printer->info_2->drivername);
6411 * flag which changes actually occured. This is a small subset of
6412 * all the possible changes. We also have to update things in the
6416 if (!strequal(printer->info_2->comment, old_printer->info_2->comment)) {
6417 init_unistr2( &buffer, printer->info_2->comment, UNI_STR_TERMINATE);
6418 set_printer_dataex( printer, SPOOL_DSSPOOLER_KEY, "description",
6419 REG_SZ, (uint8*)buffer.buffer, buffer.uni_str_len*2 );
6421 notify_printer_comment(snum, printer->info_2->comment);
6424 if (!strequal(printer->info_2->sharename, old_printer->info_2->sharename)) {
6425 init_unistr2( &buffer, printer->info_2->sharename, UNI_STR_TERMINATE);
6426 set_printer_dataex( printer, SPOOL_DSSPOOLER_KEY, "shareName",
6427 REG_SZ, (uint8*)buffer.buffer, buffer.uni_str_len*2 );
6429 notify_printer_sharename(snum, printer->info_2->sharename);
6432 if (!strequal(printer->info_2->printername, old_printer->info_2->printername)) {
6435 if ( (pname = strchr_m( printer->info_2->printername+2, '\\' )) != NULL )
6438 pname = printer->info_2->printername;
6441 init_unistr2( &buffer, pname, UNI_STR_TERMINATE);
6442 set_printer_dataex( printer, SPOOL_DSSPOOLER_KEY, "printerName",
6443 REG_SZ, (uint8*)buffer.buffer, buffer.uni_str_len*2 );
6445 notify_printer_printername( snum, pname );
6448 if (!strequal(printer->info_2->portname, old_printer->info_2->portname)) {
6449 init_unistr2( &buffer, printer->info_2->portname, UNI_STR_TERMINATE);
6450 set_printer_dataex( printer, SPOOL_DSSPOOLER_KEY, "portName",
6451 REG_SZ, (uint8*)buffer.buffer, buffer.uni_str_len*2 );
6453 notify_printer_port(snum, printer->info_2->portname);
6456 if (!strequal(printer->info_2->location, old_printer->info_2->location)) {
6457 init_unistr2( &buffer, printer->info_2->location, UNI_STR_TERMINATE);
6458 set_printer_dataex( printer, SPOOL_DSSPOOLER_KEY, "location",
6459 REG_SZ, (uint8*)buffer.buffer, buffer.uni_str_len*2 );
6461 notify_printer_location(snum, printer->info_2->location);
6464 /* here we need to update some more DsSpooler keys */
6465 /* uNCName, serverName, shortServerName */
6467 init_unistr2( &buffer, global_myname(), UNI_STR_TERMINATE);
6468 set_printer_dataex( printer, SPOOL_DSSPOOLER_KEY, "serverName",
6469 REG_SZ, (uint8*)buffer.buffer, buffer.uni_str_len*2 );
6470 set_printer_dataex( printer, SPOOL_DSSPOOLER_KEY, "shortServerName",
6471 REG_SZ, (uint8*)buffer.buffer, buffer.uni_str_len*2 );
6473 slprintf( asc_buffer, sizeof(asc_buffer)-1, "\\\\%s\\%s",
6474 global_myname(), printer->info_2->sharename );
6475 init_unistr2( &buffer, asc_buffer, UNI_STR_TERMINATE);
6476 set_printer_dataex( printer, SPOOL_DSSPOOLER_KEY, "uNCName",
6477 REG_SZ, (uint8*)buffer.buffer, buffer.uni_str_len*2 );
6479 /* Update printer info */
6480 result = mod_a_printer(printer, 2);
6483 free_a_printer(&printer, 2);
6484 free_a_printer(&old_printer, 2);
6490 /****************************************************************************
6491 ****************************************************************************/
6492 static WERROR publish_or_unpublish_printer(pipes_struct *p, POLICY_HND *handle,
6493 const SPOOL_PRINTER_INFO_LEVEL *info)
6496 SPOOL_PRINTER_INFO_LEVEL_7 *info7 = info->info_7;
6498 Printer_entry *Printer;
6500 if ( lp_security() != SEC_ADS ) {
6501 return WERR_UNKNOWN_LEVEL;
6504 Printer = find_printer_index_by_hnd(p, handle);
6506 DEBUG(5,("publish_or_unpublish_printer, action = %d\n",info7->action));
6511 if (!get_printer_snum(p, handle, &snum, NULL))
6514 nt_printer_publish(Printer, snum, info7->action);
6518 return WERR_UNKNOWN_LEVEL;
6521 /****************************************************************************
6522 ****************************************************************************/
6524 WERROR _spoolss_setprinter(pipes_struct *p, SPOOL_Q_SETPRINTER *q_u, SPOOL_R_SETPRINTER *r_u)
6526 POLICY_HND *handle = &q_u->handle;
6527 uint32 level = q_u->level;
6528 SPOOL_PRINTER_INFO_LEVEL *info = &q_u->info;
6529 DEVMODE_CTR devmode_ctr = q_u->devmode_ctr;
6530 SEC_DESC_BUF *secdesc_ctr = q_u->secdesc_ctr;
6531 uint32 command = q_u->command;
6534 Printer_entry *Printer = find_printer_index_by_hnd(p, handle);
6537 DEBUG(2,("_spoolss_setprinter: Invalid handle (%s:%u:%u)\n", OUR_HANDLE(handle)));
6541 /* check the level */
6544 return control_printer(handle, command, p);
6546 result = update_printer(p, handle, level, info, devmode_ctr.devmode);
6547 if (!W_ERROR_IS_OK(result))
6550 result = update_printer_sec(handle, level, info, p, secdesc_ctr);
6553 return update_printer_sec(handle, level, info, p,
6556 return publish_or_unpublish_printer(p, handle, info);
6558 return WERR_UNKNOWN_LEVEL;
6562 /****************************************************************************
6563 ****************************************************************************/
6565 WERROR _spoolss_fcpn(pipes_struct *p, SPOOL_Q_FCPN *q_u, SPOOL_R_FCPN *r_u)
6567 POLICY_HND *handle = &q_u->handle;
6568 Printer_entry *Printer= find_printer_index_by_hnd(p, handle);
6571 DEBUG(2,("_spoolss_fcpn: Invalid handle (%s:%u:%u)\n", OUR_HANDLE(handle)));
6575 if (Printer->notify.client_connected==True) {
6578 if ( Printer->printer_type == SPLHND_SERVER)
6580 else if ( (Printer->printer_type == SPLHND_PRINTER) &&
6581 !get_printer_snum(p, handle, &snum, NULL) )
6584 srv_spoolss_replycloseprinter(snum, &Printer->notify.client_hnd);
6587 Printer->notify.flags=0;
6588 Printer->notify.options=0;
6589 Printer->notify.localmachine[0]='\0';
6590 Printer->notify.printerlocal=0;
6591 if (Printer->notify.option)
6592 free_spool_notify_option(&Printer->notify.option);
6593 Printer->notify.client_connected=False;
6598 /****************************************************************************
6599 ****************************************************************************/
6601 WERROR _spoolss_addjob(pipes_struct *p, SPOOL_Q_ADDJOB *q_u, SPOOL_R_ADDJOB *r_u)
6603 /* that's an [in out] buffer */
6605 if (!q_u->buffer && (q_u->offered!=0)) {
6606 return WERR_INVALID_PARAM;
6609 rpcbuf_move(q_u->buffer, &r_u->buffer);
6612 return WERR_INVALID_PARAM; /* this is what a NT server
6613 returns for AddJob. AddJob
6614 must fail on non-local
6618 /****************************************************************************
6619 ****************************************************************************/
6621 static void fill_job_info_1(JOB_INFO_1 *job_info, const print_queue_struct *queue,
6622 int position, int snum,
6623 const NT_PRINTER_INFO_LEVEL *ntprinter)
6627 t=gmtime(&queue->time);
6629 job_info->jobid=queue->job;
6630 init_unistr(&job_info->printername, lp_servicename(snum));
6631 init_unistr(&job_info->machinename, ntprinter->info_2->servername);
6632 init_unistr(&job_info->username, queue->fs_user);
6633 init_unistr(&job_info->document, queue->fs_file);
6634 init_unistr(&job_info->datatype, "RAW");
6635 init_unistr(&job_info->text_status, "");
6636 job_info->status=nt_printj_status(queue->status);
6637 job_info->priority=queue->priority;
6638 job_info->position=position;
6639 job_info->totalpages=queue->page_count;
6640 job_info->pagesprinted=0;
6642 make_systemtime(&job_info->submitted, t);
6645 /****************************************************************************
6646 ****************************************************************************/
6648 static bool fill_job_info_2(JOB_INFO_2 *job_info, const print_queue_struct *queue,
6649 int position, int snum,
6650 const NT_PRINTER_INFO_LEVEL *ntprinter,
6651 DEVICEMODE *devmode)
6655 t=gmtime(&queue->time);
6657 job_info->jobid=queue->job;
6659 init_unistr(&job_info->printername, ntprinter->info_2->printername);
6661 init_unistr(&job_info->machinename, ntprinter->info_2->servername);
6662 init_unistr(&job_info->username, queue->fs_user);
6663 init_unistr(&job_info->document, queue->fs_file);
6664 init_unistr(&job_info->notifyname, queue->fs_user);
6665 init_unistr(&job_info->datatype, "RAW");
6666 init_unistr(&job_info->printprocessor, "winprint");
6667 init_unistr(&job_info->parameters, "");
6668 init_unistr(&job_info->drivername, ntprinter->info_2->drivername);
6669 init_unistr(&job_info->text_status, "");
6671 /* and here the security descriptor */
6673 job_info->status=nt_printj_status(queue->status);
6674 job_info->priority=queue->priority;
6675 job_info->position=position;
6676 job_info->starttime=0;
6677 job_info->untiltime=0;
6678 job_info->totalpages=queue->page_count;
6679 job_info->size=queue->size;
6680 make_systemtime(&(job_info->submitted), t);
6681 job_info->timeelapsed=0;
6682 job_info->pagesprinted=0;
6684 job_info->devmode = devmode;
6689 /****************************************************************************
6690 Enumjobs at level 1.
6691 ****************************************************************************/
6693 static WERROR enumjobs_level1(const print_queue_struct *queue, int snum,
6694 const NT_PRINTER_INFO_LEVEL *ntprinter,
6695 RPC_BUFFER *buffer, uint32 offered,
6696 uint32 *needed, uint32 *returned)
6700 WERROR result = WERR_OK;
6702 info=SMB_MALLOC_ARRAY(JOB_INFO_1,*returned);
6708 for (i=0; i<*returned; i++)
6709 fill_job_info_1( &info[i], &queue[i], i, snum, ntprinter );
6711 /* check the required size. */
6712 for (i=0; i<*returned; i++)
6713 (*needed) += spoolss_size_job_info_1(&info[i]);
6715 if (*needed > offered) {
6716 result = WERR_INSUFFICIENT_BUFFER;
6720 if (!rpcbuf_alloc_size(buffer, *needed)) {
6721 result = WERR_NOMEM;
6725 /* fill the buffer with the structures */
6726 for (i=0; i<*returned; i++)
6727 smb_io_job_info_1("", buffer, &info[i], 0);
6733 if ( !W_ERROR_IS_OK(result) )
6739 /****************************************************************************
6740 Enumjobs at level 2.
6741 ****************************************************************************/
6743 static WERROR enumjobs_level2(const print_queue_struct *queue, int snum,
6744 const NT_PRINTER_INFO_LEVEL *ntprinter,
6745 RPC_BUFFER *buffer, uint32 offered,
6746 uint32 *needed, uint32 *returned)
6748 JOB_INFO_2 *info = NULL;
6750 WERROR result = WERR_OK;
6751 DEVICEMODE *devmode = NULL;
6753 if ( !(info = SMB_MALLOC_ARRAY(JOB_INFO_2,*returned)) ) {
6758 /* this should not be a failure condition if the devmode is NULL */
6760 devmode = construct_dev_mode(lp_const_servicename(snum));
6762 for (i=0; i<*returned; i++)
6763 fill_job_info_2(&(info[i]), &queue[i], i, snum, ntprinter, devmode);
6765 /* check the required size. */
6766 for (i=0; i<*returned; i++)
6767 (*needed) += spoolss_size_job_info_2(&info[i]);
6769 if (*needed > offered) {
6770 result = WERR_INSUFFICIENT_BUFFER;
6774 if (!rpcbuf_alloc_size(buffer, *needed)) {
6775 result = WERR_NOMEM;
6779 /* fill the buffer with the structures */
6780 for (i=0; i<*returned; i++)
6781 smb_io_job_info_2("", buffer, &info[i], 0);
6784 free_devmode(devmode);
6787 if ( !W_ERROR_IS_OK(result) )
6794 /****************************************************************************
6796 ****************************************************************************/
6798 WERROR _spoolss_enumjobs( pipes_struct *p, SPOOL_Q_ENUMJOBS *q_u, SPOOL_R_ENUMJOBS *r_u)
6800 POLICY_HND *handle = &q_u->handle;
6801 uint32 level = q_u->level;
6802 RPC_BUFFER *buffer = NULL;
6803 uint32 offered = q_u->offered;
6804 uint32 *needed = &r_u->needed;
6805 uint32 *returned = &r_u->returned;
6807 NT_PRINTER_INFO_LEVEL *ntprinter = NULL;
6809 print_status_struct prt_status;
6810 print_queue_struct *queue=NULL;
6812 /* that's an [in out] buffer */
6814 if (!q_u->buffer && (offered!=0)) {
6815 return WERR_INVALID_PARAM;
6818 rpcbuf_move(q_u->buffer, &r_u->buffer);
6819 buffer = r_u->buffer;
6821 DEBUG(4,("_spoolss_enumjobs\n"));
6826 /* lookup the printer snum and tdb entry */
6828 if (!get_printer_snum(p, handle, &snum, NULL))
6831 wret = get_a_printer(NULL, &ntprinter, 2, lp_servicename(snum));
6832 if ( !W_ERROR_IS_OK(wret) )
6835 *returned = print_queue_status(snum, &queue, &prt_status);
6836 DEBUGADD(4,("count:[%d], status:[%d], [%s]\n", *returned, prt_status.status, prt_status.message));
6838 if (*returned == 0) {
6840 free_a_printer(&ntprinter, 2);
6846 wret = enumjobs_level1(queue, snum, ntprinter, buffer, offered, needed, returned);
6849 wret = enumjobs_level2(queue, snum, ntprinter, buffer, offered, needed, returned);
6853 wret = WERR_UNKNOWN_LEVEL;
6858 free_a_printer( &ntprinter, 2 );
6862 /****************************************************************************
6863 ****************************************************************************/
6865 WERROR _spoolss_schedulejob( pipes_struct *p, SPOOL_Q_SCHEDULEJOB *q_u, SPOOL_R_SCHEDULEJOB *r_u)
6870 /****************************************************************************
6871 ****************************************************************************/
6873 WERROR _spoolss_setjob(pipes_struct *p, SPOOL_Q_SETJOB *q_u, SPOOL_R_SETJOB *r_u)
6875 POLICY_HND *handle = &q_u->handle;
6876 uint32 jobid = q_u->jobid;
6877 uint32 command = q_u->command;
6880 WERROR errcode = WERR_BADFUNC;
6882 if (!get_printer_snum(p, handle, &snum, NULL)) {
6886 if (!print_job_exists(lp_const_servicename(snum), jobid)) {
6887 return WERR_INVALID_PRINTER_NAME;
6891 case JOB_CONTROL_CANCEL:
6892 case JOB_CONTROL_DELETE:
6893 if (print_job_delete(p->server_info, snum, jobid, &errcode)) {
6897 case JOB_CONTROL_PAUSE:
6898 if (print_job_pause(p->server_info, snum, jobid, &errcode)) {
6902 case JOB_CONTROL_RESTART:
6903 case JOB_CONTROL_RESUME:
6904 if (print_job_resume(p->server_info, snum, jobid, &errcode)) {
6909 return WERR_UNKNOWN_LEVEL;
6915 /****************************************************************************
6916 Enumerates all printer drivers at level 1.
6917 ****************************************************************************/
6919 static WERROR enumprinterdrivers_level1(const char *servername, fstring architecture, RPC_BUFFER *buffer, uint32 offered, uint32 *needed, uint32 *returned)
6924 fstring *list = NULL;
6925 NT_PRINTER_DRIVER_INFO_LEVEL driver;
6926 DRIVER_INFO_1 *driver_info_1=NULL;
6927 WERROR result = WERR_OK;
6931 for (version=0; version<DRIVER_MAX_VERSION; version++) {
6933 ndrivers=get_ntdrivers(&list, architecture, version);
6934 DEBUGADD(4,("we have:[%d] drivers in environment [%s] and version [%d]\n", ndrivers, architecture, version));
6936 if(ndrivers == -1) {
6937 SAFE_FREE(driver_info_1);
6942 if((driver_info_1=SMB_REALLOC_ARRAY(driver_info_1, DRIVER_INFO_1, *returned+ndrivers )) == NULL) {
6943 DEBUG(0,("enumprinterdrivers_level1: failed to enlarge driver info buffer!\n"));
6949 for (i=0; i<ndrivers; i++) {
6951 DEBUGADD(5,("\tdriver: [%s]\n", list[i]));
6952 ZERO_STRUCT(driver);
6953 status = get_a_printer_driver(&driver, 3, list[i],
6954 architecture, version);
6955 if (!W_ERROR_IS_OK(status)) {
6957 SAFE_FREE(driver_info_1);
6960 fill_printer_driver_info_1(&driver_info_1[*returned+i], driver, servername, architecture );
6961 free_a_printer_driver(driver, 3);
6964 *returned+=ndrivers;
6968 /* check the required size. */
6969 for (i=0; i<*returned; i++) {
6970 DEBUGADD(6,("adding driver [%d]'s size\n",i));
6971 *needed += spoolss_size_printer_driver_info_1(&driver_info_1[i]);
6974 if (*needed > offered) {
6975 result = WERR_INSUFFICIENT_BUFFER;
6979 if (!rpcbuf_alloc_size(buffer, *needed)) {
6980 result = WERR_NOMEM;
6984 /* fill the buffer with the driver structures */
6985 for (i=0; i<*returned; i++) {
6986 DEBUGADD(6,("adding driver [%d] to buffer\n",i));
6987 smb_io_printer_driver_info_1("", buffer, &driver_info_1[i], 0);
6991 SAFE_FREE(driver_info_1);
6993 if ( !W_ERROR_IS_OK(result) )
6999 /****************************************************************************
7000 Enumerates all printer drivers at level 2.
7001 ****************************************************************************/
7003 static WERROR enumprinterdrivers_level2(const char *servername, fstring architecture, RPC_BUFFER *buffer, uint32 offered, uint32 *needed, uint32 *returned)
7008 fstring *list = NULL;
7009 NT_PRINTER_DRIVER_INFO_LEVEL driver;
7010 DRIVER_INFO_2 *driver_info_2=NULL;
7011 WERROR result = WERR_OK;
7015 for (version=0; version<DRIVER_MAX_VERSION; version++) {
7017 ndrivers=get_ntdrivers(&list, architecture, version);
7018 DEBUGADD(4,("we have:[%d] drivers in environment [%s] and version [%d]\n", ndrivers, architecture, version));
7020 if(ndrivers == -1) {
7021 SAFE_FREE(driver_info_2);
7026 if((driver_info_2=SMB_REALLOC_ARRAY(driver_info_2, DRIVER_INFO_2, *returned+ndrivers )) == NULL) {
7027 DEBUG(0,("enumprinterdrivers_level2: failed to enlarge driver info buffer!\n"));
7033 for (i=0; i<ndrivers; i++) {
7036 DEBUGADD(5,("\tdriver: [%s]\n", list[i]));
7037 ZERO_STRUCT(driver);
7038 status = get_a_printer_driver(&driver, 3, list[i],
7039 architecture, version);
7040 if (!W_ERROR_IS_OK(status)) {
7042 SAFE_FREE(driver_info_2);
7045 fill_printer_driver_info_2(&driver_info_2[*returned+i], driver, servername);
7046 free_a_printer_driver(driver, 3);
7049 *returned+=ndrivers;
7053 /* check the required size. */
7054 for (i=0; i<*returned; i++) {
7055 DEBUGADD(6,("adding driver [%d]'s size\n",i));
7056 *needed += spoolss_size_printer_driver_info_2(&(driver_info_2[i]));
7059 if (*needed > offered) {
7060 result = WERR_INSUFFICIENT_BUFFER;
7064 if (!rpcbuf_alloc_size(buffer, *needed)) {
7065 result = WERR_NOMEM;
7069 /* fill the buffer with the form structures */
7070 for (i=0; i<*returned; i++) {
7071 DEBUGADD(6,("adding driver [%d] to buffer\n",i));
7072 smb_io_printer_driver_info_2("", buffer, &(driver_info_2[i]), 0);
7076 SAFE_FREE(driver_info_2);
7078 if ( !W_ERROR_IS_OK(result) )
7084 /****************************************************************************
7085 Enumerates all printer drivers at level 3.
7086 ****************************************************************************/
7088 static WERROR enumprinterdrivers_level3(const char *servername, fstring architecture, RPC_BUFFER *buffer, uint32 offered, uint32 *needed, uint32 *returned)
7093 fstring *list = NULL;
7094 DRIVER_INFO_3 *driver_info_3=NULL;
7095 NT_PRINTER_DRIVER_INFO_LEVEL driver;
7096 WERROR result = WERR_OK;
7100 for (version=0; version<DRIVER_MAX_VERSION; version++) {
7102 ndrivers=get_ntdrivers(&list, architecture, version);
7103 DEBUGADD(4,("we have:[%d] drivers in environment [%s] and version [%d]\n", ndrivers, architecture, version));
7105 if(ndrivers == -1) {
7106 SAFE_FREE(driver_info_3);
7111 if((driver_info_3=SMB_REALLOC_ARRAY(driver_info_3, DRIVER_INFO_3, *returned+ndrivers )) == NULL) {
7112 DEBUG(0,("enumprinterdrivers_level3: failed to enlarge driver info buffer!\n"));
7118 for (i=0; i<ndrivers; i++) {
7121 DEBUGADD(5,("\tdriver: [%s]\n", list[i]));
7122 ZERO_STRUCT(driver);
7123 status = get_a_printer_driver(&driver, 3, list[i],
7124 architecture, version);
7125 if (!W_ERROR_IS_OK(status)) {
7127 SAFE_FREE(driver_info_3);
7130 fill_printer_driver_info_3(&driver_info_3[*returned+i], driver, servername);
7131 free_a_printer_driver(driver, 3);
7134 *returned+=ndrivers;
7138 /* check the required size. */
7139 for (i=0; i<*returned; i++) {
7140 DEBUGADD(6,("adding driver [%d]'s size\n",i));
7141 *needed += spoolss_size_printer_driver_info_3(&driver_info_3[i]);
7144 if (*needed > offered) {
7145 result = WERR_INSUFFICIENT_BUFFER;
7149 if (!rpcbuf_alloc_size(buffer, *needed)) {
7150 result = WERR_NOMEM;
7154 /* fill the buffer with the driver structures */
7155 for (i=0; i<*returned; i++) {
7156 DEBUGADD(6,("adding driver [%d] to buffer\n",i));
7157 smb_io_printer_driver_info_3("", buffer, &driver_info_3[i], 0);
7161 for (i=0; i<*returned; i++) {
7162 SAFE_FREE(driver_info_3[i].dependentfiles);
7165 SAFE_FREE(driver_info_3);
7167 if ( !W_ERROR_IS_OK(result) )
7173 /****************************************************************************
7174 Enumerates all printer drivers.
7175 ****************************************************************************/
7177 WERROR _spoolss_enumprinterdrivers( pipes_struct *p, SPOOL_Q_ENUMPRINTERDRIVERS *q_u, SPOOL_R_ENUMPRINTERDRIVERS *r_u)
7179 uint32 level = q_u->level;
7180 RPC_BUFFER *buffer = NULL;
7181 uint32 offered = q_u->offered;
7182 uint32 *needed = &r_u->needed;
7183 uint32 *returned = &r_u->returned;
7184 const char *cservername;
7186 fstring architecture;
7188 /* that's an [in out] buffer */
7190 if (!q_u->buffer && (offered!=0)) {
7191 return WERR_INVALID_PARAM;
7194 rpcbuf_move(q_u->buffer, &r_u->buffer);
7195 buffer = r_u->buffer;
7197 DEBUG(4,("_spoolss_enumprinterdrivers\n"));
7202 unistr2_to_ascii(architecture, &q_u->environment, sizeof(architecture));
7203 unistr2_to_ascii(servername, &q_u->name, sizeof(servername));
7205 cservername = canon_servername(servername);
7207 if (!is_myname_or_ipaddr(cservername))
7208 return WERR_UNKNOWN_PRINTER_DRIVER;
7212 return enumprinterdrivers_level1(cservername, architecture, buffer, offered, needed, returned);
7214 return enumprinterdrivers_level2(cservername, architecture, buffer, offered, needed, returned);
7216 return enumprinterdrivers_level3(cservername, architecture, buffer, offered, needed, returned);
7218 return WERR_UNKNOWN_LEVEL;
7222 /****************************************************************************
7223 ****************************************************************************/
7225 static void fill_form_1(FORM_1 *form, nt_forms_struct *list)
7227 form->flag=list->flag;
7228 init_unistr(&form->name, list->name);
7229 form->width=list->width;
7230 form->length=list->length;
7231 form->left=list->left;
7232 form->top=list->top;
7233 form->right=list->right;
7234 form->bottom=list->bottom;
7237 /****************************************************************************
7238 ****************************************************************************/
7240 WERROR _spoolss_enumforms(pipes_struct *p, SPOOL_Q_ENUMFORMS *q_u, SPOOL_R_ENUMFORMS *r_u)
7242 uint32 level = q_u->level;
7243 RPC_BUFFER *buffer = NULL;
7244 uint32 offered = q_u->offered;
7245 uint32 *needed = &r_u->needed;
7246 uint32 *numofforms = &r_u->numofforms;
7247 uint32 numbuiltinforms;
7249 nt_forms_struct *list=NULL;
7250 nt_forms_struct *builtinlist=NULL;
7255 /* that's an [in out] buffer */
7257 if (!q_u->buffer && (offered!=0) ) {
7258 return WERR_INVALID_PARAM;
7261 rpcbuf_move(q_u->buffer, &r_u->buffer);
7262 buffer = r_u->buffer;
7264 DEBUG(4,("_spoolss_enumforms\n"));
7265 DEBUGADD(5,("Offered buffer size [%d]\n", offered));
7266 DEBUGADD(5,("Info level [%d]\n", level));
7268 numbuiltinforms = get_builtin_ntforms(&builtinlist);
7269 DEBUGADD(5,("Number of builtin forms [%d]\n", numbuiltinforms));
7270 *numofforms = get_ntforms(&list);
7271 DEBUGADD(5,("Number of user forms [%d]\n", *numofforms));
7272 *numofforms += numbuiltinforms;
7274 if (*numofforms == 0) {
7275 SAFE_FREE(builtinlist);
7277 return WERR_NO_MORE_ITEMS;
7282 if ((forms_1=SMB_MALLOC_ARRAY(FORM_1, *numofforms)) == NULL) {
7283 SAFE_FREE(builtinlist);
7289 /* construct the list of form structures */
7290 for (i=0; i<numbuiltinforms; i++) {
7291 DEBUGADD(6,("Filling form number [%d]\n",i));
7292 fill_form_1(&forms_1[i], &builtinlist[i]);
7295 SAFE_FREE(builtinlist);
7297 for (; i<*numofforms; i++) {
7298 DEBUGADD(6,("Filling form number [%d]\n",i));
7299 fill_form_1(&forms_1[i], &list[i-numbuiltinforms]);
7304 /* check the required size. */
7305 for (i=0; i<numbuiltinforms; i++) {
7306 DEBUGADD(6,("adding form [%d]'s size\n",i));
7307 buffer_size += spoolss_size_form_1(&forms_1[i]);
7309 for (; i<*numofforms; i++) {
7310 DEBUGADD(6,("adding form [%d]'s size\n",i));
7311 buffer_size += spoolss_size_form_1(&forms_1[i]);
7314 *needed=buffer_size;
7316 if (*needed > offered) {
7319 return WERR_INSUFFICIENT_BUFFER;
7322 if (!rpcbuf_alloc_size(buffer, buffer_size)){
7328 /* fill the buffer with the form structures */
7329 for (i=0; i<numbuiltinforms; i++) {
7330 DEBUGADD(6,("adding form [%d] to buffer\n",i));
7331 smb_io_form_1("", buffer, &forms_1[i], 0);
7333 for (; i<*numofforms; i++) {
7334 DEBUGADD(6,("adding form [%d] to buffer\n",i));
7335 smb_io_form_1("", buffer, &forms_1[i], 0);
7344 SAFE_FREE(builtinlist);
7345 return WERR_UNKNOWN_LEVEL;
7349 /****************************************************************************
7350 ****************************************************************************/
7352 WERROR _spoolss_getform(pipes_struct *p, SPOOL_Q_GETFORM *q_u, SPOOL_R_GETFORM *r_u)
7354 uint32 level = q_u->level;
7355 UNISTR2 *uni_formname = &q_u->formname;
7356 RPC_BUFFER *buffer = NULL;
7357 uint32 offered = q_u->offered;
7358 uint32 *needed = &r_u->needed;
7360 nt_forms_struct *list=NULL;
7361 nt_forms_struct builtin_form;
7366 int numofforms=0, i=0;
7368 /* that's an [in out] buffer */
7370 if (!q_u->buffer && (offered!=0)) {
7371 return WERR_INVALID_PARAM;
7374 rpcbuf_move(q_u->buffer, &r_u->buffer);
7375 buffer = r_u->buffer;
7377 unistr2_to_ascii(form_name, uni_formname, sizeof(form_name));
7379 DEBUG(4,("_spoolss_getform\n"));
7380 DEBUGADD(5,("Offered buffer size [%d]\n", offered));
7381 DEBUGADD(5,("Info level [%d]\n", level));
7383 foundBuiltin = get_a_builtin_ntform(uni_formname,&builtin_form);
7384 if (!foundBuiltin) {
7385 numofforms = get_ntforms(&list);
7386 DEBUGADD(5,("Number of forms [%d]\n", numofforms));
7388 if (numofforms == 0)
7395 fill_form_1(&form_1, &builtin_form);
7398 /* Check if the requested name is in the list of form structures */
7399 for (i=0; i<numofforms; i++) {
7401 DEBUG(4,("_spoolss_getform: checking form %s (want %s)\n", list[i].name, form_name));
7403 if (strequal(form_name, list[i].name)) {
7404 DEBUGADD(6,("Found form %s number [%d]\n", form_name, i));
7405 fill_form_1(&form_1, &list[i]);
7411 if (i == numofforms) {
7415 /* check the required size. */
7417 *needed=spoolss_size_form_1(&form_1);
7419 if (*needed > offered)
7420 return WERR_INSUFFICIENT_BUFFER;
7422 if (!rpcbuf_alloc_size(buffer, buffer_size))
7425 /* fill the buffer with the form structures */
7426 DEBUGADD(6,("adding form %s [%d] to buffer\n", form_name, i));
7427 smb_io_form_1("", buffer, &form_1, 0);
7433 return WERR_UNKNOWN_LEVEL;
7437 /****************************************************************************
7438 ****************************************************************************/
7440 static void fill_port_1(PORT_INFO_1 *port, const char *name)
7442 init_unistr(&port->port_name, name);
7445 /****************************************************************************
7446 TODO: This probably needs distinguish between TCP/IP and Local ports
7448 ****************************************************************************/
7450 static void fill_port_2(PORT_INFO_2 *port, const char *name)
7452 init_unistr(&port->port_name, name);
7453 init_unistr(&port->monitor_name, "Local Monitor");
7454 init_unistr(&port->description, SPL_LOCAL_PORT );
7455 port->port_type=PORT_TYPE_WRITE;
7460 /****************************************************************************
7461 wrapper around the enumer ports command
7462 ****************************************************************************/
7464 WERROR enumports_hook(TALLOC_CTX *ctx, int *count, char ***lines )
7466 char *cmd = lp_enumports_cmd();
7467 char **qlines = NULL;
7468 char *command = NULL;
7476 /* if no hook then just fill in the default port */
7479 if (!(qlines = TALLOC_ARRAY( NULL, char*, 2 ))) {
7482 if (!(qlines[0] = talloc_strdup(qlines, SAMBA_PRINTER_PORT_NAME ))) {
7483 TALLOC_FREE(qlines);
7490 /* we have a valid enumport command */
7492 command = talloc_asprintf(ctx, "%s \"%d\"", cmd, 1);
7497 DEBUG(10,("Running [%s]\n", command));
7498 ret = smbrun(command, &fd);
7499 DEBUG(10,("Returned [%d]\n", ret));
7500 TALLOC_FREE(command);
7505 return WERR_ACCESS_DENIED;
7509 qlines = fd_lines_load(fd, &numlines, 0, NULL);
7510 DEBUGADD(10,("Lines returned = [%d]\n", numlines));
7520 /****************************************************************************
7522 ****************************************************************************/
7524 static WERROR enumports_level_1(RPC_BUFFER *buffer, uint32 offered, uint32 *needed, uint32 *returned)
7526 PORT_INFO_1 *ports=NULL;
7528 WERROR result = WERR_OK;
7529 char **qlines = NULL;
7532 result = enumports_hook(talloc_tos(), &numlines, &qlines );
7533 if (!W_ERROR_IS_OK(result)) {
7534 TALLOC_FREE(qlines);
7539 if((ports=SMB_MALLOC_ARRAY( PORT_INFO_1, numlines )) == NULL) {
7540 DEBUG(10,("Returning WERR_NOMEM [%s]\n",
7541 win_errstr(WERR_NOMEM)));
7542 TALLOC_FREE(qlines);
7546 for (i=0; i<numlines; i++) {
7547 DEBUG(6,("Filling port number [%d] with port [%s]\n", i, qlines[i]));
7548 fill_port_1(&ports[i], qlines[i]);
7551 TALLOC_FREE(qlines);
7553 *returned = numlines;
7555 /* check the required size. */
7556 for (i=0; i<*returned; i++) {
7557 DEBUGADD(6,("adding port [%d]'s size\n", i));
7558 *needed += spoolss_size_port_info_1(&ports[i]);
7561 if (*needed > offered) {
7562 result = WERR_INSUFFICIENT_BUFFER;
7566 if (!rpcbuf_alloc_size(buffer, *needed)) {
7567 result = WERR_NOMEM;
7571 /* fill the buffer with the ports structures */
7572 for (i=0; i<*returned; i++) {
7573 DEBUGADD(6,("adding port [%d] to buffer\n", i));
7574 smb_io_port_1("", buffer, &ports[i], 0);
7580 if ( !W_ERROR_IS_OK(result) )
7586 /****************************************************************************
7588 ****************************************************************************/
7590 static WERROR enumports_level_2(RPC_BUFFER *buffer, uint32 offered, uint32 *needed, uint32 *returned)
7592 PORT_INFO_2 *ports=NULL;
7594 WERROR result = WERR_OK;
7595 char **qlines = NULL;
7598 result = enumports_hook(talloc_tos(), &numlines, &qlines );
7599 if ( !W_ERROR_IS_OK(result)) {
7600 TALLOC_FREE(qlines);
7605 if((ports=SMB_MALLOC_ARRAY( PORT_INFO_2, numlines)) == NULL) {
7606 TALLOC_FREE(qlines);
7610 for (i=0; i<numlines; i++) {
7611 DEBUG(6,("Filling port number [%d] with port [%s]\n", i, qlines[i]));
7612 fill_port_2(&(ports[i]), qlines[i]);
7616 TALLOC_FREE(qlines);
7618 *returned = numlines;
7620 /* check the required size. */
7621 for (i=0; i<*returned; i++) {
7622 DEBUGADD(6,("adding port [%d]'s size\n", i));
7623 *needed += spoolss_size_port_info_2(&ports[i]);
7626 if (*needed > offered) {
7627 result = WERR_INSUFFICIENT_BUFFER;
7631 if (!rpcbuf_alloc_size(buffer, *needed)) {
7632 result = WERR_NOMEM;
7636 /* fill the buffer with the ports structures */
7637 for (i=0; i<*returned; i++) {
7638 DEBUGADD(6,("adding port [%d] to buffer\n", i));
7639 smb_io_port_2("", buffer, &ports[i], 0);
7645 if ( !W_ERROR_IS_OK(result) )
7651 /****************************************************************************
7653 ****************************************************************************/
7655 WERROR _spoolss_enumports( pipes_struct *p, SPOOL_Q_ENUMPORTS *q_u, SPOOL_R_ENUMPORTS *r_u)
7657 uint32 level = q_u->level;
7658 RPC_BUFFER *buffer = NULL;
7659 uint32 offered = q_u->offered;
7660 uint32 *needed = &r_u->needed;
7661 uint32 *returned = &r_u->returned;
7663 /* that's an [in out] buffer */
7665 if (!q_u->buffer && (offered!=0)) {
7666 return WERR_INVALID_PARAM;
7669 rpcbuf_move(q_u->buffer, &r_u->buffer);
7670 buffer = r_u->buffer;
7672 DEBUG(4,("_spoolss_enumports\n"));
7679 return enumports_level_1(buffer, offered, needed, returned);
7681 return enumports_level_2(buffer, offered, needed, returned);
7683 return WERR_UNKNOWN_LEVEL;
7687 /****************************************************************************
7688 ****************************************************************************/
7690 static WERROR spoolss_addprinterex_level_2( pipes_struct *p, const UNISTR2 *uni_srv_name,
7691 const SPOOL_PRINTER_INFO_LEVEL *info,
7692 DEVICEMODE *devmode, SEC_DESC_BUF *sec_desc_buf,
7693 uint32 user_switch, const SPOOL_USER_CTR *user,
7696 NT_PRINTER_INFO_LEVEL *printer = NULL;
7699 WERROR err = WERR_OK;
7701 if ( !(printer = TALLOC_ZERO_P(NULL, NT_PRINTER_INFO_LEVEL)) ) {
7702 DEBUG(0,("spoolss_addprinterex_level_2: malloc fail.\n"));
7706 /* convert from UNICODE to ASCII - this allocates the info_2 struct inside *printer.*/
7707 if (!convert_printer_info(info, printer, 2)) {
7708 free_a_printer(&printer, 2);
7712 /* check to see if the printer already exists */
7714 if ((snum = print_queue_snum(printer->info_2->sharename)) != -1) {
7715 DEBUG(5, ("spoolss_addprinterex_level_2: Attempted to add a printer named [%s] when one already existed!\n",
7716 printer->info_2->sharename));
7717 free_a_printer(&printer, 2);
7718 return WERR_PRINTER_ALREADY_EXISTS;
7721 /* FIXME!!! smbd should check to see if the driver is installed before
7722 trying to add a printer like this --jerry */
7724 if (*lp_addprinter_cmd() ) {
7725 if ( !add_printer_hook(p->mem_ctx, p->server_info->ptok,
7727 free_a_printer(&printer,2);
7728 return WERR_ACCESS_DENIED;
7731 DEBUG(0,("spoolss_addprinterex_level_2: add printer for printer %s called and no"
7732 "smb.conf parameter \"addprinter command\" is defined. This"
7733 "parameter must exist for this call to succeed\n",
7734 printer->info_2->sharename ));
7737 /* use our primary netbios name since get_a_printer() will convert
7738 it to what the client expects on a case by case basis */
7740 slprintf(name, sizeof(name)-1, "\\\\%s\\%s", global_myname(),
7741 printer->info_2->sharename);
7744 if ((snum = print_queue_snum(printer->info_2->sharename)) == -1) {
7745 free_a_printer(&printer,2);
7746 return WERR_ACCESS_DENIED;
7749 /* you must be a printer admin to add a new printer */
7750 if (!print_access_check(NULL, snum, PRINTER_ACCESS_ADMINISTER)) {
7751 free_a_printer(&printer,2);
7752 return WERR_ACCESS_DENIED;
7756 * Do sanity check on the requested changes for Samba.
7759 if (!check_printer_ok(printer->info_2, snum)) {
7760 free_a_printer(&printer,2);
7761 return WERR_INVALID_PARAM;
7765 * When a printer is created, the drivername bound to the printer is used
7766 * to lookup previously saved driver initialization info, which is then
7767 * bound to the new printer, simulating what happens in the Windows arch.
7772 set_driver_init(printer, 2);
7776 /* A valid devmode was included, convert and link it
7778 DEBUGADD(10, ("spoolss_addprinterex_level_2: devmode included, converting\n"));
7780 if (!convert_devicemode(printer->info_2->printername, devmode,
7781 &printer->info_2->devmode))
7785 /* write the ASCII on disk */
7786 err = mod_a_printer(printer, 2);
7787 if (!W_ERROR_IS_OK(err)) {
7788 free_a_printer(&printer,2);
7792 if (!open_printer_hnd(p, handle, name, PRINTER_ACCESS_ADMINISTER)) {
7793 /* Handle open failed - remove addition. */
7794 del_a_printer(printer->info_2->sharename);
7795 free_a_printer(&printer,2);
7796 return WERR_ACCESS_DENIED;
7799 update_c_setprinter(False);
7800 free_a_printer(&printer,2);
7805 /****************************************************************************
7806 ****************************************************************************/
7808 WERROR _spoolss_addprinterex( pipes_struct *p, SPOOL_Q_ADDPRINTEREX *q_u, SPOOL_R_ADDPRINTEREX *r_u)
7810 UNISTR2 *uni_srv_name = q_u->server_name;
7811 uint32 level = q_u->level;
7812 SPOOL_PRINTER_INFO_LEVEL *info = &q_u->info;
7813 DEVICEMODE *devmode = q_u->devmode_ctr.devmode;
7814 SEC_DESC_BUF *sdb = q_u->secdesc_ctr;
7815 uint32 user_switch = q_u->user_switch;
7816 SPOOL_USER_CTR *user = &q_u->user_ctr;
7817 POLICY_HND *handle = &r_u->handle;
7821 /* we don't handle yet */
7822 /* but I know what to do ... */
7823 return WERR_UNKNOWN_LEVEL;
7825 return spoolss_addprinterex_level_2(p, uni_srv_name, info,
7827 user_switch, user, handle);
7829 return WERR_UNKNOWN_LEVEL;
7833 /****************************************************************************
7834 ****************************************************************************/
7836 WERROR _spoolss_addprinterdriver(pipes_struct *p, SPOOL_Q_ADDPRINTERDRIVER *q_u, SPOOL_R_ADDPRINTERDRIVER *r_u)
7838 uint32 level = q_u->level;
7839 SPOOL_PRINTER_DRIVER_INFO_LEVEL *info = &q_u->info;
7840 WERROR err = WERR_OK;
7841 NT_PRINTER_DRIVER_INFO_LEVEL driver;
7842 fstring driver_name;
7845 ZERO_STRUCT(driver);
7847 if (!convert_printer_driver_info(info, &driver, level)) {
7852 DEBUG(5,("Cleaning driver's information\n"));
7853 err = clean_up_driver_struct(p, driver, level);
7854 if (!W_ERROR_IS_OK(err))
7857 DEBUG(5,("Moving driver to final destination\n"));
7858 if( !W_ERROR_IS_OK(err = move_driver_to_download_area(p, driver, level,
7863 if (add_a_printer_driver(driver, level)!=0) {
7864 err = WERR_ACCESS_DENIED;
7870 fstrcpy(driver_name,
7871 driver.info_3->name ? driver.info_3->name : "");
7874 fstrcpy(driver_name,
7875 driver.info_6->name ? driver.info_6->name : "");
7880 * I think this is where he DrvUpgradePrinter() hook would be
7881 * be called in a driver's interface DLL on a Windows NT 4.0/2k
7882 * server. Right now, we just need to send ourselves a message
7883 * to update each printer bound to this driver. --jerry
7886 if (!srv_spoolss_drv_upgrade_printer(driver_name)) {
7887 DEBUG(0,("_spoolss_addprinterdriver: Failed to send message about upgrading driver [%s]!\n",
7892 * Based on the version (e.g. driver destination dir: 0=9x,2=Nt/2k,3=2k/Xp),
7893 * decide if the driver init data should be deleted. The rules are:
7894 * 1) never delete init data if it is a 9x driver, they don't use it anyway
7895 * 2) delete init data only if there is no 2k/Xp driver
7896 * 3) always delete init data
7897 * The generalized rule is always use init data from the highest order driver.
7898 * It is necessary to follow the driver install by an initialization step to
7899 * finish off this process.
7902 version = driver.info_3->cversion;
7903 else if (level == 6)
7904 version = driver.info_6->version;
7909 * 9x printer driver - never delete init data
7912 DEBUG(10,("_spoolss_addprinterdriver: init data not deleted for 9x driver [%s]\n",
7917 * Nt or 2k (compatiblity mode) printer driver - only delete init data if
7918 * there is no 2k/Xp driver init data for this driver name.
7922 NT_PRINTER_DRIVER_INFO_LEVEL driver1;
7924 if (!W_ERROR_IS_OK(get_a_printer_driver(&driver1, 3, driver_name, "Windows NT x86", 3))) {
7926 * No 2k/Xp driver found, delete init data (if any) for the new Nt driver.
7928 if (!del_driver_init(driver_name))
7929 DEBUG(6,("_spoolss_addprinterdriver: del_driver_init(%s) Nt failed!\n", driver_name));
7932 * a 2k/Xp driver was found, don't delete init data because Nt driver will use it.
7934 free_a_printer_driver(driver1,3);
7935 DEBUG(10,("_spoolss_addprinterdriver: init data not deleted for Nt driver [%s]\n",
7942 * 2k or Xp printer driver - always delete init data
7945 if (!del_driver_init(driver_name))
7946 DEBUG(6,("_spoolss_addprinterdriver: del_driver_init(%s) 2k/Xp failed!\n", driver_name));
7950 DEBUG(0,("_spoolss_addprinterdriver: invalid level=%d\n", level));
7956 free_a_printer_driver(driver, level);
7960 /********************************************************************
7961 * spoolss_addprinterdriverex
7962 ********************************************************************/
7964 WERROR _spoolss_addprinterdriverex(pipes_struct *p, SPOOL_Q_ADDPRINTERDRIVEREX *q_u, SPOOL_R_ADDPRINTERDRIVEREX *r_u)
7966 SPOOL_Q_ADDPRINTERDRIVER q_u_local;
7967 SPOOL_R_ADDPRINTERDRIVER r_u_local;
7970 * we only support the semantics of AddPrinterDriver()
7971 * i.e. only copy files that are newer than existing ones
7974 if ( q_u->copy_flags != APD_COPY_NEW_FILES )
7975 return WERR_ACCESS_DENIED;
7977 ZERO_STRUCT(q_u_local);
7978 ZERO_STRUCT(r_u_local);
7980 /* just pass the information off to _spoolss_addprinterdriver() */
7981 q_u_local.server_name_ptr = q_u->server_name_ptr;
7982 copy_unistr2(&q_u_local.server_name, &q_u->server_name);
7983 q_u_local.level = q_u->level;
7984 memcpy( &q_u_local.info, &q_u->info, sizeof(SPOOL_PRINTER_DRIVER_INFO_LEVEL) );
7986 return _spoolss_addprinterdriver( p, &q_u_local, &r_u_local );
7989 /****************************************************************************
7990 ****************************************************************************/
7992 static void fill_driverdir_1(DRIVER_DIRECTORY_1 *info, char *name)
7994 init_unistr(&info->name, name);
7997 /****************************************************************************
7998 ****************************************************************************/
8000 static WERROR getprinterdriverdir_level_1(UNISTR2 *name, UNISTR2 *uni_environment, RPC_BUFFER *buffer, uint32 offered, uint32 *needed)
8003 char *long_archi = NULL;
8004 char *servername = NULL;
8005 const char *pservername = NULL;
8006 const char *short_archi;
8007 DRIVER_DIRECTORY_1 *info=NULL;
8008 WERROR result = WERR_OK;
8009 TALLOC_CTX *ctx = talloc_tos();
8011 servername = unistr2_to_ascii_talloc(ctx, name);
8015 long_archi = unistr2_to_ascii_talloc(ctx, uni_environment);
8020 pservername = canon_servername(servername);
8022 if ( !is_myname_or_ipaddr(pservername))
8023 return WERR_INVALID_PARAM;
8025 if (!(short_archi = get_short_archi(long_archi)))
8026 return WERR_INVALID_ENVIRONMENT;
8028 if((info=SMB_MALLOC_P(DRIVER_DIRECTORY_1)) == NULL)
8031 path = talloc_asprintf(ctx,
8032 "\\\\%s\\print$\\%s", pservername, short_archi);
8034 result = WERR_NOMEM;
8038 DEBUG(4,("printer driver directory: [%s]\n", path));
8040 fill_driverdir_1(info, path);
8042 *needed += spoolss_size_driverdir_info_1(info);
8044 if (*needed > offered) {
8045 result = WERR_INSUFFICIENT_BUFFER;
8049 if (!rpcbuf_alloc_size(buffer, *needed)) {
8050 result = WERR_NOMEM;
8054 smb_io_driverdir_1("", buffer, info, 0);
8062 /****************************************************************************
8063 ****************************************************************************/
8065 WERROR _spoolss_getprinterdriverdirectory(pipes_struct *p, SPOOL_Q_GETPRINTERDRIVERDIR *q_u, SPOOL_R_GETPRINTERDRIVERDIR *r_u)
8067 UNISTR2 *name = &q_u->name;
8068 UNISTR2 *uni_environment = &q_u->environment;
8069 uint32 level = q_u->level;
8070 RPC_BUFFER *buffer = NULL;
8071 uint32 offered = q_u->offered;
8072 uint32 *needed = &r_u->needed;
8074 /* that's an [in out] buffer */
8076 if (!q_u->buffer && (offered!=0)) {
8077 return WERR_INVALID_PARAM;
8080 rpcbuf_move(q_u->buffer, &r_u->buffer);
8081 buffer = r_u->buffer;
8083 DEBUG(4,("_spoolss_getprinterdriverdirectory\n"));
8089 return getprinterdriverdir_level_1(name, uni_environment, buffer, offered, needed);
8091 return WERR_UNKNOWN_LEVEL;
8095 /****************************************************************************
8096 ****************************************************************************/
8098 WERROR _spoolss_enumprinterdata(pipes_struct *p, SPOOL_Q_ENUMPRINTERDATA *q_u, SPOOL_R_ENUMPRINTERDATA *r_u)
8100 POLICY_HND *handle = &q_u->handle;
8101 uint32 idx = q_u->index;
8102 uint32 in_value_len = q_u->valuesize;
8103 uint32 in_data_len = q_u->datasize;
8104 uint32 *out_max_value_len = &r_u->valuesize;
8105 uint16 **out_value = &r_u->value;
8106 uint32 *out_value_len = &r_u->realvaluesize;
8107 uint32 *out_type = &r_u->type;
8108 uint32 *out_max_data_len = &r_u->datasize;
8109 uint8 **data_out = &r_u->data;
8110 uint32 *out_data_len = &r_u->realdatasize;
8112 NT_PRINTER_INFO_LEVEL *printer = NULL;
8114 uint32 biggest_valuesize;
8115 uint32 biggest_datasize;
8117 Printer_entry *Printer = find_printer_index_by_hnd(p, handle);
8120 REGISTRY_VALUE *val = NULL;
8121 NT_PRINTER_DATA *p_data;
8122 int i, key_index, num_values;
8127 *out_max_data_len = 0;
8131 DEBUG(5,("spoolss_enumprinterdata\n"));
8134 DEBUG(2,("_spoolss_enumprinterdata: Invalid handle (%s:%u:%u).\n", OUR_HANDLE(handle)));
8138 if (!get_printer_snum(p,handle, &snum, NULL))
8141 result = get_a_printer(Printer, &printer, 2, lp_const_servicename(snum));
8142 if (!W_ERROR_IS_OK(result))
8145 p_data = printer->info_2->data;
8146 key_index = lookup_printerkey( p_data, SPOOL_PRINTERDATA_KEY );
8151 * The NT machine wants to know the biggest size of value and data
8153 * cf: MSDN EnumPrinterData remark section
8156 if ( !in_value_len && !in_data_len && (key_index != -1) )
8158 DEBUGADD(6,("Activating NT mega-hack to find sizes\n"));
8160 biggest_valuesize = 0;
8161 biggest_datasize = 0;
8163 num_values = regval_ctr_numvals( p_data->keys[key_index].values );
8165 for ( i=0; i<num_values; i++ )
8167 val = regval_ctr_specific_value( p_data->keys[key_index].values, i );
8169 name_length = strlen(val->valuename);
8170 if ( strlen(val->valuename) > biggest_valuesize )
8171 biggest_valuesize = name_length;
8173 if ( val->size > biggest_datasize )
8174 biggest_datasize = val->size;
8176 DEBUG(6,("current values: [%d], [%d]\n", biggest_valuesize,
8180 /* the value is an UNICODE string but real_value_size is the length
8181 in bytes including the trailing 0 */
8183 *out_value_len = 2 * (1+biggest_valuesize);
8184 *out_data_len = biggest_datasize;
8186 DEBUG(6,("final values: [%d], [%d]\n", *out_value_len, *out_data_len));
8192 * the value len is wrong in NT sp3
8193 * that's the number of bytes not the number of unicode chars
8196 if ( key_index != -1 )
8197 val = regval_ctr_specific_value( p_data->keys[key_index].values, idx );
8202 /* out_value should default to "" or else NT4 has
8203 problems unmarshalling the response */
8205 *out_max_value_len=(in_value_len/sizeof(uint16));
8208 if((*out_value=(uint16 *)TALLOC_ZERO(p->mem_ctx, in_value_len*sizeof(uint8))) == NULL)
8210 result = WERR_NOMEM;
8213 *out_value_len = (uint32)rpcstr_push((char *)*out_value, "", in_value_len, 0);
8219 /* the data is counted in bytes */
8221 *out_max_data_len = in_data_len;
8222 *out_data_len = in_data_len;
8224 /* only allocate when given a non-zero data_len */
8226 if ( in_data_len && ((*data_out=(uint8 *)TALLOC_ZERO(p->mem_ctx, in_data_len*sizeof(uint8))) == NULL) )
8228 result = WERR_NOMEM;
8232 result = WERR_NO_MORE_ITEMS;
8238 * - counted in bytes in the request
8239 * - counted in UNICODE chars in the max reply
8240 * - counted in bytes in the real size
8242 * take a pause *before* coding not *during* coding
8246 *out_max_value_len=(in_value_len/sizeof(uint16));
8248 if ( (*out_value = (uint16 *)TALLOC_ZERO(p->mem_ctx, in_value_len*sizeof(uint8))) == NULL )
8250 result = WERR_NOMEM;
8254 *out_value_len = (uint32)rpcstr_push((char *)*out_value, regval_name(val), (size_t)in_value_len, 0);
8262 *out_type = regval_type( val );
8264 /* data - counted in bytes */
8266 *out_max_data_len = in_data_len;
8267 if ( in_data_len && (*data_out = (uint8 *)TALLOC_ZERO(p->mem_ctx, in_data_len*sizeof(uint8))) == NULL)
8269 result = WERR_NOMEM;
8272 data_len = regval_size(val);
8273 if ( *data_out && data_len )
8274 memcpy( *data_out, regval_data_p(val), data_len );
8275 *out_data_len = data_len;
8279 free_a_printer(&printer, 2);
8283 /****************************************************************************
8284 ****************************************************************************/
8286 WERROR _spoolss_setprinterdata( pipes_struct *p, SPOOL_Q_SETPRINTERDATA *q_u, SPOOL_R_SETPRINTERDATA *r_u)
8288 POLICY_HND *handle = &q_u->handle;
8289 UNISTR2 *value = &q_u->value;
8290 uint32 type = q_u->type;
8291 uint8 *data = q_u->data;
8292 uint32 real_len = q_u->real_len;
8294 NT_PRINTER_INFO_LEVEL *printer = NULL;
8296 WERROR status = WERR_OK;
8297 Printer_entry *Printer=find_printer_index_by_hnd(p, handle);
8300 DEBUG(5,("spoolss_setprinterdata\n"));
8303 DEBUG(2,("_spoolss_setprinterdata: Invalid handle (%s:%u:%u).\n", OUR_HANDLE(handle)));
8307 if ( Printer->printer_type == SPLHND_SERVER ) {
8308 DEBUG(10,("_spoolss_setprinterdata: Not implemented for server handles yet\n"));
8309 return WERR_INVALID_PARAM;
8312 if (!get_printer_snum(p,handle, &snum, NULL))
8316 * Access check : NT returns "access denied" if you make a
8317 * SetPrinterData call without the necessary privildge.
8318 * we were originally returning OK if nothing changed
8319 * which made Win2k issue **a lot** of SetPrinterData
8320 * when connecting to a printer --jerry
8323 if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER)
8325 DEBUG(3, ("_spoolss_setprinterdata: change denied by handle access permissions\n"));
8326 status = WERR_ACCESS_DENIED;
8330 status = get_a_printer(Printer, &printer, 2, lp_const_servicename(snum));
8331 if (!W_ERROR_IS_OK(status))
8334 unistr2_to_ascii(valuename, value, sizeof(valuename));
8337 * When client side code sets a magic printer data key, detect it and save
8338 * the current printer data and the magic key's data (its the DEVMODE) for
8339 * future printer/driver initializations.
8341 if ( (type == REG_BINARY) && strequal( valuename, PHANTOM_DEVMODE_KEY))
8343 /* Set devmode and printer initialization info */
8344 status = save_driver_init( printer, 2, data, real_len );
8346 srv_spoolss_reset_printerdata( printer->info_2->drivername );
8350 status = set_printer_dataex( printer, SPOOL_PRINTERDATA_KEY, valuename,
8351 type, data, real_len );
8352 if ( W_ERROR_IS_OK(status) )
8353 status = mod_a_printer(printer, 2);
8357 free_a_printer(&printer, 2);
8362 /****************************************************************************
8363 ****************************************************************************/
8365 WERROR _spoolss_resetprinter(pipes_struct *p, SPOOL_Q_RESETPRINTER *q_u, SPOOL_R_RESETPRINTER *r_u)
8367 POLICY_HND *handle = &q_u->handle;
8368 Printer_entry *Printer=find_printer_index_by_hnd(p, handle);
8371 DEBUG(5,("_spoolss_resetprinter\n"));
8374 * All we do is to check to see if the handle and queue is valid.
8375 * This call really doesn't mean anything to us because we only
8376 * support RAW printing. --jerry
8380 DEBUG(2,("_spoolss_resetprinter: Invalid handle (%s:%u:%u).\n", OUR_HANDLE(handle)));
8384 if (!get_printer_snum(p,handle, &snum, NULL))
8388 /* blindly return success */
8393 /****************************************************************************
8394 ****************************************************************************/
8396 WERROR _spoolss_deleteprinterdata(pipes_struct *p, SPOOL_Q_DELETEPRINTERDATA *q_u, SPOOL_R_DELETEPRINTERDATA *r_u)
8398 POLICY_HND *handle = &q_u->handle;
8399 UNISTR2 *value = &q_u->valuename;
8401 NT_PRINTER_INFO_LEVEL *printer = NULL;
8403 WERROR status = WERR_OK;
8404 Printer_entry *Printer=find_printer_index_by_hnd(p, handle);
8405 char *valuename = NULL;
8406 TALLOC_CTX *ctx = p->mem_ctx;
8408 DEBUG(5,("spoolss_deleteprinterdata\n"));
8411 DEBUG(2,("_spoolss_deleteprinterdata: Invalid handle (%s:%u:%u).\n", OUR_HANDLE(handle)));
8415 if (!get_printer_snum(p, handle, &snum, NULL))
8418 if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
8419 DEBUG(3, ("_spoolss_deleteprinterdata: printer properties change denied by handle\n"));
8420 return WERR_ACCESS_DENIED;
8423 status = get_a_printer(Printer, &printer, 2, lp_const_servicename(snum));
8424 if (!W_ERROR_IS_OK(status))
8427 valuename = unistr2_to_ascii_talloc(ctx, value);
8429 free_a_printer(&printer, 2);
8433 status = delete_printer_dataex( printer, SPOOL_PRINTERDATA_KEY, valuename );
8435 if ( W_ERROR_IS_OK(status) )
8436 mod_a_printer( printer, 2 );
8438 free_a_printer(&printer, 2);
8439 TALLOC_FREE(valuename);
8444 /****************************************************************************
8445 ****************************************************************************/
8447 WERROR _spoolss_addform( pipes_struct *p, SPOOL_Q_ADDFORM *q_u, SPOOL_R_ADDFORM *r_u)
8449 POLICY_HND *handle = &q_u->handle;
8450 FORM *form = &q_u->form;
8451 nt_forms_struct tmpForm;
8453 WERROR status = WERR_OK;
8454 NT_PRINTER_INFO_LEVEL *printer = NULL;
8457 nt_forms_struct *list=NULL;
8458 Printer_entry *Printer = find_printer_index_by_hnd(p, handle);
8460 DEBUG(5,("spoolss_addform\n"));
8463 DEBUG(2,("_spoolss_addform: Invalid handle (%s:%u:%u).\n", OUR_HANDLE(handle)));
8468 /* forms can be added on printer of on the print server handle */
8470 if ( Printer->printer_type == SPLHND_PRINTER )
8472 if (!get_printer_snum(p,handle, &snum, NULL))
8475 status = get_a_printer(Printer, &printer, 2, lp_const_servicename(snum));
8476 if (!W_ERROR_IS_OK(status))
8480 if ( !(Printer->access_granted & (PRINTER_ACCESS_ADMINISTER|SERVER_ACCESS_ADMINISTER)) ) {
8481 DEBUG(2,("_spoolss_addform: denied by handle permissions.\n"));
8482 status = WERR_ACCESS_DENIED;
8486 /* can't add if builtin */
8488 if (get_a_builtin_ntform(&form->name,&tmpForm)) {
8489 status = WERR_FILE_EXISTS;
8493 count = get_ntforms(&list);
8495 if(!add_a_form(&list, form, &count)) {
8496 status = WERR_NOMEM;
8500 write_ntforms(&list, count);
8503 * ChangeID must always be set if this is a printer
8506 if ( Printer->printer_type == SPLHND_PRINTER )
8507 status = mod_a_printer(printer, 2);
8511 free_a_printer(&printer, 2);
8517 /****************************************************************************
8518 ****************************************************************************/
8520 WERROR _spoolss_deleteform( pipes_struct *p, SPOOL_Q_DELETEFORM *q_u, SPOOL_R_DELETEFORM *r_u)
8522 POLICY_HND *handle = &q_u->handle;
8523 UNISTR2 *form_name = &q_u->name;
8524 nt_forms_struct tmpForm;
8526 nt_forms_struct *list=NULL;
8527 Printer_entry *Printer = find_printer_index_by_hnd(p, handle);
8529 WERROR status = WERR_OK;
8530 NT_PRINTER_INFO_LEVEL *printer = NULL;
8532 DEBUG(5,("spoolss_deleteform\n"));
8535 DEBUG(2,("_spoolss_deleteform: Invalid handle (%s:%u:%u).\n", OUR_HANDLE(handle)));
8539 /* forms can be deleted on printer of on the print server handle */
8541 if ( Printer->printer_type == SPLHND_PRINTER )
8543 if (!get_printer_snum(p,handle, &snum, NULL))
8546 status = get_a_printer(Printer, &printer, 2, lp_const_servicename(snum));
8547 if (!W_ERROR_IS_OK(status))
8551 if ( !(Printer->access_granted & (PRINTER_ACCESS_ADMINISTER|SERVER_ACCESS_ADMINISTER)) ) {
8552 DEBUG(2,("_spoolss_deleteform: denied by handle permissions.\n"));
8553 status = WERR_ACCESS_DENIED;
8557 /* can't delete if builtin */
8559 if (get_a_builtin_ntform(form_name,&tmpForm)) {
8560 status = WERR_INVALID_PARAM;
8564 count = get_ntforms(&list);
8566 if ( !delete_a_form(&list, form_name, &count, &status ))
8570 * ChangeID must always be set if this is a printer
8573 if ( Printer->printer_type == SPLHND_PRINTER )
8574 status = mod_a_printer(printer, 2);
8578 free_a_printer(&printer, 2);
8584 /****************************************************************************
8585 ****************************************************************************/
8587 WERROR _spoolss_setform(pipes_struct *p, SPOOL_Q_SETFORM *q_u, SPOOL_R_SETFORM *r_u)
8589 POLICY_HND *handle = &q_u->handle;
8590 FORM *form = &q_u->form;
8591 nt_forms_struct tmpForm;
8593 WERROR status = WERR_OK;
8594 NT_PRINTER_INFO_LEVEL *printer = NULL;
8597 nt_forms_struct *list=NULL;
8598 Printer_entry *Printer = find_printer_index_by_hnd(p, handle);
8600 DEBUG(5,("spoolss_setform\n"));
8603 DEBUG(2,("_spoolss_setform: Invalid handle (%s:%u:%u).\n", OUR_HANDLE(handle)));
8607 /* forms can be modified on printer of on the print server handle */
8609 if ( Printer->printer_type == SPLHND_PRINTER )
8611 if (!get_printer_snum(p,handle, &snum, NULL))
8614 status = get_a_printer(Printer, &printer, 2, lp_const_servicename(snum));
8615 if (!W_ERROR_IS_OK(status))
8619 if ( !(Printer->access_granted & (PRINTER_ACCESS_ADMINISTER|SERVER_ACCESS_ADMINISTER)) ) {
8620 DEBUG(2,("_spoolss_setform: denied by handle permissions\n"));
8621 status = WERR_ACCESS_DENIED;
8625 /* can't set if builtin */
8626 if (get_a_builtin_ntform(&form->name,&tmpForm)) {
8627 status = WERR_INVALID_PARAM;
8631 count = get_ntforms(&list);
8632 update_a_form(&list, form, count);
8633 write_ntforms(&list, count);
8636 * ChangeID must always be set if this is a printer
8639 if ( Printer->printer_type == SPLHND_PRINTER )
8640 status = mod_a_printer(printer, 2);
8645 free_a_printer(&printer, 2);
8651 /****************************************************************************
8652 enumprintprocessors level 1.
8653 ****************************************************************************/
8655 static WERROR enumprintprocessors_level_1(RPC_BUFFER *buffer, uint32 offered, uint32 *needed, uint32 *returned)
8657 PRINTPROCESSOR_1 *info_1=NULL;
8658 WERROR result = WERR_OK;
8660 if((info_1 = SMB_MALLOC_P(PRINTPROCESSOR_1)) == NULL)
8665 init_unistr(&info_1->name, "winprint");
8667 *needed += spoolss_size_printprocessor_info_1(info_1);
8669 if (*needed > offered) {
8670 result = WERR_INSUFFICIENT_BUFFER;
8674 if (!rpcbuf_alloc_size(buffer, *needed)) {
8675 result = WERR_NOMEM;
8679 smb_io_printprocessor_info_1("", buffer, info_1, 0);
8684 if ( !W_ERROR_IS_OK(result) )
8690 /****************************************************************************
8691 ****************************************************************************/
8693 WERROR _spoolss_enumprintprocessors(pipes_struct *p, SPOOL_Q_ENUMPRINTPROCESSORS *q_u, SPOOL_R_ENUMPRINTPROCESSORS *r_u)
8695 uint32 level = q_u->level;
8696 RPC_BUFFER *buffer = NULL;
8697 uint32 offered = q_u->offered;
8698 uint32 *needed = &r_u->needed;
8699 uint32 *returned = &r_u->returned;
8701 /* that's an [in out] buffer */
8703 if (!q_u->buffer && (offered!=0)) {
8704 return WERR_INVALID_PARAM;
8707 rpcbuf_move(q_u->buffer, &r_u->buffer);
8708 buffer = r_u->buffer;
8710 DEBUG(5,("spoolss_enumprintprocessors\n"));
8713 * Enumerate the print processors ...
8715 * Just reply with "winprint", to keep NT happy
8716 * and I can use my nice printer checker.
8724 return enumprintprocessors_level_1(buffer, offered, needed, returned);
8726 return WERR_UNKNOWN_LEVEL;
8730 /****************************************************************************
8731 enumprintprocdatatypes level 1.
8732 ****************************************************************************/
8734 static WERROR enumprintprocdatatypes_level_1(RPC_BUFFER *buffer, uint32 offered, uint32 *needed, uint32 *returned)
8736 PRINTPROCDATATYPE_1 *info_1=NULL;
8737 WERROR result = WERR_OK;
8739 if((info_1 = SMB_MALLOC_P(PRINTPROCDATATYPE_1)) == NULL)
8744 init_unistr(&info_1->name, "RAW");
8746 *needed += spoolss_size_printprocdatatype_info_1(info_1);
8748 if (*needed > offered) {
8749 result = WERR_INSUFFICIENT_BUFFER;
8753 if (!rpcbuf_alloc_size(buffer, *needed)) {
8754 result = WERR_NOMEM;
8758 smb_io_printprocdatatype_info_1("", buffer, info_1, 0);
8763 if ( !W_ERROR_IS_OK(result) )
8769 /****************************************************************************
8770 ****************************************************************************/
8772 WERROR _spoolss_enumprintprocdatatypes(pipes_struct *p, SPOOL_Q_ENUMPRINTPROCDATATYPES *q_u, SPOOL_R_ENUMPRINTPROCDATATYPES *r_u)
8774 uint32 level = q_u->level;
8775 RPC_BUFFER *buffer = NULL;
8776 uint32 offered = q_u->offered;
8777 uint32 *needed = &r_u->needed;
8778 uint32 *returned = &r_u->returned;
8780 /* that's an [in out] buffer */
8782 if (!q_u->buffer && (offered!=0)) {
8783 return WERR_INVALID_PARAM;
8786 rpcbuf_move(q_u->buffer, &r_u->buffer);
8787 buffer = r_u->buffer;
8789 DEBUG(5,("_spoolss_enumprintprocdatatypes\n"));
8796 return enumprintprocdatatypes_level_1(buffer, offered, needed, returned);
8798 return WERR_UNKNOWN_LEVEL;
8802 /****************************************************************************
8803 enumprintmonitors level 1.
8804 ****************************************************************************/
8806 static WERROR enumprintmonitors_level_1(RPC_BUFFER *buffer, uint32 offered, uint32 *needed, uint32 *returned)
8808 PRINTMONITOR_1 *info_1;
8809 WERROR result = WERR_OK;
8812 if((info_1 = SMB_MALLOC_ARRAY(PRINTMONITOR_1, 2)) == NULL)
8817 init_unistr(&(info_1[0].name), SPL_LOCAL_PORT );
8818 init_unistr(&(info_1[1].name), SPL_TCPIP_PORT );
8820 for ( i=0; i<*returned; i++ ) {
8821 *needed += spoolss_size_printmonitor_info_1(&info_1[i]);
8824 if (*needed > offered) {
8825 result = WERR_INSUFFICIENT_BUFFER;
8829 if (!rpcbuf_alloc_size(buffer, *needed)) {
8830 result = WERR_NOMEM;
8834 for ( i=0; i<*returned; i++ ) {
8835 smb_io_printmonitor_info_1("", buffer, &info_1[i], 0);
8841 if ( !W_ERROR_IS_OK(result) )
8847 /****************************************************************************
8848 enumprintmonitors level 2.
8849 ****************************************************************************/
8851 static WERROR enumprintmonitors_level_2(RPC_BUFFER *buffer, uint32 offered, uint32 *needed, uint32 *returned)
8853 PRINTMONITOR_2 *info_2;
8854 WERROR result = WERR_OK;
8857 if((info_2 = SMB_MALLOC_ARRAY(PRINTMONITOR_2, 2)) == NULL)
8862 init_unistr( &(info_2[0].name), SPL_LOCAL_PORT );
8863 init_unistr( &(info_2[0].environment), "Windows NT X86" );
8864 init_unistr( &(info_2[0].dll_name), "localmon.dll" );
8866 init_unistr( &(info_2[1].name), SPL_TCPIP_PORT );
8867 init_unistr( &(info_2[1].environment), "Windows NT X86" );
8868 init_unistr( &(info_2[1].dll_name), "tcpmon.dll" );
8870 for ( i=0; i<*returned; i++ ) {
8871 *needed += spoolss_size_printmonitor_info_2(&info_2[i]);
8874 if (*needed > offered) {
8875 result = WERR_INSUFFICIENT_BUFFER;
8879 if (!rpcbuf_alloc_size(buffer, *needed)) {
8880 result = WERR_NOMEM;
8884 for ( i=0; i<*returned; i++ ) {
8885 smb_io_printmonitor_info_2("", buffer, &info_2[i], 0);
8891 if ( !W_ERROR_IS_OK(result) )
8897 /****************************************************************************
8898 ****************************************************************************/
8900 WERROR _spoolss_enumprintmonitors(pipes_struct *p, SPOOL_Q_ENUMPRINTMONITORS *q_u, SPOOL_R_ENUMPRINTMONITORS *r_u)
8902 uint32 level = q_u->level;
8903 RPC_BUFFER *buffer = NULL;
8904 uint32 offered = q_u->offered;
8905 uint32 *needed = &r_u->needed;
8906 uint32 *returned = &r_u->returned;
8908 /* that's an [in out] buffer */
8910 if (!q_u->buffer && (offered!=0)) {
8911 return WERR_INVALID_PARAM;
8914 rpcbuf_move(q_u->buffer, &r_u->buffer);
8915 buffer = r_u->buffer;
8917 DEBUG(5,("spoolss_enumprintmonitors\n"));
8920 * Enumerate the print monitors ...
8922 * Just reply with "Local Port", to keep NT happy
8923 * and I can use my nice printer checker.
8931 return enumprintmonitors_level_1(buffer, offered, needed, returned);
8933 return enumprintmonitors_level_2(buffer, offered, needed, returned);
8935 return WERR_UNKNOWN_LEVEL;
8939 /****************************************************************************
8940 ****************************************************************************/
8942 static WERROR getjob_level_1(print_queue_struct **queue, int count, int snum,
8943 NT_PRINTER_INFO_LEVEL *ntprinter,
8944 uint32 jobid, RPC_BUFFER *buffer, uint32 offered,
8949 JOB_INFO_1 *info_1=NULL;
8950 WERROR result = WERR_OK;
8952 info_1=SMB_MALLOC_P(JOB_INFO_1);
8954 if (info_1 == NULL) {
8958 for (i=0; i<count && found==False; i++) {
8959 if ((*queue)[i].job==(int)jobid)
8965 /* NT treats not found as bad param... yet another bad choice */
8966 return WERR_INVALID_PARAM;
8969 fill_job_info_1( info_1, &((*queue)[i-1]), i, snum, ntprinter );
8971 *needed += spoolss_size_job_info_1(info_1);
8973 if (*needed > offered) {
8974 result = WERR_INSUFFICIENT_BUFFER;
8978 if (!rpcbuf_alloc_size(buffer, *needed)) {
8979 result = WERR_NOMEM;
8983 smb_io_job_info_1("", buffer, info_1, 0);
8991 /****************************************************************************
8992 ****************************************************************************/
8994 static WERROR getjob_level_2(print_queue_struct **queue, int count, int snum,
8995 NT_PRINTER_INFO_LEVEL *ntprinter,
8996 uint32 jobid, RPC_BUFFER *buffer, uint32 offered,
9003 DEVICEMODE *devmode = NULL;
9004 NT_DEVICEMODE *nt_devmode = NULL;
9006 if ( !(info_2=SMB_MALLOC_P(JOB_INFO_2)) )
9009 ZERO_STRUCTP(info_2);
9011 for ( i=0; i<count && found==False; i++ )
9013 if ((*queue)[i].job == (int)jobid)
9018 /* NT treats not found as bad param... yet another bad
9020 result = WERR_INVALID_PARAM;
9025 * if the print job does not have a DEVMODE associated with it,
9026 * just use the one for the printer. A NULL devicemode is not
9027 * a failure condition
9030 if ( !(nt_devmode=print_job_devmode( lp_const_servicename(snum), jobid )) )
9031 devmode = construct_dev_mode(lp_const_servicename(snum));
9033 if ((devmode = SMB_MALLOC_P(DEVICEMODE)) != NULL) {
9034 ZERO_STRUCTP( devmode );
9035 convert_nt_devicemode( devmode, nt_devmode );
9039 fill_job_info_2(info_2, &((*queue)[i-1]), i, snum, ntprinter, devmode);
9041 *needed += spoolss_size_job_info_2(info_2);
9043 if (*needed > offered) {
9044 result = WERR_INSUFFICIENT_BUFFER;
9048 if (!rpcbuf_alloc_size(buffer, *needed)) {
9049 result = WERR_NOMEM;
9053 smb_io_job_info_2("", buffer, info_2, 0);
9058 /* Cleanup allocated memory */
9060 free_job_info_2(info_2); /* Also frees devmode */
9066 /****************************************************************************
9067 ****************************************************************************/
9069 WERROR _spoolss_getjob( pipes_struct *p, SPOOL_Q_GETJOB *q_u, SPOOL_R_GETJOB *r_u)
9071 POLICY_HND *handle = &q_u->handle;
9072 uint32 jobid = q_u->jobid;
9073 uint32 level = q_u->level;
9074 RPC_BUFFER *buffer = NULL;
9075 uint32 offered = q_u->offered;
9076 uint32 *needed = &r_u->needed;
9077 WERROR wstatus = WERR_OK;
9078 NT_PRINTER_INFO_LEVEL *ntprinter = NULL;
9081 print_queue_struct *queue = NULL;
9082 print_status_struct prt_status;
9084 /* that's an [in out] buffer */
9086 if (!q_u->buffer && (offered!=0)) {
9087 return WERR_INVALID_PARAM;
9090 rpcbuf_move(q_u->buffer, &r_u->buffer);
9091 buffer = r_u->buffer;
9093 DEBUG(5,("spoolss_getjob\n"));
9097 if (!get_printer_snum(p, handle, &snum, NULL))
9100 wstatus = get_a_printer(NULL, &ntprinter, 2, lp_servicename(snum));
9101 if ( !W_ERROR_IS_OK(wstatus) )
9104 count = print_queue_status(snum, &queue, &prt_status);
9106 DEBUGADD(4,("count:[%d], prt_status:[%d], [%s]\n",
9107 count, prt_status.status, prt_status.message));
9111 wstatus = getjob_level_1(&queue, count, snum, ntprinter, jobid,
9112 buffer, offered, needed);
9115 wstatus = getjob_level_2(&queue, count, snum, ntprinter, jobid,
9116 buffer, offered, needed);
9119 wstatus = WERR_UNKNOWN_LEVEL;
9124 free_a_printer( &ntprinter, 2 );
9129 /********************************************************************
9130 spoolss_getprinterdataex
9132 From MSDN documentation of GetPrinterDataEx: pass request
9133 to GetPrinterData if key is "PrinterDriverData".
9134 ********************************************************************/
9136 WERROR _spoolss_getprinterdataex(pipes_struct *p, SPOOL_Q_GETPRINTERDATAEX *q_u, SPOOL_R_GETPRINTERDATAEX *r_u)
9138 POLICY_HND *handle = &q_u->handle;
9139 uint32 in_size = q_u->size;
9140 uint32 *type = &r_u->type;
9141 uint32 *out_size = &r_u->size;
9142 uint8 **data = &r_u->data;
9143 uint32 *needed = &r_u->needed;
9144 fstring keyname, valuename;
9146 Printer_entry *Printer = find_printer_index_by_hnd(p, handle);
9148 NT_PRINTER_INFO_LEVEL *printer = NULL;
9150 WERROR status = WERR_OK;
9152 DEBUG(4,("_spoolss_getprinterdataex\n"));
9154 unistr2_to_ascii(keyname, &q_u->keyname, sizeof(keyname));
9155 unistr2_to_ascii(valuename, &q_u->valuename, sizeof(valuename));
9157 DEBUG(10, ("_spoolss_getprinterdataex: key => [%s], value => [%s]\n",
9158 keyname, valuename));
9160 /* in case of problem, return some default values */
9164 *out_size = in_size;
9167 DEBUG(2,("_spoolss_getprinterdataex: Invalid handle (%s:%u:%u).\n", OUR_HANDLE(handle)));
9168 status = WERR_BADFID;
9172 /* Is the handle to a printer or to the server? */
9174 if (Printer->printer_type == SPLHND_SERVER) {
9175 DEBUG(10,("_spoolss_getprinterdataex: Not implemented for server handles yet\n"));
9176 status = WERR_INVALID_PARAM;
9180 if ( !get_printer_snum(p,handle, &snum, NULL) )
9183 status = get_a_printer(Printer, &printer, 2, lp_servicename(snum));
9184 if ( !W_ERROR_IS_OK(status) )
9187 /* check to see if the keyname is valid */
9188 if ( !strlen(keyname) ) {
9189 status = WERR_INVALID_PARAM;
9193 if ( lookup_printerkey( printer->info_2->data, keyname ) == -1 ) {
9194 DEBUG(4,("_spoolss_getprinterdataex: Invalid keyname [%s]\n", keyname ));
9195 free_a_printer( &printer, 2 );
9196 status = WERR_BADFILE;
9200 /* When given a new keyname, we should just create it */
9202 status = get_printer_dataex( p->mem_ctx, printer, keyname, valuename, type, data, needed, in_size );
9204 if (*needed > *out_size)
9205 status = WERR_MORE_DATA;
9208 if ( !W_ERROR_IS_OK(status) )
9210 DEBUG(5, ("error: allocating %d\n", *out_size));
9212 /* reply this param doesn't exist */
9216 if( (*data=(uint8 *)TALLOC_ZERO(p->mem_ctx, *out_size*sizeof(uint8))) == NULL ) {
9217 status = WERR_NOMEM;
9226 free_a_printer( &printer, 2 );
9231 /********************************************************************
9232 * spoolss_setprinterdataex
9233 ********************************************************************/
9235 WERROR _spoolss_setprinterdataex(pipes_struct *p, SPOOL_Q_SETPRINTERDATAEX *q_u, SPOOL_R_SETPRINTERDATAEX *r_u)
9237 POLICY_HND *handle = &q_u->handle;
9238 uint32 type = q_u->type;
9239 uint8 *data = q_u->data;
9240 uint32 real_len = q_u->real_len;
9242 NT_PRINTER_INFO_LEVEL *printer = NULL;
9244 WERROR status = WERR_OK;
9245 Printer_entry *Printer = find_printer_index_by_hnd(p, handle);
9250 DEBUG(4,("_spoolss_setprinterdataex\n"));
9252 /* From MSDN documentation of SetPrinterDataEx: pass request to
9253 SetPrinterData if key is "PrinterDriverData" */
9256 DEBUG(2,("_spoolss_setprinterdataex: Invalid handle (%s:%u:%u).\n", OUR_HANDLE(handle)));
9260 if ( Printer->printer_type == SPLHND_SERVER ) {
9261 DEBUG(10,("_spoolss_setprinterdataex: Not implemented for server handles yet\n"));
9262 return WERR_INVALID_PARAM;
9265 if ( !get_printer_snum(p,handle, &snum, NULL) )
9269 * Access check : NT returns "access denied" if you make a
9270 * SetPrinterData call without the necessary privildge.
9271 * we were originally returning OK if nothing changed
9272 * which made Win2k issue **a lot** of SetPrinterData
9273 * when connecting to a printer --jerry
9276 if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER)
9278 DEBUG(3, ("_spoolss_setprinterdataex: change denied by handle access permissions\n"));
9279 return WERR_ACCESS_DENIED;
9282 status = get_a_printer(Printer, &printer, 2, lp_servicename(snum));
9283 if (!W_ERROR_IS_OK(status))
9286 unistr2_to_ascii( valuename, &q_u->value, sizeof(valuename));
9287 unistr2_to_ascii( keyname, &q_u->key, sizeof(keyname));
9289 /* check for OID in valuename */
9291 if ( (oid_string = strchr( valuename, ',' )) != NULL )
9297 /* save the registry data */
9299 status = set_printer_dataex( printer, keyname, valuename, type, data, real_len );
9301 if ( W_ERROR_IS_OK(status) )
9303 /* save the OID if one was specified */
9305 fstrcat( keyname, "\\" );
9306 fstrcat( keyname, SPOOL_OID_KEY );
9309 * I'm not checking the status here on purpose. Don't know
9310 * if this is right, but I'm returning the status from the
9311 * previous set_printer_dataex() call. I have no idea if
9312 * this is right. --jerry
9315 set_printer_dataex( printer, keyname, valuename,
9316 REG_SZ, (uint8 *)oid_string,
9317 strlen(oid_string)+1 );
9320 status = mod_a_printer(printer, 2);
9323 free_a_printer(&printer, 2);
9329 /********************************************************************
9330 * spoolss_deleteprinterdataex
9331 ********************************************************************/
9333 WERROR _spoolss_deleteprinterdataex(pipes_struct *p, SPOOL_Q_DELETEPRINTERDATAEX *q_u, SPOOL_R_DELETEPRINTERDATAEX *r_u)
9335 POLICY_HND *handle = &q_u->handle;
9336 UNISTR2 *value = &q_u->valuename;
9337 UNISTR2 *key = &q_u->keyname;
9339 NT_PRINTER_INFO_LEVEL *printer = NULL;
9341 WERROR status = WERR_OK;
9342 Printer_entry *Printer=find_printer_index_by_hnd(p, handle);
9343 char *valuename = NULL;
9344 char *keyname = NULL;
9345 TALLOC_CTX *ctx = p->mem_ctx;
9347 DEBUG(5,("spoolss_deleteprinterdataex\n"));
9350 DEBUG(2,("_spoolss_deleteprinterdata: Invalid handle (%s:%u:%u).\n", OUR_HANDLE(handle)));
9354 if (!get_printer_snum(p, handle, &snum, NULL))
9357 if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
9358 DEBUG(3, ("_spoolss_deleteprinterdataex: printer properties change denied by handle\n"));
9359 return WERR_ACCESS_DENIED;
9362 valuename = unistr2_to_ascii_talloc(ctx, value);
9363 keyname = unistr2_to_ascii_talloc(ctx, key);
9364 if (!valuename || !keyname) {
9368 status = get_a_printer(Printer, &printer, 2, lp_const_servicename(snum));
9369 if (!W_ERROR_IS_OK(status))
9372 status = delete_printer_dataex( printer, keyname, valuename );
9374 if ( W_ERROR_IS_OK(status) )
9375 mod_a_printer( printer, 2 );
9377 free_a_printer(&printer, 2);
9382 /********************************************************************
9383 * spoolss_enumprinterkey
9384 ********************************************************************/
9387 WERROR _spoolss_enumprinterkey(pipes_struct *p, SPOOL_Q_ENUMPRINTERKEY *q_u, SPOOL_R_ENUMPRINTERKEY *r_u)
9390 fstring *keynames = NULL;
9391 uint16 *enumkeys = NULL;
9394 POLICY_HND *handle = &q_u->handle;
9395 Printer_entry *Printer = find_printer_index_by_hnd(p, handle);
9396 NT_PRINTER_DATA *data;
9397 NT_PRINTER_INFO_LEVEL *printer = NULL;
9399 WERROR status = WERR_BADFILE;
9402 DEBUG(4,("_spoolss_enumprinterkey\n"));
9405 DEBUG(2,("_spoolss_enumprinterkey: Invalid handle (%s:%u:%u).\n", OUR_HANDLE(handle)));
9409 if ( !get_printer_snum(p,handle, &snum, NULL) )
9412 status = get_a_printer(Printer, &printer, 2, lp_const_servicename(snum));
9413 if (!W_ERROR_IS_OK(status))
9416 /* get the list of subkey names */
9418 unistr2_to_ascii(key, &q_u->key, sizeof(key));
9419 data = printer->info_2->data;
9421 num_keys = get_printer_subkeys( data, key, &keynames );
9423 if ( num_keys == -1 ) {
9424 status = WERR_BADFILE;
9428 printerkey_len = init_unistr_array( &enumkeys, keynames, NULL );
9430 r_u->needed = printerkey_len*2;
9432 if ( q_u->size < r_u->needed ) {
9433 status = WERR_MORE_DATA;
9437 if (!make_spoolss_buffer5(p->mem_ctx, &r_u->keys, printerkey_len, enumkeys)) {
9438 status = WERR_NOMEM;
9444 if ( q_u->size < r_u->needed )
9445 status = WERR_MORE_DATA;
9448 free_a_printer( &printer, 2 );
9449 SAFE_FREE( keynames );
9454 /********************************************************************
9455 * spoolss_deleteprinterkey
9456 ********************************************************************/
9458 WERROR _spoolss_deleteprinterkey(pipes_struct *p, SPOOL_Q_DELETEPRINTERKEY *q_u, SPOOL_R_DELETEPRINTERKEY *r_u)
9460 POLICY_HND *handle = &q_u->handle;
9461 Printer_entry *Printer = find_printer_index_by_hnd(p, &q_u->handle);
9463 NT_PRINTER_INFO_LEVEL *printer = NULL;
9467 DEBUG(5,("spoolss_deleteprinterkey\n"));
9470 DEBUG(2,("_spoolss_deleteprinterkey: Invalid handle (%s:%u:%u).\n", OUR_HANDLE(handle)));
9474 /* if keyname == NULL, return error */
9476 if ( !q_u->keyname.buffer )
9477 return WERR_INVALID_PARAM;
9479 if (!get_printer_snum(p, handle, &snum, NULL))
9482 if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
9483 DEBUG(3, ("_spoolss_deleteprinterkey: printer properties change denied by handle\n"));
9484 return WERR_ACCESS_DENIED;
9487 status = get_a_printer(Printer, &printer, 2, lp_const_servicename(snum));
9488 if (!W_ERROR_IS_OK(status))
9491 /* delete the key and all subneys */
9493 unistr2_to_ascii(key, &q_u->keyname, sizeof(key));
9495 status = delete_all_printer_data( printer->info_2, key );
9497 if ( W_ERROR_IS_OK(status) )
9498 status = mod_a_printer(printer, 2);
9500 free_a_printer( &printer, 2 );
9506 /********************************************************************
9507 * spoolss_enumprinterdataex
9508 ********************************************************************/
9510 WERROR _spoolss_enumprinterdataex(pipes_struct *p, SPOOL_Q_ENUMPRINTERDATAEX *q_u, SPOOL_R_ENUMPRINTERDATAEX *r_u)
9512 POLICY_HND *handle = &q_u->handle;
9513 uint32 in_size = q_u->size;
9516 NT_PRINTER_INFO_LEVEL *printer = NULL;
9517 PRINTER_ENUM_VALUES *enum_values = NULL;
9518 NT_PRINTER_DATA *p_data;
9520 Printer_entry *Printer = find_printer_index_by_hnd(p, handle);
9525 REGISTRY_VALUE *val;
9530 DEBUG(4,("_spoolss_enumprinterdataex\n"));
9533 DEBUG(2,("_spoolss_enumprinterdataex: Invalid handle (%s:%u:%u1<).\n", OUR_HANDLE(handle)));
9538 * first check for a keyname of NULL or "". Win2k seems to send
9539 * this a lot and we should send back WERR_INVALID_PARAM
9540 * no need to spend time looking up the printer in this case.
9544 unistr2_to_ascii(key, &q_u->key, sizeof(key));
9545 if ( !strlen(key) ) {
9546 result = WERR_INVALID_PARAM;
9550 /* get the printer off of disk */
9552 if (!get_printer_snum(p,handle, &snum, NULL))
9555 ZERO_STRUCT(printer);
9556 result = get_a_printer(Printer, &printer, 2, lp_const_servicename(snum));
9557 if (!W_ERROR_IS_OK(result))
9560 /* now look for a match on the key name */
9562 p_data = printer->info_2->data;
9564 unistr2_to_ascii(key, &q_u->key, sizeof(key));
9565 if ( (key_index = lookup_printerkey( p_data, key)) == -1 )
9567 DEBUG(10,("_spoolss_enumprinterdataex: Unknown keyname [%s]\n", key));
9568 result = WERR_INVALID_PARAM;
9575 /* allocate the memory for the array of pointers -- if necessary */
9577 num_entries = regval_ctr_numvals( p_data->keys[key_index].values );
9580 if ( (enum_values=TALLOC_ARRAY(p->mem_ctx, PRINTER_ENUM_VALUES, num_entries)) == NULL )
9582 DEBUG(0,("_spoolss_enumprinterdataex: talloc() failed to allocate memory for [%lu] bytes!\n",
9583 (unsigned long)num_entries*sizeof(PRINTER_ENUM_VALUES)));
9584 result = WERR_NOMEM;
9588 memset( enum_values, 0x0, num_entries*sizeof(PRINTER_ENUM_VALUES) );
9592 * loop through all params and build the array to pass
9593 * back to the client
9596 for ( i=0; i<num_entries; i++ )
9598 /* lookup the registry value */
9600 val = regval_ctr_specific_value( p_data->keys[key_index].values, i );
9601 DEBUG(10,("retrieved value number [%d] [%s]\n", i, regval_name(val) ));
9605 value_name = regval_name( val );
9606 init_unistr( &enum_values[i].valuename, value_name );
9607 enum_values[i].value_len = (strlen(value_name)+1) * 2;
9608 enum_values[i].type = regval_type( val );
9610 data_len = regval_size( val );
9612 if ( !(enum_values[i].data = (uint8 *)TALLOC_MEMDUP(p->mem_ctx, regval_data_p(val), data_len)) )
9614 DEBUG(0,("TALLOC_MEMDUP failed to allocate memory [data_len=%d] for data!\n",
9616 result = WERR_NOMEM;
9620 enum_values[i].data_len = data_len;
9622 /* keep track of the size of the array in bytes */
9624 needed += spoolss_size_printer_enum_values(&enum_values[i]);
9627 /* housekeeping information in the reply */
9629 /* Fix from Martin Zielinski <mz@seh.de> - ensure
9630 * the hand marshalled container size is a multiple
9631 * of 4 bytes for RPC alignment.
9635 needed += 4-(needed % 4);
9638 r_u->needed = needed;
9639 r_u->returned = num_entries;
9641 if (needed > in_size) {
9642 result = WERR_MORE_DATA;
9646 /* copy data into the reply */
9648 /* mz: Vista x64 returns 0x6f7 (The stub received bad data), if the
9649 response buffer size is != the offered buffer size
9651 r_u->ctr.size = r_u->needed;
9653 r_u->ctr.size = in_size;
9655 r_u->ctr.size_of_array = r_u->returned;
9656 r_u->ctr.values = enum_values;
9660 free_a_printer(&printer, 2);
9665 /****************************************************************************
9666 ****************************************************************************/
9668 static void fill_printprocessordirectory_1(PRINTPROCESSOR_DIRECTORY_1 *info, const char *name)
9670 init_unistr(&info->name, name);
9673 static WERROR getprintprocessordirectory_level_1(UNISTR2 *name,
9674 UNISTR2 *environment,
9679 char *long_archi = NULL;
9680 PRINTPROCESSOR_DIRECTORY_1 *info=NULL;
9681 WERROR result = WERR_OK;
9682 TALLOC_CTX *ctx = talloc_tos();
9684 long_archi = unistr2_to_ascii_talloc(ctx, environment);
9689 if (!get_short_archi(long_archi))
9690 return WERR_INVALID_ENVIRONMENT;
9692 if((info=SMB_MALLOC_P(PRINTPROCESSOR_DIRECTORY_1)) == NULL)
9695 fill_printprocessordirectory_1(info, "C:\\WINNT\\System32\\spool\\PRTPROCS\\W32X86");
9697 *needed += spoolss_size_printprocessordirectory_info_1(info);
9699 if (*needed > offered) {
9700 result = WERR_INSUFFICIENT_BUFFER;
9704 if (!rpcbuf_alloc_size(buffer, *needed)) {
9705 result = WERR_INSUFFICIENT_BUFFER;
9709 smb_io_printprocessordirectory_1("", buffer, info, 0);
9717 WERROR _spoolss_getprintprocessordirectory(pipes_struct *p, SPOOL_Q_GETPRINTPROCESSORDIRECTORY *q_u, SPOOL_R_GETPRINTPROCESSORDIRECTORY *r_u)
9719 uint32 level = q_u->level;
9720 RPC_BUFFER *buffer = NULL;
9721 uint32 offered = q_u->offered;
9722 uint32 *needed = &r_u->needed;
9725 /* that's an [in out] buffer */
9727 if (!q_u->buffer && (offered!=0)) {
9728 return WERR_INVALID_PARAM;
9731 rpcbuf_move(q_u->buffer, &r_u->buffer);
9732 buffer = r_u->buffer;
9734 DEBUG(5,("_spoolss_getprintprocessordirectory\n"));
9740 result = getprintprocessordirectory_level_1
9741 (&q_u->name, &q_u->environment, buffer, offered, needed);
9744 result = WERR_UNKNOWN_LEVEL;
9750 /*******************************************************************
9751 Streams the monitor UI DLL name in UNICODE
9752 *******************************************************************/
9754 static WERROR xcvtcp_monitorui( NT_USER_TOKEN *token, RPC_BUFFER *in,
9755 RPC_BUFFER *out, uint32 *needed )
9757 const char *dllname = "tcpmonui.dll";
9759 *needed = (strlen(dllname)+1) * 2;
9761 if ( rpcbuf_get_size(out) < *needed ) {
9762 return WERR_INSUFFICIENT_BUFFER;
9765 if ( !make_monitorui_buf( out, dllname ) ) {
9772 /*******************************************************************
9773 Create a new TCP/IP port
9774 *******************************************************************/
9776 static WERROR xcvtcp_addport( NT_USER_TOKEN *token, RPC_BUFFER *in,
9777 RPC_BUFFER *out, uint32 *needed )
9779 NT_PORT_DATA_1 port1;
9780 TALLOC_CTX *ctx = talloc_tos();
9781 char *device_uri = NULL;
9783 ZERO_STRUCT( port1 );
9785 /* convert to our internal port data structure */
9787 if ( !convert_port_data_1( &port1, in ) ) {
9791 /* create the device URI and call the add_port_hook() */
9793 switch ( port1.protocol ) {
9794 case PORT_PROTOCOL_DIRECT:
9795 device_uri = talloc_asprintf(ctx,
9796 "socket://%s:%d/", port1.hostaddr, port1.port );
9799 case PORT_PROTOCOL_LPR:
9800 device_uri = talloc_asprintf(ctx,
9801 "lpr://%s/%s", port1.hostaddr, port1.queue );
9805 return WERR_UNKNOWN_PORT;
9812 return add_port_hook(ctx, token, port1.name, device_uri );
9815 /*******************************************************************
9816 *******************************************************************/
9818 struct xcv_api_table xcvtcp_cmds[] = {
9819 { "MonitorUI", xcvtcp_monitorui },
9820 { "AddPort", xcvtcp_addport},
9824 static WERROR process_xcvtcp_command( NT_USER_TOKEN *token, const char *command,
9825 RPC_BUFFER *inbuf, RPC_BUFFER *outbuf,
9830 DEBUG(10,("process_xcvtcp_command: Received command \"%s\"\n", command));
9832 for ( i=0; xcvtcp_cmds[i].name; i++ ) {
9833 if ( strcmp( command, xcvtcp_cmds[i].name ) == 0 )
9834 return xcvtcp_cmds[i].fn( token, inbuf, outbuf, needed );
9837 return WERR_BADFUNC;
9840 /*******************************************************************
9841 *******************************************************************/
9842 #if 0 /* don't support management using the "Local Port" monitor */
9844 static WERROR xcvlocal_monitorui( NT_USER_TOKEN *token, RPC_BUFFER *in,
9845 RPC_BUFFER *out, uint32 *needed )
9847 const char *dllname = "localui.dll";
9849 *needed = (strlen(dllname)+1) * 2;
9851 if ( rpcbuf_get_size(out) < *needed ) {
9852 return WERR_INSUFFICIENT_BUFFER;
9855 if ( !make_monitorui_buf( out, dllname )) {
9862 /*******************************************************************
9863 *******************************************************************/
9865 struct xcv_api_table xcvlocal_cmds[] = {
9866 { "MonitorUI", xcvlocal_monitorui },
9870 struct xcv_api_table xcvlocal_cmds[] = {
9877 /*******************************************************************
9878 *******************************************************************/
9880 static WERROR process_xcvlocal_command( NT_USER_TOKEN *token, const char *command,
9881 RPC_BUFFER *inbuf, RPC_BUFFER *outbuf,
9886 DEBUG(10,("process_xcvlocal_command: Received command \"%s\"\n", command));
9888 for ( i=0; xcvlocal_cmds[i].name; i++ ) {
9889 if ( strcmp( command, xcvlocal_cmds[i].name ) == 0 )
9890 return xcvlocal_cmds[i].fn( token, inbuf, outbuf , needed );
9892 return WERR_BADFUNC;
9895 /*******************************************************************
9896 *******************************************************************/
9898 WERROR _spoolss_xcvdataport(pipes_struct *p, SPOOL_Q_XCVDATAPORT *q_u, SPOOL_R_XCVDATAPORT *r_u)
9900 Printer_entry *Printer = find_printer_index_by_hnd(p, &q_u->handle);
9904 DEBUG(2,("_spoolss_xcvdataport: Invalid handle (%s:%u:%u).\n", OUR_HANDLE(&q_u->handle)));
9908 /* Has to be a handle to the TCP/IP port monitor */
9910 if ( !(Printer->printer_type & (SPLHND_PORTMON_LOCAL|SPLHND_PORTMON_TCP)) ) {
9911 DEBUG(2,("_spoolss_xcvdataport: Call only valid for Port Monitors\n"));
9915 /* requires administrative access to the server */
9917 if ( !(Printer->access_granted & SERVER_ACCESS_ADMINISTER) ) {
9918 DEBUG(2,("_spoolss_xcvdataport: denied by handle permissions.\n"));
9919 return WERR_ACCESS_DENIED;
9922 /* Get the command name. There's numerous commands supported by the
9923 TCPMON interface. */
9925 rpcstr_pull(command, q_u->dataname.buffer, sizeof(command),
9926 q_u->dataname.uni_str_len*2, 0);
9928 /* Allocate the outgoing buffer */
9930 if (!rpcbuf_init( &r_u->outdata, q_u->offered, p->mem_ctx ))
9933 switch ( Printer->printer_type ) {
9934 case SPLHND_PORTMON_TCP:
9935 return process_xcvtcp_command( p->server_info->ptok, command,
9936 &q_u->indata, &r_u->outdata, &r_u->needed );
9937 case SPLHND_PORTMON_LOCAL:
9938 return process_xcvlocal_command( p->server_info->ptok, command,
9939 &q_u->indata, &r_u->outdata, &r_u->needed );
9942 return WERR_INVALID_PRINT_MONITOR;