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