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