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