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