Herb's warning fixes. Also the POSIX locking fix.
[samba.git] / source3 / rpc_server / srv_spoolss_nt.c
1 #define OLD_NTDOMAIN 1
2 /* 
3  *  Unix SMB/Netbios implementation.
4  *  Version 1.9.
5  *  RPC Pipe client / server routines
6  *  Copyright (C) Andrew Tridgell              1992-2000,
7  *  Copyright (C) Luke Kenneth Casson Leighton 1996-2000,
8  *  Copyright (C) Jean Fran├žois Micouleau      1998-2000.
9  *  
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *  
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *  
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24
25 #include "includes.h"
26
27 extern int DEBUGLEVEL;
28 extern pstring global_myname;
29
30 #ifndef MAX_OPEN_PRINTER_EXS
31 #define MAX_OPEN_PRINTER_EXS 50
32 #endif
33
34 #define PRINTER_HANDLE_IS_PRINTER       0
35 #define PRINTER_HANDLE_IS_PRINTSERVER   1
36
37 /* structure to store the printer handles */
38 /* and a reference to what it's pointing to */
39 /* and the notify info asked about */
40 /* that's the central struct */
41 typedef struct _Printer{
42         ubi_dlNode Next;
43         ubi_dlNode Prev;
44
45         BOOL open;
46         BOOL document_started;
47         BOOL page_started;
48         int jobid; /* jobid in printing backend */
49         POLICY_HND printer_hnd;
50         BOOL printer_type;
51         union {
52                 fstring handlename;
53                 fstring printerservername;
54         } dev;
55         uint32 type;
56         uint32 access;
57         struct {
58                 uint32 flags;
59                 uint32 options;
60                 fstring localmachine; 
61                 uint32 printerlocal;
62                 SPOOL_NOTIFY_OPTION *option;
63                 POLICY_HND client_hnd;
64                 uint32 client_connected;
65         } notify;
66         struct {
67                 fstring machine;
68                 fstring user;
69         } client;
70 } Printer_entry;
71
72 typedef struct _counter_printer_0 {
73         ubi_dlNode Next;
74         ubi_dlNode Prev;
75         
76         int snum;
77         uint32 counter;
78 } counter_printer_0;
79
80 static ubi_dlList Printer_list;
81 static ubi_dlList counter_list;
82
83 static struct cli_state cli;
84 static uint32 smb_connections=0;
85
86 #define OPEN_HANDLE(pnum)    ((pnum!=NULL) && (pnum->open!=False) && (IVAL(pnum->printer_hnd.data,16)==(uint32)sys_getpid()))
87 #define OUR_HANDLE(pnum) ((pnum==NULL)?"NULL":(IVAL(pnum->data,16)==sys_getpid()?"OURS":"OTHER"))
88
89 /* translate between internal status numbers and NT status numbers */
90 static int nt_printj_status(int v)
91 {
92         switch (v) {
93         case LPQ_PAUSED:
94                 return PRINTER_STATUS_PAUSED;
95         case LPQ_QUEUED:
96         case LPQ_SPOOLING:
97         case LPQ_PRINTING:
98                 return 0;
99         }
100         return 0;
101 }
102
103 static int nt_printq_status(int v)
104 {
105         switch (v) {
106         case LPQ_PAUSED:
107                 return PRINTER_STATUS_PAUSED;
108         case LPQ_QUEUED:
109         case LPQ_SPOOLING:
110         case LPQ_PRINTING:
111                 return 0;
112         }
113         return 0;
114 }
115
116 /****************************************************************************
117   initialise printer handle states...
118 ****************************************************************************/
119 void init_printer_hnd(void)
120 {
121         ubi_dlInitList(&Printer_list);
122         ubi_dlInitList(&counter_list);
123 }
124
125 /****************************************************************************
126   create a unique printer handle
127 ****************************************************************************/
128 static void create_printer_hnd(POLICY_HND *hnd)
129 {
130         static uint32 prt_hnd_low  = 0;
131         static uint32 prt_hnd_high = 0;
132
133         if (hnd == NULL) return;
134
135         /* i severely doubt that prt_hnd_high will ever be non-zero... */
136         prt_hnd_low++;
137         if (prt_hnd_low == 0) prt_hnd_high++;
138
139         SIVAL(hnd->data, 0 , 0x0);          /* first bit must be null */
140         SIVAL(hnd->data, 4 , prt_hnd_low ); /* second bit is incrementing */
141         SIVAL(hnd->data, 8 , prt_hnd_high); /* second bit is incrementing */
142         SIVAL(hnd->data, 12, time(NULL));   /* something random */
143         SIVAL(hnd->data, 16, sys_getpid());     /* something more random */
144 }
145
146 /****************************************************************************
147   find printer index by handle
148 ****************************************************************************/
149 static Printer_entry *find_printer_index_by_hnd(const POLICY_HND *hnd)
150 {
151         Printer_entry *find_printer;
152
153         find_printer = (Printer_entry *)ubi_dlFirst(&Printer_list);
154
155         for(; find_printer; find_printer = (Printer_entry *)ubi_dlNext(find_printer)) {
156
157                 if (memcmp(&(find_printer->printer_hnd), hnd, sizeof(*hnd)) == 0) {
158                         DEBUG(4,("Found printer handle \n"));
159                         /*dump_data(4, hnd->data, sizeof(hnd->data));*/
160                         return find_printer;
161                 }
162         }
163         
164         DEBUG(3,("Whoops, Printer handle not found: "));
165         /*dump_data(4, hnd->data, sizeof(hnd->data));*/
166         return NULL;
167 }
168
169 /****************************************************************************
170   clear an handle
171 ****************************************************************************/
172 static void clear_handle(POLICY_HND *hnd)
173 {
174         ZERO_STRUCTP(hnd);
175 }
176
177 /***************************************************************************
178  Disconnect from the client
179 ****************************************************************************/
180 static BOOL srv_spoolss_replycloseprinter(POLICY_HND *handle)
181 {
182         uint32 status;
183
184         /* weird if the test succeds !!! */
185         if (smb_connections==0) {
186                 DEBUG(0,("srv_spoolss_replycloseprinter:Trying to close non-existant notify backchannel !\n"));
187                 return False;
188         }
189
190         if(!cli_spoolss_reply_close_printer(&cli, handle, &status))
191                 return False;
192
193         /* if it's the last connection, deconnect the IPC$ share */
194         if (smb_connections==1) {
195                 if(!spoolss_disconnect_from_client(&cli))
196                         return False;
197
198                 message_deregister(MSG_PRINTER_NOTIFY);
199         }
200
201         smb_connections--;
202
203         return True;
204 }
205
206 /****************************************************************************
207   close printer index by handle
208 ****************************************************************************/
209 static BOOL close_printer_handle(POLICY_HND *hnd)
210 {
211         Printer_entry *Printer = find_printer_index_by_hnd(hnd);
212
213         if (!OPEN_HANDLE(Printer)) {
214                 DEBUG(0,("close_printer_handle: Invalid handle (%s)\n", OUR_HANDLE(hnd)));
215                 return False;
216         }
217
218         if (Printer->notify.client_connected==True)
219                 if(!srv_spoolss_replycloseprinter(&Printer->notify.client_hnd))
220                         return ERROR_INVALID_HANDLE;
221
222         Printer->open=False;
223         Printer->notify.flags=0;
224         Printer->notify.options=0;
225         Printer->notify.localmachine[0]='\0';
226         Printer->notify.printerlocal=0;
227         safe_free(Printer->notify.option);
228         Printer->notify.option=NULL;
229         Printer->notify.client_connected=False;
230
231         clear_handle(hnd);
232
233         ubi_dlRemThis(&Printer_list, Printer);
234
235         safe_free(Printer);
236
237         return True;
238 }       
239
240 /****************************************************************************
241   delete a printer given a handle
242 ****************************************************************************/
243 static BOOL delete_printer_handle(POLICY_HND *hnd)
244 {
245         Printer_entry *Printer = find_printer_index_by_hnd(hnd);
246
247         if (!OPEN_HANDLE(Printer)) {
248                 DEBUG(0,("delete_printer_handle: Invalid handle (%s)\n", OUR_HANDLE(hnd)));
249                 return False;
250         }
251
252         if (del_a_printer(Printer->dev.handlename) != 0) {
253                 DEBUG(3,("Error deleting printer %s\n", Printer->dev.handlename));
254                 return False;
255         }
256
257         if (*lp_deleteprinter_cmd()) {
258
259                 pid_t local_pid = sys_getpid();
260                 char *cmd = lp_deleteprinter_cmd();
261                 char *path;
262                 pstring tmp_file;
263                 pstring command;
264                 int ret;
265                 int i;
266
267                 if (*lp_pathname(lp_servicenumber(PRINTERS_NAME)))
268                         path = lp_pathname(lp_servicenumber(PRINTERS_NAME));
269                 else
270                         path = tmpdir();
271                 
272                 /* Printer->dev.handlename equals portname equals sharename */
273                 slprintf(command, sizeof(command), "%s \"%s\"", cmd,
274                                         Printer->dev.handlename);
275                 slprintf(tmp_file, sizeof(tmp_file), "%s/smbcmd.%d", path, local_pid);
276
277                 unlink(tmp_file);
278                 DEBUG(10,("Running [%s > %s]\n", command,tmp_file));
279                 ret = smbrun(command, tmp_file, False);
280                 if (ret != 0) {
281                         unlink(tmp_file);
282                         return False;
283                 }
284                 DEBUGADD(10,("returned [%d]\n", ret));
285                 DEBUGADD(10,("Unlinking output file [%s]\n", tmp_file));
286                 unlink(tmp_file);
287
288                 /* Send SIGHUP to process group... is there a better way? */
289                 kill(0, SIGHUP);
290
291                 if ( ( i = lp_servicenumber( Printer->dev.handlename ) ) >= 0 ) {
292                         lp_remove_service( i );
293                         lp_killservice( i );
294                         return True;
295                 } else
296                         return False;
297         }
298
299         return True;
300 }       
301
302 /****************************************************************************
303   return the snum of a printer corresponding to an handle
304 ****************************************************************************/
305 static BOOL get_printer_snum(POLICY_HND *hnd, int *number)
306 {
307         Printer_entry *Printer = find_printer_index_by_hnd(hnd);
308                 
309         if (!OPEN_HANDLE(Printer)) {
310                 DEBUG(0,("get_printer_snum: Invalid handle (%s)\n", OUR_HANDLE(hnd)));
311                 return False;
312         }
313         
314         switch (Printer->printer_type) {
315         case PRINTER_HANDLE_IS_PRINTER:            
316                 DEBUG(4,("short name:%s\n", Printer->dev.handlename));                  
317                 *number = print_queue_snum(Printer->dev.handlename);
318                 return (*number != -1);
319         case PRINTER_HANDLE_IS_PRINTSERVER:
320                 return False;
321         default:
322                 return False;
323         }
324 }
325
326 /****************************************************************************
327   set printer handle type.
328 ****************************************************************************/
329 static BOOL set_printer_hnd_accesstype(POLICY_HND *hnd, uint32 access_required)
330 {
331         Printer_entry *Printer = find_printer_index_by_hnd(hnd);
332
333         if (!OPEN_HANDLE(Printer)) {
334                 DEBUG(0,("set_printer_hnd_accesstype: Invalid handle (%s)", OUR_HANDLE(hnd)));
335                 return False;
336         }
337
338         DEBUG(4,("Setting printer access=%x\n", access_required));
339         Printer->access = access_required;
340         return True;            
341 }
342
343 /****************************************************************************
344  Set printer handle type.
345  Check if it's \\server or \\server\printer
346 ****************************************************************************/
347
348 static BOOL set_printer_hnd_printertype(Printer_entry *Printer, char *handlename)
349 {
350         DEBUG(3,("Setting printer type=%s\n", handlename));
351
352         if ( strlen(handlename) < 3 ) {
353                 DEBUGADD(4,("A print server must have at least 1 char ! %s\n", handlename));
354                 return False;
355         }
356
357         /* it's a print server */
358         if (!strchr(handlename+2, '\\')) {
359                 DEBUGADD(4,("Printer is a print server\n"));
360                 Printer->printer_type = PRINTER_HANDLE_IS_PRINTSERVER;          
361         }
362         /* it's a printer */
363         else {
364                 DEBUGADD(4,("Printer is a printer\n"));
365                 Printer->printer_type = PRINTER_HANDLE_IS_PRINTER;
366         }
367
368         return True;
369 }
370
371 /****************************************************************************
372  Set printer handle name.
373 ****************************************************************************/
374
375 static BOOL set_printer_hnd_name(Printer_entry *Printer, char *handlename)
376 {
377         NT_PRINTER_INFO_LEVEL *printer = NULL;
378         int snum;
379         int n_services=lp_numservices();
380         char *aprinter;
381         BOOL found=False;
382         
383         DEBUG(4,("Setting printer name=%s (len=%d)\n", handlename, strlen(handlename)));
384
385         if (Printer->printer_type==PRINTER_HANDLE_IS_PRINTSERVER) {
386                 ZERO_STRUCT(Printer->dev.printerservername);
387                 strncpy(Printer->dev.printerservername, handlename, strlen(handlename));
388                 return True;
389         }
390
391         if (Printer->printer_type!=PRINTER_HANDLE_IS_PRINTER)
392                 return False;
393         
394         aprinter=strchr(handlename+2, '\\');
395         aprinter++;
396
397         DEBUGADD(5,("searching for [%s] (len=%d)\n", aprinter, strlen(aprinter)));
398
399         /*
400          * store the Samba share name in it
401          * in back we have the long printer name
402          * need to iterate all the snum and do a
403          * get_a_printer each time to find the printer
404          * faster to do it here than later.
405          */
406
407         for (snum=0;snum<n_services && found==False;snum++) {
408         
409                 if ( !(lp_snum_ok(snum) && lp_print_ok(snum) ) )
410                         continue;
411                 
412                 DEBUGADD(5,("share:%s\n",lp_servicename(snum)));
413
414                 if (get_a_printer(&printer, 2, lp_servicename(snum))!=0)
415                         continue;
416
417                 DEBUG(10,("set_printer_hnd_name: name [%s], aprinter [%s]\n", 
418                                 printer->info_2->printername, aprinter ));
419
420                 if ( strlen(printer->info_2->printername) != strlen(aprinter) ) {
421                         free_a_printer(&printer, 2);
422                         continue;
423                 }
424                 
425                 if ( strncasecmp(printer->info_2->printername, aprinter, strlen(aprinter)))  {
426                         free_a_printer(&printer, 2);
427                         continue;
428                 }
429                 
430                 found=True;
431         }
432
433         /* 
434          * if we haven't found a printer with the given handlename
435          * then it can be a share name as you can open both \\server\printer and
436          * \\server\share
437          */
438
439         /*
440          * we still check if the printer description file exists as NT won't be happy
441          * if we reply OK in the openprinter call and can't reply in the subsequent RPC calls
442          */
443
444         if (found==False) {
445                 DEBUGADD(5,("Printer not found, checking for share now\n"));
446         
447                 for (snum=0;snum<n_services && found==False;snum++) {
448         
449                         if ( !(lp_snum_ok(snum) && lp_print_ok(snum) ) )
450                                 continue;
451                 
452                         DEBUGADD(5,("set_printer_hnd_name: share:%s\n",lp_servicename(snum)));
453
454                         if (get_a_printer(&printer, 2, lp_servicename(snum))!=0)
455                                 continue;
456
457                         DEBUG(10,("set_printer_hnd_name: printername [%s], aprinter [%s]\n", 
458                                         printer->info_2->printername, aprinter ));
459
460                         if ( strlen(lp_servicename(snum)) != strlen(aprinter) ) {
461                                 free_a_printer(&printer, 2);
462                                 continue;
463                         }
464                 
465                         if ( strncasecmp(lp_servicename(snum), aprinter, strlen(aprinter)))  {
466                                 free_a_printer(&printer, 2);
467                                 continue;
468                         }
469                 
470                         found=True;
471                 }
472         }
473                 
474         if (found==False) {
475                 DEBUGADD(4,("Printer not found\n"));
476                 return False;
477         }
478         
479         snum--;
480         DEBUGADD(4,("set_printer_hnd_name: Printer found: %s -> %s[%x]\n",
481                         printer->info_2->printername, lp_servicename(snum),snum));
482
483         ZERO_STRUCT(Printer->dev.handlename);
484         strncpy(Printer->dev.handlename, lp_servicename(snum), strlen(lp_servicename(snum)));
485         
486         free_a_printer(&printer, 2);
487
488         return True;
489 }
490
491 /****************************************************************************
492   find first available printer slot. creates a printer handle for you.
493  ****************************************************************************/
494
495 static BOOL open_printer_hnd(POLICY_HND *hnd, char *name)
496 {
497         Printer_entry *new_printer;
498
499         DEBUG(10,("open_printer_hnd: name [%s]\n", name));
500         clear_handle(hnd);
501         create_printer_hnd(hnd);
502
503         if((new_printer=(Printer_entry *)malloc(sizeof(Printer_entry))) == NULL)
504                 return False;
505
506         ZERO_STRUCTP(new_printer);
507         
508         new_printer->open = True;
509         new_printer->notify.option=NULL;
510                                 
511         memcpy(&new_printer->printer_hnd, hnd, sizeof(*hnd));
512         
513         ubi_dlAddHead( &Printer_list, (ubi_dlNode *)new_printer);
514
515         if (!set_printer_hnd_printertype(new_printer, name)) {
516                 close_printer_handle(hnd);
517                 return False;
518         }
519         
520         if (!set_printer_hnd_name(new_printer, name)) {
521                 close_printer_handle(hnd);
522                 return False;
523         }
524
525         return True;
526 }
527
528 /********************************************************************
529  Return True is the handle is a print server.
530  ********************************************************************/
531 static BOOL handle_is_printserver(const POLICY_HND *handle)
532 {
533         Printer_entry *Printer=find_printer_index_by_hnd(handle);
534
535         if (!OPEN_HANDLE(Printer))
536                 return False;
537                 
538         if (Printer->printer_type != PRINTER_HANDLE_IS_PRINTSERVER)
539                 return False;
540         
541         return True;
542 }
543
544 /****************************************************************************
545  allocate more memory for a BUFFER.
546 ****************************************************************************/
547 static BOOL alloc_buffer_size(NEW_BUFFER *buffer, uint32 buffer_size)
548 {
549         prs_struct *ps;
550         uint32 extra_space;
551         uint32 old_offset;
552         
553         ps= &buffer->prs;
554
555         /* damn, I'm doing the reverse operation of prs_grow() :) */
556         if (buffer_size < prs_data_size(ps))
557                 extra_space=0;
558         else    
559                 extra_space = buffer_size - prs_data_size(ps);
560
561         /* 
562          * save the offset and move to the end of the buffer
563          * prs_grow() checks the extra_space against the offset 
564          */
565         old_offset=prs_offset(ps);      
566         prs_set_offset(ps, prs_data_size(ps));
567         
568         if (!prs_grow(ps, extra_space))
569                 return False;
570
571         prs_set_offset(ps, old_offset);
572
573         buffer->string_at_end=prs_data_size(ps);
574
575         return True;
576 }
577
578 /***************************************************************************
579  receive the notify message
580 ****************************************************************************/
581 void srv_spoolss_receive_message(int msg_type, pid_t src, void *buf, size_t len)
582 {      
583         fstring printer;
584         uint32 status;
585         Printer_entry *find_printer;
586
587         *printer = '\0';
588         fstrcpy(printer,buf);
589
590         if (len == 0) {
591                 DEBUG(0,("srv_spoolss_receive_message: got null message !\n"));
592                 return;
593         }
594
595         DEBUG(10,("srv_spoolss_receive_message: Got message about printer %s\n", printer ));
596
597         find_printer = (Printer_entry *)ubi_dlFirst(&Printer_list);
598
599         /* Iterate the printer list. */
600         for(; find_printer; find_printer = (Printer_entry *)ubi_dlNext(find_printer)) {
601
602                 /* 
603                  * if the entry is the given printer or if it's a printerserver
604                  * we send the message
605                  */
606
607                 if (find_printer->printer_type==PRINTER_HANDLE_IS_PRINTER)
608                         if (strcmp(find_printer->dev.handlename, printer))
609                                 continue;
610
611                 if (find_printer->notify.client_connected==True)
612                         cli_spoolss_reply_rrpcn(&cli, &find_printer->notify.client_hnd, PRINTER_CHANGE_ALL, 0x0, &status);
613
614         }
615 }
616
617 /***************************************************************************
618  send a notify event
619 ****************************************************************************/
620 static BOOL srv_spoolss_sendnotify(POLICY_HND *handle)
621 {
622         fstring printer;
623
624         Printer_entry *Printer=find_printer_index_by_hnd(handle);
625
626         if (!OPEN_HANDLE(Printer)) {
627                 DEBUG(0,("srv_spoolss_sendnotify: Invalid handle (%s).\n", OUR_HANDLE(handle)));
628                 return False;
629         }
630
631         if (Printer->printer_type==PRINTER_HANDLE_IS_PRINTER)
632                 fstrcpy(printer, Printer->dev.handlename);
633         else
634                 fstrcpy(printer, "");
635
636         /*srv_spoolss_receive_message(printer);*/
637         DEBUG(10,("srv_spoolss_sendnotify: Sending message about printer %s\n", printer ));
638
639         message_send_all(MSG_PRINTER_NOTIFY, printer, strlen(printer) + 1); /* Null terminate... */
640
641         return True;
642 }       
643
644 /********************************************************************
645  * spoolss_open_printer
646  *
647  * called from the spoolss dispatcher
648  ********************************************************************/
649 uint32 _spoolss_open_printer_ex( const UNISTR2 *printername,
650                                  const PRINTER_DEFAULT *printer_default,
651                                  uint32  user_switch, SPOOL_USER_CTR user_ctr,
652                                  POLICY_HND *handle)
653 {
654         fstring name;
655         
656         if (printername == NULL)
657                 return ERROR_INVALID_PRINTER_NAME;
658
659         /* some sanity check because you can open a printer or a print server */
660         /* aka: \\server\printer or \\server */
661         unistr2_to_ascii(name, printername, sizeof(name)-1);
662
663         DEBUGADD(3,("checking name: %s\n",name));
664
665         if (!open_printer_hnd(handle, name))
666                 return ERROR_INVALID_PRINTER_NAME;
667         
668 /*
669         if (printer_default->datatype_ptr != NULL)
670         {
671                 unistr2_to_ascii(datatype, printer_default->datatype, sizeof(datatype)-1);
672                 set_printer_hnd_datatype(handle, datatype);
673         }
674         else
675                 set_printer_hnd_datatype(handle, "");
676 */
677         
678         if (!set_printer_hnd_accesstype(handle, printer_default->access_required)) {
679                 close_printer_handle(handle);
680                 return ERROR_ACCESS_DENIED;
681         }
682                 
683         /* Disallow MS AddPrinterWizard if access rights are insufficient OR
684            if parameter disables it. The client tries an OpenPrinterEx with
685            SERVER_ALL_ACCESS(0xf0003), which we force to fail. It then tries
686            OpenPrinterEx with SERVER_READ(0x20002) which we allow. This lets
687            it see any printers there, but does not show the MSAPW */
688         if (handle_is_printserver(handle) &&
689                 printer_default->access_required != (SERVER_READ) &&
690                 !lp_ms_add_printer_wizard() ) {
691                 return ERROR_ACCESS_DENIED;
692         }
693
694         return NT_STATUS_NO_PROBLEMO;
695 }
696
697 /****************************************************************************
698 ****************************************************************************/
699 static BOOL convert_printer_info(const SPOOL_PRINTER_INFO_LEVEL *uni,
700                                 NT_PRINTER_INFO_LEVEL *printer, uint32 level)
701 {
702         switch (level) {
703                 case 2: 
704                         uni_2_asc_printer_info_2(uni->info_2, &printer->info_2);
705                         break;
706                 default:
707                         break;
708         }
709
710         return True;
711 }
712
713 static BOOL convert_printer_driver_info(const SPOOL_PRINTER_DRIVER_INFO_LEVEL *uni,
714                                         NT_PRINTER_DRIVER_INFO_LEVEL *printer, uint32 level)
715 {
716         switch (level) {
717                 case 3: 
718                         printer->info_3=NULL;
719                         uni_2_asc_printer_driver_3(uni->info_3, &printer->info_3);
720                         break;
721                 case 6: 
722                         printer->info_6=NULL;
723                         uni_2_asc_printer_driver_6(uni->info_6, &printer->info_6);
724                         break;
725                 default:
726                         break;
727         }
728
729         return True;
730 }
731
732 static BOOL convert_devicemode(const DEVICEMODE *devmode, NT_DEVICEMODE *nt_devmode)
733 {
734         unistr_to_dos(nt_devmode->devicename, (const char *)devmode->devicename.buffer, 31);
735         unistr_to_dos(nt_devmode->formname, (const char *)devmode->formname.buffer, 31);
736
737         nt_devmode->specversion=devmode->specversion;
738         nt_devmode->driverversion=devmode->driverversion;
739         nt_devmode->size=devmode->size;
740         nt_devmode->driverextra=devmode->driverextra;
741         nt_devmode->fields=devmode->fields;
742         nt_devmode->orientation=devmode->orientation;
743         nt_devmode->papersize=devmode->papersize;
744         nt_devmode->paperlength=devmode->paperlength;
745         nt_devmode->paperwidth=devmode->paperwidth;
746         nt_devmode->scale=devmode->scale;
747         nt_devmode->copies=devmode->copies;
748         nt_devmode->defaultsource=devmode->defaultsource;
749         nt_devmode->printquality=devmode->printquality;
750         nt_devmode->color=devmode->color;
751         nt_devmode->duplex=devmode->duplex;
752         nt_devmode->yresolution=devmode->yresolution;
753         nt_devmode->ttoption=devmode->ttoption;
754         nt_devmode->collate=devmode->collate;
755
756         nt_devmode->logpixels=devmode->logpixels;
757         nt_devmode->bitsperpel=devmode->bitsperpel;
758         nt_devmode->pelswidth=devmode->pelswidth;
759         nt_devmode->pelsheight=devmode->pelsheight;
760         nt_devmode->displayflags=devmode->displayflags;
761         nt_devmode->displayfrequency=devmode->displayfrequency;
762         nt_devmode->icmmethod=devmode->icmmethod;
763         nt_devmode->icmintent=devmode->icmintent;
764         nt_devmode->mediatype=devmode->mediatype;
765         nt_devmode->dithertype=devmode->dithertype;
766         nt_devmode->reserved1=devmode->reserved1;
767         nt_devmode->reserved2=devmode->reserved2;
768         nt_devmode->panningwidth=devmode->panningwidth;
769         nt_devmode->panningheight=devmode->panningheight;
770
771         if (nt_devmode->driverextra != 0) {
772                 /* if we had a previous private delete it and make a new one */
773                 safe_free(nt_devmode->private);
774                 if((nt_devmode->private=(uint8 *)malloc(nt_devmode->driverextra * sizeof(uint8))) == NULL)
775                         return False;
776                 memcpy(nt_devmode->private, devmode->private, nt_devmode->driverextra);
777         }
778
779         return True;
780 }
781
782 /********************************************************************
783  * api_spoolss_closeprinter
784  ********************************************************************/
785 uint32 _spoolss_closeprinter(POLICY_HND *handle)
786 {
787         Printer_entry *Printer=find_printer_index_by_hnd(handle);
788
789         if (Printer && Printer->document_started)
790                 _spoolss_enddocprinter(handle);          /* print job was not closed */
791
792         if (!close_printer_handle(handle))
793                 return ERROR_INVALID_HANDLE;    
794                 
795         return NT_STATUS_NO_PROBLEMO;
796 }
797
798 /********************************************************************
799  * api_spoolss_deleteprinter
800  ********************************************************************/
801 uint32 _spoolss_deleteprinter(POLICY_HND *handle)
802 {
803         Printer_entry *Printer=find_printer_index_by_hnd(handle);
804
805         if (Printer && Printer->document_started)
806                 _spoolss_enddocprinter(handle);          /* print job was not closed */
807
808         if (!delete_printer_handle(handle))
809                 return ERROR_INVALID_HANDLE;    
810
811         srv_spoolss_sendnotify(handle);
812                 
813         return NT_STATUS_NO_PROBLEMO;
814 }
815
816 /********************************************************************
817  GetPrinterData on a printer server Handle.
818 ********************************************************************/
819 static BOOL getprinterdata_printer_server(fstring value, uint32 *type, uint8 **data, uint32 *needed, uint32 in_size)
820 {               
821         int i;
822         
823         DEBUG(8,("getprinterdata_printer_server:%s\n", value));
824                 
825         if (!strcmp(value, "BeepEnabled")) {
826                 *type = 0x4;
827                 if((*data = (uint8 *)malloc( 4*sizeof(uint8) )) == NULL)
828                         return False;
829                 SIVAL(*data, 0, 0x01);
830                 *needed = 0x4;                  
831                 return True;
832         }
833
834         if (!strcmp(value, "EventLog")) {
835                 *type = 0x4;
836                 if((*data = (uint8 *)malloc( 4*sizeof(uint8) )) == NULL)
837                         return False;
838                 SIVAL(*data, 0, 0x1B);
839                 *needed = 0x4;                  
840                 return True;
841         }
842
843         if (!strcmp(value, "NetPopup")) {
844                 *type = 0x4;
845                 if((*data = (uint8 *)malloc( 4*sizeof(uint8) )) == NULL)
846                         return False;
847                 SIVAL(*data, 0, 0x01);
848                 *needed = 0x4;
849                 return True;
850         }
851
852         if (!strcmp(value, "MajorVersion")) {
853                 *type = 0x4;
854                 if((*data = (uint8 *)malloc( 4*sizeof(uint8) )) == NULL)
855                         return False;
856                 SIVAL(*data, 0, 0x02);
857                 *needed = 0x4;
858                 return True;
859         }
860
861         if (!strcmp(value, "DefaultSpoolDirectory")) {
862                 pstring string="You are using a Samba server";
863                 *type = 0x1;                    
864                 *needed = 2*(strlen(string)+1);         
865                 if((*data  = (uint8 *)malloc( ((*needed > in_size) ? *needed:in_size) *sizeof(uint8))) == NULL)
866                         return False;
867                 memset(*data, 0, (*needed > in_size) ? *needed:in_size);
868                 
869                 /* it's done by hand ready to go on the wire */
870                 for (i=0; i<strlen(string); i++) {
871                         (*data)[2*i]=string[i];
872                         (*data)[2*i+1]='\0';
873                 }                       
874                 return True;
875         }
876
877         if (!strcmp(value, "Architecture")) {                   
878                 pstring string="Windows NT x86";
879                 *type = 0x1;                    
880                 *needed = 2*(strlen(string)+1); 
881                 if((*data  = (uint8 *)malloc( ((*needed > in_size) ? *needed:in_size) *sizeof(uint8))) == NULL)
882                         return False;
883                 memset(*data, 0, (*needed > in_size) ? *needed:in_size);
884                 for (i=0; i<strlen(string); i++) {
885                         (*data)[2*i]=string[i];
886                         (*data)[2*i+1]='\0';
887                 }                       
888                 return True;
889         }
890         
891         return False;
892 }
893
894 /********************************************************************
895  GetPrinterData on a printer Handle.
896 ********************************************************************/
897 static BOOL getprinterdata_printer(POLICY_HND *handle,
898                                 fstring value, uint32 *type, 
899                                 uint8 **data, uint32 *needed, uint32 in_size )
900 {
901         NT_PRINTER_INFO_LEVEL *printer = NULL;
902         int snum=0;
903         uint8 *idata=NULL;
904         uint32 len;
905         Printer_entry *Printer = find_printer_index_by_hnd(handle);
906         
907         DEBUG(5,("getprinterdata_printer\n"));
908
909         if (!OPEN_HANDLE(Printer)) {
910                 DEBUG(0,("getprinterdata_printer: Invalid handle (%s).\n", OUR_HANDLE(handle)));
911                 return False;
912         }
913
914         if(!get_printer_snum(handle, &snum))
915                 return False;
916
917         if(get_a_printer(&printer, 2, lp_servicename(snum)) != 0)
918                 return False;
919
920         if (!get_specific_param(*printer, 2, value, &idata, type, &len)) {
921                 free_a_printer(&printer, 2);
922                 return False;
923         }
924
925         free_a_printer(&printer, 2);
926
927         DEBUG(5,("getprinterdata_printer:allocating %d\n", in_size));
928
929         if (in_size) {
930                 if((*data  = (uint8 *)malloc( in_size *sizeof(uint8) )) == NULL) {
931                         return False;
932                 }
933
934                 memset(*data, 0, in_size *sizeof(uint8));
935                 /* copy the min(in_size, len) */
936                 memcpy(*data, idata, (len>in_size)?in_size:len *sizeof(uint8));
937         } else {
938                 *data = NULL;
939         }
940
941         *needed = len;
942         
943         DEBUG(5,("getprinterdata_printer:copy done\n"));
944                         
945         safe_free(idata);
946         
947         return True;
948 }       
949
950 /********************************************************************
951  * spoolss_getprinterdata
952  ********************************************************************/
953 uint32 _spoolss_getprinterdata(POLICY_HND *handle, UNISTR2 *valuename,
954                                 uint32 in_size,
955                                 uint32 *type,
956                                 uint32 *out_size,
957                                 uint8 **data,
958                                 uint32 *needed)
959 {
960         fstring value;
961         BOOL found=False;
962         Printer_entry *Printer = find_printer_index_by_hnd(handle);
963         
964         /* 
965          * Reminder: when it's a string, the length is in BYTES
966          * even if UNICODE is negociated.
967          *
968          * JFM, 4/19/1999
969          */
970
971         *out_size=in_size;
972
973         /* in case of problem, return some default values */
974         *needed=0;
975         *type=0;
976         
977         DEBUG(4,("_spoolss_getprinterdata\n"));
978         
979         if (!OPEN_HANDLE(Printer)) {
980                 if((*data=(uint8 *)malloc(4*sizeof(uint8))) == NULL)
981                         return ERROR_NOT_ENOUGH_MEMORY;
982                 DEBUG(0,("_spoolss_getprinterdata: Invalid handle (%s).\n", OUR_HANDLE(handle)));
983                 return ERROR_INVALID_HANDLE;
984         }
985         
986         unistr2_to_ascii(value, valuename, sizeof(value)-1);
987         
988         if (handle_is_printserver(handle))
989                 found=getprinterdata_printer_server(value, type, data, needed, *out_size);
990         else
991                 found=getprinterdata_printer(handle, value, type, data, needed, *out_size);
992
993         if (found==False) {
994                 DEBUG(5, ("value not found, allocating %d\n", *out_size));
995                 /* reply this param doesn't exist */
996                 if (*out_size) {
997                         if((*data=(uint8 *)malloc(*out_size*sizeof(uint8))) == NULL)
998                                 return ERROR_NOT_ENOUGH_MEMORY;
999                         memset(*data, '\0', *out_size*sizeof(uint8));
1000                 } else {
1001                         *data = NULL;
1002                 }
1003
1004                 return ERROR_INVALID_PARAMETER;
1005         }
1006         
1007         if (*needed > *out_size)
1008                 return ERROR_MORE_DATA;
1009         else
1010                 return NT_STATUS_NO_PROBLEMO;
1011 }
1012
1013 /***************************************************************************
1014  connect to the client
1015 ****************************************************************************/
1016 static BOOL srv_spoolss_replyopenprinter(char *printer, uint32 localprinter, uint32 type, POLICY_HND *handle)
1017 {
1018         uint32 status;
1019
1020         /*
1021          * If it's the first connection, contact the client 
1022          * and connect to the IPC$ share anonumously
1023          */
1024         if (smb_connections==0) {
1025                 if(!spoolss_connect_to_client(&cli, printer+2)) /* the +2 is to strip the leading 2 backslashs */
1026                         return False;
1027                 message_register(MSG_PRINTER_NOTIFY, srv_spoolss_receive_message);
1028
1029         }
1030
1031         smb_connections++;
1032
1033         if(!cli_spoolss_reply_open_printer(&cli, printer, localprinter, type, &status, handle))
1034                 return False;
1035
1036         return True;
1037 }
1038
1039 /********************************************************************
1040  * _spoolss_rffpcnex
1041  * ReplyFindFirstPrinterChangeNotifyEx
1042  *
1043  * jfmxxxx: before replying OK: status=0
1044  * should do a rpc call to the workstation asking ReplyOpenPrinter
1045  * have to code it, later.
1046  *
1047  * in fact ReplyOpenPrinter is the changenotify equivalent on the spoolss pipe
1048  * called from api_spoolss_rffpcnex 
1049  ********************************************************************/
1050 uint32 _spoolss_rffpcnex(POLICY_HND *handle, uint32 flags, uint32 options,
1051                          const UNISTR2 *localmachine, uint32 printerlocal,
1052                          SPOOL_NOTIFY_OPTION *option)
1053 {
1054         /* store the notify value in the printer struct */
1055
1056         Printer_entry *Printer=find_printer_index_by_hnd(handle);
1057
1058         if (!OPEN_HANDLE(Printer)) {
1059                 DEBUG(0,("_spoolss_rffpcnex: Invalid handle (%s).\n", OUR_HANDLE(handle)));
1060                 return ERROR_INVALID_HANDLE;
1061         }
1062
1063         Printer->notify.flags=flags;
1064         Printer->notify.options=options;
1065         Printer->notify.printerlocal=printerlocal;
1066         Printer->notify.option=option;
1067         unistr2_to_ascii(Printer->notify.localmachine, localmachine, sizeof(Printer->notify.localmachine)-1);
1068
1069         /* connect to the client machine and send a ReplyOpenPrinter */
1070         if(srv_spoolss_replyopenprinter(Printer->notify.localmachine, 
1071                                         Printer->notify.printerlocal, 1, 
1072                                         &Printer->notify.client_hnd))
1073                 Printer->notify.client_connected=True;
1074
1075         return NT_STATUS_NO_PROBLEMO;
1076 }
1077
1078 /*******************************************************************
1079  * fill a notify_info_data with the servername
1080  ********************************************************************/
1081 static void spoolss_notify_server_name(int snum, SPOOL_NOTIFY_INFO_DATA *data, print_queue_struct *queue,
1082                                                                                 NT_PRINTER_INFO_LEVEL *printer)
1083 {
1084         pstring temp_name;
1085
1086         snprintf(temp_name, sizeof(temp_name)-1, "\\\\%s", global_myname);
1087
1088         data->notify_data.data.length= (uint32)((dos_PutUniCode((char *)data->notify_data.data.string,
1089                                         temp_name, sizeof(data->notify_data.data.string), True) - sizeof(uint16))/sizeof(uint16));
1090 }
1091
1092 /*******************************************************************
1093  * fill a notify_info_data with the servicename
1094  * jfmxxxx: it's incorrect should be long_printername
1095  ********************************************************************/
1096 static void spoolss_notify_printer_name(int snum, SPOOL_NOTIFY_INFO_DATA *data, print_queue_struct *queue,
1097                                                                                 NT_PRINTER_INFO_LEVEL *printer)
1098 {
1099 /*
1100         data->notify_data.data.length=strlen(lp_servicename(snum));
1101         dos_PutUniCode(data->notify_data.data.string, lp_servicename(snum), sizeof(data->notify_data.data.string), True);
1102 */
1103         data->notify_data.data.length=(uint32)((dos_PutUniCode((char *)data->notify_data.data.string,
1104                                 printer->info_2->printername, sizeof(data->notify_data.data.string), True) - sizeof(uint16))/sizeof(uint16));
1105 }
1106
1107 /*******************************************************************
1108  * fill a notify_info_data with the servicename
1109  ********************************************************************/
1110 static void spoolss_notify_share_name(int snum, SPOOL_NOTIFY_INFO_DATA *data, print_queue_struct *queue, NT_PRINTER_INFO_LEVEL *printer)
1111 {
1112         data->notify_data.data.length=(uint32)((dos_PutUniCode((char *)data->notify_data.data.string,
1113                         lp_servicename(snum), sizeof(data->notify_data.data.string),True) - sizeof(uint16))/sizeof(uint16));
1114 }
1115
1116 /*******************************************************************
1117  * fill a notify_info_data with the port name
1118  ********************************************************************/
1119 static void spoolss_notify_port_name(int snum, SPOOL_NOTIFY_INFO_DATA *data, print_queue_struct *queue, NT_PRINTER_INFO_LEVEL *printer)
1120 {
1121         /* even if it's strange, that's consistant in all the code */
1122
1123         data->notify_data.data.length=(uint32)((dos_PutUniCode((char *)data->notify_data.data.string,
1124                 lp_servicename(snum), sizeof(data->notify_data.data.string), True)  - sizeof(uint16))/sizeof(uint16));
1125 }
1126
1127 /*******************************************************************
1128  * fill a notify_info_data with the printername
1129  * jfmxxxx: it's incorrect, should be lp_printerdrivername()
1130  * but it doesn't exist, have to see what to do
1131  ********************************************************************/
1132 static void spoolss_notify_driver_name(int snum, SPOOL_NOTIFY_INFO_DATA *data, print_queue_struct *queue, NT_PRINTER_INFO_LEVEL *printer)
1133 {
1134         data->notify_data.data.length=(uint32)((dos_PutUniCode((char *)data->notify_data.data.string,
1135                         printer->info_2->drivername, sizeof(data->notify_data.data.string)-1, True)  - sizeof(uint16))/sizeof(uint16));
1136 }
1137
1138 /*******************************************************************
1139  * fill a notify_info_data with the comment
1140  ********************************************************************/
1141 static void spoolss_notify_comment(int snum, SPOOL_NOTIFY_INFO_DATA *data, print_queue_struct *queue, NT_PRINTER_INFO_LEVEL *printer)
1142 {
1143         if (*printer->info_2->comment == '\0')
1144                 data->notify_data.data.length=(uint32)((dos_PutUniCode((char *)data->notify_data.data.string,
1145                         lp_comment(snum), sizeof(data->notify_data.data.string)-1, True)  - sizeof(uint16))/sizeof(uint16));
1146         else
1147                 data->notify_data.data.length=(uint32)((dos_PutUniCode((char *)data->notify_data.data.string,
1148                         printer->info_2->comment, sizeof(data->notify_data.data.string)-1, True)  - sizeof(uint16))/sizeof(uint16));
1149 }
1150
1151 /*******************************************************************
1152  * fill a notify_info_data with the comment
1153  * jfm:xxxx incorrect, have to create a new smb.conf option
1154  * location = "Room 1, floor 2, building 3"
1155  ********************************************************************/
1156 static void spoolss_notify_location(int snum, SPOOL_NOTIFY_INFO_DATA *data, print_queue_struct *queue, NT_PRINTER_INFO_LEVEL *printer)
1157 {
1158         data->notify_data.data.length=(uint32)((dos_PutUniCode((char *)data->notify_data.data.string,
1159                         printer->info_2->location, sizeof(data->notify_data.data.string)-1, True)  - sizeof(uint16))/sizeof(uint16));
1160 }
1161
1162 /*******************************************************************
1163  * fill a notify_info_data with the device mode
1164  * jfm:xxxx don't to it for know but that's a real problem !!!
1165  ********************************************************************/
1166 static void spoolss_notify_devmode(int snum, SPOOL_NOTIFY_INFO_DATA *data, print_queue_struct *queue, NT_PRINTER_INFO_LEVEL *printer)
1167 {
1168 }
1169
1170 /*******************************************************************
1171  * fill a notify_info_data with the separator file name
1172  * jfm:xxxx just return no file could add an option to smb.conf
1173  * separator file = "separator.txt"
1174  ********************************************************************/
1175 static void spoolss_notify_sepfile(int snum, SPOOL_NOTIFY_INFO_DATA *data, print_queue_struct *queue, NT_PRINTER_INFO_LEVEL *printer)
1176 {
1177         data->notify_data.data.length=(uint32)((dos_PutUniCode((char *)data->notify_data.data.string,
1178                         printer->info_2->sepfile, sizeof(data->notify_data.data.string)-1,True)  - sizeof(uint16))/sizeof(uint16));
1179 }
1180
1181 /*******************************************************************
1182  * fill a notify_info_data with the print processor
1183  * jfm:xxxx return always winprint to indicate we don't do anything to it
1184  ********************************************************************/
1185 static void spoolss_notify_print_processor(int snum, SPOOL_NOTIFY_INFO_DATA *data, print_queue_struct *queue, NT_PRINTER_INFO_LEVEL *printer)
1186 {
1187         data->notify_data.data.length=(uint32)((dos_PutUniCode((char *)data->notify_data.data.string,
1188                         printer->info_2->printprocessor, sizeof(data->notify_data.data.string)-1, True)  - sizeof(uint16))/sizeof(uint16));
1189 }
1190
1191 /*******************************************************************
1192  * fill a notify_info_data with the print processor options
1193  * jfm:xxxx send an empty string
1194  ********************************************************************/
1195 static void spoolss_notify_parameters(int snum, SPOOL_NOTIFY_INFO_DATA *data, print_queue_struct *queue, NT_PRINTER_INFO_LEVEL *printer)
1196 {
1197         data->notify_data.data.length=(uint32)((dos_PutUniCode((char *)data->notify_data.data.string,
1198                         printer->info_2->parameters, sizeof(data->notify_data.data.string)-1, True)  - sizeof(uint16))/sizeof(uint16));
1199 }
1200
1201 /*******************************************************************
1202  * fill a notify_info_data with the data type
1203  * jfm:xxxx always send RAW as data type
1204  ********************************************************************/
1205 static void spoolss_notify_datatype(int snum, SPOOL_NOTIFY_INFO_DATA *data, print_queue_struct *queue, NT_PRINTER_INFO_LEVEL *printer)
1206 {
1207         data->notify_data.data.length=(uint32)((dos_PutUniCode((char *)data->notify_data.data.string,
1208                         printer->info_2->datatype, sizeof(data->notify_data.data.string)-1, True)  - sizeof(uint16))/sizeof(uint16));
1209 }
1210
1211 /*******************************************************************
1212  * fill a notify_info_data with the security descriptor
1213  * jfm:xxxx send an null pointer to say no security desc
1214  * have to implement security before !
1215  ********************************************************************/
1216 static void spoolss_notify_security_desc(int snum, SPOOL_NOTIFY_INFO_DATA *data, print_queue_struct *queue, NT_PRINTER_INFO_LEVEL *printer)
1217 {
1218         data->notify_data.data.length=0;
1219         data->notify_data.data.string[0]=0x00;
1220 }
1221
1222 /*******************************************************************
1223  * fill a notify_info_data with the attributes
1224  * jfm:xxxx a samba printer is always shared
1225  ********************************************************************/
1226 static void spoolss_notify_attributes(int snum, SPOOL_NOTIFY_INFO_DATA *data, print_queue_struct *queue, NT_PRINTER_INFO_LEVEL *printer)
1227 {
1228         data->notify_data.value[0] =   PRINTER_ATTRIBUTE_SHARED   \
1229                                      | PRINTER_ATTRIBUTE_LOCAL  \
1230                                      | PRINTER_ATTRIBUTE_RAW_ONLY ;
1231 }
1232
1233 /*******************************************************************
1234  * fill a notify_info_data with the priority
1235  ********************************************************************/
1236 static void spoolss_notify_priority(int snum, SPOOL_NOTIFY_INFO_DATA *data, print_queue_struct *queue, NT_PRINTER_INFO_LEVEL *printer)
1237 {
1238         data->notify_data.value[0] = printer->info_2->priority;
1239 }
1240
1241 /*******************************************************************
1242  * fill a notify_info_data with the default priority
1243  ********************************************************************/
1244 static void spoolss_notify_default_priority(int snum, SPOOL_NOTIFY_INFO_DATA *data, print_queue_struct *queue, NT_PRINTER_INFO_LEVEL *printer)
1245 {
1246         data->notify_data.value[0] = printer->info_2->default_priority;
1247 }
1248
1249 /*******************************************************************
1250  * fill a notify_info_data with the start time
1251  ********************************************************************/
1252 static void spoolss_notify_start_time(int snum, SPOOL_NOTIFY_INFO_DATA *data, print_queue_struct *queue, NT_PRINTER_INFO_LEVEL *printer)
1253 {
1254         data->notify_data.value[0] = printer->info_2->starttime;
1255 }
1256
1257 /*******************************************************************
1258  * fill a notify_info_data with the until time
1259  ********************************************************************/
1260 static void spoolss_notify_until_time(int snum, SPOOL_NOTIFY_INFO_DATA *data, print_queue_struct *queue, NT_PRINTER_INFO_LEVEL *printer)
1261 {
1262         data->notify_data.value[0] = printer->info_2->untiltime;
1263 }
1264
1265 /*******************************************************************
1266  * fill a notify_info_data with the status
1267  ********************************************************************/
1268 static void spoolss_notify_status(int snum, SPOOL_NOTIFY_INFO_DATA *data, print_queue_struct *queue, NT_PRINTER_INFO_LEVEL *printer)
1269 {
1270         int count;
1271         print_queue_struct *q=NULL;
1272         print_status_struct status;
1273
1274         memset(&status, 0, sizeof(status));
1275         count = print_queue_status(snum, &q, &status);
1276         data->notify_data.value[0]=(uint32) status.status;
1277         safe_free(q);
1278 }
1279
1280 /*******************************************************************
1281  * fill a notify_info_data with the number of jobs queued
1282  ********************************************************************/
1283 static void spoolss_notify_cjobs(int snum, SPOOL_NOTIFY_INFO_DATA *data, print_queue_struct *queue, NT_PRINTER_INFO_LEVEL *printer)
1284 {
1285         print_queue_struct *q=NULL;
1286         print_status_struct status;
1287
1288         memset(&status, 0, sizeof(status));
1289         data->notify_data.value[0] = print_queue_status(snum, &q, &status);
1290         safe_free(q);
1291 }
1292
1293 /*******************************************************************
1294  * fill a notify_info_data with the average ppm
1295  ********************************************************************/
1296 static void spoolss_notify_average_ppm(int snum, SPOOL_NOTIFY_INFO_DATA *data, print_queue_struct *queue, NT_PRINTER_INFO_LEVEL *printer)
1297 {
1298         /* always respond 8 pages per minutes */
1299         /* a little hard ! */
1300         data->notify_data.value[0] = printer->info_2->averageppm;
1301 }
1302
1303 /*******************************************************************
1304  * fill a notify_info_data with 
1305  ********************************************************************/
1306 static void spoolss_notify_username(int snum, SPOOL_NOTIFY_INFO_DATA *data, print_queue_struct *queue, NT_PRINTER_INFO_LEVEL *printer)
1307 {
1308         data->notify_data.data.length=(uint32)((dos_PutUniCode((char *)data->notify_data.data.string,
1309                         queue->user, sizeof(data->notify_data.data.string)-1, True)  - sizeof(uint16))/sizeof(uint16));
1310 }
1311
1312 /*******************************************************************
1313  * fill a notify_info_data with 
1314  ********************************************************************/
1315 static void spoolss_notify_job_status(int snum, SPOOL_NOTIFY_INFO_DATA *data, print_queue_struct *queue, NT_PRINTER_INFO_LEVEL *printer)
1316 {
1317         data->notify_data.value[0]=nt_printj_status(queue->status);
1318 }
1319
1320 /*******************************************************************
1321  * fill a notify_info_data with 
1322  ********************************************************************/
1323 static void spoolss_notify_job_name(int snum, SPOOL_NOTIFY_INFO_DATA *data, print_queue_struct *queue, NT_PRINTER_INFO_LEVEL *printer)
1324 {
1325         data->notify_data.data.length=(uint32)((dos_PutUniCode((char *)data->notify_data.data.string,
1326                         queue->file, sizeof(data->notify_data.data.string)-1, True)  - sizeof(uint16))/sizeof(uint16));
1327 }
1328
1329 /*******************************************************************
1330  * fill a notify_info_data with 
1331  ********************************************************************/
1332 static void spoolss_notify_job_status_string(int snum, SPOOL_NOTIFY_INFO_DATA *data, print_queue_struct *queue, NT_PRINTER_INFO_LEVEL *printer)
1333 {
1334         char *p = "unknown";
1335         switch (queue->status) {
1336         case LPQ_QUEUED:
1337                 p = "QUEUED";
1338                 break;
1339         case LPQ_PAUSED:
1340                 p = "PAUSED";
1341                 break;
1342         case LPQ_SPOOLING:
1343                 p = "SPOOLING";
1344                 break;
1345         case LPQ_PRINTING:
1346                 p = "PRINTING";
1347                 break;
1348         }
1349         data->notify_data.data.length=(uint32)((dos_PutUniCode((char *)data->notify_data.data.string,
1350                                 p, sizeof(data->notify_data.data.string)-1, True)  - sizeof(uint16))/sizeof(uint16));
1351 }
1352
1353 /*******************************************************************
1354  * fill a notify_info_data with 
1355  ********************************************************************/
1356 static void spoolss_notify_job_time(int snum, SPOOL_NOTIFY_INFO_DATA *data, print_queue_struct *queue, NT_PRINTER_INFO_LEVEL *printer)
1357 {
1358         data->notify_data.value[0]=0x0;
1359 }
1360
1361 /*******************************************************************
1362  * fill a notify_info_data with 
1363  ********************************************************************/
1364 static void spoolss_notify_job_size(int snum, SPOOL_NOTIFY_INFO_DATA *data, print_queue_struct *queue, NT_PRINTER_INFO_LEVEL *printer)
1365 {
1366         data->notify_data.value[0]=queue->size;
1367 }
1368
1369 /*******************************************************************
1370  * fill a notify_info_data with 
1371  ********************************************************************/
1372 static void spoolss_notify_job_position(int snum, SPOOL_NOTIFY_INFO_DATA *data, print_queue_struct *queue, NT_PRINTER_INFO_LEVEL *printer)
1373 {
1374         data->notify_data.value[0]=queue->job;
1375 }
1376
1377 #define END 65535
1378
1379 struct s_notify_info_data_table
1380 {
1381         uint16 type;
1382         uint16 field;
1383         char *name;
1384         uint32 size;
1385         void (*fn) (int snum, SPOOL_NOTIFY_INFO_DATA *data,
1386                     print_queue_struct *queue,
1387                     NT_PRINTER_INFO_LEVEL *printer);
1388 };
1389
1390 struct s_notify_info_data_table notify_info_data_table[] =
1391 {
1392 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_SERVER_NAME,         "PRINTER_NOTIFY_SERVER_NAME",         POINTER,   spoolss_notify_server_name },
1393 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_PRINTER_NAME,        "PRINTER_NOTIFY_PRINTER_NAME",        POINTER,   spoolss_notify_printer_name },
1394 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_SHARE_NAME,          "PRINTER_NOTIFY_SHARE_NAME",          POINTER,   spoolss_notify_share_name },
1395 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_PORT_NAME,           "PRINTER_NOTIFY_PORT_NAME",           POINTER,   spoolss_notify_port_name },
1396 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_DRIVER_NAME,         "PRINTER_NOTIFY_DRIVER_NAME",         POINTER,   spoolss_notify_driver_name },
1397 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_COMMENT,             "PRINTER_NOTIFY_COMMENT",             POINTER,   spoolss_notify_comment },
1398 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_LOCATION,            "PRINTER_NOTIFY_LOCATION",            POINTER,   spoolss_notify_location },
1399 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_DEVMODE,             "PRINTER_NOTIFY_DEVMODE",             POINTER,   spoolss_notify_devmode },
1400 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_SEPFILE,             "PRINTER_NOTIFY_SEPFILE",             POINTER,   spoolss_notify_sepfile },
1401 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_PRINT_PROCESSOR,     "PRINTER_NOTIFY_PRINT_PROCESSOR",     POINTER,   spoolss_notify_print_processor },
1402 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_PARAMETERS,          "PRINTER_NOTIFY_PARAMETERS",          POINTER,   spoolss_notify_parameters },
1403 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_DATATYPE,            "PRINTER_NOTIFY_DATATYPE",            POINTER,   spoolss_notify_datatype },
1404 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_SECURITY_DESCRIPTOR, "PRINTER_NOTIFY_SECURITY_DESCRIPTOR", POINTER,   spoolss_notify_security_desc },
1405 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_ATTRIBUTES,          "PRINTER_NOTIFY_ATTRIBUTES",          ONE_VALUE, spoolss_notify_attributes },
1406 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_PRIORITY,            "PRINTER_NOTIFY_PRIORITY",            ONE_VALUE, spoolss_notify_priority },
1407 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_DEFAULT_PRIORITY,    "PRINTER_NOTIFY_DEFAULT_PRIORITY",    ONE_VALUE, spoolss_notify_default_priority },
1408 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_START_TIME,          "PRINTER_NOTIFY_START_TIME",          ONE_VALUE, spoolss_notify_start_time },
1409 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_UNTIL_TIME,          "PRINTER_NOTIFY_UNTIL_TIME",          ONE_VALUE, spoolss_notify_until_time },
1410 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_STATUS,              "PRINTER_NOTIFY_STATUS",              ONE_VALUE, spoolss_notify_status },
1411 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_STATUS_STRING,       "PRINTER_NOTIFY_STATUS_STRING",       POINTER,   NULL },
1412 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_CJOBS,               "PRINTER_NOTIFY_CJOBS",               ONE_VALUE, spoolss_notify_cjobs },
1413 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_AVERAGE_PPM,         "PRINTER_NOTIFY_AVERAGE_PPM",         ONE_VALUE, spoolss_notify_average_ppm },
1414 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_TOTAL_PAGES,         "PRINTER_NOTIFY_TOTAL_PAGES",         POINTER,   NULL },
1415 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_PAGES_PRINTED,       "PRINTER_NOTIFY_PAGES_PRINTED",       POINTER,   NULL },
1416 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_TOTAL_BYTES,         "PRINTER_NOTIFY_TOTAL_BYTES",         POINTER,   NULL },
1417 { PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_BYTES_PRINTED,       "PRINTER_NOTIFY_BYTES_PRINTED",       POINTER,   NULL },
1418 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_PRINTER_NAME,            "JOB_NOTIFY_PRINTER_NAME",            POINTER,   spoolss_notify_printer_name },
1419 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_MACHINE_NAME,            "JOB_NOTIFY_MACHINE_NAME",            POINTER,   spoolss_notify_server_name },
1420 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_PORT_NAME,               "JOB_NOTIFY_PORT_NAME",               POINTER,   spoolss_notify_port_name },
1421 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_USER_NAME,               "JOB_NOTIFY_USER_NAME",               POINTER,   spoolss_notify_username },
1422 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_NOTIFY_NAME,             "JOB_NOTIFY_NOTIFY_NAME",             POINTER,   spoolss_notify_username },
1423 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_DATATYPE,                "JOB_NOTIFY_DATATYPE",                POINTER,   spoolss_notify_datatype },
1424 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_PRINT_PROCESSOR,         "JOB_NOTIFY_PRINT_PROCESSOR",         POINTER,   spoolss_notify_print_processor },
1425 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_PARAMETERS,              "JOB_NOTIFY_PARAMETERS",              POINTER,   spoolss_notify_parameters },
1426 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_DRIVER_NAME,             "JOB_NOTIFY_DRIVER_NAME",             POINTER,   spoolss_notify_driver_name },
1427 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_DEVMODE,                 "JOB_NOTIFY_DEVMODE",                 POINTER,   spoolss_notify_devmode },
1428 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_STATUS,                  "JOB_NOTIFY_STATUS",                  ONE_VALUE, spoolss_notify_job_status },
1429 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_STATUS_STRING,           "JOB_NOTIFY_STATUS_STRING",           POINTER,   spoolss_notify_job_status_string },
1430 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_SECURITY_DESCRIPTOR,     "JOB_NOTIFY_SECURITY_DESCRIPTOR",     POINTER,   NULL },
1431 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_DOCUMENT,                "JOB_NOTIFY_DOCUMENT",                POINTER,   spoolss_notify_job_name },
1432 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_PRIORITY,                "JOB_NOTIFY_PRIORITY",                ONE_VALUE, spoolss_notify_priority },
1433 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_POSITION,                "JOB_NOTIFY_POSITION",                ONE_VALUE, spoolss_notify_job_position },
1434 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_SUBMITTED,               "JOB_NOTIFY_SUBMITTED",               POINTER,   NULL },
1435 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_START_TIME,              "JOB_NOTIFY_START_TIME",              ONE_VALUE, spoolss_notify_start_time },
1436 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_UNTIL_TIME,              "JOB_NOTIFY_UNTIL_TIME",              ONE_VALUE, spoolss_notify_until_time },
1437 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_TIME,                    "JOB_NOTIFY_TIME",                    ONE_VALUE, spoolss_notify_job_time },
1438 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_TOTAL_PAGES,             "JOB_NOTIFY_TOTAL_PAGES",             ONE_VALUE, NULL },
1439 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_PAGES_PRINTED,           "JOB_NOTIFY_PAGES_PRINTED",           ONE_VALUE, NULL },
1440 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_TOTAL_BYTES,             "JOB_NOTIFY_TOTAL_BYTES",             ONE_VALUE, spoolss_notify_job_size },
1441 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_BYTES_PRINTED,           "JOB_NOTIFY_BYTES_PRINTED",           ONE_VALUE, NULL },
1442 { END,                 END,                                "",                                   END,       NULL }
1443 };
1444
1445 /*******************************************************************
1446 return the size of info_data structure
1447 ********************************************************************/  
1448 static uint32 size_of_notify_info_data(uint16 type, uint16 field)
1449 {
1450         int i=0;
1451
1452         while (notify_info_data_table[i].type != END)
1453         {
1454                 if ( (notify_info_data_table[i].type == type ) &&
1455                      (notify_info_data_table[i].field == field ) )
1456                 {
1457                         return (notify_info_data_table[i].size);
1458                 }
1459                 i++;
1460         }
1461         return (65535);
1462 }
1463
1464 /*******************************************************************
1465 return the type of notify_info_data
1466 ********************************************************************/  
1467 static BOOL type_of_notify_info_data(uint16 type, uint16 field)
1468 {
1469         int i=0;
1470
1471         while (notify_info_data_table[i].type != END)
1472         {
1473                 if ( (notify_info_data_table[i].type == type ) &&
1474                      (notify_info_data_table[i].field == field ) )
1475                 {
1476                         if (notify_info_data_table[i].size == POINTER)
1477                         {
1478                                 return (False);
1479                         }
1480                         else
1481                         {
1482                                 return (True);
1483                         }
1484                 }
1485                 i++;
1486         }
1487         return (False);
1488 }
1489
1490 /****************************************************************************
1491 ****************************************************************************/
1492 static int search_notify(uint16 type, uint16 field, int *value)
1493 {       
1494         int j;
1495         BOOL found;
1496
1497         for (j=0, found=False; found==False && notify_info_data_table[j].type != END ; j++)
1498         {
1499                 if ( (notify_info_data_table[j].type  == type  ) &&
1500                      (notify_info_data_table[j].field == field ) )
1501                         found=True;
1502         }
1503         *value=--j;
1504
1505         if ( found && (notify_info_data_table[j].fn != NULL) )
1506                 return True;
1507         else
1508                 return False;   
1509 }
1510
1511 /****************************************************************************
1512 ****************************************************************************/
1513 static void construct_info_data(SPOOL_NOTIFY_INFO_DATA *info_data, uint16 type, uint16 field, int id)
1514 {
1515         info_data->type     = type;
1516         info_data->field    = field;
1517         info_data->reserved = 0;
1518         info_data->id       = id;
1519         info_data->size     = size_of_notify_info_data(type, field);
1520         info_data->enc_type = type_of_notify_info_data(type, field);
1521 }
1522
1523
1524 /*******************************************************************
1525  *
1526  * fill a notify_info struct with info asked
1527  * 
1528  ********************************************************************/
1529 static BOOL construct_notify_printer_info(SPOOL_NOTIFY_INFO *info, int snum, SPOOL_NOTIFY_OPTION_TYPE *option_type, uint32 id)
1530 {
1531         int field_num,j;
1532         uint16 type;
1533         uint16 field;
1534
1535         SPOOL_NOTIFY_INFO_DATA *current_data;
1536         NT_PRINTER_INFO_LEVEL *printer = NULL;
1537         print_queue_struct *queue=NULL;
1538         
1539         DEBUG(4,("construct_notify_printer_info\n"));
1540         
1541         type=option_type->type;
1542
1543         DEBUGADD(4,("Notify type: [%s], number of notify info: [%d] on printer: [%s]\n",
1544                 (option_type->type==PRINTER_NOTIFY_TYPE?"PRINTER_NOTIFY_TYPE":"JOB_NOTIFY_TYPE"), 
1545                 option_type->count, lp_servicename(snum)));
1546         
1547         if (get_a_printer(&printer, 2, lp_servicename(snum))!=0)
1548                 return False;
1549
1550         for(field_num=0; field_num<option_type->count; field_num++) {
1551                 field = option_type->fields[field_num];
1552                 DEBUGADD(4,("notify [%d]: type [%x], field [%x]\n", field_num, type, field));
1553
1554                 if (!search_notify(type, field, &j) )
1555                         continue;
1556                 
1557                 if((info->data=Realloc(info->data, (info->count+1)*sizeof(SPOOL_NOTIFY_INFO_DATA))) == NULL) {
1558                         return False;
1559                 }
1560                 current_data=&info->data[info->count];
1561
1562                 construct_info_data(current_data, type, field, id);             
1563                 notify_info_data_table[j].fn(snum, current_data, queue, printer);
1564
1565                 info->count++;
1566         }
1567
1568         free_a_printer(&printer, 2);
1569         return True;
1570 }
1571
1572 /*******************************************************************
1573  *
1574  * fill a notify_info struct with info asked
1575  * 
1576  ********************************************************************/
1577 static BOOL construct_notify_jobs_info(print_queue_struct *queue, SPOOL_NOTIFY_INFO *info, int snum, SPOOL_NOTIFY_OPTION_TYPE *option_type, uint32 id)
1578 {
1579         int field_num,j;
1580         uint16 type;
1581         uint16 field;
1582
1583         SPOOL_NOTIFY_INFO_DATA *current_data;
1584         NT_PRINTER_INFO_LEVEL *printer = NULL;
1585         
1586         DEBUG(4,("construct_notify_jobs_info\n"));
1587         
1588         type = option_type->type;
1589
1590         DEBUGADD(4,("Notify type: [%s], number of notify info: [%d]\n",
1591                 (option_type->type==PRINTER_NOTIFY_TYPE?"PRINTER_NOTIFY_TYPE":"JOB_NOTIFY_TYPE"), 
1592                 option_type->count));
1593
1594         if (get_a_printer(&printer, 2, lp_servicename(snum))!=0)
1595                 return False;
1596         
1597         for(field_num=0; field_num<option_type->count; field_num++) {
1598                 field = option_type->fields[field_num];
1599
1600                 if (!search_notify(type, field, &j) )
1601                         continue;
1602
1603                 if((info->data=Realloc(info->data, (info->count+1)*sizeof(SPOOL_NOTIFY_INFO_DATA))) == NULL) {
1604                         return False;
1605                 }
1606
1607                 current_data=&(info->data[info->count]);
1608
1609                 construct_info_data(current_data, type, field, id);
1610                 notify_info_data_table[j].fn(snum, current_data, queue, printer);
1611                 info->count++;
1612         }
1613
1614         free_a_printer(&printer, 2);    
1615         return True;
1616 }
1617
1618 /*
1619  * JFM: The enumeration is not that simple, it's even non obvious.
1620  *
1621  * let's take an example: I want to monitor the PRINTER SERVER for
1622  * the printer's name and the number of jobs currently queued.
1623  * So in the NOTIFY_OPTION, I have one NOTIFY_OPTION_TYPE structure.
1624  * Its type is PRINTER_NOTIFY_TYPE and it has 2 fields NAME and CJOBS.
1625  * 
1626  * I have 3 printers on the back of my server.
1627  *
1628  * Now the response is a NOTIFY_INFO structure, with 6 NOTIFY_INFO_DATA
1629  * structures.
1630  *   Number     Data                    Id
1631  *      1       printer 1 name          1
1632  *      2       printer 1 cjob          1
1633  *      3       printer 2 name          2
1634  *      4       printer 2 cjob          2
1635  *      5       printer 3 name          3
1636  *      6       printer 3 name          3
1637  *
1638  * that's the print server case, the printer case is even worse.
1639  */
1640
1641
1642
1643 /*******************************************************************
1644  *
1645  * enumerate all printers on the printserver
1646  * fill a notify_info struct with info asked
1647  * 
1648  ********************************************************************/
1649 static uint32 printserver_notify_info(const POLICY_HND *hnd, SPOOL_NOTIFY_INFO *info)
1650 {
1651         int snum;
1652         Printer_entry *Printer=find_printer_index_by_hnd(hnd);
1653         int n_services=lp_numservices();
1654         int i;
1655         uint32 id;
1656         SPOOL_NOTIFY_OPTION *option;
1657         SPOOL_NOTIFY_OPTION_TYPE *option_type;
1658
1659         DEBUG(4,("printserver_notify_info\n"));
1660         
1661         option=Printer->notify.option;
1662         id=1;
1663         info->version=2;
1664         info->data=NULL;
1665         info->count=0;
1666
1667         for (i=0; i<option->count; i++) {
1668                 option_type=&(option->ctr.type[i]);
1669                 
1670                 if (option_type->type!=PRINTER_NOTIFY_TYPE)
1671                         continue;
1672                 
1673                 for (snum=0; snum<n_services; snum++)
1674                         if ( lp_browseable(snum) && lp_snum_ok(snum) && lp_print_ok(snum) )
1675                                 if (construct_notify_printer_info(info, snum, option_type, id))
1676                                         id++;
1677         }
1678                         
1679         /*
1680          * Debugging information, don't delete.
1681          */
1682         /* 
1683         DEBUG(1,("dumping the NOTIFY_INFO\n"));
1684         DEBUGADD(1,("info->version:[%d], info->flags:[%d], info->count:[%d]\n", info->version, info->flags, info->count));
1685         DEBUGADD(1,("num\ttype\tfield\tres\tid\tsize\tenc_type\n"));
1686         
1687         for (i=0; i<info->count; i++) {
1688                 DEBUGADD(1,("[%d]\t[%d]\t[%d]\t[%d]\t[%d]\t[%d]\t[%d]\n",
1689                 i, info->data[i].type, info->data[i].field, info->data[i].reserved,
1690                 info->data[i].id, info->data[i].size, info->data[i].enc_type));
1691         }
1692         */
1693         
1694         return NT_STATUS_NO_PROBLEMO;
1695 }
1696
1697 /*******************************************************************
1698  *
1699  * fill a notify_info struct with info asked
1700  * 
1701  ********************************************************************/
1702 static uint32 printer_notify_info(POLICY_HND *hnd, SPOOL_NOTIFY_INFO *info)
1703 {
1704         int snum;
1705         Printer_entry *Printer=find_printer_index_by_hnd(hnd);
1706         int i;
1707         uint32 id;
1708         SPOOL_NOTIFY_OPTION *option;
1709         SPOOL_NOTIFY_OPTION_TYPE *option_type;
1710         int count,j;
1711         print_queue_struct *queue=NULL;
1712         print_status_struct status;
1713         
1714         DEBUG(4,("printer_notify_info\n"));
1715
1716         option=Printer->notify.option;
1717         id=0xffffffff;
1718         info->version=2;
1719         info->data=NULL;
1720         info->count=0;
1721
1722         get_printer_snum(hnd, &snum);
1723
1724         for (i=0; i<option->count; i++) {
1725                 option_type=&option->ctr.type[i];
1726                 
1727                 switch ( option_type->type ) {
1728                 case PRINTER_NOTIFY_TYPE:
1729                         if(construct_notify_printer_info(info, snum, option_type, id))
1730                                 id--;
1731                         break;
1732                         
1733                 case JOB_NOTIFY_TYPE:
1734                         memset(&status, 0, sizeof(status));     
1735                         count = print_queue_status(snum, &queue, &status);
1736                         for (j=0; j<count; j++)
1737                                 construct_notify_jobs_info(&queue[j], info, snum, option_type, queue[j].job);
1738                         safe_free(queue);
1739                         break;
1740                 }
1741         }
1742         
1743         /*
1744          * Debugging information, don't delete.
1745          */
1746         /* 
1747         DEBUG(1,("dumping the NOTIFY_INFO\n"));
1748         DEBUGADD(1,("info->version:[%d], info->flags:[%d], info->count:[%d]\n", info->version, info->flags, info->count));
1749         DEBUGADD(1,("num\ttype\tfield\tres\tid\tsize\tenc_type\n"));
1750         
1751         for (i=0; i<info->count; i++) {
1752                 DEBUGADD(1,("[%d]\t[%d]\t[%d]\t[%d]\t[%d]\t[%d]\t[%d]\n",
1753                 i, info->data[i].type, info->data[i].field, info->data[i].reserved,
1754                 info->data[i].id, info->data[i].size, info->data[i].enc_type));
1755         }
1756         */
1757         return NT_STATUS_NO_PROBLEMO;
1758 }
1759
1760 /********************************************************************
1761  * spoolss_rfnpcnex
1762  ********************************************************************/
1763 uint32 _spoolss_rfnpcnex( POLICY_HND *handle, uint32 change,
1764                           SPOOL_NOTIFY_OPTION *option, SPOOL_NOTIFY_INFO *info)
1765 {
1766         Printer_entry *Printer=find_printer_index_by_hnd(handle);
1767
1768         if (!OPEN_HANDLE(Printer)) {
1769                 DEBUG(0,("_spoolss_rfnpcnex: Invalid handle (%s).\n",OUR_HANDLE(handle)));
1770                 return ERROR_INVALID_HANDLE;
1771         }
1772
1773         DEBUG(4,("Printer type %x\n",Printer->printer_type));
1774
1775         /* jfm: the change value isn't used right now.
1776          *      we will honour it when
1777          *      a) we'll be able to send notification to the client
1778          *      b) we'll have a way to communicate between the spoolss process.
1779          *
1780          *      same thing for option->flags
1781          *      I should check for PRINTER_NOTIFY_OPTIONS_REFRESH but as 
1782          *      I don't have a global notification system, I'm sending back all the
1783          *      informations even when _NOTHING_ has changed.
1784          */
1785
1786         /* just discard the SPOOL_NOTIFY_OPTION */
1787         if (option!=NULL)
1788                 safe_free(option->ctr.type);
1789         
1790         switch (Printer->printer_type) {
1791                 case PRINTER_HANDLE_IS_PRINTSERVER:
1792                         return printserver_notify_info(handle, info);
1793                 case PRINTER_HANDLE_IS_PRINTER:
1794                         return printer_notify_info(handle, info);
1795         }
1796
1797         return ERROR_INVALID_HANDLE;
1798 }
1799
1800 /********************************************************************
1801  * construct_printer_info_0
1802  * fill a printer_info_1 struct
1803  ********************************************************************/
1804 static BOOL construct_printer_info_0(PRINTER_INFO_0 *printer, int snum, fstring servername)
1805 {
1806         pstring chaine;
1807         int count;
1808         NT_PRINTER_INFO_LEVEL *ntprinter = NULL;
1809         counter_printer_0 *session_counter;
1810         uint32 global_counter;
1811         struct tm *t;
1812         time_t setup_time = time(NULL);
1813
1814         print_queue_struct *queue=NULL;
1815         print_status_struct status;
1816         
1817         memset(&status, 0, sizeof(status));     
1818
1819         if (get_a_printer(&ntprinter, 2, lp_servicename(snum)) != 0)
1820                 return False;
1821
1822         count = print_queue_status(snum, &queue, &status);
1823
1824         /* check if we already have a counter for this printer */       
1825         session_counter = (counter_printer_0 *)ubi_dlFirst(&counter_list);
1826
1827         for(; session_counter; session_counter = (counter_printer_0 *)ubi_dlNext(session_counter)) {
1828                 if (session_counter->snum == snum)
1829                         break;
1830         }
1831
1832         /* it's the first time, add it to the list */
1833         if (session_counter==NULL) {
1834                 if((session_counter=(counter_printer_0 *)malloc(sizeof(counter_printer_0))) == NULL) {
1835                         free_a_printer(&ntprinter, 2);
1836                         return False;
1837                 }
1838                 ZERO_STRUCTP(session_counter);
1839                 session_counter->snum=snum;
1840                 session_counter->counter=0;
1841                 ubi_dlAddHead( &counter_list, (ubi_dlNode *)session_counter);
1842         }
1843         
1844         /* increment it */
1845         session_counter->counter++;
1846         
1847         /* JFM:
1848          * the global_counter should be stored in a TDB as it's common to all the clients
1849          * and should be zeroed on samba startup
1850          */
1851         global_counter=session_counter->counter;
1852         
1853         /* the description and the name are of the form \\server\share */
1854         slprintf(chaine,sizeof(chaine)-1,"\\\\%s\\%s",servername, ntprinter->info_2->printername);
1855
1856         init_unistr(&printer->printername, chaine);
1857         
1858         slprintf(chaine,sizeof(chaine)-1,"\\\\%s", servername);
1859         init_unistr(&printer->servername, chaine);
1860         
1861         printer->cjobs = count;
1862         printer->total_jobs = 0;
1863         printer->total_bytes = 0;
1864
1865         t=gmtime(&setup_time);
1866         ntprinter->info_2->setuptime = (uint32)setup_time; /* FIXME !! */
1867
1868         printer->year = t->tm_year+1900;
1869         printer->month = t->tm_mon+1;
1870         printer->dayofweek = t->tm_wday;
1871         printer->day = t->tm_mday;
1872         printer->hour = t->tm_hour;
1873         printer->minute = t->tm_min;
1874         printer->second = t->tm_sec;
1875         printer->milliseconds = 0;
1876
1877         printer->global_counter = global_counter;
1878         printer->total_pages = 0;
1879         printer->major_version = 0x0004;        /* NT 4 */
1880         printer->build_version = 0x0565;        /* build 1381 */
1881         printer->unknown7 = 0x1;
1882         printer->unknown8 = 0x0;
1883         printer->unknown9 = 0x0;
1884         printer->session_counter = session_counter->counter;
1885         printer->unknown11 = 0x0;
1886         printer->printer_errors = 0x0;          /* number of print failure */
1887         printer->unknown13 = 0x0;
1888         printer->unknown14 = 0x1;
1889         printer->unknown15 = 0x024a;            /* 586 Pentium ? */
1890         printer->unknown16 = 0x0;
1891         printer->change_id = ntprinter->info_2->changeid; /* ChangeID in milliseconds*/
1892         printer->unknown18 = 0x0;
1893         printer->status = nt_printq_status(status.status);
1894         printer->unknown20 = 0x0;
1895         printer->c_setprinter = ntprinter->info_2->c_setprinter; /* how many times setprinter has been called */
1896         printer->unknown22 = 0x0;
1897         printer->unknown23 = 0x6;               /* 6  ???*/
1898         printer->unknown24 = 0;                 /* unknown 24 to 26 are always 0 */
1899         printer->unknown25 = 0;
1900         printer->unknown26 = 0;
1901         printer->unknown27 = 0;
1902         printer->unknown28 = 0;
1903         printer->unknown29 = 0;
1904         
1905         safe_free(queue);
1906         free_a_printer(&ntprinter,2);
1907         return (True);  
1908 }
1909
1910 /********************************************************************
1911  * construct_printer_info_1
1912  * fill a printer_info_1 struct
1913  ********************************************************************/
1914 static BOOL construct_printer_info_1(fstring server, uint32 flags, PRINTER_INFO_1 *printer, int snum)
1915 {
1916         pstring chaine;
1917         pstring chaine2;
1918         NT_PRINTER_INFO_LEVEL *ntprinter = NULL;
1919
1920         if (get_a_printer(&ntprinter, 2, lp_servicename(snum)) != 0)
1921                 return False;
1922
1923         printer->flags=flags;
1924
1925         if (*ntprinter->info_2->comment == '\0') {
1926                 init_unistr(&printer->comment, lp_comment(snum));
1927                 snprintf(chaine,sizeof(chaine)-1,"%s%s,%s,%s",server, ntprinter->info_2->printername,
1928                         ntprinter->info_2->drivername, lp_comment(snum));
1929         }
1930         else {
1931                 init_unistr(&printer->comment, ntprinter->info_2->comment); /* saved comment. */
1932                 snprintf(chaine,sizeof(chaine)-1,"%s%s,%s,%s",server, ntprinter->info_2->printername,
1933                         ntprinter->info_2->drivername, ntprinter->info_2->comment);
1934         }
1935                 
1936         snprintf(chaine2,sizeof(chaine)-1,"%s%s", server, ntprinter->info_2->printername);
1937
1938         init_unistr(&printer->description, chaine);
1939         init_unistr(&printer->name, chaine2);   
1940         
1941         free_a_printer(&ntprinter,2);
1942
1943         return True;
1944 }
1945
1946 /****************************************************************************
1947  Free a DEVMODE struct.
1948 ****************************************************************************/
1949
1950 static void free_dev_mode(DEVICEMODE *dev)
1951 {
1952         if (dev == NULL)
1953                 return;
1954
1955         if (dev->private)
1956                 safe_free(dev->private);
1957
1958         safe_free(dev); 
1959 }
1960
1961 /****************************************************************************
1962  Create a DEVMODE struct. Returns malloced memory.
1963 ****************************************************************************/
1964
1965 static DEVICEMODE *construct_dev_mode(int snum, char *servername)
1966 {
1967         char adevice[32];
1968         char aform[32];
1969         NT_PRINTER_INFO_LEVEL *printer = NULL;
1970         NT_DEVICEMODE *ntdevmode = NULL;
1971         DEVICEMODE *devmode = NULL;
1972
1973         DEBUG(7,("construct_dev_mode\n"));
1974         
1975         DEBUGADD(8,("getting printer characteristics\n"));
1976
1977         if ((devmode = (DEVICEMODE *)malloc(sizeof(DEVICEMODE))) == NULL) {
1978                 DEBUG(0,("construct_dev_mode: malloc fail.\n"));
1979                 return NULL;
1980         }
1981
1982         ZERO_STRUCTP(devmode);  
1983
1984         if(get_a_printer(&printer, 2, lp_servicename(snum)) != 0)
1985                 goto fail;
1986
1987         if (printer->info_2->devmode)
1988                 ntdevmode = dup_nt_devicemode(printer->info_2->devmode);
1989
1990         if (ntdevmode == NULL)
1991                 goto fail;
1992
1993         DEBUGADD(8,("loading DEVICEMODE\n"));
1994
1995         snprintf(adevice, sizeof(adevice), "\\\\%s\\%s", global_myname, printer->info_2->printername);
1996         init_unistr(&devmode->devicename, adevice);
1997
1998         snprintf(aform, sizeof(aform), ntdevmode->formname);
1999         init_unistr(&devmode->formname, aform);
2000
2001         devmode->specversion      = ntdevmode->specversion;
2002         devmode->driverversion    = ntdevmode->driverversion;
2003         devmode->size             = ntdevmode->size;
2004         devmode->driverextra      = ntdevmode->driverextra;
2005         devmode->fields           = ntdevmode->fields;
2006                                     
2007         devmode->orientation      = ntdevmode->orientation;     
2008         devmode->papersize        = ntdevmode->papersize;
2009         devmode->paperlength      = ntdevmode->paperlength;
2010         devmode->paperwidth       = ntdevmode->paperwidth;
2011         devmode->scale            = ntdevmode->scale;
2012         devmode->copies           = ntdevmode->copies;
2013         devmode->defaultsource    = ntdevmode->defaultsource;
2014         devmode->printquality     = ntdevmode->printquality;
2015         devmode->color            = ntdevmode->color;
2016         devmode->duplex           = ntdevmode->duplex;
2017         devmode->yresolution      = ntdevmode->yresolution;
2018         devmode->ttoption         = ntdevmode->ttoption;
2019         devmode->collate          = ntdevmode->collate;
2020         devmode->icmmethod        = ntdevmode->icmmethod;
2021         devmode->icmintent        = ntdevmode->icmintent;
2022         devmode->mediatype        = ntdevmode->mediatype;
2023         devmode->dithertype       = ntdevmode->dithertype;
2024
2025         if (ntdevmode->private != NULL) {
2026                 if ((devmode->private=(uint8 *)memdup(ntdevmode->private, ntdevmode->driverextra)) == NULL)
2027                         goto fail;
2028         }
2029
2030         free_nt_devicemode(&ntdevmode);
2031         free_a_printer(&printer,2);
2032
2033         return devmode;
2034
2035   fail:
2036
2037         if (ntdevmode)
2038                 free_nt_devicemode(&ntdevmode);
2039         if (printer)
2040                 free_a_printer(&printer,2);
2041         free_dev_mode(devmode);
2042
2043         return NULL;
2044 }
2045
2046 /********************************************************************
2047  * construct_printer_info_2
2048  * fill a printer_info_2 struct
2049  ********************************************************************/
2050
2051 static BOOL construct_printer_info_2(fstring servername, PRINTER_INFO_2 *printer, int snum)
2052 {
2053         pstring chaine;
2054         pstring chaine2;
2055         pstring sl;
2056         int count;
2057         NT_PRINTER_INFO_LEVEL *ntprinter = NULL;
2058
2059         print_queue_struct *queue=NULL;
2060         print_status_struct status;
2061         memset(&status, 0, sizeof(status));     
2062
2063         if (get_a_printer(&ntprinter, 2, lp_servicename(snum)) !=0 )
2064                 return False;
2065                 
2066         memset(&status, 0, sizeof(status));             
2067         count = print_queue_status(snum, &queue, &status);
2068
2069         snprintf(chaine, sizeof(chaine)-1, "%s", servername);
2070
2071         if (strlen(servername)!=0)
2072                 fstrcpy(sl, "\\");
2073         else
2074                 fstrcpy(sl, '\0');
2075
2076         snprintf(chaine2, sizeof(chaine)-1, "%s%s%s", servername, sl, ntprinter->info_2->printername);
2077
2078         init_unistr(&printer->servername, chaine);                              /* servername*/
2079         init_unistr(&printer->printername, chaine2);                            /* printername*/
2080         init_unistr(&printer->sharename, lp_servicename(snum));                 /* sharename */
2081         init_unistr(&printer->portname, ntprinter->info_2->portname);                   /* port */      
2082         init_unistr(&printer->drivername, ntprinter->info_2->drivername);       /* drivername */
2083
2084         if (*ntprinter->info_2->comment == '\0')
2085                 init_unistr(&printer->comment, lp_comment(snum));                       /* comment */   
2086         else
2087                 init_unistr(&printer->comment, ntprinter->info_2->comment); /* saved comment. */
2088
2089         init_unistr(&printer->location, ntprinter->info_2->location);           /* location */  
2090         init_unistr(&printer->sepfile, ntprinter->info_2->sepfile);             /* separator file */
2091         init_unistr(&printer->printprocessor, ntprinter->info_2->printprocessor);/* print processor */
2092         init_unistr(&printer->datatype, ntprinter->info_2->datatype);           /* datatype */  
2093         init_unistr(&printer->parameters, ntprinter->info_2->parameters);       /* parameters (of print processor) */   
2094
2095         printer->attributes = ntprinter->info_2->attributes;
2096
2097         printer->priority = ntprinter->info_2->priority;                                /* priority */  
2098         printer->defaultpriority = ntprinter->info_2->default_priority;         /* default priority */
2099         printer->starttime = ntprinter->info_2->starttime;                      /* starttime */
2100         printer->untiltime = ntprinter->info_2->untiltime;                      /* untiltime */
2101         printer->status = nt_printq_status(status.status);                      /* status */
2102         printer->cjobs = count;                                                 /* jobs */
2103         printer->averageppm = ntprinter->info_2->averageppm;                    /* average pages per minute */
2104                         
2105         if((printer->devmode = construct_dev_mode(snum, servername)) == NULL) {
2106                 DEBUG(8, ("Returning NULL Devicemode!\n"));
2107         }
2108
2109         if (ntprinter->info_2->secdesc_buf && ntprinter->info_2->secdesc_buf->len != 0) {
2110                 /* steal the printer info sec_desc structure.  [badly done]. */
2111                 printer->secdesc = ntprinter->info_2->secdesc_buf->sec;
2112                 ntprinter->info_2->secdesc_buf->sec = NULL; /* Stolen memory. */
2113                 ntprinter->info_2->secdesc_buf->len = 0; /* Stolen memory. */
2114                 ntprinter->info_2->secdesc_buf->max_len = 0; /* Stolen memory. */
2115         }
2116         else {
2117                 printer->secdesc = NULL;
2118         }
2119
2120         free_a_printer(&ntprinter, 2);
2121         safe_free(queue);
2122         return True;
2123 }
2124
2125 /********************************************************************
2126  * construct_printer_info_3
2127  * fill a printer_info_3 struct
2128  ********************************************************************/
2129 static BOOL construct_printer_info_3(fstring servername,
2130                         PRINTER_INFO_3 **pp_printer, int snum)
2131 {
2132         NT_PRINTER_INFO_LEVEL *ntprinter = NULL;
2133         PRINTER_INFO_3 *printer = NULL;
2134
2135         if (get_a_printer(&ntprinter, 2, lp_servicename(snum)) !=0 )
2136                 return False;
2137
2138         *pp_printer = NULL;
2139         if ((printer = (PRINTER_INFO_3 *)malloc(sizeof(PRINTER_INFO_3))) == NULL) {
2140                 DEBUG(0,("construct_printer_info_3: malloc fail.\n"));
2141                 return False;
2142         }
2143
2144         ZERO_STRUCTP(printer);
2145         
2146         printer->flags = 4; /* These are the components of the SD we are returning. */
2147         if (ntprinter->info_2->secdesc_buf && ntprinter->info_2->secdesc_buf->len != 0) {
2148                 /* steal the printer info sec_desc structure.  [badly done]. */
2149                 printer->secdesc = ntprinter->info_2->secdesc_buf->sec;
2150
2151 #if 0
2152                 /*
2153                  * Set the flags for the components we are returning.
2154                  */
2155
2156                 if (printer->secdesc->owner_sid)
2157                         printer->flags |= OWNER_SECURITY_INFORMATION;
2158
2159                 if (printer->secdesc->grp_sid)
2160                         printer->flags |= GROUP_SECURITY_INFORMATION;
2161
2162                 if (printer->secdesc->dacl)
2163                         printer->flags |= DACL_SECURITY_INFORMATION;
2164
2165                 if (printer->secdesc->sacl)
2166                         printer->flags |= SACL_SECURITY_INFORMATION;
2167 #endif
2168
2169                 ntprinter->info_2->secdesc_buf->sec = NULL; /* Stolen the malloced memory. */
2170                 ntprinter->info_2->secdesc_buf->len = 0; /* Stolen the malloced memory. */
2171                 ntprinter->info_2->secdesc_buf->max_len = 0; /* Stolen the malloced memory. */
2172         }
2173
2174         free_a_printer(&ntprinter, 2);
2175
2176         *pp_printer = printer;
2177         return True;
2178 }
2179
2180 /********************************************************************
2181  Spoolss_enumprinters.
2182 ********************************************************************/
2183 static BOOL enum_all_printers_info_1(fstring server, uint32 flags, NEW_BUFFER *buffer, uint32 offered, uint32 *needed, uint32 *returned)
2184 {
2185         int snum;
2186         int i;
2187         int n_services=lp_numservices();
2188         PRINTER_INFO_1 *printers=NULL;
2189         PRINTER_INFO_1 current_prt;
2190         
2191         DEBUG(4,("enum_all_printers_info_1\n"));        
2192
2193         for (snum=0; snum<n_services; snum++) {
2194                 if (lp_browseable(snum) && lp_snum_ok(snum) && lp_print_ok(snum) ) {
2195                         DEBUG(4,("Found a printer in smb.conf: %s[%x]\n", lp_servicename(snum), snum));
2196                                 
2197                         if (construct_printer_info_1(server, flags, &current_prt, snum)) {
2198                                 if((printers=Realloc(printers, (*returned +1)*sizeof(PRINTER_INFO_1))) == NULL) {
2199                                         *returned=0;
2200                                         return ERROR_NOT_ENOUGH_MEMORY;
2201                                 }
2202                                 DEBUG(4,("ReAlloced memory for [%d] PRINTER_INFO_1\n", *returned));             
2203                                 memcpy(&(printers[*returned]), &current_prt, sizeof(PRINTER_INFO_1));
2204                                 (*returned)++;
2205                         }
2206                 }
2207         }
2208                 
2209         /* check the required size. */  
2210         for (i=0; i<*returned; i++)
2211                 (*needed) += spoolss_size_printer_info_1(&(printers[i]));
2212
2213         if (!alloc_buffer_size(buffer, *needed))
2214                 return ERROR_INSUFFICIENT_BUFFER;
2215
2216         /* fill the buffer with the structures */
2217         for (i=0; i<*returned; i++)
2218                 new_smb_io_printer_info_1("", buffer, &(printers[i]), 0);       
2219
2220         /* clear memory */
2221         safe_free(printers);
2222
2223         if (*needed > offered) {
2224                 *returned=0;
2225                 return ERROR_INSUFFICIENT_BUFFER;
2226         }
2227         else
2228                 return NT_STATUS_NO_PROBLEMO;
2229 }
2230
2231 /********************************************************************
2232  enum_all_printers_info_1_local.
2233 *********************************************************************/
2234 static BOOL enum_all_printers_info_1_local(fstring name, NEW_BUFFER *buffer, uint32 offered, uint32 *needed, uint32 *returned)
2235 {
2236         fstring temp;
2237         DEBUG(4,("enum_all_printers_info_1_local\n"));  
2238         
2239         fstrcpy(temp, "\\\\");
2240         fstrcat(temp, global_myname);
2241
2242         if (!strcmp(name, temp)) {
2243                 fstrcat(temp, "\\");
2244                 return enum_all_printers_info_1(temp, PRINTER_ENUM_ICON8, buffer, offered, needed, returned);
2245         }
2246         else
2247                 return enum_all_printers_info_1("", PRINTER_ENUM_ICON8, buffer, offered, needed, returned);
2248 }
2249
2250 /********************************************************************
2251  enum_all_printers_info_1_name.
2252 *********************************************************************/
2253 static BOOL enum_all_printers_info_1_name(fstring name, NEW_BUFFER *buffer, uint32 offered, uint32 *needed, uint32 *returned)
2254 {
2255         fstring temp;
2256         DEBUG(4,("enum_all_printers_info_1_name\n"));   
2257         
2258         fstrcpy(temp, "\\\\");
2259         fstrcat(temp, global_myname);
2260
2261         if (!strcmp(name, temp)) {
2262                 fstrcat(temp, "\\");
2263                 return enum_all_printers_info_1(temp, PRINTER_ENUM_ICON8, buffer, offered, needed, returned);
2264         }
2265         else
2266                 return ERROR_INVALID_NAME;
2267 }
2268
2269 /********************************************************************
2270  enum_all_printers_info_1_remote.
2271 *********************************************************************/
2272 static BOOL enum_all_printers_info_1_remote(fstring name, NEW_BUFFER *buffer, uint32 offered, uint32 *needed, uint32 *returned)
2273 {
2274         PRINTER_INFO_1 *printer;
2275         fstring printername;
2276         fstring desc;
2277         fstring comment;
2278         DEBUG(4,("enum_all_printers_info_1_remote\n")); 
2279
2280         /* JFM: currently it's more a place holder than anything else.
2281          * In the spooler world there is a notion of server registration.
2282          * the print servers are registring (sp ?) on the PDC (in the same domain)
2283          * 
2284          * We should have a TDB here. The registration is done thru an undocumented RPC call.
2285          */
2286         
2287         if((printer=(PRINTER_INFO_1 *)malloc(sizeof(PRINTER_INFO_1))) == NULL)
2288                 return ERROR_NOT_ENOUGH_MEMORY;
2289
2290         *returned=1;
2291         
2292         snprintf(printername, sizeof(printername)-1,"Windows NT Remote Printers!!\\\\%s", global_myname);               
2293         snprintf(desc, sizeof(desc)-1,"%s", global_myname);
2294         snprintf(comment, sizeof(comment)-1, "Logged on Domain");
2295
2296         init_unistr(&printer->description, desc);
2297         init_unistr(&printer->name, printername);       
2298         init_unistr(&printer->comment, comment);
2299         printer->flags=PRINTER_ENUM_ICON3|PRINTER_ENUM_CONTAINER;
2300                 
2301         /* check the required size. */  
2302         *needed += spoolss_size_printer_info_1(printer);
2303
2304         if (!alloc_buffer_size(buffer, *needed)) {
2305                 safe_free(printer);
2306                 return ERROR_INSUFFICIENT_BUFFER;
2307         }
2308
2309         /* fill the buffer with the structures */
2310         new_smb_io_printer_info_1("", buffer, printer, 0);      
2311
2312         /* clear memory */
2313         safe_free(printer);
2314
2315         if (*needed > offered) {
2316                 *returned=0;
2317                 return ERROR_INSUFFICIENT_BUFFER;
2318         }
2319         else
2320                 return NT_STATUS_NO_PROBLEMO;
2321 }
2322
2323 /********************************************************************
2324  enum_all_printers_info_1_network.
2325 *********************************************************************/
2326 static BOOL enum_all_printers_info_1_network(fstring name, NEW_BUFFER *buffer, uint32 offered, uint32 *needed, uint32 *returned)
2327 {
2328         fstring temp;
2329         DEBUG(4,("enum_all_printers_info_1_network\n"));        
2330         
2331         fstrcpy(temp, "\\\\");
2332         fstrcat(temp, global_myname);
2333         fstrcat(temp, "\\");
2334         return enum_all_printers_info_1(temp, PRINTER_ENUM_UNKNOWN_8, buffer, offered, needed, returned);
2335 }
2336
2337 /********************************************************************
2338  * api_spoolss_enumprinters
2339  *
2340  * called from api_spoolss_enumprinters (see this to understand)
2341  ********************************************************************/
2342 static BOOL enum_all_printers_info_2(fstring servername, NEW_BUFFER *buffer, uint32 offered, uint32 *needed, uint32 *returned)
2343 {
2344         int snum;
2345         int i;
2346         int n_services=lp_numservices();
2347         PRINTER_INFO_2 *printers=NULL;
2348         PRINTER_INFO_2 current_prt;
2349
2350         for (snum=0; snum<n_services; snum++) {
2351                 if (lp_browseable(snum) && lp_snum_ok(snum) && lp_print_ok(snum) ) {
2352                         DEBUG(4,("Found a printer in smb.conf: %s[%x]\n", lp_servicename(snum), snum));
2353                                 
2354                         if (construct_printer_info_2(servername, &current_prt, snum)) {
2355                                 if((printers=Realloc(printers, (*returned +1)*sizeof(PRINTER_INFO_2))) == NULL)
2356                                         return ERROR_NOT_ENOUGH_MEMORY;
2357                                 DEBUG(4,("ReAlloced memory for [%d] PRINTER_INFO_2\n", *returned));             
2358                                 memcpy(&printers[*returned], &current_prt, sizeof(PRINTER_INFO_2));
2359                                 (*returned)++;
2360                         }
2361                 }
2362         }
2363         
2364         /* check the required size. */  
2365         for (i=0; i<*returned; i++)
2366                 (*needed) += spoolss_size_printer_info_2(&printers[i]);
2367
2368         if (!alloc_buffer_size(buffer, *needed)) {
2369                 for (i=0; i<*returned; i++) {
2370                         free_devmode(printers[i].devmode);
2371                         free_sec_desc(&printers[i].secdesc);
2372                 }
2373                 safe_free(printers);
2374                 return ERROR_INSUFFICIENT_BUFFER;
2375         }
2376
2377         /* fill the buffer with the structures */
2378         for (i=0; i<*returned; i++)
2379                 new_smb_io_printer_info_2("", buffer, &(printers[i]), 0);       
2380         
2381         /* clear memory */
2382         for (i=0; i<*returned; i++) {
2383                 free_devmode(printers[i].devmode);
2384                 free_sec_desc(&printers[i].secdesc);
2385         }
2386         safe_free(printers);
2387
2388         if (*needed > offered) {
2389                 *returned=0;
2390                 return ERROR_INSUFFICIENT_BUFFER;
2391         }
2392         else
2393                 return NT_STATUS_NO_PROBLEMO;
2394 }
2395
2396 /********************************************************************
2397  * handle enumeration of printers at level 1
2398  ********************************************************************/
2399 static uint32 enumprinters_level1( uint32 flags, fstring name,
2400                                  NEW_BUFFER *buffer, uint32 offered,
2401                                  uint32 *needed, uint32 *returned)
2402 {
2403         /* Not all the flags are equals */
2404
2405         if (flags & PRINTER_ENUM_LOCAL)
2406                 return enum_all_printers_info_1_local(name, buffer, offered, needed, returned);
2407
2408         if (flags & PRINTER_ENUM_NAME)
2409                 return enum_all_printers_info_1_name(name, buffer, offered, needed, returned);
2410
2411         if (flags & PRINTER_ENUM_REMOTE)
2412                 return enum_all_printers_info_1_remote(name, buffer, offered, needed, returned);
2413
2414         if (flags & PRINTER_ENUM_NETWORK)
2415                 return enum_all_printers_info_1_network(name, buffer, offered, needed, returned);
2416
2417         return NT_STATUS_NO_PROBLEMO; /* NT4sp5 does that */
2418 }
2419
2420 /********************************************************************
2421  * handle enumeration of printers at level 2
2422  ********************************************************************/
2423 static uint32 enumprinters_level2( uint32 flags, fstring servername,
2424                                  NEW_BUFFER *buffer, uint32 offered,
2425                                  uint32 *needed, uint32 *returned)
2426 {
2427         fstring temp;
2428         
2429         fstrcpy(temp, "\\\\");
2430         fstrcat(temp, global_myname);
2431
2432         if (flags & PRINTER_ENUM_LOCAL) {
2433                 if (!strcmp(servername, temp))
2434                         return enum_all_printers_info_2(temp, buffer, offered, needed, returned);
2435                 else
2436                         return enum_all_printers_info_2("", buffer, offered, needed, returned);
2437         }
2438
2439         if (flags & PRINTER_ENUM_NAME) {
2440                 if (!strcmp(servername, temp))
2441                         return enum_all_printers_info_2(temp, buffer, offered, needed, returned);
2442                 else
2443                         return ERROR_INVALID_NAME;
2444         }
2445
2446         if (flags & PRINTER_ENUM_REMOTE)
2447                 return ERROR_INVALID_LEVEL;
2448
2449         return NT_STATUS_NO_PROBLEMO;
2450 }
2451
2452 /********************************************************************
2453  * handle enumeration of printers at level 5
2454  ********************************************************************/
2455 static uint32 enumprinters_level5( uint32 flags, fstring servername,
2456                                  NEW_BUFFER *buffer, uint32 offered,
2457                                  uint32 *needed, uint32 *returned)
2458 {
2459 /*      return enum_all_printers_info_5(buffer, offered, needed, returned);*/
2460         return NT_STATUS_NO_PROBLEMO;
2461 }
2462
2463 /********************************************************************
2464  * api_spoolss_enumprinters
2465  *
2466  * called from api_spoolss_enumprinters (see this to understand)
2467  ********************************************************************/
2468 uint32 _spoolss_enumprinters( uint32 flags, const UNISTR2 *servername, uint32 level,
2469                               NEW_BUFFER *buffer, uint32 offered,
2470                               uint32 *needed, uint32 *returned)
2471 {
2472         fstring name;
2473         
2474         DEBUG(4,("_spoolss_enumprinters\n"));
2475
2476         *needed=0;
2477         *returned=0;
2478         
2479         /*
2480          * Level 1: 
2481          *          flags==PRINTER_ENUM_NAME
2482          *           if name=="" then enumerates all printers
2483          *           if name!="" then enumerate the printer
2484          *          flags==PRINTER_ENUM_REMOTE
2485          *          name is NULL, enumerate printers
2486          * Level 2: name!="" enumerates printers, name can't be NULL
2487          * Level 3: doesn't exist
2488          * Level 4: does a local registry lookup
2489          * Level 5: same as Level 2
2490          */
2491
2492         unistr2_to_ascii(name, servername, sizeof(name)-1);
2493         strupper(name);
2494
2495         switch (level) {
2496         case 1:
2497                 return enumprinters_level1(flags, name, buffer, offered, needed, returned);
2498         case 2:
2499                 return enumprinters_level2(flags, name, buffer, offered, needed, returned);
2500         case 5:
2501                 return enumprinters_level5(flags, name, buffer, offered, needed, returned);
2502         case 3:
2503         case 4:
2504         default:
2505                 return ERROR_INVALID_LEVEL;
2506         }
2507 }
2508
2509 /****************************************************************************
2510 ****************************************************************************/
2511 static uint32 getprinter_level_0(fstring servername, int snum, NEW_BUFFER *buffer, uint32 offered, uint32 *needed)
2512 {
2513         PRINTER_INFO_0 *printer=NULL;
2514
2515         if((printer=(PRINTER_INFO_0*)malloc(sizeof(PRINTER_INFO_0))) == NULL)
2516                 return ERROR_NOT_ENOUGH_MEMORY;
2517
2518         construct_printer_info_0(printer, snum, servername);
2519         
2520         /* check the required size. */  
2521         *needed += spoolss_size_printer_info_0(printer);
2522
2523         if (!alloc_buffer_size(buffer, *needed)) {
2524                 safe_free(printer);
2525                 return ERROR_INSUFFICIENT_BUFFER;
2526         }
2527
2528         /* fill the buffer with the structures */
2529         new_smb_io_printer_info_0("", buffer, printer, 0);      
2530         
2531         /* clear memory */
2532         safe_free(printer);
2533
2534         if (*needed > offered) {
2535                 return ERROR_INSUFFICIENT_BUFFER;
2536         }
2537         else
2538                 return NT_STATUS_NO_PROBLEMO;   
2539 }
2540
2541 /****************************************************************************
2542 ****************************************************************************/
2543 static uint32 getprinter_level_1(fstring servername, int snum, NEW_BUFFER *buffer, uint32 offered, uint32 *needed)
2544 {
2545         PRINTER_INFO_1 *printer=NULL;
2546
2547         if((printer=(PRINTER_INFO_1*)malloc(sizeof(PRINTER_INFO_1))) == NULL)
2548                 return ERROR_NOT_ENOUGH_MEMORY;
2549
2550         construct_printer_info_1(servername, PRINTER_ENUM_ICON8, printer, snum);
2551         
2552         /* check the required size. */  
2553         *needed += spoolss_size_printer_info_1(printer);
2554
2555         if (!alloc_buffer_size(buffer, *needed)) {
2556                 safe_free(printer);
2557                 return ERROR_INSUFFICIENT_BUFFER;
2558         }
2559
2560         /* fill the buffer with the structures */
2561         new_smb_io_printer_info_1("", buffer, printer, 0);      
2562         
2563         /* clear memory */
2564         safe_free(printer);
2565
2566         if (*needed > offered) {
2567                 return ERROR_INSUFFICIENT_BUFFER;
2568         }
2569         else
2570                 return NT_STATUS_NO_PROBLEMO;   
2571 }
2572
2573 /****************************************************************************
2574 ****************************************************************************/
2575 static uint32 getprinter_level_2(fstring servername, int snum, NEW_BUFFER *buffer, uint32 offered, uint32 *needed)
2576 {
2577         PRINTER_INFO_2 *printer=NULL;
2578         fstring temp;
2579
2580         if((printer=(PRINTER_INFO_2*)malloc(sizeof(PRINTER_INFO_2)))==NULL)
2581                 return ERROR_NOT_ENOUGH_MEMORY;
2582         
2583         fstrcpy(temp, "\\\\");
2584         fstrcat(temp, servername);
2585         construct_printer_info_2(temp, printer, snum);
2586         
2587         /* check the required size. */  
2588         *needed += spoolss_size_printer_info_2(printer);
2589
2590         if (!alloc_buffer_size(buffer, *needed)) {
2591                 free_printer_info_2(printer);
2592                 return ERROR_INSUFFICIENT_BUFFER;
2593         }
2594
2595         /* fill the buffer with the structures */
2596         if (!new_smb_io_printer_info_2("", buffer, printer, 0)) {
2597                 free_printer_info_2(printer);
2598                 return ERROR_NOT_ENOUGH_MEMORY;
2599         }
2600         
2601         /* clear memory */
2602         free_printer_info_2(printer);
2603
2604         if (*needed > offered) {
2605                 return ERROR_INSUFFICIENT_BUFFER;
2606         }
2607         else
2608                 return NT_STATUS_NO_PROBLEMO;   
2609 }
2610
2611 /****************************************************************************
2612 ****************************************************************************/
2613 static uint32 getprinter_level_3(fstring servername, int snum, NEW_BUFFER *buffer, uint32 offered, uint32 *needed)
2614 {
2615         PRINTER_INFO_3 *printer=NULL;
2616         fstring temp;
2617
2618         fstrcpy(temp, "\\\\");
2619         fstrcat(temp, servername);
2620         if (!construct_printer_info_3(temp, &printer, snum))
2621                 return ERROR_NOT_ENOUGH_MEMORY;
2622         
2623         /* check the required size. */  
2624         *needed += spoolss_size_printer_info_3(printer);
2625
2626         if (!alloc_buffer_size(buffer, *needed)) {
2627                 free_printer_info_3(printer);
2628                 return ERROR_INSUFFICIENT_BUFFER;
2629         }
2630
2631         /* fill the buffer with the structures */
2632         new_smb_io_printer_info_3("", buffer, printer, 0);      
2633         
2634         /* clear memory */
2635         free_printer_info_3(printer);
2636         
2637         if (*needed > offered) {
2638                 return ERROR_INSUFFICIENT_BUFFER;
2639         }
2640         else
2641                 return NT_STATUS_NO_PROBLEMO;   
2642 }
2643
2644 /****************************************************************************
2645 ****************************************************************************/
2646 uint32 _spoolss_getprinter(POLICY_HND *handle, uint32 level,
2647                            NEW_BUFFER *buffer, uint32 offered, uint32 *needed)
2648 {
2649         int snum;
2650         fstring servername;
2651         
2652         *needed=0;
2653
2654         pstrcpy(servername, global_myname);
2655
2656         if (!get_printer_snum(handle, &snum))
2657                 return ERROR_INVALID_HANDLE;
2658
2659         switch (level) {
2660         case 0:
2661                 return getprinter_level_0(servername, snum, buffer, offered, needed);
2662         case 1:
2663                 return getprinter_level_1(servername,snum, buffer, offered, needed);
2664         case 2:         
2665                 return getprinter_level_2(servername,snum, buffer, offered, needed);
2666         case 3:         
2667                 return getprinter_level_3(servername,snum, buffer, offered, needed);
2668         default:
2669                 return ERROR_INVALID_LEVEL;
2670         }
2671 }       
2672                 
2673 /********************************************************************
2674  * fill a DRIVER_INFO_1 struct
2675  ********************************************************************/
2676 static void fill_printer_driver_info_1(DRIVER_INFO_1 *info, NT_PRINTER_DRIVER_INFO_LEVEL driver, fstring servername, fstring architecture)
2677 {
2678         init_unistr( &info->name, driver.info_3->name);
2679 }
2680
2681 /********************************************************************
2682  * construct_printer_driver_info_1
2683  ********************************************************************/
2684 static uint32 construct_printer_driver_info_1(DRIVER_INFO_1 *info, int snum, fstring servername, fstring architecture, uint32 version)
2685 {       
2686         NT_PRINTER_INFO_LEVEL *printer = NULL;
2687         NT_PRINTER_DRIVER_INFO_LEVEL driver;
2688
2689         ZERO_STRUCT(driver);
2690
2691         if (get_a_printer(&printer, 2, lp_servicename(snum)) != 0)
2692                 return ERROR_INVALID_PRINTER_NAME;
2693
2694         if (get_a_printer_driver(&driver, 3, printer->info_2->drivername, architecture, version) != 0)
2695                 return ERROR_UNKNOWN_PRINTER_DRIVER;
2696
2697         fill_printer_driver_info_1(info, driver, servername, architecture);
2698
2699         free_a_printer(&printer,2);
2700
2701         return NT_STATUS_NO_PROBLEMO;
2702 }
2703
2704 /********************************************************************
2705  * construct_printer_driver_info_2
2706  * fill a printer_info_2 struct
2707  ********************************************************************/
2708 static void fill_printer_driver_info_2(DRIVER_INFO_2 *info, NT_PRINTER_DRIVER_INFO_LEVEL driver, fstring servername)
2709 {
2710         pstring temp_driverpath;
2711         pstring temp_datafile;
2712         pstring temp_configfile;
2713
2714         info->version=driver.info_3->cversion;
2715
2716         init_unistr( &info->name, driver.info_3->name );
2717         init_unistr( &info->architecture, driver.info_3->environment );
2718
2719         snprintf(temp_driverpath, sizeof(temp_driverpath)-1, "\\\\%s%s", servername, driver.info_3->driverpath);
2720         init_unistr( &info->driverpath, temp_driverpath );
2721
2722         snprintf(temp_datafile, sizeof(temp_datafile)-1, "\\\\%s%s", servername, driver.info_3->datafile);
2723         init_unistr( &info->datafile, temp_datafile );
2724
2725         snprintf(temp_configfile, sizeof(temp_configfile)-1, "\\\\%s%s", servername, driver.info_3->configfile);
2726         init_unistr( &info->configfile, temp_configfile );      
2727 }
2728
2729 /********************************************************************
2730  * construct_printer_driver_info_2
2731  * fill a printer_info_2 struct
2732  ********************************************************************/
2733 static uint32 construct_printer_driver_info_2(DRIVER_INFO_2 *info, int snum, fstring servername, fstring architecture, uint32 version)
2734 {
2735         NT_PRINTER_INFO_LEVEL *printer = NULL;
2736         NT_PRINTER_DRIVER_INFO_LEVEL driver;
2737
2738         ZERO_STRUCT(printer);
2739         ZERO_STRUCT(driver);
2740
2741         if (!get_a_printer(&printer, 2, lp_servicename(snum)) != 0)
2742                 return ERROR_INVALID_PRINTER_NAME;
2743
2744         if (!get_a_printer_driver(&driver, 3, printer->info_2->drivername, architecture, version) != 0)
2745                 return ERROR_UNKNOWN_PRINTER_DRIVER;
2746
2747         fill_printer_driver_info_2(info, driver, servername);
2748
2749         free_a_printer(&printer,2);
2750
2751         return NT_STATUS_NO_PROBLEMO;
2752 }
2753
2754 /********************************************************************
2755  * copy a strings array and convert to UNICODE
2756  *
2757  * convert an array of ascii string to a UNICODE string
2758  ********************************************************************/
2759 static void init_unistr_array(uint16 **uni_array, fstring *char_array, char *servername)
2760 {
2761         int i=0;
2762         int j=0;
2763         char *v;
2764         pstring line;
2765
2766         DEBUG(6,("init_unistr_array\n"));
2767         *uni_array=NULL;
2768
2769         while (1) {
2770                 if (char_array == NULL)
2771                         v = "";
2772                 else {
2773                         v = char_array[i];
2774                         if (!v) v = ""; /* hack to handle null lists */
2775                 }
2776                 if (strlen(v) == 0) break;
2777                 snprintf(line, sizeof(line)-1, "\\\\%s%s", servername, v);
2778                 DEBUGADD(6,("%d:%s:%d\n", i, line, strlen(line)));
2779                 if((*uni_array=Realloc(*uni_array, (j+strlen(line)+2)*sizeof(uint16))) == NULL) {
2780                         DEBUG(0,("init_unistr_array: Realloc error\n" ));
2781                         return;
2782                 }
2783                 j += (dos_PutUniCode((char *)(*uni_array+j), line , sizeof(uint16)*strlen(line), True) / sizeof(uint16) );
2784                 i++;
2785         }
2786         
2787         if (*uni_array) {
2788                 (*uni_array)[j]=0x0000;
2789         }
2790         
2791         DEBUGADD(6,("last one:done\n"));
2792 }
2793
2794 /********************************************************************
2795  * construct_printer_info_3
2796  * fill a printer_info_3 struct
2797  ********************************************************************/
2798 static void fill_printer_driver_info_3(DRIVER_INFO_3 *info, NT_PRINTER_DRIVER_INFO_LEVEL driver, fstring servername)
2799 {
2800         pstring temp_driverpath;
2801         pstring temp_datafile;
2802         pstring temp_configfile;
2803         pstring temp_helpfile;
2804
2805         ZERO_STRUCTP(info);
2806
2807         info->version=driver.info_3->cversion;
2808
2809         init_unistr( &info->name, driver.info_3->name );        
2810         init_unistr( &info->architecture, driver.info_3->environment );
2811
2812         snprintf(temp_driverpath, sizeof(temp_driverpath)-1, "\\\\%s%s", servername, driver.info_3->driverpath);                 
2813         init_unistr( &info->driverpath, temp_driverpath );
2814
2815         snprintf(temp_datafile, sizeof(temp_datafile)-1, "\\\\%s%s", servername, driver.info_3->datafile); 
2816         init_unistr( &info->datafile, temp_datafile );
2817
2818         snprintf(temp_configfile, sizeof(temp_configfile)-1, "\\\\%s%s", servername, driver.info_3->configfile);
2819         init_unistr( &info->configfile, temp_configfile );      
2820
2821         snprintf(temp_helpfile, sizeof(temp_helpfile)-1, "\\\\%s%s", servername, driver.info_3->helpfile);
2822         init_unistr( &info->helpfile, temp_helpfile );
2823
2824         init_unistr( &info->monitorname, driver.info_3->monitorname );
2825         init_unistr( &info->defaultdatatype, driver.info_3->defaultdatatype );
2826
2827         info->dependentfiles=NULL;
2828         init_unistr_array(&info->dependentfiles, driver.info_3->dependentfiles, servername);
2829 }
2830
2831 /********************************************************************
2832  * construct_printer_info_3
2833  * fill a printer_info_3 struct
2834  ********************************************************************/
2835 static uint32 construct_printer_driver_info_3(DRIVER_INFO_3 *info, int snum, fstring servername, fstring architecture, uint32 version)
2836 {       
2837         NT_PRINTER_INFO_LEVEL *printer = NULL;
2838         NT_PRINTER_DRIVER_INFO_LEVEL driver;
2839         uint32 status=0;
2840         ZERO_STRUCT(driver);
2841
2842         status=get_a_printer(&printer, 2, lp_servicename(snum) );
2843         DEBUG(8,("construct_printer_driver_info_3: status: %d\n", status));
2844         if (status != 0)
2845                 return ERROR_INVALID_PRINTER_NAME;
2846
2847         status=get_a_printer_driver(&driver, 3, printer->info_2->drivername, architecture, version);    
2848         DEBUG(8,("construct_printer_driver_info_3: status: %d\n", status));
2849         if (status != 0) {
2850                 free_a_printer(&printer,2);
2851                 return ERROR_UNKNOWN_PRINTER_DRIVER;
2852         }
2853
2854         fill_printer_driver_info_3(info, driver, servername);
2855
2856         free_a_printer(&printer,2);
2857
2858         return NT_STATUS_NO_PROBLEMO;
2859 }
2860
2861 /********************************************************************
2862  * construct_printer_info_6
2863  * fill a printer_info_6 struct - we know that driver is really level 3. This sucks. JRA.
2864  ********************************************************************/
2865
2866 static void fill_printer_driver_info_6(DRIVER_INFO_6 *info, NT_PRINTER_DRIVER_INFO_LEVEL driver, fstring servername)
2867 {
2868         pstring temp_driverpath;
2869         pstring temp_datafile;
2870         pstring temp_configfile;
2871         pstring temp_helpfile;
2872         fstring nullstr;
2873
2874         ZERO_STRUCTP(info);
2875         memset(&nullstr, '\0', sizeof(fstring));
2876
2877         info->version=driver.info_3->cversion;
2878
2879         init_unistr( &info->name, driver.info_3->name );        
2880         init_unistr( &info->architecture, driver.info_3->environment );
2881
2882         snprintf(temp_driverpath, sizeof(temp_driverpath)-1, "\\\\%s%s", servername, driver.info_3->driverpath);                 
2883         init_unistr( &info->driverpath, temp_driverpath );
2884
2885         snprintf(temp_datafile, sizeof(temp_datafile)-1, "\\\\%s%s", servername, driver.info_3->datafile); 
2886         init_unistr( &info->datafile, temp_datafile );
2887
2888         snprintf(temp_configfile, sizeof(temp_configfile)-1, "\\\\%s%s", servername, driver.info_3->configfile);
2889         init_unistr( &info->configfile, temp_configfile );      
2890
2891         snprintf(temp_helpfile, sizeof(temp_helpfile)-1, "\\\\%s%s", servername, driver.info_3->helpfile);
2892         init_unistr( &info->helpfile, temp_helpfile );
2893
2894         init_unistr( &info->monitorname, driver.info_3->monitorname );
2895         init_unistr( &info->defaultdatatype, driver.info_3->defaultdatatype );
2896
2897         info->dependentfiles=NULL;
2898         init_unistr_array(&info->dependentfiles, driver.info_3->dependentfiles, servername);
2899
2900         info->previousdrivernames=NULL;
2901         init_unistr_array(&info->previousdrivernames, &nullstr, servername);
2902
2903         info->driver_date.low=0;
2904         info->driver_date.high=0;
2905
2906         info->padding=0;
2907         info->driver_version_low=0;
2908         info->driver_version_high=0;
2909
2910         init_unistr( &info->mfgname, "");
2911         init_unistr( &info->oem_url, "");
2912         init_unistr( &info->hardware_id, "");
2913         init_unistr( &info->provider, "");
2914 }
2915
2916 /********************************************************************
2917  * construct_printer_info_6
2918  * fill a printer_info_6 struct
2919  ********************************************************************/
2920 static uint32 construct_printer_driver_info_6(DRIVER_INFO_6 *info, int snum, fstring servername, fstring architecture, uint32 version)
2921 {       
2922         NT_PRINTER_INFO_LEVEL *printer = NULL;
2923         NT_PRINTER_DRIVER_INFO_LEVEL driver;
2924         uint32 status=0;
2925         ZERO_STRUCT(driver);
2926
2927         status=get_a_printer(&printer, 2, lp_servicename(snum) );
2928         DEBUG(8,("construct_printer_driver_info_6: status: %d\n", status));
2929         if (status != 0)
2930                 return ERROR_INVALID_PRINTER_NAME;
2931
2932         status=get_a_printer_driver(&driver, 3, printer->info_2->drivername, architecture, version);    
2933         DEBUG(8,("construct_printer_driver_info_6: status: %d\n", status));
2934         if (status != 0) {
2935                 free_a_printer(&printer,2);
2936                 return ERROR_UNKNOWN_PRINTER_DRIVER;
2937         }
2938
2939         fill_printer_driver_info_6(info, driver, servername);
2940
2941         free_a_printer(&printer,2);
2942
2943         return NT_STATUS_NO_PROBLEMO;
2944 }
2945
2946 /****************************************************************************
2947 ****************************************************************************/
2948
2949 static void free_printer_driver_info_3(DRIVER_INFO_3 *info)
2950 {
2951         safe_free(info->dependentfiles);
2952 }
2953
2954 /****************************************************************************
2955 ****************************************************************************/
2956
2957 static void free_printer_driver_info_6(DRIVER_INFO_6 *info)
2958 {
2959         safe_free(info->dependentfiles);
2960         
2961 }
2962
2963 /****************************************************************************
2964 ****************************************************************************/
2965 static uint32 getprinterdriver2_level1(fstring servername, fstring architecture, uint32 version, int snum, NEW_BUFFER *buffer, uint32 offered, uint32 *needed)
2966 {
2967         DRIVER_INFO_1 *info=NULL;
2968         uint32 status;
2969         
2970         if((info=(DRIVER_INFO_1 *)malloc(sizeof(DRIVER_INFO_1))) == NULL)
2971                 return ERROR_NOT_ENOUGH_MEMORY;
2972         
2973         status=construct_printer_driver_info_1(info, snum, servername, architecture, version);
2974         if (status != NT_STATUS_NO_PROBLEMO) {
2975                 safe_free(info);
2976                 return status;
2977         }
2978
2979         /* check the required size. */  
2980         *needed += spoolss_size_printer_driver_info_1(info);
2981
2982         if (!alloc_buffer_size(buffer, *needed)) {
2983                 safe_free(info);
2984                 return ERROR_INSUFFICIENT_BUFFER;
2985         }
2986
2987         /* fill the buffer with the structures */
2988         new_smb_io_printer_driver_info_1("", buffer, info, 0);  
2989
2990         /* clear memory */
2991         safe_free(info);
2992
2993         if (*needed > offered)
2994                 return ERROR_INSUFFICIENT_BUFFER;
2995         else
2996                 return NT_STATUS_NO_PROBLEMO;
2997 }
2998
2999 /****************************************************************************
3000 ****************************************************************************/
3001 static uint32 getprinterdriver2_level2(fstring servername, fstring architecture, uint32 version, int snum, NEW_BUFFER *buffer, uint32 offered, uint32 *needed)
3002 {
3003         DRIVER_INFO_2 *info=NULL;
3004         uint32 status;
3005         
3006         if((info=(DRIVER_INFO_2 *)malloc(sizeof(DRIVER_INFO_2))) == NULL)
3007                 return ERROR_NOT_ENOUGH_MEMORY;
3008         
3009         status=construct_printer_driver_info_2(info, snum, servername, architecture, version);
3010         if (status != NT_STATUS_NO_PROBLEMO) {
3011                 safe_free(info);
3012                 return status;
3013         }
3014
3015         /* check the required size. */  
3016         *needed += spoolss_size_printer_driver_info_2(info);
3017
3018         if (!alloc_buffer_size(buffer, *needed)) {
3019                 safe_free(info);
3020                 return ERROR_INSUFFICIENT_BUFFER;
3021         }
3022
3023         /* fill the buffer with the structures */
3024         new_smb_io_printer_driver_info_2("", buffer, info, 0);  
3025
3026         /* clear memory */
3027         safe_free(info);
3028
3029         if (*needed > offered)
3030                 return ERROR_INSUFFICIENT_BUFFER;
3031         else
3032                 return NT_STATUS_NO_PROBLEMO;
3033 }
3034
3035 /****************************************************************************
3036 ****************************************************************************/
3037 static uint32 getprinterdriver2_level3(fstring servername, fstring architecture, uint32 version, int snum, NEW_BUFFER *buffer, uint32 offered, uint32 *needed)
3038 {
3039         DRIVER_INFO_3 info;
3040         uint32 status;
3041
3042         ZERO_STRUCT(info);
3043
3044         status=construct_printer_driver_info_3(&info, snum, servername, architecture, version);
3045         if (status != NT_STATUS_NO_PROBLEMO) {
3046                 return status;
3047         }
3048
3049         /* check the required size. */  
3050         *needed += spoolss_size_printer_driver_info_3(&info);
3051
3052         if (!alloc_buffer_size(buffer, *needed)) {
3053                 free_printer_driver_info_3(&info);
3054                 return ERROR_INSUFFICIENT_BUFFER;
3055         }
3056
3057         /* fill the buffer with the structures */
3058         new_smb_io_printer_driver_info_3("", buffer, &info, 0);
3059
3060         free_printer_driver_info_3(&info);
3061
3062         if (*needed > offered)
3063                 return ERROR_INSUFFICIENT_BUFFER;
3064         else
3065                 return NT_STATUS_NO_PROBLEMO;
3066 }
3067
3068 /****************************************************************************
3069 ****************************************************************************/
3070 static uint32 getprinterdriver2_level6(fstring servername, fstring architecture, uint32 version, int snum, NEW_BUFFER *buffer, uint32 offered, uint32 *needed)
3071 {
3072         DRIVER_INFO_6 info;
3073         uint32 status;
3074
3075         ZERO_STRUCT(info);
3076
3077         status=construct_printer_driver_info_6(&info, snum, servername, architecture, version);
3078         if (status != NT_STATUS_NO_PROBLEMO) {
3079                 return status;
3080         }
3081
3082         /* check the required size. */  
3083         *needed += spoolss_size_printer_driver_info_6(&info);
3084
3085         if (!alloc_buffer_size(buffer, *needed)) {
3086                 free_printer_driver_info_6(&info);
3087                 return ERROR_INSUFFICIENT_BUFFER;
3088         }
3089
3090         /* fill the buffer with the structures */
3091         new_smb_io_printer_driver_info_6("", buffer, &info, 0);
3092
3093         free_printer_driver_info_6(&info);
3094
3095         if (*needed > offered)
3096                 return ERROR_INSUFFICIENT_BUFFER;
3097         else
3098                 return NT_STATUS_NO_PROBLEMO;
3099 }
3100
3101 /****************************************************************************
3102 ****************************************************************************/
3103 uint32 _spoolss_getprinterdriver2(POLICY_HND *handle, const UNISTR2 *uni_arch, uint32 level, 
3104                                 uint32 clientmajorversion, uint32 clientminorversion,
3105                                 NEW_BUFFER *buffer, uint32 offered,
3106                                 uint32 *needed, uint32 *servermajorversion, uint32 *serverminorversion)
3107 {
3108         fstring servername;
3109         fstring architecture;
3110         int snum;
3111
3112         DEBUG(4,("_spoolss_getprinterdriver2\n"));
3113
3114         *needed=0;
3115         *servermajorversion=0;
3116         *serverminorversion=0;
3117
3118         pstrcpy(servername, global_myname);
3119         unistr2_to_ascii(architecture, uni_arch, sizeof(architecture)-1);
3120
3121         if (!get_printer_snum(handle, &snum))
3122                 return ERROR_INVALID_HANDLE;
3123
3124         switch (level) {
3125         case 1:
3126                 return getprinterdriver2_level1(servername, architecture, clientmajorversion, snum, buffer, offered, needed);
3127         case 2:
3128                 return getprinterdriver2_level2(servername, architecture, clientmajorversion, snum, buffer, offered, needed);
3129         case 3:
3130                 return getprinterdriver2_level3(servername, architecture, clientmajorversion, snum, buffer, offered, needed);
3131         case 6:
3132                 return getprinterdriver2_level6(servername, architecture, clientmajorversion, snum, buffer, offered, needed);
3133         default:
3134                 return ERROR_INVALID_LEVEL;
3135         }
3136 }
3137
3138 /****************************************************************************
3139 ****************************************************************************/
3140 uint32 _spoolss_startpageprinter(POLICY_HND *handle)
3141 {
3142         Printer_entry *Printer = find_printer_index_by_hnd(handle);
3143
3144         if (OPEN_HANDLE(Printer)) {
3145                 Printer->page_started=True;
3146                 return 0x0;
3147         }
3148
3149         DEBUG(3,("Error in startpageprinter printer handle\n"));
3150         return ERROR_INVALID_HANDLE;
3151 }
3152
3153 /****************************************************************************
3154 ****************************************************************************/
3155 uint32 _spoolss_endpageprinter(POLICY_HND *handle)
3156 {
3157         Printer_entry *Printer = find_printer_index_by_hnd(handle);
3158
3159         if (!OPEN_HANDLE(Printer)) {
3160                 DEBUG(0,("_spoolss_endpageprinter: Invalid handle (%s).\n",OUR_HANDLE(handle)));
3161                 return ERROR_INVALID_HANDLE;
3162         }
3163         
3164         Printer->page_started=False;
3165
3166         return NT_STATUS_NO_PROBLEMO;
3167 }
3168
3169 /****************************************************************************
3170  Return a user struct for a pipe user.
3171 ****************************************************************************/
3172
3173 static struct current_user *get_current_user(struct current_user *user, pipes_struct *p)
3174 {
3175         if (p->ntlmssp_auth_validated) {
3176                 memcpy(user, &p->pipe_user, sizeof(struct current_user));
3177         } else {
3178                 extern struct current_user current_user;
3179                 memcpy(user, &current_user, sizeof(struct current_user));
3180         }
3181
3182         return user;
3183 }
3184
3185 /********************************************************************
3186  * api_spoolss_getprinter
3187  * called from the spoolss dispatcher
3188  *
3189  ********************************************************************/
3190 uint32 _spoolss_startdocprinter(POLICY_HND *handle, uint32 level,
3191                                 pipes_struct *p, DOC_INFO *docinfo, 
3192                                 uint32 *jobid)
3193 {
3194         DOC_INFO_1 *info_1 = &docinfo->doc_info_1;
3195         int snum;
3196         pstring jobname;
3197         fstring datatype;
3198         Printer_entry *Printer = find_printer_index_by_hnd(handle);
3199         struct current_user user;
3200
3201         if (!OPEN_HANDLE(Printer)) {
3202                 DEBUG(0,("_spoolss_startdocprinter: Invalid handle (%s)\n", OUR_HANDLE(handle)));
3203                 return ERROR_INVALID_HANDLE;
3204         }
3205
3206         get_current_user(&user, p);
3207
3208         /*
3209          * a nice thing with NT is it doesn't listen to what you tell it.
3210          * when asked to send _only_ RAW datas, it tries to send datas
3211          * in EMF format.
3212          *
3213          * So I add checks like in NT Server ...
3214          *
3215          * lkclXXXX jean-francois, i love this kind of thing.  oh, well,
3216          * there's a bug in NT client-side code, so we'll fix it in the
3217          * server-side code. *nnnnnggggh!*
3218          */
3219         
3220         if (info_1->p_datatype != 0) {
3221                 unistr2_to_ascii(datatype, &info_1->datatype, sizeof(datatype));
3222                 if (strcmp(datatype, "RAW") != 0) {
3223                         (*jobid)=0;
3224                         return ERROR_INVALID_DATATYPE;
3225                 }               
3226         }                
3227         
3228         /* get the share number of the printer */
3229         if (!get_printer_snum(handle, &snum)) {
3230                 return ERROR_INVALID_HANDLE;
3231         }
3232
3233         unistr2_to_ascii(jobname, &info_1->docname, sizeof(jobname));
3234         
3235         Printer->jobid = print_job_start(&user, snum, jobname);
3236
3237         /* An error occured in print_job_start() so return an appropriate
3238            NT error code. */
3239
3240         if (Printer->jobid == -1) {
3241                 return map_nt_error_from_unix(errno);
3242         }
3243         
3244         Printer->document_started=True;
3245         (*jobid) = Printer->jobid;
3246
3247         srv_spoolss_sendnotify(handle);
3248         return 0x0;
3249 }
3250
3251 /********************************************************************
3252  * api_spoolss_getprinter
3253  * called from the spoolss dispatcher
3254  *
3255  ********************************************************************/
3256 uint32 _spoolss_enddocprinter(POLICY_HND *handle)
3257 {
3258         Printer_entry *Printer=find_printer_index_by_hnd(handle);
3259         
3260         if (!OPEN_HANDLE(Printer)) {
3261                 DEBUG(0,("_spoolss_enddocprinter: Invalid handle (%s)\n", OUR_HANDLE(handle)));
3262                 return ERROR_INVALID_HANDLE;
3263         }
3264         
3265         Printer->document_started=False;
3266         print_job_end(Printer->jobid);
3267         /* error codes unhandled so far ... */
3268
3269         srv_spoolss_sendnotify(handle);
3270
3271         return 0x0;
3272 }
3273
3274 /****************************************************************************
3275 ****************************************************************************/
3276 uint32 _spoolss_writeprinter( POLICY_HND *handle,
3277                                 uint32 buffer_size,
3278                                 uint8 *buffer,
3279                                 uint32 *buffer_written)
3280 {
3281         Printer_entry *Printer = find_printer_index_by_hnd(handle);
3282         
3283         if (!OPEN_HANDLE(Printer)) {
3284                 DEBUG(0,("_spoolss_writeprinter: Invalid handle (%s)\n",OUR_HANDLE(handle)));
3285                 return ERROR_INVALID_HANDLE;
3286         }
3287
3288         (*buffer_written) = print_job_write(Printer->jobid, (char *)buffer, 
3289                                             buffer_size);
3290
3291         return 0x0;
3292 }
3293
3294 /********************************************************************
3295  * api_spoolss_getprinter
3296  * called from the spoolss dispatcher
3297  *
3298  ********************************************************************/
3299 static uint32 control_printer(POLICY_HND *handle, uint32 command,
3300                               pipes_struct *p)
3301 {
3302         struct current_user user;
3303         int snum;
3304         int errcode = 0;
3305         Printer_entry *Printer = find_printer_index_by_hnd(handle);
3306
3307         get_current_user(&user, p);
3308
3309         if (!OPEN_HANDLE(Printer)) {
3310                 DEBUG(0,("control_printer: Invalid handle (%s)\n", OUR_HANDLE(handle)));
3311                 return ERROR_INVALID_HANDLE;
3312         }
3313
3314         if (!get_printer_snum(handle, &snum) )   
3315                 return ERROR_INVALID_HANDLE;
3316
3317         switch (command) {
3318         case PRINTER_CONTROL_PAUSE:
3319                 if (print_queue_pause(&user, snum, &errcode)) {
3320                         srv_spoolss_sendnotify(handle);
3321                         return 0;
3322                 }
3323                 break;
3324         case PRINTER_CONTROL_RESUME:
3325         case PRINTER_CONTROL_UNPAUSE:
3326                 if (print_queue_resume(&user, snum, &errcode)) {
3327                         srv_spoolss_sendnotify(handle);
3328                         return 0;
3329                 }
3330                 break;
3331         case PRINTER_CONTROL_PURGE:
3332                 if (print_queue_purge(&user, snum, &errcode)) {
3333                         srv_spoolss_sendnotify(handle);
3334                         return 0;
3335                 }
3336                 break;
3337         }
3338
3339         if (errcode)
3340                 return (uint32)errcode;
3341
3342         return ERROR_INVALID_FUNCTION;
3343 }
3344
3345 /********************************************************************
3346  * api_spoolss_abortprinter
3347  ********************************************************************/
3348
3349 uint32 _spoolss_abortprinter(POLICY_HND *handle, pipes_struct *p)
3350 {
3351         return control_printer(handle, PRINTER_CONTROL_PURGE, p);
3352 }
3353
3354 /********************************************************************
3355  * called by spoolss_api_setprinter
3356  * when updating a printer description