Merge branch 'master' of ssh://git.samba.org/data/git/samba into wspp-schema
[ira/wip.git] / source3 / rpc_server / srv_spoolss_nt.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  RPC Pipe client / server routines
4  *  Copyright (C) Andrew Tridgell              1992-2000,
5  *  Copyright (C) Luke Kenneth Casson Leighton 1996-2000,
6  *  Copyright (C) Jean François Micouleau      1998-2000,
7  *  Copyright (C) Jeremy Allison               2001-2002,
8  *  Copyright (C) Gerald Carter                2000-2004,
9  *  Copyright (C) Tim Potter                   2001-2002.
10  *  Copyright (C) Guenther Deschner                 2009.
11  *
12  *  This program is free software; you can redistribute it and/or modify
13  *  it under the terms of the GNU General Public License as published by
14  *  the Free Software Foundation; either version 3 of the License, or
15  *  (at your option) any later version.
16  *
17  *  This program is distributed in the hope that it will be useful,
18  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *  GNU General Public License for more details.
21  *
22  *  You should have received a copy of the GNU General Public License
23  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
24  */
25
26 /* Since the SPOOLSS rpc routines are basically DOS 16-bit calls wrapped
27    up, all the errors returned are DOS errors, not NT status codes. */
28
29 #include "includes.h"
30
31 /* macros stolen from s4 spoolss server */
32 #define SPOOLSS_BUFFER_UNION(fn,ic,info,level) \
33         ((info)?ndr_size_##fn(info, level, ic, 0):0)
34
35 #define SPOOLSS_BUFFER_UNION_ARRAY(mem_ctx,fn,ic,info,level,count) \
36         ((info)?ndr_size_##fn##_info(mem_ctx, ic, level, count, info):0)
37
38 #define SPOOLSS_BUFFER_ARRAY(mem_ctx,fn,ic,info,count) \
39         ((info)?ndr_size_##fn##_info(mem_ctx, ic, count, info):0)
40
41 #define SPOOLSS_BUFFER_OK(val_true,val_false) ((r->in.offered >= *r->out.needed)?val_true:val_false)
42
43
44 extern userdom_struct current_user_info;
45
46 #undef DBGC_CLASS
47 #define DBGC_CLASS DBGC_RPC_SRV
48
49 #ifndef MAX_OPEN_PRINTER_EXS
50 #define MAX_OPEN_PRINTER_EXS 50
51 #endif
52
53 #define MAGIC_DISPLAY_FREQUENCY 0xfade2bad
54 #define PHANTOM_DEVMODE_KEY "_p_f_a_n_t_0_m_"
55
56 struct table_node {
57         const char    *long_archi;
58         const char    *short_archi;
59         int     version;
60 };
61
62 static Printer_entry *printers_list;
63
64 typedef struct _counter_printer_0 {
65         struct _counter_printer_0 *next;
66         struct _counter_printer_0 *prev;
67
68         int snum;
69         uint32_t counter;
70 } counter_printer_0;
71
72 static counter_printer_0 *counter_list;
73
74 static struct rpc_pipe_client *notify_cli_pipe; /* print notify back-channel pipe handle*/
75 static uint32_t smb_connections = 0;
76
77
78 /* in printing/nt_printing.c */
79
80 extern struct standard_mapping printer_std_mapping, printserver_std_mapping;
81
82 /* API table for Xcv Monitor functions */
83
84 struct xcv_api_table {
85         const char *name;
86         WERROR(*fn) (TALLOC_CTX *mem_ctx, NT_USER_TOKEN *token, DATA_BLOB *in, DATA_BLOB *out, uint32_t *needed);
87 };
88
89 /********************************************************************
90  * Canonicalize servername.
91  ********************************************************************/
92
93 static const char *canon_servername(const char *servername)
94 {
95         const char *pservername = servername;
96         while (*pservername == '\\') {
97                 pservername++;
98         }
99         return pservername;
100 }
101
102 /* translate between internal status numbers and NT status numbers */
103 static int nt_printj_status(int v)
104 {
105         switch (v) {
106         case LPQ_QUEUED:
107                 return 0;
108         case LPQ_PAUSED:
109                 return JOB_STATUS_PAUSED;
110         case LPQ_SPOOLING:
111                 return JOB_STATUS_SPOOLING;
112         case LPQ_PRINTING:
113                 return JOB_STATUS_PRINTING;
114         case LPQ_ERROR:
115                 return JOB_STATUS_ERROR;
116         case LPQ_DELETING:
117                 return JOB_STATUS_DELETING;
118         case LPQ_OFFLINE:
119                 return JOB_STATUS_OFFLINE;
120         case LPQ_PAPEROUT:
121                 return JOB_STATUS_PAPEROUT;
122         case LPQ_PRINTED:
123                 return JOB_STATUS_PRINTED;
124         case LPQ_DELETED:
125                 return JOB_STATUS_DELETED;
126         case LPQ_BLOCKED:
127                 return JOB_STATUS_BLOCKED_DEVQ;
128         case LPQ_USER_INTERVENTION:
129                 return JOB_STATUS_USER_INTERVENTION;
130         }
131         return 0;
132 }
133
134 static int nt_printq_status(int v)
135 {
136         switch (v) {
137         case LPQ_PAUSED:
138                 return PRINTER_STATUS_PAUSED;
139         case LPQ_QUEUED:
140         case LPQ_SPOOLING:
141         case LPQ_PRINTING:
142                 return 0;
143         }
144         return 0;
145 }
146
147 /***************************************************************************
148  Disconnect from the client
149 ****************************************************************************/
150
151 static void srv_spoolss_replycloseprinter(int snum, struct policy_handle *handle)
152 {
153         WERROR result;
154         NTSTATUS status;
155
156         /*
157          * Tell the specific printing tdb we no longer want messages for this printer
158          * by deregistering our PID.
159          */
160
161         if (!print_notify_deregister_pid(snum))
162                 DEBUG(0,("print_notify_register_pid: Failed to register our pid for printer %s\n", lp_const_servicename(snum) ));
163
164         /* weird if the test succeds !!! */
165         if (smb_connections==0) {
166                 DEBUG(0,("srv_spoolss_replycloseprinter:Trying to close non-existant notify backchannel !\n"));
167                 return;
168         }
169
170         status = rpccli_spoolss_ReplyClosePrinter(notify_cli_pipe, talloc_tos(),
171                                                   handle,
172                                                   &result);
173         if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result))
174                 DEBUG(0,("srv_spoolss_replycloseprinter: reply_close_printer failed [%s].\n",
175                         win_errstr(result)));
176
177         /* if it's the last connection, deconnect the IPC$ share */
178         if (smb_connections==1) {
179
180                 cli_shutdown( rpc_pipe_np_smb_conn(notify_cli_pipe) );
181                 notify_cli_pipe = NULL; /* The above call shuts downn the pipe also. */
182
183                 messaging_deregister(smbd_messaging_context(),
184                                      MSG_PRINTER_NOTIFY2, NULL);
185
186                 /* Tell the connections db we're no longer interested in
187                  * printer notify messages. */
188
189                 register_message_flags(false, FLAG_MSG_PRINT_NOTIFY);
190         }
191
192         smb_connections--;
193 }
194
195 /****************************************************************************
196  Functions to free a printer entry datastruct.
197 ****************************************************************************/
198
199 static int printer_entry_destructor(Printer_entry *Printer)
200 {
201         if (Printer->notify.client_connected == true) {
202                 int snum = -1;
203
204                 if ( Printer->printer_type == SPLHND_SERVER) {
205                         snum = -1;
206                         srv_spoolss_replycloseprinter(snum, &Printer->notify.client_hnd);
207                 } else if (Printer->printer_type == SPLHND_PRINTER) {
208                         snum = print_queue_snum(Printer->sharename);
209                         if (snum != -1)
210                                 srv_spoolss_replycloseprinter(snum,
211                                                 &Printer->notify.client_hnd);
212                 }
213         }
214
215         Printer->notify.flags=0;
216         Printer->notify.options=0;
217         Printer->notify.localmachine[0]='\0';
218         Printer->notify.printerlocal=0;
219         TALLOC_FREE(Printer->notify.option);
220         Printer->notify.client_connected = false;
221
222         free_nt_devicemode( &Printer->nt_devmode );
223         free_a_printer( &Printer->printer_info, 2 );
224
225         /* Remove from the internal list. */
226         DLIST_REMOVE(printers_list, Printer);
227         return 0;
228 }
229
230 /****************************************************************************
231   find printer index by handle
232 ****************************************************************************/
233
234 static Printer_entry *find_printer_index_by_hnd(pipes_struct *p,
235                                                 struct policy_handle *hnd)
236 {
237         Printer_entry *find_printer = NULL;
238
239         if(!find_policy_by_hnd(p,hnd,(void **)(void *)&find_printer)) {
240                 DEBUG(2,("find_printer_index_by_hnd: Printer handle not found: "));
241                 return NULL;
242         }
243
244         return find_printer;
245 }
246
247 /****************************************************************************
248  Close printer index by handle.
249 ****************************************************************************/
250
251 static bool close_printer_handle(pipes_struct *p, struct policy_handle *hnd)
252 {
253         Printer_entry *Printer = find_printer_index_by_hnd(p, hnd);
254
255         if (!Printer) {
256                 DEBUG(2,("close_printer_handle: Invalid handle (%s:%u:%u)\n",
257                         OUR_HANDLE(hnd)));
258                 return false;
259         }
260
261         close_policy_hnd(p, hnd);
262
263         return true;
264 }
265
266 /****************************************************************************
267  Delete a printer given a handle.
268 ****************************************************************************/
269
270 WERROR delete_printer_hook(TALLOC_CTX *ctx, NT_USER_TOKEN *token, const char *sharename )
271 {
272         char *cmd = lp_deleteprinter_cmd();
273         char *command = NULL;
274         int ret;
275         SE_PRIV se_printop = SE_PRINT_OPERATOR;
276         bool is_print_op = false;
277
278         /* can't fail if we don't try */
279
280         if ( !*cmd )
281                 return WERR_OK;
282
283         command = talloc_asprintf(ctx,
284                         "%s \"%s\"",
285                         cmd, sharename);
286         if (!command) {
287                 return WERR_NOMEM;
288         }
289         if ( token )
290                 is_print_op = user_has_privileges( token, &se_printop );
291
292         DEBUG(10,("Running [%s]\n", command));
293
294         /********** BEGIN SePrintOperatorPrivlege BLOCK **********/
295
296         if ( is_print_op )
297                 become_root();
298
299         if ( (ret = smbrun(command, NULL)) == 0 ) {
300                 /* Tell everyone we updated smb.conf. */
301                 message_send_all(smbd_messaging_context(),
302                                  MSG_SMB_CONF_UPDATED, NULL, 0, NULL);
303         }
304
305         if ( is_print_op )
306                 unbecome_root();
307
308         /********** END SePrintOperatorPrivlege BLOCK **********/
309
310         DEBUGADD(10,("returned [%d]\n", ret));
311
312         TALLOC_FREE(command);
313
314         if (ret != 0)
315                 return WERR_BADFID; /* What to return here? */
316
317         /* go ahead and re-read the services immediately */
318         reload_services(false);
319
320         if ( lp_servicenumber( sharename )  < 0 )
321                 return WERR_ACCESS_DENIED;
322
323         return WERR_OK;
324 }
325
326 /****************************************************************************
327  Delete a printer given a handle.
328 ****************************************************************************/
329
330 static WERROR delete_printer_handle(pipes_struct *p, struct policy_handle *hnd)
331 {
332         Printer_entry *Printer = find_printer_index_by_hnd(p, hnd);
333
334         if (!Printer) {
335                 DEBUG(2,("delete_printer_handle: Invalid handle (%s:%u:%u)\n",
336                         OUR_HANDLE(hnd)));
337                 return WERR_BADFID;
338         }
339
340         /*
341          * It turns out that Windows allows delete printer on a handle
342          * opened by an admin user, then used on a pipe handle created
343          * by an anonymous user..... but they're working on security.... riiight !
344          * JRA.
345          */
346
347         if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
348                 DEBUG(3, ("delete_printer_handle: denied by handle\n"));
349                 return WERR_ACCESS_DENIED;
350         }
351
352         /* this does not need a become root since the access check has been
353            done on the handle already */
354
355         if (del_a_printer( Printer->sharename ) != 0) {
356                 DEBUG(3,("Error deleting printer %s\n", Printer->sharename));
357                 return WERR_BADFID;
358         }
359
360         return delete_printer_hook(p->mem_ctx, p->server_info->ptok,
361                                    Printer->sharename );
362 }
363
364 /****************************************************************************
365  Return the snum of a printer corresponding to an handle.
366 ****************************************************************************/
367
368 static bool get_printer_snum(pipes_struct *p, struct policy_handle *hnd,
369                              int *number, struct share_params **params)
370 {
371         Printer_entry *Printer = find_printer_index_by_hnd(p, hnd);
372
373         if (!Printer) {
374                 DEBUG(2,("get_printer_snum: Invalid handle (%s:%u:%u)\n",
375                         OUR_HANDLE(hnd)));
376                 return false;
377         }
378
379         switch (Printer->printer_type) {
380                 case SPLHND_PRINTER:
381                         DEBUG(4,("short name:%s\n", Printer->sharename));
382                         *number = print_queue_snum(Printer->sharename);
383                         return (*number != -1);
384                 case SPLHND_SERVER:
385                         return false;
386                 default:
387                         return false;
388         }
389 }
390
391 /****************************************************************************
392  Set printer handle type.
393  Check if it's \\server or \\server\printer
394 ****************************************************************************/
395
396 static bool set_printer_hnd_printertype(Printer_entry *Printer, const char *handlename)
397 {
398         DEBUG(3,("Setting printer type=%s\n", handlename));
399
400         if ( strlen(handlename) < 3 ) {
401                 DEBUGADD(4,("A print server must have at least 1 char ! %s\n", handlename));
402                 return false;
403         }
404
405         /* it's a print server */
406         if (*handlename=='\\' && *(handlename+1)=='\\' && !strchr_m(handlename+2, '\\')) {
407                 DEBUGADD(4,("Printer is a print server\n"));
408                 Printer->printer_type = SPLHND_SERVER;
409         }
410         /* it's a printer (set_printer_hnd_name() will handle port monitors */
411         else {
412                 DEBUGADD(4,("Printer is a printer\n"));
413                 Printer->printer_type = SPLHND_PRINTER;
414         }
415
416         return true;
417 }
418
419 /****************************************************************************
420  Set printer handle name..  Accept names like \\server, \\server\printer,
421  \\server\SHARE, & "\\server\,XcvMonitor Standard TCP/IP Port"    See
422  the MSDN docs regarding OpenPrinter() for details on the XcvData() and
423  XcvDataPort() interface.
424 ****************************************************************************/
425
426 static bool set_printer_hnd_name(Printer_entry *Printer, const char *handlename)
427 {
428         int snum;
429         int n_services=lp_numservices();
430         char *aprinter, *printername;
431         const char *servername;
432         fstring sname;
433         bool found = false;
434         NT_PRINTER_INFO_LEVEL *printer = NULL;
435         WERROR result;
436
437         DEBUG(4,("Setting printer name=%s (len=%lu)\n", handlename,
438                 (unsigned long)strlen(handlename)));
439
440         aprinter = CONST_DISCARD(char *, handlename);
441         if ( *handlename == '\\' ) {
442                 servername = canon_servername(handlename);
443                 if ( (aprinter = strchr_m( servername, '\\' )) != NULL ) {
444                         *aprinter = '\0';
445                         aprinter++;
446                 }
447         } else {
448                 servername = "";
449         }
450
451         /* save the servername to fill in replies on this handle */
452
453         if ( !is_myname_or_ipaddr( servername ) )
454                 return false;
455
456         fstrcpy( Printer->servername, servername );
457
458         if ( Printer->printer_type == SPLHND_SERVER )
459                 return true;
460
461         if ( Printer->printer_type != SPLHND_PRINTER )
462                 return false;
463
464         DEBUGADD(5, ("searching for [%s]\n", aprinter ));
465
466         /* check for the Port Monitor Interface */
467
468         if ( strequal( aprinter, SPL_XCV_MONITOR_TCPMON ) ) {
469                 Printer->printer_type = SPLHND_PORTMON_TCP;
470                 fstrcpy(sname, SPL_XCV_MONITOR_TCPMON);
471                 found = true;
472         }
473         else if ( strequal( aprinter, SPL_XCV_MONITOR_LOCALMON ) ) {
474                 Printer->printer_type = SPLHND_PORTMON_LOCAL;
475                 fstrcpy(sname, SPL_XCV_MONITOR_LOCALMON);
476                 found = true;
477         }
478
479         /* Search all sharenames first as this is easier than pulling
480            the printer_info_2 off of disk. Don't use find_service() since
481            that calls out to map_username() */
482
483         /* do another loop to look for printernames */
484
485         for (snum=0; !found && snum<n_services; snum++) {
486
487                 /* no point going on if this is not a printer */
488
489                 if ( !(lp_snum_ok(snum) && lp_print_ok(snum)) )
490                         continue;
491
492                 fstrcpy(sname, lp_servicename(snum));
493                 if ( strequal( aprinter, sname ) ) {
494                         found = true;
495                         break;
496                 }
497
498                 /* no point looking up the printer object if
499                    we aren't allowing printername != sharename */
500
501                 if ( lp_force_printername(snum) )
502                         continue;
503
504                 fstrcpy(sname, lp_servicename(snum));
505
506                 printer = NULL;
507
508                 /* This call doesn't fill in the location or comment from
509                  * a CUPS server for efficiency with large numbers of printers.
510                  * JRA.
511                  */
512
513                 result = get_a_printer_search( NULL, &printer, 2, sname );
514                 if ( !W_ERROR_IS_OK(result) ) {
515                         DEBUG(0,("set_printer_hnd_name: failed to lookup printer [%s] -- result [%s]\n",
516                                 sname, win_errstr(result)));
517                         continue;
518                 }
519
520                 /* printername is always returned as \\server\printername */
521                 if ( !(printername = strchr_m(&printer->info_2->printername[2], '\\')) ) {
522                         DEBUG(0,("set_printer_hnd_name: info2->printername in wrong format! [%s]\n",
523                                 printer->info_2->printername));
524                         free_a_printer( &printer, 2);
525                         continue;
526                 }
527
528                 printername++;
529
530                 if ( strequal(printername, aprinter) ) {
531                         free_a_printer( &printer, 2);
532                         found = true;
533                         break;
534                 }
535
536                 DEBUGADD(10, ("printername: %s\n", printername));
537
538                 free_a_printer( &printer, 2);
539         }
540
541         free_a_printer( &printer, 2);
542
543         if ( !found ) {
544                 DEBUGADD(4,("Printer not found\n"));
545                 return false;
546         }
547
548         DEBUGADD(4,("set_printer_hnd_name: Printer found: %s -> %s\n", aprinter, sname));
549
550         fstrcpy(Printer->sharename, sname);
551
552         return true;
553 }
554
555 /****************************************************************************
556  Find first available printer slot. creates a printer handle for you.
557  ****************************************************************************/
558
559 static bool open_printer_hnd(pipes_struct *p, struct policy_handle *hnd,
560                              const char *name, uint32_t access_granted)
561 {
562         Printer_entry *new_printer;
563
564         DEBUG(10,("open_printer_hnd: name [%s]\n", name));
565
566         new_printer = TALLOC_ZERO_P(NULL, Printer_entry);
567         if (new_printer == NULL) {
568                 return false;
569         }
570         talloc_set_destructor(new_printer, printer_entry_destructor);
571
572         if (!create_policy_hnd(p, hnd, new_printer)) {
573                 TALLOC_FREE(new_printer);
574                 return false;
575         }
576
577         /* Add to the internal list. */
578         DLIST_ADD(printers_list, new_printer);
579
580         new_printer->notify.option=NULL;
581
582         if (!set_printer_hnd_printertype(new_printer, name)) {
583                 close_printer_handle(p, hnd);
584                 return false;
585         }
586
587         if (!set_printer_hnd_name(new_printer, name)) {
588                 close_printer_handle(p, hnd);
589                 return false;
590         }
591
592         new_printer->access_granted = access_granted;
593
594         DEBUG(5, ("%d printer handles active\n", (int)p->pipe_handles->count ));
595
596         return true;
597 }
598
599 /***************************************************************************
600  check to see if the client motify handle is monitoring the notification
601  given by (notify_type, notify_field).
602  **************************************************************************/
603
604 static bool is_monitoring_event_flags(uint32_t flags, uint16_t notify_type,
605                                       uint16_t notify_field)
606 {
607         return true;
608 }
609
610 static bool is_monitoring_event(Printer_entry *p, uint16_t notify_type,
611                                 uint16_t notify_field)
612 {
613         struct spoolss_NotifyOption *option = p->notify.option;
614         uint32_t i, j;
615
616         /*
617          * Flags should always be zero when the change notify
618          * is registered by the client's spooler.  A user Win32 app
619          * might use the flags though instead of the NOTIFY_OPTION_INFO
620          * --jerry
621          */
622
623         if (!option) {
624                 return false;
625         }
626
627         if (p->notify.flags)
628                 return is_monitoring_event_flags(
629                         p->notify.flags, notify_type, notify_field);
630
631         for (i = 0; i < option->count; i++) {
632
633                 /* Check match for notify_type */
634
635                 if (option->types[i].type != notify_type)
636                         continue;
637
638                 /* Check match for field */
639
640                 for (j = 0; j < option->types[i].count; j++) {
641                         if (option->types[i].fields[j].field == notify_field) {
642                                 return true;
643                         }
644                 }
645         }
646
647         DEBUG(10, ("Open handle for \\\\%s\\%s is not monitoring 0x%02x/0x%02x\n",
648                    p->servername, p->sharename, notify_type, notify_field));
649
650         return false;
651 }
652
653 #define SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(_data, _integer) \
654         _data->data.integer[0] = _integer; \
655         _data->data.integer[1] = 0;
656
657
658 #define SETUP_SPOOLSS_NOTIFY_DATA_STRING(_data, _p) \
659         _data->data.string.string = talloc_strdup(mem_ctx, _p); \
660         if (!_data->data.string.string) {\
661                 _data->data.string.size = 0; \
662         } \
663         _data->data.string.size = strlen_m_term(_p) * 2;
664
665 #define SETUP_SPOOLSS_NOTIFY_DATA_DEVMODE(_data, _devmode) \
666         _data->data.devmode.devmode = _devmode;
667
668 #define SETUP_SPOOLSS_NOTIFY_DATA_SECDESC(_data, _size, _sd) \
669         _data->data.sd.sd = dup_sec_desc(mem_ctx, _sd); \
670         if (!_data->data.sd.sd) { \
671                 _data->data.sd.sd_size = 0; \
672         } \
673         _data->data.sd.sd_size = _size;
674
675 static void init_systemtime_buffer(TALLOC_CTX *mem_ctx,
676                                    struct tm *t,
677                                    const char **pp,
678                                    uint32_t *plen)
679 {
680         struct spoolss_Time st;
681         uint32_t len = 16;
682         char *p;
683
684         if (!init_systemtime(&st, t)) {
685                 return;
686         }
687
688         p = talloc_array(mem_ctx, char, len);
689         if (!p) {
690                 return;
691         }
692
693         /*
694          * Systemtime must be linearized as a set of UINT16's.
695          * Fix from Benjamin (Bj) Kuit bj@it.uts.edu.au
696          */
697
698         SSVAL(p, 0, st.year);
699         SSVAL(p, 2, st.month);
700         SSVAL(p, 4, st.day_of_week);
701         SSVAL(p, 6, st.day);
702         SSVAL(p, 8, st.hour);
703         SSVAL(p, 10, st.minute);
704         SSVAL(p, 12, st.second);
705         SSVAL(p, 14, st.millisecond);
706
707         *pp = p;
708         *plen = len;
709 }
710
711 /* Convert a notification message to a struct spoolss_Notify */
712
713 static void notify_one_value(struct spoolss_notify_msg *msg,
714                              struct spoolss_Notify *data,
715                              TALLOC_CTX *mem_ctx)
716 {
717         SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, msg->notify.value[0]);
718 }
719
720 static void notify_string(struct spoolss_notify_msg *msg,
721                           struct spoolss_Notify *data,
722                           TALLOC_CTX *mem_ctx)
723 {
724         /* The length of the message includes the trailing \0 */
725
726         data->data.string.size = msg->len * 2;
727         data->data.string.string = talloc_strdup(mem_ctx, msg->notify.data);
728         if (!data->data.string.string) {
729                 data->data.string.size = 0;
730                 return;
731         }
732 }
733
734 static void notify_system_time(struct spoolss_notify_msg *msg,
735                                struct spoolss_Notify *data,
736                                TALLOC_CTX *mem_ctx)
737 {
738         data->data.string.string = NULL;
739         data->data.string.size = 0;
740
741         if (msg->len != sizeof(time_t)) {
742                 DEBUG(5, ("notify_system_time: received wrong sized message (%d)\n",
743                           msg->len));
744                 return;
745         }
746
747         init_systemtime_buffer(mem_ctx, gmtime((time_t *)msg->notify.data),
748                                &data->data.string.string,
749                                &data->data.string.size);
750 }
751
752 struct notify2_message_table {
753         const char *name;
754         void (*fn)(struct spoolss_notify_msg *msg,
755                    struct spoolss_Notify *data, TALLOC_CTX *mem_ctx);
756 };
757
758 static struct notify2_message_table printer_notify_table[] = {
759         /* 0x00 */ { "PRINTER_NOTIFY_FIELD_SERVER_NAME", notify_string },
760         /* 0x01 */ { "PRINTER_NOTIFY_FIELD_PRINTER_NAME", notify_string },
761         /* 0x02 */ { "PRINTER_NOTIFY_FIELD_SHARE_NAME", notify_string },
762         /* 0x03 */ { "PRINTER_NOTIFY_FIELD_PORT_NAME", notify_string },
763         /* 0x04 */ { "PRINTER_NOTIFY_FIELD_DRIVER_NAME", notify_string },
764         /* 0x05 */ { "PRINTER_NOTIFY_FIELD_COMMENT", notify_string },
765         /* 0x06 */ { "PRINTER_NOTIFY_FIELD_LOCATION", notify_string },
766         /* 0x07 */ { "PRINTER_NOTIFY_FIELD_DEVMODE", NULL },
767         /* 0x08 */ { "PRINTER_NOTIFY_FIELD_SEPFILE", notify_string },
768         /* 0x09 */ { "PRINTER_NOTIFY_FIELD_PRINT_PROCESSOR", notify_string },
769         /* 0x0a */ { "PRINTER_NOTIFY_FIELD_PARAMETERS", NULL },
770         /* 0x0b */ { "PRINTER_NOTIFY_FIELD_DATATYPE", notify_string },
771         /* 0x0c */ { "PRINTER_NOTIFY_FIELD_SECURITY_DESCRIPTOR", NULL },
772         /* 0x0d */ { "PRINTER_NOTIFY_FIELD_ATTRIBUTES", notify_one_value },
773         /* 0x0e */ { "PRINTER_NOTIFY_FIELD_PRIORITY", notify_one_value },
774         /* 0x0f */ { "PRINTER_NOTIFY_FIELD_DEFAULT_PRIORITY", NULL },
775         /* 0x10 */ { "PRINTER_NOTIFY_FIELD_START_TIME", NULL },
776         /* 0x11 */ { "PRINTER_NOTIFY_FIELD_UNTIL_TIME", NULL },
777         /* 0x12 */ { "PRINTER_NOTIFY_FIELD_STATUS", notify_one_value },
778 };
779
780 static struct notify2_message_table job_notify_table[] = {
781         /* 0x00 */ { "JOB_NOTIFY_FIELD_PRINTER_NAME", NULL },
782         /* 0x01 */ { "JOB_NOTIFY_FIELD_MACHINE_NAME", NULL },
783         /* 0x02 */ { "JOB_NOTIFY_FIELD_PORT_NAME", NULL },
784         /* 0x03 */ { "JOB_NOTIFY_FIELD_USER_NAME", notify_string },
785         /* 0x04 */ { "JOB_NOTIFY_FIELD_NOTIFY_NAME", NULL },
786         /* 0x05 */ { "JOB_NOTIFY_FIELD_DATATYPE", NULL },
787         /* 0x06 */ { "JOB_NOTIFY_FIELD_PRINT_PROCESSOR", NULL },
788         /* 0x07 */ { "JOB_NOTIFY_FIELD_PARAMETERS", NULL },
789         /* 0x08 */ { "JOB_NOTIFY_FIELD_DRIVER_NAME", NULL },
790         /* 0x09 */ { "JOB_NOTIFY_FIELD_DEVMODE", NULL },
791         /* 0x0a */ { "JOB_NOTIFY_FIELD_STATUS", notify_one_value },
792         /* 0x0b */ { "JOB_NOTIFY_FIELD_STATUS_STRING", NULL },
793         /* 0x0c */ { "JOB_NOTIFY_FIELD_SECURITY_DESCRIPTOR", NULL },
794         /* 0x0d */ { "JOB_NOTIFY_FIELD_DOCUMENT", notify_string },
795         /* 0x0e */ { "JOB_NOTIFY_FIELD_PRIORITY", NULL },
796         /* 0x0f */ { "JOB_NOTIFY_FIELD_POSITION", NULL },
797         /* 0x10 */ { "JOB_NOTIFY_FIELD_SUBMITTED", notify_system_time },
798         /* 0x11 */ { "JOB_NOTIFY_FIELD_START_TIME", NULL },
799         /* 0x12 */ { "JOB_NOTIFY_FIELD_UNTIL_TIME", NULL },
800         /* 0x13 */ { "JOB_NOTIFY_FIELD_TIME", NULL },
801         /* 0x14 */ { "JOB_NOTIFY_FIELD_TOTAL_PAGES", notify_one_value },
802         /* 0x15 */ { "JOB_NOTIFY_FIELD_PAGES_PRINTED", NULL },
803         /* 0x16 */ { "JOB_NOTIFY_FIELD_TOTAL_BYTES", notify_one_value },
804         /* 0x17 */ { "JOB_NOTIFY_FIELD_BYTES_PRINTED", NULL },
805 };
806
807
808 /***********************************************************************
809  Allocate talloc context for container object
810  **********************************************************************/
811
812 static void notify_msg_ctr_init( SPOOLSS_NOTIFY_MSG_CTR *ctr )
813 {
814         if ( !ctr )
815                 return;
816
817         ctr->ctx = talloc_init("notify_msg_ctr_init %p", ctr);
818
819         return;
820 }
821
822 /***********************************************************************
823  release all allocated memory and zero out structure
824  **********************************************************************/
825
826 static void notify_msg_ctr_destroy( SPOOLSS_NOTIFY_MSG_CTR *ctr )
827 {
828         if ( !ctr )
829                 return;
830
831         if ( ctr->ctx )
832                 talloc_destroy(ctr->ctx);
833
834         ZERO_STRUCTP(ctr);
835
836         return;
837 }
838
839 /***********************************************************************
840  **********************************************************************/
841
842 static TALLOC_CTX* notify_ctr_getctx( SPOOLSS_NOTIFY_MSG_CTR *ctr )
843 {
844         if ( !ctr )
845                 return NULL;
846
847         return ctr->ctx;
848 }
849
850 /***********************************************************************
851  **********************************************************************/
852
853 static SPOOLSS_NOTIFY_MSG_GROUP* notify_ctr_getgroup( SPOOLSS_NOTIFY_MSG_CTR *ctr, uint32_t idx )
854 {
855         if ( !ctr || !ctr->msg_groups )
856                 return NULL;
857
858         if ( idx >= ctr->num_groups )
859                 return NULL;
860
861         return &ctr->msg_groups[idx];
862
863 }
864
865 /***********************************************************************
866  How many groups of change messages do we have ?
867  **********************************************************************/
868
869 static int notify_msg_ctr_numgroups( SPOOLSS_NOTIFY_MSG_CTR *ctr )
870 {
871         if ( !ctr )
872                 return 0;
873
874         return ctr->num_groups;
875 }
876
877 /***********************************************************************
878  Add a SPOOLSS_NOTIFY_MSG_CTR to the correct group
879  **********************************************************************/
880
881 static int notify_msg_ctr_addmsg( SPOOLSS_NOTIFY_MSG_CTR *ctr, SPOOLSS_NOTIFY_MSG *msg )
882 {
883         SPOOLSS_NOTIFY_MSG_GROUP        *groups = NULL;
884         SPOOLSS_NOTIFY_MSG_GROUP        *msg_grp = NULL;
885         SPOOLSS_NOTIFY_MSG              *msg_list = NULL;
886         int                             i, new_slot;
887
888         if ( !ctr || !msg )
889                 return 0;
890
891         /* loop over all groups looking for a matching printer name */
892
893         for ( i=0; i<ctr->num_groups; i++ ) {
894                 if ( strcmp(ctr->msg_groups[i].printername, msg->printer) == 0 )
895                         break;
896         }
897
898         /* add a new group? */
899
900         if ( i == ctr->num_groups ) {
901                 ctr->num_groups++;
902
903                 if ( !(groups = TALLOC_REALLOC_ARRAY( ctr->ctx, ctr->msg_groups, SPOOLSS_NOTIFY_MSG_GROUP, ctr->num_groups)) ) {
904                         DEBUG(0,("notify_msg_ctr_addmsg: talloc_realloc() failed!\n"));
905                         return 0;
906                 }
907                 ctr->msg_groups = groups;
908
909                 /* clear the new entry and set the printer name */
910
911                 ZERO_STRUCT( ctr->msg_groups[ctr->num_groups-1] );
912                 fstrcpy( ctr->msg_groups[ctr->num_groups-1].printername, msg->printer );
913         }
914
915         /* add the change messages; 'i' is the correct index now regardless */
916
917         msg_grp = &ctr->msg_groups[i];
918
919         msg_grp->num_msgs++;
920
921         if ( !(msg_list = TALLOC_REALLOC_ARRAY( ctr->ctx, msg_grp->msgs, SPOOLSS_NOTIFY_MSG, msg_grp->num_msgs )) ) {
922                 DEBUG(0,("notify_msg_ctr_addmsg: talloc_realloc() failed for new message [%d]!\n", msg_grp->num_msgs));
923                 return 0;
924         }
925         msg_grp->msgs = msg_list;
926
927         new_slot = msg_grp->num_msgs-1;
928         memcpy( &msg_grp->msgs[new_slot], msg, sizeof(SPOOLSS_NOTIFY_MSG) );
929
930         /* need to allocate own copy of data */
931
932         if ( msg->len != 0 )
933                 msg_grp->msgs[new_slot].notify.data = (char *)
934                         TALLOC_MEMDUP( ctr->ctx, msg->notify.data, msg->len );
935
936         return ctr->num_groups;
937 }
938
939 /***********************************************************************
940  Send a change notication message on all handles which have a call
941  back registered
942  **********************************************************************/
943
944 static void send_notify2_changes( SPOOLSS_NOTIFY_MSG_CTR *ctr, uint32_t idx )
945 {
946         Printer_entry            *p;
947         TALLOC_CTX               *mem_ctx = notify_ctr_getctx( ctr );
948         SPOOLSS_NOTIFY_MSG_GROUP *msg_group = notify_ctr_getgroup( ctr, idx );
949         SPOOLSS_NOTIFY_MSG       *messages;
950         int                      sending_msg_count;
951
952         if ( !msg_group ) {
953                 DEBUG(5,("send_notify2_changes() called with no msg group!\n"));
954                 return;
955         }
956
957         messages = msg_group->msgs;
958
959         if ( !messages ) {
960                 DEBUG(5,("send_notify2_changes() called with no messages!\n"));
961                 return;
962         }
963
964         DEBUG(8,("send_notify2_changes: Enter...[%s]\n", msg_group->printername));
965
966         /* loop over all printers */
967
968         for (p = printers_list; p; p = p->next) {
969                 struct spoolss_Notify *notifies;
970                 uint32_t count = 0;
971                 uint32_t id;
972                 int     i;
973
974                 /* Is there notification on this handle? */
975
976                 if ( !p->notify.client_connected )
977                         continue;
978
979                 DEBUG(10,("Client connected! [\\\\%s\\%s]\n", p->servername, p->sharename));
980
981                 /* For this printer?  Print servers always receive
982                    notifications. */
983
984                 if ( ( p->printer_type == SPLHND_PRINTER )  &&
985                     ( !strequal(msg_group->printername, p->sharename) ) )
986                         continue;
987
988                 DEBUG(10,("Our printer\n"));
989
990                 /* allocate the max entries possible */
991
992                 notifies = TALLOC_ZERO_ARRAY(mem_ctx, struct spoolss_Notify, msg_group->num_msgs);
993                 if (!notifies) {
994                         return;
995                 }
996
997                 /* build the array of change notifications */
998
999                 sending_msg_count = 0;
1000
1001                 for ( i=0; i<msg_group->num_msgs; i++ ) {
1002                         SPOOLSS_NOTIFY_MSG      *msg = &messages[i];
1003
1004                         /* Are we monitoring this event? */
1005
1006                         if (!is_monitoring_event(p, msg->type, msg->field))
1007                                 continue;
1008
1009                         sending_msg_count++;
1010
1011
1012                         DEBUG(10,("process_notify2_message: Sending message type [0x%x] field [0x%2x] for printer [%s]\n",
1013                                 msg->type, msg->field, p->sharename));
1014
1015                         /*
1016                          * if the is a printer notification handle and not a job notification
1017                          * type, then set the id to 0.  Other wise just use what was specified
1018                          * in the message.
1019                          *
1020                          * When registering change notification on a print server handle
1021                          * we always need to send back the id (snum) matching the printer
1022                          * for which the change took place.  For change notify registered
1023                          * on a printer handle, this does not matter and the id should be 0.
1024                          *
1025                          * --jerry
1026                          */
1027
1028                         if ( ( p->printer_type == SPLHND_PRINTER ) && ( msg->type == PRINTER_NOTIFY_TYPE ) )
1029                                 id = 0;
1030                         else
1031                                 id = msg->id;
1032
1033
1034                         /* Convert unix jobid to smb jobid */
1035
1036                         if (msg->flags & SPOOLSS_NOTIFY_MSG_UNIX_JOBID) {
1037                                 id = sysjob_to_jobid(msg->id);
1038
1039                                 if (id == -1) {
1040                                         DEBUG(3, ("no such unix jobid %d\n", msg->id));
1041                                         goto done;
1042                                 }
1043                         }
1044
1045                         construct_info_data( &notifies[count], msg->type, msg->field, id );
1046
1047                         switch(msg->type) {
1048                         case PRINTER_NOTIFY_TYPE:
1049                                 if ( printer_notify_table[msg->field].fn )
1050                                         printer_notify_table[msg->field].fn(msg, &notifies[count], mem_ctx);
1051                                 break;
1052
1053                         case JOB_NOTIFY_TYPE:
1054                                 if ( job_notify_table[msg->field].fn )
1055                                         job_notify_table[msg->field].fn(msg, &notifies[count], mem_ctx);
1056                                 break;
1057
1058                         default:
1059                                 DEBUG(5, ("Unknown notification type %d\n", msg->type));
1060                                 goto done;
1061                         }
1062
1063                         count++;
1064                 }
1065
1066                 if ( sending_msg_count ) {
1067                         NTSTATUS status;
1068                         WERROR werr;
1069                         union spoolss_ReplyPrinterInfo info;
1070                         struct spoolss_NotifyInfo info0;
1071                         uint32_t reply_result;
1072
1073                         info0.version   = 0x2;
1074                         info0.flags     = count ? 0x00020000 /* ??? */ : PRINTER_NOTIFY_INFO_DISCARDED;
1075                         info0.count     = count;
1076                         info0.notifies  = notifies;
1077
1078                         info.info0 = &info0;
1079
1080                         status = rpccli_spoolss_RouterReplyPrinterEx(notify_cli_pipe, mem_ctx,
1081                                                                      &p->notify.client_hnd,
1082                                                                      p->notify.change, /* color */
1083                                                                      p->notify.flags,
1084                                                                      &reply_result,
1085                                                                      0, /* reply_type, must be 0 */
1086                                                                      info,
1087                                                                      &werr);
1088                         if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(werr)) {
1089                                 DEBUG(1,("RouterReplyPrinterEx to client: %s failed: %s\n",
1090                                         notify_cli_pipe->srv_name_slash,
1091                                         win_errstr(werr)));
1092                         }
1093                         switch (reply_result) {
1094                                 case 0:
1095                                         break;
1096                                 case PRINTER_NOTIFY_INFO_DISCARDED:
1097                                 case PRINTER_NOTIFY_INFO_DISCARDNOTED:
1098                                 case PRINTER_NOTIFY_INFO_COLOR_MISMATCH:
1099                                         break;
1100                                 default:
1101                                         break;
1102                         }
1103                 }
1104         }
1105
1106 done:
1107         DEBUG(8,("send_notify2_changes: Exit...\n"));
1108         return;
1109 }
1110
1111 /***********************************************************************
1112  **********************************************************************/
1113
1114 static bool notify2_unpack_msg( SPOOLSS_NOTIFY_MSG *msg, struct timeval *tv, void *buf, size_t len )
1115 {
1116
1117         uint32_t tv_sec, tv_usec;
1118         size_t offset = 0;
1119
1120         /* Unpack message */
1121
1122         offset += tdb_unpack((uint8_t *)buf + offset, len - offset, "f",
1123                              msg->printer);
1124
1125         offset += tdb_unpack((uint8_t *)buf + offset, len - offset, "ddddddd",
1126                                 &tv_sec, &tv_usec,
1127                                 &msg->type, &msg->field, &msg->id, &msg->len, &msg->flags);
1128
1129         if (msg->len == 0)
1130                 tdb_unpack((uint8_t *)buf + offset, len - offset, "dd",
1131                            &msg->notify.value[0], &msg->notify.value[1]);
1132         else
1133                 tdb_unpack((uint8_t *)buf + offset, len - offset, "B",
1134                            &msg->len, &msg->notify.data);
1135
1136         DEBUG(3, ("notify2_unpack_msg: got NOTIFY2 message for printer %s, jobid %u type %d, field 0x%02x, flags 0x%04x\n",
1137                   msg->printer, (unsigned int)msg->id, msg->type, msg->field, msg->flags));
1138
1139         tv->tv_sec = tv_sec;
1140         tv->tv_usec = tv_usec;
1141
1142         if (msg->len == 0)
1143                 DEBUG(3, ("notify2_unpack_msg: value1 = %d, value2 = %d\n", msg->notify.value[0],
1144                           msg->notify.value[1]));
1145         else
1146                 dump_data(3, (uint8_t *)msg->notify.data, msg->len);
1147
1148         return true;
1149 }
1150
1151 /********************************************************************
1152  Receive a notify2 message list
1153  ********************************************************************/
1154
1155 static void receive_notify2_message_list(struct messaging_context *msg,
1156                                          void *private_data,
1157                                          uint32_t msg_type,
1158                                          struct server_id server_id,
1159                                          DATA_BLOB *data)
1160 {
1161         size_t                  msg_count, i;
1162         char                    *buf = (char *)data->data;
1163         char                    *msg_ptr;
1164         size_t                  msg_len;
1165         SPOOLSS_NOTIFY_MSG      notify;
1166         SPOOLSS_NOTIFY_MSG_CTR  messages;
1167         int                     num_groups;
1168
1169         if (data->length < 4) {
1170                 DEBUG(0,("receive_notify2_message_list: bad message format (len < 4)!\n"));
1171                 return;
1172         }
1173
1174         msg_count = IVAL(buf, 0);
1175         msg_ptr = buf + 4;
1176
1177         DEBUG(5, ("receive_notify2_message_list: got %lu messages in list\n", (unsigned long)msg_count));
1178
1179         if (msg_count == 0) {
1180                 DEBUG(0,("receive_notify2_message_list: bad message format (msg_count == 0) !\n"));
1181                 return;
1182         }
1183
1184         /* initialize the container */
1185
1186         ZERO_STRUCT( messages );
1187         notify_msg_ctr_init( &messages );
1188
1189         /*
1190          * build message groups for each printer identified
1191          * in a change_notify msg.  Remember that a PCN message
1192          * includes the handle returned for the srv_spoolss_replyopenprinter()
1193          * call.  Therefore messages are grouped according to printer handle.
1194          */
1195
1196         for ( i=0; i<msg_count; i++ ) {
1197                 struct timeval msg_tv;
1198
1199                 if (msg_ptr + 4 - buf > data->length) {
1200                         DEBUG(0,("receive_notify2_message_list: bad message format (len > buf_size) !\n"));
1201                         return;
1202                 }
1203
1204                 msg_len = IVAL(msg_ptr,0);
1205                 msg_ptr += 4;
1206
1207                 if (msg_ptr + msg_len - buf > data->length) {
1208                         DEBUG(0,("receive_notify2_message_list: bad message format (bad len) !\n"));
1209                         return;
1210                 }
1211
1212                 /* unpack messages */
1213
1214                 ZERO_STRUCT( notify );
1215                 notify2_unpack_msg( &notify, &msg_tv, msg_ptr, msg_len );
1216                 msg_ptr += msg_len;
1217
1218                 /* add to correct list in container */
1219
1220                 notify_msg_ctr_addmsg( &messages, &notify );
1221
1222                 /* free memory that might have been allocated by notify2_unpack_msg() */
1223
1224                 if ( notify.len != 0 )
1225                         SAFE_FREE( notify.notify.data );
1226         }
1227
1228         /* process each group of messages */
1229
1230         num_groups = notify_msg_ctr_numgroups( &messages );
1231         for ( i=0; i<num_groups; i++ )
1232                 send_notify2_changes( &messages, i );
1233
1234
1235         /* cleanup */
1236
1237         DEBUG(10,("receive_notify2_message_list: processed %u messages\n",
1238                 (uint32_t)msg_count ));
1239
1240         notify_msg_ctr_destroy( &messages );
1241
1242         return;
1243 }
1244
1245 /********************************************************************
1246  Send a message to ourself about new driver being installed
1247  so we can upgrade the information for each printer bound to this
1248  driver
1249  ********************************************************************/
1250
1251 static bool srv_spoolss_drv_upgrade_printer(const char *drivername)
1252 {
1253         int len = strlen(drivername);
1254
1255         if (!len)
1256                 return false;
1257
1258         DEBUG(10,("srv_spoolss_drv_upgrade_printer: Sending message about driver upgrade [%s]\n",
1259                 drivername));
1260
1261         messaging_send_buf(smbd_messaging_context(), procid_self(),
1262                            MSG_PRINTER_DRVUPGRADE,
1263                            (uint8_t *)drivername, len+1);
1264
1265         return true;
1266 }
1267
1268 /**********************************************************************
1269  callback to receive a MSG_PRINTER_DRVUPGRADE message and interate
1270  over all printers, upgrading ones as necessary
1271  **********************************************************************/
1272
1273 void do_drv_upgrade_printer(struct messaging_context *msg,
1274                             void *private_data,
1275                             uint32_t msg_type,
1276                             struct server_id server_id,
1277                             DATA_BLOB *data)
1278 {
1279         fstring drivername;
1280         int snum;
1281         int n_services = lp_numservices();
1282         size_t len;
1283
1284         len = MIN(data->length,sizeof(drivername)-1);
1285         strncpy(drivername, (const char *)data->data, len);
1286
1287         DEBUG(10,("do_drv_upgrade_printer: Got message for new driver [%s]\n", drivername ));
1288
1289         /* Iterate the printer list */
1290
1291         for (snum=0; snum<n_services; snum++)
1292         {
1293                 if (lp_snum_ok(snum) && lp_print_ok(snum) )
1294                 {
1295                         WERROR result;
1296                         NT_PRINTER_INFO_LEVEL *printer = NULL;
1297
1298                         result = get_a_printer(NULL, &printer, 2, lp_const_servicename(snum));
1299                         if (!W_ERROR_IS_OK(result))
1300                                 continue;
1301
1302                         if (printer && printer->info_2 && !strcmp(drivername, printer->info_2->drivername))
1303                         {
1304                                 DEBUG(6,("Updating printer [%s]\n", printer->info_2->printername));
1305
1306                                 /* all we care about currently is the change_id */
1307
1308                                 result = mod_a_printer(printer, 2);
1309                                 if (!W_ERROR_IS_OK(result)) {
1310                                         DEBUG(3,("do_drv_upgrade_printer: mod_a_printer() failed with status [%s]\n",
1311                                                 win_errstr(result)));
1312                                 }
1313                         }
1314
1315                         free_a_printer(&printer, 2);
1316                 }
1317         }
1318
1319         /* all done */
1320 }
1321
1322 /********************************************************************
1323  Update the cache for all printq's with a registered client
1324  connection
1325  ********************************************************************/
1326
1327 void update_monitored_printq_cache( void )
1328 {
1329         Printer_entry *printer = printers_list;
1330         int snum;
1331
1332         /* loop through all printers and update the cache where
1333            client_connected == true */
1334         while ( printer )
1335         {
1336                 if ( (printer->printer_type == SPLHND_PRINTER)
1337                         && printer->notify.client_connected )
1338                 {
1339                         snum = print_queue_snum(printer->sharename);
1340                         print_queue_status( snum, NULL, NULL );
1341                 }
1342
1343                 printer = printer->next;
1344         }
1345
1346         return;
1347 }
1348 /********************************************************************
1349  Send a message to ourself about new driver being installed
1350  so we can upgrade the information for each printer bound to this
1351  driver
1352  ********************************************************************/
1353
1354 static bool srv_spoolss_reset_printerdata(char* drivername)
1355 {
1356         int len = strlen(drivername);
1357
1358         if (!len)
1359                 return false;
1360
1361         DEBUG(10,("srv_spoolss_reset_printerdata: Sending message about resetting printerdata [%s]\n",
1362                 drivername));
1363
1364         messaging_send_buf(smbd_messaging_context(), procid_self(),
1365                            MSG_PRINTERDATA_INIT_RESET,
1366                            (uint8_t *)drivername, len+1);
1367
1368         return true;
1369 }
1370
1371 /**********************************************************************
1372  callback to receive a MSG_PRINTERDATA_INIT_RESET message and interate
1373  over all printers, resetting printer data as neessary
1374  **********************************************************************/
1375
1376 void reset_all_printerdata(struct messaging_context *msg,
1377                            void *private_data,
1378                            uint32_t msg_type,
1379                            struct server_id server_id,
1380                            DATA_BLOB *data)
1381 {
1382         fstring drivername;
1383         int snum;
1384         int n_services = lp_numservices();
1385         size_t len;
1386
1387         len = MIN( data->length, sizeof(drivername)-1 );
1388         strncpy( drivername, (const char *)data->data, len );
1389
1390         DEBUG(10,("reset_all_printerdata: Got message for new driver [%s]\n", drivername ));
1391
1392         /* Iterate the printer list */
1393
1394         for ( snum=0; snum<n_services; snum++ )
1395         {
1396                 if ( lp_snum_ok(snum) && lp_print_ok(snum) )
1397                 {
1398                         WERROR result;
1399                         NT_PRINTER_INFO_LEVEL *printer = NULL;
1400
1401                         result = get_a_printer( NULL, &printer, 2, lp_const_servicename(snum) );
1402                         if ( !W_ERROR_IS_OK(result) )
1403                                 continue;
1404
1405                         /*
1406                          * if the printer is bound to the driver,
1407                          * then reset to the new driver initdata
1408                          */
1409
1410                         if ( printer && printer->info_2 && !strcmp(drivername, printer->info_2->drivername) )
1411                         {
1412                                 DEBUG(6,("reset_all_printerdata: Updating printer [%s]\n", printer->info_2->printername));
1413
1414                                 if ( !set_driver_init(printer, 2) ) {
1415                                         DEBUG(5,("reset_all_printerdata: Error resetting printer data for printer [%s], driver [%s]!\n",
1416                                                 printer->info_2->printername, printer->info_2->drivername));
1417                                 }
1418
1419                                 result = mod_a_printer( printer, 2 );
1420                                 if ( !W_ERROR_IS_OK(result) ) {
1421                                         DEBUG(3,("reset_all_printerdata: mod_a_printer() failed!  (%s)\n",
1422                                                 get_dos_error_msg(result)));
1423                                 }
1424                         }
1425
1426                         free_a_printer( &printer, 2 );
1427                 }
1428         }
1429
1430         /* all done */
1431
1432         return;
1433 }
1434
1435 /****************************************************************
1436  _spoolss_OpenPrinter
1437 ****************************************************************/
1438
1439 WERROR _spoolss_OpenPrinter(pipes_struct *p,
1440                             struct spoolss_OpenPrinter *r)
1441 {
1442         struct spoolss_OpenPrinterEx e;
1443         WERROR werr;
1444
1445         ZERO_STRUCT(e.in.userlevel);
1446
1447         e.in.printername        = r->in.printername;
1448         e.in.datatype           = r->in.datatype;
1449         e.in.devmode_ctr        = r->in.devmode_ctr;
1450         e.in.access_mask        = r->in.access_mask;
1451         e.in.level              = 0;
1452
1453         e.out.handle            = r->out.handle;
1454
1455         werr = _spoolss_OpenPrinterEx(p, &e);
1456
1457         if (W_ERROR_EQUAL(werr, WERR_INVALID_PARAM)) {
1458                 /* OpenPrinterEx returns this for a bad
1459                  * printer name. We must return WERR_INVALID_PRINTER_NAME
1460                  * instead.
1461                  */
1462                 werr = WERR_INVALID_PRINTER_NAME;
1463         }
1464
1465         return werr;
1466 }
1467
1468 /********************************************************************
1469  ********************************************************************/
1470
1471 bool convert_devicemode(const char *printername,
1472                         const struct spoolss_DeviceMode *devmode,
1473                         NT_DEVICEMODE **pp_nt_devmode)
1474 {
1475         NT_DEVICEMODE *nt_devmode = *pp_nt_devmode;
1476
1477         /*
1478          * Ensure nt_devmode is a valid pointer
1479          * as we will be overwriting it.
1480          */
1481
1482         if (nt_devmode == NULL) {
1483                 DEBUG(5, ("convert_devicemode: allocating a generic devmode\n"));
1484                 if ((nt_devmode = construct_nt_devicemode(printername)) == NULL)
1485                         return false;
1486         }
1487
1488         rpcstr_push(nt_devmode->devicename, devmode->devicename, 31, 0);
1489         rpcstr_push(nt_devmode->formname, devmode->formname, 31, 0);
1490
1491         nt_devmode->specversion         = devmode->specversion;
1492         nt_devmode->driverversion       = devmode->driverversion;
1493         nt_devmode->size                = devmode->size;
1494         nt_devmode->fields              = devmode->fields;
1495         nt_devmode->orientation         = devmode->orientation;
1496         nt_devmode->papersize           = devmode->papersize;
1497         nt_devmode->paperlength         = devmode->paperlength;
1498         nt_devmode->paperwidth          = devmode->paperwidth;
1499         nt_devmode->scale               = devmode->scale;
1500         nt_devmode->copies              = devmode->copies;
1501         nt_devmode->defaultsource       = devmode->defaultsource;
1502         nt_devmode->printquality        = devmode->printquality;
1503         nt_devmode->color               = devmode->color;
1504         nt_devmode->duplex              = devmode->duplex;
1505         nt_devmode->yresolution         = devmode->yresolution;
1506         nt_devmode->ttoption            = devmode->ttoption;
1507         nt_devmode->collate             = devmode->collate;
1508
1509         nt_devmode->logpixels           = devmode->logpixels;
1510         nt_devmode->bitsperpel          = devmode->bitsperpel;
1511         nt_devmode->pelswidth           = devmode->pelswidth;
1512         nt_devmode->pelsheight          = devmode->pelsheight;
1513         nt_devmode->displayflags        = devmode->displayflags;
1514         nt_devmode->displayfrequency    = devmode->displayfrequency;
1515         nt_devmode->icmmethod           = devmode->icmmethod;
1516         nt_devmode->icmintent           = devmode->icmintent;
1517         nt_devmode->mediatype           = devmode->mediatype;
1518         nt_devmode->dithertype          = devmode->dithertype;
1519         nt_devmode->reserved1           = devmode->reserved1;
1520         nt_devmode->reserved2           = devmode->reserved2;
1521         nt_devmode->panningwidth        = devmode->panningwidth;
1522         nt_devmode->panningheight       = devmode->panningheight;
1523
1524         /*
1525          * Only change private and driverextra if the incoming devmode
1526          * has a new one. JRA.
1527          */
1528
1529         if ((devmode->__driverextra_length != 0) && (devmode->driverextra_data.data != NULL)) {
1530                 SAFE_FREE(nt_devmode->nt_dev_private);
1531                 nt_devmode->driverextra = devmode->__driverextra_length;
1532                 if((nt_devmode->nt_dev_private = SMB_MALLOC_ARRAY(uint8_t, nt_devmode->driverextra)) == NULL)
1533                         return false;
1534                 memcpy(nt_devmode->nt_dev_private, devmode->driverextra_data.data, nt_devmode->driverextra);
1535         }
1536
1537         *pp_nt_devmode = nt_devmode;
1538
1539         return true;
1540 }
1541
1542 /****************************************************************
1543  _spoolss_OpenPrinterEx
1544 ****************************************************************/
1545
1546 WERROR _spoolss_OpenPrinterEx(pipes_struct *p,
1547                               struct spoolss_OpenPrinterEx *r)
1548 {
1549         int snum;
1550         Printer_entry *Printer=NULL;
1551
1552         if (!r->in.printername) {
1553                 return WERR_INVALID_PARAM;
1554         }
1555
1556         /* some sanity check because you can open a printer or a print server */
1557         /* aka: \\server\printer or \\server */
1558
1559         DEBUGADD(3,("checking name: %s\n", r->in.printername));
1560
1561         if (!open_printer_hnd(p, r->out.handle, r->in.printername, 0)) {
1562                 ZERO_STRUCTP(r->out.handle);
1563                 return WERR_INVALID_PARAM;
1564         }
1565
1566         Printer = find_printer_index_by_hnd(p, r->out.handle);
1567         if ( !Printer ) {
1568                 DEBUG(0,("_spoolss_OpenPrinterEx: logic error.  Can't find printer "
1569                         "handle we created for printer %s\n", r->in.printername));
1570                 close_printer_handle(p, r->out.handle);
1571                 ZERO_STRUCTP(r->out.handle);
1572                 return WERR_INVALID_PARAM;
1573         }
1574
1575         /*
1576          * First case: the user is opening the print server:
1577          *
1578          * Disallow MS AddPrinterWizard if parameter disables it. A Win2k
1579          * client 1st tries an OpenPrinterEx with access==0, MUST be allowed.
1580          *
1581          * Then both Win2k and WinNT clients try an OpenPrinterEx with
1582          * SERVER_ALL_ACCESS, which we allow only if the user is root (uid=0)
1583          * or if the user is listed in the smb.conf printer admin parameter.
1584          *
1585          * Then they try OpenPrinterEx with SERVER_READ which we allow. This lets the
1586          * client view printer folder, but does not show the MSAPW.
1587          *
1588          * Note: this test needs code to check access rights here too. Jeremy
1589          * could you look at this?
1590          *
1591          * Second case: the user is opening a printer:
1592          * NT doesn't let us connect to a printer if the connecting user
1593          * doesn't have print permission.
1594          *
1595          * Third case: user is opening a Port Monitor
1596          * access checks same as opening a handle to the print server.
1597          */
1598
1599         switch (Printer->printer_type )
1600         {
1601         case SPLHND_SERVER:
1602         case SPLHND_PORTMON_TCP:
1603         case SPLHND_PORTMON_LOCAL:
1604                 /* Printserver handles use global struct... */
1605
1606                 snum = -1;
1607
1608                 /* Map standard access rights to object specific access rights */
1609
1610                 se_map_standard(&r->in.access_mask,
1611                                 &printserver_std_mapping);
1612
1613                 /* Deny any object specific bits that don't apply to print
1614                    servers (i.e printer and job specific bits) */
1615
1616                 r->in.access_mask &= SPECIFIC_RIGHTS_MASK;
1617
1618                 if (r->in.access_mask &
1619                     ~(SERVER_ACCESS_ADMINISTER | SERVER_ACCESS_ENUMERATE)) {
1620                         DEBUG(3, ("access DENIED for non-printserver bits\n"));
1621                         close_printer_handle(p, r->out.handle);
1622                         ZERO_STRUCTP(r->out.handle);
1623                         return WERR_ACCESS_DENIED;
1624                 }
1625
1626                 /* Allow admin access */
1627
1628                 if ( r->in.access_mask & SERVER_ACCESS_ADMINISTER )
1629                 {
1630                         SE_PRIV se_printop = SE_PRINT_OPERATOR;
1631
1632                         if (!lp_ms_add_printer_wizard()) {
1633                                 close_printer_handle(p, r->out.handle);
1634                                 ZERO_STRUCTP(r->out.handle);
1635                                 return WERR_ACCESS_DENIED;
1636                         }
1637
1638                         /* if the user is not root, doesn't have SE_PRINT_OPERATOR privilege,
1639                            and not a printer admin, then fail */
1640
1641                         if ((p->server_info->utok.uid != 0) &&
1642                             !user_has_privileges(p->server_info->ptok,
1643                                                  &se_printop ) &&
1644                             !token_contains_name_in_list(
1645                                     uidtoname(p->server_info->utok.uid),
1646                                     NULL, NULL,
1647                                     p->server_info->ptok,
1648                                     lp_printer_admin(snum))) {
1649                                 close_printer_handle(p, r->out.handle);
1650                                 ZERO_STRUCTP(r->out.handle);
1651                                 return WERR_ACCESS_DENIED;
1652                         }
1653
1654                         r->in.access_mask = SERVER_ACCESS_ADMINISTER;
1655                 }
1656                 else
1657                 {
1658                         r->in.access_mask = SERVER_ACCESS_ENUMERATE;
1659                 }
1660
1661                 DEBUG(4,("Setting print server access = %s\n", (r->in.access_mask == SERVER_ACCESS_ADMINISTER)
1662                         ? "SERVER_ACCESS_ADMINISTER" : "SERVER_ACCESS_ENUMERATE" ));
1663
1664                 /* We fall through to return WERR_OK */
1665                 break;
1666
1667         case SPLHND_PRINTER:
1668                 /* NT doesn't let us connect to a printer if the connecting user
1669                    doesn't have print permission.  */
1670
1671                 if (!get_printer_snum(p, r->out.handle, &snum, NULL)) {
1672                         close_printer_handle(p, r->out.handle);
1673                         ZERO_STRUCTP(r->out.handle);
1674                         return WERR_BADFID;
1675                 }
1676
1677                 se_map_standard(&r->in.access_mask, &printer_std_mapping);
1678
1679                 /* map an empty access mask to the minimum access mask */
1680                 if (r->in.access_mask == 0x0)
1681                         r->in.access_mask = PRINTER_ACCESS_USE;
1682
1683                 /*
1684                  * If we are not serving the printer driver for this printer,
1685                  * map PRINTER_ACCESS_ADMINISTER to PRINTER_ACCESS_USE.  This
1686                  * will keep NT clients happy  --jerry
1687                  */
1688
1689                 if (lp_use_client_driver(snum)
1690                         && (r->in.access_mask & PRINTER_ACCESS_ADMINISTER))
1691                 {
1692                         r->in.access_mask = PRINTER_ACCESS_USE;
1693                 }
1694
1695                 /* check smb.conf parameters and the the sec_desc */
1696
1697                 if ( !check_access(get_client_fd(), lp_hostsallow(snum), lp_hostsdeny(snum)) ) {
1698                         DEBUG(3, ("access DENIED (hosts allow/deny) for printer open\n"));
1699                         ZERO_STRUCTP(r->out.handle);
1700                         return WERR_ACCESS_DENIED;
1701                 }
1702
1703                 if (!user_ok_token(uidtoname(p->server_info->utok.uid), NULL,
1704                                    p->server_info->ptok, snum) ||
1705                     !print_access_check(p->server_info, snum,
1706                                         r->in.access_mask)) {
1707                         DEBUG(3, ("access DENIED for printer open\n"));
1708                         close_printer_handle(p, r->out.handle);
1709                         ZERO_STRUCTP(r->out.handle);
1710                         return WERR_ACCESS_DENIED;
1711                 }
1712
1713                 if ((r->in.access_mask & SPECIFIC_RIGHTS_MASK)& ~(PRINTER_ACCESS_ADMINISTER|PRINTER_ACCESS_USE)) {
1714                         DEBUG(3, ("access DENIED for printer open - unknown bits\n"));
1715                         close_printer_handle(p, r->out.handle);
1716                         ZERO_STRUCTP(r->out.handle);
1717                         return WERR_ACCESS_DENIED;
1718                 }
1719
1720                 if (r->in.access_mask & PRINTER_ACCESS_ADMINISTER)
1721                         r->in.access_mask = PRINTER_ACCESS_ADMINISTER;
1722                 else
1723                         r->in.access_mask = PRINTER_ACCESS_USE;
1724
1725                 DEBUG(4,("Setting printer access = %s\n", (r->in.access_mask == PRINTER_ACCESS_ADMINISTER)
1726                         ? "PRINTER_ACCESS_ADMINISTER" : "PRINTER_ACCESS_USE" ));
1727
1728                 break;
1729
1730         default:
1731                 /* sanity check to prevent programmer error */
1732                 ZERO_STRUCTP(r->out.handle);
1733                 return WERR_BADFID;
1734         }
1735
1736         Printer->access_granted = r->in.access_mask;
1737
1738         /*
1739          * If the client sent a devmode in the OpenPrinter() call, then
1740          * save it here in case we get a job submission on this handle
1741          */
1742
1743          if ((Printer->printer_type != SPLHND_SERVER) &&
1744              r->in.devmode_ctr.devmode) {
1745                 convert_devicemode(Printer->sharename,
1746                                    r->in.devmode_ctr.devmode,
1747                                    &Printer->nt_devmode);
1748          }
1749
1750 #if 0   /* JERRY -- I'm doubtful this is really effective */
1751         /* HACK ALERT!!! Sleep for 1/3 of a second to try trigger a LAN/WAN
1752            optimization in Windows 2000 clients  --jerry */
1753
1754         if ( (r->in.access_mask == PRINTER_ACCESS_ADMINISTER)
1755                 && (RA_WIN2K == get_remote_arch()) )
1756         {
1757                 DEBUG(10,("_spoolss_OpenPrinterEx: Enabling LAN/WAN hack for Win2k clients.\n"));
1758                 sys_usleep( 500000 );
1759         }
1760 #endif
1761
1762         return WERR_OK;
1763 }
1764
1765 /****************************************************************************
1766 ****************************************************************************/
1767
1768 static bool printer_info2_to_nt_printer_info2(struct spoolss_SetPrinterInfo2 *r,
1769                                               NT_PRINTER_INFO_LEVEL_2 *d)
1770 {
1771         DEBUG(7,("printer_info2_to_nt_printer_info2\n"));
1772
1773         if (!r || !d) {
1774                 return false;
1775         }
1776
1777         d->attributes           = r->attributes;
1778         d->priority             = r->priority;
1779         d->default_priority     = r->defaultpriority;
1780         d->starttime            = r->starttime;
1781         d->untiltime            = r->untiltime;
1782         d->status               = r->status;
1783         d->cjobs                = r->cjobs;
1784
1785         fstrcpy(d->servername,  r->servername);
1786         fstrcpy(d->printername, r->printername);
1787         fstrcpy(d->sharename,   r->sharename);
1788         fstrcpy(d->portname,    r->portname);
1789         fstrcpy(d->drivername,  r->drivername);
1790         slprintf(d->comment, sizeof(d->comment)-1, "%s", r->comment);
1791         fstrcpy(d->location,    r->location);
1792         fstrcpy(d->sepfile,     r->sepfile);
1793         fstrcpy(d->printprocessor, r->printprocessor);
1794         fstrcpy(d->datatype,    r->datatype);
1795         fstrcpy(d->parameters,  r->parameters);
1796
1797         return true;
1798 }
1799
1800 /****************************************************************************
1801 ****************************************************************************/
1802
1803 static bool convert_printer_info(struct spoolss_SetPrinterInfoCtr *info_ctr,
1804                                  NT_PRINTER_INFO_LEVEL *printer)
1805 {
1806         bool ret;
1807
1808         switch (info_ctr->level) {
1809         case 2:
1810                 /* allocate memory if needed.  Messy because
1811                    convert_printer_info is used to update an existing
1812                    printer or build a new one */
1813
1814                 if (!printer->info_2) {
1815                         printer->info_2 = TALLOC_ZERO_P(printer, NT_PRINTER_INFO_LEVEL_2);
1816                         if (!printer->info_2) {
1817                                 DEBUG(0,("convert_printer_info: "
1818                                         "talloc() failed!\n"));
1819                                 return false;
1820                         }
1821                 }
1822
1823                 ret = printer_info2_to_nt_printer_info2(info_ctr->info.info2,
1824                                                         printer->info_2);
1825                 printer->info_2->setuptime = time(NULL);
1826                 return ret;
1827         }
1828
1829         return false;
1830 }
1831
1832 /*******************************************************************
1833 ********************************************************************/
1834
1835 static bool string_array_to_fstring_array(const char **sarray, fstring **farray)
1836 {
1837         int i;
1838
1839         if (!sarray) {
1840                 *farray = NULL;
1841                 return true;
1842         }
1843
1844         *farray = SMB_MALLOC_ARRAY(fstring, 1);
1845         if (!*farray) {
1846                 return false;
1847         }
1848
1849         for (i=0; sarray[i] != NULL; i++) {
1850                 *farray = SMB_REALLOC_ARRAY(*farray, fstring, i+2);
1851                 if (!*farray) {
1852                         return false;
1853                 }
1854                 fstrcpy((*farray)[i], sarray[i]);
1855         }
1856
1857         fstrcpy((*farray)[i], "");
1858
1859         return true;
1860 }
1861
1862 /*******************************************************************
1863 ********************************************************************/
1864
1865 static bool driver_info3_to_nt_driver_info3(struct spoolss_AddDriverInfo3 *r,
1866                                             NT_PRINTER_DRIVER_INFO_LEVEL_3 **p)
1867 {
1868         NT_PRINTER_DRIVER_INFO_LEVEL_3 *d;
1869
1870         DEBUG(7,("driver_info3_to_nt_driver_info3: Converting from UNICODE to ASCII\n"));
1871
1872         if (*p == NULL) {
1873                 *p = SMB_MALLOC_P(NT_PRINTER_DRIVER_INFO_LEVEL_3);
1874                 if (*p == NULL) {
1875                         return false;
1876                 }
1877                 ZERO_STRUCTP(*p);
1878         }
1879
1880         d = *p;
1881
1882         d->cversion =                   r->version;
1883
1884         fstrcpy(d->name,                r->driver_name);
1885         fstrcpy(d->environment,         r->architecture);
1886         fstrcpy(d->driverpath,          r->driver_path);
1887         fstrcpy(d->datafile,            r->data_file);
1888         fstrcpy(d->configfile,          r->config_file);
1889         fstrcpy(d->helpfile,            r->help_file);
1890         fstrcpy(d->monitorname,         r->monitor_name);
1891         fstrcpy(d->defaultdatatype,     r->default_datatype);
1892
1893         DEBUGADD(8,( "version:         %d\n", d->cversion));
1894         DEBUGADD(8,( "name:            %s\n", d->name));
1895         DEBUGADD(8,( "environment:     %s\n", d->environment));
1896         DEBUGADD(8,( "driverpath:      %s\n", d->driverpath));
1897         DEBUGADD(8,( "datafile:        %s\n", d->datafile));
1898         DEBUGADD(8,( "configfile:      %s\n", d->configfile));
1899         DEBUGADD(8,( "helpfile:        %s\n", d->helpfile));
1900         DEBUGADD(8,( "monitorname:     %s\n", d->monitorname));
1901         DEBUGADD(8,( "defaultdatatype: %s\n", d->defaultdatatype));
1902
1903         if (r->dependent_files) {
1904                 if (!string_array_to_fstring_array(r->dependent_files->string,
1905                                                    &d->dependentfiles)) {
1906                         SAFE_FREE(*p);
1907                         return false;
1908                 }
1909         }
1910
1911         return true;
1912 }
1913
1914 /*******************************************************************
1915 ********************************************************************/
1916
1917 static bool driver_info6_to_nt_driver_info6(struct spoolss_AddDriverInfo6 *r,
1918                                             NT_PRINTER_DRIVER_INFO_LEVEL_6 **p)
1919 {
1920         NT_PRINTER_DRIVER_INFO_LEVEL_6 *d;
1921
1922         DEBUG(7,("driver_info6_to_nt_driver_info6: Converting from UNICODE to ASCII\n"));
1923
1924         if (*p == NULL) {
1925                 *p = SMB_MALLOC_P(NT_PRINTER_DRIVER_INFO_LEVEL_6);
1926                 if (*p == NULL) {
1927                         return false;
1928                 }
1929                 ZERO_STRUCTP(*p);
1930         }
1931
1932         d = *p;
1933
1934         d->version =                    r->version;
1935
1936         fstrcpy(d->name,                r->driver_name);
1937         fstrcpy(d->environment,         r->architecture);
1938         fstrcpy(d->driverpath,          r->driver_path);
1939         fstrcpy(d->datafile,            r->data_file);
1940         fstrcpy(d->configfile,          r->config_file);
1941         fstrcpy(d->helpfile,            r->help_file);
1942         fstrcpy(d->monitorname,         r->monitor_name);
1943         fstrcpy(d->defaultdatatype,     r->default_datatype);
1944
1945         DEBUGADD(8,( "version:         %d\n", d->version));
1946         DEBUGADD(8,( "name:            %s\n", d->name));
1947         DEBUGADD(8,( "environment:     %s\n", d->environment));
1948         DEBUGADD(8,( "driverpath:      %s\n", d->driverpath));
1949         DEBUGADD(8,( "datafile:        %s\n", d->datafile));
1950         DEBUGADD(8,( "configfile:      %s\n", d->configfile));
1951         DEBUGADD(8,( "helpfile:        %s\n", d->helpfile));
1952         DEBUGADD(8,( "monitorname:     %s\n", d->monitorname));
1953         DEBUGADD(8,( "defaultdatatype: %s\n", d->defaultdatatype));
1954
1955         if (r->dependent_files) {
1956                 if (!string_array_to_fstring_array(r->dependent_files->string,
1957                                                    &d->dependentfiles)) {
1958                         goto error;
1959                 }
1960         }
1961
1962         if (r->previous_names) {
1963                 if (!string_array_to_fstring_array(r->previous_names->string,
1964                                                    &d->previousnames)) {
1965                         goto error;
1966                 }
1967         }
1968
1969         return true;
1970
1971  error:
1972         SAFE_FREE(*p);
1973         return false;
1974 }
1975
1976 /********************************************************************
1977  ********************************************************************/
1978
1979 static bool convert_printer_driver_info(const struct spoolss_AddDriverInfoCtr *r,
1980                                         NT_PRINTER_DRIVER_INFO_LEVEL *printer,
1981                                         uint32_t level)
1982 {
1983         switch (level) {
1984         case 3:
1985                 printer->info_3 = NULL;
1986                 if (!driver_info3_to_nt_driver_info3(r->info.info3, &printer->info_3)) {
1987                         return false;
1988                 }
1989                 break;
1990         case 6:
1991                 printer->info_6 = NULL;
1992                 if (!driver_info6_to_nt_driver_info6(r->info.info6, &printer->info_6)) {
1993                         return false;
1994                 }
1995                 break;
1996         default:
1997                 return false;
1998         }
1999
2000         return true;
2001 }
2002
2003 /********************************************************************
2004  * _spoolss_enddocprinter_internal.
2005  ********************************************************************/
2006
2007 static WERROR _spoolss_enddocprinter_internal(pipes_struct *p,
2008                                               struct policy_handle *handle)
2009 {
2010         Printer_entry *Printer = find_printer_index_by_hnd(p, handle);
2011         int snum;
2012
2013         if (!Printer) {
2014                 DEBUG(2,("_spoolss_enddocprinter_internal: Invalid handle (%s:%u:%u)\n", OUR_HANDLE(handle)));
2015                 return WERR_BADFID;
2016         }
2017
2018         if (!get_printer_snum(p, handle, &snum, NULL))
2019                 return WERR_BADFID;
2020
2021         Printer->document_started = false;
2022         print_job_end(snum, Printer->jobid,NORMAL_CLOSE);
2023         /* error codes unhandled so far ... */
2024
2025         return WERR_OK;
2026 }
2027
2028 /****************************************************************
2029  _spoolss_ClosePrinter
2030 ****************************************************************/
2031
2032 WERROR _spoolss_ClosePrinter(pipes_struct *p,
2033                              struct spoolss_ClosePrinter *r)
2034 {
2035         Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
2036
2037         if (Printer && Printer->document_started)
2038                 _spoolss_enddocprinter_internal(p, r->in.handle);          /* print job was not closed */
2039
2040         if (!close_printer_handle(p, r->in.handle))
2041                 return WERR_BADFID;
2042
2043         /* clear the returned printer handle.  Observed behavior
2044            from Win2k server.  Don't think this really matters.
2045            Previous code just copied the value of the closed
2046            handle.    --jerry */
2047
2048         ZERO_STRUCTP(r->out.handle);
2049
2050         return WERR_OK;
2051 }
2052
2053 /****************************************************************
2054  _spoolss_DeletePrinter
2055 ****************************************************************/
2056
2057 WERROR _spoolss_DeletePrinter(pipes_struct *p,
2058                               struct spoolss_DeletePrinter *r)
2059 {
2060         Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
2061         WERROR result;
2062
2063         if (Printer && Printer->document_started)
2064                 _spoolss_enddocprinter_internal(p, r->in.handle);  /* print job was not closed */
2065
2066         result = delete_printer_handle(p, r->in.handle);
2067
2068         update_c_setprinter(false);
2069
2070         return result;
2071 }
2072
2073 /*******************************************************************
2074  * static function to lookup the version id corresponding to an
2075  * long architecture string
2076  ******************************************************************/
2077
2078 static int get_version_id(const char *arch)
2079 {
2080         int i;
2081         struct table_node archi_table[]= {
2082
2083                 {"Windows 4.0",          "WIN40",       0 },
2084                 {"Windows NT x86",       "W32X86",      2 },
2085                 {"Windows NT R4000",     "W32MIPS",     2 },
2086                 {"Windows NT Alpha_AXP", "W32ALPHA",    2 },
2087                 {"Windows NT PowerPC",   "W32PPC",      2 },
2088                 {"Windows IA64",         "IA64",        3 },
2089                 {"Windows x64",          "x64",         3 },
2090                 {NULL,                   "",            -1 }
2091         };
2092
2093         for (i=0; archi_table[i].long_archi != NULL; i++)
2094         {
2095                 if (strcmp(arch, archi_table[i].long_archi) == 0)
2096                         return (archi_table[i].version);
2097         }
2098
2099         return -1;
2100 }
2101
2102 /****************************************************************
2103  _spoolss_DeletePrinterDriver
2104 ****************************************************************/
2105
2106 WERROR _spoolss_DeletePrinterDriver(pipes_struct *p,
2107                                     struct spoolss_DeletePrinterDriver *r)
2108 {
2109         NT_PRINTER_DRIVER_INFO_LEVEL    info;
2110         NT_PRINTER_DRIVER_INFO_LEVEL    info_win2k;
2111         int                             version;
2112         WERROR                          status;
2113         WERROR                          status_win2k = WERR_ACCESS_DENIED;
2114         SE_PRIV                         se_printop = SE_PRINT_OPERATOR;
2115
2116         /* if the user is not root, doesn't have SE_PRINT_OPERATOR privilege,
2117            and not a printer admin, then fail */
2118
2119         if ( (p->server_info->utok.uid != 0)
2120                 && !user_has_privileges(p->server_info->ptok, &se_printop )
2121                 && !token_contains_name_in_list(
2122                         uidtoname(p->server_info->utok.uid), NULL,
2123                         NULL, p->server_info->ptok,
2124                         lp_printer_admin(-1)) )
2125         {
2126                 return WERR_ACCESS_DENIED;
2127         }
2128
2129         /* check that we have a valid driver name first */
2130
2131         if ((version = get_version_id(r->in.architecture)) == -1)
2132                 return WERR_INVALID_ENVIRONMENT;
2133
2134         ZERO_STRUCT(info);
2135         ZERO_STRUCT(info_win2k);
2136
2137         if (!W_ERROR_IS_OK(get_a_printer_driver(&info, 3, r->in.driver,
2138                                                 r->in.architecture,
2139                                                 version)))
2140         {
2141                 /* try for Win2k driver if "Windows NT x86" */
2142
2143                 if ( version == 2 ) {
2144                         version = 3;
2145                         if (!W_ERROR_IS_OK(get_a_printer_driver(&info, 3,
2146                                                                 r->in.driver,
2147                                                                 r->in.architecture,
2148                                                                 version))) {
2149                                 status = WERR_UNKNOWN_PRINTER_DRIVER;
2150                                 goto done;
2151                         }
2152                 }
2153                 /* otherwise it was a failure */
2154                 else {
2155                         status = WERR_UNKNOWN_PRINTER_DRIVER;
2156                         goto done;
2157                 }
2158
2159         }
2160
2161         if (printer_driver_in_use(info.info_3)) {
2162                 status = WERR_PRINTER_DRIVER_IN_USE;
2163                 goto done;
2164         }
2165
2166         if ( version == 2 )
2167         {
2168                 if (W_ERROR_IS_OK(get_a_printer_driver(&info_win2k, 3,
2169                                                        r->in.driver,
2170                                                        r->in.architecture, 3)))
2171                 {
2172                         /* if we get to here, we now have 2 driver info structures to remove */
2173                         /* remove the Win2k driver first*/
2174
2175                         status_win2k = delete_printer_driver(
2176                                 p, info_win2k.info_3, 3, false);
2177                         free_a_printer_driver( info_win2k, 3 );
2178
2179                         /* this should not have failed---if it did, report to client */
2180                         if ( !W_ERROR_IS_OK(status_win2k) )
2181                         {
2182                                 status = status_win2k;
2183                                 goto done;
2184                         }
2185                 }
2186         }
2187
2188         status = delete_printer_driver(p, info.info_3, version, false);
2189
2190         /* if at least one of the deletes succeeded return OK */
2191
2192         if ( W_ERROR_IS_OK(status) || W_ERROR_IS_OK(status_win2k) )
2193                 status = WERR_OK;
2194
2195 done:
2196         free_a_printer_driver( info, 3 );
2197
2198         return status;
2199 }
2200
2201 /****************************************************************
2202  _spoolss_DeletePrinterDriverEx
2203 ****************************************************************/
2204
2205 WERROR _spoolss_DeletePrinterDriverEx(pipes_struct *p,
2206                                       struct spoolss_DeletePrinterDriverEx *r)
2207 {
2208         NT_PRINTER_DRIVER_INFO_LEVEL    info;
2209         NT_PRINTER_DRIVER_INFO_LEVEL    info_win2k;
2210         int                             version;
2211         bool                            delete_files;
2212         WERROR                          status;
2213         WERROR                          status_win2k = WERR_ACCESS_DENIED;
2214         SE_PRIV                         se_printop = SE_PRINT_OPERATOR;
2215
2216         /* if the user is not root, doesn't have SE_PRINT_OPERATOR privilege,
2217            and not a printer admin, then fail */
2218
2219         if ( (p->server_info->utok.uid != 0)
2220                 && !user_has_privileges(p->server_info->ptok, &se_printop )
2221                 && !token_contains_name_in_list(
2222                         uidtoname(p->server_info->utok.uid), NULL, NULL,
2223                         p->server_info->ptok, lp_printer_admin(-1)) )
2224         {
2225                 return WERR_ACCESS_DENIED;
2226         }
2227
2228         /* check that we have a valid driver name first */
2229         if ((version = get_version_id(r->in.architecture)) == -1) {
2230                 /* this is what NT returns */
2231                 return WERR_INVALID_ENVIRONMENT;
2232         }
2233
2234         if (r->in.delete_flags & DPD_DELETE_SPECIFIC_VERSION)
2235                 version = r->in.version;
2236
2237         ZERO_STRUCT(info);
2238         ZERO_STRUCT(info_win2k);
2239
2240         status = get_a_printer_driver(&info, 3, r->in.driver,
2241                                       r->in.architecture, version);
2242
2243         if ( !W_ERROR_IS_OK(status) )
2244         {
2245                 /*
2246                  * if the client asked for a specific version,
2247                  * or this is something other than Windows NT x86,
2248                  * then we've failed
2249                  */
2250
2251                 if ( (r->in.delete_flags & DPD_DELETE_SPECIFIC_VERSION) || (version !=2) )
2252                         goto done;
2253
2254                 /* try for Win2k driver if "Windows NT x86" */
2255
2256                 version = 3;
2257                 if (!W_ERROR_IS_OK(get_a_printer_driver(&info, 3, r->in.driver,
2258                                                         r->in.architecture,
2259                                                         version))) {
2260                         status = WERR_UNKNOWN_PRINTER_DRIVER;
2261                         goto done;
2262                 }
2263         }
2264
2265         if ( printer_driver_in_use(info.info_3) ) {
2266                 status = WERR_PRINTER_DRIVER_IN_USE;
2267                 goto done;
2268         }
2269
2270         /*
2271          * we have a couple of cases to consider.
2272          * (1) Are any files in use?  If so and DPD_DELTE_ALL_FILE is set,
2273          *     then the delete should fail if **any** files overlap with
2274          *     other drivers
2275          * (2) If DPD_DELTE_UNUSED_FILES is sert, then delete all
2276          *     non-overlapping files
2277          * (3) If neither DPD_DELTE_ALL_FILE nor DPD_DELTE_ALL_FILES
2278          *     is set, the do not delete any files
2279          * Refer to MSDN docs on DeletePrinterDriverEx() for details.
2280          */
2281
2282         delete_files = r->in.delete_flags & (DPD_DELETE_ALL_FILES|DPD_DELETE_UNUSED_FILES);
2283
2284         /* fail if any files are in use and DPD_DELETE_ALL_FILES is set */
2285
2286         if ( delete_files && printer_driver_files_in_use(info.info_3) & (r->in.delete_flags & DPD_DELETE_ALL_FILES) ) {
2287                 /* no idea of the correct error here */
2288                 status = WERR_ACCESS_DENIED;
2289                 goto done;
2290         }
2291
2292
2293         /* also check for W32X86/3 if necessary; maybe we already have? */
2294
2295         if ( (version == 2) && ((r->in.delete_flags & DPD_DELETE_SPECIFIC_VERSION) != DPD_DELETE_SPECIFIC_VERSION)  ) {
2296                 if (W_ERROR_IS_OK(get_a_printer_driver(&info_win2k, 3,
2297                                                        r->in.driver,
2298                                                        r->in.architecture, 3)))
2299                 {
2300
2301                         if ( delete_files && printer_driver_files_in_use(info_win2k.info_3) & (r->in.delete_flags & DPD_DELETE_ALL_FILES) ) {
2302                                 /* no idea of the correct error here */
2303                                 free_a_printer_driver( info_win2k, 3 );
2304                                 status = WERR_ACCESS_DENIED;
2305                                 goto done;
2306                         }
2307
2308                         /* if we get to here, we now have 2 driver info structures to remove */
2309                         /* remove the Win2k driver first*/
2310
2311                         status_win2k = delete_printer_driver(
2312                                 p, info_win2k.info_3, 3, delete_files);
2313                         free_a_printer_driver( info_win2k, 3 );
2314
2315                         /* this should not have failed---if it did, report to client */
2316
2317                         if ( !W_ERROR_IS_OK(status_win2k) )
2318                                 goto done;
2319                 }
2320         }
2321
2322         status = delete_printer_driver(p, info.info_3, version, delete_files);
2323
2324         if ( W_ERROR_IS_OK(status) || W_ERROR_IS_OK(status_win2k) )
2325                 status = WERR_OK;
2326 done:
2327         free_a_printer_driver( info, 3 );
2328
2329         return status;
2330 }
2331
2332
2333 /****************************************************************************
2334  Internal routine for removing printerdata
2335  ***************************************************************************/
2336
2337 static WERROR delete_printer_dataex( NT_PRINTER_INFO_LEVEL *printer, const char *key, const char *value )
2338 {
2339         return delete_printer_data( printer->info_2, key, value );
2340 }
2341
2342 /****************************************************************************
2343  Internal routine for storing printerdata
2344  ***************************************************************************/
2345
2346 WERROR set_printer_dataex(NT_PRINTER_INFO_LEVEL *printer,
2347                           const char *key, const char *value,
2348                           uint32_t type, uint8_t *data, int real_len)
2349 {
2350         /* the registry objects enforce uniqueness based on value name */
2351
2352         return add_printer_data( printer->info_2, key, value, type, data, real_len );
2353 }
2354
2355 /********************************************************************
2356  GetPrinterData on a printer server Handle.
2357 ********************************************************************/
2358
2359 static WERROR getprinterdata_printer_server(TALLOC_CTX *mem_ctx,
2360                                             const char *value,
2361                                             enum winreg_Type *type,
2362                                             union spoolss_PrinterData *data)
2363 {
2364         DEBUG(8,("getprinterdata_printer_server:%s\n", value));
2365
2366         if (!StrCaseCmp(value, "W3SvcInstalled")) {
2367                 *type = REG_DWORD;
2368                 data->value = 0x00;
2369                 return WERR_OK;
2370         }
2371
2372         if (!StrCaseCmp(value, "BeepEnabled")) {
2373                 *type = REG_DWORD;
2374                 data->value = 0x00;
2375                 return WERR_OK;
2376         }
2377
2378         if (!StrCaseCmp(value, "EventLog")) {
2379                 *type = REG_DWORD;
2380                 /* formally was 0x1b */
2381                 data->value = 0x00;
2382                 return WERR_OK;
2383         }
2384
2385         if (!StrCaseCmp(value, "NetPopup")) {
2386                 *type = REG_DWORD;
2387                 data->value = 0x00;
2388                 return WERR_OK;
2389         }
2390
2391         if (!StrCaseCmp(value, "MajorVersion")) {
2392                 *type = REG_DWORD;
2393
2394                 /* Windows NT 4.0 seems to not allow uploading of drivers
2395                    to a server that reports 0x3 as the MajorVersion.
2396                    need to investigate more how Win2k gets around this .
2397                    -- jerry */
2398
2399                 if (RA_WINNT == get_remote_arch()) {
2400                         data->value = 0x02;
2401                 } else {
2402                         data->value = 0x03;
2403                 }
2404
2405                 return WERR_OK;
2406         }
2407
2408         if (!StrCaseCmp(value, "MinorVersion")) {
2409                 *type = REG_DWORD;
2410                 data->value = 0x00;
2411                 return WERR_OK;
2412         }
2413
2414         /* REG_BINARY
2415          *  uint32_t size        = 0x114
2416          *  uint32_t major       = 5
2417          *  uint32_t minor       = [0|1]
2418          *  uint32_t build       = [2195|2600]
2419          *  extra unicode string = e.g. "Service Pack 3"
2420          */
2421         if (!StrCaseCmp(value, "OSVersion")) {
2422                 DATA_BLOB blob;
2423                 enum ndr_err_code ndr_err;
2424                 struct spoolss_OSVersion os;
2425
2426                 os.major                = 5;    /* Windows 2000 == 5.0 */
2427                 os.minor                = 0;
2428                 os.build                = 2195; /* build */
2429                 os.extra_string         = "";   /* leave extra string empty */
2430
2431                 ndr_err = ndr_push_struct_blob(&blob, mem_ctx, NULL, &os,
2432                         (ndr_push_flags_fn_t)ndr_push_spoolss_OSVersion);
2433                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2434                         return WERR_GENERAL_FAILURE;
2435                 }
2436
2437                 *type = REG_BINARY;
2438                 data->binary = blob;
2439
2440                 return WERR_OK;
2441         }
2442
2443
2444         if (!StrCaseCmp(value, "DefaultSpoolDirectory")) {
2445                 *type = REG_SZ;
2446
2447                 data->string = talloc_strdup(mem_ctx, "C:\\PRINTERS");
2448                 W_ERROR_HAVE_NO_MEMORY(data->string);
2449
2450                 return WERR_OK;
2451         }
2452
2453         if (!StrCaseCmp(value, "Architecture")) {
2454                 *type = REG_SZ;
2455
2456                 data->string = talloc_strdup(mem_ctx, "Windows NT x86");
2457                 W_ERROR_HAVE_NO_MEMORY(data->string);
2458
2459                 return WERR_OK;
2460         }
2461
2462         if (!StrCaseCmp(value, "DsPresent")) {
2463                 *type = REG_DWORD;
2464
2465                 /* only show the publish check box if we are a
2466                    member of a AD domain */
2467
2468                 if (lp_security() == SEC_ADS) {
2469                         data->value = 0x01;
2470                 } else {
2471                         data->value = 0x00;
2472                 }
2473                 return WERR_OK;
2474         }
2475
2476         if (!StrCaseCmp(value, "DNSMachineName")) {
2477                 const char *hostname = get_mydnsfullname();
2478
2479                 if (!hostname) {
2480                         return WERR_BADFILE;
2481                 }
2482
2483                 *type = REG_SZ;
2484                 data->string = talloc_strdup(mem_ctx, hostname);
2485                 W_ERROR_HAVE_NO_MEMORY(data->string);
2486
2487                 return WERR_OK;
2488         }
2489
2490         return WERR_INVALID_PARAM;
2491 }
2492
2493 /****************************************************************
2494  _spoolss_GetPrinterData
2495 ****************************************************************/
2496
2497 WERROR _spoolss_GetPrinterData(pipes_struct *p,
2498                                struct spoolss_GetPrinterData *r)
2499 {
2500         WERROR result;
2501         Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
2502         NT_PRINTER_INFO_LEVEL *printer = NULL;
2503         int snum = 0;
2504
2505         /*
2506          * Reminder: when it's a string, the length is in BYTES
2507          * even if UNICODE is negociated.
2508          *
2509          * JFM, 4/19/1999
2510          */
2511
2512         /* in case of problem, return some default values */
2513
2514         *r->out.needed  = 0;
2515         *r->out.type    = 0;
2516
2517         DEBUG(4,("_spoolss_GetPrinterData\n"));
2518
2519         if (!Printer) {
2520                 DEBUG(2,("_spoolss_GetPrinterData: Invalid handle (%s:%u:%u).\n",
2521                         OUR_HANDLE(r->in.handle)));
2522                 result = WERR_BADFID;
2523                 goto done;
2524         }
2525
2526         if (Printer->printer_type == SPLHND_SERVER) {
2527                 result = getprinterdata_printer_server(p->mem_ctx,
2528                                                        r->in.value_name,
2529                                                        r->out.type,
2530                                                        r->out.data);
2531         } else {
2532                 if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
2533                         result = WERR_BADFID;
2534                         goto done;
2535                 }
2536
2537                 result = get_a_printer(Printer, &printer, 2, lp_servicename(snum));
2538                 if (!W_ERROR_IS_OK(result)) {
2539                         goto done;
2540                 }
2541
2542                 /* XP sends this and wants to change id value from the PRINTER_INFO_0 */
2543
2544                 if (strequal(r->in.value_name, "ChangeId")) {
2545                         *r->out.type = REG_DWORD;
2546                         r->out.data->value = printer->info_2->changeid;
2547                         result = WERR_OK;
2548                 } else {
2549                         REGISTRY_VALUE *v;
2550                         DATA_BLOB blob;
2551
2552                         v = get_printer_data(printer->info_2,
2553                                              SPOOL_PRINTERDATA_KEY,
2554                                              r->in.value_name);
2555                         if (!v) {
2556                                 result = WERR_BADFILE;
2557                                 goto done;
2558                         }
2559
2560                         *r->out.type = v->type;
2561
2562                         blob = data_blob_const(v->data_p, v->size);
2563
2564                         result = pull_spoolss_PrinterData(p->mem_ctx, &blob,
2565                                                           r->out.data,
2566                                                           *r->out.type);
2567                 }
2568         }
2569
2570  done:
2571         /* cleanup & exit */
2572
2573         if (printer) {
2574                 free_a_printer(&printer, 2);
2575         }
2576
2577         if (!W_ERROR_IS_OK(result)) {
2578                 return result;
2579         }
2580
2581         *r->out.needed  = ndr_size_spoolss_PrinterData(r->out.data, *r->out.type, NULL, 0);
2582         *r->out.type    = SPOOLSS_BUFFER_OK(*r->out.type, REG_NONE);
2583         r->out.data     = SPOOLSS_BUFFER_OK(r->out.data, r->out.data);
2584
2585         return SPOOLSS_BUFFER_OK(WERR_OK, WERR_MORE_DATA);
2586 }
2587
2588 /*********************************************************
2589  Connect to the client machine.
2590 **********************************************************/
2591
2592 static bool spoolss_connect_to_client(struct rpc_pipe_client **pp_pipe,
2593                         struct sockaddr_storage *client_ss, const char *remote_machine)
2594 {
2595         NTSTATUS ret;
2596         struct cli_state *the_cli;
2597         struct sockaddr_storage rm_addr;
2598
2599         if ( is_zero_addr((struct sockaddr *)client_ss) ) {
2600                 if ( !resolve_name( remote_machine, &rm_addr, 0x20) ) {
2601                         DEBUG(2,("spoolss_connect_to_client: Can't resolve address for %s\n", remote_machine));
2602                         return false;
2603                 }
2604
2605                 if (ismyaddr((struct sockaddr *)&rm_addr)) {
2606                         DEBUG(0,("spoolss_connect_to_client: Machine %s is one of our addresses. Cannot add to ourselves.\n", remote_machine));
2607                         return false;
2608                 }
2609         } else {
2610                 char addr[INET6_ADDRSTRLEN];
2611                 rm_addr = *client_ss;
2612                 print_sockaddr(addr, sizeof(addr), &rm_addr);
2613                 DEBUG(5,("spoolss_connect_to_client: Using address %s (no name resolution necessary)\n",
2614                         addr));
2615         }
2616
2617         /* setup the connection */
2618
2619         ret = cli_full_connection( &the_cli, global_myname(), remote_machine,
2620                 &rm_addr, 0, "IPC$", "IPC",
2621                 "", /* username */
2622                 "", /* domain */
2623                 "", /* password */
2624                 0, lp_client_signing(), NULL );
2625
2626         if ( !NT_STATUS_IS_OK( ret ) ) {
2627                 DEBUG(2,("spoolss_connect_to_client: connection to [%s] failed!\n",
2628                         remote_machine ));
2629                 return false;
2630         }
2631
2632         if ( the_cli->protocol != PROTOCOL_NT1 ) {
2633                 DEBUG(0,("spoolss_connect_to_client: machine %s didn't negotiate NT protocol.\n", remote_machine));
2634                 cli_shutdown(the_cli);
2635                 return false;
2636         }
2637
2638         /*
2639          * Ok - we have an anonymous connection to the IPC$ share.
2640          * Now start the NT Domain stuff :-).
2641          */
2642
2643         ret = cli_rpc_pipe_open_noauth(the_cli, &ndr_table_spoolss.syntax_id, pp_pipe);
2644         if (!NT_STATUS_IS_OK(ret)) {
2645                 DEBUG(2,("spoolss_connect_to_client: unable to open the spoolss pipe on machine %s. Error was : %s.\n",
2646                         remote_machine, nt_errstr(ret)));
2647                 cli_shutdown(the_cli);
2648                 return false;
2649         }
2650
2651         return true;
2652 }
2653
2654 /***************************************************************************
2655  Connect to the client.
2656 ****************************************************************************/
2657
2658 static bool srv_spoolss_replyopenprinter(int snum, const char *printer,
2659                                         uint32_t localprinter, uint32_t type,
2660                                         struct policy_handle *handle,
2661                                         struct sockaddr_storage *client_ss)
2662 {
2663         WERROR result;
2664         NTSTATUS status;
2665
2666         /*
2667          * If it's the first connection, contact the client
2668          * and connect to the IPC$ share anonymously
2669          */
2670         if (smb_connections==0) {
2671                 fstring unix_printer;
2672
2673                 fstrcpy(unix_printer, printer+2); /* the +2 is to strip the leading 2 backslashs */
2674
2675                 if ( !spoolss_connect_to_client( &notify_cli_pipe, client_ss, unix_printer ))
2676                         return false;
2677
2678                 messaging_register(smbd_messaging_context(), NULL,
2679                                    MSG_PRINTER_NOTIFY2,
2680                                    receive_notify2_message_list);
2681                 /* Tell the connections db we're now interested in printer
2682                  * notify messages. */
2683                 register_message_flags(true, FLAG_MSG_PRINT_NOTIFY);
2684         }
2685
2686         /*
2687          * Tell the specific printing tdb we want messages for this printer
2688          * by registering our PID.
2689          */
2690
2691         if (!print_notify_register_pid(snum))
2692                 DEBUG(0,("print_notify_register_pid: Failed to register our pid for printer %s\n", printer ));
2693
2694         smb_connections++;
2695
2696         status = rpccli_spoolss_ReplyOpenPrinter(notify_cli_pipe, talloc_tos(),
2697                                                  printer,
2698                                                  localprinter,
2699                                                  type,
2700                                                  0,
2701                                                  NULL,
2702                                                  handle,
2703                                                  &result);
2704         if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result))
2705                 DEBUG(5,("srv_spoolss_reply_open_printer: Client RPC returned [%s]\n",
2706                         win_errstr(result)));
2707
2708         return (W_ERROR_IS_OK(result));
2709 }
2710
2711 /****************************************************************
2712  ****************************************************************/
2713
2714 static struct spoolss_NotifyOption *dup_spoolss_NotifyOption(TALLOC_CTX *mem_ctx,
2715                                                              const struct spoolss_NotifyOption *r)
2716 {
2717         struct spoolss_NotifyOption *option;
2718         uint32_t i,k;
2719
2720         if (!r) {
2721                 return NULL;
2722         }
2723
2724         option = talloc_zero(mem_ctx, struct spoolss_NotifyOption);
2725         if (!option) {
2726                 return NULL;
2727         }
2728
2729         *option = *r;
2730
2731         if (!option->count) {
2732                 return option;
2733         }
2734
2735         option->types = talloc_zero_array(option,
2736                 struct spoolss_NotifyOptionType, option->count);
2737         if (!option->types) {
2738                 talloc_free(option);
2739                 return NULL;
2740         }
2741
2742         for (i=0; i < option->count; i++) {
2743                 option->types[i] = r->types[i];
2744
2745                 if (option->types[i].count) {
2746                         option->types[i].fields = talloc_zero_array(option,
2747                                 union spoolss_Field, option->types[i].count);
2748                         if (!option->types[i].fields) {
2749                                 talloc_free(option);
2750                                 return NULL;
2751                         }
2752                         for (k=0; k<option->types[i].count; k++) {
2753                                 option->types[i].fields[k] =
2754                                         r->types[i].fields[k];
2755                         }
2756                 }
2757         }
2758
2759         return option;
2760 }
2761
2762 /****************************************************************
2763  * _spoolss_RemoteFindFirstPrinterChangeNotifyEx
2764  *
2765  * before replying OK: status=0 a rpc call is made to the workstation
2766  * asking ReplyOpenPrinter
2767  *
2768  * in fact ReplyOpenPrinter is the changenotify equivalent on the spoolss pipe
2769  * called from api_spoolss_rffpcnex
2770 ****************************************************************/
2771
2772 WERROR _spoolss_RemoteFindFirstPrinterChangeNotifyEx(pipes_struct *p,
2773                                                      struct spoolss_RemoteFindFirstPrinterChangeNotifyEx *r)
2774 {
2775         int snum = -1;
2776         struct spoolss_NotifyOption *option = r->in.notify_options;
2777         struct sockaddr_storage client_ss;
2778
2779         /* store the notify value in the printer struct */
2780
2781         Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
2782
2783         if (!Printer) {
2784                 DEBUG(2,("_spoolss_RemoteFindFirstPrinterChangeNotifyEx: "
2785                         "Invalid handle (%s:%u:%u).\n",
2786                         OUR_HANDLE(r->in.handle)));
2787                 return WERR_BADFID;
2788         }
2789
2790         Printer->notify.flags           = r->in.flags;
2791         Printer->notify.options         = r->in.options;
2792         Printer->notify.printerlocal    = r->in.printer_local;
2793
2794         TALLOC_FREE(Printer->notify.option);
2795         Printer->notify.option = dup_spoolss_NotifyOption(Printer, option);
2796
2797         fstrcpy(Printer->notify.localmachine, r->in.local_machine);
2798
2799         /* Connect to the client machine and send a ReplyOpenPrinter */
2800
2801         if ( Printer->printer_type == SPLHND_SERVER)
2802                 snum = -1;
2803         else if ( (Printer->printer_type == SPLHND_PRINTER) &&
2804                         !get_printer_snum(p, r->in.handle, &snum, NULL) )
2805                 return WERR_BADFID;
2806
2807         if (!interpret_string_addr(&client_ss, p->client_address,
2808                                    AI_NUMERICHOST)) {
2809                 return WERR_SERVER_UNAVAILABLE;
2810         }
2811
2812         if(!srv_spoolss_replyopenprinter(snum, Printer->notify.localmachine,
2813                                         Printer->notify.printerlocal, 1,
2814                                         &Printer->notify.client_hnd, &client_ss))
2815                 return WERR_SERVER_UNAVAILABLE;
2816
2817         Printer->notify.client_connected = true;
2818
2819         return WERR_OK;
2820 }
2821
2822 /*******************************************************************
2823  * fill a notify_info_data with the servername
2824  ********************************************************************/
2825
2826 void spoolss_notify_server_name(int snum,
2827                                        struct spoolss_Notify *data,
2828                                        print_queue_struct *queue,
2829                                        NT_PRINTER_INFO_LEVEL *printer,
2830                                        TALLOC_CTX *mem_ctx)
2831 {
2832         SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, printer->info_2->servername);
2833 }
2834
2835 /*******************************************************************
2836  * fill a notify_info_data with the printername (not including the servername).
2837  ********************************************************************/
2838
2839 void spoolss_notify_printer_name(int snum,
2840                                         struct spoolss_Notify *data,
2841                                         print_queue_struct *queue,
2842                                         NT_PRINTER_INFO_LEVEL *printer,
2843                                         TALLOC_CTX *mem_ctx)
2844 {
2845         /* the notify name should not contain the \\server\ part */
2846         char *p = strrchr(printer->info_2->printername, '\\');
2847
2848         if (!p) {
2849                 p = printer->info_2->printername;
2850         } else {
2851                 p++;
2852         }
2853
2854         SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, p);
2855 }
2856
2857 /*******************************************************************
2858  * fill a notify_info_data with the servicename
2859  ********************************************************************/
2860
2861 void spoolss_notify_share_name(int snum,
2862                                       struct spoolss_Notify *data,
2863                                       print_queue_struct *queue,
2864                                       NT_PRINTER_INFO_LEVEL *printer,
2865                                       TALLOC_CTX *mem_ctx)
2866 {
2867         SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, lp_servicename(snum));
2868 }
2869
2870 /*******************************************************************
2871  * fill a notify_info_data with the port name
2872  ********************************************************************/
2873
2874 void spoolss_notify_port_name(int snum,
2875                                      struct spoolss_Notify *data,
2876                                      print_queue_struct *queue,
2877                                      NT_PRINTER_INFO_LEVEL *printer,
2878                                      TALLOC_CTX *mem_ctx)
2879 {
2880         SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, printer->info_2->portname);
2881 }
2882
2883 /*******************************************************************
2884  * fill a notify_info_data with the printername
2885  * but it doesn't exist, have to see what to do
2886  ********************************************************************/
2887
2888 void spoolss_notify_driver_name(int snum,
2889                                        struct spoolss_Notify *data,
2890                                        print_queue_struct *queue,
2891                                        NT_PRINTER_INFO_LEVEL *printer,
2892                                        TALLOC_CTX *mem_ctx)
2893 {
2894         SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, printer->info_2->drivername);
2895 }
2896
2897 /*******************************************************************
2898  * fill a notify_info_data with the comment
2899  ********************************************************************/
2900
2901 void spoolss_notify_comment(int snum,
2902                                    struct spoolss_Notify *data,
2903                                    print_queue_struct *queue,
2904                                    NT_PRINTER_INFO_LEVEL *printer,
2905                                    TALLOC_CTX *mem_ctx)
2906 {
2907         char *p;
2908
2909         if (*printer->info_2->comment == '\0') {
2910                 p = lp_comment(snum);
2911         } else {
2912                 p = printer->info_2->comment;
2913         }
2914
2915         SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, printer->info_2->comment);
2916 }
2917
2918 /*******************************************************************
2919  * fill a notify_info_data with the comment
2920  * location = "Room 1, floor 2, building 3"
2921  ********************************************************************/
2922
2923 void spoolss_notify_location(int snum,
2924                                     struct spoolss_Notify *data,
2925                                     print_queue_struct *queue,
2926                                     NT_PRINTER_INFO_LEVEL *printer,
2927                                     TALLOC_CTX *mem_ctx)
2928 {
2929         SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, printer->info_2->location);
2930 }
2931
2932 /*******************************************************************
2933  * fill a notify_info_data with the device mode
2934  * jfm:xxxx don't to it for know but that's a real problem !!!
2935  ********************************************************************/
2936
2937 static void spoolss_notify_devmode(int snum,
2938                                    struct spoolss_Notify *data,
2939                                    print_queue_struct *queue,
2940                                    NT_PRINTER_INFO_LEVEL *printer,
2941                                    TALLOC_CTX *mem_ctx)
2942 {
2943         /* for a dummy implementation we have to zero the fields */
2944         SETUP_SPOOLSS_NOTIFY_DATA_DEVMODE(data, NULL);
2945 }
2946
2947 /*******************************************************************
2948  * fill a notify_info_data with the separator file name
2949  ********************************************************************/
2950
2951 void spoolss_notify_sepfile(int snum,
2952                                    struct spoolss_Notify *data,
2953                                    print_queue_struct *queue,
2954                                    NT_PRINTER_INFO_LEVEL *printer,
2955                                    TALLOC_CTX *mem_ctx)
2956 {
2957         SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, printer->info_2->sepfile);
2958 }
2959
2960 /*******************************************************************
2961  * fill a notify_info_data with the print processor
2962  * jfm:xxxx return always winprint to indicate we don't do anything to it
2963  ********************************************************************/
2964
2965 void spoolss_notify_print_processor(int snum,
2966                                            struct spoolss_Notify *data,
2967                                            print_queue_struct *queue,
2968                                            NT_PRINTER_INFO_LEVEL *printer,
2969                                            TALLOC_CTX *mem_ctx)
2970 {
2971         SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, printer->info_2->printprocessor);
2972 }
2973
2974 /*******************************************************************
2975  * fill a notify_info_data with the print processor options
2976  * jfm:xxxx send an empty string
2977  ********************************************************************/
2978
2979 void spoolss_notify_parameters(int snum,
2980                                       struct spoolss_Notify *data,
2981                                       print_queue_struct *queue,
2982                                       NT_PRINTER_INFO_LEVEL *printer,
2983                                       TALLOC_CTX *mem_ctx)
2984 {
2985         SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, printer->info_2->parameters);
2986 }
2987
2988 /*******************************************************************
2989  * fill a notify_info_data with the data type
2990  * jfm:xxxx always send RAW as data type
2991  ********************************************************************/
2992
2993 void spoolss_notify_datatype(int snum,
2994                                     struct spoolss_Notify *data,
2995                                     print_queue_struct *queue,
2996                                     NT_PRINTER_INFO_LEVEL *printer,
2997                                     TALLOC_CTX *mem_ctx)
2998 {
2999         SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, printer->info_2->datatype);
3000 }
3001
3002 /*******************************************************************
3003  * fill a notify_info_data with the security descriptor
3004  * jfm:xxxx send an null pointer to say no security desc
3005  * have to implement security before !
3006  ********************************************************************/
3007
3008 static void spoolss_notify_security_desc(int snum,
3009                                          struct spoolss_Notify *data,
3010                                          print_queue_struct *queue,
3011                                          NT_PRINTER_INFO_LEVEL *printer,
3012                                          TALLOC_CTX *mem_ctx)
3013 {
3014         SETUP_SPOOLSS_NOTIFY_DATA_SECDESC(data,
3015                                           printer->info_2->secdesc_buf->sd_size,
3016                                           printer->info_2->secdesc_buf->sd);
3017 }
3018
3019 /*******************************************************************
3020  * fill a notify_info_data with the attributes
3021  * jfm:xxxx a samba printer is always shared
3022  ********************************************************************/
3023
3024 void spoolss_notify_attributes(int snum,
3025                                       struct spoolss_Notify *data,
3026                                       print_queue_struct *queue,
3027                                       NT_PRINTER_INFO_LEVEL *printer,
3028                                       TALLOC_CTX *mem_ctx)
3029 {
3030         SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, printer->info_2->attributes);
3031 }
3032
3033 /*******************************************************************
3034  * fill a notify_info_data with the priority
3035  ********************************************************************/
3036
3037 static void spoolss_notify_priority(int snum,
3038                                     struct spoolss_Notify *data,
3039                                     print_queue_struct *queue,
3040                                     NT_PRINTER_INFO_LEVEL *printer,
3041                                     TALLOC_CTX *mem_ctx)
3042 {
3043         SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, printer->info_2->priority);
3044 }
3045
3046 /*******************************************************************
3047  * fill a notify_info_data with the default priority
3048  ********************************************************************/
3049
3050 static void spoolss_notify_default_priority(int snum,
3051                                             struct spoolss_Notify *data,
3052                                             print_queue_struct *queue,
3053                                             NT_PRINTER_INFO_LEVEL *printer,
3054                                             TALLOC_CTX *mem_ctx)
3055 {
3056         SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, printer->info_2->default_priority);
3057 }
3058
3059 /*******************************************************************
3060  * fill a notify_info_data with the start time
3061  ********************************************************************/
3062
3063 static void spoolss_notify_start_time(int snum,
3064                                       struct spoolss_Notify *data,
3065                                       print_queue_struct *queue,
3066                                       NT_PRINTER_INFO_LEVEL *printer,
3067                                       TALLOC_CTX *mem_ctx)
3068 {
3069         SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, printer->info_2->starttime);
3070 }
3071
3072 /*******************************************************************
3073  * fill a notify_info_data with the until time
3074  ********************************************************************/
3075
3076 static void spoolss_notify_until_time(int snum,
3077                                       struct spoolss_Notify *data,
3078                                       print_queue_struct *queue,
3079                                       NT_PRINTER_INFO_LEVEL *printer,
3080                                       TALLOC_CTX *mem_ctx)
3081 {
3082         SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, printer->info_2->untiltime);
3083 }
3084
3085 /*******************************************************************
3086  * fill a notify_info_data with the status
3087  ********************************************************************/
3088
3089 static void spoolss_notify_status(int snum,
3090                                   struct spoolss_Notify *data,
3091                                   print_queue_struct *queue,
3092                                   NT_PRINTER_INFO_LEVEL *printer,
3093                                   TALLOC_CTX *mem_ctx)
3094 {
3095         print_status_struct status;
3096
3097         print_queue_length(snum, &status);
3098         SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, status.status);
3099 }
3100
3101 /*******************************************************************
3102  * fill a notify_info_data with the number of jobs queued
3103  ********************************************************************/
3104
3105 void spoolss_notify_cjobs(int snum,
3106                                  struct spoolss_Notify *data,
3107                                  print_queue_struct *queue,
3108                                  NT_PRINTER_INFO_LEVEL *printer,
3109                                  TALLOC_CTX *mem_ctx)
3110 {
3111         SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, print_queue_length(snum, NULL));
3112 }
3113
3114 /*******************************************************************
3115  * fill a notify_info_data with the average ppm
3116  ********************************************************************/
3117
3118 static void spoolss_notify_average_ppm(int snum,
3119                                        struct spoolss_Notify *data,
3120                                        print_queue_struct *queue,
3121                                        NT_PRINTER_INFO_LEVEL *printer,
3122                                        TALLOC_CTX *mem_ctx)
3123 {
3124         /* always respond 8 pages per minutes */
3125         /* a little hard ! */
3126         SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, printer->info_2->averageppm);
3127 }
3128
3129 /*******************************************************************
3130  * fill a notify_info_data with username
3131  ********************************************************************/
3132
3133 static void spoolss_notify_username(int snum,
3134                                     struct spoolss_Notify *data,
3135                                     print_queue_struct *queue,
3136                                     NT_PRINTER_INFO_LEVEL *printer,
3137                                     TALLOC_CTX *mem_ctx)
3138 {
3139         SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, queue->fs_user);
3140 }
3141
3142 /*******************************************************************
3143  * fill a notify_info_data with job status
3144  ********************************************************************/
3145
3146 static void spoolss_notify_job_status(int snum,
3147                                       struct spoolss_Notify *data,
3148                                       print_queue_struct *queue,
3149                                       NT_PRINTER_INFO_LEVEL *printer,
3150                                       TALLOC_CTX *mem_ctx)
3151 {
3152         SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, nt_printj_status(queue->status));
3153 }
3154
3155 /*******************************************************************
3156  * fill a notify_info_data with job name
3157  ********************************************************************/
3158
3159 static void spoolss_notify_job_name(int snum,
3160                                     struct spoolss_Notify *data,
3161                                     print_queue_struct *queue,
3162                                     NT_PRINTER_INFO_LEVEL *printer,
3163                                     TALLOC_CTX *mem_ctx)
3164 {
3165         SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, queue->fs_file);
3166 }
3167
3168 /*******************************************************************
3169  * fill a notify_info_data with job status
3170  ********************************************************************/
3171
3172 static void spoolss_notify_job_status_string(int snum,
3173                                              struct spoolss_Notify *data,
3174                                              print_queue_struct *queue,
3175                                              NT_PRINTER_INFO_LEVEL *printer,
3176                                              TALLOC_CTX *mem_ctx)
3177 {
3178         /*
3179          * Now we're returning job status codes we just return a "" here. JRA.
3180          */
3181
3182         const char *p = "";
3183
3184 #if 0 /* NO LONGER NEEDED - JRA. 02/22/2001 */
3185         p = "unknown";
3186
3187         switch (queue->status) {
3188         case LPQ_QUEUED:
3189                 p = "Queued";
3190                 break;
3191         case LPQ_PAUSED:
3192                 p = "";    /* NT provides the paused string */
3193                 break;
3194         case LPQ_SPOOLING:
3195                 p = "Spooling";
3196                 break;
3197         case LPQ_PRINTING:
3198                 p = "Printing";
3199                 break;
3200         }
3201 #endif /* NO LONGER NEEDED. */
3202
3203         SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, p);
3204 }
3205
3206 /*******************************************************************
3207  * fill a notify_info_data with job time
3208  ********************************************************************/
3209
3210 static void spoolss_notify_job_time(int snum,
3211                                     struct spoolss_Notify *data,
3212                                     print_queue_struct *queue,
3213                                     NT_PRINTER_INFO_LEVEL *printer,
3214                                     TALLOC_CTX *mem_ctx)
3215 {
3216         SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, 0);
3217 }
3218
3219 /*******************************************************************
3220  * fill a notify_info_data with job size
3221  ********************************************************************/
3222
3223 static void spoolss_notify_job_size(int snum,
3224                                     struct spoolss_Notify *data,
3225                                     print_queue_struct *queue,
3226                                     NT_PRINTER_INFO_LEVEL *printer,
3227                                     TALLOC_CTX *mem_ctx)
3228 {
3229         SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, queue->size);
3230 }
3231
3232 /*******************************************************************
3233  * fill a notify_info_data with page info
3234  ********************************************************************/
3235 static void spoolss_notify_total_pages(int snum,
3236                                 struct spoolss_Notify *data,
3237                                 print_queue_struct *queue,
3238                                 NT_PRINTER_INFO_LEVEL *printer,
3239                                 TALLOC_CTX *mem_ctx)
3240 {
3241         SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, queue->page_count);
3242 }
3243
3244 /*******************************************************************
3245  * fill a notify_info_data with pages printed info.
3246  ********************************************************************/
3247 static void spoolss_notify_pages_printed(int snum,
3248                                 struct spoolss_Notify *data,
3249                                 print_queue_struct *queue,
3250                                 NT_PRINTER_INFO_LEVEL *printer,
3251                                 TALLOC_CTX *mem_ctx)
3252 {
3253         /* Add code when back-end tracks this */
3254         SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, 0);
3255 }
3256
3257 /*******************************************************************
3258  Fill a notify_info_data with job position.
3259  ********************************************************************/
3260
3261 static void spoolss_notify_job_position(int snum,
3262                                         struct spoolss_Notify *data,
3263                                         print_queue_struct *queue,
3264                                         NT_PRINTER_INFO_LEVEL *printer,
3265                                         TALLOC_CTX *mem_ctx)
3266 {
3267         SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, queue->job);
3268 }
3269
3270 /*******************************************************************
3271  Fill a notify_info_data with submitted time.
3272  ********************************************************************/
3273
3274 static void spoolss_notify_submitted_time(int snum,
3275                                           struct spoolss_Notify *data,
3276                                           print_queue_struct *queue,
3277                                           NT_PRINTER_INFO_LEVEL *printer,
3278                                           TALLOC_CTX *mem_ctx)
3279 {
3280         data->data.string.string = NULL;
3281         data->data.string.size = 0;
3282
3283         init_systemtime_buffer(mem_ctx, gmtime(&queue->time),
3284                                &data->data.string.string,
3285                                &data->data.string.size);
3286
3287 }
3288
3289 struct s_notify_info_data_table
3290 {
3291         enum spoolss_NotifyType type;
3292         uint16_t field;
3293         const char *name;
3294         enum spoolss_NotifyTable variable_type;
3295         void (*fn) (int snum, struct spoolss_Notify *data,
3296                     print_queue_struct *queue,
3297                     NT_PRINTER_INFO_LEVEL *printer, TALLOC_CTX *mem_ctx);
3298 };
3299
3300 /* A table describing the various print notification constants and
3301    whether the notification data is a pointer to a variable sized
3302    buffer, a one value uint32_t or a two value uint32_t. */
3303
3304 static const struct s_notify_info_data_table notify_info_data_table[] =
3305 {
3306 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_SERVER_NAME,         "PRINTER_NOTIFY_FIELD_SERVER_NAME",         NOTIFY_TABLE_STRING,   spoolss_notify_server_name },
3307 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_PRINTER_NAME,        "PRINTER_NOTIFY_FIELD_PRINTER_NAME",        NOTIFY_TABLE_STRING,   spoolss_notify_printer_name },
3308 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_SHARE_NAME,          "PRINTER_NOTIFY_FIELD_SHARE_NAME",          NOTIFY_TABLE_STRING,   spoolss_notify_share_name },
3309 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_PORT_NAME,           "PRINTER_NOTIFY_FIELD_PORT_NAME",           NOTIFY_TABLE_STRING,   spoolss_notify_port_name },
3310 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_DRIVER_NAME,         "PRINTER_NOTIFY_FIELD_DRIVER_NAME",         NOTIFY_TABLE_STRING,   spoolss_notify_driver_name },
3311 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_COMMENT,             "PRINTER_NOTIFY_FIELD_COMMENT",             NOTIFY_TABLE_STRING,   spoolss_notify_comment },
3312 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_LOCATION,            "PRINTER_NOTIFY_FIELD_LOCATION",            NOTIFY_TABLE_STRING,   spoolss_notify_location },
3313 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_DEVMODE,             "PRINTER_NOTIFY_FIELD_DEVMODE",             NOTIFY_TABLE_DEVMODE,  spoolss_notify_devmode },
3314 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_SEPFILE,             "PRINTER_NOTIFY_FIELD_SEPFILE",             NOTIFY_TABLE_STRING,   spoolss_notify_sepfile },
3315 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_PRINT_PROCESSOR,     "PRINTER_NOTIFY_FIELD_PRINT_PROCESSOR",     NOTIFY_TABLE_STRING,   spoolss_notify_print_processor },
3316 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_PARAMETERS,          "PRINTER_NOTIFY_FIELD_PARAMETERS",          NOTIFY_TABLE_STRING,   spoolss_notify_parameters },
3317 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_DATATYPE,            "PRINTER_NOTIFY_FIELD_DATATYPE",            NOTIFY_TABLE_STRING,   spoolss_notify_datatype },
3318 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_SECURITY_DESCRIPTOR, "PRINTER_NOTIFY_FIELD_SECURITY_DESCRIPTOR", NOTIFY_TABLE_SECURITYDESCRIPTOR,   spoolss_notify_security_desc },
3319 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_ATTRIBUTES,          "PRINTER_NOTIFY_FIELD_ATTRIBUTES",          NOTIFY_TABLE_DWORD,    spoolss_notify_attributes },
3320 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_PRIORITY,            "PRINTER_NOTIFY_FIELD_PRIORITY",            NOTIFY_TABLE_DWORD,    spoolss_notify_priority },
3321 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_DEFAULT_PRIORITY,    "PRINTER_NOTIFY_FIELD_DEFAULT_PRIORITY",    NOTIFY_TABLE_DWORD,    spoolss_notify_default_priority },
3322 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_START_TIME,          "PRINTER_NOTIFY_FIELD_START_TIME",          NOTIFY_TABLE_DWORD,    spoolss_notify_start_time },
3323 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_UNTIL_TIME,          "PRINTER_NOTIFY_FIELD_UNTIL_TIME",          NOTIFY_TABLE_DWORD,    spoolss_notify_until_time },
3324 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_STATUS,              "PRINTER_NOTIFY_FIELD_STATUS",              NOTIFY_TABLE_DWORD,    spoolss_notify_status },
3325 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_STATUS_STRING,       "PRINTER_NOTIFY_FIELD_STATUS_STRING",       NOTIFY_TABLE_STRING,   NULL },
3326 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_CJOBS,               "PRINTER_NOTIFY_FIELD_CJOBS",               NOTIFY_TABLE_DWORD,    spoolss_notify_cjobs },
3327 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_AVERAGE_PPM,         "PRINTER_NOTIFY_FIELD_AVERAGE_PPM",         NOTIFY_TABLE_DWORD,    spoolss_notify_average_ppm },
3328 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_TOTAL_PAGES,         "PRINTER_NOTIFY_FIELD_TOTAL_PAGES",         NOTIFY_TABLE_DWORD,    NULL },
3329 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_PAGES_PRINTED,       "PRINTER_NOTIFY_FIELD_PAGES_PRINTED",       NOTIFY_TABLE_DWORD,    NULL },
3330 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_TOTAL_BYTES,         "PRINTER_NOTIFY_FIELD_TOTAL_BYTES",         NOTIFY_TABLE_DWORD,    NULL },
3331 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_BYTES_PRINTED,       "PRINTER_NOTIFY_FIELD_BYTES_PRINTED",       NOTIFY_TABLE_DWORD,    NULL },
3332 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_PRINTER_NAME,            "JOB_NOTIFY_FIELD_PRINTER_NAME",            NOTIFY_TABLE_STRING,   spoolss_notify_printer_name },
3333 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_MACHINE_NAME,            "JOB_NOTIFY_FIELD_MACHINE_NAME",            NOTIFY_TABLE_STRING,   spoolss_notify_server_name },
3334 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_PORT_NAME,               "JOB_NOTIFY_FIELD_PORT_NAME",               NOTIFY_TABLE_STRING,   spoolss_notify_port_name },
3335 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_USER_NAME,               "JOB_NOTIFY_FIELD_USER_NAME",               NOTIFY_TABLE_STRING,   spoolss_notify_username },
3336 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_NOTIFY_NAME,             "JOB_NOTIFY_FIELD_NOTIFY_NAME",             NOTIFY_TABLE_STRING,   spoolss_notify_username },
3337 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_DATATYPE,                "JOB_NOTIFY_FIELD_DATATYPE",                NOTIFY_TABLE_STRING,   spoolss_notify_datatype },
3338 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_PRINT_PROCESSOR,         "JOB_NOTIFY_FIELD_PRINT_PROCESSOR",         NOTIFY_TABLE_STRING,   spoolss_notify_print_processor },
3339 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_PARAMETERS,              "JOB_NOTIFY_FIELD_PARAMETERS",              NOTIFY_TABLE_STRING,   spoolss_notify_parameters },
3340 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_DRIVER_NAME,             "JOB_NOTIFY_FIELD_DRIVER_NAME",             NOTIFY_TABLE_STRING,   spoolss_notify_driver_name },
3341 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_DEVMODE,                 "JOB_NOTIFY_FIELD_DEVMODE",                 NOTIFY_TABLE_DEVMODE,  spoolss_notify_devmode },
3342 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_STATUS,                  "JOB_NOTIFY_FIELD_STATUS",                  NOTIFY_TABLE_DWORD,    spoolss_notify_job_status },
3343 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_STATUS_STRING,           "JOB_NOTIFY_FIELD_STATUS_STRING",           NOTIFY_TABLE_STRING,   spoolss_notify_job_status_string },
3344 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_SECURITY_DESCRIPTOR,     "JOB_NOTIFY_FIELD_SECURITY_DESCRIPTOR",     NOTIFY_TABLE_SECURITYDESCRIPTOR,   NULL },
3345 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_DOCUMENT,                "JOB_NOTIFY_FIELD_DOCUMENT",                NOTIFY_TABLE_STRING,   spoolss_notify_job_name },
3346 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_PRIORITY,                "JOB_NOTIFY_FIELD_PRIORITY",                NOTIFY_TABLE_DWORD,    spoolss_notify_priority },
3347 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_POSITION,                "JOB_NOTIFY_FIELD_POSITION",                NOTIFY_TABLE_DWORD,    spoolss_notify_job_position },
3348 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_SUBMITTED,               "JOB_NOTIFY_FIELD_SUBMITTED",               NOTIFY_TABLE_TIME,     spoolss_notify_submitted_time },
3349 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_START_TIME,              "JOB_NOTIFY_FIELD_START_TIME",              NOTIFY_TABLE_DWORD,    spoolss_notify_start_time },
3350 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_UNTIL_TIME,              "JOB_NOTIFY_FIELD_UNTIL_TIME",              NOTIFY_TABLE_DWORD,    spoolss_notify_until_time },
3351 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_TIME,                    "JOB_NOTIFY_FIELD_TIME",                    NOTIFY_TABLE_DWORD,    spoolss_notify_job_time },
3352 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_TOTAL_PAGES,             "JOB_NOTIFY_FIELD_TOTAL_PAGES",             NOTIFY_TABLE_DWORD,    spoolss_notify_total_pages },
3353 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_PAGES_PRINTED,           "JOB_NOTIFY_FIELD_PAGES_PRINTED",           NOTIFY_TABLE_DWORD,    spoolss_notify_pages_printed },
3354 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_FIELD_TOTAL_BYTES,             "JOB_NOTIFY_FIELD_TOTAL_BYTES",             NOTIFY_TABLE_DWORD,    spoolss_notify_job_size },
3355 };
3356
3357 /*******************************************************************
3358  Return the variable_type of info_data structure.
3359 ********************************************************************/
3360
3361 static uint32_t variable_type_of_notify_info_data(enum spoolss_NotifyType type,
3362                                                   uint16_t field)
3363 {
3364         int i=0;
3365
3366         for (i = 0; i < ARRAY_SIZE(notify_info_data_table); i++) {
3367                 if ( (notify_info_data_table[i].type == type) &&
3368                      (notify_info_data_table[i].field == field) ) {
3369                         return notify_info_data_table[i].variable_type;
3370                 }
3371         }
3372
3373         DEBUG(5, ("invalid notify data type %d/%d\n", type, field));
3374
3375         return 0;
3376 }
3377
3378 /****************************************************************************
3379 ****************************************************************************/
3380
3381 static bool search_notify(enum spoolss_NotifyType type,
3382                           uint16_t field,
3383                           int *value)
3384 {
3385         int i;
3386
3387         for (i = 0; i < ARRAY_SIZE(notify_info_data_table); i++) {
3388                 if (notify_info_data_table[i].type == type &&
3389                     notify_info_data_table[i].field == field &&
3390                     notify_info_data_table[i].fn != NULL) {
3391                         *value = i;
3392                         return true;
3393                 }
3394         }
3395
3396         return false;
3397 }
3398
3399 /****************************************************************************
3400 ****************************************************************************/
3401
3402 void construct_info_data(struct spoolss_Notify *info_data,
3403                          enum spoolss_NotifyType type,
3404                          uint16_t field,
3405                          int id)
3406 {
3407         info_data->type                 = type;
3408         info_data->field.field          = field;
3409         info_data->variable_type        = variable_type_of_notify_info_data(type, field);
3410         info_data->job_id               = id;
3411 }
3412
3413 /*******************************************************************
3414  *
3415  * fill a notify_info struct with info asked
3416  *
3417  ********************************************************************/
3418
3419 static bool construct_notify_printer_info(Printer_entry *print_hnd,
3420                                           struct spoolss_NotifyInfo *info,
3421                                           int snum,
3422                                           const struct spoolss_NotifyOptionType *option_type,
3423                                           uint32_t id,
3424                                           TALLOC_CTX *mem_ctx)
3425 {
3426         int field_num,j;
3427         enum spoolss_NotifyType type;
3428         uint16_t field;
3429
3430         struct spoolss_Notify *current_data;
3431         NT_PRINTER_INFO_LEVEL *printer = NULL;
3432         print_queue_struct *queue=NULL;
3433
3434         type = option_type->type;
3435
3436         DEBUG(4,("construct_notify_printer_info: Notify type: [%s], number of notify info: [%d] on printer: [%s]\n",
3437                 (type == PRINTER_NOTIFY_TYPE ? "PRINTER_NOTIFY_TYPE" : "JOB_NOTIFY_TYPE"),
3438                 option_type->count, lp_servicename(snum)));
3439
3440         if (!W_ERROR_IS_OK(get_a_printer(print_hnd, &printer, 2, lp_const_servicename(snum))))
3441                 return false;
3442
3443         for(field_num=0; field_num < option_type->count; field_num++) {
3444                 field = option_type->fields[field_num].field;
3445
3446                 DEBUG(4,("construct_notify_printer_info: notify [%d]: type [%x], field [%x]\n", field_num, type, field));
3447
3448                 if (!search_notify(type, field, &j) )
3449                         continue;
3450
3451                 info->notifies = TALLOC_REALLOC_ARRAY(info, info->notifies,
3452                                                       struct spoolss_Notify,
3453                                                       info->count + 1);
3454                 if (info->notifies == NULL) {
3455                         DEBUG(2,("construct_notify_printer_info: failed to enlarge buffer info->data!\n"));
3456                         free_a_printer(&printer, 2);
3457                         return false;
3458                 }
3459
3460                 current_data = &info->notifies[info->count];
3461
3462                 construct_info_data(current_data, type, field, id);
3463
3464                 DEBUG(10,("construct_notify_printer_info: calling [%s]  snum=%d  printername=[%s])\n",
3465                                 notify_info_data_table[j].name, snum, printer->info_2->printername ));
3466
3467                 notify_info_data_table[j].fn(snum, current_data, queue,
3468                                              printer, mem_ctx);
3469
3470                 info->count++;
3471         }
3472
3473         free_a_printer(&printer, 2);
3474         return true;
3475 }
3476
3477 /*******************************************************************
3478  *
3479  * fill a notify_info struct with info asked
3480  *
3481  ********************************************************************/
3482
3483 static bool construct_notify_jobs_info(print_queue_struct *queue,
3484                                        struct spoolss_NotifyInfo *info,
3485                                        NT_PRINTER_INFO_LEVEL *printer,
3486                                        int snum,
3487                                        const struct spoolss_NotifyOptionType *option_type,
3488                                        uint32_t id,
3489                                        TALLOC_CTX *mem_ctx)
3490 {
3491         int field_num,j;
3492         enum spoolss_NotifyType type;
3493         uint16_t field;
3494         struct spoolss_Notify *current_data;
3495
3496         DEBUG(4,("construct_notify_jobs_info\n"));
3497
3498         type = option_type->type;
3499
3500         DEBUGADD(4,("Notify type: [%s], number of notify info: [%d]\n",
3501                 (type == PRINTER_NOTIFY_TYPE ? "PRINTER_NOTIFY_TYPE" : "JOB_NOTIFY_TYPE"),
3502                 option_type->count));
3503
3504         for(field_num=0; field_num<option_type->count; field_num++) {
3505                 field = option_type->fields[field_num].field;
3506
3507                 if (!search_notify(type, field, &j) )
3508                         continue;
3509
3510                 info->notifies = TALLOC_REALLOC_ARRAY(info, info->notifies,
3511                                                       struct spoolss_Notify,
3512                                                       info->count + 1);
3513                 if (info->notifies == NULL) {
3514                         DEBUG(2,("construct_notify_jobs_info: failed to enlarg buffer info->data!\n"));
3515                         return false;
3516                 }
3517
3518                 current_data=&(info->notifies[info->count]);
3519
3520                 construct_info_data(current_data, type, field, id);
3521                 notify_info_data_table[j].fn(snum, current_data, queue,
3522                                              printer, mem_ctx);
3523                 info->count++;
3524         }
3525
3526         return true;
3527 }
3528
3529 /*
3530  * JFM: The enumeration is not that simple, it's even non obvious.
3531  *
3532  * let's take an example: I want to monitor the PRINTER SERVER for
3533  * the printer's name and the number of jobs currently queued.
3534  * So in the NOTIFY_OPTION, I have one NOTIFY_OPTION_TYPE structure.
3535  * Its type is PRINTER_NOTIFY_TYPE and it has 2 fields NAME and CJOBS.
3536  *
3537  * I have 3 printers on the back of my server.
3538  *
3539  * Now the response is a NOTIFY_INFO structure, with 6 NOTIFY_INFO_DATA
3540  * structures.
3541  *   Number     Data                    Id
3542  *      1       printer 1 name          1
3543  *      2       printer 1 cjob          1
3544  *      3       printer 2 name          2
3545  *      4       printer 2 cjob          2
3546  *      5       printer 3 name          3
3547  *      6       printer 3 name          3
3548  *
3549  * that's the print server case, the printer case is even worse.
3550  */
3551
3552 /*******************************************************************
3553  *
3554  * enumerate all printers on the printserver
3555  * fill a notify_info struct with info asked
3556  *
3557  ********************************************************************/
3558
3559 static WERROR printserver_notify_info(pipes_struct *p,
3560                                       struct policy_handle *hnd,
3561                                       struct spoolss_NotifyInfo *info,
3562                                       TALLOC_CTX *mem_ctx)
3563 {
3564         int snum;
3565         Printer_entry *Printer = find_printer_index_by_hnd(p, hnd);
3566         int n_services=lp_numservices();
3567         int i;
3568         struct spoolss_NotifyOption *option;
3569         struct spoolss_NotifyOptionType option_type;
3570
3571         DEBUG(4,("printserver_notify_info\n"));
3572
3573         if (!Printer)
3574                 return WERR_BADFID;
3575
3576         option = Printer->notify.option;
3577
3578         info->version   = 2;
3579         info->notifies  = NULL;
3580         info->count     = 0;
3581
3582         /* a bug in xp sp2 rc2 causes it to send a fnpcn request without
3583            sending a ffpcn() request first */
3584
3585         if ( !option )
3586                 return WERR_BADFID;
3587
3588         for (i=0; i<option->count; i++) {
3589                 option_type = option->types[i];
3590
3591                 if (option_type.type != PRINTER_NOTIFY_TYPE)
3592                         continue;
3593
3594                 for (snum=0; snum<n_services; snum++)
3595                 {
3596                         if ( lp_browseable(snum) && lp_snum_ok(snum) && lp_print_ok(snum) )
3597                                 construct_notify_printer_info ( Printer, info, snum, &option_type, snum, mem_ctx );
3598                 }
3599         }
3600
3601 #if 0
3602         /*
3603          * Debugging information, don't delete.
3604          */
3605
3606         DEBUG(1,("dumping the NOTIFY_INFO\n"));
3607         DEBUGADD(1,("info->version:[%d], info->flags:[%d], info->count:[%d]\n", info->version, info->flags, info->count));
3608         DEBUGADD(1,("num\ttype\tfield\tres\tid\tsize\tenc_type\n"));
3609
3610         for (i=0; i<info->count; i++) {
3611                 DEBUGADD(1,("[%d]\t[%d]\t[%d]\t[%d]\t[%d]\t[%d]\t[%d]\n",
3612                 i, info->data[i].type, info->data[i].field, info->data[i].reserved,
3613                 info->data[i].id, info->data[i].size, info->data[i].enc_type));
3614         }
3615 #endif
3616
3617         return WERR_OK;
3618 }
3619
3620 /*******************************************************************
3621  *
3622  * fill a notify_info struct with info asked
3623  *
3624  ********************************************************************/
3625
3626 static WERROR printer_notify_info(pipes_struct *p, struct policy_handle *hnd,
3627                                   struct spoolss_NotifyInfo *info,
3628                                   TALLOC_CTX *mem_ctx)
3629 {
3630         int snum;
3631         Printer_entry *Printer = find_printer_index_by_hnd(p, hnd);
3632         int i;
3633         uint32_t id;
3634         struct spoolss_NotifyOption *option;
3635         struct spoolss_NotifyOptionType option_type;
3636         int count,j;
3637         print_queue_struct *queue=NULL;
3638         print_status_struct status;
3639
3640         DEBUG(4,("printer_notify_info\n"));
3641
3642         if (!Printer)
3643                 return WERR_BADFID;
3644
3645         option = Printer->notify.option;
3646         id = 0x0;
3647
3648         info->version   = 2;
3649         info->notifies  = NULL;
3650         info->count     = 0;
3651
3652         /* a bug in xp sp2 rc2 causes it to send a fnpcn request without
3653            sending a ffpcn() request first */
3654
3655         if ( !option )
3656                 return WERR_BADFID;
3657
3658         get_printer_snum(p, hnd, &snum, NULL);
3659
3660         for (i=0; i<option->count; i++) {
3661                 option_type = option->types[i];
3662
3663                 switch (option_type.type) {
3664                 case PRINTER_NOTIFY_TYPE:
3665                         if(construct_notify_printer_info(Printer, info, snum,
3666                                                          &option_type, id,
3667                                                          mem_ctx))
3668                                 id--;
3669                         break;
3670
3671                 case JOB_NOTIFY_TYPE: {
3672                         NT_PRINTER_INFO_LEVEL *printer = NULL;
3673
3674                         count = print_queue_status(snum, &queue, &status);
3675
3676                         if (!W_ERROR_IS_OK(get_a_printer(Printer, &printer, 2, lp_const_servicename(snum))))
3677                                 goto done;
3678
3679                         for (j=0; j<count; j++) {
3680                                 construct_notify_jobs_info(&queue[j], info,
3681                                                            printer, snum,
3682                                                            &option_type,
3683                                                            queue[j].job,
3684                                                            mem_ctx);
3685                         }
3686
3687                         free_a_printer(&printer, 2);
3688
3689                 done:
3690                         SAFE_FREE(queue);
3691                         break;
3692                 }
3693                 }
3694         }
3695
3696         /*
3697          * Debugging information, don't delete.
3698          */
3699         /*
3700         DEBUG(1,("dumping the NOTIFY_INFO\n"));
3701         DEBUGADD(1,("info->version:[%d], info->flags:[%d], info->count:[%d]\n", info->version, info->flags, info->count));
3702         DEBUGADD(1,("num\ttype\tfield\tres\tid\tsize\tenc_type\n"));
3703
3704         for (i=0; i<info->count; i++) {
3705                 DEBUGADD(1,("[%d]\t[%d]\t[%d]\t[%d]\t[%d]\t[%d]\t[%d]\n",
3706                 i, info->data[i].type, info->data[i].field, info->data[i].reserved,
3707                 info->data[i].id, info->data[i].size, info->data[i].enc_type));
3708         }
3709         */
3710         return WERR_OK;
3711 }
3712
3713 /****************************************************************
3714  _spoolss_RouterRefreshPrinterChangeNotify
3715 ****************************************************************/
3716
3717 WERROR _spoolss_RouterRefreshPrinterChangeNotify(pipes_struct *p,
3718                                                  struct spoolss_RouterRefreshPrinterChangeNotify *r)
3719 {
3720         struct spoolss_NotifyInfo *info;
3721
3722         Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
3723         WERROR result = WERR_BADFID;
3724
3725         /* we always have a spoolss_NotifyInfo struct */
3726         info = talloc_zero(p->mem_ctx, struct spoolss_NotifyInfo);
3727         if (!info) {
3728                 result = WERR_NOMEM;
3729                 goto done;
3730         }
3731
3732         *r->out.info = info;
3733
3734         if (!Printer) {
3735                 DEBUG(2,("_spoolss_RouterRefreshPrinterChangeNotify: "
3736                         "Invalid handle (%s:%u:%u).\n",
3737                         OUR_HANDLE(r->in.handle)));
3738                 goto done;
3739         }
3740
3741         DEBUG(4,("Printer type %x\n",Printer->printer_type));
3742
3743         /*
3744          *      We are now using the change value, and
3745          *      I should check for PRINTER_NOTIFY_OPTIONS_REFRESH but as
3746          *      I don't have a global notification system, I'm sending back all the
3747          *      informations even when _NOTHING_ has changed.
3748          */
3749
3750         /* We need to keep track of the change value to send back in
3751            RRPCN replies otherwise our updates are ignored. */
3752
3753         Printer->notify.fnpcn = true;
3754
3755         if (Printer->notify.client_connected) {
3756                 DEBUG(10,("_spoolss_RouterRefreshPrinterChangeNotify: "
3757                         "Saving change value in request [%x]\n",
3758                         r->in.change_low));
3759                 Printer->notify.change = r->in.change_low;
3760         }
3761
3762         /* just ignore the spoolss_NotifyOption */
3763
3764         switch (Printer->printer_type) {
3765                 case SPLHND_SERVER:
3766                         result = printserver_notify_info(p, r->in.handle,
3767                                                          info, p->mem_ctx);
3768                         break;
3769
3770                 case SPLHND_PRINTER:
3771                         result = printer_notify_info(p, r->in.handle,
3772                                                      info, p->mem_ctx);
3773                         break;
3774         }
3775
3776         Printer->notify.fnpcn = false;
3777
3778 done:
3779         return result;
3780 }
3781
3782 /********************************************************************
3783  * construct_printer_info_0
3784  * fill a printer_info_0 struct
3785  ********************************************************************/
3786
3787 static WERROR construct_printer_info0(TALLOC_CTX *mem_ctx,
3788                                       const NT_PRINTER_INFO_LEVEL *ntprinter,
3789                                       struct spoolss_PrinterInfo0 *r,
3790                                       int snum)
3791 {
3792         int count;
3793         counter_printer_0 *session_counter;
3794         time_t setuptime;
3795         print_status_struct status;
3796
3797         r->printername          = talloc_strdup(mem_ctx, ntprinter->info_2->printername);
3798         W_ERROR_HAVE_NO_MEMORY(r->printername);
3799
3800         r->servername           = talloc_strdup(mem_ctx, ntprinter->info_2->servername);
3801         W_ERROR_HAVE_NO_MEMORY(r->servername);
3802
3803         count = print_queue_length(snum, &status);
3804
3805         /* check if we already have a counter for this printer */
3806         for (session_counter = counter_list; session_counter; session_counter = session_counter->next) {
3807                 if (session_counter->snum == snum)
3808                         break;
3809         }
3810
3811         /* it's the first time, add it to the list */
3812         if (session_counter == NULL) {
3813                 session_counter = SMB_MALLOC_P(counter_printer_0);
3814                 W_ERROR_HAVE_NO_MEMORY(session_counter);
3815                 ZERO_STRUCTP(session_counter);
3816                 session_counter->snum           = snum;
3817                 session_counter->counter        = 0;
3818                 DLIST_ADD(counter_list, session_counter);
3819         }
3820
3821         /* increment it */
3822         session_counter->counter++;
3823
3824         r->cjobs                        = count;
3825         r->total_jobs                   = 0;
3826         r->total_bytes                  = 0;
3827
3828         setuptime = (time_t)ntprinter->info_2->setuptime;
3829
3830         init_systemtime(&r->time, gmtime(&setuptime));
3831
3832         /* JFM:
3833          * the global_counter should be stored in a TDB as it's common to all the clients
3834          * and should be zeroed on samba startup
3835          */
3836         r->global_counter               = session_counter->counter;
3837         r->total_pages                  = 0;
3838         /* in 2.2 we reported ourselves as 0x0004 and 0x0565 */
3839         r->version                      = 0x0005;       /* NT 5 */
3840         r->free_build                   = 0x0893;       /* build 2195 */
3841         r->spooling                     = 0;
3842         r->max_spooling                 = 0;
3843         r->session_counter              = session_counter->counter;
3844         r->num_error_out_of_paper       = 0x0;
3845         r->num_error_not_ready          = 0x0;          /* number of print failure */
3846         r->job_error                    = 0x0;
3847         r->number_of_processors         = 0x1;
3848         r->processor_type               = PROCESSOR_INTEL_PENTIUM; /* 586 Pentium ? */
3849         r->high_part_total_bytes        = 0x0;
3850         r->change_id                    = ntprinter->info_2->changeid; /* ChangeID in milliseconds*/
3851         r->last_error                   = WERR_OK;
3852         r->status                       = nt_printq_status(status.status);
3853         r->enumerate_network_printers   = 0x0;
3854         r->c_setprinter                 = get_c_setprinter(); /* monotonically increasing sum of delta printer counts */
3855         r->processor_architecture       = 0x0;
3856         r->processor_level              = 0x6;          /* 6  ???*/
3857         r->ref_ic                       = 0;
3858         r->reserved2                    = 0;
3859         r->reserved3                    = 0;
3860
3861         return WERR_OK;
3862 }
3863
3864 /****************************************************************************
3865  Convert an NT_DEVICEMODE to a spoolss_DeviceMode structure.  Both pointers
3866  should be valid upon entry
3867 ****************************************************************************/
3868
3869 static WERROR convert_nt_devicemode(TALLOC_CTX *mem_ctx,
3870                                     struct spoolss_DeviceMode *r,
3871                                     const NT_DEVICEMODE *ntdevmode)
3872 {
3873         if (!r || !ntdevmode) {
3874                 return WERR_INVALID_PARAM;
3875         }
3876
3877         r->devicename           = talloc_strdup(mem_ctx, ntdevmode->devicename);
3878         W_ERROR_HAVE_NO_MEMORY(r->devicename);
3879
3880         r->specversion          = ntdevmode->specversion;
3881         r->driverversion        = ntdevmode->driverversion;
3882         r->size                 = ntdevmode->size;
3883         r->__driverextra_length = ntdevmode->driverextra;
3884         r->fields               = ntdevmode->fields;
3885
3886         r->orientation          = ntdevmode->orientation;
3887         r->papersize            = ntdevmode->papersize;
3888         r->paperlength          = ntdevmode->paperlength;
3889         r->paperwidth           = ntdevmode->paperwidth;
3890         r->scale                = ntdevmode->scale;
3891         r->copies               = ntdevmode->copies;
3892         r->defaultsource        = ntdevmode->defaultsource;
3893         r->printquality         = ntdevmode->printquality;
3894         r->color                = ntdevmode->color;
3895         r->duplex               = ntdevmode->duplex;
3896         r->yresolution          = ntdevmode->yresolution;
3897         r->ttoption             = ntdevmode->ttoption;
3898         r->collate              = ntdevmode->collate;
3899
3900         r->formname             = talloc_strdup(mem_ctx, ntdevmode->formname);
3901         W_ERROR_HAVE_NO_MEMORY(r->formname);
3902
3903         /* all 0 below are values that have not been set in the old parsing/copy
3904          * function, maybe they should... - gd */
3905
3906         r->logpixels            = 0;
3907         r->bitsperpel           = 0;
3908         r->pelswidth            = 0;
3909         r->pelsheight           = 0;
3910         r->displayflags         = 0;
3911         r->displayfrequency     = 0;
3912         r->icmmethod            = ntdevmode->icmmethod;
3913         r->icmintent            = ntdevmode->icmintent;
3914         r->mediatype            = ntdevmode->mediatype;
3915         r->dithertype           = ntdevmode->dithertype;
3916         r->reserved1            = 0;
3917         r->reserved2            = 0;
3918         r->panningwidth         = 0;
3919         r->panningheight        = 0;
3920
3921         if (ntdevmode->nt_dev_private != NULL) {
3922                 r->driverextra_data = data_blob_talloc(mem_ctx,
3923                         ntdevmode->nt_dev_private,
3924                         ntdevmode->driverextra);
3925                 W_ERROR_HAVE_NO_MEMORY(r->driverextra_data.data);
3926         }
3927
3928         return WERR_OK;
3929 }
3930
3931
3932 /****************************************************************************
3933  Create a spoolss_DeviceMode struct. Returns talloced memory.
3934 ****************************************************************************/
3935
3936 struct spoolss_DeviceMode *construct_dev_mode(TALLOC_CTX *mem_ctx,
3937                                               const char *servicename)
3938 {
3939         WERROR result;
3940         NT_PRINTER_INFO_LEVEL   *printer = NULL;
3941         struct spoolss_DeviceMode *devmode = NULL;
3942
3943         DEBUG(7,("construct_dev_mode\n"));
3944
3945         DEBUGADD(8,("getting printer characteristics\n"));
3946
3947         if (!W_ERROR_IS_OK(get_a_printer(NULL, &printer, 2, servicename)))
3948                 return NULL;
3949
3950         if (!printer->info_2->devmode) {
3951                 DEBUG(5, ("BONG! There was no device mode!\n"));
3952                 goto done;
3953         }
3954
3955         devmode = TALLOC_ZERO_P(mem_ctx, struct spoolss_DeviceMode);
3956         if (!devmode) {
3957                 DEBUG(2,("construct_dev_mode: talloc fail.\n"));
3958                 goto done;
3959         }
3960
3961         DEBUGADD(8,("loading DEVICEMODE\n"));
3962
3963         result = convert_nt_devicemode(mem_ctx, devmode, printer->info_2->devmode);
3964         if (!W_ERROR_IS_OK(result)) {
3965                 TALLOC_FREE(devmode);
3966         }
3967
3968 done:
3969         free_a_printer(&printer,2);
3970
3971         return devmode;
3972 }
3973
3974 /********************************************************************
3975  * construct_printer_info3
3976  * fill a spoolss_PrinterInfo3 struct
3977  ********************************************************************/
3978
3979 static WERROR construct_printer_info3(TALLOC_CTX *mem_ctx,
3980                                       const NT_PRINTER_INFO_LEVEL *ntprinter,
3981                                       struct spoolss_PrinterInfo3 *r,
3982                                       int snum)
3983 {
3984         /* These are the components of the SD we are returning. */
3985
3986         if (ntprinter->info_2->secdesc_buf && ntprinter->info_2->secdesc_buf->sd_size != 0) {
3987                 /* don't use talloc_steal() here unless you do a deep steal of all
3988                    the SEC_DESC members */
3989
3990                 r->secdesc = dup_sec_desc(mem_ctx,
3991                                           ntprinter->info_2->secdesc_buf->sd);
3992                 W_ERROR_HAVE_NO_MEMORY(r->secdesc);
3993         }
3994
3995         return WERR_OK;
3996 }
3997
3998 /********************************************************************
3999  * construct_printer_info4
4000  * fill a spoolss_PrinterInfo4 struct
4001  ********************************************************************/
4002
4003 static WERROR construct_printer_info4(TALLOC_CTX *mem_ctx,
4004                                       const NT_PRINTER_INFO_LEVEL *ntprinter,
4005                                       struct spoolss_PrinterInfo4 *r,
4006                                       int snum)
4007 {
4008         r->printername  = talloc_strdup(mem_ctx, ntprinter->info_2->printername);
4009         W_ERROR_HAVE_NO_MEMORY(r->printername);
4010         r->servername   = talloc_strdup(mem_ctx, ntprinter->info_2->servername);
4011         W_ERROR_HAVE_NO_MEMORY(r->servername);
4012
4013         r->attributes   = ntprinter->info_2->attributes;
4014
4015         return WERR_OK;
4016 }
4017
4018 /********************************************************************
4019  * construct_printer_info5
4020  * fill a spoolss_PrinterInfo5 struct
4021  ********************************************************************/
4022
4023 static WERROR construct_printer_info5(TALLOC_CTX *mem_ctx,
4024                                       const NT_PRINTER_INFO_LEVEL *ntprinter,
4025                                       struct spoolss_PrinterInfo5 *r,
4026                                       int snum)
4027 {
4028         r->printername  = talloc_strdup(mem_ctx, ntprinter->info_2->printername);
4029         W_ERROR_HAVE_NO_MEMORY(r->printername);
4030         r->portname     = talloc_strdup(mem_ctx, ntprinter->info_2->portname);
4031         W_ERROR_HAVE_NO_MEMORY(r->portname);
4032
4033         r->attributes   = ntprinter->info_2->attributes;
4034
4035         /* these two are not used by NT+ according to MSDN */
4036
4037         r->device_not_selected_timeout          = 0x0;  /* have seen 0x3a98 */
4038         r->transmission_retry_timeout           = 0x0;  /* have seen 0xafc8 */
4039
4040         return WERR_OK;
4041 }
4042
4043 /********************************************************************
4044  * construct_printer_info_6
4045  * fill a spoolss_PrinterInfo6 struct
4046  ********************************************************************/
4047
4048 static WERROR construct_printer_info6(TALLOC_CTX *mem_ctx,
4049                                       const NT_PRINTER_INFO_LEVEL *ntprinter,
4050                                       struct spoolss_PrinterInfo6 *r,
4051                                       int snum)
4052 {
4053         int count;
4054         print_status_struct status;
4055
4056         count = print_queue_length(snum, &status);
4057
4058         r->status = nt_printq_status(status.status);
4059
4060         return WERR_OK;
4061 }
4062
4063 /********************************************************************
4064  * construct_printer_info7
4065  * fill a spoolss_PrinterInfo7 struct
4066  ********************************************************************/
4067
4068 static WERROR construct_printer_info7(TALLOC_CTX *mem_ctx,
4069                                       Printer_entry *print_hnd,
4070                                       struct spoolss_PrinterInfo7 *r,
4071                                       int snum)
4072 {
4073         struct GUID guid;
4074
4075         if (is_printer_published(print_hnd, snum, &guid)) {
4076                 r->guid = talloc_strdup_upper(mem_ctx, GUID_string2(mem_ctx, &guid));
4077                 r->action = DSPRINT_PUBLISH;
4078         } else {
4079                 r->guid = talloc_strdup(mem_ctx, "");
4080                 r->action = DSPRINT_UNPUBLISH;
4081         }
4082         W_ERROR_HAVE_NO_MEMORY(r->guid);
4083
4084         return WERR_OK;
4085 }
4086
4087 /********************************************************************
4088  * construct_printer_info1
4089  * fill a spoolss_PrinterInfo1 struct
4090 ********************************************************************/
4091
4092 static WERROR construct_printer_info1(TALLOC_CTX *mem_ctx,
4093                                       const NT_PRINTER_INFO_LEVEL *ntprinter,
4094                                       uint32_t flags,
4095                                       struct spoolss_PrinterInfo1 *r,
4096                                       int snum)
4097 {
4098         char *chaine = NULL;
4099         r->flags                = flags;
4100
4101         if (*ntprinter->info_2->comment == '\0') {
4102                 r->comment      = talloc_strdup(mem_ctx, lp_comment(snum));
4103                 chaine = talloc_asprintf(mem_ctx,
4104                                 "%s,%s,%s", ntprinter->info_2->printername,
4105                                 ntprinter->info_2->drivername, lp_comment(snum));
4106         } else {
4107                 r->comment      = talloc_strdup(mem_ctx, ntprinter->info_2->comment); /* saved comment */
4108                 chaine = talloc_asprintf(mem_ctx,
4109                                 "%s,%s,%s", ntprinter->info_2->printername,
4110                                 ntprinter->info_2->drivername, ntprinter->info_2->comment);
4111         }
4112         W_ERROR_HAVE_NO_MEMORY(chaine);
4113         W_ERROR_HAVE_NO_MEMORY(r->comment);
4114
4115         r->description          = talloc_strdup(mem_ctx, chaine);
4116         W_ERROR_HAVE_NO_MEMORY(r->description);
4117         r->name                 = talloc_strdup(mem_ctx, ntprinter->info_2->printername);
4118         W_ERROR_HAVE_NO_MEMORY(r->name);
4119
4120         return WERR_OK;
4121 }
4122
4123 /********************************************************************
4124  * construct_printer_info2
4125  * fill a spoolss_PrinterInfo2 struct
4126 ********************************************************************/
4127
4128 static WERROR construct_printer_info2(TALLOC_CTX *mem_ctx,
4129                                       const NT_PRINTER_INFO_LEVEL *ntprinter,
4130                                       struct spoolss_PrinterInfo2 *r,
4131                                       int snum)
4132 {
4133         int count;
4134
4135         print_status_struct status;
4136
4137         count = print_queue_length(snum, &status);
4138
4139         r->servername           = talloc_strdup(mem_ctx, ntprinter->info_2->servername);
4140         W_ERROR_HAVE_NO_MEMORY(r->servername);
4141         r->printername          = talloc_strdup(mem_ctx, ntprinter->info_2->printername);
4142         W_ERROR_HAVE_NO_MEMORY(r->printername);
4143         r->sharename            = talloc_strdup(mem_ctx, lp_servicename(snum));
4144         W_ERROR_HAVE_NO_MEMORY(r->sharename);
4145         r->portname             = talloc_strdup(mem_ctx, ntprinter->info_2->portname);
4146         W_ERROR_HAVE_NO_MEMORY(r->portname);
4147         r->drivername           = talloc_strdup(mem_ctx, ntprinter->info_2->drivername);
4148         W_ERROR_HAVE_NO_MEMORY(r->drivername);
4149
4150         if (*ntprinter->info_2->comment == '\0') {
4151                 r->comment      = talloc_strdup(mem_ctx, lp_comment(snum));
4152         } else {
4153                 r->comment      = talloc_strdup(mem_ctx, ntprinter->info_2->comment);
4154         }
4155         W_ERROR_HAVE_NO_MEMORY(r->comment);
4156
4157         r->location             = talloc_strdup(mem_ctx, ntprinter->info_2->location);
4158         W_ERROR_HAVE_NO_MEMORY(r->location);
4159         r->sepfile              = talloc_strdup(mem_ctx, ntprinter->info_2->sepfile);
4160         W_ERROR_HAVE_NO_MEMORY(r->sepfile);
4161         r->printprocessor       = talloc_strdup(mem_ctx, ntprinter->info_2->printprocessor);
4162         W_ERROR_HAVE_NO_MEMORY(r->printprocessor);
4163         r->datatype             = talloc_strdup(mem_ctx, ntprinter->info_2->datatype);
4164         W_ERROR_HAVE_NO_MEMORY(r->datatype);
4165         r->parameters           = talloc_strdup(mem_ctx, ntprinter->info_2->parameters);
4166         W_ERROR_HAVE_NO_MEMORY(r->parameters);
4167
4168         r->attributes           = ntprinter->info_2->attributes;
4169
4170         r->priority             = ntprinter->info_2->priority;
4171         r->defaultpriority      = ntprinter->info_2->default_priority;
4172         r->starttime            = ntprinter->info_2->starttime;
4173         r->untiltime            = ntprinter->info_2->untiltime;
4174         r->status               = nt_printq_status(status.status);
4175         r->cjobs                = count;
4176         r->averageppm           = ntprinter->info_2->averageppm;
4177
4178         r->devmode = construct_dev_mode(mem_ctx, lp_const_servicename(snum));
4179         if (!r->devmode) {
4180                 DEBUG(8,("Returning NULL Devicemode!\n"));
4181         }
4182
4183         r->secdesc              = NULL;
4184
4185         if (ntprinter->info_2->secdesc_buf && ntprinter->info_2->secdesc_buf->sd_size != 0) {
4186                 /* don't use talloc_steal() here unless you do a deep steal of all
4187                    the SEC_DESC members */
4188
4189                 r->secdesc      = dup_sec_desc(mem_ctx, ntprinter->info_2->secdesc_buf->sd);
4190         }
4191
4192         return WERR_OK;
4193 }
4194
4195 /********************************************************************
4196 ********************************************************************/
4197
4198 static bool snum_is_shared_printer(int snum)
4199 {
4200         return (lp_browseable(snum) && lp_snum_ok(snum) && lp_print_ok(snum));
4201 }
4202
4203 /********************************************************************
4204  Spoolss_enumprinters.
4205 ********************************************************************/
4206
4207 static WERROR enum_all_printers_info_level(TALLOC_CTX *mem_ctx,
4208                                            uint32_t level,
4209                                            uint32_t flags,
4210                                            union spoolss_PrinterInfo **info_p,
4211                                            uint32_t *count_p)
4212 {
4213         int snum;
4214         int n_services = lp_numservices();
4215         union spoolss_PrinterInfo *info = NULL;
4216         uint32_t count = 0;
4217         WERROR result = WERR_OK;
4218
4219         *count_p = 0;
4220         *info_p = NULL;
4221
4222         for (snum = 0; snum < n_services; snum++) {
4223
4224                 NT_PRINTER_INFO_LEVEL *ntprinter = NULL;
4225
4226                 if (!snum_is_shared_printer(snum)) {
4227                         continue;
4228                 }
4229
4230                 DEBUG(4,("Found a printer in smb.conf: %s[%x]\n",
4231                         lp_servicename(snum), snum));
4232
4233                 info = TALLOC_REALLOC_ARRAY(mem_ctx, info,
4234                                             union spoolss_PrinterInfo,
4235                                             count + 1);
4236                 if (!info) {
4237                         result = WERR_NOMEM;
4238                         goto out;
4239                 }
4240
4241                 result = get_a_printer(NULL, &ntprinter, 2,
4242                                        lp_const_servicename(snum));
4243                 if (!W_ERROR_IS_OK(result)) {
4244                         goto out;
4245                 }
4246
4247                 switch (level) {
4248                 case 0:
4249                         result = construct_printer_info0(info, ntprinter,
4250                                                          &info[count].info0, snum);
4251                         break;
4252                 case 1:
4253                         result = construct_printer_info1(info, ntprinter, flags,
4254                                                          &info[count].info1, snum);
4255                         break;
4256                 case 2:
4257                         result = construct_printer_info2(info, ntprinter,
4258                                                          &info[count].info2, snum);
4259                         break;
4260                 case 4:
4261                         result = construct_printer_info4(info, ntprinter,
4262                                                          &info[count].info4, snum);
4263                         break;
4264                 case 5:
4265                         result = construct_printer_info5(info, ntprinter,
4266                                                          &info[count].info5, snum);
4267                         break;
4268
4269                 default:
4270                         result = WERR_UNKNOWN_LEVEL;
4271                         free_a_printer(&ntprinter, 2);
4272                         goto out;
4273                 }
4274
4275                 free_a_printer(&ntprinter, 2);
4276                 if (!W_ERROR_IS_OK(result)) {
4277                         goto out;
4278                 }
4279
4280                 count++;
4281         }
4282
4283         *count_p = count;
4284         *info_p = info;
4285
4286  out:
4287         if (!W_ERROR_IS_OK(result)) {
4288                 TALLOC_FREE(info);
4289                 return result;
4290         }
4291
4292         *info_p = info;
4293
4294         return WERR_OK;
4295 }
4296
4297 /********************************************************************
4298  * handle enumeration of printers at level 0
4299  ********************************************************************/
4300
4301 static WERROR enumprinters_level0(TALLOC_CTX *mem_ctx,
4302                                   uint32_t flags,
4303                                   const char *servername,
4304                                   union spoolss_PrinterInfo **info,
4305                                   uint32_t *count)
4306 {
4307         DEBUG(4,("enum_all_printers_info_0\n"));
4308
4309         return enum_all_printers_info_level(mem_ctx, 0, flags, info, count);
4310 }
4311
4312
4313 /********************************************************************
4314 ********************************************************************/
4315
4316 static WERROR enum_all_printers_info_1(TALLOC_CTX *mem_ctx,
4317                                        uint32_t flags,
4318                                        union spoolss_PrinterInfo **info,
4319                                        uint32_t *count)
4320 {
4321         DEBUG(4,("enum_all_printers_info_1\n"));
4322
4323         return enum_all_printers_info_level(mem_ctx, 1, flags, info, count);
4324 }
4325
4326 /********************************************************************
4327  enum_all_printers_info_1_local.
4328 *********************************************************************/
4329
4330 static WERROR enum_all_printers_info_1_local(TALLOC_CTX *mem_ctx,
4331                                              union spoolss_PrinterInfo **info,
4332                                              uint32_t *count)
4333 {
4334         DEBUG(4,("enum_all_printers_info_1_local\n"));
4335
4336         return enum_all_printers_info_1(mem_ctx, PRINTER_ENUM_ICON8, info, count);
4337 }
4338
4339 /********************************************************************
4340  enum_all_printers_info_1_name.
4341 *********************************************************************/
4342
4343 static WERROR enum_all_printers_info_1_name(TALLOC_CTX *mem_ctx,
4344                                             const char *name,
4345                                             union spoolss_PrinterInfo **info,
4346                                             uint32_t *count)
4347 {
4348         const char *s = name;
4349
4350         DEBUG(4,("enum_all_printers_info_1_name\n"));
4351
4352         if ((name[0] == '\\') && (name[1] == '\\')) {
4353                 s = name + 2;
4354         }
4355
4356         if (!is_myname_or_ipaddr(s)) {
4357                 return WERR_INVALID_NAME;
4358         }
4359
4360         return enum_all_printers_info_1(mem_ctx, PRINTER_ENUM_ICON8, info, count);
4361 }
4362
4363 /********************************************************************
4364  enum_all_printers_info_1_network.
4365 *********************************************************************/
4366
4367 static WERROR enum_all_printers_info_1_network(TALLOC_CTX *mem_ctx,
4368                                                const char *name,
4369                                                union spoolss_PrinterInfo **info,
4370                                                uint32_t *count)
4371 {
4372         const char *s = name;
4373
4374         DEBUG(4,("enum_all_printers_info_1_network\n"));
4375
4376         /* If we respond to a enum_printers level 1 on our name with flags
4377            set to PRINTER_ENUM_REMOTE with a list of printers then these
4378            printers incorrectly appear in the APW browse list.
4379            Specifically the printers for the server appear at the workgroup
4380            level where all the other servers in the domain are
4381            listed. Windows responds to this call with a
4382            WERR_CAN_NOT_COMPLETE so we should do the same. */
4383
4384         if (name[0] == '\\' && name[1] == '\\') {
4385                  s = name + 2;
4386         }
4387
4388         if (is_myname_or_ipaddr(s)) {
4389                  return WERR_CAN_NOT_COMPLETE;
4390         }
4391
4392         return enum_all_printers_info_1(mem_ctx, PRINTER_ENUM_NAME, info, count);
4393 }
4394
4395 /********************************************************************
4396  * api_spoolss_enumprinters
4397  *
4398  * called from api_spoolss_enumprinters (see this to understand)
4399  ********************************************************************/
4400
4401 static WERROR enum_all_printers_info_2(TALLOC_CTX *mem_ctx,
4402                                        union spoolss_PrinterInfo **info,
4403                                        uint32_t *count)
4404 {
4405         DEBUG(4,("enum_all_printers_info_2\n"));
4406
4407         return enum_all_printers_info_level(mem_ctx, 2, 0, info, count);
4408 }
4409
4410 /********************************************************************
4411  * handle enumeration of printers at level 1
4412  ********************************************************************/
4413
4414 static WERROR enumprinters_level1(TALLOC_CTX *mem_ctx,
4415                                   uint32_t flags,
4416                                   const char *name,
4417                                   union spoolss_PrinterInfo **info,
4418                                   uint32_t *count)
4419 {
4420         /* Not all the flags are equals */
4421
4422         if (flags & PRINTER_ENUM_LOCAL) {
4423                 return enum_all_printers_info_1_local(mem_ctx, info, count);
4424         }
4425
4426         if (flags & PRINTER_ENUM_NAME) {
4427                 return enum_all_printers_info_1_name(mem_ctx, name, info, count);
4428         }
4429
4430         if (flags & PRINTER_ENUM_NETWORK) {
4431                 return enum_all_printers_info_1_network(mem_ctx, name, info, count);
4432         }
4433
4434         return WERR_OK; /* NT4sp5 does that */
4435 }
4436
4437 /********************************************************************
4438  * handle enumeration of printers at level 2
4439  ********************************************************************/
4440
4441 static WERROR enumprinters_level2(TALLOC_CTX *mem_ctx,
4442                                   uint32_t flags,
4443                                   const char *servername,
4444                                   union spoolss_PrinterInfo **info,
4445                                   uint32_t *count)
4446 {
4447         if (flags & PRINTER_ENUM_LOCAL) {
4448                 return enum_all_printers_info_2(mem_ctx, info, count);
4449         }
4450
4451         if (flags & PRINTER_ENUM_NAME) {
4452                 if (!is_myname_or_ipaddr(canon_servername(servername))) {
4453                         return WERR_INVALID_NAME;
4454                 }
4455
4456                 return enum_all_printers_info_2(mem_ctx, info, count);
4457         }
4458
4459         if (flags & PRINTER_ENUM_REMOTE) {
4460                 return WERR_UNKNOWN_LEVEL;
4461         }
4462
4463         return WERR_OK;
4464 }
4465
4466 /********************************************************************
4467  * handle enumeration of printers at level 4
4468  ********************************************************************/
4469
4470 static WERROR enumprinters_level4(TALLOC_CTX *mem_ctx,
4471                                   uint32_t flags,
4472                                   const char *servername,
4473                                   union spoolss_PrinterInfo **info,
4474                                   uint32_t *count)
4475 {
4476         DEBUG(4,("enum_all_printers_info_4\n"));
4477
4478         return enum_all_printers_info_level(mem_ctx, 4, flags, info, count);
4479 }
4480
4481
4482 /********************************************************************
4483  * handle enumeration of printers at level 5
4484  ********************************************************************/
4485
4486 static WERROR enumprinters_level5(TALLOC_CTX *mem_ctx,
4487                                   uint32_t flags,
4488                                   const char *servername,
4489                                   union spoolss_PrinterInfo **info,
4490                                   uint32_t *count)
4491 {
4492         DEBUG(4,("enum_all_printers_info_5\n"));
4493
4494         return enum_all_printers_info_level(mem_ctx, 5, flags, info, count);
4495 }
4496
4497 /****************************************************************
4498  _spoolss_EnumPrinters
4499 ****************************************************************/
4500
4501 WERROR _spoolss_EnumPrinters(pipes_struct *p,
4502                              struct spoolss_EnumPrinters *r)
4503 {
4504         const char *name;
4505         WERROR result;
4506
4507         /* that's an [in out] buffer */
4508
4509         if (!r->in.buffer && (r->in.offered != 0)) {
4510                 return WERR_INVALID_PARAM;
4511         }
4512
4513         DEBUG(4,("_spoolss_EnumPrinters\n"));
4514
4515         *r->out.needed = 0;
4516         *r->out.count = 0;
4517         *r->out.info = NULL;
4518
4519         /*
4520          * Level 1:
4521          *          flags==PRINTER_ENUM_NAME
4522          *           if name=="" then enumerates all printers
4523          *           if name!="" then enumerate the printer
4524          *          flags==PRINTER_ENUM_REMOTE
4525          *          name is NULL, enumerate printers
4526          * Level 2: name!="" enumerates printers, name can't be NULL
4527          * Level 3: doesn't exist
4528          * Level 4: does a local registry lookup
4529          * Level 5: same as Level 2
4530          */
4531
4532         name = talloc_strdup_upper(p->mem_ctx, r->in.server);
4533         W_ERROR_HAVE_NO_MEMORY(name);
4534
4535         switch (r->in.level) {
4536         case 0:
4537                 result = enumprinters_level0(p->mem_ctx, r->in.flags, name,
4538                                              r->out.info, r->out.count);
4539                 break;
4540         case 1:
4541                 result = enumprinters_level1(p->mem_ctx, r->in.flags, name,
4542                                              r->out.info, r->out.count);
4543                 break;
4544         case 2:
4545                 result = enumprinters_level2(p->mem_ctx, r->in.flags, name,
4546                                              r->out.info, r->out.count);
4547                 break;
4548         case 4:
4549                 result = enumprinters_level4(p->mem_ctx, r->in.flags, name,
4550                                              r->out.info, r->out.count);
4551                 break;
4552         case 5:
4553                 result = enumprinters_level5(p->mem_ctx, r->in.flags, name,
4554                                              r->out.info, r->out.count);
4555                 break;
4556         default:
4557                 return WERR_UNKNOWN_LEVEL;
4558         }
4559
4560         if (!W_ERROR_IS_OK(result)) {
4561                 return result;
4562         }
4563
4564         *r->out.needed  = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
4565                                                      spoolss_EnumPrinters, NULL,
4566                                                      *r->out.info, r->in.level,
4567                                                      *r->out.count);
4568         *r->out.info    = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
4569         *r->out.count   = SPOOLSS_BUFFER_OK(*r->out.count, 0);
4570
4571         return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
4572 }
4573
4574 /****************************************************************
4575  _spoolss_GetPrinter
4576 ****************************************************************/
4577
4578 WERROR _spoolss_GetPrinter(pipes_struct *p,
4579                            struct spoolss_GetPrinter *r)
4580 {
4581         Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
4582         NT_PRINTER_INFO_LEVEL *ntprinter = NULL;
4583         WERROR result = WERR_OK;
4584
4585         int snum;
4586
4587         /* that's an [in out] buffer */
4588
4589         if (!r->in.buffer && (r->in.offered != 0)) {
4590                 return WERR_INVALID_PARAM;
4591         }
4592
4593         *r->out.needed = 0;
4594
4595         if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
4596                 return WERR_BADFID;
4597         }
4598
4599         result = get_a_printer(Printer, &ntprinter, 2,
4600                                lp_const_servicename(snum));
4601         if (!W_ERROR_IS_OK(result)) {
4602                 return result;
4603         }
4604
4605         switch (r->in.level) {
4606         case 0:
4607                 result = construct_printer_info0(p->mem_ctx, ntprinter,
4608                                                  &r->out.info->info0, snum);
4609                 break;
4610         case 1:
4611                 result = construct_printer_info1(p->mem_ctx, ntprinter,
4612                                                  PRINTER_ENUM_ICON8,
4613                                                  &r->out.info->info1, snum);
4614                 break;
4615         case 2:
4616                 result = construct_printer_info2(p->mem_ctx, ntprinter,
4617                                                  &r->out.info->info2, snum);
4618                 break;
4619         case 3:
4620                 result = construct_printer_info3(p->mem_ctx, ntprinter,
4621                                                  &r->out.info->info3, snum);
4622                 break;
4623         case 4:
4624                 result = construct_printer_info4(p->mem_ctx, ntprinter,
4625                                                  &r->out.info->info4, snum);
4626                 break;
4627         case 5:
4628                 result = construct_printer_info5(p->mem_ctx, ntprinter,
4629                                                  &r->out.info->info5, snum);
4630                 break;
4631         case 6:
4632                 result = construct_printer_info6(p->mem_ctx, ntprinter,
4633                                                  &r->out.info->info6, snum);
4634                 break;
4635         case 7:
4636                 result = construct_printer_info7(p->mem_ctx, Printer,
4637                                                  &r->out.info->info7, snum);
4638                 break;
4639         default:
4640                 result = WERR_UNKNOWN_LEVEL;
4641                 break;
4642         }
4643
4644         free_a_printer(&ntprinter, 2);
4645
4646         if (!W_ERROR_IS_OK(result)) {
4647                 TALLOC_FREE(r->out.info);
4648                 return result;
4649         }
4650
4651         *r->out.needed  = SPOOLSS_BUFFER_UNION(spoolss_PrinterInfo, NULL,
4652                                                r->out.info, r->in.level);
4653         r->out.info     = SPOOLSS_BUFFER_OK(r->out.info, NULL);
4654
4655         return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
4656 }
4657
4658 /********************************************************************
4659  ********************************************************************/
4660
4661 static const char **string_array_from_driver_info(TALLOC_CTX *mem_ctx,
4662                                                   fstring *fstring_array,
4663                                                   const char *cservername)
4664 {
4665         int i, num_strings = 0;
4666         const char **array = NULL;
4667
4668         for (i=0; fstring_array && fstring_array[i][0] != '\0'; i++) {
4669
4670                 const char *str = talloc_asprintf(mem_ctx, "\\\\%s%s",
4671                                                   cservername, fstring_array[i]);
4672                 if (!str) {
4673                         TALLOC_FREE(array);
4674                         return NULL;
4675                 }
4676
4677
4678                 if (!add_string_to_array(mem_ctx, str, &array, &num_strings)) {
4679                         TALLOC_FREE(array);
4680                         return NULL;
4681                 }
4682         }
4683
4684         if (i > 0) {
4685                 ADD_TO_ARRAY(mem_ctx, const char *, NULL,
4686                              &array, &num_strings);
4687         }
4688
4689         return array;
4690 }
4691
4692 /********************************************************************
4693  * fill a spoolss_DriverInfo1 struct
4694  ********************************************************************/
4695
4696 static WERROR fill_printer_driver_info1(TALLOC_CTX *mem_ctx,
4697                                         struct spoolss_DriverInfo1 *r,
4698                                         const NT_PRINTER_DRIVER_INFO_LEVEL *driver,
4699                                         const char *servername,
4700                                         const char *architecture)
4701 {
4702         r->driver_name          = talloc_strdup(mem_ctx, driver->info_3->name);
4703         W_ERROR_HAVE_NO_MEMORY(r->driver_name);
4704
4705         return WERR_OK;
4706 }
4707
4708 /********************************************************************
4709  * fill a spoolss_DriverInfo2 struct
4710  ********************************************************************/
4711
4712 static WERROR fill_printer_driver_info2(TALLOC_CTX *mem_ctx,
4713                                         struct spoolss_DriverInfo2 *r,
4714                                         const NT_PRINTER_DRIVER_INFO_LEVEL *driver,
4715                                         const char *servername)
4716
4717 {
4718         const char *cservername = canon_servername(servername);
4719
4720         r->version              = driver->info_3->cversion;
4721
4722         r->driver_name          = talloc_strdup(mem_ctx, driver->info_3->name);
4723         W_ERROR_HAVE_NO_MEMORY(r->driver_name);
4724         r->architecture         = talloc_strdup(mem_ctx, driver->info_3->environment);
4725         W_ERROR_HAVE_NO_MEMORY(r->architecture);
4726
4727         if (strlen(driver->info_3->driverpath)) {
4728                 r->driver_path  = talloc_asprintf(mem_ctx, "\\\\%s%s",
4729                                 cservername, driver->info_3->driverpath);
4730         } else {
4731                 r->driver_path  = talloc_strdup(mem_ctx, "");
4732         }
4733         W_ERROR_HAVE_NO_MEMORY(r->driver_path);
4734
4735         if (strlen(driver->info_3->datafile)) {
4736                 r->data_file    = talloc_asprintf(mem_ctx, "\\\\%s%s",
4737                                 cservername, driver->info_3->datafile);
4738         } else {
4739                 r->data_file    = talloc_strdup(mem_ctx, "");
4740         }
4741         W_ERROR_HAVE_NO_MEMORY(r->data_file);
4742
4743         if (strlen(driver->info_3->configfile)) {
4744                 r->config_file  = talloc_asprintf(mem_ctx, "\\\\%s%s",
4745                                 cservername, driver->info_3->configfile);
4746         } else {
4747                 r->config_file  = talloc_strdup(mem_ctx, "");
4748         }
4749         W_ERROR_HAVE_NO_MEMORY(r->config_file);
4750
4751         return WERR_OK;
4752 }
4753
4754 /********************************************************************
4755  * fill a spoolss_DriverInfo3 struct
4756  ********************************************************************/
4757
4758 static WERROR fill_printer_driver_info3(TALLOC_CTX *mem_ctx,
4759                                         struct spoolss_DriverInfo3 *r,
4760                                         const NT_PRINTER_DRIVER_INFO_LEVEL *driver,
4761                                         const char *servername)
4762 {
4763         const char *cservername = canon_servername(servername);
4764
4765         r->version              = driver->info_3->cversion;
4766
4767         r->driver_name          = talloc_strdup(mem_ctx, driver->info_3->name);
4768         W_ERROR_HAVE_NO_MEMORY(r->driver_name);
4769         r->architecture         = talloc_strdup(mem_ctx, driver->info_3->environment);
4770         W_ERROR_HAVE_NO_MEMORY(r->architecture);
4771
4772         if (strlen(driver->info_3->driverpath)) {
4773                 r->driver_path  = talloc_asprintf(mem_ctx, "\\\\%s%s",
4774                                 cservername, driver->info_3->driverpath);
4775         } else {
4776                 r->driver_path  = talloc_strdup(mem_ctx, "");
4777         }
4778         W_ERROR_HAVE_NO_MEMORY(r->driver_path);
4779
4780         if (strlen(driver->info_3->datafile)) {
4781                 r->data_file    = talloc_asprintf(mem_ctx, "\\\\%s%s",
4782                                 cservername, driver->info_3->datafile);
4783         } else {
4784                 r->data_file    = talloc_strdup(mem_ctx, "");
4785         }
4786         W_ERROR_HAVE_NO_MEMORY(r->data_file);
4787
4788         if (strlen(driver->info_3->configfile)) {
4789                 r->config_file  = talloc_asprintf(mem_ctx, "\\\\%s%s",
4790                                 cservername, driver->info_3->configfile);
4791         } else {
4792                 r->config_file  = talloc_strdup(mem_ctx, "");
4793         }
4794         W_ERROR_HAVE_NO_MEMORY(r->config_file);
4795
4796         if (strlen(driver->info_3->helpfile)) {
4797                 r->help_file    = talloc_asprintf(mem_ctx, "\\\\%s%s",
4798                                 cservername, driver->info_3->helpfile);
4799         } else {
4800                 r->help_file    = talloc_strdup(mem_ctx, "");
4801         }
4802         W_ERROR_HAVE_NO_MEMORY(r->config_file);
4803
4804         r->monitor_name         = talloc_strdup(mem_ctx, driver->info_3->monitorname);
4805         W_ERROR_HAVE_NO_MEMORY(r->monitor_name);
4806         r->default_datatype     = talloc_strdup(mem_ctx, driver->info_3->defaultdatatype);
4807         W_ERROR_HAVE_NO_MEMORY(r->default_datatype);
4808
4809         r->dependent_files = string_array_from_driver_info(mem_ctx,
4810                                                            driver->info_3->dependentfiles,
4811                                                            cservername);
4812         return WERR_OK;
4813 }
4814
4815 /********************************************************************
4816  * fill a spoolss_DriverInfo6 struct
4817  ********************************************************************/
4818
4819 static WERROR fill_printer_driver_info6(TALLOC_CTX *mem_ctx,
4820                                         struct spoolss_DriverInfo6 *r,
4821                                         const NT_PRINTER_DRIVER_INFO_LEVEL *driver,
4822                                         const char *servername)
4823 {
4824         const char *cservername = canon_servername(servername);
4825
4826         r->version              = driver->info_3->cversion;
4827
4828         r->driver_name          = talloc_strdup(mem_ctx, driver->info_3->name);
4829         W_ERROR_HAVE_NO_MEMORY(r->driver_name);
4830         r->architecture         = talloc_strdup(mem_ctx, driver->info_3->environment);
4831         W_ERROR_HAVE_NO_MEMORY(r->architecture);
4832
4833         if (strlen(driver->info_3->driverpath)) {
4834                 r->driver_path  = talloc_asprintf(mem_ctx, "\\\\%s%s",
4835                                 cservername, driver->info_3->driverpath);
4836         } else {
4837                 r->driver_path  = talloc_strdup(mem_ctx, "");
4838         }
4839         W_ERROR_HAVE_NO_MEMORY(r->driver_path);
4840
4841         if (strlen(driver->info_3->datafile)) {
4842                 r->data_file    = talloc_asprintf(mem_ctx, "\\\\%s%s",
4843                                 cservername, driver->info_3->datafile);
4844         } else {
4845                 r->data_file    = talloc_strdup(mem_ctx, "");
4846         }
4847         W_ERROR_HAVE_NO_MEMORY(r->data_file);
4848
4849         if (strlen(driver->info_3->configfile)) {
4850                 r->config_file  = talloc_asprintf(mem_ctx, "\\\\%s%s",
4851                                 cservername, driver->info_3->configfile);
4852         } else {
4853                 r->config_file  = talloc_strdup(mem_ctx, "");
4854         }
4855         W_ERROR_HAVE_NO_MEMORY(r->config_file);
4856
4857         if (strlen(driver->info_3->helpfile)) {
4858                 r->help_file    = talloc_asprintf(mem_ctx, "\\\\%s%s",
4859                                 cservername, driver->info_3->helpfile);
4860         } else {
4861                 r->help_file    = talloc_strdup(mem_ctx, "");
4862         }
4863         W_ERROR_HAVE_NO_MEMORY(r->config_file);
4864
4865         r->monitor_name         = talloc_strdup(mem_ctx, driver->info_3->monitorname);
4866         W_ERROR_HAVE_NO_MEMORY(r->monitor_name);
4867         r->default_datatype     = talloc_strdup(mem_ctx, driver->info_3->defaultdatatype);
4868         W_ERROR_HAVE_NO_MEMORY(r->default_datatype);
4869
4870         r->dependent_files = string_array_from_driver_info(mem_ctx,
4871                                                            driver->info_3->dependentfiles,
4872                                                            cservername);
4873         r->previous_names = string_array_from_driver_info(mem_ctx,
4874                                                           NULL,
4875                                                           cservername);
4876
4877         r->driver_date          = 0;
4878         r->driver_version       = 0;
4879
4880         r->manufacturer_name    = talloc_strdup(mem_ctx, "");
4881         W_ERROR_HAVE_NO_MEMORY(r->manufacturer_name);
4882         r->manufacturer_url     = talloc_strdup(mem_ctx, "");
4883         W_ERROR_HAVE_NO_MEMORY(r->manufacturer_url);
4884         r->hardware_id          = talloc_strdup(mem_ctx, "");
4885         W_ERROR_HAVE_NO_MEMORY(r->hardware_id);
4886         r->provider             = talloc_strdup(mem_ctx, "");
4887         W_ERROR_HAVE_NO_MEMORY(r->provider);
4888
4889         return WERR_OK;
4890 }
4891
4892 /********************************************************************
4893  ********************************************************************/
4894
4895 static WERROR fill_spoolss_DriverFileInfo(TALLOC_CTX *mem_ctx,
4896                                           struct spoolss_DriverFileInfo *r,
4897                                           const char *cservername,
4898                                           const char *file_name,
4899                                           enum spoolss_DriverFileType file_type,
4900                                           uint32_t file_version)
4901 {
4902         r->file_name    = talloc_asprintf(mem_ctx, "\\\\%s%s",
4903                                           cservername, file_name);
4904         W_ERROR_HAVE_NO_MEMORY(r->file_name);
4905         r->file_type    = file_type;
4906         r->file_version = file_version;
4907
4908         return WERR_OK;
4909 }
4910
4911 /********************************************************************
4912  ********************************************************************/
4913
4914 static WERROR spoolss_DriverFileInfo_from_driver(TALLOC_CTX *mem_ctx,
4915                                                  const NT_PRINTER_DRIVER_INFO_LEVEL *driver,
4916                                                  const char *cservername,
4917                                                  struct spoolss_DriverFileInfo **info_p,
4918                                                  uint32_t *count_p)
4919 {
4920         struct spoolss_DriverFileInfo *info = NULL;
4921         uint32_t count = 0;
4922         WERROR result;
4923         uint32_t i;
4924
4925         *info_p = NULL;
4926         *count_p = 0;
4927
4928         if (strlen(driver->info_3->driverpath)) {
4929                 info = TALLOC_REALLOC_ARRAY(mem_ctx, info,
4930                                             struct spoolss_DriverFileInfo,
4931                                             count + 1);
4932                 W_ERROR_HAVE_NO_MEMORY(info);
4933                 result = fill_spoolss_DriverFileInfo(info,
4934                                                      &info[count],
4935                                                      cservername,
4936                                                      driver->info_3->driverpath,
4937                                                      SPOOLSS_DRIVER_FILE_TYPE_RENDERING,
4938                                                      0);
4939                 W_ERROR_NOT_OK_RETURN(result);
4940                 count++;
4941         }
4942
4943         if (strlen(driver->info_3->configfile)) {
4944                 info = TALLOC_REALLOC_ARRAY(mem_ctx, info,
4945                                             struct spoolss_DriverFileInfo,
4946                                             count + 1);
4947                 W_ERROR_HAVE_NO_MEMORY(info);
4948                 result = fill_spoolss_DriverFileInfo(info,
4949                                                      &info[count],
4950                                                      cservername,
4951                                                      driver->info_3->configfile,
4952                                                      SPOOLSS_DRIVER_FILE_TYPE_CONFIGURATION,
4953                                                      0);
4954                 W_ERROR_NOT_OK_RETURN(result);
4955                 count++;
4956         }
4957
4958         if (strlen(driver->info_3->datafile)) {
4959                 info = TALLOC_REALLOC_ARRAY(mem_ctx, info,
4960                                             struct spoolss_DriverFileInfo,
4961                                             count + 1);
4962                 W_ERROR_HAVE_NO_MEMORY(info);
4963                 result = fill_spoolss_DriverFileInfo(info,
4964                                                      &info[count],
4965                                                      cservername,
4966                                                      driver->info_3->datafile,
4967                                                      SPOOLSS_DRIVER_FILE_TYPE_DATA,
4968                                                      0);
4969                 W_ERROR_NOT_OK_RETURN(result);
4970                 count++;
4971         }
4972
4973         if (strlen(driver->info_3->helpfile)) {
4974                 info = TALLOC_REALLOC_ARRAY(mem_ctx, info,
4975                                             struct spoolss_DriverFileInfo,
4976                                             count + 1);
4977                 W_ERROR_HAVE_NO_MEMORY(info);
4978                 result = fill_spoolss_DriverFileInfo(info,
4979                                                      &info[count],
4980                                                      cservername,
4981                                                      driver->info_3->helpfile,
4982                                                      SPOOLSS_DRIVER_FILE_TYPE_HELP,
4983                                                      0);
4984                 W_ERROR_NOT_OK_RETURN(result);
4985                 count++;
4986         }
4987
4988         for (i=0; driver->info_3->dependentfiles[i][0] != '\0'; i++) {
4989                 info = TALLOC_REALLOC_ARRAY(mem_ctx, info,
4990                                             struct spoolss_DriverFileInfo,
4991                                             count + 1);
4992                 W_ERROR_HAVE_NO_MEMORY(info);
4993                 result = fill_spoolss_DriverFileInfo(info,
4994                                                      &info[count],
4995                                                      cservername,
4996                                                      driver->info_3->dependentfiles[i],
4997                                                      SPOOLSS_DRIVER_FILE_TYPE_OTHER,
4998                                                      0);
4999                 W_ERROR_NOT_OK_RETURN(result);
5000                 count++;
5001         }
5002
5003         *info_p = info;
5004         *count_p = count;
5005
5006         return WERR_OK;
5007 }
5008
5009 /********************************************************************
5010  * fill a spoolss_DriverInfo101 sttruct
5011  ********************************************************************/
5012
5013 static WERROR fill_printer_driver_info101(TALLOC_CTX *mem_ctx,
5014                                           struct spoolss_DriverInfo101 *r,
5015                                           const NT_PRINTER_DRIVER_INFO_LEVEL *driver,
5016                                           const char *servername)
5017 {
5018         const char *cservername = canon_servername(servername);
5019         WERROR result;
5020
5021         r->version              = driver->info_3->cversion;
5022
5023         r->driver_name          = talloc_strdup(mem_ctx, driver->info_3->name);
5024         W_ERROR_HAVE_NO_MEMORY(r->driver_name);
5025         r->architecture         = talloc_strdup(mem_ctx, driver->info_3->environment);
5026         W_ERROR_HAVE_NO_MEMORY(r->architecture);
5027
5028         result = spoolss_DriverFileInfo_from_driver(mem_ctx, driver,
5029                                                     cservername,
5030                                                     &r->file_info,
5031                                                     &r->file_count);
5032         if (!W_ERROR_IS_OK(result)) {
5033                 return result;
5034         }
5035
5036         r->monitor_name         = talloc_strdup(mem_ctx, driver->info_3->monitorname);
5037         W_ERROR_HAVE_NO_MEMORY(r->monitor_name);
5038
5039         r->default_datatype     = talloc_strdup(mem_ctx, driver->info_3->defaultdatatype);
5040         W_ERROR_HAVE_NO_MEMORY(r->default_datatype);
5041
5042         r->previous_names = string_array_from_driver_info(mem_ctx,
5043                                                           NULL,
5044                                                           cservername);
5045         r->driver_date          = 0;
5046         r->driver_version       = 0;
5047
5048         r->manufacturer_name    = talloc_strdup(mem_ctx, "");
5049         W_ERROR_HAVE_NO_MEMORY(r->manufacturer_name);
5050         r->manufacturer_url     = talloc_strdup(mem_ctx, "");
5051         W_ERROR_HAVE_NO_MEMORY(r->manufacturer_url);
5052         r->hardware_id          = talloc_strdup(mem_ctx, "");
5053         W_ERROR_HAVE_NO_MEMORY(r->hardware_id);
5054         r->provider             = talloc_strdup(mem_ctx, "");
5055         W_ERROR_HAVE_NO_MEMORY(r->provider);
5056
5057         return WERR_OK;
5058 }
5059
5060 /********************************************************************
5061  * construct_printer_driver_info_1
5062  ********************************************************************/
5063
5064 static WERROR construct_printer_driver_info_1(TALLOC_CTX *mem_ctx,
5065                                               struct spoolss_DriverInfo1 *r,
5066                                               int snum,
5067                                               const char *servername,
5068                                               const char *architecture,
5069                                               uint32_t version)
5070 {
5071         NT_PRINTER_INFO_LEVEL *printer = NULL;
5072         NT_PRINTER_DRIVER_INFO_LEVEL driver;
5073         WERROR result;
5074
5075         ZERO_STRUCT(driver);
5076
5077         if (!W_ERROR_IS_OK(get_a_printer(NULL, &printer, 2, lp_const_servicename(snum))))
5078                 return WERR_INVALID_PRINTER_NAME;
5079
5080         if (!W_ERROR_IS_OK(get_a_printer_driver(&driver, 3, printer->info_2->drivername, architecture, version))) {
5081                 free_a_printer(&printer, 2);
5082                 return WERR_UNKNOWN_PRINTER_DRIVER;
5083         }
5084
5085         result = fill_printer_driver_info1(mem_ctx, r, &driver, servername, architecture);
5086
5087         free_a_printer(&printer,2);
5088
5089         return result;
5090 }
5091
5092 /********************************************************************
5093  * construct_printer_driver_info_2
5094  * fill a printer_info_2 struct
5095  ********************************************************************/
5096
5097 static WERROR construct_printer_driver_info_2(TALLOC_CTX *mem_ctx,
5098                                               struct spoolss_DriverInfo2 *r,
5099                                               int snum,
5100                                               const char *servername,
5101                                               const char *architecture,
5102                                               uint32_t version)
5103 {
5104         NT_PRINTER_INFO_LEVEL *printer = NULL;
5105         NT_PRINTER_DRIVER_INFO_LEVEL driver;
5106         WERROR result;
5107
5108         ZERO_STRUCT(printer);
5109         ZERO_STRUCT(driver);
5110
5111         if (!W_ERROR_IS_OK(get_a_printer(NULL, &printer, 2, lp_const_servicename(snum))))
5112                 return WERR_INVALID_PRINTER_NAME;
5113
5114         if (!W_ERROR_IS_OK(get_a_printer_driver(&driver, 3, printer->info_2->drivername, architecture, version))) {
5115                 free_a_printer(&printer, 2);
5116                 return WERR_UNKNOWN_PRINTER_DRIVER;
5117         }
5118
5119         result = fill_printer_driver_info2(mem_ctx, r, &driver, servername);
5120
5121         free_a_printer(&printer,2);
5122
5123         return result;
5124 }
5125
5126 /********************************************************************
5127  * construct_printer_info_3
5128  * fill a printer_info_3 struct
5129  ********************************************************************/
5130
5131 static WERROR construct_printer_driver_info_3(TALLOC_CTX *mem_ctx,
5132                                               struct spoolss_DriverInfo3 *r,
5133                                               int snum,
5134                                               const char *servername,
5135                                               const char *architecture,
5136                                               uint32_t version)
5137 {
5138         NT_PRINTER_INFO_LEVEL *printer = NULL;
5139         NT_PRINTER_DRIVER_INFO_LEVEL driver;
5140         WERROR status;
5141         ZERO_STRUCT(driver);
5142
5143         status=get_a_printer(NULL, &printer, 2, lp_const_servicename(snum) );
5144         DEBUG(8,("construct_printer_driver_info_3: status: %s\n", win_errstr(status)));
5145         if (!W_ERROR_IS_OK(status))
5146                 return WERR_INVALID_PRINTER_NAME;
5147
5148         status=get_a_printer_driver(&driver, 3, printer->info_2->drivername, architecture, version);
5149         DEBUG(8,("construct_printer_driver_info_3: status: %s\n", win_errstr(status)));
5150
5151 #if 0   /* JERRY */
5152
5153         /*
5154          * I put this code in during testing.  Helpful when commenting out the
5155          * support for DRIVER_INFO_6 in regards to win2k.  Not needed in general
5156          * as win2k always queries the driver using an infor level of 6.
5157          * I've left it in (but ifdef'd out) because I'll probably
5158          * use it in experimentation again in the future.   --jerry 22/01/2002
5159          */
5160
5161         if (!W_ERROR_IS_OK(status)) {
5162                 /*
5163                  * Is this a W2k client ?
5164                  */
5165                 if (version == 3) {
5166                         /* Yes - try again with a WinNT driver. */
5167                         version = 2;
5168                         status=get_a_printer_driver(&driver, 3, printer->info_2->drivername, architecture, version);
5169                         DEBUG(8,("construct_printer_driver_info_3: status: %s\n", win_errstr(status)));
5170                 }
5171 #endif
5172
5173                 if (!W_ERROR_IS_OK(status)) {
5174                         free_a_printer(&printer,2);
5175                         return WERR_UNKNOWN_PRINTER_DRIVER;
5176                 }
5177
5178 #if 0   /* JERRY */
5179         }
5180 #endif
5181
5182
5183         status = fill_printer_driver_info3(mem_ctx, r, &driver, servername);
5184
5185         free_a_printer(&printer,2);
5186
5187         return status;
5188 }
5189
5190 /********************************************************************
5191  * construct_printer_info_6
5192  * fill a printer_info_6 struct
5193  ********************************************************************/
5194
5195 static WERROR construct_printer_driver_info_6(TALLOC_CTX *mem_ctx,
5196                                               struct spoolss_DriverInfo6 *r,
5197                                               int snum,
5198                                               const char *servername,
5199                                               const char *architecture,
5200                                               uint32_t version)
5201 {
5202         NT_PRINTER_INFO_LEVEL           *printer = NULL;
5203         NT_PRINTER_DRIVER_INFO_LEVEL    driver;
5204         WERROR                          status;
5205
5206         ZERO_STRUCT(driver);
5207
5208         status=get_a_printer(NULL, &printer, 2, lp_const_servicename(snum) );
5209
5210         DEBUG(8,("construct_printer_driver_info_6: status: %s\n", win_errstr(status)));
5211
5212         if (!W_ERROR_IS_OK(status))
5213                 return WERR_INVALID_PRINTER_NAME;
5214
5215         status = get_a_printer_driver(&driver, 3, printer->info_2->drivername, architecture, version);
5216
5217         DEBUG(8,("construct_printer_driver_info_6: status: %s\n", win_errstr(status)));
5218
5219         if (!W_ERROR_IS_OK(status))
5220         {
5221                 /*
5222                  * Is this a W2k client ?
5223                  */
5224
5225                 if (version < 3) {
5226                         free_a_printer(&printer,2);
5227                         return WERR_UNKNOWN_PRINTER_DRIVER;
5228                 }
5229
5230                 /* Yes - try again with a WinNT driver. */
5231                 version = 2;
5232                 status=get_a_printer_driver(&driver, 3, printer->info_2->drivername, architecture, version);
5233                 DEBUG(8,("construct_printer_driver_info_6: status: %s\n", win_errstr(status)));
5234                 if (!W_ERROR_IS_OK(status)) {
5235                         free_a_printer(&printer,2);
5236                         return WERR_UNKNOWN_PRINTER_DRIVER;
5237                 }
5238         }
5239
5240         status = fill_printer_driver_info6(mem_ctx, r, &driver, servername);
5241
5242         free_a_printer(&printer,2);
5243         free_a_printer_driver(driver, 3);
5244
5245         return status;
5246 }
5247
5248 /********************************************************************
5249  * construct_printer_info_101
5250  * fill a printer_info_101 struct
5251  ********************************************************************/
5252
5253 static WERROR construct_printer_driver_info_101(TALLOC_CTX *mem_ctx,
5254                                                 struct spoolss_DriverInfo101 *r,
5255                                                 int snum,
5256                                                 const char *servername,
5257                                                 const char *architecture,
5258                                                 uint32_t version)
5259 {
5260         NT_PRINTER_INFO_LEVEL           *printer = NULL;
5261         NT_PRINTER_DRIVER_INFO_LEVEL    driver;
5262         WERROR                          result;
5263
5264         ZERO_STRUCT(driver);
5265
5266         result = get_a_printer(NULL, &printer, 2, lp_const_servicename(snum));
5267
5268         DEBUG(8,("construct_printer_driver_info_101: status: %s\n",
5269                 win_errstr(result)));
5270
5271         if (!W_ERROR_IS_OK(result)) {
5272                 return WERR_INVALID_PRINTER_NAME;
5273         }
5274
5275         result = get_a_printer_driver(&driver, 3, printer->info_2->drivername,
5276                                       architecture, version);
5277
5278         DEBUG(8,("construct_printer_driver_info_101: status: %s\n",
5279                 win_errstr(result)));
5280
5281         if (!W_ERROR_IS_OK(result)) {
5282                 /*
5283                  * Is this a W2k client ?
5284                  */
5285
5286                 if (version < 3) {
5287                         free_a_printer(&printer, 2);
5288                         return WERR_UNKNOWN_PRINTER_DRIVER;
5289                 }
5290
5291                 /* Yes - try again with a WinNT driver. */
5292                 version = 2;
5293                 result = get_a_printer_driver(&driver, 3, printer->info_2->drivername,
5294                                               architecture, version);
5295                 DEBUG(8,("construct_printer_driver_info_6: status: %s\n",
5296                         win_errstr(result)));
5297                 if (!W_ERROR_IS_OK(result)) {
5298                         free_a_printer(&printer, 2);
5299                         return WERR_UNKNOWN_PRINTER_DRIVER;
5300                 }
5301         }
5302
5303         result = fill_printer_driver_info101(mem_ctx, r, &driver, servername);
5304
5305         free_a_printer(&printer, 2);
5306         free_a_printer_driver(driver, 3);
5307
5308         return result;
5309 }
5310
5311 /****************************************************************
5312  _spoolss_GetPrinterDriver2
5313 ****************************************************************/
5314
5315 WERROR _spoolss_GetPrinterDriver2(pipes_struct *p,
5316                                   struct spoolss_GetPrinterDriver2 *r)
5317 {
5318         Printer_entry *printer;
5319         WERROR result;
5320
5321         const char *servername;
5322         int snum;
5323
5324         /* that's an [in out] buffer */
5325
5326         if (!r->in.buffer && (r->in.offered != 0)) {
5327                 return WERR_INVALID_PARAM;
5328         }
5329
5330         DEBUG(4,("_spoolss_GetPrinterDriver2\n"));
5331
5332         if (!(printer = find_printer_index_by_hnd(p, r->in.handle))) {
5333                 DEBUG(0,("_spoolss_GetPrinterDriver2: invalid printer handle!\n"));
5334                 return WERR_INVALID_PRINTER_NAME;
5335         }
5336
5337         *r->out.needed = 0;
5338         *r->out.server_major_version = 0;
5339         *r->out.server_minor_version = 0;
5340
5341         servername = get_server_name(printer);
5342
5343         if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
5344                 return WERR_BADFID;
5345         }
5346
5347         switch (r->in.level) {
5348         case 1:
5349                 result = construct_printer_driver_info_1(p->mem_ctx,
5350                                                          &r->out.info->info1,
5351                                                          snum,
5352                                                          servername,
5353                                                          r->in.architecture,
5354                                                          r->in.client_major_version);
5355                 break;
5356         case 2:
5357                 result = construct_printer_driver_info_2(p->mem_ctx,
5358                                                          &r->out.info->info2,
5359                                                          snum,
5360                                                          servername,
5361                                                          r->in.architecture,
5362                                                          r->in.client_major_version);
5363                 break;
5364         case 3:
5365                 result = construct_printer_driver_info_3(p->mem_ctx,
5366                                                          &r->out.info->info3,
5367                                                          snum,
5368                                                          servername,
5369                                                          r->in.architecture,
5370                                                          r->in.client_major_version);
5371                 break;
5372         case 6:
5373                 result = construct_printer_driver_info_6(p->mem_ctx,
5374                                                          &r->out.info->info6,
5375                                                          snum,
5376                                                          servername,
5377                                                          r->in.architecture,
5378                                                          r->in.client_major_version);
5379                 break;
5380         case 101:
5381                 result = construct_printer_driver_info_101(p->mem_ctx,
5382                                                            &r->out.info->info101,
5383                                                            snum,
5384                                                            servername,
5385                                                            r->in.architecture,
5386                                                            r->in.client_major_version);
5387                 break;
5388         default:
5389                 result = WERR_UNKNOWN_LEVEL;
5390                 break;
5391         }
5392
5393         if (!W_ERROR_IS_OK(result)) {
5394                 TALLOC_FREE(r->out.info);
5395                 return result;
5396         }
5397
5398         *r->out.needed  = SPOOLSS_BUFFER_UNION(spoolss_DriverInfo, NULL,
5399                                                r->out.info, r->in.level);
5400         r->out.info     = SPOOLSS_BUFFER_OK(r->out.info, NULL);
5401
5402         return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
5403 }
5404
5405
5406 /****************************************************************
5407  _spoolss_StartPagePrinter
5408 ****************************************************************/
5409
5410 WERROR _spoolss_StartPagePrinter(pipes_struct *p,
5411                                  struct spoolss_StartPagePrinter *r)
5412 {
5413         Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
5414
5415         if (!Printer) {
5416                 DEBUG(3,("_spoolss_StartPagePrinter: "
5417                         "Error in startpageprinter printer handle\n"));
5418                 return WERR_BADFID;
5419         }
5420
5421         Printer->page_started = true;
5422         return WERR_OK;
5423 }
5424
5425 /****************************************************************
5426  _spoolss_EndPagePrinter
5427 ****************************************************************/
5428
5429 WERROR _spoolss_EndPagePrinter(pipes_struct *p,
5430                                struct spoolss_EndPagePrinter *r)
5431 {
5432         int snum;
5433
5434         Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
5435
5436         if (!Printer) {
5437                 DEBUG(2,("_spoolss_EndPagePrinter: Invalid handle (%s:%u:%u).\n",
5438                         OUR_HANDLE(r->in.handle)));
5439                 return WERR_BADFID;
5440         }
5441
5442         if (!get_printer_snum(p, r->in.handle, &snum, NULL))
5443                 return WERR_BADFID;
5444
5445         Printer->page_started = false;
5446         print_job_endpage(snum, Printer->jobid);
5447
5448         return WERR_OK;
5449 }
5450
5451 /****************************************************************
5452  _spoolss_StartDocPrinter
5453 ****************************************************************/
5454
5455 WERROR _spoolss_StartDocPrinter(pipes_struct *p,
5456                                 struct spoolss_StartDocPrinter *r)
5457 {
5458         struct spoolss_DocumentInfo1 *info_1;
5459         int snum;
5460         Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
5461
5462         if (!Printer) {
5463                 DEBUG(2,("_spoolss_StartDocPrinter: "
5464                         "Invalid handle (%s:%u:%u)\n",
5465                         OUR_HANDLE(r->in.handle)));
5466                 return WERR_BADFID;
5467         }
5468
5469         if (r->in.level != 1) {
5470                 return WERR_UNKNOWN_LEVEL;
5471         }
5472
5473         info_1 = r->in.info.info1;
5474
5475         /*
5476          * a nice thing with NT is it doesn't listen to what you tell it.
5477          * when asked to send _only_ RAW datas, it tries to send datas
5478          * in EMF format.
5479          *
5480          * So I add checks like in NT Server ...
5481          */
5482
5483         if (info_1->datatype) {
5484                 if (strcmp(info_1->datatype, "RAW") != 0) {
5485                         *r->out.job_id = 0;
5486                         return WERR_INVALID_DATATYPE;
5487                 }
5488         }
5489
5490         /* get the share number of the printer */
5491         if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
5492                 return WERR_BADFID;
5493         }
5494
5495         Printer->jobid = print_job_start(p->server_info, snum,
5496                                          info_1->document_name,
5497                                          Printer->nt_devmode);
5498
5499         /* An error occured in print_job_start() so return an appropriate
5500            NT error code. */
5501
5502         if (Printer->jobid == -1) {
5503                 return map_werror_from_unix(errno);
5504         }
5505
5506         Printer->document_started = true;
5507         *r->out.job_id = Printer->jobid;
5508
5509         return WERR_OK;
5510 }
5511
5512 /****************************************************************
5513  _spoolss_EndDocPrinter
5514 ****************************************************************/
5515
5516 WERROR _spoolss_EndDocPrinter(pipes_struct *p,
5517                               struct spoolss_EndDocPrinter *r)
5518 {
5519         return _spoolss_enddocprinter_internal(p, r->in.handle);
5520 }
5521
5522 /****************************************************************
5523  _spoolss_WritePrinter
5524 ****************************************************************/
5525
5526 WERROR _spoolss_WritePrinter(pipes_struct *p,
5527                              struct spoolss_WritePrinter *r)
5528 {
5529         uint32_t buffer_written;
5530         int snum;
5531         Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
5532
5533         if (!Printer) {
5534                 DEBUG(2,("_spoolss_WritePrinter: Invalid handle (%s:%u:%u)\n",
5535                         OUR_HANDLE(r->in.handle)));
5536                 *r->out.num_written = r->in._data_size;
5537                 return WERR_BADFID;
5538         }
5539
5540         if (!get_printer_snum(p, r->in.handle, &snum, NULL))
5541                 return WERR_BADFID;
5542
5543         buffer_written = (uint32_t)print_job_write(snum, Printer->jobid,
5544                                                    (const char *)r->in.data.data,
5545                                                    (SMB_OFF_T)-1,
5546                                                    (size_t)r->in._data_size);
5547         if (buffer_written == (uint32_t)-1) {
5548                 *r->out.num_written = 0;
5549                 if (errno == ENOSPC)
5550                         return WERR_NO_SPOOL_SPACE;
5551                 else
5552                         return WERR_ACCESS_DENIED;
5553         }
5554
5555         *r->out.num_written = r->in._data_size;
5556
5557         return WERR_OK;
5558 }
5559
5560 /********************************************************************
5561  * api_spoolss_getprinter
5562  * called from the spoolss dispatcher
5563  *
5564  ********************************************************************/
5565
5566 static WERROR control_printer(struct policy_handle *handle, uint32_t command,
5567                               pipes_struct *p)
5568 {
5569         int snum;
5570         WERROR errcode = WERR_BADFUNC;
5571         Printer_entry *Printer = find_printer_index_by_hnd(p, handle);
5572
5573         if (!Printer) {
5574                 DEBUG(2,("control_printer: Invalid handle (%s:%u:%u)\n",
5575                         OUR_HANDLE(handle)));
5576                 return WERR_BADFID;
5577         }
5578
5579         if (!get_printer_snum(p, handle, &snum, NULL))
5580                 return WERR_BADFID;
5581
5582         switch (command) {
5583         case SPOOLSS_PRINTER_CONTROL_PAUSE:
5584                 if (print_queue_pause(p->server_info, snum, &errcode)) {
5585                         errcode = WERR_OK;
5586                 }
5587                 break;
5588         case SPOOLSS_PRINTER_CONTROL_RESUME:
5589         case SPOOLSS_PRINTER_CONTROL_UNPAUSE:
5590                 if (print_queue_resume(p->server_info, snum, &errcode)) {
5591                         errcode = WERR_OK;
5592                 }
5593                 break;
5594         case SPOOLSS_PRINTER_CONTROL_PURGE:
5595                 if (print_queue_purge(p->server_info, snum, &errcode)) {
5596                         errcode = WERR_OK;
5597                 }
5598                 break;
5599         default:
5600                 return WERR_UNKNOWN_LEVEL;
5601         }
5602
5603         return errcode;
5604 }
5605
5606
5607 /****************************************************************
5608  _spoolss_AbortPrinter
5609  * From MSDN: "Deletes printer's spool file if printer is configured
5610  * for spooling"
5611 ****************************************************************/
5612
5613 WERROR _spoolss_AbortPrinter(pipes_struct *p,
5614                              struct spoolss_AbortPrinter *r)
5615 {
5616         Printer_entry   *Printer = find_printer_index_by_hnd(p, r->in.handle);
5617         int             snum;
5618         WERROR          errcode = WERR_OK;
5619
5620         if (!Printer) {
5621                 DEBUG(2,("_spoolss_AbortPrinter: Invalid handle (%s:%u:%u)\n",
5622                         OUR_HANDLE(r->in.handle)));
5623                 return WERR_BADFID;
5624         }
5625
5626         if (!get_printer_snum(p, r->in.handle, &snum, NULL))
5627                 return WERR_BADFID;
5628
5629         print_job_delete(p->server_info, snum, Printer->jobid, &errcode );
5630
5631         return errcode;
5632 }
5633
5634 /********************************************************************
5635  * called by spoolss_api_setprinter
5636  * when updating a printer description
5637  ********************************************************************/
5638
5639 static WERROR update_printer_sec(struct policy_handle *handle,
5640                                  pipes_struct *p, SEC_DESC_BUF *secdesc_ctr)
5641 {
5642         SEC_DESC_BUF *new_secdesc_ctr = NULL, *old_secdesc_ctr = NULL;
5643         WERROR result;
5644         int snum;
5645
5646         Printer_entry *Printer = find_printer_index_by_hnd(p, handle);
5647
5648         if (!Printer || !get_printer_snum(p, handle, &snum, NULL)) {
5649                 DEBUG(2,("update_printer_sec: Invalid handle (%s:%u:%u)\n",
5650                          OUR_HANDLE(handle)));
5651
5652                 result = WERR_BADFID;
5653                 goto done;
5654         }
5655
5656         if (!secdesc_ctr) {
5657                 DEBUG(10,("update_printer_sec: secdesc_ctr is NULL !\n"));
5658                 result = WERR_INVALID_PARAM;
5659                 goto done;
5660         }
5661
5662         /* Check the user has permissions to change the security
5663            descriptor.  By experimentation with two NT machines, the user
5664            requires Full Access to the printer to change security
5665            information. */
5666
5667         if ( Printer->access_granted != PRINTER_ACCESS_ADMINISTER ) {
5668                 DEBUG(4,("update_printer_sec: updated denied by printer permissions\n"));
5669                 result = WERR_ACCESS_DENIED;
5670                 goto done;
5671         }
5672
5673         /* NT seems to like setting the security descriptor even though
5674            nothing may have actually changed. */
5675
5676         if ( !nt_printing_getsec(p->mem_ctx, Printer->sharename, &old_secdesc_ctr)) {
5677                 DEBUG(2,("update_printer_sec: nt_printing_getsec() failed\n"));
5678                 result = WERR_BADFID;
5679                 goto done;
5680         }
5681
5682         if (DEBUGLEVEL >= 10) {
5683                 SEC_ACL *the_acl;
5684                 int i;
5685
5686                 the_acl = old_secdesc_ctr->sd->dacl;
5687                 DEBUG(10, ("old_secdesc_ctr for %s has %d aces:\n",
5688                            PRINTERNAME(snum), the_acl->num_aces));
5689
5690                 for (i = 0; i < the_acl->num_aces; i++) {
5691                         DEBUG(10, ("%s 0x%08x\n", sid_string_dbg(
5692                                            &the_acl->aces[i].trustee),
5693                                   the_acl->aces[i].access_mask));
5694                 }
5695
5696                 the_acl = secdesc_ctr->sd->dacl;
5697
5698                 if (the_acl) {
5699                         DEBUG(10, ("secdesc_ctr for %s has %d aces:\n",
5700                                    PRINTERNAME(snum), the_acl->num_aces));
5701
5702                         for (i = 0; i < the_acl->num_aces; i++) {
5703                                 DEBUG(10, ("%s 0x%08x\n", sid_string_dbg(
5704                                                    &the_acl->aces[i].trustee),
5705                                            the_acl->aces[i].access_mask));
5706                         }
5707                 } else {
5708                         DEBUG(10, ("dacl for secdesc_ctr is NULL\n"));
5709                 }
5710         }
5711
5712         new_secdesc_ctr = sec_desc_merge(p->mem_ctx, secdesc_ctr, old_secdesc_ctr);
5713         if (!new_secdesc_ctr) {
5714                 result = WERR_NOMEM;
5715                 goto done;
5716         }
5717
5718         if (sec_desc_equal(new_secdesc_ctr->sd, old_secdesc_ctr->sd)) {
5719                 result = WERR_OK;
5720                 goto done;
5721         }
5722
5723         result = nt_printing_setsec(Printer->sharename, new_secdesc_ctr);
5724
5725  done:
5726
5727         return result;
5728 }
5729
5730 /********************************************************************
5731  Canonicalize printer info from a client
5732
5733  ATTN: It does not matter what we set the servername to hear
5734  since we do the necessary work in get_a_printer() to set it to
5735  the correct value based on what the client sent in the
5736  _spoolss_open_printer_ex().
5737  ********************************************************************/
5738
5739 static bool check_printer_ok(NT_PRINTER_INFO_LEVEL_2 *info, int snum)
5740 {
5741         fstring printername;
5742         const char *p;
5743
5744         DEBUG(5,("check_printer_ok: servername=%s printername=%s sharename=%s "
5745                 "portname=%s drivername=%s comment=%s location=%s\n",
5746                 info->servername, info->printername, info->sharename,
5747                 info->portname, info->drivername, info->comment, info->location));
5748
5749         /* we force some elements to "correct" values */
5750         slprintf(info->servername, sizeof(info->servername)-1, "\\\\%s", global_myname());
5751         fstrcpy(info->sharename, lp_servicename(snum));
5752
5753         /* check to see if we allow printername != sharename */
5754
5755         if ( lp_force_printername(snum) ) {
5756                 slprintf(info->printername, sizeof(info->printername)-1, "\\\\%s\\%s",
5757                         global_myname(), info->sharename );
5758         } else {
5759
5760                 /* make sure printername is in \\server\printername format */
5761
5762                 fstrcpy( printername, info->printername );
5763                 p = printername;
5764                 if ( printername[0] == '\\' && printername[1] == '\\' ) {
5765                         if ( (p = strchr_m( &printername[2], '\\' )) != NULL )
5766                                 p++;
5767                 }
5768
5769                 slprintf(info->printername, sizeof(info->printername)-1, "\\\\%s\\%s",
5770                          global_myname(), p );
5771         }
5772
5773         info->attributes |= PRINTER_ATTRIBUTE_SAMBA;
5774         info->attributes &= ~PRINTER_ATTRIBUTE_NOT_SAMBA;
5775
5776
5777
5778         return true;
5779 }
5780
5781 /****************************************************************************
5782 ****************************************************************************/
5783
5784 WERROR add_port_hook(TALLOC_CTX *ctx, NT_USER_TOKEN *token, const char *portname, const char *uri )
5785 {
5786         char *cmd = lp_addport_cmd();
5787         char *command = NULL;
5788         int ret;
5789         SE_PRIV se_printop = SE_PRINT_OPERATOR;
5790         bool is_print_op = false;
5791
5792         if ( !*cmd ) {
5793                 return WERR_ACCESS_DENIED;
5794         }
5795
5796         command = talloc_asprintf(ctx,
5797                         "%s \"%s\" \"%s\"", cmd, portname, uri );
5798         if (!command) {
5799                 return WERR_NOMEM;
5800         }
5801
5802         if ( token )
5803                 is_print_op = user_has_privileges( token, &se_printop );
5804
5805         DEBUG(10,("Running [%s]\n", command));
5806
5807         /********* BEGIN SePrintOperatorPrivilege **********/
5808
5809         if ( is_print_op )
5810                 become_root();
5811
5812         ret = smbrun(command, NULL);
5813
5814         if ( is_print_op )
5815                 unbecome_root();
5816
5817         /********* END SePrintOperatorPrivilege **********/
5818
5819         DEBUGADD(10,("returned [%d]\n", ret));
5820
5821         TALLOC_FREE(command);
5822
5823         if ( ret != 0 ) {
5824                 return WERR_ACCESS_DENIED;
5825         }
5826
5827         return WERR_OK;
5828 }
5829
5830 /****************************************************************************
5831 ****************************************************************************/
5832
5833 bool add_printer_hook(TALLOC_CTX *ctx, NT_USER_TOKEN *token, NT_PRINTER_INFO_LEVEL *printer)
5834 {
5835         char *cmd = lp_addprinter_cmd();
5836         char **qlines;
5837         char *command = NULL;
5838         int numlines;
5839         int ret;
5840         int fd;
5841         SE_PRIV se_printop = SE_PRINT_OPERATOR;
5842         bool is_print_op = false;
5843         char *remote_machine = talloc_strdup(ctx, "%m");
5844
5845         if (!remote_machine) {
5846                 return false;
5847         }
5848         remote_machine = talloc_sub_basic(ctx,
5849                                 current_user_info.smb_name,
5850                                 current_user_info.domain,
5851                                 remote_machine);
5852         if (!remote_machine) {
5853                 return false;
5854         }
5855
5856         command = talloc_asprintf(ctx,
5857                         "%s \"%s\" \"%s\" \"%s\" \"%s\" \"%s\" \"%s\" \"%s\"",
5858                         cmd, printer->info_2->printername, printer->info_2->sharename,
5859                         printer->info_2->portname, printer->info_2->drivername,
5860                         printer->info_2->location, printer->info_2->comment, remote_machine);
5861         if (!command) {
5862                 return false;
5863         }
5864
5865         if ( token )
5866                 is_print_op = user_has_privileges( token, &se_printop );
5867
5868         DEBUG(10,("Running [%s]\n", command));
5869
5870         /********* BEGIN SePrintOperatorPrivilege **********/
5871
5872         if ( is_print_op )
5873                 become_root();
5874
5875         if ( (ret = smbrun(command, &fd)) == 0 ) {
5876                 /* Tell everyone we updated smb.conf. */
5877                 message_send_all(smbd_messaging_context(),
5878                                  MSG_SMB_CONF_UPDATED, NULL, 0, NULL);
5879         }
5880
5881         if ( is_print_op )
5882                 unbecome_root();
5883
5884         /********* END SePrintOperatorPrivilege **********/
5885
5886         DEBUGADD(10,("returned [%d]\n", ret));
5887
5888         TALLOC_FREE(command);
5889         TALLOC_FREE(remote_machine);
5890
5891         if ( ret != 0 ) {
5892                 if (fd != -1)
5893                         close(fd);
5894                 return false;
5895         }
5896
5897         /* reload our services immediately */
5898         reload_services(false);
5899
5900         numlines = 0;
5901         /* Get lines and convert them back to dos-codepage */
5902         qlines = fd_lines_load(fd, &numlines, 0, NULL);
5903         DEBUGADD(10,("Lines returned = [%d]\n", numlines));
5904         close(fd);
5905
5906         /* Set the portname to what the script says the portname should be. */
5907         /* but don't require anything to be return from the script exit a good error code */
5908
5909         if (numlines) {
5910                 /* Set the portname to what the script says the portname should be. */
5911                 strncpy(printer->info_2->portname, qlines[0], sizeof(printer->info_2->portname));
5912                 DEBUGADD(6,("Line[0] = [%s]\n", qlines[0]));
5913         }
5914
5915         TALLOC_FREE(qlines);
5916         return true;
5917 }
5918
5919
5920 /********************************************************************
5921  * Called by spoolss_api_setprinter
5922  * when updating a printer description.
5923  ********************************************************************/
5924
5925 static WERROR update_printer(pipes_struct *p, struct policy_handle *handle,
5926                              struct spoolss_SetPrinterInfoCtr *info_ctr,
5927                              struct spoolss_DeviceMode *devmode)
5928 {
5929         int snum;
5930         NT_PRINTER_INFO_LEVEL *printer = NULL, *old_printer = NULL;
5931         Printer_entry *Printer = find_printer_index_by_hnd(p, handle);
5932         WERROR result;
5933         UNISTR2 buffer;
5934         fstring asc_buffer;
5935
5936         DEBUG(8,("update_printer\n"));
5937
5938         result = WERR_OK;
5939
5940         if (!Printer) {
5941                 result = WERR_BADFID;
5942                 goto done;
5943         }
5944
5945         if (!get_printer_snum(p, handle, &snum, NULL)) {
5946                 result = WERR_BADFID;
5947                 goto done;
5948         }
5949
5950         if (!W_ERROR_IS_OK(get_a_printer(Printer, &printer, 2, lp_const_servicename(snum))) ||
5951             (!W_ERROR_IS_OK(get_a_printer(Printer, &old_printer, 2, lp_const_servicename(snum))))) {
5952                 result = WERR_BADFID;
5953                 goto done;
5954         }
5955
5956         DEBUGADD(8,("Converting info_2 struct\n"));
5957
5958         /*
5959          * convert_printer_info converts the incoming
5960          * info from the client and overwrites the info
5961          * just read from the tdb in the pointer 'printer'.
5962          */
5963
5964         if (!convert_printer_info(info_ctr, printer)) {
5965                 result =  WERR_NOMEM;
5966                 goto done;
5967         }
5968
5969         if (devmode) {
5970                 /* we have a valid devmode
5971                    convert it and link it*/
5972
5973                 DEBUGADD(8,("update_printer: Converting the devicemode struct\n"));
5974                 if (!convert_devicemode(printer->info_2->printername, devmode,
5975                                         &printer->info_2->devmode)) {
5976                         result =  WERR_NOMEM;
5977                         goto done;
5978                 }
5979         }
5980
5981         /* Do sanity check on the requested changes for Samba */
5982
5983         if (!check_printer_ok(printer->info_2, snum)) {
5984                 result = WERR_INVALID_PARAM;
5985                 goto done;
5986         }
5987
5988         /* FIXME!!! If the driver has changed we really should verify that
5989            it is installed before doing much else   --jerry */
5990
5991         /* Check calling user has permission to update printer description */
5992
5993         if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
5994                 DEBUG(3, ("update_printer: printer property change denied by handle\n"));
5995                 result = WERR_ACCESS_DENIED;
5996                 goto done;
5997         }
5998
5999         /* Call addprinter hook */
6000         /* Check changes to see if this is really needed */
6001
6002         if ( *lp_addprinter_cmd()
6003                 && (!strequal(printer->info_2->drivername, old_printer->info_2->drivername)
6004                         || !strequal(printer->info_2->comment, old_printer->info_2->comment)
6005                         || !strequal(printer->info_2->portname, old_printer->info_2->portname)
6006                         || !strequal(printer->info_2->location, old_printer->info_2->location)) )
6007         {
6008                 /* add_printer_hook() will call reload_services() */
6009
6010                 if ( !add_printer_hook(p->mem_ctx, p->server_info->ptok,
6011                                        printer) ) {
6012                         result = WERR_ACCESS_DENIED;
6013                         goto done;
6014                 }
6015         }
6016
6017         /*
6018          * When a *new* driver is bound to a printer, the drivername is used to
6019          * lookup previously saved driver initialization info, which is then
6020          * bound to the printer, simulating what happens in the Windows arch.
6021          */
6022         if (!strequal(printer->info_2->drivername, old_printer->info_2->drivername))
6023         {
6024                 if (!set_driver_init(printer, 2))
6025                 {
6026                         DEBUG(5,("update_printer: Error restoring driver initialization data for driver [%s]!\n",
6027                                 printer->info_2->drivername));
6028                 }
6029
6030                 DEBUG(10,("update_printer: changing driver [%s]!  Sending event!\n",
6031                         printer->info_2->drivername));
6032
6033                 notify_printer_driver(snum, printer->info_2->drivername);
6034         }
6035
6036         /*
6037          * flag which changes actually occured.  This is a small subset of
6038          * all the possible changes.  We also have to update things in the
6039          * DsSpooler key.
6040          */
6041
6042         if (!strequal(printer->info_2->comment, old_printer->info_2->comment)) {
6043                 init_unistr2( &buffer, printer->info_2->comment, UNI_STR_TERMINATE);
6044                 set_printer_dataex( printer, SPOOL_DSSPOOLER_KEY, "description",
6045                         REG_SZ, (uint8_t *)buffer.buffer, buffer.uni_str_len*2 );
6046
6047                 notify_printer_comment(snum, printer->info_2->comment);
6048         }
6049
6050         if (!strequal(printer->info_2->sharename, old_printer->info_2->sharename)) {
6051                 init_unistr2( &buffer, printer->info_2->sharename, UNI_STR_TERMINATE);
6052                 set_printer_dataex( printer, SPOOL_DSSPOOLER_KEY, "shareName",
6053                         REG_SZ, (uint8_t *)buffer.buffer, buffer.uni_str_len*2 );
6054
6055                 notify_printer_sharename(snum, printer->info_2->sharename);
6056         }
6057
6058         if (!strequal(printer->info_2->printername, old_printer->info_2->printername)) {
6059                 char *pname;
6060
6061                 if ( (pname = strchr_m( printer->info_2->printername+2, '\\' )) != NULL )
6062                         pname++;
6063                 else
6064                         pname = printer->info_2->printername;
6065
6066
6067                 init_unistr2( &buffer, pname, UNI_STR_TERMINATE);
6068                 set_printer_dataex( printer, SPOOL_DSSPOOLER_KEY, "printerName",
6069                         REG_SZ, (uint8_t *)buffer.buffer, buffer.uni_str_len*2 );
6070
6071                 notify_printer_printername( snum, pname );
6072         }
6073
6074         if (!strequal(printer->info_2->portname, old_printer->info_2->portname)) {
6075                 init_unistr2( &buffer, printer->info_2->portname, UNI_STR_TERMINATE);
6076                 set_printer_dataex( printer, SPOOL_DSSPOOLER_KEY, "portName",
6077                         REG_SZ, (uint8_t *)buffer.buffer, buffer.uni_str_len*2 );
6078
6079                 notify_printer_port(snum, printer->info_2->portname);
6080         }
6081
6082         if (!strequal(printer->info_2->location, old_printer->info_2->location)) {
6083                 init_unistr2( &buffer, printer->info_2->location, UNI_STR_TERMINATE);
6084                 set_printer_dataex( printer, SPOOL_DSSPOOLER_KEY, "location",
6085                         REG_SZ, (uint8_t *)buffer.buffer, buffer.uni_str_len*2 );
6086
6087                 notify_printer_location(snum, printer->info_2->location);
6088         }
6089
6090         /* here we need to update some more DsSpooler keys */
6091         /* uNCName, serverName, shortServerName */
6092
6093         init_unistr2( &buffer, global_myname(), UNI_STR_TERMINATE);
6094         set_printer_dataex( printer, SPOOL_DSSPOOLER_KEY, "serverName",
6095                 REG_SZ, (uint8_t *)buffer.buffer, buffer.uni_str_len*2 );
6096         set_printer_dataex( printer, SPOOL_DSSPOOLER_KEY, "shortServerName",
6097                 REG_SZ, (uint8_t *)buffer.buffer, buffer.uni_str_len*2 );
6098
6099         slprintf( asc_buffer, sizeof(asc_buffer)-1, "\\\\%s\\%s",
6100                  global_myname(), printer->info_2->sharename );
6101         init_unistr2( &buffer, asc_buffer, UNI_STR_TERMINATE);
6102         set_printer_dataex( printer, SPOOL_DSSPOOLER_KEY, "uNCName",
6103                 REG_SZ, (uint8_t *)buffer.buffer, buffer.uni_str_len*2 );
6104
6105         /* Update printer info */
6106         result = mod_a_printer(printer, 2);
6107
6108 done:
6109         free_a_printer(&printer, 2);
6110         free_a_printer(&old_printer, 2);
6111
6112
6113         return result;
6114 }
6115
6116 /****************************************************************************
6117 ****************************************************************************/
6118 static WERROR publish_or_unpublish_printer(pipes_struct *p,
6119                                            struct policy_handle *handle,
6120                                            struct spoolss_SetPrinterInfo7 *info7)
6121 {
6122 #ifdef HAVE_ADS
6123         int snum;
6124         Printer_entry *Printer;
6125
6126         if ( lp_security() != SEC_ADS ) {
6127                 return WERR_UNKNOWN_LEVEL;
6128         }
6129
6130         Printer = find_printer_index_by_hnd(p, handle);
6131
6132         DEBUG(5,("publish_or_unpublish_printer, action = %d\n",info7->action));
6133
6134         if (!Printer)
6135                 return WERR_BADFID;
6136
6137         if (!get_printer_snum(p, handle, &snum, NULL))
6138                 return WERR_BADFID;
6139
6140         nt_printer_publish(Printer, snum, info7->action);
6141
6142         return WERR_OK;
6143 #else
6144         return WERR_UNKNOWN_LEVEL;
6145 #endif
6146 }
6147
6148 /****************************************************************
6149  _spoolss_SetPrinter
6150 ****************************************************************/
6151
6152 WERROR _spoolss_SetPrinter(pipes_struct *p,
6153                            struct spoolss_SetPrinter *r)
6154 {
6155         WERROR result;
6156
6157         Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
6158
6159         if (!Printer) {
6160                 DEBUG(2,("_spoolss_SetPrinter: Invalid handle (%s:%u:%u)\n",
6161                         OUR_HANDLE(r->in.handle)));
6162                 return WERR_BADFID;
6163         }
6164
6165         /* check the level */
6166         switch (r->in.info_ctr->level) {
6167                 case 0:
6168                         return control_printer(r->in.handle, r->in.command, p);
6169                 case 2:
6170                         result = update_printer(p, r->in.handle,
6171                                                 r->in.info_ctr,
6172                                                 r->in.devmode_ctr->devmode);
6173                         if (!W_ERROR_IS_OK(result))
6174                                 return result;
6175                         if (r->in.secdesc_ctr->sd)
6176                                 result = update_printer_sec(r->in.handle, p,
6177                                                             r->in.secdesc_ctr);
6178                         return result;
6179                 case 3:
6180                         return update_printer_sec(r->in.handle, p,
6181                                                   r->in.secdesc_ctr);
6182                 case 7:
6183                         return publish_or_unpublish_printer(p, r->in.handle,
6184                                                             r->in.info_ctr->info.info7);
6185                 default:
6186                         return WERR_UNKNOWN_LEVEL;
6187         }
6188 }
6189
6190 /****************************************************************
6191  _spoolss_FindClosePrinterNotify
6192 ****************************************************************/
6193
6194 WERROR _spoolss_FindClosePrinterNotify(pipes_struct *p,
6195                                        struct spoolss_FindClosePrinterNotify *r)
6196 {
6197         Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
6198
6199         if (!Printer) {
6200                 DEBUG(2,("_spoolss_FindClosePrinterNotify: "
6201                         "Invalid handle (%s:%u:%u)\n", OUR_HANDLE(r->in.handle)));
6202                 return WERR_BADFID;
6203         }
6204
6205         if (Printer->notify.client_connected == true) {
6206                 int snum = -1;
6207
6208                 if ( Printer->printer_type == SPLHND_SERVER)
6209                         snum = -1;
6210                 else if ( (Printer->printer_type == SPLHND_PRINTER) &&
6211                                 !get_printer_snum(p, r->in.handle, &snum, NULL) )
6212                         return WERR_BADFID;
6213
6214                 srv_spoolss_replycloseprinter(snum, &Printer->notify.client_hnd);
6215         }
6216
6217         Printer->notify.flags=0;
6218         Printer->notify.options=0;
6219         Printer->notify.localmachine[0]='\0';
6220         Printer->notify.printerlocal=0;
6221         TALLOC_FREE(Printer->notify.option);
6222         Printer->notify.client_connected = false;
6223
6224         return WERR_OK;
6225 }
6226
6227 /****************************************************************
6228  _spoolss_AddJob
6229 ****************************************************************/
6230
6231 WERROR _spoolss_AddJob(pipes_struct *p,
6232                        struct spoolss_AddJob *r)
6233 {
6234         if (!r->in.buffer && (r->in.offered != 0)) {
6235                 return WERR_INVALID_PARAM;
6236         }
6237
6238         /* this is what a NT server returns for AddJob. AddJob must fail on
6239          * non-local printers */
6240
6241         if (r->in.level != 1) {
6242                 return WERR_UNKNOWN_LEVEL;
6243         }
6244
6245         return WERR_INVALID_PARAM;
6246 }
6247
6248 /****************************************************************************
6249 fill_job_info1
6250 ****************************************************************************/
6251
6252 static WERROR fill_job_info1(TALLOC_CTX *mem_ctx,
6253                              struct spoolss_JobInfo1 *r,
6254                              const print_queue_struct *queue,
6255                              int position, int snum,
6256                              const NT_PRINTER_INFO_LEVEL *ntprinter)
6257 {
6258         struct tm *t;
6259
6260         t = gmtime(&queue->time);
6261
6262         r->job_id               = queue->job;
6263
6264         r->printer_name         = talloc_strdup(mem_ctx, lp_servicename(snum));
6265         W_ERROR_HAVE_NO_MEMORY(r->printer_name);
6266         r->server_name          = talloc_strdup(mem_ctx, ntprinter->info_2->servername);
6267         W_ERROR_HAVE_NO_MEMORY(r->server_name);
6268         r->user_name            = talloc_strdup(mem_ctx, queue->fs_user);
6269         W_ERROR_HAVE_NO_MEMORY(r->user_name);
6270         r->document_name        = talloc_strdup(mem_ctx, queue->fs_file);
6271         W_ERROR_HAVE_NO_MEMORY(r->document_name);
6272         r->data_type            = talloc_strdup(mem_ctx, "RAW");
6273         W_ERROR_HAVE_NO_MEMORY(r->data_type);
6274         r->text_status          = talloc_strdup(mem_ctx, "");
6275         W_ERROR_HAVE_NO_MEMORY(r->text_status);
6276
6277         r->status               = nt_printj_status(queue->status);
6278         r->priority             = queue->priority;
6279         r->position             = position;
6280         r->total_pages          = queue->page_count;
6281         r->pages_printed        = 0; /* ??? */
6282
6283         init_systemtime(&r->submitted, t);
6284
6285         return WERR_OK;
6286 }
6287
6288 /****************************************************************************
6289 fill_job_info2
6290 ****************************************************************************/
6291
6292 static WERROR fill_job_info2(TALLOC_CTX *mem_ctx,
6293                              struct spoolss_JobInfo2 *r,
6294                              const print_queue_struct *queue,
6295                              int position, int snum,
6296                              const NT_PRINTER_INFO_LEVEL *ntprinter,
6297                              struct spoolss_DeviceMode *devmode)
6298 {
6299         struct tm *t;
6300
6301         t = gmtime(&queue->time);
6302
6303         r->job_id               = queue->job;
6304
6305         r->printer_name         = talloc_strdup(mem_ctx, lp_servicename(snum));
6306         W_ERROR_HAVE_NO_MEMORY(r->printer_name);
6307         r->server_name          = talloc_strdup(mem_ctx, ntprinter->info_2->servername);
6308         W_ERROR_HAVE_NO_MEMORY(r->server_name);
6309         r->user_name            = talloc_strdup(mem_ctx, queue->fs_user);
6310         W_ERROR_HAVE_NO_MEMORY(r->user_name);
6311         r->document_name        = talloc_strdup(mem_ctx, queue->fs_file);
6312         W_ERROR_HAVE_NO_MEMORY(r->document_name);
6313         r->notify_name          = talloc_strdup(mem_ctx, queue->fs_user);
6314         W_ERROR_HAVE_NO_MEMORY(r->notify_name);
6315         r->data_type            = talloc_strdup(mem_ctx, "RAW");
6316         W_ERROR_HAVE_NO_MEMORY(r->data_type);
6317         r->print_processor      = talloc_strdup(mem_ctx, "winprint");
6318         W_ERROR_HAVE_NO_MEMORY(r->print_processor);
6319         r->parameters           = talloc_strdup(mem_ctx, "");
6320         W_ERROR_HAVE_NO_MEMORY(r->parameters);
6321         r->driver_name          = talloc_strdup(mem_ctx, ntprinter->info_2->drivername);
6322         W_ERROR_HAVE_NO_MEMORY(r->driver_name);
6323
6324         r->devmode              = devmode;
6325
6326         r->text_status          = talloc_strdup(mem_ctx, "");
6327         W_ERROR_HAVE_NO_MEMORY(r->text_status);
6328
6329         r->secdesc              = NULL;
6330
6331         r->status               = nt_printj_status(queue->status);
6332         r->priority             = queue->priority;
6333         r->position             = position;
6334         r->start_time           = 0;
6335         r->until_time           = 0;
6336         r->total_pages          = queue->page_count;
6337         r->size                 = queue->size;
6338         init_systemtime(&r->submitted, t);
6339         r->time                 = 0;
6340         r->pages_printed        = 0; /* ??? */
6341
6342         return WERR_OK;
6343 }
6344
6345 /****************************************************************************
6346  Enumjobs at level 1.
6347 ****************************************************************************/
6348
6349 static WERROR enumjobs_level1(TALLOC_CTX *mem_ctx,
6350                               const print_queue_struct *queue,
6351                               uint32_t num_queues, int snum,
6352                               const NT_PRINTER_INFO_LEVEL *ntprinter,
6353                               union spoolss_JobInfo **info_p,
6354                               uint32_t *count)
6355 {
6356         union spoolss_JobInfo *info;
6357         int i;
6358         WERROR result = WERR_OK;
6359
6360         info = TALLOC_ARRAY(mem_ctx, union spoolss_JobInfo, num_queues);
6361         W_ERROR_HAVE_NO_MEMORY(info);
6362
6363         *count = num_queues;
6364
6365         for (i=0; i<*count; i++) {
6366                 result = fill_job_info1(info,
6367                                         &info[i].info1,
6368                                         &queue[i],
6369                                         i,
6370                                         snum,
6371                                         ntprinter);
6372                 if (!W_ERROR_IS_OK(result)) {
6373                         goto out;
6374                 }
6375         }
6376
6377  out:
6378         if (!W_ERROR_IS_OK(result)) {
6379                 TALLOC_FREE(info);
6380                 *count = 0;
6381                 return result;
6382         }
6383
6384         *info_p = info;
6385
6386         return WERR_OK;
6387 }
6388
6389 /****************************************************************************
6390  Enumjobs at level 2.
6391 ****************************************************************************/
6392
6393 static WERROR enumjobs_level2(TALLOC_CTX *mem_ctx,
6394                               const print_queue_struct *queue,
6395                               uint32_t num_queues, int snum,
6396                               const NT_PRINTER_INFO_LEVEL *ntprinter,
6397                               union spoolss_JobInfo **info_p,
6398                               uint32_t *count)
6399 {
6400         union spoolss_JobInfo *info;
6401         int i;
6402         WERROR result = WERR_OK;
6403
6404         info = TALLOC_ARRAY(mem_ctx, union spoolss_JobInfo, num_queues);
6405         W_ERROR_HAVE_NO_MEMORY(info);
6406
6407         *count = num_queues;
6408
6409         for (i=0; i<*count; i++) {
6410
6411                 struct spoolss_DeviceMode *devmode;
6412
6413                 devmode = construct_dev_mode(info, lp_const_servicename(snum));
6414                 if (!devmode) {
6415                         result = WERR_NOMEM;
6416                         goto out;
6417                 }
6418
6419                 result = fill_job_info2(info,
6420                                         &info[i].info2,
6421                                         &queue[i],
6422                                         i,
6423                                         snum,
6424                                         ntprinter,
6425                                         devmode);
6426                 if (!W_ERROR_IS_OK(result)) {
6427                         goto out;
6428                 }
6429         }
6430
6431  out:
6432         if (!W_ERROR_IS_OK(result)) {
6433                 TALLOC_FREE(info);
6434                 *count = 0;
6435                 return result;
6436         }
6437
6438         *info_p = info;
6439
6440         return WERR_OK;
6441 }
6442
6443 /****************************************************************
6444  _spoolss_EnumJobs
6445 ****************************************************************/
6446
6447 WERROR _spoolss_EnumJobs(pipes_struct *p,
6448                          struct spoolss_EnumJobs *r)
6449 {
6450         WERROR result;
6451         NT_PRINTER_INFO_LEVEL *ntprinter = NULL;
6452         int snum;
6453         print_status_struct prt_status;
6454         print_queue_struct *queue = NULL;
6455         uint32_t count;
6456
6457         /* that's an [in out] buffer */
6458
6459         if (!r->in.buffer && (r->in.offered != 0)) {
6460                 return WERR_INVALID_PARAM;
6461         }
6462
6463         DEBUG(4,("_spoolss_EnumJobs\n"));
6464
6465         *r->out.needed = 0;
6466         *r->out.count = 0;
6467         *r->out.info = NULL;
6468
6469         /* lookup the printer snum and tdb entry */
6470
6471         if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
6472                 return WERR_BADFID;
6473         }
6474
6475         result = get_a_printer(NULL, &ntprinter, 2, lp_servicename(snum));
6476         if (!W_ERROR_IS_OK(result)) {
6477                 return result;
6478         }
6479
6480         count = print_queue_status(snum, &queue, &prt_status);
6481         DEBUGADD(4,("count:[%d], status:[%d], [%s]\n",
6482                 count, prt_status.status, prt_status.message));
6483
6484         if (count == 0) {
6485                 SAFE_FREE(queue);
6486                 free_a_printer(&ntprinter, 2);
6487                 return WERR_OK;
6488         }
6489
6490         switch (r->in.level) {
6491         case 1:
6492                 result = enumjobs_level1(p->mem_ctx, queue, count, snum,
6493                                          ntprinter, r->out.info, r->out.count);
6494                 break;
6495         case 2:
6496                 result = enumjobs_level2(p->mem_ctx, queue, count, snum,
6497                                          ntprinter, r->out.info, r->out.count);
6498                 break;
6499         default:
6500                 result = WERR_UNKNOWN_LEVEL;
6501                 break;
6502         }
6503
6504         SAFE_FREE(queue);
6505         free_a_printer(&ntprinter, 2);
6506
6507         if (!W_ERROR_IS_OK(result)) {
6508                 return result;
6509         }
6510
6511         *r->out.needed  = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
6512                                                      spoolss_EnumJobs, NULL,
6513                                                      *r->out.info, r->in.level,
6514                                                      *r->out.count);
6515         *r->out.info    = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
6516         *r->out.count   = SPOOLSS_BUFFER_OK(*r->out.count, 0);
6517
6518         return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
6519 }
6520
6521 /****************************************************************
6522  _spoolss_ScheduleJob
6523 ****************************************************************/
6524
6525 WERROR _spoolss_ScheduleJob(pipes_struct *p,
6526                             struct spoolss_ScheduleJob *r)
6527 {
6528         return WERR_OK;
6529 }
6530
6531 /****************************************************************
6532  _spoolss_SetJob
6533 ****************************************************************/
6534
6535 WERROR _spoolss_SetJob(pipes_struct *p,
6536                        struct spoolss_SetJob *r)
6537 {
6538         int snum;
6539         WERROR errcode = WERR_BADFUNC;
6540
6541         if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
6542                 return WERR_BADFID;
6543         }
6544
6545         if (!print_job_exists(lp_const_servicename(snum), r->in.job_id)) {
6546                 return WERR_INVALID_PRINTER_NAME;
6547         }
6548
6549         switch (r->in.command) {
6550         case SPOOLSS_JOB_CONTROL_CANCEL:
6551         case SPOOLSS_JOB_CONTROL_DELETE:
6552                 if (print_job_delete(p->server_info, snum, r->in.job_id, &errcode)) {
6553                         errcode = WERR_OK;
6554                 }
6555                 break;
6556         case SPOOLSS_JOB_CONTROL_PAUSE:
6557                 if (print_job_pause(p->server_info, snum, r->in.job_id, &errcode)) {
6558                         errcode = WERR_OK;
6559                 }
6560                 break;
6561         case SPOOLSS_JOB_CONTROL_RESTART:
6562         case SPOOLSS_JOB_CONTROL_RESUME:
6563                 if (print_job_resume(p->server_info, snum, r->in.job_id, &errcode)) {
6564                         errcode = WERR_OK;
6565                 }
6566                 break;
6567         default:
6568                 return WERR_UNKNOWN_LEVEL;
6569         }
6570
6571         return errcode;
6572 }
6573
6574 /****************************************************************************
6575  Enumerates all printer drivers at level 1.
6576 ****************************************************************************/
6577
6578 static WERROR enumprinterdrivers_level1(TALLOC_CTX *mem_ctx,
6579                                         const char *servername,
6580                                         const char *architecture,
6581                                         union spoolss_DriverInfo **info_p,
6582                                         uint32_t *count)
6583 {
6584         int i;
6585         int ndrivers;
6586         uint32_t version;
6587         fstring *list = NULL;
6588         NT_PRINTER_DRIVER_INFO_LEVEL driver;
6589         union spoolss_DriverInfo *info = NULL;
6590         WERROR result = WERR_OK;
6591
6592         *count = 0;
6593
6594         for (version=0; version<DRIVER_MAX_VERSION; version++) {
6595                 list = NULL;
6596                 ndrivers = get_ntdrivers(&list, architecture, version);
6597                 DEBUGADD(4,("we have:[%d] drivers in environment [%s] and version [%d]\n",
6598                         ndrivers, architecture, version));
6599
6600                 if (ndrivers == -1) {
6601                         result = WERR_NOMEM;
6602                         goto out;
6603                 }
6604
6605                 if (ndrivers != 0) {
6606                         info = TALLOC_REALLOC_ARRAY(mem_ctx, info,
6607                                                     union spoolss_DriverInfo,
6608                                                     *count + ndrivers);
6609                         if (!info) {
6610                                 DEBUG(0,("enumprinterdrivers_level1: "
6611                                         "failed to enlarge driver info buffer!\n"));
6612                                 result = WERR_NOMEM;
6613                                 goto out;
6614                         }
6615                 }
6616
6617                 for (i=0; i<ndrivers; i++) {
6618                         DEBUGADD(5,("\tdriver: [%s]\n", list[i]));
6619                         ZERO_STRUCT(driver);
6620                         result = get_a_printer_driver(&driver, 3, list[i],
6621                                                       architecture, version);
6622                         if (!W_ERROR_IS_OK(result)) {
6623                                 goto out;
6624                         }
6625                         result = fill_printer_driver_info1(info, &info[*count+i].info1,
6626                                                            &driver, servername,
6627                                                            architecture);
6628                         if (!W_ERROR_IS_OK(result)) {
6629                                 free_a_printer_driver(driver, 3);
6630                                 goto out;
6631                         }
6632                         free_a_printer_driver(driver, 3);
6633                 }
6634
6635                 *count += ndrivers;
6636                 SAFE_FREE(list);
6637         }
6638
6639  out:
6640         SAFE_FREE(list);
6641
6642         if (!W_ERROR_IS_OK(result)) {
6643                 TALLOC_FREE(info);
6644                 *count = 0;
6645                 return result;
6646         }
6647
6648         *info_p = info;
6649
6650         return WERR_OK;
6651 }
6652
6653 /****************************************************************************
6654  Enumerates all printer drivers at level 2.
6655 ****************************************************************************/
6656
6657 static WERROR enumprinterdrivers_level2(TALLOC_CTX *mem_ctx,
6658                                         const char *servername,
6659                                         const char *architecture,
6660                                         union spoolss_DriverInfo **info_p,
6661                                         uint32_t *count)
6662 {
6663         int i;
6664         int ndrivers;
6665         uint32_t version;
6666         fstring *list = NULL;
6667         NT_PRINTER_DRIVER_INFO_LEVEL driver;
6668         union spoolss_DriverInfo *info = NULL;
6669         WERROR result = WERR_OK;
6670
6671         *count = 0;
6672
6673         for (version=0; version<DRIVER_MAX_VERSION; version++) {
6674                 list = NULL;
6675                 ndrivers = get_ntdrivers(&list, architecture, version);
6676                 DEBUGADD(4,("we have:[%d] drivers in environment [%s] and version [%d]\n",
6677                         ndrivers, architecture, version));
6678
6679                 if (ndrivers == -1) {
6680                         result = WERR_NOMEM;
6681                         goto out;
6682                 }
6683
6684                 if (ndrivers != 0) {
6685                         info = TALLOC_REALLOC_ARRAY(mem_ctx, info,
6686                                                     union spoolss_DriverInfo,
6687                                                     *count + ndrivers);
6688                         if (!info) {
6689                                 DEBUG(0,("enumprinterdrivers_level2: "
6690                                         "failed to enlarge driver info buffer!\n"));
6691                                 result = WERR_NOMEM;
6692                                 goto out;
6693                         }
6694                 }
6695
6696                 for (i=0; i<ndrivers; i++) {
6697                         DEBUGADD(5,("\tdriver: [%s]\n", list[i]));
6698                         ZERO_STRUCT(driver);
6699                         result = get_a_printer_driver(&driver, 3, list[i],
6700                                                       architecture, version);
6701                         if (!W_ERROR_IS_OK(result)) {
6702                                 goto out;
6703                         }
6704                         result = fill_printer_driver_info2(info, &info[*count+i].info2,
6705                                                            &driver, servername);
6706                         if (!W_ERROR_IS_OK(result)) {
6707                                 free_a_printer_driver(driver, 3);
6708                                 goto out;
6709                         }
6710                         free_a_printer_driver(driver, 3);
6711                 }
6712
6713                 *count += ndrivers;
6714                 SAFE_FREE(list);
6715         }
6716
6717  out:
6718         SAFE_FREE(list);
6719
6720         if (!W_ERROR_IS_OK(result)) {
6721                 TALLOC_FREE(info);
6722                 *count = 0;
6723                 return result;
6724         }
6725
6726         *info_p = info;
6727
6728         return WERR_OK;
6729 }
6730
6731 /****************************************************************************
6732  Enumerates all printer drivers at level 3.
6733 ****************************************************************************/
6734
6735 static WERROR enumprinterdrivers_level3(TALLOC_CTX *mem_ctx,
6736                                         const char *servername,
6737                                         const char *architecture,
6738                                         union spoolss_DriverInfo **info_p,
6739                                         uint32_t *count)
6740 {
6741         int i;
6742         int ndrivers;
6743         uint32_t version;
6744         fstring *list = NULL;
6745         union spoolss_DriverInfo *info = NULL;
6746         NT_PRINTER_DRIVER_INFO_LEVEL driver;
6747         WERROR result = WERR_OK;
6748
6749         *count = 0;
6750
6751         for (version=0; version<DRIVER_MAX_VERSION; version++) {
6752                 list = NULL;
6753                 ndrivers = get_ntdrivers(&list, architecture, version);
6754                 DEBUGADD(4,("we have:[%d] drivers in environment [%s] and version [%d]\n",
6755                         ndrivers, architecture, version));
6756
6757                 if (ndrivers == -1) {
6758                         result = WERR_NOMEM;
6759                         goto out;
6760                 }
6761
6762                 if (ndrivers != 0) {
6763                         info = TALLOC_REALLOC_ARRAY(mem_ctx, info,
6764                                                     union spoolss_DriverInfo,
6765                                                     *count + ndrivers);
6766                         if (!info) {
6767                                 DEBUG(0,("enumprinterdrivers_level3: "
6768                                         "failed to enlarge driver info buffer!\n"));
6769                                 result = WERR_NOMEM;
6770                                 goto out;
6771                         }
6772                 }
6773
6774                 for (i=0; i<ndrivers; i++) {
6775                         DEBUGADD(5,("\tdriver: [%s]\n", list[i]));
6776                         ZERO_STRUCT(driver);
6777                         result = get_a_printer_driver(&driver, 3, list[i],
6778                                                       architecture, version);
6779                         if (!W_ERROR_IS_OK(result)) {
6780                                 goto out;
6781                         }
6782                         result = fill_printer_driver_info3(info, &info[*count+i].info3,
6783                                                            &driver, servername);
6784                         if (!W_ERROR_IS_OK(result)) {
6785                                 free_a_printer_driver(driver, 3);
6786                                 goto out;
6787                         }
6788
6789                         free_a_printer_driver(driver, 3);
6790                 }
6791
6792                 *count += ndrivers;
6793                 SAFE_FREE(list);
6794         }
6795
6796  out:
6797         SAFE_FREE(list);
6798
6799         if (!W_ERROR_IS_OK(result)) {
6800                 TALLOC_FREE(info);
6801                 *count = 0;
6802                 return result;
6803         }
6804
6805         *info_p = info;
6806
6807         return WERR_OK;
6808 }
6809
6810 /****************************************************************
6811  _spoolss_EnumPrinterDrivers
6812 ****************************************************************/
6813
6814 WERROR _spoolss_EnumPrinterDrivers(pipes_struct *p,
6815                                    struct spoolss_EnumPrinterDrivers *r)
6816 {
6817         const char *cservername;
6818         WERROR result;
6819
6820         /* that's an [in out] buffer */
6821
6822         if (!r->in.buffer && (r->in.offered != 0)) {
6823                 return WERR_INVALID_PARAM;
6824         }
6825
6826         DEBUG(4,("_spoolss_EnumPrinterDrivers\n"));
6827
6828         *r->out.needed = 0;
6829         *r->out.count = 0;
6830         *r->out.info = NULL;
6831
6832         cservername = canon_servername(r->in.server);
6833
6834         if (!is_myname_or_ipaddr(cservername)) {
6835                 return WERR_UNKNOWN_PRINTER_DRIVER;
6836         }
6837
6838         switch (r->in.level) {
6839         case 1:
6840                 result = enumprinterdrivers_level1(p->mem_ctx, cservername,
6841                                                    r->in.environment,
6842                                                    r->out.info, r->out.count);
6843                 break;
6844         case 2:
6845                 result = enumprinterdrivers_level2(p->mem_ctx, cservername,
6846                                                    r->in.environment,
6847                                                    r->out.info, r->out.count);
6848                 break;
6849         case 3:
6850                 result = enumprinterdrivers_level3(p->mem_ctx, cservername,
6851                                                    r->in.environment,
6852                                                    r->out.info, r->out.count);
6853                 break;
6854         default:
6855                 return WERR_UNKNOWN_LEVEL;
6856         }
6857
6858         if (!W_ERROR_IS_OK(result)) {
6859                 return result;
6860         }
6861
6862         *r->out.needed  = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
6863                                                      spoolss_EnumPrinterDrivers, NULL,
6864                                                      *r->out.info, r->in.level,
6865                                                      *r->out.count);
6866         *r->out.info    = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
6867         *r->out.count   = SPOOLSS_BUFFER_OK(*r->out.count, 0);
6868
6869         return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
6870 }
6871
6872 /****************************************************************************
6873 ****************************************************************************/
6874
6875 static WERROR fill_form_info_1(TALLOC_CTX *mem_ctx,
6876                                struct spoolss_FormInfo1 *r,
6877                                const nt_forms_struct *form)
6878 {
6879         r->form_name    = talloc_strdup(mem_ctx, form->name);
6880         W_ERROR_HAVE_NO_MEMORY(r->form_name);
6881
6882         r->flags        = form->flag;
6883         r->size.width   = form->width;
6884         r->size.height  = form->length;
6885         r->area.left    = form->left;
6886         r->area.top     = form->top;
6887         r->area.right   = form->right;
6888         r->area.bottom  = form->bottom;
6889
6890         return WERR_OK;
6891 }
6892
6893 /****************************************************************
6894  spoolss_enumforms_level1
6895 ****************************************************************/
6896
6897 static WERROR spoolss_enumforms_level1(TALLOC_CTX *mem_ctx,
6898                                        const nt_forms_struct *builtin_forms,
6899                                        uint32_t num_builtin_forms,
6900                                        const nt_forms_struct *user_forms,
6901                                        uint32_t num_user_forms,
6902                                        union spoolss_FormInfo **info_p,
6903                                        uint32_t *count)
6904 {
6905         union spoolss_FormInfo *info;
6906         WERROR result = WERR_OK;
6907         int i;
6908
6909         *count = num_builtin_forms + num_user_forms;
6910
6911         info = TALLOC_ARRAY(mem_ctx, union spoolss_FormInfo, *count);
6912         W_ERROR_HAVE_NO_MEMORY(info);
6913
6914         /* construct the list of form structures */
6915         for (i=0; i<num_builtin_forms; i++) {
6916                 DEBUGADD(6,("Filling form number [%d]\n",i));
6917                 result = fill_form_info_1(info, &info[i].info1,
6918                                           &builtin_forms[i]);
6919                 if (!W_ERROR_IS_OK(result)) {
6920                         goto out;
6921                 }
6922         }
6923
6924         for (; i<num_user_forms; i++) {
6925                 DEBUGADD(6,("Filling form number [%d]\n",i));
6926                 result = fill_form_info_1(info, &info[i].info1,
6927                                           &user_forms[i-num_builtin_forms]);
6928                 if (!W_ERROR_IS_OK(result)) {
6929                         goto out;
6930                 }
6931         }
6932
6933  out:
6934         if (!W_ERROR_IS_OK(result)) {
6935                 TALLOC_FREE(info);
6936                 *count = 0;
6937                 return result;
6938         }
6939
6940         *info_p = info;
6941
6942         return WERR_OK;
6943 }
6944
6945 /****************************************************************
6946  _spoolss_EnumForms
6947 ****************************************************************/
6948
6949 WERROR _spoolss_EnumForms(pipes_struct *p,
6950                           struct spoolss_EnumForms *r)
6951 {
6952         WERROR result;
6953         nt_forms_struct *user_forms = NULL;
6954         nt_forms_struct *builtin_forms = NULL;
6955         uint32_t num_user_forms;
6956         uint32_t num_builtin_forms;
6957
6958         *r->out.count = 0;
6959         *r->out.needed = 0;
6960         *r->out.info = NULL;
6961
6962         /* that's an [in out] buffer */
6963
6964         if (!r->in.buffer && (r->in.offered != 0) ) {
6965                 return WERR_INVALID_PARAM;
6966         }
6967
6968         DEBUG(4,("_spoolss_EnumForms\n"));
6969         DEBUGADD(5,("Offered buffer size [%d]\n", r->in.offered));
6970         DEBUGADD(5,("Info level [%d]\n",          r->in.level));
6971
6972         num_builtin_forms = get_builtin_ntforms(&builtin_forms);
6973         DEBUGADD(5,("Number of builtin forms [%d]\n", num_builtin_forms));
6974         num_user_forms = get_ntforms(&user_forms);
6975         DEBUGADD(5,("Number of user forms [%d]\n", num_user_forms));
6976
6977         if (num_user_forms + num_builtin_forms == 0) {
6978                 SAFE_FREE(builtin_forms);
6979                 SAFE_FREE(user_forms);
6980                 return WERR_NO_MORE_ITEMS;
6981         }
6982
6983         switch (r->in.level) {
6984         case 1:
6985                 result = spoolss_enumforms_level1(p->mem_ctx,
6986                                                   builtin_forms,
6987                                                   num_builtin_forms,
6988                                                   user_forms,
6989                                                   num_user_forms,
6990                                                   r->out.info,
6991                                                   r->out.count);
6992                 break;
6993         default:
6994                 result = WERR_UNKNOWN_LEVEL;
6995                 break;
6996         }
6997
6998         SAFE_FREE(user_forms);
6999         SAFE_FREE(builtin_forms);
7000
7001         if (!W_ERROR_IS_OK(result)) {
7002                 return result;
7003         }
7004
7005         *r->out.needed  = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
7006                                                      spoolss_EnumForms, NULL,
7007                                                      *r->out.info, r->in.level,
7008                                                      *r->out.count);
7009         *r->out.info    = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
7010         *r->out.count   = SPOOLSS_BUFFER_OK(*r->out.count, 0);
7011
7012         return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
7013 }
7014
7015 /****************************************************************
7016 ****************************************************************/
7017
7018 static WERROR find_form_byname(const char *name,
7019                                nt_forms_struct *form)
7020 {
7021         nt_forms_struct *list = NULL;
7022         int num_forms = 0, i = 0;
7023
7024         if (get_a_builtin_ntform_by_string(name, form)) {
7025                 return WERR_OK;
7026         }
7027
7028         num_forms = get_ntforms(&list);
7029         DEBUGADD(5,("Number of forms [%d]\n", num_forms));
7030
7031         if (num_forms == 0) {
7032                 return WERR_BADFID;
7033         }
7034
7035         /* Check if the requested name is in the list of form structures */
7036         for (i = 0; i < num_forms; i++) {
7037
7038                 DEBUG(4,("checking form %s (want %s)\n", list[i].name, name));
7039
7040                 if (strequal(name, list[i].name)) {
7041                         DEBUGADD(6,("Found form %s number [%d]\n", name, i));
7042                         *form = list[i];
7043                         SAFE_FREE(list);
7044                         return WERR_OK;
7045                 }
7046         }
7047
7048         SAFE_FREE(list);
7049
7050         return WERR_BADFID;
7051 }
7052
7053 /****************************************************************
7054  _spoolss_GetForm
7055 ****************************************************************/
7056
7057 WERROR _spoolss_GetForm(pipes_struct *p,
7058                         struct spoolss_GetForm *r)
7059 {
7060         WERROR result;
7061         nt_forms_struct form;
7062
7063         /* that's an [in out] buffer */
7064
7065         if (!r->in.buffer && (r->in.offered != 0)) {
7066                 return WERR_INVALID_PARAM;
7067         }
7068
7069         DEBUG(4,("_spoolss_GetForm\n"));
7070         DEBUGADD(5,("Offered buffer size [%d]\n", r->in.offered));
7071         DEBUGADD(5,("Info level [%d]\n",          r->in.level));
7072
7073         result = find_form_byname(r->in.form_name, &form);
7074         if (!W_ERROR_IS_OK(result)) {
7075                 TALLOC_FREE(r->out.info);
7076                 return result;
7077         }
7078
7079         switch (r->in.level) {
7080         case 1:
7081                 result = fill_form_info_1(p->mem_ctx,
7082                                           &r->out.info->info1,
7083                                           &form);
7084                 break;
7085
7086         default:
7087                 result = WERR_UNKNOWN_LEVEL;
7088                 break;
7089         }
7090
7091         if (!W_ERROR_IS_OK(result)) {
7092                 TALLOC_FREE(r->out.info);
7093                 return result;
7094         }
7095
7096         *r->out.needed  = SPOOLSS_BUFFER_UNION(spoolss_FormInfo, NULL,
7097                                                r->out.info, r->in.level);
7098         r->out.info     = SPOOLSS_BUFFER_OK(r->out.info, NULL);
7099
7100         return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
7101 }
7102
7103 /****************************************************************************
7104 ****************************************************************************/
7105
7106 static WERROR fill_port_1(TALLOC_CTX *mem_ctx,
7107                           struct spoolss_PortInfo1 *r,
7108                           const char *name)
7109 {
7110         r->port_name = talloc_strdup(mem_ctx, name);
7111         W_ERROR_HAVE_NO_MEMORY(r->port_name);
7112
7113         return WERR_OK;
7114 }
7115
7116 /****************************************************************************
7117  TODO: This probably needs distinguish between TCP/IP and Local ports
7118  somehow.
7119 ****************************************************************************/
7120
7121 static WERROR fill_port_2(TALLOC_CTX *mem_ctx,
7122                           struct spoolss_PortInfo2 *r,
7123                           const char *name)
7124 {
7125         r->port_name = talloc_strdup(mem_ctx, name);
7126         W_ERROR_HAVE_NO_MEMORY(r->port_name);
7127
7128         r->monitor_name = talloc_strdup(mem_ctx, "Local Monitor");
7129         W_ERROR_HAVE_NO_MEMORY(r->monitor_name);
7130
7131         r->description = talloc_strdup(mem_ctx, SPL_LOCAL_PORT);
7132         W_ERROR_HAVE_NO_MEMORY(r->description);
7133
7134         r->port_type = SPOOLSS_PORT_TYPE_WRITE;
7135         r->reserved = 0;
7136
7137         return WERR_OK;
7138 }
7139
7140
7141 /****************************************************************************
7142  wrapper around the enumer ports command
7143 ****************************************************************************/
7144
7145 WERROR enumports_hook(TALLOC_CTX *ctx, int *count, char ***lines )
7146 {
7147         char *cmd = lp_enumports_cmd();
7148         char **qlines = NULL;
7149         char *command = NULL;
7150         int numlines;
7151         int ret;
7152         int fd;
7153
7154         *count = 0;
7155         *lines = NULL;
7156
7157         /* if no hook then just fill in the default port */
7158
7159         if ( !*cmd ) {
7160                 if (!(qlines = TALLOC_ARRAY( NULL, char*, 2 ))) {
7161                         return WERR_NOMEM;
7162                 }
7163                 if (!(qlines[0] = talloc_strdup(qlines, SAMBA_PRINTER_PORT_NAME ))) {
7164                         TALLOC_FREE(qlines);
7165                         return WERR_NOMEM;
7166                 }
7167                 qlines[1] = NULL;
7168                 numlines = 1;
7169         }
7170         else {
7171                 /* we have a valid enumport command */
7172
7173                 command = talloc_asprintf(ctx, "%s \"%d\"", cmd, 1);
7174                 if (!command) {
7175                         return WERR_NOMEM;
7176                 }
7177
7178                 DEBUG(10,("Running [%s]\n", command));
7179                 ret = smbrun(command, &fd);
7180                 DEBUG(10,("Returned [%d]\n", ret));
7181                 TALLOC_FREE(command);
7182                 if (ret != 0) {
7183                         if (fd != -1) {
7184                                 close(fd);
7185                         }
7186                         return WERR_ACCESS_DENIED;
7187                 }
7188
7189                 numlines = 0;
7190                 qlines = fd_lines_load(fd, &numlines, 0, NULL);
7191                 DEBUGADD(10,("Lines returned = [%d]\n", numlines));
7192                 close(fd);
7193         }
7194
7195         *count = numlines;
7196         *lines = qlines;
7197
7198         return WERR_OK;
7199 }
7200
7201 /****************************************************************************
7202  enumports level 1.
7203 ****************************************************************************/
7204
7205 static WERROR enumports_level_1(TALLOC_CTX *mem_ctx,
7206                                 union spoolss_PortInfo **info_p,
7207                                 uint32_t *count)
7208 {
7209         union spoolss_PortInfo *info = NULL;
7210         int i=0;
7211         WERROR result = WERR_OK;
7212         char **qlines = NULL;
7213         int numlines = 0;
7214
7215         result = enumports_hook(talloc_tos(), &numlines, &qlines );
7216         if (!W_ERROR_IS_OK(result)) {
7217                 goto out;
7218         }
7219
7220         if (numlines) {
7221                 info = TALLOC_ARRAY(mem_ctx, union spoolss_PortInfo, numlines);
7222                 if (!info) {
7223                         DEBUG(10,("Returning WERR_NOMEM\n"));
7224                         result = WERR_NOMEM;
7225                         goto out;
7226                 }
7227
7228                 for (i=0; i<numlines; i++) {
7229                         DEBUG(6,("Filling port number [%d] with port [%s]\n", i, qlines[i]));
7230                         result = fill_port_1(info, &info[i].info1, qlines[i]);
7231                         if (!W_ERROR_IS_OK(result)) {
7232                                 goto out;
7233                         }
7234                 }
7235         }
7236         TALLOC_FREE(qlines);
7237
7238 out:
7239         if (!W_ERROR_IS_OK(result)) {
7240                 TALLOC_FREE(info);
7241                 TALLOC_FREE(qlines);
7242                 *count = 0;
7243                 *info_p = NULL;
7244                 return result;
7245         }
7246
7247         *info_p = info;
7248         *count = numlines;
7249
7250         return WERR_OK;
7251 }
7252
7253 /****************************************************************************
7254  enumports level 2.
7255 ****************************************************************************/
7256
7257 static WERROR enumports_level_2(TALLOC_CTX *mem_ctx,
7258                                 union spoolss_PortInfo **info_p,
7259                                 uint32_t *count)
7260 {
7261         union spoolss_PortInfo *info = NULL;
7262         int i=0;
7263         WERROR result = WERR_OK;
7264         char **qlines = NULL;
7265         int numlines = 0;
7266
7267         result = enumports_hook(talloc_tos(), &numlines, &qlines );
7268         if (!W_ERROR_IS_OK(result)) {
7269                 goto out;
7270         }
7271
7272         if (numlines) {
7273                 info = TALLOC_ARRAY(mem_ctx, union spoolss_PortInfo, numlines);
7274                 if (!info) {
7275                         DEBUG(10,("Returning WERR_NOMEM\n"));
7276                         result = WERR_NOMEM;
7277                         goto out;
7278                 }
7279
7280                 for (i=0; i<numlines; i++) {
7281                         DEBUG(6,("Filling port number [%d] with port [%s]\n", i, qlines[i]));
7282                         result = fill_port_2(info, &info[i].info2, qlines[i]);
7283                         if (!W_ERROR_IS_OK(result)) {
7284                                 goto out;
7285                         }
7286                 }
7287         }
7288         TALLOC_FREE(qlines);
7289
7290 out:
7291         if (!W_ERROR_IS_OK(result)) {
7292                 TALLOC_FREE(info);
7293                 TALLOC_FREE(qlines);
7294                 *count = 0;
7295                 *info_p = NULL;
7296                 return result;
7297         }
7298
7299         *info_p = info;
7300         *count = numlines;
7301
7302         return WERR_OK;
7303 }
7304
7305 /****************************************************************
7306  _spoolss_EnumPorts
7307 ****************************************************************/
7308
7309 WERROR _spoolss_EnumPorts(pipes_struct *p,
7310                           struct spoolss_EnumPorts *r)
7311 {
7312         WERROR result;
7313
7314         /* that's an [in out] buffer */
7315
7316         if (!r->in.buffer && (r->in.offered != 0)) {
7317                 return WERR_INVALID_PARAM;
7318         }
7319
7320         DEBUG(4,("_spoolss_EnumPorts\n"));
7321
7322         *r->out.count = 0;
7323         *r->out.needed = 0;
7324         *r->out.info = NULL;
7325
7326         switch (r->in.level) {
7327         case 1:
7328                 result = enumports_level_1(p->mem_ctx, r->out.info,
7329                                            r->out.count);
7330                 break;
7331         case 2:
7332                 result = enumports_level_2(p->mem_ctx, r->out.info,
7333                                            r->out.count);
7334                 break;
7335         default:
7336                 return WERR_UNKNOWN_LEVEL;
7337         }
7338
7339         if (!W_ERROR_IS_OK(result)) {
7340                 return result;
7341         }
7342
7343         *r->out.needed  = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
7344                                                      spoolss_EnumPorts, NULL,
7345                                                      *r->out.info, r->in.level,
7346                                                      *r->out.count);
7347         *r->out.info    = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
7348         *r->out.count   = SPOOLSS_BUFFER_OK(*r->out.count, 0);
7349
7350         return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
7351 }
7352
7353 /****************************************************************************
7354 ****************************************************************************/
7355
7356 static WERROR spoolss_addprinterex_level_2(pipes_struct *p,
7357                                            const char *server,
7358                                            struct spoolss_SetPrinterInfoCtr *info_ctr,
7359                                            struct spoolss_DeviceMode *devmode,
7360                                            struct security_descriptor *sec_desc,
7361                                            struct spoolss_UserLevelCtr *user_ctr,
7362                                            struct policy_handle *handle)
7363 {
7364         NT_PRINTER_INFO_LEVEL *printer = NULL;
7365         fstring name;
7366         int     snum;
7367         WERROR err = WERR_OK;
7368
7369         if ( !(printer = TALLOC_ZERO_P(NULL, NT_PRINTER_INFO_LEVEL)) ) {
7370                 DEBUG(0,("spoolss_addprinterex_level_2: malloc fail.\n"));
7371                 return WERR_NOMEM;
7372         }
7373
7374         /* convert from UNICODE to ASCII - this allocates the info_2 struct inside *printer.*/
7375         if (!convert_printer_info(info_ctr, printer)) {
7376                 free_a_printer(&printer, 2);
7377                 return WERR_NOMEM;
7378         }
7379
7380         /* check to see if the printer already exists */
7381
7382         if ((snum = print_queue_snum(printer->info_2->sharename)) != -1) {
7383                 DEBUG(5, ("spoolss_addprinterex_level_2: Attempted to add a printer named [%s] when one already existed!\n",
7384                         printer->info_2->sharename));
7385                 free_a_printer(&printer, 2);
7386                 return WERR_PRINTER_ALREADY_EXISTS;
7387         }
7388
7389         /* FIXME!!!  smbd should check to see if the driver is installed before
7390            trying to add a printer like this  --jerry */
7391
7392         if (*lp_addprinter_cmd() ) {
7393                 if ( !add_printer_hook(p->mem_ctx, p->server_info->ptok,
7394                                        printer) ) {
7395                         free_a_printer(&printer,2);
7396                         return WERR_ACCESS_DENIED;
7397                 }
7398         } else {
7399                 DEBUG(0,("spoolss_addprinterex_level_2: add printer for printer %s called and no"
7400                         "smb.conf parameter \"addprinter command\" is defined. This"
7401                         "parameter must exist for this call to succeed\n",
7402                         printer->info_2->sharename ));
7403         }
7404
7405         /* use our primary netbios name since get_a_printer() will convert
7406            it to what the client expects on a case by case basis */
7407
7408         slprintf(name, sizeof(name)-1, "\\\\%s\\%s", global_myname(),
7409              printer->info_2->sharename);
7410
7411
7412         if ((snum = print_queue_snum(printer->info_2->sharename)) == -1) {
7413                 free_a_printer(&printer,2);
7414                 return WERR_ACCESS_DENIED;
7415         }
7416
7417         /* you must be a printer admin to add a new printer */
7418         if (!print_access_check(NULL, snum, PRINTER_ACCESS_ADMINISTER)) {
7419                 free_a_printer(&printer,2);
7420                 return WERR_ACCESS_DENIED;
7421         }
7422
7423         /*
7424          * Do sanity check on the requested changes for Samba.
7425          */
7426
7427         if (!check_printer_ok(printer->info_2, snum)) {
7428                 free_a_printer(&printer,2);
7429                 return WERR_INVALID_PARAM;
7430         }
7431
7432         /*
7433          * When a printer is created, the drivername bound to the printer is used
7434          * to lookup previously saved driver initialization info, which is then
7435          * bound to the new printer, simulating what happens in the Windows arch.
7436          */
7437
7438         if (!devmode)
7439         {
7440                 set_driver_init(printer, 2);
7441         }
7442         else
7443         {
7444                 /* A valid devmode was included, convert and link it
7445                 */
7446                 DEBUGADD(10, ("spoolss_addprinterex_level_2: devmode included, converting\n"));
7447
7448                 if (!convert_devicemode(printer->info_2->printername, devmode,
7449                                         &printer->info_2->devmode)) {
7450                         return  WERR_NOMEM;
7451                 }
7452         }
7453
7454         /* write the ASCII on disk */
7455         err = mod_a_printer(printer, 2);
7456         if (!W_ERROR_IS_OK(err)) {
7457                 free_a_printer(&printer,2);
7458                 return err;
7459         }
7460
7461         if (!open_printer_hnd(p, handle, name, PRINTER_ACCESS_ADMINISTER)) {
7462                 /* Handle open failed - remove addition. */
7463                 del_a_printer(printer->info_2->sharename);
7464                 free_a_printer(&printer,2);
7465                 ZERO_STRUCTP(handle);
7466                 return WERR_ACCESS_DENIED;
7467         }
7468
7469         update_c_setprinter(false);
7470         free_a_printer(&printer,2);
7471
7472         return WERR_OK;
7473 }
7474
7475 /****************************************************************
7476  _spoolss_AddPrinterEx
7477 ****************************************************************/
7478
7479 WERROR _spoolss_AddPrinterEx(pipes_struct *p,
7480                              struct spoolss_AddPrinterEx *r)
7481 {
7482         switch (r->in.info_ctr->level) {
7483         case 1:
7484                 /* we don't handle yet */
7485                 /* but I know what to do ... */
7486                 return WERR_UNKNOWN_LEVEL;
7487         case 2:
7488                 return spoolss_addprinterex_level_2(p, r->in.server,
7489                                                     r->in.info_ctr,
7490                                                     r->in.devmode_ctr->devmode,
7491                                                     r->in.secdesc_ctr->sd,
7492                                                     r->in.userlevel_ctr,
7493                                                     r->out.handle);
7494         default:
7495                 return WERR_UNKNOWN_LEVEL;
7496         }
7497 }
7498
7499 /****************************************************************
7500  _spoolss_AddPrinterDriver
7501 ****************************************************************/
7502
7503 WERROR _spoolss_AddPrinterDriver(pipes_struct *p,
7504                                  struct spoolss_AddPrinterDriver *r)
7505 {
7506         uint32_t level = r->in.info_ctr->level;
7507         struct spoolss_AddDriverInfoCtr *info = r->in.info_ctr;
7508         WERROR err = WERR_OK;
7509         NT_PRINTER_DRIVER_INFO_LEVEL driver;
7510         const char *driver_name = NULL;
7511         uint32_t version;
7512         const char *fn;
7513
7514         switch (p->hdr_req.opnum) {
7515                 case NDR_SPOOLSS_ADDPRINTERDRIVER:
7516                         fn = "_spoolss_AddPrinterDriver";
7517                         break;
7518                 case NDR_SPOOLSS_ADDPRINTERDRIVEREX:
7519                         fn = "_spoolss_AddPrinterDriverEx";
7520                         break;
7521                 default:
7522                         return WERR_INVALID_PARAM;
7523         }
7524
7525
7526         /* FIXME */
7527         if (level != 3 && level != 6) {
7528                 /* Clever hack from Martin Zielinski <mz@seh.de>
7529                  * to allow downgrade from level 8 (Vista).
7530                  */
7531                 DEBUG(0,("%s: level %d not yet implemented\n", fn, level));
7532                 return WERR_UNKNOWN_LEVEL;
7533         }
7534
7535         ZERO_STRUCT(driver);
7536
7537         if (!convert_printer_driver_info(info, &driver, level)) {
7538                 err = WERR_NOMEM;
7539                 goto done;
7540         }
7541
7542         DEBUG(5,("Cleaning driver's information\n"));
7543         err = clean_up_driver_struct(p, driver, level);
7544         if (!W_ERROR_IS_OK(err))
7545                 goto done;
7546
7547         DEBUG(5,("Moving driver to final destination\n"));
7548         if( !W_ERROR_IS_OK(err = move_driver_to_download_area(p, driver, level,
7549                                                               &err)) ) {
7550                 goto done;
7551         }
7552
7553         if (add_a_printer_driver(driver, level)!=0) {
7554                 err = WERR_ACCESS_DENIED;
7555                 goto done;
7556         }
7557
7558         switch(level) {
7559         case 3:
7560                 driver_name = driver.info_3->name ? driver.info_3->name : "";
7561                 break;
7562         case 6:
7563                 driver_name = driver.info_6->name ? driver.info_6->name : "";
7564                 break;
7565         }
7566
7567         /*
7568          * I think this is where he DrvUpgradePrinter() hook would be
7569          * be called in a driver's interface DLL on a Windows NT 4.0/2k
7570          * server.  Right now, we just need to send ourselves a message
7571          * to update each printer bound to this driver.   --jerry
7572          */
7573
7574         if (!srv_spoolss_drv_upgrade_printer(driver_name)) {
7575                 DEBUG(0,("%s: Failed to send message about upgrading driver [%s]!\n",
7576                         fn, driver_name));
7577         }
7578
7579         /*
7580          * Based on the version (e.g. driver destination dir: 0=9x,2=Nt/2k,3=2k/Xp),
7581          * decide if the driver init data should be deleted. The rules are:
7582          *  1) never delete init data if it is a 9x driver, they don't use it anyway
7583          *  2) delete init data only if there is no 2k/Xp driver
7584          *  3) always delete init data
7585          * The generalized rule is always use init data from the highest order driver.
7586          * It is necessary to follow the driver install by an initialization step to
7587          * finish off this process.
7588         */
7589         if (level == 3)
7590                 version = driver.info_3->cversion;
7591         else if (level == 6)
7592                 version = driver.info_6->version;
7593         else
7594                 version = -1;
7595         switch (version) {
7596                 /*
7597                  * 9x printer driver - never delete init data
7598                 */
7599                 case 0:
7600                         DEBUG(10,("%s: init data not deleted for 9x driver [%s]\n",
7601                                 fn, driver_name));
7602                         break;
7603
7604                 /*
7605                  * Nt or 2k (compatiblity mode) printer driver - only delete init data if
7606                  * there is no 2k/Xp driver init data for this driver name.
7607                 */
7608                 case 2:
7609                 {
7610                         NT_PRINTER_DRIVER_INFO_LEVEL driver1;
7611
7612                         if (!W_ERROR_IS_OK(get_a_printer_driver(&driver1, 3, driver_name, "Windows NT x86", 3))) {
7613                                 /*
7614                                  * No 2k/Xp driver found, delete init data (if any) for the new Nt driver.
7615                                 */
7616                                 if (!del_driver_init(driver_name))
7617                                         DEBUG(6,("%s: del_driver_init(%s) Nt failed!\n",
7618                                                 fn, driver_name));
7619                         } else {
7620                                 /*
7621                                  * a 2k/Xp driver was found, don't delete init data because Nt driver will use it.
7622                                 */
7623                                 free_a_printer_driver(driver1,3);
7624                                 DEBUG(10,("%s: init data not deleted for Nt driver [%s]\n",
7625                                         fn, driver_name));
7626                         }
7627                 }
7628                 break;
7629
7630                 /*
7631                  * 2k or Xp printer driver - always delete init data
7632                 */
7633                 case 3:
7634                         if (!del_driver_init(driver_name))
7635                                 DEBUG(6,("%s: del_driver_init(%s) 2k/Xp failed!\n",
7636                                         fn, driver_name));
7637                         break;
7638
7639                 default:
7640                         DEBUG(0,("%s: invalid level=%d\n", fn, level));
7641                         break;
7642         }
7643
7644
7645 done:
7646         free_a_printer_driver(driver, level);
7647         return err;
7648 }
7649
7650 /****************************************************************
7651  _spoolss_AddPrinterDriverEx
7652 ****************************************************************/
7653
7654 WERROR _spoolss_AddPrinterDriverEx(pipes_struct *p,
7655                                    struct spoolss_AddPrinterDriverEx *r)
7656 {
7657         struct spoolss_AddPrinterDriver a;
7658
7659         /*
7660          * we only support the semantics of AddPrinterDriver()
7661          * i.e. only copy files that are newer than existing ones
7662          */
7663
7664         if (r->in.flags != APD_COPY_NEW_FILES) {
7665                 return WERR_ACCESS_DENIED;
7666         }
7667
7668         a.in.servername         = r->in.servername;
7669         a.in.info_ctr           = r->in.info_ctr;
7670
7671         return _spoolss_AddPrinterDriver(p, &a);
7672 }
7673
7674 /****************************************************************************
7675 ****************************************************************************/
7676
7677 struct _spoolss_paths {
7678         int type;
7679         const char *share;
7680         const char *dir;
7681 };
7682
7683 enum { SPOOLSS_DRIVER_PATH, SPOOLSS_PRTPROCS_PATH };
7684
7685 static const struct _spoolss_paths spoolss_paths[]= {
7686         { SPOOLSS_DRIVER_PATH,          "print$",       "DRIVERS" },
7687         { SPOOLSS_PRTPROCS_PATH,        "prnproc$",     "PRTPROCS" }
7688 };
7689
7690 static WERROR compose_spoolss_server_path(TALLOC_CTX *mem_ctx,
7691                                           const char *servername,
7692                                           const char *environment,
7693                                           int component,
7694                                           char **path)
7695 {
7696         const char *pservername = NULL;
7697         const char *long_archi = SPOOLSS_ARCHITECTURE_NT_X86;
7698         const char *short_archi;
7699
7700         *path = NULL;
7701
7702         /* environment may be empty */
7703         if (environment && strlen(environment)) {
7704                 long_archi = environment;
7705         }
7706
7707         /* servername may be empty */
7708         if (servername && strlen(servername)) {
7709                 pservername = canon_servername(servername);
7710
7711                 if (!is_myname_or_ipaddr(pservername)) {
7712                         return WERR_INVALID_PARAM;
7713                 }
7714         }
7715
7716         if (!(short_archi = get_short_archi(long_archi))) {
7717                 return WERR_INVALID_ENVIRONMENT;
7718         }
7719
7720         switch (component) {
7721         case SPOOLSS_PRTPROCS_PATH:
7722         case SPOOLSS_DRIVER_PATH:
7723                 if (pservername) {
7724                         *path = talloc_asprintf(mem_ctx,
7725                                         "\\\\%s\\%s\\%s",
7726                                         pservername,
7727                                         spoolss_paths[component].share,
7728                                         short_archi);
7729                 } else {
7730                         *path = talloc_asprintf(mem_ctx, "%s\\%s\\%s",
7731                                         SPOOLSS_DEFAULT_SERVER_PATH,
7732                                         spoolss_paths[component].dir,
7733                                         short_archi);
7734                 }
7735                 break;
7736         default:
7737                 return WERR_INVALID_PARAM;
7738         }
7739
7740         if (!*path) {
7741                 return WERR_NOMEM;
7742         }
7743
7744         return WERR_OK;
7745 }
7746
7747 /****************************************************************************
7748 ****************************************************************************/
7749
7750 static WERROR getprinterdriverdir_level_1(TALLOC_CTX *mem_ctx,
7751                                           const char *servername,
7752                                           const char *environment,
7753                                           struct spoolss_DriverDirectoryInfo1 *r)
7754 {
7755         WERROR werr;
7756         char *path = NULL;
7757
7758         werr = compose_spoolss_server_path(mem_ctx,
7759                                            servername,
7760                                            environment,
7761                                            SPOOLSS_DRIVER_PATH,
7762                                            &path);
7763         if (!W_ERROR_IS_OK(werr)) {
7764                 return werr;
7765         }
7766
7767         DEBUG(4,("printer driver directory: [%s]\n", path));
7768
7769         r->directory_name = path;
7770
7771         return WERR_OK;
7772 }
7773
7774 /****************************************************************
7775  _spoolss_GetPrinterDriverDirectory
7776 ****************************************************************/
7777
7778 WERROR _spoolss_GetPrinterDriverDirectory(pipes_struct *p,
7779                                           struct spoolss_GetPrinterDriverDirectory *r)
7780 {
7781         WERROR werror;
7782
7783         /* that's an [in out] buffer */
7784
7785         if (!r->in.buffer && (r->in.offered != 0)) {
7786                 return WERR_INVALID_PARAM;
7787         }
7788
7789         DEBUG(5,("_spoolss_GetPrinterDriverDirectory: level %d\n",
7790                 r->in.level));
7791
7792         *r->out.needed = 0;
7793
7794         /* r->in.level is ignored */
7795
7796         werror = getprinterdriverdir_level_1(p->mem_ctx,
7797                                              r->in.server,
7798                                              r->in.environment,
7799                                              &r->out.info->info1);
7800         if (!W_ERROR_IS_OK(werror)) {
7801                 TALLOC_FREE(r->out.info);
7802                 return werror;
7803         }
7804
7805         *r->out.needed  = SPOOLSS_BUFFER_UNION(spoolss_DriverDirectoryInfo, NULL,
7806                                                r->out.info, r->in.level);
7807         r->out.info     = SPOOLSS_BUFFER_OK(r->out.info, NULL);
7808
7809         return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
7810 }
7811
7812 /****************************************************************
7813  _spoolss_EnumPrinterData
7814 ****************************************************************/
7815
7816 WERROR _spoolss_EnumPrinterData(pipes_struct *p,
7817                                 struct spoolss_EnumPrinterData *r)
7818 {
7819         NT_PRINTER_INFO_LEVEL *printer = NULL;
7820         Printer_entry   *Printer = find_printer_index_by_hnd(p, r->in.handle);
7821         int             snum;
7822         WERROR          result;
7823         REGISTRY_VALUE  *val = NULL;
7824         NT_PRINTER_DATA *p_data;
7825         int             i, key_index, num_values;
7826         int             name_length;
7827
7828         *r->out.value_needed    = 0;
7829         *r->out.type            = REG_NONE;
7830         *r->out.data_needed     = 0;
7831
7832         DEBUG(5,("_spoolss_EnumPrinterData\n"));
7833
7834         if (!Printer) {
7835                 DEBUG(2,("_spoolss_EnumPrinterData: Invalid handle (%s:%u:%u).\n",
7836                         OUR_HANDLE(r->in.handle)));
7837                 return WERR_BADFID;
7838         }
7839
7840         if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
7841                 return WERR_BADFID;
7842         }
7843
7844         result = get_a_printer(Printer, &printer, 2, lp_const_servicename(snum));
7845         if (!W_ERROR_IS_OK(result)) {
7846                 return result;
7847         }
7848
7849         p_data = printer->info_2->data;
7850         key_index = lookup_printerkey( p_data, SPOOL_PRINTERDATA_KEY );
7851
7852         result = WERR_OK;
7853
7854         /*
7855          * The NT machine wants to know the biggest size of value and data
7856          *
7857          * cf: MSDN EnumPrinterData remark section
7858          */
7859
7860         if (!r->in.value_offered && !r->in.data_offered && (key_index != -1)) {
7861
7862                 uint32_t biggest_valuesize = 0;
7863                 uint32_t biggest_datasize = 0;
7864
7865                 DEBUGADD(6,("Activating NT mega-hack to find sizes\n"));
7866
7867                 num_values = regval_ctr_numvals( p_data->keys[key_index].values );
7868
7869                 for ( i=0; i<num_values; i++ )
7870                 {
7871                         val = regval_ctr_specific_value( p_data->keys[key_index].values, i );
7872
7873                         name_length = strlen(val->valuename);
7874                         if ( strlen(val->valuename) > biggest_valuesize )
7875                                 biggest_valuesize = name_length;
7876
7877                         if ( val->size > biggest_datasize )
7878                                 biggest_datasize = val->size;
7879
7880                         DEBUG(6,("current values: [%d], [%d]\n", biggest_valuesize,
7881                                 biggest_datasize));
7882                 }
7883
7884                 /* the value is an UNICODE string but real_value_size is the length
7885                    in bytes including the trailing 0 */
7886
7887                 *r->out.value_needed = 2 * (1 + biggest_valuesize);
7888                 *r->out.data_needed  = biggest_datasize;
7889
7890                 DEBUG(6,("final values: [%d], [%d]\n",
7891                         *r->out.value_needed, *r->out.data_needed));
7892
7893                 goto done;
7894         }
7895
7896         /*
7897          * the value len is wrong in NT sp3
7898          * that's the number of bytes not the number of unicode chars
7899          */
7900
7901         if (key_index != -1) {
7902                 val = regval_ctr_specific_value(p_data->keys[key_index].values,
7903                                                 r->in.enum_index);
7904         }
7905
7906         if (!val) {
7907
7908                 /* out_value should default to "" or else NT4 has
7909                    problems unmarshalling the response */
7910
7911                 if (r->in.value_offered) {
7912                         *r->out.value_needed = 1;
7913                         r->out.value_name = talloc_strdup(r, "");
7914                         if (!r->out.value_name) {
7915                                 result = WERR_NOMEM;
7916                                 goto done;
7917                         }
7918                 } else {
7919                         r->out.value_name = NULL;
7920                         *r->out.value_needed = 0;
7921                 }
7922
7923                 /* the data is counted in bytes */
7924
7925                 *r->out.data_needed = r->in.data_offered;
7926
7927                 result = WERR_NO_MORE_ITEMS;
7928         } else {
7929                 /*
7930                  * the value is:
7931                  * - counted in bytes in the request
7932                  * - counted in UNICODE chars in the max reply
7933                  * - counted in bytes in the real size
7934                  *
7935                  * take a pause *before* coding not *during* coding
7936                  */
7937
7938                 /* name */
7939                 if (r->in.value_offered) {
7940                         r->out.value_name = talloc_strdup(r, regval_name(val));
7941                         if (!r->out.value_name) {
7942                                 result = WERR_NOMEM;
7943                                 goto done;
7944                         }
7945                         *r->out.value_needed = strlen_m(regval_name(val));
7946                 } else {
7947                         r->out.value_name = NULL;
7948                         *r->out.value_needed = 0;
7949                 }
7950
7951                 /* type */
7952
7953                 *r->out.type = regval_type(val);
7954
7955                 /* data - counted in bytes */
7956
7957                 if (r->out.data && regval_size(val)) {
7958                         memcpy(r->out.data, regval_data_p(val), regval_size(val));
7959                 }
7960
7961                 *r->out.data_needed = regval_size(val);
7962         }
7963
7964 done:
7965         free_a_printer(&printer, 2);
7966         return result;
7967 }
7968
7969 /****************************************************************
7970  _spoolss_SetPrinterData
7971 ****************************************************************/
7972
7973 WERROR _spoolss_SetPrinterData(pipes_struct *p,
7974                                struct spoolss_SetPrinterData *r)
7975 {
7976         NT_PRINTER_INFO_LEVEL *printer = NULL;
7977         int snum=0;
7978         WERROR result = WERR_OK;
7979         Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
7980         DATA_BLOB blob;
7981
7982         DEBUG(5,("_spoolss_SetPrinterData\n"));
7983
7984         if (!Printer) {
7985                 DEBUG(2,("_spoolss_SetPrinterData: Invalid handle (%s:%u:%u).\n",
7986                         OUR_HANDLE(r->in.handle)));
7987                 return WERR_BADFID;
7988         }
7989
7990         if (Printer->printer_type == SPLHND_SERVER) {
7991                 DEBUG(10,("_spoolss_SetPrinterData: "
7992                         "Not implemented for server handles yet\n"));
7993                 return WERR_INVALID_PARAM;
7994         }
7995
7996         if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
7997                 return WERR_BADFID;
7998         }
7999
8000         /*
8001          * Access check : NT returns "access denied" if you make a
8002          * SetPrinterData call without the necessary privildge.
8003          * we were originally returning OK if nothing changed
8004          * which made Win2k issue **a lot** of SetPrinterData
8005          * when connecting to a printer  --jerry
8006          */
8007
8008         if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
8009                 DEBUG(3,("_spoolss_SetPrinterData: "
8010                         "change denied by handle access permissions\n"));
8011                 result = WERR_ACCESS_DENIED;
8012                 goto done;
8013         }
8014
8015         result = get_a_printer(Printer, &printer, 2, lp_const_servicename(snum));
8016         if (!W_ERROR_IS_OK(result)) {
8017                 return result;
8018         }
8019
8020         result = push_spoolss_PrinterData(p->mem_ctx, &blob,
8021                                           r->in.type, &r->in.data);
8022         if (!W_ERROR_IS_OK(result)) {
8023                 goto done;
8024         }
8025
8026         /*
8027          * When client side code sets a magic printer data key, detect it and save
8028          * the current printer data and the magic key's data (its the DEVMODE) for
8029          * future printer/driver initializations.
8030          */
8031         if ((r->in.type == REG_BINARY) && strequal(r->in.value_name, PHANTOM_DEVMODE_KEY)) {
8032                 /* Set devmode and printer initialization info */
8033                 result = save_driver_init(printer, 2, blob.data, blob.length);
8034
8035                 srv_spoolss_reset_printerdata(printer->info_2->drivername);
8036
8037                 goto done;
8038         }
8039
8040         result = set_printer_dataex(printer, SPOOL_PRINTERDATA_KEY,
8041                                     r->in.value_name, r->in.type,
8042                                     blob.data, blob.length);
8043         if (W_ERROR_IS_OK(result)) {
8044                 result = mod_a_printer(printer, 2);
8045         }
8046
8047 done:
8048         free_a_printer(&printer, 2);
8049
8050         return result;
8051 }
8052
8053 /****************************************************************
8054  _spoolss_ResetPrinter
8055 ****************************************************************/
8056
8057 WERROR _spoolss_ResetPrinter(pipes_struct *p,
8058                              struct spoolss_ResetPrinter *r)
8059 {
8060         Printer_entry   *Printer = find_printer_index_by_hnd(p, r->in.handle);
8061         int             snum;
8062
8063         DEBUG(5,("_spoolss_ResetPrinter\n"));
8064
8065         /*
8066          * All we do is to check to see if the handle and queue is valid.
8067          * This call really doesn't mean anything to us because we only
8068          * support RAW printing.   --jerry
8069          */
8070
8071         if (!Printer) {
8072                 DEBUG(2,("_spoolss_ResetPrinter: Invalid handle (%s:%u:%u).\n",
8073                         OUR_HANDLE(r->in.handle)));
8074                 return WERR_BADFID;
8075         }
8076
8077         if (!get_printer_snum(p, r->in.handle, &snum, NULL))
8078                 return WERR_BADFID;
8079
8080
8081         /* blindly return success */
8082         return WERR_OK;
8083 }
8084
8085 /****************************************************************
8086  _spoolss_DeletePrinterData
8087 ****************************************************************/
8088
8089 WERROR _spoolss_DeletePrinterData(pipes_struct *p,
8090                                   struct spoolss_DeletePrinterData *r)
8091 {
8092         NT_PRINTER_INFO_LEVEL   *printer = NULL;
8093         int             snum=0;
8094         WERROR          status = WERR_OK;
8095         Printer_entry   *Printer = find_printer_index_by_hnd(p, r->in.handle);
8096
8097         DEBUG(5,("_spoolss_DeletePrinterData\n"));
8098
8099         if (!Printer) {
8100                 DEBUG(2,("_spoolss_DeletePrinterData: Invalid handle (%s:%u:%u).\n",
8101                         OUR_HANDLE(r->in.handle)));
8102                 return WERR_BADFID;
8103         }
8104
8105         if (!get_printer_snum(p, r->in.handle, &snum, NULL))
8106                 return WERR_BADFID;
8107
8108         if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
8109                 DEBUG(3, ("_spoolss_DeletePrinterData: "
8110                         "printer properties change denied by handle\n"));
8111                 return WERR_ACCESS_DENIED;
8112         }
8113
8114         status = get_a_printer(Printer, &printer, 2, lp_const_servicename(snum));
8115         if (!W_ERROR_IS_OK(status))
8116                 return status;
8117
8118         if (!r->in.value_name) {
8119                 free_a_printer(&printer, 2);
8120                 return WERR_NOMEM;
8121         }
8122
8123         status = delete_printer_dataex( printer, SPOOL_PRINTERDATA_KEY,
8124                                         r->in.value_name );
8125
8126         if ( W_ERROR_IS_OK(status) )
8127                 mod_a_printer( printer, 2 );
8128
8129         free_a_printer(&printer, 2);
8130
8131         return status;
8132 }
8133
8134 /****************************************************************
8135  _spoolss_AddForm
8136 ****************************************************************/
8137
8138 WERROR _spoolss_AddForm(pipes_struct *p,
8139                         struct spoolss_AddForm *r)
8140 {
8141         struct spoolss_AddFormInfo1 *form = r->in.info.info1;
8142         nt_forms_struct tmpForm;
8143         int snum;
8144         WERROR status = WERR_OK;
8145         NT_PRINTER_INFO_LEVEL *printer = NULL;
8146
8147         int count=0;
8148         nt_forms_struct *list=NULL;
8149         Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
8150
8151         DEBUG(5,("_spoolss_AddForm\n"));
8152
8153         if (!Printer) {
8154                 DEBUG(2,("_spoolss_AddForm: Invalid handle (%s:%u:%u).\n",
8155                         OUR_HANDLE(r->in.handle)));
8156                 return WERR_BADFID;
8157         }
8158
8159
8160         /* forms can be added on printer of on the print server handle */
8161
8162         if ( Printer->printer_type == SPLHND_PRINTER )
8163         {
8164                 if (!get_printer_snum(p, r->in.handle, &snum, NULL))
8165                         return WERR_BADFID;
8166
8167                 status = get_a_printer(Printer, &printer, 2, lp_const_servicename(snum));
8168                 if (!W_ERROR_IS_OK(status))
8169                         goto done;
8170         }
8171
8172         if ( !(Printer->access_granted & (PRINTER_ACCESS_ADMINISTER|SERVER_ACCESS_ADMINISTER)) ) {
8173                 DEBUG(2,("_spoolss_addform: denied by handle permissions.\n"));
8174                 status = WERR_ACCESS_DENIED;
8175                 goto done;
8176         }
8177
8178         /* can't add if builtin */
8179
8180         if (get_a_builtin_ntform_by_string(form->form_name, &tmpForm)) {
8181                 status = WERR_FILE_EXISTS;
8182                 goto done;
8183         }
8184
8185         count = get_ntforms(&list);
8186
8187         if(!add_a_form(&list, form, &count)) {
8188                 status =  WERR_NOMEM;
8189                 goto done;
8190         }
8191
8192         write_ntforms(&list, count);
8193
8194         /*
8195          * ChangeID must always be set if this is a printer
8196          */
8197
8198         if ( Printer->printer_type == SPLHND_PRINTER )
8199                 status = mod_a_printer(printer, 2);
8200
8201 done:
8202         if ( printer )
8203                 free_a_printer(&printer, 2);
8204         SAFE_FREE(list);
8205
8206         return status;
8207 }
8208
8209 /****************************************************************
8210  _spoolss_DeleteForm
8211 ****************************************************************/
8212
8213 WERROR _spoolss_DeleteForm(pipes_struct *p,
8214                            struct spoolss_DeleteForm *r)
8215 {
8216         const char *form_name = r->in.form_name;
8217         nt_forms_struct tmpForm;
8218         int count=0;
8219         nt_forms_struct *list=NULL;
8220         Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
8221         int snum;
8222         WERROR status = WERR_OK;
8223         NT_PRINTER_INFO_LEVEL *printer = NULL;
8224
8225         DEBUG(5,("_spoolss_DeleteForm\n"));
8226
8227         if (!Printer) {
8228                 DEBUG(2,("_spoolss_DeleteForm: Invalid handle (%s:%u:%u).\n",
8229                         OUR_HANDLE(r->in.handle)));
8230                 return WERR_BADFID;
8231         }
8232
8233         /* forms can be deleted on printer of on the print server handle */
8234
8235         if ( Printer->printer_type == SPLHND_PRINTER )
8236         {
8237                 if (!get_printer_snum(p, r->in.handle, &snum, NULL))
8238                         return WERR_BADFID;
8239
8240                 status = get_a_printer(Printer, &printer, 2, lp_const_servicename(snum));
8241                 if (!W_ERROR_IS_OK(status))
8242                         goto done;
8243         }
8244
8245         if ( !(Printer->access_granted & (PRINTER_ACCESS_ADMINISTER|SERVER_ACCESS_ADMINISTER)) ) {
8246                 DEBUG(2,("_spoolss_DeleteForm: denied by handle permissions.\n"));
8247                 status = WERR_ACCESS_DENIED;
8248                 goto done;
8249         }
8250
8251         /* can't delete if builtin */
8252
8253         if (get_a_builtin_ntform_by_string(form_name,&tmpForm)) {
8254                 status = WERR_INVALID_PARAM;
8255                 goto done;
8256         }
8257
8258         count = get_ntforms(&list);
8259
8260         if ( !delete_a_form(&list, form_name, &count, &status ))
8261                 goto done;
8262
8263         /*
8264          * ChangeID must always be set if this is a printer
8265          */
8266
8267         if ( Printer->printer_type == SPLHND_PRINTER )
8268                 status = mod_a_printer(printer, 2);
8269
8270 done:
8271         if ( printer )
8272                 free_a_printer(&printer, 2);
8273         SAFE_FREE(list);
8274
8275         return status;
8276 }
8277
8278 /****************************************************************
8279  _spoolss_SetForm
8280 ****************************************************************/
8281
8282 WERROR _spoolss_SetForm(pipes_struct *p,
8283                         struct spoolss_SetForm *r)
8284 {
8285         struct spoolss_AddFormInfo1 *form = r->in.info.info1;
8286         nt_forms_struct tmpForm;
8287         int snum;
8288         WERROR status = WERR_OK;
8289         NT_PRINTER_INFO_LEVEL *printer = NULL;
8290
8291         int count=0;
8292         nt_forms_struct *list=NULL;
8293         Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
8294
8295         DEBUG(5,("_spoolss_SetForm\n"));
8296
8297         if (!Printer) {
8298                 DEBUG(2,("_spoolss_SetForm: Invalid handle (%s:%u:%u).\n",
8299                         OUR_HANDLE(r->in.handle)));
8300                 return WERR_BADFID;
8301         }
8302
8303         /* forms can be modified on printer of on the print server handle */
8304
8305         if ( Printer->printer_type == SPLHND_PRINTER )
8306         {
8307                 if (!get_printer_snum(p, r->in.handle, &snum, NULL))
8308                         return WERR_BADFID;
8309
8310                 status = get_a_printer(Printer, &printer, 2, lp_const_servicename(snum));
8311                 if (!W_ERROR_IS_OK(status))
8312                         goto done;
8313         }
8314
8315         if ( !(Printer->access_granted & (PRINTER_ACCESS_ADMINISTER|SERVER_ACCESS_ADMINISTER)) ) {
8316                 DEBUG(2,("_spoolss_SetForm: denied by handle permissions\n"));
8317                 status = WERR_ACCESS_DENIED;
8318                 goto done;
8319         }
8320
8321         /* can't set if builtin */
8322         if (get_a_builtin_ntform_by_string(form->form_name, &tmpForm)) {
8323                 status = WERR_INVALID_PARAM;
8324                 goto done;
8325         }
8326
8327         count = get_ntforms(&list);
8328         update_a_form(&list, form, count);
8329         write_ntforms(&list, count);
8330
8331         /*
8332          * ChangeID must always be set if this is a printer
8333          */
8334
8335         if ( Printer->printer_type == SPLHND_PRINTER )
8336                 status = mod_a_printer(printer, 2);
8337
8338
8339 done:
8340         if ( printer )
8341                 free_a_printer(&printer, 2);
8342         SAFE_FREE(list);
8343
8344         return status;
8345 }
8346
8347 /****************************************************************************
8348  fill_print_processor1
8349 ****************************************************************************/
8350
8351 static WERROR fill_print_processor1(TALLOC_CTX *mem_ctx,
8352                                     struct spoolss_PrintProcessorInfo1 *r,
8353                                     const char *print_processor_name)
8354 {
8355         r->print_processor_name = talloc_strdup(mem_ctx, print_processor_name);
8356         W_ERROR_HAVE_NO_MEMORY(r->print_processor_name);
8357
8358         return WERR_OK;
8359 }
8360
8361 /****************************************************************************
8362  enumprintprocessors level 1.
8363 ****************************************************************************/
8364
8365 static WERROR enumprintprocessors_level_1(TALLOC_CTX *mem_ctx,
8366                                           union spoolss_PrintProcessorInfo **info_p,
8367                                           uint32_t *count)
8368 {
8369         union spoolss_PrintProcessorInfo *info;
8370         WERROR result;
8371
8372         info = TALLOC_ARRAY(mem_ctx, union spoolss_PrintProcessorInfo, 1);
8373         W_ERROR_HAVE_NO_MEMORY(info);
8374
8375         *count = 1;
8376
8377         result = fill_print_processor1(info, &info[0].info1, "winprint");
8378         if (!W_ERROR_IS_OK(result)) {
8379                 goto out;
8380         }
8381
8382  out:
8383         if (!W_ERROR_IS_OK(result)) {
8384                 TALLOC_FREE(info);
8385                 *count = 0;
8386                 return result;
8387         }
8388
8389         *info_p = info;
8390
8391         return WERR_OK;
8392 }
8393
8394 /****************************************************************
8395  _spoolss_EnumPrintProcessors
8396 ****************************************************************/
8397
8398 WERROR _spoolss_EnumPrintProcessors(pipes_struct *p,
8399                                     struct spoolss_EnumPrintProcessors *r)
8400 {
8401         WERROR result;
8402
8403         /* that's an [in out] buffer */
8404
8405         if (!r->in.buffer && (r->in.offered != 0)) {
8406                 return WERR_INVALID_PARAM;
8407         }
8408
8409         DEBUG(5,("_spoolss_EnumPrintProcessors\n"));
8410
8411         /*
8412          * Enumerate the print processors ...
8413          *
8414          * Just reply with "winprint", to keep NT happy
8415          * and I can use my nice printer checker.
8416          */
8417
8418         *r->out.count = 0;
8419         *r->out.needed = 0;
8420         *r->out.info = NULL;
8421
8422         switch (r->in.level) {
8423         case 1:
8424                 result = enumprintprocessors_level_1(p->mem_ctx, r->out.info,
8425                                                      r->out.count);
8426                 break;
8427         default:
8428                 return WERR_UNKNOWN_LEVEL;
8429         }
8430
8431         if (!W_ERROR_IS_OK(result)) {
8432                 return result;
8433         }
8434
8435         *r->out.needed  = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
8436                                                      spoolss_EnumPrintProcessors, NULL,
8437                                                      *r->out.info, r->in.level,
8438                                                      *r->out.count);
8439         *r->out.info    = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
8440         *r->out.count   = SPOOLSS_BUFFER_OK(*r->out.count, 0);
8441
8442         return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
8443 }
8444
8445 /****************************************************************************
8446  fill_printprocdatatype1
8447 ****************************************************************************/
8448
8449 static WERROR fill_printprocdatatype1(TALLOC_CTX *mem_ctx,
8450                                       struct spoolss_PrintProcDataTypesInfo1 *r,
8451                                       const char *name_array)
8452 {
8453         r->name_array = talloc_strdup(mem_ctx, name_array);
8454         W_ERROR_HAVE_NO_MEMORY(r->name_array);
8455
8456         return WERR_OK;
8457 }
8458
8459 /****************************************************************************
8460  enumprintprocdatatypes level 1.
8461 ****************************************************************************/
8462
8463 static WERROR enumprintprocdatatypes_level_1(TALLOC_CTX *mem_ctx,
8464                                              union spoolss_PrintProcDataTypesInfo **info_p,
8465                                              uint32_t *count)
8466 {
8467         WERROR result;
8468         union spoolss_PrintProcDataTypesInfo *info;
8469
8470         info = TALLOC_ARRAY(mem_ctx, union spoolss_PrintProcDataTypesInfo, 1);
8471         W_ERROR_HAVE_NO_MEMORY(info);
8472
8473         *count = 1;
8474
8475         result = fill_printprocdatatype1(info, &info[0].info1, "RAW");
8476         if (!W_ERROR_IS_OK(result)) {
8477                 goto out;
8478         }
8479
8480  out:
8481         if (!W_ERROR_IS_OK(result)) {
8482                 TALLOC_FREE(info);
8483                 *count = 0;
8484                 return result;
8485         }
8486
8487         *info_p = info;
8488
8489         return WERR_OK;
8490 }
8491
8492 /****************************************************************
8493  _spoolss_EnumPrintProcDataTypes
8494 ****************************************************************/
8495
8496 WERROR _spoolss_EnumPrintProcDataTypes(pipes_struct *p,
8497                                        struct spoolss_EnumPrintProcDataTypes *r)
8498 {
8499         WERROR result;
8500
8501         /* that's an [in out] buffer */
8502
8503         if (!r->in.buffer && (r->in.offered != 0)) {
8504                 return WERR_INVALID_PARAM;
8505         }
8506
8507         DEBUG(5,("_spoolss_EnumPrintProcDataTypes\n"));
8508
8509         *r->out.count = 0;
8510         *r->out.needed = 0;
8511         *r->out.info = NULL;
8512
8513         switch (r->in.level) {
8514         case 1:
8515                 result = enumprintprocdatatypes_level_1(p->mem_ctx, r->out.info,
8516                                                         r->out.count);
8517                 break;
8518         default:
8519                 return WERR_UNKNOWN_LEVEL;
8520         }
8521
8522         *r->out.needed  = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
8523                                                      spoolss_EnumPrintProcDataTypes, NULL,
8524                                                      *r->out.info, r->in.level,
8525                                                      *r->out.count);
8526         *r->out.info    = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
8527         *r->out.count   = SPOOLSS_BUFFER_OK(*r->out.count, 0);
8528
8529         return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
8530 }
8531
8532 /****************************************************************************
8533  fill_monitor_1
8534 ****************************************************************************/
8535
8536 static WERROR fill_monitor_1(TALLOC_CTX *mem_ctx,
8537                              struct spoolss_MonitorInfo1 *r,
8538                              const char *monitor_name)
8539 {
8540         r->monitor_name                 = talloc_strdup(mem_ctx, monitor_name);
8541         W_ERROR_HAVE_NO_MEMORY(r->monitor_name);
8542
8543         return WERR_OK;
8544 }
8545
8546 /****************************************************************************
8547  fill_monitor_2
8548 ****************************************************************************/
8549
8550 static WERROR fill_monitor_2(TALLOC_CTX *mem_ctx,
8551                              struct spoolss_MonitorInfo2 *r,
8552                              const char *monitor_name,
8553                              const char *environment,
8554                              const char *dll_name)
8555 {
8556         r->monitor_name                 = talloc_strdup(mem_ctx, monitor_name);
8557         W_ERROR_HAVE_NO_MEMORY(r->monitor_name);
8558         r->environment                  = talloc_strdup(mem_ctx, environment);
8559         W_ERROR_HAVE_NO_MEMORY(r->environment);
8560         r->dll_name                     = talloc_strdup(mem_ctx, dll_name);
8561         W_ERROR_HAVE_NO_MEMORY(r->dll_name);
8562
8563         return WERR_OK;
8564 }
8565
8566 /****************************************************************************
8567  enumprintmonitors level 1.
8568 ****************************************************************************/
8569
8570 static WERROR enumprintmonitors_level_1(TALLOC_CTX *mem_ctx,
8571                                         union spoolss_MonitorInfo **info_p,
8572                                         uint32_t *count)
8573 {
8574         union spoolss_MonitorInfo *info;
8575         WERROR result = WERR_OK;
8576
8577         info = TALLOC_ARRAY(mem_ctx, union spoolss_MonitorInfo, 2);
8578         W_ERROR_HAVE_NO_MEMORY(info);
8579
8580         *count = 2;
8581
8582         result = fill_monitor_1(info, &info[0].info1,
8583                                 SPL_LOCAL_PORT);
8584         if (!W_ERROR_IS_OK(result)) {
8585                 goto out;
8586         }
8587
8588         result = fill_monitor_1(info, &info[1].info1,
8589                                 SPL_TCPIP_PORT);
8590         if (!W_ERROR_IS_OK(result)) {
8591                 goto out;
8592         }
8593
8594 out:
8595         if (!W_ERROR_IS_OK(result)) {
8596                 TALLOC_FREE(info);
8597                 *count = 0;
8598                 return result;
8599         }
8600
8601         *info_p = info;
8602
8603         return WERR_OK;
8604 }
8605
8606 /****************************************************************************
8607  enumprintmonitors level 2.
8608 ****************************************************************************/
8609
8610 static WERROR enumprintmonitors_level_2(TALLOC_CTX *mem_ctx,
8611                                         union spoolss_MonitorInfo **info_p,
8612                                         uint32_t *count)
8613 {
8614         union spoolss_MonitorInfo *info;
8615         WERROR result = WERR_OK;
8616
8617         info = TALLOC_ARRAY(mem_ctx, union spoolss_MonitorInfo, 2);
8618         W_ERROR_HAVE_NO_MEMORY(info);
8619
8620         *count = 2;
8621
8622         result = fill_monitor_2(info, &info[0].info2,
8623                                 SPL_LOCAL_PORT,
8624                                 "Windows NT X86", /* FIXME */
8625                                 "localmon.dll");
8626         if (!W_ERROR_IS_OK(result)) {
8627                 goto out;
8628         }
8629
8630         result = fill_monitor_2(info, &info[1].info2,
8631                                 SPL_TCPIP_PORT,
8632                                 "Windows NT X86", /* FIXME */
8633                                 "tcpmon.dll");
8634         if (!W_ERROR_IS_OK(result)) {
8635                 goto out;
8636         }
8637
8638 out:
8639         if (!W_ERROR_IS_OK(result)) {
8640                 TALLOC_FREE(info);
8641                 *count = 0;
8642                 return result;
8643         }
8644
8645         *info_p = info;
8646
8647         return WERR_OK;
8648 }
8649
8650 /****************************************************************
8651  _spoolss_EnumMonitors
8652 ****************************************************************/
8653
8654 WERROR _spoolss_EnumMonitors(pipes_struct *p,
8655                              struct spoolss_EnumMonitors *r)
8656 {
8657         WERROR result;
8658
8659         /* that's an [in out] buffer */
8660
8661         if (!r->in.buffer && (r->in.offered != 0)) {
8662                 return WERR_INVALID_PARAM;
8663         }
8664
8665         DEBUG(5,("_spoolss_EnumMonitors\n"));
8666
8667         /*
8668          * Enumerate the print monitors ...
8669          *
8670          * Just reply with "Local Port", to keep NT happy
8671          * and I can use my nice printer checker.
8672          */
8673
8674         *r->out.count = 0;
8675         *r->out.needed = 0;
8676         *r->out.info = NULL;
8677
8678         switch (r->in.level) {
8679         case 1:
8680                 result = enumprintmonitors_level_1(p->mem_ctx, r->out.info,
8681                                                    r->out.count);
8682                 break;
8683         case 2:
8684                 result = enumprintmonitors_level_2(p->mem_ctx, r->out.info,
8685                                                    r->out.count);
8686                 break;
8687         default:
8688                 return WERR_UNKNOWN_LEVEL;
8689         }
8690
8691         if (!W_ERROR_IS_OK(result)) {
8692                 return result;
8693         }
8694
8695         *r->out.needed  = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
8696                                                      spoolss_EnumMonitors, NULL,
8697                                                      *r->out.info, r->in.level,
8698                                                      *r->out.count);
8699         *r->out.info    = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
8700         *r->out.count   = SPOOLSS_BUFFER_OK(*r->out.count, 0);
8701
8702         return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
8703 }
8704
8705 /****************************************************************************
8706 ****************************************************************************/
8707
8708 static WERROR getjob_level_1(TALLOC_CTX *mem_ctx,
8709                              const print_queue_struct *queue,
8710                              int count, int snum,
8711                              const NT_PRINTER_INFO_LEVEL *ntprinter,
8712                              uint32_t jobid,
8713                              struct spoolss_JobInfo1 *r)
8714 {
8715         int i = 0;
8716         bool found = false;
8717
8718         for (i=0; i<count && found == false; i++) {
8719                 if (queue[i].job == (int)jobid) {
8720                         found = true;
8721                 }
8722         }
8723
8724         if (found == false) {
8725                 /* NT treats not found as bad param... yet another bad choice */
8726                 return WERR_INVALID_PARAM;
8727         }
8728
8729         return fill_job_info1(mem_ctx,
8730                               r,
8731                               &queue[i-1],
8732                               i,
8733                               snum,
8734                               ntprinter);
8735 }
8736
8737 /****************************************************************************
8738 ****************************************************************************/
8739
8740 static WERROR getjob_level_2(TALLOC_CTX *mem_ctx,
8741                              const print_queue_struct *queue,
8742                              int count, int snum,
8743                              const NT_PRINTER_INFO_LEVEL *ntprinter,
8744                              uint32_t jobid,
8745                              struct spoolss_JobInfo2 *r)
8746 {
8747         int i = 0;
8748         bool found = false;
8749         struct spoolss_DeviceMode *devmode;
8750         NT_DEVICEMODE *nt_devmode;
8751         WERROR result;
8752
8753         for (i=0; i<count && found == false; i++) {
8754                 if (queue[i].job == (int)jobid) {
8755                         found = true;
8756                 }
8757         }
8758
8759         if (found == false) {
8760                 /* NT treats not found as bad param... yet another bad
8761                    choice */
8762                 return WERR_INVALID_PARAM;
8763         }
8764
8765         /*
8766          * if the print job does not have a DEVMODE associated with it,
8767          * just use the one for the printer. A NULL devicemode is not
8768          *  a failure condition
8769          */
8770
8771         nt_devmode = print_job_devmode(lp_const_servicename(snum), jobid);
8772         if (nt_devmode) {
8773                 devmode = TALLOC_ZERO_P(mem_ctx, struct spoolss_DeviceMode);
8774                 W_ERROR_HAVE_NO_MEMORY(devmode);
8775                 result = convert_nt_devicemode(devmode, devmode, nt_devmode);
8776                 if (!W_ERROR_IS_OK(result)) {
8777                         return result;
8778                 }
8779         } else {
8780                 devmode = construct_dev_mode(mem_ctx, lp_const_servicename(snum));
8781                 W_ERROR_HAVE_NO_MEMORY(devmode);
8782         }
8783
8784         return fill_job_info2(mem_ctx,
8785                               r,
8786                               &queue[i-1],
8787                               i,
8788                               snum,
8789                               ntprinter,
8790                               devmode);
8791 }
8792
8793 /****************************************************************
8794  _spoolss_GetJob
8795 ****************************************************************/
8796
8797 WERROR _spoolss_GetJob(pipes_struct *p,
8798                        struct spoolss_GetJob *r)
8799 {
8800         WERROR result = WERR_OK;
8801         NT_PRINTER_INFO_LEVEL *ntprinter = NULL;
8802         int snum;
8803         int count;
8804         print_queue_struct      *queue = NULL;
8805         print_status_struct prt_status;
8806
8807         /* that's an [in out] buffer */
8808
8809         if (!r->in.buffer && (r->in.offered != 0)) {
8810                 return WERR_INVALID_PARAM;
8811         }
8812
8813         DEBUG(5,("_spoolss_GetJob\n"));
8814
8815         *r->out.needed = 0;
8816
8817         if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
8818                 return WERR_BADFID;
8819         }
8820
8821         result = get_a_printer(NULL, &ntprinter, 2, lp_servicename(snum));
8822         if (!W_ERROR_IS_OK(result)) {
8823                 return result;
8824         }
8825
8826         count = print_queue_status(snum, &queue, &prt_status);
8827
8828         DEBUGADD(4,("count:[%d], prt_status:[%d], [%s]\n",
8829                      count, prt_status.status, prt_status.message));
8830
8831         switch (r->in.level) {
8832         case 1:
8833                 result = getjob_level_1(p->mem_ctx,
8834                                         queue, count, snum, ntprinter,
8835                                         r->in.job_id, &r->out.info->info1);
8836                 break;
8837         case 2:
8838                 result = getjob_level_2(p->mem_ctx,
8839                                         queue, count, snum, ntprinter,
8840                                         r->in.job_id, &r->out.info->info2);
8841                 break;
8842         default:
8843                 result = WERR_UNKNOWN_LEVEL;
8844                 break;
8845         }
8846
8847         SAFE_FREE(queue);
8848         free_a_printer(&ntprinter, 2);
8849
8850         if (!W_ERROR_IS_OK(result)) {
8851                 TALLOC_FREE(r->out.info);
8852                 return result;
8853         }
8854
8855         *r->out.needed  = SPOOLSS_BUFFER_UNION(spoolss_JobInfo, NULL,
8856                                                r->out.info, r->in.level);
8857         r->out.info     = SPOOLSS_BUFFER_OK(r->out.info, NULL);
8858
8859         return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
8860 }
8861
8862 /****************************************************************
8863  _spoolss_GetPrinterDataEx
8864
8865  From MSDN documentation of GetPrinterDataEx: pass request
8866  to GetPrinterData if key is "PrinterDriverData".
8867 ****************************************************************/
8868
8869 WERROR _spoolss_GetPrinterDataEx(pipes_struct *p,
8870                                  struct spoolss_GetPrinterDataEx *r)
8871 {
8872
8873         Printer_entry   *Printer = find_printer_index_by_hnd(p, r->in.handle);
8874         REGISTRY_VALUE          *val = NULL;
8875         NT_PRINTER_INFO_LEVEL   *printer = NULL;
8876         int                     snum = 0;
8877         WERROR result = WERR_OK;
8878
8879         DEBUG(4,("_spoolss_GetPrinterDataEx\n"));
8880
8881         DEBUG(10, ("_spoolss_GetPrinterDataEx: key => [%s], value => [%s]\n",
8882                 r->in.key_name, r->in.value_name));
8883
8884         /* in case of problem, return some default values */
8885
8886         *r->out.needed  = 0;
8887         *r->out.type    = REG_NONE;
8888
8889         if (!Printer) {
8890                 DEBUG(2,("_spoolss_GetPrinterDataEx: Invalid handle (%s:%u:%u).\n",
8891                         OUR_HANDLE(r->in.handle)));
8892                 result = WERR_BADFID;
8893                 goto done;
8894         }
8895
8896         /* Is the handle to a printer or to the server? */
8897
8898         if (Printer->printer_type == SPLHND_SERVER) {
8899                 DEBUG(10,("_spoolss_GetPrinterDataEx: "
8900                         "Not implemented for server handles yet\n"));
8901                 result = WERR_INVALID_PARAM;
8902                 goto done;
8903         }
8904
8905         if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
8906                 return WERR_BADFID;
8907         }
8908
8909         result = get_a_printer(Printer, &printer, 2, lp_servicename(snum));
8910         if (!W_ERROR_IS_OK(result)) {
8911                 goto done;
8912         }
8913
8914         /* check to see if the keyname is valid */
8915         if (!strlen(r->in.key_name)) {
8916                 result = WERR_INVALID_PARAM;
8917                 goto done;
8918         }
8919
8920         if (lookup_printerkey(printer->info_2->data, r->in.key_name) == -1) {
8921                 DEBUG(4,("_spoolss_GetPrinterDataEx: "
8922                         "Invalid keyname [%s]\n", r->in.key_name ));
8923                 result = WERR_BADFILE;
8924                 goto done;
8925         }
8926
8927         /* When given a new keyname, we should just create it */
8928
8929         val = get_printer_data(printer->info_2,
8930                                r->in.key_name, r->in.value_name);
8931         if (!val) {
8932                 result = WERR_BADFILE;
8933                 goto done;
8934         }
8935
8936         *r->out.needed = regval_size(val);
8937
8938         if (*r->out.needed > r->in.offered) {
8939                 result = WERR_MORE_DATA;
8940                 goto done;
8941         }
8942
8943         *r->out.type = regval_type(val);
8944
8945         memcpy(r->out.buffer, regval_data_p(val), regval_size(val));
8946
8947  done:
8948         if (printer) {
8949                 free_a_printer(&printer, 2);
8950         }
8951
8952         return result;
8953 }
8954
8955 /****************************************************************
8956  _spoolss_SetPrinterDataEx
8957 ****************************************************************/
8958
8959 WERROR _spoolss_SetPrinterDataEx(pipes_struct *p,
8960                                  struct spoolss_SetPrinterDataEx *r)
8961 {
8962         NT_PRINTER_INFO_LEVEL   *printer = NULL;
8963         int                     snum = 0;
8964         WERROR                  result = WERR_OK;
8965         Printer_entry           *Printer = find_printer_index_by_hnd(p, r->in.handle);
8966         char                    *oid_string;
8967
8968         DEBUG(4,("_spoolss_SetPrinterDataEx\n"));
8969
8970         /* From MSDN documentation of SetPrinterDataEx: pass request to
8971            SetPrinterData if key is "PrinterDriverData" */
8972
8973         if (!Printer) {
8974                 DEBUG(2,("_spoolss_SetPrinterDataEx: Invalid handle (%s:%u:%u).\n",
8975                         OUR_HANDLE(r->in.handle)));
8976                 return WERR_BADFID;
8977         }
8978
8979         if (Printer->printer_type == SPLHND_SERVER) {
8980                 DEBUG(10,("_spoolss_SetPrinterDataEx: "
8981                         "Not implemented for server handles yet\n"));
8982                 return WERR_INVALID_PARAM;
8983         }
8984
8985         if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
8986                 return WERR_BADFID;
8987         }
8988
8989         /*
8990          * Access check : NT returns "access denied" if you make a
8991          * SetPrinterData call without the necessary privildge.
8992          * we were originally returning OK if nothing changed
8993          * which made Win2k issue **a lot** of SetPrinterData
8994          * when connecting to a printer  --jerry
8995          */
8996
8997         if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
8998                 DEBUG(3, ("_spoolss_SetPrinterDataEx: "
8999                         "change denied by handle access permissions\n"));
9000                 return WERR_ACCESS_DENIED;
9001         }
9002
9003         result = get_a_printer(Printer, &printer, 2, lp_servicename(snum));
9004         if (!W_ERROR_IS_OK(result)) {
9005                 return result;
9006         }
9007
9008         /* check for OID in valuename */
9009
9010         oid_string = strchr(r->in.value_name, ',');
9011         if (oid_string) {
9012                 *oid_string = '\0';
9013                 oid_string++;
9014         }
9015
9016         /* save the registry data */
9017
9018         result = set_printer_dataex(printer, r->in.key_name, r->in.value_name,
9019                                     r->in.type, r->in.buffer, r->in.offered);
9020
9021         if (W_ERROR_IS_OK(result)) {
9022                 /* save the OID if one was specified */
9023                 if (oid_string) {
9024                         char *str = talloc_asprintf(p->mem_ctx, "%s\\%s",
9025                                 r->in.key_name, SPOOL_OID_KEY);
9026                         if (!str) {
9027                                 result = WERR_NOMEM;
9028                                 goto done;
9029                         }
9030
9031                         /*
9032                          * I'm not checking the status here on purpose.  Don't know
9033                          * if this is right, but I'm returning the status from the
9034                          * previous set_printer_dataex() call.  I have no idea if
9035                          * this is right.    --jerry
9036                          */
9037
9038                         set_printer_dataex(printer, str, r->in.value_name,
9039                                            REG_SZ, (uint8_t *)oid_string,
9040                                            strlen(oid_string)+1);
9041                 }
9042
9043                 result = mod_a_printer(printer, 2);
9044         }
9045
9046  done:
9047         free_a_printer(&printer, 2);
9048
9049         return result;
9050 }
9051
9052 /****************************************************************
9053  _spoolss_DeletePrinterDataEx
9054 ****************************************************************/
9055
9056 WERROR _spoolss_DeletePrinterDataEx(pipes_struct *p,
9057                                     struct spoolss_DeletePrinterDataEx *r)
9058 {
9059         NT_PRINTER_INFO_LEVEL   *printer = NULL;
9060         int             snum=0;
9061         WERROR          status = WERR_OK;
9062         Printer_entry   *Printer = find_printer_index_by_hnd(p, r->in.handle);
9063
9064         DEBUG(5,("_spoolss_DeletePrinterDataEx\n"));
9065
9066         if (!Printer) {
9067                 DEBUG(2,("_spoolss_DeletePrinterDataEx: "
9068                         "Invalid handle (%s:%u:%u).\n",
9069                         OUR_HANDLE(r->in.handle)));
9070                 return WERR_BADFID;
9071         }
9072
9073         if (!get_printer_snum(p, r->in.handle, &snum, NULL))
9074                 return WERR_BADFID;
9075
9076         if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
9077                 DEBUG(3, ("_spoolss_DeletePrinterDataEx: "
9078                         "printer properties change denied by handle\n"));
9079                 return WERR_ACCESS_DENIED;
9080         }
9081
9082         if (!r->in.value_name || !r->in.key_name) {
9083                 return WERR_NOMEM;
9084         }
9085
9086         status = get_a_printer(Printer, &printer, 2, lp_const_servicename(snum));
9087         if (!W_ERROR_IS_OK(status))
9088                 return status;
9089
9090         status = delete_printer_dataex( printer, r->in.key_name, r->in.value_name );
9091
9092         if ( W_ERROR_IS_OK(status) )
9093                 mod_a_printer( printer, 2 );
9094
9095         free_a_printer(&printer, 2);
9096
9097         return status;
9098 }
9099
9100 /****************************************************************
9101  _spoolss_EnumPrinterKey
9102 ****************************************************************/
9103
9104 WERROR _spoolss_EnumPrinterKey(pipes_struct *p,
9105                                struct spoolss_EnumPrinterKey *r)
9106 {
9107         fstring         *keynames = NULL;
9108         int             num_keys;
9109         Printer_entry   *Printer = find_printer_index_by_hnd(p, r->in.handle);
9110         NT_PRINTER_DATA *data;
9111         NT_PRINTER_INFO_LEVEL   *printer = NULL;
9112         int             snum = 0;
9113         WERROR          result = WERR_BADFILE;
9114         int i;
9115         const char **array = NULL;
9116
9117
9118         DEBUG(4,("_spoolss_EnumPrinterKey\n"));
9119
9120         if (!Printer) {
9121                 DEBUG(2,("_spoolss_EnumPrinterKey: Invalid handle (%s:%u:%u).\n",
9122                         OUR_HANDLE(r->in.handle)));
9123                 return WERR_BADFID;
9124         }
9125
9126         if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
9127                 return WERR_BADFID;
9128         }
9129
9130         result = get_a_printer(Printer, &printer, 2, lp_const_servicename(snum));
9131         if (!W_ERROR_IS_OK(result)) {
9132                 return result;
9133         }
9134
9135         /* get the list of subkey names */
9136
9137         data = printer->info_2->data;
9138
9139         num_keys = get_printer_subkeys(data, r->in.key_name, &keynames);
9140         if (num_keys == -1) {
9141                 result = WERR_BADFILE;
9142                 goto done;
9143         }
9144
9145         *r->out.needed = 4;
9146
9147         array = talloc_zero_array(r->out.key_buffer, const char *, num_keys + 1);
9148         if (!array) {
9149                 result = WERR_NOMEM;
9150                 goto done;
9151         }
9152
9153         for (i=0; i < num_keys; i++) {
9154                 array[i] = talloc_strdup(array, keynames[i]);
9155                 if (!array[i]) {
9156                         result = WERR_NOMEM;
9157                         goto done;
9158                 }
9159
9160                 *r->out.needed += strlen_m_term(keynames[i]) * 2;
9161         }
9162
9163         if (r->in.offered < *r->out.needed) {
9164                 result = WERR_MORE_DATA;
9165                 goto done;
9166         }
9167
9168         result = WERR_OK;
9169
9170         *r->out.key_buffer = array;
9171
9172  done:
9173         if (!W_ERROR_IS_OK(result)) {
9174                 TALLOC_FREE(array);
9175                 ZERO_STRUCTP(r->out.key_buffer);
9176         }
9177
9178         free_a_printer(&printer, 2);
9179         SAFE_FREE(keynames);
9180
9181         return result;
9182 }
9183
9184 /****************************************************************
9185  _spoolss_DeletePrinterKey
9186 ****************************************************************/
9187
9188 WERROR _spoolss_DeletePrinterKey(pipes_struct *p,
9189                                  struct spoolss_DeletePrinterKey *r)
9190 {
9191         Printer_entry           *Printer = find_printer_index_by_hnd(p, r->in.handle);
9192         NT_PRINTER_INFO_LEVEL   *printer = NULL;
9193         int                     snum=0;
9194         WERROR                  status;
9195
9196         DEBUG(5,("_spoolss_DeletePrinterKey\n"));
9197
9198         if (!Printer) {
9199                 DEBUG(2,("_spoolss_DeletePrinterKey: Invalid handle (%s:%u:%u).\n",
9200                         OUR_HANDLE(r->in.handle)));
9201                 return WERR_BADFID;
9202         }
9203
9204         /* if keyname == NULL, return error */
9205
9206         if ( !r->in.key_name )
9207                 return WERR_INVALID_PARAM;
9208
9209         if (!get_printer_snum(p, r->in.handle, &snum, NULL))
9210                 return WERR_BADFID;
9211
9212         if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
9213                 DEBUG(3, ("_spoolss_DeletePrinterKey: "
9214                         "printer properties change denied by handle\n"));
9215                 return WERR_ACCESS_DENIED;
9216         }
9217
9218         status = get_a_printer(Printer, &printer, 2, lp_const_servicename(snum));
9219         if (!W_ERROR_IS_OK(status))
9220                 return status;
9221
9222         /* delete the key and all subneys */
9223
9224         status = delete_all_printer_data( printer->info_2, r->in.key_name );
9225
9226         if ( W_ERROR_IS_OK(status) )
9227                 status = mod_a_printer(printer, 2);
9228
9229         free_a_printer( &printer, 2 );
9230
9231         return status;
9232 }
9233
9234 /****************************************************************
9235 ****************************************************************/
9236
9237 static WERROR registry_value_to_printer_enum_value(TALLOC_CTX *mem_ctx,
9238                                                    REGISTRY_VALUE *v,
9239                                                    struct spoolss_PrinterEnumValues *r)
9240 {
9241         WERROR result;
9242
9243         r->data = TALLOC_ZERO_P(mem_ctx, union spoolss_PrinterData);
9244         W_ERROR_HAVE_NO_MEMORY(r->data);
9245
9246         r->value_name   = talloc_strdup(mem_ctx, regval_name(v));
9247         W_ERROR_HAVE_NO_MEMORY(r->value_name);
9248
9249         r->type         = regval_type(v);
9250         r->data_length  = regval_size(v);
9251
9252         if (r->data_length) {
9253                 DATA_BLOB blob = data_blob_const(regval_data_p(v),
9254                                                  regval_size(v));
9255                 result = pull_spoolss_PrinterData(mem_ctx, &blob,
9256                                                   r->data,
9257                                                   r->type);
9258                 if (!W_ERROR_IS_OK(result)) {
9259                         return result;
9260                 }
9261         }
9262
9263         return WERR_OK;
9264 }
9265
9266 /****************************************************************
9267  _spoolss_EnumPrinterDataEx
9268 ****************************************************************/
9269
9270 WERROR _spoolss_EnumPrinterDataEx(pipes_struct *p,
9271                                   struct spoolss_EnumPrinterDataEx *r)
9272 {
9273         uint32_t        count = 0;
9274         NT_PRINTER_INFO_LEVEL   *printer = NULL;
9275         struct spoolss_PrinterEnumValues *info = NULL;
9276         NT_PRINTER_DATA         *p_data;
9277         Printer_entry   *Printer = find_printer_index_by_hnd(p, r->in.handle);
9278         int             snum;
9279         WERROR          result;
9280         int             key_index;
9281         int             i;
9282
9283         DEBUG(4,("_spoolss_EnumPrinterDataEx\n"));
9284
9285         *r->out.count = 0;
9286         *r->out.needed = 0;
9287         *r->out.info = NULL;
9288
9289         if (!Printer) {
9290                 DEBUG(2,("_spoolss_EnumPrinterDataEx: Invalid handle (%s:%u:%u1<).\n",
9291                         OUR_HANDLE(r->in.handle)));
9292                 return WERR_BADFID;
9293         }
9294
9295         /*
9296          * first check for a keyname of NULL or "".  Win2k seems to send
9297          * this a lot and we should send back WERR_INVALID_PARAM
9298          * no need to spend time looking up the printer in this case.
9299          * --jerry
9300          */
9301
9302         if (!strlen(r->in.key_name)) {
9303                 result = WERR_INVALID_PARAM;
9304                 goto done;
9305         }
9306
9307         /* get the printer off of disk */
9308
9309         if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
9310                 return WERR_BADFID;
9311         }
9312
9313         ZERO_STRUCT(printer);
9314         result = get_a_printer(Printer, &printer, 2, lp_const_servicename(snum));
9315         if (!W_ERROR_IS_OK(result)) {
9316                 return result;
9317         }
9318
9319         /* now look for a match on the key name */
9320
9321         p_data = printer->info_2->data;
9322
9323         key_index = lookup_printerkey(p_data, r->in.key_name);
9324         if (key_index == -1) {
9325                 DEBUG(10,("_spoolss_EnumPrinterDataEx: Unknown keyname [%s]\n",
9326                         r->in.key_name));
9327                 result = WERR_INVALID_PARAM;
9328                 goto done;
9329         }
9330
9331         /* allocate the memory for the array of pointers -- if necessary */
9332
9333         count = regval_ctr_numvals(p_data->keys[key_index].values);
9334         if (!count) {
9335                 result = WERR_OK; /* ??? */
9336                 goto done;
9337         }
9338
9339         info = TALLOC_ZERO_ARRAY(p->mem_ctx,
9340                                  struct spoolss_PrinterEnumValues,
9341                                  count);
9342         if (!info) {
9343                 DEBUG(0,("_spoolss_EnumPrinterDataEx: talloc() failed\n"));
9344                 result = WERR_NOMEM;
9345                 goto done;
9346         }
9347
9348         /*
9349          * loop through all params and build the array to pass
9350          * back to the  client
9351          */
9352
9353         for (i=0; i < count; i++) {
9354
9355                 REGISTRY_VALUE  *val;
9356
9357                 /* lookup the registry value */
9358
9359                 val = regval_ctr_specific_value(p_data->keys[key_index].values, i);
9360
9361                 DEBUG(10,("retrieved value number [%d] [%s]\n", i, regval_name(val)));
9362
9363                 /* copy the data */
9364
9365                 result = registry_value_to_printer_enum_value(info, val, &info[i]);
9366                 if (!W_ERROR_IS_OK(result)) {
9367                         goto done;
9368                 }
9369         }
9370
9371 #if 0 /* FIXME - gd */
9372         /* housekeeping information in the reply */
9373
9374         /* Fix from Martin Zielinski <mz@seh.de> - ensure
9375          * the hand marshalled container size is a multiple
9376          * of 4 bytes for RPC alignment.
9377          */
9378
9379         if (needed % 4) {
9380                 needed += 4-(needed % 4);
9381         }
9382 #endif
9383         *r->out.count   = count;
9384         *r->out.info    = info;
9385
9386  done:
9387
9388         if (printer) {
9389                 free_a_printer(&printer, 2);
9390         }
9391
9392         if (!W_ERROR_IS_OK(result)) {
9393                 return result;
9394         }
9395
9396         *r->out.needed  = SPOOLSS_BUFFER_ARRAY(p->mem_ctx,
9397                                                spoolss_EnumPrinterDataEx, NULL,
9398                                                *r->out.info,
9399                                                *r->out.count);
9400         *r->out.info    = SPOOLSS_BUFFER_OK(*r->out.info, NULL);
9401         *r->out.count   = SPOOLSS_BUFFER_OK(*r->out.count, *r->out.count);
9402
9403         return SPOOLSS_BUFFER_OK(WERR_OK, WERR_MORE_DATA);
9404 }
9405
9406 /****************************************************************************
9407 ****************************************************************************/
9408
9409 static WERROR getprintprocessordirectory_level_1(TALLOC_CTX *mem_ctx,
9410                                                  const char *servername,
9411                                                  const char *environment,
9412                                                  struct spoolss_PrintProcessorDirectoryInfo1 *r)
9413 {
9414         WERROR werr;
9415         char *path = NULL;
9416
9417         werr = compose_spoolss_server_path(mem_ctx,
9418                                            servername,
9419                                            environment,
9420                                            SPOOLSS_PRTPROCS_PATH,
9421                                            &path);
9422         if (!W_ERROR_IS_OK(werr)) {
9423                 return werr;
9424         }
9425
9426         DEBUG(4,("print processor directory: [%s]\n", path));
9427
9428         r->directory_name = path;
9429
9430         return WERR_OK;
9431 }
9432
9433 /****************************************************************
9434  _spoolss_GetPrintProcessorDirectory
9435 ****************************************************************/
9436
9437 WERROR _spoolss_GetPrintProcessorDirectory(pipes_struct *p,
9438                                            struct spoolss_GetPrintProcessorDirectory *r)
9439 {
9440         WERROR result;
9441
9442         /* that's an [in out] buffer */
9443
9444         if (!r->in.buffer && (r->in.offered != 0)) {
9445                 return WERR_INVALID_PARAM;
9446         }
9447
9448         DEBUG(5,("_spoolss_GetPrintProcessorDirectory: level %d\n",
9449                 r->in.level));
9450
9451         *r->out.needed = 0;
9452
9453         /* r->in.level is ignored */
9454
9455         result = getprintprocessordirectory_level_1(p->mem_ctx,
9456                                                     r->in.server,
9457                                                     r->in.environment,
9458                                                     &r->out.info->info1);
9459         if (!W_ERROR_IS_OK(result)) {
9460                 TALLOC_FREE(r->out.info);
9461                 return result;
9462         }
9463
9464         *r->out.needed  = SPOOLSS_BUFFER_UNION(spoolss_PrintProcessorDirectoryInfo, NULL,
9465                                                r->out.info, r->in.level);
9466         r->out.info     = SPOOLSS_BUFFER_OK(r->out.info, NULL);
9467
9468         return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
9469 }
9470
9471 /*******************************************************************
9472  ********************************************************************/
9473
9474 static bool push_monitorui_buf(TALLOC_CTX *mem_ctx, DATA_BLOB *buf,
9475                                const char *dllname)
9476 {
9477         enum ndr_err_code ndr_err;
9478         struct spoolss_MonitorUi ui;
9479
9480         ui.dll_name = dllname;
9481
9482         ndr_err = ndr_push_struct_blob(buf, mem_ctx, NULL, &ui,
9483                        (ndr_push_flags_fn_t)ndr_push_spoolss_MonitorUi);
9484         if (NDR_ERR_CODE_IS_SUCCESS(ndr_err) && (DEBUGLEVEL >= 10)) {
9485                 NDR_PRINT_DEBUG(spoolss_MonitorUi, &ui);
9486         }
9487         return NDR_ERR_CODE_IS_SUCCESS(ndr_err);
9488 }
9489
9490 /*******************************************************************
9491  Streams the monitor UI DLL name in UNICODE
9492 *******************************************************************/
9493
9494 static WERROR xcvtcp_monitorui(TALLOC_CTX *mem_ctx,
9495                                NT_USER_TOKEN *token, DATA_BLOB *in,
9496                                DATA_BLOB *out, uint32_t *needed)
9497 {
9498         const char *dllname = "tcpmonui.dll";
9499
9500         *needed = (strlen(dllname)+1) * 2;
9501
9502         if (out->length < *needed) {
9503                 return WERR_INSUFFICIENT_BUFFER;
9504         }
9505
9506         if (!push_monitorui_buf(mem_ctx, out, dllname)) {
9507                 return WERR_NOMEM;
9508         }
9509
9510         return WERR_OK;
9511 }
9512
9513 /*******************************************************************
9514  ********************************************************************/
9515
9516 static bool pull_port_data_1(TALLOC_CTX *mem_ctx,
9517                              struct spoolss_PortData1 *port1,
9518                              const DATA_BLOB *buf)
9519 {
9520         enum ndr_err_code ndr_err;
9521         ndr_err = ndr_pull_struct_blob(buf, mem_ctx, NULL, port1,
9522                        (ndr_pull_flags_fn_t)ndr_pull_spoolss_PortData1);
9523         if (NDR_ERR_CODE_IS_SUCCESS(ndr_err) && (DEBUGLEVEL >= 10)) {
9524                 NDR_PRINT_DEBUG(spoolss_PortData1, port1);
9525         }
9526         return NDR_ERR_CODE_IS_SUCCESS(ndr_err);
9527 }
9528
9529 /*******************************************************************
9530  ********************************************************************/
9531
9532 static bool pull_port_data_2(TALLOC_CTX *mem_ctx,
9533                              struct spoolss_PortData2 *port2,
9534                              const DATA_BLOB *buf)
9535 {
9536         enum ndr_err_code ndr_err;
9537         ndr_err = ndr_pull_struct_blob(buf, mem_ctx, NULL, port2,
9538                        (ndr_pull_flags_fn_t)ndr_pull_spoolss_PortData2);
9539         if (NDR_ERR_CODE_IS_SUCCESS(ndr_err) && (DEBUGLEVEL >= 10)) {
9540                 NDR_PRINT_DEBUG(spoolss_PortData2, port2);
9541         }
9542         return NDR_ERR_CODE_IS_SUCCESS(ndr_err);
9543 }
9544
9545 /*******************************************************************
9546  Create a new TCP/IP port
9547 *******************************************************************/
9548
9549 static WERROR xcvtcp_addport(TALLOC_CTX *mem_ctx,
9550                              NT_USER_TOKEN *token, DATA_BLOB *in,
9551                              DATA_BLOB *out, uint32_t *needed)
9552 {
9553         struct spoolss_PortData1 port1;
9554         struct spoolss_PortData2 port2;
9555         char *device_uri = NULL;
9556         uint32_t version;
9557
9558         const char *portname;
9559         const char *hostaddress;
9560         const char *queue;
9561         uint32_t port_number;
9562         uint32_t protocol;
9563
9564         /* peek for spoolss_PortData version */
9565
9566         if (!in || (in->length < (128 + 4))) {
9567                 return WERR_GENERAL_FAILURE;
9568         }
9569
9570         version = IVAL(in->data, 128);
9571
9572         switch (version) {
9573                 case 1:
9574                         ZERO_STRUCT(port1);
9575
9576                         if (!pull_port_data_1(mem_ctx, &port1, in)) {
9577                                 return WERR_NOMEM;
9578                         }
9579
9580                         portname        = port1.portname;
9581                         hostaddress     = port1.hostaddress;
9582                         queue           = port1.queue;
9583                         protocol        = port1.protocol;
9584                         port_number     = port1.port_number;
9585
9586                         break;
9587                 case 2:
9588                         ZERO_STRUCT(port2);
9589
9590                         if (!pull_port_data_2(mem_ctx, &port2, in)) {
9591                                 return WERR_NOMEM;
9592                         }
9593
9594                         portname        = port2.portname;
9595                         hostaddress     = port2.hostaddress;
9596                         queue           = port2.queue;
9597                         protocol        = port2.protocol;
9598                         port_number     = port2.port_number;
9599
9600                         break;
9601                 default:
9602                         DEBUG(1,("xcvtcp_addport: "
9603                                 "unknown version of port_data: %d\n", version));
9604                         return WERR_UNKNOWN_PORT;
9605         }
9606
9607         /* create the device URI and call the add_port_hook() */
9608
9609         switch (protocol) {
9610         case PROTOCOL_RAWTCP_TYPE:
9611                 device_uri = talloc_asprintf(mem_ctx,
9612                                 "socket://%s:%d/", hostaddress,
9613                                 port_number);
9614                 break;
9615
9616         case PROTOCOL_LPR_TYPE:
9617                 device_uri = talloc_asprintf(mem_ctx,
9618                         "lpr://%s/%s", hostaddress, queue );
9619                 break;
9620
9621         default:
9622                 return WERR_UNKNOWN_PORT;
9623         }
9624
9625         if (!device_uri) {
9626                 return WERR_NOMEM;
9627         }
9628
9629         return add_port_hook(mem_ctx, token, portname, device_uri);
9630 }
9631
9632 /*******************************************************************
9633 *******************************************************************/
9634
9635 struct xcv_api_table xcvtcp_cmds[] = {
9636         { "MonitorUI",  xcvtcp_monitorui },
9637         { "AddPort",    xcvtcp_addport},
9638         { NULL,         NULL }
9639 };
9640
9641 static WERROR process_xcvtcp_command(TALLOC_CTX *mem_ctx,
9642                                      NT_USER_TOKEN *token, const char *command,
9643                                      DATA_BLOB *inbuf,
9644                                      DATA_BLOB *outbuf,
9645                                      uint32_t *needed )
9646 {
9647         int i;
9648
9649         DEBUG(10,("process_xcvtcp_command: Received command \"%s\"\n", command));
9650
9651         for ( i=0; xcvtcp_cmds[i].name; i++ ) {
9652                 if ( strcmp( command, xcvtcp_cmds[i].name ) == 0 )
9653                         return xcvtcp_cmds[i].fn(mem_ctx, token, inbuf, outbuf, needed);
9654         }
9655
9656         return WERR_BADFUNC;
9657 }
9658
9659 /*******************************************************************
9660 *******************************************************************/
9661 #if 0   /* don't support management using the "Local Port" monitor */
9662
9663 static WERROR xcvlocal_monitorui(TALLOC_CTX *mem_ctx,
9664                                  NT_USER_TOKEN *token, DATA_BLOB *in,
9665                                  DATA_BLOB *out, uint32_t *needed)
9666 {
9667         const char *dllname = "localui.dll";
9668
9669         *needed = (strlen(dllname)+1) * 2;
9670
9671         if (out->length < *needed) {
9672                 return WERR_INSUFFICIENT_BUFFER;
9673         }
9674
9675         if (!push_monitorui_buf(mem_ctx, out, dllname)) {
9676                 return WERR_NOMEM;
9677         }
9678
9679         return WERR_OK;
9680 }
9681
9682 /*******************************************************************
9683 *******************************************************************/
9684
9685 struct xcv_api_table xcvlocal_cmds[] = {
9686         { "MonitorUI",  xcvlocal_monitorui },
9687         { NULL,         NULL }
9688 };
9689 #else
9690 struct xcv_api_table xcvlocal_cmds[] = {
9691         { NULL,         NULL }
9692 };
9693 #endif
9694
9695
9696
9697 /*******************************************************************
9698 *******************************************************************/
9699
9700 static WERROR process_xcvlocal_command(TALLOC_CTX *mem_ctx,
9701                                        NT_USER_TOKEN *token, const char *command,
9702                                        DATA_BLOB *inbuf, DATA_BLOB *outbuf,
9703                                        uint32_t *needed)
9704 {
9705         int i;
9706
9707         DEBUG(10,("process_xcvlocal_command: Received command \"%s\"\n", command));
9708
9709         for ( i=0; xcvlocal_cmds[i].name; i++ ) {
9710                 if ( strcmp( command, xcvlocal_cmds[i].name ) == 0 )
9711                         return xcvlocal_cmds[i].fn(mem_ctx, token, inbuf, outbuf, needed);
9712         }
9713         return WERR_BADFUNC;
9714 }
9715
9716 /****************************************************************
9717  _spoolss_XcvData
9718 ****************************************************************/
9719
9720 WERROR _spoolss_XcvData(pipes_struct *p,
9721                         struct spoolss_XcvData *r)
9722 {
9723         Printer_entry *Printer = find_printer_index_by_hnd(p, r->in.handle);
9724         DATA_BLOB out_data = data_blob_null;
9725         WERROR werror;
9726
9727         if (!Printer) {
9728                 DEBUG(2,("_spoolss_XcvData: Invalid handle (%s:%u:%u).\n",
9729                         OUR_HANDLE(r->in.handle)));
9730                 return WERR_BADFID;
9731         }
9732
9733         /* Has to be a handle to the TCP/IP port monitor */
9734
9735         if ( !(Printer->printer_type & (SPLHND_PORTMON_LOCAL|SPLHND_PORTMON_TCP)) ) {
9736                 DEBUG(2,("_spoolss_XcvData: Call only valid for Port Monitors\n"));
9737                 return WERR_BADFID;
9738         }
9739
9740         /* requires administrative access to the server */
9741
9742         if ( !(Printer->access_granted & SERVER_ACCESS_ADMINISTER) ) {
9743                 DEBUG(2,("_spoolss_XcvData: denied by handle permissions.\n"));
9744                 return WERR_ACCESS_DENIED;
9745         }
9746
9747         /* Allocate the outgoing buffer */
9748
9749         if (r->in.out_data_size) {
9750                 out_data = data_blob_talloc_zero(p->mem_ctx, r->in.out_data_size);
9751                 if (out_data.data == NULL) {
9752                         return WERR_NOMEM;
9753                 }
9754         }
9755
9756         switch ( Printer->printer_type ) {
9757         case SPLHND_PORTMON_TCP:
9758                 werror = process_xcvtcp_command(p->mem_ctx,
9759                                                 p->server_info->ptok,
9760                                                 r->in.function_name,
9761                                                 &r->in.in_data, &out_data,
9762                                                 r->out.needed);
9763                 break;
9764         case SPLHND_PORTMON_LOCAL:
9765                 werror = process_xcvlocal_command(p->mem_ctx,
9766                                                   p->server_info->ptok,
9767                                                   r->in.function_name,
9768                                                   &r->in.in_data, &out_data,
9769                                                   r->out.needed);
9770                 break;
9771         default:
9772                 werror = WERR_INVALID_PRINT_MONITOR;
9773         }
9774
9775         if (!W_ERROR_IS_OK(werror)) {
9776                 return werror;
9777         }
9778
9779         *r->out.status_code = 0;
9780
9781         memcpy(r->out.out_data, out_data.data, out_data.length);
9782
9783         return WERR_OK;
9784 }
9785
9786 /****************************************************************
9787  _spoolss_AddPrintProcessor
9788 ****************************************************************/
9789
9790 WERROR _spoolss_AddPrintProcessor(pipes_struct *p,
9791                                   struct spoolss_AddPrintProcessor *r)
9792 {
9793         /* for now, just indicate success and ignore the add.  We'll
9794            automatically set the winprint processor for printer
9795            entries later.  Used to debug the LexMark Optra S 1855 PCL
9796            driver --jerry */
9797
9798         return WERR_OK;
9799 }
9800
9801 /****************************************************************
9802  _spoolss_AddPrinter
9803 ****************************************************************/
9804
9805 WERROR _spoolss_AddPrinter(pipes_struct *p,
9806                            struct spoolss_AddPrinter *r)
9807 {
9808         p->rng_fault_state = true;
9809         return WERR_NOT_SUPPORTED;
9810 }
9811
9812 /****************************************************************
9813  _spoolss_GetPrinterDriver
9814 ****************************************************************/
9815
9816 WERROR _spoolss_GetPrinterDriver(pipes_struct *p,
9817                                  struct spoolss_GetPrinterDriver *r)
9818 {
9819         p->rng_fault_state = true;
9820         return WERR_NOT_SUPPORTED;
9821 }
9822
9823 /****************************************************************
9824  _spoolss_ReadPrinter
9825 ****************************************************************/
9826
9827 WERROR _spoolss_ReadPrinter(pipes_struct *p,
9828                             struct spoolss_ReadPrinter *r)
9829 {
9830         p->rng_fault_state = true;
9831         return WERR_NOT_SUPPORTED;
9832 }
9833
9834 /****************************************************************
9835  _spoolss_WaitForPrinterChange
9836 ****************************************************************/
9837
9838 WERROR _spoolss_WaitForPrinterChange(pipes_struct *p,
9839                                      struct spoolss_WaitForPrinterChange *r)
9840 {
9841         p->rng_fault_state = true;
9842         return WERR_NOT_SUPPORTED;
9843 }
9844
9845 /****************************************************************
9846  _spoolss_AddPort
9847 ****************************************************************/
9848
9849 WERROR _spoolss_AddPort(pipes_struct *p,
9850                         struct spoolss_AddPort *r)
9851 {
9852         p->rng_fault_state = true;
9853         return WERR_NOT_SUPPORTED;
9854 }
9855
9856 /****************************************************************
9857  _spoolss_ConfigurePort
9858 ****************************************************************/
9859
9860 WERROR _spoolss_ConfigurePort(pipes_struct *p,
9861                               struct spoolss_ConfigurePort *r)
9862 {
9863         p->rng_fault_state = true;
9864         return WERR_NOT_SUPPORTED;
9865 }
9866
9867 /****************************************************************
9868  _spoolss_DeletePort
9869 ****************************************************************/
9870
9871 WERROR _spoolss_DeletePort(pipes_struct *p,
9872                            struct spoolss_DeletePort *r)
9873 {
9874         p->rng_fault_state = true;
9875         return WERR_NOT_SUPPORTED;
9876 }
9877
9878 /****************************************************************
9879  _spoolss_CreatePrinterIC
9880 ****************************************************************/
9881
9882 WERROR _spoolss_CreatePrinterIC(pipes_struct *p,
9883                                 struct spoolss_CreatePrinterIC *r)
9884 {
9885         p->rng_fault_state = true;
9886         return WERR_NOT_SUPPORTED;
9887 }
9888
9889 /****************************************************************
9890  _spoolss_PlayGDIScriptOnPrinterIC
9891 ****************************************************************/
9892
9893 WERROR _spoolss_PlayGDIScriptOnPrinterIC(pipes_struct *p,
9894                                          struct spoolss_PlayGDIScriptOnPrinterIC *r)
9895 {
9896         p->rng_fault_state = true;
9897         return WERR_NOT_SUPPORTED;
9898 }
9899
9900 /****************************************************************
9901  _spoolss_DeletePrinterIC
9902 ****************************************************************/
9903
9904 WERROR _spoolss_DeletePrinterIC(pipes_struct *p,
9905                                 struct spoolss_DeletePrinterIC *r)
9906 {
9907         p->rng_fault_state = true;
9908         return WERR_NOT_SUPPORTED;
9909 }
9910
9911 /****************************************************************
9912  _spoolss_AddPrinterConnection
9913 ****************************************************************/
9914
9915 WERROR _spoolss_AddPrinterConnection(pipes_struct *p,
9916                                      struct spoolss_AddPrinterConnection *r)
9917 {
9918         p->rng_fault_state = true;
9919         return WERR_NOT_SUPPORTED;
9920 }
9921
9922 /****************************************************************
9923  _spoolss_DeletePrinterConnection
9924 ****************************************************************/
9925
9926 WERROR _spoolss_DeletePrinterConnection(pipes_struct *p,
9927                                         struct spoolss_DeletePrinterConnection *r)
9928 {
9929         p->rng_fault_state = true;
9930         return WERR_NOT_SUPPORTED;
9931 }
9932
9933 /****************************************************************
9934  _spoolss_PrinterMessageBox
9935 ****************************************************************/
9936
9937 WERROR _spoolss_PrinterMessageBox(pipes_struct *p,
9938                                   struct spoolss_PrinterMessageBox *r)
9939 {
9940         p->rng_fault_state = true;
9941         return WERR_NOT_SUPPORTED;
9942 }
9943
9944 /****************************************************************
9945  _spoolss_AddMonitor
9946 ****************************************************************/
9947
9948 WERROR _spoolss_AddMonitor(pipes_struct *p,
9949                            struct spoolss_AddMonitor *r)
9950 {
9951         p->rng_fault_state = true;
9952         return WERR_NOT_SUPPORTED;
9953 }
9954
9955 /****************************************************************
9956  _spoolss_DeleteMonitor
9957 ****************************************************************/
9958
9959 WERROR _spoolss_DeleteMonitor(pipes_struct *p,
9960                               struct spoolss_DeleteMonitor *r)
9961 {
9962         p->rng_fault_state = true;
9963         return WERR_NOT_SUPPORTED;
9964 }
9965
9966 /****************************************************************
9967  _spoolss_DeletePrintProcessor
9968 ****************************************************************/
9969
9970 WERROR _spoolss_DeletePrintProcessor(pipes_struct *p,
9971                                      struct spoolss_DeletePrintProcessor *r)
9972 {
9973         p->rng_fault_state = true;
9974         return WERR_NOT_SUPPORTED;
9975 }
9976
9977 /****************************************************************
9978  _spoolss_AddPrintProvidor
9979 ****************************************************************/
9980
9981 WERROR _spoolss_AddPrintProvidor(pipes_struct *p,
9982                                  struct spoolss_AddPrintProvidor *r)
9983 {
9984         p->rng_fault_state = true;
9985         return WERR_NOT_SUPPORTED;
9986 }
9987
9988 /****************************************************************
9989  _spoolss_DeletePrintProvidor
9990 ****************************************************************/
9991
9992 WERROR _spoolss_DeletePrintProvidor(pipes_struct *p,
9993                                     struct spoolss_DeletePrintProvidor *r)
9994 {
9995         p->rng_fault_state = true;
9996         return WERR_NOT_SUPPORTED;
9997 }
9998
9999 /****************************************************************
10000  _spoolss_FindFirstPrinterChangeNotification
10001 ****************************************************************/
10002
10003 WERROR _spoolss_FindFirstPrinterChangeNotification(pipes_struct *p,
10004                                                    struct spoolss_FindFirstPrinterChangeNotification *r)
10005 {
10006         p->rng_fault_state = true;
10007         return WERR_NOT_SUPPORTED;
10008 }
10009
10010 /****************************************************************
10011  _spoolss_FindNextPrinterChangeNotification
10012 ****************************************************************/
10013
10014 WERROR _spoolss_FindNextPrinterChangeNotification(pipes_struct *p,
10015                                                   struct spoolss_FindNextPrinterChangeNotification *r)
10016 {
10017         p->rng_fault_state = true;
10018         return WERR_NOT_SUPPORTED;
10019 }
10020
10021 /****************************************************************
10022  _spoolss_RouterFindFirstPrinterChangeNotificationOld
10023 ****************************************************************/
10024
10025 WERROR _spoolss_RouterFindFirstPrinterChangeNotificationOld(pipes_struct *p,
10026                                                             struct spoolss_RouterFindFirstPrinterChangeNotificationOld *r)
10027 {
10028         p->rng_fault_state = true;
10029         return WERR_NOT_SUPPORTED;
10030 }
10031
10032 /****************************************************************
10033  _spoolss_ReplyOpenPrinter
10034 ****************************************************************/
10035
10036 WERROR _spoolss_ReplyOpenPrinter(pipes_struct *p,
10037                                  struct spoolss_ReplyOpenPrinter *r)
10038 {
10039         p->rng_fault_state = true;
10040         return WERR_NOT_SUPPORTED;
10041 }
10042
10043 /****************************************************************
10044  _spoolss_RouterReplyPrinter
10045 ****************************************************************/
10046
10047 WERROR _spoolss_RouterReplyPrinter(pipes_struct *p,
10048                                    struct spoolss_RouterReplyPrinter *r)
10049 {
10050         p->rng_fault_state = true;
10051         return WERR_NOT_SUPPORTED;
10052 }
10053
10054 /****************************************************************
10055  _spoolss_ReplyClosePrinter
10056 ****************************************************************/
10057
10058 WERROR _spoolss_ReplyClosePrinter(pipes_struct *p,
10059                                   struct spoolss_ReplyClosePrinter *r)
10060 {
10061         p->rng_fault_state = true;
10062         return WERR_NOT_SUPPORTED;
10063 }
10064
10065 /****************************************************************
10066  _spoolss_AddPortEx
10067 ****************************************************************/
10068
10069 WERROR _spoolss_AddPortEx(pipes_struct *p,
10070                           struct spoolss_AddPortEx *r)
10071 {
10072         p->rng_fault_state = true;
10073         return WERR_NOT_SUPPORTED;
10074 }
10075
10076 /****************************************************************
10077  _spoolss_RouterFindFirstPrinterChangeNotification
10078 ****************************************************************/
10079
10080 WERROR _spoolss_RouterFindFirstPrinterChangeNotification(pipes_struct *p,
10081                                                          struct spoolss_RouterFindFirstPrinterChangeNotification *r)
10082 {
10083         p->rng_fault_state = true;
10084         return WERR_NOT_SUPPORTED;
10085 }
10086
10087 /****************************************************************
10088  _spoolss_SpoolerInit
10089 ****************************************************************/
10090
10091 WERROR _spoolss_SpoolerInit(pipes_struct *p,
10092                             struct spoolss_SpoolerInit *r)
10093 {
10094         p->rng_fault_state = true;
10095         return WERR_NOT_SUPPORTED;
10096 }
10097
10098 /****************************************************************
10099  _spoolss_ResetPrinterEx
10100 ****************************************************************/
10101
10102 WERROR _spoolss_ResetPrinterEx(pipes_struct *p,
10103                                struct spoolss_ResetPrinterEx *r)
10104 {
10105         p->rng_fault_state = true;
10106         return WERR_NOT_SUPPORTED;
10107 }
10108
10109 /****************************************************************
10110  _spoolss_RouterReplyPrinterEx
10111 ****************************************************************/
10112
10113 WERROR _spoolss_RouterReplyPrinterEx(pipes_struct *p,
10114                                      struct spoolss_RouterReplyPrinterEx *r)
10115 {
10116         p->rng_fault_state = true;
10117         return WERR_NOT_SUPPORTED;
10118 }
10119
10120 /****************************************************************
10121  _spoolss_44
10122 ****************************************************************/
10123
10124 WERROR _spoolss_44(pipes_struct *p,
10125                    struct spoolss_44 *r)
10126 {
10127         p->rng_fault_state = true;
10128         return WERR_NOT_SUPPORTED;
10129 }
10130
10131 /****************************************************************
10132  _spoolss_47
10133 ****************************************************************/
10134
10135 WERROR _spoolss_47(pipes_struct *p,
10136                    struct spoolss_47 *r)
10137 {
10138         p->rng_fault_state = true;
10139         return WERR_NOT_SUPPORTED;
10140 }
10141
10142 /****************************************************************
10143  _spoolss_4a
10144 ****************************************************************/
10145
10146 WERROR _spoolss_4a(pipes_struct *p,
10147                    struct spoolss_4a *r)
10148 {
10149         p->rng_fault_state = true;
10150         return WERR_NOT_SUPPORTED;
10151 }
10152
10153 /****************************************************************
10154  _spoolss_4b
10155 ****************************************************************/
10156
10157 WERROR _spoolss_4b(pipes_struct *p,
10158                    struct spoolss_4b *r)
10159 {
10160         p->rng_fault_state = true;
10161         return WERR_NOT_SUPPORTED;
10162 }
10163
10164 /****************************************************************
10165  _spoolss_4c
10166 ****************************************************************/
10167
10168 WERROR _spoolss_4c(pipes_struct *p,
10169                    struct spoolss_4c *r)
10170 {
10171         p->rng_fault_state = true;
10172         return WERR_NOT_SUPPORTED;
10173 }
10174
10175 /****************************************************************
10176  _spoolss_53
10177 ****************************************************************/
10178
10179 WERROR _spoolss_53(pipes_struct *p,
10180                    struct spoolss_53 *r)
10181 {
10182         p->rng_fault_state = true;
10183         return WERR_NOT_SUPPORTED;
10184 }
10185
10186 /****************************************************************
10187  _spoolss_55
10188 ****************************************************************/
10189
10190 WERROR _spoolss_55(pipes_struct *p,
10191                    struct spoolss_55 *r)
10192 {
10193         p->rng_fault_state = true;
10194         return WERR_NOT_SUPPORTED;
10195 }
10196
10197 /****************************************************************
10198  _spoolss_56
10199 ****************************************************************/
10200
10201 WERROR _spoolss_56(pipes_struct *p,
10202                    struct spoolss_56 *r)
10203 {
10204         p->rng_fault_state = true;
10205         return WERR_NOT_SUPPORTED;
10206 }
10207
10208 /****************************************************************
10209  _spoolss_57
10210 ****************************************************************/
10211
10212 WERROR _spoolss_57(pipes_struct *p,
10213                    struct spoolss_57 *r)
10214 {
10215         p->rng_fault_state = true;
10216         return WERR_NOT_SUPPORTED;
10217 }
10218
10219 /****************************************************************
10220  _spoolss_5a
10221 ****************************************************************/
10222
10223 WERROR _spoolss_5a(pipes_struct *p,
10224                    struct spoolss_5a *r)
10225 {
10226         p->rng_fault_state = true;
10227         return WERR_NOT_SUPPORTED;
10228 }
10229
10230 /****************************************************************
10231  _spoolss_5b
10232 ****************************************************************/
10233
10234 WERROR _spoolss_5b(pipes_struct *p,
10235                    struct spoolss_5b *r)
10236 {
10237         p->rng_fault_state = true;
10238         return WERR_NOT_SUPPORTED;
10239 }
10240
10241 /****************************************************************
10242  _spoolss_5c
10243 ****************************************************************/
10244
10245 WERROR _spoolss_5c(pipes_struct *p,
10246                    struct spoolss_5c *r)
10247 {
10248         p->rng_fault_state = true;
10249         return WERR_NOT_SUPPORTED;
10250 }
10251
10252 /****************************************************************
10253  _spoolss_5d
10254 ****************************************************************/
10255
10256 WERROR _spoolss_5d(pipes_struct *p,
10257                    struct spoolss_5d *r)
10258 {
10259         p->rng_fault_state = true;
10260         return WERR_NOT_SUPPORTED;
10261 }
10262
10263 /****************************************************************
10264  _spoolss_5e
10265 ****************************************************************/
10266
10267 WERROR _spoolss_5e(pipes_struct *p,
10268                    struct spoolss_5e *r)
10269 {
10270         p->rng_fault_state = true;
10271         return WERR_NOT_SUPPORTED;
10272 }
10273
10274 /****************************************************************
10275  _spoolss_5f
10276 ****************************************************************/
10277
10278 WERROR _spoolss_5f(pipes_struct *p,
10279                    struct spoolss_5f *r)
10280 {
10281         p->rng_fault_state = true;
10282         return WERR_NOT_SUPPORTED;
10283 }
10284