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