s3: Lift the server_messaging_context from print_queue_length
[amitay/samba.git] / source3 / rpc_server / srv_spoolss_nt.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  RPC Pipe client / server routines
4  *  Copyright (C) Andrew Tridgell              1992-2000,
5  *  Copyright (C) Luke Kenneth Casson Leighton 1996-2000,
6  *  Copyright (C) Jean François Micouleau      1998-2000,
7  *  Copyright (C) Jeremy Allison               2001-2002,
8  *  Copyright (C) Gerald Carter                2000-2004,
9  *  Copyright (C) Tim Potter                   2001-2002.
10  *  Copyright (C) Guenther Deschner            2009-2010.
11  *  Copyright (C) Andreas Schneider            2010.
12  *
13  *  This program is free software; you can redistribute it and/or modify
14  *  it under the terms of the GNU General Public License as published by
15  *  the Free Software Foundation; either version 3 of the License, or
16  *  (at your option) any later version.
17  *
18  *  This program is distributed in the hope that it will be useful,
19  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  *  GNU General Public License for more details.
22  *
23  *  You should have received a copy of the GNU General Public License
24  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
25  */
26
27 /* Since the SPOOLSS rpc routines are basically DOS 16-bit calls wrapped
28    up, all the errors returned are DOS errors, not NT status codes. */
29
30 #include "includes.h"
31 #include "nt_printing.h"
32 #include "srv_spoolss_util.h"
33 #include "../librpc/gen_ndr/srv_spoolss.h"
34 #include "../librpc/gen_ndr/cli_spoolss.h"
35 #include "rpc_client/init_spoolss.h"
36 #include "librpc/gen_ndr/messaging.h"
37 #include "librpc/gen_ndr/ndr_security.h"
38 #include "registry.h"
39 #include "registry/reg_objects.h"
40 #include "include/printing.h"
41 #include "secrets.h"
42 #include "../librpc/gen_ndr/netlogon.h"
43
44 /* macros stolen from s4 spoolss server */
45 #define SPOOLSS_BUFFER_UNION(fn,info,level) \
46         ((info)?ndr_size_##fn(info, level, 0):0)
47
48 #define SPOOLSS_BUFFER_UNION_ARRAY(mem_ctx,fn,info,level,count) \
49         ((info)?ndr_size_##fn##_info(mem_ctx, level, count, info):0)
50
51 #define SPOOLSS_BUFFER_ARRAY(mem_ctx,fn,info,count) \
52         ((info)?ndr_size_##fn##_info(mem_ctx, count, info):0)
53
54 #define SPOOLSS_BUFFER_OK(val_true,val_false) ((r->in.offered >= *r->out.needed)?val_true:val_false)
55
56 #undef DBGC_CLASS
57 #define DBGC_CLASS DBGC_RPC_SRV
58
59 static Printer_entry *printers_list;
60
61 typedef struct _counter_printer_0 {
62         struct _counter_printer_0 *next;
63         struct _counter_printer_0 *prev;
64
65         int snum;
66         uint32_t counter;
67 } counter_printer_0;
68
69 static counter_printer_0 *counter_list;
70
71 static struct rpc_pipe_client *notify_cli_pipe; /* print notify back-channel pipe handle*/
72 static uint32_t smb_connections = 0;
73
74
75 /* Map generic permissions to printer object specific permissions */
76
77 const struct standard_mapping printer_std_mapping = {
78         PRINTER_READ,
79         PRINTER_WRITE,
80         PRINTER_EXECUTE,
81         PRINTER_ALL_ACCESS
82 };
83
84 /* Map generic permissions to print server object specific permissions */
85
86 const struct standard_mapping printserver_std_mapping = {
87         SERVER_READ,
88         SERVER_WRITE,
89         SERVER_EXECUTE,
90         SERVER_ALL_ACCESS
91 };
92
93 /* API table for Xcv Monitor functions */
94
95 struct xcv_api_table {
96         const char *name;
97         WERROR(*fn) (TALLOC_CTX *mem_ctx, NT_USER_TOKEN *token, DATA_BLOB *in, DATA_BLOB *out, uint32_t *needed);
98 };
99
100 /********************************************************************
101  * Canonicalize servername.
102  ********************************************************************/
103
104 static const char *canon_servername(const char *servername)
105 {
106         const char *pservername = servername;
107         while (*pservername == '\\') {
108                 pservername++;
109         }
110         return pservername;
111 }
112
113 /* translate between internal status numbers and NT status numbers */
114 static int nt_printj_status(int v)
115 {
116         switch (v) {
117         case LPQ_QUEUED:
118                 return 0;
119         case LPQ_PAUSED:
120                 return JOB_STATUS_PAUSED;
121         case LPQ_SPOOLING:
122                 return JOB_STATUS_SPOOLING;
123         case LPQ_PRINTING:
124                 return JOB_STATUS_PRINTING;
125         case LPQ_ERROR:
126                 return JOB_STATUS_ERROR;
127         case LPQ_DELETING:
128                 return JOB_STATUS_DELETING;
129         case LPQ_OFFLINE:
130                 return JOB_STATUS_OFFLINE;
131         case LPQ_PAPEROUT:
132                 return JOB_STATUS_PAPEROUT;
133         case LPQ_PRINTED:
134                 return JOB_STATUS_PRINTED;
135         case LPQ_DELETED:
136                 return JOB_STATUS_DELETED;
137         case LPQ_BLOCKED:
138                 return JOB_STATUS_BLOCKED_DEVQ;
139         case LPQ_USER_INTERVENTION:
140                 return JOB_STATUS_USER_INTERVENTION;
141         }
142         return 0;
143 }
144
145 static int nt_printq_status(int v)
146 {
147         switch (v) {
148         case LPQ_PAUSED:
149                 return PRINTER_STATUS_PAUSED;
150         case LPQ_QUEUED:
151         case LPQ_SPOOLING:
152         case LPQ_PRINTING:
153                 return 0;
154         }
155         return 0;
156 }
157
158 /***************************************************************************
159  Disconnect from the client
160 ****************************************************************************/
161
162 static void srv_spoolss_replycloseprinter(
163         int snum, struct policy_handle *handle,
164         struct messaging_context *msg_ctx)
165 {
166         WERROR result;
167         NTSTATUS status;
168
169         /*
170          * Tell the specific printing tdb we no longer want messages for this printer
171          * by deregistering our PID.
172          */
173
174         if (!print_notify_deregister_pid(snum))
175                 DEBUG(0,("print_notify_register_pid: Failed to register our pid for printer %s\n", lp_const_servicename(snum) ));
176
177         /* weird if the test succeeds !!! */
178         if (smb_connections==0) {
179                 DEBUG(0,("srv_spoolss_replycloseprinter:Trying to close non-existant notify backchannel !\n"));
180                 return;
181         }
182
183         status = rpccli_spoolss_ReplyClosePrinter(notify_cli_pipe, talloc_tos(),
184                                                   handle,
185                                                   &result);
186         if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result))
187                 DEBUG(0,("srv_spoolss_replycloseprinter: reply_close_printer failed [%s].\n",
188                         win_errstr(result)));
189
190         /* if it's the last connection, deconnect the IPC$ share */
191         if (smb_connections==1) {
192
193                 cli_shutdown( rpc_pipe_np_smb_conn(notify_cli_pipe) );
194                 /*
195                  * The above call shuts down the pipe also.
196                  */
197                 notify_cli_pipe = NULL;
198
199                 if (msg_ctx != NULL) {
200                         messaging_deregister(msg_ctx, MSG_PRINTER_NOTIFY2,
201                                              NULL);
202
203                         /*
204                          * Tell the serverid.tdb we're no longer
205                          * interested in printer notify messages.
206                          */
207
208                         serverid_register_msg_flags(
209                                 messaging_server_id(msg_ctx),
210                                 false, FLAG_MSG_PRINT_NOTIFY);
211                 }
212         }
213
214         smb_connections--;
215 }
216
217 /****************************************************************************
218  Functions to free a printer entry datastruct.
219 ****************************************************************************/
220
221 static int printer_entry_destructor(Printer_entry *Printer)
222 {
223         if (Printer->notify.client_connected == true) {
224                 int snum = -1;
225
226                 if ( Printer->printer_type == SPLHND_SERVER) {
227                         snum = -1;
228                         srv_spoolss_replycloseprinter(
229                                 snum, &Printer->notify.client_hnd,
230                                 Printer->notify.msg_ctx);
231                 } else if (Printer->printer_type == SPLHND_PRINTER) {
232                         snum = print_queue_snum(Printer->sharename);
233                         if (snum != -1)
234                                 srv_spoolss_replycloseprinter(
235                                         snum, &Printer->notify.client_hnd,
236                                         Printer->notify.msg_ctx);
237                 }
238         }
239
240         Printer->notify.flags=0;
241         Printer->notify.options=0;
242         Printer->notify.localmachine[0]='\0';
243         Printer->notify.printerlocal=0;
244         TALLOC_FREE(Printer->notify.option);
245         Printer->notify.client_connected = false;
246
247         TALLOC_FREE(Printer->devmode);
248
249         /* Remove from the internal list. */
250         DLIST_REMOVE(printers_list, Printer);
251         return 0;
252 }
253
254 /****************************************************************************
255   find printer index by handle
256 ****************************************************************************/
257
258 static Printer_entry *find_printer_index_by_hnd(struct pipes_struct *p,
259                                                 struct policy_handle *hnd)
260 {
261         Printer_entry *find_printer = NULL;
262
263         if(!find_policy_by_hnd(p,hnd,(void **)(void *)&find_printer)) {
264                 DEBUG(2,("find_printer_index_by_hnd: Printer handle not found: "));
265                 return NULL;
266         }
267
268         return find_printer;
269 }
270
271 /****************************************************************************
272  Close printer index by handle.
273 ****************************************************************************/
274
275 static bool close_printer_handle(struct pipes_struct *p, struct policy_handle *hnd)
276 {
277         Printer_entry *Printer = find_printer_index_by_hnd(p, hnd);
278
279         if (!Printer) {
280                 DEBUG(2,("close_printer_handle: Invalid handle (%s:%u:%u)\n",
281                         OUR_HANDLE(hnd)));
282                 return false;
283         }
284
285         close_policy_hnd(p, hnd);
286
287         return true;
288 }
289
290 /****************************************************************************
291  Delete a printer given a handle.
292 ****************************************************************************/
293
294 static WERROR delete_printer_hook(TALLOC_CTX *ctx, NT_USER_TOKEN *token,
295                                   const char *sharename,
296                                   struct messaging_context *msg_ctx)
297 {
298         char *cmd = lp_deleteprinter_cmd();
299         char *command = NULL;
300         int ret;
301         SE_PRIV se_printop = SE_PRINT_OPERATOR;
302         bool is_print_op = false;
303
304         /* can't fail if we don't try */
305
306         if ( !*cmd )
307                 return WERR_OK;
308
309         command = talloc_asprintf(ctx,
310                         "%s \"%s\"",
311                         cmd, sharename);
312         if (!command) {
313                 return WERR_NOMEM;
314         }
315         if ( token )
316                 is_print_op = user_has_privileges( token, &se_printop );
317
318         DEBUG(10,("Running [%s]\n", command));
319
320         /********** BEGIN SePrintOperatorPrivlege BLOCK **********/
321
322         if ( is_print_op )
323                 become_root();
324
325         if ( (ret = smbrun(command, NULL)) == 0 ) {
326                 /* Tell everyone we updated smb.conf. */
327                 message_send_all(msg_ctx, MSG_SMB_CONF_UPDATED, NULL, 0, NULL);
328         }
329
330         if ( is_print_op )
331                 unbecome_root();
332
333         /********** END SePrintOperatorPrivlege BLOCK **********/
334
335         DEBUGADD(10,("returned [%d]\n", ret));
336
337         TALLOC_FREE(command);
338
339         if (ret != 0)
340                 return WERR_BADFID; /* What to return here? */
341
342         /* go ahead and re-read the services immediately */
343         become_root();
344         reload_services(msg_ctx, false);
345         unbecome_root();
346
347         if ( lp_servicenumber( sharename )  > 0 )
348                 return WERR_ACCESS_DENIED;
349
350         return WERR_OK;
351 }
352
353 /****************************************************************************
354  Delete a printer given a handle.
355 ****************************************************************************/
356
357 static WERROR delete_printer_handle(struct pipes_struct *p, struct policy_handle *hnd)
358 {
359         Printer_entry *Printer = find_printer_index_by_hnd(p, hnd);
360         WERROR result;
361
362         if (!Printer) {
363                 DEBUG(2,("delete_printer_handle: Invalid handle (%s:%u:%u)\n",
364                         OUR_HANDLE(hnd)));
365                 return WERR_BADFID;
366         }
367
368         /*
369          * It turns out that Windows allows delete printer on a handle
370          * opened by an admin user, then used on a pipe handle created
371          * by an anonymous user..... but they're working on security.... riiight !
372          * JRA.
373          */
374
375         if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
376                 DEBUG(3, ("delete_printer_handle: denied by handle\n"));
377                 return WERR_ACCESS_DENIED;
378         }
379
380         /* this does not need a become root since the access check has been
381            done on the handle already */
382
383         result = winreg_delete_printer_key(p->mem_ctx, p->server_info,
384                                            p->msg_ctx, Printer->sharename, "");
385         if (!W_ERROR_IS_OK(result)) {
386                 DEBUG(3,("Error deleting printer %s\n", Printer->sharename));
387                 return WERR_BADFID;
388         }
389
390         return delete_printer_hook(p->mem_ctx, p->server_info->ptok,
391                                    Printer->sharename, p->msg_ctx);
392 }
393
394 /****************************************************************************
395  Return the snum of a printer corresponding to an handle.
396 ****************************************************************************/
397
398 static bool get_printer_snum(struct pipes_struct *p, struct policy_handle *hnd,
399                              int *number, struct share_params **params)
400 {
401         Printer_entry *Printer = find_printer_index_by_hnd(p, hnd);
402
403         if (!Printer) {
404                 DEBUG(2,("get_printer_snum: Invalid handle (%s:%u:%u)\n",
405                         OUR_HANDLE(hnd)));
406                 return false;
407         }
408
409         switch (Printer->printer_type) {
410                 case SPLHND_PRINTER:
411                         DEBUG(4,("short name:%s\n", Printer->sharename));
412                         *number = print_queue_snum(Printer->sharename);
413                         return (*number != -1);
414                 case SPLHND_SERVER:
415                         return false;
416                 default:
417                         return false;
418         }
419 }
420
421 /****************************************************************************
422  Set printer handle type.
423  Check if it's \\server or \\server\printer
424 ****************************************************************************/
425
426 static bool set_printer_hnd_printertype(Printer_entry *Printer, const char *handlename)
427 {
428         DEBUG(3,("Setting printer type=%s\n", handlename));
429
430         if ( strlen(handlename) < 3 ) {
431                 DEBUGADD(4,("A print server must have at least 1 char ! %s\n", handlename));
432                 return false;
433         }
434
435         /* it's a print server */
436         if (*handlename=='\\' && *(handlename+1)=='\\' && !strchr_m(handlename+2, '\\')) {
437                 DEBUGADD(4,("Printer is a print server\n"));
438                 Printer->printer_type = SPLHND_SERVER;
439         }
440         /* it's a printer (set_printer_hnd_name() will handle port monitors */
441         else {
442                 DEBUGADD(4,("Printer is a printer\n"));
443                 Printer->printer_type = SPLHND_PRINTER;
444         }
445
446         return true;
447 }
448
449 /****************************************************************************
450  Set printer handle name..  Accept names like \\server, \\server\printer,
451  \\server\SHARE, & "\\server\,XcvMonitor Standard TCP/IP Port"    See
452  the MSDN docs regarding OpenPrinter() for details on the XcvData() and
453  XcvDataPort() interface.
454 ****************************************************************************/
455
456 static bool set_printer_hnd_name(TALLOC_CTX *mem_ctx,
457                                  struct auth_serversupplied_info *server_info,
458                                  struct messaging_context *msg_ctx,
459                                  Printer_entry *Printer,
460                                  const char *handlename)
461 {
462         int snum;
463         int n_services=lp_numservices();
464         char *aprinter;
465         const char *printername;
466         const char *servername = NULL;
467         fstring sname;
468         bool found = false;
469         struct spoolss_PrinterInfo2 *info2 = NULL;
470         WERROR result;
471
472         DEBUG(4,("Setting printer name=%s (len=%lu)\n", handlename,
473                 (unsigned long)strlen(handlename)));
474
475         aprinter = CONST_DISCARD(char *, handlename);
476         if ( *handlename == '\\' ) {
477                 servername = canon_servername(handlename);
478                 if ( (aprinter = strchr_m( servername, '\\' )) != NULL ) {
479                         *aprinter = '\0';
480                         aprinter++;
481                 }
482                 if (!is_myname_or_ipaddr(servername)) {
483                         return false;
484                 }
485
486                 fstrcpy(Printer->servername, servername);
487         }
488
489         if (Printer->printer_type == SPLHND_SERVER) {
490                 return true;
491         }
492
493         if (Printer->printer_type != SPLHND_PRINTER) {
494                 return false;
495         }
496
497         DEBUGADD(5, ("searching for [%s]\n", aprinter));
498
499         /* check for the Port Monitor Interface */
500         if ( strequal( aprinter, SPL_XCV_MONITOR_TCPMON ) ) {
501                 Printer->printer_type = SPLHND_PORTMON_TCP;
502                 fstrcpy(sname, SPL_XCV_MONITOR_TCPMON);
503                 found = true;
504         }
505         else if ( strequal( aprinter, SPL_XCV_MONITOR_LOCALMON ) ) {
506                 Printer->printer_type = SPLHND_PORTMON_LOCAL;
507                 fstrcpy(sname, SPL_XCV_MONITOR_LOCALMON);
508                 found = true;
509         }
510
511         /* Search all sharenames first as this is easier than pulling
512            the printer_info_2 off of disk. Don't use find_service() since
513            that calls out to map_username() */
514
515         /* do another loop to look for printernames */
516         for (snum = 0; !found && snum < n_services; snum++) {
517                 const char *printer = lp_const_servicename(snum);
518
519                 /* no point going on if this is not a printer */
520                 if (!(lp_snum_ok(snum) && lp_print_ok(snum))) {
521                         continue;
522                 }
523
524                 /* ignore [printers] share */
525                 if (strequal(printer, "printers")) {
526                         continue;
527                 }
528
529                 fstrcpy(sname, printer);
530                 if (strequal(aprinter, printer)) {
531                         found = true;
532                         break;
533                 }
534
535                 /* no point looking up the printer object if
536                    we aren't allowing printername != sharename */
537                 if (lp_force_printername(snum)) {
538                         continue;
539                 }
540
541                 result = winreg_get_printer(mem_ctx,
542                                             server_info,
543                                             msg_ctx,
544                                             servername,
545                                             sname,
546                                             &info2);
547                 if ( !W_ERROR_IS_OK(result) ) {
548                         DEBUG(2,("set_printer_hnd_name: failed to lookup printer [%s] -- result [%s]\n",
549                                  sname, win_errstr(result)));
550                         continue;
551                 }
552
553                 printername = strrchr(info2->printername, '\\');
554                 if (printername == NULL) {
555                         printername = info2->printername;
556                 } else {
557                         printername++;
558                 }
559
560                 if (strequal(printername, aprinter)) {
561                         found = true;
562                         break;
563                 }
564
565                 DEBUGADD(10, ("printername: %s\n", printername));
566
567                 TALLOC_FREE(info2);
568         }
569
570         if ( !found ) {
571                 DEBUGADD(4,("Printer not found\n"));
572                 return false;
573         }
574
575         DEBUGADD(4,("set_printer_hnd_name: Printer found: %s -> %s\n", aprinter, sname));
576
577         fstrcpy(Printer->sharename, sname);
578
579         return true;
580 }
581
582 /****************************************************************************
583  Find first available printer slot. creates a printer handle for you.
584  ****************************************************************************/
585
586 static bool open_printer_hnd(struct pipes_struct *p, struct policy_handle *hnd,
587                              const char *name, uint32_t access_granted)
588 {
589         Printer_entry *new_printer;
590
591         DEBUG(10,("open_printer_hnd: name [%s]\n", name));
592
593         new_printer = TALLOC_ZERO_P(NULL, Printer_entry);
594         if (new_printer == NULL) {
595                 return false;
596         }
597         talloc_set_destructor(new_printer, printer_entry_destructor);
598
599         if (!create_policy_hnd(p, hnd, new_printer)) {
600                 TALLOC_FREE(new_printer);
601                 return false;
602         }
603
604         /* Add to the internal list. */
605         DLIST_ADD(printers_list, new_printer);
606
607         new_printer->notify.option=NULL;
608
609         if (!set_printer_hnd_printertype(new_printer, name)) {
610                 close_printer_handle(p, hnd);
611                 return false;
612         }
613
614         if (!set_printer_hnd_name(p->mem_ctx, p->server_info, p->msg_ctx,
615                                   new_printer, name)) {
616                 close_printer_handle(p, hnd);
617                 return false;
618         }
619
620         new_printer->access_granted = access_granted;
621
622         DEBUG(5, ("%d printer handles active\n",
623                   (int)num_pipe_handles(p)));
624
625         return true;
626 }
627
628 /***************************************************************************
629  check to see if the client motify handle is monitoring the notification
630  given by (notify_type, notify_field).
631  **************************************************************************/
632
633 static bool is_monitoring_event_flags(uint32_t flags, uint16_t notify_type,
634                                       uint16_t notify_field)
635 {
636         return true;
637 }
638
639 static bool is_monitoring_event(Printer_entry *p, uint16_t notify_type,
640                                 uint16_t notify_field)
641 {
642         struct spoolss_NotifyOption *option = p->notify.option;
643         uint32_t i, j;
644
645         /*
646          * Flags should always be zero when the change notify
647          * is registered by the client's spooler.  A user Win32 app
648          * might use the flags though instead of the NOTIFY_OPTION_INFO
649          * --jerry
650          */
651
652         if (!option) {
653                 return false;
654         }
655
656         if (p->notify.flags)
657                 return is_monitoring_event_flags(
658                         p->notify.flags, notify_type, notify_field);
659
660         for (i = 0; i < option->count; i++) {
661
662                 /* Check match for notify_type */
663
664                 if (option->types[i].type != notify_type)
665                         continue;
666
667                 /* Check match for field */
668
669                 for (j = 0; j < option->types[i].count; j++) {
670                         if (option->types[i].fields[j].field == notify_field) {
671                                 return true;
672                         }
673                 }
674         }
675
676         DEBUG(10, ("Open handle for \\\\%s\\%s is not monitoring 0x%02x/0x%02x\n",
677                    p->servername, p->sharename, notify_type, notify_field));
678
679         return false;
680 }
681
682 #define SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(_data, _integer) \
683         _data->data.integer[0] = _integer; \
684         _data->data.integer[1] = 0;
685
686
687 #define SETUP_SPOOLSS_NOTIFY_DATA_STRING(_data, _p) \
688         _data->data.string.string = talloc_strdup(mem_ctx, _p); \
689         if (!_data->data.string.string) {\
690                 _data->data.string.size = 0; \
691         } \
692         _data->data.string.size = strlen_m_term(_p) * 2;
693
694 #define SETUP_SPOOLSS_NOTIFY_DATA_DEVMODE(_data, _devmode) \
695         _data->data.devmode.devmode = _devmode;
696
697 #define SETUP_SPOOLSS_NOTIFY_DATA_SECDESC(_data, _sd) \
698         _data->data.sd.sd = dup_sec_desc(mem_ctx, _sd); \
699         if (!_data->data.sd.sd) { \
700                 _data->data.sd.sd_size = 0; \
701         } \
702         _data->data.sd.sd_size = \
703                 ndr_size_security_descriptor(_data->data.sd.sd, 0);
704
705 static void init_systemtime_buffer(TALLOC_CTX *mem_ctx,
706                                    struct tm *t,
707                                    const char **pp,
708                                    uint32_t *plen)
709 {
710         struct spoolss_Time st;
711         uint32_t len = 16;
712         char *p;
713
714         if (!init_systemtime(&st, t)) {
715                 return;
716         }
717
718         p = talloc_array(mem_ctx, char, len);
719         if (!p) {
720                 return;
721         }
722
723         /*
724          * Systemtime must be linearized as a set of UINT16's.
725          * Fix from Benjamin (Bj) Kuit bj@it.uts.edu.au
726          */
727
728         SSVAL(p, 0, st.year);
729         SSVAL(p, 2, st.month);
730         SSVAL(p, 4, st.day_of_week);
731         SSVAL(p, 6, st.day);
732         SSVAL(p, 8, st.hour);
733         SSVAL(p, 10, st.minute);
734         SSVAL(p, 12, st.second);
735         SSVAL(p, 14, st.millisecond);
736
737         *pp = p;
738         *plen = len;
739 }
740
741 /* Convert a notification message to a struct spoolss_Notify */
742
743 static void notify_one_value(struct spoolss_notify_msg *msg,
744                              struct spoolss_Notify *data,
745                              TALLOC_CTX *mem_ctx)
746 {
747         SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, msg->notify.value[0]);
748 }
749
750 static void notify_string(struct spoolss_notify_msg *msg,
751                           struct spoolss_Notify *data,
752                           TALLOC_CTX *mem_ctx)
753 {
754         /* The length of the message includes the trailing \0 */
755
756         data->data.string.size = msg->len * 2;
757         data->data.string.string = talloc_strdup(mem_ctx, msg->notify.data);
758         if (!data->data.string.string) {
759                 data->data.string.size = 0;
760                 return;
761         }
762 }
763
764 static void notify_system_time(struct spoolss_notify_msg *msg,
765                                struct spoolss_Notify *data,
766                                TALLOC_CTX *mem_ctx)
767 {
768         data->data.string.string = NULL;
769         data->data.string.size = 0;
770
771         if (msg->len != sizeof(time_t)) {
772                 DEBUG(5, ("notify_system_time: received wrong sized message (%d)\n",
773                           msg->len));
774                 return;
775         }
776
777         init_systemtime_buffer(mem_ctx, gmtime((time_t *)msg->notify.data),
778                                &data->data.string.string,
779                                &data->data.string.size);
780 }
781
782 struct notify2_message_table {
783         const char *name;
784         void (*fn)(struct spoolss_notify_msg *msg,
785                    struct spoolss_Notify *data, TALLOC_CTX *mem_ctx);
786 };
787
788 static struct notify2_message_table printer_notify_table[] = {
789         /* 0x00 */ { "PRINTER_NOTIFY_FIELD_SERVER_NAME", notify_string },
790         /* 0x01 */ { "PRINTER_NOTIFY_FIELD_PRINTER_NAME", notify_string },
791         /* 0x02 */ { "PRINTER_NOTIFY_FIELD_SHARE_NAME", notify_string },
792         /* 0x03 */ { "PRINTER_NOTIFY_FIELD_PORT_NAME", notify_string },
793         /* 0x04 */ { "PRINTER_NOTIFY_FIELD_DRIVER_NAME", notify_string },
794         /* 0x05 */ { "PRINTER_NOTIFY_FIELD_COMMENT", notify_string },
795         /* 0x06 */ { "PRINTER_NOTIFY_FIELD_LOCATION", notify_string },
796         /* 0x07 */ { "PRINTER_NOTIFY_FIELD_DEVMODE", NULL },
797         /* 0x08 */ { "PRINTER_NOTIFY_FIELD_SEPFILE", notify_string },
798         /* 0x09 */ { "PRINTER_NOTIFY_FIELD_PRINT_PROCESSOR", notify_string },
799         /* 0x0a */ { "PRINTER_NOTIFY_FIELD_PARAMETERS", NULL },
800         /* 0x0b */ { "PRINTER_NOTIFY_FIELD_DATATYPE", notify_string },
801         /* 0x0c */ { "PRINTER_NOTIFY_FIELD_SECURITY_DESCRIPTOR", NULL },
802         /* 0x0d */ { "PRINTER_NOTIFY_FIELD_ATTRIBUTES", notify_one_value },
803         /* 0x0e */ { "PRINTER_NOTIFY_FIELD_PRIORITY", notify_one_value },
804         /* 0x0f */ { "PRINTER_NOTIFY_FIELD_DEFAULT_PRIORITY", NULL },
805         /* 0x10 */ { "PRINTER_NOTIFY_FIELD_START_TIME", NULL },
806         /* 0x11 */ { "PRINTER_NOTIFY_FIELD_UNTIL_TIME", NULL },
807         /* 0x12 */ { "PRINTER_NOTIFY_FIELD_STATUS", notify_one_value },
808 };
809
810 static struct notify2_message_table job_notify_table[] = {
811         /* 0x00 */ { "JOB_NOTIFY_FIELD_PRINTER_NAME", NULL },
812         /* 0x01 */ { "JOB_NOTIFY_FIELD_MACHINE_NAME", NULL },
813         /* 0x02 */ { "JOB_NOTIFY_FIELD_PORT_NAME", NULL },
814         /* 0x03 */ { "JOB_NOTIFY_FIELD_USER_NAME", notify_string },
815         /* 0x04 */ { "JOB_NOTIFY_FIELD_NOTIFY_NAME", NULL },
816         /* 0x05 */ { "JOB_NOTIFY_FIELD_DATATYPE", NULL },
817         /* 0x06 */ { "JOB_NOTIFY_FIELD_PRINT_PROCESSOR", NULL },
818         /* 0x07 */ { "JOB_NOTIFY_FIELD_PARAMETERS", NULL },
819         /* 0x08 */ { "JOB_NOTIFY_FIELD_DRIVER_NAME", NULL },
820         /* 0x09 */ { "JOB_NOTIFY_FIELD_DEVMODE", NULL },
821         /* 0x0a */ { "JOB_NOTIFY_FIELD_STATUS", notify_one_value },
822         /* 0x0b */ { "JOB_NOTIFY_FIELD_STATUS_STRING", NULL },
823         /* 0x0c */ { "JOB_NOTIFY_FIELD_SECURITY_DESCRIPTOR", NULL },
824         /* 0x0d */ { "JOB_NOTIFY_FIELD_DOCUMENT", notify_string },
825         /* 0x0e */ { "JOB_NOTIFY_FIELD_PRIORITY", NULL },
826         /* 0x0f */ { "JOB_NOTIFY_FIELD_POSITION", NULL },
827         /* 0x10 */ { "JOB_NOTIFY_FIELD_SUBMITTED", notify_system_time },
828         /* 0x11 */ { "JOB_NOTIFY_FIELD_START_TIME", NULL },
829         /* 0x12 */ { "JOB_NOTIFY_FIELD_UNTIL_TIME", NULL },
830         /* 0x13 */ { "JOB_NOTIFY_FIELD_TIME", NULL },
831         /* 0x14 */ { "JOB_NOTIFY_FIELD_TOTAL_PAGES", notify_one_value },
832         /* 0x15 */ { "JOB_NOTIFY_FIELD_PAGES_PRINTED", NULL },
833         /* 0x16 */ { "JOB_NOTIFY_FIELD_TOTAL_BYTES", notify_one_value },
834         /* 0x17 */ { "JOB_NOTIFY_FIELD_BYTES_PRINTED", NULL },
835 };
836
837
838 /***********************************************************************
839  Allocate talloc context for container object
840  **********************************************************************/
841
842 static void notify_msg_ctr_init( SPOOLSS_NOTIFY_MSG_CTR *ctr )
843 {
844         if ( !ctr )
845                 return;
846
847         ctr->ctx = talloc_init("notify_msg_ctr_init %p", ctr);
848
849         return;
850 }
851
852 /***********************************************************************
853  release all allocated memory and zero out structure
854  **********************************************************************/
855
856 static void notify_msg_ctr_destroy( SPOOLSS_NOTIFY_MSG_CTR *ctr )
857 {
858         if ( !ctr )
859                 return;
860
861         if ( ctr->ctx )
862                 talloc_destroy(ctr->ctx);
863
864         ZERO_STRUCTP(ctr);
865
866         return;
867 }
868
869 /***********************************************************************
870  **********************************************************************/
871
872 static TALLOC_CTX* notify_ctr_getctx( SPOOLSS_NOTIFY_MSG_CTR *ctr )
873 {
874         if ( !ctr )
875                 return NULL;
876
877         return ctr->ctx;
878 }
879
880 /***********************************************************************
881  **********************************************************************/
882
883 static SPOOLSS_NOTIFY_MSG_GROUP* notify_ctr_getgroup( SPOOLSS_NOTIFY_MSG_CTR *ctr, uint32_t idx )
884 {
885         if ( !ctr || !ctr->msg_groups )
886                 return NULL;
887
888         if ( idx >= ctr->num_groups )
889                 return NULL;
890
891         return &ctr->msg_groups[idx];
892
893 }
894
895 /***********************************************************************
896  How many groups of change messages do we have ?
897  **********************************************************************/
898
899 static int notify_msg_ctr_numgroups( SPOOLSS_NOTIFY_MSG_CTR *ctr )
900 {
901         if ( !ctr )
902                 return 0;
903
904         return ctr->num_groups;
905 }
906
907 /***********************************************************************
908  Add a SPOOLSS_NOTIFY_MSG_CTR to the correct group
909  **********************************************************************/
910
911 static int notify_msg_ctr_addmsg( SPOOLSS_NOTIFY_MSG_CTR *ctr, SPOOLSS_NOTIFY_MSG *msg )
912 {
913         SPOOLSS_NOTIFY_MSG_GROUP        *groups = NULL;
914         SPOOLSS_NOTIFY_MSG_GROUP        *msg_grp = NULL;
915         SPOOLSS_NOTIFY_MSG              *msg_list = NULL;
916         int                             i, new_slot;
917
918         if ( !ctr || !msg )
919                 return 0;
920
921         /* loop over all groups looking for a matching printer name */
922
923         for ( i=0; i<ctr->num_groups; i++ ) {
924                 if ( strcmp(ctr->msg_groups[i].printername, msg->printer) == 0 )
925                         break;
926         }
927
928         /* add a new group? */
929
930         if ( i == ctr->num_groups ) {
931                 ctr->num_groups++;
932
933                 if ( !(groups = TALLOC_REALLOC_ARRAY( ctr->ctx, ctr->msg_groups, SPOOLSS_NOTIFY_MSG_GROUP, ctr->num_groups)) ) {
934                         DEBUG(0,("notify_msg_ctr_addmsg: talloc_realloc() failed!\n"));
935                         return 0;
936                 }
937                 ctr->msg_groups = groups;
938
939                 /* clear the new entry and set the printer name */
940
941                 ZERO_STRUCT( ctr->msg_groups[ctr->num_groups-1] );
942                 fstrcpy( ctr->msg_groups[ctr->num_groups-1].printername, msg->printer );
943         }
944
945         /* add the change messages; 'i' is the correct index now regardless */
946
947         msg_grp = &ctr->msg_groups[i];
948
949         msg_grp->num_msgs++;
950
951         if ( !(msg_list = TALLOC_REALLOC_ARRAY( ctr->ctx, msg_grp->msgs, SPOOLSS_NOTIFY_MSG, msg_grp->num_msgs )) ) {
952                 DEBUG(0,("notify_msg_ctr_addmsg: talloc_realloc() failed for new message [%d]!\n", msg_grp->num_msgs));
953                 return 0;
954         }
955         msg_grp->msgs = msg_list;
956
957         new_slot = msg_grp->num_msgs-1;
958         memcpy( &msg_grp->msgs[new_slot], msg, sizeof(SPOOLSS_NOTIFY_MSG) );
959
960         /* need to allocate own copy of data */
961
962         if ( msg->len != 0 )
963                 msg_grp->msgs[new_slot].notify.data = (char *)
964                         TALLOC_MEMDUP( ctr->ctx, msg->notify.data, msg->len );
965
966         return ctr->num_groups;
967 }
968
969 static void construct_info_data(struct spoolss_Notify *info_data,
970                                 enum spoolss_NotifyType type,
971                                 uint16_t field, int id);
972
973 /***********************************************************************
974  Send a change notication message on all handles which have a call
975  back registered
976  **********************************************************************/
977
978 static void send_notify2_changes( SPOOLSS_NOTIFY_MSG_CTR *ctr, uint32_t idx )
979 {
980         Printer_entry            *p;
981         TALLOC_CTX               *mem_ctx = notify_ctr_getctx( ctr );
982         SPOOLSS_NOTIFY_MSG_GROUP *msg_group = notify_ctr_getgroup( ctr, idx );
983         SPOOLSS_NOTIFY_MSG       *messages;
984         int                      sending_msg_count;
985
986         if ( !msg_group ) {
987                 DEBUG(5,("send_notify2_changes() called with no msg group!\n"));
988                 return;
989         }
990
991         messages = msg_group->msgs;
992
993         if ( !messages ) {
994                 DEBUG(5,("send_notify2_changes() called with no messages!\n"));
995                 return;
996         }
997
998         DEBUG(8,("send_notify2_changes: Enter...[%s]\n", msg_group->printername));
999
1000         /* loop over all printers */
1001
1002         for (p = printers_list; p; p = p->next) {
1003                 struct spoolss_Notify *notifies;
1004                 uint32_t count = 0;
1005                 uint32_t id;
1006                 int     i;
1007
1008                 /* Is there notification on this handle? */
1009
1010                 if ( !p->notify.client_connected )
1011                         continue;
1012
1013                 DEBUG(10,("Client connected! [\\\\%s\\%s]\n", p->servername, p->sharename));
1014
1015                 /* For this printer?  Print servers always receive
1016                    notifications. */
1017
1018                 if ( ( p->printer_type == SPLHND_PRINTER )  &&
1019                     ( !strequal(msg_group->printername, p->sharename) ) )
1020                         continue;
1021
1022                 DEBUG(10,("Our printer\n"));
1023
1024                 /* allocate the max entries possible */
1025
1026                 notifies = TALLOC_ZERO_ARRAY(mem_ctx, struct spoolss_Notify, msg_group->num_msgs);
1027                 if (!notifies) {
1028                         return;
1029                 }
1030
1031                 /* build the array of change notifications */
1032
1033                 sending_msg_count = 0;
1034
1035                 for ( i=0; i<msg_group->num_msgs; i++ ) {
1036                         SPOOLSS_NOTIFY_MSG      *msg = &messages[i];
1037
1038                         /* Are we monitoring this event? */
1039
1040                         if (!is_monitoring_event(p, msg->type, msg->field))
1041                                 continue;
1042
1043                         sending_msg_count++;
1044
1045
1046                         DEBUG(10,("process_notify2_message: Sending message type [0x%x] field [0x%2x] for printer [%s]\n",
1047                                 msg->type, msg->field, p->sharename));
1048
1049                         /*
1050                          * if the is a printer notification handle and not a job notification
1051                          * type, then set the id to 0.  Other wise just use what was specified
1052                          * in the message.
1053                          *
1054                          * When registering change notification on a print server handle
1055                          * we always need to send back the id (snum) matching the printer
1056                          * for which the change took place.  For change notify registered
1057                          * on a printer handle, this does not matter and the id should be 0.
1058                          *
1059                          * --jerry
1060                          */
1061
1062                         if ( ( p->printer_type == SPLHND_PRINTER ) && ( msg->type == PRINTER_NOTIFY_TYPE ) )
1063                                 id = 0;
1064                         else
1065                                 id = msg->id;
1066
1067
1068                         /* Convert unix jobid to smb jobid */
1069
1070                         if (msg->flags & SPOOLSS_NOTIFY_MSG_UNIX_JOBID) {
1071                                 id = sysjob_to_jobid(msg->id);
1072
1073                                 if (id == -1) {
1074                                         DEBUG(3, ("no such unix jobid %d\n", msg->id));
1075                                         goto done;
1076                                 }
1077                         }
1078
1079                         construct_info_data( &notifies[count], msg->type, msg->field, id );
1080
1081                         switch(msg->type) {
1082                         case PRINTER_NOTIFY_TYPE:
1083                                 if ( printer_notify_table[msg->field].fn )
1084                                         printer_notify_table[msg->field].fn(msg, &notifies[count], mem_ctx);
1085                                 break;
1086
1087                         case JOB_NOTIFY_TYPE:
1088                                 if ( job_notify_table[msg->field].fn )
1089                                         job_notify_table[msg->field].fn(msg, &notifies[count], mem_ctx);
1090                                 break;
1091
1092                         default:
1093                                 DEBUG(5, ("Unknown notification type %d\n", msg->type));
1094                                 goto done;
1095                         }
1096
1097                         count++;
1098                 }
1099
1100                 if ( sending_msg_count ) {
1101                         NTSTATUS status;
1102                         WERROR werr;
1103                         union spoolss_ReplyPrinterInfo info;
1104                         struct spoolss_NotifyInfo info0;
1105                         uint32_t reply_result;
1106
1107                         info0.version   = 0x2;
1108                         info0.flags     = count ? 0x00020000 /* ??? */ : PRINTER_NOTIFY_INFO_DISCARDED;
1109                         info0.count     = count;
1110                         info0.notifies  = notifies;
1111
1112                         info.info0 = &info0;
1113
1114                         status = rpccli_spoolss_RouterReplyPrinterEx(notify_cli_pipe, mem_ctx,
1115                                                                      &p->notify.client_hnd,
1116                                                                      p->notify.change, /* color */
1117                                                                      p->notify.flags,
1118                                                                      &reply_result,
1119                                                                      0, /* reply_type, must be 0 */
1120                                                                      info,
1121                                                                      &werr);
1122                         if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(werr)) {
1123                                 DEBUG(1,("RouterReplyPrinterEx to client: %s failed: %s\n",
1124                                         notify_cli_pipe->srv_name_slash,
1125                                         win_errstr(werr)));
1126                         }
1127                         switch (reply_result) {
1128                                 case 0:
1129                                         break;
1130                                 case PRINTER_NOTIFY_INFO_DISCARDED:
1131                                 case PRINTER_NOTIFY_INFO_DISCARDNOTED:
1132                                 case PRINTER_NOTIFY_INFO_COLOR_MISMATCH:
1133                                         break;
1134                                 default:
1135                                         break;
1136                         }
1137                 }
1138         }
1139
1140 done:
1141         DEBUG(8,("send_notify2_changes: Exit...\n"));
1142         return;
1143 }
1144
1145 /***********************************************************************
1146  **********************************************************************/
1147
1148 static bool notify2_unpack_msg( SPOOLSS_NOTIFY_MSG *msg, struct timeval *tv, void *buf, size_t len )
1149 {
1150
1151         uint32_t tv_sec, tv_usec;
1152         size_t offset = 0;
1153
1154         /* Unpack message */
1155
1156         offset += tdb_unpack((uint8_t *)buf + offset, len - offset, "f",
1157                              msg->printer);
1158
1159         offset += tdb_unpack((uint8_t *)buf + offset, len - offset, "ddddddd",
1160                                 &tv_sec, &tv_usec,
1161                                 &msg->type, &msg->field, &msg->id, &msg->len, &msg->flags);
1162
1163         if (msg->len == 0)
1164                 tdb_unpack((uint8_t *)buf + offset, len - offset, "dd",
1165                            &msg->notify.value[0], &msg->notify.value[1]);
1166         else
1167                 tdb_unpack((uint8_t *)buf + offset, len - offset, "B",
1168                            &msg->len, &msg->notify.data);
1169
1170         DEBUG(3, ("notify2_unpack_msg: got NOTIFY2 message for printer %s, jobid %u type %d, field 0x%02x, flags 0x%04x\n",
1171                   msg->printer, (unsigned int)msg->id, msg->type, msg->field, msg->flags));
1172
1173         tv->tv_sec = tv_sec;
1174         tv->tv_usec = tv_usec;
1175
1176         if (msg->len == 0)
1177                 DEBUG(3, ("notify2_unpack_msg: value1 = %d, value2 = %d\n", msg->notify.value[0],
1178                           msg->notify.value[1]));
1179         else
1180                 dump_data(3, (uint8_t *)msg->notify.data, msg->len);
1181
1182         return true;
1183 }
1184
1185 /********************************************************************
1186  Receive a notify2 message list
1187  ********************************************************************/
1188
1189 static void receive_notify2_message_list(struct messaging_context *msg,
1190                                          void *private_data,
1191                                          uint32_t msg_type,
1192                                          struct server_id server_id,
1193                                          DATA_BLOB *data)
1194 {
1195         size_t                  msg_count, i;
1196         char                    *buf = (char *)data->data;
1197         char                    *msg_ptr;
1198         size_t                  msg_len;
1199         SPOOLSS_NOTIFY_MSG      notify;
1200         SPOOLSS_NOTIFY_MSG_CTR  messages;
1201         int                     num_groups;
1202
1203         if (data->length < 4) {
1204                 DEBUG(0,("receive_notify2_message_list: bad message format (len < 4)!\n"));
1205                 return;
1206         }
1207
1208         msg_count = IVAL(buf, 0);
1209         msg_ptr = buf + 4;
1210
1211         DEBUG(5, ("receive_notify2_message_list: got %lu messages in list\n", (unsigned long)msg_count));
1212
1213         if (msg_count == 0) {
1214                 DEBUG(0,("receive_notify2_message_list: bad message format (msg_count == 0) !\n"));
1215                 return;
1216         }
1217
1218         /* initialize the container */
1219
1220         ZERO_STRUCT( messages );
1221         notify_msg_ctr_init( &messages );
1222
1223         /*
1224          * build message groups for each printer identified
1225          * in a change_notify msg.  Remember that a PCN message
1226          * includes the handle returned for the srv_spoolss_replyopenprinter()
1227          * call.  Therefore messages are grouped according to printer handle.
1228          */
1229
1230         for ( i=0; i<msg_count; i++ ) {
1231                 struct timeval msg_tv;
1232
1233                 if (msg_ptr + 4 - buf > data->length) {
1234                         DEBUG(0,("receive_notify2_message_list: bad message format (len > buf_size) !\n"));
1235                         return;
1236                 }
1237
1238                 msg_len = IVAL(msg_ptr,0);
1239                 msg_ptr += 4;
1240
1241                 if (msg_ptr + msg_len - buf > data->length) {
1242                         DEBUG(0,("receive_notify2_message_list: bad message format (bad len) !\n"));
1243                         return;
1244                 }
1245
1246                 /* unpack messages */
1247
1248                 ZERO_STRUCT( notify );
1249                 notify2_unpack_msg( &notify, &msg_tv, msg_ptr, msg_len );
1250                 msg_ptr += msg_len;
1251
1252                 /* add to correct list in container */
1253
1254                 notify_msg_ctr_addmsg( &messages, &notify );
1255
1256                 /* free memory that might have been allocated by notify2_unpack_msg() */
1257
1258                 if ( notify.len != 0 )
1259                         SAFE_FREE( notify.notify.data );
1260         }
1261
1262         /* process each group of messages */
1263
1264         num_groups = notify_msg_ctr_numgroups( &messages );
1265         for ( i=0; i<num_groups; i++ )
1266                 send_notify2_changes( &messages, i );
1267
1268
1269         /* cleanup */
1270
1271         DEBUG(10,("receive_notify2_message_list: processed %u messages\n",
1272                 (uint32_t)msg_count ));
1273
1274         notify_msg_ctr_destroy( &messages );
1275
1276         return;
1277 }
1278
1279 /********************************************************************
1280  Send a message to ourself about new driver being installed
1281  so we can upgrade the information for each printer bound to this
1282  driver
1283  ********************************************************************/
1284
1285 static bool srv_spoolss_drv_upgrade_printer(const char *drivername,
1286                                             struct messaging_context *msg_ctx)
1287 {
1288         int len = strlen(drivername);
1289
1290         if (!len)
1291                 return false;
1292
1293         DEBUG(10,("srv_spoolss_drv_upgrade_printer: Sending message about driver upgrade [%s]\n",
1294                 drivername));
1295
1296         messaging_send_buf(msg_ctx, messaging_server_id(msg_ctx),
1297                            MSG_PRINTER_DRVUPGRADE,
1298                            (uint8_t *)drivername, len+1);
1299
1300         return true;
1301 }
1302
1303 /**********************************************************************
1304  callback to receive a MSG_PRINTER_DRVUPGRADE message and interate
1305  over all printers, upgrading ones as necessary
1306  **********************************************************************/
1307
1308 void do_drv_upgrade_printer(struct messaging_context *msg,
1309                             void *private_data,
1310                             uint32_t msg_type,
1311                             struct server_id server_id,
1312                             DATA_BLOB *data)
1313 {
1314         TALLOC_CTX *tmp_ctx;
1315         struct auth_serversupplied_info *server_info = NULL;
1316         struct spoolss_PrinterInfo2 *pinfo2;
1317         NTSTATUS status;
1318         WERROR result;
1319         const char *drivername;
1320         int snum;
1321         int n_services = lp_numservices();
1322         size_t len;
1323
1324         tmp_ctx = talloc_new(NULL);
1325         if (!tmp_ctx) return;
1326
1327         status = make_server_info_system(tmp_ctx, &server_info);
1328         if (!NT_STATUS_IS_OK(status)) {
1329                 DEBUG(0, ("do_drv_upgrade_printer: "
1330                           "Could not create system server_info\n"));
1331                 goto done;
1332         }
1333
1334         len = MIN(data->length,sizeof(drivername)-1);
1335         drivername = talloc_strndup(tmp_ctx, (const char *)data->data, len);
1336         if (!drivername) {
1337                 DEBUG(0, ("do_drv_upgrade_printer: Out of memoery ?!\n"));
1338                 goto done;
1339         }
1340
1341         DEBUG(10, ("do_drv_upgrade_printer: "
1342                    "Got message for new driver [%s]\n", drivername));
1343
1344         /* Iterate the printer list */
1345
1346         for (snum = 0; snum < n_services; snum++) {
1347                 if (!lp_snum_ok(snum) || !lp_print_ok(snum)) {
1348                         continue;
1349                 }
1350
1351                 result = winreg_get_printer(tmp_ctx, server_info, msg,
1352                                             NULL,
1353                                             lp_const_servicename(snum),
1354                                             &pinfo2);
1355
1356                 if (!W_ERROR_IS_OK(result)) {
1357                         continue;
1358                 }
1359
1360                 if (!pinfo2->drivername) {
1361                         continue;
1362                 }
1363
1364                 if (strcmp(drivername, pinfo2->drivername) != 0) {
1365                         continue;
1366                 }
1367
1368                 DEBUG(6,("Updating printer [%s]\n", pinfo2->printername));
1369
1370                 /* all we care about currently is the change_id */
1371                 result = winreg_printer_update_changeid(tmp_ctx,
1372                                                         server_info,
1373                                                         msg,
1374                                                         pinfo2->printername);
1375
1376                 if (!W_ERROR_IS_OK(result)) {
1377                         DEBUG(3, ("do_drv_upgrade_printer: "
1378                                   "Failed to update changeid [%s]\n",
1379                                   win_errstr(result)));
1380                 }
1381         }
1382
1383         /* all done */
1384 done:
1385         talloc_free(tmp_ctx);
1386 }
1387
1388 /********************************************************************
1389  Update the cache for all printq's with a registered client
1390  connection
1391  ********************************************************************/
1392
1393 void update_monitored_printq_cache( void )
1394 {
1395         Printer_entry *printer = printers_list;
1396         int snum;
1397
1398         /* loop through all printers and update the cache where
1399            client_connected == true */
1400         while ( printer )
1401         {
1402                 if ( (printer->printer_type == SPLHND_PRINTER)
1403                         && printer->notify.client_connected )
1404                 {
1405                         snum = print_queue_snum(printer->sharename);
1406                         print_queue_status( snum, NULL, NULL );
1407                 }
1408
1409                 printer = printer->next;
1410         }
1411
1412         return;
1413 }
1414
1415 /****************************************************************
1416  _spoolss_OpenPrinter
1417 ****************************************************************/
1418
1419 WERROR _spoolss_OpenPrinter(struct pipes_struct *p,
1420                             struct spoolss_OpenPrinter *r)
1421 {
1422         struct spoolss_OpenPrinterEx e;
1423         WERROR werr;
1424
1425         ZERO_STRUCT(e.in.userlevel);
1426
1427         e.in.printername        = r->in.printername;
1428         e.in.datatype           = r->in.datatype;
1429         e.in.devmode_ctr        = r->in.devmode_ctr;
1430         e.in.access_mask        = r->in.access_mask;
1431         e.in.level              = 0;
1432
1433         e.out.handle            = r->out.handle;
1434
1435         werr = _spoolss_OpenPrinterEx(p, &e);
1436
1437         if (W_ERROR_EQUAL(werr, WERR_INVALID_PARAM)) {
1438                 /* OpenPrinterEx returns this for a bad
1439                  * printer name. We must return WERR_INVALID_PRINTER_NAME
1440                  * instead.
1441                  */
1442                 werr = WERR_INVALID_PRINTER_NAME;
1443         }
1444
1445         return werr;
1446 }
1447
1448 static WERROR copy_devicemode(TALLOC_CTX *mem_ctx,
1449                               struct spoolss_DeviceMode *orig,
1450                               struct spoolss_DeviceMode **dest)
1451 {
1452         struct spoolss_DeviceMode *dm;
1453
1454         dm = talloc(mem_ctx, struct spoolss_DeviceMode);
1455         if (!dm) {
1456                 return WERR_NOMEM;
1457         }
1458
1459         /* copy all values, then duplicate strings and structs */
1460         *dm = *orig;
1461
1462         dm->devicename = talloc_strdup(dm, orig->devicename);
1463         if (!dm->devicename) {
1464                 return WERR_NOMEM;
1465         }
1466         dm->formname = talloc_strdup(dm, orig->formname);
1467         if (!dm->formname) {
1468                 return WERR_NOMEM;
1469         }
1470         if (orig->driverextra_data.data) {
1471                 dm->driverextra_data.data =
1472                         (uint8_t *) talloc_memdup(dm, orig->driverextra_data.data,
1473                                         orig->driverextra_data.length);
1474                 if (!dm->driverextra_data.data) {
1475                         return WERR_NOMEM;
1476                 }
1477         }
1478
1479         *dest = dm;
1480         return WERR_OK;
1481 }
1482
1483 /****************************************************************
1484  _spoolss_OpenPrinterEx
1485 ****************************************************************/
1486
1487 WERROR _spoolss_OpenPrinterEx(struct pipes_struct *p,
1488                               struct spoolss_OpenPrinterEx *r)
1489 {
1490         int snum;
1491         Printer_entry *Printer=NULL;
1492
1493         if (!r->in.printername) {
1494                 return WERR_INVALID_PARAM;
1495         }
1496
1497         /* some sanity check because you can open a printer or a print server */
1498         /* aka: \\server\printer or \\server */
1499
1500         DEBUGADD(3,("checking name: %s\n", r->in.printername));
1501
1502         if (!open_printer_hnd(p, r->out.handle, r->in.printername, 0)) {
1503                 DEBUG(0,("_spoolss_OpenPrinterEx: Cannot open a printer handle "
1504                         " for printer %s\n", r->in.printername));
1505                 ZERO_STRUCTP(r->out.handle);
1506                 return WERR_INVALID_PARAM;
1507         }
1508
1509         Printer = find_printer_index_by_hnd(p, r->out.handle);
1510         if ( !Printer ) {
1511                 DEBUG(0,("_spoolss_OpenPrinterEx: logic error.  Can't find printer "
1512                         "handle we created for printer %s\n", r->in.printername));
1513                 close_printer_handle(p, r->out.handle);
1514                 ZERO_STRUCTP(r->out.handle);
1515                 return WERR_INVALID_PARAM;
1516         }
1517
1518         /*
1519          * First case: the user is opening the print server:
1520          *
1521          * Disallow MS AddPrinterWizard if parameter disables it. A Win2k
1522          * client 1st tries an OpenPrinterEx with access==0, MUST be allowed.
1523          *
1524          * Then both Win2k and WinNT clients try an OpenPrinterEx with
1525          * SERVER_ALL_ACCESS, which we allow only if the user is root (uid=0)
1526          * or if the user is listed in the smb.conf printer admin parameter.
1527          *
1528          * Then they try OpenPrinterEx with SERVER_READ which we allow. This lets the
1529          * client view printer folder, but does not show the MSAPW.
1530          *
1531          * Note: this test needs code to check access rights here too. Jeremy
1532          * could you look at this?
1533          *
1534          * Second case: the user is opening a printer:
1535          * NT doesn't let us connect to a printer if the connecting user
1536          * doesn't have print permission.
1537          *
1538          * Third case: user is opening a Port Monitor
1539          * access checks same as opening a handle to the print server.
1540          */
1541
1542         switch (Printer->printer_type )
1543         {
1544         case SPLHND_SERVER:
1545         case SPLHND_PORTMON_TCP:
1546         case SPLHND_PORTMON_LOCAL:
1547                 /* Printserver handles use global struct... */
1548
1549                 snum = -1;
1550
1551                 /* Map standard access rights to object specific access rights */
1552
1553                 se_map_standard(&r->in.access_mask,
1554                                 &printserver_std_mapping);
1555
1556                 /* Deny any object specific bits that don't apply to print
1557                    servers (i.e printer and job specific bits) */
1558
1559                 r->in.access_mask &= SEC_MASK_SPECIFIC;
1560
1561                 if (r->in.access_mask &
1562                     ~(SERVER_ACCESS_ADMINISTER | SERVER_ACCESS_ENUMERATE)) {
1563                         DEBUG(3, ("access DENIED for non-printserver bits\n"));
1564                         close_printer_handle(p, r->out.handle);
1565                         ZERO_STRUCTP(r->out.handle);
1566                         return WERR_ACCESS_DENIED;
1567                 }
1568
1569                 /* Allow admin access */
1570
1571                 if ( r->in.access_mask & SERVER_ACCESS_ADMINISTER )
1572                 {
1573                         SE_PRIV se_printop = SE_PRINT_OPERATOR;
1574
1575                         if (!lp_ms_add_printer_wizard()) {
1576                                 close_printer_handle(p, r->out.handle);
1577                                 ZERO_STRUCTP(r->out.handle);
1578                                 return WERR_ACCESS_DENIED;
1579                         }
1580
1581                         /* if the user is not root, doesn't have SE_PRINT_OPERATOR privilege,
1582                            and not a printer admin, then fail */
1583
1584                         if ((p->server_info->utok.uid != sec_initial_uid()) &&
1585                             !user_has_privileges(p->server_info->ptok,
1586                                                  &se_printop ) &&
1587                             !token_contains_name_in_list(
1588                                     uidtoname(p->server_info->utok.uid),
1589                                     p->server_info->info3->base.domain.string,
1590                                     NULL,
1591                                     p->server_info->ptok,
1592                                     lp_printer_admin(snum))) {
1593                                 close_printer_handle(p, r->out.handle);
1594                                 ZERO_STRUCTP(r->out.handle);
1595                                 return WERR_ACCESS_DENIED;
1596                         }
1597
1598                         r->in.access_mask = SERVER_ACCESS_ADMINISTER;
1599                 }
1600                 else
1601                 {
1602                         r->in.access_mask = SERVER_ACCESS_ENUMERATE;
1603                 }
1604
1605                 DEBUG(4,("Setting print server access = %s\n", (r->in.access_mask == SERVER_ACCESS_ADMINISTER)
1606                         ? "SERVER_ACCESS_ADMINISTER" : "SERVER_ACCESS_ENUMERATE" ));
1607
1608                 /* We fall through to return WERR_OK */
1609                 break;
1610
1611         case SPLHND_PRINTER:
1612                 /* NT doesn't let us connect to a printer if the connecting user
1613                    doesn't have print permission.  */
1614
1615                 if (!get_printer_snum(p, r->out.handle, &snum, NULL)) {
1616                         close_printer_handle(p, r->out.handle);
1617                         ZERO_STRUCTP(r->out.handle);
1618                         return WERR_BADFID;
1619                 }
1620
1621                 if (r->in.access_mask == SEC_FLAG_MAXIMUM_ALLOWED) {
1622                         r->in.access_mask = PRINTER_ACCESS_ADMINISTER;
1623                 }
1624
1625                 se_map_standard(&r->in.access_mask, &printer_std_mapping);
1626
1627                 /* map an empty access mask to the minimum access mask */
1628                 if (r->in.access_mask == 0x0)
1629                         r->in.access_mask = PRINTER_ACCESS_USE;
1630
1631                 /*
1632                  * If we are not serving the printer driver for this printer,
1633                  * map PRINTER_ACCESS_ADMINISTER to PRINTER_ACCESS_USE.  This
1634                  * will keep NT clients happy  --jerry
1635                  */
1636
1637                 if (lp_use_client_driver(snum)
1638                         && (r->in.access_mask & PRINTER_ACCESS_ADMINISTER))
1639                 {
1640                         r->in.access_mask = PRINTER_ACCESS_USE;
1641                 }
1642
1643                 /* check smb.conf parameters and the the sec_desc */
1644
1645                 if ( !check_access(get_client_fd(), lp_hostsallow(snum), lp_hostsdeny(snum)) ) {
1646                         DEBUG(3, ("access DENIED (hosts allow/deny) for printer open\n"));
1647                         ZERO_STRUCTP(r->out.handle);
1648                         return WERR_ACCESS_DENIED;
1649                 }
1650
1651                 if (!user_ok_token(uidtoname(p->server_info->utok.uid), NULL,
1652                                    p->server_info->ptok, snum) ||
1653                     !print_access_check(p->server_info, p->msg_ctx, snum,
1654                                         r->in.access_mask)) {
1655                         DEBUG(3, ("access DENIED for printer open\n"));
1656                         close_printer_handle(p, r->out.handle);
1657                         ZERO_STRUCTP(r->out.handle);
1658                         return WERR_ACCESS_DENIED;
1659                 }
1660
1661                 if ((r->in.access_mask & SEC_MASK_SPECIFIC)& ~(PRINTER_ACCESS_ADMINISTER|PRINTER_ACCESS_USE)) {
1662                         DEBUG(3, ("access DENIED for printer open - unknown bits\n"));
1663                         close_printer_handle(p, r->out.handle);
1664                         ZERO_STRUCTP(r->out.handle);
1665                         return WERR_ACCESS_DENIED;
1666                 }
1667
1668                 if (r->in.access_mask & PRINTER_ACCESS_ADMINISTER)
1669                         r->in.access_mask = PRINTER_ACCESS_ADMINISTER;
1670                 else
1671                         r->in.access_mask = PRINTER_ACCESS_USE;
1672
1673                 DEBUG(4,("Setting printer access = %s\n", (r->in.access_mask == PRINTER_ACCESS_ADMINISTER)
1674                         ? "PRINTER_ACCESS_ADMINISTER" : "PRINTER_ACCESS_USE" ));
1675
1676                 winreg_create_printer(p->mem_ctx,
1677                                       p->server_info,
1678                                       p->msg_ctx,
1679                                       Printer->servername,
1680                                       lp_const_servicename(snum));
1681
1682                 break;
1683
1684         default:
1685                 /* sanity check to prevent programmer error */
1686                 ZERO_STRUCTP(r->out.handle);
1687                 return WERR_BADFID;
1688         }
1689
1690         Printer->access_granted = r->in.access_mask;
1691
1692         /*
1693          * If the client sent a devmode in the OpenPrinter() call, then
1694          * save it here in case we get a job submission on this handle
1695          */
1696
1697          if ((Printer->printer_type != SPLHND_SERVER) &&
1698              r->in.devmode_ctr.devmode) {
1699                 copy_devicemode(NULL, r->in.devmode_ctr.devmode,
1700                                 &Printer->devmode);
1701          }
1702
1703 #if 0   /* JERRY -- I'm doubtful this is really effective */
1704         /* HACK ALERT!!! Sleep for 1/3 of a second to try trigger a LAN/WAN
1705            optimization in Windows 2000 clients  --jerry */
1706
1707         if ( (r->in.access_mask == PRINTER_ACCESS_ADMINISTER)
1708                 && (RA_WIN2K == get_remote_arch()) )
1709         {
1710                 DEBUG(10,("_spoolss_OpenPrinterEx: Enabling LAN/WAN hack for Win2k clients.\n"));
1711                 sys_usleep( 500000 );
1712         }
1713 #endif
1714
1715         return WERR_OK;
1716 }
1717
1718 /****************************************************************
1719  _spoolss_ClosePrinter
1720 ****************************************************************/
1721
1722 WERROR _spoolss_ClosePrinter(struct pipes_struct *p,
1723                              struct spoolss_ClosePrinter *r)
1724 {
1725         Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
1726
1727         if (Printer && Printer->document_started) {
1728                 struct spoolss_EndDocPrinter e;
1729
1730                 e.in.handle = r->in.handle;
1731
1732                 _spoolss_EndDocPrinter(p, &e);
1733         }
1734
1735         if (!close_printer_handle(p, r->in.handle))
1736                 return WERR_BADFID;
1737
1738         /* clear the returned printer handle.  Observed behavior
1739            from Win2k server.  Don't think this really matters.
1740            Previous code just copied the value of the closed
1741            handle.    --jerry */
1742
1743         ZERO_STRUCTP(r->out.handle);
1744
1745         return WERR_OK;
1746 }
1747
1748 /****************************************************************
1749  _spoolss_DeletePrinter
1750 ****************************************************************/
1751
1752 WERROR _spoolss_DeletePrinter(struct pipes_struct *p,
1753                               struct spoolss_DeletePrinter *r)
1754 {
1755         Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
1756         WERROR result;
1757         int snum;
1758
1759         if (Printer && Printer->document_started) {
1760                 struct spoolss_EndDocPrinter e;
1761
1762                 e.in.handle = r->in.handle;
1763
1764                 _spoolss_EndDocPrinter(p, &e);
1765         }
1766
1767         if (get_printer_snum(p, r->in.handle, &snum, NULL)) {
1768                 winreg_delete_printer_key(p->mem_ctx,
1769                                           p->server_info,
1770                                           p->msg_ctx,
1771                                           lp_const_servicename(snum),
1772                                           "");
1773         }
1774
1775         result = delete_printer_handle(p, r->in.handle);
1776
1777         return result;
1778 }
1779
1780 /*******************************************************************
1781  * static function to lookup the version id corresponding to an
1782  * long architecture string
1783  ******************************************************************/
1784
1785 static const struct print_architecture_table_node archi_table[]= {
1786
1787         {"Windows 4.0",          SPL_ARCH_WIN40,        0 },
1788         {"Windows NT x86",       SPL_ARCH_W32X86,       2 },
1789         {"Windows NT R4000",     SPL_ARCH_W32MIPS,      2 },
1790         {"Windows NT Alpha_AXP", SPL_ARCH_W32ALPHA,     2 },
1791         {"Windows NT PowerPC",   SPL_ARCH_W32PPC,       2 },
1792         {"Windows IA64",         SPL_ARCH_IA64,         3 },
1793         {"Windows x64",          SPL_ARCH_X64,          3 },
1794         {NULL,                   "",            -1 }
1795 };
1796
1797 static int get_version_id(const char *arch)
1798 {
1799         int i;
1800
1801         for (i=0; archi_table[i].long_archi != NULL; i++)
1802         {
1803                 if (strcmp(arch, archi_table[i].long_archi) == 0)
1804                         return (archi_table[i].version);
1805         }
1806
1807         return -1;
1808 }
1809
1810 /****************************************************************
1811  _spoolss_DeletePrinterDriver
1812 ****************************************************************/
1813
1814 WERROR _spoolss_DeletePrinterDriver(struct pipes_struct *p,
1815                                     struct spoolss_DeletePrinterDriver *r)
1816 {
1817
1818         struct spoolss_DriverInfo8 *info = NULL;
1819         struct spoolss_DriverInfo8 *info_win2k = NULL;
1820         int                             version;
1821         WERROR                          status;
1822         SE_PRIV                         se_printop = SE_PRINT_OPERATOR;
1823
1824         /* if the user is not root, doesn't have SE_PRINT_OPERATOR privilege,
1825            and not a printer admin, then fail */
1826
1827         if ( (p->server_info->utok.uid != sec_initial_uid())
1828                 && !user_has_privileges(p->server_info->ptok, &se_printop )
1829                 && !token_contains_name_in_list(
1830                         uidtoname(p->server_info->utok.uid),
1831                         p->server_info->info3->base.domain.string,
1832                         NULL,
1833                         p->server_info->ptok,
1834                         lp_printer_admin(-1)) )
1835         {
1836                 return WERR_ACCESS_DENIED;
1837         }
1838
1839         /* check that we have a valid driver name first */
1840
1841         if ((version = get_version_id(r->in.architecture)) == -1)
1842                 return WERR_INVALID_ENVIRONMENT;
1843
1844         status = winreg_get_driver(p->mem_ctx, p->server_info, p->msg_ctx,
1845                                    r->in.architecture, r->in.driver,
1846                                    version, &info);
1847         if (!W_ERROR_IS_OK(status)) {
1848                 /* try for Win2k driver if "Windows NT x86" */
1849
1850                 if ( version == 2 ) {
1851                         version = 3;
1852
1853                         status = winreg_get_driver(p->mem_ctx, p->server_info,
1854                                                    p->msg_ctx,
1855                                                    r->in.architecture,
1856                                                    r->in.driver,
1857                                                    version, &info);
1858                         if (!W_ERROR_IS_OK(status)) {
1859                                 status = WERR_UNKNOWN_PRINTER_DRIVER;
1860                                 goto done;
1861                         }
1862                 }
1863                 /* otherwise it was a failure */
1864                 else {
1865                         status = WERR_UNKNOWN_PRINTER_DRIVER;
1866                         goto done;
1867                 }
1868
1869         }
1870
1871         if (printer_driver_in_use(p->mem_ctx, p->server_info, p->msg_ctx,
1872                                   info)) {
1873                 status = WERR_PRINTER_DRIVER_IN_USE;
1874                 goto done;
1875         }
1876
1877         if (version == 2) {
1878                 status = winreg_get_driver(p->mem_ctx, p->server_info,
1879                                            p->msg_ctx,
1880                                            r->in.architecture,
1881                                            r->in.driver, 3, &info_win2k);
1882                 if (W_ERROR_IS_OK(status)) {
1883                         /* if we get to here, we now have 2 driver info structures to remove */
1884                         /* remove the Win2k driver first*/
1885
1886                         status = winreg_del_driver(p->mem_ctx,
1887                                                    p->server_info,
1888                                                    p->msg_ctx,
1889                                                    info_win2k, 3);
1890                         talloc_free(info_win2k);
1891
1892                         /* this should not have failed---if it did, report to client */
1893                         if (!W_ERROR_IS_OK(status)) {
1894                                 goto done;
1895                         }
1896                 }
1897         }
1898
1899         status = winreg_del_driver(p->mem_ctx, p->server_info, p->msg_ctx,
1900                                    info, version);
1901
1902 done:
1903         talloc_free(info);
1904
1905         return status;
1906 }
1907
1908 /****************************************************************
1909  _spoolss_DeletePrinterDriverEx
1910 ****************************************************************/
1911
1912 WERROR _spoolss_DeletePrinterDriverEx(struct pipes_struct *p,
1913                                       struct spoolss_DeletePrinterDriverEx *r)
1914 {
1915         struct spoolss_DriverInfo8      *info = NULL;
1916         struct spoolss_DriverInfo8      *info_win2k = NULL;
1917         int                             version;
1918         bool                            delete_files;
1919         WERROR                          status;
1920         SE_PRIV                         se_printop = SE_PRINT_OPERATOR;
1921
1922         /* if the user is not root, doesn't have SE_PRINT_OPERATOR privilege,
1923            and not a printer admin, then fail */
1924
1925         if ( (p->server_info->utok.uid != sec_initial_uid())
1926                 && !user_has_privileges(p->server_info->ptok, &se_printop )
1927                 && !token_contains_name_in_list(
1928                         uidtoname(p->server_info->utok.uid),
1929                         p->server_info->info3->base.domain.string,
1930                         NULL,
1931                         p->server_info->ptok, lp_printer_admin(-1)) )
1932         {
1933                 return WERR_ACCESS_DENIED;
1934         }
1935
1936         /* check that we have a valid driver name first */
1937         if ((version = get_version_id(r->in.architecture)) == -1) {
1938                 /* this is what NT returns */
1939                 return WERR_INVALID_ENVIRONMENT;
1940         }
1941
1942         if (r->in.delete_flags & DPD_DELETE_SPECIFIC_VERSION)
1943                 version = r->in.version;
1944
1945         status = winreg_get_driver(p->mem_ctx, p->server_info,
1946                                    p->msg_ctx, r->in.architecture,
1947                                    r->in.driver, version, &info);
1948         if (!W_ERROR_IS_OK(status)) {
1949                 status = WERR_UNKNOWN_PRINTER_DRIVER;
1950
1951                 /*
1952                  * if the client asked for a specific version,
1953                  * or this is something other than Windows NT x86,
1954                  * then we've failed
1955                  */
1956
1957                 if ( (r->in.delete_flags & DPD_DELETE_SPECIFIC_VERSION) || (version !=2) )
1958                         goto done;
1959
1960                 /* try for Win2k driver if "Windows NT x86" */
1961
1962                 version = 3;
1963                 status = winreg_get_driver(info, p->server_info, p->msg_ctx,
1964                                            r->in.architecture,
1965                                            r->in.driver,
1966                                            version, &info);
1967                 if (!W_ERROR_IS_OK(status)) {
1968                         status = WERR_UNKNOWN_PRINTER_DRIVER;
1969                         goto done;
1970                 }
1971         }
1972
1973         if (printer_driver_in_use(info, p->server_info, p->msg_ctx, info)) {
1974                 status = WERR_PRINTER_DRIVER_IN_USE;
1975                 goto done;
1976         }
1977
1978         /*
1979          * we have a couple of cases to consider.
1980          * (1) Are any files in use?  If so and DPD_DELTE_ALL_FILE is set,
1981          *     then the delete should fail if **any** files overlap with
1982          *     other drivers
1983          * (2) If DPD_DELTE_UNUSED_FILES is sert, then delete all
1984          *     non-overlapping files
1985          * (3) If neither DPD_DELTE_ALL_FILE nor DPD_DELTE_ALL_FILES
1986          *     is set, the do not delete any files
1987          * Refer to MSDN docs on DeletePrinterDriverEx() for details.
1988          */
1989
1990         delete_files = r->in.delete_flags & (DPD_DELETE_ALL_FILES|DPD_DELETE_UNUSED_FILES);
1991
1992         /* fail if any files are in use and DPD_DELETE_ALL_FILES is set */
1993
1994         if (delete_files &&
1995             (r->in.delete_flags & DPD_DELETE_ALL_FILES) &&
1996             printer_driver_files_in_use(info, p->server_info, p->msg_ctx,
1997                                         info)) {
1998                 /* no idea of the correct error here */
1999                 status = WERR_ACCESS_DENIED;
2000                 goto done;
2001         }
2002
2003
2004         /* also check for W32X86/3 if necessary; maybe we already have? */
2005
2006         if ( (version == 2) && ((r->in.delete_flags & DPD_DELETE_SPECIFIC_VERSION) != DPD_DELETE_SPECIFIC_VERSION)  ) {
2007                 status = winreg_get_driver(info, p->server_info, p->msg_ctx,
2008                                            r->in.architecture,
2009                                            r->in.driver, 3, &info_win2k);
2010                 if (W_ERROR_IS_OK(status)) {
2011
2012                         if (delete_files &&
2013                             (r->in.delete_flags & DPD_DELETE_ALL_FILES) &&
2014                             printer_driver_files_in_use(info, p->server_info,
2015                                                         p->msg_ctx,
2016                                                         info_win2k)) {
2017                                 /* no idea of the correct error here */
2018                                 talloc_free(info_win2k);
2019                                 status = WERR_ACCESS_DENIED;
2020                                 goto done;
2021                         }
2022
2023                         /* if we get to here, we now have 2 driver info structures to remove */
2024                         /* remove the Win2k driver first*/
2025
2026                         status = winreg_del_driver(info, p->server_info,
2027                                                    p->msg_ctx, info_win2k, 3);
2028
2029                         /* this should not have failed---if it did, report to client */
2030
2031                         if (!W_ERROR_IS_OK(status)) {
2032                                 goto done;
2033                         }
2034
2035                         /*
2036                          * now delete any associated files if delete_files is
2037                          * true. Even if this part failes, we return succes
2038                          * because the driver doesn not exist any more
2039                          */
2040                         if (delete_files) {
2041                                 delete_driver_files(p->server_info,
2042                                                     info_win2k);
2043                         }
2044                 }
2045         }
2046
2047         status = winreg_del_driver(info, p->server_info, p->msg_ctx, info,
2048                                    version);
2049         if (!W_ERROR_IS_OK(status)) {
2050                 goto done;
2051         }
2052
2053         /*
2054          * now delete any associated files if delete_files is
2055          * true. Even if this part failes, we return succes
2056          * because the driver doesn not exist any more
2057          */
2058         if (delete_files) {
2059                 delete_driver_files(p->server_info, info);
2060         }
2061
2062 done:
2063         talloc_free(info);
2064         return status;
2065 }
2066
2067
2068 /********************************************************************
2069  GetPrinterData on a printer server Handle.
2070 ********************************************************************/
2071
2072 static WERROR getprinterdata_printer_server(TALLOC_CTX *mem_ctx,
2073                                             const char *value,
2074                                             enum winreg_Type *type,
2075                                             union spoolss_PrinterData *data)
2076 {
2077         DEBUG(8,("getprinterdata_printer_server:%s\n", value));
2078
2079         if (!StrCaseCmp(value, "W3SvcInstalled")) {
2080                 *type = REG_DWORD;
2081                 data->value = 0x00;
2082                 return WERR_OK;
2083         }
2084
2085         if (!StrCaseCmp(value, "BeepEnabled")) {
2086                 *type = REG_DWORD;
2087                 data->value = 0x00;
2088                 return WERR_OK;
2089         }
2090
2091         if (!StrCaseCmp(value, "EventLog")) {
2092                 *type = REG_DWORD;
2093                 /* formally was 0x1b */
2094                 data->value = 0x00;
2095                 return WERR_OK;
2096         }
2097
2098         if (!StrCaseCmp(value, "NetPopup")) {
2099                 *type = REG_DWORD;
2100                 data->value = 0x00;
2101                 return WERR_OK;
2102         }
2103
2104         if (!StrCaseCmp(value, "MajorVersion")) {
2105                 *type = REG_DWORD;
2106
2107                 /* Windows NT 4.0 seems to not allow uploading of drivers
2108                    to a server that reports 0x3 as the MajorVersion.
2109                    need to investigate more how Win2k gets around this .
2110                    -- jerry */
2111
2112                 if (RA_WINNT == get_remote_arch()) {
2113                         data->value = 0x02;
2114                 } else {
2115                         data->value = 0x03;
2116                 }
2117
2118                 return WERR_OK;
2119         }
2120
2121         if (!StrCaseCmp(value, "MinorVersion")) {
2122                 *type = REG_DWORD;
2123                 data->value = 0x00;
2124                 return WERR_OK;
2125         }
2126
2127         /* REG_BINARY
2128          *  uint32_t size        = 0x114
2129          *  uint32_t major       = 5
2130          *  uint32_t minor       = [0|1]
2131          *  uint32_t build       = [2195|2600]
2132          *  extra unicode string = e.g. "Service Pack 3"
2133          */
2134         if (!StrCaseCmp(value, "OSVersion")) {
2135                 DATA_BLOB blob;
2136                 enum ndr_err_code ndr_err;
2137                 struct spoolss_OSVersion os;
2138
2139                 os.major                = 5;    /* Windows 2000 == 5.0 */
2140                 os.minor                = 0;
2141                 os.build                = 2195; /* build */
2142                 os.extra_string         = "";   /* leave extra string empty */
2143
2144                 ndr_err = ndr_push_struct_blob(&blob, mem_ctx, &os,
2145                         (ndr_push_flags_fn_t)ndr_push_spoolss_OSVersion);
2146                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2147                         return WERR_GENERAL_FAILURE;
2148                 }
2149
2150                 *type = REG_BINARY;
2151                 data->binary = blob;
2152
2153                 return WERR_OK;
2154         }
2155
2156
2157         if (!StrCaseCmp(value, "DefaultSpoolDirectory")) {
2158                 *type = REG_SZ;
2159
2160                 data->string = talloc_strdup(mem_ctx, "C:\\PRINTERS");
2161                 W_ERROR_HAVE_NO_MEMORY(data->string);
2162
2163                 return WERR_OK;
2164         }
2165
2166         if (!StrCaseCmp(value, "Architecture")) {
2167                 *type = REG_SZ;
2168                 data->string = talloc_strdup(mem_ctx,
2169                         lp_parm_const_string(GLOBAL_SECTION_SNUM, "spoolss", "architecture", SPOOLSS_ARCHITECTURE_NT_X86));
2170                 W_ERROR_HAVE_NO_MEMORY(data->string);
2171
2172                 return WERR_OK;
2173         }
2174
2175         if (!StrCaseCmp(value, "DsPresent")) {
2176                 *type = REG_DWORD;
2177
2178                 /* only show the publish check box if we are a
2179                    member of a AD domain */
2180
2181                 if (lp_security() == SEC_ADS) {
2182                         data->value = 0x01;
2183                 } else {
2184                         data->value = 0x00;
2185                 }
2186                 return WERR_OK;
2187         }
2188
2189         if (!StrCaseCmp(value, "DNSMachineName")) {
2190                 const char *hostname = get_mydnsfullname();
2191
2192                 if (!hostname) {
2193                         return WERR_BADFILE;
2194                 }
2195
2196                 *type = REG_SZ;
2197                 data->string = talloc_strdup(mem_ctx, hostname);
2198                 W_ERROR_HAVE_NO_MEMORY(data->string);
2199
2200                 return WERR_OK;
2201         }
2202
2203         *type = REG_NONE;
2204
2205         return WERR_INVALID_PARAM;
2206 }
2207
2208 /****************************************************************
2209  _spoolss_GetPrinterData
2210 ****************************************************************/
2211
2212 WERROR _spoolss_GetPrinterData(struct pipes_struct *p,
2213                                struct spoolss_GetPrinterData *r)
2214 {
2215         struct spoolss_GetPrinterDataEx r2;
2216
2217         r2.in.handle            = r->in.handle;
2218         r2.in.key_name          = "PrinterDriverData";
2219         r2.in.value_name        = r->in.value_name;
2220         r2.in.offered           = r->in.offered;
2221         r2.out.type             = r->out.type;
2222         r2.out.data             = r->out.data;
2223         r2.out.needed           = r->out.needed;
2224
2225         return _spoolss_GetPrinterDataEx(p, &r2);
2226 }
2227
2228 /*********************************************************
2229  Connect to the client machine.
2230 **********************************************************/
2231
2232 static bool spoolss_connect_to_client(struct rpc_pipe_client **pp_pipe,
2233                         struct sockaddr_storage *client_ss, const char *remote_machine)
2234 {
2235         NTSTATUS ret;
2236         struct cli_state *the_cli;
2237         struct sockaddr_storage rm_addr;
2238         char addr[INET6_ADDRSTRLEN];
2239
2240         if ( is_zero_addr((struct sockaddr *)client_ss) ) {
2241                 DEBUG(2,("spoolss_connect_to_client: resolving %s\n",
2242                         remote_machine));
2243                 if ( !resolve_name( remote_machine, &rm_addr, 0x20, false) ) {
2244                         DEBUG(2,("spoolss_connect_to_client: Can't resolve address for %s\n", remote_machine));
2245                         return false;
2246                 }
2247                 print_sockaddr(addr, sizeof(addr), &rm_addr);
2248         } else {
2249                 rm_addr = *client_ss;
2250                 print_sockaddr(addr, sizeof(addr), &rm_addr);
2251                 DEBUG(5,("spoolss_connect_to_client: Using address %s (no name resolution necessary)\n",
2252                         addr));
2253         }
2254
2255         if (ismyaddr((struct sockaddr *)(void *)&rm_addr)) {
2256                 DEBUG(0,("spoolss_connect_to_client: Machine %s is one of our addresses. Cannot add to ourselves.\n",
2257                         addr));
2258                 return false;
2259         }
2260
2261         /* setup the connection */
2262         ret = cli_full_connection( &the_cli, global_myname(), remote_machine,
2263                 &rm_addr, 0, "IPC$", "IPC",
2264                 "", /* username */
2265                 "", /* domain */
2266                 "", /* password */
2267                 0, lp_client_signing(), NULL );
2268
2269         if ( !NT_STATUS_IS_OK( ret ) ) {
2270                 DEBUG(2,("spoolss_connect_to_client: connection to [%s] failed!\n",
2271                         remote_machine ));
2272                 return false;
2273         }
2274
2275         if ( the_cli->protocol != PROTOCOL_NT1 ) {
2276                 DEBUG(0,("spoolss_connect_to_client: machine %s didn't negotiate NT protocol.\n", remote_machine));
2277                 cli_shutdown(the_cli);
2278                 return false;
2279         }
2280
2281         /*
2282          * Ok - we have an anonymous connection to the IPC$ share.
2283          * Now start the NT Domain stuff :-).
2284          */
2285
2286         ret = cli_rpc_pipe_open_noauth(the_cli, &ndr_table_spoolss.syntax_id, pp_pipe);
2287         if (!NT_STATUS_IS_OK(ret)) {
2288                 DEBUG(2,("spoolss_connect_to_client: unable to open the spoolss pipe on machine %s. Error was : %s.\n",
2289                         remote_machine, nt_errstr(ret)));
2290                 cli_shutdown(the_cli);
2291                 return false;
2292         }
2293
2294         return true;
2295 }
2296
2297 /***************************************************************************
2298  Connect to the client.
2299 ****************************************************************************/
2300
2301 static bool srv_spoolss_replyopenprinter(int snum, const char *printer,
2302                                         uint32_t localprinter, uint32_t type,
2303                                         struct policy_handle *handle,
2304                                         struct sockaddr_storage *client_ss,
2305                                         struct messaging_context *msg_ctx)
2306 {
2307         WERROR result;
2308         NTSTATUS status;
2309
2310         /*
2311          * If it's the first connection, contact the client
2312          * and connect to the IPC$ share anonymously
2313          */
2314         if (smb_connections==0) {
2315                 fstring unix_printer;
2316
2317                 fstrcpy(unix_printer, printer+2); /* the +2 is to strip the leading 2 backslashs */
2318
2319                 if ( !spoolss_connect_to_client( &notify_cli_pipe, client_ss, unix_printer ))
2320                         return false;
2321
2322                 messaging_register(msg_ctx, NULL, MSG_PRINTER_NOTIFY2,
2323                                    receive_notify2_message_list);
2324                 /* Tell the connections db we're now interested in printer
2325                  * notify messages. */
2326                 serverid_register_msg_flags(messaging_server_id(msg_ctx),
2327                                             true, FLAG_MSG_PRINT_NOTIFY);
2328         }
2329
2330         /*
2331          * Tell the specific printing tdb we want messages for this printer
2332          * by registering our PID.
2333          */
2334
2335         if (!print_notify_register_pid(snum))
2336                 DEBUG(0,("print_notify_register_pid: Failed to register our pid for printer %s\n", printer ));
2337
2338         smb_connections++;
2339
2340         status = rpccli_spoolss_ReplyOpenPrinter(notify_cli_pipe, talloc_tos(),
2341                                                  printer,
2342                                                  localprinter,
2343                                                  type,
2344                                                  0,
2345                                                  NULL,
2346                                                  handle,
2347                                                  &result);
2348         if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result))
2349                 DEBUG(5,("srv_spoolss_reply_open_printer: Client RPC returned [%s]\n",
2350                         win_errstr(result)));
2351
2352         return (W_ERROR_IS_OK(result));
2353 }
2354
2355 /****************************************************************
2356  ****************************************************************/
2357
2358 static struct spoolss_NotifyOption *dup_spoolss_NotifyOption(TALLOC_CTX *mem_ctx,
2359                                                              const struct spoolss_NotifyOption *r)
2360 {
2361         struct spoolss_NotifyOption *option;
2362         uint32_t i,k;
2363
2364         if (!r) {
2365                 return NULL;
2366         }
2367
2368         option = talloc_zero(mem_ctx, struct spoolss_NotifyOption);
2369         if (!option) {
2370                 return NULL;
2371         }
2372
2373         *option = *r;
2374
2375         if (!option->count) {
2376                 return option;
2377         }
2378
2379         option->types = talloc_zero_array(option,
2380                 struct spoolss_NotifyOptionType, option->count);
2381         if (!option->types) {
2382                 talloc_free(option);
2383                 return NULL;
2384         }
2385
2386         for (i=0; i < option->count; i++) {
2387                 option->types[i] = r->types[i];
2388
2389                 if (option->types[i].count) {
2390                         option->types[i].fields = talloc_zero_array(option,
2391                                 union spoolss_Field, option->types[i].count);
2392                         if (!option->types[i].fields) {
2393                                 talloc_free(option);
2394                                 return NULL;
2395                         }
2396                         for (k=0; k<option->types[i].count; k++) {
2397                                 option->types[i].fields[k] =
2398                                         r->types[i].fields[k];
2399                         }
2400                 }
2401         }
2402
2403         return option;
2404 }
2405
2406 /****************************************************************
2407  * _spoolss_RemoteFindFirstPrinterChangeNotifyEx
2408  *
2409  * before replying OK: status=0 a rpc call is made to the workstation
2410  * asking ReplyOpenPrinter
2411  *
2412  * in fact ReplyOpenPrinter is the changenotify equivalent on the spoolss pipe
2413  * called from api_spoolss_rffpcnex
2414 ****************************************************************/
2415
2416 WERROR _spoolss_RemoteFindFirstPrinterChangeNotifyEx(struct pipes_struct *p,
2417                                                      struct spoolss_RemoteFindFirstPrinterChangeNotifyEx *r)
2418 {
2419         int snum = -1;
2420         struct spoolss_NotifyOption *option = r->in.notify_options;
2421         struct sockaddr_storage client_ss;
2422
2423         /* store the notify value in the printer struct */
2424
2425         Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
2426
2427         if (!Printer) {
2428                 DEBUG(2,("_spoolss_RemoteFindFirstPrinterChangeNotifyEx: "
2429                         "Invalid handle (%s:%u:%u).\n",
2430                         OUR_HANDLE(r->in.handle)));
2431                 return WERR_BADFID;
2432         }
2433
2434         Printer->notify.flags           = r->in.flags;
2435         Printer->notify.options         = r->in.options;
2436         Printer->notify.printerlocal    = r->in.printer_local;
2437         Printer->notify.msg_ctx         = p->msg_ctx;
2438
2439         TALLOC_FREE(Printer->notify.option);
2440         Printer->notify.option = dup_spoolss_NotifyOption(Printer, option);
2441
2442         fstrcpy(Printer->notify.localmachine, r->in.local_machine);
2443
2444         /* Connect to the client machine and send a ReplyOpenPrinter */
2445
2446         if ( Printer->printer_type == SPLHND_SERVER)
2447                 snum = -1;
2448         else if ( (Printer->printer_type == SPLHND_PRINTER) &&
2449                         !get_printer_snum(p, r->in.handle, &snum, NULL) )
2450                 return WERR_BADFID;
2451
2452         DEBUG(10,("_spoolss_RemoteFindFirstPrinterChangeNotifyEx: "
2453                 "client_address is %s\n", p->client_address));
2454
2455         if (!interpret_string_addr(&client_ss, p->client_address,
2456                                    AI_NUMERICHOST)) {
2457                 return WERR_SERVER_UNAVAILABLE;
2458         }
2459
2460         if(!srv_spoolss_replyopenprinter(snum, Printer->notify.localmachine,
2461                                         Printer->notify.printerlocal, 1,
2462                                         &Printer->notify.client_hnd,
2463                                         &client_ss, p->msg_ctx))
2464                 return WERR_SERVER_UNAVAILABLE;
2465
2466         Printer->notify.client_connected = true;
2467
2468         return WERR_OK;
2469 }
2470
2471 /*******************************************************************
2472  * fill a notify_info_data with the servername
2473  ********************************************************************/
2474
2475 static void spoolss_notify_server_name(int snum,
2476                                        struct spoolss_Notify *data,
2477                                        print_queue_struct *queue,
2478                                        struct spoolss_PrinterInfo2 *pinfo2,
2479                                        TALLOC_CTX *mem_ctx)
2480 {
2481         SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, pinfo2->servername);
2482 }
2483
2484 /*******************************************************************
2485  * fill a notify_info_data with the printername (not including the servername).
2486  ********************************************************************/
2487
2488 static void spoolss_notify_printer_name(int snum,
2489                                         struct spoolss_Notify *data,
2490                                         print_queue_struct *queue,
2491                                         struct spoolss_PrinterInfo2 *pinfo2,
2492                                         TALLOC_CTX *mem_ctx)
2493 {
2494         /* the notify name should not contain the \\server\ part */
2495         const char *p = strrchr(pinfo2->printername, '\\');
2496
2497         if (!p) {
2498                 p = pinfo2->printername;
2499         } else {
2500                 p++;
2501         }
2502
2503         SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, p);
2504 }
2505
2506 /*******************************************************************
2507  * fill a notify_info_data with the servicename
2508  ********************************************************************/
2509
2510 static void spoolss_notify_share_name(int snum,
2511                                       struct spoolss_Notify *data,
2512                                       print_queue_struct *queue,
2513                                       struct spoolss_PrinterInfo2 *pinfo2,
2514                                       TALLOC_CTX *mem_ctx)
2515 {
2516         SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, lp_servicename(snum));
2517 }
2518
2519 /*******************************************************************
2520  * fill a notify_info_data with the port name
2521  ********************************************************************/
2522
2523 static void spoolss_notify_port_name(int snum,
2524                                      struct spoolss_Notify *data,
2525                                      print_queue_struct *queue,
2526                                      struct spoolss_PrinterInfo2 *pinfo2,
2527                                      TALLOC_CTX *mem_ctx)
2528 {
2529         SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, pinfo2->portname);
2530 }
2531
2532 /*******************************************************************
2533  * fill a notify_info_data with the printername
2534  * but it doesn't exist, have to see what to do
2535  ********************************************************************/
2536
2537 static void spoolss_notify_driver_name(int snum,
2538                                        struct spoolss_Notify *data,
2539                                        print_queue_struct *queue,
2540                                        struct spoolss_PrinterInfo2 *pinfo2,
2541                                        TALLOC_CTX *mem_ctx)
2542 {
2543         SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, pinfo2->drivername);
2544 }
2545
2546 /*******************************************************************
2547  * fill a notify_info_data with the comment
2548  ********************************************************************/
2549
2550 static void spoolss_notify_comment(int snum,
2551                                    struct spoolss_Notify *data,
2552                                    print_queue_struct *queue,
2553                                    struct spoolss_PrinterInfo2 *pinfo2,
2554                                    TALLOC_CTX *mem_ctx)
2555 {
2556         const char *p;
2557
2558         if (*pinfo2->comment == '\0') {
2559                 p = lp_comment(snum);
2560         } else {
2561                 p = pinfo2->comment;
2562         }
2563
2564         SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, p);
2565 }
2566
2567 /*******************************************************************
2568  * fill a notify_info_data with the comment
2569  * location = "Room 1, floor 2, building 3"
2570  ********************************************************************/
2571
2572 static void spoolss_notify_location(int snum,
2573                                     struct spoolss_Notify *data,
2574                                     print_queue_struct *queue,
2575                                     struct spoolss_PrinterInfo2 *pinfo2,
2576                                     TALLOC_CTX *mem_ctx)
2577 {
2578         SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, pinfo2->location);
2579 }
2580
2581 /*******************************************************************
2582  * fill a notify_info_data with the device mode
2583  * jfm:xxxx don't to it for know but that's a real problem !!!
2584  ********************************************************************/
2585
2586 static void spoolss_notify_devmode(int snum,
2587                                    struct spoolss_Notify *data,
2588                                    print_queue_struct *queue,
2589                                    struct spoolss_PrinterInfo2 *pinfo2,
2590                                    TALLOC_CTX *mem_ctx)
2591 {
2592         /* for a dummy implementation we have to zero the fields */
2593         SETUP_SPOOLSS_NOTIFY_DATA_DEVMODE(data, NULL);
2594 }
2595
2596 /*******************************************************************
2597  * fill a notify_info_data with the separator file name
2598  ********************************************************************/
2599
2600 static void spoolss_notify_sepfile(int snum,
2601                                    struct spoolss_Notify *data,
2602                                    print_queue_struct *queue,
2603                                    struct spoolss_PrinterInfo2 *pinfo2,
2604                                    TALLOC_CTX *mem_ctx)
2605 {
2606         SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, pinfo2->sepfile);
2607 }
2608
2609 /*******************************************************************
2610  * fill a notify_info_data with the print processor
2611  * jfm:xxxx return always winprint to indicate we don't do anything to it
2612  ********************************************************************/
2613
2614 static void spoolss_notify_print_processor(int snum,
2615                                            struct spoolss_Notify *data,
2616                                            print_queue_struct *queue,
2617                                            struct spoolss_PrinterInfo2 *pinfo2,
2618                                            TALLOC_CTX *mem_ctx)
2619 {
2620         SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, pinfo2->printprocessor);
2621 }
2622
2623 /*******************************************************************
2624  * fill a notify_info_data with the print processor options
2625  * jfm:xxxx send an empty string
2626  ********************************************************************/
2627
2628 static void spoolss_notify_parameters(int snum,
2629                                       struct spoolss_Notify *data,
2630                                       print_queue_struct *queue,
2631                                       struct spoolss_PrinterInfo2 *pinfo2,
2632                                       TALLOC_CTX *mem_ctx)
2633 {
2634         SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, pinfo2->parameters);
2635 }
2636
2637 /*******************************************************************
2638  * fill a notify_info_data with the data type
2639  * jfm:xxxx always send RAW as data type
2640  ********************************************************************/
2641
2642 static void spoolss_notify_datatype(int snum,
2643                                     struct spoolss_Notify *data,
2644                                     print_queue_struct *queue,
2645                                     struct spoolss_PrinterInfo2 *pinfo2,
2646                                     TALLOC_CTX *mem_ctx)
2647 {
2648         SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, pinfo2->datatype);
2649 }
2650
2651 /*******************************************************************
2652  * fill a notify_info_data with the security descriptor
2653  * jfm:xxxx send an null pointer to say no security desc
2654  * have to implement security before !
2655  ********************************************************************/
2656
2657 static void spoolss_notify_security_desc(int snum,
2658                                          struct spoolss_Notify *data,
2659                                          print_queue_struct *queue,
2660                                          struct spoolss_PrinterInfo2 *pinfo2,
2661                                          TALLOC_CTX *mem_ctx)
2662 {
2663         SETUP_SPOOLSS_NOTIFY_DATA_SECDESC(data, pinfo2->secdesc);
2664 }
2665
2666 /*******************************************************************
2667  * fill a notify_info_data with the attributes
2668  * jfm:xxxx a samba printer is always shared
2669  ********************************************************************/
2670
2671 static void spoolss_notify_attributes(int snum,
2672                                       struct spoolss_Notify *data,
2673                                       print_queue_struct *queue,
2674                                       struct spoolss_PrinterInfo2 *pinfo2,
2675                                       TALLOC_CTX *mem_ctx)
2676 {
2677         SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, pinfo2->attributes);
2678 }
2679
2680 /*******************************************************************
2681  * fill a notify_info_data with the priority
2682  ********************************************************************/
2683
2684 static void spoolss_notify_priority(int snum,
2685                                     struct spoolss_Notify *data,
2686                                     print_queue_struct *queue,
2687                                     struct spoolss_PrinterInfo2 *pinfo2,
2688                                     TALLOC_CTX *mem_ctx)
2689 {
2690         SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, pinfo2->priority);
2691 }
2692
2693 /*******************************************************************
2694  * fill a notify_info_data with the default priority
2695  ********************************************************************/
2696
2697 static void spoolss_notify_default_priority(int snum,
2698                                             struct spoolss_Notify *data,
2699                                             print_queue_struct *queue,
2700                                             struct spoolss_PrinterInfo2 *pinfo2,
2701                                             TALLOC_CTX *mem_ctx)
2702 {
2703         SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, pinfo2->defaultpriority);
2704 }
2705
2706 /*******************************************************************
2707  * fill a notify_info_data with the start time
2708  ********************************************************************/
2709
2710 static void spoolss_notify_start_time(int snum,
2711                                       struct spoolss_Notify *data,
2712                                       print_queue_struct *queue,
2713                                       struct spoolss_PrinterInfo2 *pinfo2,
2714                                       TALLOC_CTX *mem_ctx)
2715 {
2716         SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, pinfo2->starttime);
2717 }
2718
2719 /*******************************************************************
2720  * fill a notify_info_data with the until time
2721  ********************************************************************/
2722
2723 static void spoolss_notify_until_time(int snum,
2724                                       struct spoolss_Notify *data,
2725                                       print_queue_struct *queue,
2726                                       struct spoolss_PrinterInfo2 *pinfo2,
2727                                       TALLOC_CTX *mem_ctx)
2728 {
2729         SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, pinfo2->untiltime);
2730 }
2731
2732 /*******************************************************************
2733  * fill a notify_info_data with the status
2734  ********************************************************************/
2735
2736 static void spoolss_notify_status(int snum,
2737                                   struct spoolss_Notify *data,
2738                                   print_queue_struct *queue,
2739                                   struct spoolss_PrinterInfo2 *pinfo2,
2740                                   TALLOC_CTX *mem_ctx)
2741 {
2742         print_status_struct status;
2743
2744         print_queue_length(server_messaging_context(), snum, &status);
2745         SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, status.status);
2746 }
2747
2748 /*******************************************************************
2749  * fill a notify_info_data with the number of jobs queued
2750  ********************************************************************/
2751
2752 static void spoolss_notify_cjobs(int snum,
2753                                  struct spoolss_Notify *data,
2754                                  print_queue_struct *queue,
2755                                  struct spoolss_PrinterInfo2 *pinfo2,
2756                                  TALLOC_CTX *mem_ctx)
2757 {
2758         SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(
2759                 data, print_queue_length(server_messaging_context(), snum,
2760                                          NULL));
2761 }
2762
2763 /*******************************************************************
2764  * fill a notify_info_data with the average ppm
2765  ********************************************************************/
2766
2767 static void spoolss_notify_average_ppm(int snum,
2768                                        struct spoolss_Notify *data,
2769                                        print_queue_struct *queue,
2770                                        struct spoolss_PrinterInfo2 *pinfo2,
2771                                        TALLOC_CTX *mem_ctx)
2772 {
2773         /* always respond 8 pages per minutes */
2774         /* a little hard ! */
2775         SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, pinfo2->averageppm);
2776 }
2777
2778 /*******************************************************************
2779  * fill a notify_info_data with username
2780  ********************************************************************/
2781
2782 static void spoolss_notify_username(int snum,
2783                                     struct spoolss_Notify *data,
2784                                     print_queue_struct *queue,
2785                                     struct spoolss_PrinterInfo2 *pinfo2,
2786                                     TALLOC_CTX *mem_ctx)
2787 {
2788         SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, queue->fs_user);
2789 }
2790
2791 /*******************************************************************
2792  * fill a notify_info_data with job status
2793  ********************************************************************/
2794
2795 static void spoolss_notify_job_status(int snum,
2796                                       struct spoolss_Notify *data,
2797                                       print_queue_struct *queue,
2798                                       struct spoolss_PrinterInfo2 *pinfo2,
2799                                       TALLOC_CTX *mem_ctx)
2800 {
2801         SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, nt_printj_status(queue->status));
2802 }
2803
2804 /*******************************************************************
2805  * fill a notify_info_data with job name
2806  ********************************************************************/
2807
2808 static void spoolss_notify_job_name(int snum,
2809                                     struct spoolss_Notify *data,
2810                                     print_queue_struct *queue,
2811                                     struct spoolss_PrinterInfo2 *pinfo2,
2812                                     TALLOC_CTX *mem_ctx)
2813 {
2814         SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, queue->fs_file);
2815 }
2816
2817 /*******************************************************************
2818  * fill a notify_info_data with job status
2819  ********************************************************************/
2820
2821 static void spoolss_notify_job_status_string(int snum,
2822                                              struct spoolss_Notify *data,
2823                                              print_queue_struct *queue,
2824                                              struct spoolss_PrinterInfo2 *pinfo2,
2825                                              TALLOC_CTX *mem_ctx)
2826 {
2827         /*
2828          * Now we're returning job status codes we just return a "" here. JRA.
2829          */
2830
2831         const char *p = "";
2832
2833 #if 0 /* NO LONGER NEEDED - JRA. 02/22/2001 */
2834         p = "unknown";
2835
2836         switch (queue->status) {
2837         case LPQ_QUEUED:
2838                 p = "Queued";
2839                 break;
2840         case LPQ_PAUSED:
2841                 p = "";    /* NT provides the paused string */
2842                 break;
2843         case LPQ_SPOOLING:
2844                 p = "Spooling";
2845                 break;
2846         case LPQ_PRINTING:
2847                 p = "Printing";
2848                 break;
2849         }
2850 #endif /* NO LONGER NEEDED. */
2851
2852         SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, p);
2853 }
2854
2855 /*******************************************************************
2856  * fill a notify_info_data with job time
2857  ********************************************************************/
2858
2859 static void spoolss_notify_job_time(int snum,
2860                                     struct spoolss_Notify *data,
2861                                     print_queue_struct *queue,
2862                                     struct spoolss_PrinterInfo2 *pinfo2,
2863                                     TALLOC_CTX *mem_ctx)
2864 {
2865         SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, 0);
2866 }
2867
2868 /*******************************************************************
2869  * fill a notify_info_data with job size
2870  ********************************************************************/
2871
2872 static void spoolss_notify_job_size(int snum,
2873                                     struct spoolss_Notify *data,
2874                                     print_queue_struct *queue,
2875                                     struct spoolss_PrinterInfo2 *pinfo2,
2876                                     TALLOC_CTX *mem_ctx)
2877 {
2878         SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, queue->size);
2879 }
2880
2881 /*******************************************************************
2882  * fill a notify_info_data with page info
2883  ********************************************************************/
2884 static void spoolss_notify_total_pages(int snum,
2885                                 struct spoolss_Notify *data,
2886                                 print_queue_struct *queue,
2887                                 struct spoolss_PrinterInfo2 *pinfo2,
2888                                 TALLOC_CTX *mem_ctx)
2889 {
2890         SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, queue->page_count);
2891 }
2892
2893 /*******************************************************************
2894  * fill a notify_info_data with pages printed info.
2895  ********************************************************************/
2896 static void spoolss_notify_pages_printed(int snum,
2897                                 struct spoolss_Notify *data,
2898                                 print_queue_struct *queue,
2899                                 struct spoolss_PrinterInfo2 *pinfo2,
2900                                 TALLOC_CTX *mem_ctx)
2901 {
2902         /* Add code when back-end tracks this */
2903         SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, 0);
2904 }
2905
2906 /*******************************************************************
2907  Fill a notify_info_data with job position.
2908  ********************************************************************/
2909
2910 static void spoolss_notify_job_position(int snum,
2911                                         struct spoolss_Notify *data,
2912                                         print_queue_struct *queue,
2913                                         struct spoolss_PrinterInfo2 *pinfo2,
2914                                         TALLOC_CTX *mem_ctx)
2915 {
2916         SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, queue->job);
2917 }
2918
2919 /*******************************************************************
2920  Fill a notify_info_data with submitted time.
2921  ********************************************************************/
2922
2923 static void spoolss_notify_submitted_time(int snum,
2924                                           struct spoolss_Notify *data,
2925                                           print_queue_struct *queue,
2926                                           struct spoolss_PrinterInfo2 *pinfo2,
2927                                           TALLOC_CTX *mem_ctx)
2928 {
2929         data->data.string.string = NULL;
2930         data->data.string.size = 0;
2931
2932         init_systemtime_buffer(mem_ctx, gmtime(&queue->time),
2933                                &data->data.string.string,
2934                                &data->data.string.size);
2935
2936 }
2937
2938 struct s_notify_info_data_table
2939 {
2940         enum spoolss_NotifyType type;
2941         uint16_t field;
2942         const char *name;
2943         enum spoolss_NotifyTable variable_type;
2944         void (*fn) (int snum, struct spoolss_Notify *data,
2945                     print_queue_struct *queue,
2946                     struct spoolss_PrinterInfo2 *pinfo2,
2947                     TALLOC_CTX *mem_ctx);
2948 };
2949
2950 /* A table describing the various print notification constants and
2951    whether the notification data is a pointer to a variable sized
2952    buffer, a one value uint32_t or a two value uint32_t. */
2953
2954 static const struct s_notify_info_data_table notify_info_data_table[] =
2955 {
2956 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_SERVER_NAME,         "PRINTER_NOTIFY_FIELD_SERVER_NAME",         NOTIFY_TABLE_STRING,   spoolss_notify_server_name },
2957 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_PRINTER_NAME,        "PRINTER_NOTIFY_FIELD_PRINTER_NAME",        NOTIFY_TABLE_STRING,   spoolss_notify_printer_name },
2958 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_SHARE_NAME,          "PRINTER_NOTIFY_FIELD_SHARE_NAME",          NOTIFY_TABLE_STRING,   spoolss_notify_share_name },
2959 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_PORT_NAME,           "PRINTER_NOTIFY_FIELD_PORT_NAME",           NOTIFY_TABLE_STRING,   spoolss_notify_port_name },
2960 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_DRIVER_NAME,         "PRINTER_NOTIFY_FIELD_DRIVER_NAME",         NOTIFY_TABLE_STRING,   spoolss_notify_driver_name },
2961 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_COMMENT,             "PRINTER_NOTIFY_FIELD_COMMENT",             NOTIFY_TABLE_STRING,   spoolss_notify_comment },
2962 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_LOCATION,            "PRINTER_NOTIFY_FIELD_LOCATION",            NOTIFY_TABLE_STRING,   spoolss_notify_location },
2963 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_DEVMODE,             "PRINTER_NOTIFY_FIELD_DEVMODE",             NOTIFY_TABLE_DEVMODE,  spoolss_notify_devmode },
2964 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_SEPFILE,             "PRINTER_NOTIFY_FIELD_SEPFILE",             NOTIFY_TABLE_STRING,   spoolss_notify_sepfile },
2965 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_PRINT_PROCESSOR,     "PRINTER_NOTIFY_FIELD_PRINT_PROCESSOR",     NOTIFY_TABLE_STRING,   spoolss_notify_print_processor },
2966 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_PARAMETERS,          "PRINTER_NOTIFY_FIELD_PARAMETERS",          NOTIFY_TABLE_STRING,   spoolss_notify_parameters },
2967 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_DATATYPE,            "PRINTER_NOTIFY_FIELD_DATATYPE",            NOTIFY_TABLE_STRING,   spoolss_notify_datatype },
2968 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_SECURITY_DESCRIPTOR, "PRINTER_NOTIFY_FIELD_SECURITY_DESCRIPTOR", NOTIFY_TABLE_SECURITYDESCRIPTOR,   spoolss_notify_security_desc },
2969 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_ATTRIBUTES,          "PRINTER_NOTIFY_FIELD_ATTRIBUTES",          NOTIFY_TABLE_DWORD,    spoolss_notify_attributes },
2970 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_PRIORITY,            "PRINTER_NOTIFY_FIELD_PRIORITY",            NOTIFY_TABLE_DWORD,    spoolss_notify_priority },
2971 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_DEFAULT_PRIORITY,    "PRINTER_NOTIFY_FIELD_DEFAULT_PRIORITY",    NOTIFY_TABLE_DWORD,    spoolss_notify_default_priority },
2972 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_START_TIME,          "PRINTER_NOTIFY_FIELD_START_TIME",          NOTIFY_TABLE_DWORD,    spoolss_notify_start_time },
2973 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_UNTIL_TIME,          "PRINTER_NOTIFY_FIELD_UNTIL_TIME",          NOTIFY_TABLE_DWORD,    spoolss_notify_until_time },
2974 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_STATUS,              "PRINTER_NOTIFY_FIELD_STATUS",              NOTIFY_TABLE_DWORD,    spoolss_notify_status },
2975 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_STATUS_STRING,       "PRINTER_NOTIFY_FIELD_STATUS_STRING",       NOTIFY_TABLE_STRING,   NULL },
2976 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_CJOBS,               "PRINTER_NOTIFY_FIELD_CJOBS",               NOTIFY_TABLE_DWORD,    spoolss_notify_cjobs },
2977 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_AVERAGE_PPM,         "PRINTER_NOTIFY_FIELD_AVERAGE_PPM",         NOTIFY_TABLE_DWORD,    spoolss_notify_average_ppm },
2978 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_TOTAL_PAGES,         "PRINTER_NOTIFY_FIELD_TOTAL_PAGES",         NOTIFY_TABLE_DWORD,    NULL },
2979 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_PAGES_PRINTED,       "PRINTER_NOTIFY_FIELD_PAGES_PRINTED",       NOTIFY_TABLE_DWORD,    NULL },
2980 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_TOTAL_BYTES,         "PRINTER_NOTIFY_FIELD_TOTAL_BYTES",         NOTIFY_TABLE_DWORD,    NULL },
2981 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_BYTES_PRINTED,       "PRINTER_NOTIFY_FIELD_BYTES_PRINTED",       NOTIFY_TABLE_DWORD,    NULL },
2982 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_PRINTER_NAME,            "JOB_NOTIFY_FIELD_PRINTER_NAME",            NOTIFY_TABLE_STRING,   spoolss_notify_printer_name },
2983 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_MACHINE_NAME,            "JOB_NOTIFY_FIELD_MACHINE_NAME",            NOTIFY_TABLE_STRING,   spoolss_notify_server_name },
2984 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_PORT_NAME,               "JOB_NOTIFY_FIELD_PORT_NAME",               NOTIFY_TABLE_STRING,   spoolss_notify_port_name },
2985 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_USER_NAME,               "JOB_NOTIFY_FIELD_USER_NAME",               NOTIFY_TABLE_STRING,   spoolss_notify_username },
2986 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_NOTIFY_NAME,             "JOB_NOTIFY_FIELD_NOTIFY_NAME",             NOTIFY_TABLE_STRING,   spoolss_notify_username },
2987 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_DATATYPE,                "JOB_NOTIFY_FIELD_DATATYPE",                NOTIFY_TABLE_STRING,   spoolss_notify_datatype },
2988 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_PRINT_PROCESSOR,         "JOB_NOTIFY_FIELD_PRINT_PROCESSOR",         NOTIFY_TABLE_STRING,   spoolss_notify_print_processor },
2989 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_PARAMETERS,              "JOB_NOTIFY_FIELD_PARAMETERS",              NOTIFY_TABLE_STRING,   spoolss_notify_parameters },
2990 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_DRIVER_NAME,             "JOB_NOTIFY_FIELD_DRIVER_NAME",             NOTIFY_TABLE_STRING,   spoolss_notify_driver_name },
2991 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_DEVMODE,                 "JOB_NOTIFY_FIELD_DEVMODE",                 NOTIFY_TABLE_DEVMODE,  spoolss_notify_devmode },
2992 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_STATUS,                  "JOB_NOTIFY_FIELD_STATUS",                  NOTIFY_TABLE_DWORD,    spoolss_notify_job_status },
2993 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_STATUS_STRING,           "JOB_NOTIFY_FIELD_STATUS_STRING",           NOTIFY_TABLE_STRING,   spoolss_notify_job_status_string },
2994 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_SECURITY_DESCRIPTOR,     "JOB_NOTIFY_FIELD_SECURITY_DESCRIPTOR",     NOTIFY_TABLE_SECURITYDESCRIPTOR,   NULL },
2995 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_DOCUMENT,                "JOB_NOTIFY_FIELD_DOCUMENT",                NOTIFY_TABLE_STRING,   spoolss_notify_job_name },
2996 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_PRIORITY,                "JOB_NOTIFY_FIELD_PRIORITY",                NOTIFY_TABLE_DWORD,    spoolss_notify_priority },
2997 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_POSITION,                "JOB_NOTIFY_FIELD_POSITION",                NOTIFY_TABLE_DWORD,    spoolss_notify_job_position },
2998 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_SUBMITTED,               "JOB_NOTIFY_FIELD_SUBMITTED",               NOTIFY_TABLE_TIME,     spoolss_notify_submitted_time },
2999 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_START_TIME,              "JOB_NOTIFY_FIELD_START_TIME",              NOTIFY_TABLE_DWORD,    spoolss_notify_start_time },
3000 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_UNTIL_TIME,              "JOB_NOTIFY_FIELD_UNTIL_TIME",              NOTIFY_TABLE_DWORD,    spoolss_notify_until_time },
3001 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_TIME,                    "JOB_NOTIFY_FIELD_TIME",                    NOTIFY_TABLE_DWORD,    spoolss_notify_job_time },
3002 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_TOTAL_PAGES,             "JOB_NOTIFY_FIELD_TOTAL_PAGES",             NOTIFY_TABLE_DWORD,    spoolss_notify_total_pages },
3003 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_PAGES_PRINTED,           "JOB_NOTIFY_FIELD_PAGES_PRINTED",           NOTIFY_TABLE_DWORD,    spoolss_notify_pages_printed },
3004 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_TOTAL_BYTES,             "JOB_NOTIFY_FIELD_TOTAL_BYTES",             NOTIFY_TABLE_DWORD,    spoolss_notify_job_size },
3005 };
3006
3007 /*******************************************************************
3008  Return the variable_type of info_data structure.
3009 ********************************************************************/
3010
3011 static uint32_t variable_type_of_notify_info_data(enum spoolss_NotifyType type,
3012                                                   uint16_t field)
3013 {
3014         int i=0;
3015
3016         for (i = 0; i < ARRAY_SIZE(notify_info_data_table); i++) {
3017                 if ( (notify_info_data_table[i].type == type) &&
3018                      (notify_info_data_table[i].field == field) ) {
3019                         return notify_info_data_table[i].variable_type;
3020                 }
3021         }
3022
3023         DEBUG(5, ("invalid notify data type %d/%d\n", type, field));
3024
3025         return 0;
3026 }
3027
3028 /****************************************************************************
3029 ****************************************************************************/
3030
3031 static bool search_notify(enum spoolss_NotifyType type,
3032                           uint16_t field,
3033                           int *value)
3034 {
3035         int i;
3036
3037         for (i = 0; i < ARRAY_SIZE(notify_info_data_table); i++) {
3038                 if (notify_info_data_table[i].type == type &&
3039                     notify_info_data_table[i].field == field &&
3040                     notify_info_data_table[i].fn != NULL) {
3041                         *value = i;
3042                         return true;
3043                 }
3044         }
3045
3046         return false;
3047 }
3048
3049 /****************************************************************************
3050 ****************************************************************************/
3051
3052 static void construct_info_data(struct spoolss_Notify *info_data,
3053                                 enum spoolss_NotifyType type,
3054                                 uint16_t field, int id)
3055 {
3056         info_data->type                 = type;
3057         info_data->field.field          = field;
3058         info_data->variable_type        = variable_type_of_notify_info_data(type, field);
3059         info_data->job_id               = id;
3060 }
3061
3062 /*******************************************************************
3063  *
3064  * fill a notify_info struct with info asked
3065  *
3066  ********************************************************************/
3067
3068 static bool construct_notify_printer_info(Printer_entry *print_hnd,
3069                                           struct spoolss_NotifyInfo *info,
3070                                           struct spoolss_PrinterInfo2 *pinfo2,
3071                                           int snum,
3072                                           const struct spoolss_NotifyOptionType *option_type,
3073                                           uint32_t id,
3074                                           TALLOC_CTX *mem_ctx)
3075 {
3076         int field_num,j;
3077         enum spoolss_NotifyType type;
3078         uint16_t field;
3079
3080         struct spoolss_Notify *current_data;
3081         print_queue_struct *queue=NULL;
3082
3083         type = option_type->type;
3084
3085         DEBUG(4,("construct_notify_printer_info: Notify type: [%s], number of notify info: [%d] on printer: [%s]\n",
3086                 (type == PRINTER_NOTIFY_TYPE ? "PRINTER_NOTIFY_TYPE" : "JOB_NOTIFY_TYPE"),
3087                 option_type->count, lp_servicename(snum)));
3088
3089         for(field_num=0; field_num < option_type->count; field_num++) {
3090                 field = option_type->fields[field_num].field;
3091
3092                 DEBUG(4,("construct_notify_printer_info: notify [%d]: type [%x], field [%x]\n", field_num, type, field));
3093
3094                 if (!search_notify(type, field, &j) )
3095                         continue;
3096
3097                 info->notifies = TALLOC_REALLOC_ARRAY(info, info->notifies,
3098                                                       struct spoolss_Notify,
3099                                                       info->count + 1);
3100                 if (info->notifies == NULL) {
3101                         DEBUG(2,("construct_notify_printer_info: failed to enlarge buffer info->data!\n"));
3102                         return false;
3103                 }
3104
3105                 current_data = &info->notifies[info->count];
3106
3107                 construct_info_data(current_data, type, field, id);
3108
3109                 DEBUG(10, ("construct_notify_printer_info: "
3110                            "calling [%s]  snum=%d  printername=[%s])\n",
3111                            notify_info_data_table[j].name, snum,
3112                            pinfo2->printername));
3113
3114                 notify_info_data_table[j].fn(snum, current_data, queue,
3115                                              pinfo2, mem_ctx);
3116
3117                 info->count++;
3118         }
3119
3120         return true;
3121 }
3122
3123 /*******************************************************************
3124  *
3125  * fill a notify_info struct with info asked
3126  *
3127  ********************************************************************/
3128
3129 static bool construct_notify_jobs_info(print_queue_struct *queue,
3130                                        struct spoolss_NotifyInfo *info,
3131                                        struct spoolss_PrinterInfo2 *pinfo2,
3132                                        int snum,
3133                                        const struct spoolss_NotifyOptionType *option_type,
3134                                        uint32_t id,
3135                                        TALLOC_CTX *mem_ctx)
3136 {
3137         int field_num,j;
3138         enum spoolss_NotifyType type;
3139         uint16_t field;
3140         struct spoolss_Notify *current_data;
3141
3142         DEBUG(4,("construct_notify_jobs_info\n"));
3143
3144         type = option_type->type;
3145
3146         DEBUGADD(4,("Notify type: [%s], number of notify info: [%d]\n",
3147                 (type == PRINTER_NOTIFY_TYPE ? "PRINTER_NOTIFY_TYPE" : "JOB_NOTIFY_TYPE"),
3148                 option_type->count));
3149
3150         for(field_num=0; field_num<option_type->count; field_num++) {
3151                 field = option_type->fields[field_num].field;
3152
3153                 if (!search_notify(type, field, &j) )
3154                         continue;
3155
3156                 info->notifies = TALLOC_REALLOC_ARRAY(info, info->notifies,
3157                                                       struct spoolss_Notify,
3158                                                       info->count + 1);
3159                 if (info->notifies == NULL) {
3160                         DEBUG(2,("construct_notify_jobs_info: failed to enlarg buffer info->data!\n"));
3161                         return false;
3162                 }
3163
3164                 current_data=&(info->notifies[info->count]);
3165
3166                 construct_info_data(current_data, type, field, id);
3167                 notify_info_data_table[j].fn(snum, current_data, queue,
3168                                              pinfo2, mem_ctx);
3169                 info->count++;
3170         }
3171
3172         return true;
3173 }
3174
3175 /*
3176  * JFM: The enumeration is not that simple, it's even non obvious.
3177  *
3178  * let's take an example: I want to monitor the PRINTER SERVER for
3179  * the printer's name and the number of jobs currently queued.
3180  * So in the NOTIFY_OPTION, I have one NOTIFY_OPTION_TYPE structure.
3181  * Its type is PRINTER_NOTIFY_TYPE and it has 2 fields NAME and CJOBS.
3182  *
3183  * I have 3 printers on the back of my server.
3184  *
3185  * Now the response is a NOTIFY_INFO structure, with 6 NOTIFY_INFO_DATA
3186  * structures.
3187  *   Number     Data                    Id
3188  *      1       printer 1 name          1
3189  *      2       printer 1 cjob          1
3190  *      3       printer 2 name          2
3191  *      4       printer 2 cjob          2
3192  *      5       printer 3 name          3
3193  *      6       printer 3 name          3
3194  *
3195  * that's the print server case, the printer case is even worse.
3196  */
3197
3198 /*******************************************************************
3199  *
3200  * enumerate all printers on the printserver
3201  * fill a notify_info struct with info asked
3202  *
3203  ********************************************************************/
3204
3205 static WERROR printserver_notify_info(struct pipes_struct *p,
3206                                       struct policy_handle *hnd,
3207                                       struct spoolss_NotifyInfo *info,
3208                                       TALLOC_CTX *mem_ctx)
3209 {
3210         int snum;
3211         Printer_entry *Printer = find_printer_index_by_hnd(p, hnd);
3212         int n_services=lp_numservices();
3213         int i;
3214         struct spoolss_NotifyOption *option;
3215         struct spoolss_NotifyOptionType option_type;
3216         struct spoolss_PrinterInfo2 *pinfo2 = NULL;
3217         WERROR result;
3218
3219         DEBUG(4,("printserver_notify_info\n"));
3220
3221         if (!Printer)
3222                 return WERR_BADFID;
3223
3224         option = Printer->notify.option;
3225
3226         info->version   = 2;
3227         info->notifies  = NULL;
3228         info->count     = 0;
3229
3230         /* a bug in xp sp2 rc2 causes it to send a fnpcn request without
3231            sending a ffpcn() request first */
3232
3233         if ( !option )
3234                 return WERR_BADFID;
3235
3236         for (i=0; i<option->count; i++) {
3237                 option_type = option->types[i];
3238
3239                 if (option_type.type != PRINTER_NOTIFY_TYPE)
3240                         continue;
3241
3242                 for (snum = 0; snum < n_services; snum++) {
3243                         if (!lp_browseable(snum) ||
3244                             !lp_snum_ok(snum) ||
3245                             !lp_print_ok(snum)) {
3246                                 continue; /* skip */
3247                         }
3248
3249                         /* Maybe we should use the SYSTEM server_info here... */
3250                         result = winreg_get_printer(mem_ctx, p->server_info,
3251                                                     p->msg_ctx,
3252                                                     Printer->servername,
3253                                                     lp_servicename(snum),
3254                                                     &pinfo2);
3255                         if (!W_ERROR_IS_OK(result)) {
3256                                 DEBUG(4, ("printserver_notify_info: "
3257                                           "Failed to get printer [%s]\n",
3258                                           lp_servicename(snum)));
3259                                 continue;
3260                         }
3261
3262
3263                         construct_notify_printer_info(Printer, info,
3264                                                       pinfo2, snum,
3265                                                       &option_type, snum,
3266                                                       mem_ctx);
3267
3268                         TALLOC_FREE(pinfo2);
3269                 }
3270         }
3271
3272 #if 0
3273         /*
3274          * Debugging information, don't delete.
3275          */
3276
3277         DEBUG(1,("dumping the NOTIFY_INFO\n"));
3278         DEBUGADD(1,("info->version:[%d], info->flags:[%d], info->count:[%d]\n", info->version, info->flags, info->count));
3279         DEBUGADD(1,("num\ttype\tfield\tres\tid\tsize\tenc_type\n"));
3280
3281         for (i=0; i<info->count; i++) {
3282                 DEBUGADD(1,("[%d]\t[%d]\t[%d]\t[%d]\t[%d]\t[%d]\t[%d]\n",
3283                 i, info->data[i].type, info->data[i].field, info->data[i].reserved,
3284                 info->data[i].id, info->data[i].size, info->data[i].enc_type));
3285         }
3286 #endif
3287
3288         return WERR_OK;
3289 }
3290
3291 /*******************************************************************
3292  *
3293  * fill a notify_info struct with info asked
3294  *
3295  ********************************************************************/
3296
3297 static WERROR printer_notify_info(struct pipes_struct *p,
3298                                   struct policy_handle *hnd,
3299                                   struct spoolss_NotifyInfo *info,
3300                                   TALLOC_CTX *mem_ctx)
3301 {
3302         int snum;
3303         Printer_entry *Printer = find_printer_index_by_hnd(p, hnd);
3304         int i;
3305         uint32_t id;
3306         struct spoolss_NotifyOption *option;
3307         struct spoolss_NotifyOptionType option_type;
3308         int count,j;
3309         print_queue_struct *queue=NULL;
3310         print_status_struct status;
3311         struct spoolss_PrinterInfo2 *pinfo2 = NULL;
3312         WERROR result;
3313
3314         DEBUG(4,("printer_notify_info\n"));
3315
3316         if (!Printer)
3317                 return WERR_BADFID;
3318
3319         option = Printer->notify.option;
3320         id = 0x0;
3321
3322         info->version   = 2;
3323         info->notifies  = NULL;
3324         info->count     = 0;
3325
3326         /* a bug in xp sp2 rc2 causes it to send a fnpcn request without
3327            sending a ffpcn() request first */
3328
3329         if ( !option )
3330                 return WERR_BADFID;
3331
3332         get_printer_snum(p, hnd, &snum, NULL);
3333
3334         /* Maybe we should use the SYSTEM server_info here... */
3335         result = winreg_get_printer(mem_ctx, p->server_info, p->msg_ctx,
3336                                     Printer->servername,
3337                                     lp_servicename(snum), &pinfo2);
3338         if (!W_ERROR_IS_OK(result)) {
3339                 return WERR_BADFID;
3340         }
3341
3342         for (i=0; i<option->count; i++) {
3343                 option_type = option->types[i];
3344
3345                 switch (option_type.type) {
3346                 case PRINTER_NOTIFY_TYPE:
3347                         if (construct_notify_printer_info(Printer, info,
3348                                                           pinfo2, snum,
3349                                                           &option_type, id,
3350                                                           mem_ctx)) {
3351                                 id--;
3352                         }
3353                         break;
3354
3355                 case JOB_NOTIFY_TYPE:
3356
3357                         count = print_queue_status(snum, &queue, &status);
3358
3359                         for (j=0; j<count; j++) {
3360                                 construct_notify_jobs_info(&queue[j], info,
3361                                                            pinfo2, snum,
3362                                                            &option_type,
3363                                                            queue[j].job,
3364                                                            mem_ctx);
3365                         }
3366
3367                         SAFE_FREE(queue);
3368                         break;
3369                 }
3370         }
3371
3372         /*
3373          * Debugging information, don't delete.
3374          */
3375         /*
3376         DEBUG(1,("dumping the NOTIFY_INFO\n"));
3377         DEBUGADD(1,("info->version:[%d], info->flags:[%d], info->count:[%d]\n", info->version, info->flags, info->count));
3378         DEBUGADD(1,("num\ttype\tfield\tres\tid\tsize\tenc_type\n"));
3379
3380         for (i=0; i<info->count; i++) {
3381                 DEBUGADD(1,("[%d]\t[%d]\t[%d]\t[%d]\t[%d]\t[%d]\t[%d]\n",
3382                 i, info->data[i].type, info->data[i].field, info->data[i].reserved,
3383                 info->data[i].id, info->data[i].size, info->data[i].enc_type));
3384         }
3385         */
3386
3387         talloc_free(pinfo2);
3388         return WERR_OK;
3389 }
3390
3391 /****************************************************************
3392  _spoolss_RouterRefreshPrinterChangeNotify
3393 ****************************************************************/
3394
3395 WERROR _spoolss_RouterRefreshPrinterChangeNotify(struct pipes_struct *p,
3396                                                  struct spoolss_RouterRefreshPrinterChangeNotify *r)
3397 {
3398         struct spoolss_NotifyInfo *info;
3399
3400         Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
3401         WERROR result = WERR_BADFID;
3402
3403         /* we always have a spoolss_NotifyInfo struct */
3404         info = talloc_zero(p->mem_ctx, struct spoolss_NotifyInfo);
3405         if (!info) {
3406                 result = WERR_NOMEM;
3407                 goto done;
3408         }
3409
3410         *r->out.info = info;
3411
3412         if (!Printer) {
3413                 DEBUG(2,("_spoolss_RouterRefreshPrinterChangeNotify: "
3414                         "Invalid handle (%s:%u:%u).\n",
3415                         OUR_HANDLE(r->in.handle)));
3416                 goto done;
3417         }
3418
3419         DEBUG(4,("Printer type %x\n",Printer->printer_type));
3420
3421         /*
3422          *      We are now using the change value, and
3423          *      I should check for PRINTER_NOTIFY_OPTIONS_REFRESH but as
3424          *      I don't have a global notification system, I'm sending back all the
3425          *      informations even when _NOTHING_ has changed.
3426          */
3427
3428         /* We need to keep track of the change value to send back in
3429            RRPCN replies otherwise our updates are ignored. */
3430
3431         Printer->notify.fnpcn = true;
3432
3433         if (Printer->notify.client_connected) {
3434                 DEBUG(10,("_spoolss_RouterRefreshPrinterChangeNotify: "
3435                         "Saving change value in request [%x]\n",
3436                         r->in.change_low));
3437                 Printer->notify.change = r->in.change_low;
3438         }
3439
3440         /* just ignore the spoolss_NotifyOption */
3441
3442         switch (Printer->printer_type) {
3443                 case SPLHND_SERVER:
3444                         result = printserver_notify_info(p, r->in.handle,
3445                                                          info, p->mem_ctx);
3446                         break;
3447
3448                 case SPLHND_PRINTER:
3449                         result = printer_notify_info(p, r->in.handle,
3450                                                      info, p->mem_ctx);
3451                         break;
3452         }
3453
3454         Printer->notify.fnpcn = false;
3455
3456 done:
3457         return result;
3458 }
3459
3460 /********************************************************************
3461  * construct_printer_info_0
3462  * fill a printer_info_0 struct
3463  ********************************************************************/
3464
3465 static WERROR construct_printer_info0(TALLOC_CTX *mem_ctx,
3466                                       struct auth_serversupplied_info *server_info,
3467                                       struct messaging_context *msg_ctx,
3468                                       struct spoolss_PrinterInfo2 *info2,
3469                                       struct spoolss_PrinterInfo0 *r,
3470                                       int snum)
3471 {
3472         int count;
3473         counter_printer_0 *session_counter;
3474         struct timeval setuptime;
3475         print_status_struct status;
3476
3477         r->printername          = talloc_strdup(mem_ctx, info2->printername);
3478         W_ERROR_HAVE_NO_MEMORY(r->printername);
3479
3480         r->servername           = talloc_strdup(mem_ctx, info2->servername);
3481         W_ERROR_HAVE_NO_MEMORY(r->servername);
3482
3483         count = print_queue_length(msg_ctx, snum, &status);
3484
3485         /* check if we already have a counter for this printer */
3486         for (session_counter = counter_list; session_counter; session_counter = session_counter->next) {
3487                 if (session_counter->snum == snum)
3488                         break;
3489         }
3490
3491         /* it's the first time, add it to the list */
3492         if (session_counter == NULL) {
3493                 session_counter = SMB_MALLOC_P(counter_printer_0);
3494                 W_ERROR_HAVE_NO_MEMORY(session_counter);
3495                 ZERO_STRUCTP(session_counter);
3496                 session_counter->snum           = snum;
3497                 session_counter->counter        = 0;
3498                 DLIST_ADD(counter_list, session_counter);
3499         }
3500
3501         /* increment it */
3502         session_counter->counter++;
3503
3504         r->cjobs                        = count;
3505         r->total_jobs                   = 0;
3506         r->total_bytes                  = 0;
3507
3508         get_startup_time(&setuptime);
3509         init_systemtime(&r->time, gmtime(&setuptime.tv_sec));
3510
3511         /* JFM:
3512          * the global_counter should be stored in a TDB as it's common to all the clients
3513          * and should be zeroed on samba startup
3514          */
3515         r->global_counter               = session_counter->counter;
3516         r->total_pages                  = 0;
3517         /* in 2.2 we reported ourselves as 0x0004 and 0x0565 */
3518         SSVAL(&r->version, 0, 0x0005); /* NT 5 */
3519         SSVAL(&r->version, 2, 0x0893); /* build 2195 */
3520         r->free_build                   = SPOOLSS_RELEASE_BUILD;
3521         r->spooling                     = 0;
3522         r->max_spooling                 = 0;
3523         r->session_counter              = session_counter->counter;
3524         r->num_error_out_of_paper       = 0x0;
3525         r->num_error_not_ready          = 0x0;          /* number of print failure */
3526         r->job_error                    = 0x0;
3527         r->number_of_processors         = 0x1;
3528         r->processor_type               = PROCESSOR_INTEL_PENTIUM; /* 586 Pentium ? */
3529         r->high_part_total_bytes        = 0x0;
3530
3531         /* ChangeID in milliseconds*/
3532         winreg_printer_get_changeid(mem_ctx, server_info, msg_ctx,
3533                                     info2->sharename, &r->change_id);
3534
3535         r->last_error                   = WERR_OK;
3536         r->status                       = nt_printq_status(status.status);
3537         r->enumerate_network_printers   = 0x0;
3538         r->c_setprinter                 = 0x0;
3539         r->processor_architecture       = 0x0;
3540         r->processor_level              = 0x6;          /* 6  ???*/
3541         r->ref_ic                       = 0;
3542         r->reserved2                    = 0;
3543         r->reserved3                    = 0;
3544
3545         return WERR_OK;
3546 }
3547
3548
3549 /********************************************************************
3550  * construct_printer_info1
3551  * fill a spoolss_PrinterInfo1 struct
3552 ********************************************************************/
3553
3554 static WERROR construct_printer_info1(TALLOC_CTX *mem_ctx,
3555                                       const struct spoolss_PrinterInfo2 *info2,
3556                                       uint32_t flags,
3557                                       struct spoolss_PrinterInfo1 *r,
3558                                       int snum)
3559 {
3560         r->flags                = flags;
3561
3562         r->description          = talloc_asprintf(mem_ctx, "%s,%s,%s",
3563                                                   info2->printername,
3564                                                   info2->drivername,
3565                                                   info2->location);
3566         W_ERROR_HAVE_NO_MEMORY(r->description);
3567
3568         if (info2->comment == NULL || info2->comment[0] == '\0') {
3569                 r->comment      = talloc_strdup(mem_ctx, lp_comment(snum));
3570         } else {
3571                 r->comment      = talloc_strdup(mem_ctx, info2->comment); /* saved comment */
3572         }
3573         W_ERROR_HAVE_NO_MEMORY(r->comment);
3574
3575         r->name                 = talloc_strdup(mem_ctx, info2->printername);
3576         W_ERROR_HAVE_NO_MEMORY(r->name);
3577
3578         return WERR_OK;
3579 }
3580
3581 /********************************************************************
3582  * construct_printer_info2
3583  * fill a spoolss_PrinterInfo2 struct
3584 ********************************************************************/
3585
3586 static WERROR construct_printer_info2(TALLOC_CTX *mem_ctx,
3587                                       const struct spoolss_PrinterInfo2 *info2,
3588                                       struct spoolss_PrinterInfo2 *r,
3589                                       int snum)
3590 {
3591         int count;
3592         print_status_struct status;
3593
3594         count = print_queue_length(server_messaging_context(), snum, &status);
3595
3596         r->servername           = talloc_strdup(mem_ctx, info2->servername);
3597         W_ERROR_HAVE_NO_MEMORY(r->servername);
3598         r->printername          = talloc_strdup(mem_ctx, info2->printername);
3599         W_ERROR_HAVE_NO_MEMORY(r->printername);
3600         r->sharename            = talloc_strdup(mem_ctx, lp_servicename(snum));
3601         W_ERROR_HAVE_NO_MEMORY(r->sharename);
3602         r->portname             = talloc_strdup(mem_ctx, info2->portname);
3603         W_ERROR_HAVE_NO_MEMORY(r->portname);
3604         r->drivername           = talloc_strdup(mem_ctx, info2->drivername);
3605         W_ERROR_HAVE_NO_MEMORY(r->drivername);
3606
3607         if (info2->comment[0] == '\0') {
3608                 r->comment      = talloc_strdup(mem_ctx, lp_comment(snum));
3609         } else {
3610                 r->comment      = talloc_strdup(mem_ctx, info2->comment);
3611         }
3612         W_ERROR_HAVE_NO_MEMORY(r->comment);
3613
3614         r->location             = talloc_strdup(mem_ctx, info2->location);
3615         W_ERROR_HAVE_NO_MEMORY(r->location);
3616         r->sepfile              = talloc_strdup(mem_ctx, info2->sepfile);
3617         W_ERROR_HAVE_NO_MEMORY(r->sepfile);
3618         r->printprocessor       = talloc_strdup(mem_ctx, info2->printprocessor);
3619         W_ERROR_HAVE_NO_MEMORY(r->printprocessor);
3620         r->datatype             = talloc_strdup(mem_ctx, info2->datatype);
3621         W_ERROR_HAVE_NO_MEMORY(r->datatype);
3622         r->parameters           = talloc_strdup(mem_ctx, info2->parameters);
3623         W_ERROR_HAVE_NO_MEMORY(r->parameters);
3624
3625         r->attributes           = info2->attributes;
3626
3627         r->priority             = info2->priority;
3628         r->defaultpriority      = info2->defaultpriority;
3629         r->starttime            = info2->starttime;
3630         r->untiltime            = info2->untiltime;
3631         r->status               = nt_printq_status(status.status);
3632         r->cjobs                = count;
3633         r->averageppm           = info2->averageppm;
3634
3635         copy_devicemode(mem_ctx, info2->devmode, &r->devmode);
3636         if (!r->devmode) {
3637                 DEBUG(8,("Returning NULL Devicemode!\n"));
3638         }
3639
3640         r->secdesc = NULL;
3641
3642         if (info2->secdesc != NULL) {
3643                 /* don't use talloc_steal() here unless you do a deep steal of all
3644                    the SEC_DESC members */
3645
3646                 r->secdesc      = dup_sec_desc(mem_ctx, info2->secdesc);
3647         }
3648
3649         return WERR_OK;
3650 }
3651
3652 /********************************************************************
3653  * construct_printer_info3
3654  * fill a spoolss_PrinterInfo3 struct
3655  ********************************************************************/
3656
3657 static WERROR construct_printer_info3(TALLOC_CTX *mem_ctx,
3658                                       const struct spoolss_PrinterInfo2 *info2,
3659                                       struct spoolss_PrinterInfo3 *r,
3660                                       int snum)
3661 {
3662         /* These are the components of the SD we are returning. */
3663
3664         if (info2->secdesc != NULL) {
3665                 /* don't use talloc_steal() here unless you do a deep steal of all
3666                    the SEC_DESC members */
3667
3668                 r->secdesc = dup_sec_desc(mem_ctx, info2->secdesc);
3669                 W_ERROR_HAVE_NO_MEMORY(r->secdesc);
3670         }
3671
3672         return WERR_OK;
3673 }
3674
3675 /********************************************************************
3676  * construct_printer_info4
3677  * fill a spoolss_PrinterInfo4 struct
3678  ********************************************************************/
3679
3680 static WERROR construct_printer_info4(TALLOC_CTX *mem_ctx,
3681                                       const struct spoolss_PrinterInfo2 *info2,
3682                                       struct spoolss_PrinterInfo4 *r,
3683                                       int snum)
3684 {
3685         r->printername  = talloc_strdup(mem_ctx, info2->printername);
3686         W_ERROR_HAVE_NO_MEMORY(r->printername);
3687         r->servername   = talloc_strdup(mem_ctx, info2->servername);
3688         W_ERROR_HAVE_NO_MEMORY(r->servername);
3689
3690         r->attributes   = info2->attributes;
3691
3692         return WERR_OK;
3693 }
3694
3695 /********************************************************************
3696  * construct_printer_info5
3697  * fill a spoolss_PrinterInfo5 struct
3698  ********************************************************************/
3699
3700 static WERROR construct_printer_info5(TALLOC_CTX *mem_ctx,
3701                                       const struct spoolss_PrinterInfo2 *info2,
3702                                       struct spoolss_PrinterInfo5 *r,
3703                                       int snum)
3704 {
3705         r->printername  = talloc_strdup(mem_ctx, info2->printername);
3706         W_ERROR_HAVE_NO_MEMORY(r->printername);
3707         r->portname     = talloc_strdup(mem_ctx, info2->portname);
3708         W_ERROR_HAVE_NO_MEMORY(r->portname);
3709
3710         r->attributes   = info2->attributes;
3711
3712         /* these two are not used by NT+ according to MSDN */
3713         r->device_not_selected_timeout          = 0x0;  /* have seen 0x3a98 */
3714         r->transmission_retry_timeout           = 0x0;  /* have seen 0xafc8 */
3715
3716         return WERR_OK;
3717 }
3718
3719 /********************************************************************
3720  * construct_printer_info_6
3721  * fill a spoolss_PrinterInfo6 struct
3722  ********************************************************************/
3723
3724 static WERROR construct_printer_info6(TALLOC_CTX *mem_ctx,
3725                                       const struct spoolss_PrinterInfo2 *info2,
3726                                       struct spoolss_PrinterInfo6 *r,
3727                                       int snum)
3728 {
3729         int count;
3730         print_status_struct status;
3731
3732         count = print_queue_length(server_messaging_context(), snum, &status);
3733
3734         r->status = nt_printq_status(status.status);
3735
3736         return WERR_OK;
3737 }
3738
3739 /********************************************************************
3740  * construct_printer_info7
3741  * fill a spoolss_PrinterInfo7 struct
3742  ********************************************************************/
3743
3744 static WERROR construct_printer_info7(TALLOC_CTX *mem_ctx,
3745                                       struct messaging_context *msg_ctx,
3746                                       Printer_entry *print_hnd,
3747                                       struct spoolss_PrinterInfo7 *r,
3748                                       int snum)
3749 {
3750         struct auth_serversupplied_info *server_info;
3751         struct GUID guid;
3752         NTSTATUS status;
3753
3754         status = make_server_info_system(mem_ctx, &server_info);
3755         if (!NT_STATUS_IS_OK(status)) {
3756                 DEBUG(0, ("construct_printer_info7: "
3757                           "Could not create system server_info\n"));
3758                 return WERR_NOMEM;
3759         }
3760
3761         if (is_printer_published(mem_ctx, server_info, msg_ctx,
3762                                  print_hnd->servername,
3763                                  lp_servicename(snum), &guid, NULL)) {
3764                 r->guid = talloc_strdup_upper(mem_ctx, GUID_string2(mem_ctx, &guid));
3765                 r->action = DSPRINT_PUBLISH;
3766         } else {
3767                 r->guid = talloc_strdup(mem_ctx, "");
3768                 r->action = DSPRINT_UNPUBLISH;
3769         }
3770         W_ERROR_HAVE_NO_MEMORY(r->guid);
3771
3772         TALLOC_FREE(server_info);
3773         return WERR_OK;
3774 }
3775
3776 /********************************************************************
3777  * construct_printer_info8
3778  * fill a spoolss_PrinterInfo8 struct
3779  ********************************************************************/
3780
3781 static WERROR construct_printer_info8(TALLOC_CTX *mem_ctx,
3782                                       const struct spoolss_PrinterInfo2 *info2,
3783                                       struct spoolss_DeviceModeInfo *r,
3784                                       int snum)
3785 {
3786         copy_devicemode(mem_ctx, info2->devmode, &r->devmode);
3787         if (!r->devmode) {
3788                 DEBUG(8,("Returning NULL Devicemode!\n"));
3789         }
3790
3791         return WERR_OK;
3792 }
3793
3794
3795 /********************************************************************
3796 ********************************************************************/
3797
3798 static bool snum_is_shared_printer(int snum)
3799 {
3800         return (lp_browseable(snum) && lp_snum_ok(snum) && lp_print_ok(snum));
3801 }
3802
3803 /********************************************************************
3804  Spoolss_enumprinters.
3805 ********************************************************************/
3806
3807 static WERROR enum_all_printers_info_level(TALLOC_CTX *mem_ctx,
3808                                            struct auth_serversupplied_info *server_info,
3809                                            struct messaging_context *msg_ctx,
3810                                            uint32_t level,
3811                                            uint32_t flags,
3812                                            union spoolss_PrinterInfo **info_p,
3813                                            uint32_t *count_p)
3814 {
3815         int snum;
3816         int n_services = lp_numservices();
3817         union spoolss_PrinterInfo *info = NULL;
3818         uint32_t count = 0;
3819         WERROR result = WERR_OK;
3820
3821         *count_p = 0;
3822         *info_p = NULL;
3823
3824         for (snum = 0; snum < n_services; snum++) {
3825
3826                 const char *printer;
3827                 struct spoolss_PrinterInfo2 *info2;
3828
3829                 if (!snum_is_shared_printer(snum)) {
3830                         continue;
3831                 }
3832
3833                 printer = lp_const_servicename(snum);
3834
3835                 DEBUG(4,("Found a printer in smb.conf: %s[%x]\n",
3836                         printer, snum));
3837
3838                 result = winreg_create_printer(mem_ctx,
3839                                                server_info,
3840                                                msg_ctx,
3841                                                NULL,
3842                                                printer);
3843                 if (!W_ERROR_IS_OK(result)) {
3844                         goto out;
3845                 }
3846
3847                 info = TALLOC_REALLOC_ARRAY(mem_ctx, info,
3848                                             union spoolss_PrinterInfo,
3849                                             count + 1);
3850                 if (!info) {
3851                         result = WERR_NOMEM;
3852                         goto out;
3853                 }
3854
3855                 result = winreg_get_printer(mem_ctx, server_info, msg_ctx,
3856                                             NULL, printer, &info2);
3857                 if (!W_ERROR_IS_OK(result)) {
3858                         goto out;
3859                 }
3860
3861                 switch (level) {
3862                 case 0:
3863                         result = construct_printer_info0(info, server_info,
3864                                                          msg_ctx, info2,
3865                                                          &info[count].info0, snum);
3866                         break;
3867                 case 1:
3868                         result = construct_printer_info1(info, info2, flags,
3869                                                          &info[count].info1, snum);
3870                         break;
3871                 case 2:
3872                         result = construct_printer_info2(info, info2,
3873                                                          &info[count].info2, snum);
3874                         break;
3875                 case 4:
3876                         result = construct_printer_info4(info, info2,
3877                                                          &info[count].info4, snum);
3878                         break;
3879                 case 5:
3880                         result = construct_printer_info5(info, info2,
3881                                                          &info[count].info5, snum);
3882                         break;
3883
3884                 default:
3885                         result = WERR_UNKNOWN_LEVEL;
3886                         goto out;
3887                 }
3888
3889                 if (!W_ERROR_IS_OK(result)) {
3890                         goto out;
3891                 }
3892
3893                 count++;
3894         }
3895
3896         *count_p = count;
3897         *info_p = info;
3898
3899  out:
3900         if (!W_ERROR_IS_OK(result)) {
3901                 TALLOC_FREE(info);
3902                 return result;
3903         }
3904
3905         *info_p = info;
3906
3907         return WERR_OK;
3908 }
3909
3910 /********************************************************************
3911  * handle enumeration of printers at level 0
3912  ********************************************************************/
3913
3914 static WERROR enumprinters_level0(TALLOC_CTX *mem_ctx,
3915                                   struct auth_serversupplied_info *server_info,
3916                                   struct messaging_context *msg_ctx,
3917                                   uint32_t flags,
3918                                   const char *servername,
3919                                   union spoolss_PrinterInfo **info,
3920                                   uint32_t *count)
3921 {
3922         DEBUG(4,("enum_all_printers_info_0\n"));
3923
3924         return enum_all_printers_info_level(mem_ctx, server_info, msg_ctx,
3925                                             0, flags, info, count);
3926 }
3927
3928
3929 /********************************************************************
3930 ********************************************************************/
3931
3932 static WERROR enum_all_printers_info_1(TALLOC_CTX *mem_ctx,
3933                                        struct auth_serversupplied_info *server_info,
3934                                        struct messaging_context *msg_ctx,
3935                                        uint32_t flags,
3936                                        union spoolss_PrinterInfo **info,
3937                                        uint32_t *count)
3938 {
3939         DEBUG(4,("enum_all_printers_info_1\n"));
3940
3941         return enum_all_printers_info_level(mem_ctx, server_info, msg_ctx,
3942                                             1, flags, info, count);
3943 }
3944
3945 /********************************************************************
3946  enum_all_printers_info_1_local.
3947 *********************************************************************/
3948
3949 static WERROR enum_all_printers_info_1_local(TALLOC_CTX *mem_ctx,
3950                                              struct auth_serversupplied_info *server_info,
3951                                              struct messaging_context *msg_ctx,
3952                                              union spoolss_PrinterInfo **info,
3953                                              uint32_t *count)
3954 {
3955         DEBUG(4,("enum_all_printers_info_1_local\n"));
3956
3957         return enum_all_printers_info_1(mem_ctx, server_info, msg_ctx,
3958                                         PRINTER_ENUM_ICON8, info, count);
3959 }
3960
3961 /********************************************************************
3962  enum_all_printers_info_1_name.
3963 *********************************************************************/
3964
3965 static WERROR enum_all_printers_info_1_name(TALLOC_CTX *mem_ctx,
3966                                             struct auth_serversupplied_info *server_info,
3967                                             struct messaging_context *msg_ctx,
3968                                             const char *name,
3969                                             union spoolss_PrinterInfo **info,
3970                                             uint32_t *count)
3971 {
3972         const char *s = name;
3973
3974         DEBUG(4,("enum_all_printers_info_1_name\n"));
3975
3976         if ((name[0] == '\\') && (name[1] == '\\')) {
3977                 s = name + 2;
3978         }
3979
3980         if (!is_myname_or_ipaddr(s)) {
3981                 return WERR_INVALID_NAME;
3982         }
3983
3984         return enum_all_printers_info_1(mem_ctx, server_info, msg_ctx,
3985                                         PRINTER_ENUM_ICON8, info, count);
3986 }
3987
3988 /********************************************************************
3989  enum_all_printers_info_1_network.
3990 *********************************************************************/
3991
3992 static WERROR enum_all_printers_info_1_network(TALLOC_CTX *mem_ctx,
3993                                                struct auth_serversupplied_info *server_info,
3994                                                struct messaging_context *msg_ctx,
3995                                                const char *name,
3996                                                union spoolss_PrinterInfo **info,
3997                                                uint32_t *count)
3998 {
3999         const char *s = name;
4000
4001         DEBUG(4,("enum_all_printers_info_1_network\n"));
4002
4003         /* If we respond to a enum_printers level 1 on our name with flags
4004            set to PRINTER_ENUM_REMOTE with a list of printers then these
4005            printers incorrectly appear in the APW browse list.
4006            Specifically the printers for the server appear at the workgroup
4007            level where all the other servers in the domain are
4008            listed. Windows responds to this call with a
4009            WERR_CAN_NOT_COMPLETE so we should do the same. */
4010
4011         if (name[0] == '\\' && name[1] == '\\') {
4012                  s = name + 2;
4013         }
4014
4015         if (is_myname_or_ipaddr(s)) {
4016                  return WERR_CAN_NOT_COMPLETE;
4017         }
4018
4019         return enum_all_printers_info_1(mem_ctx, server_info, msg_ctx,
4020                                         PRINTER_ENUM_NAME, info, count);
4021 }
4022
4023 /********************************************************************
4024  * api_spoolss_enumprinters
4025  *
4026  * called from api_spoolss_enumprinters (see this to understand)
4027  ********************************************************************/
4028
4029 static WERROR enum_all_printers_info_2(TALLOC_CTX *mem_ctx,
4030                                        struct auth_serversupplied_info *server_info,
4031                                        struct messaging_context *msg_ctx,
4032                                        union spoolss_PrinterInfo **info,
4033                                        uint32_t *count)
4034 {
4035         DEBUG(4,("enum_all_printers_info_2\n"));
4036
4037         return enum_all_printers_info_level(mem_ctx, server_info, msg_ctx,
4038                                             2, 0, info, count);
4039 }
4040
4041 /********************************************************************
4042  * handle enumeration of printers at level 1
4043  ********************************************************************/
4044
4045 static WERROR enumprinters_level1(TALLOC_CTX *mem_ctx,
4046                                   struct auth_serversupplied_info *server_info,
4047                                   struct messaging_context *msg_ctx,
4048                                   uint32_t flags,
4049                                   const char *name,
4050                                   union spoolss_PrinterInfo **info,
4051                                   uint32_t *count)
4052 {
4053         /* Not all the flags are equals */
4054
4055         if (flags & PRINTER_ENUM_LOCAL) {
4056                 return enum_all_printers_info_1_local(mem_ctx, server_info,
4057                                                       msg_ctx, info, count);
4058         }
4059
4060         if (flags & PRINTER_ENUM_NAME) {
4061                 return enum_all_printers_info_1_name(mem_ctx, server_info,
4062                                                      msg_ctx, name, info,
4063                                                      count);
4064         }
4065
4066         if (flags & PRINTER_ENUM_NETWORK) {
4067                 return enum_all_printers_info_1_network(mem_ctx, server_info,
4068                                                         msg_ctx, name, info,
4069                                                         count);
4070         }
4071
4072         return WERR_OK; /* NT4sp5 does that */
4073 }
4074
4075 /********************************************************************
4076  * handle enumeration of printers at level 2
4077  ********************************************************************/
4078
4079 static WERROR enumprinters_level2(TALLOC_CTX *mem_ctx,
4080                                   struct auth_serversupplied_info *server_info,
4081                                   struct messaging_context *msg_ctx,
4082                                   uint32_t flags,
4083                                   const char *servername,
4084                                   union spoolss_PrinterInfo **info,
4085                                   uint32_t *count)
4086 {
4087         if (flags & PRINTER_ENUM_LOCAL) {
4088                 return enum_all_printers_info_2(mem_ctx, server_info, msg_ctx,
4089                                                 info, count);
4090         }
4091
4092         if (flags & PRINTER_ENUM_NAME) {
4093                 if (!is_myname_or_ipaddr(canon_servername(servername))) {
4094                         return WERR_INVALID_NAME;
4095                 }
4096
4097                 return enum_all_printers_info_2(mem_ctx, server_info, msg_ctx,
4098                                                 info, count);
4099         }
4100
4101         if (flags & PRINTER_ENUM_REMOTE) {
4102                 return WERR_UNKNOWN_LEVEL;
4103         }
4104
4105         return WERR_OK;
4106 }
4107
4108 /********************************************************************
4109  * handle enumeration of printers at level 4
4110  ********************************************************************/
4111
4112 static WERROR enumprinters_level4(TALLOC_CTX *mem_ctx,
4113                                   struct auth_serversupplied_info *server_info,
4114                                   struct messaging_context *msg_ctx,
4115                                   uint32_t flags,
4116                                   const char *servername,
4117                                   union spoolss_PrinterInfo **info,
4118                                   uint32_t *count)
4119 {
4120         DEBUG(4,("enum_all_printers_info_4\n"));
4121
4122         return enum_all_printers_info_level(mem_ctx, server_info, msg_ctx,
4123                                             4, flags, info, count);
4124 }
4125
4126
4127 /********************************************************************
4128  * handle enumeration of printers at level 5
4129  ********************************************************************/
4130
4131 static WERROR enumprinters_level5(TALLOC_CTX *mem_ctx,
4132                                   struct auth_serversupplied_info *server_info,
4133                                   struct messaging_context *msg_ctx,
4134                                   uint32_t flags,
4135                                   const char *servername,
4136                                   union spoolss_PrinterInfo **info,
4137                                   uint32_t *count)
4138 {
4139         DEBUG(4,("enum_all_printers_info_5\n"));
4140
4141         return enum_all_printers_info_level(mem_ctx, server_info, msg_ctx,
4142                                             5, flags, info, count);
4143 }
4144
4145 /****************************************************************
4146  _spoolss_EnumPrinters
4147 ****************************************************************/
4148
4149 WERROR _spoolss_EnumPrinters(struct pipes_struct *p,
4150                              struct spoolss_EnumPrinters *r)
4151 {
4152         const char *name = NULL;
4153         WERROR result;
4154
4155         /* that's an [in out] buffer */
4156
4157         if (!r->in.buffer && (r->in.offered != 0)) {
4158                 return WERR_INVALID_PARAM;
4159         }
4160
4161         DEBUG(4,("_spoolss_EnumPrinters\n"));
4162
4163         *r->out.needed = 0;
4164         *r->out.count = 0;
4165         *r->out.info = NULL;
4166
4167         /*
4168          * Level 1:
4169          *          flags==PRINTER_ENUM_NAME
4170          *           if name=="" then enumerates all printers
4171          *           if name!="" then enumerate the printer
4172          *          flags==PRINTER_ENUM_REMOTE
4173          *          name is NULL, enumerate printers
4174          * Level 2: name!="" enumerates printers, name can't be NULL
4175          * Level 3: doesn't exist
4176          * Level 4: does a local registry lookup
4177          * Level 5: same as Level 2
4178          */
4179
4180         if (r->in.server) {
4181                 name = talloc_strdup_upper(p->mem_ctx, r->in.server);
4182                 W_ERROR_HAVE_NO_MEMORY(name);
4183         }
4184
4185         switch (r->in.level) {
4186         case 0:
4187                 result = enumprinters_level0(p->mem_ctx, p->server_info,
4188                                              p->msg_ctx, r->in.flags, name,
4189                                              r->out.info, r->out.count);
4190                 break;
4191         case 1:
4192                 result = enumprinters_level1(p->mem_ctx, p->server_info,
4193                                              p->msg_ctx, r->in.flags, name,
4194                                              r->out.info, r->out.count);
4195                 break;
4196         case 2:
4197                 result = enumprinters_level2(p->mem_ctx, p->server_info,
4198                                              p->msg_ctx, r->in.flags, name,
4199                                              r->out.info, r->out.count);
4200                 break;
4201         case 4:
4202                 result = enumprinters_level4(p->mem_ctx, p->server_info,
4203                                              p->msg_ctx, r->in.flags, name,
4204                                              r->out.info, r->out.count);
4205                 break;
4206         case 5:
4207                 result = enumprinters_level5(p->mem_ctx, p->server_info,
4208                                              p->msg_ctx, r->in.flags, name,
4209                                              r->out.info, r->out.count);
4210                 break;
4211         default:
4212                 return WERR_UNKNOWN_LEVEL;
4213         }
4214
4215         if (!W_ERROR_IS_OK(result)) {
4216                 return result;
4217         }
4218
4219         *r->out.needed  = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
4220                                                      spoolss_EnumPrinters, 
4221                                                      *r->out.info, r->in.level,
4222                                                      *r->out.count);
4223         *r->out.info    = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
4224         *r->out.count   = SPOOLSS_BUFFER_OK(*r->out.count, 0);
4225
4226         return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
4227 }
4228
4229 /****************************************************************
4230  _spoolss_GetPrinter
4231 ****************************************************************/
4232
4233 WERROR _spoolss_GetPrinter(struct pipes_struct *p,
4234                            struct spoolss_GetPrinter *r)
4235 {
4236         Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
4237         struct spoolss_PrinterInfo2 *info2 = NULL;
4238         WERROR result = WERR_OK;
4239         const char *servername = NULL;
4240         int snum;
4241
4242         /* that's an [in out] buffer */
4243
4244         if (!r->in.buffer && (r->in.offered != 0)) {
4245                 return WERR_INVALID_PARAM;
4246         }
4247
4248         *r->out.needed = 0;
4249
4250         if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
4251                 return WERR_BADFID;
4252         }
4253
4254         if (Printer != NULL || Printer->servername != NULL) {
4255                 servername = Printer->servername;
4256         }
4257
4258         result = winreg_get_printer(p->mem_ctx,
4259                                     p->server_info,
4260                                     p->msg_ctx,
4261                                     servername,
4262                                     lp_const_servicename(snum),
4263                                     &info2);
4264         if (!W_ERROR_IS_OK(result)) {
4265                 return result;
4266         }
4267
4268         switch (r->in.level) {
4269         case 0:
4270                 result = construct_printer_info0(p->mem_ctx, p->server_info,
4271                                                  p->msg_ctx, info2,
4272                                                  &r->out.info->info0, snum);
4273                 break;
4274         case 1:
4275                 result = construct_printer_info1(p->mem_ctx, info2,
4276                                                  PRINTER_ENUM_ICON8,
4277                                                  &r->out.info->info1, snum);
4278                 break;
4279         case 2:
4280                 result = construct_printer_info2(p->mem_ctx, info2,
4281                                                  &r->out.info->info2, snum);
4282                 break;
4283         case 3:
4284                 result = construct_printer_info3(p->mem_ctx, info2,
4285                                                  &r->out.info->info3, snum);
4286                 break;
4287         case 4:
4288                 result = construct_printer_info4(p->mem_ctx, info2,
4289                                                  &r->out.info->info4, snum);
4290                 break;
4291         case 5:
4292                 result = construct_printer_info5(p->mem_ctx, info2,
4293                                                  &r->out.info->info5, snum);
4294                 break;
4295         case 6:
4296                 result = construct_printer_info6(p->mem_ctx, info2,
4297                                                  &r->out.info->info6, snum);
4298                 break;
4299         case 7:
4300                 result = construct_printer_info7(p->mem_ctx, p->msg_ctx,
4301                                                  Printer,
4302                                                  &r->out.info->info7, snum);
4303                 break;
4304         case 8:
4305                 result = construct_printer_info8(p->mem_ctx, info2,
4306                                                  &r->out.info->info8, snum);
4307                 break;
4308         default:
4309                 result = WERR_UNKNOWN_LEVEL;
4310                 break;
4311         }
4312
4313         if (!W_ERROR_IS_OK(result)) {
4314                 DEBUG(0, ("_spoolss_GetPrinter: failed to construct printer info level %d - %s\n",
4315                           r->in.level, win_errstr(result)));
4316                 TALLOC_FREE(r->out.info);
4317                 return result;
4318         }
4319
4320         *r->out.needed  = SPOOLSS_BUFFER_UNION(spoolss_PrinterInfo, 
4321                                                r->out.info, r->in.level);
4322         r->out.info     = SPOOLSS_BUFFER_OK(r->out.info, NULL);
4323
4324         return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
4325 }
4326
4327 /********************************************************************
4328  ********************************************************************/
4329
4330 #define FILL_DRIVER_STRING(mem_ctx, in, out) \
4331         do { \
4332                 if (in && strlen(in)) { \
4333                         out = talloc_strdup(mem_ctx, in); \
4334                 } else { \
4335                         out = talloc_strdup(mem_ctx, ""); \
4336                 } \
4337                 W_ERROR_HAVE_NO_MEMORY(out); \
4338         } while (0);
4339
4340 #define FILL_DRIVER_UNC_STRING(mem_ctx, server, arch, ver, in, out) \
4341         do { \
4342                 if (in && strlen(in)) { \
4343                         out = talloc_asprintf(mem_ctx, "\\\\%s\\print$\\%s\\%d\\%s", server, get_short_archi(arch), ver, in); \
4344                 } else { \
4345                         out = talloc_strdup(mem_ctx, ""); \
4346                 } \
4347                 W_ERROR_HAVE_NO_MEMORY(out); \
4348         } while (0);
4349
4350 static WERROR string_array_from_driver_info(TALLOC_CTX *mem_ctx,
4351                                                   const char **string_array,
4352                                                   const char ***presult,
4353                                                   const char *cservername,
4354                                                   const char *arch,
4355                                                   int version)
4356 {
4357         int i, num_strings = 0;
4358         const char **array = NULL;
4359
4360         if (string_array == NULL) {
4361                 return WERR_INVALID_PARAMETER;;
4362         }
4363
4364         for (i=0; string_array[i] && string_array[i][0] != '\0'; i++) {
4365                 const char *str = NULL;
4366
4367                 if (cservername == NULL || arch == NULL) {
4368                         FILL_DRIVER_STRING(mem_ctx, string_array[i], str);
4369                 } else {
4370                         FILL_DRIVER_UNC_STRING(mem_ctx, cservername, arch, version, string_array[i], str);
4371                 }
4372
4373                 if (!add_string_to_array(mem_ctx, str, &array, &num_strings)) {
4374                         TALLOC_FREE(array);
4375                         return WERR_NOMEM;
4376                 }
4377         }
4378
4379         if (i > 0) {
4380                 ADD_TO_ARRAY(mem_ctx, const char *, NULL,
4381                              &array, &num_strings);
4382         }
4383
4384         if (presult) {
4385                 *presult = array;
4386         }
4387
4388         return WERR_OK;
4389 }
4390
4391 /********************************************************************
4392  * fill a spoolss_DriverInfo1 struct
4393  ********************************************************************/
4394
4395 static WERROR fill_printer_driver_info1(TALLOC_CTX *mem_ctx,
4396                                         struct spoolss_DriverInfo1 *r,
4397                                         const struct spoolss_DriverInfo8 *driver,
4398                                         const char *servername)
4399 {
4400         r->driver_name          = talloc_strdup(mem_ctx, driver->driver_name);
4401         W_ERROR_HAVE_NO_MEMORY(r->driver_name);
4402
4403         return WERR_OK;
4404 }
4405
4406 /********************************************************************
4407  * fill a spoolss_DriverInfo2 struct
4408  ********************************************************************/
4409
4410 static WERROR fill_printer_driver_info2(TALLOC_CTX *mem_ctx,
4411                                         struct spoolss_DriverInfo2 *r,
4412                                         const struct spoolss_DriverInfo8 *driver,
4413                                         const char *servername)
4414
4415 {
4416         const char *cservername = canon_servername(servername);
4417
4418         r->version              = driver->version;
4419
4420         r->driver_name          = talloc_strdup(mem_ctx, driver->driver_name);
4421         W_ERROR_HAVE_NO_MEMORY(r->driver_name);
4422         r->architecture         = talloc_strdup(mem_ctx, driver->architecture);
4423         W_ERROR_HAVE_NO_MEMORY(r->architecture);
4424
4425         FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4426                                driver->architecture,
4427                                driver->version,
4428                                driver->driver_path,
4429                                r->driver_path);
4430
4431         FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4432                                driver->architecture,
4433                                driver->version,
4434                                driver->data_file,
4435                                r->data_file);
4436
4437         FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4438                                driver->architecture,
4439                                driver->version,
4440                                driver->config_file,
4441                                r->config_file);
4442
4443         return WERR_OK;
4444 }
4445
4446 /********************************************************************
4447  * fill a spoolss_DriverInfo3 struct
4448  ********************************************************************/
4449
4450 static WERROR fill_printer_driver_info3(TALLOC_CTX *mem_ctx,
4451                                         struct spoolss_DriverInfo3 *r,
4452                                         const struct spoolss_DriverInfo8 *driver,
4453                                         const char *servername)
4454 {
4455         const char *cservername = canon_servername(servername);
4456
4457         r->version              = driver->version;
4458
4459         r->driver_name          = talloc_strdup(mem_ctx, driver->driver_name);
4460         W_ERROR_HAVE_NO_MEMORY(r->driver_name);
4461         r->architecture         = talloc_strdup(mem_ctx, driver->architecture);
4462         W_ERROR_HAVE_NO_MEMORY(r->architecture);
4463
4464         FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4465                                driver->architecture,
4466                                driver->version,
4467                                driver->driver_path,
4468                                r->driver_path);
4469
4470         FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4471                                driver->architecture,
4472                                driver->version,
4473                                driver->data_file,
4474                                r->data_file);
4475
4476         FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4477                                driver->architecture,
4478                                driver->version,
4479                                driver->config_file,
4480                                r->config_file);
4481
4482         FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4483                                driver->architecture,
4484                                driver->version,
4485                                driver->help_file,
4486                                r->help_file);
4487
4488         FILL_DRIVER_STRING(mem_ctx,
4489                            driver->monitor_name,
4490                            r->monitor_name);
4491
4492         FILL_DRIVER_STRING(mem_ctx,
4493                            driver->default_datatype,
4494                            r->default_datatype);
4495
4496         return string_array_from_driver_info(mem_ctx,
4497                                              driver->dependent_files,
4498                                              &r->dependent_files,
4499                                              cservername,
4500                                              driver->architecture,
4501                                              driver->version);
4502 }
4503
4504 /********************************************************************
4505  * fill a spoolss_DriverInfo4 struct
4506  ********************************************************************/
4507
4508 static WERROR fill_printer_driver_info4(TALLOC_CTX *mem_ctx,
4509                                         struct spoolss_DriverInfo4 *r,
4510                                         const struct spoolss_DriverInfo8 *driver,
4511                                         const char *servername)
4512 {
4513         const char *cservername = canon_servername(servername);
4514         WERROR result;
4515
4516         r->version              = driver->version;
4517
4518         r->driver_name          = talloc_strdup(mem_ctx, driver->driver_name);
4519         W_ERROR_HAVE_NO_MEMORY(r->driver_name);
4520         r->architecture         = talloc_strdup(mem_ctx, driver->architecture);
4521         W_ERROR_HAVE_NO_MEMORY(r->architecture);
4522
4523         FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4524                                driver->architecture,
4525                                driver->version,
4526                                driver->driver_path,
4527                                r->driver_path);
4528
4529         FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4530                                driver->architecture,
4531                                driver->version,
4532                                driver->data_file,
4533                                r->data_file);
4534
4535         FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4536                                driver->architecture,
4537                                driver->version,
4538                                driver->config_file,
4539                                r->config_file);
4540
4541         FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4542                                driver->architecture,
4543                                driver->version,
4544                                driver->help_file,
4545                                r->help_file);
4546
4547         result = string_array_from_driver_info(mem_ctx,
4548                                                driver->dependent_files,
4549                                                &r->dependent_files,
4550                                                cservername,
4551                                                driver->architecture,
4552                                                driver->version);
4553         if (!W_ERROR_IS_OK(result)) {
4554                 return result;
4555         }
4556
4557         FILL_DRIVER_STRING(mem_ctx,
4558                            driver->monitor_name,
4559                            r->monitor_name);
4560
4561         FILL_DRIVER_STRING(mem_ctx,
4562                            driver->default_datatype,
4563                            r->default_datatype);
4564
4565
4566         result = string_array_from_driver_info(mem_ctx,
4567                                                driver->previous_names,
4568                                                &r->previous_names,
4569                                                NULL, NULL, 0);
4570
4571         return result;
4572 }
4573
4574 /********************************************************************
4575  * fill a spoolss_DriverInfo5 struct
4576  ********************************************************************/
4577
4578 static WERROR fill_printer_driver_info5(TALLOC_CTX *mem_ctx,
4579                                         struct spoolss_DriverInfo5 *r,
4580                                         const struct spoolss_DriverInfo8 *driver,
4581                                         const char *servername)
4582 {
4583         const char *cservername = canon_servername(servername);
4584
4585         r->version              = driver->version;
4586
4587         r->driver_name          = talloc_strdup(mem_ctx, driver->driver_name);
4588         W_ERROR_HAVE_NO_MEMORY(r->driver_name);
4589         r->architecture         = talloc_strdup(mem_ctx, driver->architecture);
4590         W_ERROR_HAVE_NO_MEMORY(r->architecture);
4591
4592         FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4593                                driver->architecture,
4594                                driver->version,
4595                                driver->driver_path,
4596                                r->driver_path);
4597
4598         FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4599                                driver->architecture,
4600                                driver->version,
4601                                driver->data_file,
4602                                r->data_file);
4603
4604         FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4605                                driver->architecture,
4606                                driver->version,
4607                                driver->config_file,
4608                                r->config_file);
4609
4610         r->driver_attributes    = 0;
4611         r->config_version       = 0;
4612         r->driver_version       = 0;
4613
4614         return WERR_OK;
4615 }
4616 /********************************************************************
4617  * fill a spoolss_DriverInfo6 struct
4618  ********************************************************************/
4619
4620 static WERROR fill_printer_driver_info6(TALLOC_CTX *mem_ctx,
4621                                         struct spoolss_DriverInfo6 *r,
4622                                         const struct spoolss_DriverInfo8 *driver,
4623                                         const char *servername)
4624 {
4625         const char *cservername = canon_servername(servername);
4626         WERROR result;
4627
4628         r->version              = driver->version;
4629
4630         r->driver_name          = talloc_strdup(mem_ctx, driver->driver_name);
4631         W_ERROR_HAVE_NO_MEMORY(r->driver_name);
4632         r->architecture         = talloc_strdup(mem_ctx, driver->architecture);
4633         W_ERROR_HAVE_NO_MEMORY(r->architecture);
4634
4635         FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4636                                driver->architecture,
4637                                driver->version,
4638                                driver->driver_path,
4639                                r->driver_path);
4640
4641         FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4642                                driver->architecture,
4643                                driver->version,
4644                                driver->data_file,
4645                                r->data_file);
4646
4647         FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4648                                driver->architecture,
4649                                driver->version,
4650                                driver->config_file,
4651                                r->config_file);
4652
4653         FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4654                                driver->architecture,
4655                                driver->version,
4656                                driver->help_file,
4657                                r->help_file);
4658
4659         FILL_DRIVER_STRING(mem_ctx,
4660                            driver->monitor_name,
4661                            r->monitor_name);
4662
4663         FILL_DRIVER_STRING(mem_ctx,
4664                            driver->default_datatype,
4665                            r->default_datatype);
4666
4667         result = string_array_from_driver_info(mem_ctx,
4668                                                driver->dependent_files,
4669                                                &r->dependent_files,
4670                                                cservername,
4671                                                driver->architecture,
4672                                                driver->version);
4673         if (!W_ERROR_IS_OK(result)) {
4674                 return result;
4675         }
4676
4677         result = string_array_from_driver_info(mem_ctx,
4678                                                driver->previous_names,
4679                                                &r->previous_names,
4680                                                NULL, NULL, 0);
4681         if (!W_ERROR_IS_OK(result)) {
4682                 return result;
4683         }
4684
4685         r->driver_date          = driver->driver_date;
4686         r->driver_version       = driver->driver_version;
4687
4688         FILL_DRIVER_STRING(mem_ctx,
4689                            driver->manufacturer_name,
4690                            r->manufacturer_name);
4691         FILL_DRIVER_STRING(mem_ctx,
4692                            driver->manufacturer_url,
4693                            r->manufacturer_url);
4694         FILL_DRIVER_STRING(mem_ctx,
4695                            driver->hardware_id,
4696                            r->hardware_id);
4697         FILL_DRIVER_STRING(mem_ctx,
4698                            driver->provider,
4699                            r->provider);
4700
4701         return WERR_OK;
4702 }
4703
4704 /********************************************************************
4705  * fill a spoolss_DriverInfo8 struct
4706  ********************************************************************/
4707
4708 static WERROR fill_printer_driver_info8(TALLOC_CTX *mem_ctx,
4709                                         struct spoolss_DriverInfo8 *r,
4710                                         const struct spoolss_DriverInfo8 *driver,
4711                                         const char *servername)
4712 {
4713         const char *cservername = canon_servername(servername);
4714         WERROR result;
4715
4716         r->version              = driver->version;
4717
4718         r->driver_name          = talloc_strdup(mem_ctx, driver->driver_name);
4719         W_ERROR_HAVE_NO_MEMORY(r->driver_name);
4720         r->architecture         = talloc_strdup(mem_ctx, driver->architecture);
4721         W_ERROR_HAVE_NO_MEMORY(r->architecture);
4722
4723         FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4724                                driver->architecture,
4725                                driver->version,
4726                                driver->driver_path,
4727                                r->driver_path);
4728
4729         FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4730                                driver->architecture,
4731                                driver->version,
4732                                driver->data_file,
4733                                r->data_file);
4734
4735         FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4736                                driver->architecture,
4737                                driver->version,
4738                                driver->config_file,
4739                                r->config_file);
4740
4741         FILL_DRIVER_UNC_STRING(mem_ctx, cservername,
4742                                driver->architecture,
4743                                driver->version,
4744                                driver->help_file,
4745                                r->help_file);
4746
4747         FILL_DRIVER_STRING(mem_ctx,
4748                            driver->monitor_name,
4749                            r->monitor_name);
4750
4751         FILL_DRIVER_STRING(mem_ctx,
4752                            driver->default_datatype,
4753                            r->default_datatype);
4754
4755         result = string_array_from_driver_info(mem_ctx,
4756                                                driver->dependent_files,
4757                                                &r->dependent_files,
4758                                                cservername,
4759                                                driver->architecture,
4760                                                driver->version);
4761         if (!W_ERROR_IS_OK(result)) {
4762                 return result;
4763         }
4764
4765         result = string_array_from_driver_info(mem_ctx,
4766                                                driver->previous_names,
4767                                                &r->previous_names,
4768                                                NULL, NULL, 0);
4769         if (!W_ERROR_IS_OK(result)) {
4770                 return result;
4771         }
4772
4773         r->driver_date          = driver->driver_date;
4774         r->driver_version       = driver->driver_version;
4775
4776         FILL_DRIVER_STRING(mem_ctx,
4777                            driver->manufacturer_name,
4778                            r->manufacturer_name);
4779         FILL_DRIVER_STRING(mem_ctx,
4780                            driver->manufacturer_url,
4781                            r->manufacturer_url);
4782         FILL_DRIVER_STRING(mem_ctx,
4783                            driver->hardware_id,
4784                            r->hardware_id);
4785         FILL_DRIVER_STRING(mem_ctx,
4786                            driver->provider,
4787                            r->provider);
4788
4789         FILL_DRIVER_STRING(mem_ctx,
4790                            driver->print_processor,
4791                            r->print_processor);
4792         FILL_DRIVER_STRING(mem_ctx,
4793                            driver->vendor_setup,
4794                            r->vendor_setup);
4795
4796         result = string_array_from_driver_info(mem_ctx,
4797                                                driver->color_profiles,
4798                                                &r->color_profiles,
4799                                                NULL, NULL, 0);
4800         if (!W_ERROR_IS_OK(result)) {
4801                 return result;
4802         }
4803
4804         FILL_DRIVER_STRING(mem_ctx,
4805                            driver->inf_path,
4806                            r->inf_path);
4807
4808         r->printer_driver_attributes    = driver->printer_driver_attributes;
4809
4810         result = string_array_from_driver_info(mem_ctx,
4811                                                driver->core_driver_dependencies,
4812                                                &r->core_driver_dependencies,
4813                                                NULL, NULL, 0);
4814         if (!W_ERROR_IS_OK(result)) {
4815                 return result;
4816         }
4817
4818         r->min_inbox_driver_ver_date    = driver->min_inbox_driver_ver_date;
4819         r->min_inbox_driver_ver_version = driver->min_inbox_driver_ver_version;
4820
4821         return WERR_OK;
4822 }
4823
4824 #if 0 /* disabled until marshalling issues are resolved - gd */
4825 /********************************************************************
4826  ********************************************************************/
4827
4828 static WERROR fill_spoolss_DriverFileInfo(TALLOC_CTX *mem_ctx,
4829                                           struct spoolss_DriverFileInfo *r,
4830                                           const char *cservername,
4831                                           const char *file_name,
4832                                           enum spoolss_DriverFileType file_type,
4833                                           uint32_t file_version)
4834 {
4835         r->file_name    = talloc_asprintf(mem_ctx, "\\\\%s%s",
4836                                           cservername, file_name);
4837         W_ERROR_HAVE_NO_MEMORY(r->file_name);
4838         r->file_type    = file_type;
4839         r->file_version = file_version;
4840
4841         return WERR_OK;
4842 }
4843
4844 /********************************************************************
4845  ********************************************************************/
4846
4847 static WERROR spoolss_DriverFileInfo_from_driver(TALLOC_CTX *mem_ctx,
4848                                                  const struct spoolss_DriverInfo8 *driver,
4849                                                  const char *cservername,
4850                                                  struct spoolss_DriverFileInfo **info_p,
4851                                                  uint32_t *count_p)
4852 {
4853         struct spoolss_DriverFileInfo *info = NULL;
4854         uint32_t count = 0;
4855         WERROR result;
4856         uint32_t i;
4857
4858         *info_p = NULL;
4859         *count_p = 0;
4860
4861         if (strlen(driver->driver_path)) {
4862                 info = TALLOC_REALLOC_ARRAY(mem_ctx, info,
4863                                             struct spoolss_DriverFileInfo,
4864                                             count + 1);
4865                 W_ERROR_HAVE_NO_MEMORY(info);
4866                 result = fill_spoolss_DriverFileInfo(info,
4867                                                      &info[count],
4868                                                      cservername,
4869                                                      driver->driver_path,
4870                                                      SPOOLSS_DRIVER_FILE_TYPE_RENDERING,
4871                                                      0);
4872                 W_ERROR_NOT_OK_RETURN(result);
4873                 count++;
4874         }
4875
4876         if (strlen(driver->config_file)) {
4877                 info = TALLOC_REALLOC_ARRAY(mem_ctx, info,
4878                                             struct spoolss_DriverFileInfo,
4879                                             count + 1);
4880                 W_ERROR_HAVE_NO_MEMORY(info);
4881                 result = fill_spoolss_DriverFileInfo(info,
4882                                                      &info[count],
4883                                                      cservername,
4884                                                      driver->config_file,
4885                                                      SPOOLSS_DRIVER_FILE_TYPE_CONFIGURATION,
4886                                                      0);
4887                 W_ERROR_NOT_OK_RETURN(result);
4888                 count++;
4889         }
4890
4891         if (strlen(driver->data_file)) {
4892                 info = TALLOC_REALLOC_ARRAY(mem_ctx, info,
4893                                             struct spoolss_DriverFileInfo,
4894                                             count + 1);
4895                 W_ERROR_HAVE_NO_MEMORY(info);
4896                 result = fill_spoolss_DriverFileInfo(info,
4897                                                      &info[count],
4898                                                      cservername,
4899                                                      driver->data_file,
4900                                                      SPOOLSS_DRIVER_FILE_TYPE_DATA,
4901                                                      0);
4902                 W_ERROR_NOT_OK_RETURN(result);
4903                 count++;
4904         }
4905
4906         if (strlen(driver->help_file)) {
4907                 info = TALLOC_REALLOC_ARRAY(mem_ctx, info,
4908                                             struct spoolss_DriverFileInfo,
4909                                             count + 1);
4910                 W_ERROR_HAVE_NO_MEMORY(info);
4911                 result = fill_spoolss_DriverFileInfo(info,
4912                                                      &info[count],
4913                                                      cservername,
4914                                                      driver->help_file,
4915                                                      SPOOLSS_DRIVER_FILE_TYPE_HELP,
4916                                                      0);
4917                 W_ERROR_NOT_OK_RETURN(result);
4918                 count++;
4919         }
4920
4921         for (i=0; driver->dependent_files[i] && driver->dependent_files[i][0] != '\0'; i++) {
4922                 info = TALLOC_REALLOC_ARRAY(mem_ctx, info,
4923                                             struct spoolss_DriverFileInfo,
4924                                             count + 1);
4925                 W_ERROR_HAVE_NO_MEMORY(info);
4926                 result = fill_spoolss_DriverFileInfo(info,
4927                                                      &info[count],
4928                                                      cservername,
4929                                                      driver->dependent_files[i],
4930                                                      SPOOLSS_DRIVER_FILE_TYPE_OTHER,
4931                                                      0);
4932                 W_ERROR_NOT_OK_RETURN(result);
4933                 count++;
4934         }
4935
4936         *info_p = info;
4937         *count_p = count;
4938
4939         return WERR_OK;
4940 }
4941
4942 /********************************************************************
4943  * fill a spoolss_DriverInfo101 struct
4944  ********************************************************************/
4945
4946 static WERROR fill_printer_driver_info101(TALLOC_CTX *mem_ctx,
4947                                           struct spoolss_DriverInfo101 *r,
4948                                           const struct spoolss_DriverInfo8 *driver,
4949                                           const char *servername)
4950 {
4951         const char *cservername = canon_servername(servername);
4952         WERROR result;
4953
4954         r->version              = driver->version;
4955
4956         r->driver_name          = talloc_strdup(mem_ctx, driver->driver_name);
4957         W_ERROR_HAVE_NO_MEMORY(r->driver_name);
4958         r->architecture         = talloc_strdup(mem_ctx, driver->architecture);
4959         W_ERROR_HAVE_NO_MEMORY(r->architecture);
4960
4961         result = spoolss_DriverFileInfo_from_driver(mem_ctx, driver,
4962                                                     cservername,
4963                                                     &r->file_info,
4964                                                     &r->file_count);
4965         if (!W_ERROR_IS_OK(result)) {
4966                 return result;
4967         }
4968
4969         FILL_DRIVER_STRING(mem_ctx,
4970                            driver->monitor_name,
4971                            r->monitor_name);
4972
4973         FILL_DRIVER_STRING(mem_ctx,
4974                            driver->default_datatype,
4975                            r->default_datatype);
4976
4977         result = string_array_from_driver_info(mem_ctx,
4978                                                driver->previous_names,
4979                                                &r->previous_names,
4980                                                NULL, NULL, 0);
4981         if (!W_ERROR_IS_OK(result)) {
4982                 return result;
4983         }
4984
4985         r->driver_date          = driver->driver_date;
4986         r->driver_version       = driver->driver_version;
4987
4988         FILL_DRIVER_STRING(mem_ctx,
4989                            driver->manufacturer_name,
4990                            r->manufacturer_name);
4991         FILL_DRIVER_STRING(mem_ctx,
4992                            driver->manufacturer_url,
4993                            r->manufacturer_url);
4994         FILL_DRIVER_STRING(mem_ctx,
4995                            driver->hardware_id,
4996                            r->hardware_id);
4997         FILL_DRIVER_STRING(mem_ctx,
4998                            driver->provider,
4999                            r->provider);
5000
5001         return WERR_OK;
5002 }
5003 #endif
5004 /********************************************************************
5005  ********************************************************************/
5006
5007 static WERROR construct_printer_driver_info_level(TALLOC_CTX *mem_ctx,
5008                                                   struct auth_serversupplied_info *server_info,
5009                                                   struct messaging_context *msg_ctx,
5010                                                   uint32_t level,
5011                                                   union spoolss_DriverInfo *r,
5012                                                   int snum,
5013                                                   const char *servername,
5014                                                   const char *architecture,
5015                                                   uint32_t version)
5016 {
5017         struct spoolss_PrinterInfo2 *pinfo2 = NULL;
5018         struct spoolss_DriverInfo8 *driver;
5019         WERROR result;
5020
5021         result = winreg_get_printer(mem_ctx,
5022                                     server_info,
5023                                     msg_ctx,
5024                                     servername,
5025                                     lp_const_servicename(snum),
5026                                     &pinfo2);
5027
5028         DEBUG(8,("construct_printer_driver_info_level: status: %s\n",
5029                 win_errstr(result)));
5030
5031         if (!W_ERROR_IS_OK(result)) {
5032                 return WERR_INVALID_PRINTER_NAME;
5033         }
5034
5035         result = winreg_get_driver(mem_ctx, server_info, msg_ctx,
5036                                    architecture,
5037                                    pinfo2->drivername, version, &driver);
5038
5039         DEBUG(8,("construct_printer_driver_info_level: status: %s\n",
5040                 win_errstr(result)));
5041
5042         if (!W_ERROR_IS_OK(result)) {
5043                 /*
5044                  * Is this a W2k client ?
5045                  */
5046
5047                 if (version < 3) {
5048                         talloc_free(pinfo2);
5049                         return WERR_UNKNOWN_PRINTER_DRIVER;
5050                 }
5051
5052                 /* Yes - try again with a WinNT driver. */
5053                 version = 2;
5054                 result = winreg_get_driver(mem_ctx, server_info, msg_ctx,
5055                                            architecture,
5056                                            pinfo2->drivername,
5057                                            version, &driver);
5058                 DEBUG(8,("construct_printer_driver_level: status: %s\n",
5059                         win_errstr(result)));
5060                 if (!W_ERROR_IS_OK(result)) {
5061                         talloc_free(pinfo2);
5062                         return WERR_UNKNOWN_PRINTER_DRIVER;
5063                 }
5064         }
5065
5066         switch (level) {
5067         case 1:
5068                 result = fill_printer_driver_info1(mem_ctx, &r->info1, driver, servername);
5069                 break;
5070         case 2:
5071                 result = fill_printer_driver_info2(mem_ctx, &r->info2, driver, servername);
5072                 break;
5073         case 3:
5074                 result = fill_printer_driver_info3(mem_ctx, &r->info3, driver, servername);
5075                 break;
5076         case 4:
5077                 result = fill_printer_driver_info4(mem_ctx, &r->info4, driver, servername);
5078                 break;
5079         case 5:
5080                 result = fill_printer_driver_info5(mem_ctx, &r->info5, driver, servername);
5081                 break;
5082         case 6:
5083                 result = fill_printer_driver_info6(mem_ctx, &r->info6, driver, servername);
5084                 break;
5085         case 8:
5086                 result = fill_printer_driver_info8(mem_ctx, &r->info8, driver, servername);
5087                 break;
5088 #if 0 /* disabled until marshalling issues are resolved - gd */
5089         case 101:
5090                 result = fill_printer_driver_info101(mem_ctx, &r->info101, driver, servername);
5091                 break;
5092 #endif
5093         default:
5094                 result = WERR_UNKNOWN_LEVEL;
5095                 break;
5096         }
5097
5098         talloc_free(pinfo2);
5099         talloc_free(driver);
5100
5101         return result;
5102 }
5103
5104 /****************************************************************
5105  _spoolss_GetPrinterDriver2
5106 ****************************************************************/
5107
5108 WERROR _spoolss_GetPrinterDriver2(struct pipes_struct *p,
5109                                   struct spoolss_GetPrinterDriver2 *r)
5110 {
5111         Printer_entry *printer;
5112         WERROR result;
5113
5114         int snum;
5115
5116         /* that's an [in out] buffer */
5117
5118         if (!r->in.buffer && (r->in.offered != 0)) {
5119                 return WERR_INVALID_PARAM;
5120         }
5121
5122         DEBUG(4,("_spoolss_GetPrinterDriver2\n"));
5123
5124         if (!(printer = find_printer_index_by_hnd(p, r->in.handle))) {
5125                 DEBUG(0,("_spoolss_GetPrinterDriver2: invalid printer handle!\n"));
5126                 return WERR_INVALID_PRINTER_NAME;
5127         }
5128
5129         *r->out.needed = 0;
5130         *r->out.server_major_version = 0;
5131         *r->out.server_minor_version = 0;
5132
5133         if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
5134                 return WERR_BADFID;
5135         }
5136
5137         result = construct_printer_driver_info_level(p->mem_ctx,
5138                                                      p->server_info,
5139                                                      p->msg_ctx,
5140                                                      r->in.level, r->out.info,
5141                                                      snum, printer->servername,
5142                                                      r->in.architecture,
5143                                                      r->in.client_major_version);
5144         if (!W_ERROR_IS_OK(result)) {
5145                 TALLOC_FREE(r->out.info);
5146                 return result;
5147         }
5148
5149         *r->out.needed  = SPOOLSS_BUFFER_UNION(spoolss_DriverInfo, 
5150                                                r->out.info, r->in.level);
5151         r->out.info     = SPOOLSS_BUFFER_OK(r->out.info, NULL);
5152
5153         return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
5154 }
5155
5156
5157 /****************************************************************
5158  _spoolss_StartPagePrinter
5159 ****************************************************************/
5160
5161 WERROR _spoolss_StartPagePrinter(struct pipes_struct *p,
5162                                  struct spoolss_StartPagePrinter *r)
5163 {
5164         Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
5165
5166         if (!Printer) {
5167                 DEBUG(3,("_spoolss_StartPagePrinter: "
5168                         "Error in startpageprinter printer handle\n"));
5169                 return WERR_BADFID;
5170         }
5171
5172         Printer->page_started = true;
5173         return WERR_OK;
5174 }
5175
5176 /****************************************************************
5177  _spoolss_EndPagePrinter
5178 ****************************************************************/
5179
5180 WERROR _spoolss_EndPagePrinter(struct pipes_struct *p,
5181                                struct spoolss_EndPagePrinter *r)
5182 {
5183         int snum;
5184
5185         Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
5186
5187         if (!Printer) {
5188                 DEBUG(2,("_spoolss_EndPagePrinter: Invalid handle (%s:%u:%u).\n",
5189                         OUR_HANDLE(r->in.handle)));
5190                 return WERR_BADFID;
5191         }
5192
5193         if (!get_printer_snum(p, r->in.handle, &snum, NULL))
5194                 return WERR_BADFID;
5195
5196         Printer->page_started = false;
5197         print_job_endpage(snum, Printer->jobid);
5198
5199         return WERR_OK;
5200 }
5201
5202 /****************************************************************
5203  _spoolss_StartDocPrinter
5204 ****************************************************************/
5205
5206 WERROR _spoolss_StartDocPrinter(struct pipes_struct *p,
5207                                 struct spoolss_StartDocPrinter *r)
5208 {
5209         struct spoolss_DocumentInfo1 *info_1;
5210         int snum;
5211         Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
5212         WERROR werr;
5213
5214         if (!Printer) {
5215                 DEBUG(2,("_spoolss_StartDocPrinter: "
5216                         "Invalid handle (%s:%u:%u)\n",
5217                         OUR_HANDLE(r->in.handle)));
5218                 return WERR_BADFID;
5219         }
5220
5221         if (Printer->jobid) {
5222                 DEBUG(2, ("_spoolss_StartDocPrinter: "
5223                           "StartDocPrinter called twice! "
5224                           "(existing jobid = %d)\n", Printer->jobid));
5225                 return WERR_INVALID_HANDLE;
5226         }
5227
5228         if (r->in.level != 1) {
5229                 return WERR_UNKNOWN_LEVEL;
5230         }
5231
5232         info_1 = r->in.info.info1;
5233
5234         /*
5235          * a nice thing with NT is it doesn't listen to what you tell it.
5236          * when asked to send _only_ RAW datas, it tries to send datas
5237          * in EMF format.
5238          *
5239          * So I add checks like in NT Server ...
5240          */
5241
5242         if (info_1->datatype) {
5243                 if (strcmp(info_1->datatype, "RAW") != 0) {
5244                         *r->out.job_id = 0;
5245                         return WERR_INVALID_DATATYPE;
5246                 }
5247         }
5248
5249         /* get the share number of the printer */
5250         if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
5251                 return WERR_BADFID;
5252         }
5253
5254         werr = print_job_start(p->server_info, p->msg_ctx, snum,
5255                                 info_1->document_name, info_1->output_file,
5256                                 Printer->devmode, &Printer->jobid);
5257
5258         /* An error occured in print_job_start() so return an appropriate
5259            NT error code. */
5260
5261         if (!W_ERROR_IS_OK(werr)) {
5262                 return werr;
5263         }
5264
5265         Printer->document_started = true;
5266         *r->out.job_id = Printer->jobid;
5267
5268         return WERR_OK;
5269 }
5270
5271 /****************************************************************
5272  _spoolss_EndDocPrinter
5273 ****************************************************************/
5274
5275 WERROR _spoolss_EndDocPrinter(struct pipes_struct *p,
5276                               struct spoolss_EndDocPrinter *r)
5277 {
5278         Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
5279         NTSTATUS status;
5280         int snum;
5281
5282         if (!Printer) {
5283                 DEBUG(2,("_spoolss_EndDocPrinter: Invalid handle (%s:%u:%u)\n",
5284                         OUR_HANDLE(r->in.handle)));
5285                 return WERR_BADFID;
5286         }
5287
5288         if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
5289                 return WERR_BADFID;
5290         }
5291
5292         Printer->document_started = false;
5293         status = print_job_end(snum, Printer->jobid, NORMAL_CLOSE);
5294         if (!NT_STATUS_IS_OK(status)) {
5295                 DEBUG(2, ("_spoolss_EndDocPrinter: "
5296                           "print_job_end failed [%s]\n",
5297                           nt_errstr(status)));
5298         }
5299
5300         Printer->jobid = 0;
5301         return ntstatus_to_werror(status);
5302 }
5303
5304 /****************************************************************
5305  _spoolss_WritePrinter
5306 ****************************************************************/
5307
5308 WERROR _spoolss_WritePrinter(struct pipes_struct *p,
5309                              struct spoolss_WritePrinter *r)
5310 {
5311         ssize_t buffer_written;
5312         int snum;
5313         Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
5314
5315         if (!Printer) {
5316                 DEBUG(2,("_spoolss_WritePrinter: Invalid handle (%s:%u:%u)\n",
5317                         OUR_HANDLE(r->in.handle)));
5318                 *r->out.num_written = r->in._data_size;
5319                 return WERR_BADFID;
5320         }
5321
5322         if (!get_printer_snum(p, r->in.handle, &snum, NULL))
5323                 return WERR_BADFID;
5324
5325         /* print_job_write takes care of checking for PJOB_SMBD_SPOOLING */
5326         buffer_written = print_job_write(snum, Printer->jobid,
5327                                                    (const char *)r->in.data.data,
5328                                                    (SMB_OFF_T)-1,
5329                                                    (size_t)r->in._data_size);
5330         if (buffer_written == (ssize_t)-1) {
5331                 *r->out.num_written = 0;
5332                 if (errno == ENOSPC)
5333                         return WERR_NO_SPOOL_SPACE;
5334                 else
5335                         return WERR_ACCESS_DENIED;
5336         }
5337
5338         *r->out.num_written = r->in._data_size;
5339
5340         return WERR_OK;
5341 }
5342
5343 /********************************************************************
5344  * api_spoolss_getprinter
5345  * called from the spoolss dispatcher
5346  *
5347  ********************************************************************/
5348
5349 static WERROR control_printer(struct policy_handle *handle, uint32_t command,
5350                               struct pipes_struct *p)
5351 {
5352         int snum;
5353         WERROR errcode = WERR_BADFUNC;
5354         Printer_entry *Printer = find_printer_index_by_hnd(p, handle);
5355
5356         if (!Printer) {
5357                 DEBUG(2,("control_printer: Invalid handle (%s:%u:%u)\n",
5358                         OUR_HANDLE(handle)));
5359                 return WERR_BADFID;
5360         }
5361
5362         if (!get_printer_snum(p, handle, &snum, NULL))
5363                 return WERR_BADFID;
5364
5365         switch (command) {
5366         case SPOOLSS_PRINTER_CONTROL_PAUSE:
5367                 errcode = print_queue_pause(p->server_info, p->msg_ctx, snum);
5368                 break;
5369         case SPOOLSS_PRINTER_CONTROL_RESUME:
5370         case SPOOLSS_PRINTER_CONTROL_UNPAUSE:
5371                 errcode = print_queue_resume(p->server_info, p->msg_ctx, snum);
5372                 break;
5373         case SPOOLSS_PRINTER_CONTROL_PURGE:
5374                 errcode = print_queue_purge(p->server_info, p->msg_ctx, snum);
5375                 break;
5376         default:
5377                 return WERR_UNKNOWN_LEVEL;
5378         }
5379
5380         return errcode;
5381 }
5382
5383
5384 /****************************************************************
5385  _spoolss_AbortPrinter
5386  * From MSDN: "Deletes printer's spool file if printer is configured
5387  * for spooling"
5388 ****************************************************************/
5389
5390 WERROR _spoolss_AbortPrinter(struct pipes_struct *p,
5391                              struct spoolss_AbortPrinter *r)
5392 {
5393         Printer_entry   *Printer = find_printer_index_by_hnd(p, r->in.handle);
5394         int             snum;
5395         WERROR          errcode = WERR_OK;
5396
5397         if (!Printer) {
5398                 DEBUG(2,("_spoolss_AbortPrinter: Invalid handle (%s:%u:%u)\n",
5399                         OUR_HANDLE(r->in.handle)));
5400                 return WERR_BADFID;
5401         }
5402
5403         if (!get_printer_snum(p, r->in.handle, &snum, NULL))
5404                 return WERR_BADFID;
5405
5406         if (!Printer->document_started) {
5407                 return WERR_SPL_NO_STARTDOC;
5408         }
5409
5410         errcode = print_job_delete(p->server_info, p->msg_ctx, snum,
5411                                    Printer->jobid);
5412
5413         return errcode;
5414 }
5415
5416 /********************************************************************
5417  * called by spoolss_api_setprinter
5418  * when updating a printer description
5419  ********************************************************************/
5420
5421 static WERROR update_printer_sec(struct policy_handle *handle,
5422                                  struct pipes_struct *p,
5423                                  struct sec_desc_buf *secdesc_ctr)
5424 {
5425         struct spoolss_security_descriptor *new_secdesc = NULL;
5426         struct spoolss_security_descriptor *old_secdesc = NULL;
5427         const char *printer;
5428         WERROR result;
5429         int snum;
5430
5431         Printer_entry *Printer = find_printer_index_by_hnd(p, handle);
5432
5433         if (!Printer || !get_printer_snum(p, handle, &snum, NULL)) {
5434                 DEBUG(2,("update_printer_sec: Invalid handle (%s:%u:%u)\n",
5435                          OUR_HANDLE(handle)));
5436
5437                 result = WERR_BADFID;
5438                 goto done;
5439         }
5440
5441         if (secdesc_ctr == NULL) {
5442                 DEBUG(10,("update_printer_sec: secdesc_ctr is NULL !\n"));
5443                 result = WERR_INVALID_PARAM;
5444                 goto done;
5445         }
5446         printer = lp_const_servicename(snum);
5447
5448         /* Check the user has permissions to change the security
5449            descriptor.  By experimentation with two NT machines, the user
5450            requires Full Access to the printer to change security
5451            information. */
5452
5453         if ( Printer->access_granted != PRINTER_ACCESS_ADMINISTER ) {
5454                 DEBUG(4,("update_printer_sec: updated denied by printer permissions\n"));
5455                 result = WERR_ACCESS_DENIED;
5456                 goto done;
5457         }
5458
5459         /* NT seems to like setting the security descriptor even though
5460            nothing may have actually changed. */
5461         result = winreg_get_printer_secdesc(p->mem_ctx,
5462                                             p->server_info,
5463                                             p->msg_ctx,
5464                                             printer,
5465                                             &old_secdesc);
5466         if (!W_ERROR_IS_OK(result)) {
5467                 DEBUG(2,("update_printer_sec: winreg_get_printer_secdesc() failed\n"));
5468                 result = WERR_BADFID;
5469                 goto done;
5470         }
5471
5472         if (DEBUGLEVEL >= 10) {
5473                 struct security_acl *the_acl;
5474                 int i;
5475
5476                 the_acl = old_secdesc->dacl;
5477                 DEBUG(10, ("old_secdesc_ctr for %s has %d aces:\n",
5478                            printer, the_acl->num_aces));
5479
5480                 for (i = 0; i < the_acl->num_aces; i++) {
5481                         DEBUG(10, ("%s 0x%08x\n", sid_string_dbg(
5482                                            &the_acl->aces[i].trustee),
5483                                   the_acl->aces[i].access_mask));
5484                 }
5485
5486                 the_acl = secdesc_ctr->sd->dacl;
5487
5488                 if (the_acl) {
5489                         DEBUG(10, ("secdesc_ctr for %s has %d aces:\n",
5490                                    printer, the_acl->num_aces));
5491
5492                         for (i = 0; i < the_acl->num_aces; i++) {
5493                                 DEBUG(10, ("%s 0x%08x\n", sid_string_dbg(
5494                                                    &the_acl->aces[i].trustee),
5495                                            the_acl->aces[i].access_mask));
5496                         }
5497                 } else {
5498                         DEBUG(10, ("dacl for secdesc_ctr is NULL\n"));
5499                 }
5500         }
5501
5502         new_secdesc = sec_desc_merge(p->mem_ctx, secdesc_ctr->sd, old_secdesc);
5503         if (new_secdesc == NULL) {
5504                 result = WERR_NOMEM;
5505                 goto done;
5506         }
5507
5508         if (security_descriptor_equal(new_secdesc, old_secdesc)) {
5509                 result = WERR_OK;
5510                 goto done;
5511         }
5512
5513         result = winreg_set_printer_secdesc(p->mem_ctx,
5514                                             p->server_info,
5515                                             p->msg_ctx,
5516                                             printer,
5517                                             new_secdesc);
5518
5519  done:
5520         return result;
5521 }
5522
5523 /********************************************************************
5524  Canonicalize printer info from a client
5525  ********************************************************************/
5526
5527 static bool check_printer_ok(TALLOC_CTX *mem_ctx,
5528                              struct spoolss_SetPrinterInfo2 *info2,
5529                              int snum)
5530 {
5531         fstring printername;
5532         const char *p;
5533
5534         DEBUG(5,("check_printer_ok: servername=%s printername=%s sharename=%s "
5535                 "portname=%s drivername=%s comment=%s location=%s\n",
5536                 info2->servername, info2->printername, info2->sharename,
5537                 info2->portname, info2->drivername, info2->comment,
5538                 info2->location));
5539
5540         /* we force some elements to "correct" values */
5541         info2->servername = talloc_asprintf(mem_ctx, "\\\\%s", global_myname());
5542         if (info2->servername == NULL) {
5543                 return false;
5544         }
5545         info2->sharename = talloc_strdup(mem_ctx, lp_const_servicename(snum));
5546         if (info2->sharename == NULL) {
5547                 return false;
5548         }
5549
5550         /* check to see if we allow printername != sharename */
5551         if (lp_force_printername(snum)) {
5552                 info2->printername = talloc_asprintf(mem_ctx, "\\\\%s\\%s",
5553                                         global_myname(), info2->sharename);
5554         } else {
5555                 /* make sure printername is in \\server\printername format */
5556                 fstrcpy(printername, info2->printername);
5557                 p = printername;
5558                 if ( printername[0] == '\\' && printername[1] == '\\' ) {
5559                         if ( (p = strchr_m( &printername[2], '\\' )) != NULL )
5560                                 p++;
5561                 }
5562
5563                 info2->printername = talloc_asprintf(mem_ctx, "\\\\%s\\%s",
5564                                         global_myname(), p);
5565         }
5566         if (info2->printername == NULL) {
5567                 return false;
5568         }
5569
5570         info2->attributes |= PRINTER_ATTRIBUTE_SAMBA;
5571         info2->attributes &= ~PRINTER_ATTRIBUTE_NOT_SAMBA;
5572
5573         return true;
5574 }
5575
5576 /****************************************************************************
5577 ****************************************************************************/
5578
5579 static WERROR add_port_hook(TALLOC_CTX *ctx, NT_USER_TOKEN *token, const char *portname, const char *uri)
5580 {
5581         char *cmd = lp_addport_cmd();
5582         char *command = NULL;
5583         int ret;
5584         SE_PRIV se_printop = SE_PRINT_OPERATOR;
5585         bool is_print_op = false;
5586
5587         if ( !*cmd ) {
5588                 return WERR_ACCESS_DENIED;
5589         }
5590
5591         command = talloc_asprintf(ctx,
5592                         "%s \"%s\" \"%s\"", cmd, portname, uri );
5593         if (!command) {
5594                 return WERR_NOMEM;
5595         }
5596
5597         if ( token )
5598                 is_print_op = user_has_privileges( token, &se_printop );
5599
5600         DEBUG(10,("Running [%s]\n", command));
5601
5602         /********* BEGIN SePrintOperatorPrivilege **********/
5603
5604         if ( is_print_op )
5605                 become_root();
5606
5607         ret = smbrun(command, NULL);
5608
5609         if ( is_print_op )
5610                 unbecome_root();
5611
5612         /********* END SePrintOperatorPrivilege **********/
5613
5614         DEBUGADD(10,("returned [%d]\n", ret));
5615
5616         TALLOC_FREE(command);
5617
5618         if ( ret != 0 ) {
5619                 return WERR_ACCESS_DENIED;
5620         }
5621
5622         return WERR_OK;
5623 }
5624
5625 /****************************************************************************
5626 ****************************************************************************/
5627
5628 static bool add_printer_hook(TALLOC_CTX *ctx, NT_USER_TOKEN *token,
5629                              struct spoolss_SetPrinterInfo2 *info2,
5630                              const char *remote_machine,
5631                              struct messaging_context *msg_ctx)
5632 {
5633         char *cmd = lp_addprinter_cmd();
5634         char **qlines;
5635         char *command = NULL;
5636         int numlines;
5637         int ret;
5638         int fd;
5639         SE_PRIV se_printop = SE_PRINT_OPERATOR;
5640         bool is_print_op = false;
5641
5642         if (!remote_machine) {
5643                 return false;
5644         }
5645
5646         command = talloc_asprintf(ctx,
5647                         "%s \"%s\" \"%s\" \"%s\" \"%s\" \"%s\" \"%s\" \"%s\"",
5648                         cmd, info2->printername, info2->sharename,
5649                         info2->portname, info2->drivername,
5650                         info2->location, info2->comment, remote_machine);
5651         if (!command) {
5652                 return false;
5653         }
5654
5655         if ( token )
5656                 is_print_op = user_has_privileges( token, &se_printop );
5657
5658         DEBUG(10,("Running [%s]\n", command));
5659
5660         /********* BEGIN SePrintOperatorPrivilege **********/
5661
5662         if ( is_print_op )
5663                 become_root();
5664
5665         if ( (ret = smbrun(command, &fd)) == 0 ) {
5666                 /* Tell everyone we updated smb.conf. */
5667                 message_send_all(msg_ctx, MSG_SMB_CONF_UPDATED, NULL, 0, NULL);
5668         }
5669
5670         if ( is_print_op )
5671                 unbecome_root();
5672
5673         /********* END SePrintOperatorPrivilege **********/
5674
5675         DEBUGADD(10,("returned [%d]\n", ret));
5676
5677         TALLOC_FREE(command);
5678
5679         if ( ret != 0 ) {
5680                 if (fd != -1)
5681                         close(fd);
5682                 return false;
5683         }
5684
5685         /* reload our services immediately */
5686         become_root();
5687         reload_services(msg_ctx, false);
5688         unbecome_root();
5689
5690         numlines = 0;
5691         /* Get lines and convert them back to dos-codepage */
5692         qlines = fd_lines_load(fd, &numlines, 0, NULL);
5693         DEBUGADD(10,("Lines returned = [%d]\n", numlines));
5694         close(fd);
5695
5696         /* Set the portname to what the script says the portname should be. */
5697         /* but don't require anything to be return from the script exit a good error code */
5698
5699         if (numlines) {
5700                 /* Set the portname to what the script says the portname should be. */
5701                 info2->portname = talloc_strdup(ctx, qlines[0]);
5702                 DEBUGADD(6,("Line[0] = [%s]\n", qlines[0]));
5703         }
5704
5705         TALLOC_FREE(qlines);
5706         return true;
5707 }
5708
5709 static WERROR update_dsspooler(TALLOC_CTX *mem_ctx,
5710                                struct auth_serversupplied_info *server_info,
5711                                struct messaging_context *msg_ctx,
5712                                int snum,
5713                                struct spoolss_SetPrinterInfo2 *printer,
5714                                struct spoolss_PrinterInfo2 *old_printer)
5715 {
5716         bool force_update = (old_printer == NULL);
5717         const char *dnsdomname;
5718         const char *longname;
5719         const char *uncname;
5720         const char *spooling;
5721         DATA_BLOB buffer;
5722         WERROR result = WERR_OK;
5723
5724         if (force_update || !strequal(printer->drivername, old_printer->drivername)) {
5725                 push_reg_sz(mem_ctx, &buffer, printer->drivername);
5726                 winreg_set_printer_dataex(mem_ctx,
5727                                           server_info,
5728                                           msg_ctx,
5729                                           printer->sharename,
5730                                           SPOOL_DSSPOOLER_KEY,
5731                                           SPOOL_REG_DRIVERNAME,
5732                                           REG_SZ,
5733                                           buffer.data,
5734                                           buffer.length);
5735
5736                 if (!force_update) {
5737                         DEBUG(10,("update_printer: changing driver [%s]!  Sending event!\n",
5738                                 printer->drivername));
5739
5740                         notify_printer_driver(snum, printer->drivername);
5741                 }
5742         }
5743
5744         if (force_update || !strequal(printer->comment, old_printer->comment)) {
5745                 push_reg_sz(mem_ctx, &buffer, printer->comment);
5746                 winreg_set_printer_dataex(mem_ctx,
5747                                           server_info,
5748                                           msg_ctx,
5749                                           printer->sharename,
5750                                           SPOOL_DSSPOOLER_KEY,
5751                                           SPOOL_REG_DESCRIPTION,
5752                                           REG_SZ,
5753                                           buffer.data,
5754                                           buffer.length);
5755
5756                 if (!force_update) {
5757                         notify_printer_comment(snum, printer->comment);
5758                 }
5759         }
5760
5761         if (force_update || !strequal(printer->sharename, old_printer->sharename)) {
5762                 push_reg_sz(mem_ctx, &buffer, printer->sharename);
5763                 winreg_set_printer_dataex(mem_ctx,
5764                                           server_info,
5765                                           msg_ctx,
5766                                           printer->sharename,
5767                                           SPOOL_DSSPOOLER_KEY,
5768                                           SPOOL_REG_PRINTSHARENAME,
5769                                           REG_SZ,
5770                                           buffer.data,
5771                                           buffer.length);
5772
5773                 if (!force_update) {
5774                         notify_printer_sharename(snum, printer->sharename);
5775                 }
5776         }
5777
5778         if (force_update || !strequal(printer->printername, old_printer->printername)) {
5779                 const char *p;
5780
5781                 p = strrchr(printer->printername, '\\' );
5782                 if (p != NULL) {
5783                         p++;
5784                 } else {
5785                         p = printer->printername;
5786                 }
5787
5788                 push_reg_sz(mem_ctx, &buffer, p);
5789                 winreg_set_printer_dataex(mem_ctx,
5790                                           server_info,
5791                                           msg_ctx,
5792                                           printer->sharename,
5793                                           SPOOL_DSSPOOLER_KEY,
5794                                           SPOOL_REG_PRINTERNAME,
5795                                           REG_SZ,
5796                                           buffer.data,
5797                                           buffer.length);
5798
5799                 if (!force_update) {
5800                         notify_printer_printername(snum, p);
5801                 }
5802         }
5803
5804         if (force_update || !strequal(printer->portname, old_printer->portname)) {
5805                 push_reg_sz(mem_ctx, &buffer, printer->portname);
5806                 winreg_set_printer_dataex(mem_ctx,
5807                                           server_info,
5808                                           msg_ctx,
5809                                           printer->sharename,
5810                                           SPOOL_DSSPOOLER_KEY,
5811                                           SPOOL_REG_PORTNAME,
5812                                           REG_SZ,
5813                                           buffer.data,
5814                                           buffer.length);
5815
5816                 if (!force_update) {
5817                         notify_printer_port(snum, printer->portname);
5818                 }
5819         }
5820
5821         if (force_update || !strequal(printer->location, old_printer->location)) {
5822                 push_reg_sz(mem_ctx, &buffer, printer->location);
5823                 winreg_set_printer_dataex(mem_ctx,
5824                                           server_info,
5825                                           msg_ctx,
5826                                           printer->sharename,
5827                                           SPOOL_DSSPOOLER_KEY,
5828                                           SPOOL_REG_LOCATION,
5829                                           REG_SZ,
5830                                           buffer.data,
5831                                           buffer.length);
5832
5833                 if (!force_update) {
5834                         notify_printer_location(snum, printer->location);
5835                 }
5836         }
5837
5838         if (force_update || !strequal(printer->sepfile, old_printer->sepfile)) {
5839                 push_reg_sz(mem_ctx, &buffer, printer->sepfile);
5840                 winreg_set_printer_dataex(mem_ctx,
5841                                           server_info,
5842                                           msg_ctx,
5843                                           printer->sharename,
5844                                           SPOOL_DSSPOOLER_KEY,
5845                                           SPOOL_REG_PRINTSEPARATORFILE,
5846                                           REG_SZ,
5847                                           buffer.data,
5848                                           buffer.length);
5849
5850                 if (!force_update) {
5851                         notify_printer_location(snum, printer->location);
5852                 }
5853         }
5854
5855         if (force_update || printer->starttime != old_printer->starttime) {
5856                 buffer = data_blob_talloc(mem_ctx, NULL, 4);
5857                 SIVAL(buffer.data, 0, printer->starttime);
5858                 winreg_set_printer_dataex(mem_ctx,
5859                                           server_info,
5860                                           msg_ctx,
5861                                           printer->sharename,
5862                                           SPOOL_DSSPOOLER_KEY,
5863                                           SPOOL_REG_PRINTSTARTTIME,
5864                                           REG_DWORD,
5865                                           buffer.data,
5866                                           buffer.length);
5867         }
5868
5869         if (force_update || printer->untiltime != old_printer->untiltime) {
5870                 buffer = data_blob_talloc(mem_ctx, NULL, 4);
5871                 SIVAL(buffer.data, 0, printer->untiltime);
5872                 winreg_set_printer_dataex(mem_ctx,
5873                                           server_info,
5874                                           msg_ctx,
5875                                           printer->sharename,
5876                                           SPOOL_DSSPOOLER_KEY,
5877                                           SPOOL_REG_PRINTENDTIME,
5878                                           REG_DWORD,
5879                                           buffer.data,
5880                                           buffer.length);
5881         }
5882
5883         if (force_update || printer->priority != old_printer->priority) {
5884                 buffer = data_blob_talloc(mem_ctx, NULL, 4);
5885                 SIVAL(buffer.data, 0, printer->priority);
5886                 winreg_set_printer_dataex(mem_ctx,
5887                                           server_info,
5888                                           msg_ctx,
5889                                           printer->sharename,
5890                                           SPOOL_DSSPOOLER_KEY,
5891                                           SPOOL_REG_PRIORITY,
5892                                           REG_DWORD,
5893                                           buffer.data,
5894                                           buffer.length);
5895         }
5896
5897         if (force_update || printer->attributes != old_printer->attributes) {
5898                 buffer = data_blob_talloc(mem_ctx, NULL, 4);
5899                 SIVAL(buffer.data, 0, (printer->attributes &
5900                                        PRINTER_ATTRIBUTE_KEEPPRINTEDJOBS));
5901                 winreg_set_printer_dataex(mem_ctx,
5902                                           server_info,
5903                                           msg_ctx,
5904                                           printer->sharename,
5905                                           SPOOL_DSSPOOLER_KEY,
5906                                           SPOOL_REG_PRINTKEEPPRINTEDJOBS,
5907                                           REG_DWORD,
5908                                           buffer.data,
5909                                           buffer.length);
5910
5911                 switch (printer->attributes & 0x3) {
5912                         case 0:
5913                                 spooling = SPOOL_REGVAL_PRINTWHILESPOOLING;
5914                                 break;
5915                         case 1:
5916                                 spooling = SPOOL_REGVAL_PRINTAFTERSPOOLED;
5917                                 break;
5918                         case 2:
5919                                 spooling = SPOOL_REGVAL_PRINTDIRECT;
5920                                 break;
5921                         default:
5922                                 spooling = "unknown";
5923                 }
5924                 push_reg_sz(mem_ctx, &buffer, spooling);
5925                 winreg_set_printer_dataex(mem_ctx,
5926                                           server_info,
5927                                           msg_ctx,
5928                                           printer->sharename,
5929                                           SPOOL_DSSPOOLER_KEY,
5930                                           SPOOL_REG_PRINTSPOOLING,
5931                                           REG_SZ,
5932                                           buffer.data,
5933                                           buffer.length);
5934         }
5935
5936         push_reg_sz(mem_ctx, &buffer, global_myname());
5937         winreg_set_printer_dataex(mem_ctx,
5938                                   server_info,
5939                                   msg_ctx,
5940                                   printer->sharename,
5941                                   SPOOL_DSSPOOLER_KEY,
5942                                   SPOOL_REG_SHORTSERVERNAME,
5943                                   REG_SZ,
5944                                   buffer.data,
5945                                   buffer.length);
5946
5947         dnsdomname = get_mydnsfullname();
5948         if (dnsdomname != NULL && dnsdomname[0] != '\0') {
5949                 longname = talloc_strdup(mem_ctx, dnsdomname);
5950         } else {
5951                 longname = talloc_strdup(mem_ctx, global_myname());
5952         }
5953         if (longname == NULL) {
5954                 result = WERR_NOMEM;
5955                 goto done;
5956         }
5957
5958         push_reg_sz(mem_ctx, &buffer, longname);
5959         winreg_set_printer_dataex(mem_ctx,
5960                                   server_info,
5961                                   msg_ctx,
5962                                   printer->sharename,
5963                                   SPOOL_DSSPOOLER_KEY,
5964                                   SPOOL_REG_SERVERNAME,
5965                                   REG_SZ,
5966                                   buffer.data,
5967                                   buffer.length);
5968
5969         uncname = talloc_asprintf(mem_ctx, "\\\\%s\\%s",
5970                                   global_myname(), printer->sharename);
5971         push_reg_sz(mem_ctx, &buffer, uncname);
5972         winreg_set_printer_dataex(mem_ctx,
5973                                   server_info,
5974                                   msg_ctx,
5975                                   printer->sharename,
5976                                   SPOOL_DSSPOOLER_KEY,
5977                                   SPOOL_REG_UNCNAME,
5978                                   REG_SZ,
5979                                   buffer.data,
5980                                   buffer.length);
5981
5982 done:
5983         return result;
5984 }
5985
5986 /********************************************************************
5987  * Called by spoolss_api_setprinter
5988  * when updating a printer description.
5989  ********************************************************************/
5990
5991 static WERROR update_printer(struct pipes_struct *p,
5992                              struct policy_handle *handle,
5993                              struct spoolss_SetPrinterInfoCtr *info_ctr,
5994                              struct spoolss_DeviceMode *devmode)
5995 {
5996         uint32_t printer_mask = SPOOLSS_PRINTER_INFO_ALL;
5997         struct spoolss_SetPrinterInfo2 *printer = info_ctr->info.info2;
5998         struct spoolss_PrinterInfo2 *old_printer;
5999         Printer_entry *Printer = find_printer_index_by_hnd(p, handle);
6000         const char *servername = NULL;
6001         int snum;
6002         WERROR result = WERR_OK;
6003         TALLOC_CTX *tmp_ctx;
6004
6005         DEBUG(8,("update_printer\n"));
6006
6007         tmp_ctx = talloc_new(p->mem_ctx);
6008         if (tmp_ctx == NULL) {
6009                 return WERR_NOMEM;
6010         }
6011
6012         if (!Printer) {
6013                 result = WERR_BADFID;
6014                 goto done;
6015         }
6016
6017         if (!get_printer_snum(p, handle, &snum, NULL)) {
6018                 result = WERR_BADFID;
6019                 goto done;
6020         }
6021
6022         if (Printer != NULL || Printer->servername != NULL) {
6023                 servername = Printer->servername;
6024         }
6025
6026         result = winreg_get_printer(tmp_ctx,
6027                                     p->server_info,
6028                                     p->msg_ctx,
6029                                     servername,
6030                                     lp_const_servicename(snum),
6031                                     &old_printer);
6032         if (!W_ERROR_IS_OK(result)) {
6033                 result = WERR_BADFID;
6034                 goto done;
6035         }
6036
6037         /* Do sanity check on the requested changes for Samba */
6038         if (!check_printer_ok(tmp_ctx, printer, snum)) {
6039                 result = WERR_INVALID_PARAM;
6040                 goto done;
6041         }
6042
6043         /* FIXME!!! If the driver has changed we really should verify that
6044            it is installed before doing much else   --jerry */
6045
6046         /* Check calling user has permission to update printer description */
6047         if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
6048                 DEBUG(3, ("update_printer: printer property change denied by handle\n"));
6049                 result = WERR_ACCESS_DENIED;
6050                 goto done;
6051         }
6052
6053         /* Call addprinter hook */
6054         /* Check changes to see if this is really needed */
6055
6056         if (*lp_addprinter_cmd() &&
6057                         (!strequal(printer->drivername, old_printer->drivername) ||
6058                          !strequal(printer->comment, old_printer->comment) ||
6059                          !strequal(printer->portname, old_printer->portname) ||
6060                          !strequal(printer->location, old_printer->location)) )
6061         {
6062                 /* add_printer_hook() will call reload_services() */
6063                 if (!add_printer_hook(tmp_ctx, p->server_info->ptok,
6064                                       printer, p->client_address,
6065                                       p->msg_ctx)) {
6066                         result = WERR_ACCESS_DENIED;
6067                         goto done;
6068                 }
6069         }
6070
6071         update_dsspooler(tmp_ctx,
6072                          p->server_info,
6073                          p->msg_ctx,
6074                          snum,
6075                          printer,
6076                          old_printer);
6077
6078         printer_mask &= ~SPOOLSS_PRINTER_INFO_SECDESC;
6079
6080         if (devmode == NULL) {
6081                 printer_mask &= ~SPOOLSS_PRINTER_INFO_DEVMODE;
6082         }
6083         result = winreg_update_printer(tmp_ctx,
6084                                        p->server_info,
6085                                        p->msg_ctx,
6086                                        printer->sharename,
6087                                        printer_mask,
6088                                        printer,
6089                                        devmode,
6090                                        NULL);
6091
6092 done:
6093         talloc_free(tmp_ctx);
6094
6095         return result;
6096 }
6097
6098 /****************************************************************************
6099 ****************************************************************************/
6100 static WERROR publish_or_unpublish_printer(struct pipes_struct *p,
6101                                            struct policy_handle *handle,
6102                                            struct spoolss_SetPrinterInfo7 *info7)
6103 {
6104 #ifdef HAVE_ADS
6105         struct spoolss_PrinterInfo2 *pinfo2 = NULL;
6106         WERROR result;
6107         int snum;
6108         Printer_entry *Printer;
6109
6110         if ( lp_security() != SEC_ADS ) {
6111                 return WERR_UNKNOWN_LEVEL;
6112         }
6113
6114         Printer = find_printer_index_by_hnd(p, handle);
6115
6116         DEBUG(5,("publish_or_unpublish_printer, action = %d\n",info7->action));
6117
6118         if (!Printer)
6119                 return WERR_BADFID;
6120
6121         if (!get_printer_snum(p, handle, &snum, NULL))
6122                 return WERR_BADFID;
6123
6124         result = winreg_get_printer(p->mem_ctx, p->server_info, p->msg_ctx,
6125                                     Printer->servername,
6126                                     lp_servicename(snum), &pinfo2);
6127         if (!W_ERROR_IS_OK(result)) {
6128                 return WERR_BADFID;
6129         }
6130
6131         nt_printer_publish(pinfo2, p->server_info, p->msg_ctx, pinfo2,
6132                            info7->action);
6133
6134         TALLOC_FREE(pinfo2);
6135         return WERR_OK;
6136 #else
6137         return WERR_UNKNOWN_LEVEL;
6138 #endif
6139 }
6140
6141 /********************************************************************
6142  ********************************************************************/
6143
6144 static WERROR update_printer_devmode(struct pipes_struct *p,
6145                                      struct policy_handle *handle,
6146                                      struct spoolss_DeviceMode *devmode)
6147 {
6148         int snum;
6149         Printer_entry *Printer = find_printer_index_by_hnd(p, handle);
6150         uint32_t info2_mask = SPOOLSS_PRINTER_INFO_DEVMODE;
6151
6152         DEBUG(8,("update_printer_devmode\n"));
6153
6154         if (!Printer) {
6155                 return WERR_BADFID;
6156         }
6157
6158         if (!get_printer_snum(p, handle, &snum, NULL)) {
6159                 return WERR_BADFID;
6160         }
6161
6162         /* Check calling user has permission to update printer description */
6163         if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
6164                 DEBUG(3, ("update_printer: printer property change denied by handle\n"));
6165                 return WERR_ACCESS_DENIED;
6166         }
6167
6168         return winreg_update_printer(p->mem_ctx,
6169                                      p->server_info,
6170                                      p->msg_ctx,
6171                                      lp_const_servicename(snum),
6172                                      info2_mask,
6173                                      NULL,
6174                                      devmode,
6175                                      NULL);
6176 }
6177
6178
6179 /****************************************************************
6180  _spoolss_SetPrinter
6181 ****************************************************************/
6182
6183 WERROR _spoolss_SetPrinter(struct pipes_struct *p,
6184                            struct spoolss_SetPrinter *r)
6185 {
6186         WERROR result;
6187
6188         Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
6189
6190         if (!Printer) {
6191                 DEBUG(2,("_spoolss_SetPrinter: Invalid handle (%s:%u:%u)\n",
6192                         OUR_HANDLE(r->in.handle)));
6193                 return WERR_BADFID;
6194         }
6195
6196         /* check the level */
6197         switch (r->in.info_ctr->level) {
6198                 case 0:
6199                         return control_printer(r->in.handle, r->in.command, p);
6200                 case 2:
6201                         result = update_printer(p, r->in.handle,
6202                                                 r->in.info_ctr,
6203                                                 r->in.devmode_ctr->devmode);
6204                         if (!W_ERROR_IS_OK(result))
6205                                 return result;
6206                         if (r->in.secdesc_ctr->sd)
6207                                 result = update_printer_sec(r->in.handle, p,
6208                                                             r->in.secdesc_ctr);
6209                         return result;
6210                 case 3:
6211                         return update_printer_sec(r->in.handle, p,
6212                                                   r->in.secdesc_ctr);
6213                 case 7:
6214                         return publish_or_unpublish_printer(p, r->in.handle,
6215                                                             r->in.info_ctr->info.info7);
6216                 case 8:
6217                         return update_printer_devmode(p, r->in.handle,
6218                                                       r->in.devmode_ctr->devmode);
6219                 default:
6220                         return WERR_UNKNOWN_LEVEL;
6221         }
6222 }
6223
6224 /****************************************************************
6225  _spoolss_FindClosePrinterNotify
6226 ****************************************************************/
6227
6228 WERROR _spoolss_FindClosePrinterNotify(struct pipes_struct *p,
6229                                        struct spoolss_FindClosePrinterNotify *r)
6230 {
6231         Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
6232
6233         if (!Printer) {
6234                 DEBUG(2,("_spoolss_FindClosePrinterNotify: "
6235                         "Invalid handle (%s:%u:%u)\n", OUR_HANDLE(r->in.handle)));
6236                 return WERR_BADFID;
6237         }
6238
6239         if (Printer->notify.client_connected == true) {
6240                 int snum = -1;
6241
6242                 if ( Printer->printer_type == SPLHND_SERVER)
6243                         snum = -1;
6244                 else if ( (Printer->printer_type == SPLHND_PRINTER) &&
6245                                 !get_printer_snum(p, r->in.handle, &snum, NULL) )
6246                         return WERR_BADFID;
6247
6248                 srv_spoolss_replycloseprinter(
6249                         snum, &Printer->notify.client_hnd, p->msg_ctx);
6250         }
6251
6252         Printer->notify.flags=0;
6253         Printer->notify.options=0;
6254         Printer->notify.localmachine[0]='\0';
6255         Printer->notify.printerlocal=0;
6256         TALLOC_FREE(Printer->notify.option);
6257         Printer->notify.client_connected = false;
6258
6259         return WERR_OK;
6260 }
6261
6262 /****************************************************************
6263  _spoolss_AddJob
6264 ****************************************************************/
6265
6266 WERROR _spoolss_AddJob(struct pipes_struct *p,
6267                        struct spoolss_AddJob *r)
6268 {
6269         if (!r->in.buffer && (r->in.offered != 0)) {
6270                 return WERR_INVALID_PARAM;
6271         }
6272
6273         /* this is what a NT server returns for AddJob. AddJob must fail on
6274          * non-local printers */
6275
6276         if (r->in.level != 1) {
6277                 return WERR_UNKNOWN_LEVEL;
6278         }
6279
6280         return WERR_INVALID_PARAM;
6281 }
6282
6283 /****************************************************************************
6284 fill_job_info1
6285 ****************************************************************************/
6286
6287 static WERROR fill_job_info1(TALLOC_CTX *mem_ctx,
6288                              struct spoolss_JobInfo1 *r,
6289                              const print_queue_struct *queue,
6290                              int position, int snum,
6291                              struct spoolss_PrinterInfo2 *pinfo2)
6292 {
6293         struct tm *t;
6294
6295         t = gmtime(&queue->time);
6296
6297         r->job_id               = queue->job;
6298
6299         r->printer_name         = talloc_strdup(mem_ctx, lp_servicename(snum));
6300         W_ERROR_HAVE_NO_MEMORY(r->printer_name);
6301         r->server_name          = talloc_strdup(mem_ctx, pinfo2->servername);
6302         W_ERROR_HAVE_NO_MEMORY(r->server_name);
6303         r->user_name            = talloc_strdup(mem_ctx, queue->fs_user);
6304         W_ERROR_HAVE_NO_MEMORY(r->user_name);
6305         r->document_name        = talloc_strdup(mem_ctx, queue->fs_file);
6306         W_ERROR_HAVE_NO_MEMORY(r->document_name);
6307         r->data_type            = talloc_strdup(mem_ctx, "RAW");
6308         W_ERROR_HAVE_NO_MEMORY(r->data_type);
6309         r->text_status          = talloc_strdup(mem_ctx, "");
6310         W_ERROR_HAVE_NO_MEMORY(r->text_status);
6311
6312         r->status               = nt_printj_status(queue->status);
6313         r->priority             = queue->priority;
6314         r->position             = position;
6315         r->total_pages          = queue->page_count;
6316         r->pages_printed        = 0; /* ??? */
6317
6318         init_systemtime(&r->submitted, t);
6319
6320         return WERR_OK;
6321 }
6322
6323 /****************************************************************************
6324 fill_job_info2
6325 ****************************************************************************/
6326
6327 static WERROR fill_job_info2(TALLOC_CTX *mem_ctx,
6328                              struct spoolss_JobInfo2 *r,
6329                              const print_queue_struct *queue,
6330                              int position, int snum,
6331                              struct spoolss_PrinterInfo2 *pinfo2,
6332                              struct spoolss_DeviceMode *devmode)
6333 {
6334         struct tm *t;
6335
6336         t = gmtime(&queue->time);
6337
6338         r->job_id               = queue->job;
6339
6340         r->printer_name         = talloc_strdup(mem_ctx, lp_servicename(snum));
6341         W_ERROR_HAVE_NO_MEMORY(r->printer_name);
6342         r->server_name          = talloc_strdup(mem_ctx, pinfo2->servername);
6343         W_ERROR_HAVE_NO_MEMORY(r->server_name);
6344         r->user_name            = talloc_strdup(mem_ctx, queue->fs_user);
6345         W_ERROR_HAVE_NO_MEMORY(r->user_name);
6346         r->document_name        = talloc_strdup(mem_ctx, queue->fs_file);
6347         W_ERROR_HAVE_NO_MEMORY(r->document_name);
6348         r->notify_name          = talloc_strdup(mem_ctx, queue->fs_user);
6349         W_ERROR_HAVE_NO_MEMORY(r->notify_name);
6350         r->data_type            = talloc_strdup(mem_ctx, "RAW");
6351         W_ERROR_HAVE_NO_MEMORY(r->data_type);
6352         r->print_processor      = talloc_strdup(mem_ctx, "winprint");
6353         W_ERROR_HAVE_NO_MEMORY(r->print_processor);
6354         r->parameters           = talloc_strdup(mem_ctx, "");
6355         W_ERROR_HAVE_NO_MEMORY(r->parameters);
6356         r->driver_name          = talloc_strdup(mem_ctx, pinfo2->drivername);
6357         W_ERROR_HAVE_NO_MEMORY(r->driver_name);
6358
6359         r->devmode              = devmode;
6360
6361         r->text_status          = talloc_strdup(mem_ctx, "");
6362         W_ERROR_HAVE_NO_MEMORY(r->text_status);
6363
6364         r->secdesc              = NULL;
6365
6366         r->status               = nt_printj_status(queue->status);
6367         r->priority             = queue->priority;
6368         r->position             = position;
6369         r->start_time           = 0;
6370         r->until_time           = 0;
6371         r->total_pages          = queue->page_count;
6372         r->size                 = queue->size;
6373         init_systemtime(&r->submitted, t);
6374         r->time                 = 0;
6375         r->pages_printed        = 0; /* ??? */
6376
6377         return WERR_OK;
6378 }
6379
6380 /****************************************************************************
6381 fill_job_info3
6382 ****************************************************************************/
6383
6384 static WERROR fill_job_info3(TALLOC_CTX *mem_ctx,
6385                              struct spoolss_JobInfo3 *r,
6386                              const print_queue_struct *queue,
6387                              const print_queue_struct *next_queue,
6388                              int position, int snum,
6389                              struct spoolss_PrinterInfo2 *pinfo2)
6390 {
6391         r->job_id               = queue->job;
6392         r->next_job_id          = 0;
6393         if (next_queue) {
6394                 r->next_job_id  = next_queue->job;
6395         }
6396         r->reserved             = 0;
6397
6398         return WERR_OK;
6399 }
6400
6401 /****************************************************************************
6402  Enumjobs at level 1.
6403 ****************************************************************************/
6404
6405 static WERROR enumjobs_level1(TALLOC_CTX *mem_ctx,
6406                               const print_queue_struct *queue,
6407                               uint32_t num_queues, int snum,
6408                               struct spoolss_PrinterInfo2 *pinfo2,
6409                               union spoolss_JobInfo **info_p,
6410                               uint32_t *count)
6411 {
6412         union spoolss_JobInfo *info;
6413         int i;
6414         WERROR result = WERR_OK;
6415
6416         info = TALLOC_ARRAY(mem_ctx, union spoolss_JobInfo, num_queues);
6417         W_ERROR_HAVE_NO_MEMORY(info);
6418
6419         *count = num_queues;
6420
6421         for (i=0; i<*count; i++) {
6422                 result = fill_job_info1(info,
6423                                         &info[i].info1,
6424                                         &queue[i],
6425                                         i,
6426                                         snum,
6427                                         pinfo2);
6428                 if (!W_ERROR_IS_OK(result)) {
6429                         goto out;
6430                 }
6431         }
6432
6433  out:
6434         if (!W_ERROR_IS_OK(result)) {
6435                 TALLOC_FREE(info);
6436                 *count = 0;
6437                 return result;
6438         }
6439
6440         *info_p = info;
6441
6442         return WERR_OK;
6443 }
6444
6445 /****************************************************************************
6446  Enumjobs at level 2.
6447 ****************************************************************************/
6448
6449 static WERROR enumjobs_level2(TALLOC_CTX *mem_ctx,
6450                               const print_queue_struct *queue,
6451                               uint32_t num_queues, int snum,
6452                               struct spoolss_PrinterInfo2 *pinfo2,
6453                               union spoolss_JobInfo **info_p,
6454                               uint32_t *count)
6455 {
6456         union spoolss_JobInfo *info;
6457         int i;
6458         WERROR result = WERR_OK;
6459
6460         info = TALLOC_ARRAY(mem_ctx, union spoolss_JobInfo, num_queues);
6461         W_ERROR_HAVE_NO_MEMORY(info);
6462
6463         *count = num_queues;
6464
6465         for (i=0; i<*count; i++) {
6466                 struct spoolss_DeviceMode *devmode;
6467
6468                 result = spoolss_create_default_devmode(info,
6469                                                         pinfo2->printername,
6470                                                         &devmode);
6471                 if (!W_ERROR_IS_OK(result)) {
6472                         DEBUG(3, ("Can't proceed w/o a devmode!"));
6473                         goto out;
6474                 }
6475
6476                 result = fill_job_info2(info,
6477                                         &info[i].info2,
6478                                         &queue[i],
6479                                         i,
6480                                         snum,
6481                                         pinfo2,
6482                                         devmode);
6483                 if (!W_ERROR_IS_OK(result)) {
6484                         goto out;
6485                 }
6486         }
6487
6488  out:
6489         if (!W_ERROR_IS_OK(result)) {
6490                 TALLOC_FREE(info);
6491                 *count = 0;
6492                 return result;
6493         }
6494
6495         *info_p = info;
6496
6497         return WERR_OK;
6498 }
6499
6500 /****************************************************************************
6501  Enumjobs at level 3.
6502 ****************************************************************************/
6503
6504 static WERROR enumjobs_level3(TALLOC_CTX *mem_ctx,
6505                               const print_queue_struct *queue,
6506                               uint32_t num_queues, int snum,
6507                               struct spoolss_PrinterInfo2 *pinfo2,
6508                               union spoolss_JobInfo **info_p,
6509                               uint32_t *count)
6510 {
6511         union spoolss_JobInfo *info;
6512         int i;
6513         WERROR result = WERR_OK;
6514
6515         info = TALLOC_ARRAY(mem_ctx, union spoolss_JobInfo, num_queues);
6516         W_ERROR_HAVE_NO_MEMORY(info);
6517
6518         *count = num_queues;
6519
6520         for (i=0; i<*count; i++) {
6521                 const print_queue_struct *next_queue = NULL;
6522
6523                 if (i+1 < *count) {
6524                         next_queue = &queue[i+1];
6525                 }
6526
6527                 result = fill_job_info3(info,
6528                                         &info[i].info3,
6529                                         &queue[i],
6530                                         next_queue,
6531                                         i,
6532                                         snum,
6533                                         pinfo2);
6534                 if (!W_ERROR_IS_OK(result)) {
6535                         goto out;
6536                 }
6537         }
6538
6539  out:
6540         if (!W_ERROR_IS_OK(result)) {
6541                 TALLOC_FREE(info);
6542                 *count = 0;
6543                 return result;
6544         }
6545
6546         *info_p = info;
6547
6548         return WERR_OK;
6549 }
6550
6551 /****************************************************************
6552  _spoolss_EnumJobs
6553 ****************************************************************/
6554
6555 WERROR _spoolss_EnumJobs(struct pipes_struct *p,
6556                          struct spoolss_EnumJobs *r)
6557 {
6558         WERROR result;
6559         struct spoolss_PrinterInfo2 *pinfo2 = NULL;
6560         int snum;
6561         print_status_struct prt_status;
6562         print_queue_struct *queue = NULL;
6563         uint32_t count;
6564
6565         /* that's an [in out] buffer */
6566
6567         if (!r->in.buffer && (r->in.offered != 0)) {
6568                 return WERR_INVALID_PARAM;
6569         }
6570
6571         DEBUG(4,("_spoolss_EnumJobs\n"));
6572
6573         *r->out.needed = 0;
6574         *r->out.count = 0;
6575         *r->out.info = NULL;
6576
6577         /* lookup the printer snum and tdb entry */
6578
6579         if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
6580                 return WERR_BADFID;
6581         }
6582
6583         result = winreg_get_printer(p->mem_ctx, p->server_info, p->msg_ctx,
6584                                     NULL, lp_servicename(snum), &pinfo2);
6585         if (!W_ERROR_IS_OK(result)) {
6586                 return result;
6587         }
6588
6589         count = print_queue_status(snum, &queue, &prt_status);
6590         DEBUGADD(4,("count:[%d], status:[%d], [%s]\n",
6591                 count, prt_status.status, prt_status.message));
6592
6593         if (count == 0) {
6594                 SAFE_FREE(queue);
6595                 TALLOC_FREE(pinfo2);
6596                 return WERR_OK;
6597         }
6598
6599         switch (r->in.level) {
6600         case 1:
6601                 result = enumjobs_level1(p->mem_ctx, queue, count, snum,
6602                                          pinfo2, r->out.info, r->out.count);
6603                 break;
6604         case 2:
6605                 result = enumjobs_level2(p->mem_ctx, queue, count, snum,
6606                                          pinfo2, r->out.info, r->out.count);
6607                 break;
6608         case 3:
6609                 result = enumjobs_level3(p->mem_ctx, queue, count, snum,
6610                                          pinfo2, r->out.info, r->out.count);
6611                 break;
6612         default:
6613                 result = WERR_UNKNOWN_LEVEL;
6614                 break;
6615         }
6616
6617         SAFE_FREE(queue);
6618         TALLOC_FREE(pinfo2);
6619
6620         if (!W_ERROR_IS_OK(result)) {
6621                 return result;
6622         }
6623
6624         *r->out.needed  = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
6625                                                      spoolss_EnumJobs, 
6626                                                      *r->out.info, r->in.level,
6627                                                      *r->out.count);
6628         *r->out.info    = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
6629         *r->out.count   = SPOOLSS_BUFFER_OK(*r->out.count, 0);
6630
6631         return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
6632 }
6633
6634 /****************************************************************
6635  _spoolss_ScheduleJob
6636 ****************************************************************/
6637
6638 WERROR _spoolss_ScheduleJob(struct pipes_struct *p,
6639                             struct spoolss_ScheduleJob *r)
6640 {
6641         return WERR_OK;
6642 }
6643
6644 /****************************************************************
6645 ****************************************************************/
6646
6647 static WERROR spoolss_setjob_1(TALLOC_CTX *mem_ctx,
6648                                const char *printer_name,
6649                                uint32_t job_id,
6650                                struct spoolss_SetJobInfo1 *r)
6651 {
6652         char *old_doc_name;
6653
6654         if (!print_job_get_name(mem_ctx, printer_name, job_id, &old_doc_name)) {
6655                 return WERR_BADFID;
6656         }
6657
6658         if (strequal(old_doc_name, r->document_name)) {
6659                 return WERR_OK;
6660         }
6661
6662         if (!print_job_set_name(printer_name, job_id, r->document_name)) {
6663                 return WERR_BADFID;
6664         }
6665
6666         return WERR_OK;
6667 }
6668
6669 /****************************************************************
6670  _spoolss_SetJob
6671 ****************************************************************/
6672
6673 WERROR _spoolss_SetJob(struct pipes_struct *p,
6674                        struct spoolss_SetJob *r)
6675 {
6676         int snum;
6677         WERROR errcode = WERR_BADFUNC;
6678
6679         if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
6680                 return WERR_BADFID;
6681         }
6682
6683         if (!print_job_exists(lp_const_servicename(snum), r->in.job_id)) {
6684                 return WERR_INVALID_PRINTER_NAME;
6685         }
6686
6687         switch (r->in.command) {
6688         case SPOOLSS_JOB_CONTROL_CANCEL:
6689         case SPOOLSS_JOB_CONTROL_DELETE:
6690                 errcode = print_job_delete(p->server_info, p->msg_ctx,
6691                                            snum, r->in.job_id);
6692                 if (W_ERROR_EQUAL(errcode, WERR_PRINTER_HAS_JOBS_QUEUED)) {
6693                         errcode = WERR_OK;
6694                 }
6695                 break;
6696         case SPOOLSS_JOB_CONTROL_PAUSE:
6697                 if (print_job_pause(p->server_info, p->msg_ctx,
6698                                     snum, r->in.job_id, &errcode)) {
6699                         errcode = WERR_OK;
6700                 }
6701                 break;
6702         case SPOOLSS_JOB_CONTROL_RESTART:
6703         case SPOOLSS_JOB_CONTROL_RESUME:
6704                 if (print_job_resume(p->server_info, p->msg_ctx,
6705                                      snum, r->in.job_id, &errcode)) {
6706                         errcode = WERR_OK;
6707                 }
6708                 break;
6709         case 0:
6710                 errcode = WERR_OK;
6711                 break;
6712         default:
6713                 return WERR_UNKNOWN_LEVEL;
6714         }
6715
6716         if (!W_ERROR_IS_OK(errcode)) {
6717                 return errcode;
6718         }
6719
6720         if (r->in.ctr == NULL) {
6721                 return errcode;
6722         }
6723
6724         switch (r->in.ctr->level) {
6725         case 1:
6726                 errcode = spoolss_setjob_1(p->mem_ctx, lp_const_servicename(snum),
6727                                            r->in.job_id,
6728                                            r->in.ctr->info.info1);
6729                 break;
6730         case 2:
6731         case 3:
6732         case 4:
6733         default:
6734                 return WERR_UNKNOWN_LEVEL;
6735         }
6736
6737         return errcode;
6738 }
6739
6740 /****************************************************************************
6741  Enumerates all printer drivers by level and architecture.
6742 ****************************************************************************/
6743
6744 static WERROR enumprinterdrivers_level_by_architecture(TALLOC_CTX *mem_ctx,
6745                                                        struct auth_serversupplied_info *server_info,
6746                                                        struct messaging_context *msg_ctx,
6747                                                        const char *servername,
6748                                                        const char *architecture,
6749                                                        uint32_t level,
6750                                                        union spoolss_DriverInfo **info_p,
6751                                                        uint32_t *count_p)
6752 {
6753         int i;
6754         uint32_t version;
6755         struct spoolss_DriverInfo8 *driver;
6756         union spoolss_DriverInfo *info = NULL;
6757         uint32_t count = 0;
6758         WERROR result = WERR_OK;
6759         uint32_t num_drivers;
6760         const char **drivers;
6761
6762         *count_p = 0;
6763         *info_p = NULL;
6764
6765         for (version=0; version<DRIVER_MAX_VERSION; version++) {
6766                 result = winreg_get_driver_list(mem_ctx, server_info, msg_ctx,
6767                                                 architecture, version,
6768                                                 &num_drivers, &drivers);
6769                 if (!W_ERROR_IS_OK(result)) {
6770                         goto out;
6771                 }
6772                 DEBUG(4, ("we have:[%d] drivers in environment"
6773                           " [%s] and version [%d]\n",
6774                           num_drivers, architecture, version));
6775
6776                 if (num_drivers != 0) {
6777                         info = TALLOC_REALLOC_ARRAY(mem_ctx, info,
6778                                                     union spoolss_DriverInfo,
6779                                                     count + num_drivers);
6780                         if (!info) {
6781                                 DEBUG(0,("enumprinterdrivers_level_by_architecture: "
6782                                         "failed to enlarge driver info buffer!\n"));
6783                                 result = WERR_NOMEM;
6784                                 goto out;
6785                         }
6786                 }
6787
6788                 for (i = 0; i < num_drivers; i++) {
6789                         DEBUG(5, ("\tdriver: [%s]\n", drivers[i]));
6790
6791                         result = winreg_get_driver(mem_ctx, server_info,
6792                                                    msg_ctx,
6793                                                    architecture, drivers[i],
6794                                                    version, &driver);
6795                         if (!W_ERROR_IS_OK(result)) {
6796                                 goto out;
6797                         }
6798
6799                         switch (level) {
6800                         case 1:
6801                                 result = fill_printer_driver_info1(info, &info[count+i].info1,
6802                                                                    driver, servername);
6803                                 break;
6804                         case 2:
6805                                 result = fill_printer_driver_info2(info, &info[count+i].info2,
6806                                                                    driver, servername);
6807                                 break;
6808                         case 3:
6809                                 result = fill_printer_driver_info3(info, &info[count+i].info3,
6810                                                                    driver, servername);
6811                                 break;
6812                         case 4:
6813                                 result = fill_printer_driver_info4(info, &info[count+i].info4,
6814                                                                    driver, servername);
6815                                 break;
6816                         case 5:
6817                                 result = fill_printer_driver_info5(info, &info[count+i].info5,
6818                                                                    driver, servername);
6819                                 break;
6820                         case 6:
6821                                 result = fill_printer_driver_info6(info, &info[count+i].info6,
6822                                                                    driver, servername);
6823                                 break;
6824                         case 8:
6825                                 result = fill_printer_driver_info8(info, &info[count+i].info8,
6826                                                                    driver, servername);
6827                                 break;
6828                         default:
6829                                 result = WERR_UNKNOWN_LEVEL;
6830                                 break;
6831                         }
6832
6833                         TALLOC_FREE(driver);
6834
6835                         if (!W_ERROR_IS_OK(result)) {
6836                                 goto out;
6837                         }
6838                 }
6839
6840                 count += num_drivers;
6841                 TALLOC_FREE(drivers);
6842         }
6843
6844  out:
6845         TALLOC_FREE(drivers);
6846
6847         if (!W_ERROR_IS_OK(result)) {
6848                 TALLOC_FREE(info);
6849                 return result;
6850         }
6851
6852         *info_p = info;
6853         *count_p = count;
6854
6855         return WERR_OK;
6856 }
6857
6858 /****************************************************************************
6859  Enumerates all printer drivers by level.
6860 ****************************************************************************/
6861
6862 static WERROR enumprinterdrivers_level(TALLOC_CTX *mem_ctx,
6863                                        struct auth_serversupplied_info *server_info,
6864                                        struct messaging_context *msg_ctx,
6865                                        const char *servername,
6866                                        const char *architecture,
6867                                        uint32_t level,
6868                                        union spoolss_DriverInfo **info_p,
6869                                        uint32_t *count_p)
6870 {
6871         uint32_t a,i;
6872         WERROR result = WERR_OK;
6873
6874         if (strequal(architecture, SPOOLSS_ARCHITECTURE_ALL)) {
6875
6876                 for (a=0; archi_table[a].long_archi != NULL; a++) {
6877
6878                         union spoolss_DriverInfo *info = NULL;
6879                         uint32_t count = 0;
6880
6881                         result = enumprinterdrivers_level_by_architecture(mem_ctx,
6882                                                                           server_info,
6883                                                                           msg_ctx,
6884                                                                           servername,
6885                                                                           archi_table[a].long_archi,
6886                                                                           level,
6887                                                                           &info,
6888                                                                           &count);
6889                         if (!W_ERROR_IS_OK(result)) {
6890                                 continue;
6891                         }
6892
6893                         for (i=0; i < count; i++) {
6894                                 ADD_TO_ARRAY(mem_ctx, union spoolss_DriverInfo,
6895                                              info[i], info_p, count_p);
6896                         }
6897                 }
6898
6899                 return result;
6900         }
6901
6902         return enumprinterdrivers_level_by_architecture(mem_ctx,
6903                                                         server_info,
6904                                                         msg_ctx,
6905                                                         servername,
6906                                                         architecture,
6907                                                         level,
6908                                                         info_p,
6909                                                         count_p);
6910 }
6911
6912 /****************************************************************
6913  _spoolss_EnumPrinterDrivers
6914 ****************************************************************/
6915
6916 WERROR _spoolss_EnumPrinterDrivers(struct pipes_struct *p,
6917                                    struct spoolss_EnumPrinterDrivers *r)
6918 {
6919         const char *cservername;
6920         WERROR result;
6921
6922         /* that's an [in out] buffer */
6923
6924         if (!r->in.buffer && (r->in.offered != 0)) {
6925                 return WERR_INVALID_PARAM;
6926         }
6927
6928         DEBUG(4,("_spoolss_EnumPrinterDrivers\n"));
6929
6930         *r->out.needed = 0;
6931         *r->out.count = 0;
6932         *r->out.info = NULL;
6933
6934         cservername = canon_servername(r->in.server);
6935
6936         if (!is_myname_or_ipaddr(cservername)) {
6937                 return WERR_UNKNOWN_PRINTER_DRIVER;
6938         }
6939
6940         result = enumprinterdrivers_level(p->mem_ctx,
6941                                           p->server_info,
6942                                           p->msg_ctx,
6943                                           cservername,
6944                                           r->in.environment,
6945                                           r->in.level,
6946                                           r->out.info,
6947                                           r->out.count);
6948         if (!W_ERROR_IS_OK(result)) {
6949                 return result;
6950         }
6951
6952         *r->out.needed  = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
6953                                                      spoolss_EnumPrinterDrivers, 
6954                                                      *r->out.info, r->in.level,
6955                                                      *r->out.count);
6956         *r->out.info    = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
6957         *r->out.count   = SPOOLSS_BUFFER_OK(*r->out.count, 0);
6958
6959         return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
6960 }
6961
6962 /****************************************************************
6963  _spoolss_EnumForms
6964 ****************************************************************/
6965
6966 WERROR _spoolss_EnumForms(struct pipes_struct *p,
6967                           struct spoolss_EnumForms *r)
6968 {
6969         WERROR result;
6970
6971         *r->out.count = 0;
6972         *r->out.needed = 0;
6973         *r->out.info = NULL;
6974
6975         /* that's an [in out] buffer */
6976
6977         if (!r->in.buffer && (r->in.offered != 0) ) {
6978                 return WERR_INVALID_PARAM;
6979         }
6980
6981         DEBUG(4,("_spoolss_EnumForms\n"));
6982         DEBUGADD(5,("Offered buffer size [%d]\n", r->in.offered));
6983         DEBUGADD(5,("Info level [%d]\n",          r->in.level));
6984
6985         switch (r->in.level) {
6986         case 1:
6987                 result = winreg_printer_enumforms1(p->mem_ctx,
6988                                                    p->server_info,
6989                                                    p->msg_ctx,
6990                                                    r->out.count,
6991                                                    r->out.info);
6992                 break;
6993         default:
6994                 result = WERR_UNKNOWN_LEVEL;
6995                 break;
6996         }
6997
6998         if (!W_ERROR_IS_OK(result)) {
6999                 return result;
7000         }
7001
7002         if (*r->out.count == 0) {
7003                 return WERR_NO_MORE_ITEMS;
7004         }
7005
7006         *r->out.needed  = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
7007                                                      spoolss_EnumForms, 
7008                                                      *r->out.info, r->in.level,
7009                                                      *r->out.count);
7010         *r->out.info    = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
7011         *r->out.count   = SPOOLSS_BUFFER_OK(*r->out.count, 0);
7012
7013         return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
7014 }
7015
7016 /****************************************************************
7017  _spoolss_GetForm
7018 ****************************************************************/
7019
7020 WERROR _spoolss_GetForm(struct pipes_struct *p,
7021                         struct spoolss_GetForm *r)
7022 {
7023         WERROR result;
7024
7025         /* that's an [in out] buffer */
7026
7027         if (!r->in.buffer && (r->in.offered != 0)) {
7028                 return WERR_INVALID_PARAM;
7029         }
7030
7031         DEBUG(4,("_spoolss_GetForm\n"));
7032         DEBUGADD(5,("Offered buffer size [%d]\n", r->in.offered));
7033         DEBUGADD(5,("Info level [%d]\n",          r->in.level));
7034
7035         switch (r->in.level) {
7036         case 1:
7037                 result = winreg_printer_getform1(p->mem_ctx,
7038                                                  p->server_info,
7039                                                  p->msg_ctx,
7040                                                  r->in.form_name,
7041                                                  &r->out.info->info1);
7042                 break;
7043         default:
7044                 result = WERR_UNKNOWN_LEVEL;
7045                 break;
7046         }
7047
7048         if (!W_ERROR_IS_OK(result)) {
7049                 TALLOC_FREE(r->out.info);
7050                 return result;
7051         }
7052
7053         *r->out.needed  = SPOOLSS_BUFFER_UNION(spoolss_FormInfo, 
7054                                                r->out.info, r->in.level);
7055         r->out.info     = SPOOLSS_BUFFER_OK(r->out.info, NULL);
7056
7057         return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
7058 }
7059
7060 /****************************************************************************
7061 ****************************************************************************/
7062
7063 static WERROR fill_port_1(TALLOC_CTX *mem_ctx,
7064                           struct spoolss_PortInfo1 *r,
7065                           const char *name)
7066 {
7067         r->port_name = talloc_strdup(mem_ctx, name);
7068         W_ERROR_HAVE_NO_MEMORY(r->port_name);
7069
7070         return WERR_OK;
7071 }
7072
7073 /****************************************************************************
7074  TODO: This probably needs distinguish between TCP/IP and Local ports
7075  somehow.
7076 ****************************************************************************/
7077
7078 static WERROR fill_port_2(TALLOC_CTX *mem_ctx,
7079                           struct spoolss_PortInfo2 *r,
7080                           const char *name)
7081 {
7082         r->port_name = talloc_strdup(mem_ctx, name);
7083         W_ERROR_HAVE_NO_MEMORY(r->port_name);
7084
7085         r->monitor_name = talloc_strdup(mem_ctx, "Local Monitor");
7086         W_ERROR_HAVE_NO_MEMORY(r->monitor_name);
7087
7088         r->description = talloc_strdup(mem_ctx, SPL_LOCAL_PORT);
7089         W_ERROR_HAVE_NO_MEMORY(r->description);
7090
7091         r->port_type = SPOOLSS_PORT_TYPE_WRITE;
7092         r->reserved = 0;
7093
7094         return WERR_OK;
7095 }
7096
7097
7098 /****************************************************************************
7099  wrapper around the enumer ports command
7100 ****************************************************************************/
7101
7102 static WERROR enumports_hook(TALLOC_CTX *ctx, int *count, char ***lines)
7103 {
7104         char *cmd = lp_enumports_cmd();
7105         char **qlines = NULL;
7106         char *command = NULL;
7107         int numlines;
7108         int ret;
7109         int fd;
7110
7111         *count = 0;
7112         *lines = NULL;
7113
7114         /* if no hook then just fill in the default port */
7115
7116         if ( !*cmd ) {
7117                 if (!(qlines = TALLOC_ARRAY( NULL, char*, 2 ))) {
7118                         return WERR_NOMEM;
7119                 }
7120                 if (!(qlines[0] = talloc_strdup(qlines, SAMBA_PRINTER_PORT_NAME ))) {
7121                         TALLOC_FREE(qlines);
7122                         return WERR_NOMEM;
7123                 }
7124                 qlines[1] = NULL;
7125                 numlines = 1;
7126         }
7127         else {
7128                 /* we have a valid enumport command */
7129
7130                 command = talloc_asprintf(ctx, "%s \"%d\"", cmd, 1);
7131                 if (!command) {
7132                         return WERR_NOMEM;
7133                 }
7134
7135                 DEBUG(10,("Running [%s]\n", command));
7136                 ret = smbrun(command, &fd);
7137                 DEBUG(10,("Returned [%d]\n", ret));
7138                 TALLOC_FREE(command);
7139                 if (ret != 0) {
7140                         if (fd != -1) {
7141                                 close(fd);
7142                         }
7143                         return WERR_ACCESS_DENIED;
7144                 }
7145
7146                 numlines = 0;
7147                 qlines = fd_lines_load(fd, &numlines, 0, NULL);
7148                 DEBUGADD(10,("Lines returned = [%d]\n", numlines));
7149                 close(fd);
7150         }
7151
7152         *count = numlines;
7153         *lines = qlines;
7154
7155         return WERR_OK;
7156 }
7157
7158 /****************************************************************************
7159  enumports level 1.
7160 ****************************************************************************/
7161
7162 static WERROR enumports_level_1(TALLOC_CTX *mem_ctx,
7163                                 union spoolss_PortInfo **info_p,
7164                                 uint32_t *count)
7165 {
7166         union spoolss_PortInfo *info = NULL;
7167         int i=0;
7168         WERROR result = WERR_OK;
7169         char **qlines = NULL;
7170         int numlines = 0;
7171
7172         result = enumports_hook(talloc_tos(), &numlines, &qlines );
7173         if (!W_ERROR_IS_OK(result)) {
7174                 goto out;
7175         }
7176
7177         if (numlines) {
7178                 info = TALLOC_ARRAY(mem_ctx, union spoolss_PortInfo, numlines);
7179                 if (!info) {
7180                         DEBUG(10,("Returning WERR_NOMEM\n"));
7181                         result = WERR_NOMEM;
7182                         goto out;
7183                 }
7184
7185                 for (i=0; i<numlines; i++) {
7186                         DEBUG(6,("Filling port number [%d] with port [%s]\n", i, qlines[i]));
7187                         result = fill_port_1(info, &info[i].info1, qlines[i]);
7188                         if (!W_ERROR_IS_OK(result)) {
7189                                 goto out;
7190                         }
7191                 }
7192         }
7193         TALLOC_FREE(qlines);
7194
7195 out:
7196         if (!W_ERROR_IS_OK(result)) {
7197                 TALLOC_FREE(info);
7198                 TALLOC_FREE(qlines);
7199                 *count = 0;
7200                 *info_p = NULL;
7201                 return result;
7202         }
7203
7204         *info_p = info;
7205         *count = numlines;
7206
7207         return WERR_OK;
7208 }
7209
7210 /****************************************************************************
7211  enumports level 2.
7212 ****************************************************************************/
7213
7214 static WERROR enumports_level_2(TALLOC_CTX *mem_ctx,
7215                                 union spoolss_PortInfo **info_p,
7216                                 uint32_t *count)
7217 {
7218         union spoolss_PortInfo *info = NULL;
7219         int i=0;
7220         WERROR result = WERR_OK;
7221         char **qlines = NULL;
7222         int numlines = 0;
7223
7224         result = enumports_hook(talloc_tos(), &numlines, &qlines );
7225         if (!W_ERROR_IS_OK(result)) {
7226                 goto out;
7227         }
7228
7229         if (numlines) {
7230                 info = TALLOC_ARRAY(mem_ctx, union spoolss_PortInfo, numlines);
7231                 if (!info) {
7232                         DEBUG(10,("Returning WERR_NOMEM\n"));
7233                         result = WERR_NOMEM;
7234                         goto out;
7235                 }
7236
7237                 for (i=0; i<numlines; i++) {
7238                         DEBUG(6,("Filling port number [%d] with port [%s]\n", i, qlines[i]));
7239                         result = fill_port_2(info, &info[i].info2, qlines[i]);
7240                         if (!W_ERROR_IS_OK(result)) {
7241                                 goto out;
7242                         }
7243                 }
7244         }
7245         TALLOC_FREE(qlines);
7246
7247 out:
7248         if (!W_ERROR_IS_OK(result)) {
7249                 TALLOC_FREE(info);
7250                 TALLOC_FREE(qlines);
7251                 *count = 0;
7252                 *info_p = NULL;
7253                 return result;
7254         }
7255
7256         *info_p = info;
7257         *count = numlines;
7258
7259         return WERR_OK;
7260 }
7261
7262 /****************************************************************
7263  _spoolss_EnumPorts
7264 ****************************************************************/
7265
7266 WERROR _spoolss_EnumPorts(struct pipes_struct *p,
7267                           struct spoolss_EnumPorts *r)
7268 {
7269         WERROR result;
7270
7271         /* that's an [in out] buffer */
7272
7273         if (!r->in.buffer && (r->in.offered != 0)) {
7274                 return WERR_INVALID_PARAM;
7275         }
7276
7277         DEBUG(4,("_spoolss_EnumPorts\n"));
7278
7279         *r->out.count = 0;
7280         *r->out.needed = 0;
7281         *r->out.info = NULL;
7282
7283         switch (r->in.level) {
7284         case 1:
7285                 result = enumports_level_1(p->mem_ctx, r->out.info,
7286                                            r->out.count);
7287                 break;
7288         case 2:
7289                 result = enumports_level_2(p->mem_ctx, r->out.info,
7290                                            r->out.count);
7291                 break;
7292         default:
7293                 return WERR_UNKNOWN_LEVEL;
7294         }
7295
7296         if (!W_ERROR_IS_OK(result)) {
7297                 return result;
7298         }
7299
7300         *r->out.needed  = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
7301                                                      spoolss_EnumPorts, 
7302                                                      *r->out.info, r->in.level,
7303                                                      *r->out.count);
7304         *r->out.info    = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
7305         *r->out.count   = SPOOLSS_BUFFER_OK(*r->out.count, 0);
7306
7307         return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
7308 }
7309
7310 /****************************************************************************
7311 ****************************************************************************/
7312
7313 static WERROR spoolss_addprinterex_level_2(struct pipes_struct *p,
7314                                            const char *server,
7315                                            struct spoolss_SetPrinterInfoCtr *info_ctr,
7316                                            struct spoolss_DeviceMode *devmode,
7317                                            struct security_descriptor *secdesc,
7318                                            struct spoolss_UserLevelCtr *user_ctr,
7319                                            struct policy_handle *handle)
7320 {
7321         struct spoolss_SetPrinterInfo2 *info2 = info_ctr->info.info2;
7322         uint32_t info2_mask = SPOOLSS_PRINTER_INFO_ALL;
7323         int     snum;
7324         WERROR err = WERR_OK;
7325
7326         /* samba does not have a concept of local, non-shared printers yet, so
7327          * make sure we always setup sharename - gd */
7328         if ((info2->sharename == NULL || info2->sharename[0] == '\0') &&
7329             (info2->printername != NULL && info2->printername[0] != '\0')) {
7330                 DEBUG(5, ("spoolss_addprinterex_level_2: "
7331                         "no sharename has been set, setting printername %s as sharename\n",
7332                         info2->printername));
7333                 info2->sharename = info2->printername;
7334         }
7335
7336         /* check to see if the printer already exists */
7337         if ((snum = print_queue_snum(info2->sharename)) != -1) {
7338                 DEBUG(5, ("spoolss_addprinterex_level_2: Attempted to add a printer named [%s] when one already existed!\n",
7339                         info2->sharename));
7340                 return WERR_PRINTER_ALREADY_EXISTS;
7341         }
7342
7343         if (!lp_force_printername(GLOBAL_SECTION_SNUM)) {
7344                 if ((snum = print_queue_snum(info2->printername)) != -1) {
7345                         DEBUG(5, ("spoolss_addprinterex_level_2: Attempted to add a printer named [%s] when one already existed!\n",
7346                                 info2->printername));
7347                         return WERR_PRINTER_ALREADY_EXISTS;
7348                 }
7349         }
7350
7351         /* validate printer info struct */
7352         if (!info2->printername || strlen(info2->printername) == 0) {
7353                 return WERR_INVALID_PRINTER_NAME;
7354         }
7355         if (!info2->portname || strlen(info2->portname) == 0) {
7356                 return WERR_UNKNOWN_PORT;
7357         }
7358         if (!info2->drivername || strlen(info2->drivername) == 0) {
7359                 return WERR_UNKNOWN_PRINTER_DRIVER;
7360         }
7361         if (!info2->printprocessor || strlen(info2->printprocessor) == 0) {
7362                 return WERR_UNKNOWN_PRINTPROCESSOR;
7363         }
7364
7365         /* FIXME!!!  smbd should check to see if the driver is installed before
7366            trying to add a printer like this  --jerry */
7367
7368         if (*lp_addprinter_cmd() ) {
7369                 if ( !add_printer_hook(p->mem_ctx, p->server_info->ptok,
7370                                        info2, p->client_address,
7371                                        p->msg_ctx) ) {
7372                         return WERR_ACCESS_DENIED;
7373                 }
7374         } else {
7375                 DEBUG(0,("spoolss_addprinterex_level_2: add printer for printer %s called and no"
7376                         "smb.conf parameter \"addprinter command\" is defined. This"
7377                         "parameter must exist for this call to succeed\n",
7378                         info2->sharename ));
7379         }
7380
7381         if ((snum = print_queue_snum(info2->sharename)) == -1) {
7382                 return WERR_ACCESS_DENIED;
7383         }
7384
7385         /* you must be a printer admin to add a new printer */
7386         if (!print_access_check(p->server_info, p->msg_ctx, snum,
7387                                 PRINTER_ACCESS_ADMINISTER)) {
7388                 return WERR_ACCESS_DENIED;
7389         }
7390
7391         /*
7392          * Do sanity check on the requested changes for Samba.
7393          */
7394
7395         if (!check_printer_ok(p->mem_ctx, info2, snum)) {
7396                 return WERR_INVALID_PARAM;
7397         }
7398
7399         if (devmode == NULL) {
7400                 info2_mask = ~SPOOLSS_PRINTER_INFO_DEVMODE;
7401         }
7402
7403         update_dsspooler(p->mem_ctx,
7404                          p->server_info,
7405                          p->msg_ctx,
7406                          0,
7407                          info2,
7408                          NULL);
7409
7410         err = winreg_update_printer(p->mem_ctx,
7411                                     p->server_info,
7412                                     p->msg_ctx,
7413                                     info2->sharename,
7414                                     info2_mask,
7415                                     info2,
7416                                     devmode,
7417                                     secdesc);
7418         if (!W_ERROR_IS_OK(err)) {
7419                 return err;
7420         }
7421
7422         if (!open_printer_hnd(p, handle, info2->printername, PRINTER_ACCESS_ADMINISTER)) {
7423                 /* Handle open failed - remove addition. */
7424                 ZERO_STRUCTP(handle);
7425                 return WERR_ACCESS_DENIED;
7426         }
7427
7428         return WERR_OK;
7429 }
7430
7431 /****************************************************************
7432  _spoolss_AddPrinterEx
7433 ****************************************************************/
7434
7435 WERROR _spoolss_AddPrinterEx(struct pipes_struct *p,
7436                              struct spoolss_AddPrinterEx *r)
7437 {
7438         switch (r->in.info_ctr->level) {
7439         case 1:
7440                 /* we don't handle yet */
7441                 /* but I know what to do ... */
7442                 return WERR_UNKNOWN_LEVEL;
7443         case 2:
7444                 return spoolss_addprinterex_level_2(p, r->in.server,
7445                                                     r->in.info_ctr,
7446                                                     r->in.devmode_ctr->devmode,
7447                                                     r->in.secdesc_ctr->sd,
7448                                                     r->in.userlevel_ctr,
7449                                                     r->out.handle);
7450         default:
7451                 return WERR_UNKNOWN_LEVEL;
7452         }
7453 }
7454
7455 /****************************************************************
7456  _spoolss_AddPrinter
7457 ****************************************************************/
7458
7459 WERROR _spoolss_AddPrinter(struct pipes_struct *p,
7460                            struct spoolss_AddPrinter *r)
7461 {
7462         struct spoolss_AddPrinterEx a;
7463         struct spoolss_UserLevelCtr userlevel_ctr;
7464
7465         ZERO_STRUCT(userlevel_ctr);
7466
7467         userlevel_ctr.level = 1;
7468
7469         a.in.server             = r->in.server;
7470         a.in.info_ctr           = r->in.info_ctr;
7471         a.in.devmode_ctr        = r->in.devmode_ctr;
7472         a.in.secdesc_ctr        = r->in.secdesc_ctr;
7473         a.in.userlevel_ctr      = &userlevel_ctr;
7474         a.out.handle            = r->out.handle;
7475
7476         return _spoolss_AddPrinterEx(p, &a);
7477 }
7478
7479 /****************************************************************
7480  _spoolss_AddPrinterDriverEx
7481 ****************************************************************/
7482
7483 WERROR _spoolss_AddPrinterDriverEx(struct pipes_struct *p,
7484                                    struct spoolss_AddPrinterDriverEx *r)
7485 {
7486         WERROR err = WERR_OK;
7487         const char *driver_name = NULL;
7488         uint32_t version;
7489         const char *fn;
7490
7491         switch (p->opnum) {
7492                 case NDR_SPOOLSS_ADDPRINTERDRIVER:
7493                         fn = "_spoolss_AddPrinterDriver";
7494                         break;
7495                 case NDR_SPOOLSS_ADDPRINTERDRIVEREX:
7496                         fn = "_spoolss_AddPrinterDriverEx";
7497                         break;
7498                 default:
7499                         return WERR_INVALID_PARAM;
7500         }
7501
7502         /*
7503          * we only support the semantics of AddPrinterDriver()
7504          * i.e. only copy files that are newer than existing ones
7505          */
7506
7507         if (r->in.flags == 0) {
7508                 return WERR_INVALID_PARAM;
7509         }
7510
7511         if (r->in.flags != APD_COPY_NEW_FILES) {
7512                 return WERR_ACCESS_DENIED;
7513         }
7514
7515         /* FIXME */
7516         if (r->in.info_ctr->level != 3 && r->in.info_ctr->level != 6) {
7517                 /* Clever hack from Martin Zielinski <mz@seh.de>
7518                  * to allow downgrade from level 8 (Vista).
7519                  */
7520                 DEBUG(0,("%s: level %d not yet implemented\n", fn,
7521                         r->in.info_ctr->level));
7522                 return WERR_UNKNOWN_LEVEL;
7523         }
7524
7525         DEBUG(5,("Cleaning driver's information\n"));
7526         err = clean_up_driver_struct(p->mem_ctx, p, r->in.info_ctr);
7527         if (!W_ERROR_IS_OK(err))
7528                 goto done;
7529
7530         DEBUG(5,("Moving driver to final destination\n"));
7531         if( !W_ERROR_IS_OK(err = move_driver_to_download_area(p, r->in.info_ctr,
7532                                                               &err)) ) {
7533                 goto done;
7534         }
7535
7536         err = winreg_add_driver(p->mem_ctx, p->server_info, p->msg_ctx,
7537                                 r->in.info_ctr, &driver_name, &version);
7538         if (!W_ERROR_IS_OK(err)) {
7539                 goto done;
7540         }
7541
7542         /*
7543          * I think this is where he DrvUpgradePrinter() hook would be
7544          * be called in a driver's interface DLL on a Windows NT 4.0/2k
7545          * server.  Right now, we just need to send ourselves a message
7546          * to update each printer bound to this driver.   --jerry
7547          */
7548
7549         if (!srv_spoolss_drv_upgrade_printer(driver_name, p->msg_ctx)) {
7550                 DEBUG(0,("%s: Failed to send message about upgrading driver [%s]!\n",
7551                         fn, driver_name));
7552         }
7553
7554 done:
7555         return err;
7556 }
7557
7558 /****************************************************************
7559  _spoolss_AddPrinterDriver
7560 ****************************************************************/
7561
7562 WERROR _spoolss_AddPrinterDriver(struct pipes_struct *p,
7563                                  struct spoolss_AddPrinterDriver *r)
7564 {
7565         struct spoolss_AddPrinterDriverEx a;
7566
7567         switch (r->in.info_ctr->level) {
7568         case 2:
7569         case 3:
7570         case 4:
7571         case 5:
7572                 break;
7573         default:
7574                 return WERR_UNKNOWN_LEVEL;
7575         }
7576
7577         a.in.servername         = r->in.servername;
7578         a.in.info_ctr           = r->in.info_ctr;
7579         a.in.flags              = APD_COPY_NEW_FILES;
7580
7581         return _spoolss_AddPrinterDriverEx(p, &a);
7582 }
7583
7584 /****************************************************************************
7585 ****************************************************************************/
7586
7587 struct _spoolss_paths {
7588         int type;
7589         const char *share;
7590         const char *dir;
7591 };
7592
7593 enum { SPOOLSS_DRIVER_PATH, SPOOLSS_PRTPROCS_PATH };
7594
7595 static const struct _spoolss_paths spoolss_paths[]= {
7596         { SPOOLSS_DRIVER_PATH,          "print$",       "DRIVERS" },
7597         { SPOOLSS_PRTPROCS_PATH,        "prnproc$",     "PRTPROCS" }
7598 };
7599
7600 static WERROR compose_spoolss_server_path(TALLOC_CTX *mem_ctx,
7601                                           const char *servername,
7602                                           const char *environment,
7603                                           int component,
7604                                           char **path)
7605 {
7606         const char *pservername = NULL;
7607         const char *long_archi = SPOOLSS_ARCHITECTURE_NT_X86;
7608         const char *short_archi;
7609
7610         *path = NULL;
7611
7612         /* environment may be empty */
7613         if (environment && strlen(environment)) {
7614                 long_archi = environment;
7615         }
7616
7617         /* servername may be empty */
7618         if (servername && strlen(servername)) {
7619                 pservername = canon_servername(servername);
7620
7621                 if (!is_myname_or_ipaddr(pservername)) {
7622                         return WERR_INVALID_PARAM;
7623                 }
7624         }
7625
7626         if (!(short_archi = get_short_archi(long_archi))) {
7627                 return WERR_INVALID_ENVIRONMENT;
7628         }
7629
7630         switch (component) {
7631         case SPOOLSS_PRTPROCS_PATH:
7632         case SPOOLSS_DRIVER_PATH:
7633                 if (pservername) {
7634                         *path = talloc_asprintf(mem_ctx,
7635                                         "\\\\%s\\%s\\%s",
7636                                         pservername,
7637                                         spoolss_paths[component].share,
7638                                         short_archi);
7639                 } else {
7640                         *path = talloc_asprintf(mem_ctx, "%s\\%s\\%s",
7641                                         SPOOLSS_DEFAULT_SERVER_PATH,
7642                                         spoolss_paths[component].dir,
7643                                         short_archi);
7644                 }
7645                 break;
7646         default:
7647                 return WERR_INVALID_PARAM;
7648         }
7649
7650         if (!*path) {
7651                 return WERR_NOMEM;
7652         }
7653
7654         return WERR_OK;
7655 }
7656
7657 /****************************************************************************
7658 ****************************************************************************/
7659
7660 static WERROR getprinterdriverdir_level_1(TALLOC_CTX *mem_ctx,
7661                                           const char *servername,
7662                                           const char *environment,
7663                                           struct spoolss_DriverDirectoryInfo1 *r)
7664 {
7665         WERROR werr;
7666         char *path = NULL;
7667
7668         werr = compose_spoolss_server_path(mem_ctx,
7669                                            servername,
7670                                            environment,
7671                                            SPOOLSS_DRIVER_PATH,
7672                                            &path);
7673         if (!W_ERROR_IS_OK(werr)) {
7674                 return werr;
7675         }
7676
7677         DEBUG(4,("printer driver directory: [%s]\n", path));
7678
7679         r->directory_name = path;
7680
7681         return WERR_OK;
7682 }
7683
7684 /****************************************************************
7685  _spoolss_GetPrinterDriverDirectory
7686 ****************************************************************/
7687
7688 WERROR _spoolss_GetPrinterDriverDirectory(struct pipes_struct *p,
7689                                           struct spoolss_GetPrinterDriverDirectory *r)
7690 {
7691         WERROR werror;
7692
7693         /* that's an [in out] buffer */
7694
7695         if (!r->in.buffer && (r->in.offered != 0)) {
7696                 return WERR_INVALID_PARAM;
7697         }
7698
7699         DEBUG(5,("_spoolss_GetPrinterDriverDirectory: level %d\n",
7700                 r->in.level));
7701
7702         *r->out.needed = 0;
7703
7704         /* r->in.level is ignored */
7705
7706         werror = getprinterdriverdir_level_1(p->mem_ctx,
7707                                              r->in.server,
7708                                              r->in.environment,
7709                                              &r->out.info->info1);
7710         if (!W_ERROR_IS_OK(werror)) {
7711                 TALLOC_FREE(r->out.info);
7712                 return werror;
7713         }
7714
7715         *r->out.needed  = SPOOLSS_BUFFER_UNION(spoolss_DriverDirectoryInfo, 
7716                                                r->out.info, r->in.level);
7717         r->out.info     = SPOOLSS_BUFFER_OK(r->out.info, NULL);
7718
7719         return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
7720 }
7721
7722 /****************************************************************
7723  _spoolss_EnumPrinterData
7724 ****************************************************************/
7725
7726 WERROR _spoolss_EnumPrinterData(struct pipes_struct *p,
7727                                 struct spoolss_EnumPrinterData *r)
7728 {
7729         WERROR result;
7730         struct spoolss_EnumPrinterDataEx r2;
7731         uint32_t count;
7732         struct spoolss_PrinterEnumValues *info, *val = NULL;
7733         uint32_t needed;
7734
7735         r2.in.handle    = r->in.handle;
7736         r2.in.key_name  = "PrinterDriverData";
7737         r2.in.offered   = 0;
7738         r2.out.count    = &count;
7739         r2.out.info     = &info;
7740         r2.out.needed   = &needed;
7741
7742         result = _spoolss_EnumPrinterDataEx(p, &r2);
7743         if (W_ERROR_EQUAL(result, WERR_MORE_DATA)) {
7744                 r2.in.offered = needed;
7745                 result = _spoolss_EnumPrinterDataEx(p, &r2);
7746         }
7747         if (!W_ERROR_IS_OK(result)) {
7748                 return result;
7749         }
7750
7751         /*
7752          * The NT machine wants to know the biggest size of value and data
7753          *
7754          * cf: MSDN EnumPrinterData remark section
7755          */
7756
7757         if (!r->in.value_offered && !r->in.data_offered) {
7758                 uint32_t biggest_valuesize = 0;
7759                 uint32_t biggest_datasize = 0;
7760                 int i, name_length;
7761
7762                 DEBUGADD(6,("Activating NT mega-hack to find sizes\n"));
7763
7764                 for (i=0; i<count; i++) {
7765
7766                         name_length = strlen(info[i].value_name);
7767                         if (strlen(info[i].value_name) > biggest_valuesize) {
7768                                 biggest_valuesize = name_length;
7769                         }
7770
7771                         if (info[i].data_length > biggest_datasize) {
7772                                 biggest_datasize = info[i].data_length;
7773                         }
7774
7775                         DEBUG(6,("current values: [%d], [%d]\n", biggest_valuesize,
7776                                 biggest_datasize));
7777                 }
7778
7779                 /* the value is an UNICODE string but real_value_size is the length
7780                    in bytes including the trailing 0 */
7781
7782                 *r->out.value_needed = 2 * (1 + biggest_valuesize);
7783                 *r->out.data_needed  = biggest_datasize;
7784
7785                 DEBUG(6,("final values: [%d], [%d]\n",
7786                         *r->out.value_needed, *r->out.data_needed));
7787
7788                 return WERR_OK;
7789         }
7790
7791         if (r->in.enum_index < count) {
7792                 val = &info[r->in.enum_index];
7793         }
7794
7795         if (val == NULL) {
7796                 /* out_value should default to "" or else NT4 has
7797                    problems unmarshalling the response */
7798
7799                 if (r->in.value_offered) {
7800                         *r->out.value_needed = 1;
7801                         r->out.value_name = talloc_strdup(r, "");
7802                         if (!r->out.value_name) {
7803                                 return WERR_NOMEM;
7804                         }
7805                 } else {
7806                         r->out.value_name = NULL;
7807                         *r->out.value_needed = 0;
7808                 }
7809
7810                 /* the data is counted in bytes */
7811
7812                 *r->out.data_needed = r->in.data_offered;
7813
7814                 result = WERR_NO_MORE_ITEMS;
7815         } else {
7816                 /*
7817                  * the value is:
7818                  * - counted in bytes in the request
7819                  * - counted in UNICODE chars in the max reply
7820                  * - counted in bytes in the real size
7821                  *
7822                  * take a pause *before* coding not *during* coding
7823                  */
7824
7825                 /* name */
7826                 if (r->in.value_offered) {
7827                         r->out.value_name = talloc_strdup(r, val->value_name);
7828                         if (!r->out.value_name) {
7829                                 return WERR_NOMEM;
7830                         }
7831                         *r->out.value_needed = val->value_name_len;
7832                 } else {
7833                         r->out.value_name = NULL;
7834                         *r->out.value_needed = 0;
7835                 }
7836
7837                 /* type */
7838
7839                 *r->out.type = val->type;
7840
7841                 /* data - counted in bytes */
7842
7843                 /*
7844                  * See the section "Dynamically Typed Query Parameters"
7845                  * in MS-RPRN.
7846                  */
7847
7848                 if (r->out.data && val->data && val->data->data &&
7849                                 val->data_length && r->in.data_offered) {
7850                         memcpy(r->out.data, val->data->data,
7851                                 MIN(val->data_length,r->in.data_offered));
7852                 }
7853
7854                 *r->out.data_needed = val->data_length;
7855
7856                 result = WERR_OK;
7857         }
7858
7859         return result;
7860 }
7861
7862 /****************************************************************
7863  _spoolss_SetPrinterData
7864 ****************************************************************/
7865
7866 WERROR _spoolss_SetPrinterData(struct pipes_struct *p,
7867                                struct spoolss_SetPrinterData *r)
7868 {
7869         struct spoolss_SetPrinterDataEx r2;
7870
7871         r2.in.handle            = r->in.handle;
7872         r2.in.key_name          = "PrinterDriverData";
7873         r2.in.value_name        = r->in.value_name;
7874         r2.in.type              = r->in.type;
7875         r2.in.data              = r->in.data;
7876         r2.in.offered           = r->in.offered;
7877
7878         return _spoolss_SetPrinterDataEx(p, &r2);
7879 }
7880
7881 /****************************************************************
7882  _spoolss_ResetPrinter
7883 ****************************************************************/
7884
7885 WERROR _spoolss_ResetPrinter(struct pipes_struct *p,
7886                              struct spoolss_ResetPrinter *r)
7887 {
7888         Printer_entry   *Printer = find_printer_index_by_hnd(p, r->in.handle);
7889         int             snum;
7890
7891         DEBUG(5,("_spoolss_ResetPrinter\n"));
7892
7893         /*
7894          * All we do is to check to see if the handle and queue is valid.
7895          * This call really doesn't mean anything to us because we only
7896          * support RAW printing.   --jerry
7897          */
7898
7899         if (!Printer) {
7900                 DEBUG(2,("_spoolss_ResetPrinter: Invalid handle (%s:%u:%u).\n",
7901                         OUR_HANDLE(r->in.handle)));
7902                 return WERR_BADFID;
7903         }
7904
7905         if (!get_printer_snum(p, r->in.handle, &snum, NULL))
7906                 return WERR_BADFID;
7907
7908
7909         /* blindly return success */
7910         return WERR_OK;
7911 }
7912
7913 /****************************************************************
7914  _spoolss_DeletePrinterData
7915 ****************************************************************/
7916
7917 WERROR _spoolss_DeletePrinterData(struct pipes_struct *p,
7918                                   struct spoolss_DeletePrinterData *r)
7919 {
7920         struct spoolss_DeletePrinterDataEx r2;
7921
7922         r2.in.handle            = r->in.handle;
7923         r2.in.key_name          = "PrinterDriverData";
7924         r2.in.value_name        = r->in.value_name;
7925
7926         return _spoolss_DeletePrinterDataEx(p, &r2);
7927 }
7928
7929 /****************************************************************
7930  _spoolss_AddForm
7931 ****************************************************************/
7932
7933 WERROR _spoolss_AddForm(struct pipes_struct *p,
7934                         struct spoolss_AddForm *r)
7935 {
7936         struct spoolss_AddFormInfo1 *form = r->in.info.info1;
7937         int snum = -1;
7938         WERROR status = WERR_OK;
7939         SE_PRIV se_printop = SE_PRINT_OPERATOR;
7940
7941         Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
7942
7943         DEBUG(5,("_spoolss_AddForm\n"));
7944
7945         if (!Printer) {
7946                 DEBUG(2,("_spoolss_AddForm: Invalid handle (%s:%u:%u).\n",
7947                         OUR_HANDLE(r->in.handle)));
7948                 return WERR_BADFID;
7949         }
7950
7951         /* if the user is not root, doesn't have SE_PRINT_OPERATOR privilege,
7952            and not a printer admin, then fail */
7953
7954         if ((p->server_info->utok.uid != sec_initial_uid()) &&
7955              !user_has_privileges(p->server_info->ptok, &se_printop) &&
7956              !token_contains_name_in_list(uidtoname(p->server_info->utok.uid),
7957                                           p->server_info->info3->base.domain.string,
7958                                           NULL,
7959                                           p->server_info->ptok,
7960                                           lp_printer_admin(snum))) {
7961                 DEBUG(2,("_spoolss_Addform: denied by insufficient permissions.\n"));
7962                 return WERR_ACCESS_DENIED;
7963         }
7964
7965         switch (form->flags) {
7966         case SPOOLSS_FORM_USER:
7967         case SPOOLSS_FORM_BUILTIN:
7968         case SPOOLSS_FORM_PRINTER:
7969                 break;
7970         default:
7971                 return WERR_INVALID_PARAM;
7972         }
7973
7974         status = winreg_printer_addform1(p->mem_ctx, p->server_info,
7975                                          p->msg_ctx, form);
7976         if (!W_ERROR_IS_OK(status)) {
7977                 return status;
7978         }
7979
7980         /*
7981          * ChangeID must always be set if this is a printer
7982          */
7983         if (Printer->printer_type == SPLHND_PRINTER) {
7984                 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
7985                         return WERR_BADFID;
7986                 }
7987
7988                 status = winreg_printer_update_changeid(p->mem_ctx,
7989                                                         p->server_info,
7990                                                         p->msg_ctx,
7991                                                         lp_const_servicename(snum));
7992                 if (!W_ERROR_IS_OK(status)) {
7993                         return status;
7994                 }
7995         }
7996
7997         return status;
7998 }
7999
8000 /****************************************************************
8001  _spoolss_DeleteForm
8002 ****************************************************************/
8003
8004 WERROR _spoolss_DeleteForm(struct pipes_struct *p,
8005                            struct spoolss_DeleteForm *r)
8006 {
8007         const char *form_name = r->in.form_name;
8008         Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
8009         int snum = -1;
8010         WERROR status = WERR_OK;
8011         SE_PRIV se_printop = SE_PRINT_OPERATOR;
8012
8013         DEBUG(5,("_spoolss_DeleteForm\n"));
8014
8015         if (!Printer) {
8016                 DEBUG(2,("_spoolss_DeleteForm: Invalid handle (%s:%u:%u).\n",
8017                         OUR_HANDLE(r->in.handle)));
8018                 return WERR_BADFID;
8019         }
8020
8021         if ((p->server_info->utok.uid != sec_initial_uid()) &&
8022              !user_has_privileges(p->server_info->ptok, &se_printop) &&
8023              !token_contains_name_in_list(uidtoname(p->server_info->utok.uid),
8024                                           p->server_info->info3->base.domain.string,
8025                                           NULL,
8026                                           p->server_info->ptok,
8027                                           lp_printer_admin(snum))) {
8028                 DEBUG(2,("_spoolss_DeleteForm: denied by insufficient permissions.\n"));
8029                 return WERR_ACCESS_DENIED;
8030         }
8031
8032         status = winreg_printer_deleteform1(p->mem_ctx,
8033                                             p->server_info,
8034                                             p->msg_ctx,
8035                                             form_name);
8036         if (!W_ERROR_IS_OK(status)) {
8037                 return status;
8038         }
8039
8040         /*
8041          * ChangeID must always be set if this is a printer
8042          */
8043         if (Printer->printer_type == SPLHND_PRINTER) {
8044                 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
8045                         return WERR_BADFID;
8046                 }
8047
8048                 status = winreg_printer_update_changeid(p->mem_ctx,
8049                                                         p->server_info,
8050                                                         p->msg_ctx,
8051                                                         lp_const_servicename(snum));
8052                 if (!W_ERROR_IS_OK(status)) {
8053                         return status;
8054                 }
8055         }
8056
8057         return status;
8058 }
8059
8060 /****************************************************************
8061  _spoolss_SetForm
8062 ****************************************************************/
8063
8064 WERROR _spoolss_SetForm(struct pipes_struct *p,
8065                         struct spoolss_SetForm *r)
8066 {
8067         struct spoolss_AddFormInfo1 *form = r->in.info.info1;
8068         const char *form_name = r->in.form_name;
8069         int snum = -1;
8070         WERROR status = WERR_OK;
8071         SE_PRIV se_printop = SE_PRINT_OPERATOR;
8072
8073         Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
8074
8075         DEBUG(5,("_spoolss_SetForm\n"));
8076
8077         if (!Printer) {
8078                 DEBUG(2,("_spoolss_SetForm: Invalid handle (%s:%u:%u).\n",
8079                         OUR_HANDLE(r->in.handle)));
8080                 return WERR_BADFID;
8081         }
8082
8083         /* if the user is not root, doesn't have SE_PRINT_OPERATOR privilege,
8084            and not a printer admin, then fail */
8085
8086         if ((p->server_info->utok.uid != sec_initial_uid()) &&
8087              !user_has_privileges(p->server_info->ptok, &se_printop) &&
8088              !token_contains_name_in_list(uidtoname(p->server_info->utok.uid),
8089                                           p->server_info->info3->base.domain.string,
8090                                           NULL,
8091                                           p->server_info->ptok,
8092                                           lp_printer_admin(snum))) {
8093                 DEBUG(2,("_spoolss_Setform: denied by insufficient permissions.\n"));
8094                 return WERR_ACCESS_DENIED;
8095         }
8096
8097         status = winreg_printer_setform1(p->mem_ctx,
8098                                          p->server_info,
8099                                          p->msg_ctx,
8100                                          form_name,
8101                                          form);
8102         if (!W_ERROR_IS_OK(status)) {
8103                 return status;
8104         }
8105
8106         /*
8107          * ChangeID must always be set if this is a printer
8108          */
8109         if (Printer->printer_type == SPLHND_PRINTER) {
8110                 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
8111                         return WERR_BADFID;
8112                 }
8113
8114                 status = winreg_printer_update_changeid(p->mem_ctx,
8115                                                         p->server_info,
8116                                                         p->msg_ctx,
8117                                                         lp_const_servicename(snum));
8118                 if (!W_ERROR_IS_OK(status)) {
8119                         return status;
8120                 }
8121         }
8122
8123         return status;
8124 }
8125
8126 /****************************************************************************
8127  fill_print_processor1
8128 ****************************************************************************/
8129
8130 static WERROR fill_print_processor1(TALLOC_CTX *mem_ctx,
8131                                     struct spoolss_PrintProcessorInfo1 *r,
8132                                     const char *print_processor_name)
8133 {
8134         r->print_processor_name = talloc_strdup(mem_ctx, print_processor_name);
8135         W_ERROR_HAVE_NO_MEMORY(r->print_processor_name);
8136
8137         return WERR_OK;
8138 }
8139
8140 /****************************************************************************
8141  enumprintprocessors level 1.
8142 ****************************************************************************/
8143
8144 static WERROR enumprintprocessors_level_1(TALLOC_CTX *mem_ctx,
8145                                           union spoolss_PrintProcessorInfo **info_p,
8146                                           uint32_t *count)
8147 {
8148         union spoolss_PrintProcessorInfo *info;
8149         WERROR result;
8150
8151         info = TALLOC_ARRAY(mem_ctx, union spoolss_PrintProcessorInfo, 1);
8152         W_ERROR_HAVE_NO_MEMORY(info);
8153
8154         *count = 1;
8155
8156         result = fill_print_processor1(info, &info[0].info1, "winprint");
8157         if (!W_ERROR_IS_OK(result)) {
8158                 goto out;
8159         }
8160
8161  out:
8162         if (!W_ERROR_IS_OK(result)) {
8163                 TALLOC_FREE(info);
8164                 *count = 0;
8165                 return result;
8166         }
8167
8168         *info_p = info;
8169
8170         return WERR_OK;
8171 }
8172
8173 /****************************************************************
8174  _spoolss_EnumPrintProcessors
8175 ****************************************************************/
8176
8177 WERROR _spoolss_EnumPrintProcessors(struct pipes_struct *p,
8178                                     struct spoolss_EnumPrintProcessors *r)
8179 {
8180         WERROR result;
8181
8182         /* that's an [in out] buffer */
8183
8184         if (!r->in.buffer && (r->in.offered != 0)) {
8185                 return WERR_INVALID_PARAM;
8186         }
8187
8188         DEBUG(5,("_spoolss_EnumPrintProcessors\n"));
8189
8190         /*
8191          * Enumerate the print processors ...
8192          *
8193          * Just reply with "winprint", to keep NT happy
8194          * and I can use my nice printer checker.
8195          */
8196
8197         *r->out.count = 0;
8198         *r->out.needed = 0;
8199         *r->out.info = NULL;
8200
8201         switch (r->in.level) {
8202         case 1:
8203                 result = enumprintprocessors_level_1(p->mem_ctx, r->out.info,
8204                                                      r->out.count);
8205                 break;
8206         default:
8207                 return WERR_UNKNOWN_LEVEL;
8208         }
8209
8210         if (!W_ERROR_IS_OK(result)) {
8211                 return result;
8212         }
8213
8214         *r->out.needed  = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
8215                                                      spoolss_EnumPrintProcessors,
8216                                                      *r->out.info, r->in.level,
8217                                                      *r->out.count);
8218         *r->out.info    = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
8219         *r->out.count   = SPOOLSS_BUFFER_OK(*r->out.count, 0);
8220
8221         return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
8222 }
8223
8224 /****************************************************************************
8225  fill_printprocdatatype1
8226 ****************************************************************************/
8227
8228 static WERROR fill_printprocdatatype1(TALLOC_CTX *mem_ctx,
8229                                       struct spoolss_PrintProcDataTypesInfo1 *r,
8230                                       const char *name_array)
8231 {
8232         r->name_array = talloc_strdup(mem_ctx, name_array);
8233         W_ERROR_HAVE_NO_MEMORY(r->name_array);
8234
8235         return WERR_OK;
8236 }
8237
8238 /****************************************************************************
8239  enumprintprocdatatypes level 1.
8240 ****************************************************************************/
8241
8242 static WERROR enumprintprocdatatypes_level_1(TALLOC_CTX *mem_ctx,
8243                                              union spoolss_PrintProcDataTypesInfo **info_p,
8244                                              uint32_t *count)
8245 {
8246         WERROR result;
8247         union spoolss_PrintProcDataTypesInfo *info;
8248
8249         info = TALLOC_ARRAY(mem_ctx, union spoolss_PrintProcDataTypesInfo, 1);
8250         W_ERROR_HAVE_NO_MEMORY(info);
8251
8252         *count = 1;
8253
8254         result = fill_printprocdatatype1(info, &info[0].info1, "RAW");
8255         if (!W_ERROR_IS_OK(result)) {
8256                 goto out;
8257         }
8258
8259  out:
8260         if (!W_ERROR_IS_OK(result)) {
8261                 TALLOC_FREE(info);
8262                 *count = 0;
8263                 return result;
8264         }
8265
8266         *info_p = info;
8267
8268         return WERR_OK;
8269 }
8270
8271 /****************************************************************
8272  _spoolss_EnumPrintProcDataTypes
8273 ****************************************************************/
8274
8275 WERROR _spoolss_EnumPrintProcDataTypes(struct pipes_struct *p,
8276                                        struct spoolss_EnumPrintProcDataTypes *r)
8277 {
8278         WERROR result;
8279
8280         /* that's an [in out] buffer */
8281
8282         if (!r->in.buffer && (r->in.offered != 0)) {
8283                 return WERR_INVALID_PARAM;
8284         }
8285
8286         DEBUG(5,("_spoolss_EnumPrintProcDataTypes\n"));
8287
8288         *r->out.count = 0;
8289         *r->out.needed = 0;
8290         *r->out.info = NULL;
8291
8292         switch (r->in.level) {
8293         case 1:
8294                 result = enumprintprocdatatypes_level_1(p->mem_ctx, r->out.info,
8295                                                         r->out.count);
8296                 break;
8297         default:
8298                 return WERR_UNKNOWN_LEVEL;
8299         }
8300
8301         *r->out.needed  = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
8302                                                      spoolss_EnumPrintProcDataTypes, 
8303                                                      *r->out.info, r->in.level,
8304                                                      *r->out.count);
8305         *r->out.info    = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
8306         *r->out.count   = SPOOLSS_BUFFER_OK(*r->out.count, 0);
8307
8308         return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
8309 }
8310
8311 /****************************************************************************
8312  fill_monitor_1
8313 ****************************************************************************/
8314
8315 static WERROR fill_monitor_1(TALLOC_CTX *mem_ctx,
8316                              struct spoolss_MonitorInfo1 *r,
8317                              const char *monitor_name)
8318 {
8319         r->monitor_name                 = talloc_strdup(mem_ctx, monitor_name);
8320         W_ERROR_HAVE_NO_MEMORY(r->monitor_name);
8321
8322         return WERR_OK;
8323 }
8324
8325 /****************************************************************************
8326  fill_monitor_2
8327 ****************************************************************************/
8328
8329 static WERROR fill_monitor_2(TALLOC_CTX *mem_ctx,
8330                              struct spoolss_MonitorInfo2 *r,
8331                              const char *monitor_name,
8332                              const char *environment,
8333                              const char *dll_name)
8334 {
8335         r->monitor_name                 = talloc_strdup(mem_ctx, monitor_name);
8336         W_ERROR_HAVE_NO_MEMORY(r->monitor_name);
8337         r->environment                  = talloc_strdup(mem_ctx, environment);
8338         W_ERROR_HAVE_NO_MEMORY(r->environment);
8339         r->dll_name                     = talloc_strdup(mem_ctx, dll_name);
8340         W_ERROR_HAVE_NO_MEMORY(r->dll_name);
8341
8342         return WERR_OK;
8343 }
8344
8345 /****************************************************************************
8346  enumprintmonitors level 1.
8347 ****************************************************************************/
8348
8349 static WERROR enumprintmonitors_level_1(TALLOC_CTX *mem_ctx,
8350                                         union spoolss_MonitorInfo **info_p,
8351                                         uint32_t *count)
8352 {
8353         union spoolss_MonitorInfo *info;
8354         WERROR result = WERR_OK;
8355
8356         info = TALLOC_ARRAY(mem_ctx, union spoolss_MonitorInfo, 2);
8357         W_ERROR_HAVE_NO_MEMORY(info);
8358
8359         *count = 2;
8360
8361         result = fill_monitor_1(info, &info[0].info1,
8362                                 SPL_LOCAL_PORT);
8363         if (!W_ERROR_IS_OK(result)) {
8364                 goto out;
8365         }
8366
8367         result = fill_monitor_1(info, &info[1].info1,
8368                                 SPL_TCPIP_PORT);
8369         if (!W_ERROR_IS_OK(result)) {
8370                 goto out;
8371         }
8372
8373 out:
8374         if (!W_ERROR_IS_OK(result)) {
8375                 TALLOC_FREE(info);
8376                 *count = 0;
8377                 return result;
8378         }
8379
8380         *info_p = info;
8381
8382         return WERR_OK;
8383 }
8384
8385 /****************************************************************************
8386  enumprintmonitors level 2.
8387 ****************************************************************************/
8388
8389 static WERROR enumprintmonitors_level_2(TALLOC_CTX *mem_ctx,
8390                                         union spoolss_MonitorInfo **info_p,
8391                                         uint32_t *count)
8392 {
8393         union spoolss_MonitorInfo *info;
8394         WERROR result = WERR_OK;
8395
8396         info = TALLOC_ARRAY(mem_ctx, union spoolss_MonitorInfo, 2);
8397         W_ERROR_HAVE_NO_MEMORY(info);
8398
8399         *count = 2;
8400
8401         result = fill_monitor_2(info, &info[0].info2,
8402                                 SPL_LOCAL_PORT,
8403                                 "Windows NT X86", /* FIXME */
8404                                 "localmon.dll");
8405         if (!W_ERROR_IS_OK(result)) {
8406                 goto out;
8407         }
8408
8409         result = fill_monitor_2(info, &info[1].info2,
8410                                 SPL_TCPIP_PORT,
8411                                 "Windows NT X86", /* FIXME */
8412                                 "tcpmon.dll");
8413         if (!W_ERROR_IS_OK(result)) {
8414                 goto out;
8415         }
8416
8417 out:
8418         if (!W_ERROR_IS_OK(result)) {
8419                 TALLOC_FREE(info);
8420                 *count = 0;
8421                 return result;
8422         }
8423
8424         *info_p = info;
8425
8426         return WERR_OK;
8427 }
8428
8429 /****************************************************************
8430  _spoolss_EnumMonitors
8431 ****************************************************************/
8432
8433 WERROR _spoolss_EnumMonitors(struct pipes_struct *p,
8434                              struct spoolss_EnumMonitors *r)
8435 {
8436         WERROR result;
8437
8438         /* that's an [in out] buffer */
8439
8440         if (!r->in.buffer && (r->in.offered != 0)) {
8441                 return WERR_INVALID_PARAM;
8442         }
8443
8444         DEBUG(5,("_spoolss_EnumMonitors\n"));
8445
8446         /*
8447          * Enumerate the print monitors ...
8448          *
8449          * Just reply with "Local Port", to keep NT happy
8450          * and I can use my nice printer checker.
8451          */
8452
8453         *r->out.count = 0;
8454         *r->out.needed = 0;
8455         *r->out.info = NULL;
8456
8457         switch (r->in.level) {
8458         case 1:
8459                 result = enumprintmonitors_level_1(p->mem_ctx, r->out.info,
8460                                                    r->out.count);
8461                 break;
8462         case 2:
8463                 result = enumprintmonitors_level_2(p->mem_ctx, r->out.info,
8464                                                    r->out.count);
8465                 break;
8466         default:
8467                 return WERR_UNKNOWN_LEVEL;
8468         }
8469
8470         if (!W_ERROR_IS_OK(result)) {
8471                 return result;
8472         }
8473
8474         *r->out.needed  = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
8475                                                      spoolss_EnumMonitors, 
8476                                                      *r->out.info, r->in.level,
8477                                                      *r->out.count);
8478         *r->out.info    = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
8479         *r->out.count   = SPOOLSS_BUFFER_OK(*r->out.count, 0);
8480
8481         return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
8482 }
8483
8484 /****************************************************************************
8485 ****************************************************************************/
8486
8487 static WERROR getjob_level_1(TALLOC_CTX *mem_ctx,
8488                              const print_queue_struct *queue,
8489                              int count, int snum,
8490                              struct spoolss_PrinterInfo2 *pinfo2,
8491                              uint32_t jobid,
8492                              struct spoolss_JobInfo1 *r)
8493 {
8494         int i = 0;
8495         bool found = false;
8496
8497         for (i=0; i<count; i++) {
8498                 if (queue[i].job == (int)jobid) {
8499                         found = true;
8500                         break;
8501                 }
8502         }
8503
8504         if (found == false) {
8505                 /* NT treats not found as bad param... yet another bad choice */
8506                 return WERR_INVALID_PARAM;
8507         }
8508
8509         return fill_job_info1(mem_ctx,
8510                               r,
8511                               &queue[i],
8512                               i,
8513                               snum,
8514                               pinfo2);
8515 }
8516
8517 /****************************************************************************
8518 ****************************************************************************/
8519
8520 static WERROR getjob_level_2(TALLOC_CTX *mem_ctx,
8521                              const print_queue_struct *queue,
8522                              int count, int snum,
8523                              struct spoolss_PrinterInfo2 *pinfo2,
8524                              uint32_t jobid,
8525                              struct spoolss_JobInfo2 *r)
8526 {
8527         int i = 0;
8528         bool found = false;
8529         struct spoolss_DeviceMode *devmode;
8530         WERROR result;
8531
8532         for (i=0; i<count; i++) {
8533                 if (queue[i].job == (int)jobid) {
8534                         found = true;
8535                         break;
8536                 }
8537         }
8538
8539         if (found == false) {
8540                 /* NT treats not found as bad param... yet another bad
8541                    choice */
8542                 return WERR_INVALID_PARAM;
8543         }
8544
8545         /*
8546          * if the print job does not have a DEVMODE associated with it,
8547          * just use the one for the printer. A NULL devicemode is not
8548          *  a failure condition
8549          */
8550
8551         devmode = print_job_devmode(lp_const_servicename(snum), jobid);
8552         if (!devmode) {
8553                 result = spoolss_create_default_devmode(mem_ctx,
8554                                                 pinfo2->printername,
8555                                                 &devmode);
8556                 if (!W_ERROR_IS_OK(result)) {
8557                         DEBUG(3, ("Can't proceed w/o a devmode!"));
8558                         return result;
8559                 }
8560         }
8561
8562         return fill_job_info2(mem_ctx,
8563                               r,
8564                               &queue[i],
8565                               i,
8566                               snum,
8567                               pinfo2,
8568                               devmode);
8569 }
8570
8571 /****************************************************************
8572  _spoolss_GetJob
8573 ****************************************************************/
8574
8575 WERROR _spoolss_GetJob(struct pipes_struct *p,
8576                        struct spoolss_GetJob *r)
8577 {
8578         WERROR result = WERR_OK;
8579         struct spoolss_PrinterInfo2 *pinfo2 = NULL;
8580         int snum;
8581         int count;
8582         print_queue_struct      *queue = NULL;
8583         print_status_struct prt_status;
8584
8585         /* that's an [in out] buffer */
8586
8587         if (!r->in.buffer && (r->in.offered != 0)) {
8588                 return WERR_INVALID_PARAM;
8589         }
8590
8591         DEBUG(5,("_spoolss_GetJob\n"));
8592
8593         *r->out.needed = 0;
8594
8595         if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
8596                 return WERR_BADFID;
8597         }
8598
8599         result = winreg_get_printer(p->mem_ctx, p->server_info, p->msg_ctx,
8600                                     NULL, lp_servicename(snum), &pinfo2);
8601         if (!W_ERROR_IS_OK(result)) {
8602                 return result;
8603         }
8604
8605         count = print_queue_status(snum, &queue, &prt_status);
8606
8607         DEBUGADD(4,("count:[%d], prt_status:[%d], [%s]\n",
8608                      count, prt_status.status, prt_status.message));
8609
8610         switch (r->in.level) {
8611         case 1:
8612                 result = getjob_level_1(p->mem_ctx,
8613                                         queue, count, snum, pinfo2,
8614                                         r->in.job_id, &r->out.info->info1);
8615                 break;
8616         case 2:
8617                 result = getjob_level_2(p->mem_ctx,
8618                                         queue, count, snum, pinfo2,
8619                                         r->in.job_id, &r->out.info->info2);
8620                 break;
8621         default:
8622                 result = WERR_UNKNOWN_LEVEL;
8623                 break;
8624         }
8625
8626         SAFE_FREE(queue);
8627         TALLOC_FREE(pinfo2);
8628
8629         if (!W_ERROR_IS_OK(result)) {
8630                 TALLOC_FREE(r->out.info);
8631                 return result;
8632         }
8633
8634         *r->out.needed  = SPOOLSS_BUFFER_UNION(spoolss_JobInfo, r->out.info,
8635                                                                                    r->in.level);
8636         r->out.info     = SPOOLSS_BUFFER_OK(r->out.info, NULL);
8637
8638         return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
8639 }
8640
8641 /****************************************************************
8642  _spoolss_GetPrinterDataEx
8643 ****************************************************************/
8644
8645 WERROR _spoolss_GetPrinterDataEx(struct pipes_struct *p,
8646                                  struct spoolss_GetPrinterDataEx *r)
8647 {
8648
8649         Printer_entry   *Printer = find_printer_index_by_hnd(p, r->in.handle);
8650         const char *printer;
8651         int                     snum = 0;
8652         WERROR result = WERR_OK;
8653         DATA_BLOB blob;
8654         enum winreg_Type val_type;
8655         uint8_t *val_data;
8656         uint32_t val_size;
8657
8658
8659         DEBUG(4,("_spoolss_GetPrinterDataEx\n"));
8660
8661         DEBUG(10, ("_spoolss_GetPrinterDataEx: key => [%s], value => [%s]\n",
8662                 r->in.key_name, r->in.value_name));
8663
8664         /* in case of problem, return some default values */
8665
8666         *r->out.needed  = 0;
8667         *r->out.type    = REG_NONE;
8668
8669         if (!Printer) {
8670                 DEBUG(2,("_spoolss_GetPrinterDataEx: Invalid handle (%s:%u:%u).\n",
8671                         OUR_HANDLE(r->in.handle)));
8672                 result = WERR_BADFID;
8673                 goto done;
8674         }
8675
8676         /* Is the handle to a printer or to the server? */
8677
8678         if (Printer->printer_type == SPLHND_SERVER) {
8679
8680                 union spoolss_PrinterData data;
8681
8682                 result = getprinterdata_printer_server(p->mem_ctx,
8683                                                        r->in.value_name,
8684                                                        r->out.type,
8685                                                        &data);
8686                 if (!W_ERROR_IS_OK(result)) {
8687                         return result;
8688                 }
8689
8690                 result = push_spoolss_PrinterData(p->mem_ctx, &blob,
8691                                                   *r->out.type, &data);
8692                 if (!W_ERROR_IS_OK(result)) {
8693                         return result;
8694                 }
8695
8696                 *r->out.needed = blob.length;
8697
8698                 if (r->in.offered >= *r->out.needed) {
8699                         memcpy(r->out.data, blob.data, blob.length);
8700                 }
8701
8702                 return SPOOLSS_BUFFER_OK(WERR_OK, WERR_MORE_DATA);
8703         }
8704
8705         if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
8706                 return WERR_BADFID;
8707         }
8708         printer = lp_const_servicename(snum);
8709
8710         /* check to see if the keyname is valid */
8711         if (!strlen(r->in.key_name)) {
8712                 return WERR_INVALID_PARAM;
8713         }
8714
8715         /* XP sends this and wants the ChangeID value from PRINTER_INFO_0 */
8716         if (strequal(r->in.key_name, SPOOL_PRINTERDATA_KEY) &&
8717             strequal(r->in.value_name, "ChangeId")) {
8718                 *r->out.type = REG_DWORD;
8719                 *r->out.needed = 4;
8720                 if (r->in.offered >= *r->out.needed) {
8721                         uint32_t changeid = 0;
8722
8723                         result = winreg_printer_get_changeid(p->mem_ctx,
8724                                                              p->server_info,
8725                                                              p->msg_ctx,
8726                                                              printer,
8727                                                              &changeid);
8728                         if (!W_ERROR_IS_OK(result)) {
8729                                 return result;
8730                         }
8731
8732                         SIVAL(r->out.data, 0, changeid);
8733                         result = WERR_OK;
8734                 }
8735                 goto done;
8736         }
8737
8738         result = winreg_get_printer_dataex(p->mem_ctx,
8739                                            p->server_info,
8740                                            p->msg_ctx,
8741                                            printer,
8742                                            r->in.key_name,
8743                                            r->in.value_name,
8744                                            &val_type,
8745                                            &val_data,
8746                                            &val_size);
8747         if (!W_ERROR_IS_OK(result)) {
8748                 return result;
8749         }
8750
8751         *r->out.needed = val_size;
8752         *r->out.type = val_type;
8753
8754         if (r->in.offered >= *r->out.needed) {
8755                 memcpy(r->out.data, val_data, val_size);
8756         }
8757
8758  done:
8759         *r->out.type    = SPOOLSS_BUFFER_OK(*r->out.type, REG_NONE);
8760         r->out.data     = SPOOLSS_BUFFER_OK(r->out.data, r->out.data);
8761
8762         return SPOOLSS_BUFFER_OK(WERR_OK, WERR_MORE_DATA);
8763 }
8764
8765 /****************************************************************
8766  _spoolss_SetPrinterDataEx
8767 ****************************************************************/
8768
8769 WERROR _spoolss_SetPrinterDataEx(struct pipes_struct *p,
8770                                  struct spoolss_SetPrinterDataEx *r)
8771 {
8772         struct spoolss_PrinterInfo2 *pinfo2 = NULL;
8773         int                     snum = 0;
8774         WERROR                  result = WERR_OK;
8775         Printer_entry           *Printer = find_printer_index_by_hnd(p, r->in.handle);
8776         char                    *oid_string;
8777
8778         DEBUG(4,("_spoolss_SetPrinterDataEx\n"));
8779
8780         /* From MSDN documentation of SetPrinterDataEx: pass request to
8781            SetPrinterData if key is "PrinterDriverData" */
8782
8783         if (!Printer) {
8784                 DEBUG(2,("_spoolss_SetPrinterDataEx: Invalid handle (%s:%u:%u).\n",
8785                         OUR_HANDLE(r->in.handle)));
8786                 return WERR_BADFID;
8787         }
8788
8789         if (Printer->printer_type == SPLHND_SERVER) {
8790                 DEBUG(10,("_spoolss_SetPrinterDataEx: "
8791                         "Not implemented for server handles yet\n"));
8792                 return WERR_INVALID_PARAM;
8793         }
8794
8795         if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
8796                 return WERR_BADFID;
8797         }
8798
8799         /*
8800          * Access check : NT returns "access denied" if you make a
8801          * SetPrinterData call without the necessary privildge.
8802          * we were originally returning OK if nothing changed
8803          * which made Win2k issue **a lot** of SetPrinterData
8804          * when connecting to a printer  --jerry
8805          */
8806
8807         if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
8808                 DEBUG(3, ("_spoolss_SetPrinterDataEx: "
8809                         "change denied by handle access permissions\n"));
8810                 return WERR_ACCESS_DENIED;
8811         }
8812
8813         result = winreg_get_printer(Printer, p->server_info, p->msg_ctx,
8814                                     Printer->servername,
8815                                     lp_servicename(snum),
8816                                     &pinfo2);
8817         if (!W_ERROR_IS_OK(result)) {
8818                 return result;
8819         }
8820
8821         /* check for OID in valuename */
8822
8823         oid_string = strchr(r->in.value_name, ',');
8824         if (oid_string) {
8825                 *oid_string = '\0';
8826                 oid_string++;
8827         }
8828
8829         /* save the registry data */
8830
8831         result = winreg_set_printer_dataex(p->mem_ctx,
8832                                            p->server_info,
8833                                            p->msg_ctx,
8834                                            pinfo2->sharename,
8835                                            r->in.key_name,
8836                                            r->in.value_name,
8837                                            r->in.type,
8838                                            r->in.data,
8839                                            r->in.offered);
8840
8841         if (W_ERROR_IS_OK(result)) {
8842                 /* save the OID if one was specified */
8843                 if (oid_string) {
8844                         char *str = talloc_asprintf(p->mem_ctx, "%s\\%s",
8845                                 r->in.key_name, SPOOL_OID_KEY);
8846                         if (!str) {
8847                                 result = WERR_NOMEM;
8848                                 goto done;
8849                         }
8850
8851                         /*
8852                          * I'm not checking the status here on purpose.  Don't know
8853                          * if this is right, but I'm returning the status from the
8854                          * previous set_printer_dataex() call.  I have no idea if
8855                          * this is right.    --jerry
8856                          */
8857                         winreg_set_printer_dataex(p->mem_ctx,
8858                                                   p->server_info,
8859                                                   p->msg_ctx,
8860                                                   pinfo2->sharename,
8861                                                   str,
8862                                                   r->in.value_name,
8863                                                   REG_SZ,
8864                                                   (uint8_t *) oid_string,
8865                                                   strlen(oid_string) + 1);
8866                 }
8867
8868                 result = winreg_printer_update_changeid(p->mem_ctx,
8869                                                         p->server_info,
8870                                                         p->msg_ctx,
8871                                                         lp_const_servicename(snum));
8872
8873         }
8874
8875 done:
8876         talloc_free(pinfo2);
8877         return result;
8878 }
8879
8880 /****************************************************************
8881  _spoolss_DeletePrinterDataEx
8882 ****************************************************************/
8883
8884 WERROR _spoolss_DeletePrinterDataEx(struct pipes_struct *p,
8885                                     struct spoolss_DeletePrinterDataEx *r)
8886 {
8887         const char *printer;
8888         int             snum=0;
8889         WERROR          status = WERR_OK;
8890         Printer_entry   *Printer = find_printer_index_by_hnd(p, r->in.handle);
8891
8892         DEBUG(5,("_spoolss_DeletePrinterDataEx\n"));
8893
8894         if (!Printer) {
8895                 DEBUG(2,("_spoolss_DeletePrinterDataEx: "
8896                         "Invalid handle (%s:%u:%u).\n",
8897                         OUR_HANDLE(r->in.handle)));
8898                 return WERR_BADFID;
8899         }
8900
8901         if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
8902                 DEBUG(3, ("_spoolss_DeletePrinterDataEx: "
8903                         "printer properties change denied by handle\n"));
8904                 return WERR_ACCESS_DENIED;
8905         }
8906
8907         if (!r->in.value_name || !r->in.key_name) {
8908                 return WERR_NOMEM;
8909         }
8910
8911         if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
8912                 return WERR_BADFID;
8913         }
8914         printer = lp_const_servicename(snum);
8915
8916         status = winreg_delete_printer_dataex(p->mem_ctx,
8917                                               p->server_info,
8918                                               p->msg_ctx,
8919                                               printer,
8920                                               r->in.key_name,
8921                                               r->in.value_name);
8922         if (W_ERROR_IS_OK(status)) {
8923                 status = winreg_printer_update_changeid(p->mem_ctx,
8924                                                         p->server_info,
8925                                                         p->msg_ctx,
8926                                                         printer);
8927         }
8928
8929         return status;
8930 }
8931
8932 /****************************************************************
8933  _spoolss_EnumPrinterKey
8934 ****************************************************************/
8935
8936 WERROR _spoolss_EnumPrinterKey(struct pipes_struct *p,
8937                                struct spoolss_EnumPrinterKey *r)
8938 {
8939         uint32_t        num_keys;
8940         Printer_entry   *Printer = find_printer_index_by_hnd(p, r->in.handle);
8941         int             snum = 0;
8942         WERROR          result = WERR_BADFILE;
8943         const char **array = NULL;
8944         DATA_BLOB blob;
8945
8946         DEBUG(4,("_spoolss_EnumPrinterKey\n"));
8947
8948         if (!Printer) {
8949                 DEBUG(2,("_spoolss_EnumPrinterKey: Invalid handle (%s:%u:%u).\n",
8950                         OUR_HANDLE(r->in.handle)));
8951                 return WERR_BADFID;
8952         }
8953
8954         if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
8955                 return WERR_BADFID;
8956         }
8957
8958         result = winreg_enum_printer_key(p->mem_ctx,
8959                                          p->server_info,
8960                                          p->msg_ctx,
8961                                          lp_const_servicename(snum),
8962                                          r->in.key_name,
8963                                          &num_keys,
8964                                          &array);
8965         if (!W_ERROR_IS_OK(result)) {
8966                 goto done;
8967         }
8968
8969         if (!push_reg_multi_sz(p->mem_ctx, &blob, array)) {
8970                 result = WERR_NOMEM;
8971                 goto done;
8972         }
8973
8974         *r->out._ndr_size = r->in.offered / 2;
8975         *r->out.needed = blob.length;
8976
8977         if (r->in.offered < *r->out.needed) {
8978                 result = WERR_MORE_DATA;
8979         } else {
8980                 result = WERR_OK;
8981                 r->out.key_buffer->string_array = array;
8982         }
8983
8984  done:
8985         if (!W_ERROR_IS_OK(result)) {
8986                 TALLOC_FREE(array);
8987                 if (!W_ERROR_EQUAL(result, WERR_MORE_DATA)) {
8988                         *r->out.needed = 0;
8989                 }
8990         }
8991
8992         return result;
8993 }
8994
8995 /****************************************************************
8996  _spoolss_DeletePrinterKey
8997 ****************************************************************/
8998
8999 WERROR _spoolss_DeletePrinterKey(struct pipes_struct *p,
9000                                  struct spoolss_DeletePrinterKey *r)
9001 {
9002         Printer_entry           *Printer = find_printer_index_by_hnd(p, r->in.handle);
9003         int                     snum=0;
9004         WERROR                  status;
9005         const char *printer;
9006
9007         DEBUG(5,("_spoolss_DeletePrinterKey\n"));
9008
9009         if (!Printer) {
9010                 DEBUG(2,("_spoolss_DeletePrinterKey: Invalid handle (%s:%u:%u).\n",
9011                         OUR_HANDLE(r->in.handle)));
9012                 return WERR_BADFID;
9013         }
9014
9015         /* if keyname == NULL, return error */
9016         if ( !r->in.key_name )
9017                 return WERR_INVALID_PARAM;
9018
9019         if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
9020                 return WERR_BADFID;
9021         }
9022
9023         if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
9024                 DEBUG(3, ("_spoolss_DeletePrinterKey: "
9025                         "printer properties change denied by handle\n"));
9026                 return WERR_ACCESS_DENIED;
9027         }
9028
9029         printer = lp_const_servicename(snum);
9030
9031         /* delete the key and all subkeys */
9032         status = winreg_delete_printer_key(p->mem_ctx,
9033                                            p->server_info,
9034                                            p->msg_ctx,
9035                                            printer,
9036                                            r->in.key_name);
9037         if (W_ERROR_IS_OK(status)) {
9038                 status = winreg_printer_update_changeid(p->mem_ctx,
9039                                                         p->server_info,
9040                                                         p->msg_ctx,
9041                                                         printer);
9042         }
9043
9044         return status;
9045 }
9046
9047 /****************************************************************
9048  _spoolss_EnumPrinterDataEx
9049 ****************************************************************/
9050
9051 WERROR _spoolss_EnumPrinterDataEx(struct pipes_struct *p,
9052                                   struct spoolss_EnumPrinterDataEx *r)
9053 {
9054         uint32_t        count = 0;
9055         struct spoolss_PrinterEnumValues *info = NULL;
9056         Printer_entry   *Printer = find_printer_index_by_hnd(p, r->in.handle);
9057         int             snum;
9058         WERROR          result;
9059
9060         DEBUG(4,("_spoolss_EnumPrinterDataEx\n"));
9061
9062         *r->out.count = 0;
9063         *r->out.needed = 0;
9064         *r->out.info = NULL;
9065
9066         if (!Printer) {
9067                 DEBUG(2,("_spoolss_EnumPrinterDataEx: Invalid handle (%s:%u:%u1<).\n",
9068                         OUR_HANDLE(r->in.handle)));
9069                 return WERR_BADFID;
9070         }
9071
9072         /*
9073          * first check for a keyname of NULL or "".  Win2k seems to send
9074          * this a lot and we should send back WERR_INVALID_PARAM
9075          * no need to spend time looking up the printer in this case.
9076          * --jerry
9077          */
9078
9079         if (!strlen(r->in.key_name)) {
9080                 result = WERR_INVALID_PARAM;
9081                 goto done;
9082         }
9083
9084         if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
9085                 return WERR_BADFID;
9086         }
9087
9088         /* now look for a match on the key name */
9089         result = winreg_enum_printer_dataex(p->mem_ctx,
9090                                             p->server_info,
9091                                             p->msg_ctx,
9092                                             lp_const_servicename(snum),
9093                                             r->in.key_name,
9094                                             &count,
9095                                             &info);
9096         if (!W_ERROR_IS_OK(result)) {
9097                 goto done;
9098         }
9099
9100 #if 0 /* FIXME - gd */
9101         /* housekeeping information in the reply */
9102
9103         /* Fix from Martin Zielinski <mz@seh.de> - ensure
9104          * the hand marshalled container size is a multiple
9105          * of 4 bytes for RPC alignment.
9106          */
9107
9108         if (needed % 4) {
9109                 needed += 4-(needed % 4);
9110         }
9111 #endif
9112         *r->out.count   = count;
9113         *r->out.info    = info;
9114
9115  done:
9116         if (!W_ERROR_IS_OK(result)) {
9117                 return result;
9118         }
9119
9120         *r->out.needed  = SPOOLSS_BUFFER_ARRAY(p->mem_ctx,
9121                                                spoolss_EnumPrinterDataEx, 
9122                                                *r->out.info,
9123                                                *r->out.count);
9124         *r->out.info    = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
9125         *r->out.count   = SPOOLSS_BUFFER_OK(*r->out.count, *r->out.count);
9126
9127         return SPOOLSS_BUFFER_OK(WERR_OK, WERR_MORE_DATA);
9128 }
9129
9130 /****************************************************************************
9131 ****************************************************************************/
9132
9133 static WERROR getprintprocessordirectory_level_1(TALLOC_CTX *mem_ctx,
9134                                                  const char *servername,
9135                                                  const char *environment,
9136                                                  struct spoolss_PrintProcessorDirectoryInfo1 *r)
9137 {
9138         WERROR werr;
9139         char *path = NULL;
9140
9141         werr = compose_spoolss_server_path(mem_ctx,
9142                                            servername,
9143                                            environment,
9144                                            SPOOLSS_PRTPROCS_PATH,
9145                                            &path);
9146         if (!W_ERROR_IS_OK(werr)) {
9147                 return werr;
9148         }
9149
9150         DEBUG(4,("print processor directory: [%s]\n", path));
9151
9152         r->directory_name = path;
9153
9154         return WERR_OK;
9155 }
9156
9157 /****************************************************************
9158  _spoolss_GetPrintProcessorDirectory
9159 ****************************************************************/
9160
9161 WERROR _spoolss_GetPrintProcessorDirectory(struct pipes_struct *p,
9162                                            struct spoolss_GetPrintProcessorDirectory *r)
9163 {
9164         WERROR result;
9165
9166         /* that's an [in out] buffer */
9167
9168         if (!r->in.buffer && (r->in.offered != 0)) {
9169                 return WERR_INVALID_PARAM;
9170         }
9171
9172         DEBUG(5,("_spoolss_GetPrintProcessorDirectory: level %d\n",
9173                 r->in.level));
9174
9175         *r->out.needed = 0;
9176
9177         /* r->in.level is ignored */
9178
9179         /* We always should reply with a local print processor directory so that
9180          * users are not forced to have a [prnproc$] share on the Samba spoolss
9181          * server - Guenther */
9182
9183         result = getprintprocessordirectory_level_1(p->mem_ctx,
9184                                                     NULL, /* r->in.server */
9185                                                     r->in.environment,
9186                                                     &r->out.info->info1);
9187         if (!W_ERROR_IS_OK(result)) {
9188                 TALLOC_FREE(r->out.info);
9189                 return result;
9190         }
9191
9192         *r->out.needed  = SPOOLSS_BUFFER_UNION(spoolss_PrintProcessorDirectoryInfo,
9193                                                                                    r->out.info, r->in.level);
9194         r->out.info     = SPOOLSS_BUFFER_OK(r->out.info, NULL);
9195
9196         return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
9197 }
9198
9199 /*******************************************************************
9200  ********************************************************************/
9201
9202 static bool push_monitorui_buf(TALLOC_CTX *mem_ctx, DATA_BLOB *buf,
9203                                const char *dllname)
9204 {
9205         enum ndr_err_code ndr_err;
9206         struct spoolss_MonitorUi ui;
9207
9208         ui.dll_name = dllname;
9209
9210         ndr_err = ndr_push_struct_blob(buf, mem_ctx, &ui,
9211                        (ndr_push_flags_fn_t)ndr_push_spoolss_MonitorUi);
9212         if (NDR_ERR_CODE_IS_SUCCESS(ndr_err) && (DEBUGLEVEL >= 10)) {
9213                 NDR_PRINT_DEBUG(spoolss_MonitorUi, &ui);
9214         }
9215         return NDR_ERR_CODE_IS_SUCCESS(ndr_err);
9216 }
9217
9218 /*******************************************************************
9219  Streams the monitor UI DLL name in UNICODE
9220 *******************************************************************/
9221
9222 static WERROR xcvtcp_monitorui(TALLOC_CTX *mem_ctx,
9223                                NT_USER_TOKEN *token, DATA_BLOB *in,
9224                                DATA_BLOB *out, uint32_t *needed)
9225 {
9226         const char *dllname = "tcpmonui.dll";
9227
9228         *needed = (strlen(dllname)+1) * 2;
9229
9230         if (out->length < *needed) {
9231                 return WERR_INSUFFICIENT_BUFFER;
9232         }
9233
9234         if (!push_monitorui_buf(mem_ctx, out, dllname)) {
9235                 return WERR_NOMEM;
9236         }
9237
9238         return WERR_OK;
9239 }
9240
9241 /*******************************************************************
9242  ********************************************************************/
9243
9244 static bool pull_port_data_1(TALLOC_CTX *mem_ctx,
9245                              struct spoolss_PortData1 *port1,
9246                              const DATA_BLOB *buf)
9247 {
9248         enum ndr_err_code ndr_err;
9249         ndr_err = ndr_pull_struct_blob(buf, mem_ctx, port1,
9250                        (ndr_pull_flags_fn_t)ndr_pull_spoolss_PortData1);
9251         if (NDR_ERR_CODE_IS_SUCCESS(ndr_err) && (DEBUGLEVEL >= 10)) {
9252                 NDR_PRINT_DEBUG(spoolss_PortData1, port1);
9253         }
9254         return NDR_ERR_CODE_IS_SUCCESS(ndr_err);
9255 }
9256
9257 /*******************************************************************
9258  ********************************************************************/
9259
9260 static bool pull_port_data_2(TALLOC_CTX *mem_ctx,
9261                              struct spoolss_PortData2 *port2,
9262                              const DATA_BLOB *buf)
9263 {
9264         enum ndr_err_code ndr_err;
9265         ndr_err = ndr_pull_struct_blob(buf, mem_ctx, port2,
9266                        (ndr_pull_flags_fn_t)ndr_pull_spoolss_PortData2);
9267         if (NDR_ERR_CODE_IS_SUCCESS(ndr_err) && (DEBUGLEVEL >= 10)) {
9268                 NDR_PRINT_DEBUG(spoolss_PortData2, port2);
9269         }
9270         return NDR_ERR_CODE_IS_SUCCESS(ndr_err);
9271 }
9272
9273 /*******************************************************************
9274  Create a new TCP/IP port
9275 *******************************************************************/
9276
9277 static WERROR xcvtcp_addport(TALLOC_CTX *mem_ctx,
9278                              NT_USER_TOKEN *token, DATA_BLOB *in,
9279                              DATA_BLOB *out, uint32_t *needed)
9280 {
9281         struct spoolss_PortData1 port1;
9282         struct spoolss_PortData2 port2;
9283         char *device_uri = NULL;
9284         uint32_t version;
9285
9286         const char *portname;
9287         const char *hostaddress;
9288         const char *queue;
9289         uint32_t port_number;
9290         uint32_t protocol;
9291
9292         /* peek for spoolss_PortData version */
9293
9294         if (!in || (in->length < (128 + 4))) {
9295                 return WERR_GENERAL_FAILURE;
9296         }
9297
9298         version = IVAL(in->data, 128);
9299
9300         switch (version) {
9301                 case 1:
9302                         ZERO_STRUCT(port1);
9303
9304                         if (!pull_port_data_1(mem_ctx, &port1, in)) {
9305                                 return WERR_NOMEM;
9306                         }
9307
9308                         portname        = port1.portname;
9309                         hostaddress     = port1.hostaddress;
9310                         queue           = port1.queue;
9311                         protocol        = port1.protocol;
9312                         port_number     = port1.port_number;
9313
9314                         break;
9315                 case 2:
9316                         ZERO_STRUCT(port2);
9317
9318                         if (!pull_port_data_2(mem_ctx, &port2, in)) {
9319                                 return WERR_NOMEM;
9320                         }
9321
9322                         portname        = port2.portname;
9323                         hostaddress     = port2.hostaddress;
9324                         queue           = port2.queue;
9325                         protocol        = port2.protocol;
9326                         port_number     = port2.port_number;
9327
9328                         break;
9329                 default:
9330                         DEBUG(1,("xcvtcp_addport: "
9331                                 "unknown version of port_data: %d\n", version));
9332                         return WERR_UNKNOWN_PORT;
9333         }
9334
9335         /* create the device URI and call the add_port_hook() */
9336
9337         switch (protocol) {
9338         case PROTOCOL_RAWTCP_TYPE:
9339                 device_uri = talloc_asprintf(mem_ctx,
9340                                 "socket://%s:%d/", hostaddress,
9341                                 port_number);
9342                 break;
9343
9344         case PROTOCOL_LPR_TYPE:
9345                 device_uri = talloc_asprintf(mem_ctx,
9346                         "lpr://%s/%s", hostaddress, queue );
9347                 break;
9348
9349         default:
9350                 return WERR_UNKNOWN_PORT;
9351         }
9352
9353         if (!device_uri) {
9354                 return WERR_NOMEM;
9355         }
9356
9357         return add_port_hook(mem_ctx, token, portname, device_uri);
9358 }
9359
9360 /*******************************************************************
9361 *******************************************************************/
9362
9363 struct xcv_api_table xcvtcp_cmds[] = {
9364         { "MonitorUI",  xcvtcp_monitorui },
9365         { "AddPort",    xcvtcp_addport},
9366         { NULL,         NULL }
9367 };
9368
9369 static WERROR process_xcvtcp_command(TALLOC_CTX *mem_ctx,
9370                                      NT_USER_TOKEN *token, const char *command,
9371                                      DATA_BLOB *inbuf,
9372                                      DATA_BLOB *outbuf,
9373                                      uint32_t *needed )
9374 {
9375         int i;
9376
9377         DEBUG(10,("process_xcvtcp_command: Received command \"%s\"\n", command));
9378
9379         for ( i=0; xcvtcp_cmds[i].name; i++ ) {
9380                 if ( strcmp( command, xcvtcp_cmds[i].name ) == 0 )
9381                         return xcvtcp_cmds[i].fn(mem_ctx, token, inbuf, outbuf, needed);
9382         }
9383
9384         return WERR_BADFUNC;
9385 }
9386
9387 /*******************************************************************
9388 *******************************************************************/
9389 #if 0   /* don't support management using the "Local Port" monitor */
9390
9391 static WERROR xcvlocal_monitorui(TALLOC_CTX *mem_ctx,
9392                                  NT_USER_TOKEN *token, DATA_BLOB *in,
9393                                  DATA_BLOB *out, uint32_t *needed)
9394 {
9395         const char *dllname = "localui.dll";
9396
9397         *needed = (strlen(dllname)+1) * 2;
9398
9399         if (out->length < *needed) {
9400                 return WERR_INSUFFICIENT_BUFFER;
9401         }
9402
9403         if (!push_monitorui_buf(mem_ctx, out, dllname)) {
9404                 return WERR_NOMEM;
9405         }
9406
9407         return WERR_OK;
9408 }
9409
9410 /*******************************************************************
9411 *******************************************************************/
9412
9413 struct xcv_api_table xcvlocal_cmds[] = {
9414         { "MonitorUI",  xcvlocal_monitorui },
9415         { NULL,         NULL }
9416 };
9417 #else
9418 struct xcv_api_table xcvlocal_cmds[] = {
9419         { NULL,         NULL }
9420 };
9421 #endif
9422
9423
9424
9425 /*******************************************************************
9426 *******************************************************************/
9427
9428 static WERROR process_xcvlocal_command(TALLOC_CTX *mem_ctx,
9429                                        NT_USER_TOKEN *token, const char *command,
9430                                        DATA_BLOB *inbuf, DATA_BLOB *outbuf,
9431                                        uint32_t *needed)
9432 {
9433         int i;
9434
9435         DEBUG(10,("process_xcvlocal_command: Received command \"%s\"\n", command));
9436
9437         for ( i=0; xcvlocal_cmds[i].name; i++ ) {
9438                 if ( strcmp( command, xcvlocal_cmds[i].name ) == 0 )
9439                         return xcvlocal_cmds[i].fn(mem_ctx, token, inbuf, outbuf, needed);
9440         }
9441         return WERR_BADFUNC;
9442 }
9443
9444 /****************************************************************
9445  _spoolss_XcvData
9446 ****************************************************************/
9447
9448 WERROR _spoolss_XcvData(struct pipes_struct *p,
9449                         struct spoolss_XcvData *r)
9450 {
9451         Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
9452         DATA_BLOB out_data = data_blob_null;
9453         WERROR werror;
9454
9455         if (!Printer) {
9456                 DEBUG(2,("_spoolss_XcvData: Invalid handle (%s:%u:%u).\n",
9457                         OUR_HANDLE(r->in.handle)));
9458                 return WERR_BADFID;
9459         }
9460
9461         /* Has to be a handle to the TCP/IP port monitor */
9462
9463         if ( !(Printer->printer_type & (SPLHND_PORTMON_LOCAL|SPLHND_PORTMON_TCP)) ) {
9464                 DEBUG(2,("_spoolss_XcvData: Call only valid for Port Monitors\n"));
9465                 return WERR_BADFID;
9466         }
9467
9468         /* requires administrative access to the server */
9469
9470         if ( !(Printer->access_granted & SERVER_ACCESS_ADMINISTER) ) {
9471                 DEBUG(2,("_spoolss_XcvData: denied by handle permissions.\n"));
9472                 return WERR_ACCESS_DENIED;
9473         }
9474
9475         /* Allocate the outgoing buffer */
9476
9477         if (r->in.out_data_size) {
9478                 out_data = data_blob_talloc_zero(p->mem_ctx, r->in.out_data_size);
9479                 if (out_data.data == NULL) {
9480                         return WERR_NOMEM;
9481                 }
9482         }
9483
9484         switch ( Printer->printer_type ) {
9485         case SPLHND_PORTMON_TCP:
9486                 werror = process_xcvtcp_command(p->mem_ctx,
9487                                                 p->server_info->ptok,
9488                                                 r->in.function_name,
9489                                                 &r->in.in_data, &out_data,
9490                                                 r->out.needed);
9491                 break;
9492         case SPLHND_PORTMON_LOCAL:
9493                 werror = process_xcvlocal_command(p->mem_ctx,
9494                                                   p->server_info->ptok,
9495                                                   r->in.function_name,
9496                                                   &r->in.in_data, &out_data,
9497                                                   r->out.needed);
9498                 break;
9499         default:
9500                 werror = WERR_INVALID_PRINT_MONITOR;
9501         }
9502
9503         if (!W_ERROR_IS_OK(werror)) {
9504                 return werror;
9505         }
9506
9507         *r->out.status_code = 0;
9508
9509         if (r->out.out_data && out_data.data && r->in.out_data_size && out_data.length) {
9510                 memcpy(r->out.out_data, out_data.data,
9511                         MIN(r->in.out_data_size, out_data.length));
9512         }
9513
9514         return WERR_OK;
9515 }
9516
9517 /****************************************************************
9518  _spoolss_AddPrintProcessor
9519 ****************************************************************/
9520
9521 WERROR _spoolss_AddPrintProcessor(struct pipes_struct *p,
9522                                   struct spoolss_AddPrintProcessor *r)
9523 {
9524         /* for now, just indicate success and ignore the add.  We'll
9525            automatically set the winprint processor for printer
9526            entries later.  Used to debug the LexMark Optra S 1855 PCL
9527            driver --jerry */
9528
9529         return WERR_OK;
9530 }
9531
9532 /****************************************************************
9533  _spoolss_AddPort
9534 ****************************************************************/
9535
9536 WERROR _spoolss_AddPort(struct pipes_struct *p,
9537                         struct spoolss_AddPort *r)
9538 {
9539         /* do what w2k3 does */
9540
9541         return WERR_NOT_SUPPORTED;
9542 }
9543
9544 /****************************************************************
9545  _spoolss_GetPrinterDriver
9546 ****************************************************************/
9547
9548 WERROR _spoolss_GetPrinterDriver(struct pipes_struct *p,
9549                                  struct spoolss_GetPrinterDriver *r)
9550 {
9551         p->rng_fault_state = true;
9552         return WERR_NOT_SUPPORTED;
9553 }
9554
9555 /****************************************************************
9556  _spoolss_ReadPrinter
9557 ****************************************************************/
9558
9559 WERROR _spoolss_ReadPrinter(struct pipes_struct *p,
9560                             struct spoolss_ReadPrinter *r)
9561 {
9562         p->rng_fault_state = true;
9563         return WERR_NOT_SUPPORTED;
9564 }
9565
9566 /****************************************************************
9567  _spoolss_WaitForPrinterChange
9568 ****************************************************************/
9569
9570 WERROR _spoolss_WaitForPrinterChange(struct pipes_struct *p,
9571                                      struct spoolss_WaitForPrinterChange *r)
9572 {
9573         p->rng_fault_state = true;
9574         return WERR_NOT_SUPPORTED;
9575 }
9576
9577 /****************************************************************
9578  _spoolss_ConfigurePort
9579 ****************************************************************/
9580
9581 WERROR _spoolss_ConfigurePort(struct pipes_struct *p,
9582                               struct spoolss_ConfigurePort *r)
9583 {
9584         p->rng_fault_state = true;
9585         return WERR_NOT_SUPPORTED;
9586 }
9587
9588 /****************************************************************
9589  _spoolss_DeletePort
9590 ****************************************************************/
9591
9592 WERROR _spoolss_DeletePort(struct pipes_struct *p,
9593                            struct spoolss_DeletePort *r)
9594 {
9595         p->rng_fault_state = true;
9596         return WERR_NOT_SUPPORTED;
9597 }
9598
9599 /****************************************************************
9600  _spoolss_CreatePrinterIC
9601 ****************************************************************/
9602
9603 WERROR _spoolss_CreatePrinterIC(struct pipes_struct *p,
9604                                 struct spoolss_CreatePrinterIC *r)
9605 {
9606         p->rng_fault_state = true;
9607         return WERR_NOT_SUPPORTED;
9608 }
9609
9610 /****************************************************************
9611  _spoolss_PlayGDIScriptOnPrinterIC
9612 ****************************************************************/
9613
9614 WERROR _spoolss_PlayGDIScriptOnPrinterIC(struct pipes_struct *p,
9615                                          struct spoolss_PlayGDIScriptOnPrinterIC *r)
9616 {
9617         p->rng_fault_state = true;
9618         return WERR_NOT_SUPPORTED;
9619 }
9620
9621 /****************************************************************
9622  _spoolss_DeletePrinterIC
9623 ****************************************************************/
9624
9625 WERROR _spoolss_DeletePrinterIC(struct pipes_struct *p,
9626                                 struct spoolss_DeletePrinterIC *r)
9627 {
9628         p->rng_fault_state = true;
9629         return WERR_NOT_SUPPORTED;
9630 }
9631
9632 /****************************************************************
9633  _spoolss_AddPrinterConnection
9634 ****************************************************************/
9635
9636 WERROR _spoolss_AddPrinterConnection(struct pipes_struct *p,
9637                                      struct spoolss_AddPrinterConnection *r)
9638 {
9639         p->rng_fault_state = true;
9640         return WERR_NOT_SUPPORTED;
9641 }
9642
9643 /****************************************************************
9644  _spoolss_DeletePrinterConnection
9645 ****************************************************************/
9646
9647 WERROR _spoolss_DeletePrinterConnection(struct pipes_struct *p,
9648                                         struct spoolss_DeletePrinterConnection *r)
9649 {
9650         p->rng_fault_state = true;
9651         return WERR_NOT_SUPPORTED;
9652 }
9653
9654 /****************************************************************
9655  _spoolss_PrinterMessageBox
9656 ****************************************************************/
9657
9658 WERROR _spoolss_PrinterMessageBox(struct pipes_struct *p,
9659                                   struct spoolss_PrinterMessageBox *r)
9660 {
9661         p->rng_fault_state = true;
9662         return WERR_NOT_SUPPORTED;
9663 }
9664
9665 /****************************************************************
9666  _spoolss_AddMonitor
9667 ****************************************************************/
9668
9669 WERROR _spoolss_AddMonitor(struct pipes_struct *p,
9670                            struct spoolss_AddMonitor *r)
9671 {
9672         p->rng_fault_state = true;
9673         return WERR_NOT_SUPPORTED;
9674 }
9675
9676 /****************************************************************
9677  _spoolss_DeleteMonitor
9678 ****************************************************************/
9679
9680 WERROR _spoolss_DeleteMonitor(struct pipes_struct *p,
9681                               struct spoolss_DeleteMonitor *r)
9682 {
9683         p->rng_fault_state = true;
9684         return WERR_NOT_SUPPORTED;
9685 }
9686
9687 /****************************************************************
9688  _spoolss_DeletePrintProcessor
9689 ****************************************************************/
9690
9691 WERROR _spoolss_DeletePrintProcessor(struct pipes_struct *p,
9692                                      struct spoolss_DeletePrintProcessor *r)
9693 {
9694         p->rng_fault_state = true;
9695         return WERR_NOT_SUPPORTED;
9696 }
9697
9698 /****************************************************************
9699  _spoolss_AddPrintProvidor
9700 ****************************************************************/
9701
9702 WERROR _spoolss_AddPrintProvidor(struct pipes_struct *p,
9703                                  struct spoolss_AddPrintProvidor *r)
9704 {
9705         p->rng_fault_state = true;
9706         return WERR_NOT_SUPPORTED;
9707 }
9708
9709 /****************************************************************
9710  _spoolss_DeletePrintProvidor
9711 ****************************************************************/
9712
9713 WERROR _spoolss_DeletePrintProvidor(struct pipes_struct *p,
9714                                     struct spoolss_DeletePrintProvidor *r)
9715 {
9716         p->rng_fault_state = true;
9717         return WERR_NOT_SUPPORTED;
9718 }
9719
9720 /****************************************************************
9721  _spoolss_FindFirstPrinterChangeNotification
9722 ****************************************************************/
9723
9724 WERROR _spoolss_FindFirstPrinterChangeNotification(struct pipes_struct *p,
9725                                                    struct spoolss_FindFirstPrinterChangeNotification *r)
9726 {
9727         p->rng_fault_state = true;
9728         return WERR_NOT_SUPPORTED;
9729 }
9730
9731 /****************************************************************
9732  _spoolss_FindNextPrinterChangeNotification
9733 ****************************************************************/
9734
9735 WERROR _spoolss_FindNextPrinterChangeNotification(struct pipes_struct *p,
9736                                                   struct spoolss_FindNextPrinterChangeNotification *r)
9737 {
9738         p->rng_fault_state = true;
9739         return WERR_NOT_SUPPORTED;
9740 }
9741
9742 /****************************************************************
9743  _spoolss_RouterFindFirstPrinterChangeNotificationOld
9744 ****************************************************************/
9745
9746 WERROR _spoolss_RouterFindFirstPrinterChangeNotificationOld(struct pipes_struct *p,
9747                                                             struct spoolss_RouterFindFirstPrinterChangeNotificationOld *r)
9748 {
9749         p->rng_fault_state = true;
9750         return WERR_NOT_SUPPORTED;
9751 }
9752
9753 /****************************************************************
9754  _spoolss_ReplyOpenPrinter
9755 ****************************************************************/
9756
9757 WERROR _spoolss_ReplyOpenPrinter(struct pipes_struct *p,
9758                                  struct spoolss_ReplyOpenPrinter *r)
9759 {
9760         p->rng_fault_state = true;
9761         return WERR_NOT_SUPPORTED;
9762 }
9763
9764 /****************************************************************
9765  _spoolss_RouterReplyPrinter
9766 ****************************************************************/
9767
9768 WERROR _spoolss_RouterReplyPrinter(struct pipes_struct *p,
9769                                    struct spoolss_RouterReplyPrinter *r)
9770 {
9771         p->rng_fault_state = true;
9772         return WERR_NOT_SUPPORTED;
9773 }
9774
9775 /****************************************************************
9776  _spoolss_ReplyClosePrinter
9777 ****************************************************************/
9778
9779 WERROR _spoolss_ReplyClosePrinter(struct pipes_struct *p,
9780                                   struct spoolss_ReplyClosePrinter *r)
9781 {
9782         p->rng_fault_state = true;
9783         return WERR_NOT_SUPPORTED;
9784 }
9785
9786 /****************************************************************
9787  _spoolss_AddPortEx
9788 ****************************************************************/
9789
9790 WERROR _spoolss_AddPortEx(struct pipes_struct *p,
9791                           struct spoolss_AddPortEx *r)
9792 {
9793         p->rng_fault_state = true;
9794         return WERR_NOT_SUPPORTED;
9795 }
9796
9797 /****************************************************************
9798  _spoolss_RouterFindFirstPrinterChangeNotification
9799 ****************************************************************/
9800
9801 WERROR _spoolss_RouterFindFirstPrinterChangeNotification(struct pipes_struct *p,
9802                                                          struct spoolss_RouterFindFirstPrinterChangeNotification *r)
9803 {
9804         p->rng_fault_state = true;
9805         return WERR_NOT_SUPPORTED;
9806 }
9807
9808 /****************************************************************
9809  _spoolss_SpoolerInit
9810 ****************************************************************/
9811
9812 WERROR _spoolss_SpoolerInit(struct pipes_struct *p,
9813                             struct spoolss_SpoolerInit *r)
9814 {
9815         p->rng_fault_state = true;
9816         return WERR_NOT_SUPPORTED;
9817 }
9818
9819 /****************************************************************
9820  _spoolss_ResetPrinterEx
9821 ****************************************************************/
9822
9823 WERROR _spoolss_ResetPrinterEx(struct pipes_struct *p,
9824                                struct spoolss_ResetPrinterEx *r)
9825 {
9826         p->rng_fault_state = true;
9827         return WERR_NOT_SUPPORTED;
9828 }
9829
9830 /****************************************************************
9831  _spoolss_RouterReplyPrinterEx
9832 ****************************************************************/
9833
9834 WERROR _spoolss_RouterReplyPrinterEx(struct pipes_struct *p,
9835                                      struct spoolss_RouterReplyPrinterEx *r)
9836 {
9837         p->rng_fault_state = true;
9838         return WERR_NOT_SUPPORTED;
9839 }
9840
9841 /****************************************************************
9842  _spoolss_44
9843 ****************************************************************/
9844
9845 WERROR _spoolss_44(struct pipes_struct *p,
9846                    struct spoolss_44 *r)
9847 {
9848         p->rng_fault_state = true;
9849         return WERR_NOT_SUPPORTED;
9850 }
9851
9852 /****************************************************************
9853  _spoolss_47
9854 ****************************************************************/
9855
9856 WERROR _spoolss_47(struct pipes_struct *p,
9857                    struct spoolss_47 *r)
9858 {
9859         p->rng_fault_state = true;
9860         return WERR_NOT_SUPPORTED;
9861 }
9862
9863 /****************************************************************
9864  _spoolss_4a
9865 ****************************************************************/
9866
9867 WERROR _spoolss_4a(struct pipes_struct *p,
9868                    struct spoolss_4a *r)
9869 {
9870         p->rng_fault_state = true;
9871         return WERR_NOT_SUPPORTED;
9872 }
9873
9874 /****************************************************************
9875  _spoolss_4b
9876 ****************************************************************/
9877
9878 WERROR _spoolss_4b(struct pipes_struct *p,
9879                    struct spoolss_4b *r)
9880 {
9881         p->rng_fault_state = true;
9882         return WERR_NOT_SUPPORTED;
9883 }
9884
9885 /****************************************************************
9886  _spoolss_4c
9887 ****************************************************************/
9888
9889 WERROR _spoolss_4c(struct pipes_struct *p,
9890                    struct spoolss_4c *r)
9891 {
9892         p->rng_fault_state = true;
9893         return WERR_NOT_SUPPORTED;
9894 }
9895
9896 /****************************************************************
9897  _spoolss_53
9898 ****************************************************************/
9899
9900 WERROR _spoolss_53(struct pipes_struct *p,
9901                    struct spoolss_53 *r)
9902 {
9903         p->rng_fault_state = true;
9904         return WERR_NOT_SUPPORTED;
9905 }
9906
9907 /****************************************************************
9908  _spoolss_55
9909 ****************************************************************/
9910
9911 WERROR _spoolss_55(struct pipes_struct *p,
9912                    struct spoolss_55 *r)
9913 {
9914         p->rng_fault_state = true;
9915         return WERR_NOT_SUPPORTED;
9916 }
9917
9918 /****************************************************************
9919  _spoolss_56
9920 ****************************************************************/
9921
9922 WERROR _spoolss_56(struct pipes_struct *p,
9923                    struct spoolss_56 *r)
9924 {
9925         p->rng_fault_state = true;
9926         return WERR_NOT_SUPPORTED;
9927 }
9928
9929 /****************************************************************
9930  _spoolss_57
9931 ****************************************************************/
9932
9933 WERROR _spoolss_57(struct pipes_struct *p,
9934                    struct spoolss_57 *r)
9935 {
9936         p->rng_fault_state = true;
9937         return WERR_NOT_SUPPORTED;
9938 }
9939
9940 /****************************************************************
9941  _spoolss_5a
9942 ****************************************************************/
9943
9944 WERROR _spoolss_5a(struct pipes_struct *p,
9945                    struct spoolss_5a *r)
9946 {
9947         p->rng_fault_state = true;
9948         return WERR_NOT_SUPPORTED;
9949 }
9950
9951 /****************************************************************
9952  _spoolss_5b
9953 ****************************************************************/
9954
9955 WERROR _spoolss_5b(struct pipes_struct *p,
9956                    struct spoolss_5b *r)
9957 {
9958         p->rng_fault_state = true;
9959         return WERR_NOT_SUPPORTED;
9960 }
9961
9962 /****************************************************************
9963  _spoolss_5c
9964 ****************************************************************/
9965
9966 WERROR _spoolss_5c(struct pipes_struct *p,
9967                    struct spoolss_5c *r)
9968 {
9969         p->rng_fault_state = true;
9970         return WERR_NOT_SUPPORTED;
9971 }
9972
9973 /****************************************************************
9974  _spoolss_5d
9975 ****************************************************************/
9976
9977 WERROR _spoolss_5d(struct pipes_struct *p,
9978                    struct spoolss_5d *r)
9979 {
9980         p->rng_fault_state = true;
9981         return WERR_NOT_SUPPORTED;
9982 }
9983
9984 /****************************************************************
9985  _spoolss_5e
9986 ****************************************************************/
9987
9988 WERROR _spoolss_5e(struct pipes_struct *p,
9989                    struct spoolss_5e *r)
9990 {
9991         p->rng_fault_state = true;
9992         return WERR_NOT_SUPPORTED;
9993 }
9994
9995 /****************************************************************
9996  _spoolss_5f
9997 ****************************************************************/
9998
9999 WERROR _spoolss_5f(struct pipes_struct *p,
10000                    struct spoolss_5f *r)
10001 {
10002         p->rng_fault_state = true;
10003         return WERR_NOT_SUPPORTED;
10004 }
10005
10006 /****************************************************************
10007  _spoolss_60
10008 ****************************************************************/
10009
10010 WERROR _spoolss_60(struct pipes_struct *p,
10011                    struct spoolss_60 *r)
10012 {
10013         p->rng_fault_state = true;
10014         return WERR_NOT_SUPPORTED;
10015 }
10016
10017 /****************************************************************
10018  _spoolss_61
10019 ****************************************************************/
10020
10021 WERROR _spoolss_61(struct pipes_struct *p,
10022                    struct spoolss_61 *r)
10023 {
10024         p->rng_fault_state = true;
10025         return WERR_NOT_SUPPORTED;
10026 }
10027
10028 /****************************************************************
10029  _spoolss_62
10030 ****************************************************************/
10031
10032 WERROR _spoolss_62(struct pipes_struct *p,
10033                    struct spoolss_62 *r)
10034 {
10035         p->rng_fault_state = true;
10036         return WERR_NOT_SUPPORTED;
10037 }
10038
10039 /****************************************************************
10040  _spoolss_63
10041 ****************************************************************/
10042
10043 WERROR _spoolss_63(struct pipes_struct *p,
10044                    struct spoolss_63 *r)
10045 {
10046         p->rng_fault_state = true;
10047         return WERR_NOT_SUPPORTED;
10048 }
10049
10050 /****************************************************************
10051  _spoolss_64
10052 ****************************************************************/
10053
10054 WERROR _spoolss_64(struct pipes_struct *p,
10055                    struct spoolss_64 *r)
10056 {
10057         p->rng_fault_state = true;
10058         return WERR_NOT_SUPPORTED;
10059 }
10060
10061 /****************************************************************
10062  _spoolss_65
10063 ****************************************************************/
10064
10065 WERROR _spoolss_65(struct pipes_struct *p,
10066                    struct spoolss_65 *r)
10067 {
10068         p->rng_fault_state = true;
10069         return WERR_NOT_SUPPORTED;
10070 }
10071
10072 /****************************************************************
10073  _spoolss_GetCorePrinterDrivers
10074 ****************************************************************/
10075
10076 WERROR _spoolss_GetCorePrinterDrivers(struct pipes_struct *p,
10077                                       struct spoolss_GetCorePrinterDrivers *r)
10078 {
10079         p->rng_fault_state = true;
10080         return WERR_NOT_SUPPORTED;
10081 }
10082
10083 /****************************************************************
10084  _spoolss_67
10085 ****************************************************************/
10086
10087 WERROR _spoolss_67(struct pipes_struct *p,
10088                    struct spoolss_67 *r)
10089 {
10090         p->rng_fault_state = true;
10091         return WERR_NOT_SUPPORTED;
10092 }
10093
10094 /****************************************************************
10095  _spoolss_GetPrinterDriverPackagePath
10096 ****************************************************************/
10097
10098 WERROR _spoolss_GetPrinterDriverPackagePath(struct pipes_struct *p,
10099                                             struct spoolss_GetPrinterDriverPackagePath *r)
10100 {
10101         p->rng_fault_state = true;
10102         return WERR_NOT_SUPPORTED;
10103 }
10104
10105 /****************************************************************
10106  _spoolss_69
10107 ****************************************************************/
10108
10109 WERROR _spoolss_69(struct pipes_struct *p,
10110                    struct spoolss_69 *r)
10111 {
10112         p->rng_fault_state = true;
10113         return WERR_NOT_SUPPORTED;
10114 }
10115
10116 /****************************************************************
10117  _spoolss_6a
10118 ****************************************************************/
10119
10120 WERROR _spoolss_6a(struct pipes_struct *p,
10121                    struct spoolss_6a *r)
10122 {
10123         p->rng_fault_state = true;
10124         return WERR_NOT_SUPPORTED;
10125 }
10126
10127 /****************************************************************
10128  _spoolss_6b
10129 ****************************************************************/
10130
10131 WERROR _spoolss_6b(struct pipes_struct *p,
10132                    struct spoolss_6b *r)
10133 {
10134         p->rng_fault_state = true;
10135         return WERR_NOT_SUPPORTED;
10136 }
10137
10138 /****************************************************************
10139  _spoolss_6c
10140 ****************************************************************/
10141
10142 WERROR _spoolss_6c(struct pipes_struct *p,
10143                    struct spoolss_6c *r)
10144 {
10145         p->rng_fault_state = true;
10146         return WERR_NOT_SUPPORTED;
10147 }
10148
10149 /****************************************************************
10150  _spoolss_6d
10151 ****************************************************************/
10152
10153 WERROR _spoolss_6d(struct pipes_struct *p,
10154                    struct spoolss_6d *r)
10155 {
10156         p->rng_fault_state = true;
10157         return WERR_NOT_SUPPORTED;
10158 }