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