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