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