Fix bug 7063 - Samba 3.4.5 on ubuntu 8.04 64 bit - Core dumps.
[samba.git] / source3 / rpc_server / srv_spoolss_nt.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  RPC Pipe client / server routines
4  *  Copyright (C) Andrew Tridgell              1992-2000,
5  *  Copyright (C) Luke Kenneth Casson Leighton 1996-2000,
6  *  Copyright (C) Jean François Micouleau      1998-2000,
7  *  Copyright (C) Jeremy Allison               2001-2002,
8  *  Copyright (C) Gerald Carter                2000-2004,
9  *  Copyright (C) Tim Potter                   2001-2002.
10  *  Copyright (C) Guenther Deschner                 2009.
11  *
12  *  This program is free software; you can redistribute it and/or modify
13  *  it under the terms of the GNU General Public License as published by
14  *  the Free Software Foundation; either version 3 of the License, or
15  *  (at your option) any later version.
16  *
17  *  This program is distributed in the hope that it will be useful,
18  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *  GNU General Public License for more details.
21  *
22  *  You should have received a copy of the GNU General Public License
23  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
24  */
25
26 /* Since the SPOOLSS rpc routines are basically DOS 16-bit calls wrapped
27    up, all the errors returned are DOS errors, not NT status codes. */
28
29 #include "includes.h"
30
31 /* macros stolen from s4 spoolss server */
32 #define SPOOLSS_BUFFER_UNION(fn,ic,info,level) \
33         ((info)?ndr_size_##fn(info, level, ic, 0):0)
34
35 #define SPOOLSS_BUFFER_UNION_ARRAY(mem_ctx,fn,ic,info,level,count) \
36         ((info)?ndr_size_##fn##_info(mem_ctx, ic, level, count, info):0)
37
38 #define SPOOLSS_BUFFER_ARRAY(mem_ctx,fn,ic,info,count) \
39         ((info)?ndr_size_##fn##_info(mem_ctx, ic, count, info):0)
40
41 #define SPOOLSS_BUFFER_OK(val_true,val_false) ((r->in.offered >= *r->out.needed)?val_true:val_false)
42
43
44 extern userdom_struct current_user_info;
45
46 #undef DBGC_CLASS
47 #define DBGC_CLASS DBGC_RPC_SRV
48
49 #ifndef MAX_OPEN_PRINTER_EXS
50 #define MAX_OPEN_PRINTER_EXS 50
51 #endif
52
53 #define MAGIC_DISPLAY_FREQUENCY 0xfade2bad
54 #define PHANTOM_DEVMODE_KEY "_p_f_a_n_t_0_m_"
55
56 static Printer_entry *printers_list;
57
58 typedef struct _counter_printer_0 {
59         struct _counter_printer_0 *next;
60         struct _counter_printer_0 *prev;
61
62         int snum;
63         uint32_t counter;
64 } counter_printer_0;
65
66 static counter_printer_0 *counter_list;
67
68 static struct rpc_pipe_client *notify_cli_pipe; /* print notify back-channel pipe handle*/
69 static uint32_t smb_connections = 0;
70
71
72 /* in printing/nt_printing.c */
73
74 extern struct standard_mapping printer_std_mapping, printserver_std_mapping;
75
76 /* API table for Xcv Monitor functions */
77
78 struct xcv_api_table {
79         const char *name;
80         WERROR(*fn) (TALLOC_CTX *mem_ctx, NT_USER_TOKEN *token, DATA_BLOB *in, DATA_BLOB *out, uint32_t *needed);
81 };
82
83 /********************************************************************
84  * Canonicalize servername.
85  ********************************************************************/
86
87 static const char *canon_servername(const char *servername)
88 {
89         const char *pservername = servername;
90         while (*pservername == '\\') {
91                 pservername++;
92         }
93         return pservername;
94 }
95
96 /* translate between internal status numbers and NT status numbers */
97 static int nt_printj_status(int v)
98 {
99         switch (v) {
100         case LPQ_QUEUED:
101                 return 0;
102         case LPQ_PAUSED:
103                 return JOB_STATUS_PAUSED;
104         case LPQ_SPOOLING:
105                 return JOB_STATUS_SPOOLING;
106         case LPQ_PRINTING:
107                 return JOB_STATUS_PRINTING;
108         case LPQ_ERROR:
109                 return JOB_STATUS_ERROR;
110         case LPQ_DELETING:
111                 return JOB_STATUS_DELETING;
112         case LPQ_OFFLINE:
113                 return JOB_STATUS_OFFLINE;
114         case LPQ_PAPEROUT:
115                 return JOB_STATUS_PAPEROUT;
116         case LPQ_PRINTED:
117                 return JOB_STATUS_PRINTED;
118         case LPQ_DELETED:
119                 return JOB_STATUS_DELETED;
120         case LPQ_BLOCKED:
121                 return JOB_STATUS_BLOCKED_DEVQ;
122         case LPQ_USER_INTERVENTION:
123                 return JOB_STATUS_USER_INTERVENTION;
124         }
125         return 0;
126 }
127
128 static int nt_printq_status(int v)
129 {
130         switch (v) {
131         case LPQ_PAUSED:
132                 return PRINTER_STATUS_PAUSED;
133         case LPQ_QUEUED:
134         case LPQ_SPOOLING:
135         case LPQ_PRINTING:
136                 return 0;
137         }
138         return 0;
139 }
140
141 /***************************************************************************
142  Disconnect from the client
143 ****************************************************************************/
144
145 static void srv_spoolss_replycloseprinter(int snum, struct policy_handle *handle)
146 {
147         WERROR result;
148         NTSTATUS status;
149
150         /*
151          * Tell the specific printing tdb we no longer want messages for this printer
152          * by deregistering our PID.
153          */
154
155         if (!print_notify_deregister_pid(snum))
156                 DEBUG(0,("print_notify_register_pid: Failed to register our pid for printer %s\n", lp_const_servicename(snum) ));
157
158         /* weird if the test succeds !!! */
159         if (smb_connections==0) {
160                 DEBUG(0,("srv_spoolss_replycloseprinter:Trying to close non-existant notify backchannel !\n"));
161                 return;
162         }
163
164         status = rpccli_spoolss_ReplyClosePrinter(notify_cli_pipe, talloc_tos(),
165                                                   handle,
166                                                   &result);
167         if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result))
168                 DEBUG(0,("srv_spoolss_replycloseprinter: reply_close_printer failed [%s].\n",
169                         win_errstr(result)));
170
171         /* if it's the last connection, deconnect the IPC$ share */
172         if (smb_connections==1) {
173
174                 cli_shutdown( rpc_pipe_np_smb_conn(notify_cli_pipe) );
175                 notify_cli_pipe = NULL; /* The above call shuts downn the pipe also. */
176
177                 messaging_deregister(smbd_messaging_context(),
178                                      MSG_PRINTER_NOTIFY2, NULL);
179
180                 /* Tell the connections db we're no longer interested in
181                  * printer notify messages. */
182
183                 register_message_flags(false, FLAG_MSG_PRINT_NOTIFY);
184         }
185
186         smb_connections--;
187 }
188
189 /****************************************************************************
190  Functions to free a printer entry datastruct.
191 ****************************************************************************/
192
193 static int printer_entry_destructor(Printer_entry *Printer)
194 {
195         if (Printer->notify.client_connected == true) {
196                 int snum = -1;
197
198                 if ( Printer->printer_type == SPLHND_SERVER) {
199                         snum = -1;
200                         srv_spoolss_replycloseprinter(snum, &Printer->notify.client_hnd);
201                 } else if (Printer->printer_type == SPLHND_PRINTER) {
202                         snum = print_queue_snum(Printer->sharename);
203                         if (snum != -1)
204                                 srv_spoolss_replycloseprinter(snum,
205                                                 &Printer->notify.client_hnd);
206                 }
207         }
208
209         Printer->notify.flags=0;
210         Printer->notify.options=0;
211         Printer->notify.localmachine[0]='\0';
212         Printer->notify.printerlocal=0;
213         TALLOC_FREE(Printer->notify.option);
214         Printer->notify.client_connected = false;
215
216         free_nt_devicemode( &Printer->nt_devmode );
217         free_a_printer( &Printer->printer_info, 2 );
218
219         /* Remove from the internal list. */
220         DLIST_REMOVE(printers_list, Printer);
221         return 0;
222 }
223
224 /****************************************************************************
225   find printer index by handle
226 ****************************************************************************/
227
228 static Printer_entry *find_printer_index_by_hnd(pipes_struct *p,
229                                                 struct policy_handle *hnd)
230 {
231         Printer_entry *find_printer = NULL;
232
233         if(!find_policy_by_hnd(p,hnd,(void **)(void *)&find_printer)) {
234                 DEBUG(2,("find_printer_index_by_hnd: Printer handle not found: "));
235                 return NULL;
236         }
237
238         return find_printer;
239 }
240
241 /****************************************************************************
242  Close printer index by handle.
243 ****************************************************************************/
244
245 static bool close_printer_handle(pipes_struct *p, struct policy_handle *hnd)
246 {
247         Printer_entry *Printer = find_printer_index_by_hnd(p, hnd);
248
249         if (!Printer) {
250                 DEBUG(2,("close_printer_handle: Invalid handle (%s:%u:%u)\n",
251                         OUR_HANDLE(hnd)));
252                 return false;
253         }
254
255         close_policy_hnd(p, hnd);
256
257         return true;
258 }
259
260 /****************************************************************************
261  Delete a printer given a handle.
262 ****************************************************************************/
263
264 WERROR delete_printer_hook(TALLOC_CTX *ctx, NT_USER_TOKEN *token, const char *sharename )
265 {
266         char *cmd = lp_deleteprinter_cmd();
267         char *command = NULL;
268         int ret;
269         SE_PRIV se_printop = SE_PRINT_OPERATOR;
270         bool is_print_op = false;
271
272         /* can't fail if we don't try */
273
274         if ( !*cmd )
275                 return WERR_OK;
276
277         command = talloc_asprintf(ctx,
278                         "%s \"%s\"",
279                         cmd, sharename);
280         if (!command) {
281                 return WERR_NOMEM;
282         }
283         if ( token )
284                 is_print_op = user_has_privileges( token, &se_printop );
285
286         DEBUG(10,("Running [%s]\n", command));
287
288         /********** BEGIN SePrintOperatorPrivlege BLOCK **********/
289
290         if ( is_print_op )
291                 become_root();
292
293         if ( (ret = smbrun(command, NULL)) == 0 ) {
294                 /* Tell everyone we updated smb.conf. */
295                 message_send_all(smbd_messaging_context(),
296                                  MSG_SMB_CONF_UPDATED, NULL, 0, NULL);
297         }
298
299         if ( is_print_op )
300                 unbecome_root();
301
302         /********** END SePrintOperatorPrivlege BLOCK **********/
303
304         DEBUGADD(10,("returned [%d]\n", ret));
305
306         TALLOC_FREE(command);
307
308         if (ret != 0)
309                 return WERR_BADFID; /* What to return here? */
310
311         /* go ahead and re-read the services immediately */
312         become_root();
313         reload_services(false);
314         unbecome_root();
315
316         if ( lp_servicenumber( sharename )  < 0 )
317                 return WERR_ACCESS_DENIED;
318
319         return WERR_OK;
320 }
321
322 /****************************************************************************
323  Delete a printer given a handle.
324 ****************************************************************************/
325
326 static WERROR delete_printer_handle(pipes_struct *p, struct policy_handle *hnd)
327 {
328         Printer_entry *Printer = find_printer_index_by_hnd(p, hnd);
329
330         if (!Printer) {
331                 DEBUG(2,("delete_printer_handle: Invalid handle (%s:%u:%u)\n",
332                         OUR_HANDLE(hnd)));
333                 return WERR_BADFID;
334         }
335
336         /*
337          * It turns out that Windows allows delete printer on a handle
338          * opened by an admin user, then used on a pipe handle created
339          * by an anonymous user..... but they're working on security.... riiight !
340          * JRA.
341          */
342
343         if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
344                 DEBUG(3, ("delete_printer_handle: denied by handle\n"));
345                 return WERR_ACCESS_DENIED;
346         }
347
348         /* this does not need a become root since the access check has been
349            done on the handle already */
350
351         if (del_a_printer( Printer->sharename ) != 0) {
352                 DEBUG(3,("Error deleting printer %s\n", Printer->sharename));
353                 return WERR_BADFID;
354         }
355
356         return delete_printer_hook(p->mem_ctx, p->server_info->ptok,
357                                    Printer->sharename );
358 }
359
360 /****************************************************************************
361  Return the snum of a printer corresponding to an handle.
362 ****************************************************************************/
363
364 static bool get_printer_snum(pipes_struct *p, struct policy_handle *hnd,
365                              int *number, struct share_params **params)
366 {
367         Printer_entry *Printer = find_printer_index_by_hnd(p, hnd);
368
369         if (!Printer) {
370                 DEBUG(2,("get_printer_snum: Invalid handle (%s:%u:%u)\n",
371                         OUR_HANDLE(hnd)));
372                 return false;
373         }
374
375         switch (Printer->printer_type) {
376                 case SPLHND_PRINTER:
377                         DEBUG(4,("short name:%s\n", Printer->sharename));
378                         *number = print_queue_snum(Printer->sharename);
379                         return (*number != -1);
380                 case SPLHND_SERVER:
381                         return false;
382                 default:
383                         return false;
384         }
385 }
386
387 /****************************************************************************
388  Set printer handle type.
389  Check if it's \\server or \\server\printer
390 ****************************************************************************/
391
392 static bool set_printer_hnd_printertype(Printer_entry *Printer, const char *handlename)
393 {
394         DEBUG(3,("Setting printer type=%s\n", handlename));
395
396         if ( strlen(handlename) < 3 ) {
397                 DEBUGADD(4,("A print server must have at least 1 char ! %s\n", handlename));
398                 return false;
399         }
400
401         /* it's a print server */
402         if (*handlename=='\\' && *(handlename+1)=='\\' && !strchr_m(handlename+2, '\\')) {
403                 DEBUGADD(4,("Printer is a print server\n"));
404                 Printer->printer_type = SPLHND_SERVER;
405         }
406         /* it's a printer (set_printer_hnd_name() will handle port monitors */
407         else {
408                 DEBUGADD(4,("Printer is a printer\n"));
409                 Printer->printer_type = SPLHND_PRINTER;
410         }
411
412         return true;
413 }
414
415 /****************************************************************************
416  Set printer handle name..  Accept names like \\server, \\server\printer,
417  \\server\SHARE, & "\\server\,XcvMonitor Standard TCP/IP Port"    See
418  the MSDN docs regarding OpenPrinter() for details on the XcvData() and
419  XcvDataPort() interface.
420 ****************************************************************************/
421
422 static bool set_printer_hnd_name(Printer_entry *Printer, const char *handlename)
423 {
424         int snum;
425         int n_services=lp_numservices();
426         char *aprinter, *printername;
427         const char *servername;
428         fstring sname;
429         bool found = false;
430         NT_PRINTER_INFO_LEVEL *printer = NULL;
431         WERROR result;
432
433         DEBUG(4,("Setting printer name=%s (len=%lu)\n", handlename,
434                 (unsigned long)strlen(handlename)));
435
436         aprinter = CONST_DISCARD(char *, handlename);
437         if ( *handlename == '\\' ) {
438                 servername = canon_servername(handlename);
439                 if ( (aprinter = strchr_m( servername, '\\' )) != NULL ) {
440                         *aprinter = '\0';
441                         aprinter++;
442                 }
443         } else {
444                 servername = global_myname();
445         }
446
447         /* save the servername to fill in replies on this handle */
448
449         if ( !is_myname_or_ipaddr( servername ) )
450                 return false;
451
452         fstrcpy( Printer->servername, servername );
453
454         if ( Printer->printer_type == SPLHND_SERVER )
455                 return true;
456
457         if ( Printer->printer_type != SPLHND_PRINTER )
458                 return false;
459
460         DEBUGADD(5, ("searching for [%s]\n", aprinter ));
461
462         /* check for the Port Monitor Interface */
463
464         if ( strequal( aprinter, SPL_XCV_MONITOR_TCPMON ) ) {
465                 Printer->printer_type = SPLHND_PORTMON_TCP;
466                 fstrcpy(sname, SPL_XCV_MONITOR_TCPMON);
467                 found = true;
468         }
469         else if ( strequal( aprinter, SPL_XCV_MONITOR_LOCALMON ) ) {
470                 Printer->printer_type = SPLHND_PORTMON_LOCAL;
471                 fstrcpy(sname, SPL_XCV_MONITOR_LOCALMON);
472                 found = true;
473         }
474
475         /* Search all sharenames first as this is easier than pulling
476            the printer_info_2 off of disk. Don't use find_service() since
477            that calls out to map_username() */
478
479         /* do another loop to look for printernames */
480
481         for (snum=0; !found && snum<n_services; snum++) {
482
483                 /* no point going on if this is not a printer */
484
485                 if ( !(lp_snum_ok(snum) && lp_print_ok(snum)) )
486                         continue;
487
488                 fstrcpy(sname, lp_servicename(snum));
489                 if ( strequal( aprinter, sname ) ) {
490                         found = true;
491                         break;
492                 }
493
494                 /* no point looking up the printer object if
495                    we aren't allowing printername != sharename */
496
497                 if ( lp_force_printername(snum) )
498                         continue;
499
500                 fstrcpy(sname, lp_servicename(snum));
501
502                 printer = NULL;
503
504                 /* This call doesn't fill in the location or comment from
505                  * a CUPS server for efficiency with large numbers of printers.
506                  * JRA.
507                  */
508
509                 result = get_a_printer_search( NULL, &printer, 2, sname );
510                 if ( !W_ERROR_IS_OK(result) ) {
511                         DEBUG(0,("set_printer_hnd_name: failed to lookup printer [%s] -- result [%s]\n",
512                                 sname, win_errstr(result)));
513                         continue;
514                 }
515
516                 /* printername is always returned as \\server\printername */
517                 if ( !(printername = strchr_m(&printer->info_2->printername[2], '\\')) ) {
518                         DEBUG(0,("set_printer_hnd_name: info2->printername in wrong format! [%s]\n",
519                                 printer->info_2->printername));
520                         free_a_printer( &printer, 2);
521                         continue;
522                 }
523
524                 printername++;
525
526                 if ( strequal(printername, aprinter) ) {
527                         free_a_printer( &printer, 2);
528                         found = true;
529                         break;
530                 }
531
532                 DEBUGADD(10, ("printername: %s\n", printername));
533
534                 free_a_printer( &printer, 2);
535         }
536
537         free_a_printer( &printer, 2);
538
539         if ( !found ) {
540                 DEBUGADD(4,("Printer not found\n"));
541                 return false;
542         }
543
544         DEBUGADD(4,("set_printer_hnd_name: Printer found: %s -> %s\n", aprinter, sname));
545
546         fstrcpy(Printer->sharename, sname);
547
548         return true;
549 }
550
551 /****************************************************************************
552  Find first available printer slot. creates a printer handle for you.
553  ****************************************************************************/
554
555 static bool open_printer_hnd(pipes_struct *p, struct policy_handle *hnd,
556                              const char *name, uint32_t access_granted)
557 {
558         Printer_entry *new_printer;
559
560         DEBUG(10,("open_printer_hnd: name [%s]\n", name));
561
562         new_printer = TALLOC_ZERO_P(NULL, Printer_entry);
563         if (new_printer == NULL) {
564                 return false;
565         }
566         talloc_set_destructor(new_printer, printer_entry_destructor);
567
568         if (!create_policy_hnd(p, hnd, new_printer)) {
569                 TALLOC_FREE(new_printer);
570                 return false;
571         }
572
573         /* Add to the internal list. */
574         DLIST_ADD(printers_list, new_printer);
575
576         new_printer->notify.option=NULL;
577
578         if (!set_printer_hnd_printertype(new_printer, name)) {
579                 close_printer_handle(p, hnd);
580                 return false;
581         }
582
583         if (!set_printer_hnd_name(new_printer, name)) {
584                 close_printer_handle(p, hnd);
585                 return false;
586         }
587
588         new_printer->access_granted = access_granted;
589
590         DEBUG(5, ("%d printer handles active\n", (int)p->pipe_handles->count ));
591
592         return true;
593 }
594
595 /***************************************************************************
596  check to see if the client motify handle is monitoring the notification
597  given by (notify_type, notify_field).
598  **************************************************************************/
599
600 static bool is_monitoring_event_flags(uint32_t flags, uint16_t notify_type,
601                                       uint16_t notify_field)
602 {
603         return true;
604 }
605
606 static bool is_monitoring_event(Printer_entry *p, uint16_t notify_type,
607                                 uint16_t notify_field)
608 {
609         struct spoolss_NotifyOption *option = p->notify.option;
610         uint32_t i, j;
611
612         /*
613          * Flags should always be zero when the change notify
614          * is registered by the client's spooler.  A user Win32 app
615          * might use the flags though instead of the NOTIFY_OPTION_INFO
616          * --jerry
617          */
618
619         if (!option) {
620                 return false;
621         }
622
623         if (p->notify.flags)
624                 return is_monitoring_event_flags(
625                         p->notify.flags, notify_type, notify_field);
626
627         for (i = 0; i < option->count; i++) {
628
629                 /* Check match for notify_type */
630
631                 if (option->types[i].type != notify_type)
632                         continue;
633
634                 /* Check match for field */
635
636                 for (j = 0; j < option->types[i].count; j++) {
637                         if (option->types[i].fields[j].field == notify_field) {
638                                 return true;
639                         }
640                 }
641         }
642
643         DEBUG(10, ("Open handle for \\\\%s\\%s is not monitoring 0x%02x/0x%02x\n",
644                    p->servername, p->sharename, notify_type, notify_field));
645
646         return false;
647 }
648
649 #define SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(_data, _integer) \
650         _data->data.integer[0] = _integer; \
651         _data->data.integer[1] = 0;
652
653
654 #define SETUP_SPOOLSS_NOTIFY_DATA_STRING(_data, _p) \
655         _data->data.string.string = talloc_strdup(mem_ctx, _p); \
656         if (!_data->data.string.string) {\
657                 _data->data.string.size = 0; \
658         } \
659         _data->data.string.size = strlen_m_term(_p) * 2;
660
661 #define SETUP_SPOOLSS_NOTIFY_DATA_DEVMODE(_data, _devmode) \
662         _data->data.devmode.devmode = _devmode;
663
664 #define SETUP_SPOOLSS_NOTIFY_DATA_SECDESC(_data, _size, _sd) \
665         _data->data.sd.sd = dup_sec_desc(mem_ctx, _sd); \
666         if (!_data->data.sd.sd) { \
667                 _data->data.sd.sd_size = 0; \
668         } \
669         _data->data.sd.sd_size = _size;
670
671 static void init_systemtime_buffer(TALLOC_CTX *mem_ctx,
672                                    struct tm *t,
673                                    const char **pp,
674                                    uint32_t *plen)
675 {
676         struct spoolss_Time st;
677         uint32_t len = 16;
678         char *p;
679
680         if (!init_systemtime(&st, t)) {
681                 return;
682         }
683
684         p = talloc_array(mem_ctx, char, len);
685         if (!p) {
686                 return;
687         }
688
689         /*
690          * Systemtime must be linearized as a set of UINT16's.
691          * Fix from Benjamin (Bj) Kuit bj@it.uts.edu.au
692          */
693
694         SSVAL(p, 0, st.year);
695         SSVAL(p, 2, st.month);
696         SSVAL(p, 4, st.day_of_week);
697         SSVAL(p, 6, st.day);
698         SSVAL(p, 8, st.hour);
699         SSVAL(p, 10, st.minute);
700         SSVAL(p, 12, st.second);
701         SSVAL(p, 14, st.millisecond);
702
703         *pp = p;
704         *plen = len;
705 }
706
707 /* Convert a notification message to a struct spoolss_Notify */
708
709 static void notify_one_value(struct spoolss_notify_msg *msg,
710                              struct spoolss_Notify *data,
711                              TALLOC_CTX *mem_ctx)
712 {
713         SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, msg->notify.value[0]);
714 }
715
716 static void notify_string(struct spoolss_notify_msg *msg,
717                           struct spoolss_Notify *data,
718                           TALLOC_CTX *mem_ctx)
719 {
720         /* The length of the message includes the trailing \0 */
721
722         data->data.string.size = msg->len * 2;
723         data->data.string.string = talloc_strdup(mem_ctx, msg->notify.data);
724         if (!data->data.string.string) {
725                 data->data.string.size = 0;
726                 return;
727         }
728 }
729
730 static void notify_system_time(struct spoolss_notify_msg *msg,
731                                struct spoolss_Notify *data,
732                                TALLOC_CTX *mem_ctx)
733 {
734         data->data.string.string = NULL;
735         data->data.string.size = 0;
736
737         if (msg->len != sizeof(time_t)) {
738                 DEBUG(5, ("notify_system_time: received wrong sized message (%d)\n",
739                           msg->len));
740                 return;
741         }
742
743         init_systemtime_buffer(mem_ctx, gmtime((time_t *)msg->notify.data),
744                                &data->data.string.string,
745                                &data->data.string.size);
746 }
747
748 struct notify2_message_table {
749         const char *name;
750         void (*fn)(struct spoolss_notify_msg *msg,
751                    struct spoolss_Notify *data, TALLOC_CTX *mem_ctx);
752 };
753
754 static struct notify2_message_table printer_notify_table[] = {
755         /* 0x00 */ { "PRINTER_NOTIFY_FIELD_SERVER_NAME", notify_string },
756         /* 0x01 */ { "PRINTER_NOTIFY_FIELD_PRINTER_NAME", notify_string },
757         /* 0x02 */ { "PRINTER_NOTIFY_FIELD_SHARE_NAME", notify_string },
758         /* 0x03 */ { "PRINTER_NOTIFY_FIELD_PORT_NAME", notify_string },
759         /* 0x04 */ { "PRINTER_NOTIFY_FIELD_DRIVER_NAME", notify_string },
760         /* 0x05 */ { "PRINTER_NOTIFY_FIELD_COMMENT", notify_string },
761         /* 0x06 */ { "PRINTER_NOTIFY_FIELD_LOCATION", notify_string },
762         /* 0x07 */ { "PRINTER_NOTIFY_FIELD_DEVMODE", NULL },
763         /* 0x08 */ { "PRINTER_NOTIFY_FIELD_SEPFILE", notify_string },
764         /* 0x09 */ { "PRINTER_NOTIFY_FIELD_PRINT_PROCESSOR", notify_string },
765         /* 0x0a */ { "PRINTER_NOTIFY_FIELD_PARAMETERS", NULL },
766         /* 0x0b */ { "PRINTER_NOTIFY_FIELD_DATATYPE", notify_string },
767         /* 0x0c */ { "PRINTER_NOTIFY_FIELD_SECURITY_DESCRIPTOR", NULL },
768         /* 0x0d */ { "PRINTER_NOTIFY_FIELD_ATTRIBUTES", notify_one_value },
769         /* 0x0e */ { "PRINTER_NOTIFY_FIELD_PRIORITY", notify_one_value },
770         /* 0x0f */ { "PRINTER_NOTIFY_FIELD_DEFAULT_PRIORITY", NULL },
771         /* 0x10 */ { "PRINTER_NOTIFY_FIELD_START_TIME", NULL },
772         /* 0x11 */ { "PRINTER_NOTIFY_FIELD_UNTIL_TIME", NULL },
773         /* 0x12 */ { "PRINTER_NOTIFY_FIELD_STATUS", notify_one_value },
774 };
775
776 static struct notify2_message_table job_notify_table[] = {
777         /* 0x00 */ { "JOB_NOTIFY_FIELD_PRINTER_NAME", NULL },
778         /* 0x01 */ { "JOB_NOTIFY_FIELD_MACHINE_NAME", NULL },
779         /* 0x02 */ { "JOB_NOTIFY_FIELD_PORT_NAME", NULL },
780         /* 0x03 */ { "JOB_NOTIFY_FIELD_USER_NAME", notify_string },
781         /* 0x04 */ { "JOB_NOTIFY_FIELD_NOTIFY_NAME", NULL },
782         /* 0x05 */ { "JOB_NOTIFY_FIELD_DATATYPE", NULL },
783         /* 0x06 */ { "JOB_NOTIFY_FIELD_PRINT_PROCESSOR", NULL },
784         /* 0x07 */ { "JOB_NOTIFY_FIELD_PARAMETERS", NULL },
785         /* 0x08 */ { "JOB_NOTIFY_FIELD_DRIVER_NAME", NULL },
786         /* 0x09 */ { "JOB_NOTIFY_FIELD_DEVMODE", NULL },
787         /* 0x0a */ { "JOB_NOTIFY_FIELD_STATUS", notify_one_value },
788         /* 0x0b */ { "JOB_NOTIFY_FIELD_STATUS_STRING", NULL },
789         /* 0x0c */ { "JOB_NOTIFY_FIELD_SECURITY_DESCRIPTOR", NULL },
790         /* 0x0d */ { "JOB_NOTIFY_FIELD_DOCUMENT", notify_string },
791         /* 0x0e */ { "JOB_NOTIFY_FIELD_PRIORITY", NULL },
792         /* 0x0f */ { "JOB_NOTIFY_FIELD_POSITION", NULL },
793         /* 0x10 */ { "JOB_NOTIFY_FIELD_SUBMITTED", notify_system_time },
794         /* 0x11 */ { "JOB_NOTIFY_FIELD_START_TIME", NULL },
795         /* 0x12 */ { "JOB_NOTIFY_FIELD_UNTIL_TIME", NULL },
796         /* 0x13 */ { "JOB_NOTIFY_FIELD_TIME", NULL },
797         /* 0x14 */ { "JOB_NOTIFY_FIELD_TOTAL_PAGES", notify_one_value },
798         /* 0x15 */ { "JOB_NOTIFY_FIELD_PAGES_PRINTED", NULL },
799         /* 0x16 */ { "JOB_NOTIFY_FIELD_TOTAL_BYTES", notify_one_value },
800         /* 0x17 */ { "JOB_NOTIFY_FIELD_BYTES_PRINTED", NULL },
801 };
802
803
804 /***********************************************************************
805  Allocate talloc context for container object
806  **********************************************************************/
807
808 static void notify_msg_ctr_init( SPOOLSS_NOTIFY_MSG_CTR *ctr )
809 {
810         if ( !ctr )
811                 return;
812
813         ctr->ctx = talloc_init("notify_msg_ctr_init %p", ctr);
814
815         return;
816 }
817
818 /***********************************************************************
819  release all allocated memory and zero out structure
820  **********************************************************************/
821
822 static void notify_msg_ctr_destroy( SPOOLSS_NOTIFY_MSG_CTR *ctr )
823 {
824         if ( !ctr )
825                 return;
826
827         if ( ctr->ctx )
828                 talloc_destroy(ctr->ctx);
829
830         ZERO_STRUCTP(ctr);
831
832         return;
833 }
834
835 /***********************************************************************
836  **********************************************************************/
837
838 static TALLOC_CTX* notify_ctr_getctx( SPOOLSS_NOTIFY_MSG_CTR *ctr )
839 {
840         if ( !ctr )
841                 return NULL;
842
843         return ctr->ctx;
844 }
845
846 /***********************************************************************
847  **********************************************************************/
848
849 static SPOOLSS_NOTIFY_MSG_GROUP* notify_ctr_getgroup( SPOOLSS_NOTIFY_MSG_CTR *ctr, uint32_t idx )
850 {
851         if ( !ctr || !ctr->msg_groups )
852                 return NULL;
853
854         if ( idx >= ctr->num_groups )
855                 return NULL;
856
857         return &ctr->msg_groups[idx];
858
859 }
860
861 /***********************************************************************
862  How many groups of change messages do we have ?
863  **********************************************************************/
864
865 static int notify_msg_ctr_numgroups( SPOOLSS_NOTIFY_MSG_CTR *ctr )
866 {
867         if ( !ctr )
868                 return 0;
869
870         return ctr->num_groups;
871 }
872
873 /***********************************************************************
874  Add a SPOOLSS_NOTIFY_MSG_CTR to the correct group
875  **********************************************************************/
876
877 static int notify_msg_ctr_addmsg( SPOOLSS_NOTIFY_MSG_CTR *ctr, SPOOLSS_NOTIFY_MSG *msg )
878 {
879         SPOOLSS_NOTIFY_MSG_GROUP        *groups = NULL;
880         SPOOLSS_NOTIFY_MSG_GROUP        *msg_grp = NULL;
881         SPOOLSS_NOTIFY_MSG              *msg_list = NULL;
882         int                             i, new_slot;
883
884         if ( !ctr || !msg )
885                 return 0;
886
887         /* loop over all groups looking for a matching printer name */
888
889         for ( i=0; i<ctr->num_groups; i++ ) {
890                 if ( strcmp(ctr->msg_groups[i].printername, msg->printer) == 0 )
891                         break;
892         }
893
894         /* add a new group? */
895
896         if ( i == ctr->num_groups ) {
897                 ctr->num_groups++;
898
899                 if ( !(groups = TALLOC_REALLOC_ARRAY( ctr->ctx, ctr->msg_groups, SPOOLSS_NOTIFY_MSG_GROUP, ctr->num_groups)) ) {
900                         DEBUG(0,("notify_msg_ctr_addmsg: talloc_realloc() failed!\n"));
901                         return 0;
902                 }
903                 ctr->msg_groups = groups;
904
905                 /* clear the new entry and set the printer name */
906
907                 ZERO_STRUCT( ctr->msg_groups[ctr->num_groups-1] );
908                 fstrcpy( ctr->msg_groups[ctr->num_groups-1].printername, msg->printer );
909         }
910
911         /* add the change messages; 'i' is the correct index now regardless */
912
913         msg_grp = &ctr->msg_groups[i];
914
915         msg_grp->num_msgs++;
916
917         if ( !(msg_list = TALLOC_REALLOC_ARRAY( ctr->ctx, msg_grp->msgs, SPOOLSS_NOTIFY_MSG, msg_grp->num_msgs )) ) {
918                 DEBUG(0,("notify_msg_ctr_addmsg: talloc_realloc() failed for new message [%d]!\n", msg_grp->num_msgs));
919                 return 0;
920         }
921         msg_grp->msgs = msg_list;
922
923         new_slot = msg_grp->num_msgs-1;
924         memcpy( &msg_grp->msgs[new_slot], msg, sizeof(SPOOLSS_NOTIFY_MSG) );
925
926         /* need to allocate own copy of data */
927
928         if ( msg->len != 0 )
929                 msg_grp->msgs[new_slot].notify.data = (char *)
930                         TALLOC_MEMDUP( ctr->ctx, msg->notify.data, msg->len );
931
932         return ctr->num_groups;
933 }
934
935 /***********************************************************************
936  Send a change notication message on all handles which have a call
937  back registered
938  **********************************************************************/
939
940 static void send_notify2_changes( SPOOLSS_NOTIFY_MSG_CTR *ctr, uint32_t idx )
941 {
942         Printer_entry            *p;
943         TALLOC_CTX               *mem_ctx = notify_ctr_getctx( ctr );
944         SPOOLSS_NOTIFY_MSG_GROUP *msg_group = notify_ctr_getgroup( ctr, idx );
945         SPOOLSS_NOTIFY_MSG       *messages;
946         int                      sending_msg_count;
947
948         if ( !msg_group ) {
949                 DEBUG(5,("send_notify2_changes() called with no msg group!\n"));
950                 return;
951         }
952
953         messages = msg_group->msgs;
954
955         if ( !messages ) {
956                 DEBUG(5,("send_notify2_changes() called with no messages!\n"));
957                 return;
958         }
959
960         DEBUG(8,("send_notify2_changes: Enter...[%s]\n", msg_group->printername));
961
962         /* loop over all printers */
963
964         for (p = printers_list; p; p = p->next) {
965                 struct spoolss_Notify *notifies;
966                 uint32_t count = 0;
967                 uint32_t id;
968                 int     i;
969
970                 /* Is there notification on this handle? */
971
972                 if ( !p->notify.client_connected )
973                         continue;
974
975                 DEBUG(10,("Client connected! [\\\\%s\\%s]\n", p->servername, p->sharename));
976
977                 /* For this printer?  Print servers always receive
978                    notifications. */
979
980                 if ( ( p->printer_type == SPLHND_PRINTER )  &&
981                     ( !strequal(msg_group->printername, p->sharename) ) )
982                         continue;
983
984                 DEBUG(10,("Our printer\n"));
985
986                 /* allocate the max entries possible */
987
988                 notifies = TALLOC_ZERO_ARRAY(mem_ctx, struct spoolss_Notify, msg_group->num_msgs);
989                 if (!notifies) {
990                         return;
991                 }
992
993                 /* build the array of change notifications */
994
995                 sending_msg_count = 0;
996
997                 for ( i=0; i<msg_group->num_msgs; i++ ) {
998                         SPOOLSS_NOTIFY_MSG      *msg = &messages[i];
999
1000                         /* Are we monitoring this event? */
1001
1002                         if (!is_monitoring_event(p, msg->type, msg->field))
1003                                 continue;
1004
1005                         sending_msg_count++;
1006
1007
1008                         DEBUG(10,("process_notify2_message: Sending message type [0x%x] field [0x%2x] for printer [%s]\n",
1009                                 msg->type, msg->field, p->sharename));
1010
1011                         /*
1012                          * if the is a printer notification handle and not a job notification
1013                          * type, then set the id to 0.  Other wise just use what was specified
1014                          * in the message.
1015                          *
1016                          * When registering change notification on a print server handle
1017                          * we always need to send back the id (snum) matching the printer
1018                          * for which the change took place.  For change notify registered
1019                          * on a printer handle, this does not matter and the id should be 0.
1020                          *
1021                          * --jerry
1022                          */
1023
1024                         if ( ( p->printer_type == SPLHND_PRINTER ) && ( msg->type == PRINTER_NOTIFY_TYPE ) )
1025                                 id = 0;
1026                         else
1027                                 id = msg->id;
1028
1029
1030                         /* Convert unix jobid to smb jobid */
1031
1032                         if (msg->flags & SPOOLSS_NOTIFY_MSG_UNIX_JOBID) {
1033                                 id = sysjob_to_jobid(msg->id);
1034
1035                                 if (id == -1) {
1036                                         DEBUG(3, ("no such unix jobid %d\n", msg->id));
1037                                         goto done;
1038                                 }
1039                         }
1040
1041                         construct_info_data( &notifies[count], msg->type, msg->field, id );
1042
1043                         switch(msg->type) {
1044                         case PRINTER_NOTIFY_TYPE:
1045                                 if ( printer_notify_table[msg->field].fn )
1046                                         printer_notify_table[msg->field].fn(msg, &notifies[count], mem_ctx);
1047                                 break;
1048
1049                         case JOB_NOTIFY_TYPE:
1050                                 if ( job_notify_table[msg->field].fn )
1051                                         job_notify_table[msg->field].fn(msg, &notifies[count], mem_ctx);
1052                                 break;
1053
1054                         default:
1055                                 DEBUG(5, ("Unknown notification type %d\n", msg->type));
1056                                 goto done;
1057                         }
1058
1059                         count++;
1060                 }
1061
1062                 if ( sending_msg_count ) {
1063                         NTSTATUS status;
1064                         WERROR werr;
1065                         union spoolss_ReplyPrinterInfo info;
1066                         struct spoolss_NotifyInfo info0;
1067                         uint32_t reply_result;
1068
1069                         info0.version   = 0x2;
1070                         info0.flags     = count ? 0x00020000 /* ??? */ : PRINTER_NOTIFY_INFO_DISCARDED;
1071                         info0.count     = count;
1072                         info0.notifies  = notifies;
1073
1074                         info.info0 = &info0;
1075
1076                         status = rpccli_spoolss_RouterReplyPrinterEx(notify_cli_pipe, mem_ctx,
1077                                                                      &p->notify.client_hnd,
1078                                                                      p->notify.change, /* color */
1079                                                                      p->notify.flags,
1080                                                                      &reply_result,
1081                                                                      0, /* reply_type, must be 0 */
1082                                                                      info,
1083                                                                      &werr);
1084                         if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(werr)) {
1085                                 DEBUG(1,("RouterReplyPrinterEx to client: %s failed: %s\n",
1086                                         notify_cli_pipe->srv_name_slash,
1087                                         win_errstr(werr)));
1088                         }
1089                         switch (reply_result) {
1090                                 case 0:
1091                                         break;
1092                                 case PRINTER_NOTIFY_INFO_DISCARDED:
1093                                 case PRINTER_NOTIFY_INFO_DISCARDNOTED:
1094                                 case PRINTER_NOTIFY_INFO_COLOR_MISMATCH:
1095                                         break;
1096                                 default:
1097                                         break;
1098                         }
1099                 }
1100         }
1101
1102 done:
1103         DEBUG(8,("send_notify2_changes: Exit...\n"));
1104         return;
1105 }
1106
1107 /***********************************************************************
1108  **********************************************************************/
1109
1110 static bool notify2_unpack_msg( SPOOLSS_NOTIFY_MSG *msg, struct timeval *tv, void *buf, size_t len )
1111 {
1112
1113         uint32_t tv_sec, tv_usec;
1114         size_t offset = 0;
1115
1116         /* Unpack message */
1117
1118         offset += tdb_unpack((uint8_t *)buf + offset, len - offset, "f",
1119                              msg->printer);
1120
1121         offset += tdb_unpack((uint8_t *)buf + offset, len - offset, "ddddddd",
1122                                 &tv_sec, &tv_usec,
1123                                 &msg->type, &msg->field, &msg->id, &msg->len, &msg->flags);
1124
1125         if (msg->len == 0)
1126                 tdb_unpack((uint8_t *)buf + offset, len - offset, "dd",
1127                            &msg->notify.value[0], &msg->notify.value[1]);
1128         else
1129                 tdb_unpack((uint8_t *)buf + offset, len - offset, "B",
1130                            &msg->len, &msg->notify.data);
1131
1132         DEBUG(3, ("notify2_unpack_msg: got NOTIFY2 message for printer %s, jobid %u type %d, field 0x%02x, flags 0x%04x\n",
1133                   msg->printer, (unsigned int)msg->id, msg->type, msg->field, msg->flags));
1134
1135         tv->tv_sec = tv_sec;
1136         tv->tv_usec = tv_usec;
1137
1138         if (msg->len == 0)
1139                 DEBUG(3, ("notify2_unpack_msg: value1 = %d, value2 = %d\n", msg->notify.value[0],
1140                           msg->notify.value[1]));
1141         else
1142                 dump_data(3, (uint8_t *)msg->notify.data, msg->len);
1143
1144         return true;
1145 }
1146
1147 /********************************************************************
1148  Receive a notify2 message list
1149  ********************************************************************/
1150
1151 static void receive_notify2_message_list(struct messaging_context *msg,
1152                                          void *private_data,
1153                                          uint32_t msg_type,
1154                                          struct server_id server_id,
1155                                          DATA_BLOB *data)
1156 {
1157         size_t                  msg_count, i;
1158         char                    *buf = (char *)data->data;
1159         char                    *msg_ptr;
1160         size_t                  msg_len;
1161         SPOOLSS_NOTIFY_MSG      notify;
1162         SPOOLSS_NOTIFY_MSG_CTR  messages;
1163         int                     num_groups;
1164
1165         if (data->length < 4) {
1166                 DEBUG(0,("receive_notify2_message_list: bad message format (len < 4)!\n"));
1167                 return;
1168         }
1169
1170         msg_count = IVAL(buf, 0);
1171         msg_ptr = buf + 4;
1172
1173         DEBUG(5, ("receive_notify2_message_list: got %lu messages in list\n", (unsigned long)msg_count));
1174
1175         if (msg_count == 0) {
1176                 DEBUG(0,("receive_notify2_message_list: bad message format (msg_count == 0) !\n"));
1177                 return;
1178         }
1179
1180         /* initialize the container */
1181
1182         ZERO_STRUCT( messages );
1183         notify_msg_ctr_init( &messages );
1184
1185         /*
1186          * build message groups for each printer identified
1187          * in a change_notify msg.  Remember that a PCN message
1188          * includes the handle returned for the srv_spoolss_replyopenprinter()
1189          * call.  Therefore messages are grouped according to printer handle.
1190          */
1191
1192         for ( i=0; i<msg_count; i++ ) {
1193                 struct timeval msg_tv;
1194
1195                 if (msg_ptr + 4 - buf > data->length) {
1196                         DEBUG(0,("receive_notify2_message_list: bad message format (len > buf_size) !\n"));
1197                         return;
1198                 }
1199
1200                 msg_len = IVAL(msg_ptr,0);
1201                 msg_ptr += 4;
1202
1203                 if (msg_ptr + msg_len - buf > data->length) {
1204                         DEBUG(0,("receive_notify2_message_list: bad message format (bad len) !\n"));
1205                         return;
1206                 }
1207
1208                 /* unpack messages */
1209
1210                 ZERO_STRUCT( notify );
1211                 notify2_unpack_msg( &notify, &msg_tv, msg_ptr, msg_len );
1212                 msg_ptr += msg_len;
1213
1214                 /* add to correct list in container */
1215
1216                 notify_msg_ctr_addmsg( &messages, &notify );
1217
1218                 /* free memory that might have been allocated by notify2_unpack_msg() */
1219
1220                 if ( notify.len != 0 )
1221                         SAFE_FREE( notify.notify.data );
1222         }
1223
1224         /* process each group of messages */
1225
1226         num_groups = notify_msg_ctr_numgroups( &messages );
1227         for ( i=0; i<num_groups; i++ )
1228                 send_notify2_changes( &messages, i );
1229
1230
1231         /* cleanup */
1232
1233         DEBUG(10,("receive_notify2_message_list: processed %u messages\n",
1234                 (uint32_t)msg_count ));
1235
1236         notify_msg_ctr_destroy( &messages );
1237
1238         return;
1239 }
1240
1241 /********************************************************************
1242  Send a message to ourself about new driver being installed
1243  so we can upgrade the information for each printer bound to this
1244  driver
1245  ********************************************************************/
1246
1247 static bool srv_spoolss_drv_upgrade_printer(const char *drivername)
1248 {
1249         int len = strlen(drivername);
1250
1251         if (!len)
1252                 return false;
1253
1254         DEBUG(10,("srv_spoolss_drv_upgrade_printer: Sending message about driver upgrade [%s]\n",
1255                 drivername));
1256
1257         messaging_send_buf(smbd_messaging_context(), procid_self(),
1258                            MSG_PRINTER_DRVUPGRADE,
1259                            (uint8_t *)drivername, len+1);
1260
1261         return true;
1262 }
1263
1264 /**********************************************************************
1265  callback to receive a MSG_PRINTER_DRVUPGRADE message and interate
1266  over all printers, upgrading ones as necessary
1267  **********************************************************************/
1268
1269 void do_drv_upgrade_printer(struct messaging_context *msg,
1270                             void *private_data,
1271                             uint32_t msg_type,
1272                             struct server_id server_id,
1273                             DATA_BLOB *data)
1274 {
1275         fstring drivername;
1276         int snum;
1277         int n_services = lp_numservices();
1278         size_t len;
1279
1280         len = MIN(data->length,sizeof(drivername)-1);
1281         strncpy(drivername, (const char *)data->data, len);
1282
1283         DEBUG(10,("do_drv_upgrade_printer: Got message for new driver [%s]\n", drivername ));
1284
1285         /* Iterate the printer list */
1286
1287         for (snum=0; snum<n_services; snum++)
1288         {
1289                 if (lp_snum_ok(snum) && lp_print_ok(snum) )
1290                 {
1291                         WERROR result;
1292                         NT_PRINTER_INFO_LEVEL *printer = NULL;
1293
1294                         result = get_a_printer(NULL, &printer, 2, lp_const_servicename(snum));
1295                         if (!W_ERROR_IS_OK(result))
1296                                 continue;
1297
1298                         if (printer && printer->info_2 && !strcmp(drivername, printer->info_2->drivername))
1299                         {
1300                                 DEBUG(6,("Updating printer [%s]\n", printer->info_2->printername));
1301
1302                                 /* all we care about currently is the change_id */
1303
1304                                 result = mod_a_printer(printer, 2);
1305                                 if (!W_ERROR_IS_OK(result)) {
1306                                         DEBUG(3,("do_drv_upgrade_printer: mod_a_printer() failed with status [%s]\n",
1307                                                 win_errstr(result)));
1308                                 }
1309                         }
1310
1311                         free_a_printer(&printer, 2);
1312                 }
1313         }
1314
1315         /* all done */
1316 }
1317
1318 /********************************************************************
1319  Update the cache for all printq's with a registered client
1320  connection
1321  ********************************************************************/
1322
1323 void update_monitored_printq_cache( void )
1324 {
1325         Printer_entry *printer = printers_list;
1326         int snum;
1327
1328         /* loop through all printers and update the cache where
1329            client_connected == true */
1330         while ( printer )
1331         {
1332                 if ( (printer->printer_type == SPLHND_PRINTER)
1333                         && printer->notify.client_connected )
1334                 {
1335                         snum = print_queue_snum(printer->sharename);
1336                         print_queue_status( snum, NULL, NULL );
1337                 }
1338
1339                 printer = printer->next;
1340         }
1341
1342         return;
1343 }
1344 /********************************************************************
1345  Send a message to ourself about new driver being installed
1346  so we can upgrade the information for each printer bound to this
1347  driver
1348  ********************************************************************/
1349
1350 static bool srv_spoolss_reset_printerdata(char* drivername)
1351 {
1352         int len = strlen(drivername);
1353
1354         if (!len)
1355                 return false;
1356
1357         DEBUG(10,("srv_spoolss_reset_printerdata: Sending message about resetting printerdata [%s]\n",
1358                 drivername));
1359
1360         messaging_send_buf(smbd_messaging_context(), procid_self(),
1361                            MSG_PRINTERDATA_INIT_RESET,
1362                            (uint8_t *)drivername, len+1);
1363
1364         return true;
1365 }
1366
1367 /**********************************************************************
1368  callback to receive a MSG_PRINTERDATA_INIT_RESET message and interate
1369  over all printers, resetting printer data as neessary
1370  **********************************************************************/
1371
1372 void reset_all_printerdata(struct messaging_context *msg,
1373                            void *private_data,
1374                            uint32_t msg_type,
1375                            struct server_id server_id,
1376                            DATA_BLOB *data)
1377 {
1378         fstring drivername;
1379         int snum;
1380         int n_services = lp_numservices();
1381         size_t len;
1382
1383         len = MIN( data->length, sizeof(drivername)-1 );
1384         strncpy( drivername, (const char *)data->data, len );
1385
1386         DEBUG(10,("reset_all_printerdata: Got message for new driver [%s]\n", drivername ));
1387
1388         /* Iterate the printer list */
1389
1390         for ( snum=0; snum<n_services; snum++ )
1391         {
1392                 if ( lp_snum_ok(snum) && lp_print_ok(snum) )
1393                 {
1394                         WERROR result;
1395                         NT_PRINTER_INFO_LEVEL *printer = NULL;
1396
1397                         result = get_a_printer( NULL, &printer, 2, lp_const_servicename(snum) );
1398                         if ( !W_ERROR_IS_OK(result) )
1399                                 continue;
1400
1401                         /*
1402                          * if the printer is bound to the driver,
1403                          * then reset to the new driver initdata
1404                          */
1405
1406                         if ( printer && printer->info_2 && !strcmp(drivername, printer->info_2->drivername) )
1407                         {
1408                                 DEBUG(6,("reset_all_printerdata: Updating printer [%s]\n", printer->info_2->printername));
1409
1410                                 if ( !set_driver_init(printer, 2) ) {
1411                                         DEBUG(5,("reset_all_printerdata: Error resetting printer data for printer [%s], driver [%s]!\n",
1412                                                 printer->info_2->printername, printer->info_2->drivername));
1413                                 }
1414
1415                                 result = mod_a_printer( printer, 2 );
1416                                 if ( !W_ERROR_IS_OK(result) ) {
1417                                         DEBUG(3,("reset_all_printerdata: mod_a_printer() failed!  (%s)\n",
1418                                                 get_dos_error_msg(result)));
1419                                 }
1420                         }
1421
1422                         free_a_printer( &printer, 2 );
1423                 }
1424         }
1425
1426         /* all done */
1427
1428         return;
1429 }
1430
1431 /****************************************************************
1432  _spoolss_OpenPrinter
1433 ****************************************************************/
1434
1435 WERROR _spoolss_OpenPrinter(pipes_struct *p,
1436                             struct spoolss_OpenPrinter *r)
1437 {
1438         struct spoolss_OpenPrinterEx e;
1439         WERROR werr;
1440
1441         ZERO_STRUCT(e.in.userlevel);
1442
1443         e.in.printername        = r->in.printername;
1444         e.in.datatype           = r->in.datatype;
1445         e.in.devmode_ctr        = r->in.devmode_ctr;
1446         e.in.access_mask        = r->in.access_mask;
1447         e.in.level              = 0;
1448
1449         e.out.handle            = r->out.handle;
1450
1451         werr = _spoolss_OpenPrinterEx(p, &e);
1452
1453         if (W_ERROR_EQUAL(werr, WERR_INVALID_PARAM)) {
1454                 /* OpenPrinterEx returns this for a bad
1455                  * printer name. We must return WERR_INVALID_PRINTER_NAME
1456                  * instead.
1457                  */
1458                 werr = WERR_INVALID_PRINTER_NAME;
1459         }
1460
1461         return werr;
1462 }
1463
1464 /********************************************************************
1465  ********************************************************************/
1466
1467 bool convert_devicemode(const char *printername,
1468                         const struct spoolss_DeviceMode *devmode,
1469                         NT_DEVICEMODE **pp_nt_devmode)
1470 {
1471         NT_DEVICEMODE *nt_devmode = *pp_nt_devmode;
1472
1473         /*
1474          * Ensure nt_devmode is a valid pointer
1475          * as we will be overwriting it.
1476          */
1477
1478         if (nt_devmode == NULL) {
1479                 DEBUG(5, ("convert_devicemode: allocating a generic devmode\n"));
1480                 if ((nt_devmode = construct_nt_devicemode(printername)) == NULL)
1481                         return false;
1482         }
1483
1484         rpcstr_push(nt_devmode->devicename, devmode->devicename, 31, 0);
1485         rpcstr_push(nt_devmode->formname, devmode->formname, 31, 0);
1486
1487         nt_devmode->specversion         = devmode->specversion;
1488         nt_devmode->driverversion       = devmode->driverversion;
1489         nt_devmode->size                = devmode->size;
1490         nt_devmode->fields              = devmode->fields;
1491         nt_devmode->orientation         = devmode->orientation;
1492         nt_devmode->papersize           = devmode->papersize;
1493         nt_devmode->paperlength         = devmode->paperlength;
1494         nt_devmode->paperwidth          = devmode->paperwidth;
1495         nt_devmode->scale               = devmode->scale;
1496         nt_devmode->copies              = devmode->copies;
1497         nt_devmode->defaultsource       = devmode->defaultsource;
1498         nt_devmode->printquality        = devmode->printquality;
1499         nt_devmode->color               = devmode->color;
1500         nt_devmode->duplex              = devmode->duplex;
1501         nt_devmode->yresolution         = devmode->yresolution;
1502         nt_devmode->ttoption            = devmode->ttoption;
1503         nt_devmode->collate             = devmode->collate;
1504
1505         nt_devmode->logpixels           = devmode->logpixels;
1506         nt_devmode->bitsperpel          = devmode->bitsperpel;
1507         nt_devmode->pelswidth           = devmode->pelswidth;
1508         nt_devmode->pelsheight          = devmode->pelsheight;
1509         nt_devmode->displayflags        = devmode->displayflags;
1510         nt_devmode->displayfrequency    = devmode->displayfrequency;
1511         nt_devmode->icmmethod           = devmode->icmmethod;
1512         nt_devmode->icmintent           = devmode->icmintent;
1513         nt_devmode->mediatype           = devmode->mediatype;
1514         nt_devmode->dithertype          = devmode->dithertype;
1515         nt_devmode->reserved1           = devmode->reserved1;
1516         nt_devmode->reserved2           = devmode->reserved2;
1517         nt_devmode->panningwidth        = devmode->panningwidth;
1518         nt_devmode->panningheight       = devmode->panningheight;
1519
1520         /*
1521          * Only change private and driverextra if the incoming devmode
1522          * has a new one. JRA.
1523          */
1524
1525         if ((devmode->__driverextra_length != 0) && (devmode->driverextra_data.data != NULL)) {
1526                 SAFE_FREE(nt_devmode->nt_dev_private);
1527                 nt_devmode->driverextra = devmode->__driverextra_length;
1528                 if((nt_devmode->nt_dev_private = SMB_MALLOC_ARRAY(uint8_t, nt_devmode->driverextra)) == NULL)
1529                         return false;
1530                 memcpy(nt_devmode->nt_dev_private, devmode->driverextra_data.data, nt_devmode->driverextra);
1531         }
1532
1533         *pp_nt_devmode = nt_devmode;
1534
1535         return true;
1536 }
1537
1538 /****************************************************************
1539  _spoolss_OpenPrinterEx
1540 ****************************************************************/
1541
1542 WERROR _spoolss_OpenPrinterEx(pipes_struct *p,
1543                               struct spoolss_OpenPrinterEx *r)
1544 {
1545         int snum;
1546         Printer_entry *Printer=NULL;
1547
1548         if (!r->in.printername) {
1549                 return WERR_INVALID_PARAM;
1550         }
1551
1552         /* some sanity check because you can open a printer or a print server */
1553         /* aka: \\server\printer or \\server */
1554
1555         DEBUGADD(3,("checking name: %s\n", r->in.printername));
1556
1557         if (!open_printer_hnd(p, r->out.handle, r->in.printername, 0)) {
1558                 ZERO_STRUCTP(r->out.handle);
1559                 return WERR_INVALID_PARAM;
1560         }
1561
1562         Printer = find_printer_index_by_hnd(p, r->out.handle);
1563         if ( !Printer ) {
1564                 DEBUG(0,("_spoolss_OpenPrinterEx: logic error.  Can't find printer "
1565                         "handle we created for printer %s\n", r->in.printername));
1566                 close_printer_handle(p, r->out.handle);
1567                 ZERO_STRUCTP(r->out.handle);
1568                 return WERR_INVALID_PARAM;
1569         }
1570
1571         /*
1572          * First case: the user is opening the print server:
1573          *
1574          * Disallow MS AddPrinterWizard if parameter disables it. A Win2k
1575          * client 1st tries an OpenPrinterEx with access==0, MUST be allowed.
1576          *
1577          * Then both Win2k and WinNT clients try an OpenPrinterEx with
1578          * SERVER_ALL_ACCESS, which we allow only if the user is root (uid=0)
1579          * or if the user is listed in the smb.conf printer admin parameter.
1580          *
1581          * Then they try OpenPrinterEx with SERVER_READ which we allow. This lets the
1582          * client view printer folder, but does not show the MSAPW.
1583          *
1584          * Note: this test needs code to check access rights here too. Jeremy
1585          * could you look at this?
1586          *
1587          * Second case: the user is opening a printer:
1588          * NT doesn't let us connect to a printer if the connecting user
1589          * doesn't have print permission.
1590          *
1591          * Third case: user is opening a Port Monitor
1592          * access checks same as opening a handle to the print server.
1593          */
1594
1595         switch (Printer->printer_type )
1596         {
1597         case SPLHND_SERVER:
1598         case SPLHND_PORTMON_TCP:
1599         case SPLHND_PORTMON_LOCAL:
1600                 /* Printserver handles use global struct... */
1601
1602                 snum = -1;
1603
1604                 /* Map standard access rights to object specific access rights */
1605
1606                 se_map_standard(&r->in.access_mask,
1607                                 &printserver_std_mapping);
1608
1609                 /* Deny any object specific bits that don't apply to print
1610                    servers (i.e printer and job specific bits) */
1611
1612                 r->in.access_mask &= SPECIFIC_RIGHTS_MASK;
1613
1614                 if (r->in.access_mask &
1615                     ~(SERVER_ACCESS_ADMINISTER | SERVER_ACCESS_ENUMERATE)) {
1616                         DEBUG(3, ("access DENIED for non-printserver bits\n"));
1617                         close_printer_handle(p, r->out.handle);
1618                         ZERO_STRUCTP(r->out.handle);
1619                         return WERR_ACCESS_DENIED;
1620                 }
1621
1622                 /* Allow admin access */
1623
1624                 if ( r->in.access_mask & SERVER_ACCESS_ADMINISTER )
1625                 {
1626                         SE_PRIV se_printop = SE_PRINT_OPERATOR;
1627
1628                         if (!lp_ms_add_printer_wizard()) {
1629                                 close_printer_handle(p, r->out.handle);
1630                                 ZERO_STRUCTP(r->out.handle);
1631                                 return WERR_ACCESS_DENIED;
1632                         }
1633
1634                         /* if the user is not root, doesn't have SE_PRINT_OPERATOR privilege,
1635                            and not a printer admin, then fail */
1636
1637                         if ((p->server_info->utok.uid != sec_initial_uid()) &&
1638                             !user_has_privileges(p->server_info->ptok,
1639                                                  &se_printop ) &&
1640                             !token_contains_name_in_list(
1641                                     uidtoname(p->server_info->utok.uid),
1642                                     NULL, NULL,
1643                                     p->server_info->ptok,
1644                                     lp_printer_admin(snum))) {
1645                                 close_printer_handle(p, r->out.handle);
1646                                 ZERO_STRUCTP(r->out.handle);
1647                                 return WERR_ACCESS_DENIED;
1648                         }
1649
1650                         r->in.access_mask = SERVER_ACCESS_ADMINISTER;
1651                 }
1652                 else
1653                 {
1654                         r->in.access_mask = SERVER_ACCESS_ENUMERATE;
1655                 }
1656
1657                 DEBUG(4,("Setting print server access = %s\n", (r->in.access_mask == SERVER_ACCESS_ADMINISTER)
1658                         ? "SERVER_ACCESS_ADMINISTER" : "SERVER_ACCESS_ENUMERATE" ));
1659
1660                 /* We fall through to return WERR_OK */
1661                 break;
1662
1663         case SPLHND_PRINTER:
1664                 /* NT doesn't let us connect to a printer if the connecting user
1665                    doesn't have print permission.  */
1666
1667                 if (!get_printer_snum(p, r->out.handle, &snum, NULL)) {
1668                         close_printer_handle(p, r->out.handle);
1669                         ZERO_STRUCTP(r->out.handle);
1670                         return WERR_BADFID;
1671                 }
1672
1673                 if (r->in.access_mask == SEC_FLAG_MAXIMUM_ALLOWED) {
1674                         r->in.access_mask = PRINTER_ACCESS_ADMINISTER;
1675                 }
1676
1677                 se_map_standard(&r->in.access_mask, &printer_std_mapping);
1678
1679                 /* map an empty access mask to the minimum access mask */
1680                 if (r->in.access_mask == 0x0)
1681                         r->in.access_mask = PRINTER_ACCESS_USE;
1682
1683                 /*
1684                  * If we are not serving the printer driver for this printer,
1685                  * map PRINTER_ACCESS_ADMINISTER to PRINTER_ACCESS_USE.  This
1686                  * will keep NT clients happy  --jerry
1687                  */
1688
1689                 if (lp_use_client_driver(snum)
1690                         && (r->in.access_mask & PRINTER_ACCESS_ADMINISTER))
1691                 {
1692                         r->in.access_mask = PRINTER_ACCESS_USE;
1693                 }
1694
1695                 /* check smb.conf parameters and the the sec_desc */
1696
1697                 if ( !check_access(get_client_fd(), lp_hostsallow(snum), lp_hostsdeny(snum)) ) {
1698                         DEBUG(3, ("access DENIED (hosts allow/deny) for printer open\n"));
1699                         ZERO_STRUCTP(r->out.handle);
1700                         return WERR_ACCESS_DENIED;
1701                 }
1702
1703                 if (!user_ok_token(uidtoname(p->server_info->utok.uid), NULL,
1704                                    p->server_info->ptok, snum) ||
1705                     !print_access_check(p->server_info, snum,
1706                                         r->in.access_mask)) {
1707                         DEBUG(3, ("access DENIED for printer open\n"));
1708                         close_printer_handle(p, r->out.handle);
1709                         ZERO_STRUCTP(r->out.handle);
1710                         return WERR_ACCESS_DENIED;
1711                 }
1712
1713                 if ((r->in.access_mask & SPECIFIC_RIGHTS_MASK)& ~(PRINTER_ACCESS_ADMINISTER|PRINTER_ACCESS_USE)) {
1714                         DEBUG(3, ("access DENIED for printer open - unknown bits\n"));
1715                         close_printer_handle(p, r->out.handle);
1716                         ZERO_STRUCTP(r->out.handle);
1717                         return WERR_ACCESS_DENIED;
1718                 }
1719
1720                 if (r->in.access_mask & PRINTER_ACCESS_ADMINISTER)
1721                         r->in.access_mask = PRINTER_ACCESS_ADMINISTER;
1722                 else
1723                         r->in.access_mask = PRINTER_ACCESS_USE;
1724
1725                 DEBUG(4,("Setting printer access = %s\n", (r->in.access_mask == PRINTER_ACCESS_ADMINISTER)
1726                         ? "PRINTER_ACCESS_ADMINISTER" : "PRINTER_ACCESS_USE" ));
1727
1728                 break;
1729
1730         default:
1731                 /* sanity check to prevent programmer error */
1732                 ZERO_STRUCTP(r->out.handle);
1733                 return WERR_BADFID;
1734         }
1735
1736         Printer->access_granted = r->in.access_mask;
1737
1738         /*
1739          * If the client sent a devmode in the OpenPrinter() call, then
1740          * save it here in case we get a job submission on this handle
1741          */
1742
1743          if ((Printer->printer_type != SPLHND_SERVER) &&
1744              r->in.devmode_ctr.devmode) {
1745                 convert_devicemode(Printer->sharename,
1746                                    r->in.devmode_ctr.devmode,
1747                                    &Printer->nt_devmode);
1748          }
1749
1750 #if 0   /* JERRY -- I'm doubtful this is really effective */
1751         /* HACK ALERT!!! Sleep for 1/3 of a second to try trigger a LAN/WAN
1752            optimization in Windows 2000 clients  --jerry */
1753
1754         if ( (r->in.access_mask == PRINTER_ACCESS_ADMINISTER)
1755                 && (RA_WIN2K == get_remote_arch()) )
1756         {
1757                 DEBUG(10,("_spoolss_OpenPrinterEx: Enabling LAN/WAN hack for Win2k clients.\n"));
1758                 sys_usleep( 500000 );
1759         }
1760 #endif
1761
1762         return WERR_OK;
1763 }
1764
1765 /****************************************************************************
1766 ****************************************************************************/
1767
1768 static bool printer_info2_to_nt_printer_info2(struct spoolss_SetPrinterInfo2 *r,
1769                                               NT_PRINTER_INFO_LEVEL_2 *d)
1770 {
1771         DEBUG(7,("printer_info2_to_nt_printer_info2\n"));
1772
1773         if (!r || !d) {
1774                 return false;
1775         }
1776
1777         d->attributes           = r->attributes;
1778         d->priority             = r->priority;
1779         d->default_priority     = r->defaultpriority;
1780         d->starttime            = r->starttime;
1781         d->untiltime            = r->untiltime;
1782         d->status               = r->status;
1783         d->cjobs                = r->cjobs;
1784
1785         fstrcpy(d->servername,  r->servername);
1786         fstrcpy(d->printername, r->printername);
1787         fstrcpy(d->sharename,   r->sharename);
1788         fstrcpy(d->portname,    r->portname);
1789         fstrcpy(d->drivername,  r->drivername);
1790         slprintf(d->comment, sizeof(d->comment)-1, "%s", r->comment);
1791         fstrcpy(d->location,    r->location);
1792         fstrcpy(d->sepfile,     r->sepfile);
1793         fstrcpy(d->printprocessor, r->printprocessor);
1794         fstrcpy(d->datatype,    r->datatype);
1795         fstrcpy(d->parameters,  r->parameters);
1796
1797         return true;
1798 }
1799
1800 /****************************************************************************
1801 ****************************************************************************/
1802
1803 static bool convert_printer_info(struct spoolss_SetPrinterInfoCtr *info_ctr,
1804                                  NT_PRINTER_INFO_LEVEL *printer)
1805 {
1806         bool ret;
1807
1808         switch (info_ctr->level) {
1809         case 2:
1810                 /* allocate memory if needed.  Messy because
1811                    convert_printer_info is used to update an existing
1812                    printer or build a new one */
1813
1814                 if (!printer->info_2) {
1815                         printer->info_2 = TALLOC_ZERO_P(printer, NT_PRINTER_INFO_LEVEL_2);
1816                         if (!printer->info_2) {
1817                                 DEBUG(0,("convert_printer_info: "
1818                                         "talloc() failed!\n"));
1819                                 return false;
1820                         }
1821                 }
1822
1823                 ret = printer_info2_to_nt_printer_info2(info_ctr->info.info2,
1824                                                         printer->info_2);
1825                 printer->info_2->setuptime = time(NULL);
1826                 return ret;
1827         }
1828
1829         return false;
1830 }
1831
1832 /*******************************************************************
1833 ********************************************************************/
1834
1835 static bool string_array_to_fstring_array(const char **sarray, fstring **farray)
1836 {
1837         int i;
1838
1839         if (!sarray) {
1840                 *farray = NULL;
1841                 return true;
1842         }
1843
1844         *farray = SMB_MALLOC_ARRAY(fstring, 1);
1845         if (!*farray) {
1846                 return false;
1847         }
1848
1849         for (i=0; sarray[i] != NULL; i++) {
1850                 *farray = SMB_REALLOC_ARRAY(*farray, fstring, i+2);
1851                 if (!*farray) {
1852                         return false;
1853                 }
1854                 fstrcpy((*farray)[i], sarray[i]);
1855         }
1856
1857         fstrcpy((*farray)[i], "");
1858
1859         return true;
1860 }
1861
1862 /*******************************************************************
1863 ********************************************************************/
1864
1865 static bool driver_info3_to_nt_driver_info3(struct spoolss_AddDriverInfo3 *r,
1866                                             NT_PRINTER_DRIVER_INFO_LEVEL_3 **p)
1867 {
1868         NT_PRINTER_DRIVER_INFO_LEVEL_3 *d;
1869
1870         DEBUG(7,("driver_info3_to_nt_driver_info3: Converting from UNICODE to ASCII\n"));
1871
1872         if (*p == NULL) {
1873                 *p = SMB_MALLOC_P(NT_PRINTER_DRIVER_INFO_LEVEL_3);
1874                 if (*p == NULL) {
1875                         return false;
1876                 }
1877                 ZERO_STRUCTP(*p);
1878         }
1879
1880         d = *p;
1881
1882         d->cversion =                   r->version;
1883
1884         fstrcpy(d->name,                r->driver_name);
1885         fstrcpy(d->environment,         r->architecture);
1886         fstrcpy(d->driverpath,          r->driver_path);
1887         fstrcpy(d->datafile,            r->data_file);
1888         fstrcpy(d->configfile,          r->config_file);
1889         fstrcpy(d->helpfile,            r->help_file);
1890         fstrcpy(d->monitorname,         r->monitor_name);
1891         fstrcpy(d->defaultdatatype,     r->default_datatype);
1892
1893         DEBUGADD(8,( "version:         %d\n", d->cversion));
1894         DEBUGADD(8,( "name:            %s\n", d->name));
1895         DEBUGADD(8,( "environment:     %s\n", d->environment));
1896         DEBUGADD(8,( "driverpath:      %s\n", d->driverpath));
1897         DEBUGADD(8,( "datafile:        %s\n", d->datafile));
1898         DEBUGADD(8,( "configfile:      %s\n", d->configfile));
1899         DEBUGADD(8,( "helpfile:        %s\n", d->helpfile));
1900         DEBUGADD(8,( "monitorname:     %s\n", d->monitorname));
1901         DEBUGADD(8,( "defaultdatatype: %s\n", d->defaultdatatype));
1902
1903         if (r->dependent_files) {
1904                 if (!string_array_to_fstring_array(r->dependent_files->string,
1905                                                    &d->dependentfiles)) {
1906                         SAFE_FREE(*p);
1907                         return false;
1908                 }
1909         }
1910
1911         return true;
1912 }
1913
1914 /*******************************************************************
1915 ********************************************************************/
1916
1917 static bool driver_info6_to_nt_driver_info6(struct spoolss_AddDriverInfo6 *r,
1918                                             NT_PRINTER_DRIVER_INFO_LEVEL_6 **p)
1919 {
1920         NT_PRINTER_DRIVER_INFO_LEVEL_6 *d;
1921
1922         DEBUG(7,("driver_info6_to_nt_driver_info6: Converting from UNICODE to ASCII\n"));
1923
1924         if (*p == NULL) {
1925                 *p = SMB_MALLOC_P(NT_PRINTER_DRIVER_INFO_LEVEL_6);
1926                 if (*p == NULL) {
1927                         return false;
1928                 }
1929                 ZERO_STRUCTP(*p);
1930         }
1931
1932         d = *p;
1933
1934         d->version =                    r->version;
1935
1936         fstrcpy(d->name,                r->driver_name);
1937         fstrcpy(d->environment,         r->architecture);
1938         fstrcpy(d->driverpath,          r->driver_path);
1939         fstrcpy(d->datafile,            r->data_file);
1940         fstrcpy(d->configfile,          r->config_file);
1941         fstrcpy(d->helpfile,            r->help_file);
1942         fstrcpy(d->monitorname,         r->monitor_name);
1943         fstrcpy(d->defaultdatatype,     r->default_datatype);
1944
1945         DEBUGADD(8,( "version:         %d\n", d->version));
1946         DEBUGADD(8,( "name:            %s\n", d->name));
1947         DEBUGADD(8,( "environment:     %s\n", d->environment));
1948         DEBUGADD(8,( "driverpath:      %s\n", d->driverpath));
1949         DEBUGADD(8,( "datafile:        %s\n", d->datafile));
1950         DEBUGADD(8,( "configfile:      %s\n", d->configfile));
1951         DEBUGADD(8,( "helpfile:        %s\n", d->helpfile));
1952         DEBUGADD(8,( "monitorname:     %s\n", d->monitorname));
1953         DEBUGADD(8,( "defaultdatatype: %s\n", d->defaultdatatype));
1954
1955         if (r->dependent_files) {
1956                 if (!string_array_to_fstring_array(r->dependent_files->string,
1957                                                    &d->dependentfiles)) {
1958                         goto error;
1959                 }
1960         }
1961
1962         if (r->previous_names) {
1963                 if (!string_array_to_fstring_array(r->previous_names->string,
1964                                                    &d->previousnames)) {
1965                         goto error;
1966                 }
1967         }
1968
1969         return true;
1970
1971  error:
1972         SAFE_FREE(*p);
1973         return false;
1974 }
1975
1976 /********************************************************************
1977  ********************************************************************/
1978
1979 static bool convert_printer_driver_info(const struct spoolss_AddDriverInfoCtr *r,
1980                                         NT_PRINTER_DRIVER_INFO_LEVEL *printer,
1981                                         uint32_t level)
1982 {
1983         switch (level) {
1984         case 3:
1985                 printer->info_3 = NULL;
1986                 if (!driver_info3_to_nt_driver_info3(r->info.info3, &printer->info_3)) {
1987                         return false;
1988                 }
1989                 break;
1990         case 6:
1991                 printer->info_6 = NULL;
1992                 if (!driver_info6_to_nt_driver_info6(r->info.info6, &printer->info_6)) {
1993                         return false;
1994                 }
1995                 break;
1996         default:
1997                 return false;
1998         }
1999
2000         return true;
2001 }
2002
2003 /********************************************************************
2004  * _spoolss_enddocprinter_internal.
2005  ********************************************************************/
2006
2007 static WERROR _spoolss_enddocprinter_internal(pipes_struct *p,
2008                                               struct policy_handle *handle)
2009 {
2010         Printer_entry *Printer = find_printer_index_by_hnd(p, handle);
2011         int snum;
2012
2013         if (!Printer) {
2014                 DEBUG(2,("_spoolss_enddocprinter_internal: Invalid handle (%s:%u:%u)\n", OUR_HANDLE(handle)));
2015                 return WERR_BADFID;
2016         }
2017
2018         if (!get_printer_snum(p, handle, &snum, NULL))
2019                 return WERR_BADFID;
2020
2021         Printer->document_started = false;
2022         print_job_end(snum, Printer->jobid,NORMAL_CLOSE);
2023         /* error codes unhandled so far ... */
2024
2025         return WERR_OK;
2026 }
2027
2028 /****************************************************************
2029  _spoolss_ClosePrinter
2030 ****************************************************************/
2031
2032 WERROR _spoolss_ClosePrinter(pipes_struct *p,
2033                              struct spoolss_ClosePrinter *r)
2034 {
2035         Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
2036
2037         if (Printer && Printer->document_started)
2038                 _spoolss_enddocprinter_internal(p, r->in.handle);          /* print job was not closed */
2039
2040         if (!close_printer_handle(p, r->in.handle))
2041                 return WERR_BADFID;
2042
2043         /* clear the returned printer handle.  Observed behavior
2044            from Win2k server.  Don't think this really matters.
2045            Previous code just copied the value of the closed
2046            handle.    --jerry */
2047
2048         ZERO_STRUCTP(r->out.handle);
2049
2050         return WERR_OK;
2051 }
2052
2053 /****************************************************************
2054  _spoolss_DeletePrinter
2055 ****************************************************************/
2056
2057 WERROR _spoolss_DeletePrinter(pipes_struct *p,
2058                               struct spoolss_DeletePrinter *r)
2059 {
2060         Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
2061         WERROR result;
2062
2063         if (Printer && Printer->document_started)
2064                 _spoolss_enddocprinter_internal(p, r->in.handle);  /* print job was not closed */
2065
2066         result = delete_printer_handle(p, r->in.handle);
2067
2068         update_c_setprinter(false);
2069
2070         return result;
2071 }
2072
2073 /*******************************************************************
2074  * static function to lookup the version id corresponding to an
2075  * long architecture string
2076  ******************************************************************/
2077
2078 static int get_version_id(const char *arch)
2079 {
2080         int i;
2081         struct print_architecture_table_node archi_table[]= {
2082
2083                 {"Windows 4.0",          "WIN40",       0 },
2084                 {"Windows NT x86",       "W32X86",      2 },
2085                 {"Windows NT R4000",     "W32MIPS",     2 },
2086                 {"Windows NT Alpha_AXP", "W32ALPHA",    2 },
2087                 {"Windows NT PowerPC",   "W32PPC",      2 },
2088                 {"Windows IA64",         "IA64",        3 },
2089                 {"Windows x64",          "x64",         3 },
2090                 {NULL,                   "",            -1 }
2091         };
2092
2093         for (i=0; archi_table[i].long_archi != NULL; i++)
2094         {
2095                 if (strcmp(arch, archi_table[i].long_archi) == 0)
2096                         return (archi_table[i].version);
2097         }
2098
2099         return -1;
2100 }
2101
2102 /****************************************************************
2103  _spoolss_DeletePrinterDriver
2104 ****************************************************************/
2105
2106 WERROR _spoolss_DeletePrinterDriver(pipes_struct *p,
2107                                     struct spoolss_DeletePrinterDriver *r)
2108 {
2109         NT_PRINTER_DRIVER_INFO_LEVEL    info;
2110         NT_PRINTER_DRIVER_INFO_LEVEL    info_win2k;
2111         int                             version;
2112         WERROR                          status;
2113         WERROR                          status_win2k = WERR_ACCESS_DENIED;
2114         SE_PRIV                         se_printop = SE_PRINT_OPERATOR;
2115
2116         /* if the user is not root, doesn't have SE_PRINT_OPERATOR privilege,
2117            and not a printer admin, then fail */
2118
2119         if ( (p->server_info->utok.uid != sec_initial_uid())
2120                 && !user_has_privileges(p->server_info->ptok, &se_printop )
2121                 && !token_contains_name_in_list(
2122                         uidtoname(p->server_info->utok.uid), NULL,
2123                         NULL, p->server_info->ptok,
2124                         lp_printer_admin(-1)) )
2125         {
2126                 return WERR_ACCESS_DENIED;
2127         }
2128
2129         /* check that we have a valid driver name first */
2130
2131         if ((version = get_version_id(r->in.architecture)) == -1)
2132                 return WERR_INVALID_ENVIRONMENT;
2133
2134         ZERO_STRUCT(info);
2135         ZERO_STRUCT(info_win2k);
2136
2137         if (!W_ERROR_IS_OK(get_a_printer_driver(&info, 3, r->in.driver,
2138                                                 r->in.architecture,
2139                                                 version)))
2140         {
2141                 /* try for Win2k driver if "Windows NT x86" */
2142
2143                 if ( version == 2 ) {
2144                         version = 3;
2145                         if (!W_ERROR_IS_OK(get_a_printer_driver(&info, 3,
2146                                                                 r->in.driver,
2147                                                                 r->in.architecture,
2148                                                                 version))) {
2149                                 status = WERR_UNKNOWN_PRINTER_DRIVER;
2150                                 goto done;
2151                         }
2152                 }
2153                 /* otherwise it was a failure */
2154                 else {
2155                         status = WERR_UNKNOWN_PRINTER_DRIVER;
2156                         goto done;
2157                 }
2158
2159         }
2160
2161         if (printer_driver_in_use(info.info_3)) {
2162                 status = WERR_PRINTER_DRIVER_IN_USE;
2163                 goto done;
2164         }
2165
2166         if ( version == 2 )
2167         {
2168                 if (W_ERROR_IS_OK(get_a_printer_driver(&info_win2k, 3,
2169                                                        r->in.driver,
2170                                                        r->in.architecture, 3)))
2171                 {
2172                         /* if we get to here, we now have 2 driver info structures to remove */
2173                         /* remove the Win2k driver first*/
2174
2175                         status_win2k = delete_printer_driver(
2176                                 p, info_win2k.info_3, 3, false);
2177                         free_a_printer_driver( info_win2k, 3 );
2178
2179                         /* this should not have failed---if it did, report to client */
2180                         if ( !W_ERROR_IS_OK(status_win2k) )
2181                         {
2182                                 status = status_win2k;
2183                                 goto done;
2184                         }
2185                 }
2186         }
2187
2188         status = delete_printer_driver(p, info.info_3, version, false);
2189
2190         /* if at least one of the deletes succeeded return OK */
2191
2192         if ( W_ERROR_IS_OK(status) || W_ERROR_IS_OK(status_win2k) )
2193                 status = WERR_OK;
2194
2195 done:
2196         free_a_printer_driver( info, 3 );
2197
2198         return status;
2199 }
2200
2201 /****************************************************************
2202  _spoolss_DeletePrinterDriverEx
2203 ****************************************************************/
2204
2205 WERROR _spoolss_DeletePrinterDriverEx(pipes_struct *p,
2206                                       struct spoolss_DeletePrinterDriverEx *r)
2207 {
2208         NT_PRINTER_DRIVER_INFO_LEVEL    info;
2209         NT_PRINTER_DRIVER_INFO_LEVEL    info_win2k;
2210         int                             version;
2211         bool                            delete_files;
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 != sec_initial_uid())
2220                 && !user_has_privileges(p->server_info->ptok, &se_printop )
2221                 && !token_contains_name_in_list(
2222                         uidtoname(p->server_info->utok.uid), NULL, NULL,
2223                         p->server_info->ptok, lp_printer_admin(-1)) )
2224         {
2225                 return WERR_ACCESS_DENIED;
2226         }
2227
2228         /* check that we have a valid driver name first */
2229         if ((version = get_version_id(r->in.architecture)) == -1) {
2230                 /* this is what NT returns */
2231                 return WERR_INVALID_ENVIRONMENT;
2232         }
2233
2234         if (r->in.delete_flags & DPD_DELETE_SPECIFIC_VERSION)
2235                 version = r->in.version;
2236
2237         ZERO_STRUCT(info);
2238         ZERO_STRUCT(info_win2k);
2239
2240         status = get_a_printer_driver(&info, 3, r->in.driver,
2241                                       r->in.architecture, version);
2242
2243         if ( !W_ERROR_IS_OK(status) )
2244         {
2245                 /*
2246                  * if the client asked for a specific version,
2247                  * or this is something other than Windows NT x86,
2248                  * then we've failed
2249                  */
2250
2251                 if ( (r->in.delete_flags & DPD_DELETE_SPECIFIC_VERSION) || (version !=2) )
2252                         goto done;
2253
2254                 /* try for Win2k driver if "Windows NT x86" */
2255
2256                 version = 3;
2257                 if (!W_ERROR_IS_OK(get_a_printer_driver(&info, 3, r->in.driver,
2258                                                         r->in.architecture,
2259                                                         version))) {
2260                         status = WERR_UNKNOWN_PRINTER_DRIVER;
2261                         goto done;
2262                 }
2263         }
2264
2265         if ( printer_driver_in_use(info.info_3) ) {
2266                 status = WERR_PRINTER_DRIVER_IN_USE;
2267                 goto done;
2268         }
2269
2270         /*
2271          * we have a couple of cases to consider.
2272          * (1) Are any files in use?  If so and DPD_DELTE_ALL_FILE is set,
2273          *     then the delete should fail if **any** files overlap with
2274          *     other drivers
2275          * (2) If DPD_DELTE_UNUSED_FILES is sert, then delete all
2276          *     non-overlapping files
2277          * (3) If neither DPD_DELTE_ALL_FILE nor DPD_DELTE_ALL_FILES
2278          *     is set, the do not delete any files
2279          * Refer to MSDN docs on DeletePrinterDriverEx() for details.
2280          */
2281
2282         delete_files = r->in.delete_flags & (DPD_DELETE_ALL_FILES|DPD_DELETE_UNUSED_FILES);
2283
2284         /* fail if any files are in use and DPD_DELETE_ALL_FILES is set */
2285
2286         if ( delete_files && printer_driver_files_in_use(info.info_3) & (r->in.delete_flags & DPD_DELETE_ALL_FILES) ) {
2287                 /* no idea of the correct error here */
2288                 status = WERR_ACCESS_DENIED;
2289                 goto done;
2290         }
2291
2292
2293         /* also check for W32X86/3 if necessary; maybe we already have? */
2294
2295         if ( (version == 2) && ((r->in.delete_flags & DPD_DELETE_SPECIFIC_VERSION) != DPD_DELETE_SPECIFIC_VERSION)  ) {
2296                 if (W_ERROR_IS_OK(get_a_printer_driver(&info_win2k, 3,
2297                                                        r->in.driver,
2298                                                        r->in.architecture, 3)))
2299                 {
2300
2301                         if ( delete_files && printer_driver_files_in_use(info_win2k.info_3) & (r->in.delete_flags & DPD_DELETE_ALL_FILES) ) {
2302                                 /* no idea of the correct error here */
2303                                 free_a_printer_driver( info_win2k, 3 );
2304                                 status = WERR_ACCESS_DENIED;
2305                                 goto done;
2306                         }
2307
2308                         /* if we get to here, we now have 2 driver info structures to remove */
2309                         /* remove the Win2k driver first*/
2310
2311                         status_win2k = delete_printer_driver(
2312                                 p, info_win2k.info_3, 3, delete_files);
2313                         free_a_printer_driver( info_win2k, 3 );
2314
2315                         /* this should not have failed---if it did, report to client */
2316
2317                         if ( !W_ERROR_IS_OK(status_win2k) )
2318                                 goto done;
2319                 }
2320         }
2321
2322         status = delete_printer_driver(p, info.info_3, version, delete_files);
2323
2324         if ( W_ERROR_IS_OK(status) || W_ERROR_IS_OK(status_win2k) )
2325                 status = WERR_OK;
2326 done:
2327         free_a_printer_driver( info, 3 );
2328
2329         return status;
2330 }
2331
2332
2333 /****************************************************************************
2334  Internal routine for removing printerdata
2335  ***************************************************************************/
2336
2337 static WERROR delete_printer_dataex( NT_PRINTER_INFO_LEVEL *printer, const char *key, const char *value )
2338 {
2339         return delete_printer_data( printer->info_2, key, value );
2340 }
2341
2342 /****************************************************************************
2343  Internal routine for storing printerdata
2344  ***************************************************************************/
2345
2346 WERROR set_printer_dataex(NT_PRINTER_INFO_LEVEL *printer,
2347                           const char *key, const char *value,
2348                           uint32_t type, uint8_t *data, int real_len)
2349 {
2350         /* the registry objects enforce uniqueness based on value name */
2351
2352         return add_printer_data( printer->info_2, key, value, type, data, real_len );
2353 }
2354
2355 /********************************************************************
2356  GetPrinterData on a printer server Handle.
2357 ********************************************************************/
2358
2359 static WERROR getprinterdata_printer_server(TALLOC_CTX *mem_ctx,
2360                                             const char *value,
2361                                             enum winreg_Type *type,
2362                                             union spoolss_PrinterData *data)
2363 {
2364         DEBUG(8,("getprinterdata_printer_server:%s\n", value));
2365
2366         if (!StrCaseCmp(value, "W3SvcInstalled")) {
2367                 *type = REG_DWORD;
2368                 data->value = 0x00;
2369                 return WERR_OK;
2370         }
2371
2372         if (!StrCaseCmp(value, "BeepEnabled")) {
2373                 *type = REG_DWORD;
2374                 data->value = 0x00;
2375                 return WERR_OK;
2376         }
2377
2378         if (!StrCaseCmp(value, "EventLog")) {
2379                 *type = REG_DWORD;
2380                 /* formally was 0x1b */
2381                 data->value = 0x00;
2382                 return WERR_OK;
2383         }
2384
2385         if (!StrCaseCmp(value, "NetPopup")) {
2386                 *type = REG_DWORD;
2387                 data->value = 0x00;
2388                 return WERR_OK;
2389         }
2390
2391         if (!StrCaseCmp(value, "MajorVersion")) {
2392                 *type = REG_DWORD;
2393
2394                 /* Windows NT 4.0 seems to not allow uploading of drivers
2395                    to a server that reports 0x3 as the MajorVersion.
2396                    need to investigate more how Win2k gets around this .
2397                    -- jerry */
2398
2399                 if (RA_WINNT == get_remote_arch()) {
2400                         data->value = 0x02;
2401                 } else {
2402                         data->value = 0x03;
2403                 }
2404
2405                 return WERR_OK;
2406         }
2407
2408         if (!StrCaseCmp(value, "MinorVersion")) {
2409                 *type = REG_DWORD;
2410                 data->value = 0x00;
2411                 return WERR_OK;
2412         }
2413
2414         /* REG_BINARY
2415          *  uint32_t size        = 0x114
2416          *  uint32_t major       = 5
2417          *  uint32_t minor       = [0|1]
2418          *  uint32_t build       = [2195|2600]
2419          *  extra unicode string = e.g. "Service Pack 3"
2420          */
2421         if (!StrCaseCmp(value, "OSVersion")) {
2422                 DATA_BLOB blob;
2423                 enum ndr_err_code ndr_err;
2424                 struct spoolss_OSVersion os;
2425
2426                 os.major                = 5;    /* Windows 2000 == 5.0 */
2427                 os.minor                = 0;
2428                 os.build                = 2195; /* build */
2429                 os.extra_string         = "";   /* leave extra string empty */
2430
2431                 ndr_err = ndr_push_struct_blob(&blob, mem_ctx, NULL, &os,
2432                         (ndr_push_flags_fn_t)ndr_push_spoolss_OSVersion);
2433                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2434                         return WERR_GENERAL_FAILURE;
2435                 }
2436
2437                 *type = REG_BINARY;
2438                 data->binary = blob;
2439
2440                 return WERR_OK;
2441         }
2442
2443
2444         if (!StrCaseCmp(value, "DefaultSpoolDirectory")) {
2445                 *type = REG_SZ;
2446
2447                 data->string = talloc_strdup(mem_ctx, "C:\\PRINTERS");
2448                 W_ERROR_HAVE_NO_MEMORY(data->string);
2449
2450                 return WERR_OK;
2451         }
2452
2453         if (!StrCaseCmp(value, "Architecture")) {
2454                 *type = REG_SZ;
2455
2456                 data->string = talloc_strdup(mem_ctx, "Windows NT x86");
2457                 W_ERROR_HAVE_NO_MEMORY(data->string);
2458
2459                 return WERR_OK;
2460         }
2461
2462         if (!StrCaseCmp(value, "DsPresent")) {
2463                 *type = REG_DWORD;
2464
2465                 /* only show the publish check box if we are a
2466                    member of a AD domain */
2467
2468                 if (lp_security() == SEC_ADS) {
2469                         data->value = 0x01;
2470                 } else {
2471                         data->value = 0x00;
2472                 }
2473                 return WERR_OK;
2474         }
2475
2476         if (!StrCaseCmp(value, "DNSMachineName")) {
2477                 const char *hostname = get_mydnsfullname();
2478
2479                 if (!hostname) {
2480                         return WERR_BADFILE;
2481                 }
2482
2483                 *type = REG_SZ;
2484                 data->string = talloc_strdup(mem_ctx, hostname);
2485                 W_ERROR_HAVE_NO_MEMORY(data->string);
2486
2487                 return WERR_OK;
2488         }
2489
2490         return WERR_INVALID_PARAM;
2491 }
2492
2493 /****************************************************************
2494  _spoolss_GetPrinterData
2495 ****************************************************************/
2496
2497 WERROR _spoolss_GetPrinterData(pipes_struct *p,
2498                                struct spoolss_GetPrinterData *r)
2499 {
2500         WERROR result;
2501         Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
2502         NT_PRINTER_INFO_LEVEL *printer = NULL;
2503         int snum = 0;
2504
2505         /*
2506          * Reminder: when it's a string, the length is in BYTES
2507          * even if UNICODE is negociated.
2508          *
2509          * JFM, 4/19/1999
2510          */
2511
2512         /* in case of problem, return some default values */
2513
2514         *r->out.needed  = 0;
2515         *r->out.type    = 0;
2516
2517         DEBUG(4,("_spoolss_GetPrinterData\n"));
2518
2519         if (!Printer) {
2520                 DEBUG(2,("_spoolss_GetPrinterData: Invalid handle (%s:%u:%u).\n",
2521                         OUR_HANDLE(r->in.handle)));
2522                 result = WERR_BADFID;
2523                 goto done;
2524         }
2525
2526         if (Printer->printer_type == SPLHND_SERVER) {
2527                 result = getprinterdata_printer_server(p->mem_ctx,
2528                                                        r->in.value_name,
2529                                                        r->out.type,
2530                                                        r->out.data);
2531         } else {
2532                 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
2533                         result = WERR_BADFID;
2534                         goto done;
2535                 }
2536
2537                 result = get_a_printer(Printer, &printer, 2, lp_servicename(snum));
2538                 if (!W_ERROR_IS_OK(result)) {
2539                         goto done;
2540                 }
2541
2542                 /* XP sends this and wants to change id value from the PRINTER_INFO_0 */
2543
2544                 if (strequal(r->in.value_name, "ChangeId")) {
2545                         *r->out.type = REG_DWORD;
2546                         r->out.data->value = printer->info_2->changeid;
2547                         result = WERR_OK;
2548                 } else {
2549                         REGISTRY_VALUE *v;
2550                         DATA_BLOB blob;
2551
2552                         v = get_printer_data(printer->info_2,
2553                                              SPOOL_PRINTERDATA_KEY,
2554                                              r->in.value_name);
2555                         if (!v) {
2556                                 result = WERR_BADFILE;
2557                                 goto done;
2558                         }
2559
2560                         *r->out.type = v->type;
2561
2562                         blob = data_blob_const(v->data_p, v->size);
2563
2564                         result = pull_spoolss_PrinterData(p->mem_ctx, &blob,
2565                                                           r->out.data,
2566                                                           *r->out.type);
2567                 }
2568         }
2569
2570  done:
2571         /* cleanup & exit */
2572
2573         if (printer) {
2574                 free_a_printer(&printer, 2);
2575         }
2576
2577         if (!W_ERROR_IS_OK(result)) {
2578                 return result;
2579         }
2580
2581         *r->out.needed  = ndr_size_spoolss_PrinterData(r->out.data, *r->out.type, NULL, 0);
2582         *r->out.type    = SPOOLSS_BUFFER_OK(*r->out.type, REG_NONE);
2583         r->out.data     = SPOOLSS_BUFFER_OK(r->out.data, r->out.data);
2584
2585         return SPOOLSS_BUFFER_OK(WERR_OK, WERR_MORE_DATA);
2586 }
2587
2588 /*********************************************************
2589  Connect to the client machine.
2590 **********************************************************/
2591
2592 static bool spoolss_connect_to_client(struct rpc_pipe_client **pp_pipe,
2593                         struct sockaddr_storage *client_ss, const char *remote_machine)
2594 {
2595         NTSTATUS ret;
2596         struct cli_state *the_cli;
2597         struct sockaddr_storage rm_addr;
2598
2599         if ( is_zero_addr((struct sockaddr *)client_ss) ) {
2600                 if ( !resolve_name( remote_machine, &rm_addr, 0x20) ) {
2601                         DEBUG(2,("spoolss_connect_to_client: Can't resolve address for %s\n", remote_machine));
2602                         return false;
2603                 }
2604
2605                 if (ismyaddr((struct sockaddr *)&rm_addr)) {
2606                         DEBUG(0,("spoolss_connect_to_client: Machine %s is one of our addresses. Cannot add to ourselves.\n", remote_machine));
2607                         return false;
2608                 }
2609         } else {
2610                 char addr[INET6_ADDRSTRLEN];
2611                 rm_addr = *client_ss;
2612                 print_sockaddr(addr, sizeof(addr), &rm_addr);
2613                 DEBUG(5,("spoolss_connect_to_client: Using address %s (no name resolution necessary)\n",
2614                         addr));
2615         }
2616
2617         /* setup the connection */
2618
2619         ret = cli_full_connection( &the_cli, global_myname(), remote_machine,
2620                 &rm_addr, 0, "IPC$", "IPC",
2621                 "", /* username */
2622                 "", /* domain */
2623                 "", /* password */
2624                 0, lp_client_signing(), NULL );
2625
2626         if ( !NT_STATUS_IS_OK( ret ) ) {
2627                 DEBUG(2,("spoolss_connect_to_client: connection to [%s] failed!\n",
2628                         remote_machine ));
2629                 return false;
2630         }
2631
2632         if ( the_cli->protocol != PROTOCOL_NT1 ) {
2633                 DEBUG(0,("spoolss_connect_to_client: machine %s didn't negotiate NT protocol.\n", remote_machine));
2634                 cli_shutdown(the_cli);
2635                 return false;
2636         }
2637
2638         /*
2639          * Ok - we have an anonymous connection to the IPC$ share.
2640          * Now start the NT Domain stuff :-).
2641          */
2642
2643         ret = cli_rpc_pipe_open_noauth(the_cli, &ndr_table_spoolss.syntax_id, pp_pipe);
2644         if (!NT_STATUS_IS_OK(ret)) {
2645                 DEBUG(2,("spoolss_connect_to_client: unable to open the spoolss pipe on machine %s. Error was : %s.\n",
2646                         remote_machine, nt_errstr(ret)));
2647                 cli_shutdown(the_cli);
2648                 return false;
2649         }
2650
2651         return true;
2652 }
2653
2654 /***************************************************************************
2655  Connect to the client.
2656 ****************************************************************************/
2657
2658 static bool srv_spoolss_replyopenprinter(int snum, const char *printer,
2659                                         uint32_t localprinter, uint32_t type,
2660                                         struct policy_handle *handle,
2661                                         struct sockaddr_storage *client_ss)
2662 {
2663         WERROR result;
2664         NTSTATUS status;
2665
2666         /*
2667          * If it's the first connection, contact the client
2668          * and connect to the IPC$ share anonymously
2669          */
2670         if (smb_connections==0) {
2671                 fstring unix_printer;
2672
2673                 fstrcpy(unix_printer, printer+2); /* the +2 is to strip the leading 2 backslashs */
2674
2675                 if ( !spoolss_connect_to_client( &notify_cli_pipe, client_ss, unix_printer ))
2676                         return false;
2677
2678                 messaging_register(smbd_messaging_context(), NULL,
2679                                    MSG_PRINTER_NOTIFY2,
2680                                    receive_notify2_message_list);
2681                 /* Tell the connections db we're now interested in printer
2682                  * notify messages. */
2683                 register_message_flags(true, FLAG_MSG_PRINT_NOTIFY);
2684         }
2685
2686         /*
2687          * Tell the specific printing tdb we want messages for this printer
2688          * by registering our PID.
2689          */
2690
2691         if (!print_notify_register_pid(snum))
2692                 DEBUG(0,("print_notify_register_pid: Failed to register our pid for printer %s\n", printer ));
2693
2694         smb_connections++;
2695
2696         status = rpccli_spoolss_ReplyOpenPrinter(notify_cli_pipe, talloc_tos(),
2697                                                  printer,
2698                                                  localprinter,
2699                                                  type,
2700                                                  0,
2701                                                  NULL,
2702                                                  handle,
2703                                                  &result);
2704         if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result))
2705                 DEBUG(5,("srv_spoolss_reply_open_printer: Client RPC returned [%s]\n",
2706                         win_errstr(result)));
2707
2708         return (W_ERROR_IS_OK(result));
2709 }
2710
2711 /****************************************************************
2712  ****************************************************************/
2713
2714 static struct spoolss_NotifyOption *dup_spoolss_NotifyOption(TALLOC_CTX *mem_ctx,
2715                                                              const struct spoolss_NotifyOption *r)
2716 {
2717         struct spoolss_NotifyOption *option;
2718         uint32_t i,k;
2719
2720         if (!r) {
2721                 return NULL;
2722         }
2723
2724         option = talloc_zero(mem_ctx, struct spoolss_NotifyOption);
2725         if (!option) {
2726                 return NULL;
2727         }
2728
2729         *option = *r;
2730
2731         if (!option->count) {
2732                 return option;
2733         }
2734
2735         option->types = talloc_zero_array(option,
2736                 struct spoolss_NotifyOptionType, option->count);
2737         if (!option->types) {
2738                 talloc_free(option);
2739                 return NULL;
2740         }
2741
2742         for (i=0; i < option->count; i++) {
2743                 option->types[i] = r->types[i];
2744
2745                 if (option->types[i].count) {
2746                         option->types[i].fields = talloc_zero_array(option,
2747                                 union spoolss_Field, option->types[i].count);
2748                         if (!option->types[i].fields) {
2749                                 talloc_free(option);
2750                                 return NULL;
2751                         }
2752                         for (k=0; k<option->types[i].count; k++) {
2753                                 option->types[i].fields[k] =
2754                                         r->types[i].fields[k];
2755                         }
2756                 }
2757         }
2758
2759         return option;
2760 }
2761
2762 /****************************************************************
2763  * _spoolss_RemoteFindFirstPrinterChangeNotifyEx
2764  *
2765  * before replying OK: status=0 a rpc call is made to the workstation
2766  * asking ReplyOpenPrinter
2767  *
2768  * in fact ReplyOpenPrinter is the changenotify equivalent on the spoolss pipe
2769  * called from api_spoolss_rffpcnex
2770 ****************************************************************/
2771
2772 WERROR _spoolss_RemoteFindFirstPrinterChangeNotifyEx(pipes_struct *p,
2773                                                      struct spoolss_RemoteFindFirstPrinterChangeNotifyEx *r)
2774 {
2775         int snum = -1;
2776         struct spoolss_NotifyOption *option = r->in.notify_options;
2777         struct sockaddr_storage client_ss;
2778
2779         /* store the notify value in the printer struct */
2780
2781         Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
2782
2783         if (!Printer) {
2784                 DEBUG(2,("_spoolss_RemoteFindFirstPrinterChangeNotifyEx: "
2785                         "Invalid handle (%s:%u:%u).\n",
2786                         OUR_HANDLE(r->in.handle)));
2787                 return WERR_BADFID;
2788         }
2789
2790         Printer->notify.flags           = r->in.flags;
2791         Printer->notify.options         = r->in.options;
2792         Printer->notify.printerlocal    = r->in.printer_local;
2793
2794         TALLOC_FREE(Printer->notify.option);
2795         Printer->notify.option = dup_spoolss_NotifyOption(Printer, option);
2796
2797         fstrcpy(Printer->notify.localmachine, r->in.local_machine);
2798
2799         /* Connect to the client machine and send a ReplyOpenPrinter */
2800
2801         if ( Printer->printer_type == SPLHND_SERVER)
2802                 snum = -1;
2803         else if ( (Printer->printer_type == SPLHND_PRINTER) &&
2804                         !get_printer_snum(p, r->in.handle, &snum, NULL) )
2805                 return WERR_BADFID;
2806
2807         if (!interpret_string_addr(&client_ss, p->client_address,
2808                                    AI_NUMERICHOST)) {
2809                 return WERR_SERVER_UNAVAILABLE;
2810         }
2811
2812         if(!srv_spoolss_replyopenprinter(snum, Printer->notify.localmachine,
2813                                         Printer->notify.printerlocal, 1,
2814                                         &Printer->notify.client_hnd, &client_ss))
2815                 return WERR_SERVER_UNAVAILABLE;
2816
2817         Printer->notify.client_connected = true;
2818
2819         return WERR_OK;
2820 }
2821
2822 /*******************************************************************
2823  * fill a notify_info_data with the servername
2824  ********************************************************************/
2825
2826 void spoolss_notify_server_name(int snum,
2827                                        struct spoolss_Notify *data,
2828                                        print_queue_struct *queue,
2829                                        NT_PRINTER_INFO_LEVEL *printer,
2830                                        TALLOC_CTX *mem_ctx)
2831 {
2832         SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, printer->info_2->servername);
2833 }
2834
2835 /*******************************************************************
2836  * fill a notify_info_data with the printername (not including the servername).
2837  ********************************************************************/
2838
2839 void spoolss_notify_printer_name(int snum,
2840                                         struct spoolss_Notify *data,
2841                                         print_queue_struct *queue,
2842                                         NT_PRINTER_INFO_LEVEL *printer,
2843                                         TALLOC_CTX *mem_ctx)
2844 {
2845         /* the notify name should not contain the \\server\ part */
2846         char *p = strrchr(printer->info_2->printername, '\\');
2847
2848         if (!p) {
2849                 p = printer->info_2->printername;
2850         } else {
2851                 p++;
2852         }
2853
2854         SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, p);
2855 }
2856
2857 /*******************************************************************
2858  * fill a notify_info_data with the servicename
2859  ********************************************************************/
2860
2861 void spoolss_notify_share_name(int snum,
2862                                       struct spoolss_Notify *data,
2863                                       print_queue_struct *queue,
2864                                       NT_PRINTER_INFO_LEVEL *printer,
2865                                       TALLOC_CTX *mem_ctx)
2866 {
2867         SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, lp_servicename(snum));
2868 }
2869
2870 /*******************************************************************
2871  * fill a notify_info_data with the port name
2872  ********************************************************************/
2873
2874 void spoolss_notify_port_name(int snum,
2875                                      struct spoolss_Notify *data,
2876                                      print_queue_struct *queue,
2877                                      NT_PRINTER_INFO_LEVEL *printer,
2878                                      TALLOC_CTX *mem_ctx)
2879 {
2880         SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, printer->info_2->portname);
2881 }
2882
2883 /*******************************************************************
2884  * fill a notify_info_data with the printername
2885  * but it doesn't exist, have to see what to do
2886  ********************************************************************/
2887
2888 void spoolss_notify_driver_name(int snum,
2889                                        struct spoolss_Notify *data,
2890                                        print_queue_struct *queue,
2891                                        NT_PRINTER_INFO_LEVEL *printer,
2892                                        TALLOC_CTX *mem_ctx)
2893 {
2894         SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, printer->info_2->drivername);
2895 }
2896
2897 /*******************************************************************
2898  * fill a notify_info_data with the comment
2899  ********************************************************************/
2900
2901 void spoolss_notify_comment(int snum,
2902                                    struct spoolss_Notify *data,
2903                                    print_queue_struct *queue,
2904                                    NT_PRINTER_INFO_LEVEL *printer,
2905                                    TALLOC_CTX *mem_ctx)
2906 {
2907         char *p;
2908
2909         if (*printer->info_2->comment == '\0') {
2910                 p = lp_comment(snum);
2911         } else {
2912                 p = printer->info_2->comment;
2913         }
2914
2915         SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, printer->info_2->comment);
2916 }
2917
2918 /*******************************************************************
2919  * fill a notify_info_data with the comment
2920  * location = "Room 1, floor 2, building 3"
2921  ********************************************************************/
2922
2923 void spoolss_notify_location(int snum,
2924                                     struct spoolss_Notify *data,
2925                                     print_queue_struct *queue,
2926                                     NT_PRINTER_INFO_LEVEL *printer,
2927                                     TALLOC_CTX *mem_ctx)
2928 {
2929         SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, printer->info_2->location);
2930 }
2931
2932 /*******************************************************************
2933  * fill a notify_info_data with the device mode
2934  * jfm:xxxx don't to it for know but that's a real problem !!!
2935  ********************************************************************/
2936
2937 static void spoolss_notify_devmode(int snum,
2938                                    struct spoolss_Notify *data,
2939                                    print_queue_struct *queue,
2940                                    NT_PRINTER_INFO_LEVEL *printer,
2941                                    TALLOC_CTX *mem_ctx)
2942 {
2943         /* for a dummy implementation we have to zero the fields */
2944         SETUP_SPOOLSS_NOTIFY_DATA_DEVMODE(data, NULL);
2945 }
2946
2947 /*******************************************************************
2948  * fill a notify_info_data with the separator file name
2949  ********************************************************************/
2950
2951 void spoolss_notify_sepfile(int snum,
2952                                    struct spoolss_Notify *data,
2953                                    print_queue_struct *queue,
2954                                    NT_PRINTER_INFO_LEVEL *printer,
2955                                    TALLOC_CTX *mem_ctx)
2956 {
2957         SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, printer->info_2->sepfile);
2958 }
2959
2960 /*******************************************************************
2961  * fill a notify_info_data with the print processor
2962  * jfm:xxxx return always winprint to indicate we don't do anything to it
2963  ********************************************************************/
2964
2965 void spoolss_notify_print_processor(int snum,
2966                                            struct spoolss_Notify *data,
2967                                            print_queue_struct *queue,
2968                                            NT_PRINTER_INFO_LEVEL *printer,
2969                                            TALLOC_CTX *mem_ctx)
2970 {
2971         SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, printer->info_2->printprocessor);
2972 }
2973
2974 /*******************************************************************
2975  * fill a notify_info_data with the print processor options
2976  * jfm:xxxx send an empty string
2977  ********************************************************************/
2978
2979 void spoolss_notify_parameters(int snum,
2980                                       struct spoolss_Notify *data,
2981                                       print_queue_struct *queue,
2982                                       NT_PRINTER_INFO_LEVEL *printer,
2983                                       TALLOC_CTX *mem_ctx)
2984 {
2985         SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, printer->info_2->parameters);
2986 }
2987
2988 /*******************************************************************
2989  * fill a notify_info_data with the data type
2990  * jfm:xxxx always send RAW as data type
2991  ********************************************************************/
2992
2993 void spoolss_notify_datatype(int snum,
2994                                     struct spoolss_Notify *data,
2995                                     print_queue_struct *queue,
2996                                     NT_PRINTER_INFO_LEVEL *printer,
2997                                     TALLOC_CTX *mem_ctx)
2998 {
2999         SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, printer->info_2->datatype);
3000 }
3001
3002 /*******************************************************************
3003  * fill a notify_info_data with the security descriptor
3004  * jfm:xxxx send an null pointer to say no security desc
3005  * have to implement security before !
3006  ********************************************************************/
3007
3008 static void spoolss_notify_security_desc(int snum,
3009                                          struct spoolss_Notify *data,
3010                                          print_queue_struct *queue,
3011                                          NT_PRINTER_INFO_LEVEL *printer,
3012                                          TALLOC_CTX *mem_ctx)
3013 {
3014         SETUP_SPOOLSS_NOTIFY_DATA_SECDESC(data,
3015                                           printer->info_2->secdesc_buf->sd_size,
3016                                           printer->info_2->secdesc_buf->sd);
3017 }
3018
3019 /*******************************************************************
3020  * fill a notify_info_data with the attributes
3021  * jfm:xxxx a samba printer is always shared
3022  ********************************************************************/
3023
3024 void spoolss_notify_attributes(int snum,
3025                                       struct spoolss_Notify *data,
3026                                       print_queue_struct *queue,
3027                                       NT_PRINTER_INFO_LEVEL *printer,
3028                                       TALLOC_CTX *mem_ctx)
3029 {
3030         SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, printer->info_2->attributes);
3031 }
3032
3033 /*******************************************************************
3034  * fill a notify_info_data with the priority
3035  ********************************************************************/
3036
3037 static void spoolss_notify_priority(int snum,
3038                                     struct spoolss_Notify *data,
3039                                     print_queue_struct *queue,
3040                                     NT_PRINTER_INFO_LEVEL *printer,
3041                                     TALLOC_CTX *mem_ctx)
3042 {
3043         SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, printer->info_2->priority);
3044 }
3045
3046 /*******************************************************************
3047  * fill a notify_info_data with the default priority
3048  ********************************************************************/
3049
3050 static void spoolss_notify_default_priority(int snum,
3051                                             struct spoolss_Notify *data,
3052                                             print_queue_struct *queue,
3053                                             NT_PRINTER_INFO_LEVEL *printer,
3054                                             TALLOC_CTX *mem_ctx)
3055 {
3056         SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, printer->info_2->default_priority);
3057 }
3058
3059 /*******************************************************************
3060  * fill a notify_info_data with the start time
3061  ********************************************************************/
3062
3063 static void spoolss_notify_start_time(int snum,
3064                                       struct spoolss_Notify *data,
3065                                       print_queue_struct *queue,
3066                                       NT_PRINTER_INFO_LEVEL *printer,
3067                                       TALLOC_CTX *mem_ctx)
3068 {
3069         SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, printer->info_2->starttime);
3070 }
3071
3072 /*******************************************************************
3073  * fill a notify_info_data with the until time
3074  ********************************************************************/
3075
3076 static void spoolss_notify_until_time(int snum,
3077                                       struct spoolss_Notify *data,
3078                                       print_queue_struct *queue,
3079                                       NT_PRINTER_INFO_LEVEL *printer,
3080                                       TALLOC_CTX *mem_ctx)
3081 {
3082         SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, printer->info_2->untiltime);
3083 }
3084
3085 /*******************************************************************
3086  * fill a notify_info_data with the status
3087  ********************************************************************/
3088
3089 static void spoolss_notify_status(int snum,
3090                                   struct spoolss_Notify *data,
3091                                   print_queue_struct *queue,
3092                                   NT_PRINTER_INFO_LEVEL *printer,
3093                                   TALLOC_CTX *mem_ctx)
3094 {
3095         print_status_struct status;
3096
3097         print_queue_length(snum, &status);
3098         SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, status.status);
3099 }
3100
3101 /*******************************************************************
3102  * fill a notify_info_data with the number of jobs queued
3103  ********************************************************************/
3104
3105 void spoolss_notify_cjobs(int snum,
3106                                  struct spoolss_Notify *data,
3107                                  print_queue_struct *queue,
3108                                  NT_PRINTER_INFO_LEVEL *printer,
3109                                  TALLOC_CTX *mem_ctx)
3110 {
3111         SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, print_queue_length(snum, NULL));
3112 }
3113
3114 /*******************************************************************
3115  * fill a notify_info_data with the average ppm
3116  ********************************************************************/
3117
3118 static void spoolss_notify_average_ppm(int snum,
3119                                        struct spoolss_Notify *data,
3120                                        print_queue_struct *queue,
3121                                        NT_PRINTER_INFO_LEVEL *printer,
3122                                        TALLOC_CTX *mem_ctx)
3123 {
3124         /* always respond 8 pages per minutes */
3125         /* a little hard ! */
3126         SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, printer->info_2->averageppm);
3127 }
3128
3129 /*******************************************************************
3130  * fill a notify_info_data with username
3131  ********************************************************************/
3132
3133 static void spoolss_notify_username(int snum,
3134                                     struct spoolss_Notify *data,
3135                                     print_queue_struct *queue,
3136                                     NT_PRINTER_INFO_LEVEL *printer,
3137                                     TALLOC_CTX *mem_ctx)
3138 {
3139         SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, queue->fs_user);
3140 }
3141
3142 /*******************************************************************
3143  * fill a notify_info_data with job status
3144  ********************************************************************/
3145
3146 static void spoolss_notify_job_status(int snum,
3147                                       struct spoolss_Notify *data,
3148                                       print_queue_struct *queue,
3149                                       NT_PRINTER_INFO_LEVEL *printer,
3150                                       TALLOC_CTX *mem_ctx)
3151 {
3152         SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, nt_printj_status(queue->status));
3153 }
3154
3155 /*******************************************************************
3156  * fill a notify_info_data with job name
3157  ********************************************************************/
3158
3159 static void spoolss_notify_job_name(int snum,
3160                                     struct spoolss_Notify *data,
3161                                     print_queue_struct *queue,
3162                                     NT_PRINTER_INFO_LEVEL *printer,
3163                                     TALLOC_CTX *mem_ctx)
3164 {
3165         SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, queue->fs_file);
3166 }
3167
3168 /*******************************************************************
3169  * fill a notify_info_data with job status
3170  ********************************************************************/
3171
3172 static void spoolss_notify_job_status_string(int snum,
3173                                              struct spoolss_Notify *data,
3174                                              print_queue_struct *queue,
3175                                              NT_PRINTER_INFO_LEVEL *printer,
3176                                              TALLOC_CTX *mem_ctx)
3177 {
3178         /*
3179          * Now we're returning job status codes we just return a "" here. JRA.
3180          */
3181
3182         const char *p = "";
3183
3184 #if 0 /* NO LONGER NEEDED - JRA. 02/22/2001 */
3185         p = "unknown";
3186
3187         switch (queue->status) {
3188         case LPQ_QUEUED:
3189                 p = "Queued";
3190                 break;
3191         case LPQ_PAUSED:
3192                 p = "";    /* NT provides the paused string */
3193                 break;
3194         case LPQ_SPOOLING:
3195                 p = "Spooling";
3196                 break;
3197         case LPQ_PRINTING:
3198                 p = "Printing";
3199                 break;
3200         }
3201 #endif /* NO LONGER NEEDED. */
3202
3203         SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, p);
3204 }
3205
3206 /*******************************************************************
3207  * fill a notify_info_data with job time
3208  ********************************************************************/
3209
3210 static void spoolss_notify_job_time(int snum,
3211                                     struct spoolss_Notify *data,
3212                                     print_queue_struct *queue,
3213                                     NT_PRINTER_INFO_LEVEL *printer,
3214                                     TALLOC_CTX *mem_ctx)
3215 {
3216         SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, 0);
3217 }
3218
3219 /*******************************************************************
3220  * fill a notify_info_data with job size
3221  ********************************************************************/
3222
3223 static void spoolss_notify_job_size(int snum,
3224                                     struct spoolss_Notify *data,
3225                                     print_queue_struct *queue,
3226                                     NT_PRINTER_INFO_LEVEL *printer,
3227                                     TALLOC_CTX *mem_ctx)
3228 {
3229         SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, queue->size);
3230 }
3231
3232 /*******************************************************************
3233  * fill a notify_info_data with page info
3234  ********************************************************************/
3235 static void spoolss_notify_total_pages(int snum,
3236                                 struct spoolss_Notify *data,
3237                                 print_queue_struct *queue,
3238                                 NT_PRINTER_INFO_LEVEL *printer,
3239                                 TALLOC_CTX *mem_ctx)
3240 {
3241         SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, queue->page_count);
3242 }
3243
3244 /*******************************************************************
3245  * fill a notify_info_data with pages printed info.
3246  ********************************************************************/
3247 static void spoolss_notify_pages_printed(int snum,
3248                                 struct spoolss_Notify *data,
3249                                 print_queue_struct *queue,
3250                                 NT_PRINTER_INFO_LEVEL *printer,
3251                                 TALLOC_CTX *mem_ctx)
3252 {
3253         /* Add code when back-end tracks this */
3254         SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, 0);
3255 }
3256
3257 /*******************************************************************
3258  Fill a notify_info_data with job position.
3259  ********************************************************************/
3260
3261 static void spoolss_notify_job_position(int snum,
3262                                         struct spoolss_Notify *data,
3263                                         print_queue_struct *queue,
3264                                         NT_PRINTER_INFO_LEVEL *printer,
3265                                         TALLOC_CTX *mem_ctx)
3266 {
3267         SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, queue->job);
3268 }
3269
3270 /*******************************************************************
3271  Fill a notify_info_data with submitted time.
3272  ********************************************************************/
3273
3274 static void spoolss_notify_submitted_time(int snum,
3275                                           struct spoolss_Notify *data,
3276                                           print_queue_struct *queue,
3277                                           NT_PRINTER_INFO_LEVEL *printer,
3278                                           TALLOC_CTX *mem_ctx)
3279 {
3280         data->data.string.string = NULL;
3281         data->data.string.size = 0;
3282
3283         init_systemtime_buffer(mem_ctx, gmtime(&queue->time),
3284                                &data->data.string.string,
3285                                &data->data.string.size);
3286
3287 }
3288
3289 struct s_notify_info_data_table
3290 {
3291         enum spoolss_NotifyType type;
3292         uint16_t field;
3293         const char *name;
3294         enum spoolss_NotifyTable variable_type;
3295         void (*fn) (int snum, struct spoolss_Notify *data,
3296                     print_queue_struct *queue,
3297                     NT_PRINTER_INFO_LEVEL *printer, TALLOC_CTX *mem_ctx);
3298 };
3299
3300 /* A table describing the various print notification constants and
3301    whether the notification data is a pointer to a variable sized
3302    buffer, a one value uint32_t or a two value uint32_t. */
3303
3304 static const struct s_notify_info_data_table notify_info_data_table[] =
3305 {
3306 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_SERVER_NAME,         "PRINTER_NOTIFY_FIELD_SERVER_NAME",         NOTIFY_TABLE_STRING,   spoolss_notify_server_name },
3307 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_PRINTER_NAME,        "PRINTER_NOTIFY_FIELD_PRINTER_NAME",        NOTIFY_TABLE_STRING,   spoolss_notify_printer_name },
3308 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_SHARE_NAME,          "PRINTER_NOTIFY_FIELD_SHARE_NAME",          NOTIFY_TABLE_STRING,   spoolss_notify_share_name },
3309 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_PORT_NAME,           "PRINTER_NOTIFY_FIELD_PORT_NAME",           NOTIFY_TABLE_STRING,   spoolss_notify_port_name },
3310 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_DRIVER_NAME,         "PRINTER_NOTIFY_FIELD_DRIVER_NAME",         NOTIFY_TABLE_STRING,   spoolss_notify_driver_name },
3311 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_COMMENT,             "PRINTER_NOTIFY_FIELD_COMMENT",             NOTIFY_TABLE_STRING,   spoolss_notify_comment },
3312 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_LOCATION,            "PRINTER_NOTIFY_FIELD_LOCATION",            NOTIFY_TABLE_STRING,   spoolss_notify_location },
3313 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_DEVMODE,             "PRINTER_NOTIFY_FIELD_DEVMODE",             NOTIFY_TABLE_DEVMODE,  spoolss_notify_devmode },
3314 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_SEPFILE,             "PRINTER_NOTIFY_FIELD_SEPFILE",             NOTIFY_TABLE_STRING,   spoolss_notify_sepfile },
3315 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_PRINT_PROCESSOR,     "PRINTER_NOTIFY_FIELD_PRINT_PROCESSOR",     NOTIFY_TABLE_STRING,   spoolss_notify_print_processor },
3316 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_PARAMETERS,          "PRINTER_NOTIFY_FIELD_PARAMETERS",          NOTIFY_TABLE_STRING,   spoolss_notify_parameters },
3317 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_DATATYPE,            "PRINTER_NOTIFY_FIELD_DATATYPE",            NOTIFY_TABLE_STRING,   spoolss_notify_datatype },
3318 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_SECURITY_DESCRIPTOR, "PRINTER_NOTIFY_FIELD_SECURITY_DESCRIPTOR", NOTIFY_TABLE_SECURITYDESCRIPTOR,   spoolss_notify_security_desc },
3319 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_ATTRIBUTES,          "PRINTER_NOTIFY_FIELD_ATTRIBUTES",          NOTIFY_TABLE_DWORD,    spoolss_notify_attributes },
3320 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_PRIORITY,            "PRINTER_NOTIFY_FIELD_PRIORITY",            NOTIFY_TABLE_DWORD,    spoolss_notify_priority },
3321 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_DEFAULT_PRIORITY,    "PRINTER_NOTIFY_FIELD_DEFAULT_PRIORITY",    NOTIFY_TABLE_DWORD,    spoolss_notify_default_priority },
3322 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_START_TIME,          "PRINTER_NOTIFY_FIELD_START_TIME",          NOTIFY_TABLE_DWORD,    spoolss_notify_start_time },
3323 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_UNTIL_TIME,          "PRINTER_NOTIFY_FIELD_UNTIL_TIME",          NOTIFY_TABLE_DWORD,    spoolss_notify_until_time },
3324 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_STATUS,              "PRINTER_NOTIFY_FIELD_STATUS",              NOTIFY_TABLE_DWORD,    spoolss_notify_status },
3325 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_STATUS_STRING,       "PRINTER_NOTIFY_FIELD_STATUS_STRING",       NOTIFY_TABLE_STRING,   NULL },
3326 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_CJOBS,               "PRINTER_NOTIFY_FIELD_CJOBS",               NOTIFY_TABLE_DWORD,    spoolss_notify_cjobs },
3327 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_AVERAGE_PPM,         "PRINTER_NOTIFY_FIELD_AVERAGE_PPM",         NOTIFY_TABLE_DWORD,    spoolss_notify_average_ppm },
3328 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_TOTAL_PAGES,         "PRINTER_NOTIFY_FIELD_TOTAL_PAGES",         NOTIFY_TABLE_DWORD,    NULL },
3329 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_PAGES_PRINTED,       "PRINTER_NOTIFY_FIELD_PAGES_PRINTED",       NOTIFY_TABLE_DWORD,    NULL },
3330 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_TOTAL_BYTES,         "PRINTER_NOTIFY_FIELD_TOTAL_BYTES",         NOTIFY_TABLE_DWORD,    NULL },
3331 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_BYTES_PRINTED,       "PRINTER_NOTIFY_FIELD_BYTES_PRINTED",       NOTIFY_TABLE_DWORD,    NULL },
3332 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_PRINTER_NAME,            "JOB_NOTIFY_FIELD_PRINTER_NAME",            NOTIFY_TABLE_STRING,   spoolss_notify_printer_name },
3333 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_MACHINE_NAME,            "JOB_NOTIFY_FIELD_MACHINE_NAME",            NOTIFY_TABLE_STRING,   spoolss_notify_server_name },
3334 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_PORT_NAME,               "JOB_NOTIFY_FIELD_PORT_NAME",               NOTIFY_TABLE_STRING,   spoolss_notify_port_name },
3335 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_USER_NAME,               "JOB_NOTIFY_FIELD_USER_NAME",               NOTIFY_TABLE_STRING,   spoolss_notify_username },
3336 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_NOTIFY_NAME,             "JOB_NOTIFY_FIELD_NOTIFY_NAME",             NOTIFY_TABLE_STRING,   spoolss_notify_username },
3337 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_DATATYPE,                "JOB_NOTIFY_FIELD_DATATYPE",                NOTIFY_TABLE_STRING,   spoolss_notify_datatype },
3338 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_PRINT_PROCESSOR,         "JOB_NOTIFY_FIELD_PRINT_PROCESSOR",         NOTIFY_TABLE_STRING,   spoolss_notify_print_processor },
3339 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_PARAMETERS,              "JOB_NOTIFY_FIELD_PARAMETERS",              NOTIFY_TABLE_STRING,   spoolss_notify_parameters },
3340 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_DRIVER_NAME,             "JOB_NOTIFY_FIELD_DRIVER_NAME",             NOTIFY_TABLE_STRING,   spoolss_notify_driver_name },
3341 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_DEVMODE,                 "JOB_NOTIFY_FIELD_DEVMODE",                 NOTIFY_TABLE_DEVMODE,  spoolss_notify_devmode },
3342 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_STATUS,                  "JOB_NOTIFY_FIELD_STATUS",                  NOTIFY_TABLE_DWORD,    spoolss_notify_job_status },
3343 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_STATUS_STRING,           "JOB_NOTIFY_FIELD_STATUS_STRING",           NOTIFY_TABLE_STRING,   spoolss_notify_job_status_string },
3344 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_SECURITY_DESCRIPTOR,     "JOB_NOTIFY_FIELD_SECURITY_DESCRIPTOR",     NOTIFY_TABLE_SECURITYDESCRIPTOR,   NULL },
3345 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_DOCUMENT,                "JOB_NOTIFY_FIELD_DOCUMENT",                NOTIFY_TABLE_STRING,   spoolss_notify_job_name },
3346 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_PRIORITY,                "JOB_NOTIFY_FIELD_PRIORITY",                NOTIFY_TABLE_DWORD,    spoolss_notify_priority },
3347 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_POSITION,                "JOB_NOTIFY_FIELD_POSITION",                NOTIFY_TABLE_DWORD,    spoolss_notify_job_position },
3348 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_SUBMITTED,               "JOB_NOTIFY_FIELD_SUBMITTED",               NOTIFY_TABLE_TIME,     spoolss_notify_submitted_time },
3349 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_START_TIME,              "JOB_NOTIFY_FIELD_START_TIME",              NOTIFY_TABLE_DWORD,    spoolss_notify_start_time },
3350 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_UNTIL_TIME,              "JOB_NOTIFY_FIELD_UNTIL_TIME",              NOTIFY_TABLE_DWORD,    spoolss_notify_until_time },
3351 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_TIME,                    "JOB_NOTIFY_FIELD_TIME",                    NOTIFY_TABLE_DWORD,    spoolss_notify_job_time },
3352 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_TOTAL_PAGES,             "JOB_NOTIFY_FIELD_TOTAL_PAGES",             NOTIFY_TABLE_DWORD,    spoolss_notify_total_pages },
3353 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_PAGES_PRINTED,           "JOB_NOTIFY_FIELD_PAGES_PRINTED",           NOTIFY_TABLE_DWORD,    spoolss_notify_pages_printed },
3354 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_TOTAL_BYTES,             "JOB_NOTIFY_FIELD_TOTAL_BYTES",             NOTIFY_TABLE_DWORD,    spoolss_notify_job_size },
3355 };
3356
3357 /*******************************************************************
3358  Return the variable_type of info_data structure.
3359 ********************************************************************/
3360
3361 static uint32_t variable_type_of_notify_info_data(enum spoolss_NotifyType type,
3362                                                   uint16_t field)
3363 {
3364         int i=0;
3365
3366         for (i = 0; i < ARRAY_SIZE(notify_info_data_table); i++) {
3367                 if ( (notify_info_data_table[i].type == type) &&
3368                      (notify_info_data_table[i].field == field) ) {
3369                         return notify_info_data_table[i].variable_type;
3370                 }
3371         }
3372
3373         DEBUG(5, ("invalid notify data type %d/%d\n", type, field));
3374
3375         return 0;
3376 }
3377
3378 /****************************************************************************
3379 ****************************************************************************/
3380
3381 static bool search_notify(enum spoolss_NotifyType type,
3382                           uint16_t field,
3383                           int *value)
3384 {
3385         int i;
3386
3387         for (i = 0; i < ARRAY_SIZE(notify_info_data_table); i++) {
3388                 if (notify_info_data_table[i].type == type &&
3389                     notify_info_data_table[i].field == field &&
3390                     notify_info_data_table[i].fn != NULL) {
3391                         *value = i;
3392                         return true;
3393                 }
3394         }
3395
3396         return false;
3397 }
3398
3399 /****************************************************************************
3400 ****************************************************************************/
3401
3402 void construct_info_data(struct spoolss_Notify *info_data,
3403                          enum spoolss_NotifyType type,
3404                          uint16_t field,
3405                          int id)
3406 {
3407         info_data->type                 = type;
3408         info_data->field.field          = field;
3409         info_data->variable_type        = variable_type_of_notify_info_data(type, field);
3410         info_data->job_id               = id;
3411 }
3412
3413 /*******************************************************************
3414  *
3415  * fill a notify_info struct with info asked
3416  *
3417  ********************************************************************/
3418
3419 static bool construct_notify_printer_info(Printer_entry *print_hnd,
3420                                           struct spoolss_NotifyInfo *info,
3421                                           int snum,
3422                                           const struct spoolss_NotifyOptionType *option_type,
3423                                           uint32_t id,
3424                                           TALLOC_CTX *mem_ctx)
3425 {
3426         int field_num,j;
3427         enum spoolss_NotifyType type;
3428         uint16_t field;
3429
3430         struct spoolss_Notify *current_data;
3431         NT_PRINTER_INFO_LEVEL *printer = NULL;
3432         print_queue_struct *queue=NULL;
3433
3434         type = option_type->type;
3435
3436         DEBUG(4,("construct_notify_printer_info: Notify type: [%s], number of notify info: [%d] on printer: [%s]\n",
3437                 (type == PRINTER_NOTIFY_TYPE ? "PRINTER_NOTIFY_TYPE" : "JOB_NOTIFY_TYPE"),
3438                 option_type->count, lp_servicename(snum)));
3439
3440         if (!W_ERROR_IS_OK(get_a_printer(print_hnd, &printer, 2, lp_const_servicename(snum))))
3441                 return false;
3442
3443         for(field_num=0; field_num < option_type->count; field_num++) {
3444                 field = option_type->fields[field_num].field;
3445
3446                 DEBUG(4,("construct_notify_printer_info: notify [%d]: type [%x], field [%x]\n", field_num, type, field));
3447
3448                 if (!search_notify(type, field, &j) )
3449                         continue;
3450
3451                 info->notifies = TALLOC_REALLOC_ARRAY(info, info->notifies,
3452                                                       struct spoolss_Notify,
3453                                                       info->count + 1);
3454                 if (info->notifies == NULL) {
3455                         DEBUG(2,("construct_notify_printer_info: failed to enlarge buffer info->data!\n"));
3456                         free_a_printer(&printer, 2);
3457                         return false;
3458                 }
3459
3460                 current_data = &info->notifies[info->count];
3461
3462                 construct_info_data(current_data, type, field, id);
3463
3464                 DEBUG(10,("construct_notify_printer_info: calling [%s]  snum=%d  printername=[%s])\n",
3465                                 notify_info_data_table[j].name, snum, printer->info_2->printername ));
3466
3467                 notify_info_data_table[j].fn(snum, current_data, queue,
3468                                              printer, mem_ctx);
3469
3470                 info->count++;
3471         }
3472
3473         free_a_printer(&printer, 2);
3474         return true;
3475 }
3476
3477 /*******************************************************************
3478  *
3479  * fill a notify_info struct with info asked
3480  *
3481  ********************************************************************/
3482
3483 static bool construct_notify_jobs_info(print_queue_struct *queue,
3484                                        struct spoolss_NotifyInfo *info,
3485                                        NT_PRINTER_INFO_LEVEL *printer,
3486                                        int snum,
3487                                        const struct spoolss_NotifyOptionType *option_type,
3488                                        uint32_t id,
3489                                        TALLOC_CTX *mem_ctx)
3490 {
3491         int field_num,j;
3492         enum spoolss_NotifyType type;
3493         uint16_t field;
3494         struct spoolss_Notify *current_data;
3495
3496         DEBUG(4,("construct_notify_jobs_info\n"));
3497
3498         type = option_type->type;
3499
3500         DEBUGADD(4,("Notify type: [%s], number of notify info: [%d]\n",
3501                 (type == PRINTER_NOTIFY_TYPE ? "PRINTER_NOTIFY_TYPE" : "JOB_NOTIFY_TYPE"),
3502                 option_type->count));
3503
3504         for(field_num=0; field_num<option_type->count; field_num++) {
3505                 field = option_type->fields[field_num].field;
3506
3507                 if (!search_notify(type, field, &j) )
3508                         continue;
3509
3510                 info->notifies = TALLOC_REALLOC_ARRAY(info, info->notifies,
3511                                                       struct spoolss_Notify,
3512                                                       info->count + 1);
3513                 if (info->notifies == NULL) {
3514                         DEBUG(2,("construct_notify_jobs_info: failed to enlarg buffer info->data!\n"));
3515                         return false;
3516                 }
3517
3518                 current_data=&(info->notifies[info->count]);
3519
3520                 construct_info_data(current_data, type, field, id);
3521                 notify_info_data_table[j].fn(snum, current_data, queue,
3522                                              printer, mem_ctx);
3523                 info->count++;
3524         }
3525
3526         return true;
3527 }
3528
3529 /*
3530  * JFM: The enumeration is not that simple, it's even non obvious.
3531  *
3532  * let's take an example: I want to monitor the PRINTER SERVER for
3533  * the printer's name and the number of jobs currently queued.
3534  * So in the NOTIFY_OPTION, I have one NOTIFY_OPTION_TYPE structure.
3535  * Its type is PRINTER_NOTIFY_TYPE and it has 2 fields NAME and CJOBS.
3536  *
3537  * I have 3 printers on the back of my server.
3538  *
3539  * Now the response is a NOTIFY_INFO structure, with 6 NOTIFY_INFO_DATA
3540  * structures.
3541  *   Number     Data                    Id
3542  *      1       printer 1 name          1
3543  *      2       printer 1 cjob          1
3544  *      3       printer 2 name          2
3545  *      4       printer 2 cjob          2
3546  *      5       printer 3 name          3
3547  *      6       printer 3 name          3
3548  *
3549  * that's the print server case, the printer case is even worse.
3550  */
3551
3552 /*******************************************************************
3553  *
3554  * enumerate all printers on the printserver
3555  * fill a notify_info struct with info asked
3556  *
3557  ********************************************************************/
3558
3559 static WERROR printserver_notify_info(pipes_struct *p,
3560                                       struct policy_handle *hnd,
3561                                       struct spoolss_NotifyInfo *info,
3562                                       TALLOC_CTX *mem_ctx)
3563 {
3564         int snum;
3565         Printer_entry *Printer = find_printer_index_by_hnd(p, hnd);
3566         int n_services=lp_numservices();
3567         int i;
3568         struct spoolss_NotifyOption *option;
3569         struct spoolss_NotifyOptionType option_type;
3570
3571         DEBUG(4,("printserver_notify_info\n"));
3572
3573         if (!Printer)
3574                 return WERR_BADFID;
3575
3576         option = Printer->notify.option;
3577
3578         info->version   = 2;
3579         info->notifies  = NULL;
3580         info->count     = 0;
3581
3582         /* a bug in xp sp2 rc2 causes it to send a fnpcn request without
3583            sending a ffpcn() request first */
3584
3585         if ( !option )
3586                 return WERR_BADFID;
3587
3588         for (i=0; i<option->count; i++) {
3589                 option_type = option->types[i];
3590
3591                 if (option_type.type != PRINTER_NOTIFY_TYPE)
3592                         continue;
3593
3594                 for (snum=0; snum<n_services; snum++)
3595                 {
3596                         if ( lp_browseable(snum) && lp_snum_ok(snum) && lp_print_ok(snum) )
3597                                 construct_notify_printer_info ( Printer, info, snum, &option_type, snum, mem_ctx );
3598                 }
3599         }
3600
3601 #if 0
3602         /*
3603          * Debugging information, don't delete.
3604          */
3605
3606         DEBUG(1,("dumping the NOTIFY_INFO\n"));
3607         DEBUGADD(1,("info->version:[%d], info->flags:[%d], info->count:[%d]\n", info->version, info->flags, info->count));
3608         DEBUGADD(1,("num\ttype\tfield\tres\tid\tsize\tenc_type\n"));
3609
3610         for (i=0; i<info->count; i++) {
3611                 DEBUGADD(1,("[%d]\t[%d]\t[%d]\t[%d]\t[%d]\t[%d]\t[%d]\n",
3612                 i, info->data[i].type, info->data[i].field, info->data[i].reserved,
3613                 info->data[i].id, info->data[i].size, info->data[i].enc_type));
3614         }
3615 #endif
3616
3617         return WERR_OK;
3618 }
3619
3620 /*******************************************************************
3621  *
3622  * fill a notify_info struct with info asked
3623  *
3624  ********************************************************************/
3625
3626 static WERROR printer_notify_info(pipes_struct *p, struct policy_handle *hnd,
3627                                   struct spoolss_NotifyInfo *info,
3628                                   TALLOC_CTX *mem_ctx)
3629 {
3630         int snum;
3631         Printer_entry *Printer = find_printer_index_by_hnd(p, hnd);
3632         int i;
3633         uint32_t id;
3634         struct spoolss_NotifyOption *option;
3635         struct spoolss_NotifyOptionType option_type;
3636         int count,j;
3637         print_queue_struct *queue=NULL;
3638         print_status_struct status;
3639
3640         DEBUG(4,("printer_notify_info\n"));
3641
3642         if (!Printer)
3643                 return WERR_BADFID;
3644
3645         option = Printer->notify.option;
3646         id = 0x0;
3647
3648         info->version   = 2;
3649         info->notifies  = NULL;
3650         info->count     = 0;
3651
3652         /* a bug in xp sp2 rc2 causes it to send a fnpcn request without
3653            sending a ffpcn() request first */
3654
3655         if ( !option )
3656                 return WERR_BADFID;
3657
3658         get_printer_snum(p, hnd, &snum, NULL);
3659
3660         for (i=0; i<option->count; i++) {
3661                 option_type = option->types[i];
3662
3663                 switch (option_type.type) {
3664                 case PRINTER_NOTIFY_TYPE:
3665                         if(construct_notify_printer_info(Printer, info, snum,
3666                                                          &option_type, id,
3667                                                          mem_ctx))
3668                                 id--;
3669                         break;
3670
3671                 case JOB_NOTIFY_TYPE: {
3672                         NT_PRINTER_INFO_LEVEL *printer = NULL;
3673
3674                         count = print_queue_status(snum, &queue, &status);
3675
3676                         if (!W_ERROR_IS_OK(get_a_printer(Printer, &printer, 2, lp_const_servicename(snum))))
3677                                 goto done;
3678
3679                         for (j=0; j<count; j++) {
3680                                 construct_notify_jobs_info(&queue[j], info,
3681                                                            printer, snum,
3682                                                            &option_type,
3683                                                            queue[j].job,
3684                                                            mem_ctx);
3685                         }
3686
3687                         free_a_printer(&printer, 2);
3688
3689                 done:
3690                         SAFE_FREE(queue);
3691                         break;
3692                 }
3693                 }
3694         }
3695
3696         /*
3697          * Debugging information, don't delete.
3698          */
3699         /*
3700         DEBUG(1,("dumping the NOTIFY_INFO\n"));
3701         DEBUGADD(1,("info->version:[%d], info->flags:[%d], info->count:[%d]\n", info->version, info->flags, info->count));
3702         DEBUGADD(1,("num\ttype\tfield\tres\tid\tsize\tenc_type\n"));
3703
3704         for (i=0; i<info->count; i++) {
3705                 DEBUGADD(1,("[%d]\t[%d]\t[%d]\t[%d]\t[%d]\t[%d]\t[%d]\n",
3706                 i, info->data[i].type, info->data[i].field, info->data[i].reserved,
3707                 info->data[i].id, info->data[i].size, info->data[i].enc_type));
3708         }
3709         */
3710         return WERR_OK;
3711 }
3712
3713 /****************************************************************
3714  _spoolss_RouterRefreshPrinterChangeNotify
3715 ****************************************************************/
3716
3717 WERROR _spoolss_RouterRefreshPrinterChangeNotify(pipes_struct *p,
3718                                                  struct spoolss_RouterRefreshPrinterChangeNotify *r)
3719 {
3720         struct spoolss_NotifyInfo *info;
3721
3722         Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
3723         WERROR result = WERR_BADFID;
3724
3725         /* we always have a spoolss_NotifyInfo struct */
3726         info = talloc_zero(p->mem_ctx, struct spoolss_NotifyInfo);
3727         if (!info) {
3728                 result = WERR_NOMEM;
3729                 goto done;
3730         }
3731
3732         *r->out.info = info;
3733
3734         if (!Printer) {
3735                 DEBUG(2,("_spoolss_RouterRefreshPrinterChangeNotify: "
3736                         "Invalid handle (%s:%u:%u).\n",
3737                         OUR_HANDLE(r->in.handle)));
3738                 goto done;
3739         }
3740
3741         DEBUG(4,("Printer type %x\n",Printer->printer_type));
3742
3743         /*
3744          *      We are now using the change value, and
3745          *      I should check for PRINTER_NOTIFY_OPTIONS_REFRESH but as
3746          *      I don't have a global notification system, I'm sending back all the
3747          *      informations even when _NOTHING_ has changed.
3748          */
3749
3750         /* We need to keep track of the change value to send back in
3751            RRPCN replies otherwise our updates are ignored. */
3752
3753         Printer->notify.fnpcn = true;
3754
3755         if (Printer->notify.client_connected) {
3756                 DEBUG(10,("_spoolss_RouterRefreshPrinterChangeNotify: "
3757                         "Saving change value in request [%x]\n",
3758                         r->in.change_low));
3759                 Printer->notify.change = r->in.change_low;
3760         }
3761
3762         /* just ignore the spoolss_NotifyOption */
3763
3764         switch (Printer->printer_type) {
3765                 case SPLHND_SERVER:
3766                         result = printserver_notify_info(p, r->in.handle,
3767                                                          info, p->mem_ctx);
3768                         break;
3769
3770                 case SPLHND_PRINTER:
3771                         result = printer_notify_info(p, r->in.handle,
3772                                                      info, p->mem_ctx);
3773                         break;
3774         }
3775
3776         Printer->notify.fnpcn = false;
3777
3778 done:
3779         return result;
3780 }
3781
3782 /********************************************************************
3783  * construct_printer_info_0
3784  * fill a printer_info_0 struct
3785  ********************************************************************/
3786
3787 static WERROR construct_printer_info0(TALLOC_CTX *mem_ctx,
3788                                       const NT_PRINTER_INFO_LEVEL *ntprinter,
3789                                       struct spoolss_PrinterInfo0 *r,
3790                                       int snum)
3791 {
3792         int count;
3793         counter_printer_0 *session_counter;
3794         time_t setuptime;
3795         print_status_struct status;
3796
3797         r->printername          = talloc_strdup(mem_ctx, ntprinter->info_2->printername);
3798         W_ERROR_HAVE_NO_MEMORY(r->printername);
3799
3800         r->servername           = talloc_strdup(mem_ctx, ntprinter->info_2->servername);
3801         W_ERROR_HAVE_NO_MEMORY(r->servername);
3802
3803         count = print_queue_length(snum, &status);
3804
3805         /* check if we already have a counter for this printer */
3806         for (session_counter = counter_list; session_counter; session_counter = session_counter->next) {
3807                 if (session_counter->snum == snum)
3808                         break;
3809         }
3810
3811         /* it's the first time, add it to the list */
3812         if (session_counter == NULL) {
3813                 session_counter = SMB_MALLOC_P(counter_printer_0);
3814                 W_ERROR_HAVE_NO_MEMORY(session_counter);
3815                 ZERO_STRUCTP(session_counter);
3816                 session_counter->snum           = snum;
3817                 session_counter->counter        = 0;
3818                 DLIST_ADD(counter_list, session_counter);
3819         }
3820
3821         /* increment it */
3822         session_counter->counter++;
3823
3824         r->cjobs                        = count;
3825         r->total_jobs                   = 0;
3826         r->total_bytes                  = 0;
3827
3828         setuptime = (time_t)ntprinter->info_2->setuptime;
3829
3830         init_systemtime(&r->time, gmtime(&setuptime));
3831
3832         /* JFM:
3833          * the global_counter should be stored in a TDB as it's common to all the clients
3834          * and should be zeroed on samba startup
3835          */
3836         r->global_counter               = session_counter->counter;
3837         r->total_pages                  = 0;
3838         /* in 2.2 we reported ourselves as 0x0004 and 0x0565 */
3839         r->version                      = 0x0005;       /* NT 5 */
3840         r->free_build                   = 0x0893;       /* build 2195 */
3841         r->spooling                     = 0;
3842         r->max_spooling                 = 0;
3843         r->session_counter              = session_counter->counter;
3844         r->num_error_out_of_paper       = 0x0;
3845         r->num_error_not_ready          = 0x0;          /* number of print failure */
3846         r->job_error                    = 0x0;
3847         r->number_of_processors         = 0x1;
3848         r->processor_type               = PROCESSOR_INTEL_PENTIUM; /* 586 Pentium ? */
3849         r->high_part_total_bytes        = 0x0;
3850         r->change_id                    = ntprinter->info_2->changeid; /* ChangeID in milliseconds*/
3851         r->last_error                   = WERR_OK;
3852         r->status                       = nt_printq_status(status.status);
3853         r->enumerate_network_printers   = 0x0;
3854         r->c_setprinter                 = get_c_setprinter(); /* monotonically increasing sum of delta printer counts */
3855         r->processor_architecture       = 0x0;
3856         r->processor_level              = 0x6;          /* 6  ???*/
3857         r->ref_ic                       = 0;
3858         r->reserved2                    = 0;
3859         r->reserved3                    = 0;
3860
3861         return WERR_OK;
3862 }
3863
3864 /****************************************************************************
3865  Convert an NT_DEVICEMODE to a spoolss_DeviceMode structure.  Both pointers
3866  should be valid upon entry
3867 ****************************************************************************/
3868
3869 static WERROR convert_nt_devicemode(TALLOC_CTX *mem_ctx,
3870                                     struct spoolss_DeviceMode *r,
3871                                     const NT_DEVICEMODE *ntdevmode)
3872 {
3873         if (!r || !ntdevmode) {
3874                 return WERR_INVALID_PARAM;
3875         }
3876
3877         r->devicename           = talloc_strdup(mem_ctx, ntdevmode->devicename);
3878         W_ERROR_HAVE_NO_MEMORY(r->devicename);
3879
3880         r->specversion          = ntdevmode->specversion;
3881         r->driverversion        = ntdevmode->driverversion;
3882         r->size                 = ntdevmode->size;
3883         r->__driverextra_length = ntdevmode->driverextra;
3884         r->fields               = ntdevmode->fields;
3885
3886         r->orientation          = ntdevmode->orientation;
3887         r->papersize            = ntdevmode->papersize;
3888         r->paperlength          = ntdevmode->paperlength;
3889         r->paperwidth           = ntdevmode->paperwidth;
3890         r->scale                = ntdevmode->scale;
3891         r->copies               = ntdevmode->copies;
3892         r->defaultsource        = ntdevmode->defaultsource;
3893         r->printquality         = ntdevmode->printquality;
3894         r->color                = ntdevmode->color;
3895         r->duplex               = ntdevmode->duplex;
3896         r->yresolution          = ntdevmode->yresolution;
3897         r->ttoption             = ntdevmode->ttoption;
3898         r->collate              = ntdevmode->collate;
3899
3900         r->formname             = talloc_strdup(mem_ctx, ntdevmode->formname);
3901         W_ERROR_HAVE_NO_MEMORY(r->formname);
3902
3903         /* all 0 below are values that have not been set in the old parsing/copy
3904          * function, maybe they should... - gd */
3905
3906         r->logpixels            = 0;
3907         r->bitsperpel           = 0;
3908         r->pelswidth            = 0;
3909         r->pelsheight           = 0;
3910         r->displayflags         = 0;
3911         r->displayfrequency     = 0;
3912         r->icmmethod            = ntdevmode->icmmethod;
3913         r->icmintent            = ntdevmode->icmintent;
3914         r->mediatype            = ntdevmode->mediatype;
3915         r->dithertype           = ntdevmode->dithertype;
3916         r->reserved1            = 0;
3917         r->reserved2            = 0;
3918         r->panningwidth         = 0;
3919         r->panningheight        = 0;
3920
3921         if (ntdevmode->nt_dev_private != NULL) {
3922                 r->driverextra_data = data_blob_talloc(mem_ctx,
3923                         ntdevmode->nt_dev_private,
3924                         ntdevmode->driverextra);
3925                 W_ERROR_HAVE_NO_MEMORY(r->driverextra_data.data);
3926         }
3927
3928         return WERR_OK;
3929 }
3930
3931
3932 /****************************************************************************
3933  Create a spoolss_DeviceMode struct. Returns talloced memory.
3934 ****************************************************************************/
3935
3936 struct spoolss_DeviceMode *construct_dev_mode(TALLOC_CTX *mem_ctx,
3937                                               const char *servicename)
3938 {
3939         WERROR result;
3940         NT_PRINTER_INFO_LEVEL   *printer = NULL;
3941         struct spoolss_DeviceMode *devmode = NULL;
3942
3943         DEBUG(7,("construct_dev_mode\n"));
3944
3945         DEBUGADD(8,("getting printer characteristics\n"));
3946
3947         if (!W_ERROR_IS_OK(get_a_printer(NULL, &printer, 2, servicename)))
3948                 return NULL;
3949
3950         if (!printer->info_2->devmode) {
3951                 DEBUG(5, ("BONG! There was no device mode!\n"));
3952                 goto done;
3953         }
3954
3955         devmode = TALLOC_ZERO_P(mem_ctx, struct spoolss_DeviceMode);
3956         if (!devmode) {
3957                 DEBUG(2,("construct_dev_mode: talloc fail.\n"));
3958                 goto done;
3959         }
3960
3961         DEBUGADD(8,("loading DEVICEMODE\n"));
3962
3963         result = convert_nt_devicemode(mem_ctx, devmode, printer->info_2->devmode);
3964         if (!W_ERROR_IS_OK(result)) {
3965                 TALLOC_FREE(devmode);
3966         }
3967
3968 done:
3969         free_a_printer(&printer,2);
3970
3971         return devmode;
3972 }
3973
3974 /********************************************************************
3975  * construct_printer_info3
3976  * fill a spoolss_PrinterInfo3 struct
3977  ********************************************************************/
3978
3979 static WERROR construct_printer_info3(TALLOC_CTX *mem_ctx,
3980                                       const NT_PRINTER_INFO_LEVEL *ntprinter,
3981                                       struct spoolss_PrinterInfo3 *r,
3982                                       int snum)
3983 {
3984         /* These are the components of the SD we are returning. */
3985
3986         if (ntprinter->info_2->secdesc_buf && ntprinter->info_2->secdesc_buf->sd_size != 0) {
3987                 /* don't use talloc_steal() here unless you do a deep steal of all
3988                    the SEC_DESC members */
3989
3990                 r->secdesc = dup_sec_desc(mem_ctx,
3991                                           ntprinter->info_2->secdesc_buf->sd);
3992                 W_ERROR_HAVE_NO_MEMORY(r->secdesc);
3993         }
3994
3995         return WERR_OK;
3996 }
3997
3998 /********************************************************************
3999  * construct_printer_info4
4000  * fill a spoolss_PrinterInfo4 struct
4001  ********************************************************************/
4002
4003 static WERROR construct_printer_info4(TALLOC_CTX *mem_ctx,
4004                                       const NT_PRINTER_INFO_LEVEL *ntprinter,
4005                                       struct spoolss_PrinterInfo4 *r,
4006                                       int snum)
4007 {
4008         r->printername  = talloc_strdup(mem_ctx, ntprinter->info_2->printername);
4009         W_ERROR_HAVE_NO_MEMORY(r->printername);
4010         r->servername   = talloc_strdup(mem_ctx, ntprinter->info_2->servername);
4011         W_ERROR_HAVE_NO_MEMORY(r->servername);
4012
4013         r->attributes   = ntprinter->info_2->attributes;
4014
4015         return WERR_OK;
4016 }
4017
4018 /********************************************************************
4019  * construct_printer_info5
4020  * fill a spoolss_PrinterInfo5 struct
4021  ********************************************************************/
4022
4023 static WERROR construct_printer_info5(TALLOC_CTX *mem_ctx,
4024                                       const NT_PRINTER_INFO_LEVEL *ntprinter,
4025                                       struct spoolss_PrinterInfo5 *r,
4026                                       int snum)
4027 {
4028         r->printername  = talloc_strdup(mem_ctx, ntprinter->info_2->printername);
4029         W_ERROR_HAVE_NO_MEMORY(r->printername);
4030         r->portname     = talloc_strdup(mem_ctx, ntprinter->info_2->portname);
4031         W_ERROR_HAVE_NO_MEMORY(r->portname);
4032
4033         r->attributes   = ntprinter->info_2->attributes;
4034
4035         /* these two are not used by NT+ according to MSDN */
4036
4037         r->device_not_selected_timeout          = 0x0;  /* have seen 0x3a98 */
4038         r->transmission_retry_timeout           = 0x0;  /* have seen 0xafc8 */
4039
4040         return WERR_OK;
4041 }
4042
4043 /********************************************************************
4044  * construct_printer_info_6
4045  * fill a spoolss_PrinterInfo6 struct
4046  ********************************************************************/
4047
4048 static WERROR construct_printer_info6(TALLOC_CTX *mem_ctx,
4049                                       const NT_PRINTER_INFO_LEVEL *ntprinter,
4050                                       struct spoolss_PrinterInfo6 *r,
4051                                       int snum)
4052 {
4053         int count;
4054         print_status_struct status;
4055
4056         count = print_queue_length(snum, &status);
4057
4058         r->status = nt_printq_status(status.status);
4059
4060         return WERR_OK;
4061 }
4062
4063 /********************************************************************
4064  * construct_printer_info7
4065  * fill a spoolss_PrinterInfo7 struct
4066  ********************************************************************/
4067
4068 static WERROR construct_printer_info7(TALLOC_CTX *mem_ctx,
4069                                       Printer_entry *print_hnd,
4070                                       struct spoolss_PrinterInfo7 *r,
4071                                       int snum)
4072 {
4073         struct GUID guid;
4074
4075         if (is_printer_published(print_hnd, snum, &guid)) {
4076                 r->guid = talloc_strdup_upper(mem_ctx, GUID_string2(mem_ctx, &guid));
4077                 r->action = DSPRINT_PUBLISH;
4078         } else {
4079                 r->guid = talloc_strdup(mem_ctx, "");
4080                 r->action = DSPRINT_UNPUBLISH;
4081         }
4082         W_ERROR_HAVE_NO_MEMORY(r->guid);
4083
4084         return WERR_OK;
4085 }
4086
4087 /********************************************************************
4088  * construct_printer_info8
4089  * fill a spoolss_PrinterInfo8 struct
4090  ********************************************************************/
4091
4092 static WERROR construct_printer_info8(TALLOC_CTX *mem_ctx,
4093                                       const NT_PRINTER_INFO_LEVEL *ntprinter,
4094                                       struct spoolss_DeviceModeInfo *r,
4095                                       int snum)
4096 {
4097         struct spoolss_DeviceMode *devmode;
4098         WERROR result;
4099
4100         if (!ntprinter->info_2->devmode) {
4101                 r->devmode = NULL;
4102                 return WERR_OK;
4103         }
4104
4105         devmode = TALLOC_ZERO_P(mem_ctx, struct spoolss_DeviceMode);
4106         W_ERROR_HAVE_NO_MEMORY(devmode);
4107
4108         result = convert_nt_devicemode(mem_ctx, devmode, ntprinter->info_2->devmode);
4109         if (!W_ERROR_IS_OK(result)) {
4110                 TALLOC_FREE(devmode);
4111                 return result;
4112         }
4113
4114         r->devmode      = devmode;
4115
4116         return WERR_OK;
4117 }
4118
4119
4120 /********************************************************************
4121  * construct_printer_info1
4122  * fill a spoolss_PrinterInfo1 struct
4123 ********************************************************************/
4124
4125 static WERROR construct_printer_info1(TALLOC_CTX *mem_ctx,
4126                                       const NT_PRINTER_INFO_LEVEL *ntprinter,
4127                                       uint32_t flags,
4128                                       struct spoolss_PrinterInfo1 *r,
4129                                       int snum)
4130 {
4131         r->flags                = flags;
4132
4133         r->description          = talloc_asprintf(mem_ctx, "%s,%s,%s",
4134                                                   ntprinter->info_2->printername,
4135                                                   ntprinter->info_2->drivername,
4136                                                   ntprinter->info_2->location);
4137         W_ERROR_HAVE_NO_MEMORY(r->description);
4138
4139         if (*ntprinter->info_2->comment == '\0') {
4140                 r->comment      = talloc_strdup(mem_ctx, lp_comment(snum));
4141         } else {
4142                 r->comment      = talloc_strdup(mem_ctx, ntprinter->info_2->comment); /* saved comment */
4143         }
4144         W_ERROR_HAVE_NO_MEMORY(r->comment);
4145
4146         r->name                 = talloc_strdup(mem_ctx, ntprinter->info_2->printername);
4147         W_ERROR_HAVE_NO_MEMORY(r->name);
4148
4149         return WERR_OK;
4150 }
4151
4152 /********************************************************************
4153  * construct_printer_info2
4154  * fill a spoolss_PrinterInfo2 struct
4155 ********************************************************************/
4156
4157 static WERROR construct_printer_info2(TALLOC_CTX *mem_ctx,
4158                                       const NT_PRINTER_INFO_LEVEL *ntprinter,
4159                                       struct spoolss_PrinterInfo2 *r,
4160                                       int snum)
4161 {
4162         int count;
4163
4164         print_status_struct status;
4165
4166         count = print_queue_length(snum, &status);
4167
4168         r->servername           = talloc_strdup(mem_ctx, ntprinter->info_2->servername);
4169         W_ERROR_HAVE_NO_MEMORY(r->servername);
4170         r->printername          = talloc_strdup(mem_ctx, ntprinter->info_2->printername);
4171         W_ERROR_HAVE_NO_MEMORY(r->printername);
4172         r->sharename            = talloc_strdup(mem_ctx, lp_servicename(snum));
4173         W_ERROR_HAVE_NO_MEMORY(r->sharename);
4174         r->portname             = talloc_strdup(mem_ctx, ntprinter->info_2->portname);
4175         W_ERROR_HAVE_NO_MEMORY(r->portname);
4176         r->drivername           = talloc_strdup(mem_ctx, ntprinter->info_2->drivername);
4177         W_ERROR_HAVE_NO_MEMORY(r->drivername);
4178
4179         if (*ntprinter->info_2->comment == '\0') {
4180                 r->comment      = talloc_strdup(mem_ctx, lp_comment(snum));
4181         } else {
4182                 r->comment      = talloc_strdup(mem_ctx, ntprinter->info_2->comment);
4183         }
4184         W_ERROR_HAVE_NO_MEMORY(r->comment);
4185
4186         r->location             = talloc_strdup(mem_ctx, ntprinter->info_2->location);
4187         W_ERROR_HAVE_NO_MEMORY(r->location);
4188         r->sepfile              = talloc_strdup(mem_ctx, ntprinter->info_2->sepfile);
4189         W_ERROR_HAVE_NO_MEMORY(r->sepfile);
4190         r->printprocessor       = talloc_strdup(mem_ctx, ntprinter->info_2->printprocessor);
4191         W_ERROR_HAVE_NO_MEMORY(r->printprocessor);
4192         r->datatype             = talloc_strdup(mem_ctx, ntprinter->info_2->datatype);
4193         W_ERROR_HAVE_NO_MEMORY(r->datatype);
4194         r->parameters           = talloc_strdup(mem_ctx, ntprinter->info_2->parameters);
4195         W_ERROR_HAVE_NO_MEMORY(r->parameters);
4196
4197         r->attributes           = ntprinter->info_2->attributes;
4198
4199         r->priority             = ntprinter->info_2->priority;
4200         r->defaultpriority      = ntprinter->info_2->default_priority;
4201         r->starttime            = ntprinter->info_2->starttime;
4202         r->untiltime            = ntprinter->info_2->untiltime;
4203         r->status               = nt_printq_status(status.status);
4204         r->cjobs                = count;
4205         r->averageppm           = ntprinter->info_2->averageppm;
4206
4207         r->devmode = construct_dev_mode(mem_ctx, lp_const_servicename(snum));
4208         if (!r->devmode) {
4209                 DEBUG(8,("Returning NULL Devicemode!\n"));
4210         }
4211
4212         r->secdesc              = NULL;
4213
4214         if (ntprinter->info_2->secdesc_buf && ntprinter->info_2->secdesc_buf->sd_size != 0) {
4215                 /* don't use talloc_steal() here unless you do a deep steal of all
4216                    the SEC_DESC members */
4217
4218                 r->secdesc      = dup_sec_desc(mem_ctx, ntprinter->info_2->secdesc_buf->sd);
4219         }
4220
4221         return WERR_OK;
4222 }
4223
4224 /********************************************************************
4225 ********************************************************************/
4226
4227 static bool snum_is_shared_printer(int snum)
4228 {
4229         return (lp_browseable(snum) && lp_snum_ok(snum) && lp_print_ok(snum));
4230 }
4231
4232 /********************************************************************
4233  Spoolss_enumprinters.
4234 ********************************************************************/
4235
4236 static WERROR enum_all_printers_info_level(TALLOC_CTX *mem_ctx,
4237                                            uint32_t level,
4238                                            uint32_t flags,
4239                                            union spoolss_PrinterInfo **info_p,
4240                                            uint32_t *count_p)
4241 {
4242         int snum;
4243         int n_services = lp_numservices();
4244         union spoolss_PrinterInfo *info = NULL;
4245         uint32_t count = 0;
4246         WERROR result = WERR_OK;
4247
4248         *count_p = 0;
4249         *info_p = NULL;
4250
4251         for (snum = 0; snum < n_services; snum++) {
4252
4253                 NT_PRINTER_INFO_LEVEL *ntprinter = NULL;
4254
4255                 if (!snum_is_shared_printer(snum)) {
4256                         continue;
4257                 }
4258
4259                 DEBUG(4,("Found a printer in smb.conf: %s[%x]\n",
4260                         lp_servicename(snum), snum));
4261
4262                 info = TALLOC_REALLOC_ARRAY(mem_ctx, info,
4263                                             union spoolss_PrinterInfo,
4264                                             count + 1);
4265                 if (!info) {
4266                         result = WERR_NOMEM;
4267                         goto out;
4268                 }
4269
4270                 result = get_a_printer(NULL, &ntprinter, 2,
4271                                        lp_const_servicename(snum));
4272                 if (!W_ERROR_IS_OK(result)) {
4273                         goto out;
4274                 }
4275
4276                 switch (level) {
4277                 case 0:
4278                         result = construct_printer_info0(info, ntprinter,
4279                                                          &info[count].info0, snum);
4280                         break;
4281                 case 1:
4282                         result = construct_printer_info1(info, ntprinter, flags,
4283                                                          &info[count].info1, snum);
4284                         break;
4285                 case 2:
4286                         result = construct_printer_info2(info, ntprinter,
4287                                                          &info[count].info2, snum);
4288                         break;
4289                 case 4:
4290                         result = construct_printer_info4(info, ntprinter,
4291                                                          &info[count].info4, snum);
4292                         break;
4293                 case 5:
4294                         result = construct_printer_info5(info, ntprinter,
4295                                                          &info[count].info5, snum);
4296                         break;
4297
4298                 default:
4299                         result = WERR_UNKNOWN_LEVEL;
4300                         free_a_printer(&ntprinter, 2);
4301                         goto out;
4302                 }
4303
4304                 free_a_printer(&ntprinter, 2);
4305                 if (!W_ERROR_IS_OK(result)) {
4306                         goto out;
4307                 }
4308
4309                 count++;
4310         }
4311
4312         *count_p = count;
4313         *info_p = info;
4314
4315  out:
4316         if (!W_ERROR_IS_OK(result)) {
4317                 TALLOC_FREE(info);
4318                 return result;
4319         }
4320
4321         *info_p = info;
4322
4323         return WERR_OK;
4324 }
4325
4326 /********************************************************************
4327  * handle enumeration of printers at level 0
4328  ********************************************************************/
4329
4330 static WERROR enumprinters_level0(TALLOC_CTX *mem_ctx,
4331                                   uint32_t flags,
4332                                   const char *servername,
4333                                   union spoolss_PrinterInfo **info,
4334                                   uint32_t *count)
4335 {
4336         DEBUG(4,("enum_all_printers_info_0\n"));
4337
4338         return enum_all_printers_info_level(mem_ctx, 0, flags, info, count);
4339 }
4340
4341
4342 /********************************************************************
4343 ********************************************************************/
4344
4345 static WERROR enum_all_printers_info_1(TALLOC_CTX *mem_ctx,
4346                                        uint32_t flags,
4347                                        union spoolss_PrinterInfo **info,
4348                                        uint32_t *count)
4349 {
4350         DEBUG(4,("enum_all_printers_info_1\n"));
4351
4352         return enum_all_printers_info_level(mem_ctx, 1, flags, info, count);
4353 }
4354
4355 /********************************************************************
4356  enum_all_printers_info_1_local.
4357 *********************************************************************/
4358
4359 static WERROR enum_all_printers_info_1_local(TALLOC_CTX *mem_ctx,
4360                                              union spoolss_PrinterInfo **info,
4361                                              uint32_t *count)
4362 {
4363         DEBUG(4,("enum_all_printers_info_1_local\n"));
4364
4365         return enum_all_printers_info_1(mem_ctx, PRINTER_ENUM_ICON8, info, count);
4366 }
4367
4368 /********************************************************************
4369  enum_all_printers_info_1_name.
4370 *********************************************************************/
4371
4372 static WERROR enum_all_printers_info_1_name(TALLOC_CTX *mem_ctx,
4373                                             const char *name,
4374                                             union spoolss_PrinterInfo **info,
4375                                             uint32_t *count)
4376 {
4377         const char *s = name;
4378
4379         DEBUG(4,("enum_all_printers_info_1_name\n"));
4380
4381         if ((name[0] == '\\') && (name[1] == '\\')) {
4382                 s = name + 2;
4383         }
4384
4385         if (!is_myname_or_ipaddr(s)) {
4386                 return WERR_INVALID_NAME;
4387         }
4388
4389         return enum_all_printers_info_1(mem_ctx, PRINTER_ENUM_ICON8, info, count);
4390 }
4391
4392 /********************************************************************
4393  enum_all_printers_info_1_network.
4394 *********************************************************************/
4395
4396 static WERROR enum_all_printers_info_1_network(TALLOC_CTX *mem_ctx,
4397                                                const char *name,
4398                                                union spoolss_PrinterInfo **info,
4399                                                uint32_t *count)
4400 {
4401         const char *s = name;
4402
4403         DEBUG(4,("enum_all_printers_info_1_network\n"));
4404
4405         /* If we respond to a enum_printers level 1 on our name with flags
4406            set to PRINTER_ENUM_REMOTE with a list of printers then these
4407            printers incorrectly appear in the APW browse list.
4408            Specifically the printers for the server appear at the workgroup
4409            level where all the other servers in the domain are
4410            listed. Windows responds to this call with a
4411            WERR_CAN_NOT_COMPLETE so we should do the same. */
4412
4413         if (name[0] == '\\' && name[1] == '\\') {
4414                  s = name + 2;
4415         }
4416
4417         if (is_myname_or_ipaddr(s)) {
4418                  return WERR_CAN_NOT_COMPLETE;
4419         }
4420
4421         return enum_all_printers_info_1(mem_ctx, PRINTER_ENUM_NAME, info, count);
4422 }
4423
4424 /********************************************************************
4425  * api_spoolss_enumprinters
4426  *
4427  * called from api_spoolss_enumprinters (see this to understand)
4428  ********************************************************************/
4429
4430 static WERROR enum_all_printers_info_2(TALLOC_CTX *mem_ctx,
4431                                        union spoolss_PrinterInfo **info,
4432                                        uint32_t *count)
4433 {
4434         DEBUG(4,("enum_all_printers_info_2\n"));
4435
4436         return enum_all_printers_info_level(mem_ctx, 2, 0, info, count);
4437 }
4438
4439 /********************************************************************
4440  * handle enumeration of printers at level 1
4441  ********************************************************************/
4442
4443 static WERROR enumprinters_level1(TALLOC_CTX *mem_ctx,
4444                                   uint32_t flags,
4445                                   const char *name,
4446                                   union spoolss_PrinterInfo **info,
4447                                   uint32_t *count)
4448 {
4449         /* Not all the flags are equals */
4450
4451         if (flags & PRINTER_ENUM_LOCAL) {
4452                 return enum_all_printers_info_1_local(mem_ctx, info, count);
4453         }
4454
4455         if (flags & PRINTER_ENUM_NAME) {
4456                 return enum_all_printers_info_1_name(mem_ctx, name, info, count);
4457         }
4458
4459         if (flags & PRINTER_ENUM_NETWORK) {
4460                 return enum_all_printers_info_1_network(mem_ctx, name, info, count);
4461         }
4462
4463         return WERR_OK; /* NT4sp5 does that */
4464 }
4465
4466 /********************************************************************
4467  * handle enumeration of printers at level 2
4468  ********************************************************************/
4469
4470 static WERROR enumprinters_level2(TALLOC_CTX *mem_ctx,
4471                                   uint32_t flags,
4472                                   const char *servername,
4473                                   union spoolss_PrinterInfo **info,
4474                                   uint32_t *count)
4475 {
4476         if (flags & PRINTER_ENUM_LOCAL) {
4477                 return enum_all_printers_info_2(mem_ctx, info, count);
4478         }
4479
4480         if (flags & PRINTER_ENUM_NAME) {
4481                 if (!is_myname_or_ipaddr(canon_servername(servername))) {
4482                         return WERR_INVALID_NAME;
4483                 }
4484
4485                 return enum_all_printers_info_2(mem_ctx, info, count);
4486         }
4487
4488         if (flags & PRINTER_ENUM_REMOTE) {
4489                 return WERR_UNKNOWN_LEVEL;
4490         }
4491
4492         return WERR_OK;
4493 }
4494
4495 /********************************************************************
4496  * handle enumeration of printers at level 4
4497  ********************************************************************/
4498
4499 static WERROR enumprinters_level4(TALLOC_CTX *mem_ctx,
4500                                   uint32_t flags,
4501                                   const char *servername,
4502                                   union spoolss_PrinterInfo **info,
4503                                   uint32_t *count)
4504 {
4505         DEBUG(4,("enum_all_printers_info_4\n"));
4506
4507         return enum_all_printers_info_level(mem_ctx, 4, flags, info, count);
4508 }
4509
4510
4511 /********************************************************************
4512  * handle enumeration of printers at level 5
4513  ********************************************************************/
4514
4515 static WERROR enumprinters_level5(TALLOC_CTX *mem_ctx,
4516                                   uint32_t flags,
4517                                   const char *servername,
4518                                   union spoolss_PrinterInfo **info,
4519                                   uint32_t *count)
4520 {
4521         DEBUG(4,("enum_all_printers_info_5\n"));
4522
4523         return enum_all_printers_info_level(mem_ctx, 5, flags, info, count);
4524 }
4525
4526 /****************************************************************
4527  _spoolss_EnumPrinters
4528 ****************************************************************/
4529
4530 WERROR _spoolss_EnumPrinters(pipes_struct *p,
4531                              struct spoolss_EnumPrinters *r)
4532 {
4533         const char *name;
4534         WERROR result;
4535
4536         /* that's an [in out] buffer */
4537
4538         if (!r->in.buffer && (r->in.offered != 0)) {
4539                 return WERR_INVALID_PARAM;
4540         }
4541
4542         DEBUG(4,("_spoolss_EnumPrinters\n"));
4543
4544         *r->out.needed = 0;
4545         *r->out.count = 0;
4546         *r->out.info = NULL;
4547
4548         /*
4549          * Level 1:
4550          *          flags==PRINTER_ENUM_NAME
4551          *           if name=="" then enumerates all printers
4552          *           if name!="" then enumerate the printer
4553          *          flags==PRINTER_ENUM_REMOTE
4554          *          name is NULL, enumerate printers
4555          * Level 2: name!="" enumerates printers, name can't be NULL
4556          * Level 3: doesn't exist
4557          * Level 4: does a local registry lookup
4558          * Level 5: same as Level 2
4559          */
4560
4561         name = talloc_strdup_upper(p->mem_ctx, r->in.server);
4562         W_ERROR_HAVE_NO_MEMORY(name);
4563
4564         switch (r->in.level) {
4565         case 0:
4566                 result = enumprinters_level0(p->mem_ctx, r->in.flags, name,
4567                                              r->out.info, r->out.count);
4568                 break;
4569         case 1:
4570                 result = enumprinters_level1(p->mem_ctx, r->in.flags, name,
4571                                              r->out.info, r->out.count);
4572                 break;
4573         case 2:
4574                 result = enumprinters_level2(p->mem_ctx, r->in.flags, name,
4575                                              r->out.info, r->out.count);
4576                 break;
4577         case 4:
4578                 result = enumprinters_level4(p->mem_ctx, r->in.flags, name,
4579                                              r->out.info, r->out.count);
4580                 break;
4581         case 5:
4582                 result = enumprinters_level5(p->mem_ctx, r->in.flags, name,
4583                                              r->out.info, r->out.count);
4584                 break;
4585         default:
4586                 return WERR_UNKNOWN_LEVEL;
4587         }
4588
4589         if (!W_ERROR_IS_OK(result)) {
4590                 return result;
4591         }
4592
4593         *r->out.needed  = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
4594                                                      spoolss_EnumPrinters, NULL,
4595                                                      *r->out.info, r->in.level,
4596                                                      *r->out.count);
4597         *r->out.info    = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
4598         *r->out.count   = SPOOLSS_BUFFER_OK(*r->out.count, 0);
4599
4600         return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
4601 }
4602
4603 /****************************************************************
4604  _spoolss_GetPrinter
4605 ****************************************************************/
4606
4607 WERROR _spoolss_GetPrinter(pipes_struct *p,
4608                            struct spoolss_GetPrinter *r)
4609 {
4610         Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
4611         NT_PRINTER_INFO_LEVEL *ntprinter = NULL;
4612         WERROR result = WERR_OK;
4613
4614         int snum;
4615
4616         /* that's an [in out] buffer */
4617
4618         if (!r->in.buffer && (r->in.offered != 0)) {
4619                 return WERR_INVALID_PARAM;
4620         }
4621
4622         *r->out.needed = 0;
4623
4624         if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
4625                 return WERR_BADFID;
4626         }
4627
4628         result = get_a_printer(Printer, &ntprinter, 2,
4629                                lp_const_servicename(snum));
4630         if (!W_ERROR_IS_OK(result)) {
4631                 return result;
4632         }
4633
4634         switch (r->in.level) {
4635         case 0:
4636                 result = construct_printer_info0(p->mem_ctx, ntprinter,
4637                                                  &r->out.info->info0, snum);
4638                 break;
4639         case 1:
4640                 result = construct_printer_info1(p->mem_ctx, ntprinter,
4641                                                  PRINTER_ENUM_ICON8,
4642                                                  &r->out.info->info1, snum);
4643                 break;
4644         case 2:
4645                 result = construct_printer_info2(p->mem_ctx, ntprinter,
4646                                                  &r->out.info->info2, snum);
4647                 break;
4648         case 3:
4649                 result = construct_printer_info3(p->mem_ctx, ntprinter,
4650                                                  &r->out.info->info3, snum);
4651                 break;
4652         case 4:
4653                 result = construct_printer_info4(p->mem_ctx, ntprinter,
4654                                                  &r->out.info->info4, snum);
4655                 break;
4656         case 5:
4657                 result = construct_printer_info5(p->mem_ctx, ntprinter,
4658                                                  &r->out.info->info5, snum);
4659                 break;
4660         case 6:
4661                 result = construct_printer_info6(p->mem_ctx, ntprinter,
4662                                                  &r->out.info->info6, snum);
4663                 break;
4664         case 7:
4665                 result = construct_printer_info7(p->mem_ctx, Printer,
4666                                                  &r->out.info->info7, snum);
4667                 break;
4668         case 8:
4669                 result = construct_printer_info8(p->mem_ctx, ntprinter,
4670                                                  &r->out.info->info8, snum);
4671                 break;
4672         default:
4673                 result = WERR_UNKNOWN_LEVEL;
4674                 break;
4675         }
4676
4677         free_a_printer(&ntprinter, 2);
4678
4679         if (!W_ERROR_IS_OK(result)) {
4680                 TALLOC_FREE(r->out.info);
4681                 return result;
4682         }
4683
4684         *r->out.needed  = SPOOLSS_BUFFER_UNION(spoolss_PrinterInfo, NULL,
4685                                                r->out.info, r->in.level);
4686         r->out.info     = SPOOLSS_BUFFER_OK(r->out.info, NULL);
4687
4688         return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
4689 }
4690
4691 /********************************************************************
4692  ********************************************************************/
4693
4694 static const char **string_array_from_driver_info(TALLOC_CTX *mem_ctx,
4695                                                   fstring *fstring_array,
4696                                                   const char *cservername)
4697 {
4698         int i, num_strings = 0;
4699         const char **array = NULL;
4700
4701         for (i=0; fstring_array && fstring_array[i][0] != '\0'; i++) {
4702
4703                 const char *str = talloc_asprintf(mem_ctx, "\\\\%s%s",
4704                                                   cservername, fstring_array[i]);
4705                 if (!str) {
4706                         TALLOC_FREE(array);
4707                         return NULL;
4708                 }
4709
4710
4711                 if (!add_string_to_array(mem_ctx, str, &array, &num_strings)) {
4712                         TALLOC_FREE(array);
4713                         return NULL;
4714                 }
4715         }
4716
4717         if (i > 0) {
4718                 ADD_TO_ARRAY(mem_ctx, const char *, NULL,
4719                              &array, &num_strings);
4720         }
4721
4722         return array;
4723 }
4724
4725 /********************************************************************
4726  * fill a spoolss_DriverInfo1 struct
4727  ********************************************************************/
4728
4729 static WERROR fill_printer_driver_info1(TALLOC_CTX *mem_ctx,
4730                                         struct spoolss_DriverInfo1 *r,
4731                                         const NT_PRINTER_DRIVER_INFO_LEVEL *driver,
4732                                         const char *servername,
4733                                         const char *architecture)
4734 {
4735         r->driver_name          = talloc_strdup(mem_ctx, driver->info_3->name);
4736         W_ERROR_HAVE_NO_MEMORY(r->driver_name);
4737
4738         return WERR_OK;
4739 }
4740
4741 /********************************************************************
4742  * fill a spoolss_DriverInfo2 struct
4743  ********************************************************************/
4744
4745 static WERROR fill_printer_driver_info2(TALLOC_CTX *mem_ctx,
4746                                         struct spoolss_DriverInfo2 *r,
4747                                         const NT_PRINTER_DRIVER_INFO_LEVEL *driver,
4748                                         const char *servername)
4749
4750 {
4751         const char *cservername = canon_servername(servername);
4752
4753         r->version              = driver->info_3->cversion;
4754
4755         r->driver_name          = talloc_strdup(mem_ctx, driver->info_3->name);
4756         W_ERROR_HAVE_NO_MEMORY(r->driver_name);
4757         r->architecture         = talloc_strdup(mem_ctx, driver->info_3->environment);
4758         W_ERROR_HAVE_NO_MEMORY(r->architecture);
4759
4760         if (strlen(driver->info_3->driverpath)) {
4761                 r->driver_path  = talloc_asprintf(mem_ctx, "\\\\%s%s",
4762                                 cservername, driver->info_3->driverpath);
4763         } else {
4764                 r->driver_path  = talloc_strdup(mem_ctx, "");
4765         }
4766         W_ERROR_HAVE_NO_MEMORY(r->driver_path);
4767
4768         if (strlen(driver->info_3->datafile)) {
4769                 r->data_file    = talloc_asprintf(mem_ctx, "\\\\%s%s",
4770                                 cservername, driver->info_3->datafile);
4771         } else {
4772                 r->data_file    = talloc_strdup(mem_ctx, "");
4773         }
4774         W_ERROR_HAVE_NO_MEMORY(r->data_file);
4775
4776         if (strlen(driver->info_3->configfile)) {
4777                 r->config_file  = talloc_asprintf(mem_ctx, "\\\\%s%s",
4778                                 cservername, driver->info_3->configfile);
4779         } else {
4780                 r->config_file  = talloc_strdup(mem_ctx, "");
4781         }
4782         W_ERROR_HAVE_NO_MEMORY(r->config_file);
4783
4784         return WERR_OK;
4785 }
4786
4787 /********************************************************************
4788  * fill a spoolss_DriverInfo3 struct
4789  ********************************************************************/
4790
4791 static WERROR fill_printer_driver_info3(TALLOC_CTX *mem_ctx,
4792                                         struct spoolss_DriverInfo3 *r,
4793                                         const NT_PRINTER_DRIVER_INFO_LEVEL *driver,
4794                                         const char *servername)
4795 {
4796         const char *cservername = canon_servername(servername);
4797
4798         r->version              = driver->info_3->cversion;
4799
4800         r->driver_name          = talloc_strdup(mem_ctx, driver->info_3->name);
4801         W_ERROR_HAVE_NO_MEMORY(r->driver_name);
4802         r->architecture         = talloc_strdup(mem_ctx, driver->info_3->environment);
4803         W_ERROR_HAVE_NO_MEMORY(r->architecture);
4804
4805         if (strlen(driver->info_3->driverpath)) {
4806                 r->driver_path  = talloc_asprintf(mem_ctx, "\\\\%s%s",
4807                                 cservername, driver->info_3->driverpath);
4808         } else {
4809                 r->driver_path  = talloc_strdup(mem_ctx, "");
4810         }
4811         W_ERROR_HAVE_NO_MEMORY(r->driver_path);
4812
4813         if (strlen(driver->info_3->datafile)) {
4814                 r->data_file    = talloc_asprintf(mem_ctx, "\\\\%s%s",
4815                                 cservername, driver->info_3->datafile);
4816         } else {
4817                 r->data_file    = talloc_strdup(mem_ctx, "");
4818         }
4819         W_ERROR_HAVE_NO_MEMORY(r->data_file);
4820
4821         if (strlen(driver->info_3->configfile)) {
4822                 r->config_file  = talloc_asprintf(mem_ctx, "\\\\%s%s",
4823                                 cservername, driver->info_3->configfile);
4824         } else {
4825                 r->config_file  = talloc_strdup(mem_ctx, "");
4826         }
4827         W_ERROR_HAVE_NO_MEMORY(r->config_file);
4828
4829         if (strlen(driver->info_3->helpfile)) {
4830                 r->help_file    = talloc_asprintf(mem_ctx, "\\\\%s%s",
4831                                 cservername, driver->info_3->helpfile);
4832         } else {
4833                 r->help_file    = talloc_strdup(mem_ctx, "");
4834         }
4835         W_ERROR_HAVE_NO_MEMORY(r->help_file);
4836
4837         r->monitor_name         = talloc_strdup(mem_ctx, driver->info_3->monitorname);
4838         W_ERROR_HAVE_NO_MEMORY(r->monitor_name);
4839         r->default_datatype     = talloc_strdup(mem_ctx, driver->info_3->defaultdatatype);
4840         W_ERROR_HAVE_NO_MEMORY(r->default_datatype);
4841
4842         r->dependent_files = string_array_from_driver_info(mem_ctx,
4843                                                            driver->info_3->dependentfiles,
4844                                                            cservername);
4845         return WERR_OK;
4846 }
4847
4848 /********************************************************************
4849  * fill a spoolss_DriverInfo4 struct
4850  ********************************************************************/
4851
4852 static WERROR fill_printer_driver_info4(TALLOC_CTX *mem_ctx,
4853                                         struct spoolss_DriverInfo4 *r,
4854                                         const NT_PRINTER_DRIVER_INFO_LEVEL *driver,
4855                                         const char *servername)
4856 {
4857         const char *cservername = canon_servername(servername);
4858
4859         r->version              = driver->info_3->cversion;
4860
4861         r->driver_name          = talloc_strdup(mem_ctx, driver->info_3->name);
4862         W_ERROR_HAVE_NO_MEMORY(r->driver_name);
4863         r->architecture         = talloc_strdup(mem_ctx, driver->info_3->environment);
4864         W_ERROR_HAVE_NO_MEMORY(r->architecture);
4865
4866         if (strlen(driver->info_3->driverpath)) {
4867                 r->driver_path  = talloc_asprintf(mem_ctx, "\\\\%s%s",
4868                                 cservername, driver->info_3->driverpath);
4869         } else {
4870                 r->driver_path  = talloc_strdup(mem_ctx, "");
4871         }
4872         W_ERROR_HAVE_NO_MEMORY(r->driver_path);
4873
4874         if (strlen(driver->info_3->datafile)) {
4875                 r->data_file    = talloc_asprintf(mem_ctx, "\\\\%s%s",
4876                                 cservername, driver->info_3->datafile);
4877         } else {
4878                 r->data_file    = talloc_strdup(mem_ctx, "");
4879         }
4880         W_ERROR_HAVE_NO_MEMORY(r->data_file);
4881
4882         if (strlen(driver->info_3->configfile)) {
4883                 r->config_file  = talloc_asprintf(mem_ctx, "\\\\%s%s",
4884                                 cservername, driver->info_3->configfile);
4885         } else {
4886                 r->config_file  = talloc_strdup(mem_ctx, "");
4887         }
4888         W_ERROR_HAVE_NO_MEMORY(r->config_file);
4889
4890         if (strlen(driver->info_3->helpfile)) {
4891                 r->help_file    = talloc_asprintf(mem_ctx, "\\\\%s%s",
4892                                 cservername, driver->info_3->helpfile);
4893         } else {
4894                 r->help_file    = talloc_strdup(mem_ctx, "");
4895         }
4896         W_ERROR_HAVE_NO_MEMORY(r->help_file);
4897
4898         r->dependent_files = string_array_from_driver_info(mem_ctx,
4899                                                            driver->info_3->dependentfiles,
4900                                                            cservername);
4901
4902
4903         r->monitor_name         = talloc_strdup(mem_ctx, driver->info_3->monitorname);
4904         W_ERROR_HAVE_NO_MEMORY(r->monitor_name);
4905         r->default_datatype     = talloc_strdup(mem_ctx, driver->info_3->defaultdatatype);
4906         W_ERROR_HAVE_NO_MEMORY(r->default_datatype);
4907
4908         r->previous_names = string_array_from_driver_info(mem_ctx,
4909                                                           NULL,
4910                                                           cservername);
4911
4912         return WERR_OK;
4913 }
4914
4915 /********************************************************************
4916  * fill a spoolss_DriverInfo5 struct
4917  ********************************************************************/
4918
4919 static WERROR fill_printer_driver_info5(TALLOC_CTX *mem_ctx,
4920                                         struct spoolss_DriverInfo5 *r,
4921                                         const NT_PRINTER_DRIVER_INFO_LEVEL *driver,
4922                                         const char *servername)
4923 {
4924         const char *cservername = canon_servername(servername);
4925
4926         r->version              = driver->info_3->cversion;
4927
4928         r->driver_name          = talloc_strdup(mem_ctx, driver->info_3->name);
4929         W_ERROR_HAVE_NO_MEMORY(r->driver_name);
4930         r->architecture         = talloc_strdup(mem_ctx, driver->info_3->environment);
4931         W_ERROR_HAVE_NO_MEMORY(r->architecture);
4932
4933         if (strlen(driver->info_3->driverpath)) {
4934                 r->driver_path  = talloc_asprintf(mem_ctx, "\\\\%s%s",
4935                                 cservername, driver->info_3->driverpath);
4936         } else {
4937                 r->driver_path  = talloc_strdup(mem_ctx, "");
4938         }
4939         W_ERROR_HAVE_NO_MEMORY(r->driver_path);
4940
4941         if (strlen(driver->info_3->datafile)) {
4942                 r->data_file    = talloc_asprintf(mem_ctx, "\\\\%s%s",
4943                                 cservername, driver->info_3->datafile);
4944         } else {
4945                 r->data_file    = talloc_strdup(mem_ctx, "");
4946         }
4947         W_ERROR_HAVE_NO_MEMORY(r->data_file);
4948
4949         if (strlen(driver->info_3->configfile)) {
4950                 r->config_file  = talloc_asprintf(mem_ctx, "\\\\%s%s",
4951                                 cservername, driver->info_3->configfile);
4952         } else {
4953                 r->config_file  = talloc_strdup(mem_ctx, "");
4954         }
4955         W_ERROR_HAVE_NO_MEMORY(r->config_file);
4956
4957         r->driver_attributes    = 0;
4958         r->config_version       = 0;
4959         r->driver_version       = 0;
4960
4961         return WERR_OK;
4962 }
4963 /********************************************************************
4964  * fill a spoolss_DriverInfo6 struct
4965  ********************************************************************/
4966
4967 static WERROR fill_printer_driver_info6(TALLOC_CTX *mem_ctx,
4968                                         struct spoolss_DriverInfo6 *r,
4969                                         const NT_PRINTER_DRIVER_INFO_LEVEL *driver,
4970                                         const char *servername)
4971 {
4972         const char *cservername = canon_servername(servername);
4973
4974         r->version              = driver->info_3->cversion;
4975
4976         r->driver_name          = talloc_strdup(mem_ctx, driver->info_3->name);
4977         W_ERROR_HAVE_NO_MEMORY(r->driver_name);
4978         r->architecture         = talloc_strdup(mem_ctx, driver->info_3->environment);
4979         W_ERROR_HAVE_NO_MEMORY(r->architecture);
4980
4981         if (strlen(driver->info_3->driverpath)) {
4982                 r->driver_path  = talloc_asprintf(mem_ctx, "\\\\%s%s",
4983                                 cservername, driver->info_3->driverpath);
4984         } else {
4985                 r->driver_path  = talloc_strdup(mem_ctx, "");
4986         }
4987         W_ERROR_HAVE_NO_MEMORY(r->driver_path);
4988
4989         if (strlen(driver->info_3->datafile)) {
4990                 r->data_file    = talloc_asprintf(mem_ctx, "\\\\%s%s",
4991                                 cservername, driver->info_3->datafile);
4992         } else {
4993                 r->data_file    = talloc_strdup(mem_ctx, "");
4994         }
4995         W_ERROR_HAVE_NO_MEMORY(r->data_file);
4996
4997         if (strlen(driver->info_3->configfile)) {
4998                 r->config_file  = talloc_asprintf(mem_ctx, "\\\\%s%s",
4999                                 cservername, driver->info_3->configfile);
5000         } else {
5001                 r->config_file  = talloc_strdup(mem_ctx, "");
5002         }
5003         W_ERROR_HAVE_NO_MEMORY(r->config_file);
5004
5005         if (strlen(driver->info_3->helpfile)) {
5006                 r->help_file    = talloc_asprintf(mem_ctx, "\\\\%s%s",
5007                                 cservername, driver->info_3->helpfile);
5008         } else {
5009                 r->help_file    = talloc_strdup(mem_ctx, "");
5010         }
5011         W_ERROR_HAVE_NO_MEMORY(r->help_file);
5012
5013         r->monitor_name         = talloc_strdup(mem_ctx, driver->info_3->monitorname);
5014         W_ERROR_HAVE_NO_MEMORY(r->monitor_name);
5015         r->default_datatype     = talloc_strdup(mem_ctx, driver->info_3->defaultdatatype);
5016         W_ERROR_HAVE_NO_MEMORY(r->default_datatype);
5017
5018         r->dependent_files = string_array_from_driver_info(mem_ctx,
5019                                                            driver->info_3->dependentfiles,
5020                                                            cservername);
5021         r->previous_names = string_array_from_driver_info(mem_ctx,
5022                                                           NULL,
5023                                                           cservername);
5024
5025         r->driver_date          = 0;
5026         r->driver_version       = 0;
5027
5028         r->manufacturer_name    = talloc_strdup(mem_ctx, "");
5029         W_ERROR_HAVE_NO_MEMORY(r->manufacturer_name);
5030         r->manufacturer_url     = talloc_strdup(mem_ctx, "");
5031         W_ERROR_HAVE_NO_MEMORY(r->manufacturer_url);
5032         r->hardware_id          = talloc_strdup(mem_ctx, "");
5033         W_ERROR_HAVE_NO_MEMORY(r->hardware_id);
5034         r->provider             = talloc_strdup(mem_ctx, "");
5035         W_ERROR_HAVE_NO_MEMORY(r->provider);
5036
5037         return WERR_OK;
5038 }
5039
5040 /********************************************************************
5041  ********************************************************************/
5042
5043 static WERROR fill_spoolss_DriverFileInfo(TALLOC_CTX *mem_ctx,
5044                                           struct spoolss_DriverFileInfo *r,
5045                                           const char *cservername,
5046                                           const char *file_name,
5047                                           enum spoolss_DriverFileType file_type,
5048                                           uint32_t file_version)
5049 {
5050         r->file_name    = talloc_asprintf(mem_ctx, "\\\\%s%s",
5051                                           cservername, file_name);
5052         W_ERROR_HAVE_NO_MEMORY(r->file_name);
5053         r->file_type    = file_type;
5054         r->file_version = file_version;
5055
5056         return WERR_OK;
5057 }
5058
5059 /********************************************************************
5060  ********************************************************************/
5061
5062 static WERROR spoolss_DriverFileInfo_from_driver(TALLOC_CTX *mem_ctx,
5063                                                  const NT_PRINTER_DRIVER_INFO_LEVEL *driver,
5064                                                  const char *cservername,
5065                                                  struct spoolss_DriverFileInfo **info_p,
5066                                                  uint32_t *count_p)
5067 {
5068         struct spoolss_DriverFileInfo *info = NULL;
5069         uint32_t count = 0;
5070         WERROR result;
5071         uint32_t i;
5072
5073         *info_p = NULL;
5074         *count_p = 0;
5075
5076         if (strlen(driver->info_3->driverpath)) {
5077                 info = TALLOC_REALLOC_ARRAY(mem_ctx, info,
5078                                             struct spoolss_DriverFileInfo,
5079                                             count + 1);
5080                 W_ERROR_HAVE_NO_MEMORY(info);
5081                 result = fill_spoolss_DriverFileInfo(info,
5082                                                      &info[count],
5083                                                      cservername,
5084                                                      driver->info_3->driverpath,
5085                                                      SPOOLSS_DRIVER_FILE_TYPE_RENDERING,
5086                                                      0);
5087                 W_ERROR_NOT_OK_RETURN(result);
5088                 count++;
5089         }
5090
5091         if (strlen(driver->info_3->configfile)) {
5092                 info = TALLOC_REALLOC_ARRAY(mem_ctx, info,
5093                                             struct spoolss_DriverFileInfo,
5094                                             count + 1);
5095                 W_ERROR_HAVE_NO_MEMORY(info);
5096                 result = fill_spoolss_DriverFileInfo(info,
5097                                                      &info[count],
5098                                                      cservername,
5099                                                      driver->info_3->configfile,
5100                                                      SPOOLSS_DRIVER_FILE_TYPE_CONFIGURATION,
5101                                                      0);
5102                 W_ERROR_NOT_OK_RETURN(result);
5103                 count++;
5104         }
5105
5106         if (strlen(driver->info_3->datafile)) {
5107                 info = TALLOC_REALLOC_ARRAY(mem_ctx, info,
5108                                             struct spoolss_DriverFileInfo,
5109                                             count + 1);
5110                 W_ERROR_HAVE_NO_MEMORY(info);
5111                 result = fill_spoolss_DriverFileInfo(info,
5112                                                      &info[count],
5113                                                      cservername,
5114                                                      driver->info_3->datafile,
5115                                                      SPOOLSS_DRIVER_FILE_TYPE_DATA,
5116                                                      0);
5117                 W_ERROR_NOT_OK_RETURN(result);
5118                 count++;
5119         }
5120
5121         if (strlen(driver->info_3->helpfile)) {
5122                 info = TALLOC_REALLOC_ARRAY(mem_ctx, info,
5123                                             struct spoolss_DriverFileInfo,
5124                                             count + 1);
5125                 W_ERROR_HAVE_NO_MEMORY(info);
5126                 result = fill_spoolss_DriverFileInfo(info,
5127                                                      &info[count],
5128                                                      cservername,
5129                                                      driver->info_3->helpfile,
5130                                                      SPOOLSS_DRIVER_FILE_TYPE_HELP,
5131                                                      0);
5132                 W_ERROR_NOT_OK_RETURN(result);
5133                 count++;
5134         }
5135
5136         for (i=0; driver->info_3->dependentfiles[i][0] != '\0'; i++) {
5137                 info = TALLOC_REALLOC_ARRAY(mem_ctx, info,
5138                                             struct spoolss_DriverFileInfo,
5139                                             count + 1);
5140                 W_ERROR_HAVE_NO_MEMORY(info);
5141                 result = fill_spoolss_DriverFileInfo(info,
5142                                                      &info[count],
5143                                                      cservername,
5144                                                      driver->info_3->dependentfiles[i],
5145                                                      SPOOLSS_DRIVER_FILE_TYPE_OTHER,
5146                                                      0);
5147                 W_ERROR_NOT_OK_RETURN(result);
5148                 count++;
5149         }
5150
5151         *info_p = info;
5152         *count_p = count;
5153
5154         return WERR_OK;
5155 }
5156
5157 /********************************************************************
5158  * fill a spoolss_DriverInfo101 sttruct
5159  ********************************************************************/
5160
5161 static WERROR fill_printer_driver_info101(TALLOC_CTX *mem_ctx,
5162                                           struct spoolss_DriverInfo101 *r,
5163                                           const NT_PRINTER_DRIVER_INFO_LEVEL *driver,
5164                                           const char *servername)
5165 {
5166         const char *cservername = canon_servername(servername);
5167         WERROR result;
5168
5169         r->version              = driver->info_3->cversion;
5170
5171         r->driver_name          = talloc_strdup(mem_ctx, driver->info_3->name);
5172         W_ERROR_HAVE_NO_MEMORY(r->driver_name);
5173         r->architecture         = talloc_strdup(mem_ctx, driver->info_3->environment);
5174         W_ERROR_HAVE_NO_MEMORY(r->architecture);
5175
5176         result = spoolss_DriverFileInfo_from_driver(mem_ctx, driver,
5177                                                     cservername,
5178                                                     &r->file_info,
5179                                                     &r->file_count);
5180         if (!W_ERROR_IS_OK(result)) {
5181                 return result;
5182         }
5183
5184         r->monitor_name         = talloc_strdup(mem_ctx, driver->info_3->monitorname);
5185         W_ERROR_HAVE_NO_MEMORY(r->monitor_name);
5186
5187         r->default_datatype     = talloc_strdup(mem_ctx, driver->info_3->defaultdatatype);
5188         W_ERROR_HAVE_NO_MEMORY(r->default_datatype);
5189
5190         r->previous_names = string_array_from_driver_info(mem_ctx,
5191                                                           NULL,
5192                                                           cservername);
5193         r->driver_date          = 0;
5194         r->driver_version       = 0;
5195
5196         r->manufacturer_name    = talloc_strdup(mem_ctx, "");
5197         W_ERROR_HAVE_NO_MEMORY(r->manufacturer_name);
5198         r->manufacturer_url     = talloc_strdup(mem_ctx, "");
5199         W_ERROR_HAVE_NO_MEMORY(r->manufacturer_url);
5200         r->hardware_id          = talloc_strdup(mem_ctx, "");
5201         W_ERROR_HAVE_NO_MEMORY(r->hardware_id);
5202         r->provider             = talloc_strdup(mem_ctx, "");
5203         W_ERROR_HAVE_NO_MEMORY(r->provider);
5204
5205         return WERR_OK;
5206 }
5207
5208 /********************************************************************
5209  * construct_printer_driver_info_1
5210  ********************************************************************/
5211
5212 static WERROR construct_printer_driver_info_1(TALLOC_CTX *mem_ctx,
5213                                               struct spoolss_DriverInfo1 *r,
5214                                               int snum,
5215                                               const char *servername,
5216                                               const char *architecture,
5217                                               uint32_t version)
5218 {
5219         NT_PRINTER_INFO_LEVEL *printer = NULL;
5220         NT_PRINTER_DRIVER_INFO_LEVEL driver;
5221         WERROR result;
5222
5223         ZERO_STRUCT(driver);
5224
5225         if (!W_ERROR_IS_OK(get_a_printer(NULL, &printer, 2, lp_const_servicename(snum))))
5226                 return WERR_INVALID_PRINTER_NAME;
5227
5228         if (!W_ERROR_IS_OK(get_a_printer_driver(&driver, 3, printer->info_2->drivername, architecture, version))) {
5229                 free_a_printer(&printer, 2);
5230                 return WERR_UNKNOWN_PRINTER_DRIVER;
5231         }
5232
5233         result = fill_printer_driver_info1(mem_ctx, r, &driver, servername, architecture);
5234
5235         free_a_printer(&printer,2);
5236
5237         return result;
5238 }
5239
5240 /********************************************************************
5241  * construct_printer_driver_info_2
5242  * fill a printer_info_2 struct
5243  ********************************************************************/
5244
5245 static WERROR construct_printer_driver_info_2(TALLOC_CTX *mem_ctx,
5246                                               struct spoolss_DriverInfo2 *r,
5247                                               int snum,
5248                                               const char *servername,
5249                                               const char *architecture,
5250                                               uint32_t version)
5251 {
5252         NT_PRINTER_INFO_LEVEL *printer = NULL;
5253         NT_PRINTER_DRIVER_INFO_LEVEL driver;
5254         WERROR result;
5255
5256         ZERO_STRUCT(printer);
5257         ZERO_STRUCT(driver);
5258
5259         if (!W_ERROR_IS_OK(get_a_printer(NULL, &printer, 2, lp_const_servicename(snum))))
5260                 return WERR_INVALID_PRINTER_NAME;
5261
5262         if (!W_ERROR_IS_OK(get_a_printer_driver(&driver, 3, printer->info_2->drivername, architecture, version))) {
5263                 free_a_printer(&printer, 2);
5264                 return WERR_UNKNOWN_PRINTER_DRIVER;
5265         }
5266
5267         result = fill_printer_driver_info2(mem_ctx, r, &driver, servername);
5268
5269         free_a_printer(&printer,2);
5270
5271         return result;
5272 }
5273
5274 /********************************************************************
5275  * construct_printer_info_3
5276  * fill a printer_info_3 struct
5277  ********************************************************************/
5278
5279 static WERROR construct_printer_driver_info_3(TALLOC_CTX *mem_ctx,
5280                                               struct spoolss_DriverInfo3 *r,
5281                                               int snum,
5282                                               const char *servername,
5283                                               const char *architecture,
5284                                               uint32_t version)
5285 {
5286         NT_PRINTER_INFO_LEVEL *printer = NULL;
5287         NT_PRINTER_DRIVER_INFO_LEVEL driver;
5288         WERROR status;
5289         ZERO_STRUCT(driver);
5290
5291         status=get_a_printer(NULL, &printer, 2, lp_const_servicename(snum) );
5292         DEBUG(8,("construct_printer_driver_info_3: status: %s\n", win_errstr(status)));
5293         if (!W_ERROR_IS_OK(status))
5294                 return WERR_INVALID_PRINTER_NAME;
5295
5296         status=get_a_printer_driver(&driver, 3, printer->info_2->drivername, architecture, version);
5297         DEBUG(8,("construct_printer_driver_info_3: status: %s\n", win_errstr(status)));
5298
5299 #if 0   /* JERRY */
5300
5301         /*
5302          * I put this code in during testing.  Helpful when commenting out the
5303          * support for DRIVER_INFO_6 in regards to win2k.  Not needed in general
5304          * as win2k always queries the driver using an infor level of 6.
5305          * I've left it in (but ifdef'd out) because I'll probably
5306          * use it in experimentation again in the future.   --jerry 22/01/2002
5307          */
5308
5309         if (!W_ERROR_IS_OK(status)) {
5310                 /*
5311                  * Is this a W2k client ?
5312                  */
5313                 if (version == 3) {
5314                         /* Yes - try again with a WinNT driver. */
5315                         version = 2;
5316                         status=get_a_printer_driver(&driver, 3, printer->info_2->drivername, architecture, version);
5317                         DEBUG(8,("construct_printer_driver_info_3: status: %s\n", win_errstr(status)));
5318                 }
5319 #endif
5320
5321                 if (!W_ERROR_IS_OK(status)) {
5322                         free_a_printer(&printer,2);
5323                         return WERR_UNKNOWN_PRINTER_DRIVER;
5324                 }
5325
5326 #if 0   /* JERRY */
5327         }
5328 #endif
5329
5330
5331         status = fill_printer_driver_info3(mem_ctx, r, &driver, servername);
5332
5333         free_a_printer(&printer,2);
5334
5335         return status;
5336 }
5337
5338 /********************************************************************
5339  * construct_printer_info_6
5340  * fill a printer_info_6 struct
5341  ********************************************************************/
5342
5343 static WERROR construct_printer_driver_info_6(TALLOC_CTX *mem_ctx,
5344                                               struct spoolss_DriverInfo6 *r,
5345                                               int snum,
5346                                               const char *servername,
5347                                               const char *architecture,
5348                                               uint32_t version)
5349 {
5350         NT_PRINTER_INFO_LEVEL           *printer = NULL;
5351         NT_PRINTER_DRIVER_INFO_LEVEL    driver;
5352         WERROR                          status;
5353
5354         ZERO_STRUCT(driver);
5355
5356         status=get_a_printer(NULL, &printer, 2, lp_const_servicename(snum) );
5357
5358         DEBUG(8,("construct_printer_driver_info_6: status: %s\n", win_errstr(status)));
5359
5360         if (!W_ERROR_IS_OK(status))
5361                 return WERR_INVALID_PRINTER_NAME;
5362
5363         status = get_a_printer_driver(&driver, 3, printer->info_2->drivername, architecture, version);
5364
5365         DEBUG(8,("construct_printer_driver_info_6: status: %s\n", win_errstr(status)));
5366
5367         if (!W_ERROR_IS_OK(status))
5368         {
5369                 /*
5370                  * Is this a W2k client ?
5371                  */
5372
5373                 if (version < 3) {
5374                         free_a_printer(&printer,2);
5375                         return WERR_UNKNOWN_PRINTER_DRIVER;
5376                 }
5377
5378                 /* Yes - try again with a WinNT driver. */
5379                 version = 2;
5380                 status=get_a_printer_driver(&driver, 3, printer->info_2->drivername, architecture, version);
5381                 DEBUG(8,("construct_printer_driver_info_6: status: %s\n", win_errstr(status)));
5382                 if (!W_ERROR_IS_OK(status)) {
5383                         free_a_printer(&printer,2);
5384                         return WERR_UNKNOWN_PRINTER_DRIVER;
5385                 }
5386         }
5387
5388         status = fill_printer_driver_info6(mem_ctx, r, &driver, servername);
5389
5390         free_a_printer(&printer,2);
5391         free_a_printer_driver(driver, 3);
5392
5393         return status;
5394 }
5395
5396 /********************************************************************
5397  * construct_printer_info_101
5398  * fill a printer_info_101 struct
5399  ********************************************************************/
5400
5401 static WERROR construct_printer_driver_info_101(TALLOC_CTX *mem_ctx,
5402                                                 struct spoolss_DriverInfo101 *r,
5403                                                 int snum,
5404                                                 const char *servername,
5405                                                 const char *architecture,
5406                                                 uint32_t version)
5407 {
5408         NT_PRINTER_INFO_LEVEL           *printer = NULL;
5409         NT_PRINTER_DRIVER_INFO_LEVEL    driver;
5410         WERROR                          result;
5411
5412         ZERO_STRUCT(driver);
5413
5414         result = get_a_printer(NULL, &printer, 2, lp_const_servicename(snum));
5415
5416         DEBUG(8,("construct_printer_driver_info_101: status: %s\n",
5417                 win_errstr(result)));
5418
5419         if (!W_ERROR_IS_OK(result)) {
5420                 return WERR_INVALID_PRINTER_NAME;
5421         }
5422
5423         result = get_a_printer_driver(&driver, 3, printer->info_2->drivername,
5424                                       architecture, version);
5425
5426         DEBUG(8,("construct_printer_driver_info_101: status: %s\n",
5427                 win_errstr(result)));
5428
5429         if (!W_ERROR_IS_OK(result)) {
5430                 /*
5431                  * Is this a W2k client ?
5432                  */
5433
5434                 if (version < 3) {
5435                         free_a_printer(&printer, 2);
5436                         return WERR_UNKNOWN_PRINTER_DRIVER;
5437                 }
5438
5439                 /* Yes - try again with a WinNT driver. */
5440                 version = 2;
5441                 result = get_a_printer_driver(&driver, 3, printer->info_2->drivername,
5442                                               architecture, version);
5443                 DEBUG(8,("construct_printer_driver_info_6: status: %s\n",
5444                         win_errstr(result)));
5445                 if (!W_ERROR_IS_OK(result)) {
5446                         free_a_printer(&printer, 2);
5447                         return WERR_UNKNOWN_PRINTER_DRIVER;
5448                 }
5449         }
5450
5451         result = fill_printer_driver_info101(mem_ctx, r, &driver, servername);
5452
5453         free_a_printer(&printer, 2);
5454         free_a_printer_driver(driver, 3);
5455
5456         return result;
5457 }
5458
5459 /****************************************************************
5460  _spoolss_GetPrinterDriver2
5461 ****************************************************************/
5462
5463 WERROR _spoolss_GetPrinterDriver2(pipes_struct *p,
5464                                   struct spoolss_GetPrinterDriver2 *r)
5465 {
5466         Printer_entry *printer;
5467         WERROR result;
5468
5469         const char *servername;
5470         int snum;
5471
5472         /* that's an [in out] buffer */
5473
5474         if (!r->in.buffer && (r->in.offered != 0)) {
5475                 return WERR_INVALID_PARAM;
5476         }
5477
5478         DEBUG(4,("_spoolss_GetPrinterDriver2\n"));
5479
5480         if (!(printer = find_printer_index_by_hnd(p, r->in.handle))) {
5481                 DEBUG(0,("_spoolss_GetPrinterDriver2: invalid printer handle!\n"));
5482                 return WERR_INVALID_PRINTER_NAME;
5483         }
5484
5485         *r->out.needed = 0;
5486         *r->out.server_major_version = 0;
5487         *r->out.server_minor_version = 0;
5488
5489         servername = get_server_name(printer);
5490
5491         if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
5492                 return WERR_BADFID;
5493         }
5494
5495         switch (r->in.level) {
5496         case 1:
5497                 result = construct_printer_driver_info_1(p->mem_ctx,
5498                                                          &r->out.info->info1,
5499                                                          snum,
5500                                                          servername,
5501                                                          r->in.architecture,
5502                                                          r->in.client_major_version);
5503                 break;
5504         case 2:
5505                 result = construct_printer_driver_info_2(p->mem_ctx,
5506                                                          &r->out.info->info2,
5507                                                          snum,
5508                                                          servername,
5509                                                          r->in.architecture,
5510                                                          r->in.client_major_version);
5511                 break;
5512         case 3:
5513                 result = construct_printer_driver_info_3(p->mem_ctx,
5514                                                          &r->out.info->info3,
5515                                                          snum,
5516                                                          servername,
5517                                                          r->in.architecture,
5518                                                          r->in.client_major_version);
5519                 break;
5520         case 6:
5521                 result = construct_printer_driver_info_6(p->mem_ctx,
5522                                                          &r->out.info->info6,
5523                                                          snum,
5524                                                          servername,
5525                                                          r->in.architecture,
5526                                                          r->in.client_major_version);
5527                 break;
5528         case 101:
5529                 result = construct_printer_driver_info_101(p->mem_ctx,
5530                                                            &r->out.info->info101,
5531                                                            snum,
5532                                                            servername,
5533                                                            r->in.architecture,
5534                                                            r->in.client_major_version);
5535                 break;
5536         default:
5537                 result = WERR_UNKNOWN_LEVEL;
5538                 break;
5539         }
5540
5541         if (!W_ERROR_IS_OK(result)) {
5542                 TALLOC_FREE(r->out.info);
5543                 return result;
5544         }
5545
5546         *r->out.needed  = SPOOLSS_BUFFER_UNION(spoolss_DriverInfo, NULL,
5547                                                r->out.info, r->in.level);
5548         r->out.info     = SPOOLSS_BUFFER_OK(r->out.info, NULL);
5549
5550         return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
5551 }
5552
5553
5554 /****************************************************************
5555  _spoolss_StartPagePrinter
5556 ****************************************************************/
5557
5558 WERROR _spoolss_StartPagePrinter(pipes_struct *p,
5559                                  struct spoolss_StartPagePrinter *r)
5560 {
5561         Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
5562
5563         if (!Printer) {
5564                 DEBUG(3,("_spoolss_StartPagePrinter: "
5565                         "Error in startpageprinter printer handle\n"));
5566                 return WERR_BADFID;
5567         }
5568
5569         Printer->page_started = true;
5570         return WERR_OK;
5571 }
5572
5573 /****************************************************************
5574  _spoolss_EndPagePrinter
5575 ****************************************************************/
5576
5577 WERROR _spoolss_EndPagePrinter(pipes_struct *p,
5578                                struct spoolss_EndPagePrinter *r)
5579 {
5580         int snum;
5581
5582         Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
5583
5584         if (!Printer) {
5585                 DEBUG(2,("_spoolss_EndPagePrinter: Invalid handle (%s:%u:%u).\n",
5586                         OUR_HANDLE(r->in.handle)));
5587                 return WERR_BADFID;
5588         }
5589
5590         if (!get_printer_snum(p, r->in.handle, &snum, NULL))
5591                 return WERR_BADFID;
5592
5593         Printer->page_started = false;
5594         print_job_endpage(snum, Printer->jobid);
5595
5596         return WERR_OK;
5597 }
5598
5599 /****************************************************************
5600  _spoolss_StartDocPrinter
5601 ****************************************************************/
5602
5603 WERROR _spoolss_StartDocPrinter(pipes_struct *p,
5604                                 struct spoolss_StartDocPrinter *r)
5605 {
5606         struct spoolss_DocumentInfo1 *info_1;
5607         int snum;
5608         Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
5609
5610         if (!Printer) {
5611                 DEBUG(2,("_spoolss_StartDocPrinter: "
5612                         "Invalid handle (%s:%u:%u)\n",
5613                         OUR_HANDLE(r->in.handle)));
5614                 return WERR_BADFID;
5615         }
5616
5617         if (r->in.level != 1) {
5618                 return WERR_UNKNOWN_LEVEL;
5619         }
5620
5621         info_1 = r->in.info.info1;
5622
5623         /*
5624          * a nice thing with NT is it doesn't listen to what you tell it.
5625          * when asked to send _only_ RAW datas, it tries to send datas
5626          * in EMF format.
5627          *
5628          * So I add checks like in NT Server ...
5629          */
5630
5631         if (info_1->datatype) {
5632                 if (strcmp(info_1->datatype, "RAW") != 0) {
5633                         *r->out.job_id = 0;
5634                         return WERR_INVALID_DATATYPE;
5635                 }
5636         }
5637
5638         /* get the share number of the printer */
5639         if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
5640                 return WERR_BADFID;
5641         }
5642
5643         Printer->jobid = print_job_start(p->server_info, snum,
5644                                          info_1->document_name,
5645                                          Printer->nt_devmode);
5646
5647         /* An error occured in print_job_start() so return an appropriate
5648            NT error code. */
5649
5650         if (Printer->jobid == -1) {
5651                 return map_werror_from_unix(errno);
5652         }
5653
5654         Printer->document_started = true;
5655         *r->out.job_id = Printer->jobid;
5656
5657         return WERR_OK;
5658 }
5659
5660 /****************************************************************
5661  _spoolss_EndDocPrinter
5662 ****************************************************************/
5663
5664 WERROR _spoolss_EndDocPrinter(pipes_struct *p,
5665                               struct spoolss_EndDocPrinter *r)
5666 {
5667         return _spoolss_enddocprinter_internal(p, r->in.handle);
5668 }
5669
5670 /****************************************************************
5671  _spoolss_WritePrinter
5672 ****************************************************************/
5673
5674 WERROR _spoolss_WritePrinter(pipes_struct *p,
5675                              struct spoolss_WritePrinter *r)
5676 {
5677         uint32_t buffer_written;
5678         int snum;
5679         Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
5680
5681         if (!Printer) {
5682                 DEBUG(2,("_spoolss_WritePrinter: Invalid handle (%s:%u:%u)\n",
5683                         OUR_HANDLE(r->in.handle)));
5684                 *r->out.num_written = r->in._data_size;
5685                 return WERR_BADFID;
5686         }
5687
5688         if (!get_printer_snum(p, r->in.handle, &snum, NULL))
5689                 return WERR_BADFID;
5690
5691         buffer_written = (uint32_t)print_job_write(snum, Printer->jobid,
5692                                                    (const char *)r->in.data.data,
5693                                                    (SMB_OFF_T)-1,
5694                                                    (size_t)r->in._data_size);
5695         if (buffer_written == (uint32_t)-1) {
5696                 *r->out.num_written = 0;
5697                 if (errno == ENOSPC)
5698                         return WERR_NO_SPOOL_SPACE;
5699                 else
5700                         return WERR_ACCESS_DENIED;
5701         }
5702
5703         *r->out.num_written = r->in._data_size;
5704
5705         return WERR_OK;
5706 }
5707
5708 /********************************************************************
5709  * api_spoolss_getprinter
5710  * called from the spoolss dispatcher
5711  *
5712  ********************************************************************/
5713
5714 static WERROR control_printer(struct policy_handle *handle, uint32_t command,
5715                               pipes_struct *p)
5716 {
5717         int snum;
5718         WERROR errcode = WERR_BADFUNC;
5719         Printer_entry *Printer = find_printer_index_by_hnd(p, handle);
5720
5721         if (!Printer) {
5722                 DEBUG(2,("control_printer: Invalid handle (%s:%u:%u)\n",
5723                         OUR_HANDLE(handle)));
5724                 return WERR_BADFID;
5725         }
5726
5727         if (!get_printer_snum(p, handle, &snum, NULL))
5728                 return WERR_BADFID;
5729
5730         switch (command) {
5731         case SPOOLSS_PRINTER_CONTROL_PAUSE:
5732                 errcode = print_queue_pause(p->server_info, snum);
5733                 break;
5734         case SPOOLSS_PRINTER_CONTROL_RESUME:
5735         case SPOOLSS_PRINTER_CONTROL_UNPAUSE:
5736                 errcode = print_queue_resume(p->server_info, snum);
5737                 break;
5738         case SPOOLSS_PRINTER_CONTROL_PURGE:
5739                 errcode = print_queue_purge(p->server_info, snum);
5740                 break;
5741         default:
5742                 return WERR_UNKNOWN_LEVEL;
5743         }
5744
5745         return errcode;
5746 }
5747
5748
5749 /****************************************************************
5750  _spoolss_AbortPrinter
5751  * From MSDN: "Deletes printer's spool file if printer is configured
5752  * for spooling"
5753 ****************************************************************/
5754
5755 WERROR _spoolss_AbortPrinter(pipes_struct *p,
5756                              struct spoolss_AbortPrinter *r)
5757 {
5758         Printer_entry   *Printer = find_printer_index_by_hnd(p, r->in.handle);
5759         int             snum;
5760         WERROR          errcode = WERR_OK;
5761
5762         if (!Printer) {
5763                 DEBUG(2,("_spoolss_AbortPrinter: Invalid handle (%s:%u:%u)\n",
5764                         OUR_HANDLE(r->in.handle)));
5765                 return WERR_BADFID;
5766         }
5767
5768         if (!get_printer_snum(p, r->in.handle, &snum, NULL))
5769                 return WERR_BADFID;
5770
5771         print_job_delete(p->server_info, snum, Printer->jobid, &errcode );
5772
5773         return errcode;
5774 }
5775
5776 /********************************************************************
5777  * called by spoolss_api_setprinter
5778  * when updating a printer description
5779  ********************************************************************/
5780
5781 static WERROR update_printer_sec(struct policy_handle *handle,
5782                                  pipes_struct *p, SEC_DESC_BUF *secdesc_ctr)
5783 {
5784         SEC_DESC_BUF *new_secdesc_ctr = NULL, *old_secdesc_ctr = NULL;
5785         WERROR result;
5786         int snum;
5787
5788         Printer_entry *Printer = find_printer_index_by_hnd(p, handle);
5789
5790         if (!Printer || !get_printer_snum(p, handle, &snum, NULL)) {
5791                 DEBUG(2,("update_printer_sec: Invalid handle (%s:%u:%u)\n",
5792                          OUR_HANDLE(handle)));
5793
5794                 result = WERR_BADFID;
5795                 goto done;
5796         }
5797
5798         if (!secdesc_ctr) {
5799                 DEBUG(10,("update_printer_sec: secdesc_ctr is NULL !\n"));
5800                 result = WERR_INVALID_PARAM;
5801                 goto done;
5802         }
5803
5804         /* Check the user has permissions to change the security
5805            descriptor.  By experimentation with two NT machines, the user
5806            requires Full Access to the printer to change security
5807            information. */
5808
5809         if ( Printer->access_granted != PRINTER_ACCESS_ADMINISTER ) {
5810                 DEBUG(4,("update_printer_sec: updated denied by printer permissions\n"));
5811                 result = WERR_ACCESS_DENIED;
5812                 goto done;
5813         }
5814
5815         /* NT seems to like setting the security descriptor even though
5816            nothing may have actually changed. */
5817
5818         if ( !nt_printing_getsec(p->mem_ctx, Printer->sharename, &old_secdesc_ctr)) {
5819                 DEBUG(2,("update_printer_sec: nt_printing_getsec() failed\n"));
5820                 result = WERR_BADFID;
5821                 goto done;
5822         }
5823
5824         if (DEBUGLEVEL >= 10) {
5825                 SEC_ACL *the_acl;
5826                 int i;
5827
5828                 the_acl = old_secdesc_ctr->sd->dacl;
5829                 DEBUG(10, ("old_secdesc_ctr for %s has %d aces:\n",
5830                            PRINTERNAME(snum), the_acl->num_aces));
5831
5832                 for (i = 0; i < the_acl->num_aces; i++) {
5833                         DEBUG(10, ("%s 0x%08x\n", sid_string_dbg(
5834                                            &the_acl->aces[i].trustee),
5835                                   the_acl->aces[i].access_mask));
5836                 }
5837
5838                 the_acl = secdesc_ctr->sd->dacl;
5839
5840                 if (the_acl) {
5841                         DEBUG(10, ("secdesc_ctr for %s has %d aces:\n",
5842                                    PRINTERNAME(snum), the_acl->num_aces));
5843
5844                         for (i = 0; i < the_acl->num_aces; i++) {
5845                                 DEBUG(10, ("%s 0x%08x\n", sid_string_dbg(
5846                                                    &the_acl->aces[i].trustee),
5847                                            the_acl->aces[i].access_mask));
5848                         }
5849                 } else {
5850                         DEBUG(10, ("dacl for secdesc_ctr is NULL\n"));
5851                 }
5852         }
5853
5854         new_secdesc_ctr = sec_desc_merge(p->mem_ctx, secdesc_ctr, old_secdesc_ctr);
5855         if (!new_secdesc_ctr) {
5856                 result = WERR_NOMEM;
5857                 goto done;
5858         }
5859
5860         if (sec_desc_equal(new_secdesc_ctr->sd, old_secdesc_ctr->sd)) {
5861                 result = WERR_OK;
5862                 goto done;
5863         }
5864
5865         result = nt_printing_setsec(Printer->sharename, new_secdesc_ctr);
5866
5867  done:
5868
5869         return result;
5870 }
5871
5872 /********************************************************************
5873  Canonicalize printer info from a client
5874
5875  ATTN: It does not matter what we set the servername to hear
5876  since we do the necessary work in get_a_printer() to set it to
5877  the correct value based on what the client sent in the
5878  _spoolss_open_printer_ex().
5879  ********************************************************************/
5880
5881 static bool check_printer_ok(NT_PRINTER_INFO_LEVEL_2 *info, int snum)
5882 {
5883         fstring printername;
5884         const char *p;
5885
5886         DEBUG(5,("check_printer_ok: servername=%s printername=%s sharename=%s "
5887                 "portname=%s drivername=%s comment=%s location=%s\n",
5888                 info->servername, info->printername, info->sharename,
5889                 info->portname, info->drivername, info->comment, info->location));
5890
5891         /* we force some elements to "correct" values */
5892         slprintf(info->servername, sizeof(info->servername)-1, "\\\\%s", global_myname());
5893         fstrcpy(info->sharename, lp_servicename(snum));
5894
5895         /* check to see if we allow printername != sharename */
5896
5897         if ( lp_force_printername(snum) ) {
5898                 slprintf(info->printername, sizeof(info->printername)-1, "\\\\%s\\%s",
5899                         global_myname(), info->sharename );
5900         } else {
5901
5902                 /* make sure printername is in \\server\printername format */
5903
5904                 fstrcpy( printername, info->printername );
5905                 p = printername;
5906                 if ( printername[0] == '\\' && printername[1] == '\\' ) {
5907                         if ( (p = strchr_m( &printername[2], '\\' )) != NULL )
5908                                 p++;
5909                 }
5910
5911                 slprintf(info->printername, sizeof(info->printername)-1, "\\\\%s\\%s",
5912                          global_myname(), p );
5913         }
5914
5915         info->attributes |= PRINTER_ATTRIBUTE_SAMBA;
5916         info->attributes &= ~PRINTER_ATTRIBUTE_NOT_SAMBA;
5917
5918
5919
5920         return true;
5921 }
5922
5923 /****************************************************************************
5924 ****************************************************************************/
5925
5926 WERROR add_port_hook(TALLOC_CTX *ctx, NT_USER_TOKEN *token, const char *portname, const char *uri )
5927 {
5928         char *cmd = lp_addport_cmd();
5929         char *command = NULL;
5930         int ret;
5931         SE_PRIV se_printop = SE_PRINT_OPERATOR;
5932         bool is_print_op = false;
5933
5934         if ( !*cmd ) {
5935                 return WERR_ACCESS_DENIED;
5936         }
5937
5938         command = talloc_asprintf(ctx,
5939                         "%s \"%s\" \"%s\"", cmd, portname, uri );
5940         if (!command) {
5941                 return WERR_NOMEM;
5942         }
5943
5944         if ( token )
5945                 is_print_op = user_has_privileges( token, &se_printop );
5946
5947         DEBUG(10,("Running [%s]\n", command));
5948
5949         /********* BEGIN SePrintOperatorPrivilege **********/
5950
5951         if ( is_print_op )
5952                 become_root();
5953
5954         ret = smbrun(command, NULL);
5955
5956         if ( is_print_op )
5957                 unbecome_root();
5958
5959         /********* END SePrintOperatorPrivilege **********/
5960
5961         DEBUGADD(10,("returned [%d]\n", ret));
5962
5963         TALLOC_FREE(command);
5964
5965         if ( ret != 0 ) {
5966                 return WERR_ACCESS_DENIED;
5967         }
5968
5969         return WERR_OK;
5970 }
5971
5972 /****************************************************************************
5973 ****************************************************************************/
5974
5975 bool add_printer_hook(TALLOC_CTX *ctx, NT_USER_TOKEN *token, NT_PRINTER_INFO_LEVEL *printer)
5976 {
5977         char *cmd = lp_addprinter_cmd();
5978         char **qlines;
5979         char *command = NULL;
5980         int numlines;
5981         int ret;
5982         int fd;
5983         SE_PRIV se_printop = SE_PRINT_OPERATOR;
5984         bool is_print_op = false;
5985         char *remote_machine = talloc_strdup(ctx, "%m");
5986
5987         if (!remote_machine) {
5988                 return false;
5989         }
5990         remote_machine = talloc_sub_basic(ctx,
5991                                 current_user_info.smb_name,
5992                                 current_user_info.domain,
5993                                 remote_machine);
5994         if (!remote_machine) {
5995                 return false;
5996         }
5997
5998         command = talloc_asprintf(ctx,
5999                         "%s \"%s\" \"%s\" \"%s\" \"%s\" \"%s\" \"%s\" \"%s\"",
6000                         cmd, printer->info_2->printername, printer->info_2->sharename,
6001                         printer->info_2->portname, printer->info_2->drivername,
6002                         printer->info_2->location, printer->info_2->comment, remote_machine);
6003         if (!command) {
6004                 return false;
6005         }
6006
6007         if ( token )
6008                 is_print_op = user_has_privileges( token, &se_printop );
6009
6010         DEBUG(10,("Running [%s]\n", command));
6011
6012         /********* BEGIN SePrintOperatorPrivilege **********/
6013
6014         if ( is_print_op )
6015                 become_root();
6016
6017         if ( (ret = smbrun(command, &fd)) == 0 ) {
6018                 /* Tell everyone we updated smb.conf. */
6019                 message_send_all(smbd_messaging_context(),
6020                                  MSG_SMB_CONF_UPDATED, NULL, 0, NULL);
6021         }
6022
6023         if ( is_print_op )
6024                 unbecome_root();
6025
6026         /********* END SePrintOperatorPrivilege **********/
6027
6028         DEBUGADD(10,("returned [%d]\n", ret));
6029
6030         TALLOC_FREE(command);
6031         TALLOC_FREE(remote_machine);
6032
6033         if ( ret != 0 ) {
6034                 if (fd != -1)
6035                         close(fd);
6036                 return false;
6037         }
6038
6039         /* reload our services immediately */
6040         become_root();
6041         reload_services(false);
6042         unbecome_root();
6043
6044         numlines = 0;
6045         /* Get lines and convert them back to dos-codepage */
6046         qlines = fd_lines_load(fd, &numlines, 0, NULL);
6047         DEBUGADD(10,("Lines returned = [%d]\n", numlines));
6048         close(fd);
6049
6050         /* Set the portname to what the script says the portname should be. */
6051         /* but don't require anything to be return from the script exit a good error code */
6052
6053         if (numlines) {
6054                 /* Set the portname to what the script says the portname should be. */
6055                 strncpy(printer->info_2->portname, qlines[0], sizeof(printer->info_2->portname));
6056                 DEBUGADD(6,("Line[0] = [%s]\n", qlines[0]));
6057         }
6058
6059         TALLOC_FREE(qlines);
6060         return true;
6061 }
6062
6063
6064 /********************************************************************
6065  * Called by spoolss_api_setprinter
6066  * when updating a printer description.
6067  ********************************************************************/
6068
6069 static WERROR update_printer(pipes_struct *p, struct policy_handle *handle,
6070                              struct spoolss_SetPrinterInfoCtr *info_ctr,
6071                              struct spoolss_DeviceMode *devmode)
6072 {
6073         int snum;
6074         NT_PRINTER_INFO_LEVEL *printer = NULL, *old_printer = NULL;
6075         Printer_entry *Printer = find_printer_index_by_hnd(p, handle);
6076         WERROR result;
6077         UNISTR2 buffer;
6078         fstring asc_buffer;
6079
6080         DEBUG(8,("update_printer\n"));
6081
6082         result = WERR_OK;
6083
6084         if (!Printer) {
6085                 result = WERR_BADFID;
6086                 goto done;
6087         }
6088
6089         if (!get_printer_snum(p, handle, &snum, NULL)) {
6090                 result = WERR_BADFID;
6091                 goto done;
6092         }
6093
6094         if (!W_ERROR_IS_OK(get_a_printer(Printer, &printer, 2, lp_const_servicename(snum))) ||
6095             (!W_ERROR_IS_OK(get_a_printer(Printer, &old_printer, 2, lp_const_servicename(snum))))) {
6096                 result = WERR_BADFID;
6097                 goto done;
6098         }
6099
6100         DEBUGADD(8,("Converting info_2 struct\n"));
6101
6102         /*
6103          * convert_printer_info converts the incoming
6104          * info from the client and overwrites the info
6105          * just read from the tdb in the pointer 'printer'.
6106          */
6107
6108         if (!convert_printer_info(info_ctr, printer)) {
6109                 result =  WERR_NOMEM;
6110                 goto done;
6111         }
6112
6113         if (devmode) {
6114                 /* we have a valid devmode
6115                    convert it and link it*/
6116
6117                 DEBUGADD(8,("update_printer: Converting the devicemode struct\n"));
6118                 if (!convert_devicemode(printer->info_2->printername, devmode,
6119                                         &printer->info_2->devmode)) {
6120                         result =  WERR_NOMEM;
6121                         goto done;
6122                 }
6123         }
6124
6125         /* Do sanity check on the requested changes for Samba */
6126
6127         if (!check_printer_ok(printer->info_2, snum)) {
6128                 result = WERR_INVALID_PARAM;
6129                 goto done;
6130         }
6131
6132         /* FIXME!!! If the driver has changed we really should verify that
6133            it is installed before doing much else   --jerry */
6134
6135         /* Check calling user has permission to update printer description */
6136
6137         if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
6138                 DEBUG(3, ("update_printer: printer property change denied by handle\n"));
6139                 result = WERR_ACCESS_DENIED;
6140                 goto done;
6141         }
6142
6143         /* Call addprinter hook */
6144         /* Check changes to see if this is really needed */
6145
6146         if ( *lp_addprinter_cmd()
6147                 && (!strequal(printer->info_2->drivername, old_printer->info_2->drivername)
6148                         || !strequal(printer->info_2->comment, old_printer->info_2->comment)
6149                         || !strequal(printer->info_2->portname, old_printer->info_2->portname)
6150                         || !strequal(printer->info_2->location, old_printer->info_2->location)) )
6151         {
6152                 /* add_printer_hook() will call reload_services() */
6153
6154                 if ( !add_printer_hook(p->mem_ctx, p->server_info->ptok,
6155                                        printer) ) {
6156                         result = WERR_ACCESS_DENIED;
6157                         goto done;
6158                 }
6159         }
6160
6161         /*
6162          * When a *new* driver is bound to a printer, the drivername is used to
6163          * lookup previously saved driver initialization info, which is then
6164          * bound to the printer, simulating what happens in the Windows arch.
6165          */
6166         if (!strequal(printer->info_2->drivername, old_printer->info_2->drivername))
6167         {
6168                 if (!set_driver_init(printer, 2))
6169                 {
6170                         DEBUG(5,("update_printer: Error restoring driver initialization data for driver [%s]!\n",
6171                                 printer->info_2->drivername));
6172                 }
6173
6174                 DEBUG(10,("update_printer: changing driver [%s]!  Sending event!\n",
6175                         printer->info_2->drivername));
6176
6177                 notify_printer_driver(snum, printer->info_2->drivername);
6178         }
6179
6180         /*
6181          * flag which changes actually occured.  This is a small subset of
6182          * all the possible changes.  We also have to update things in the
6183          * DsSpooler key.
6184          */
6185
6186         if (!strequal(printer->info_2->comment, old_printer->info_2->comment)) {
6187                 init_unistr2( &buffer, printer->info_2->comment, UNI_STR_TERMINATE);
6188                 set_printer_dataex( printer, SPOOL_DSSPOOLER_KEY, "description",
6189                         REG_SZ, (uint8_t *)buffer.buffer, buffer.uni_str_len*2 );
6190
6191                 notify_printer_comment(snum, printer->info_2->comment);
6192         }
6193
6194         if (!strequal(printer->info_2->sharename, old_printer->info_2->sharename)) {
6195                 init_unistr2( &buffer, printer->info_2->sharename, UNI_STR_TERMINATE);
6196                 set_printer_dataex( printer, SPOOL_DSSPOOLER_KEY, "shareName",
6197                         REG_SZ, (uint8_t *)buffer.buffer, buffer.uni_str_len*2 );
6198
6199                 notify_printer_sharename(snum, printer->info_2->sharename);
6200         }
6201
6202         if (!strequal(printer->info_2->printername, old_printer->info_2->printername)) {
6203                 char *pname;
6204
6205                 if ( (pname = strchr_m( printer->info_2->printername+2, '\\' )) != NULL )
6206                         pname++;
6207                 else
6208                         pname = printer->info_2->printername;
6209
6210
6211                 init_unistr2( &buffer, pname, UNI_STR_TERMINATE);
6212                 set_printer_dataex( printer, SPOOL_DSSPOOLER_KEY, "printerName",
6213                         REG_SZ, (uint8_t *)buffer.buffer, buffer.uni_str_len*2 );
6214
6215                 notify_printer_printername( snum, pname );
6216         }
6217
6218         if (!strequal(printer->info_2->portname, old_printer->info_2->portname)) {
6219                 init_unistr2( &buffer, printer->info_2->portname, UNI_STR_TERMINATE);
6220                 set_printer_dataex( printer, SPOOL_DSSPOOLER_KEY, "portName",
6221                         REG_SZ, (uint8_t *)buffer.buffer, buffer.uni_str_len*2 );
6222
6223                 notify_printer_port(snum, printer->info_2->portname);
6224         }
6225
6226         if (!strequal(printer->info_2->location, old_printer->info_2->location)) {
6227                 init_unistr2( &buffer, printer->info_2->location, UNI_STR_TERMINATE);
6228                 set_printer_dataex( printer, SPOOL_DSSPOOLER_KEY, "location",
6229                         REG_SZ, (uint8_t *)buffer.buffer, buffer.uni_str_len*2 );
6230
6231                 notify_printer_location(snum, printer->info_2->location);
6232         }
6233
6234         /* here we need to update some more DsSpooler keys */
6235         /* uNCName, serverName, shortServerName */
6236
6237         init_unistr2( &buffer, global_myname(), UNI_STR_TERMINATE);
6238         set_printer_dataex( printer, SPOOL_DSSPOOLER_KEY, "serverName",
6239                 REG_SZ, (uint8_t *)buffer.buffer, buffer.uni_str_len*2 );
6240         set_printer_dataex( printer, SPOOL_DSSPOOLER_KEY, "shortServerName",
6241                 REG_SZ, (uint8_t *)buffer.buffer, buffer.uni_str_len*2 );
6242
6243         slprintf( asc_buffer, sizeof(asc_buffer)-1, "\\\\%s\\%s",
6244                  global_myname(), printer->info_2->sharename );
6245         init_unistr2( &buffer, asc_buffer, UNI_STR_TERMINATE);
6246         set_printer_dataex( printer, SPOOL_DSSPOOLER_KEY, "uNCName",
6247                 REG_SZ, (uint8_t *)buffer.buffer, buffer.uni_str_len*2 );
6248
6249         /* Update printer info */
6250         result = mod_a_printer(printer, 2);
6251
6252 done:
6253         free_a_printer(&printer, 2);
6254         free_a_printer(&old_printer, 2);
6255
6256
6257         return result;
6258 }
6259
6260 /****************************************************************************
6261 ****************************************************************************/
6262 static WERROR publish_or_unpublish_printer(pipes_struct *p,
6263                                            struct policy_handle *handle,
6264                                            struct spoolss_SetPrinterInfo7 *info7)
6265 {
6266 #ifdef HAVE_ADS
6267         int snum;
6268         Printer_entry *Printer;
6269
6270         if ( lp_security() != SEC_ADS ) {
6271                 return WERR_UNKNOWN_LEVEL;
6272         }
6273
6274         Printer = find_printer_index_by_hnd(p, handle);
6275
6276         DEBUG(5,("publish_or_unpublish_printer, action = %d\n",info7->action));
6277
6278         if (!Printer)
6279                 return WERR_BADFID;
6280
6281         if (!get_printer_snum(p, handle, &snum, NULL))
6282                 return WERR_BADFID;
6283
6284         nt_printer_publish(Printer, snum, info7->action);
6285
6286         return WERR_OK;
6287 #else
6288         return WERR_UNKNOWN_LEVEL;
6289 #endif
6290 }
6291
6292 /****************************************************************
6293  _spoolss_SetPrinter
6294 ****************************************************************/
6295
6296 WERROR _spoolss_SetPrinter(pipes_struct *p,
6297                            struct spoolss_SetPrinter *r)
6298 {
6299         WERROR result;
6300
6301         Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
6302
6303         if (!Printer) {
6304                 DEBUG(2,("_spoolss_SetPrinter: Invalid handle (%s:%u:%u)\n",
6305                         OUR_HANDLE(r->in.handle)));
6306                 return WERR_BADFID;
6307         }
6308
6309         /* check the level */
6310         switch (r->in.info_ctr->level) {
6311                 case 0:
6312                         return control_printer(r->in.handle, r->in.command, p);
6313                 case 2:
6314                         result = update_printer(p, r->in.handle,
6315                                                 r->in.info_ctr,
6316                                                 r->in.devmode_ctr->devmode);
6317                         if (!W_ERROR_IS_OK(result))
6318                                 return result;
6319                         if (r->in.secdesc_ctr->sd)
6320                                 result = update_printer_sec(r->in.handle, p,
6321                                                             r->in.secdesc_ctr);
6322                         return result;
6323                 case 3:
6324                         return update_printer_sec(r->in.handle, p,
6325                                                   r->in.secdesc_ctr);
6326                 case 7:
6327                         return publish_or_unpublish_printer(p, r->in.handle,
6328                                                             r->in.info_ctr->info.info7);
6329                 default:
6330                         return WERR_UNKNOWN_LEVEL;
6331         }
6332 }
6333
6334 /****************************************************************
6335  _spoolss_FindClosePrinterNotify
6336 ****************************************************************/
6337
6338 WERROR _spoolss_FindClosePrinterNotify(pipes_struct *p,
6339                                        struct spoolss_FindClosePrinterNotify *r)
6340 {
6341         Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
6342
6343         if (!Printer) {
6344                 DEBUG(2,("_spoolss_FindClosePrinterNotify: "
6345                         "Invalid handle (%s:%u:%u)\n", OUR_HANDLE(r->in.handle)));
6346                 return WERR_BADFID;
6347         }
6348
6349         if (Printer->notify.client_connected == true) {
6350                 int snum = -1;
6351
6352                 if ( Printer->printer_type == SPLHND_SERVER)
6353                         snum = -1;
6354                 else if ( (Printer->printer_type == SPLHND_PRINTER) &&
6355                                 !get_printer_snum(p, r->in.handle, &snum, NULL) )
6356                         return WERR_BADFID;
6357
6358                 srv_spoolss_replycloseprinter(snum, &Printer->notify.client_hnd);
6359         }
6360
6361         Printer->notify.flags=0;
6362         Printer->notify.options=0;
6363         Printer->notify.localmachine[0]='\0';
6364         Printer->notify.printerlocal=0;
6365         TALLOC_FREE(Printer->notify.option);
6366         Printer->notify.client_connected = false;
6367
6368         return WERR_OK;
6369 }
6370
6371 /****************************************************************
6372  _spoolss_AddJob
6373 ****************************************************************/
6374
6375 WERROR _spoolss_AddJob(pipes_struct *p,
6376                        struct spoolss_AddJob *r)
6377 {
6378         if (!r->in.buffer && (r->in.offered != 0)) {
6379                 return WERR_INVALID_PARAM;
6380         }
6381
6382         /* this is what a NT server returns for AddJob. AddJob must fail on
6383          * non-local printers */
6384
6385         if (r->in.level != 1) {
6386                 return WERR_UNKNOWN_LEVEL;
6387         }
6388
6389         return WERR_INVALID_PARAM;
6390 }
6391
6392 /****************************************************************************
6393 fill_job_info1
6394 ****************************************************************************/
6395
6396 static WERROR fill_job_info1(TALLOC_CTX *mem_ctx,
6397                              struct spoolss_JobInfo1 *r,
6398                              const print_queue_struct *queue,
6399                              int position, int snum,
6400                              const NT_PRINTER_INFO_LEVEL *ntprinter)
6401 {
6402         struct tm *t;
6403
6404         t = gmtime(&queue->time);
6405
6406         r->job_id               = queue->job;
6407
6408         r->printer_name         = talloc_strdup(mem_ctx, lp_servicename(snum));
6409         W_ERROR_HAVE_NO_MEMORY(r->printer_name);
6410         r->server_name          = talloc_strdup(mem_ctx, ntprinter->info_2->servername);
6411         W_ERROR_HAVE_NO_MEMORY(r->server_name);
6412         r->user_name            = talloc_strdup(mem_ctx, queue->fs_user);
6413         W_ERROR_HAVE_NO_MEMORY(r->user_name);
6414         r->document_name        = talloc_strdup(mem_ctx, queue->fs_file);
6415         W_ERROR_HAVE_NO_MEMORY(r->document_name);
6416         r->data_type            = talloc_strdup(mem_ctx, "RAW");
6417         W_ERROR_HAVE_NO_MEMORY(r->data_type);
6418         r->text_status          = talloc_strdup(mem_ctx, "");
6419         W_ERROR_HAVE_NO_MEMORY(r->text_status);
6420
6421         r->status               = nt_printj_status(queue->status);
6422         r->priority             = queue->priority;
6423         r->position             = position;
6424         r->total_pages          = queue->page_count;
6425         r->pages_printed        = 0; /* ??? */
6426
6427         init_systemtime(&r->submitted, t);
6428
6429         return WERR_OK;
6430 }
6431
6432 /****************************************************************************
6433 fill_job_info2
6434 ****************************************************************************/
6435
6436 static WERROR fill_job_info2(TALLOC_CTX *mem_ctx,
6437                              struct spoolss_JobInfo2 *r,
6438                              const print_queue_struct *queue,
6439                              int position, int snum,
6440                              const NT_PRINTER_INFO_LEVEL *ntprinter,
6441                              struct spoolss_DeviceMode *devmode)
6442 {
6443         struct tm *t;
6444
6445         t = gmtime(&queue->time);
6446
6447         r->job_id               = queue->job;
6448
6449         r->printer_name         = talloc_strdup(mem_ctx, lp_servicename(snum));
6450         W_ERROR_HAVE_NO_MEMORY(r->printer_name);
6451         r->server_name          = talloc_strdup(mem_ctx, ntprinter->info_2->servername);
6452         W_ERROR_HAVE_NO_MEMORY(r->server_name);
6453         r->user_name            = talloc_strdup(mem_ctx, queue->fs_user);
6454         W_ERROR_HAVE_NO_MEMORY(r->user_name);
6455         r->document_name        = talloc_strdup(mem_ctx, queue->fs_file);
6456         W_ERROR_HAVE_NO_MEMORY(r->document_name);
6457         r->notify_name          = talloc_strdup(mem_ctx, queue->fs_user);
6458         W_ERROR_HAVE_NO_MEMORY(r->notify_name);
6459         r->data_type            = talloc_strdup(mem_ctx, "RAW");
6460         W_ERROR_HAVE_NO_MEMORY(r->data_type);
6461         r->print_processor      = talloc_strdup(mem_ctx, "winprint");
6462         W_ERROR_HAVE_NO_MEMORY(r->print_processor);
6463         r->parameters           = talloc_strdup(mem_ctx, "");
6464         W_ERROR_HAVE_NO_MEMORY(r->parameters);
6465         r->driver_name          = talloc_strdup(mem_ctx, ntprinter->info_2->drivername);
6466         W_ERROR_HAVE_NO_MEMORY(r->driver_name);
6467
6468         r->devmode              = devmode;
6469
6470         r->text_status          = talloc_strdup(mem_ctx, "");
6471         W_ERROR_HAVE_NO_MEMORY(r->text_status);
6472
6473         r->secdesc              = NULL;
6474
6475         r->status               = nt_printj_status(queue->status);
6476         r->priority             = queue->priority;
6477         r->position             = position;
6478         r->start_time           = 0;
6479         r->until_time           = 0;
6480         r->total_pages          = queue->page_count;
6481         r->size                 = queue->size;
6482         init_systemtime(&r->submitted, t);
6483         r->time                 = 0;
6484         r->pages_printed        = 0; /* ??? */
6485
6486         return WERR_OK;
6487 }
6488
6489 /****************************************************************************
6490  Enumjobs at level 1.
6491 ****************************************************************************/
6492
6493 static WERROR enumjobs_level1(TALLOC_CTX *mem_ctx,
6494                               const print_queue_struct *queue,
6495                               uint32_t num_queues, int snum,
6496                               const NT_PRINTER_INFO_LEVEL *ntprinter,
6497                               union spoolss_JobInfo **info_p,
6498                               uint32_t *count)
6499 {
6500         union spoolss_JobInfo *info;
6501         int i;
6502         WERROR result = WERR_OK;
6503
6504         info = TALLOC_ARRAY(mem_ctx, union spoolss_JobInfo, num_queues);
6505         W_ERROR_HAVE_NO_MEMORY(info);
6506
6507         *count = num_queues;
6508
6509         for (i=0; i<*count; i++) {
6510                 result = fill_job_info1(info,
6511                                         &info[i].info1,
6512                                         &queue[i],
6513                                         i,
6514                                         snum,
6515                                         ntprinter);
6516                 if (!W_ERROR_IS_OK(result)) {
6517                         goto out;
6518                 }
6519         }
6520
6521  out:
6522         if (!W_ERROR_IS_OK(result)) {
6523                 TALLOC_FREE(info);
6524                 *count = 0;
6525                 return result;
6526         }
6527
6528         *info_p = info;
6529
6530         return WERR_OK;
6531 }
6532
6533 /****************************************************************************
6534  Enumjobs at level 2.
6535 ****************************************************************************/
6536
6537 static WERROR enumjobs_level2(TALLOC_CTX *mem_ctx,
6538                               const print_queue_struct *queue,
6539                               uint32_t num_queues, int snum,
6540                               const NT_PRINTER_INFO_LEVEL *ntprinter,
6541                               union spoolss_JobInfo **info_p,
6542                               uint32_t *count)
6543 {
6544         union spoolss_JobInfo *info;
6545         int i;
6546         WERROR result = WERR_OK;
6547
6548         info = TALLOC_ARRAY(mem_ctx, union spoolss_JobInfo, num_queues);
6549         W_ERROR_HAVE_NO_MEMORY(info);
6550
6551         *count = num_queues;
6552
6553         for (i=0; i<*count; i++) {
6554
6555                 struct spoolss_DeviceMode *devmode;
6556
6557                 devmode = construct_dev_mode(info, lp_const_servicename(snum));
6558                 if (!devmode) {
6559                         result = WERR_NOMEM;
6560                         goto out;
6561                 }
6562
6563                 result = fill_job_info2(info,
6564                                         &info[i].info2,
6565                                         &queue[i],
6566                                         i,
6567                                         snum,
6568                                         ntprinter,
6569                                         devmode);
6570                 if (!W_ERROR_IS_OK(result)) {
6571                         goto out;
6572                 }
6573         }
6574
6575  out:
6576         if (!W_ERROR_IS_OK(result)) {
6577                 TALLOC_FREE(info);
6578                 *count = 0;
6579                 return result;
6580         }
6581
6582         *info_p = info;
6583
6584         return WERR_OK;
6585 }
6586
6587 /****************************************************************
6588  _spoolss_EnumJobs
6589 ****************************************************************/
6590
6591 WERROR _spoolss_EnumJobs(pipes_struct *p,
6592                          struct spoolss_EnumJobs *r)
6593 {
6594         WERROR result;
6595         NT_PRINTER_INFO_LEVEL *ntprinter = NULL;
6596         int snum;
6597         print_status_struct prt_status;
6598         print_queue_struct *queue = NULL;
6599         uint32_t count;
6600
6601         /* that's an [in out] buffer */
6602
6603         if (!r->in.buffer && (r->in.offered != 0)) {
6604                 return WERR_INVALID_PARAM;
6605         }
6606
6607         DEBUG(4,("_spoolss_EnumJobs\n"));
6608
6609         *r->out.needed = 0;
6610         *r->out.count = 0;
6611         *r->out.info = NULL;
6612
6613         /* lookup the printer snum and tdb entry */
6614
6615         if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
6616                 return WERR_BADFID;
6617         }
6618
6619         result = get_a_printer(NULL, &ntprinter, 2, lp_servicename(snum));
6620         if (!W_ERROR_IS_OK(result)) {
6621                 return result;
6622         }
6623
6624         count = print_queue_status(snum, &queue, &prt_status);
6625         DEBUGADD(4,("count:[%d], status:[%d], [%s]\n",
6626                 count, prt_status.status, prt_status.message));
6627
6628         if (count == 0) {
6629                 SAFE_FREE(queue);
6630                 free_a_printer(&ntprinter, 2);
6631                 return WERR_OK;
6632         }
6633
6634         switch (r->in.level) {
6635         case 1:
6636                 result = enumjobs_level1(p->mem_ctx, queue, count, snum,
6637                                          ntprinter, r->out.info, r->out.count);
6638                 break;
6639         case 2:
6640                 result = enumjobs_level2(p->mem_ctx, queue, count, snum,
6641                                          ntprinter, r->out.info, r->out.count);
6642                 break;
6643         default:
6644                 result = WERR_UNKNOWN_LEVEL;
6645                 break;
6646         }
6647
6648         SAFE_FREE(queue);
6649         free_a_printer(&ntprinter, 2);
6650
6651         if (!W_ERROR_IS_OK(result)) {
6652                 return result;
6653         }
6654
6655         *r->out.needed  = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
6656                                                      spoolss_EnumJobs, NULL,
6657                                                      *r->out.info, r->in.level,
6658                                                      *r->out.count);
6659         *r->out.info    = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
6660         *r->out.count   = SPOOLSS_BUFFER_OK(*r->out.count, 0);
6661
6662         return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
6663 }
6664
6665 /****************************************************************
6666  _spoolss_ScheduleJob
6667 ****************************************************************/
6668
6669 WERROR _spoolss_ScheduleJob(pipes_struct *p,
6670                             struct spoolss_ScheduleJob *r)
6671 {
6672         return WERR_OK;
6673 }
6674
6675 /****************************************************************
6676  _spoolss_SetJob
6677 ****************************************************************/
6678
6679 WERROR _spoolss_SetJob(pipes_struct *p,
6680                        struct spoolss_SetJob *r)
6681 {
6682         int snum;
6683         WERROR errcode = WERR_BADFUNC;
6684
6685         if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
6686                 return WERR_BADFID;
6687         }
6688
6689         if (!print_job_exists(lp_const_servicename(snum), r->in.job_id)) {
6690                 return WERR_INVALID_PRINTER_NAME;
6691         }
6692
6693         switch (r->in.command) {
6694         case SPOOLSS_JOB_CONTROL_CANCEL:
6695         case SPOOLSS_JOB_CONTROL_DELETE:
6696                 if (print_job_delete(p->server_info, snum, r->in.job_id, &errcode)) {
6697                         errcode = WERR_OK;
6698                 }
6699                 break;
6700         case SPOOLSS_JOB_CONTROL_PAUSE:
6701                 if (print_job_pause(p->server_info, snum, r->in.job_id, &errcode)) {
6702                         errcode = WERR_OK;
6703                 }
6704                 break;
6705         case SPOOLSS_JOB_CONTROL_RESTART:
6706         case SPOOLSS_JOB_CONTROL_RESUME:
6707                 if (print_job_resume(p->server_info, snum, r->in.job_id, &errcode)) {
6708                         errcode = WERR_OK;
6709                 }
6710                 break;
6711         default:
6712                 return WERR_UNKNOWN_LEVEL;
6713         }
6714
6715         return errcode;
6716 }
6717
6718 /****************************************************************************
6719  Enumerates all printer drivers by level.
6720 ****************************************************************************/
6721
6722 static WERROR enumprinterdrivers_level(TALLOC_CTX *mem_ctx,
6723                                        const char *servername,
6724                                        const char *architecture,
6725                                        uint32_t level,
6726                                        union spoolss_DriverInfo **info_p,
6727                                        uint32_t *count_p)
6728 {
6729         int i;
6730         int ndrivers;
6731         uint32_t version;
6732         fstring *list = NULL;
6733         NT_PRINTER_DRIVER_INFO_LEVEL driver;
6734         union spoolss_DriverInfo *info = NULL;
6735         uint32_t count = 0;
6736         WERROR result = WERR_OK;
6737
6738         *count_p = 0;
6739         *info_p = NULL;
6740
6741         for (version=0; version<DRIVER_MAX_VERSION; version++) {
6742                 list = NULL;
6743                 ndrivers = get_ntdrivers(&list, architecture, version);
6744                 DEBUGADD(4,("we have:[%d] drivers in environment [%s] and version [%d]\n",
6745                         ndrivers, architecture, version));
6746
6747                 if (ndrivers == -1) {
6748                         result = WERR_NOMEM;
6749                         goto out;
6750                 }
6751
6752                 if (ndrivers != 0) {
6753                         info = TALLOC_REALLOC_ARRAY(mem_ctx, info,
6754                                                     union spoolss_DriverInfo,
6755                                                     count + ndrivers);
6756                         if (!info) {
6757                                 DEBUG(0,("enumprinterdrivers_level1: "
6758                                         "failed to enlarge driver info buffer!\n"));
6759                                 result = WERR_NOMEM;
6760                                 goto out;
6761                         }
6762                 }
6763
6764                 for (i=0; i<ndrivers; i++) {
6765                         DEBUGADD(5,("\tdriver: [%s]\n", list[i]));
6766                         ZERO_STRUCT(driver);
6767                         result = get_a_printer_driver(&driver, 3, list[i],
6768                                                       architecture, version);
6769                         if (!W_ERROR_IS_OK(result)) {
6770                                 goto out;
6771                         }
6772
6773                         switch (level) {
6774                         case 1:
6775                                 result = fill_printer_driver_info1(info, &info[count+i].info1,
6776                                                                    &driver, servername,
6777                                                                    architecture);
6778                                 break;
6779                         case 2:
6780                                 result = fill_printer_driver_info2(info, &info[count+i].info2,
6781                                                                    &driver, servername);
6782                                 break;
6783                         case 3:
6784                                 result = fill_printer_driver_info3(info, &info[count+i].info3,
6785                                                                    &driver, servername);
6786                                 break;
6787                         case 4:
6788                                 result = fill_printer_driver_info4(info, &info[count+i].info4,
6789                                                                    &driver, servername);
6790                                 break;
6791                         case 5:
6792                                 result = fill_printer_driver_info5(info, &info[count+i].info5,
6793                                                                    &driver, servername);
6794                                 break;
6795                         case 6:
6796                                 result = fill_printer_driver_info6(info, &info[count+i].info6,
6797                                                                    &driver, servername);
6798                                 break;
6799                         default:
6800                                 result = WERR_UNKNOWN_LEVEL;
6801                                 break;
6802                         }
6803
6804                         if (!W_ERROR_IS_OK(result)) {
6805                                 free_a_printer_driver(driver, 3);
6806                                 goto out;
6807                         }
6808                         free_a_printer_driver(driver, 3);
6809                 }
6810
6811                 count += ndrivers;
6812                 SAFE_FREE(list);
6813         }
6814
6815  out:
6816         SAFE_FREE(list);
6817
6818         if (!W_ERROR_IS_OK(result)) {
6819                 TALLOC_FREE(info);
6820                 return result;
6821         }
6822
6823         *info_p = info;
6824         *count_p = count;
6825
6826         return WERR_OK;
6827 }
6828
6829 /****************************************************************************
6830  Enumerates all printer drivers at level 1.
6831 ****************************************************************************/
6832
6833 static WERROR enumprinterdrivers_level1(TALLOC_CTX *mem_ctx,
6834                                         const char *servername,
6835                                         const char *architecture,
6836                                         union spoolss_DriverInfo **info_p,
6837                                         uint32_t *count)
6838 {
6839         return enumprinterdrivers_level(mem_ctx, servername, architecture, 1,
6840                                         info_p, count);
6841 }
6842
6843 /****************************************************************************
6844  Enumerates all printer drivers at level 2.
6845 ****************************************************************************/
6846
6847 static WERROR enumprinterdrivers_level2(TALLOC_CTX *mem_ctx,
6848                                         const char *servername,
6849                                         const char *architecture,
6850                                         union spoolss_DriverInfo **info_p,
6851                                         uint32_t *count)
6852 {
6853         return enumprinterdrivers_level(mem_ctx, servername, architecture, 2,
6854                                         info_p, count);
6855 }
6856
6857 /****************************************************************************
6858  Enumerates all printer drivers at level 3.
6859 ****************************************************************************/
6860
6861 static WERROR enumprinterdrivers_level3(TALLOC_CTX *mem_ctx,
6862                                         const char *servername,
6863                                         const char *architecture,
6864                                         union spoolss_DriverInfo **info_p,
6865                                         uint32_t *count)
6866 {
6867         return enumprinterdrivers_level(mem_ctx, servername, architecture, 3,
6868                                         info_p, count);
6869 }
6870
6871 /****************************************************************************
6872  Enumerates all printer drivers at level 4.
6873 ****************************************************************************/
6874
6875 static WERROR enumprinterdrivers_level4(TALLOC_CTX *mem_ctx,
6876                                         const char *servername,
6877                                         const char *architecture,
6878                                         union spoolss_DriverInfo **info_p,
6879                                         uint32_t *count)
6880 {
6881         return enumprinterdrivers_level(mem_ctx, servername, architecture, 4,
6882                                         info_p, count);
6883 }
6884
6885 /****************************************************************************
6886  Enumerates all printer drivers at level 5.
6887 ****************************************************************************/
6888
6889 static WERROR enumprinterdrivers_level5(TALLOC_CTX *mem_ctx,
6890                                         const char *servername,
6891                                         const char *architecture,
6892                                         union spoolss_DriverInfo **info_p,
6893                                         uint32_t *count)
6894 {
6895         return enumprinterdrivers_level(mem_ctx, servername, architecture, 5,
6896                                         info_p, count);
6897 }
6898
6899 /****************************************************************************
6900  Enumerates all printer drivers at level 6.
6901 ****************************************************************************/
6902
6903 static WERROR enumprinterdrivers_level6(TALLOC_CTX *mem_ctx,
6904                                         const char *servername,
6905                                         const char *architecture,
6906                                         union spoolss_DriverInfo **info_p,
6907                                         uint32_t *count)
6908 {
6909         return enumprinterdrivers_level(mem_ctx, servername, architecture, 6,
6910                                         info_p, count);
6911 }
6912
6913
6914 /****************************************************************
6915  _spoolss_EnumPrinterDrivers
6916 ****************************************************************/
6917
6918 WERROR _spoolss_EnumPrinterDrivers(pipes_struct *p,
6919                                    struct spoolss_EnumPrinterDrivers *r)
6920 {
6921         const char *cservername;
6922         WERROR result;
6923
6924         /* that's an [in out] buffer */
6925
6926         if (!r->in.buffer && (r->in.offered != 0)) {
6927                 return WERR_INVALID_PARAM;
6928         }
6929
6930         DEBUG(4,("_spoolss_EnumPrinterDrivers\n"));
6931
6932         *r->out.needed = 0;
6933         *r->out.count = 0;
6934         *r->out.info = NULL;
6935
6936         cservername = canon_servername(r->in.server);
6937
6938         if (!is_myname_or_ipaddr(cservername)) {
6939                 return WERR_UNKNOWN_PRINTER_DRIVER;
6940         }
6941
6942         switch (r->in.level) {
6943         case 1:
6944                 result = enumprinterdrivers_level1(p->mem_ctx, cservername,
6945                                                    r->in.environment,
6946                                                    r->out.info, r->out.count);
6947                 break;
6948         case 2:
6949                 result = enumprinterdrivers_level2(p->mem_ctx, cservername,
6950                                                    r->in.environment,
6951                                                    r->out.info, r->out.count);
6952                 break;
6953         case 3:
6954                 result = enumprinterdrivers_level3(p->mem_ctx, cservername,
6955                                                    r->in.environment,
6956                                                    r->out.info, r->out.count);
6957                 break;
6958         case 4:
6959                 result = enumprinterdrivers_level4(p->mem_ctx, cservername,
6960                                                    r->in.environment,
6961                                                    r->out.info, r->out.count);
6962                 break;
6963         case 5:
6964                 result = enumprinterdrivers_level5(p->mem_ctx, cservername,
6965                                                    r->in.environment,
6966                                                    r->out.info, r->out.count);
6967                 break;
6968         case 6:
6969                 result = enumprinterdrivers_level6(p->mem_ctx, cservername,
6970                                                    r->in.environment,
6971                                                    r->out.info, r->out.count);
6972                 break;
6973         default:
6974                 return WERR_UNKNOWN_LEVEL;
6975         }
6976
6977         if (!W_ERROR_IS_OK(result)) {
6978                 return result;
6979         }
6980
6981         *r->out.needed  = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
6982                                                      spoolss_EnumPrinterDrivers, NULL,
6983                                                      *r->out.info, r->in.level,
6984                                                      *r->out.count);
6985         *r->out.info    = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
6986         *r->out.count   = SPOOLSS_BUFFER_OK(*r->out.count, 0);
6987
6988         return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
6989 }
6990
6991 /****************************************************************************
6992 ****************************************************************************/
6993
6994 static WERROR fill_form_info_1(TALLOC_CTX *mem_ctx,
6995                                struct spoolss_FormInfo1 *r,
6996                                const nt_forms_struct *form)
6997 {
6998         r->form_name    = talloc_strdup(mem_ctx, form->name);
6999         W_ERROR_HAVE_NO_MEMORY(r->form_name);
7000
7001         r->flags        = form->flag;
7002         r->size.width   = form->width;
7003         r->size.height  = form->length;
7004         r->area.left    = form->left;
7005         r->area.top     = form->top;
7006         r->area.right   = form->right;
7007         r->area.bottom  = form->bottom;
7008
7009         return WERR_OK;
7010 }
7011
7012 /****************************************************************
7013  spoolss_enumforms_level1
7014 ****************************************************************/
7015
7016 static WERROR spoolss_enumforms_level1(TALLOC_CTX *mem_ctx,
7017                                        const nt_forms_struct *builtin_forms,
7018                                        uint32_t num_builtin_forms,
7019                                        const nt_forms_struct *user_forms,
7020                                        uint32_t num_user_forms,
7021                                        union spoolss_FormInfo **info_p,
7022                                        uint32_t *count)
7023 {
7024         union spoolss_FormInfo *info;
7025         WERROR result = WERR_OK;
7026         int i;
7027
7028         *count = num_builtin_forms + num_user_forms;
7029
7030         info = TALLOC_ARRAY(mem_ctx, union spoolss_FormInfo, *count);
7031         W_ERROR_HAVE_NO_MEMORY(info);
7032
7033         /* construct the list of form structures */
7034         for (i=0; i<num_builtin_forms; i++) {
7035                 DEBUGADD(6,("Filling builtin form number [%d]\n",i));
7036                 result = fill_form_info_1(info, &info[i].info1,
7037                                           &builtin_forms[i]);
7038                 if (!W_ERROR_IS_OK(result)) {
7039                         goto out;
7040                 }
7041         }
7042
7043         for (i=0; i<num_user_forms; i++) {
7044                 DEBUGADD(6,("Filling user form number [%d]\n",i));
7045                 result = fill_form_info_1(info, &info[i+num_builtin_forms].info1,
7046                                           &user_forms[i]);
7047                 if (!W_ERROR_IS_OK(result)) {
7048                         goto out;
7049                 }
7050         }
7051
7052  out:
7053         if (!W_ERROR_IS_OK(result)) {
7054                 TALLOC_FREE(info);
7055                 *count = 0;
7056                 return result;
7057         }
7058
7059         *info_p = info;
7060
7061         return WERR_OK;
7062 }
7063
7064 /****************************************************************
7065  _spoolss_EnumForms
7066 ****************************************************************/
7067
7068 WERROR _spoolss_EnumForms(pipes_struct *p,
7069                           struct spoolss_EnumForms *r)
7070 {
7071         WERROR result;
7072         nt_forms_struct *user_forms = NULL;
7073         nt_forms_struct *builtin_forms = NULL;
7074         uint32_t num_user_forms;
7075         uint32_t num_builtin_forms;
7076
7077         *r->out.count = 0;
7078         *r->out.needed = 0;
7079         *r->out.info = NULL;
7080
7081         /* that's an [in out] buffer */
7082
7083         if (!r->in.buffer && (r->in.offered != 0) ) {
7084                 return WERR_INVALID_PARAM;
7085         }
7086
7087         DEBUG(4,("_spoolss_EnumForms\n"));
7088         DEBUGADD(5,("Offered buffer size [%d]\n", r->in.offered));
7089         DEBUGADD(5,("Info level [%d]\n",          r->in.level));
7090
7091         num_builtin_forms = get_builtin_ntforms(&builtin_forms);
7092         DEBUGADD(5,("Number of builtin forms [%d]\n", num_builtin_forms));
7093         num_user_forms = get_ntforms(&user_forms);
7094         DEBUGADD(5,("Number of user forms [%d]\n", num_user_forms));
7095
7096         if (num_user_forms + num_builtin_forms == 0) {
7097                 SAFE_FREE(builtin_forms);
7098                 SAFE_FREE(user_forms);
7099                 return WERR_NO_MORE_ITEMS;
7100         }
7101
7102         switch (r->in.level) {
7103         case 1:
7104                 result = spoolss_enumforms_level1(p->mem_ctx,
7105                                                   builtin_forms,
7106                                                   num_builtin_forms,
7107                                                   user_forms,
7108                                                   num_user_forms,
7109                                                   r->out.info,
7110                                                   r->out.count);
7111                 break;
7112         default:
7113                 result = WERR_UNKNOWN_LEVEL;
7114                 break;
7115         }
7116
7117         SAFE_FREE(user_forms);
7118         SAFE_FREE(builtin_forms);
7119
7120         if (!W_ERROR_IS_OK(result)) {
7121                 return result;
7122         }
7123
7124         *r->out.needed  = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
7125                                                      spoolss_EnumForms, NULL,
7126                                                      *r->out.info, r->in.level,
7127                                                      *r->out.count);
7128         *r->out.info    = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
7129         *r->out.count   = SPOOLSS_BUFFER_OK(*r->out.count, 0);
7130
7131         return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
7132 }
7133
7134 /****************************************************************
7135 ****************************************************************/
7136
7137 static WERROR find_form_byname(const char *name,
7138                                nt_forms_struct *form)
7139 {
7140         nt_forms_struct *list = NULL;
7141         int num_forms = 0, i = 0;
7142
7143         if (get_a_builtin_ntform_by_string(name, form)) {
7144                 return WERR_OK;
7145         }
7146
7147         num_forms = get_ntforms(&list);
7148         DEBUGADD(5,("Number of forms [%d]\n", num_forms));
7149
7150         if (num_forms == 0) {
7151                 return WERR_BADFID;
7152         }
7153
7154         /* Check if the requested name is in the list of form structures */
7155         for (i = 0; i < num_forms; i++) {
7156
7157                 DEBUG(4,("checking form %s (want %s)\n", list[i].name, name));
7158
7159                 if (strequal(name, list[i].name)) {
7160                         DEBUGADD(6,("Found form %s number [%d]\n", name, i));
7161                         *form = list[i];
7162                         SAFE_FREE(list);
7163                         return WERR_OK;
7164                 }
7165         }
7166
7167         SAFE_FREE(list);
7168
7169         return WERR_BADFID;
7170 }
7171
7172 /****************************************************************
7173  _spoolss_GetForm
7174 ****************************************************************/
7175
7176 WERROR _spoolss_GetForm(pipes_struct *p,
7177                         struct spoolss_GetForm *r)
7178 {
7179         WERROR result;
7180         nt_forms_struct form;
7181
7182         /* that's an [in out] buffer */
7183
7184         if (!r->in.buffer && (r->in.offered != 0)) {
7185                 return WERR_INVALID_PARAM;
7186         }
7187
7188         DEBUG(4,("_spoolss_GetForm\n"));
7189         DEBUGADD(5,("Offered buffer size [%d]\n", r->in.offered));
7190         DEBUGADD(5,("Info level [%d]\n",          r->in.level));
7191
7192         result = find_form_byname(r->in.form_name, &form);
7193         if (!W_ERROR_IS_OK(result)) {
7194                 TALLOC_FREE(r->out.info);
7195                 return result;
7196         }
7197
7198         switch (r->in.level) {
7199         case 1:
7200                 result = fill_form_info_1(p->mem_ctx,
7201                                           &r->out.info->info1,
7202                                           &form);
7203                 break;
7204
7205         default:
7206                 result = WERR_UNKNOWN_LEVEL;
7207                 break;
7208         }
7209
7210         if (!W_ERROR_IS_OK(result)) {
7211                 TALLOC_FREE(r->out.info);
7212                 return result;
7213         }
7214
7215         *r->out.needed  = SPOOLSS_BUFFER_UNION(spoolss_FormInfo, NULL,
7216                                                r->out.info, r->in.level);
7217         r->out.info     = SPOOLSS_BUFFER_OK(r->out.info, NULL);
7218
7219         return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
7220 }
7221
7222 /****************************************************************************
7223 ****************************************************************************/
7224
7225 static WERROR fill_port_1(TALLOC_CTX *mem_ctx,
7226                           struct spoolss_PortInfo1 *r,
7227                           const char *name)
7228 {
7229         r->port_name = talloc_strdup(mem_ctx, name);
7230         W_ERROR_HAVE_NO_MEMORY(r->port_name);
7231
7232         return WERR_OK;
7233 }
7234
7235 /****************************************************************************
7236  TODO: This probably needs distinguish between TCP/IP and Local ports
7237  somehow.
7238 ****************************************************************************/
7239
7240 static WERROR fill_port_2(TALLOC_CTX *mem_ctx,
7241                           struct spoolss_PortInfo2 *r,
7242                           const char *name)
7243 {
7244         r->port_name = talloc_strdup(mem_ctx, name);
7245         W_ERROR_HAVE_NO_MEMORY(r->port_name);
7246
7247         r->monitor_name = talloc_strdup(mem_ctx, "Local Monitor");
7248         W_ERROR_HAVE_NO_MEMORY(r->monitor_name);
7249
7250         r->description = talloc_strdup(mem_ctx, SPL_LOCAL_PORT);
7251         W_ERROR_HAVE_NO_MEMORY(r->description);
7252
7253         r->port_type = SPOOLSS_PORT_TYPE_WRITE;
7254         r->reserved = 0;
7255
7256         return WERR_OK;
7257 }
7258
7259
7260 /****************************************************************************
7261  wrapper around the enumer ports command
7262 ****************************************************************************/
7263
7264 WERROR enumports_hook(TALLOC_CTX *ctx, int *count, char ***lines )
7265 {
7266         char *cmd = lp_enumports_cmd();
7267         char **qlines = NULL;
7268         char *command = NULL;
7269         int numlines;
7270         int ret;
7271         int fd;
7272
7273         *count = 0;
7274         *lines = NULL;
7275
7276         /* if no hook then just fill in the default port */
7277
7278         if ( !*cmd ) {
7279                 if (!(qlines = TALLOC_ARRAY( NULL, char*, 2 ))) {
7280                         return WERR_NOMEM;
7281                 }
7282                 if (!(qlines[0] = talloc_strdup(qlines, SAMBA_PRINTER_PORT_NAME ))) {
7283                         TALLOC_FREE(qlines);
7284                         return WERR_NOMEM;
7285                 }
7286                 qlines[1] = NULL;
7287                 numlines = 1;
7288         }
7289         else {
7290                 /* we have a valid enumport command */
7291
7292                 command = talloc_asprintf(ctx, "%s \"%d\"", cmd, 1);
7293                 if (!command) {
7294                         return WERR_NOMEM;
7295                 }
7296
7297                 DEBUG(10,("Running [%s]\n", command));
7298                 ret = smbrun(command, &fd);
7299                 DEBUG(10,("Returned [%d]\n", ret));
7300                 TALLOC_FREE(command);
7301                 if (ret != 0) {
7302                         if (fd != -1) {
7303                                 close(fd);
7304                         }
7305                         return WERR_ACCESS_DENIED;
7306                 }
7307
7308                 numlines = 0;
7309                 qlines = fd_lines_load(fd, &numlines, 0, NULL);
7310                 DEBUGADD(10,("Lines returned = [%d]\n", numlines));
7311                 close(fd);
7312         }
7313
7314         *count = numlines;
7315         *lines = qlines;
7316
7317         return WERR_OK;
7318 }
7319
7320 /****************************************************************************
7321  enumports level 1.
7322 ****************************************************************************/
7323
7324 static WERROR enumports_level_1(TALLOC_CTX *mem_ctx,
7325                                 union spoolss_PortInfo **info_p,
7326                                 uint32_t *count)
7327 {
7328         union spoolss_PortInfo *info = NULL;
7329         int i=0;
7330         WERROR result = WERR_OK;
7331         char **qlines = NULL;
7332         int numlines = 0;
7333
7334         result = enumports_hook(talloc_tos(), &numlines, &qlines );
7335         if (!W_ERROR_IS_OK(result)) {
7336                 goto out;
7337         }
7338
7339         if (numlines) {
7340                 info = TALLOC_ARRAY(mem_ctx, union spoolss_PortInfo, numlines);
7341                 if (!info) {
7342                         DEBUG(10,("Returning WERR_NOMEM\n"));
7343                         result = WERR_NOMEM;
7344                         goto out;
7345                 }
7346
7347                 for (i=0; i<numlines; i++) {
7348                         DEBUG(6,("Filling port number [%d] with port [%s]\n", i, qlines[i]));
7349                         result = fill_port_1(info, &info[i].info1, qlines[i]);
7350                         if (!W_ERROR_IS_OK(result)) {
7351                                 goto out;
7352                         }
7353                 }
7354         }
7355         TALLOC_FREE(qlines);
7356
7357 out:
7358         if (!W_ERROR_IS_OK(result)) {
7359                 TALLOC_FREE(info);
7360                 TALLOC_FREE(qlines);
7361                 *count = 0;
7362                 *info_p = NULL;
7363                 return result;
7364         }
7365
7366         *info_p = info;
7367         *count = numlines;
7368
7369         return WERR_OK;
7370 }
7371
7372 /****************************************************************************
7373  enumports level 2.
7374 ****************************************************************************/
7375
7376 static WERROR enumports_level_2(TALLOC_CTX *mem_ctx,
7377                                 union spoolss_PortInfo **info_p,
7378                                 uint32_t *count)
7379 {
7380         union spoolss_PortInfo *info = NULL;
7381         int i=0;
7382         WERROR result = WERR_OK;
7383         char **qlines = NULL;
7384         int numlines = 0;
7385
7386         result = enumports_hook(talloc_tos(), &numlines, &qlines );
7387         if (!W_ERROR_IS_OK(result)) {
7388                 goto out;
7389         }
7390
7391         if (numlines) {
7392                 info = TALLOC_ARRAY(mem_ctx, union spoolss_PortInfo, numlines);
7393                 if (!info) {
7394                         DEBUG(10,("Returning WERR_NOMEM\n"));
7395                         result = WERR_NOMEM;
7396                         goto out;
7397                 }
7398
7399                 for (i=0; i<numlines; i++) {
7400                         DEBUG(6,("Filling port number [%d] with port [%s]\n", i, qlines[i]));
7401                         result = fill_port_2(info, &info[i].info2, qlines[i]);
7402                         if (!W_ERROR_IS_OK(result)) {
7403                                 goto out;
7404                         }
7405                 }
7406         }
7407         TALLOC_FREE(qlines);
7408
7409 out:
7410         if (!W_ERROR_IS_OK(result)) {
7411                 TALLOC_FREE(info);
7412                 TALLOC_FREE(qlines);
7413                 *count = 0;
7414                 *info_p = NULL;
7415                 return result;
7416         }
7417
7418         *info_p = info;
7419         *count = numlines;
7420
7421         return WERR_OK;
7422 }
7423
7424 /****************************************************************
7425  _spoolss_EnumPorts
7426 ****************************************************************/
7427
7428 WERROR _spoolss_EnumPorts(pipes_struct *p,
7429                           struct spoolss_EnumPorts *r)
7430 {
7431         WERROR result;
7432
7433         /* that's an [in out] buffer */
7434
7435         if (!r->in.buffer && (r->in.offered != 0)) {
7436                 return WERR_INVALID_PARAM;
7437         }
7438
7439         DEBUG(4,("_spoolss_EnumPorts\n"));
7440
7441         *r->out.count = 0;
7442         *r->out.needed = 0;
7443         *r->out.info = NULL;
7444
7445         switch (r->in.level) {
7446         case 1:
7447                 result = enumports_level_1(p->mem_ctx, r->out.info,
7448                                            r->out.count);
7449                 break;
7450         case 2:
7451                 result = enumports_level_2(p->mem_ctx, r->out.info,
7452                                            r->out.count);
7453                 break;
7454         default:
7455                 return WERR_UNKNOWN_LEVEL;
7456         }
7457
7458         if (!W_ERROR_IS_OK(result)) {
7459                 return result;
7460         }
7461
7462         *r->out.needed  = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
7463                                                      spoolss_EnumPorts, NULL,
7464                                                      *r->out.info, r->in.level,
7465                                                      *r->out.count);
7466         *r->out.info    = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
7467         *r->out.count   = SPOOLSS_BUFFER_OK(*r->out.count, 0);
7468
7469         return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
7470 }
7471
7472 /****************************************************************************
7473 ****************************************************************************/
7474
7475 static WERROR spoolss_addprinterex_level_2(pipes_struct *p,
7476                                            const char *server,
7477                                            struct spoolss_SetPrinterInfoCtr *info_ctr,
7478                                            struct spoolss_DeviceMode *devmode,
7479                                            struct security_descriptor *sec_desc,
7480                                            struct spoolss_UserLevelCtr *user_ctr,
7481                                            struct policy_handle *handle)
7482 {
7483         NT_PRINTER_INFO_LEVEL *printer = NULL;
7484         fstring name;
7485         int     snum;
7486         WERROR err = WERR_OK;
7487
7488         if ( !(printer = TALLOC_ZERO_P(NULL, NT_PRINTER_INFO_LEVEL)) ) {
7489                 DEBUG(0,("spoolss_addprinterex_level_2: malloc fail.\n"));
7490                 return WERR_NOMEM;
7491         }
7492
7493         /* convert from UNICODE to ASCII - this allocates the info_2 struct inside *printer.*/
7494         if (!convert_printer_info(info_ctr, printer)) {
7495                 free_a_printer(&printer, 2);
7496                 return WERR_NOMEM;
7497         }
7498
7499         /* check to see if the printer already exists */
7500
7501         if ((snum = print_queue_snum(printer->info_2->sharename)) != -1) {
7502                 DEBUG(5, ("spoolss_addprinterex_level_2: Attempted to add a printer named [%s] when one already existed!\n",
7503                         printer->info_2->sharename));
7504                 free_a_printer(&printer, 2);
7505                 return WERR_PRINTER_ALREADY_EXISTS;
7506         }
7507
7508         /* FIXME!!!  smbd should check to see if the driver is installed before
7509            trying to add a printer like this  --jerry */
7510
7511         if (*lp_addprinter_cmd() ) {
7512                 if ( !add_printer_hook(p->mem_ctx, p->server_info->ptok,
7513                                        printer) ) {
7514                         free_a_printer(&printer,2);
7515                         return WERR_ACCESS_DENIED;
7516                 }
7517         } else {
7518                 DEBUG(0,("spoolss_addprinterex_level_2: add printer for printer %s called and no"
7519                         "smb.conf parameter \"addprinter command\" is defined. This"
7520                         "parameter must exist for this call to succeed\n",
7521                         printer->info_2->sharename ));
7522         }
7523
7524         /* use our primary netbios name since get_a_printer() will convert
7525            it to what the client expects on a case by case basis */
7526
7527         slprintf(name, sizeof(name)-1, "\\\\%s\\%s", global_myname(),
7528              printer->info_2->sharename);
7529
7530
7531         if ((snum = print_queue_snum(printer->info_2->sharename)) == -1) {
7532                 free_a_printer(&printer,2);
7533                 return WERR_ACCESS_DENIED;
7534         }
7535
7536         /* you must be a printer admin to add a new printer */
7537         if (!print_access_check(p->server_info, snum, PRINTER_ACCESS_ADMINISTER)) {
7538                 free_a_printer(&printer,2);
7539                 return WERR_ACCESS_DENIED;
7540         }
7541
7542         /*
7543          * Do sanity check on the requested changes for Samba.
7544          */
7545
7546         if (!check_printer_ok(printer->info_2, snum)) {
7547                 free_a_printer(&printer,2);
7548                 return WERR_INVALID_PARAM;
7549         }
7550
7551         /*
7552          * When a printer is created, the drivername bound to the printer is used
7553          * to lookup previously saved driver initialization info, which is then
7554          * bound to the new printer, simulating what happens in the Windows arch.
7555          */
7556
7557         if (!devmode)
7558         {
7559                 set_driver_init(printer, 2);
7560         }
7561         else
7562         {
7563                 /* A valid devmode was included, convert and link it
7564                 */
7565                 DEBUGADD(10, ("spoolss_addprinterex_level_2: devmode included, converting\n"));
7566
7567                 if (!convert_devicemode(printer->info_2->printername, devmode,
7568                                         &printer->info_2->devmode)) {
7569                         return  WERR_NOMEM;
7570                 }
7571         }
7572
7573         /* write the ASCII on disk */
7574         err = mod_a_printer(printer, 2);
7575         if (!W_ERROR_IS_OK(err)) {
7576                 free_a_printer(&printer,2);
7577                 return err;
7578         }
7579
7580         if (!open_printer_hnd(p, handle, name, PRINTER_ACCESS_ADMINISTER)) {
7581                 /* Handle open failed - remove addition. */
7582                 del_a_printer(printer->info_2->sharename);
7583                 free_a_printer(&printer,2);
7584                 ZERO_STRUCTP(handle);
7585                 return WERR_ACCESS_DENIED;
7586         }
7587
7588         update_c_setprinter(false);
7589         free_a_printer(&printer,2);
7590
7591         return WERR_OK;
7592 }
7593
7594 /****************************************************************
7595  _spoolss_AddPrinterEx
7596 ****************************************************************/
7597
7598 WERROR _spoolss_AddPrinterEx(pipes_struct *p,
7599                              struct spoolss_AddPrinterEx *r)
7600 {
7601         switch (r->in.info_ctr->level) {
7602         case 1:
7603                 /* we don't handle yet */
7604                 /* but I know what to do ... */
7605                 return WERR_UNKNOWN_LEVEL;
7606         case 2:
7607                 return spoolss_addprinterex_level_2(p, r->in.server,
7608                                                     r->in.info_ctr,
7609                                                     r->in.devmode_ctr->devmode,
7610                                                     r->in.secdesc_ctr->sd,
7611                                                     r->in.userlevel_ctr,
7612                                                     r->out.handle);
7613         default:
7614                 return WERR_UNKNOWN_LEVEL;
7615         }
7616 }
7617
7618 /****************************************************************
7619  _spoolss_AddPrinterDriver
7620 ****************************************************************/
7621
7622 WERROR _spoolss_AddPrinterDriver(pipes_struct *p,
7623                                  struct spoolss_AddPrinterDriver *r)
7624 {
7625         uint32_t level = r->in.info_ctr->level;
7626         struct spoolss_AddDriverInfoCtr *info = r->in.info_ctr;
7627         WERROR err = WERR_OK;
7628         NT_PRINTER_DRIVER_INFO_LEVEL driver;
7629         const char *driver_name = NULL;
7630         uint32_t version;
7631         const char *fn;
7632
7633         switch (p->hdr_req.opnum) {
7634                 case NDR_SPOOLSS_ADDPRINTERDRIVER:
7635                         fn = "_spoolss_AddPrinterDriver";
7636                         break;
7637                 case NDR_SPOOLSS_ADDPRINTERDRIVEREX:
7638                         fn = "_spoolss_AddPrinterDriverEx";
7639                         break;
7640                 default:
7641                         return WERR_INVALID_PARAM;
7642         }
7643
7644
7645         /* FIXME */
7646         if (level != 3 && level != 6) {
7647                 /* Clever hack from Martin Zielinski <mz@seh.de>
7648                  * to allow downgrade from level 8 (Vista).
7649                  */
7650                 DEBUG(0,("%s: level %d not yet implemented\n", fn, level));
7651                 return WERR_UNKNOWN_LEVEL;
7652         }
7653
7654         ZERO_STRUCT(driver);
7655
7656         if (!convert_printer_driver_info(info, &driver, level)) {
7657                 err = WERR_NOMEM;
7658                 goto done;
7659         }
7660
7661         DEBUG(5,("Cleaning driver's information\n"));
7662         err = clean_up_driver_struct(p, driver, level);
7663         if (!W_ERROR_IS_OK(err))
7664                 goto done;
7665
7666         DEBUG(5,("Moving driver to final destination\n"));
7667         if( !W_ERROR_IS_OK(err = move_driver_to_download_area(p, driver, level,
7668                                                               &err)) ) {
7669                 goto done;
7670         }
7671
7672         if (add_a_printer_driver(driver, level)!=0) {
7673                 err = WERR_ACCESS_DENIED;
7674                 goto done;
7675         }
7676
7677         switch(level) {
7678         case 3:
7679                 driver_name = driver.info_3->name ? driver.info_3->name : "";
7680                 break;
7681         case 6:
7682                 driver_name = driver.info_6->name ? driver.info_6->name : "";
7683                 break;
7684         }
7685
7686         /*
7687          * I think this is where he DrvUpgradePrinter() hook would be
7688          * be called in a driver's interface DLL on a Windows NT 4.0/2k
7689          * server.  Right now, we just need to send ourselves a message
7690          * to update each printer bound to this driver.   --jerry
7691          */
7692
7693         if (!srv_spoolss_drv_upgrade_printer(driver_name)) {
7694                 DEBUG(0,("%s: Failed to send message about upgrading driver [%s]!\n",
7695                         fn, driver_name));
7696         }
7697
7698         /*
7699          * Based on the version (e.g. driver destination dir: 0=9x,2=Nt/2k,3=2k/Xp),
7700          * decide if the driver init data should be deleted. The rules are:
7701          *  1) never delete init data if it is a 9x driver, they don't use it anyway
7702          *  2) delete init data only if there is no 2k/Xp driver
7703          *  3) always delete init data
7704          * The generalized rule is always use init data from the highest order driver.
7705          * It is necessary to follow the driver install by an initialization step to
7706          * finish off this process.
7707         */
7708         if (level == 3)
7709                 version = driver.info_3->cversion;
7710         else if (level == 6)
7711                 version = driver.info_6->version;
7712         else
7713                 version = -1;
7714         switch (version) {
7715                 /*
7716                  * 9x printer driver - never delete init data
7717                 */
7718                 case 0:
7719                         DEBUG(10,("%s: init data not deleted for 9x driver [%s]\n",
7720                                 fn, driver_name));
7721                         break;
7722
7723                 /*
7724                  * Nt or 2k (compatiblity mode) printer driver - only delete init data if
7725                  * there is no 2k/Xp driver init data for this driver name.
7726                 */
7727                 case 2:
7728                 {
7729                         NT_PRINTER_DRIVER_INFO_LEVEL driver1;
7730
7731                         if (!W_ERROR_IS_OK(get_a_printer_driver(&driver1, 3, driver_name, "Windows NT x86", 3))) {
7732                                 /*
7733                                  * No 2k/Xp driver found, delete init data (if any) for the new Nt driver.
7734                                 */
7735                                 if (!del_driver_init(driver_name))
7736                                         DEBUG(6,("%s: del_driver_init(%s) Nt failed!\n",
7737                                                 fn, driver_name));
7738                         } else {
7739                                 /*
7740                                  * a 2k/Xp driver was found, don't delete init data because Nt driver will use it.
7741                                 */
7742                                 free_a_printer_driver(driver1,3);
7743                                 DEBUG(10,("%s: init data not deleted for Nt driver [%s]\n",
7744                                         fn, driver_name));
7745                         }
7746                 }
7747                 break;
7748
7749                 /*
7750                  * 2k or Xp printer driver - always delete init data
7751                 */
7752                 case 3:
7753                         if (!del_driver_init(driver_name))
7754                                 DEBUG(6,("%s: del_driver_init(%s) 2k/Xp failed!\n",
7755                                         fn, driver_name));
7756                         break;
7757
7758                 default:
7759                         DEBUG(0,("%s: invalid level=%d\n", fn, level));
7760                         break;
7761         }
7762
7763
7764 done:
7765         free_a_printer_driver(driver, level);
7766         return err;
7767 }
7768
7769 /****************************************************************
7770  _spoolss_AddPrinterDriverEx
7771 ****************************************************************/
7772
7773 WERROR _spoolss_AddPrinterDriverEx(pipes_struct *p,
7774                                    struct spoolss_AddPrinterDriverEx *r)
7775 {
7776         struct spoolss_AddPrinterDriver a;
7777
7778         /*
7779          * we only support the semantics of AddPrinterDriver()
7780          * i.e. only copy files that are newer than existing ones
7781          */
7782
7783         if (r->in.flags != APD_COPY_NEW_FILES) {
7784                 return WERR_ACCESS_DENIED;
7785         }
7786
7787         a.in.servername         = r->in.servername;
7788         a.in.info_ctr           = r->in.info_ctr;
7789
7790         return _spoolss_AddPrinterDriver(p, &a);
7791 }
7792
7793 /****************************************************************************
7794 ****************************************************************************/
7795
7796 struct _spoolss_paths {
7797         int type;
7798         const char *share;
7799         const char *dir;
7800 };
7801
7802 enum { SPOOLSS_DRIVER_PATH, SPOOLSS_PRTPROCS_PATH };
7803
7804 static const struct _spoolss_paths spoolss_paths[]= {
7805         { SPOOLSS_DRIVER_PATH,          "print$",       "DRIVERS" },
7806         { SPOOLSS_PRTPROCS_PATH,        "prnproc$",     "PRTPROCS" }
7807 };
7808
7809 static WERROR compose_spoolss_server_path(TALLOC_CTX *mem_ctx,
7810                                           const char *servername,
7811                                           const char *environment,
7812                                           int component,
7813                                           char **path)
7814 {
7815         const char *pservername = NULL;
7816         const char *long_archi = SPOOLSS_ARCHITECTURE_NT_X86;
7817         const char *short_archi;
7818
7819         *path = NULL;
7820
7821         /* environment may be empty */
7822         if (environment && strlen(environment)) {
7823                 long_archi = environment;
7824         }
7825
7826         /* servername may be empty */
7827         if (servername && strlen(servername)) {
7828                 pservername = canon_servername(servername);
7829
7830                 if (!is_myname_or_ipaddr(pservername)) {
7831                         return WERR_INVALID_PARAM;
7832                 }
7833         }
7834
7835         if (!(short_archi = get_short_archi(long_archi))) {
7836                 return WERR_INVALID_ENVIRONMENT;
7837         }
7838
7839         switch (component) {
7840         case SPOOLSS_PRTPROCS_PATH:
7841         case SPOOLSS_DRIVER_PATH:
7842                 if (pservername) {
7843                         *path = talloc_asprintf(mem_ctx,
7844                                         "\\\\%s\\%s\\%s",
7845                                         pservername,
7846                                         spoolss_paths[component].share,
7847                                         short_archi);
7848                 } else {
7849                         *path = talloc_asprintf(mem_ctx, "%s\\%s\\%s",
7850                                         SPOOLSS_DEFAULT_SERVER_PATH,
7851                                         spoolss_paths[component].dir,
7852                                         short_archi);
7853                 }
7854                 break;
7855         default:
7856                 return WERR_INVALID_PARAM;
7857         }
7858
7859         if (!*path) {
7860                 return WERR_NOMEM;
7861         }
7862
7863         return WERR_OK;
7864 }
7865
7866 /****************************************************************************
7867 ****************************************************************************/
7868
7869 static WERROR getprinterdriverdir_level_1(TALLOC_CTX *mem_ctx,
7870                                           const char *servername,
7871                                           const char *environment,
7872                                           struct spoolss_DriverDirectoryInfo1 *r)
7873 {
7874         WERROR werr;
7875         char *path = NULL;
7876
7877         werr = compose_spoolss_server_path(mem_ctx,
7878                                            servername,
7879                                            environment,
7880                                            SPOOLSS_DRIVER_PATH,
7881                                            &path);
7882         if (!W_ERROR_IS_OK(werr)) {
7883                 return werr;
7884         }
7885
7886         DEBUG(4,("printer driver directory: [%s]\n", path));
7887
7888         r->directory_name = path;
7889
7890         return WERR_OK;
7891 }
7892
7893 /****************************************************************
7894  _spoolss_GetPrinterDriverDirectory
7895 ****************************************************************/
7896
7897 WERROR _spoolss_GetPrinterDriverDirectory(pipes_struct *p,
7898                                           struct spoolss_GetPrinterDriverDirectory *r)
7899 {
7900         WERROR werror;
7901
7902         /* that's an [in out] buffer */
7903
7904         if (!r->in.buffer && (r->in.offered != 0)) {
7905                 return WERR_INVALID_PARAM;
7906         }
7907
7908         DEBUG(5,("_spoolss_GetPrinterDriverDirectory: level %d\n",
7909                 r->in.level));
7910
7911         *r->out.needed = 0;
7912
7913         /* r->in.level is ignored */
7914
7915         werror = getprinterdriverdir_level_1(p->mem_ctx,
7916                                              r->in.server,
7917                                              r->in.environment,
7918                                              &r->out.info->info1);
7919         if (!W_ERROR_IS_OK(werror)) {
7920                 TALLOC_FREE(r->out.info);
7921                 return werror;
7922         }
7923
7924         *r->out.needed  = SPOOLSS_BUFFER_UNION(spoolss_DriverDirectoryInfo, NULL,
7925                                                r->out.info, r->in.level);
7926         r->out.info     = SPOOLSS_BUFFER_OK(r->out.info, NULL);
7927
7928         return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
7929 }
7930
7931 /****************************************************************
7932  _spoolss_EnumPrinterData
7933 ****************************************************************/
7934
7935 WERROR _spoolss_EnumPrinterData(pipes_struct *p,
7936                                 struct spoolss_EnumPrinterData *r)
7937 {
7938         NT_PRINTER_INFO_LEVEL *printer = NULL;
7939         Printer_entry   *Printer = find_printer_index_by_hnd(p, r->in.handle);
7940         int             snum;
7941         WERROR          result;
7942         REGISTRY_VALUE  *val = NULL;
7943         NT_PRINTER_DATA *p_data;
7944         int             i, key_index, num_values;
7945         int             name_length;
7946
7947         *r->out.value_needed    = 0;
7948         *r->out.type            = REG_NONE;
7949         *r->out.data_needed     = 0;
7950
7951         DEBUG(5,("_spoolss_EnumPrinterData\n"));
7952
7953         if (!Printer) {
7954                 DEBUG(2,("_spoolss_EnumPrinterData: Invalid handle (%s:%u:%u).\n",
7955                         OUR_HANDLE(r->in.handle)));
7956                 return WERR_BADFID;
7957         }
7958
7959         if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
7960                 return WERR_BADFID;
7961         }
7962
7963         result = get_a_printer(Printer, &printer, 2, lp_const_servicename(snum));
7964         if (!W_ERROR_IS_OK(result)) {
7965                 return result;
7966         }
7967
7968         p_data = printer->info_2->data;
7969         key_index = lookup_printerkey( p_data, SPOOL_PRINTERDATA_KEY );
7970
7971         result = WERR_OK;
7972
7973         /*
7974          * The NT machine wants to know the biggest size of value and data
7975          *
7976          * cf: MSDN EnumPrinterData remark section
7977          */
7978
7979         if (!r->in.value_offered && !r->in.data_offered && (key_index != -1)) {
7980
7981                 uint32_t biggest_valuesize = 0;
7982                 uint32_t biggest_datasize = 0;
7983
7984                 DEBUGADD(6,("Activating NT mega-hack to find sizes\n"));
7985
7986                 num_values = regval_ctr_numvals( p_data->keys[key_index].values );
7987
7988                 for ( i=0; i<num_values; i++ )
7989                 {
7990                         val = regval_ctr_specific_value( p_data->keys[key_index].values, i );
7991
7992                         name_length = strlen(val->valuename);
7993                         if ( strlen(val->valuename) > biggest_valuesize )
7994                                 biggest_valuesize = name_length;
7995
7996                         if ( val->size > biggest_datasize )
7997                                 biggest_datasize = val->size;
7998
7999                         DEBUG(6,("current values: [%d], [%d]\n", biggest_valuesize,
8000                                 biggest_datasize));
8001                 }
8002
8003                 /* the value is an UNICODE string but real_value_size is the length
8004                    in bytes including the trailing 0 */
8005
8006                 *r->out.value_needed = 2 * (1 + biggest_valuesize);
8007                 *r->out.data_needed  = biggest_datasize;
8008
8009                 DEBUG(6,("final values: [%d], [%d]\n",
8010                         *r->out.value_needed, *r->out.data_needed));
8011
8012                 goto done;
8013         }
8014
8015         /*
8016          * the value len is wrong in NT sp3
8017          * that's the number of bytes not the number of unicode chars
8018          */
8019
8020         if (key_index != -1) {
8021                 val = regval_ctr_specific_value(p_data->keys[key_index].values,
8022                                                 r->in.enum_index);
8023         }
8024
8025         if (!val) {
8026
8027                 /* out_value should default to "" or else NT4 has
8028                    problems unmarshalling the response */
8029
8030                 if (r->in.value_offered) {
8031                         *r->out.value_needed = 1;
8032                         r->out.value_name = talloc_strdup(r, "");
8033                         if (!r->out.value_name) {
8034                                 result = WERR_NOMEM;
8035                                 goto done;
8036                         }
8037                 } else {
8038                         r->out.value_name = NULL;
8039                         *r->out.value_needed = 0;
8040                 }
8041
8042                 /* the data is counted in bytes */
8043
8044                 *r->out.data_needed = r->in.data_offered;
8045
8046                 result = WERR_NO_MORE_ITEMS;
8047         } else {
8048                 /*
8049                  * the value is:
8050                  * - counted in bytes in the request
8051                  * - counted in UNICODE chars in the max reply
8052                  * - counted in bytes in the real size
8053                  *
8054                  * take a pause *before* coding not *during* coding
8055                  */
8056
8057                 /* name */
8058                 if (r->in.value_offered) {
8059                         r->out.value_name = talloc_strdup(r, regval_name(val));
8060                         if (!r->out.value_name) {
8061                                 result = WERR_NOMEM;
8062                                 goto done;
8063                         }
8064                         *r->out.value_needed = strlen_m(regval_name(val));
8065                 } else {
8066                         r->out.value_name = NULL;
8067                         *r->out.value_needed = 0;
8068                 }
8069
8070                 /* type */
8071
8072                 *r->out.type = regval_type(val);
8073
8074                 /* data - counted in bytes */
8075
8076                 /*
8077                  * See the section "Dynamically Typed Query Parameters"
8078                  * in MS-RPRN.
8079                  */
8080
8081                 if (r->out.data && regval_data_p(val) &&
8082                                 regval_size(val) && r->in.data_offered) {
8083                         memcpy(r->out.data, regval_data_p(val),
8084                                 MIN(regval_size(val),r->in.data_offered));
8085                 }
8086
8087                 *r->out.data_needed = regval_size(val);
8088         }
8089
8090 done:
8091         free_a_printer(&printer, 2);
8092         return result;
8093 }
8094
8095 /****************************************************************
8096  _spoolss_SetPrinterData
8097 ****************************************************************/
8098
8099 WERROR _spoolss_SetPrinterData(pipes_struct *p,
8100                                struct spoolss_SetPrinterData *r)
8101 {
8102         NT_PRINTER_INFO_LEVEL *printer = NULL;
8103         int snum=0;
8104         WERROR result = WERR_OK;
8105         Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
8106         DATA_BLOB blob;
8107
8108         DEBUG(5,("_spoolss_SetPrinterData\n"));
8109
8110         if (!Printer) {
8111                 DEBUG(2,("_spoolss_SetPrinterData: Invalid handle (%s:%u:%u).\n",
8112                         OUR_HANDLE(r->in.handle)));
8113                 return WERR_BADFID;
8114         }
8115
8116         if (Printer->printer_type == SPLHND_SERVER) {
8117                 DEBUG(10,("_spoolss_SetPrinterData: "
8118                         "Not implemented for server handles yet\n"));
8119                 return WERR_INVALID_PARAM;
8120         }
8121
8122         if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
8123                 return WERR_BADFID;
8124         }
8125
8126         /*
8127          * Access check : NT returns "access denied" if you make a
8128          * SetPrinterData call without the necessary privildge.
8129          * we were originally returning OK if nothing changed
8130          * which made Win2k issue **a lot** of SetPrinterData
8131          * when connecting to a printer  --jerry
8132          */
8133
8134         if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
8135                 DEBUG(3,("_spoolss_SetPrinterData: "
8136                         "change denied by handle access permissions\n"));
8137                 result = WERR_ACCESS_DENIED;
8138                 goto done;
8139         }
8140
8141         result = get_a_printer(Printer, &printer, 2, lp_const_servicename(snum));
8142         if (!W_ERROR_IS_OK(result)) {
8143                 return result;
8144         }
8145
8146         result = push_spoolss_PrinterData(p->mem_ctx, &blob,
8147                                           r->in.type, &r->in.data);
8148         if (!W_ERROR_IS_OK(result)) {
8149                 goto done;
8150         }
8151
8152         /*
8153          * When client side code sets a magic printer data key, detect it and save
8154          * the current printer data and the magic key's data (its the DEVMODE) for
8155          * future printer/driver initializations.
8156          */
8157         if ((r->in.type == REG_BINARY) && strequal(r->in.value_name, PHANTOM_DEVMODE_KEY)) {
8158                 /* Set devmode and printer initialization info */
8159                 result = save_driver_init(printer, 2, blob.data, blob.length);
8160
8161                 srv_spoolss_reset_printerdata(printer->info_2->drivername);
8162
8163                 goto done;
8164         }
8165
8166         result = set_printer_dataex(printer, SPOOL_PRINTERDATA_KEY,
8167                                     r->in.value_name, r->in.type,
8168                                     blob.data, blob.length);
8169         if (W_ERROR_IS_OK(result)) {
8170                 result = mod_a_printer(printer, 2);
8171         }
8172
8173 done:
8174         free_a_printer(&printer, 2);
8175
8176         return result;
8177 }
8178
8179 /****************************************************************
8180  _spoolss_ResetPrinter
8181 ****************************************************************/
8182
8183 WERROR _spoolss_ResetPrinter(pipes_struct *p,
8184                              struct spoolss_ResetPrinter *r)
8185 {
8186         Printer_entry   *Printer = find_printer_index_by_hnd(p, r->in.handle);
8187         int             snum;
8188
8189         DEBUG(5,("_spoolss_ResetPrinter\n"));
8190
8191         /*
8192          * All we do is to check to see if the handle and queue is valid.
8193          * This call really doesn't mean anything to us because we only
8194          * support RAW printing.   --jerry
8195          */
8196
8197         if (!Printer) {
8198                 DEBUG(2,("_spoolss_ResetPrinter: Invalid handle (%s:%u:%u).\n",
8199                         OUR_HANDLE(r->in.handle)));
8200                 return WERR_BADFID;
8201         }
8202
8203         if (!get_printer_snum(p, r->in.handle, &snum, NULL))
8204                 return WERR_BADFID;
8205
8206
8207         /* blindly return success */
8208         return WERR_OK;
8209 }
8210
8211 /****************************************************************
8212  _spoolss_DeletePrinterData
8213 ****************************************************************/
8214
8215 WERROR _spoolss_DeletePrinterData(pipes_struct *p,
8216                                   struct spoolss_DeletePrinterData *r)
8217 {
8218         NT_PRINTER_INFO_LEVEL   *printer = NULL;
8219         int             snum=0;
8220         WERROR          status = WERR_OK;
8221         Printer_entry   *Printer = find_printer_index_by_hnd(p, r->in.handle);
8222
8223         DEBUG(5,("_spoolss_DeletePrinterData\n"));
8224
8225         if (!Printer) {
8226                 DEBUG(2,("_spoolss_DeletePrinterData: Invalid handle (%s:%u:%u).\n",
8227                         OUR_HANDLE(r->in.handle)));
8228                 return WERR_BADFID;
8229         }
8230
8231         if (!get_printer_snum(p, r->in.handle, &snum, NULL))
8232                 return WERR_BADFID;
8233
8234         if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
8235                 DEBUG(3, ("_spoolss_DeletePrinterData: "
8236                         "printer properties change denied by handle\n"));
8237                 return WERR_ACCESS_DENIED;
8238         }
8239
8240         status = get_a_printer(Printer, &printer, 2, lp_const_servicename(snum));
8241         if (!W_ERROR_IS_OK(status))
8242                 return status;
8243
8244         if (!r->in.value_name) {
8245                 free_a_printer(&printer, 2);
8246                 return WERR_NOMEM;
8247         }
8248
8249         status = delete_printer_dataex( printer, SPOOL_PRINTERDATA_KEY,
8250                                         r->in.value_name );
8251
8252         if ( W_ERROR_IS_OK(status) )
8253                 mod_a_printer( printer, 2 );
8254
8255         free_a_printer(&printer, 2);
8256
8257         return status;
8258 }
8259
8260 /****************************************************************
8261  _spoolss_AddForm
8262 ****************************************************************/
8263
8264 WERROR _spoolss_AddForm(pipes_struct *p,
8265                         struct spoolss_AddForm *r)
8266 {
8267         struct spoolss_AddFormInfo1 *form = r->in.info.info1;
8268         nt_forms_struct tmpForm;
8269         int snum = -1;
8270         WERROR status = WERR_OK;
8271         NT_PRINTER_INFO_LEVEL *printer = NULL;
8272         SE_PRIV se_printop = SE_PRINT_OPERATOR;
8273
8274         int count=0;
8275         nt_forms_struct *list=NULL;
8276         Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
8277
8278         DEBUG(5,("_spoolss_AddForm\n"));
8279
8280         if (!Printer) {
8281                 DEBUG(2,("_spoolss_AddForm: Invalid handle (%s:%u:%u).\n",
8282                         OUR_HANDLE(r->in.handle)));
8283                 return WERR_BADFID;
8284         }
8285
8286
8287         /* forms can be added on printer of on the print server handle */
8288
8289         if ( Printer->printer_type == SPLHND_PRINTER )
8290         {
8291                 if (!get_printer_snum(p, r->in.handle, &snum, NULL))
8292                         return WERR_BADFID;
8293
8294                 status = get_a_printer(Printer, &printer, 2, lp_const_servicename(snum));
8295                 if (!W_ERROR_IS_OK(status))
8296                         goto done;
8297         }
8298
8299         /* if the user is not root, doesn't have SE_PRINT_OPERATOR privilege,
8300            and not a printer admin, then fail */
8301
8302         if ((p->server_info->utok.uid != sec_initial_uid()) &&
8303              !user_has_privileges(p->server_info->ptok, &se_printop) &&
8304              !token_contains_name_in_list(uidtoname(p->server_info->utok.uid),
8305                                           NULL, NULL,
8306                                           p->server_info->ptok,
8307                                           lp_printer_admin(snum))) {
8308                 DEBUG(2,("_spoolss_Addform: denied by insufficient permissions.\n"));
8309                 return WERR_ACCESS_DENIED;
8310         }
8311
8312         /* can't add if builtin */
8313
8314         if (get_a_builtin_ntform_by_string(form->form_name, &tmpForm)) {
8315                 status = WERR_FILE_EXISTS;
8316                 goto done;
8317         }
8318
8319         count = get_ntforms(&list);
8320
8321         if(!add_a_form(&list, form, &count)) {
8322                 status =  WERR_NOMEM;
8323                 goto done;
8324         }
8325
8326         become_root();
8327         write_ntforms(&list, count);
8328         unbecome_root();
8329
8330         /*
8331          * ChangeID must always be set if this is a printer
8332          */
8333
8334         if ( Printer->printer_type == SPLHND_PRINTER )
8335                 status = mod_a_printer(printer, 2);
8336
8337 done:
8338         if ( printer )
8339                 free_a_printer(&printer, 2);
8340         SAFE_FREE(list);
8341
8342         return status;
8343 }
8344
8345 /****************************************************************
8346  _spoolss_DeleteForm
8347 ****************************************************************/
8348
8349 WERROR _spoolss_DeleteForm(pipes_struct *p,
8350                            struct spoolss_DeleteForm *r)
8351 {
8352         const char *form_name = r->in.form_name;
8353         nt_forms_struct tmpForm;
8354         int count=0;
8355         nt_forms_struct *list=NULL;
8356         Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
8357         int snum = -1;
8358         WERROR status = WERR_OK;
8359         NT_PRINTER_INFO_LEVEL *printer = NULL;
8360         SE_PRIV se_printop = SE_PRINT_OPERATOR;
8361         bool ret = false;
8362
8363         DEBUG(5,("_spoolss_DeleteForm\n"));
8364
8365         if (!Printer) {
8366                 DEBUG(2,("_spoolss_DeleteForm: Invalid handle (%s:%u:%u).\n",
8367                         OUR_HANDLE(r->in.handle)));
8368                 return WERR_BADFID;
8369         }
8370
8371         /* forms can be deleted on printer of on the print server handle */
8372
8373         if ( Printer->printer_type == SPLHND_PRINTER )
8374         {
8375                 if (!get_printer_snum(p, r->in.handle, &snum, NULL))
8376                         return WERR_BADFID;
8377
8378                 status = get_a_printer(Printer, &printer, 2, lp_const_servicename(snum));
8379                 if (!W_ERROR_IS_OK(status))
8380                         goto done;
8381         }
8382
8383         if ((p->server_info->utok.uid != sec_initial_uid()) &&
8384              !user_has_privileges(p->server_info->ptok, &se_printop) &&
8385              !token_contains_name_in_list(uidtoname(p->server_info->utok.uid),
8386                                           NULL, NULL,
8387                                           p->server_info->ptok,
8388                                           lp_printer_admin(snum))) {
8389                 DEBUG(2,("_spoolss_DeleteForm: denied by insufficient permissions.\n"));
8390                 return WERR_ACCESS_DENIED;
8391         }
8392
8393
8394         /* can't delete if builtin */
8395
8396         if (get_a_builtin_ntform_by_string(form_name,&tmpForm)) {
8397                 status = WERR_INVALID_PARAM;
8398                 goto done;
8399         }
8400
8401         count = get_ntforms(&list);
8402
8403         become_root();
8404         ret = delete_a_form(&list, form_name, &count, &status);
8405         unbecome_root();
8406         if (ret == false) {
8407                 goto done;
8408         }
8409
8410         /*
8411          * ChangeID must always be set if this is a printer
8412          */
8413
8414         if ( Printer->printer_type == SPLHND_PRINTER )
8415                 status = mod_a_printer(printer, 2);
8416
8417 done:
8418         if ( printer )
8419                 free_a_printer(&printer, 2);
8420         SAFE_FREE(list);
8421
8422         return status;
8423 }
8424
8425 /****************************************************************
8426  _spoolss_SetForm
8427 ****************************************************************/
8428
8429 WERROR _spoolss_SetForm(pipes_struct *p,
8430                         struct spoolss_SetForm *r)
8431 {
8432         struct spoolss_AddFormInfo1 *form = r->in.info.info1;
8433         nt_forms_struct tmpForm;
8434         int snum = -1;
8435         WERROR status = WERR_OK;
8436         NT_PRINTER_INFO_LEVEL *printer = NULL;
8437         SE_PRIV se_printop = SE_PRINT_OPERATOR;
8438
8439         int count=0;
8440         nt_forms_struct *list=NULL;
8441         Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
8442
8443         DEBUG(5,("_spoolss_SetForm\n"));
8444
8445         if (!Printer) {
8446                 DEBUG(2,("_spoolss_SetForm: Invalid handle (%s:%u:%u).\n",
8447                         OUR_HANDLE(r->in.handle)));
8448                 return WERR_BADFID;
8449         }
8450
8451         /* forms can be modified on printer of on the print server handle */
8452
8453         if ( Printer->printer_type == SPLHND_PRINTER )
8454         {
8455                 if (!get_printer_snum(p, r->in.handle, &snum, NULL))
8456                         return WERR_BADFID;
8457
8458                 status = get_a_printer(Printer, &printer, 2, lp_const_servicename(snum));
8459                 if (!W_ERROR_IS_OK(status))
8460                         goto done;
8461         }
8462
8463         /* if the user is not root, doesn't have SE_PRINT_OPERATOR privilege,
8464            and not a printer admin, then fail */
8465
8466         if ((p->server_info->utok.uid != sec_initial_uid()) &&
8467              !user_has_privileges(p->server_info->ptok, &se_printop) &&
8468              !token_contains_name_in_list(uidtoname(p->server_info->utok.uid),
8469                                           NULL, NULL,
8470                                           p->server_info->ptok,
8471                                           lp_printer_admin(snum))) {
8472                 DEBUG(2,("_spoolss_Setform: denied by insufficient permissions.\n"));
8473                 return WERR_ACCESS_DENIED;
8474         }
8475
8476         /* can't set if builtin */
8477         if (get_a_builtin_ntform_by_string(form->form_name, &tmpForm)) {
8478                 status = WERR_INVALID_PARAM;
8479                 goto done;
8480         }
8481
8482         count = get_ntforms(&list);
8483         update_a_form(&list, form, count);
8484         become_root();
8485         write_ntforms(&list, count);
8486         unbecome_root();
8487
8488         /*
8489          * ChangeID must always be set if this is a printer
8490          */
8491
8492         if ( Printer->printer_type == SPLHND_PRINTER )
8493                 status = mod_a_printer(printer, 2);
8494
8495
8496 done:
8497         if ( printer )
8498                 free_a_printer(&printer, 2);
8499         SAFE_FREE(list);
8500
8501         return status;
8502 }
8503
8504 /****************************************************************************
8505  fill_print_processor1
8506 ****************************************************************************/
8507
8508 static WERROR fill_print_processor1(TALLOC_CTX *mem_ctx,
8509                                     struct spoolss_PrintProcessorInfo1 *r,
8510                                     const char *print_processor_name)
8511 {
8512         r->print_processor_name = talloc_strdup(mem_ctx, print_processor_name);
8513         W_ERROR_HAVE_NO_MEMORY(r->print_processor_name);
8514
8515         return WERR_OK;
8516 }
8517
8518 /****************************************************************************
8519  enumprintprocessors level 1.
8520 ****************************************************************************/
8521
8522 static WERROR enumprintprocessors_level_1(TALLOC_CTX *mem_ctx,
8523                                           union spoolss_PrintProcessorInfo **info_p,
8524                                           uint32_t *count)
8525 {
8526         union spoolss_PrintProcessorInfo *info;
8527         WERROR result;
8528
8529         info = TALLOC_ARRAY(mem_ctx, union spoolss_PrintProcessorInfo, 1);
8530         W_ERROR_HAVE_NO_MEMORY(info);
8531
8532         *count = 1;
8533
8534         result = fill_print_processor1(info, &info[0].info1, "winprint");
8535         if (!W_ERROR_IS_OK(result)) {
8536                 goto out;
8537         }
8538
8539  out:
8540         if (!W_ERROR_IS_OK(result)) {
8541                 TALLOC_FREE(info);
8542                 *count = 0;
8543                 return result;
8544         }
8545
8546         *info_p = info;
8547
8548         return WERR_OK;
8549 }
8550
8551 /****************************************************************
8552  _spoolss_EnumPrintProcessors
8553 ****************************************************************/
8554
8555 WERROR _spoolss_EnumPrintProcessors(pipes_struct *p,
8556                                     struct spoolss_EnumPrintProcessors *r)
8557 {
8558         WERROR result;
8559
8560         /* that's an [in out] buffer */
8561
8562         if (!r->in.buffer && (r->in.offered != 0)) {
8563                 return WERR_INVALID_PARAM;
8564         }
8565
8566         DEBUG(5,("_spoolss_EnumPrintProcessors\n"));
8567
8568         /*
8569          * Enumerate the print processors ...
8570          *
8571          * Just reply with "winprint", to keep NT happy
8572          * and I can use my nice printer checker.
8573          */
8574
8575         *r->out.count = 0;
8576         *r->out.needed = 0;
8577         *r->out.info = NULL;
8578
8579         switch (r->in.level) {
8580         case 1:
8581                 result = enumprintprocessors_level_1(p->mem_ctx, r->out.info,
8582                                                      r->out.count);
8583                 break;
8584         default:
8585                 return WERR_UNKNOWN_LEVEL;
8586         }
8587
8588         if (!W_ERROR_IS_OK(result)) {
8589                 return result;
8590         }
8591
8592         *r->out.needed  = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
8593                                                      spoolss_EnumPrintProcessors, NULL,
8594                                                      *r->out.info, r->in.level,
8595                                                      *r->out.count);
8596         *r->out.info    = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
8597         *r->out.count   = SPOOLSS_BUFFER_OK(*r->out.count, 0);
8598
8599         return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
8600 }
8601
8602 /****************************************************************************
8603  fill_printprocdatatype1
8604 ****************************************************************************/
8605
8606 static WERROR fill_printprocdatatype1(TALLOC_CTX *mem_ctx,
8607                                       struct spoolss_PrintProcDataTypesInfo1 *r,
8608                                       const char *name_array)
8609 {
8610         r->name_array = talloc_strdup(mem_ctx, name_array);
8611         W_ERROR_HAVE_NO_MEMORY(r->name_array);
8612
8613         return WERR_OK;
8614 }
8615
8616 /****************************************************************************
8617  enumprintprocdatatypes level 1.
8618 ****************************************************************************/
8619
8620 static WERROR enumprintprocdatatypes_level_1(TALLOC_CTX *mem_ctx,
8621                                              union spoolss_PrintProcDataTypesInfo **info_p,
8622                                              uint32_t *count)
8623 {
8624         WERROR result;
8625         union spoolss_PrintProcDataTypesInfo *info;
8626
8627         info = TALLOC_ARRAY(mem_ctx, union spoolss_PrintProcDataTypesInfo, 1);
8628         W_ERROR_HAVE_NO_MEMORY(info);
8629
8630         *count = 1;
8631
8632         result = fill_printprocdatatype1(info, &info[0].info1, "RAW");
8633         if (!W_ERROR_IS_OK(result)) {
8634                 goto out;
8635         }
8636
8637  out:
8638         if (!W_ERROR_IS_OK(result)) {
8639                 TALLOC_FREE(info);
8640                 *count = 0;
8641                 return result;
8642         }
8643
8644         *info_p = info;
8645
8646         return WERR_OK;
8647 }
8648
8649 /****************************************************************
8650  _spoolss_EnumPrintProcDataTypes
8651 ****************************************************************/
8652
8653 WERROR _spoolss_EnumPrintProcDataTypes(pipes_struct *p,
8654                                        struct spoolss_EnumPrintProcDataTypes *r)
8655 {
8656         WERROR result;
8657
8658         /* that's an [in out] buffer */
8659
8660         if (!r->in.buffer && (r->in.offered != 0)) {
8661                 return WERR_INVALID_PARAM;
8662         }
8663
8664         DEBUG(5,("_spoolss_EnumPrintProcDataTypes\n"));
8665
8666         *r->out.count = 0;
8667         *r->out.needed = 0;
8668         *r->out.info = NULL;
8669
8670         switch (r->in.level) {
8671         case 1:
8672                 result = enumprintprocdatatypes_level_1(p->mem_ctx, r->out.info,
8673                                                         r->out.count);
8674                 break;
8675         default:
8676                 return WERR_UNKNOWN_LEVEL;
8677         }
8678
8679         *r->out.needed  = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
8680                                                      spoolss_EnumPrintProcDataTypes, NULL,
8681                                                      *r->out.info, r->in.level,
8682                                                      *r->out.count);
8683         *r->out.info    = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
8684         *r->out.count   = SPOOLSS_BUFFER_OK(*r->out.count, 0);
8685
8686         return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
8687 }
8688
8689 /****************************************************************************
8690  fill_monitor_1
8691 ****************************************************************************/
8692
8693 static WERROR fill_monitor_1(TALLOC_CTX *mem_ctx,
8694                              struct spoolss_MonitorInfo1 *r,
8695                              const char *monitor_name)
8696 {
8697         r->monitor_name                 = talloc_strdup(mem_ctx, monitor_name);
8698         W_ERROR_HAVE_NO_MEMORY(r->monitor_name);
8699
8700         return WERR_OK;
8701 }
8702
8703 /****************************************************************************
8704  fill_monitor_2
8705 ****************************************************************************/
8706
8707 static WERROR fill_monitor_2(TALLOC_CTX *mem_ctx,
8708                              struct spoolss_MonitorInfo2 *r,
8709                              const char *monitor_name,
8710                              const char *environment,
8711                              const char *dll_name)
8712 {
8713         r->monitor_name                 = talloc_strdup(mem_ctx, monitor_name);
8714         W_ERROR_HAVE_NO_MEMORY(r->monitor_name);
8715         r->environment                  = talloc_strdup(mem_ctx, environment);
8716         W_ERROR_HAVE_NO_MEMORY(r->environment);
8717         r->dll_name                     = talloc_strdup(mem_ctx, dll_name);
8718         W_ERROR_HAVE_NO_MEMORY(r->dll_name);
8719
8720         return WERR_OK;
8721 }
8722
8723 /****************************************************************************
8724  enumprintmonitors level 1.
8725 ****************************************************************************/
8726
8727 static WERROR enumprintmonitors_level_1(TALLOC_CTX *mem_ctx,
8728                                         union spoolss_MonitorInfo **info_p,
8729                                         uint32_t *count)
8730 {
8731         union spoolss_MonitorInfo *info;
8732         WERROR result = WERR_OK;
8733
8734         info = TALLOC_ARRAY(mem_ctx, union spoolss_MonitorInfo, 2);
8735         W_ERROR_HAVE_NO_MEMORY(info);
8736
8737         *count = 2;
8738
8739         result = fill_monitor_1(info, &info[0].info1,
8740                                 SPL_LOCAL_PORT);
8741         if (!W_ERROR_IS_OK(result)) {
8742                 goto out;
8743         }
8744
8745         result = fill_monitor_1(info, &info[1].info1,
8746                                 SPL_TCPIP_PORT);
8747         if (!W_ERROR_IS_OK(result)) {
8748                 goto out;
8749         }
8750
8751 out:
8752         if (!W_ERROR_IS_OK(result)) {
8753                 TALLOC_FREE(info);
8754                 *count = 0;
8755                 return result;
8756         }
8757
8758         *info_p = info;
8759
8760         return WERR_OK;
8761 }
8762
8763 /****************************************************************************
8764  enumprintmonitors level 2.
8765 ****************************************************************************/
8766
8767 static WERROR enumprintmonitors_level_2(TALLOC_CTX *mem_ctx,
8768                                         union spoolss_MonitorInfo **info_p,
8769                                         uint32_t *count)
8770 {
8771         union spoolss_MonitorInfo *info;
8772         WERROR result = WERR_OK;
8773
8774         info = TALLOC_ARRAY(mem_ctx, union spoolss_MonitorInfo, 2);
8775         W_ERROR_HAVE_NO_MEMORY(info);
8776
8777         *count = 2;
8778
8779         result = fill_monitor_2(info, &info[0].info2,
8780                                 SPL_LOCAL_PORT,
8781                                 "Windows NT X86", /* FIXME */
8782                                 "localmon.dll");
8783         if (!W_ERROR_IS_OK(result)) {
8784                 goto out;
8785         }
8786
8787         result = fill_monitor_2(info, &info[1].info2,
8788                                 SPL_TCPIP_PORT,
8789                                 "Windows NT X86", /* FIXME */
8790                                 "tcpmon.dll");
8791         if (!W_ERROR_IS_OK(result)) {
8792                 goto out;
8793         }
8794
8795 out:
8796         if (!W_ERROR_IS_OK(result)) {
8797                 TALLOC_FREE(info);
8798                 *count = 0;
8799                 return result;
8800         }
8801
8802         *info_p = info;
8803
8804         return WERR_OK;
8805 }
8806
8807 /****************************************************************
8808  _spoolss_EnumMonitors
8809 ****************************************************************/
8810
8811 WERROR _spoolss_EnumMonitors(pipes_struct *p,
8812                              struct spoolss_EnumMonitors *r)
8813 {
8814         WERROR result;
8815
8816         /* that's an [in out] buffer */
8817
8818         if (!r->in.buffer && (r->in.offered != 0)) {
8819                 return WERR_INVALID_PARAM;
8820         }
8821
8822         DEBUG(5,("_spoolss_EnumMonitors\n"));
8823
8824         /*
8825          * Enumerate the print monitors ...
8826          *
8827          * Just reply with "Local Port", to keep NT happy
8828          * and I can use my nice printer checker.
8829          */
8830
8831         *r->out.count = 0;
8832         *r->out.needed = 0;
8833         *r->out.info = NULL;
8834
8835         switch (r->in.level) {
8836         case 1:
8837                 result = enumprintmonitors_level_1(p->mem_ctx, r->out.info,
8838                                                    r->out.count);
8839                 break;
8840         case 2:
8841                 result = enumprintmonitors_level_2(p->mem_ctx, r->out.info,
8842                                                    r->out.count);
8843                 break;
8844         default:
8845                 return WERR_UNKNOWN_LEVEL;
8846         }
8847
8848         if (!W_ERROR_IS_OK(result)) {
8849                 return result;
8850         }
8851
8852         *r->out.needed  = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
8853                                                      spoolss_EnumMonitors, NULL,
8854                                                      *r->out.info, r->in.level,
8855                                                      *r->out.count);
8856         *r->out.info    = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
8857         *r->out.count   = SPOOLSS_BUFFER_OK(*r->out.count, 0);
8858
8859         return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
8860 }
8861
8862 /****************************************************************************
8863 ****************************************************************************/
8864
8865 static WERROR getjob_level_1(TALLOC_CTX *mem_ctx,
8866                              const print_queue_struct *queue,
8867                              int count, int snum,
8868                              const NT_PRINTER_INFO_LEVEL *ntprinter,
8869                              uint32_t jobid,
8870                              struct spoolss_JobInfo1 *r)
8871 {
8872         int i = 0;
8873         bool found = false;
8874
8875         for (i=0; i<count && found == false; i++) {
8876                 if (queue[i].job == (int)jobid) {
8877                         found = true;
8878                 }
8879         }
8880
8881         if (found == false) {
8882                 /* NT treats not found as bad param... yet another bad choice */
8883                 return WERR_INVALID_PARAM;
8884         }
8885
8886         return fill_job_info1(mem_ctx,
8887                               r,
8888                               &queue[i-1],
8889                               i,
8890                               snum,
8891                               ntprinter);
8892 }
8893
8894 /****************************************************************************
8895 ****************************************************************************/
8896
8897 static WERROR getjob_level_2(TALLOC_CTX *mem_ctx,
8898                              const print_queue_struct *queue,
8899                              int count, int snum,
8900                              const NT_PRINTER_INFO_LEVEL *ntprinter,
8901                              uint32_t jobid,
8902                              struct spoolss_JobInfo2 *r)
8903 {
8904         int i = 0;
8905         bool found = false;
8906         struct spoolss_DeviceMode *devmode;
8907         NT_DEVICEMODE *nt_devmode;
8908         WERROR result;
8909
8910         for (i=0; i<count && found == false; i++) {
8911                 if (queue[i].job == (int)jobid) {
8912                         found = true;
8913                 }
8914         }
8915
8916         if (found == false) {
8917                 /* NT treats not found as bad param... yet another bad
8918                    choice */
8919                 return WERR_INVALID_PARAM;
8920         }
8921
8922         /*
8923          * if the print job does not have a DEVMODE associated with it,
8924          * just use the one for the printer. A NULL devicemode is not
8925          *  a failure condition
8926          */
8927
8928         nt_devmode = print_job_devmode(lp_const_servicename(snum), jobid);
8929         if (nt_devmode) {
8930                 devmode = TALLOC_ZERO_P(mem_ctx, struct spoolss_DeviceMode);
8931                 W_ERROR_HAVE_NO_MEMORY(devmode);
8932                 result = convert_nt_devicemode(devmode, devmode, nt_devmode);
8933                 if (!W_ERROR_IS_OK(result)) {
8934                         return result;
8935                 }
8936         } else {
8937                 devmode = construct_dev_mode(mem_ctx, lp_const_servicename(snum));
8938                 W_ERROR_HAVE_NO_MEMORY(devmode);
8939         }
8940
8941         return fill_job_info2(mem_ctx,
8942                               r,
8943                               &queue[i-1],
8944                               i,
8945                               snum,
8946                               ntprinter,
8947                               devmode);
8948 }
8949
8950 /****************************************************************
8951  _spoolss_GetJob
8952 ****************************************************************/
8953
8954 WERROR _spoolss_GetJob(pipes_struct *p,
8955                        struct spoolss_GetJob *r)
8956 {
8957         WERROR result = WERR_OK;
8958         NT_PRINTER_INFO_LEVEL *ntprinter = NULL;
8959         int snum;
8960         int count;
8961         print_queue_struct      *queue = NULL;
8962         print_status_struct prt_status;
8963
8964         /* that's an [in out] buffer */
8965
8966         if (!r->in.buffer && (r->in.offered != 0)) {
8967                 return WERR_INVALID_PARAM;
8968         }
8969
8970         DEBUG(5,("_spoolss_GetJob\n"));
8971
8972         *r->out.needed = 0;
8973
8974         if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
8975                 return WERR_BADFID;
8976         }
8977
8978         result = get_a_printer(NULL, &ntprinter, 2, lp_servicename(snum));
8979         if (!W_ERROR_IS_OK(result)) {
8980                 return result;
8981         }
8982
8983         count = print_queue_status(snum, &queue, &prt_status);
8984
8985         DEBUGADD(4,("count:[%d], prt_status:[%d], [%s]\n",
8986                      count, prt_status.status, prt_status.message));
8987
8988         switch (r->in.level) {
8989         case 1:
8990                 result = getjob_level_1(p->mem_ctx,
8991                                         queue, count, snum, ntprinter,
8992                                         r->in.job_id, &r->out.info->info1);
8993                 break;
8994         case 2:
8995                 result = getjob_level_2(p->mem_ctx,
8996                                         queue, count, snum, ntprinter,
8997                                         r->in.job_id, &r->out.info->info2);
8998                 break;
8999         default:
9000                 result = WERR_UNKNOWN_LEVEL;
9001                 break;
9002         }
9003
9004         SAFE_FREE(queue);
9005         free_a_printer(&ntprinter, 2);
9006
9007         if (!W_ERROR_IS_OK(result)) {
9008                 TALLOC_FREE(r->out.info);
9009                 return result;
9010         }
9011
9012         *r->out.needed  = SPOOLSS_BUFFER_UNION(spoolss_JobInfo, NULL,
9013                                                r->out.info, r->in.level);
9014         r->out.info     = SPOOLSS_BUFFER_OK(r->out.info, NULL);
9015
9016         return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
9017 }
9018
9019 /****************************************************************
9020  _spoolss_GetPrinterDataEx
9021
9022  From MSDN documentation of GetPrinterDataEx: pass request
9023  to GetPrinterData if key is "PrinterDriverData".
9024 ****************************************************************/
9025
9026 WERROR _spoolss_GetPrinterDataEx(pipes_struct *p,
9027                                  struct spoolss_GetPrinterDataEx *r)
9028 {
9029
9030         Printer_entry   *Printer = find_printer_index_by_hnd(p, r->in.handle);
9031         REGISTRY_VALUE          *val = NULL;
9032         NT_PRINTER_INFO_LEVEL   *printer = NULL;
9033         int                     snum = 0;
9034         WERROR result = WERR_OK;
9035
9036         DEBUG(4,("_spoolss_GetPrinterDataEx\n"));
9037
9038         DEBUG(10, ("_spoolss_GetPrinterDataEx: key => [%s], value => [%s]\n",
9039                 r->in.key_name, r->in.value_name));
9040
9041         /* in case of problem, return some default values */
9042
9043         *r->out.needed  = 0;
9044         *r->out.type    = REG_NONE;
9045
9046         if (!Printer) {
9047                 DEBUG(2,("_spoolss_GetPrinterDataEx: Invalid handle (%s:%u:%u).\n",
9048                         OUR_HANDLE(r->in.handle)));
9049                 result = WERR_BADFID;
9050                 goto done;
9051         }
9052
9053         /* Is the handle to a printer or to the server? */
9054
9055         if (Printer->printer_type == SPLHND_SERVER) {
9056                 DEBUG(10,("_spoolss_GetPrinterDataEx: "
9057                         "Not implemented for server handles yet\n"));
9058                 result = WERR_INVALID_PARAM;
9059                 goto done;
9060         }
9061
9062         if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
9063                 return WERR_BADFID;
9064         }
9065
9066         result = get_a_printer(Printer, &printer, 2, lp_servicename(snum));
9067         if (!W_ERROR_IS_OK(result)) {
9068                 goto done;
9069         }
9070
9071         /* check to see if the keyname is valid */
9072         if (!strlen(r->in.key_name)) {
9073                 result = WERR_INVALID_PARAM;
9074                 goto done;
9075         }
9076
9077         if (lookup_printerkey(printer->info_2->data, r->in.key_name) == -1) {
9078                 DEBUG(4,("_spoolss_GetPrinterDataEx: "
9079                         "Invalid keyname [%s]\n", r->in.key_name ));
9080                 result = WERR_BADFILE;
9081                 goto done;
9082         }
9083
9084         /* When given a new keyname, we should just create it */
9085
9086         val = get_printer_data(printer->info_2,
9087                                r->in.key_name, r->in.value_name);
9088         if (!val) {
9089                 result = WERR_BADFILE;
9090                 goto done;
9091         }
9092
9093         *r->out.needed = regval_size(val);
9094
9095         if (*r->out.needed > r->in.offered) {
9096                 result = WERR_MORE_DATA;
9097                 goto done;
9098         }
9099
9100         *r->out.type = regval_type(val);
9101
9102         memcpy(r->out.buffer, regval_data_p(val), regval_size(val));
9103
9104  done:
9105         if (printer) {
9106                 free_a_printer(&printer, 2);
9107         }
9108
9109         return result;
9110 }
9111
9112 /****************************************************************
9113  _spoolss_SetPrinterDataEx
9114 ****************************************************************/
9115
9116 WERROR _spoolss_SetPrinterDataEx(pipes_struct *p,
9117                                  struct spoolss_SetPrinterDataEx *r)
9118 {
9119         NT_PRINTER_INFO_LEVEL   *printer = NULL;
9120         int                     snum = 0;
9121         WERROR                  result = WERR_OK;
9122         Printer_entry           *Printer = find_printer_index_by_hnd(p, r->in.handle);
9123         char                    *oid_string;
9124
9125         DEBUG(4,("_spoolss_SetPrinterDataEx\n"));
9126
9127         /* From MSDN documentation of SetPrinterDataEx: pass request to
9128            SetPrinterData if key is "PrinterDriverData" */
9129
9130         if (!Printer) {
9131                 DEBUG(2,("_spoolss_SetPrinterDataEx: Invalid handle (%s:%u:%u).\n",
9132                         OUR_HANDLE(r->in.handle)));
9133                 return WERR_BADFID;
9134         }
9135
9136         if (Printer->printer_type == SPLHND_SERVER) {
9137                 DEBUG(10,("_spoolss_SetPrinterDataEx: "
9138                         "Not implemented for server handles yet\n"));
9139                 return WERR_INVALID_PARAM;
9140         }
9141
9142         if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
9143                 return WERR_BADFID;
9144         }
9145
9146         /*
9147          * Access check : NT returns "access denied" if you make a
9148          * SetPrinterData call without the necessary privildge.
9149          * we were originally returning OK if nothing changed
9150          * which made Win2k issue **a lot** of SetPrinterData
9151          * when connecting to a printer  --jerry
9152          */
9153
9154         if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
9155                 DEBUG(3, ("_spoolss_SetPrinterDataEx: "
9156                         "change denied by handle access permissions\n"));
9157                 return WERR_ACCESS_DENIED;
9158         }
9159
9160         result = get_a_printer(Printer, &printer, 2, lp_servicename(snum));
9161         if (!W_ERROR_IS_OK(result)) {
9162                 return result;
9163         }
9164
9165         /* check for OID in valuename */
9166
9167         oid_string = strchr(r->in.value_name, ',');
9168         if (oid_string) {
9169                 *oid_string = '\0';
9170                 oid_string++;
9171         }
9172
9173         /* save the registry data */
9174
9175         result = set_printer_dataex(printer, r->in.key_name, r->in.value_name,
9176                                     r->in.type, r->in.buffer, r->in.offered);
9177
9178         if (W_ERROR_IS_OK(result)) {
9179                 /* save the OID if one was specified */
9180                 if (oid_string) {
9181                         char *str = talloc_asprintf(p->mem_ctx, "%s\\%s",
9182                                 r->in.key_name, SPOOL_OID_KEY);
9183                         if (!str) {
9184                                 result = WERR_NOMEM;
9185                                 goto done;
9186                         }
9187
9188                         /*
9189                          * I'm not checking the status here on purpose.  Don't know
9190                          * if this is right, but I'm returning the status from the
9191                          * previous set_printer_dataex() call.  I have no idea if
9192                          * this is right.    --jerry
9193                          */
9194
9195                         set_printer_dataex(printer, str, r->in.value_name,
9196                                            REG_SZ, (uint8_t *)oid_string,
9197                                            strlen(oid_string)+1);
9198                 }
9199
9200                 result = mod_a_printer(printer, 2);
9201         }
9202
9203  done:
9204         free_a_printer(&printer, 2);
9205
9206         return result;
9207 }
9208
9209 /****************************************************************
9210  _spoolss_DeletePrinterDataEx
9211 ****************************************************************/
9212
9213 WERROR _spoolss_DeletePrinterDataEx(pipes_struct *p,
9214                                     struct spoolss_DeletePrinterDataEx *r)
9215 {
9216         NT_PRINTER_INFO_LEVEL   *printer = NULL;
9217         int             snum=0;
9218         WERROR          status = WERR_OK;
9219         Printer_entry   *Printer = find_printer_index_by_hnd(p, r->in.handle);
9220
9221         DEBUG(5,("_spoolss_DeletePrinterDataEx\n"));
9222
9223         if (!Printer) {
9224                 DEBUG(2,("_spoolss_DeletePrinterDataEx: "
9225                         "Invalid handle (%s:%u:%u).\n",
9226                         OUR_HANDLE(r->in.handle)));
9227                 return WERR_BADFID;
9228         }
9229
9230         if (!get_printer_snum(p, r->in.handle, &snum, NULL))
9231                 return WERR_BADFID;
9232
9233         if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
9234                 DEBUG(3, ("_spoolss_DeletePrinterDataEx: "
9235                         "printer properties change denied by handle\n"));
9236                 return WERR_ACCESS_DENIED;
9237         }
9238
9239         if (!r->in.value_name || !r->in.key_name) {
9240                 return WERR_NOMEM;
9241         }
9242
9243         status = get_a_printer(Printer, &printer, 2, lp_const_servicename(snum));
9244         if (!W_ERROR_IS_OK(status))
9245                 return status;
9246
9247         status = delete_printer_dataex( printer, r->in.key_name, r->in.value_name );
9248
9249         if ( W_ERROR_IS_OK(status) )
9250                 mod_a_printer( printer, 2 );
9251
9252         free_a_printer(&printer, 2);
9253
9254         return status;
9255 }
9256
9257 /****************************************************************
9258  _spoolss_EnumPrinterKey
9259 ****************************************************************/
9260
9261 WERROR _spoolss_EnumPrinterKey(pipes_struct *p,
9262                                struct spoolss_EnumPrinterKey *r)
9263 {
9264         fstring         *keynames = NULL;
9265         int             num_keys;
9266         Printer_entry   *Printer = find_printer_index_by_hnd(p, r->in.handle);
9267         NT_PRINTER_DATA *data;
9268         NT_PRINTER_INFO_LEVEL   *printer = NULL;
9269         int             snum = 0;
9270         WERROR          result = WERR_BADFILE;
9271         int i;
9272         const char **array = NULL;
9273         DATA_BLOB blob;
9274
9275         DEBUG(4,("_spoolss_EnumPrinterKey\n"));
9276
9277         if (!Printer) {
9278                 DEBUG(2,("_spoolss_EnumPrinterKey: Invalid handle (%s:%u:%u).\n",
9279                         OUR_HANDLE(r->in.handle)));
9280                 return WERR_BADFID;
9281         }
9282
9283         if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
9284                 return WERR_BADFID;
9285         }
9286
9287         result = get_a_printer(Printer, &printer, 2, lp_const_servicename(snum));
9288         if (!W_ERROR_IS_OK(result)) {
9289                 return result;
9290         }
9291
9292         /* get the list of subkey names */
9293
9294         data = printer->info_2->data;
9295
9296         num_keys = get_printer_subkeys(data, r->in.key_name, &keynames);
9297         if (num_keys == -1) {
9298                 result = WERR_BADFILE;
9299                 goto done;
9300         }
9301
9302         array = talloc_zero_array(r->out.key_buffer, const char *, num_keys + 2);
9303         if (!array) {
9304                 result = WERR_NOMEM;
9305                 goto done;
9306         }
9307
9308         if (!num_keys) {
9309                 array[0] = talloc_strdup(array, "");
9310                 if (!array[0]) {
9311                         result = WERR_NOMEM;
9312                         goto done;
9313                 }
9314         }
9315
9316         for (i=0; i < num_keys; i++) {
9317
9318                 DEBUG(10,("_spoolss_EnumPrinterKey: adding keyname: %s\n",
9319                         keynames[i]));
9320
9321                 array[i] = talloc_strdup(array, keynames[i]);
9322                 if (!array[i]) {
9323                         result = WERR_NOMEM;
9324                         goto done;
9325                 }
9326         }
9327
9328         if (!push_reg_multi_sz(p->mem_ctx, &blob, array)) {
9329                 result = WERR_NOMEM;
9330                 goto done;
9331         }
9332
9333         *r->out._ndr_size = r->in.offered / 2;
9334         *r->out.needed = blob.length;
9335
9336         if (r->in.offered < *r->out.needed) {
9337                 result = WERR_MORE_DATA;
9338         } else {
9339                 result = WERR_OK;
9340                 r->out.key_buffer->string_array = array;
9341         }
9342
9343  done:
9344         if (!W_ERROR_IS_OK(result)) {
9345                 TALLOC_FREE(array);
9346                 if (!W_ERROR_EQUAL(result, WERR_MORE_DATA)) {
9347                         *r->out.needed = 0;
9348                 }
9349         }
9350
9351         free_a_printer(&printer, 2);
9352         SAFE_FREE(keynames);
9353
9354         return result;
9355 }
9356
9357 /****************************************************************
9358  _spoolss_DeletePrinterKey
9359 ****************************************************************/
9360
9361 WERROR _spoolss_DeletePrinterKey(pipes_struct *p,
9362                                  struct spoolss_DeletePrinterKey *r)
9363 {
9364         Printer_entry           *Printer = find_printer_index_by_hnd(p, r->in.handle);
9365         NT_PRINTER_INFO_LEVEL   *printer = NULL;
9366         int                     snum=0;
9367         WERROR                  status;
9368
9369         DEBUG(5,("_spoolss_DeletePrinterKey\n"));
9370
9371         if (!Printer) {
9372                 DEBUG(2,("_spoolss_DeletePrinterKey: Invalid handle (%s:%u:%u).\n",
9373                         OUR_HANDLE(r->in.handle)));
9374                 return WERR_BADFID;
9375         }
9376
9377         /* if keyname == NULL, return error */
9378
9379         if ( !r->in.key_name )
9380                 return WERR_INVALID_PARAM;
9381
9382         if (!get_printer_snum(p, r->in.handle, &snum, NULL))
9383                 return WERR_BADFID;
9384
9385         if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
9386                 DEBUG(3, ("_spoolss_DeletePrinterKey: "
9387                         "printer properties change denied by handle\n"));
9388                 return WERR_ACCESS_DENIED;
9389         }
9390
9391         status = get_a_printer(Printer, &printer, 2, lp_const_servicename(snum));
9392         if (!W_ERROR_IS_OK(status))
9393                 return status;
9394
9395         /* delete the key and all subneys */
9396
9397         status = delete_all_printer_data( printer->info_2, r->in.key_name );
9398
9399         if ( W_ERROR_IS_OK(status) )
9400                 status = mod_a_printer(printer, 2);
9401
9402         free_a_printer( &printer, 2 );
9403
9404         return status;
9405 }
9406
9407 /****************************************************************
9408 ****************************************************************/
9409
9410 static WERROR registry_value_to_printer_enum_value(TALLOC_CTX *mem_ctx,
9411                                                    REGISTRY_VALUE *v,
9412                                                    struct spoolss_PrinterEnumValues *r)
9413 {
9414         WERROR result;
9415
9416         r->data = TALLOC_ZERO_P(mem_ctx, union spoolss_PrinterData);
9417         W_ERROR_HAVE_NO_MEMORY(r->data);
9418
9419         r->value_name   = talloc_strdup(mem_ctx, regval_name(v));
9420         W_ERROR_HAVE_NO_MEMORY(r->value_name);
9421
9422         r->type         = regval_type(v);
9423         r->data_length  = regval_size(v);
9424
9425         if (r->data_length) {
9426                 DATA_BLOB blob = data_blob_const(regval_data_p(v),
9427                                                  regval_size(v));
9428                 result = pull_spoolss_PrinterData(mem_ctx, &blob,
9429                                                   r->data,
9430                                                   r->type);
9431                 if (!W_ERROR_IS_OK(result)) {
9432                         return result;
9433                 }
9434         }
9435
9436         return WERR_OK;
9437 }
9438
9439 /****************************************************************
9440  _spoolss_EnumPrinterDataEx
9441 ****************************************************************/
9442
9443 WERROR _spoolss_EnumPrinterDataEx(pipes_struct *p,
9444                                   struct spoolss_EnumPrinterDataEx *r)
9445 {
9446         uint32_t        count = 0;
9447         NT_PRINTER_INFO_LEVEL   *printer = NULL;
9448         struct spoolss_PrinterEnumValues *info = NULL;
9449         NT_PRINTER_DATA         *p_data;
9450         Printer_entry   *Printer = find_printer_index_by_hnd(p, r->in.handle);
9451         int             snum;
9452         WERROR          result;
9453         int             key_index;
9454         int             i;
9455
9456         DEBUG(4,("_spoolss_EnumPrinterDataEx\n"));
9457
9458         *r->out.count = 0;
9459         *r->out.needed = 0;
9460         *r->out.info = NULL;
9461
9462         if (!Printer) {
9463                 DEBUG(2,("_spoolss_EnumPrinterDataEx: Invalid handle (%s:%u:%u1<).\n",
9464                         OUR_HANDLE(r->in.handle)));
9465                 return WERR_BADFID;
9466         }
9467
9468         /*
9469          * first check for a keyname of NULL or "".  Win2k seems to send
9470          * this a lot and we should send back WERR_INVALID_PARAM
9471          * no need to spend time looking up the printer in this case.
9472          * --jerry
9473          */
9474
9475         if (!strlen(r->in.key_name)) {
9476                 result = WERR_INVALID_PARAM;
9477                 goto done;
9478         }
9479
9480         /* get the printer off of disk */
9481
9482         if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
9483                 return WERR_BADFID;
9484         }
9485
9486         ZERO_STRUCT(printer);
9487         result = get_a_printer(Printer, &printer, 2, lp_const_servicename(snum));
9488         if (!W_ERROR_IS_OK(result)) {
9489                 return result;
9490         }
9491
9492         /* now look for a match on the key name */
9493
9494         p_data = printer->info_2->data;
9495
9496         key_index = lookup_printerkey(p_data, r->in.key_name);
9497         if (key_index == -1) {
9498                 DEBUG(10,("_spoolss_EnumPrinterDataEx: Unknown keyname [%s]\n",
9499                         r->in.key_name));
9500                 result = WERR_INVALID_PARAM;
9501                 goto done;
9502         }
9503
9504         /* allocate the memory for the array of pointers -- if necessary */
9505
9506         count = regval_ctr_numvals(p_data->keys[key_index].values);
9507         if (!count) {
9508                 result = WERR_OK; /* ??? */
9509                 goto done;
9510         }
9511
9512         info = TALLOC_ZERO_ARRAY(p->mem_ctx,
9513                                  struct spoolss_PrinterEnumValues,
9514                                  count);
9515         if (!info) {
9516                 DEBUG(0,("_spoolss_EnumPrinterDataEx: talloc() failed\n"));
9517                 result = WERR_NOMEM;
9518                 goto done;
9519         }
9520
9521         /*
9522          * loop through all params and build the array to pass
9523          * back to the  client
9524          */
9525
9526         for (i=0; i < count; i++) {
9527
9528                 REGISTRY_VALUE  *val;
9529
9530                 /* lookup the registry value */
9531
9532                 val = regval_ctr_specific_value(p_data->keys[key_index].values, i);
9533
9534                 DEBUG(10,("retrieved value number [%d] [%s]\n", i, regval_name(val)));
9535
9536                 /* copy the data */
9537
9538                 result = registry_value_to_printer_enum_value(info, val, &info[i]);
9539                 if (!W_ERROR_IS_OK(result)) {
9540                         goto done;
9541                 }
9542         }
9543
9544 #if 0 /* FIXME - gd */
9545         /* housekeeping information in the reply */
9546
9547         /* Fix from Martin Zielinski <mz@seh.de> - ensure
9548          * the hand marshalled container size is a multiple
9549          * of 4 bytes for RPC alignment.
9550          */
9551
9552         if (needed % 4) {
9553                 needed += 4-(needed % 4);
9554         }
9555 #endif
9556         *r->out.count   = count;
9557         *r->out.info    = info;
9558
9559  done:
9560
9561         if (printer) {
9562                 free_a_printer(&printer, 2);
9563         }
9564
9565         if (!W_ERROR_IS_OK(result)) {
9566                 return result;
9567         }
9568
9569         *r->out.needed  = SPOOLSS_BUFFER_ARRAY(p->mem_ctx,
9570                                                spoolss_EnumPrinterDataEx, NULL,
9571                                                *r->out.info,
9572                                                *r->out.count);
9573         *r->out.info    = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
9574         *r->out.count   = SPOOLSS_BUFFER_OK(*r->out.count, *r->out.count);
9575
9576         return SPOOLSS_BUFFER_OK(WERR_OK, WERR_MORE_DATA);
9577 }
9578
9579 /****************************************************************************
9580 ****************************************************************************/
9581
9582 static WERROR getprintprocessordirectory_level_1(TALLOC_CTX *mem_ctx,
9583                                                  const char *servername,
9584                                                  const char *environment,
9585                                                  struct spoolss_PrintProcessorDirectoryInfo1 *r)
9586 {
9587         WERROR werr;
9588         char *path = NULL;
9589
9590         werr = compose_spoolss_server_path(mem_ctx,
9591                                            servername,
9592                                            environment,
9593                                            SPOOLSS_PRTPROCS_PATH,
9594                                            &path);
9595         if (!W_ERROR_IS_OK(werr)) {
9596                 return werr;
9597         }
9598
9599         DEBUG(4,("print processor directory: [%s]\n", path));
9600
9601         r->directory_name = path;
9602
9603         return WERR_OK;
9604 }
9605
9606 /****************************************************************
9607  _spoolss_GetPrintProcessorDirectory
9608 ****************************************************************/
9609
9610 WERROR _spoolss_GetPrintProcessorDirectory(pipes_struct *p,
9611                                            struct spoolss_GetPrintProcessorDirectory *r)
9612 {
9613         WERROR result;
9614
9615         /* that's an [in out] buffer */
9616
9617         if (!r->in.buffer && (r->in.offered != 0)) {
9618                 return WERR_INVALID_PARAM;
9619         }
9620
9621         DEBUG(5,("_spoolss_GetPrintProcessorDirectory: level %d\n",
9622                 r->in.level));
9623
9624         *r->out.needed = 0;
9625
9626         /* r->in.level is ignored */
9627
9628         /* We always should reply with a local print processor directory so that
9629          * users are not forced to have a [prnproc$] share on the Samba spoolss
9630          * server - Guenther */
9631
9632         result = getprintprocessordirectory_level_1(p->mem_ctx,
9633                                                     NULL, /* r->in.server */
9634                                                     r->in.environment,
9635                                                     &r->out.info->info1);
9636         if (!W_ERROR_IS_OK(result)) {
9637                 TALLOC_FREE(r->out.info);
9638                 return result;
9639         }
9640
9641         *r->out.needed  = SPOOLSS_BUFFER_UNION(spoolss_PrintProcessorDirectoryInfo, NULL,
9642                                                r->out.info, r->in.level);
9643         r->out.info     = SPOOLSS_BUFFER_OK(r->out.info, NULL);
9644
9645         return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
9646 }
9647
9648 /*******************************************************************
9649  ********************************************************************/
9650
9651 static bool push_monitorui_buf(TALLOC_CTX *mem_ctx, DATA_BLOB *buf,
9652                                const char *dllname)
9653 {
9654         enum ndr_err_code ndr_err;
9655         struct spoolss_MonitorUi ui;
9656
9657         ui.dll_name = dllname;
9658
9659         ndr_err = ndr_push_struct_blob(buf, mem_ctx, NULL, &ui,
9660                        (ndr_push_flags_fn_t)ndr_push_spoolss_MonitorUi);
9661         if (NDR_ERR_CODE_IS_SUCCESS(ndr_err) && (DEBUGLEVEL >= 10)) {
9662                 NDR_PRINT_DEBUG(spoolss_MonitorUi, &ui);
9663         }
9664         return NDR_ERR_CODE_IS_SUCCESS(ndr_err);
9665 }
9666
9667 /*******************************************************************
9668  Streams the monitor UI DLL name in UNICODE
9669 *******************************************************************/
9670
9671 static WERROR xcvtcp_monitorui(TALLOC_CTX *mem_ctx,
9672                                NT_USER_TOKEN *token, DATA_BLOB *in,
9673                                DATA_BLOB *out, uint32_t *needed)
9674 {
9675         const char *dllname = "tcpmonui.dll";
9676
9677         *needed = (strlen(dllname)+1) * 2;
9678
9679         if (out->length < *needed) {
9680                 return WERR_INSUFFICIENT_BUFFER;
9681         }
9682
9683         if (!push_monitorui_buf(mem_ctx, out, dllname)) {
9684                 return WERR_NOMEM;
9685         }
9686
9687         return WERR_OK;
9688 }
9689
9690 /*******************************************************************
9691  ********************************************************************/
9692
9693 static bool pull_port_data_1(TALLOC_CTX *mem_ctx,
9694                              struct spoolss_PortData1 *port1,
9695                              const DATA_BLOB *buf)
9696 {
9697         enum ndr_err_code ndr_err;
9698         ndr_err = ndr_pull_struct_blob(buf, mem_ctx, NULL, port1,
9699                        (ndr_pull_flags_fn_t)ndr_pull_spoolss_PortData1);
9700         if (NDR_ERR_CODE_IS_SUCCESS(ndr_err) && (DEBUGLEVEL >= 10)) {
9701                 NDR_PRINT_DEBUG(spoolss_PortData1, port1);
9702         }
9703         return NDR_ERR_CODE_IS_SUCCESS(ndr_err);
9704 }
9705
9706 /*******************************************************************
9707  ********************************************************************/
9708
9709 static bool pull_port_data_2(TALLOC_CTX *mem_ctx,
9710                              struct spoolss_PortData2 *port2,
9711                              const DATA_BLOB *buf)
9712 {
9713         enum ndr_err_code ndr_err;
9714         ndr_err = ndr_pull_struct_blob(buf, mem_ctx, NULL, port2,
9715                        (ndr_pull_flags_fn_t)ndr_pull_spoolss_PortData2);
9716         if (NDR_ERR_CODE_IS_SUCCESS(ndr_err) && (DEBUGLEVEL >= 10)) {
9717                 NDR_PRINT_DEBUG(spoolss_PortData2, port2);
9718         }
9719         return NDR_ERR_CODE_IS_SUCCESS(ndr_err);
9720 }
9721
9722 /*******************************************************************
9723  Create a new TCP/IP port
9724 *******************************************************************/
9725
9726 static WERROR xcvtcp_addport(TALLOC_CTX *mem_ctx,
9727                              NT_USER_TOKEN *token, DATA_BLOB *in,
9728                              DATA_BLOB *out, uint32_t *needed)
9729 {
9730         struct spoolss_PortData1 port1;
9731         struct spoolss_PortData2 port2;
9732         char *device_uri = NULL;
9733         uint32_t version;
9734
9735         const char *portname;
9736         const char *hostaddress;
9737         const char *queue;
9738         uint32_t port_number;
9739         uint32_t protocol;
9740
9741         /* peek for spoolss_PortData version */
9742
9743         if (!in || (in->length < (128 + 4))) {
9744                 return WERR_GENERAL_FAILURE;
9745         }
9746
9747         version = IVAL(in->data, 128);
9748
9749         switch (version) {
9750                 case 1:
9751                         ZERO_STRUCT(port1);
9752
9753                         if (!pull_port_data_1(mem_ctx, &port1, in)) {
9754                                 return WERR_NOMEM;
9755                         }
9756
9757                         portname        = port1.portname;
9758                         hostaddress     = port1.hostaddress;
9759                         queue           = port1.queue;
9760                         protocol        = port1.protocol;
9761                         port_number     = port1.port_number;
9762
9763                         break;
9764                 case 2:
9765                         ZERO_STRUCT(port2);
9766
9767                         if (!pull_port_data_2(mem_ctx, &port2, in)) {
9768                                 return WERR_NOMEM;
9769                         }
9770
9771                         portname        = port2.portname;
9772                         hostaddress     = port2.hostaddress;
9773                         queue           = port2.queue;
9774                         protocol        = port2.protocol;
9775                         port_number     = port2.port_number;
9776
9777                         break;
9778                 default:
9779                         DEBUG(1,("xcvtcp_addport: "
9780                                 "unknown version of port_data: %d\n", version));
9781                         return WERR_UNKNOWN_PORT;
9782         }
9783
9784         /* create the device URI and call the add_port_hook() */
9785
9786         switch (protocol) {
9787         case PROTOCOL_RAWTCP_TYPE:
9788                 device_uri = talloc_asprintf(mem_ctx,
9789                                 "socket://%s:%d/", hostaddress,
9790                                 port_number);
9791                 break;
9792
9793         case PROTOCOL_LPR_TYPE:
9794                 device_uri = talloc_asprintf(mem_ctx,
9795                         "lpr://%s/%s", hostaddress, queue );
9796                 break;
9797
9798         default:
9799                 return WERR_UNKNOWN_PORT;
9800         }
9801
9802         if (!device_uri) {
9803                 return WERR_NOMEM;
9804         }
9805
9806         return add_port_hook(mem_ctx, token, portname, device_uri);
9807 }
9808
9809 /*******************************************************************
9810 *******************************************************************/
9811
9812 struct xcv_api_table xcvtcp_cmds[] = {
9813         { "MonitorUI",  xcvtcp_monitorui },
9814         { "AddPort",    xcvtcp_addport},
9815         { NULL,         NULL }
9816 };
9817
9818 static WERROR process_xcvtcp_command(TALLOC_CTX *mem_ctx,
9819                                      NT_USER_TOKEN *token, const char *command,
9820                                      DATA_BLOB *inbuf,
9821                                      DATA_BLOB *outbuf,
9822                                      uint32_t *needed )
9823 {
9824         int i;
9825
9826         DEBUG(10,("process_xcvtcp_command: Received command \"%s\"\n", command));
9827
9828         for ( i=0; xcvtcp_cmds[i].name; i++ ) {
9829                 if ( strcmp( command, xcvtcp_cmds[i].name ) == 0 )
9830                         return xcvtcp_cmds[i].fn(mem_ctx, token, inbuf, outbuf, needed);
9831         }
9832
9833         return WERR_BADFUNC;
9834 }
9835
9836 /*******************************************************************
9837 *******************************************************************/
9838 #if 0   /* don't support management using the "Local Port" monitor */
9839
9840 static WERROR xcvlocal_monitorui(TALLOC_CTX *mem_ctx,
9841                                  NT_USER_TOKEN *token, DATA_BLOB *in,
9842                                  DATA_BLOB *out, uint32_t *needed)
9843 {
9844         const char *dllname = "localui.dll";
9845
9846         *needed = (strlen(dllname)+1) * 2;
9847
9848         if (out->length < *needed) {
9849                 return WERR_INSUFFICIENT_BUFFER;
9850         }
9851
9852         if (!push_monitorui_buf(mem_ctx, out, dllname)) {
9853                 return WERR_NOMEM;
9854         }
9855
9856         return WERR_OK;
9857 }
9858
9859 /*******************************************************************
9860 *******************************************************************/
9861
9862 struct xcv_api_table xcvlocal_cmds[] = {
9863         { "MonitorUI",  xcvlocal_monitorui },
9864         { NULL,         NULL }
9865 };
9866 #else
9867 struct xcv_api_table xcvlocal_cmds[] = {
9868         { NULL,         NULL }
9869 };
9870 #endif
9871
9872
9873
9874 /*******************************************************************
9875 *******************************************************************/
9876
9877 static WERROR process_xcvlocal_command(TALLOC_CTX *mem_ctx,
9878                                        NT_USER_TOKEN *token, const char *command,
9879                                        DATA_BLOB *inbuf, DATA_BLOB *outbuf,
9880                                        uint32_t *needed)
9881 {
9882         int i;
9883
9884         DEBUG(10,("process_xcvlocal_command: Received command \"%s\"\n", command));
9885
9886         for ( i=0; xcvlocal_cmds[i].name; i++ ) {
9887                 if ( strcmp( command, xcvlocal_cmds[i].name ) == 0 )
9888                         return xcvlocal_cmds[i].fn(mem_ctx, token, inbuf, outbuf, needed);
9889         }
9890         return WERR_BADFUNC;
9891 }
9892
9893 /****************************************************************
9894  _spoolss_XcvData
9895 ****************************************************************/
9896
9897 WERROR _spoolss_XcvData(pipes_struct *p,
9898                         struct spoolss_XcvData *r)
9899 {
9900         Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
9901         DATA_BLOB out_data = data_blob_null;
9902         WERROR werror;
9903
9904         if (!Printer) {
9905                 DEBUG(2,("_spoolss_XcvData: Invalid handle (%s:%u:%u).\n",
9906                         OUR_HANDLE(r->in.handle)));
9907                 return WERR_BADFID;
9908         }
9909
9910         /* Has to be a handle to the TCP/IP port monitor */
9911
9912         if ( !(Printer->printer_type & (SPLHND_PORTMON_LOCAL|SPLHND_PORTMON_TCP)) ) {
9913                 DEBUG(2,("_spoolss_XcvData: Call only valid for Port Monitors\n"));
9914                 return WERR_BADFID;
9915         }
9916
9917         /* requires administrative access to the server */
9918
9919         if ( !(Printer->access_granted & SERVER_ACCESS_ADMINISTER) ) {
9920                 DEBUG(2,("_spoolss_XcvData: denied by handle permissions.\n"));
9921                 return WERR_ACCESS_DENIED;
9922         }
9923
9924         /* Allocate the outgoing buffer */
9925
9926         if (r->in.out_data_size) {
9927                 out_data = data_blob_talloc_zero(p->mem_ctx, r->in.out_data_size);
9928                 if (out_data.data == NULL) {
9929                         return WERR_NOMEM;
9930                 }
9931         }
9932
9933         switch ( Printer->printer_type ) {
9934         case SPLHND_PORTMON_TCP:
9935                 werror = process_xcvtcp_command(p->mem_ctx,
9936                                                 p->server_info->ptok,
9937                                                 r->in.function_name,
9938                                                 &r->in.in_data, &out_data,
9939                                                 r->out.needed);
9940                 break;
9941         case SPLHND_PORTMON_LOCAL:
9942                 werror = process_xcvlocal_command(p->mem_ctx,
9943                                                   p->server_info->ptok,
9944                                                   r->in.function_name,
9945                                                   &r->in.in_data, &out_data,
9946                                                   r->out.needed);
9947                 break;
9948         default:
9949                 werror = WERR_INVALID_PRINT_MONITOR;
9950         }
9951
9952         if (!W_ERROR_IS_OK(werror)) {
9953                 return werror;
9954         }
9955
9956         *r->out.status_code = 0;
9957
9958         memcpy(r->out.out_data, out_data.data, out_data.length);
9959
9960         return WERR_OK;
9961 }
9962
9963 /****************************************************************
9964  _spoolss_AddPrintProcessor
9965 ****************************************************************/
9966
9967 WERROR _spoolss_AddPrintProcessor(pipes_struct *p,
9968                                   struct spoolss_AddPrintProcessor *r)
9969 {
9970         /* for now, just indicate success and ignore the add.  We'll
9971            automatically set the winprint processor for printer
9972            entries later.  Used to debug the LexMark Optra S 1855 PCL
9973            driver --jerry */
9974
9975         return WERR_OK;
9976 }
9977
9978 /****************************************************************
9979  _spoolss_AddPort
9980 ****************************************************************/
9981
9982 WERROR _spoolss_AddPort(pipes_struct *p,
9983                         struct spoolss_AddPort *r)
9984 {
9985         /* do what w2k3 does */
9986
9987         return WERR_NOT_SUPPORTED;
9988 }
9989
9990 /****************************************************************
9991  _spoolss_AddPrinter
9992 ****************************************************************/
9993
9994 WERROR _spoolss_AddPrinter(pipes_struct *p,
9995                            struct spoolss_AddPrinter *r)
9996 {
9997         p->rng_fault_state = true;
9998         return WERR_NOT_SUPPORTED;
9999 }
10000
10001 /****************************************************************
10002  _spoolss_GetPrinterDriver
10003 ****************************************************************/
10004
10005 WERROR _spoolss_GetPrinterDriver(pipes_struct *p,
10006                                  struct spoolss_GetPrinterDriver *r)
10007 {
10008         p->rng_fault_state = true;
10009         return WERR_NOT_SUPPORTED;
10010 }
10011
10012 /****************************************************************
10013  _spoolss_ReadPrinter
10014 ****************************************************************/
10015
10016 WERROR _spoolss_ReadPrinter(pipes_struct *p,
10017                             struct spoolss_ReadPrinter *r)
10018 {
10019         p->rng_fault_state = true;
10020         return WERR_NOT_SUPPORTED;
10021 }
10022
10023 /****************************************************************
10024  _spoolss_WaitForPrinterChange
10025 ****************************************************************/
10026
10027 WERROR _spoolss_WaitForPrinterChange(pipes_struct *p,
10028                                      struct spoolss_WaitForPrinterChange *r)
10029 {
10030         p->rng_fault_state = true;
10031         return WERR_NOT_SUPPORTED;
10032 }
10033
10034 /****************************************************************
10035  _spoolss_ConfigurePort
10036 ****************************************************************/
10037
10038 WERROR _spoolss_ConfigurePort(pipes_struct *p,
10039                               struct spoolss_ConfigurePort *r)
10040 {
10041         p->rng_fault_state = true;
10042         return WERR_NOT_SUPPORTED;
10043 }
10044
10045 /****************************************************************
10046  _spoolss_DeletePort
10047 ****************************************************************/
10048
10049 WERROR _spoolss_DeletePort(pipes_struct *p,
10050                            struct spoolss_DeletePort *r)
10051 {
10052         p->rng_fault_state = true;
10053         return WERR_NOT_SUPPORTED;
10054 }
10055
10056 /****************************************************************
10057  _spoolss_CreatePrinterIC
10058 ****************************************************************/
10059
10060 WERROR _spoolss_CreatePrinterIC(pipes_struct *p,
10061                                 struct spoolss_CreatePrinterIC *r)
10062 {
10063         p->rng_fault_state = true;
10064         return WERR_NOT_SUPPORTED;
10065 }
10066
10067 /****************************************************************
10068  _spoolss_PlayGDIScriptOnPrinterIC
10069 ****************************************************************/
10070
10071 WERROR _spoolss_PlayGDIScriptOnPrinterIC(pipes_struct *p,
10072                                          struct spoolss_PlayGDIScriptOnPrinterIC *r)
10073 {
10074         p->rng_fault_state = true;
10075         return WERR_NOT_SUPPORTED;
10076 }
10077
10078 /****************************************************************
10079  _spoolss_DeletePrinterIC
10080 ****************************************************************/
10081
10082 WERROR _spoolss_DeletePrinterIC(pipes_struct *p,
10083                                 struct spoolss_DeletePrinterIC *r)
10084 {
10085         p->rng_fault_state = true;
10086         return WERR_NOT_SUPPORTED;
10087 }
10088
10089 /****************************************************************
10090  _spoolss_AddPrinterConnection
10091 ****************************************************************/
10092
10093 WERROR _spoolss_AddPrinterConnection(pipes_struct *p,
10094                                      struct spoolss_AddPrinterConnection *r)
10095 {
10096         p->rng_fault_state = true;
10097         return WERR_NOT_SUPPORTED;
10098 }
10099
10100 /****************************************************************
10101  _spoolss_DeletePrinterConnection
10102 ****************************************************************/
10103
10104 WERROR _spoolss_DeletePrinterConnection(pipes_struct *p,
10105                                         struct spoolss_DeletePrinterConnection *r)
10106 {
10107         p->rng_fault_state = true;
10108         return WERR_NOT_SUPPORTED;
10109 }
10110
10111 /****************************************************************
10112  _spoolss_PrinterMessageBox
10113 ****************************************************************/
10114
10115 WERROR _spoolss_PrinterMessageBox(pipes_struct *p,
10116                                   struct spoolss_PrinterMessageBox *r)
10117 {
10118         p->rng_fault_state = true;
10119         return WERR_NOT_SUPPORTED;
10120 }
10121
10122 /****************************************************************
10123  _spoolss_AddMonitor
10124 ****************************************************************/
10125
10126 WERROR _spoolss_AddMonitor(pipes_struct *p,
10127                            struct spoolss_AddMonitor *r)
10128 {
10129         p->rng_fault_state = true;
10130         return WERR_NOT_SUPPORTED;
10131 }
10132
10133 /****************************************************************
10134  _spoolss_DeleteMonitor
10135 ****************************************************************/
10136
10137 WERROR _spoolss_DeleteMonitor(pipes_struct *p,
10138                               struct spoolss_DeleteMonitor *r)
10139 {
10140         p->rng_fault_state = true;
10141         return WERR_NOT_SUPPORTED;
10142 }
10143
10144 /****************************************************************
10145  _spoolss_DeletePrintProcessor
10146 ****************************************************************/
10147
10148 WERROR _spoolss_DeletePrintProcessor(pipes_struct *p,
10149                                      struct spoolss_DeletePrintProcessor *r)
10150 {
10151         p->rng_fault_state = true;
10152         return WERR_NOT_SUPPORTED;
10153 }
10154
10155 /****************************************************************
10156  _spoolss_AddPrintProvidor
10157 ****************************************************************/
10158
10159 WERROR _spoolss_AddPrintProvidor(pipes_struct *p,
10160                                  struct spoolss_AddPrintProvidor *r)
10161 {
10162         p->rng_fault_state = true;
10163         return WERR_NOT_SUPPORTED;
10164 }
10165
10166 /****************************************************************
10167  _spoolss_DeletePrintProvidor
10168 ****************************************************************/
10169
10170 WERROR _spoolss_DeletePrintProvidor(pipes_struct *p,
10171                                     struct spoolss_DeletePrintProvidor *r)
10172 {
10173         p->rng_fault_state = true;
10174         return WERR_NOT_SUPPORTED;
10175 }
10176
10177 /****************************************************************
10178  _spoolss_FindFirstPrinterChangeNotification
10179 ****************************************************************/
10180
10181 WERROR _spoolss_FindFirstPrinterChangeNotification(pipes_struct *p,
10182                                                    struct spoolss_FindFirstPrinterChangeNotification *r)
10183 {
10184         p->rng_fault_state = true;
10185         return WERR_NOT_SUPPORTED;
10186 }
10187
10188 /****************************************************************
10189  _spoolss_FindNextPrinterChangeNotification
10190 ****************************************************************/
10191
10192 WERROR _spoolss_FindNextPrinterChangeNotification(pipes_struct *p,
10193                                                   struct spoolss_FindNextPrinterChangeNotification *r)
10194 {
10195         p->rng_fault_state = true;
10196         return WERR_NOT_SUPPORTED;
10197 }
10198
10199 /****************************************************************
10200  _spoolss_RouterFindFirstPrinterChangeNotificationOld
10201 ****************************************************************/
10202
10203 WERROR _spoolss_RouterFindFirstPrinterChangeNotificationOld(pipes_struct *p,
10204                                                             struct spoolss_RouterFindFirstPrinterChangeNotificationOld *r)
10205 {
10206         p->rng_fault_state = true;
10207         return WERR_NOT_SUPPORTED;
10208 }
10209
10210 /****************************************************************
10211  _spoolss_ReplyOpenPrinter
10212 ****************************************************************/
10213
10214 WERROR _spoolss_ReplyOpenPrinter(pipes_struct *p,
10215                                  struct spoolss_ReplyOpenPrinter *r)
10216 {
10217         p->rng_fault_state = true;
10218         return WERR_NOT_SUPPORTED;
10219 }
10220
10221 /****************************************************************
10222  _spoolss_RouterReplyPrinter
10223 ****************************************************************/
10224
10225 WERROR _spoolss_RouterReplyPrinter(pipes_struct *p,
10226                                    struct spoolss_RouterReplyPrinter *r)
10227 {
10228         p->rng_fault_state = true;
10229         return WERR_NOT_SUPPORTED;
10230 }
10231
10232 /****************************************************************
10233  _spoolss_ReplyClosePrinter
10234 ****************************************************************/
10235
10236 WERROR _spoolss_ReplyClosePrinter(pipes_struct *p,
10237                                   struct spoolss_ReplyClosePrinter *r)
10238 {
10239         p->rng_fault_state = true;
10240         return WERR_NOT_SUPPORTED;
10241 }
10242
10243 /****************************************************************
10244  _spoolss_AddPortEx
10245 ****************************************************************/
10246
10247 WERROR _spoolss_AddPortEx(pipes_struct *p,
10248                           struct spoolss_AddPortEx *r)
10249 {
10250         p->rng_fault_state = true;
10251         return WERR_NOT_SUPPORTED;
10252 }
10253
10254 /****************************************************************
10255  _spoolss_RouterFindFirstPrinterChangeNotification
10256 ****************************************************************/
10257
10258 WERROR _spoolss_RouterFindFirstPrinterChangeNotification(pipes_struct *p,
10259                                                          struct spoolss_RouterFindFirstPrinterChangeNotification *r)
10260 {
10261         p->rng_fault_state = true;
10262         return WERR_NOT_SUPPORTED;
10263 }
10264
10265 /****************************************************************
10266  _spoolss_SpoolerInit
10267 ****************************************************************/
10268
10269 WERROR _spoolss_SpoolerInit(pipes_struct *p,
10270                             struct spoolss_SpoolerInit *r)
10271 {
10272         p->rng_fault_state = true;
10273         return WERR_NOT_SUPPORTED;
10274 }
10275
10276 /****************************************************************
10277  _spoolss_ResetPrinterEx
10278 ****************************************************************/
10279
10280 WERROR _spoolss_ResetPrinterEx(pipes_struct *p,
10281                                struct spoolss_ResetPrinterEx *r)
10282 {
10283         p->rng_fault_state = true;
10284         return WERR_NOT_SUPPORTED;
10285 }
10286
10287 /****************************************************************
10288  _spoolss_RouterReplyPrinterEx
10289 ****************************************************************/
10290
10291 WERROR _spoolss_RouterReplyPrinterEx(pipes_struct *p,
10292                                      struct spoolss_RouterReplyPrinterEx *r)
10293 {
10294         p->rng_fault_state = true;
10295         return WERR_NOT_SUPPORTED;
10296 }
10297
10298 /****************************************************************
10299  _spoolss_44
10300 ****************************************************************/
10301
10302 WERROR _spoolss_44(pipes_struct *p,
10303                    struct spoolss_44 *r)
10304 {
10305         p->rng_fault_state = true;
10306         return WERR_NOT_SUPPORTED;
10307 }
10308
10309 /****************************************************************
10310  _spoolss_47
10311 ****************************************************************/
10312
10313 WERROR _spoolss_47(pipes_struct *p,
10314                    struct spoolss_47 *r)
10315 {
10316         p->rng_fault_state = true;
10317         return WERR_NOT_SUPPORTED;
10318 }
10319
10320 /****************************************************************
10321  _spoolss_4a
10322 ****************************************************************/
10323
10324 WERROR _spoolss_4a(pipes_struct *p,
10325                    struct spoolss_4a *r)
10326 {
10327         p->rng_fault_state = true;
10328         return WERR_NOT_SUPPORTED;
10329 }
10330
10331 /****************************************************************
10332  _spoolss_4b
10333 ****************************************************************/
10334
10335 WERROR _spoolss_4b(pipes_struct *p,
10336                    struct spoolss_4b *r)
10337 {
10338         p->rng_fault_state = true;
10339         return WERR_NOT_SUPPORTED;
10340 }
10341
10342 /****************************************************************
10343  _spoolss_4c
10344 ****************************************************************/
10345
10346 WERROR _spoolss_4c(pipes_struct *p,
10347                    struct spoolss_4c *r)
10348 {
10349         p->rng_fault_state = true;
10350         return WERR_NOT_SUPPORTED;
10351 }
10352
10353 /****************************************************************
10354  _spoolss_53
10355 ****************************************************************/
10356
10357 WERROR _spoolss_53(pipes_struct *p,
10358                    struct spoolss_53 *r)
10359 {
10360         p->rng_fault_state = true;
10361         return WERR_NOT_SUPPORTED;
10362 }
10363
10364 /****************************************************************
10365  _spoolss_55
10366 ****************************************************************/
10367
10368 WERROR _spoolss_55(pipes_struct *p,
10369                    struct spoolss_55 *r)
10370 {
10371         p->rng_fault_state = true;
10372         return WERR_NOT_SUPPORTED;
10373 }
10374
10375 /****************************************************************
10376  _spoolss_56
10377 ****************************************************************/
10378
10379 WERROR _spoolss_56(pipes_struct *p,
10380                    struct spoolss_56 *r)
10381 {
10382         p->rng_fault_state = true;
10383         return WERR_NOT_SUPPORTED;
10384 }
10385
10386 /****************************************************************
10387  _spoolss_57
10388 ****************************************************************/
10389
10390 WERROR _spoolss_57(pipes_struct *p,
10391                    struct spoolss_57 *r)
10392 {
10393         p->rng_fault_state = true;
10394         return WERR_NOT_SUPPORTED;
10395 }
10396
10397 /****************************************************************
10398  _spoolss_5a
10399 ****************************************************************/
10400
10401 WERROR _spoolss_5a(pipes_struct *p,
10402                    struct spoolss_5a *r)
10403 {
10404         p->rng_fault_state = true;
10405         return WERR_NOT_SUPPORTED;
10406 }
10407
10408 /****************************************************************
10409  _spoolss_5b
10410 ****************************************************************/
10411
10412 WERROR _spoolss_5b(pipes_struct *p,
10413                    struct spoolss_5b *r)
10414 {
10415         p->rng_fault_state = true;
10416         return WERR_NOT_SUPPORTED;
10417 }
10418
10419 /****************************************************************
10420  _spoolss_5c
10421 ****************************************************************/
10422
10423 WERROR _spoolss_5c(pipes_struct *p,
10424                    struct spoolss_5c *r)
10425 {
10426         p->rng_fault_state = true;
10427         return WERR_NOT_SUPPORTED;
10428 }
10429
10430 /****************************************************************
10431  _spoolss_5d
10432 ****************************************************************/
10433
10434 WERROR _spoolss_5d(pipes_struct *p,
10435                    struct spoolss_5d *r)
10436 {
10437         p->rng_fault_state = true;
10438         return WERR_NOT_SUPPORTED;
10439 }
10440
10441 /****************************************************************
10442  _spoolss_5e
10443 ****************************************************************/
10444
10445 WERROR _spoolss_5e(pipes_struct *p,
10446                    struct spoolss_5e *r)
10447 {
10448         p->rng_fault_state = true;
10449         return WERR_NOT_SUPPORTED;
10450 }
10451
10452 /****************************************************************
10453  _spoolss_5f
10454 ****************************************************************/
10455
10456 WERROR _spoolss_5f(pipes_struct *p,
10457                    struct spoolss_5f *r)
10458 {
10459         p->rng_fault_state = true;
10460         return WERR_NOT_SUPPORTED;
10461 }
10462