The first of Martin Zielinski <mz@seh.de> Vista printing patches.
[samba.git] / source / rpc_server / srv_spoolss_nt.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  RPC Pipe client / server routines
4  *  Copyright (C) Andrew Tridgell              1992-2000,
5  *  Copyright (C) Luke Kenneth Casson Leighton 1996-2000,
6  *  Copyright (C) Jean Fran├žois Micouleau      1998-2000,
7  *  Copyright (C) Jeremy Allison               2001-2002,
8  *  Copyright (C) Gerald Carter                2000-2004,
9  *  Copyright (C) Tim Potter                   2001-2002.
10  *
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.
15  *
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.
20  *
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/>.
23  */
24
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. */
27
28 #include "includes.h"
29
30 extern userdom_struct current_user_info;
31
32 #undef DBGC_CLASS
33 #define DBGC_CLASS DBGC_RPC_SRV
34
35 #ifndef MAX_OPEN_PRINTER_EXS
36 #define MAX_OPEN_PRINTER_EXS 50
37 #endif
38
39 #define MAGIC_DISPLAY_FREQUENCY 0xfade2bad
40 #define PHANTOM_DEVMODE_KEY "_p_f_a_n_t_0_m_"
41
42 struct table_node {
43         const char    *long_archi;
44         const char    *short_archi;
45         int     version;
46 };
47
48 static Printer_entry *printers_list;
49
50 typedef struct _counter_printer_0 {
51         struct _counter_printer_0 *next;
52         struct _counter_printer_0 *prev;
53
54         int snum;
55         uint32 counter;
56 } counter_printer_0;
57
58 static counter_printer_0 *counter_list;
59
60 static struct rpc_pipe_client *notify_cli_pipe; /* print notify back-channel pipe handle*/
61 static uint32 smb_connections=0;
62
63
64 /* in printing/nt_printing.c */
65
66 extern struct standard_mapping printer_std_mapping, printserver_std_mapping;
67
68 /* API table for Xcv Monitor functions */
69
70 struct xcv_api_table {
71         const char *name;
72         WERROR(*fn) (NT_USER_TOKEN *token, RPC_BUFFER *in, RPC_BUFFER *out, uint32 *needed);
73 };
74
75
76 /* translate between internal status numbers and NT status numbers */
77 static int nt_printj_status(int v)
78 {
79         switch (v) {
80         case LPQ_QUEUED:
81                 return 0;
82         case LPQ_PAUSED:
83                 return JOB_STATUS_PAUSED;
84         case LPQ_SPOOLING:
85                 return JOB_STATUS_SPOOLING;
86         case LPQ_PRINTING:
87                 return JOB_STATUS_PRINTING;
88         case LPQ_ERROR:
89                 return JOB_STATUS_ERROR;
90         case LPQ_DELETING:
91                 return JOB_STATUS_DELETING;
92         case LPQ_OFFLINE:
93                 return JOB_STATUS_OFFLINE;
94         case LPQ_PAPEROUT:
95                 return JOB_STATUS_PAPEROUT;
96         case LPQ_PRINTED:
97                 return JOB_STATUS_PRINTED;
98         case LPQ_DELETED:
99                 return JOB_STATUS_DELETED;
100         case LPQ_BLOCKED:
101                 return JOB_STATUS_BLOCKED;
102         case LPQ_USER_INTERVENTION:
103                 return JOB_STATUS_USER_INTERVENTION;
104         }
105         return 0;
106 }
107
108 static int nt_printq_status(int v)
109 {
110         switch (v) {
111         case LPQ_PAUSED:
112                 return PRINTER_STATUS_PAUSED;
113         case LPQ_QUEUED:
114         case LPQ_SPOOLING:
115         case LPQ_PRINTING:
116                 return 0;
117         }
118         return 0;
119 }
120
121 /****************************************************************************
122  Functions to handle SPOOL_NOTIFY_OPTION struct stored in Printer_entry.
123 ****************************************************************************/
124
125 static void free_spool_notify_option(SPOOL_NOTIFY_OPTION **pp)
126 {
127         if (*pp == NULL)
128                 return;
129
130         SAFE_FREE((*pp)->ctr.type);
131         SAFE_FREE(*pp);
132 }
133
134 /***************************************************************************
135  Disconnect from the client
136 ****************************************************************************/
137
138 static void srv_spoolss_replycloseprinter(int snum, POLICY_HND *handle)
139 {
140         WERROR result;
141
142         /*
143          * Tell the specific printing tdb we no longer want messages for this printer
144          * by deregistering our PID.
145          */
146
147         if (!print_notify_deregister_pid(snum))
148                 DEBUG(0,("print_notify_register_pid: Failed to register our pid for printer %s\n", lp_const_servicename(snum) ));
149
150         /* weird if the test succeds !!! */
151         if (smb_connections==0) {
152                 DEBUG(0,("srv_spoolss_replycloseprinter:Trying to close non-existant notify backchannel !\n"));
153                 return;
154         }
155
156         result = rpccli_spoolss_reply_close_printer(notify_cli_pipe,
157                                 talloc_tos(),
158                                 handle);
159
160         if (!W_ERROR_IS_OK(result))
161                 DEBUG(0,("srv_spoolss_replycloseprinter: reply_close_printer failed [%s].\n",
162                         dos_errstr(result)));
163
164         /* if it's the last connection, deconnect the IPC$ share */
165         if (smb_connections==1) {
166
167                 cli_shutdown( rpc_pipe_np_smb_conn(notify_cli_pipe) );
168                 notify_cli_pipe = NULL; /* The above call shuts downn the pipe also. */
169
170                 messaging_deregister(smbd_messaging_context(),
171                                      MSG_PRINTER_NOTIFY2, NULL);
172
173                 /* Tell the connections db we're no longer interested in
174                  * printer notify messages. */
175
176                 register_message_flags( False, FLAG_MSG_PRINT_NOTIFY );
177         }
178
179         smb_connections--;
180 }
181
182 /****************************************************************************
183  Functions to free a printer entry datastruct.
184 ****************************************************************************/
185
186 static void free_printer_entry(void *ptr)
187 {
188         Printer_entry *Printer = (Printer_entry *)ptr;
189
190         if (Printer->notify.client_connected==True) {
191                 int snum = -1;
192
193                 if ( Printer->printer_type == SPLHND_SERVER) {
194                         snum = -1;
195                         srv_spoolss_replycloseprinter(snum, &Printer->notify.client_hnd);
196                 } else if (Printer->printer_type == SPLHND_PRINTER) {
197                         snum = print_queue_snum(Printer->sharename);
198                         if (snum != -1)
199                                 srv_spoolss_replycloseprinter(snum,
200                                                 &Printer->notify.client_hnd);
201                 }
202         }
203
204         Printer->notify.flags=0;
205         Printer->notify.options=0;
206         Printer->notify.localmachine[0]='\0';
207         Printer->notify.printerlocal=0;
208         free_spool_notify_option(&Printer->notify.option);
209         Printer->notify.option=NULL;
210         Printer->notify.client_connected=False;
211
212         free_nt_devicemode( &Printer->nt_devmode );
213         free_a_printer( &Printer->printer_info, 2 );
214
215         talloc_destroy( Printer->ctx );
216
217         /* Remove from the internal list. */
218         DLIST_REMOVE(printers_list, Printer);
219
220         SAFE_FREE(Printer);
221 }
222
223 /****************************************************************************
224  Functions to duplicate a SPOOL_NOTIFY_OPTION struct stored in Printer_entry.
225 ****************************************************************************/
226
227 static SPOOL_NOTIFY_OPTION *dup_spool_notify_option(SPOOL_NOTIFY_OPTION *sp)
228 {
229         SPOOL_NOTIFY_OPTION *new_sp = NULL;
230
231         if (!sp)
232                 return NULL;
233
234         new_sp = SMB_MALLOC_P(SPOOL_NOTIFY_OPTION);
235         if (!new_sp)
236                 return NULL;
237
238         *new_sp = *sp;
239
240         if (sp->ctr.count) {
241                 new_sp->ctr.type = (SPOOL_NOTIFY_OPTION_TYPE *)memdup(sp->ctr.type, sizeof(SPOOL_NOTIFY_OPTION_TYPE) * sp->ctr.count);
242
243                 if (!new_sp->ctr.type) {
244                         SAFE_FREE(new_sp);
245                         return NULL;
246                 }
247         }
248
249         return new_sp;
250 }
251
252 /****************************************************************************
253   find printer index by handle
254 ****************************************************************************/
255
256 static Printer_entry *find_printer_index_by_hnd(pipes_struct *p, POLICY_HND *hnd)
257 {
258         Printer_entry *find_printer = NULL;
259
260         if(!find_policy_by_hnd(p,hnd,(void **)(void *)&find_printer)) {
261                 DEBUG(2,("find_printer_index_by_hnd: Printer handle not found: "));
262                 return NULL;
263         }
264
265         return find_printer;
266 }
267
268 /****************************************************************************
269  Close printer index by handle.
270 ****************************************************************************/
271
272 static bool close_printer_handle(pipes_struct *p, POLICY_HND *hnd)
273 {
274         Printer_entry *Printer = find_printer_index_by_hnd(p, hnd);
275
276         if (!Printer) {
277                 DEBUG(2,("close_printer_handle: Invalid handle (%s:%u:%u)\n", OUR_HANDLE(hnd)));
278                 return False;
279         }
280
281         close_policy_hnd(p, hnd);
282
283         return True;
284 }
285
286 /****************************************************************************
287  Delete a printer given a handle.
288 ****************************************************************************/
289
290 WERROR delete_printer_hook(TALLOC_CTX *ctx, NT_USER_TOKEN *token, const char *sharename )
291 {
292         char *cmd = lp_deleteprinter_cmd();
293         char *command = NULL;
294         int ret;
295         SE_PRIV se_printop = SE_PRINT_OPERATOR;
296         bool is_print_op = False;
297
298         /* can't fail if we don't try */
299
300         if ( !*cmd )
301                 return WERR_OK;
302
303         command = talloc_asprintf(ctx,
304                         "%s \"%s\"",
305                         cmd, sharename);
306         if (!command) {
307                 return WERR_NOMEM;
308         }
309         if ( token )
310                 is_print_op = user_has_privileges( token, &se_printop );
311
312         DEBUG(10,("Running [%s]\n", command));
313
314         /********** BEGIN SePrintOperatorPrivlege BLOCK **********/
315
316         if ( is_print_op )
317                 become_root();
318
319         if ( (ret = smbrun(command, NULL)) == 0 ) {
320                 /* Tell everyone we updated smb.conf. */
321                 message_send_all(smbd_messaging_context(),
322                                  MSG_SMB_CONF_UPDATED, NULL, 0, NULL);
323         }
324
325         if ( is_print_op )
326                 unbecome_root();
327
328         /********** END SePrintOperatorPrivlege BLOCK **********/
329
330         DEBUGADD(10,("returned [%d]\n", ret));
331
332         TALLOC_FREE(command);
333
334         if (ret != 0)
335                 return WERR_BADFID; /* What to return here? */
336
337         /* go ahead and re-read the services immediately */
338         reload_services( False );
339
340         if ( lp_servicenumber( sharename )  < 0 )
341                 return WERR_ACCESS_DENIED;
342
343         return WERR_OK;
344 }
345
346 /****************************************************************************
347  Delete a printer given a handle.
348 ****************************************************************************/
349
350 static WERROR delete_printer_handle(pipes_struct *p, POLICY_HND *hnd)
351 {
352         Printer_entry *Printer = find_printer_index_by_hnd(p, hnd);
353
354         if (!Printer) {
355                 DEBUG(2,("delete_printer_handle: Invalid handle (%s:%u:%u)\n", OUR_HANDLE(hnd)));
356                 return WERR_BADFID;
357         }
358
359         /*
360          * It turns out that Windows allows delete printer on a handle
361          * opened by an admin user, then used on a pipe handle created
362          * by an anonymous user..... but they're working on security.... riiight !
363          * JRA.
364          */
365
366         if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
367                 DEBUG(3, ("delete_printer_handle: denied by handle\n"));
368                 return WERR_ACCESS_DENIED;
369         }
370
371         /* this does not need a become root since the access check has been
372            done on the handle already */
373
374         if (del_a_printer( Printer->sharename ) != 0) {
375                 DEBUG(3,("Error deleting printer %s\n", Printer->sharename));
376                 return WERR_BADFID;
377         }
378
379         return delete_printer_hook(p->mem_ctx, p->pipe_user.nt_user_token, Printer->sharename );
380 }
381
382 /****************************************************************************
383  Return the snum of a printer corresponding to an handle.
384 ****************************************************************************/
385
386 static bool get_printer_snum(pipes_struct *p, POLICY_HND *hnd, int *number,
387                              struct share_params **params)
388 {
389         Printer_entry *Printer = find_printer_index_by_hnd(p, hnd);
390
391         if (!Printer) {
392                 DEBUG(2,("get_printer_snum: Invalid handle (%s:%u:%u)\n", OUR_HANDLE(hnd)));
393                 return False;
394         }
395
396         switch (Printer->printer_type) {
397                 case SPLHND_PRINTER:
398                         DEBUG(4,("short name:%s\n", Printer->sharename));
399                         *number = print_queue_snum(Printer->sharename);
400                         return (*number != -1);
401                 case SPLHND_SERVER:
402                         return False;
403                 default:
404                         return False;
405         }
406 }
407
408 /****************************************************************************
409  Set printer handle type.
410  Check if it's \\server or \\server\printer
411 ****************************************************************************/
412
413 static bool set_printer_hnd_printertype(Printer_entry *Printer, char *handlename)
414 {
415         DEBUG(3,("Setting printer type=%s\n", handlename));
416
417         if ( strlen(handlename) < 3 ) {
418                 DEBUGADD(4,("A print server must have at least 1 char ! %s\n", handlename));
419                 return False;
420         }
421
422         /* it's a print server */
423         if (*handlename=='\\' && *(handlename+1)=='\\' && !strchr_m(handlename+2, '\\')) {
424                 DEBUGADD(4,("Printer is a print server\n"));
425                 Printer->printer_type = SPLHND_SERVER;
426         }
427         /* it's a printer (set_printer_hnd_name() will handle port monitors */
428         else {
429                 DEBUGADD(4,("Printer is a printer\n"));
430                 Printer->printer_type = SPLHND_PRINTER;
431         }
432
433         return True;
434 }
435
436 /****************************************************************************
437  Set printer handle name..  Accept names like \\server, \\server\printer,
438  \\server\SHARE, & "\\server\,XcvMonitor Standard TCP/IP Port"    See
439  the MSDN docs regarding OpenPrinter() for details on the XcvData() and
440  XcvDataPort() interface.
441 ****************************************************************************/
442
443 static bool set_printer_hnd_name(Printer_entry *Printer, char *handlename)
444 {
445         int snum;
446         int n_services=lp_numservices();
447         char *aprinter, *printername;
448         const char *servername;
449         fstring sname;
450         bool found=False;
451         NT_PRINTER_INFO_LEVEL *printer = NULL;
452         WERROR result;
453
454         DEBUG(4,("Setting printer name=%s (len=%lu)\n", handlename, (unsigned long)strlen(handlename)));
455
456         aprinter = handlename;
457         if ( *handlename == '\\' ) {
458                 servername = handlename + 2;
459                 if ( (aprinter = strchr_m( handlename+2, '\\' )) != NULL ) {
460                         *aprinter = '\0';
461                         aprinter++;
462                 }
463         }
464         else {
465                 servername = "";
466         }
467
468         /* save the servername to fill in replies on this handle */
469
470         if ( !is_myname_or_ipaddr( servername ) )
471                 return False;
472
473         fstrcpy( Printer->servername, servername );
474
475         if ( Printer->printer_type == SPLHND_SERVER )
476                 return True;
477
478         if ( Printer->printer_type != SPLHND_PRINTER )
479                 return False;
480
481         DEBUGADD(5, ("searching for [%s]\n", aprinter ));
482
483         /* check for the Port Monitor Interface */
484
485         if ( strequal( aprinter, SPL_XCV_MONITOR_TCPMON ) ) {
486                 Printer->printer_type = SPLHND_PORTMON_TCP;
487                 fstrcpy(sname, SPL_XCV_MONITOR_TCPMON);
488                 found = True;
489         }
490         else if ( strequal( aprinter, SPL_XCV_MONITOR_LOCALMON ) ) {
491                 Printer->printer_type = SPLHND_PORTMON_LOCAL;
492                 fstrcpy(sname, SPL_XCV_MONITOR_LOCALMON);
493                 found = True;
494         }
495
496         /* Search all sharenames first as this is easier than pulling
497            the printer_info_2 off of disk. Don't use find_service() since
498            that calls out to map_username() */
499
500         /* do another loop to look for printernames */
501
502         for (snum=0; !found && snum<n_services; snum++) {
503
504                 /* no point going on if this is not a printer */
505
506                 if ( !(lp_snum_ok(snum) && lp_print_ok(snum)) )
507                         continue;
508
509                 fstrcpy(sname, lp_servicename(snum));
510                 if ( strequal( aprinter, sname ) ) {
511                         found = True;
512                         break;
513                 }
514
515                 /* no point looking up the printer object if
516                    we aren't allowing printername != sharename */
517
518                 if ( lp_force_printername(snum) )
519                         continue;
520
521                 fstrcpy(sname, lp_servicename(snum));
522
523                 printer = NULL;
524
525                 /* This call doesn't fill in the location or comment from
526                  * a CUPS server for efficiency with large numbers of printers.
527                  * JRA.
528                  */
529
530                 result = get_a_printer_search( NULL, &printer, 2, sname );
531                 if ( !W_ERROR_IS_OK(result) ) {
532                         DEBUG(0,("set_printer_hnd_name: failed to lookup printer [%s] -- result [%s]\n",
533                                 sname, dos_errstr(result)));
534                         continue;
535                 }
536
537                 /* printername is always returned as \\server\printername */
538                 if ( !(printername = strchr_m(&printer->info_2->printername[2], '\\')) ) {
539                         DEBUG(0,("set_printer_hnd_name: info2->printername in wrong format! [%s]\n",
540                                 printer->info_2->printername));
541                         free_a_printer( &printer, 2);
542                         continue;
543                 }
544
545                 printername++;
546
547                 if ( strequal(printername, aprinter) ) {
548                         free_a_printer( &printer, 2);
549                         found = True;
550                         break;
551                 }
552
553                 DEBUGADD(10, ("printername: %s\n", printername));
554
555                 free_a_printer( &printer, 2);
556         }
557
558         free_a_printer( &printer, 2);
559
560         if ( !found ) {
561                 DEBUGADD(4,("Printer not found\n"));
562                 return False;
563         }
564
565         DEBUGADD(4,("set_printer_hnd_name: Printer found: %s -> %s\n", aprinter, sname));
566
567         fstrcpy(Printer->sharename, sname);
568
569         return True;
570 }
571
572 /****************************************************************************
573  Find first available printer slot. creates a printer handle for you.
574  ****************************************************************************/
575
576 static bool open_printer_hnd(pipes_struct *p, POLICY_HND *hnd, char *name, uint32 access_granted)
577 {
578         Printer_entry *new_printer;
579
580         DEBUG(10,("open_printer_hnd: name [%s]\n", name));
581
582         if((new_printer=SMB_MALLOC_P(Printer_entry)) == NULL)
583                 return False;
584
585         ZERO_STRUCTP(new_printer);
586
587         if (!create_policy_hnd(p, hnd, free_printer_entry, new_printer)) {
588                 SAFE_FREE(new_printer);
589                 return False;
590         }
591
592         /* Add to the internal list. */
593         DLIST_ADD(printers_list, new_printer);
594
595         new_printer->notify.option=NULL;
596
597         if ( !(new_printer->ctx = talloc_init("Printer Entry [%p]", hnd)) ) {
598                 DEBUG(0,("open_printer_hnd: talloc_init() failed!\n"));
599                 close_printer_handle(p, hnd);
600                 return False;
601         }
602
603         if (!set_printer_hnd_printertype(new_printer, name)) {
604                 close_printer_handle(p, hnd);
605                 return False;
606         }
607
608         if (!set_printer_hnd_name(new_printer, name)) {
609                 close_printer_handle(p, hnd);
610                 return False;
611         }
612
613         new_printer->access_granted = access_granted;
614
615         DEBUG(5, ("%d printer handles active\n", (int)p->pipe_handles->count ));
616
617         return True;
618 }
619
620 /***************************************************************************
621  check to see if the client motify handle is monitoring the notification
622  given by (notify_type, notify_field).
623  **************************************************************************/
624
625 static bool is_monitoring_event_flags(uint32 flags, uint16 notify_type,
626                                       uint16 notify_field)
627 {
628         return True;
629 }
630
631 static bool is_monitoring_event(Printer_entry *p, uint16 notify_type,
632                                 uint16 notify_field)
633 {
634         SPOOL_NOTIFY_OPTION *option = p->notify.option;
635         uint32 i, j;
636
637         /*
638          * Flags should always be zero when the change notify
639          * is registered by the client's spooler.  A user Win32 app
640          * might use the flags though instead of the NOTIFY_OPTION_INFO
641          * --jerry
642          */
643
644         if (!option) {
645                 return False;
646         }
647
648         if (p->notify.flags)
649                 return is_monitoring_event_flags(
650                         p->notify.flags, notify_type, notify_field);
651
652         for (i = 0; i < option->count; i++) {
653
654                 /* Check match for notify_type */
655
656                 if (option->ctr.type[i].type != notify_type)
657                         continue;
658
659                 /* Check match for field */
660
661                 for (j = 0; j < option->ctr.type[i].count; j++) {
662                         if (option->ctr.type[i].fields[j] == notify_field) {
663                                 return True;
664                         }
665                 }
666         }
667
668         DEBUG(10, ("Open handle for \\\\%s\\%s is not monitoring 0x%02x/0x%02x\n",
669                    p->servername, p->sharename, notify_type, notify_field));
670
671         return False;
672 }
673
674 /* Convert a notification message to a SPOOL_NOTIFY_INFO_DATA struct */
675
676 static void notify_one_value(struct spoolss_notify_msg *msg,
677                              SPOOL_NOTIFY_INFO_DATA *data,
678                              TALLOC_CTX *mem_ctx)
679 {
680         data->notify_data.value[0] = msg->notify.value[0];
681         data->notify_data.value[1] = 0;
682 }
683
684 static void notify_string(struct spoolss_notify_msg *msg,
685                           SPOOL_NOTIFY_INFO_DATA *data,
686                           TALLOC_CTX *mem_ctx)
687 {
688         UNISTR2 unistr;
689
690         /* The length of the message includes the trailing \0 */
691
692         init_unistr2(&unistr, msg->notify.data, UNI_STR_TERMINATE);
693
694         data->notify_data.data.length = msg->len * 2;
695         data->notify_data.data.string = TALLOC_ARRAY(mem_ctx, uint16, msg->len);
696
697         if (!data->notify_data.data.string) {
698                 data->notify_data.data.length = 0;
699                 return;
700         }
701
702         memcpy(data->notify_data.data.string, unistr.buffer, msg->len * 2);
703 }
704
705 static void notify_system_time(struct spoolss_notify_msg *msg,
706                                SPOOL_NOTIFY_INFO_DATA *data,
707                                TALLOC_CTX *mem_ctx)
708 {
709         SYSTEMTIME systime;
710         prs_struct ps;
711
712         if (msg->len != sizeof(time_t)) {
713                 DEBUG(5, ("notify_system_time: received wrong sized message (%d)\n",
714                           msg->len));
715                 return;
716         }
717
718         if (!prs_init(&ps, RPC_MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL)) {
719                 DEBUG(5, ("notify_system_time: prs_init() failed\n"));
720                 return;
721         }
722
723         if (!make_systemtime(&systime, gmtime((time_t *)msg->notify.data))) {
724                 DEBUG(5, ("notify_system_time: unable to make systemtime\n"));
725                 prs_mem_free(&ps);
726                 return;
727         }
728
729         if (!spoolss_io_system_time("", &ps, 0, &systime)) {
730                 prs_mem_free(&ps);
731                 return;
732         }
733
734         data->notify_data.data.length = prs_offset(&ps);
735         if (prs_offset(&ps)) {
736                 data->notify_data.data.string = (uint16 *)
737                         TALLOC(mem_ctx, prs_offset(&ps));
738                 if (!data->notify_data.data.string) {
739                         prs_mem_free(&ps);
740                         return;
741                 }
742                 prs_copy_all_data_out((char *)data->notify_data.data.string, &ps);
743         } else {
744                 data->notify_data.data.string = NULL;
745         }
746
747         prs_mem_free(&ps);
748 }
749
750 struct notify2_message_table {
751         const char *name;
752         void (*fn)(struct spoolss_notify_msg *msg,
753                    SPOOL_NOTIFY_INFO_DATA *data, TALLOC_CTX *mem_ctx);
754 };
755
756 static struct notify2_message_table printer_notify_table[] = {
757         /* 0x00 */ { "PRINTER_NOTIFY_SERVER_NAME", notify_string },
758         /* 0x01 */ { "PRINTER_NOTIFY_PRINTER_NAME", notify_string },
759         /* 0x02 */ { "PRINTER_NOTIFY_SHARE_NAME", notify_string },
760         /* 0x03 */ { "PRINTER_NOTIFY_PORT_NAME", notify_string },
761         /* 0x04 */ { "PRINTER_NOTIFY_DRIVER_NAME", notify_string },
762         /* 0x05 */ { "PRINTER_NOTIFY_COMMENT", notify_string },
763         /* 0x06 */ { "PRINTER_NOTIFY_LOCATION", notify_string },
764         /* 0x07 */ { "PRINTER_NOTIFY_DEVMODE", NULL },
765         /* 0x08 */ { "PRINTER_NOTIFY_SEPFILE", notify_string },
766         /* 0x09 */ { "PRINTER_NOTIFY_PRINT_PROCESSOR", notify_string },
767         /* 0x0a */ { "PRINTER_NOTIFY_PARAMETERS", NULL },
768         /* 0x0b */ { "PRINTER_NOTIFY_DATATYPE", notify_string },
769         /* 0x0c */ { "PRINTER_NOTIFY_SECURITY_DESCRIPTOR", NULL },
770         /* 0x0d */ { "PRINTER_NOTIFY_ATTRIBUTES", notify_one_value },
771         /* 0x0e */ { "PRINTER_NOTIFY_PRIORITY", notify_one_value },
772         /* 0x0f */ { "PRINTER_NOTIFY_DEFAULT_PRIORITY", NULL },
773         /* 0x10 */ { "PRINTER_NOTIFY_START_TIME", NULL },
774         /* 0x11 */ { "PRINTER_NOTIFY_UNTIL_TIME", NULL },
775         /* 0x12 */ { "PRINTER_NOTIFY_STATUS", notify_one_value },
776 };
777
778 static struct notify2_message_table job_notify_table[] = {
779         /* 0x00 */ { "JOB_NOTIFY_PRINTER_NAME", NULL },
780         /* 0x01 */ { "JOB_NOTIFY_MACHINE_NAME", NULL },
781         /* 0x02 */ { "JOB_NOTIFY_PORT_NAME", NULL },
782         /* 0x03 */ { "JOB_NOTIFY_USER_NAME", notify_string },
783         /* 0x04 */ { "JOB_NOTIFY_NOTIFY_NAME", NULL },
784         /* 0x05 */ { "JOB_NOTIFY_DATATYPE", NULL },
785         /* 0x06 */ { "JOB_NOTIFY_PRINT_PROCESSOR", NULL },
786         /* 0x07 */ { "JOB_NOTIFY_PARAMETERS", NULL },
787         /* 0x08 */ { "JOB_NOTIFY_DRIVER_NAME", NULL },
788         /* 0x09 */ { "JOB_NOTIFY_DEVMODE", NULL },
789         /* 0x0a */ { "JOB_NOTIFY_STATUS", notify_one_value },
790         /* 0x0b */ { "JOB_NOTIFY_STATUS_STRING", NULL },
791         /* 0x0c */ { "JOB_NOTIFY_SECURITY_DESCRIPTOR", NULL },
792         /* 0x0d */ { "JOB_NOTIFY_DOCUMENT", notify_string },
793         /* 0x0e */ { "JOB_NOTIFY_PRIORITY", NULL },
794         /* 0x0f */ { "JOB_NOTIFY_POSITION", NULL },
795         /* 0x10 */ { "JOB_NOTIFY_SUBMITTED", notify_system_time },
796         /* 0x11 */ { "JOB_NOTIFY_START_TIME", NULL },
797         /* 0x12 */ { "JOB_NOTIFY_UNTIL_TIME", NULL },
798         /* 0x13 */ { "JOB_NOTIFY_TIME", NULL },
799         /* 0x14 */ { "JOB_NOTIFY_TOTAL_PAGES", notify_one_value },
800         /* 0x15 */ { "JOB_NOTIFY_PAGES_PRINTED", NULL },
801         /* 0x16 */ { "JOB_NOTIFY_TOTAL_BYTES", notify_one_value },
802         /* 0x17 */ { "JOB_NOTIFY_BYTES_PRINTED", NULL },
803 };
804
805
806 /***********************************************************************
807  Allocate talloc context for container object
808  **********************************************************************/
809
810 static void notify_msg_ctr_init( SPOOLSS_NOTIFY_MSG_CTR *ctr )
811 {
812         if ( !ctr )
813                 return;
814
815         ctr->ctx = talloc_init("notify_msg_ctr_init %p", ctr);
816
817         return;
818 }
819
820 /***********************************************************************
821  release all allocated memory and zero out structure
822  **********************************************************************/
823
824 static void notify_msg_ctr_destroy( SPOOLSS_NOTIFY_MSG_CTR *ctr )
825 {
826         if ( !ctr )
827                 return;
828
829         if ( ctr->ctx )
830                 talloc_destroy(ctr->ctx);
831
832         ZERO_STRUCTP(ctr);
833
834         return;
835 }
836
837 /***********************************************************************
838  **********************************************************************/
839
840 static TALLOC_CTX* notify_ctr_getctx( SPOOLSS_NOTIFY_MSG_CTR *ctr )
841 {
842         if ( !ctr )
843                 return NULL;
844
845         return ctr->ctx;
846 }
847
848 /***********************************************************************
849  **********************************************************************/
850
851 static SPOOLSS_NOTIFY_MSG_GROUP* notify_ctr_getgroup( SPOOLSS_NOTIFY_MSG_CTR *ctr, uint32 idx )
852 {
853         if ( !ctr || !ctr->msg_groups )
854                 return NULL;
855
856         if ( idx >= ctr->num_groups )
857                 return NULL;
858
859         return &ctr->msg_groups[idx];
860
861 }
862
863 /***********************************************************************
864  How many groups of change messages do we have ?
865  **********************************************************************/
866
867 static int notify_msg_ctr_numgroups( SPOOLSS_NOTIFY_MSG_CTR *ctr )
868 {
869         if ( !ctr )
870                 return 0;
871
872         return ctr->num_groups;
873 }
874
875 /***********************************************************************
876  Add a SPOOLSS_NOTIFY_MSG_CTR to the correct group
877  **********************************************************************/
878
879 static int notify_msg_ctr_addmsg( SPOOLSS_NOTIFY_MSG_CTR *ctr, SPOOLSS_NOTIFY_MSG *msg )
880 {
881         SPOOLSS_NOTIFY_MSG_GROUP        *groups = NULL;
882         SPOOLSS_NOTIFY_MSG_GROUP        *msg_grp = NULL;
883         SPOOLSS_NOTIFY_MSG              *msg_list = NULL;
884         int                             i, new_slot;
885
886         if ( !ctr || !msg )
887                 return 0;
888
889         /* loop over all groups looking for a matching printer name */
890
891         for ( i=0; i<ctr->num_groups; i++ ) {
892                 if ( strcmp(ctr->msg_groups[i].printername, msg->printer) == 0 )
893                         break;
894         }
895
896         /* add a new group? */
897
898         if ( i == ctr->num_groups ) {
899                 ctr->num_groups++;
900
901                 if ( !(groups = TALLOC_REALLOC_ARRAY( ctr->ctx, ctr->msg_groups, SPOOLSS_NOTIFY_MSG_GROUP, ctr->num_groups)) ) {
902                         DEBUG(0,("notify_msg_ctr_addmsg: talloc_realloc() failed!\n"));
903                         return 0;
904                 }
905                 ctr->msg_groups = groups;
906
907                 /* clear the new entry and set the printer name */
908
909                 ZERO_STRUCT( ctr->msg_groups[ctr->num_groups-1] );
910                 fstrcpy( ctr->msg_groups[ctr->num_groups-1].printername, msg->printer );
911         }
912
913         /* add the change messages; 'i' is the correct index now regardless */
914
915         msg_grp = &ctr->msg_groups[i];
916
917         msg_grp->num_msgs++;
918
919         if ( !(msg_list = TALLOC_REALLOC_ARRAY( ctr->ctx, msg_grp->msgs, SPOOLSS_NOTIFY_MSG, msg_grp->num_msgs )) ) {
920                 DEBUG(0,("notify_msg_ctr_addmsg: talloc_realloc() failed for new message [%d]!\n", msg_grp->num_msgs));
921                 return 0;
922         }
923         msg_grp->msgs = msg_list;
924
925         new_slot = msg_grp->num_msgs-1;
926         memcpy( &msg_grp->msgs[new_slot], msg, sizeof(SPOOLSS_NOTIFY_MSG) );
927
928         /* need to allocate own copy of data */
929
930         if ( msg->len != 0 )
931                 msg_grp->msgs[new_slot].notify.data = (char *)
932                         TALLOC_MEMDUP( ctr->ctx, msg->notify.data, msg->len );
933
934         return ctr->num_groups;
935 }
936
937 /***********************************************************************
938  Send a change notication message on all handles which have a call
939  back registered
940  **********************************************************************/
941
942 static void send_notify2_changes( SPOOLSS_NOTIFY_MSG_CTR *ctr, uint32 idx )
943 {
944         Printer_entry            *p;
945         TALLOC_CTX               *mem_ctx = notify_ctr_getctx( ctr );
946         SPOOLSS_NOTIFY_MSG_GROUP *msg_group = notify_ctr_getgroup( ctr, idx );
947         SPOOLSS_NOTIFY_MSG       *messages;
948         int                      sending_msg_count;
949
950         if ( !msg_group ) {
951                 DEBUG(5,("send_notify2_changes() called with no msg group!\n"));
952                 return;
953         }
954
955         messages = msg_group->msgs;
956
957         if ( !messages ) {
958                 DEBUG(5,("send_notify2_changes() called with no messages!\n"));
959                 return;
960         }
961
962         DEBUG(8,("send_notify2_changes: Enter...[%s]\n", msg_group->printername));
963
964         /* loop over all printers */
965
966         for (p = printers_list; p; p = p->next) {
967                 SPOOL_NOTIFY_INFO_DATA *data;
968                 uint32  data_len = 0;
969                 uint32  id;
970                 int     i;
971
972                 /* Is there notification on this handle? */
973
974                 if ( !p->notify.client_connected )
975                         continue;
976
977                 DEBUG(10,("Client connected! [\\\\%s\\%s]\n", p->servername, p->sharename));
978
979                 /* For this printer?  Print servers always receive
980                    notifications. */
981
982                 if ( ( p->printer_type == SPLHND_PRINTER )  &&
983                     ( !strequal(msg_group->printername, p->sharename) ) )
984                         continue;
985
986                 DEBUG(10,("Our printer\n"));
987
988                 /* allocate the max entries possible */
989
990                 data = TALLOC_ARRAY( mem_ctx, SPOOL_NOTIFY_INFO_DATA, msg_group->num_msgs);
991                 if (!data) {
992                         return;
993                 }
994
995                 ZERO_STRUCTP(data);
996
997                 /* build the array of change notifications */
998
999                 sending_msg_count = 0;
1000
1001                 for ( i=0; i<msg_group->num_msgs; i++ ) {
1002                         SPOOLSS_NOTIFY_MSG      *msg = &messages[i];
1003
1004                         /* Are we monitoring this event? */
1005
1006                         if (!is_monitoring_event(p, msg->type, msg->field))
1007                                 continue;
1008
1009                         sending_msg_count++;
1010
1011
1012                         DEBUG(10,("process_notify2_message: Sending message type [0x%x] field [0x%2x] for printer [%s]\n",
1013                                 msg->type, msg->field, p->sharename));
1014
1015                         /*
1016                          * if the is a printer notification handle and not a job notification
1017                          * type, then set the id to 0.  Other wise just use what was specified
1018                          * in the message.
1019                          *
1020                          * When registering change notification on a print server handle
1021                          * we always need to send back the id (snum) matching the printer
1022                          * for which the change took place.  For change notify registered
1023                          * on a printer handle, this does not matter and the id should be 0.
1024                          *
1025                          * --jerry
1026                          */
1027
1028                         if ( ( p->printer_type == SPLHND_PRINTER ) && ( msg->type == PRINTER_NOTIFY_TYPE ) )
1029                                 id = 0;
1030                         else
1031                                 id = msg->id;
1032
1033
1034                         /* Convert unix jobid to smb jobid */
1035
1036                         if (msg->flags & SPOOLSS_NOTIFY_MSG_UNIX_JOBID) {
1037                                 id = sysjob_to_jobid(msg->id);
1038
1039                                 if (id == -1) {
1040                                         DEBUG(3, ("no such unix jobid %d\n", msg->id));
1041                                         goto done;
1042                                 }
1043                         }
1044
1045                         construct_info_data( &data[data_len], msg->type, msg->field, id );
1046
1047                         switch(msg->type) {
1048                         case PRINTER_NOTIFY_TYPE:
1049                                 if ( printer_notify_table[msg->field].fn )
1050                                         printer_notify_table[msg->field].fn(msg, &data[data_len], mem_ctx);
1051                                 break;
1052
1053                         case JOB_NOTIFY_TYPE:
1054                                 if ( job_notify_table[msg->field].fn )
1055                                         job_notify_table[msg->field].fn(msg, &data[data_len], mem_ctx);
1056                                 break;
1057
1058                         default:
1059                                 DEBUG(5, ("Unknown notification type %d\n", msg->type));
1060                                 goto done;
1061                         }
1062
1063                         data_len++;
1064                 }
1065
1066                 if ( sending_msg_count ) {
1067                         rpccli_spoolss_rrpcn( notify_cli_pipe, mem_ctx, &p->notify.client_hnd,
1068                                         data_len, data, p->notify.change, 0 );
1069                 }
1070         }
1071
1072 done:
1073         DEBUG(8,("send_notify2_changes: Exit...\n"));
1074         return;
1075 }
1076
1077 /***********************************************************************
1078  **********************************************************************/
1079
1080 static bool notify2_unpack_msg( SPOOLSS_NOTIFY_MSG *msg, struct timeval *tv, void *buf, size_t len )
1081 {
1082
1083         uint32 tv_sec, tv_usec;
1084         size_t offset = 0;
1085
1086         /* Unpack message */
1087
1088         offset += tdb_unpack((uint8 *)buf + offset, len - offset, "f",
1089                              msg->printer);
1090
1091         offset += tdb_unpack((uint8 *)buf + offset, len - offset, "ddddddd",
1092                                 &tv_sec, &tv_usec,
1093                                 &msg->type, &msg->field, &msg->id, &msg->len, &msg->flags);
1094
1095         if (msg->len == 0)
1096                 tdb_unpack((uint8 *)buf + offset, len - offset, "dd",
1097                            &msg->notify.value[0], &msg->notify.value[1]);
1098         else
1099                 tdb_unpack((uint8 *)buf + offset, len - offset, "B",
1100                            &msg->len, &msg->notify.data);
1101
1102         DEBUG(3, ("notify2_unpack_msg: got NOTIFY2 message for printer %s, jobid %u type %d, field 0x%02x, flags 0x%04x\n",
1103                   msg->printer, (unsigned int)msg->id, msg->type, msg->field, msg->flags));
1104
1105         tv->tv_sec = tv_sec;
1106         tv->tv_usec = tv_usec;
1107
1108         if (msg->len == 0)
1109                 DEBUG(3, ("notify2_unpack_msg: value1 = %d, value2 = %d\n", msg->notify.value[0],
1110                           msg->notify.value[1]));
1111         else
1112                 dump_data(3, (uint8 *)msg->notify.data, msg->len);
1113
1114         return True;
1115 }
1116
1117 /********************************************************************
1118  Receive a notify2 message list
1119  ********************************************************************/
1120
1121 static void receive_notify2_message_list(struct messaging_context *msg,
1122                                          void *private_data,
1123                                          uint32_t msg_type,
1124                                          struct server_id server_id,
1125                                          DATA_BLOB *data)
1126 {
1127         size_t                  msg_count, i;
1128         char                    *buf = (char *)data->data;
1129         char                    *msg_ptr;
1130         size_t                  msg_len;
1131         SPOOLSS_NOTIFY_MSG      notify;
1132         SPOOLSS_NOTIFY_MSG_CTR  messages;
1133         int                     num_groups;
1134
1135         if (data->length < 4) {
1136                 DEBUG(0,("receive_notify2_message_list: bad message format (len < 4)!\n"));
1137                 return;
1138         }
1139
1140         msg_count = IVAL(buf, 0);
1141         msg_ptr = buf + 4;
1142
1143         DEBUG(5, ("receive_notify2_message_list: got %lu messages in list\n", (unsigned long)msg_count));
1144
1145         if (msg_count == 0) {
1146                 DEBUG(0,("receive_notify2_message_list: bad message format (msg_count == 0) !\n"));
1147                 return;
1148         }
1149
1150         /* initialize the container */
1151
1152         ZERO_STRUCT( messages );
1153         notify_msg_ctr_init( &messages );
1154
1155         /*
1156          * build message groups for each printer identified
1157          * in a change_notify msg.  Remember that a PCN message
1158          * includes the handle returned for the srv_spoolss_replyopenprinter()
1159          * call.  Therefore messages are grouped according to printer handle.
1160          */
1161
1162         for ( i=0; i<msg_count; i++ ) {
1163                 struct timeval msg_tv;
1164
1165                 if (msg_ptr + 4 - buf > data->length) {
1166                         DEBUG(0,("receive_notify2_message_list: bad message format (len > buf_size) !\n"));
1167                         return;
1168                 }
1169
1170                 msg_len = IVAL(msg_ptr,0);
1171                 msg_ptr += 4;
1172
1173                 if (msg_ptr + msg_len - buf > data->length) {
1174                         DEBUG(0,("receive_notify2_message_list: bad message format (bad len) !\n"));
1175                         return;
1176                 }
1177
1178                 /* unpack messages */
1179
1180                 ZERO_STRUCT( notify );
1181                 notify2_unpack_msg( &notify, &msg_tv, msg_ptr, msg_len );
1182                 msg_ptr += msg_len;
1183
1184                 /* add to correct list in container */
1185
1186                 notify_msg_ctr_addmsg( &messages, &notify );
1187
1188                 /* free memory that might have been allocated by notify2_unpack_msg() */
1189
1190                 if ( notify.len != 0 )
1191                         SAFE_FREE( notify.notify.data );
1192         }
1193
1194         /* process each group of messages */
1195
1196         num_groups = notify_msg_ctr_numgroups( &messages );
1197         for ( i=0; i<num_groups; i++ )
1198                 send_notify2_changes( &messages, i );
1199
1200
1201         /* cleanup */
1202
1203         DEBUG(10,("receive_notify2_message_list: processed %u messages\n", (uint32)msg_count ));
1204
1205         notify_msg_ctr_destroy( &messages );
1206
1207         return;
1208 }
1209
1210 /********************************************************************
1211  Send a message to ourself about new driver being installed
1212  so we can upgrade the information for each printer bound to this
1213  driver
1214  ********************************************************************/
1215
1216 static bool srv_spoolss_drv_upgrade_printer(char* drivername)
1217 {
1218         int len = strlen(drivername);
1219
1220         if (!len)
1221                 return False;
1222
1223         DEBUG(10,("srv_spoolss_drv_upgrade_printer: Sending message about driver upgrade [%s]\n",
1224                 drivername));
1225
1226         messaging_send_buf(smbd_messaging_context(), procid_self(),
1227                            MSG_PRINTER_DRVUPGRADE,
1228                            (uint8 *)drivername, len+1);
1229
1230         return True;
1231 }
1232
1233 /**********************************************************************
1234  callback to receive a MSG_PRINTER_DRVUPGRADE message and interate
1235  over all printers, upgrading ones as necessary
1236  **********************************************************************/
1237
1238 void do_drv_upgrade_printer(struct messaging_context *msg,
1239                             void *private_data,
1240                             uint32_t msg_type,
1241                             struct server_id server_id,
1242                             DATA_BLOB *data)
1243 {
1244         fstring drivername;
1245         int snum;
1246         int n_services = lp_numservices();
1247         size_t len;
1248
1249         len = MIN(data->length,sizeof(drivername)-1);
1250         strncpy(drivername, (const char *)data->data, len);
1251
1252         DEBUG(10,("do_drv_upgrade_printer: Got message for new driver [%s]\n", drivername ));
1253
1254         /* Iterate the printer list */
1255
1256         for (snum=0; snum<n_services; snum++)
1257         {
1258                 if (lp_snum_ok(snum) && lp_print_ok(snum) )
1259                 {
1260                         WERROR result;
1261                         NT_PRINTER_INFO_LEVEL *printer = NULL;
1262
1263                         result = get_a_printer(NULL, &printer, 2, lp_const_servicename(snum));
1264                         if (!W_ERROR_IS_OK(result))
1265                                 continue;
1266
1267                         if (printer && printer->info_2 && !strcmp(drivername, printer->info_2->drivername))
1268                         {
1269                                 DEBUG(6,("Updating printer [%s]\n", printer->info_2->printername));
1270
1271                                 /* all we care about currently is the change_id */
1272
1273                                 result = mod_a_printer(printer, 2);
1274                                 if (!W_ERROR_IS_OK(result)) {
1275                                         DEBUG(3,("do_drv_upgrade_printer: mod_a_printer() failed with status [%s]\n",
1276                                                 dos_errstr(result)));
1277                                 }
1278                         }
1279
1280                         free_a_printer(&printer, 2);
1281                 }
1282         }
1283
1284         /* all done */
1285 }
1286
1287 /********************************************************************
1288  Update the cache for all printq's with a registered client
1289  connection
1290  ********************************************************************/
1291
1292 void update_monitored_printq_cache( void )
1293 {
1294         Printer_entry *printer = printers_list;
1295         int snum;
1296
1297         /* loop through all printers and update the cache where
1298            client_connected == True */
1299         while ( printer )
1300         {
1301                 if ( (printer->printer_type == SPLHND_PRINTER)
1302                         && printer->notify.client_connected )
1303                 {
1304                         snum = print_queue_snum(printer->sharename);
1305                         print_queue_status( snum, NULL, NULL );
1306                 }
1307
1308                 printer = printer->next;
1309         }
1310
1311         return;
1312 }
1313 /********************************************************************
1314  Send a message to ourself about new driver being installed
1315  so we can upgrade the information for each printer bound to this
1316  driver
1317  ********************************************************************/
1318
1319 static bool srv_spoolss_reset_printerdata(char* drivername)
1320 {
1321         int len = strlen(drivername);
1322
1323         if (!len)
1324                 return False;
1325
1326         DEBUG(10,("srv_spoolss_reset_printerdata: Sending message about resetting printerdata [%s]\n",
1327                 drivername));
1328
1329         messaging_send_buf(smbd_messaging_context(), procid_self(),
1330                            MSG_PRINTERDATA_INIT_RESET,
1331                            (uint8 *)drivername, len+1);
1332
1333         return True;
1334 }
1335
1336 /**********************************************************************
1337  callback to receive a MSG_PRINTERDATA_INIT_RESET message and interate
1338  over all printers, resetting printer data as neessary
1339  **********************************************************************/
1340
1341 void reset_all_printerdata(struct messaging_context *msg,
1342                            void *private_data,
1343                            uint32_t msg_type,
1344                            struct server_id server_id,
1345                            DATA_BLOB *data)
1346 {
1347         fstring drivername;
1348         int snum;
1349         int n_services = lp_numservices();
1350         size_t len;
1351
1352         len = MIN( data->length, sizeof(drivername)-1 );
1353         strncpy( drivername, (const char *)data->data, len );
1354
1355         DEBUG(10,("reset_all_printerdata: Got message for new driver [%s]\n", drivername ));
1356
1357         /* Iterate the printer list */
1358
1359         for ( snum=0; snum<n_services; snum++ )
1360         {
1361                 if ( lp_snum_ok(snum) && lp_print_ok(snum) )
1362                 {
1363                         WERROR result;
1364                         NT_PRINTER_INFO_LEVEL *printer = NULL;
1365
1366                         result = get_a_printer( NULL, &printer, 2, lp_const_servicename(snum) );
1367                         if ( !W_ERROR_IS_OK(result) )
1368                                 continue;
1369
1370                         /*
1371                          * if the printer is bound to the driver,
1372                          * then reset to the new driver initdata
1373                          */
1374
1375                         if ( printer && printer->info_2 && !strcmp(drivername, printer->info_2->drivername) )
1376                         {
1377                                 DEBUG(6,("reset_all_printerdata: Updating printer [%s]\n", printer->info_2->printername));
1378
1379                                 if ( !set_driver_init(printer, 2) ) {
1380                                         DEBUG(5,("reset_all_printerdata: Error resetting printer data for printer [%s], driver [%s]!\n",
1381                                                 printer->info_2->printername, printer->info_2->drivername));
1382                                 }
1383
1384                                 result = mod_a_printer( printer, 2 );
1385                                 if ( !W_ERROR_IS_OK(result) ) {
1386                                         DEBUG(3,("reset_all_printerdata: mod_a_printer() failed!  (%s)\n",
1387                                                 get_dos_error_msg(result)));
1388                                 }
1389                         }
1390
1391                         free_a_printer( &printer, 2 );
1392                 }
1393         }
1394
1395         /* all done */
1396
1397         return;
1398 }
1399
1400 /********************************************************************
1401  Copy routines used by convert_to_openprinterex()
1402  *******************************************************************/
1403
1404 static DEVICEMODE* dup_devicemode(TALLOC_CTX *ctx, DEVICEMODE *devmode)
1405 {
1406         DEVICEMODE *d;
1407         int len;
1408
1409         if (!devmode)
1410                 return NULL;
1411
1412         DEBUG (8,("dup_devmode\n"));
1413
1414         /* bulk copy first */
1415
1416         d = (DEVICEMODE *)TALLOC_MEMDUP(ctx, devmode, sizeof(DEVICEMODE));
1417         if (!d)
1418                 return NULL;
1419
1420         /* dup the pointer members separately */
1421
1422         len = unistrlen(devmode->devicename.buffer);
1423         if (len != -1) {
1424                 d->devicename.buffer = TALLOC_ARRAY(ctx, uint16, len);
1425                 if (!d->devicename.buffer) {
1426                         return NULL;
1427                 }
1428                 if (unistrcpy(d->devicename.buffer, devmode->devicename.buffer) != len)
1429                         return NULL;
1430         }
1431
1432
1433         len = unistrlen(devmode->formname.buffer);
1434         if (len != -1) {
1435                 d->formname.buffer = TALLOC_ARRAY(ctx, uint16, len);
1436                 if (!d->formname.buffer) {
1437                         return NULL;
1438                 }
1439                 if (unistrcpy(d->formname.buffer, devmode->formname.buffer) != len)
1440                         return NULL;
1441         }
1442
1443         if (devmode->driverextra) {
1444                 d->dev_private = (uint8 *)TALLOC_MEMDUP(ctx, devmode->dev_private,
1445                                                 devmode->driverextra);
1446                 if (!d->dev_private) {
1447                         return NULL;
1448                 }
1449         } else {
1450                 d->dev_private = NULL;
1451         }
1452         return d;
1453 }
1454
1455 static void copy_devmode_ctr(TALLOC_CTX *ctx, DEVMODE_CTR *new_ctr, DEVMODE_CTR *ctr)
1456 {
1457         if (!new_ctr || !ctr)
1458                 return;
1459
1460         DEBUG(8,("copy_devmode_ctr\n"));
1461
1462         new_ctr->size = ctr->size;
1463         new_ctr->devmode_ptr = ctr->devmode_ptr;
1464
1465         if(ctr->devmode_ptr)
1466                 new_ctr->devmode = dup_devicemode(ctx, ctr->devmode);
1467 }
1468
1469 static void copy_printer_default(TALLOC_CTX *ctx, PRINTER_DEFAULT *new_def, PRINTER_DEFAULT *def)
1470 {
1471         if (!new_def || !def)
1472                 return;
1473
1474         DEBUG(8,("copy_printer_defaults\n"));
1475
1476         new_def->datatype_ptr = def->datatype_ptr;
1477
1478         if (def->datatype_ptr)
1479                 copy_unistr2(&new_def->datatype, &def->datatype);
1480
1481         copy_devmode_ctr(ctx, &new_def->devmode_cont, &def->devmode_cont);
1482
1483         new_def->access_required = def->access_required;
1484 }
1485
1486 /********************************************************************
1487  * Convert a SPOOL_Q_OPEN_PRINTER structure to a
1488  * SPOOL_Q_OPEN_PRINTER_EX structure
1489  ********************************************************************/
1490
1491 static WERROR convert_to_openprinterex(TALLOC_CTX *ctx, SPOOL_Q_OPEN_PRINTER_EX *q_u_ex, SPOOL_Q_OPEN_PRINTER *q_u)
1492 {
1493         if (!q_u_ex || !q_u)
1494                 return WERR_OK;
1495
1496         DEBUG(8,("convert_to_openprinterex\n"));
1497
1498         if ( q_u->printername ) {
1499                 q_u_ex->printername = TALLOC_ZERO_P( ctx, UNISTR2 );
1500                 if (q_u_ex->printername == NULL)
1501                         return WERR_NOMEM;
1502                 copy_unistr2(q_u_ex->printername, q_u->printername);
1503         }
1504
1505         copy_printer_default(ctx, &q_u_ex->printer_default, &q_u->printer_default);
1506
1507         return WERR_OK;
1508 }
1509
1510 /********************************************************************
1511  * spoolss_open_printer
1512  *
1513  * called from the spoolss dispatcher
1514  ********************************************************************/
1515
1516 WERROR _spoolss_open_printer(pipes_struct *p, SPOOL_Q_OPEN_PRINTER *q_u, SPOOL_R_OPEN_PRINTER *r_u)
1517 {
1518         SPOOL_Q_OPEN_PRINTER_EX q_u_ex;
1519         SPOOL_R_OPEN_PRINTER_EX r_u_ex;
1520
1521         if (!q_u || !r_u)
1522                 return WERR_NOMEM;
1523
1524         ZERO_STRUCT(q_u_ex);
1525         ZERO_STRUCT(r_u_ex);
1526
1527         /* convert the OpenPrinter() call to OpenPrinterEx() */
1528
1529         r_u_ex.status = convert_to_openprinterex(p->mem_ctx, &q_u_ex, q_u);
1530         if (!W_ERROR_IS_OK(r_u_ex.status))
1531                 return r_u_ex.status;
1532
1533         r_u_ex.status = _spoolss_open_printer_ex(p, &q_u_ex, &r_u_ex);
1534
1535         /* convert back to OpenPrinter() */
1536
1537         memcpy(r_u, &r_u_ex, sizeof(*r_u));
1538
1539         if (W_ERROR_EQUAL(r_u->status, WERR_INVALID_PARAM)) {
1540                 /* OpenPrinterEx returns this for a bad
1541                  * printer name. We must return WERR_INVALID_PRINTER_NAME
1542                  * instead.
1543                  */
1544                 r_u->status = WERR_INVALID_PRINTER_NAME;
1545         }
1546         return r_u->status;
1547 }
1548
1549 /********************************************************************
1550  ********************************************************************/
1551
1552 WERROR _spoolss_open_printer_ex( pipes_struct *p, SPOOL_Q_OPEN_PRINTER_EX *q_u, SPOOL_R_OPEN_PRINTER_EX *r_u)
1553 {
1554         PRINTER_DEFAULT         *printer_default = &q_u->printer_default;
1555         POLICY_HND              *handle = &r_u->handle;
1556
1557         fstring name;
1558         int snum;
1559         Printer_entry *Printer=NULL;
1560
1561         if (!q_u->printername) {
1562                 return WERR_INVALID_PARAM;
1563         }
1564
1565         /* some sanity check because you can open a printer or a print server */
1566         /* aka: \\server\printer or \\server */
1567
1568         unistr2_to_ascii(name, q_u->printername, sizeof(name));
1569
1570         DEBUGADD(3,("checking name: %s\n",name));
1571
1572         if (!open_printer_hnd(p, handle, name, 0)) {
1573                 return WERR_INVALID_PARAM;
1574         }
1575
1576         Printer=find_printer_index_by_hnd(p, handle);
1577         if ( !Printer ) {
1578                 DEBUG(0,(" _spoolss_open_printer_ex: logic error.  Can't find printer "
1579                         "handle we created for printer %s\n", name ));
1580                 close_printer_handle(p,handle);
1581                 return WERR_INVALID_PARAM;
1582         }
1583
1584         /*
1585          * First case: the user is opening the print server:
1586          *
1587          * Disallow MS AddPrinterWizard if parameter disables it. A Win2k
1588          * client 1st tries an OpenPrinterEx with access==0, MUST be allowed.
1589          *
1590          * Then both Win2k and WinNT clients try an OpenPrinterEx with
1591          * SERVER_ALL_ACCESS, which we allow only if the user is root (uid=0)
1592          * or if the user is listed in the smb.conf printer admin parameter.
1593          *
1594          * Then they try OpenPrinterEx with SERVER_READ which we allow. This lets the
1595          * client view printer folder, but does not show the MSAPW.
1596          *
1597          * Note: this test needs code to check access rights here too. Jeremy
1598          * could you look at this?
1599          *
1600          * Second case: the user is opening a printer:
1601          * NT doesn't let us connect to a printer if the connecting user
1602          * doesn't have print permission.
1603          *
1604          * Third case: user is opening a Port Monitor
1605          * access checks same as opening a handle to the print server.
1606          */
1607
1608         switch (Printer->printer_type )
1609         {
1610         case SPLHND_SERVER:
1611         case SPLHND_PORTMON_TCP:
1612         case SPLHND_PORTMON_LOCAL:
1613                 /* Printserver handles use global struct... */
1614
1615                 snum = -1;
1616
1617                 /* Map standard access rights to object specific access rights */
1618
1619                 se_map_standard(&printer_default->access_required,
1620                                 &printserver_std_mapping);
1621
1622                 /* Deny any object specific bits that don't apply to print
1623                    servers (i.e printer and job specific bits) */
1624
1625                 printer_default->access_required &= SPECIFIC_RIGHTS_MASK;
1626
1627                 if (printer_default->access_required &
1628                     ~(SERVER_ACCESS_ADMINISTER | SERVER_ACCESS_ENUMERATE)) {
1629                         DEBUG(3, ("access DENIED for non-printserver bits\n"));
1630                         close_printer_handle(p, handle);
1631                         return WERR_ACCESS_DENIED;
1632                 }
1633
1634                 /* Allow admin access */
1635
1636                 if ( printer_default->access_required & SERVER_ACCESS_ADMINISTER )
1637                 {
1638                         SE_PRIV se_printop = SE_PRINT_OPERATOR;
1639
1640                         if (!lp_ms_add_printer_wizard()) {
1641                                 close_printer_handle(p, handle);
1642                                 return WERR_ACCESS_DENIED;
1643                         }
1644
1645                         /* if the user is not root, doesn't have SE_PRINT_OPERATOR privilege,
1646                            and not a printer admin, then fail */
1647
1648                         if ((p->pipe_user.ut.uid != 0) &&
1649                             !user_has_privileges(p->pipe_user.nt_user_token,
1650                                                  &se_printop ) &&
1651                             !token_contains_name_in_list(
1652                                     uidtoname(p->pipe_user.ut.uid), NULL,
1653                                     p->pipe_user.nt_user_token,
1654                                     lp_printer_admin(snum))) {
1655                                 close_printer_handle(p, handle);
1656                                 return WERR_ACCESS_DENIED;
1657                         }
1658
1659                         printer_default->access_required = SERVER_ACCESS_ADMINISTER;
1660                 }
1661                 else
1662                 {
1663                         printer_default->access_required = SERVER_ACCESS_ENUMERATE;
1664                 }
1665
1666                 DEBUG(4,("Setting print server access = %s\n", (printer_default->access_required == SERVER_ACCESS_ADMINISTER)
1667                         ? "SERVER_ACCESS_ADMINISTER" : "SERVER_ACCESS_ENUMERATE" ));
1668
1669                 /* We fall through to return WERR_OK */
1670                 break;
1671
1672         case SPLHND_PRINTER:
1673                 /* NT doesn't let us connect to a printer if the connecting user
1674                    doesn't have print permission.  */
1675
1676                 if (!get_printer_snum(p, handle, &snum, NULL)) {
1677                         close_printer_handle(p, handle);
1678                         return WERR_BADFID;
1679                 }
1680
1681                 se_map_standard(&printer_default->access_required, &printer_std_mapping);
1682
1683                 /* map an empty access mask to the minimum access mask */
1684                 if (printer_default->access_required == 0x0)
1685                         printer_default->access_required = PRINTER_ACCESS_USE;
1686
1687                 /*
1688                  * If we are not serving the printer driver for this printer,
1689                  * map PRINTER_ACCESS_ADMINISTER to PRINTER_ACCESS_USE.  This
1690                  * will keep NT clients happy  --jerry
1691                  */
1692
1693                 if (lp_use_client_driver(snum)
1694                         && (printer_default->access_required & PRINTER_ACCESS_ADMINISTER))
1695                 {
1696                         printer_default->access_required = PRINTER_ACCESS_USE;
1697                 }
1698
1699                 /* check smb.conf parameters and the the sec_desc */
1700
1701                 if ( !check_access(smbd_server_fd(), lp_hostsallow(snum), lp_hostsdeny(snum)) ) {
1702                         DEBUG(3, ("access DENIED (hosts allow/deny) for printer open\n"));
1703                         return WERR_ACCESS_DENIED;
1704                 }
1705
1706                 if (!user_ok_token(uidtoname(p->pipe_user.ut.uid),
1707                                    p->pipe_user.nt_user_token, snum) ||
1708                     !print_access_check(&p->pipe_user, snum,
1709                                         printer_default->access_required)) {
1710                         DEBUG(3, ("access DENIED for printer open\n"));
1711                         close_printer_handle(p, handle);
1712                         return WERR_ACCESS_DENIED;
1713                 }
1714
1715                 if ((printer_default->access_required & SPECIFIC_RIGHTS_MASK)& ~(PRINTER_ACCESS_ADMINISTER|PRINTER_ACCESS_USE)) {
1716                         DEBUG(3, ("access DENIED for printer open - unknown bits\n"));
1717                         close_printer_handle(p, handle);
1718                         return WERR_ACCESS_DENIED;
1719                 }
1720
1721                 if (printer_default->access_required & PRINTER_ACCESS_ADMINISTER)
1722                         printer_default->access_required = PRINTER_ACCESS_ADMINISTER;
1723                 else
1724                         printer_default->access_required = PRINTER_ACCESS_USE;
1725
1726                 DEBUG(4,("Setting printer access = %s\n", (printer_default->access_required == PRINTER_ACCESS_ADMINISTER)
1727                         ? "PRINTER_ACCESS_ADMINISTER" : "PRINTER_ACCESS_USE" ));
1728
1729                 break;
1730
1731         default:
1732                 /* sanity check to prevent programmer error */
1733                 return WERR_BADFID;
1734         }
1735
1736         Printer->access_granted = printer_default->access_required;
1737
1738         /*
1739          * If the client sent a devmode in the OpenPrinter() call, then
1740          * save it here in case we get a job submission on this handle
1741          */
1742
1743          if ( (Printer->printer_type != SPLHND_SERVER)
1744                 && q_u->printer_default.devmode_cont.devmode_ptr )
1745          {
1746                 convert_devicemode( Printer->sharename, q_u->printer_default.devmode_cont.devmode,
1747                         &Printer->nt_devmode );
1748          }
1749
1750 #if 0   /* JERRY -- I'm doubtful this is really effective */
1751         /* HACK ALERT!!! Sleep for 1/3 of a second to try trigger a LAN/WAN
1752            optimization in Windows 2000 clients  --jerry */
1753
1754         if ( (printer_default->access_required == PRINTER_ACCESS_ADMINISTER)
1755                 && (RA_WIN2K == get_remote_arch()) )
1756         {
1757                 DEBUG(10,("_spoolss_open_printer_ex: Enabling LAN/WAN hack for Win2k clients.\n"));
1758                 sys_usleep( 500000 );
1759         }
1760 #endif
1761
1762         return WERR_OK;
1763 }
1764
1765 /****************************************************************************
1766 ****************************************************************************/
1767
1768 static bool convert_printer_info(const SPOOL_PRINTER_INFO_LEVEL *uni,
1769                                 NT_PRINTER_INFO_LEVEL *printer, uint32 level)
1770 {
1771         bool ret;
1772
1773         switch (level) {
1774                 case 2:
1775                         /* allocate memory if needed.  Messy because
1776                            convert_printer_info is used to update an existing
1777                            printer or build a new one */
1778
1779                         if ( !printer->info_2 ) {
1780                                 printer->info_2 = TALLOC_ZERO_P( printer, NT_PRINTER_INFO_LEVEL_2 );
1781                                 if ( !printer->info_2 ) {
1782                                         DEBUG(0,("convert_printer_info: talloc() failed!\n"));
1783                                         return False;
1784                                 }
1785                         }
1786
1787                         ret = uni_2_asc_printer_info_2(uni->info_2, printer->info_2);
1788                         printer->info_2->setuptime = time(NULL);
1789
1790                         return ret;
1791         }
1792
1793         return False;
1794 }
1795
1796 static bool convert_printer_driver_info(const SPOOL_PRINTER_DRIVER_INFO_LEVEL *uni,
1797                                         NT_PRINTER_DRIVER_INFO_LEVEL *printer, uint32 level)
1798 {
1799         bool result = True;
1800
1801         switch (level) {
1802                 case 3:
1803                         printer->info_3=NULL;
1804                         if (!uni_2_asc_printer_driver_3(uni->info_3, &printer->info_3))
1805                                 result = False;
1806                         break;
1807                 case 6:
1808                         printer->info_6=NULL;
1809                         if (!uni_2_asc_printer_driver_6(uni->info_6, &printer->info_6))
1810                                 result = False;
1811                         break;
1812                 default:
1813                         break;
1814         }
1815
1816         return result;
1817 }
1818
1819 bool convert_devicemode(const char *printername, const DEVICEMODE *devmode,
1820                                 NT_DEVICEMODE **pp_nt_devmode)
1821 {
1822         NT_DEVICEMODE *nt_devmode = *pp_nt_devmode;
1823
1824         /*
1825          * Ensure nt_devmode is a valid pointer
1826          * as we will be overwriting it.
1827          */
1828
1829         if (nt_devmode == NULL) {
1830                 DEBUG(5, ("convert_devicemode: allocating a generic devmode\n"));
1831                 if ((nt_devmode = construct_nt_devicemode(printername)) == NULL)
1832                         return False;
1833         }
1834
1835         rpcstr_pull(nt_devmode->devicename,devmode->devicename.buffer, 31, -1, 0);
1836         rpcstr_pull(nt_devmode->formname,devmode->formname.buffer, 31, -1, 0);
1837
1838         nt_devmode->specversion=devmode->specversion;
1839         nt_devmode->driverversion=devmode->driverversion;
1840         nt_devmode->size=devmode->size;
1841         nt_devmode->fields=devmode->fields;
1842         nt_devmode->orientation=devmode->orientation;
1843         nt_devmode->papersize=devmode->papersize;
1844         nt_devmode->paperlength=devmode->paperlength;
1845         nt_devmode->paperwidth=devmode->paperwidth;
1846         nt_devmode->scale=devmode->scale;
1847         nt_devmode->copies=devmode->copies;
1848         nt_devmode->defaultsource=devmode->defaultsource;
1849         nt_devmode->printquality=devmode->printquality;
1850         nt_devmode->color=devmode->color;
1851         nt_devmode->duplex=devmode->duplex;
1852         nt_devmode->yresolution=devmode->yresolution;
1853         nt_devmode->ttoption=devmode->ttoption;
1854         nt_devmode->collate=devmode->collate;
1855
1856         nt_devmode->logpixels=devmode->logpixels;
1857         nt_devmode->bitsperpel=devmode->bitsperpel;
1858         nt_devmode->pelswidth=devmode->pelswidth;
1859         nt_devmode->pelsheight=devmode->pelsheight;
1860         nt_devmode->displayflags=devmode->displayflags;
1861         nt_devmode->displayfrequency=devmode->displayfrequency;
1862         nt_devmode->icmmethod=devmode->icmmethod;
1863         nt_devmode->icmintent=devmode->icmintent;
1864         nt_devmode->mediatype=devmode->mediatype;
1865         nt_devmode->dithertype=devmode->dithertype;
1866         nt_devmode->reserved1=devmode->reserved1;
1867         nt_devmode->reserved2=devmode->reserved2;
1868         nt_devmode->panningwidth=devmode->panningwidth;
1869         nt_devmode->panningheight=devmode->panningheight;
1870
1871         /*
1872          * Only change private and driverextra if the incoming devmode
1873          * has a new one. JRA.
1874          */
1875
1876         if ((devmode->driverextra != 0) && (devmode->dev_private != NULL)) {
1877                 SAFE_FREE(nt_devmode->nt_dev_private);
1878                 nt_devmode->driverextra=devmode->driverextra;
1879                 if((nt_devmode->nt_dev_private=SMB_MALLOC_ARRAY(uint8, nt_devmode->driverextra)) == NULL)
1880                         return False;
1881                 memcpy(nt_devmode->nt_dev_private, devmode->dev_private, nt_devmode->driverextra);
1882         }
1883
1884         *pp_nt_devmode = nt_devmode;
1885
1886         return True;
1887 }
1888
1889 /********************************************************************
1890  * _spoolss_enddocprinter_internal.
1891  ********************************************************************/
1892
1893 static WERROR _spoolss_enddocprinter_internal(pipes_struct *p, POLICY_HND *handle)
1894 {
1895         Printer_entry *Printer=find_printer_index_by_hnd(p, handle);
1896         int snum;
1897
1898         if (!Printer) {
1899                 DEBUG(2,("_spoolss_enddocprinter_internal: Invalid handle (%s:%u:%u)\n", OUR_HANDLE(handle)));
1900                 return WERR_BADFID;
1901         }
1902
1903         if (!get_printer_snum(p, handle, &snum, NULL))
1904                 return WERR_BADFID;
1905
1906         Printer->document_started=False;
1907         print_job_end(snum, Printer->jobid,NORMAL_CLOSE);
1908         /* error codes unhandled so far ... */
1909
1910         return WERR_OK;
1911 }
1912
1913 /********************************************************************
1914  * api_spoolss_closeprinter
1915  ********************************************************************/
1916
1917 WERROR _spoolss_closeprinter(pipes_struct *p, SPOOL_Q_CLOSEPRINTER *q_u, SPOOL_R_CLOSEPRINTER *r_u)
1918 {
1919         POLICY_HND *handle = &q_u->handle;
1920
1921         Printer_entry *Printer=find_printer_index_by_hnd(p, handle);
1922
1923         if (Printer && Printer->document_started)
1924                 _spoolss_enddocprinter_internal(p, handle);          /* print job was not closed */
1925
1926         if (!close_printer_handle(p, handle))
1927                 return WERR_BADFID;
1928
1929         /* clear the returned printer handle.  Observed behavior
1930            from Win2k server.  Don't think this really matters.
1931            Previous code just copied the value of the closed
1932            handle.    --jerry */
1933
1934         memset(&r_u->handle, '\0', sizeof(r_u->handle));
1935
1936         return WERR_OK;
1937 }
1938
1939 /********************************************************************
1940  * api_spoolss_deleteprinter
1941
1942  ********************************************************************/
1943
1944 WERROR _spoolss_deleteprinter(pipes_struct *p, SPOOL_Q_DELETEPRINTER *q_u, SPOOL_R_DELETEPRINTER *r_u)
1945 {
1946         POLICY_HND *handle = &q_u->handle;
1947         Printer_entry *Printer=find_printer_index_by_hnd(p, handle);
1948         WERROR result;
1949
1950         if (Printer && Printer->document_started)
1951                 _spoolss_enddocprinter_internal(p, handle);  /* print job was not closed */
1952
1953         memcpy(&r_u->handle, &q_u->handle, sizeof(r_u->handle));
1954
1955         result = delete_printer_handle(p, handle);
1956
1957         update_c_setprinter(False);
1958
1959         return result;
1960 }
1961
1962 /*******************************************************************
1963  * static function to lookup the version id corresponding to an
1964  * long architecture string
1965  ******************************************************************/
1966
1967 static int get_version_id (char * arch)
1968 {
1969         int i;
1970         struct table_node archi_table[]= {
1971
1972                 {"Windows 4.0",          "WIN40",       0 },
1973                 {"Windows NT x86",       "W32X86",      2 },
1974                 {"Windows NT R4000",     "W32MIPS",     2 },
1975                 {"Windows NT Alpha_AXP", "W32ALPHA",    2 },
1976                 {"Windows NT PowerPC",   "W32PPC",      2 },
1977                 {"Windows IA64",         "IA64",        3 },
1978                 {"Windows x64",          "x64",         3 },
1979                 {NULL,                   "",            -1 }
1980         };
1981
1982         for (i=0; archi_table[i].long_archi != NULL; i++)
1983         {
1984                 if (strcmp(arch, archi_table[i].long_archi) == 0)
1985                         return (archi_table[i].version);
1986         }
1987
1988         return -1;
1989 }
1990
1991 /********************************************************************
1992  * _spoolss_deleteprinterdriver
1993  ********************************************************************/
1994
1995 WERROR _spoolss_deleteprinterdriver(pipes_struct *p, SPOOL_Q_DELETEPRINTERDRIVER *q_u, SPOOL_R_DELETEPRINTERDRIVER *r_u)
1996 {
1997         fstring                         driver;
1998         fstring                         arch;
1999         NT_PRINTER_DRIVER_INFO_LEVEL    info;
2000         NT_PRINTER_DRIVER_INFO_LEVEL    info_win2k;
2001         int                             version;
2002         WERROR                          status;
2003         WERROR                          status_win2k = WERR_ACCESS_DENIED;
2004         SE_PRIV                         se_printop = SE_PRINT_OPERATOR;
2005
2006         /* if the user is not root, doesn't have SE_PRINT_OPERATOR privilege,
2007            and not a printer admin, then fail */
2008
2009         if ( (p->pipe_user.ut.uid != 0)
2010                 && !user_has_privileges(p->pipe_user.nt_user_token, &se_printop )
2011                 && !token_contains_name_in_list( uidtoname(p->pipe_user.ut.uid),
2012                     NULL, p->pipe_user.nt_user_token, lp_printer_admin(-1)) )
2013         {
2014                 return WERR_ACCESS_DENIED;
2015         }
2016
2017         unistr2_to_ascii(driver, &q_u->driver, sizeof(driver));
2018         unistr2_to_ascii(arch,   &q_u->arch,   sizeof(arch));
2019
2020         /* check that we have a valid driver name first */
2021
2022         if ((version=get_version_id(arch)) == -1)
2023                 return WERR_INVALID_ENVIRONMENT;
2024
2025         ZERO_STRUCT(info);
2026         ZERO_STRUCT(info_win2k);
2027
2028         if (!W_ERROR_IS_OK(get_a_printer_driver(&info, 3, driver, arch, version)))
2029         {
2030                 /* try for Win2k driver if "Windows NT x86" */
2031
2032                 if ( version == 2 ) {
2033                         version = 3;
2034                         if (!W_ERROR_IS_OK(get_a_printer_driver(&info, 3, driver, arch, version))) {
2035                                 status = WERR_UNKNOWN_PRINTER_DRIVER;
2036                                 goto done;
2037                         }
2038                 }
2039                 /* otherwise it was a failure */
2040                 else {
2041                         status = WERR_UNKNOWN_PRINTER_DRIVER;
2042                         goto done;
2043                 }
2044
2045         }
2046
2047         if (printer_driver_in_use(info.info_3)) {
2048                 status = WERR_PRINTER_DRIVER_IN_USE;
2049                 goto done;
2050         }
2051
2052         if ( version == 2 )
2053         {
2054                 if (W_ERROR_IS_OK(get_a_printer_driver(&info_win2k, 3, driver, arch, 3)))
2055                 {
2056                         /* if we get to here, we now have 2 driver info structures to remove */
2057                         /* remove the Win2k driver first*/
2058
2059                         status_win2k = delete_printer_driver(info_win2k.info_3, &p->pipe_user, 3, False );
2060                         free_a_printer_driver( info_win2k, 3 );
2061
2062                         /* this should not have failed---if it did, report to client */
2063                         if ( !W_ERROR_IS_OK(status_win2k) )
2064                         {
2065                                 status = status_win2k;
2066                                 goto done;
2067                         }
2068                 }
2069         }
2070
2071         status = delete_printer_driver(info.info_3, &p->pipe_user, version, False);
2072
2073         /* if at least one of the deletes succeeded return OK */
2074
2075         if ( W_ERROR_IS_OK(status) || W_ERROR_IS_OK(status_win2k) )
2076                 status = WERR_OK;
2077
2078 done:
2079         free_a_printer_driver( info, 3 );
2080
2081         return status;
2082 }
2083
2084 /********************************************************************
2085  * spoolss_deleteprinterdriverex
2086  ********************************************************************/
2087
2088 WERROR _spoolss_deleteprinterdriverex(pipes_struct *p, SPOOL_Q_DELETEPRINTERDRIVEREX *q_u, SPOOL_R_DELETEPRINTERDRIVEREX *r_u)
2089 {
2090         fstring                         driver;
2091         fstring                         arch;
2092         NT_PRINTER_DRIVER_INFO_LEVEL    info;
2093         NT_PRINTER_DRIVER_INFO_LEVEL    info_win2k;
2094         int                             version;
2095         uint32                          flags = q_u->delete_flags;
2096         bool                            delete_files;
2097         WERROR                          status;
2098         WERROR                          status_win2k = WERR_ACCESS_DENIED;
2099         SE_PRIV                         se_printop = SE_PRINT_OPERATOR;
2100
2101         /* if the user is not root, doesn't have SE_PRINT_OPERATOR privilege,
2102            and not a printer admin, then fail */
2103
2104         if ( (p->pipe_user.ut.uid != 0)
2105                 && !user_has_privileges(p->pipe_user.nt_user_token, &se_printop )
2106                 && !token_contains_name_in_list( uidtoname(p->pipe_user.ut.uid),
2107                     NULL, p->pipe_user.nt_user_token, lp_printer_admin(-1)) )
2108         {
2109                 return WERR_ACCESS_DENIED;
2110         }
2111
2112         unistr2_to_ascii(driver, &q_u->driver, sizeof(driver));
2113         unistr2_to_ascii(arch,   &q_u->arch,   sizeof(arch));
2114
2115         /* check that we have a valid driver name first */
2116         if ((version=get_version_id(arch)) == -1) {
2117                 /* this is what NT returns */
2118                 return WERR_INVALID_ENVIRONMENT;
2119         }
2120
2121         if ( flags & DPD_DELETE_SPECIFIC_VERSION )
2122                 version = q_u->version;
2123
2124         ZERO_STRUCT(info);
2125         ZERO_STRUCT(info_win2k);
2126
2127         status = get_a_printer_driver(&info, 3, driver, arch, version);
2128
2129         if ( !W_ERROR_IS_OK(status) )
2130         {
2131                 /*
2132                  * if the client asked for a specific version,
2133                  * or this is something other than Windows NT x86,
2134                  * then we've failed
2135                  */
2136
2137                 if ( (flags&DPD_DELETE_SPECIFIC_VERSION) || (version !=2) )
2138                         goto done;
2139
2140                 /* try for Win2k driver if "Windows NT x86" */
2141
2142                 version = 3;
2143                 if (!W_ERROR_IS_OK(get_a_printer_driver(&info, 3, driver, arch, version))) {
2144                         status = WERR_UNKNOWN_PRINTER_DRIVER;
2145                         goto done;
2146                 }
2147         }
2148
2149         if ( printer_driver_in_use(info.info_3) ) {
2150                 status = WERR_PRINTER_DRIVER_IN_USE;
2151                 goto done;
2152         }
2153
2154         /*
2155          * we have a couple of cases to consider.
2156          * (1) Are any files in use?  If so and DPD_DELTE_ALL_FILE is set,
2157          *     then the delete should fail if **any** files overlap with
2158          *     other drivers
2159          * (2) If DPD_DELTE_UNUSED_FILES is sert, then delete all
2160          *     non-overlapping files
2161          * (3) If neither DPD_DELTE_ALL_FILE nor DPD_DELTE_ALL_FILES
2162          *     is set, the do not delete any files
2163          * Refer to MSDN docs on DeletePrinterDriverEx() for details.
2164          */
2165
2166         delete_files = flags & (DPD_DELETE_ALL_FILES|DPD_DELETE_UNUSED_FILES);
2167
2168         /* fail if any files are in use and DPD_DELETE_ALL_FILES is set */
2169
2170         if ( delete_files && printer_driver_files_in_use(info.info_3) & (flags&DPD_DELETE_ALL_FILES) ) {
2171                 /* no idea of the correct error here */
2172                 status = WERR_ACCESS_DENIED;
2173                 goto done;
2174         }
2175
2176
2177         /* also check for W32X86/3 if necessary; maybe we already have? */
2178
2179         if ( (version == 2) && ((flags&DPD_DELETE_SPECIFIC_VERSION) != DPD_DELETE_SPECIFIC_VERSION)  ) {
2180                 if (W_ERROR_IS_OK(get_a_printer_driver(&info_win2k, 3, driver, arch, 3)))
2181                 {
2182
2183                         if ( delete_files && printer_driver_files_in_use(info_win2k.info_3) & (flags&DPD_DELETE_ALL_FILES) ) {
2184                                 /* no idea of the correct error here */
2185                                 free_a_printer_driver( info_win2k, 3 );
2186                                 status = WERR_ACCESS_DENIED;
2187                                 goto done;
2188                         }
2189
2190                         /* if we get to here, we now have 2 driver info structures to remove */
2191                         /* remove the Win2k driver first*/
2192
2193                         status_win2k = delete_printer_driver(info_win2k.info_3, &p->pipe_user, 3, delete_files);
2194                         free_a_printer_driver( info_win2k, 3 );
2195
2196                         /* this should not have failed---if it did, report to client */
2197
2198                         if ( !W_ERROR_IS_OK(status_win2k) )
2199                                 goto done;
2200                 }
2201         }
2202
2203         status = delete_printer_driver(info.info_3, &p->pipe_user, version, delete_files);
2204
2205         if ( W_ERROR_IS_OK(status) || W_ERROR_IS_OK(status_win2k) )
2206                 status = WERR_OK;
2207 done:
2208         free_a_printer_driver( info, 3 );
2209
2210         return status;
2211 }
2212
2213
2214 /****************************************************************************
2215  Internal routine for retreiving printerdata
2216  ***************************************************************************/
2217
2218 static WERROR get_printer_dataex( TALLOC_CTX *ctx, NT_PRINTER_INFO_LEVEL *printer,
2219                                   const char *key, const char *value, uint32 *type, uint8 **data,
2220                                   uint32 *needed, uint32 in_size  )
2221 {
2222         REGISTRY_VALUE          *val;
2223         uint32                  size;
2224         int                     data_len;
2225
2226         if ( !(val = get_printer_data( printer->info_2, key, value)) )
2227                 return WERR_BADFILE;
2228
2229         *type = regval_type( val );
2230
2231         DEBUG(5,("get_printer_dataex: allocating %d\n", in_size));
2232
2233         size = regval_size( val );
2234
2235         /* copy the min(in_size, len) */
2236
2237         if ( in_size ) {
2238                 data_len = (size > in_size) ? in_size : size*sizeof(uint8);
2239
2240                 /* special case for 0 length values */
2241                 if ( data_len ) {
2242                         if ( (*data  = (uint8 *)TALLOC_MEMDUP(ctx, regval_data_p(val), data_len)) == NULL )
2243                                 return WERR_NOMEM;
2244                 }
2245                 else {
2246                         if ( (*data  = (uint8 *)TALLOC_ZERO(ctx, in_size)) == NULL )
2247                                 return WERR_NOMEM;
2248                 }
2249         }
2250         else
2251                 *data = NULL;
2252
2253         *needed = size;
2254
2255         DEBUG(5,("get_printer_dataex: copy done\n"));
2256
2257         return WERR_OK;
2258 }
2259
2260 /****************************************************************************
2261  Internal routine for removing printerdata
2262  ***************************************************************************/
2263
2264 static WERROR delete_printer_dataex( NT_PRINTER_INFO_LEVEL *printer, const char *key, const char *value )
2265 {
2266         return delete_printer_data( printer->info_2, key, value );
2267 }
2268
2269 /****************************************************************************
2270  Internal routine for storing printerdata
2271  ***************************************************************************/
2272
2273 WERROR set_printer_dataex( NT_PRINTER_INFO_LEVEL *printer, const char *key, const char *value,
2274                                   uint32 type, uint8 *data, int real_len  )
2275 {
2276         /* the registry objects enforce uniqueness based on value name */
2277
2278         return add_printer_data( printer->info_2, key, value, type, data, real_len );
2279 }
2280
2281 /********************************************************************
2282  GetPrinterData on a printer server Handle.
2283 ********************************************************************/
2284
2285 static WERROR getprinterdata_printer_server(TALLOC_CTX *ctx, fstring value, uint32 *type, uint8 **data, uint32 *needed, uint32 in_size)
2286 {
2287         int i;
2288
2289         DEBUG(8,("getprinterdata_printer_server:%s\n", value));
2290
2291         if (!StrCaseCmp(value, "W3SvcInstalled")) {
2292                 *type = REG_DWORD;
2293                 if ( !(*data = TALLOC_ARRAY(ctx, uint8, sizeof(uint32) )) )
2294                         return WERR_NOMEM;
2295                 SIVAL(*data, 0, 0x00);
2296                 *needed = 0x4;
2297                 return WERR_OK;
2298         }
2299
2300         if (!StrCaseCmp(value, "BeepEnabled")) {
2301                 *type = REG_DWORD;
2302                 if ( !(*data = TALLOC_ARRAY(ctx, uint8, sizeof(uint32) )) )
2303                         return WERR_NOMEM;
2304                 SIVAL(*data, 0, 0x00);
2305                 *needed = 0x4;
2306                 return WERR_OK;
2307         }
2308
2309         if (!StrCaseCmp(value, "EventLog")) {
2310                 *type = REG_DWORD;
2311                 if ( !(*data = TALLOC_ARRAY(ctx, uint8, sizeof(uint32) )) )
2312                         return WERR_NOMEM;
2313                 /* formally was 0x1b */
2314                 SIVAL(*data, 0, 0x0);
2315                 *needed = 0x4;
2316                 return WERR_OK;
2317         }
2318
2319         if (!StrCaseCmp(value, "NetPopup")) {
2320                 *type = REG_DWORD;
2321                 if ( !(*data = TALLOC_ARRAY(ctx, uint8, sizeof(uint32) )) )
2322                         return WERR_NOMEM;
2323                 SIVAL(*data, 0, 0x00);
2324                 *needed = 0x4;
2325                 return WERR_OK;
2326         }
2327
2328         if (!StrCaseCmp(value, "MajorVersion")) {
2329                 *type = REG_DWORD;
2330                 if ( !(*data = TALLOC_ARRAY(ctx, uint8, sizeof(uint32) )) )
2331                         return WERR_NOMEM;
2332
2333                 /* Windows NT 4.0 seems to not allow uploading of drivers
2334                    to a server that reports 0x3 as the MajorVersion.
2335                    need to investigate more how Win2k gets around this .
2336                    -- jerry */
2337
2338                 if ( RA_WINNT == get_remote_arch() )
2339                         SIVAL(*data, 0, 2);
2340                 else
2341                         SIVAL(*data, 0, 3);
2342
2343                 *needed = 0x4;
2344                 return WERR_OK;
2345         }
2346
2347         if (!StrCaseCmp(value, "MinorVersion")) {
2348                 *type = REG_DWORD;
2349                 if ( !(*data = TALLOC_ARRAY(ctx, uint8, sizeof(uint32) )) )
2350                         return WERR_NOMEM;
2351                 SIVAL(*data, 0, 0);
2352                 *needed = 0x4;
2353                 return WERR_OK;
2354         }
2355
2356         /* REG_BINARY
2357          *  uint32 size          = 0x114
2358          *  uint32 major         = 5
2359          *  uint32 minor         = [0|1]
2360          *  uint32 build         = [2195|2600]
2361          *  extra unicode string = e.g. "Service Pack 3"
2362          */
2363         if (!StrCaseCmp(value, "OSVersion")) {
2364                 *type = REG_BINARY;
2365                 *needed = 0x114;
2366
2367                 if ( !(*data = TALLOC_ZERO_ARRAY(ctx, uint8, (*needed > in_size) ? *needed:in_size )) )
2368                         return WERR_NOMEM;
2369
2370                 SIVAL(*data, 0, *needed);       /* size */
2371                 SIVAL(*data, 4, 5);             /* Windows 2000 == 5.0 */
2372                 SIVAL(*data, 8, 0);
2373                 SIVAL(*data, 12, 2195);         /* build */
2374
2375                 /* leave extra string empty */
2376
2377                 return WERR_OK;
2378         }
2379
2380
2381         if (!StrCaseCmp(value, "DefaultSpoolDirectory")) {
2382                 const char *string="C:\\PRINTERS";
2383                 *type = REG_SZ;
2384                 *needed = 2*(strlen(string)+1);
2385                 if((*data  = (uint8 *)TALLOC(ctx, (*needed > in_size) ? *needed:in_size )) == NULL)
2386                         return WERR_NOMEM;
2387                 memset(*data, 0, (*needed > in_size) ? *needed:in_size);
2388
2389                 /* it's done by hand ready to go on the wire */
2390                 for (i=0; i<strlen(string); i++) {
2391                         (*data)[2*i]=string[i];
2392                         (*data)[2*i+1]='\0';
2393                 }
2394                 return WERR_OK;
2395         }
2396
2397         if (!StrCaseCmp(value, "Architecture")) {
2398                 const char *string="Windows NT x86";
2399                 *type = REG_SZ;
2400                 *needed = 2*(strlen(string)+1);
2401                 if((*data  = (uint8 *)TALLOC(ctx, (*needed > in_size) ? *needed:in_size )) == NULL)
2402                         return WERR_NOMEM;
2403                 memset(*data, 0, (*needed > in_size) ? *needed:in_size);
2404                 for (i=0; i<strlen(string); i++) {
2405                         (*data)[2*i]=string[i];
2406                         (*data)[2*i+1]='\0';
2407                 }
2408                 return WERR_OK;
2409         }
2410
2411         if (!StrCaseCmp(value, "DsPresent")) {
2412                 *type = REG_DWORD;
2413                 if ( !(*data = TALLOC_ARRAY(ctx, uint8, sizeof(uint32) )) )
2414                         return WERR_NOMEM;
2415
2416                 /* only show the publish check box if we are a
2417                    memeber of a AD domain */
2418
2419                 if ( lp_security() == SEC_ADS )
2420                         SIVAL(*data, 0, 0x01);
2421                 else
2422                         SIVAL(*data, 0, 0x00);
2423
2424                 *needed = 0x4;
2425                 return WERR_OK;
2426         }
2427
2428         if (!StrCaseCmp(value, "DNSMachineName")) {
2429                 const char *hostname = get_mydnsfullname();
2430
2431                 if (!hostname)
2432                         return WERR_BADFILE;
2433                 *type = REG_SZ;
2434                 *needed = 2*(strlen(hostname)+1);
2435                 if((*data  = (uint8 *)TALLOC(ctx, (*needed > in_size) ? *needed:in_size )) == NULL)
2436                         return WERR_NOMEM;
2437                 memset(*data, 0, (*needed > in_size) ? *needed:in_size);
2438                 for (i=0; i<strlen(hostname); i++) {
2439                         (*data)[2*i]=hostname[i];
2440                         (*data)[2*i+1]='\0';
2441                 }
2442                 return WERR_OK;
2443         }
2444
2445
2446         return WERR_BADFILE;
2447 }
2448
2449 /********************************************************************
2450  * spoolss_getprinterdata
2451  ********************************************************************/
2452
2453 WERROR _spoolss_getprinterdata(pipes_struct *p, SPOOL_Q_GETPRINTERDATA *q_u, SPOOL_R_GETPRINTERDATA *r_u)
2454 {
2455         POLICY_HND      *handle = &q_u->handle;
2456         UNISTR2         *valuename = &q_u->valuename;
2457         uint32          in_size = q_u->size;
2458         uint32          *type = &r_u->type;
2459         uint32          *out_size = &r_u->size;
2460         uint8           **data = &r_u->data;
2461         uint32          *needed = &r_u->needed;
2462         WERROR          status;
2463         fstring         value;
2464         Printer_entry   *Printer = find_printer_index_by_hnd(p, handle);
2465         NT_PRINTER_INFO_LEVEL   *printer = NULL;
2466         int             snum = 0;
2467
2468         /*
2469          * Reminder: when it's a string, the length is in BYTES
2470          * even if UNICODE is negociated.
2471          *
2472          * JFM, 4/19/1999
2473          */
2474
2475         *out_size = in_size;
2476
2477         /* in case of problem, return some default values */
2478
2479         *needed = 0;
2480         *type   = 0;
2481
2482         DEBUG(4,("_spoolss_getprinterdata\n"));
2483
2484         if ( !Printer ) {
2485                 DEBUG(2,("_spoolss_getprinterdata: Invalid handle (%s:%u:%u).\n", OUR_HANDLE(handle)));
2486                 status = WERR_BADFID;
2487                 goto done;
2488         }
2489
2490         unistr2_to_ascii(value, valuename, sizeof(value));
2491
2492         if ( Printer->printer_type == SPLHND_SERVER )
2493                 status = getprinterdata_printer_server( p->mem_ctx, value, type, data, needed, *out_size );
2494         else
2495         {
2496                 if ( !get_printer_snum(p,handle, &snum, NULL) ) {
2497                         status = WERR_BADFID;
2498                         goto done;
2499                 }
2500
2501                 status = get_a_printer(Printer, &printer, 2, lp_servicename(snum));
2502                 if ( !W_ERROR_IS_OK(status) )
2503                         goto done;
2504
2505                 /* XP sends this and wants to change id value from the PRINTER_INFO_0 */
2506
2507                 if ( strequal(value, "ChangeId") ) {
2508                         *type = REG_DWORD;
2509                         *needed = sizeof(uint32);
2510                         if ( (*data = (uint8*)TALLOC(p->mem_ctx, sizeof(uint32))) == NULL) {
2511                                 status = WERR_NOMEM;
2512                                 goto done;
2513                         }
2514                         SIVAL( *data, 0, printer->info_2->changeid );
2515                         status = WERR_OK;
2516                 }
2517                 else
2518                         status = get_printer_dataex( p->mem_ctx, printer, SPOOL_PRINTERDATA_KEY, value, type, data, needed, *out_size );
2519         }
2520
2521         if (*needed > *out_size)
2522                 status = WERR_MORE_DATA;
2523
2524 done:
2525         if ( !W_ERROR_IS_OK(status) )
2526         {
2527                 DEBUG(5, ("error %d: allocating %d\n", W_ERROR_V(status),*out_size));
2528
2529                 /* reply this param doesn't exist */
2530
2531                 if ( *out_size ) {
2532                         if((*data=(uint8 *)TALLOC_ZERO_ARRAY(p->mem_ctx, uint8, *out_size)) == NULL) {
2533                                 if ( printer )
2534                                         free_a_printer( &printer, 2 );
2535                                 return WERR_NOMEM;
2536                         }
2537                 } else {
2538                         *data = NULL;
2539                 }
2540         }
2541
2542         /* cleanup & exit */
2543
2544         if ( printer )
2545                 free_a_printer( &printer, 2 );
2546
2547         return status;
2548 }
2549
2550 /*********************************************************
2551  Connect to the client machine.
2552 **********************************************************/
2553
2554 static bool spoolss_connect_to_client(struct rpc_pipe_client **pp_pipe,
2555                         struct sockaddr_storage *client_ss, const char *remote_machine)
2556 {
2557         NTSTATUS ret;
2558         struct cli_state *the_cli;
2559         struct sockaddr_storage rm_addr;
2560
2561         if ( is_zero_addr(client_ss) ) {
2562                 if ( !resolve_name( remote_machine, &rm_addr, 0x20) ) {
2563                         DEBUG(2,("spoolss_connect_to_client: Can't resolve address for %s\n", remote_machine));
2564                         return False;
2565                 }
2566
2567                 if (ismyaddr(&rm_addr)) {
2568                         DEBUG(0,("spoolss_connect_to_client: Machine %s is one of our addresses. Cannot add to ourselves.\n", remote_machine));
2569                         return False;
2570                 }
2571         } else {
2572                 char addr[INET6_ADDRSTRLEN];
2573                 rm_addr = *client_ss;
2574                 print_sockaddr(addr, sizeof(addr), &rm_addr);
2575                 DEBUG(5,("spoolss_connect_to_client: Using address %s (no name resolution necessary)\n",
2576                         addr));
2577         }
2578
2579         /* setup the connection */
2580
2581         ret = cli_full_connection( &the_cli, global_myname(), remote_machine,
2582                 &rm_addr, 0, "IPC$", "IPC",
2583                 "", /* username */
2584                 "", /* domain */
2585                 "", /* password */
2586                 0, lp_client_signing(), NULL );
2587
2588         if ( !NT_STATUS_IS_OK( ret ) ) {
2589                 DEBUG(2,("spoolss_connect_to_client: connection to [%s] failed!\n",
2590                         remote_machine ));
2591                 return False;
2592         }
2593
2594         if ( the_cli->protocol != PROTOCOL_NT1 ) {
2595                 DEBUG(0,("spoolss_connect_to_client: machine %s didn't negotiate NT protocol.\n", remote_machine));
2596                 cli_shutdown(the_cli);
2597                 return False;
2598         }
2599
2600         /*
2601          * Ok - we have an anonymous connection to the IPC$ share.
2602          * Now start the NT Domain stuff :-).
2603          */
2604
2605         if ( !(*pp_pipe = cli_rpc_pipe_open_noauth(the_cli, PI_SPOOLSS, &ret)) ) {
2606                 DEBUG(2,("spoolss_connect_to_client: unable to open the spoolss pipe on machine %s. Error was : %s.\n",
2607                         remote_machine, nt_errstr(ret)));
2608                 cli_shutdown(the_cli);
2609                 return False;
2610         }
2611
2612         return True;
2613 }
2614
2615 /***************************************************************************
2616  Connect to the client.
2617 ****************************************************************************/
2618
2619 static bool srv_spoolss_replyopenprinter(int snum, const char *printer,
2620                                         uint32 localprinter, uint32 type,
2621                                         POLICY_HND *handle, struct sockaddr_storage *client_ss)
2622 {
2623         WERROR result;
2624
2625         /*
2626          * If it's the first connection, contact the client
2627          * and connect to the IPC$ share anonymously
2628          */
2629         if (smb_connections==0) {
2630                 fstring unix_printer;
2631
2632                 fstrcpy(unix_printer, printer+2); /* the +2 is to strip the leading 2 backslashs */
2633
2634                 if ( !spoolss_connect_to_client( &notify_cli_pipe, client_ss, unix_printer ))
2635                         return False;
2636
2637                 messaging_register(smbd_messaging_context(), NULL,
2638                                    MSG_PRINTER_NOTIFY2,
2639                                    receive_notify2_message_list);
2640                 /* Tell the connections db we're now interested in printer
2641                  * notify messages. */
2642                 register_message_flags( True, FLAG_MSG_PRINT_NOTIFY );
2643         }
2644
2645         /*
2646          * Tell the specific printing tdb we want messages for this printer
2647          * by registering our PID.
2648          */
2649
2650         if (!print_notify_register_pid(snum))
2651                 DEBUG(0,("print_notify_register_pid: Failed to register our pid for printer %s\n", printer ));
2652
2653         smb_connections++;
2654
2655         result = rpccli_spoolss_reply_open_printer(notify_cli_pipe,
2656                         talloc_tos(),
2657                         printer,
2658                         localprinter,
2659                         type,
2660                         handle);
2661
2662         if (!W_ERROR_IS_OK(result))
2663                 DEBUG(5,("srv_spoolss_reply_open_printer: Client RPC returned [%s]\n",
2664                         dos_errstr(result)));
2665
2666         return (W_ERROR_IS_OK(result));
2667 }
2668
2669 /********************************************************************
2670  * _spoolss_rffpcnex
2671  * ReplyFindFirstPrinterChangeNotifyEx
2672  *
2673  * before replying OK: status=0 a rpc call is made to the workstation
2674  * asking ReplyOpenPrinter
2675  *
2676  * in fact ReplyOpenPrinter is the changenotify equivalent on the spoolss pipe
2677  * called from api_spoolss_rffpcnex
2678  ********************************************************************/
2679
2680 WERROR _spoolss_rffpcnex(pipes_struct *p, SPOOL_Q_RFFPCNEX *q_u, SPOOL_R_RFFPCNEX *r_u)
2681 {
2682         POLICY_HND *handle = &q_u->handle;
2683         uint32 flags = q_u->flags;
2684         uint32 options = q_u->options;
2685         UNISTR2 *localmachine = &q_u->localmachine;
2686         uint32 printerlocal = q_u->printerlocal;
2687         int snum = -1;
2688         SPOOL_NOTIFY_OPTION *option = q_u->option;
2689         struct sockaddr_storage client_ss;
2690
2691         /* store the notify value in the printer struct */
2692
2693         Printer_entry *Printer=find_printer_index_by_hnd(p, handle);
2694
2695         if (!Printer) {
2696                 DEBUG(2,("_spoolss_rffpcnex: Invalid handle (%s:%u:%u).\n", OUR_HANDLE(handle)));
2697                 return WERR_BADFID;
2698         }
2699
2700         Printer->notify.flags=flags;
2701         Printer->notify.options=options;
2702         Printer->notify.printerlocal=printerlocal;
2703
2704         if (Printer->notify.option)
2705                 free_spool_notify_option(&Printer->notify.option);
2706
2707         Printer->notify.option=dup_spool_notify_option(option);
2708
2709         unistr2_to_ascii(Printer->notify.localmachine, localmachine,
2710                        sizeof(Printer->notify.localmachine));
2711
2712         /* Connect to the client machine and send a ReplyOpenPrinter */
2713
2714         if ( Printer->printer_type == SPLHND_SERVER)
2715                 snum = -1;
2716         else if ( (Printer->printer_type == SPLHND_PRINTER) &&
2717                         !get_printer_snum(p, handle, &snum, NULL) )
2718                 return WERR_BADFID;
2719
2720         if (!interpret_string_addr(&client_ss,
2721                                 p->conn->client_address,
2722                                 AI_NUMERICHOST)) {
2723                 return WERR_SERVER_UNAVAILABLE;
2724         }
2725
2726         if(!srv_spoolss_replyopenprinter(snum, Printer->notify.localmachine,
2727                                         Printer->notify.printerlocal, 1,
2728                                         &Printer->notify.client_hnd, &client_ss))
2729                 return WERR_SERVER_UNAVAILABLE;
2730
2731         Printer->notify.client_connected=True;
2732
2733         return WERR_OK;
2734 }
2735
2736 /*******************************************************************
2737  * fill a notify_info_data with the servername
2738  ********************************************************************/
2739
2740 void spoolss_notify_server_name(int snum,
2741                                        SPOOL_NOTIFY_INFO_DATA *data,
2742                                        print_queue_struct *queue,
2743                                        NT_PRINTER_INFO_LEVEL *printer,
2744                                        TALLOC_CTX *mem_ctx)
2745 {
2746         smb_ucs2_t *temp = NULL;
2747         uint32 len;
2748
2749         len = rpcstr_push_talloc(mem_ctx, &temp, printer->info_2->servername);
2750         if (len == (uint32)-1) {
2751                 len = 0;
2752         }
2753
2754         data->notify_data.data.length = len;
2755         if (len) {
2756                 data->notify_data.data.string = (uint16 *)temp;
2757         } else {
2758                 data->notify_data.data.string = NULL;
2759         }
2760 }
2761
2762 /*******************************************************************
2763  * fill a notify_info_data with the printername (not including the servername).
2764  ********************************************************************/
2765
2766 void spoolss_notify_printer_name(int snum,
2767                                         SPOOL_NOTIFY_INFO_DATA *data,
2768                                         print_queue_struct *queue,
2769                                         NT_PRINTER_INFO_LEVEL *printer,
2770                                         TALLOC_CTX *mem_ctx)
2771 {
2772         smb_ucs2_t *temp = NULL;
2773         uint32 len;
2774
2775         /* the notify name should not contain the \\server\ part */
2776         char *p = strrchr(printer->info_2->printername, '\\');
2777
2778         if (!p) {
2779                 p = printer->info_2->printername;
2780         } else {
2781                 p++;
2782         }
2783
2784         len = rpcstr_push_talloc(mem_ctx, &temp, p);
2785         if (len == (uint32)-1) {
2786                 len = 0;
2787         }
2788
2789         data->notify_data.data.length = len;
2790         if (len) {
2791                 data->notify_data.data.string = (uint16 *)temp;
2792         } else {
2793                 data->notify_data.data.string = NULL;
2794         }
2795 }
2796
2797 /*******************************************************************
2798  * fill a notify_info_data with the servicename
2799  ********************************************************************/
2800
2801 void spoolss_notify_share_name(int snum,
2802                                       SPOOL_NOTIFY_INFO_DATA *data,
2803                                       print_queue_struct *queue,
2804                                       NT_PRINTER_INFO_LEVEL *printer,
2805                                       TALLOC_CTX *mem_ctx)
2806 {
2807         smb_ucs2_t *temp = NULL;
2808         uint32 len;
2809
2810         len = rpcstr_push_talloc(mem_ctx, &temp, lp_servicename(snum));
2811         if (len == (uint32)-1) {
2812                 len = 0;
2813         }
2814
2815         data->notify_data.data.length = len;
2816         if (len) {
2817                 data->notify_data.data.string = (uint16 *)temp;
2818         } else {
2819                 data->notify_data.data.string = NULL;
2820         }
2821
2822 }
2823
2824 /*******************************************************************
2825  * fill a notify_info_data with the port name
2826  ********************************************************************/
2827
2828 void spoolss_notify_port_name(int snum,
2829                                      SPOOL_NOTIFY_INFO_DATA *data,
2830                                      print_queue_struct *queue,
2831                                      NT_PRINTER_INFO_LEVEL *printer,
2832                                      TALLOC_CTX *mem_ctx)
2833 {
2834         smb_ucs2_t *temp = NULL;
2835         uint32 len;
2836
2837         /* even if it's strange, that's consistant in all the code */
2838
2839         len = rpcstr_push_talloc(mem_ctx, &temp, printer->info_2->portname);
2840         if (len == (uint32)-1) {
2841                 len = 0;
2842         }
2843
2844         data->notify_data.data.length = len;
2845         if (len) {
2846                 data->notify_data.data.string = (uint16 *)temp;
2847         } else {
2848                 data->notify_data.data.string = NULL;
2849         }
2850 }
2851
2852 /*******************************************************************
2853  * fill a notify_info_data with the printername
2854  * but it doesn't exist, have to see what to do
2855  ********************************************************************/
2856
2857 void spoolss_notify_driver_name(int snum,
2858                                        SPOOL_NOTIFY_INFO_DATA *data,
2859                                        print_queue_struct *queue,
2860                                        NT_PRINTER_INFO_LEVEL *printer,
2861                                        TALLOC_CTX *mem_ctx)
2862 {
2863         smb_ucs2_t *temp = NULL;
2864         uint32 len;
2865
2866         len = rpcstr_push_talloc(mem_ctx, &temp, printer->info_2->drivername);
2867         if (len == (uint32)-1) {
2868                 len = 0;
2869         }
2870
2871         data->notify_data.data.length = len;
2872         if (len) {
2873                 data->notify_data.data.string = (uint16 *)temp;
2874         } else {
2875                 data->notify_data.data.string = NULL;
2876         }
2877 }
2878
2879 /*******************************************************************
2880  * fill a notify_info_data with the comment
2881  ********************************************************************/
2882
2883 void spoolss_notify_comment(int snum,
2884                                    SPOOL_NOTIFY_INFO_DATA *data,
2885                                    print_queue_struct *queue,
2886                                    NT_PRINTER_INFO_LEVEL *printer,
2887                                    TALLOC_CTX *mem_ctx)
2888 {
2889         smb_ucs2_t *temp = NULL;
2890         uint32 len;
2891
2892         if (*printer->info_2->comment == '\0')
2893                 len = rpcstr_push_talloc(mem_ctx, &temp, lp_comment(snum));
2894         else
2895                 len = rpcstr_push_talloc(mem_ctx, &temp, printer->info_2->comment);
2896
2897         if (len == (uint32)-1) {
2898                 len = 0;
2899         }
2900         data->notify_data.data.length = len;
2901         if (len) {
2902                 data->notify_data.data.string = (uint16 *)temp;
2903         } else {
2904                 data->notify_data.data.string = NULL;
2905         }
2906 }
2907
2908 /*******************************************************************
2909  * fill a notify_info_data with the comment
2910  * location = "Room 1, floor 2, building 3"
2911  ********************************************************************/
2912
2913 void spoolss_notify_location(int snum,
2914                                     SPOOL_NOTIFY_INFO_DATA *data,
2915                                     print_queue_struct *queue,
2916                                     NT_PRINTER_INFO_LEVEL *printer,
2917                                     TALLOC_CTX *mem_ctx)
2918 {
2919         smb_ucs2_t *temp = NULL;
2920         uint32 len;
2921
2922         len = rpcstr_push_talloc(mem_ctx, &temp, printer->info_2->location);
2923         if (len == (uint32)-1) {
2924                 len = 0;
2925         }
2926
2927         data->notify_data.data.length = len;
2928         if (len) {
2929                 data->notify_data.data.string = (uint16 *)temp;
2930         } else {
2931                 data->notify_data.data.string = NULL;
2932         }
2933 }
2934
2935 /*******************************************************************
2936  * fill a notify_info_data with the device mode
2937  * jfm:xxxx don't to it for know but that's a real problem !!!
2938  ********************************************************************/
2939
2940 static void spoolss_notify_devmode(int snum,
2941                                    SPOOL_NOTIFY_INFO_DATA *data,
2942                                    print_queue_struct *queue,
2943                                    NT_PRINTER_INFO_LEVEL *printer,
2944                                    TALLOC_CTX *mem_ctx)
2945 {
2946         /* for a dummy implementation we have to zero the fields */
2947         data->notify_data.data.length = 0;
2948         data->notify_data.data.string = NULL;
2949 }
2950
2951 /*******************************************************************
2952  * fill a notify_info_data with the separator file name
2953  ********************************************************************/
2954
2955 void spoolss_notify_sepfile(int snum,
2956                                    SPOOL_NOTIFY_INFO_DATA *data,
2957                                    print_queue_struct *queue,
2958                                    NT_PRINTER_INFO_LEVEL *printer,
2959                                    TALLOC_CTX *mem_ctx)
2960 {
2961         smb_ucs2_t *temp = NULL;
2962         uint32 len;
2963
2964         len = rpcstr_push_talloc(mem_ctx, &temp, printer->info_2->sepfile);
2965         if (len == (uint32)-1) {
2966                 len = 0;
2967         }
2968
2969         data->notify_data.data.length = len;
2970         if (len) {
2971                 data->notify_data.data.string = (uint16 *)temp;
2972         } else {
2973                 data->notify_data.data.string = NULL;
2974         }
2975 }
2976
2977 /*******************************************************************
2978  * fill a notify_info_data with the print processor
2979  * jfm:xxxx return always winprint to indicate we don't do anything to it
2980  ********************************************************************/
2981
2982 void spoolss_notify_print_processor(int snum,
2983                                            SPOOL_NOTIFY_INFO_DATA *data,
2984                                            print_queue_struct *queue,
2985                                            NT_PRINTER_INFO_LEVEL *printer,
2986                                            TALLOC_CTX *mem_ctx)
2987 {
2988         smb_ucs2_t *temp = NULL;
2989         uint32 len;
2990
2991         len = rpcstr_push_talloc(mem_ctx, &temp, printer->info_2->printprocessor);
2992         if (len == (uint32)-1) {
2993                 len = 0;
2994         }
2995
2996         data->notify_data.data.length = len;
2997         if (len) {
2998                 data->notify_data.data.string = (uint16 *)temp;
2999         } else {
3000                 data->notify_data.data.string = NULL;
3001         }
3002 }
3003
3004 /*******************************************************************
3005  * fill a notify_info_data with the print processor options
3006  * jfm:xxxx send an empty string
3007  ********************************************************************/
3008
3009 void spoolss_notify_parameters(int snum,
3010                                       SPOOL_NOTIFY_INFO_DATA *data,
3011                                       print_queue_struct *queue,
3012                                       NT_PRINTER_INFO_LEVEL *printer,
3013                                       TALLOC_CTX *mem_ctx)
3014 {
3015         smb_ucs2_t *temp = NULL;
3016         uint32 len;
3017
3018         len = rpcstr_push_talloc(mem_ctx, &temp, printer->info_2->parameters);
3019         if (len == (uint32)-1) {
3020                 len = 0;
3021         }
3022
3023         data->notify_data.data.length = len;
3024         if (len) {
3025                 data->notify_data.data.string = (uint16 *)temp;
3026         } else {
3027                 data->notify_data.data.string = NULL;
3028         }
3029 }
3030
3031 /*******************************************************************
3032  * fill a notify_info_data with the data type
3033  * jfm:xxxx always send RAW as data type
3034  ********************************************************************/
3035
3036 void spoolss_notify_datatype(int snum,
3037                                     SPOOL_NOTIFY_INFO_DATA *data,
3038                                     print_queue_struct *queue,
3039                                     NT_PRINTER_INFO_LEVEL *printer,
3040                                     TALLOC_CTX *mem_ctx)
3041 {
3042         smb_ucs2_t *temp = NULL;
3043         uint32 len;
3044
3045         len = rpcstr_push_talloc(mem_ctx, &temp, printer->info_2->datatype);
3046         if (len == (uint32)-1) {
3047                 len = 0;
3048         }
3049
3050         data->notify_data.data.length = len;
3051         if (len) {
3052                 data->notify_data.data.string = (uint16 *)temp;
3053         } else {
3054                 data->notify_data.data.string = NULL;
3055         }
3056 }
3057
3058 /*******************************************************************
3059  * fill a notify_info_data with the security descriptor
3060  * jfm:xxxx send an null pointer to say no security desc
3061  * have to implement security before !
3062  ********************************************************************/
3063
3064 static void spoolss_notify_security_desc(int snum,
3065                                          SPOOL_NOTIFY_INFO_DATA *data,
3066                                          print_queue_struct *queue,
3067                                          NT_PRINTER_INFO_LEVEL *printer,
3068                                          TALLOC_CTX *mem_ctx)
3069 {
3070         data->notify_data.sd.size = printer->info_2->secdesc_buf->sd_size;
3071         data->notify_data.sd.desc = dup_sec_desc( mem_ctx, printer->info_2->secdesc_buf->sd ) ;
3072 }
3073
3074 /*******************************************************************
3075  * fill a notify_info_data with the attributes
3076  * jfm:xxxx a samba printer is always shared
3077  ********************************************************************/
3078
3079 void spoolss_notify_attributes(int snum,
3080                                       SPOOL_NOTIFY_INFO_DATA *data,
3081                                       print_queue_struct *queue,
3082                                       NT_PRINTER_INFO_LEVEL *printer,
3083                                       TALLOC_CTX *mem_ctx)
3084 {
3085         data->notify_data.value[0] = printer->info_2->attributes;
3086         data->notify_data.value[1] = 0;
3087 }
3088
3089 /*******************************************************************
3090  * fill a notify_info_data with the priority
3091  ********************************************************************/
3092
3093 static void spoolss_notify_priority(int snum,
3094                                     SPOOL_NOTIFY_INFO_DATA *data,
3095                                     print_queue_struct *queue,
3096                                     NT_PRINTER_INFO_LEVEL *printer,
3097                                     TALLOC_CTX *mem_ctx)
3098 {
3099         data->notify_data.value[0] = printer->info_2->priority;
3100         data->notify_data.value[1] = 0;
3101 }
3102
3103 /*******************************************************************
3104  * fill a notify_info_data with the default priority
3105  ********************************************************************/
3106
3107 static void spoolss_notify_default_priority(int snum,
3108                                             SPOOL_NOTIFY_INFO_DATA *data,
3109                                             print_queue_struct *queue,
3110                                             NT_PRINTER_INFO_LEVEL *printer,
3111                                             TALLOC_CTX *mem_ctx)
3112 {
3113         data->notify_data.value[0] = printer->info_2->default_priority;
3114         data->notify_data.value[1] = 0;
3115 }
3116
3117 /*******************************************************************
3118  * fill a notify_info_data with the start time
3119  ********************************************************************/
3120
3121 static void spoolss_notify_start_time(int snum,
3122                                       SPOOL_NOTIFY_INFO_DATA *data,
3123                                       print_queue_struct *queue,
3124                                       NT_PRINTER_INFO_LEVEL *printer,
3125                                       TALLOC_CTX *mem_ctx)
3126 {
3127         data->notify_data.value[0] = printer->info_2->starttime;
3128         data->notify_data.value[1] = 0;
3129 }
3130
3131 /*******************************************************************
3132  * fill a notify_info_data with the until time
3133  ********************************************************************/
3134
3135 static void spoolss_notify_until_time(int snum,
3136                                       SPOOL_NOTIFY_INFO_DATA *data,
3137                                       print_queue_struct *queue,
3138                                       NT_PRINTER_INFO_LEVEL *printer,
3139                                       TALLOC_CTX *mem_ctx)
3140 {
3141         data->notify_data.value[0] = printer->info_2->untiltime;
3142         data->notify_data.value[1] = 0;
3143 }
3144
3145 /*******************************************************************
3146  * fill a notify_info_data with the status
3147  ********************************************************************/
3148
3149 static void spoolss_notify_status(int snum,
3150                                   SPOOL_NOTIFY_INFO_DATA *data,
3151                                   print_queue_struct *queue,
3152                                   NT_PRINTER_INFO_LEVEL *printer,
3153                                   TALLOC_CTX *mem_ctx)
3154 {
3155         print_status_struct status;
3156
3157         print_queue_length(snum, &status);
3158         data->notify_data.value[0]=(uint32) status.status;
3159         data->notify_data.value[1] = 0;
3160 }
3161
3162 /*******************************************************************
3163  * fill a notify_info_data with the number of jobs queued
3164  ********************************************************************/
3165
3166 void spoolss_notify_cjobs(int snum,
3167                                  SPOOL_NOTIFY_INFO_DATA *data,
3168                                  print_queue_struct *queue,
3169                                  NT_PRINTER_INFO_LEVEL *printer,
3170                                  TALLOC_CTX *mem_ctx)
3171 {
3172         data->notify_data.value[0] = print_queue_length(snum, NULL);
3173         data->notify_data.value[1] = 0;
3174 }
3175
3176 /*******************************************************************
3177  * fill a notify_info_data with the average ppm
3178  ********************************************************************/
3179
3180 static void spoolss_notify_average_ppm(int snum,
3181                                        SPOOL_NOTIFY_INFO_DATA *data,
3182                                        print_queue_struct *queue,
3183                                        NT_PRINTER_INFO_LEVEL *printer,
3184                                        TALLOC_CTX *mem_ctx)
3185 {
3186         /* always respond 8 pages per minutes */
3187         /* a little hard ! */
3188         data->notify_data.value[0] = printer->info_2->averageppm;
3189         data->notify_data.value[1] = 0;
3190 }
3191
3192 /*******************************************************************
3193  * fill a notify_info_data with username
3194  ********************************************************************/
3195
3196 static void spoolss_notify_username(int snum,
3197                                     SPOOL_NOTIFY_INFO_DATA *data,
3198                                     print_queue_struct *queue,
3199                                     NT_PRINTER_INFO_LEVEL *printer,
3200                                     TALLOC_CTX *mem_ctx)
3201 {
3202         smb_ucs2_t *temp = NULL;
3203         uint32 len;
3204
3205         len = rpcstr_push_talloc(mem_ctx, &temp, queue->fs_user);
3206         if (len == (uint32)-1) {
3207                 len = 0;
3208         }
3209
3210         data->notify_data.data.length = len;
3211         if (len) {
3212                 data->notify_data.data.string = (uint16 *)temp;
3213         } else {
3214                 data->notify_data.data.string = NULL;
3215         }
3216 }
3217
3218 /*******************************************************************
3219  * fill a notify_info_data with job status
3220  ********************************************************************/
3221
3222 static void spoolss_notify_job_status(int snum,
3223                                       SPOOL_NOTIFY_INFO_DATA *data,
3224                                       print_queue_struct *queue,
3225                                       NT_PRINTER_INFO_LEVEL *printer,
3226                                       TALLOC_CTX *mem_ctx)
3227 {
3228         data->notify_data.value[0]=nt_printj_status(queue->status);
3229         data->notify_data.value[1] = 0;
3230 }
3231
3232 /*******************************************************************
3233  * fill a notify_info_data with job name
3234  ********************************************************************/
3235
3236 static void spoolss_notify_job_name(int snum,
3237                                     SPOOL_NOTIFY_INFO_DATA *data,
3238                                     print_queue_struct *queue,
3239                                     NT_PRINTER_INFO_LEVEL *printer,
3240                                     TALLOC_CTX *mem_ctx)
3241 {
3242         smb_ucs2_t *temp = NULL;
3243         uint32 len;
3244
3245         len = rpcstr_push_talloc(mem_ctx, &temp, queue->fs_file);
3246         if (len == (uint32)-1) {
3247                 len = 0;
3248         }
3249
3250         data->notify_data.data.length = len;
3251         if (len) {
3252                 data->notify_data.data.string = (uint16 *)temp;
3253         } else {
3254                 data->notify_data.data.string = NULL;
3255         }
3256 }
3257
3258 /*******************************************************************
3259  * fill a notify_info_data with job status
3260  ********************************************************************/
3261
3262 static void spoolss_notify_job_status_string(int snum,
3263                                              SPOOL_NOTIFY_INFO_DATA *data,
3264                                              print_queue_struct *queue,
3265                                              NT_PRINTER_INFO_LEVEL *printer,
3266                                              TALLOC_CTX *mem_ctx)
3267 {
3268         /*
3269          * Now we're returning job status codes we just return a "" here. JRA.
3270          */
3271
3272         const char *p = "";
3273         smb_ucs2_t *temp = NULL;
3274         uint32 len;
3275
3276 #if 0 /* NO LONGER NEEDED - JRA. 02/22/2001 */
3277         p = "unknown";
3278
3279         switch (queue->status) {
3280         case LPQ_QUEUED:
3281                 p = "Queued";
3282                 break;
3283         case LPQ_PAUSED:
3284                 p = "";    /* NT provides the paused string */
3285                 break;
3286         case LPQ_SPOOLING:
3287                 p = "Spooling";
3288                 break;
3289         case LPQ_PRINTING:
3290                 p = "Printing";
3291                 break;
3292         }
3293 #endif /* NO LONGER NEEDED. */
3294
3295         len = rpcstr_push_talloc(mem_ctx, &temp, p);
3296         if (len == (uint32)-1) {
3297                 len = 0;
3298         }
3299
3300         data->notify_data.data.length = len;
3301         if (len) {
3302                 data->notify_data.data.string = (uint16 *)temp;
3303         } else {
3304                 data->notify_data.data.string = NULL;
3305         }
3306 }
3307
3308 /*******************************************************************
3309  * fill a notify_info_data with job time