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