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