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