Merge fest!!!
[sfrench/samba-autobuild/.git] / source / rpc_server / srv_spoolss_nt.c
index b341dbe12ae9554a8374fb1507dc70c5a46fb15a..67b29bdbcf84339805e77c5adf4306705e351d51 100644 (file)
  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
 
-
 #include "includes.h"
 
-#ifndef MANGLE_DRIVER_PATH
-#define MANGLE_DRIVER_PATH 0
-#endif
-
 extern int DEBUGLEVEL;
 extern pstring global_myname;
 
@@ -54,7 +49,7 @@ typedef struct _Printer{
        POLICY_HND printer_hnd;
        BOOL printer_type;
        union {
-               fstring printername;
+               fstring handlename;
                fstring printerservername;
        } dev;
        uint32 type;
@@ -65,6 +60,8 @@ typedef struct _Printer{
                fstring localmachine; 
                uint32 printerlocal;
                SPOOL_NOTIFY_OPTION *option;
+               POLICY_HND client_hnd;
+               uint32 client_connected;
        } notify;
        struct {
                fstring machine;
@@ -83,8 +80,11 @@ typedef struct _counter_printer_0 {
 static ubi_dlList Printer_list;
 static ubi_dlList counter_list;
 
+static struct cli_state cli;
+static uint32 smb_connections=0;
 
-#define OPEN_HANDLE(pnum)    ((pnum!=NULL) && (pnum->open!=False))
+#define OPEN_HANDLE(pnum)    ((pnum!=NULL) && (pnum->open!=False) && (IVAL(pnum->printer_hnd.data,16)==(uint32)sys_getpid()))
+#define OUR_HANDLE(pnum) ((pnum==NULL)?"NULL":(IVAL(pnum->data,16)==sys_getpid()?"OURS":"OTHER"))
 
 /* translate between internal status numbers and NT status numbers */
 static int nt_printj_status(int v)
@@ -104,7 +104,7 @@ static int nt_printq_status(int v)
 {
        switch (v) {
        case LPQ_PAUSED:
-               return PRINTER_STATUS_ERROR;
+               return PRINTER_STATUS_PAUSED;
        case LPQ_QUEUED:
        case LPQ_SPOOLING:
        case LPQ_PRINTING:
@@ -154,8 +154,7 @@ static Printer_entry *find_printer_index_by_hnd(const POLICY_HND *hnd)
 
        for(; find_printer; find_printer = (Printer_entry *)ubi_dlNext(find_printer)) {
 
-               if (memcmp(&(find_printer->printer_hnd), hnd, sizeof(*hnd)) == 0)
-               {
+               if (memcmp(&(find_printer->printer_hnd), hnd, sizeof(*hnd)) == 0) {
                        DEBUG(4,("Found printer handle \n"));
                        /*dump_data(4, hnd->data, sizeof(hnd->data));*/
                        return find_printer;
@@ -175,6 +174,35 @@ static void clear_handle(POLICY_HND *hnd)
        ZERO_STRUCTP(hnd);
 }
 
+/***************************************************************************
+ Disconnect from the client
+****************************************************************************/
+static BOOL srv_spoolss_replycloseprinter(POLICY_HND *handle)
+{
+       uint32 status;
+
+       /* weird if the test succeds !!! */
+       if (smb_connections==0) {
+               DEBUG(0,("srv_spoolss_replycloseprinter:Trying to close non-existant notify backchannel !\n"));
+               return False;
+       }
+
+       if(!cli_spoolss_reply_close_printer(&cli, handle, &status))
+               return False;
+
+       /* if it's the last connection, deconnect the IPC$ share */
+       if (smb_connections==1) {
+               if(!spoolss_disconnect_from_client(&cli))
+                       return False;
+
+               message_deregister(MSG_PRINTER_NOTIFY);
+       }
+
+       smb_connections--;
+
+       return True;
+}
+
 /****************************************************************************
   close printer index by handle
 ****************************************************************************/
@@ -182,12 +210,15 @@ static BOOL close_printer_handle(POLICY_HND *hnd)
 {
        Printer_entry *Printer = find_printer_index_by_hnd(hnd);
 
-       if (!OPEN_HANDLE(Printer))
-       {
-               DEBUG(3,("Error closing printer handle\n"));
+       if (!OPEN_HANDLE(Printer)) {
+               DEBUG(0,("close_printer_handle: Invalid handle (%s)\n", OUR_HANDLE(hnd)));
                return False;
        }
 
+       if (Printer->notify.client_connected==True)
+               if(!srv_spoolss_replycloseprinter(&Printer->notify.client_hnd))
+                       return ERROR_INVALID_HANDLE;
+
        Printer->open=False;
        Printer->notify.flags=0;
        Printer->notify.options=0;
@@ -195,7 +226,8 @@ static BOOL close_printer_handle(POLICY_HND *hnd)
        Printer->notify.printerlocal=0;
        safe_free(Printer->notify.option);
        Printer->notify.option=NULL;
-       
+       Printer->notify.client_connected=False;
+
        clear_handle(hnd);
 
        ubi_dlRemThis(&Printer_list, Printer);
@@ -212,68 +244,85 @@ static BOOL delete_printer_handle(POLICY_HND *hnd)
 {
        Printer_entry *Printer = find_printer_index_by_hnd(hnd);
 
-       if (!OPEN_HANDLE(Printer))
-       {
-               DEBUG(3,("Error closing printer handle\n"));
+       if (!OPEN_HANDLE(Printer)) {
+               DEBUG(0,("delete_printer_handle: Invalid handle (%s)\n", OUR_HANDLE(hnd)));
                return False;
        }
 
-       if (del_a_printer(Printer->dev.printername) != 0) {
-               DEBUG(3,("Error deleting printer %s\n", Printer->dev.printername));
+       if (del_a_printer(Printer->dev.handlename) != 0) {
+               DEBUG(3,("Error deleting printer %s\n", Printer->dev.handlename));
                return False;
        }
 
+       if (*lp_deleteprinter_cmd()) {
+
+               pid_t local_pid = sys_getpid();
+               char *cmd = lp_deleteprinter_cmd();
+               char *path;
+               pstring tmp_file;
+               pstring command;
+               int ret;
+               int i;
+
+               if (*lp_pathname(lp_servicenumber(PRINTERS_NAME)))
+                       path = lp_pathname(lp_servicenumber(PRINTERS_NAME));
+               else
+                       path = tmpdir();
+               
+               /* Printer->dev.handlename equals portname equals sharename */
+               slprintf(command, sizeof(command), "%s \"%s\"", cmd,
+                                       Printer->dev.handlename);
+               slprintf(tmp_file, sizeof(tmp_file), "%s/smbcmd.%d", path, local_pid);
+
+               unlink(tmp_file);
+               DEBUG(10,("Running [%s > %s]\n", command,tmp_file));
+               ret = smbrun(command, tmp_file, False);
+               if (ret != 0) {
+                       unlink(tmp_file);
+                       return False;
+               }
+               DEBUGADD(10,("returned [%d]\n", ret));
+               DEBUGADD(10,("Unlinking output file [%s]\n", tmp_file));
+               unlink(tmp_file);
+
+               /* Send SIGHUP to process group... is there a better way? */
+               kill(0, SIGHUP);
+
+               if ( ( i = lp_servicenumber( Printer->dev.handlename ) ) >= 0 ) {
+                       lp_remove_service( i );
+                       lp_killservice( i );
+                       return True;
+               } else
+                       return False;
+       }
+
        return True;
 }      
 
 /****************************************************************************
   return the snum of a printer corresponding to an handle
 ****************************************************************************/
-static BOOL get_printer_snum(const POLICY_HND *hnd, int *number)
+static BOOL get_printer_snum(POLICY_HND *hnd, int *number)
 {
        Printer_entry *Printer = find_printer_index_by_hnd(hnd);
                
        if (!OPEN_HANDLE(Printer)) {
-               DEBUG(3,("Error getting printer - take a nap quickly !\n"));
+               DEBUG(0,("get_printer_snum: Invalid handle (%s)\n", OUR_HANDLE(hnd)));
                return False;
        }
        
        switch (Printer->printer_type) {
        case PRINTER_HANDLE_IS_PRINTER:            
-               DEBUG(4,("short name:%s\n", Printer->dev.printername));                 
-               *number = print_queue_snum(Printer->dev.printername);
+               DEBUG(4,("short name:%s\n", Printer->dev.handlename));                  
+               *number = print_queue_snum(Printer->dev.handlename);
                return (*number != -1);
        case PRINTER_HANDLE_IS_PRINTSERVER:
                return False;
-               break;
        default:
                return False;
-               break;
        }
 }
 
-/****************************************************************************
-  find first available printer slot. creates a printer handle for you.
- ****************************************************************************/
-static BOOL open_printer_hnd(POLICY_HND *hnd)
-{
-       Printer_entry *new_printer;
-
-       if((new_printer=(Printer_entry *)malloc(sizeof(Printer_entry))) == NULL)
-               return False;
-
-       ZERO_STRUCTP(new_printer);
-       
-       new_printer->open = True;
-       new_printer->notify.option=NULL;
-                               
-       memcpy(&(new_printer->printer_hnd), hnd, sizeof(*hnd));
-       
-       ubi_dlAddHead( &Printer_list, (ubi_dlNode *)new_printer);
-
-       return True;
-}
-
 /****************************************************************************
   set printer handle type.
 ****************************************************************************/
@@ -282,7 +331,7 @@ static BOOL set_printer_hnd_accesstype(POLICY_HND *hnd, uint32 access_required)
        Printer_entry *Printer = find_printer_index_by_hnd(hnd);
 
        if (!OPEN_HANDLE(Printer)) {
-               DEBUG(4,("Error setting printer type=%x", access_required));
+               DEBUG(0,("set_printer_hnd_accesstype: Invalid handle (%s)", OUR_HANDLE(hnd)));
                return False;
        }
 
@@ -292,70 +341,57 @@ static BOOL set_printer_hnd_accesstype(POLICY_HND *hnd, uint32 access_required)
 }
 
 /****************************************************************************
 set printer handle type.
 check if it's \\server or \\server\printer
Set printer handle type.
Check if it's \\server or \\server\printer
 ****************************************************************************/
-static BOOL set_printer_hnd_printertype(POLICY_HND *hnd, char *printername)
+
+static BOOL set_printer_hnd_printertype(Printer_entry *Printer, char *handlename)
 {
-       Printer_entry *Printer = find_printer_index_by_hnd(hnd);
-               
-       if (!OPEN_HANDLE(Printer)) {
-               DEBUGADD(4,("Error setting printer name %s", printername));
-               return False;
-       }
-       
-       DEBUG(3,("Setting printer type=%s\n", printername));
+       DEBUG(3,("Setting printer type=%s\n", handlename));
 
-       if ( strlen(printername) < 3 ) {
-               DEBUGADD(4,("A print server must have at least 1 char ! %s\n", printername));
+       if ( strlen(handlename) < 3 ) {
+               DEBUGADD(4,("A print server must have at least 1 char ! %s\n", handlename));
                return False;
        }
 
        /* it's a print server */
-       if (!strchr(printername+2, '\\')) {
+       if (!strchr(handlename+2, '\\')) {
                DEBUGADD(4,("Printer is a print server\n"));
                Printer->printer_type = PRINTER_HANDLE_IS_PRINTSERVER;          
-               return True;
        }
        /* it's a printer */
        else {
                DEBUGADD(4,("Printer is a printer\n"));
                Printer->printer_type = PRINTER_HANDLE_IS_PRINTER;
-               return True;
        }
 
-       return False;
+       return True;
 }
 
 /****************************************************************************
 set printer handle printername.
Set printer handle name.
 ****************************************************************************/
-static BOOL set_printer_hnd_printername(POLICY_HND *hnd, char *printername)
+
+static BOOL set_printer_hnd_name(Printer_entry *Printer, char *handlename)
 {
-       Printer_entry *Printer = find_printer_index_by_hnd(hnd);
        NT_PRINTER_INFO_LEVEL *printer = NULL;
        int snum;
        int n_services=lp_numservices();
        char *aprinter;
        BOOL found=False;
        
-       if (!OPEN_HANDLE(Printer)) {
-               DEBUG(0,("Error setting printer name=%s\n", printername));
-               return False;
-       }
-
-       DEBUG(4,("Setting printer name=%s (len=%d)\n", printername, strlen(printername)));
+       DEBUG(4,("Setting printer name=%s (len=%d)\n", handlename, strlen(handlename)));
 
        if (Printer->printer_type==PRINTER_HANDLE_IS_PRINTSERVER) {
                ZERO_STRUCT(Printer->dev.printerservername);
-               strncpy(Printer->dev.printerservername, printername, strlen(printername));
+               strncpy(Printer->dev.printerservername, handlename, strlen(handlename));
                return True;
        }
 
        if (Printer->printer_type!=PRINTER_HANDLE_IS_PRINTER)
                return False;
        
-       aprinter=strchr(printername+2, '\\');
+       aprinter=strchr(handlename+2, '\\');
        aprinter++;
 
        DEBUGADD(5,("searching for [%s] (len=%d)\n", aprinter, strlen(aprinter)));
@@ -378,7 +414,7 @@ static BOOL set_printer_hnd_printername(POLICY_HND *hnd, char *printername)
                if (get_a_printer(&printer, 2, lp_servicename(snum))!=0)
                        continue;
 
-               DEBUG(10,("set_printer_hnd_printername: printername [%s], aprinter [%s]\n", 
+               DEBUG(10,("set_printer_hnd_name: name [%s], aprinter [%s]\n", 
                                printer->info_2->printername, aprinter ));
 
                if ( strlen(printer->info_2->printername) != strlen(aprinter) ) {
@@ -395,7 +431,7 @@ static BOOL set_printer_hnd_printername(POLICY_HND *hnd, char *printername)
        }
 
        /* 
-        * if we haven't found a printer with the given printername
+        * if we haven't found a printer with the given handlename
         * then it can be a share name as you can open both \\server\printer and
         * \\server\share
         */
@@ -413,12 +449,12 @@ static BOOL set_printer_hnd_printername(POLICY_HND *hnd, char *printername)
                        if ( !(lp_snum_ok(snum) && lp_print_ok(snum) ) )
                                continue;
                
-                       DEBUGADD(5,("share:%s\n",lp_servicename(snum)));
+                       DEBUGADD(5,("set_printer_hnd_name: share:%s\n",lp_servicename(snum)));
 
                        if (get_a_printer(&printer, 2, lp_servicename(snum))!=0)
                                continue;
 
-                       DEBUG(10,("set_printer_hnd_printername: printername [%s], aprinter [%s]\n", 
+                       DEBUG(10,("set_printer_hnd_name: printername [%s], aprinter [%s]\n", 
                                        printer->info_2->printername, aprinter ));
 
                        if ( strlen(lp_servicename(snum)) != strlen(aprinter) ) {
@@ -441,15 +477,54 @@ static BOOL set_printer_hnd_printername(POLICY_HND *hnd, char *printername)
        }
        
        snum--;
-       DEBUGADD(4,("Printer found: %s -> %s[%x]\n",printer->info_2->printername, lp_servicename(snum),snum));
-       ZERO_STRUCT(Printer->dev.printername);
-       strncpy(Printer->dev.printername, lp_servicename(snum), strlen(lp_servicename(snum)));
+       DEBUGADD(4,("set_printer_hnd_name: Printer found: %s -> %s[%x]\n",
+                       printer->info_2->printername, lp_servicename(snum),snum));
+
+       ZERO_STRUCT(Printer->dev.handlename);
+       strncpy(Printer->dev.handlename, lp_servicename(snum), strlen(lp_servicename(snum)));
        
        free_a_printer(&printer, 2);
 
        return True;
 }
 
+/****************************************************************************
+  find first available printer slot. creates a printer handle for you.
+ ****************************************************************************/
+
+static BOOL open_printer_hnd(POLICY_HND *hnd, char *name)
+{
+       Printer_entry *new_printer;
+
+       DEBUG(10,("open_printer_hnd: name [%s]\n", name));
+       clear_handle(hnd);
+       create_printer_hnd(hnd);
+
+       if((new_printer=(Printer_entry *)malloc(sizeof(Printer_entry))) == NULL)
+               return False;
+
+       ZERO_STRUCTP(new_printer);
+       
+       new_printer->open = True;
+       new_printer->notify.option=NULL;
+                               
+       memcpy(&new_printer->printer_hnd, hnd, sizeof(*hnd));
+       
+       ubi_dlAddHead( &Printer_list, (ubi_dlNode *)new_printer);
+
+       if (!set_printer_hnd_printertype(new_printer, name)) {
+               close_printer_handle(hnd);
+               return False;
+       }
+       
+       if (!set_printer_hnd_name(new_printer, name)) {
+               close_printer_handle(hnd);
+               return False;
+       }
+
+       return True;
+}
+
 /********************************************************************
  Return True is the handle is a print server.
  ********************************************************************/
@@ -500,6 +575,72 @@ static BOOL alloc_buffer_size(NEW_BUFFER *buffer, uint32 buffer_size)
        return True;
 }
 
+/***************************************************************************
+ receive the notify message
+****************************************************************************/
+void srv_spoolss_receive_message(int msg_type, pid_t src, void *buf, size_t len)
+{      
+       fstring printer;
+       uint32 status;
+       Printer_entry *find_printer;
+
+       *printer = '\0';
+       fstrcpy(printer,buf);
+
+       if (len == 0) {
+               DEBUG(0,("srv_spoolss_receive_message: got null message !\n"));
+               return;
+       }
+
+       DEBUG(10,("srv_spoolss_receive_message: Got message about printer %s\n", printer ));
+
+       find_printer = (Printer_entry *)ubi_dlFirst(&Printer_list);
+
+       /* Iterate the printer list. */
+       for(; find_printer; find_printer = (Printer_entry *)ubi_dlNext(find_printer)) {
+
+               /* 
+                * if the entry is the given printer or if it's a printerserver
+                * we send the message
+                */
+
+               if (find_printer->printer_type==PRINTER_HANDLE_IS_PRINTER)
+                       if (strcmp(find_printer->dev.handlename, printer))
+                               continue;
+
+               if (find_printer->notify.client_connected==True)
+                       cli_spoolss_reply_rrpcn(&cli, &find_printer->notify.client_hnd, PRINTER_CHANGE_ALL, 0x0, &status);
+
+       }
+}
+
+/***************************************************************************
+ send a notify event
+****************************************************************************/
+static BOOL srv_spoolss_sendnotify(POLICY_HND *handle)
+{
+       fstring printer;
+
+       Printer_entry *Printer=find_printer_index_by_hnd(handle);
+
+       if (!OPEN_HANDLE(Printer)) {
+               DEBUG(0,("srv_spoolss_sendnotify: Invalid handle (%s).\n", OUR_HANDLE(handle)));
+               return False;
+       }
+
+       if (Printer->printer_type==PRINTER_HANDLE_IS_PRINTER)
+               fstrcpy(printer, Printer->dev.handlename);
+       else
+               fstrcpy(printer, "");
+
+       /*srv_spoolss_receive_message(printer);*/
+       DEBUG(10,("srv_spoolss_sendnotify: Sending message about printer %s\n", printer ));
+
+       message_send_all(MSG_PRINTER_NOTIFY, printer, strlen(printer) + 1); /* Null terminate... */
+
+       return True;
+}      
+
 /********************************************************************
  * spoolss_open_printer
  *
@@ -512,8 +653,6 @@ uint32 _spoolss_open_printer_ex( const UNISTR2 *printername,
 {
        fstring name;
        
-       clear_handle(handle);
-       
        if (printername == NULL)
                return ERROR_INVALID_PRINTER_NAME;
 
@@ -523,20 +662,9 @@ uint32 _spoolss_open_printer_ex( const UNISTR2 *printername,
 
        DEBUGADD(3,("checking name: %s\n",name));
 
-       create_printer_hnd(handle);
-
-       open_printer_hnd(handle);
-       
-       if (!set_printer_hnd_printertype(handle, name)) {
-               close_printer_handle(handle);
+       if (!open_printer_hnd(handle, name))
                return ERROR_INVALID_PRINTER_NAME;
-       }
        
-       if (!set_printer_hnd_printername(handle, name)) {
-               close_printer_handle(handle);
-               return ERROR_INVALID_PRINTER_NAME;
-       }
-
 /*
        if (printer_default->datatype_ptr != NULL)
        {
@@ -552,6 +680,24 @@ uint32 _spoolss_open_printer_ex( const UNISTR2 *printername,
                return ERROR_ACCESS_DENIED;
        }
                
+       /* Disallow MS AddPrinterWizard if parameter disables it. A Win2k
+          client 1st tries an OpenPrinterEx with access==0, MUST be allowed.
+          Then both Win2k and WinNT clients try an OpenPrinterEx with
+          SERVER_ALL_ACCESS, which we force to fail. Then they try
+          OpenPrinterEx with SERVER_READ which we allow. This lets the
+          client view printer folder, but does not show the MSAPW.
+
+          Note: this test needs code to check access rights here too. Jeremy
+          could you look at this? */
+
+       if (handle_is_printserver(handle) &&
+           !lp_ms_add_printer_wizard()) {
+               if (printer_default->access_required == 0)
+                       return NT_STATUS_NO_PROBLEMO;
+               else if (printer_default->access_required != (SERVER_READ))
+               return ERROR_ACCESS_DENIED;
+       }
+
        return NT_STATUS_NO_PROBLEMO;
 }
 
@@ -577,11 +723,11 @@ static BOOL convert_printer_driver_info(const SPOOL_PRINTER_DRIVER_INFO_LEVEL *u
        switch (level) {
                case 3: 
                        printer->info_3=NULL;
-                       uni_2_asc_printer_driver_3(uni->info_3, &(printer->info_3));
+                       uni_2_asc_printer_driver_3(uni->info_3, &printer->info_3);
                        break;
                case 6: 
                        printer->info_6=NULL;
-                       uni_2_asc_printer_driver_6(uni->info_6, &(printer->info_6));
+                       uni_2_asc_printer_driver_6(uni->info_6, &printer->info_6);
                        break;
                default:
                        break;
@@ -645,6 +791,11 @@ static BOOL convert_devicemode(const DEVICEMODE *devmode, NT_DEVICEMODE *nt_devm
  ********************************************************************/
 uint32 _spoolss_closeprinter(POLICY_HND *handle)
 {
+       Printer_entry *Printer=find_printer_index_by_hnd(handle);
+
+       if (Printer && Printer->document_started)
+               _spoolss_enddocprinter(handle);          /* print job was not closed */
+
        if (!close_printer_handle(handle))
                return ERROR_INVALID_HANDLE;    
                
@@ -656,8 +807,15 @@ uint32 _spoolss_closeprinter(POLICY_HND *handle)
  ********************************************************************/
 uint32 _spoolss_deleteprinter(POLICY_HND *handle)
 {
+       Printer_entry *Printer=find_printer_index_by_hnd(handle);
+
+       if (Printer && Printer->document_started)
+               _spoolss_enddocprinter(handle);          /* print job was not closed */
+
        if (!delete_printer_handle(handle))
                return ERROR_INVALID_HANDLE;    
+
+       srv_spoolss_sendnotify(handle);
                
        return NT_STATUS_NO_PROBLEMO;
 }
@@ -743,7 +901,7 @@ static BOOL getprinterdata_printer_server(fstring value, uint32 *type, uint8 **d
 /********************************************************************
  GetPrinterData on a printer Handle.
 ********************************************************************/
-static BOOL getprinterdata_printer(const POLICY_HND *handle,
+static BOOL getprinterdata_printer(POLICY_HND *handle,
                                fstring value, uint32 *type, 
                                uint8 **data, uint32 *needed, uint32 in_size )
 {
@@ -755,8 +913,10 @@ static BOOL getprinterdata_printer(const POLICY_HND *handle,
        
        DEBUG(5,("getprinterdata_printer\n"));
 
-       if (!OPEN_HANDLE(Printer))
+       if (!OPEN_HANDLE(Printer)) {
+               DEBUG(0,("getprinterdata_printer: Invalid handle (%s).\n", OUR_HANDLE(handle)));
                return False;
+       }
 
        if(!get_printer_snum(handle, &snum))
                return False;
@@ -797,7 +957,7 @@ static BOOL getprinterdata_printer(const POLICY_HND *handle,
 /********************************************************************
  * spoolss_getprinterdata
  ********************************************************************/
-uint32 _spoolss_getprinterdata(const POLICY_HND *handle, UNISTR2 *valuename,
+uint32 _spoolss_getprinterdata(POLICY_HND *handle, UNISTR2 *valuename,
                                uint32 in_size,
                                uint32 *type,
                                uint32 *out_size,
@@ -826,6 +986,7 @@ uint32 _spoolss_getprinterdata(const POLICY_HND *handle, UNISTR2 *valuename,
        if (!OPEN_HANDLE(Printer)) {
                if((*data=(uint8 *)malloc(4*sizeof(uint8))) == NULL)
                        return ERROR_NOT_ENOUGH_MEMORY;
+               DEBUG(0,("_spoolss_getprinterdata: Invalid handle (%s).\n", OUR_HANDLE(handle)));
                return ERROR_INVALID_HANDLE;
        }
        
@@ -851,11 +1012,37 @@ uint32 _spoolss_getprinterdata(const POLICY_HND *handle, UNISTR2 *valuename,
        }
        
        if (*needed > *out_size)
-               return ERROR_INSUFFICIENT_BUFFER;
+               return ERROR_MORE_DATA;
        else
                return NT_STATUS_NO_PROBLEMO;
 }
 
+/***************************************************************************
+ connect to the client
+****************************************************************************/
+static BOOL srv_spoolss_replyopenprinter(char *printer, uint32 localprinter, uint32 type, POLICY_HND *handle)
+{
+       uint32 status;
+
+       /*
+        * If it's the first connection, contact the client 
+        * and connect to the IPC$ share anonumously
+        */
+       if (smb_connections==0) {
+               if(!spoolss_connect_to_client(&cli, printer+2)) /* the +2 is to strip the leading 2 backslashs */
+                       return False;
+               message_register(MSG_PRINTER_NOTIFY, srv_spoolss_receive_message);
+
+       }
+
+       smb_connections++;
+
+       if(!cli_spoolss_reply_open_printer(&cli, printer, localprinter, type, &status, handle))
+               return False;
+
+       return True;
+}
+
 /********************************************************************
  * _spoolss_rffpcnex
  * ReplyFindFirstPrinterChangeNotifyEx
@@ -867,7 +1054,7 @@ uint32 _spoolss_getprinterdata(const POLICY_HND *handle, UNISTR2 *valuename,
  * in fact ReplyOpenPrinter is the changenotify equivalent on the spoolss pipe
  * called from api_spoolss_rffpcnex 
  ********************************************************************/
-uint32 _spoolss_rffpcnex(const POLICY_HND *handle, uint32 flags, uint32 options,
+uint32 _spoolss_rffpcnex(POLICY_HND *handle, uint32 flags, uint32 options,
                         const UNISTR2 *localmachine, uint32 printerlocal,
                         SPOOL_NOTIFY_OPTION *option)
 {
@@ -875,8 +1062,10 @@ uint32 _spoolss_rffpcnex(const POLICY_HND *handle, uint32 flags, uint32 options,
 
        Printer_entry *Printer=find_printer_index_by_hnd(handle);
 
-       if (!OPEN_HANDLE(Printer))
+       if (!OPEN_HANDLE(Printer)) {
+               DEBUG(0,("_spoolss_rffpcnex: Invalid handle (%s).\n", OUR_HANDLE(handle)));
                return ERROR_INVALID_HANDLE;
+       }
 
        Printer->notify.flags=flags;
        Printer->notify.options=options;
@@ -884,6 +1073,12 @@ uint32 _spoolss_rffpcnex(const POLICY_HND *handle, uint32 flags, uint32 options,
        Printer->notify.option=option;
        unistr2_to_ascii(Printer->notify.localmachine, localmachine, sizeof(Printer->notify.localmachine)-1);
 
+       /* connect to the client machine and send a ReplyOpenPrinter */
+       if(srv_spoolss_replyopenprinter(Printer->notify.localmachine, 
+                                       Printer->notify.printerlocal, 1, 
+                                       &Printer->notify.client_hnd))
+               Printer->notify.client_connected=True;
+
        return NT_STATUS_NO_PROBLEMO;
 }
 
@@ -897,8 +1092,8 @@ static void spoolss_notify_server_name(int snum, SPOOL_NOTIFY_INFO_DATA *data, p
 
        snprintf(temp_name, sizeof(temp_name)-1, "\\\\%s", global_myname);
 
-       data->notify_data.data.length=strlen(temp_name);
-       ascii_to_unistr((char *)data->notify_data.data.string, temp_name, sizeof(data->notify_data.data.string)-1);
+       data->notify_data.data.length= (uint32)((dos_PutUniCode((char *)data->notify_data.data.string,
+                                       temp_name, sizeof(data->notify_data.data.string), True) - sizeof(uint16))/sizeof(uint16));
 }
 
 /*******************************************************************
@@ -910,12 +1105,10 @@ static void spoolss_notify_printer_name(int snum, SPOOL_NOTIFY_INFO_DATA *data,
 {
 /*
        data->notify_data.data.length=strlen(lp_servicename(snum));
-       ascii_to_unistr(data->notify_data.data.string, lp_servicename(snum), sizeof(data->notify_data.data.string)-1);
+       dos_PutUniCode(data->notify_data.data.string, lp_servicename(snum), sizeof(data->notify_data.data.string), True);
 */
-       data->notify_data.data.length=strlen(printer->info_2->printername);
-       ascii_to_unistr((char *)data->notify_data.data.string, 
-                       printer->info_2->printername, 
-                       sizeof(data->notify_data.data.string)-1);
+       data->notify_data.data.length=(uint32)((dos_PutUniCode((char *)data->notify_data.data.string,
+                               printer->info_2->printername, sizeof(data->notify_data.data.string), True) - sizeof(uint16))/sizeof(uint16));
 }
 
 /*******************************************************************
@@ -923,10 +1116,8 @@ static void spoolss_notify_printer_name(int snum, SPOOL_NOTIFY_INFO_DATA *data,
  ********************************************************************/
 static void spoolss_notify_share_name(int snum, SPOOL_NOTIFY_INFO_DATA *data, print_queue_struct *queue, NT_PRINTER_INFO_LEVEL *printer)
 {
-       data->notify_data.data.length=strlen(lp_servicename(snum));
-       ascii_to_unistr((char *)data->notify_data.data.string,
-                       lp_servicename(snum), 
-                       sizeof(data->notify_data.data.string)-1);
+       data->notify_data.data.length=(uint32)((dos_PutUniCode((char *)data->notify_data.data.string,
+                       lp_servicename(snum), sizeof(data->notify_data.data.string),True) - sizeof(uint16))/sizeof(uint16));
 }
 
 /*******************************************************************
@@ -936,10 +1127,8 @@ static void spoolss_notify_port_name(int snum, SPOOL_NOTIFY_INFO_DATA *data, pri
 {
        /* even if it's strange, that's consistant in all the code */
 
-       data->notify_data.data.length=strlen(lp_servicename(snum));
-       ascii_to_unistr((char *)data->notify_data.data.string,
-                       lp_servicename(snum), 
-                       sizeof(data->notify_data.data.string)-1);
+       data->notify_data.data.length=(uint32)((dos_PutUniCode((char *)data->notify_data.data.string,
+               lp_servicename(snum), sizeof(data->notify_data.data.string), True)  - sizeof(uint16))/sizeof(uint16));
 }
 
 /*******************************************************************
@@ -949,10 +1138,8 @@ static void spoolss_notify_port_name(int snum, SPOOL_NOTIFY_INFO_DATA *data, pri
  ********************************************************************/
 static void spoolss_notify_driver_name(int snum, SPOOL_NOTIFY_INFO_DATA *data, print_queue_struct *queue, NT_PRINTER_INFO_LEVEL *printer)
 {
-       data->notify_data.data.length=strlen(printer->info_2->drivername);
-       ascii_to_unistr((char *)data->notify_data.data.string, 
-                       printer->info_2->drivername, 
-                       sizeof(data->notify_data.data.string)-1);
+       data->notify_data.data.length=(uint32)((dos_PutUniCode((char *)data->notify_data.data.string,
+                       printer->info_2->drivername, sizeof(data->notify_data.data.string)-1, True)  - sizeof(uint16))/sizeof(uint16));
 }
 
 /*******************************************************************
@@ -960,10 +1147,12 @@ static void spoolss_notify_driver_name(int snum, SPOOL_NOTIFY_INFO_DATA *data, p
  ********************************************************************/
 static void spoolss_notify_comment(int snum, SPOOL_NOTIFY_INFO_DATA *data, print_queue_struct *queue, NT_PRINTER_INFO_LEVEL *printer)
 {
-       data->notify_data.data.length=strlen(lp_comment(snum));
-       ascii_to_unistr((char *)data->notify_data.data.string,
-                       lp_comment(snum),
-                       sizeof(data->notify_data.data.string)-1);
+       if (*printer->info_2->comment == '\0')
+               data->notify_data.data.length=(uint32)((dos_PutUniCode((char *)data->notify_data.data.string,
+                       lp_comment(snum), sizeof(data->notify_data.data.string)-1, True)  - sizeof(uint16))/sizeof(uint16));
+       else
+               data->notify_data.data.length=(uint32)((dos_PutUniCode((char *)data->notify_data.data.string,
+                       printer->info_2->comment, sizeof(data->notify_data.data.string)-1, True)  - sizeof(uint16))/sizeof(uint16));
 }
 
 /*******************************************************************
@@ -973,10 +1162,8 @@ static void spoolss_notify_comment(int snum, SPOOL_NOTIFY_INFO_DATA *data, print
  ********************************************************************/
 static void spoolss_notify_location(int snum, SPOOL_NOTIFY_INFO_DATA *data, print_queue_struct *queue, NT_PRINTER_INFO_LEVEL *printer)
 {
-       data->notify_data.data.length=strlen(printer->info_2->location);
-       ascii_to_unistr((char *)data->notify_data.data.string, 
-                       printer->info_2->location, 
-                       sizeof(data->notify_data.data.string)-1);
+       data->notify_data.data.length=(uint32)((dos_PutUniCode((char *)data->notify_data.data.string,
+                       printer->info_2->location, sizeof(data->notify_data.data.string)-1, True)  - sizeof(uint16))/sizeof(uint16));
 }
 
 /*******************************************************************
@@ -994,10 +1181,8 @@ static void spoolss_notify_devmode(int snum, SPOOL_NOTIFY_INFO_DATA *data, print
  ********************************************************************/
 static void spoolss_notify_sepfile(int snum, SPOOL_NOTIFY_INFO_DATA *data, print_queue_struct *queue, NT_PRINTER_INFO_LEVEL *printer)
 {
-       data->notify_data.data.length=strlen(printer->info_2->sepfile);
-       ascii_to_unistr((char *)data->notify_data.data.string, 
-                       printer->info_2->sepfile, 
-                       sizeof(data->notify_data.data.string)-1);
+       data->notify_data.data.length=(uint32)((dos_PutUniCode((char *)data->notify_data.data.string,
+                       printer->info_2->sepfile, sizeof(data->notify_data.data.string)-1,True)  - sizeof(uint16))/sizeof(uint16));
 }
 
 /*******************************************************************
@@ -1006,10 +1191,8 @@ static void spoolss_notify_sepfile(int snum, SPOOL_NOTIFY_INFO_DATA *data, print
  ********************************************************************/
 static void spoolss_notify_print_processor(int snum, SPOOL_NOTIFY_INFO_DATA *data, print_queue_struct *queue, NT_PRINTER_INFO_LEVEL *printer)
 {
-       data->notify_data.data.length=strlen(printer->info_2->printprocessor);
-       ascii_to_unistr((char *)data->notify_data.data.string, 
-                       printer->info_2->printprocessor, 
-                       sizeof(data->notify_data.data.string)-1);
+       data->notify_data.data.length=(uint32)((dos_PutUniCode((char *)data->notify_data.data.string,
+                       printer->info_2->printprocessor, sizeof(data->notify_data.data.string)-1, True)  - sizeof(uint16))/sizeof(uint16));
 }
 
 /*******************************************************************
@@ -1018,10 +1201,8 @@ static void spoolss_notify_print_processor(int snum, SPOOL_NOTIFY_INFO_DATA *dat
  ********************************************************************/
 static void spoolss_notify_parameters(int snum, SPOOL_NOTIFY_INFO_DATA *data, print_queue_struct *queue, NT_PRINTER_INFO_LEVEL *printer)
 {
-       data->notify_data.data.length=strlen(printer->info_2->parameters);
-       ascii_to_unistr((char *)data->notify_data.data.string, 
-                       printer->info_2->parameters, 
-                       sizeof(data->notify_data.data.string)-1);
+       data->notify_data.data.length=(uint32)((dos_PutUniCode((char *)data->notify_data.data.string,
+                       printer->info_2->parameters, sizeof(data->notify_data.data.string)-1, True)  - sizeof(uint16))/sizeof(uint16));
 }
 
 /*******************************************************************
@@ -1030,10 +1211,8 @@ static void spoolss_notify_parameters(int snum, SPOOL_NOTIFY_INFO_DATA *data, pr
  ********************************************************************/
 static void spoolss_notify_datatype(int snum, SPOOL_NOTIFY_INFO_DATA *data, print_queue_struct *queue, NT_PRINTER_INFO_LEVEL *printer)
 {
-       data->notify_data.data.length=strlen(printer->info_2->datatype);
-       ascii_to_unistr((char *)data->notify_data.data.string, 
-                       printer->info_2->datatype, 
-                       sizeof(data->notify_data.data.string)-1);
+       data->notify_data.data.length=(uint32)((dos_PutUniCode((char *)data->notify_data.data.string,
+                       printer->info_2->datatype, sizeof(data->notify_data.data.string)-1, True)  - sizeof(uint16))/sizeof(uint16));
 }
 
 /*******************************************************************
@@ -1054,7 +1233,7 @@ static void spoolss_notify_security_desc(int snum, SPOOL_NOTIFY_INFO_DATA *data,
 static void spoolss_notify_attributes(int snum, SPOOL_NOTIFY_INFO_DATA *data, print_queue_struct *queue, NT_PRINTER_INFO_LEVEL *printer)
 {
        data->notify_data.value[0] =   PRINTER_ATTRIBUTE_SHARED   \
-                                    | PRINTER_ATTRIBUTE_NETWORK  \
+                                    | PRINTER_ATTRIBUTE_LOCAL  \
                                     | PRINTER_ATTRIBUTE_RAW_ONLY ;
 }
 
@@ -1129,16 +1308,16 @@ static void spoolss_notify_average_ppm(int snum, SPOOL_NOTIFY_INFO_DATA *data, p
 }
 
 /*******************************************************************
- * fill a notify_info_data with 
+ * fill a notify_info_data with username
  ********************************************************************/
 static void spoolss_notify_username(int snum, SPOOL_NOTIFY_INFO_DATA *data, print_queue_struct *queue, NT_PRINTER_INFO_LEVEL *printer)
 {
-       data->notify_data.data.length=strlen(queue->user);
-       ascii_to_unistr((char *)data->notify_data.data.string, queue->user, sizeof(data->notify_data.data.string)-1);
+       data->notify_data.data.length=(uint32)((dos_PutUniCode((char *)data->notify_data.data.string,
+                       queue->user, sizeof(data->notify_data.data.string)-1, True)  - sizeof(uint16))/sizeof(uint16));
 }
 
 /*******************************************************************
- * fill a notify_info_data with 
+ * fill a notify_info_data with job status
  ********************************************************************/
 static void spoolss_notify_job_status(int snum, SPOOL_NOTIFY_INFO_DATA *data, print_queue_struct *queue, NT_PRINTER_INFO_LEVEL *printer)
 {
@@ -1146,40 +1325,41 @@ static void spoolss_notify_job_status(int snum, SPOOL_NOTIFY_INFO_DATA *data, pr
 }
 
 /*******************************************************************
- * fill a notify_info_data with 
+ * fill a notify_info_data with job name
  ********************************************************************/
 static void spoolss_notify_job_name(int snum, SPOOL_NOTIFY_INFO_DATA *data, print_queue_struct *queue, NT_PRINTER_INFO_LEVEL *printer)
 {
-       data->notify_data.data.length=strlen(queue->file);
-       ascii_to_unistr((char *)data->notify_data.data.string, queue->file, sizeof(data->notify_data.data.string)-1);
+       data->notify_data.data.length=(uint32)((dos_PutUniCode((char *)data->notify_data.data.string,
+                       queue->file, sizeof(data->notify_data.data.string)-1, True)  - sizeof(uint16))/sizeof(uint16));
 }
 
 /*******************************************************************
- * fill a notify_info_data with 
+ * fill a notify_info_data with job status
  ********************************************************************/
 static void spoolss_notify_job_status_string(int snum, SPOOL_NOTIFY_INFO_DATA *data, print_queue_struct *queue, NT_PRINTER_INFO_LEVEL *printer)
 {
        char *p = "unknown";
+
        switch (queue->status) {
        case LPQ_QUEUED:
-               p = "QUEUED";
+               p = "Queued";
                break;
        case LPQ_PAUSED:
-               p = "PAUSED";
+               p = "";    /* NT provides the paused string */
                break;
        case LPQ_SPOOLING:
-               p = "SPOOLING";
+               p = "Spooling";
                break;
        case LPQ_PRINTING:
-               p = "PRINTING";
+               p = "Printing";
                break;
        }
-       data->notify_data.data.length=strlen(p);
-       ascii_to_unistr((char *)data->notify_data.data.string, p, sizeof(data->notify_data.data.string)-1);
+       data->notify_data.data.length=(uint32)((dos_PutUniCode((char *)data->notify_data.data.string,
+                               p, sizeof(data->notify_data.data.string)-1, True)  - sizeof(uint16))/sizeof(uint16));
 }
 
 /*******************************************************************
- * fill a notify_info_data with 
+ * fill a notify_info_data with job time
  ********************************************************************/
 static void spoolss_notify_job_time(int snum, SPOOL_NOTIFY_INFO_DATA *data, print_queue_struct *queue, NT_PRINTER_INFO_LEVEL *printer)
 {
@@ -1187,7 +1367,7 @@ static void spoolss_notify_job_time(int snum, SPOOL_NOTIFY_INFO_DATA *data, prin
 }
 
 /*******************************************************************
- * fill a notify_info_data with 
+ * fill a notify_info_data with job size
  ********************************************************************/
 static void spoolss_notify_job_size(int snum, SPOOL_NOTIFY_INFO_DATA *data, print_queue_struct *queue, NT_PRINTER_INFO_LEVEL *printer)
 {
@@ -1195,13 +1375,26 @@ static void spoolss_notify_job_size(int snum, SPOOL_NOTIFY_INFO_DATA *data, prin
 }
 
 /*******************************************************************
- * fill a notify_info_data with 
+ * fill a notify_info_data with job position
  ********************************************************************/
 static void spoolss_notify_job_position(int snum, SPOOL_NOTIFY_INFO_DATA *data, print_queue_struct *queue, NT_PRINTER_INFO_LEVEL *printer)
 {
        data->notify_data.value[0]=queue->job;
 }
 
+/*******************************************************************
+ * fill a notify_info_data with submitted time
+ ********************************************************************/
+static void spoolss_notify_submitted_time(int snum, SPOOL_NOTIFY_INFO_DATA *data, print_queue_struct *queue, NT_PRINTER_INFO_LEVEL *printer)
+{
+       struct tm *t;
+
+       t=gmtime(&queue->time);
+
+       data->notify_data.data.length = sizeof(SYSTEMTIME);
+       make_systemtime((SYSTEMTIME*)(data->notify_data.data.string), t);
+}
+
 #define END 65535
 
 struct s_notify_info_data_table
@@ -1259,7 +1452,7 @@ struct s_notify_info_data_table notify_info_data_table[] =
 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_DOCUMENT,                "JOB_NOTIFY_DOCUMENT",                POINTER,   spoolss_notify_job_name },
 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_PRIORITY,                "JOB_NOTIFY_PRIORITY",                ONE_VALUE, spoolss_notify_priority },
 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_POSITION,                "JOB_NOTIFY_POSITION",                ONE_VALUE, spoolss_notify_job_position },
-{ JOB_NOTIFY_TYPE,     JOB_NOTIFY_SUBMITTED,               "JOB_NOTIFY_SUBMITTED",               POINTER,   NULL },
+{ JOB_NOTIFY_TYPE,     JOB_NOTIFY_SUBMITTED,               "JOB_NOTIFY_SUBMITTED",               POINTER,   spoolss_notify_submitted_time },
 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_START_TIME,              "JOB_NOTIFY_START_TIME",              ONE_VALUE, spoolss_notify_start_time },
 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_UNTIL_TIME,              "JOB_NOTIFY_UNTIL_TIME",              ONE_VALUE, spoolss_notify_until_time },
 { JOB_NOTIFY_TYPE,     JOB_NOTIFY_TIME,                    "JOB_NOTIFY_TIME",                    ONE_VALUE, spoolss_notify_job_time },
@@ -1283,7 +1476,6 @@ static uint32 size_of_notify_info_data(uint16 type, uint16 field)
                     (notify_info_data_table[i].field == field ) )
                {
                        return (notify_info_data_table[i].size);
-                       continue;
                }
                i++;
        }
@@ -1310,7 +1502,6 @@ static BOOL type_of_notify_info_data(uint16 type, uint16 field)
                        {
                                return (True);
                        }
-                       continue;
                }
                i++;
        }
@@ -1377,8 +1568,7 @@ static BOOL construct_notify_printer_info(SPOOL_NOTIFY_INFO *info, int snum, SPO
        if (get_a_printer(&printer, 2, lp_servicename(snum))!=0)
                return False;
 
-       for(field_num=0; field_num<option_type->count; field_num++)
-       {
+       for(field_num=0; field_num<option_type->count; field_num++) {
                field = option_type->fields[field_num];
                DEBUGADD(4,("notify [%d]: type [%x], field [%x]\n", field_num, type, field));
 
@@ -1388,7 +1578,7 @@ static BOOL construct_notify_printer_info(SPOOL_NOTIFY_INFO *info, int snum, SPO
                if((info->data=Realloc(info->data, (info->count+1)*sizeof(SPOOL_NOTIFY_INFO_DATA))) == NULL) {
                        return False;
                }
-               current_data=&(info->data[info->count]);
+               current_data=&info->data[info->count];
 
                construct_info_data(current_data, type, field, id);             
                notify_info_data_table[j].fn(snum, current_data, queue, printer);
@@ -1495,8 +1685,7 @@ static uint32 printserver_notify_info(const POLICY_HND *hnd, SPOOL_NOTIFY_INFO *
        info->data=NULL;
        info->count=0;
 
-       for (i=0; i<option->count; i++)
-       {
+       for (i=0; i<option->count; i++) {
                option_type=&(option->ctr.type[i]);
                
                if (option_type->type!=PRINTER_NOTIFY_TYPE)
@@ -1516,8 +1705,7 @@ static uint32 printserver_notify_info(const POLICY_HND *hnd, SPOOL_NOTIFY_INFO *
        DEBUGADD(1,("info->version:[%d], info->flags:[%d], info->count:[%d]\n", info->version, info->flags, info->count));
        DEBUGADD(1,("num\ttype\tfield\tres\tid\tsize\tenc_type\n"));
        
-       for (i=0; i<info->count; i++)
-       {
+       for (i=0; i<info->count; i++) {
                DEBUGADD(1,("[%d]\t[%d]\t[%d]\t[%d]\t[%d]\t[%d]\t[%d]\n",
                i, info->data[i].type, info->data[i].field, info->data[i].reserved,
                info->data[i].id, info->data[i].size, info->data[i].enc_type));
@@ -1532,7 +1720,7 @@ static uint32 printserver_notify_info(const POLICY_HND *hnd, SPOOL_NOTIFY_INFO *
  * fill a notify_info struct with info asked
  * 
  ********************************************************************/
-static uint32 printer_notify_info(const POLICY_HND *hnd, SPOOL_NOTIFY_INFO *info)
+static uint32 printer_notify_info(POLICY_HND *hnd, SPOOL_NOTIFY_INFO *info)
 {
        int snum;
        Printer_entry *Printer=find_printer_index_by_hnd(hnd);
@@ -1554,9 +1742,8 @@ static uint32 printer_notify_info(const POLICY_HND *hnd, SPOOL_NOTIFY_INFO *info
 
        get_printer_snum(hnd, &snum);
 
-       for (i=0; i<option->count; i++)
-       {
-               option_type=&(option->ctr.type[i]);
+       for (i=0; i<option->count; i++) {
+               option_type=&option->ctr.type[i];
                
                switch ( option_type->type ) {
                case PRINTER_NOTIFY_TYPE:
@@ -1568,7 +1755,7 @@ static uint32 printer_notify_info(const POLICY_HND *hnd, SPOOL_NOTIFY_INFO *info
                        memset(&status, 0, sizeof(status));     
                        count = print_queue_status(snum, &queue, &status);
                        for (j=0; j<count; j++)
-                               construct_notify_jobs_info(&(queue[j]), info, snum, option_type, queue[j].job);
+                               construct_notify_jobs_info(&queue[j], info, snum, option_type, queue[j].job);
                        safe_free(queue);
                        break;
                }
@@ -1582,8 +1769,7 @@ static uint32 printer_notify_info(const POLICY_HND *hnd, SPOOL_NOTIFY_INFO *info
        DEBUGADD(1,("info->version:[%d], info->flags:[%d], info->count:[%d]\n", info->version, info->flags, info->count));
        DEBUGADD(1,("num\ttype\tfield\tres\tid\tsize\tenc_type\n"));
        
-       for (i=0; i<info->count; i++)
-       {
+       for (i=0; i<info->count; i++) {
                DEBUGADD(1,("[%d]\t[%d]\t[%d]\t[%d]\t[%d]\t[%d]\t[%d]\n",
                i, info->data[i].type, info->data[i].field, info->data[i].reserved,
                info->data[i].id, info->data[i].size, info->data[i].enc_type));
@@ -1595,13 +1781,15 @@ static uint32 printer_notify_info(const POLICY_HND *hnd, SPOOL_NOTIFY_INFO *info
 /********************************************************************
  * spoolss_rfnpcnex
  ********************************************************************/
-uint32 _spoolss_rfnpcnex( const POLICY_HND *handle, uint32 change,
+uint32 _spoolss_rfnpcnex( POLICY_HND *handle, uint32 change,
                          SPOOL_NOTIFY_OPTION *option, SPOOL_NOTIFY_INFO *info)
 {
        Printer_entry *Printer=find_printer_index_by_hnd(handle);
 
-       if (!OPEN_HANDLE(Printer))
+       if (!OPEN_HANDLE(Printer)) {
+               DEBUG(0,("_spoolss_rfnpcnex: Invalid handle (%s).\n",OUR_HANDLE(handle)));
                return ERROR_INVALID_HANDLE;
+       }
 
        DEBUG(4,("Printer type %x\n",Printer->printer_type));
 
@@ -1623,10 +1811,8 @@ uint32 _spoolss_rfnpcnex( const POLICY_HND *handle, uint32 change,
        switch (Printer->printer_type) {
                case PRINTER_HANDLE_IS_PRINTSERVER:
                        return printserver_notify_info(handle, info);
-                       break;
                case PRINTER_HANDLE_IS_PRINTER:
                        return printer_notify_info(handle, info);
-                       break;
        }
 
        return ERROR_INVALID_HANDLE;
@@ -1644,7 +1830,7 @@ static BOOL construct_printer_info_0(PRINTER_INFO_0 *printer, int snum, fstring
        counter_printer_0 *session_counter;
        uint32 global_counter;
        struct tm *t;
-       time_t setup_time = time(NULL);
+       time_t setuptime;
 
        print_queue_struct *queue=NULL;
        print_status_struct status;
@@ -1687,18 +1873,18 @@ static BOOL construct_printer_info_0(PRINTER_INFO_0 *printer, int snum, fstring
        
        /* the description and the name are of the form \\server\share */
        slprintf(chaine,sizeof(chaine)-1,"\\\\%s\\%s",servername, ntprinter->info_2->printername);
-                                                           
-       init_unistr(&(printer->printername), chaine);
+
+       init_unistr(&printer->printername, chaine);
        
        slprintf(chaine,sizeof(chaine)-1,"\\\\%s", servername);
-       init_unistr(&(printer->servername), chaine);
+       init_unistr(&printer->servername, chaine);
        
        printer->cjobs = count;
        printer->total_jobs = 0;
        printer->total_bytes = 0;
 
-       t=gmtime(&setup_time);
-       ntprinter->info_2->setuptime = (uint32)setup_time; /* FIXME !! */
+       setuptime = (time_t)ntprinter->info_2->setuptime;
+       t=gmtime(&setuptime);
 
        printer->year = t->tm_year+1900;
        printer->month = t->tm_mon+1;
@@ -1757,14 +1943,21 @@ static BOOL construct_printer_info_1(fstring server, uint32 flags, PRINTER_INFO_
 
        printer->flags=flags;
 
-       snprintf(chaine,sizeof(chaine)-1,"%s%s,%s,%s",server, ntprinter->info_2->printername,
-               ntprinter->info_2->drivername, lp_comment(snum));
+       if (*ntprinter->info_2->comment == '\0') {
+               init_unistr(&printer->comment, lp_comment(snum));
+               snprintf(chaine,sizeof(chaine)-1,"%s%s,%s,%s",server, ntprinter->info_2->printername,
+                       ntprinter->info_2->drivername, lp_comment(snum));
+       }
+       else {
+               init_unistr(&printer->comment, ntprinter->info_2->comment); /* saved comment. */
+               snprintf(chaine,sizeof(chaine)-1,"%s%s,%s,%s",server, ntprinter->info_2->printername,
+                       ntprinter->info_2->drivername, ntprinter->info_2->comment);
+       }
                
        snprintf(chaine2,sizeof(chaine)-1,"%s%s", server, ntprinter->info_2->printername);
 
        init_unistr(&printer->description, chaine);
        init_unistr(&printer->name, chaine2);   
-       init_unistr(&printer->comment, lp_comment(snum));
        
        free_a_printer(&ntprinter,2);
 
@@ -1814,15 +2007,13 @@ static DEVICEMODE *construct_dev_mode(int snum, char *servername)
 
        if (printer->info_2->devmode)
                ntdevmode = dup_nt_devicemode(printer->info_2->devmode);
-       else
-               ntdevmode = construct_nt_devicemode();
 
        if (ntdevmode == NULL)
                goto fail;
 
        DEBUGADD(8,("loading DEVICEMODE\n"));
-       snprintf(adevice, sizeof(adevice), "\\\\%s\\%s", global_myname, 
-                                                        printer->info_2->printername);
+
+       snprintf(adevice, sizeof(adevice), "\\\\%s\\%s", global_myname, printer->info_2->printername);
        init_unistr(&devmode->devicename, adevice);
 
        snprintf(aform, sizeof(aform), ntdevmode->formname);
@@ -1903,14 +2094,23 @@ static BOOL construct_printer_info_2(fstring servername, PRINTER_INFO_2 *printer
        else
                fstrcpy(sl, '\0');
 
-       snprintf(chaine2, sizeof(chaine)-1, "%s%s%s", servername, sl, ntprinter->info_2->printername);
+       if (!strchr(ntprinter->info_2->printername, '\\')) {
+               snprintf(chaine2, sizeof(chaine)-1, "%s%s%s", servername, sl, ntprinter->info_2->printername);
+       } else {
+               pstrcpy(chaine2, ntprinter->info_2->printername);
+       }
 
        init_unistr(&printer->servername, chaine);                              /* servername*/
        init_unistr(&printer->printername, chaine2);                            /* printername*/
        init_unistr(&printer->sharename, lp_servicename(snum));                 /* sharename */
-       init_unistr(&printer->portname, lp_servicename(snum));                  /* port */      
+       init_unistr(&printer->portname, ntprinter->info_2->portname);                   /* port */      
        init_unistr(&printer->drivername, ntprinter->info_2->drivername);       /* drivername */
-       init_unistr(&printer->comment, lp_comment(snum));                       /* comment */   
+
+       if (*ntprinter->info_2->comment == '\0')
+               init_unistr(&printer->comment, lp_comment(snum));                       /* comment */   
+       else
+               init_unistr(&printer->comment, ntprinter->info_2->comment); /* saved comment. */
+
        init_unistr(&printer->location, ntprinter->info_2->location);           /* location */  
        init_unistr(&printer->sepfile, ntprinter->info_2->sepfile);             /* separator file */
        init_unistr(&printer->printprocessor, ntprinter->info_2->printprocessor);/* print processor */
@@ -1927,10 +2127,11 @@ static BOOL construct_printer_info_2(fstring servername, PRINTER_INFO_2 *printer
        printer->cjobs = count;                                                 /* jobs */
        printer->averageppm = ntprinter->info_2->averageppm;                    /* average pages per minute */
                        
-       if((printer->devmode = construct_dev_mode(snum, servername)) == NULL)
-               goto err;
+       if((printer->devmode = construct_dev_mode(snum, servername)) == NULL) {
+               DEBUG(8, ("Returning NULL Devicemode!\n"));
+       }
 
-       if (ntprinter->info_2->secdesc_buf->len != 0) {
+       if (ntprinter->info_2->secdesc_buf && ntprinter->info_2->secdesc_buf->len != 0) {
                /* steal the printer info sec_desc structure.  [badly done]. */
                printer->secdesc = ntprinter->info_2->secdesc_buf->sec;
                ntprinter->info_2->secdesc_buf->sec = NULL; /* Stolen memory. */
@@ -1944,13 +2145,6 @@ static BOOL construct_printer_info_2(fstring servername, PRINTER_INFO_2 *printer
        free_a_printer(&ntprinter, 2);
        safe_free(queue);
        return True;
-
-  err:
-
-       if (ntprinter)
-               free_a_printer(&ntprinter, 2);
-       safe_free(queue);
-       return False;
 }
 
 /********************************************************************
@@ -1971,11 +2165,32 @@ static BOOL construct_printer_info_3(fstring servername,
                DEBUG(0,("construct_printer_info_3: malloc fail.\n"));
                return False;
        }
+
+       ZERO_STRUCTP(printer);
        
-       printer->flags = 4; /* This is the offset to the SEC_DESC. */
-       if (ntprinter->info_2->secdesc_buf->len != 0) {
+       printer->flags = 4; /* These are the components of the SD we are returning. */
+       if (ntprinter->info_2->secdesc_buf && ntprinter->info_2->secdesc_buf->len != 0) {
                /* steal the printer info sec_desc structure.  [badly done]. */
                printer->secdesc = ntprinter->info_2->secdesc_buf->sec;
+
+#if 0
+               /*
+                * Set the flags for the components we are returning.
+                */
+
+               if (printer->secdesc->owner_sid)
+                       printer->flags |= OWNER_SECURITY_INFORMATION;
+
+               if (printer->secdesc->grp_sid)
+                       printer->flags |= GROUP_SECURITY_INFORMATION;
+
+               if (printer->secdesc->dacl)
+                       printer->flags |= DACL_SECURITY_INFORMATION;
+
+               if (printer->secdesc->sacl)
+                       printer->flags |= SACL_SECURITY_INFORMATION;
+#endif
+
                ntprinter->info_2->secdesc_buf->sec = NULL; /* Stolen the malloced memory. */
                ntprinter->info_2->secdesc_buf->len = 0; /* Stolen the malloced memory. */
                ntprinter->info_2->secdesc_buf->max_len = 0; /* Stolen the malloced memory. */
@@ -2165,7 +2380,7 @@ static BOOL enum_all_printers_info_2(fstring servername, NEW_BUFFER *buffer, uin
                                if((printers=Realloc(printers, (*returned +1)*sizeof(PRINTER_INFO_2))) == NULL)
                                        return ERROR_NOT_ENOUGH_MEMORY;
                                DEBUG(4,("ReAlloced memory for [%d] PRINTER_INFO_2\n", *returned));             
-                               memcpy(&(printers[*returned]), &current_prt, sizeof(PRINTER_INFO_2));
+                               memcpy(&printers[*returned], &current_prt, sizeof(PRINTER_INFO_2));
                                (*returned)++;
                        }
                }
@@ -2305,18 +2520,14 @@ uint32 _spoolss_enumprinters( uint32 flags, const UNISTR2 *servername, uint32 le
        switch (level) {
        case 1:
                return enumprinters_level1(flags, name, buffer, offered, needed, returned);
-               break;
        case 2:
                return enumprinters_level2(flags, name, buffer, offered, needed, returned);
-               break;                          
        case 5:
                return enumprinters_level5(flags, name, buffer, offered, needed, returned);
-               break;                          
        case 3:
        case 4:
        default:
                return ERROR_INVALID_LEVEL;
-               break;
        }
 }
 
@@ -2481,78 +2692,70 @@ uint32 _spoolss_getprinter(POLICY_HND *handle, uint32 level,
                return getprinter_level_3(servername,snum, buffer, offered, needed);
        default:
                return ERROR_INVALID_LEVEL;
-               break;
        }
 }      
                
 /********************************************************************
- * construct_printer_driver_info_1
- * fill a construct_printer_driver_info_1 struct
+ * fill a DRIVER_INFO_1 struct
  ********************************************************************/
-static void fill_printer_driver_info_1(DRIVER_INFO_1 *info, 
-                                       NT_PRINTER_DRIVER_INFO_LEVEL driver, 
-                                      fstring servername, fstring architecture)
+static void fill_printer_driver_info_1(DRIVER_INFO_1 *info, NT_PRINTER_DRIVER_INFO_LEVEL driver, fstring servername, fstring architecture)
 {
-       init_unistr( &(info->name), driver.info_3->name);
+       init_unistr( &info->name, driver.info_3->name);
 }
 
-static void construct_printer_driver_info_1(DRIVER_INFO_1 *info, int snum, 
-                                            fstring servername, fstring architecture)
+/********************************************************************
+ * construct_printer_driver_info_1
+ ********************************************************************/
+static uint32 construct_printer_driver_info_1(DRIVER_INFO_1 *info, int snum, fstring servername, fstring architecture, uint32 version)
 {      
        NT_PRINTER_INFO_LEVEL *printer = NULL;
        NT_PRINTER_DRIVER_INFO_LEVEL driver;
 
        ZERO_STRUCT(driver);
 
-       get_a_printer(&printer, 2, lp_servicename(snum) );
-       get_a_printer_driver(&driver, 3, printer->info_2->drivername, architecture);    
-       
+       if (get_a_printer(&printer, 2, lp_servicename(snum)) != 0)
+               return ERROR_INVALID_PRINTER_NAME;
+
+       if (get_a_printer_driver(&driver, 3, printer->info_2->drivername, architecture, version) != 0)
+               return ERROR_UNKNOWN_PRINTER_DRIVER;
+
        fill_printer_driver_info_1(info, driver, servername, architecture);
 
        free_a_printer(&printer,2);
+
+       return NT_STATUS_NO_PROBLEMO;
 }
 
 /********************************************************************
  * construct_printer_driver_info_2
  * fill a printer_info_2 struct
  ********************************************************************/
-static void fill_printer_driver_info_2(DRIVER_INFO_2 *info, 
-                                       NT_PRINTER_DRIVER_INFO_LEVEL driver, 
-                                      fstring servername, fstring architecture)
+static void fill_printer_driver_info_2(DRIVER_INFO_2 *info, NT_PRINTER_DRIVER_INFO_LEVEL driver, fstring servername)
 {
-       pstring where;
        pstring temp_driverpath;
        pstring temp_datafile;
        pstring temp_configfile;
-       fstring short_archi;
-
-       get_short_archi(short_archi,architecture);
-       
-       snprintf(where,sizeof(where)-1,"\\\\%s\\print$\\%s\\%s\\", servername, short_archi, driver.info_3->name);
 
        info->version=driver.info_3->cversion;
 
-       init_unistr( &(info->name),         driver.info_3->name );
-       init_unistr( &(info->architecture), architecture );
-       
-       snprintf(temp_driverpath, sizeof(temp_driverpath)-1, "%s%s", where, 
-                driver.info_3->driverpath);
-       init_unistr( &(info->driverpath),   temp_driverpath );
+       init_unistr( &info->name, driver.info_3->name );
+       init_unistr( &info->architecture, driver.info_3->environment );
 
-       snprintf(temp_datafile,   sizeof(temp_datafile)-1, "%s%s", where, 
-                driver.info_3->datafile);
-       init_unistr( &(info->datafile),     temp_datafile );
+       snprintf(temp_driverpath, sizeof(temp_driverpath)-1, "\\\\%s%s", servername, driver.info_3->driverpath);
+       init_unistr( &info->driverpath, temp_driverpath );
 
-       snprintf(temp_configfile, sizeof(temp_configfile)-1, "%s%s", where, 
-                driver.info_3->configfile);
-       init_unistr( &(info->configfile),   temp_configfile );  
+       snprintf(temp_datafile, sizeof(temp_datafile)-1, "\\\\%s%s", servername, driver.info_3->datafile);
+       init_unistr( &info->datafile, temp_datafile );
+
+       snprintf(temp_configfile, sizeof(temp_configfile)-1, "\\\\%s%s", servername, driver.info_3->configfile);
+       init_unistr( &info->configfile, temp_configfile );      
 }
 
 /********************************************************************
  * construct_printer_driver_info_2
  * fill a printer_info_2 struct
  ********************************************************************/
-static void construct_printer_driver_info_2(DRIVER_INFO_2 *info, int snum, fstring servername, fstring architecture)
+static uint32 construct_printer_driver_info_2(DRIVER_INFO_2 *info, int snum, fstring servername, fstring architecture, uint32 version)
 {
        NT_PRINTER_INFO_LEVEL *printer = NULL;
        NT_PRINTER_DRIVER_INFO_LEVEL driver;
@@ -2560,12 +2763,17 @@ static void construct_printer_driver_info_2(DRIVER_INFO_2 *info, int snum, fstri
        ZERO_STRUCT(printer);
        ZERO_STRUCT(driver);
 
-       get_a_printer(&printer, 2, lp_servicename(snum) );
-       get_a_printer_driver(&driver, 3, printer->info_2->drivername, architecture);    
+       if (!get_a_printer(&printer, 2, lp_servicename(snum)) != 0)
+               return ERROR_INVALID_PRINTER_NAME;
+
+       if (!get_a_printer_driver(&driver, 3, printer->info_2->drivername, architecture, version) != 0)
+               return ERROR_UNKNOWN_PRINTER_DRIVER;
 
-       fill_printer_driver_info_2(info, driver, servername, architecture);
+       fill_printer_driver_info_2(info, driver, servername);
 
        free_a_printer(&printer,2);
+
+       return NT_STATUS_NO_PROBLEMO;
 }
 
 /********************************************************************
@@ -2573,7 +2781,7 @@ static void construct_printer_driver_info_2(DRIVER_INFO_2 *info, int snum, fstri
  *
  * convert an array of ascii string to a UNICODE string
  ********************************************************************/
-static void init_unistr_array(uint16 **uni_array, fstring *char_array, char *where)
+static void init_unistr_array(uint16 **uni_array, fstring *char_array, char *servername)
 {
        int i=0;
        int j=0;
@@ -2590,16 +2798,15 @@ static void init_unistr_array(uint16 **uni_array, fstring *char_array, char *whe
                        v = char_array[i];
                        if (!v) v = ""; /* hack to handle null lists */
                }
-               snprintf(line, sizeof(line)-1, "%s%s", where, v);
+               if (strlen(v) == 0) break;
+               snprintf(line, sizeof(line)-1, "\\\\%s%s", servername, v);
                DEBUGADD(6,("%d:%s:%d\n", i, line, strlen(line)));
                if((*uni_array=Realloc(*uni_array, (j+strlen(line)+2)*sizeof(uint16))) == NULL) {
                        DEBUG(0,("init_unistr_array: Realloc error\n" ));
                        return;
                }
-               ascii_to_unistr((char *)(*uni_array+j), line , 2*strlen(line));
-               j+=strlen(line)+1;                      
+               j += (dos_PutUniCode((char *)(*uni_array+j), line , sizeof(uint16)*strlen(line), True) / sizeof(uint16) );
                i++;
-               if (strlen(v) == 0) break;
        }
        
        if (*uni_array) {
@@ -2613,63 +2820,152 @@ static void init_unistr_array(uint16 **uni_array, fstring *char_array, char *whe
  * construct_printer_info_3
  * fill a printer_info_3 struct
  ********************************************************************/
-static void fill_printer_driver_info_3(DRIVER_INFO_3 *info, 
-                                       NT_PRINTER_DRIVER_INFO_LEVEL driver, 
-                                      fstring servername, fstring architecture)
+static void fill_printer_driver_info_3(DRIVER_INFO_3 *info, NT_PRINTER_DRIVER_INFO_LEVEL driver, fstring servername)
 {
-       pstring where;
        pstring temp_driverpath;
        pstring temp_datafile;
        pstring temp_configfile;
        pstring temp_helpfile;
-       fstring short_archi;
-       
-       get_short_archi(short_archi, architecture);
-       
-       snprintf(where,sizeof(where)-1,"\\\\%s\\print$\\%s\\%s\\", servername, short_archi, driver.info_3->name);
+
+       ZERO_STRUCTP(info);
 
        info->version=driver.info_3->cversion;
 
-       init_unistr( &(info->name),         driver.info_3->name );      
-       init_unistr( &(info->architecture), architecture );
-       
-       snprintf(temp_driverpath, sizeof(temp_driverpath)-1, "%s%s", where, driver.info_3->driverpath);          
-       init_unistr( &(info->driverpath), temp_driverpath );
-       
-       snprintf(temp_datafile,   sizeof(temp_datafile)-1,   "%s%s", where, driver.info_3->datafile); 
-       init_unistr( &(info->datafile), temp_datafile );
-       
-       snprintf(temp_configfile, sizeof(temp_configfile)-1, "%s%s", where, driver.info_3->configfile);
-       init_unistr( &(info->configfile), temp_configfile );    
-       
-       snprintf(temp_helpfile,   sizeof(temp_helpfile)-1,   "%s%s", where, driver.info_3->helpfile);
-       init_unistr( &(info->helpfile), temp_helpfile );
+       init_unistr( &info->name, driver.info_3->name );        
+       init_unistr( &info->architecture, driver.info_3->environment );
+
+       snprintf(temp_driverpath, sizeof(temp_driverpath)-1, "\\\\%s%s", servername, driver.info_3->driverpath);                 
+       init_unistr( &info->driverpath, temp_driverpath );
+
+       snprintf(temp_datafile, sizeof(temp_datafile)-1, "\\\\%s%s", servername, driver.info_3->datafile); 
+       init_unistr( &info->datafile, temp_datafile );
 
-       init_unistr( &(info->monitorname), driver.info_3->monitorname );
-       init_unistr( &(info->defaultdatatype), driver.info_3->defaultdatatype );
+       snprintf(temp_configfile, sizeof(temp_configfile)-1, "\\\\%s%s", servername, driver.info_3->configfile);
+       init_unistr( &info->configfile, temp_configfile );      
+
+       snprintf(temp_helpfile, sizeof(temp_helpfile)-1, "\\\\%s%s", servername, driver.info_3->helpfile);
+       init_unistr( &info->helpfile, temp_helpfile );
+
+       init_unistr( &info->monitorname, driver.info_3->monitorname );
+       init_unistr( &info->defaultdatatype, driver.info_3->defaultdatatype );
 
        info->dependentfiles=NULL;
-       init_unistr_array(&info->dependentfiles, driver.info_3->dependentfiles, where);
+       init_unistr_array(&info->dependentfiles, driver.info_3->dependentfiles, servername);
 }
 
 /********************************************************************
  * construct_printer_info_3
  * fill a printer_info_3 struct
  ********************************************************************/
-static void construct_printer_driver_info_3(DRIVER_INFO_3 *info, int snum, 
-                                            fstring servername, fstring architecture)
+static uint32 construct_printer_driver_info_3(DRIVER_INFO_3 *info, int snum, fstring servername, fstring architecture, uint32 version)
 {      
        NT_PRINTER_INFO_LEVEL *printer = NULL;
        NT_PRINTER_DRIVER_INFO_LEVEL driver;
+       uint32 status=0;
+       ZERO_STRUCT(driver);
+
+       status=get_a_printer(&printer, 2, lp_servicename(snum) );
+       DEBUG(8,("construct_printer_driver_info_3: status: %d\n", status));
+       if (status != 0)
+               return ERROR_INVALID_PRINTER_NAME;
+
+       status=get_a_printer_driver(&driver, 3, printer->info_2->drivername, architecture, version);    
+       DEBUG(8,("construct_printer_driver_info_3: status: %d\n", status));
+       if (status != 0) {
+               free_a_printer(&printer,2);
+               return ERROR_UNKNOWN_PRINTER_DRIVER;
+       }
 
+       fill_printer_driver_info_3(info, driver, servername);
+
+       free_a_printer(&printer,2);
+
+       return NT_STATUS_NO_PROBLEMO;
+}
+
+/********************************************************************
+ * construct_printer_info_6
+ * fill a printer_info_6 struct - we know that driver is really level 3. This sucks. JRA.
+ ********************************************************************/
+
+static void fill_printer_driver_info_6(DRIVER_INFO_6 *info, NT_PRINTER_DRIVER_INFO_LEVEL driver, fstring servername)
+{
+       pstring temp_driverpath;
+       pstring temp_datafile;
+       pstring temp_configfile;
+       pstring temp_helpfile;
+       fstring nullstr;
+
+       ZERO_STRUCTP(info);
+       memset(&nullstr, '\0', sizeof(fstring));
+
+       info->version=driver.info_3->cversion;
+
+       init_unistr( &info->name, driver.info_3->name );        
+       init_unistr( &info->architecture, driver.info_3->environment );
+
+       snprintf(temp_driverpath, sizeof(temp_driverpath)-1, "\\\\%s%s", servername, driver.info_3->driverpath);                 
+       init_unistr( &info->driverpath, temp_driverpath );
+
+       snprintf(temp_datafile, sizeof(temp_datafile)-1, "\\\\%s%s", servername, driver.info_3->datafile); 
+       init_unistr( &info->datafile, temp_datafile );
+
+       snprintf(temp_configfile, sizeof(temp_configfile)-1, "\\\\%s%s", servername, driver.info_3->configfile);
+       init_unistr( &info->configfile, temp_configfile );      
+
+       snprintf(temp_helpfile, sizeof(temp_helpfile)-1, "\\\\%s%s", servername, driver.info_3->helpfile);
+       init_unistr( &info->helpfile, temp_helpfile );
+
+       init_unistr( &info->monitorname, driver.info_3->monitorname );
+       init_unistr( &info->defaultdatatype, driver.info_3->defaultdatatype );
+
+       info->dependentfiles=NULL;
+       init_unistr_array(&info->dependentfiles, driver.info_3->dependentfiles, servername);
+
+       info->previousdrivernames=NULL;
+       init_unistr_array(&info->previousdrivernames, &nullstr, servername);
+
+       info->driver_date.low=0;
+       info->driver_date.high=0;
+
+       info->padding=0;
+       info->driver_version_low=0;
+       info->driver_version_high=0;
+
+       init_unistr( &info->mfgname, "");
+       init_unistr( &info->oem_url, "");
+       init_unistr( &info->hardware_id, "");
+       init_unistr( &info->provider, "");
+}
+
+/********************************************************************
+ * construct_printer_info_6
+ * fill a printer_info_6 struct
+ ********************************************************************/
+static uint32 construct_printer_driver_info_6(DRIVER_INFO_6 *info, int snum, fstring servername, fstring architecture, uint32 version)
+{      
+       NT_PRINTER_INFO_LEVEL *printer = NULL;
+       NT_PRINTER_DRIVER_INFO_LEVEL driver;
+       uint32 status=0;
        ZERO_STRUCT(driver);
 
-       get_a_printer(&printer, 2, lp_servicename(snum) );      
-       get_a_printer_driver(&driver, 3, printer->info_2->drivername, architecture);    
+       status=get_a_printer(&printer, 2, lp_servicename(snum) );
+       DEBUG(8,("construct_printer_driver_info_6: status: %d\n", status));
+       if (status != 0)
+               return ERROR_INVALID_PRINTER_NAME;
+
+       status=get_a_printer_driver(&driver, 3, printer->info_2->drivername, architecture, version);    
+       DEBUG(8,("construct_printer_driver_info_6: status: %d\n", status));
+       if (status != 0) {
+               free_a_printer(&printer,2);
+               return ERROR_UNKNOWN_PRINTER_DRIVER;
+       }
 
-       fill_printer_driver_info_3(info, driver, servername, architecture);
+       fill_printer_driver_info_6(info, driver, servername);
 
        free_a_printer(&printer,2);
+
+       return NT_STATUS_NO_PROBLEMO;
 }
 
 /****************************************************************************
@@ -2682,14 +2978,28 @@ static void free_printer_driver_info_3(DRIVER_INFO_3 *info)
 
 /****************************************************************************
 ****************************************************************************/
-static uint32 getprinterdriver2_level1(fstring servername, fstring architecture, int snum, NEW_BUFFER *buffer, uint32 offered, uint32 *needed)
+
+static void free_printer_driver_info_6(DRIVER_INFO_6 *info)
+{
+       safe_free(info->dependentfiles);
+       
+}
+
+/****************************************************************************
+****************************************************************************/
+static uint32 getprinterdriver2_level1(fstring servername, fstring architecture, uint32 version, int snum, NEW_BUFFER *buffer, uint32 offered, uint32 *needed)
 {
        DRIVER_INFO_1 *info=NULL;
+       uint32 status;
        
        if((info=(DRIVER_INFO_1 *)malloc(sizeof(DRIVER_INFO_1))) == NULL)
                return ERROR_NOT_ENOUGH_MEMORY;
        
-       construct_printer_driver_info_1(info, snum, servername, architecture);
+       status=construct_printer_driver_info_1(info, snum, servername, architecture, version);
+       if (status != NT_STATUS_NO_PROBLEMO) {
+               safe_free(info);
+               return status;
+       }
 
        /* check the required size. */  
        *needed += spoolss_size_printer_driver_info_1(info);
@@ -2713,14 +3023,19 @@ static uint32 getprinterdriver2_level1(fstring servername, fstring architecture,
 
 /****************************************************************************
 ****************************************************************************/
-static uint32 getprinterdriver2_level2(fstring servername, fstring architecture, int snum, NEW_BUFFER *buffer, uint32 offered, uint32 *needed)
+static uint32 getprinterdriver2_level2(fstring servername, fstring architecture, uint32 version, int snum, NEW_BUFFER *buffer, uint32 offered, uint32 *needed)
 {
        DRIVER_INFO_2 *info=NULL;
+       uint32 status;
        
        if((info=(DRIVER_INFO_2 *)malloc(sizeof(DRIVER_INFO_2))) == NULL)
                return ERROR_NOT_ENOUGH_MEMORY;
        
-       construct_printer_driver_info_2(info, snum, servername, architecture);
+       status=construct_printer_driver_info_2(info, snum, servername, architecture, version);
+       if (status != NT_STATUS_NO_PROBLEMO) {
+               safe_free(info);
+               return status;
+       }
 
        /* check the required size. */  
        *needed += spoolss_size_printer_driver_info_2(info);
@@ -2744,13 +3059,17 @@ static uint32 getprinterdriver2_level2(fstring servername, fstring architecture,
 
 /****************************************************************************
 ****************************************************************************/
-static uint32 getprinterdriver2_level3(fstring servername, fstring architecture, int snum, NEW_BUFFER *buffer, uint32 offered, uint32 *needed)
+static uint32 getprinterdriver2_level3(fstring servername, fstring architecture, uint32 version, int snum, NEW_BUFFER *buffer, uint32 offered, uint32 *needed)
 {
        DRIVER_INFO_3 info;
+       uint32 status;
 
        ZERO_STRUCT(info);
 
-       construct_printer_driver_info_3(&info, snum, servername, architecture);
+       status=construct_printer_driver_info_3(&info, snum, servername, architecture, version);
+       if (status != NT_STATUS_NO_PROBLEMO) {
+               return status;
+       }
 
        /* check the required size. */  
        *needed += spoolss_size_printer_driver_info_3(&info);
@@ -2773,7 +3092,40 @@ static uint32 getprinterdriver2_level3(fstring servername, fstring architecture,
 
 /****************************************************************************
 ****************************************************************************/
-uint32 _spoolss_getprinterdriver2(const POLICY_HND *handle, const UNISTR2 *uni_arch, uint32 level, 
+static uint32 getprinterdriver2_level6(fstring servername, fstring architecture, uint32 version, int snum, NEW_BUFFER *buffer, uint32 offered, uint32 *needed)
+{
+       DRIVER_INFO_6 info;
+       uint32 status;
+
+       ZERO_STRUCT(info);
+
+       status=construct_printer_driver_info_6(&info, snum, servername, architecture, version);
+       if (status != NT_STATUS_NO_PROBLEMO) {
+               return status;
+       }
+
+       /* check the required size. */  
+       *needed += spoolss_size_printer_driver_info_6(&info);
+
+       if (!alloc_buffer_size(buffer, *needed)) {
+               free_printer_driver_info_6(&info);
+               return ERROR_INSUFFICIENT_BUFFER;
+       }
+
+       /* fill the buffer with the structures */
+       new_smb_io_printer_driver_info_6("", buffer, &info, 0);
+
+       free_printer_driver_info_6(&info);
+
+       if (*needed > offered)
+               return ERROR_INSUFFICIENT_BUFFER;
+       else
+               return NT_STATUS_NO_PROBLEMO;
+}
+
+/****************************************************************************
+****************************************************************************/
+uint32 _spoolss_getprinterdriver2(POLICY_HND *handle, const UNISTR2 *uni_arch, uint32 level, 
                                uint32 clientmajorversion, uint32 clientminorversion,
                                NEW_BUFFER *buffer, uint32 offered,
                                uint32 *needed, uint32 *servermajorversion, uint32 *serverminorversion)
@@ -2796,28 +3148,25 @@ uint32 _spoolss_getprinterdriver2(const POLICY_HND *handle, const UNISTR2 *uni_a
 
        switch (level) {
        case 1:
-               return getprinterdriver2_level1(servername, architecture, snum, buffer, offered, needed);
-               break;
+               return getprinterdriver2_level1(servername, architecture, clientmajorversion, snum, buffer, offered, needed);
        case 2:
-               return getprinterdriver2_level2(servername, architecture, snum, buffer, offered, needed);
-               break;                          
+               return getprinterdriver2_level2(servername, architecture, clientmajorversion, snum, buffer, offered, needed);
        case 3:
-               return getprinterdriver2_level3(servername, architecture, snum, buffer, offered, needed);
-               break;                          
+               return getprinterdriver2_level3(servername, architecture, clientmajorversion, snum, buffer, offered, needed);
+       case 6:
+               return getprinterdriver2_level6(servername, architecture, clientmajorversion, snum, buffer, offered, needed);
        default:
                return ERROR_INVALID_LEVEL;
-               break;
        }
 }
 
 /****************************************************************************
 ****************************************************************************/
-uint32 _spoolss_startpageprinter(const POLICY_HND *handle)
+uint32 _spoolss_startpageprinter(POLICY_HND *handle)
 {
        Printer_entry *Printer = find_printer_index_by_hnd(handle);
 
-       if (OPEN_HANDLE(Printer))
-       {
+       if (OPEN_HANDLE(Printer)) {
                Printer->page_started=True;
                return 0x0;
        }
@@ -2828,13 +3177,12 @@ uint32 _spoolss_startpageprinter(const POLICY_HND *handle)
 
 /****************************************************************************
 ****************************************************************************/
-uint32 _spoolss_endpageprinter(const POLICY_HND *handle)
+uint32 _spoolss_endpageprinter(POLICY_HND *handle)
 {
        Printer_entry *Printer = find_printer_index_by_hnd(handle);
 
-       if (!OPEN_HANDLE(Printer))
-       {
-               DEBUG(3,("Error in endpageprinter printer handle\n"));
+       if (!OPEN_HANDLE(Printer)) {
+               DEBUG(0,("_spoolss_endpageprinter: Invalid handle (%s).\n",OUR_HANDLE(handle)));
                return ERROR_INVALID_HANDLE;
        }
        
@@ -2843,26 +3191,45 @@ uint32 _spoolss_endpageprinter(const POLICY_HND *handle)
        return NT_STATUS_NO_PROBLEMO;
 }
 
+/****************************************************************************
+ Return a user struct for a pipe user.
+****************************************************************************/
+
+static struct current_user *get_current_user(struct current_user *user, pipes_struct *p)
+{
+       if (p->ntlmssp_auth_validated) {
+               memcpy(user, &p->pipe_user, sizeof(struct current_user));
+       } else {
+               extern struct current_user current_user;
+               memcpy(user, &current_user, sizeof(struct current_user));
+       }
+
+       return user;
+}
 
 /********************************************************************
  * api_spoolss_getprinter
  * called from the spoolss dispatcher
  *
  ********************************************************************/
-uint32 _spoolss_startdocprinter( const POLICY_HND *handle, uint32 level,
-                               DOC_INFO *docinfo, uint32 *jobid)
+uint32 _spoolss_startdocprinter(POLICY_HND *handle, uint32 level,
+                               pipes_struct *p, DOC_INFO *docinfo, 
+                               uint32 *jobid)
 {
        DOC_INFO_1 *info_1 = &docinfo->doc_info_1;
        int snum;
        pstring jobname;
        fstring datatype;
        Printer_entry *Printer = find_printer_index_by_hnd(handle);
+       struct current_user user;
 
-       if (!OPEN_HANDLE(Printer))
-       {
+       if (!OPEN_HANDLE(Printer)) {
+               DEBUG(0,("_spoolss_startdocprinter: Invalid handle (%s)\n", OUR_HANDLE(handle)));
                return ERROR_INVALID_HANDLE;
        }
 
+       get_current_user(&user, p);
+
        /*
         * a nice thing with NT is it doesn't listen to what you tell it.
         * when asked to send _only_ RAW datas, it tries to send datas
@@ -2875,35 +3242,34 @@ uint32 _spoolss_startdocprinter( const POLICY_HND *handle, uint32 level,
         * server-side code. *nnnnnggggh!*
         */
        
-       if (info_1->p_datatype != 0)
-       {
-               unistr2_to_ascii(datatype, &(info_1->docname), sizeof(datatype));
-               if (strcmp(datatype, "RAW") != 0)
-               {
+       if (info_1->p_datatype != 0) {
+               unistr2_to_ascii(datatype, &info_1->datatype, sizeof(datatype));
+               if (strcmp(datatype, "RAW") != 0) {
                        (*jobid)=0;
                        return ERROR_INVALID_DATATYPE;
                }               
        }                
        
        /* get the share number of the printer */
-       if (!get_printer_snum(handle, &snum))
-       {
+       if (!get_printer_snum(handle, &snum)) {
                return ERROR_INVALID_HANDLE;
        }
 
        unistr2_to_ascii(jobname, &info_1->docname, sizeof(jobname));
        
-       Printer->jobid = print_job_start(snum, jobname);
+       Printer->jobid = print_job_start(&user, snum, jobname);
+
+       /* An error occured in print_job_start() so return an appropriate
+          NT error code. */
 
-       /* need to map error codes properly - for now give out of
-          memory as I don't know the correct codes (tridge) */
        if (Printer->jobid == -1) {
-               return ERROR_NOT_ENOUGH_MEMORY;
+               return map_nt_error_from_unix(errno);
        }
        
        Printer->document_started=True;
        (*jobid) = Printer->jobid;
 
+       srv_spoolss_sendnotify(handle);
        return 0x0;
 }
 
@@ -2912,13 +3278,12 @@ uint32 _spoolss_startdocprinter( const POLICY_HND *handle, uint32 level,
  * called from the spoolss dispatcher
  *
  ********************************************************************/
-uint32 _spoolss_enddocprinter(const POLICY_HND *handle)
+uint32 _spoolss_enddocprinter(POLICY_HND *handle)
 {
        Printer_entry *Printer=find_printer_index_by_hnd(handle);
        
-       if (!OPEN_HANDLE(Printer))
-       {
-               DEBUG(3,("Error in enddocprinter handle\n"));
+       if (!OPEN_HANDLE(Printer)) {
+               DEBUG(0,("_spoolss_enddocprinter: Invalid handle (%s)\n", OUR_HANDLE(handle)));
                return ERROR_INVALID_HANDLE;
        }
        
@@ -2926,25 +3291,27 @@ uint32 _spoolss_enddocprinter(const POLICY_HND *handle)
        print_job_end(Printer->jobid);
        /* error codes unhandled so far ... */
 
+       srv_spoolss_sendnotify(handle);
+
        return 0x0;
 }
 
 /****************************************************************************
 ****************************************************************************/
-uint32 _spoolss_writeprinter( const POLICY_HND *handle,
+uint32 _spoolss_writeprinter( POLICY_HND *handle,
                                uint32 buffer_size,
-                               const uint8 *buffer,
+                               uint8 *buffer,
                                uint32 *buffer_written)
 {
        Printer_entry *Printer = find_printer_index_by_hnd(handle);
        
-       if (!OPEN_HANDLE(Printer))
-       {
-               DEBUG(3,("Error in writeprinter handle\n"));
+       if (!OPEN_HANDLE(Printer)) {
+               DEBUG(0,("_spoolss_writeprinter: Invalid handle (%s)\n",OUR_HANDLE(handle)));
                return ERROR_INVALID_HANDLE;
        }
 
-       (*buffer_written) = print_job_write(Printer->jobid, buffer, buffer_size);
+       (*buffer_written) = print_job_write(Printer->jobid, (char *)buffer, 
+                                           buffer_size);
 
        return 0x0;
 }
@@ -2954,53 +3321,399 @@ uint32 _spoolss_writeprinter( const POLICY_HND *handle,
  * called from the spoolss dispatcher
  *
  ********************************************************************/
-static uint32 control_printer(const POLICY_HND *handle, uint32 command)
+static uint32 control_printer(POLICY_HND *handle, uint32 command,
+                             pipes_struct *p)
 {
-       int snum;
+       struct current_user user;
+       int snum, errcode = ERROR_INVALID_FUNCTION;
        Printer_entry *Printer = find_printer_index_by_hnd(handle);
 
-       if (!OPEN_HANDLE(Printer))
+       get_current_user(&user, p);
+
+       if (!OPEN_HANDLE(Printer)) {
+               DEBUG(0,("control_printer: Invalid handle (%s)\n", OUR_HANDLE(handle)));
                return ERROR_INVALID_HANDLE;
+       }
 
-       if (!get_printer_snum(handle, &snum) )   
+       if (!get_printer_snum(handle, &snum))
                return ERROR_INVALID_HANDLE;
 
        switch (command) {
        case PRINTER_CONTROL_PAUSE:
-               if (print_queue_pause(snum)) {
-                       return 0;
+               if (print_queue_pause(&user, snum, &errcode)) {
+                       errcode = 0;
                }
                break;
        case PRINTER_CONTROL_RESUME:
        case PRINTER_CONTROL_UNPAUSE:
-               if (print_queue_resume(snum)) {
-                       return 0;
+               if (print_queue_resume(&user, snum, &errcode)) {
+                       errcode = 0;
                }
                break;
        case PRINTER_CONTROL_PURGE:
-               if (print_queue_purge(snum)) {
-                       return 0;
+               if (print_queue_purge(&user, snum, &errcode)) {
+                       errcode = 0;
                }
                break;
+       default:
+               return ERROR_INVALID_LEVEL;
        }
 
-       return ERROR_INVALID_FUNCTION;
+       return errcode;
+}
+
+/********************************************************************
+ * api_spoolss_abortprinter
+ ********************************************************************/
+
+uint32 _spoolss_abortprinter(POLICY_HND *handle, pipes_struct *p)
+{
+       return control_printer(handle, PRINTER_CONTROL_PURGE, p);
 }
 
 /********************************************************************
  * called by spoolss_api_setprinter
  * when updating a printer description
  ********************************************************************/
-static uint32 update_printer_sec(const POLICY_HND *handle, uint32 level,
+static uint32 update_printer_sec(POLICY_HND *handle, uint32 level,
                                 const SPOOL_PRINTER_INFO_LEVEL *info,
-                                SEC_DESC_BUF *secdesc_ctr)
+                                pipes_struct *p, SEC_DESC_BUF *secdesc_ctr)
 {
+       SEC_DESC_BUF *new_secdesc_ctr = NULL, *old_secdesc_ctr = NULL;
+       struct current_user user;
+       uint32 result;
+       int snum;
+
        Printer_entry *Printer = find_printer_index_by_hnd(handle);
 
-       if (!OPEN_HANDLE(Printer))
-               return ERROR_INVALID_HANDLE;
+       if (!OPEN_HANDLE(Printer) || !get_printer_snum(handle, &snum)) {
+               DEBUG(0,("update_printer_sec: Invalid handle (%s)\n", 
+                        OUR_HANDLE(handle)));
+
+               result = ERROR_INVALID_HANDLE;
+               goto done;
+       }
+
+       /* NT seems to like setting the security descriptor even though
+          nothing may have actually changed.  This causes annoying
+          dialog boxes when the user doesn't have permission to change
+          the security descriptor. */
+
+       nt_printing_getsec(Printer->dev.handlename, &old_secdesc_ctr);
+
+       new_secdesc_ctr = sec_desc_merge(secdesc_ctr, old_secdesc_ctr);
+
+       if (sec_desc_equal(new_secdesc_ctr->sec, old_secdesc_ctr->sec)) {
+               result = NT_STATUS_NO_PROBLEMO;
+               goto done;
+       }
+
+       /* Work out which user is performing the operation */
+
+       get_current_user(&user, p);
+
+       /* Check the user has permissions to change the security
+          descriptor.  By experimentation with two NT machines, the user
+          requires Full Access to the printer to change security
+          information. */ 
+
+       if (!print_access_check(&user, snum, PRINTER_ACCESS_ADMINISTER)) {
+               result = ERROR_ACCESS_DENIED;
+               goto done;
+       }
+
+       result = nt_printing_setsec(Printer->dev.handlename, new_secdesc_ctr);
+
+ done:
+       free_sec_desc_buf(&new_secdesc_ctr);
+       free_sec_desc_buf(&old_secdesc_ctr);
+
+       return result;
+}
+
+/********************************************************************
+ Do Samba sanity checks on a printer info struct.
+ ********************************************************************/
+
+static BOOL check_printer_ok(NT_PRINTER_INFO_LEVEL_2 *info, int snum)
+{
+       /*
+        * Ensure that this printer is shared under the correct name
+        * as this is what Samba insists upon.
+        */
+
+       if (!(info->attributes & (PRINTER_ATTRIBUTE_SHARED|PRINTER_ATTRIBUTE_NETWORK))) {
+               DEBUG(10,("check_printer_ok: SHARED/NETWORK check failed (%x).\n",
+                                                       (unsigned int)info->attributes ));
+               return False;
+       }
+
+       if (!(info->attributes & PRINTER_ATTRIBUTE_RAW_ONLY)) {
+               /* NT forgets to set the raw attribute but sends the correct type. */
+               if (strequal(info->datatype, "RAW"))
+                       info->attributes |= PRINTER_ATTRIBUTE_RAW_ONLY;
+               else {
+                       DEBUG(10,("check_printer_ok: RAW check failed (%x).\n", (unsigned int)info->attributes ));
+                       return False;
+               }
+       }
+
+       /*
+        * Sometimes the NT client doesn't set the sharename, but
+        * includes the sharename in the printername. This could
+        * cause SETPRINTER to fail which causes problems with the
+        * client getting confused between local/remote printers...
+        */
+        
+       if (*info->sharename == '\0') {
+               char *p = strrchr(info->printername, '\\');
+               if (p)
+                       fstrcpy(info->sharename, p+1);
+       }
+
+       if (!strequal(info->sharename, lp_servicename(snum))) {
+               DEBUG(10,("check_printer_ok: NAME check failed (%s) (%s).\n",
+                                       info->sharename, lp_servicename(snum)));
+               return False;
+       }
+
+       return True;
+}
+
+/****************************************************************************
+****************************************************************************/
+static BOOL add_printer_hook(NT_PRINTER_INFO_LEVEL *printer)
+{
+       pid_t local_pid = sys_getpid();
+       char *cmd = lp_addprinter_cmd();
+       char *path;
+       char **qlines;
+       pstring tmp_file;
+       pstring command;
+       pstring driverlocation;
+       int numlines;
+       int ret;
+
+       if (*lp_pathname(lp_servicenumber(PRINTERS_NAME)))
+               path = lp_pathname(lp_servicenumber(PRINTERS_NAME));
+       else
+               path = tmpdir();
+
+       /* build driver path... only 9X architecture is needed for legacy reasons */
+       slprintf(driverlocation, sizeof(driverlocation)-1, "\\\\%s\\print$\\WIN40\\0",
+                       global_myname);
+       /* change \ to \\ for the shell */
+       all_string_sub(driverlocation,"\\","\\\\",sizeof(pstring));
+       
+       slprintf(tmp_file, sizeof(tmp_file), "%s/smbcmd.%d", path, local_pid);
+       slprintf(command, sizeof(command), "%s \"%s\" \"%s\" \"%s\" \"%s\" \"%s\" \"%s\"",
+                       cmd, printer->info_2->printername, printer->info_2->sharename,
+                       printer->info_2->portname, printer->info_2->drivername,
+                       printer->info_2->location, driverlocation);
+
+       unlink(tmp_file);
+       DEBUG(10,("Running [%s > %s]\n", command,tmp_file));
+       ret = smbrun(command, tmp_file, False);
+       DEBUGADD(10,("returned [%d]\n", ret));
+
+       if ( ret != 0 ) {
+               unlink(tmp_file);
+               return False;
+       }
+
+       numlines = 0;
+       qlines = file_lines_load(tmp_file, &numlines);
+       DEBUGADD(10,("Lines returned = [%d]\n", numlines));
+       DEBUGADD(10,("Unlinking port file [%s]\n", tmp_file));
+       unlink(tmp_file);
+
+       if(numlines) {
+               /* Set the portname to what the script says the portname should be. */
+               strncpy(printer->info_2->portname, qlines[0], sizeof(printer->info_2->portname));
+               DEBUGADD(6,("Line[0] = [%s]\n", qlines[0]));
+
+               /* Send SIGHUP to process group... is there a better way? */
+               kill(0, SIGHUP);
+               add_all_printers();
+       }
+
+       file_lines_free(qlines);
+       return True;
+}
+
+/* Return true if two devicemodes are equal */
+
+static BOOL nt_devicemode_equal(NT_DEVICEMODE *d1, NT_DEVICEMODE *d2)
+{
+       if (!strequal(d1->devicename, d2->devicename) ||
+           !strequal(d1->formname, d2->formname)) {
+               return False;
+       }
+
+       if (d1->specversion != d2->specversion ||
+           d1->driverversion != d2->driverversion ||
+           d1->size != d2->size ||
+           d1->driverextra != d2->driverextra ||
+           d1->orientation != d2->orientation ||
+           d1->papersize != d2->papersize ||
+           d1->paperlength != d2->paperlength ||
+           d1->paperwidth != d2->paperwidth ||
+           d1->scale != d2->scale ||
+           d1->copies != d2->copies ||
+           d1->defaultsource != d2->defaultsource ||
+           d1->printquality != d2->printquality ||
+           d1->color != d2->color ||
+           d1->duplex != d2->duplex ||
+           d1->yresolution != d2->yresolution ||
+           d1->ttoption != d2->ttoption ||
+           d1->collate != d2->collate ||
+           d1->logpixels != d2->logpixels) {
+               return False;
+       }
+
+       if (d1->fields != d2->fields ||
+           d1->bitsperpel != d2->bitsperpel ||
+           d1->pelswidth != d2->pelswidth ||
+           d1->pelsheight != d2->pelsheight ||
+           d1->displayflags != d2->displayflags ||
+           d1->displayfrequency != d2->displayfrequency ||
+           d1->icmmethod != d2->icmmethod ||
+           d1->icmintent != d2->icmintent ||
+           d1->mediatype != d2->mediatype ||
+           d1->dithertype != d2->dithertype ||
+           d1->reserved1 != d2->reserved1 ||
+           d1->reserved2 != d2->reserved2 ||
+           d1->panningwidth != d2->panningwidth ||
+           d1->panningheight != d2->panningheight) {
+               return False;
+       }
+
+       /* Not sure what to do about these fields */
+#if 0
+       uint8   *private;
+#endif
+
+       return True;
+}
+
+/* Return true if two NT_PRINTER_PARAM structures are equal */
+
+static BOOL nt_printer_param_equal(NT_PRINTER_PARAM *p1,
+                                  NT_PRINTER_PARAM *p2)
+{
+       if (!p1 && !p2) return True;
+
+       if ((!p1 && p2) || (p1 && !p2)) return False;
+
+       /* Compare lists of printer parameters */
+
+       while (p1) {
+               BOOL found = False;
+               NT_PRINTER_PARAM *q = p1;
+
+               /* Find the parameter in the second structure */
+
+               while(q) {
+
+                       if (strequal(p1->value, q->value) &&
+                           p1->type == q->type &&
+                           p1->data_len == q->data_len &&
+                           memcmp(p1->data, q->data, p1->data_len) == 0) {
+                               found = True;
+                               goto found_it;
+                       }
+
+                       q = q->next;
+               }
+
+       found_it:
+               if (!found) {
+                       return False;
+               }
+
+               p1 = p1->next;
+       }
+
+       return True;
+}
+
+/********************************************************************
+ * Called by update_printer when trying to work out whether to
+ * actually update printer info.
+ ********************************************************************/
+
+static BOOL nt_printer_info_level_equal(NT_PRINTER_INFO_LEVEL *p1,
+                                       NT_PRINTER_INFO_LEVEL *p2)
+{
+       NT_PRINTER_INFO_LEVEL_2 *pi1, *pi2;
+
+       /* Trivial conditions */
+
+       if ((!p1 && !p2) || (!p1->info_2 && !p2->info_2)) {
+               return True;
+       }
+
+       if ((!p1 && p2) || (p1 && !p2) || 
+           (!p1->info_2 && p2->info_2) ||
+           (p1->info_2 && !p2->info_2)) {
+               return False;
+       }
+
+       /* Compare two nt_printer_info_level structures.  Don't compare
+          status or cjobs as they seem to have something to do with the
+          printer queue. */
+
+       pi1 = p1->info_2;
+       pi2 = p2->info_2;
+
+       if (pi1->attributes != pi2->attributes ||
+           pi1->priority != pi2->priority ||
+           pi1->default_priority != pi2->default_priority ||
+           pi1->starttime != pi2->starttime ||
+           pi1->untiltime != pi2->untiltime ||
+           pi1->averageppm != pi2->averageppm) {
+               return False;
+       }
 
-       return nt_printing_setsec(Printer->dev.printername, secdesc_ctr);
+       /* Yuck - don't check the printername or servername as the 
+          add_a_printer() code plays games with them.  You can't
+          change the printername or the sharename through this interface
+          in Samba. */
+
+       if (!strequal(pi1->sharename, pi2->sharename) ||
+           !strequal(pi1->portname, pi2->portname) ||
+           !strequal(pi1->drivername, pi2->drivername) ||
+           !strequal(pi1->comment, pi2->comment) ||
+           !strequal(pi1->location, pi2->location)) {
+               return False;
+       }
+
+       if (!nt_devicemode_equal(pi1->devmode, pi2->devmode)) {
+               return False;
+       }
+
+       if (!strequal(pi1->sepfile, pi2->sepfile) ||
+           !strequal(pi1->printprocessor, pi2->printprocessor) ||
+           !strequal(pi1->datatype, pi2->datatype) ||
+           !strequal(pi1->parameters, pi2->parameters)) {
+               return False;
+       }
+
+       if (!nt_printer_param_equal(pi1->specific, pi2->specific)) {
+               return False;
+       }
+
+       if (!sec_desc_equal(pi1->secdesc_buf->sec, pi2->secdesc_buf->sec)) {
+               return False;
+       }
+
+       if (pi1->changeid != pi2->changeid ||
+           pi1->c_setprinter != pi2->c_setprinter ||
+           pi1->setuptime != pi2->setuptime) {
+               return False;
+       }
+
+       return True;
 }
 
 /********************************************************************
@@ -3008,103 +3721,168 @@ static uint32 update_printer_sec(const POLICY_HND *handle, uint32 level,
  * when updating a printer description
  ********************************************************************/
 
-static uint32 update_printer(const POLICY_HND *handle, uint32 level,
+static uint32 update_printer(POLICY_HND *handle, uint32 level,
                            const SPOOL_PRINTER_INFO_LEVEL *info,
                            DEVICEMODE *devmode)
 {
        int snum;
-       NT_PRINTER_INFO_LEVEL *printer = NULL;
+       NT_PRINTER_INFO_LEVEL *printer = NULL, *old_printer = NULL;
        Printer_entry *Printer = find_printer_index_by_hnd(handle);
-       
+       uint32 result;
+
        DEBUG(8,("update_printer\n"));
        
+       result = NT_STATUS_NO_PROBLEMO;
+
        if (level!=2) {
                DEBUG(0,("Send a mail to samba@samba.org\n"));
                DEBUGADD(0,("with the following message: update_printer: level!=2\n"));
-               return ERROR_INVALID_LEVEL;
+               result = ERROR_INVALID_LEVEL;
+               goto done;
        }
 
-       if (!OPEN_HANDLE(Printer))
-               return ERROR_INVALID_HANDLE;
+       if (!OPEN_HANDLE(Printer)) {
+               result = ERROR_INVALID_HANDLE;
+               goto done;
+       }
 
-       if (!get_printer_snum(handle, &snum) )
-               return ERROR_INVALID_HANDLE;
-       
-       get_a_printer(&printer, 2, lp_servicename(snum));
+       if (!get_printer_snum(handle, &snum)) {
+               result = ERROR_INVALID_HANDLE;
+               goto done;
+       }
+
+       if((get_a_printer(&printer, 2, lp_servicename(snum)) != 0) ||
+          (get_a_printer(&old_printer, 2, lp_servicename(snum)) != 0)) {
+               result = ERROR_INVALID_HANDLE;
+               goto done;
+       }
 
        DEBUGADD(8,("Converting info_2 struct\n"));
+
+       /*
+        * convert_printer_info converts the incoming
+        * info from the client and overwrites the info
+        * just read from the tdb in the pointer 'printer'.
+        */
+
        convert_printer_info(info, printer, level);
        
        if (info->info_2->devmode_ptr != 0) {
-               NT_DEVICEMODE *ntdevmode = NULL;
                /* we have a valid devmode
                   convert it and link it*/
+
+               /*
+                * Ensure printer->info_2->devmode is a valid pointer 
+                * as we will be overwriting it in convert_devicemode().
+                */
                
+               if (printer->info_2->devmode == NULL)
+                       printer->info_2->devmode = construct_nt_devicemode(printer->info_2->printername);
+
                DEBUGADD(8,("Converting the devicemode struct\n"));
-               if (printer->info_2->devmode) {
-                       ntdevmode = dup_nt_devicemode(printer->info_2->devmode);
-               } else {
-                       ntdevmode = construct_nt_devicemode();
-               }
-                               
-               convert_devicemode(devmode, ntdevmode);
-               free_nt_devicemode(&ntdevmode);
+               convert_devicemode(devmode, printer->info_2->devmode);
+
        } else {
                if (printer->info_2->devmode != NULL)
                        free_nt_devicemode(&printer->info_2->devmode);
                printer->info_2->devmode=NULL;
        }
-                       
+
+       /* Do sanity check on the requested changes for Samba */
+
+       if (!check_printer_ok(printer->info_2, snum)) {
+               result = ERROR_INVALID_PARAMETER;
+               goto done;
+       }
+
+       /* NT likes to call this function even though nothing has actually
+          changed.  Check this so the user doesn't end up with an
+          annoying permission denied dialog box. */
+
+       if (nt_printer_info_level_equal(printer, old_printer)) {
+               DEBUG(3, ("printer info has not changed\n"));
+               result = NT_STATUS_NO_PROBLEMO;
+               goto done;
+       }
+
+       /* Check calling user has permission to update printer description */ 
+
+       if (!print_access_check(NULL, snum, PRINTER_ACCESS_ADMINISTER)) {
+               DEBUG(3, ("printer property change denied by security "
+                         "descriptor\n"));
+               result = ERROR_ACCESS_DENIED;
+               goto done;
+       }
+
+       /* Call addprinter hook */
+
+       if (*lp_addprinter_cmd() )
+               if ( !add_printer_hook(printer) ) {
+                       result = ERROR_ACCESS_DENIED;
+                       goto done;
+               }
+       
+       /* Update printer info */
+
        if (add_a_printer(*printer, 2)!=0) {
                /* I don't really know what to return here !!! */
-               free_a_printer(&printer, 2);
-               return ERROR_ACCESS_DENIED;
+               result = ERROR_ACCESS_DENIED;
+               goto done;
        }
 
+ done:
        free_a_printer(&printer, 2);
+       free_a_printer(&old_printer, 2);
 
-       return NT_STATUS_NO_PROBLEMO;
+       srv_spoolss_sendnotify(handle);
+
+       return result;
 }
 
 /****************************************************************************
 ****************************************************************************/
-uint32 _spoolss_setprinter(const POLICY_HND *handle, uint32 level,
+uint32 _spoolss_setprinter(POLICY_HND *handle, uint32 level,
                           const SPOOL_PRINTER_INFO_LEVEL *info,
                           DEVMODE_CTR devmode_ctr,
                           SEC_DESC_BUF *secdesc_ctr,
-                          uint32 command)
+                          uint32 command, pipes_struct *p)
 {
        Printer_entry *Printer = find_printer_index_by_hnd(handle);
        
-       if (!OPEN_HANDLE(Printer))
+       if (!OPEN_HANDLE(Printer)) {
+               DEBUG(0,("_spoolss_setprinter: Invalid handle (%s)\n", OUR_HANDLE(handle)));
                return ERROR_INVALID_HANDLE;
+       }
 
        /* check the level */   
        switch (level) {
                case 0:
-                       return control_printer(handle, command);
-                       break;
+                       return control_printer(handle, command, p);
                case 2:
                        return update_printer(handle, level, info, devmode_ctr.devmode);
-                       break;
                case 3:
-                       return update_printer_sec(handle, level, info, secdesc_ctr);
-                       break;
+                       return update_printer_sec(handle, level, info, p,
+                                                 secdesc_ctr);
                default:
                        return ERROR_INVALID_LEVEL;
-                       break;
        }
 }
 
 /****************************************************************************
 ****************************************************************************/
-uint32 _spoolss_fcpn(const POLICY_HND *handle)
+uint32 _spoolss_fcpn(POLICY_HND *handle)
 {
        Printer_entry *Printer= find_printer_index_by_hnd(handle);
        
-       if (!OPEN_HANDLE(Printer))
+       if (!OPEN_HANDLE(Printer)) {
+               DEBUG(0,("_spoolss_fcpn: Invalid handle (%s)\n", OUR_HANDLE(handle)));
                return ERROR_INVALID_HANDLE;
-       
+       }
+
+       if (Printer->notify.client_connected==True)
+               if(!srv_spoolss_replycloseprinter(&Printer->notify.client_hnd))
+                       return ERROR_INVALID_HANDLE;
+
        Printer->notify.flags=0;
        Printer->notify.options=0;
        Printer->notify.localmachine[0]='\0';
@@ -3113,16 +3891,22 @@ uint32 _spoolss_fcpn(const POLICY_HND *handle)
                safe_free(Printer->notify.option->ctr.type);
        safe_free(Printer->notify.option);
        Printer->notify.option=NULL;
-       
+       Printer->notify.client_connected=False;
+
        return NT_STATUS_NO_PROBLEMO;
 }
 
 /****************************************************************************
 ****************************************************************************/
-uint32 _spoolss_addjob(const POLICY_HND *handle, uint32 level,
-                       NEW_BUFFER *buffer, uint32 offered)
+uint32 _spoolss_addjob(POLICY_HND *handle, uint32 level,
+                      NEW_BUFFER *buffer, uint32 offered,
+                      uint32 *needed)
 {
-       return NT_STATUS_NO_PROBLEMO;
+       *needed = 0;
+       return ERROR_INVALID_PARAMETER; /* this is what a NT server
+                                           returns for AddJob. AddJob
+                                           must fail on non-local
+                                           printers */
 }
 
 /****************************************************************************
@@ -3133,25 +3917,24 @@ static void fill_job_info_1(JOB_INFO_1 *job_info, print_queue_struct *queue,
        pstring temp_name;
        
        struct tm *t;
-       time_t unixdate = time(NULL);
        
-       t=gmtime(&unixdate);
+       t=gmtime(&queue->time);
        snprintf(temp_name, sizeof(temp_name), "\\\\%s", global_myname);
 
        job_info->jobid=queue->job;     
-       init_unistr(&(job_info->printername), lp_servicename(snum));
-       init_unistr(&(job_info->machinename), temp_name);
-       init_unistr(&(job_info->username), queue->user);
-       init_unistr(&(job_info->document), queue->file);
-       init_unistr(&(job_info->datatype), "RAW");
-       init_unistr(&(job_info->text_status), "");
+       init_unistr(&job_info->printername, lp_servicename(snum));
+       init_unistr(&job_info->machinename, temp_name);
+       init_unistr(&job_info->username, queue->user);
+       init_unistr(&job_info->document, queue->file);
+       init_unistr(&job_info->datatype, "RAW");
+       init_unistr(&job_info->text_status, "");
        job_info->status=nt_printj_status(queue->status);
        job_info->priority=queue->priority;
        job_info->position=position;
        job_info->totalpages=0;
        job_info->pagesprinted=0;
 
-       make_systemtime(&(job_info->submitted), t);
+       make_systemtime(&job_info->submitted, t);
 }
 
 /****************************************************************************
@@ -3160,33 +3943,31 @@ static BOOL fill_job_info_2(JOB_INFO_2 *job_info, print_queue_struct *queue,
                             int position, int snum)
 {
        pstring temp_name;
-       DEVICEMODE *devmode;
        NT_PRINTER_INFO_LEVEL *ntprinter = NULL;
        pstring chaine;
-
        struct tm *t;
-       time_t unixdate = time(NULL);
 
        if (get_a_printer(&ntprinter, 2, lp_servicename(snum)) !=0 )
                return False;
        
-       t=gmtime(&unixdate);
+       t=gmtime(&queue->time);
        snprintf(temp_name, sizeof(temp_name), "\\\\%s", global_myname);
 
        job_info->jobid=queue->job;
        
        snprintf(chaine, sizeof(chaine)-1, "\\\\%s\\%s", global_myname, ntprinter->info_2->printername);
 
-       init_unistr(&(job_info->printername), chaine);
+       init_unistr(&job_info->printername, chaine);
        
-       init_unistr(&(job_info->machinename), temp_name);
-       init_unistr(&(job_info->username), queue->user);
-       init_unistr(&(job_info->document), queue->file);
-       init_unistr(&(job_info->notifyname), queue->user);
-       init_unistr(&(job_info->datatype), "RAW");
-       init_unistr(&(job_info->printprocessor), "winprint");
-       init_unistr(&(job_info->parameters), "");
-       init_unistr(&(job_info->text_status), "");
+       init_unistr(&job_info->machinename, temp_name);
+       init_unistr(&job_info->username, queue->user);
+       init_unistr(&job_info->document, queue->file);
+       init_unistr(&job_info->notifyname, queue->user);
+       init_unistr(&job_info->datatype, "RAW");
+       init_unistr(&job_info->printprocessor, "winprint");
+       init_unistr(&job_info->parameters, "");
+       init_unistr(&job_info->drivername, ntprinter->info_2->drivername);
+       init_unistr(&job_info->text_status, "");
        
 /* and here the security descriptor */
 
@@ -3206,7 +3987,6 @@ static BOOL fill_job_info_2(JOB_INFO_2 *job_info, print_queue_struct *queue,
                return False;
        }
 
-       job_info->devmode=devmode;
        free_a_printer(&ntprinter, 2);
        return (True);
 }
@@ -3229,13 +4009,13 @@ static uint32 enumjobs_level1(print_queue_struct *queue, int snum,
        }
        
        for (i=0; i<*returned; i++)
-               fill_job_info_1(&(info[i]), &(queue[i]), i, snum);
+               fill_job_info_1(&info[i], &queue[i], i, snum);
 
        safe_free(queue);
 
        /* check the required size. */  
        for (i=0; i<*returned; i++)
-               (*needed) += spoolss_size_job_info_1(&(info[i]));
+               (*needed) += spoolss_size_job_info_1(&info[i]);
 
        if (!alloc_buffer_size(buffer, *needed)) {
                safe_free(info);
@@ -3244,7 +4024,7 @@ static uint32 enumjobs_level1(print_queue_struct *queue, int snum,
 
        /* fill the buffer with the structures */
        for (i=0; i<*returned; i++)
-               new_smb_io_job_info_1("", buffer, &(info[i]), 0);       
+               new_smb_io_job_info_1("", buffer, &info[i], 0); 
 
        /* clear memory */
        safe_free(info);
@@ -3275,13 +4055,13 @@ static uint32 enumjobs_level2(print_queue_struct *queue, int snum,
        }
        
        for (i=0; i<*returned; i++)
-               fill_job_info_2(&(info[i]), &(queue[i]), i, snum);
+               fill_job_info_2(&(info[i]), &queue[i], i, snum);
 
        safe_free(queue);
 
        /* check the required size. */  
        for (i=0; i<*returned; i++)
-               (*needed) += spoolss_size_job_info_2(&(info[i]));
+               (*needed) += spoolss_size_job_info_2(&info[i]);
 
        if (!alloc_buffer_size(buffer, *needed)) {
                safe_free(info);
@@ -3290,7 +4070,7 @@ static uint32 enumjobs_level2(print_queue_struct *queue, int snum,
 
        /* fill the buffer with the structures */
        for (i=0; i<*returned; i++)
-               new_smb_io_job_info_2("", buffer, &(info[i]), 0);       
+               new_smb_io_job_info_2("", buffer, &info[i], 0); 
 
        /* clear memory */
        safe_free(info);
@@ -3327,40 +4107,39 @@ uint32 _spoolss_enumjobs( POLICY_HND *handle, uint32 firstjob, uint32 numofjobs,
        *returned = print_queue_status(snum, &queue, &prt_status);
        DEBUGADD(4,("count:[%d], status:[%d], [%s]\n", *returned, prt_status.status, prt_status.message));
 
+       if (*returned == 0) {
+               safe_free(queue);
+               return NT_STATUS_NO_PROBLEMO;
+       }
+
        switch (level) {
        case 1:
                return enumjobs_level1(queue, snum, buffer, offered, needed, returned);
-               break;
        case 2:
                return enumjobs_level2(queue, snum, buffer, offered, needed, returned);
-               break;                          
        default:
                safe_free(queue);
                *returned=0;
                return ERROR_INVALID_LEVEL;
-               break;
        }
 }
 
 
 /****************************************************************************
 ****************************************************************************/
-uint32 _spoolss_schedulejob( const POLICY_HND *handle, uint32 jobid)
+uint32 _spoolss_schedulejob( POLICY_HND *handle, uint32 jobid)
 {
        return 0x0;
 }
 
 /****************************************************************************
 ****************************************************************************/
-uint32 _spoolss_setjob( const POLICY_HND *handle,
-                               uint32 jobid,
-                               uint32 level,
-                               JOB_INFO *ctr,
-                               uint32 command)
-
+uint32 _spoolss_setjob(POLICY_HND *handle, uint32 jobid, uint32 level,
+                      pipes_struct *p, JOB_INFO *ctr, uint32 command)
 {
-       int snum;
+       struct current_user user;
        print_status_struct prt_status;
+       int snum, errcode = ERROR_INVALID_FUNCTION;
                
        memset(&prt_status, 0, sizeof(prt_status));
 
@@ -3371,50 +4150,86 @@ uint32 _spoolss_setjob( const POLICY_HND *handle,
        if (!print_job_exists(jobid)) {
                return ERROR_INVALID_PRINTER_NAME;
        }
-       
+
+       get_current_user(&user, p);     
+
        switch (command) {
        case JOB_CONTROL_CANCEL:
        case JOB_CONTROL_DELETE:
-               if (print_job_delete(jobid)) return 0x0;
+               if (print_job_delete(&user, jobid, &errcode)) {
+                       errcode = 0;
+               }
                break;
        case JOB_CONTROL_PAUSE:
-               if (print_job_pause(jobid)) return 0x0;
+               if (print_job_pause(&user, jobid, &errcode)) {
+                       errcode = 0;
+               }               
                break;
+       case JOB_CONTROL_RESTART:
        case JOB_CONTROL_RESUME:
-               if (print_job_resume(jobid)) return 0x0;
+               if (print_job_resume(&user, jobid, &errcode)) {
+                       errcode = 0;
+               }
                break;
        default:
                return ERROR_INVALID_LEVEL;
        }
 
-       return ERROR_INVALID_HANDLE;
+       return errcode;
 }
 
 /****************************************************************************
  Enumerates all printer drivers at level 1.
 ****************************************************************************/
-static uint32 enumprinterdrivers_level1(fstring *list, fstring servername, fstring architecture, NEW_BUFFER *buffer, uint32 offered, uint32 *needed, uint32 *returned)
+static uint32 enumprinterdrivers_level1(fstring servername, fstring architecture, NEW_BUFFER *buffer, uint32 offered, uint32 *needed, uint32 *returned)
 {
        int i;
+       int ndrivers;
+       uint32 version;
+       fstring *list = NULL;
+
        NT_PRINTER_DRIVER_INFO_LEVEL driver;
        DRIVER_INFO_1 *driver_info_1=NULL;
 
-       ZERO_STRUCT(driver);
+       *returned=0;
 
-       if((driver_info_1=(DRIVER_INFO_1 *)malloc(*returned * sizeof(DRIVER_INFO_1))) == NULL)
-               return ERROR_NOT_ENOUGH_MEMORY;
+#define MAX_VERSION 4
 
-       for (i=0; i<*returned; i++) {
-               get_a_printer_driver(&driver, 3, list[i], architecture);
-               fill_printer_driver_info_1(&(driver_info_1[i]), driver, servername, architecture );
+       for (version=0; version<MAX_VERSION; version++) {
+               list=NULL;
+               ndrivers=get_ntdrivers(&list, architecture, version);
+               DEBUGADD(4,("we have:[%d] drivers in environment [%s] and version [%d]\n", ndrivers, architecture, version));
+
+               if(ndrivers == -1)
+                       return ERROR_NOT_ENOUGH_MEMORY;
+
+               if(ndrivers != 0) {
+                       if((driver_info_1=(DRIVER_INFO_1 *)Realloc(driver_info_1, (*returned+ndrivers) * sizeof(DRIVER_INFO_1))) == NULL) {
+                               safe_free(list);
+                               return ERROR_NOT_ENOUGH_MEMORY;
+                       }
+               }
+
+               for (i=0; i<ndrivers; i++) {
+                       uint32 status;
+                       DEBUGADD(5,("\tdriver: [%s]\n", list[i]));
+                       ZERO_STRUCT(driver);
+                       if ((status = get_a_printer_driver(&driver, 3, list[i], architecture, version)) != 0) {
+                               safe_free(list);
+                               return status;
+                       }
+                       fill_printer_driver_info_1(&driver_info_1[*returned+i], driver, servername, architecture );             
+                       free_a_printer_driver(driver, 3);
+               }       
+
+               *returned+=ndrivers;
+               safe_free(list);
        }
        
-       safe_free(list);
-       
        /* check the required size. */
        for (i=0; i<*returned; i++) {
                DEBUGADD(6,("adding driver [%d]'s size\n",i));
-               *needed += spoolss_size_printer_driver_info_1(&(driver_info_1[i]));
+               *needed += spoolss_size_printer_driver_info_1(&driver_info_1[i]);
        }
 
        if (!alloc_buffer_size(buffer, *needed)) {
@@ -3425,7 +4240,7 @@ static uint32 enumprinterdrivers_level1(fstring *list, fstring servername, fstri
        /* fill the buffer with the form structures */
        for (i=0; i<*returned; i++) {
                DEBUGADD(6,("adding driver [%d] to buffer\n",i));
-               new_smb_io_printer_driver_info_1("", buffer, &(driver_info_1[i]), 0);
+               new_smb_io_printer_driver_info_1("", buffer, &driver_info_1[i], 0);
        }
 
        safe_free(driver_info_1);
@@ -3441,28 +4256,52 @@ static uint32 enumprinterdrivers_level1(fstring *list, fstring servername, fstri
 /****************************************************************************
  Enumerates all printer drivers at level 2.
 ****************************************************************************/
-static uint32 enumprinterdrivers_level2(fstring *list, fstring servername, fstring architecture, NEW_BUFFER *buffer, uint32 offered, uint32 *needed, uint32 *returned)
+static uint32 enumprinterdrivers_level2(fstring servername, fstring architecture, NEW_BUFFER *buffer, uint32 offered, uint32 *needed, uint32 *returned)
 {
        int i;
+       int ndrivers;
+       uint32 version;
+       fstring *list = NULL;
+
+       NT_PRINTER_DRIVER_INFO_LEVEL driver;
        DRIVER_INFO_2 *driver_info_2=NULL;
 
-       if (*returned > 0 && 
-           !(driver_info_2=(DRIVER_INFO_2 *)malloc(*returned * sizeof(DRIVER_INFO_2))))
-               return ERROR_NOT_ENOUGH_MEMORY;
+       *returned=0;
 
-       for (i=0; i<*returned; i++) {
-               NT_PRINTER_DRIVER_INFO_LEVEL driver;
-               ZERO_STRUCT(driver);
-               if (get_a_printer_driver(&driver, 3, list[i], architecture)
-                   != 0) { 
-                       *returned = i;
-                       break;
+#define MAX_VERSION 4
+
+       for (version=0; version<MAX_VERSION; version++) {
+               list=NULL;
+               ndrivers=get_ntdrivers(&list, architecture, version);
+               DEBUGADD(4,("we have:[%d] drivers in environment [%s] and version [%d]\n", ndrivers, architecture, version));
+
+               if(ndrivers == -1)
+                       return ERROR_NOT_ENOUGH_MEMORY;
+
+               if(ndrivers != 0) {
+                       if((driver_info_2=(DRIVER_INFO_2 *)Realloc(driver_info_2, (*returned+ndrivers) * sizeof(DRIVER_INFO_2))) == NULL) {
+                               safe_free(list);
+                               return ERROR_NOT_ENOUGH_MEMORY;
+                       }
                }
-               fill_printer_driver_info_2(&(driver_info_2[i]), driver, servername, architecture );
+               
+               for (i=0; i<ndrivers; i++) {
+                       uint32 status;
+
+                       DEBUGADD(5,("\tdriver: [%s]\n", list[i]));
+                       ZERO_STRUCT(driver);
+                       if ((status = get_a_printer_driver(&driver, 3, list[i], architecture, version)) != 0) {
+                               safe_free(list);
+                               return status;
+                       }
+                       fill_printer_driver_info_2(&driver_info_2[*returned+i], driver, servername);            
+                       free_a_printer_driver(driver, 3);
+               }       
+
+               *returned+=ndrivers;
+               safe_free(list);
        }
        
-       safe_free(list);
-       
        /* check the required size. */
        for (i=0; i<*returned; i++) {
                DEBUGADD(6,("adding driver [%d]'s size\n",i));
@@ -3493,28 +4332,56 @@ static uint32 enumprinterdrivers_level2(fstring *list, fstring servername, fstri
 /****************************************************************************
  Enumerates all printer drivers at level 3.
 ****************************************************************************/
-static uint32 enumprinterdrivers_level3(fstring *list, fstring servername, fstring architecture, NEW_BUFFER *buffer, uint32 offered, uint32 *needed, uint32 *returned)
+static uint32 enumprinterdrivers_level3(fstring servername, fstring architecture, NEW_BUFFER *buffer, uint32 offered, uint32 *needed, uint32 *returned)
 {
        int i;
+       int ndrivers;
+       uint32 version;
+       fstring *list = NULL;
+
        NT_PRINTER_DRIVER_INFO_LEVEL driver;
        DRIVER_INFO_3 *driver_info_3=NULL;
 
-       ZERO_STRUCT(driver);
+       *returned=0;
 
-       if((driver_info_3=(DRIVER_INFO_3 *)malloc((*returned)*sizeof(DRIVER_INFO_3))) == NULL)
-               return ERROR_NOT_ENOUGH_MEMORY;
+#define MAX_VERSION 4
 
-       for (i=0; i<*returned; i++) {
-               get_a_printer_driver(&driver, 3, list[i], architecture);
-               fill_printer_driver_info_3(&(driver_info_3[i]), driver, servername, architecture );
+       for (version=0; version<MAX_VERSION; version++) {
+               list=NULL;
+               ndrivers=get_ntdrivers(&list, architecture, version);
+               DEBUGADD(4,("we have:[%d] drivers in environment [%s] and version [%d]\n", ndrivers, architecture, version));
+
+               if(ndrivers == -1)
+                       return ERROR_NOT_ENOUGH_MEMORY;
+
+               if(ndrivers != 0) {
+                       if((driver_info_3=(DRIVER_INFO_3 *)Realloc(driver_info_3, (*returned+ndrivers) * sizeof(DRIVER_INFO_3))) == NULL) {
+                               safe_free(list);
+                               return ERROR_NOT_ENOUGH_MEMORY;
+                       }
+               }
+
+               for (i=0; i<ndrivers; i++) {
+                       uint32 status;
+
+                       DEBUGADD(5,("\tdriver: [%s]\n", list[i]));
+                       ZERO_STRUCT(driver);
+                       if ((status = get_a_printer_driver(&driver, 3, list[i], architecture, version)) != 0) {
+                               safe_free(list);
+                               return status;
+                       }
+                       fill_printer_driver_info_3(&driver_info_3[*returned+i], driver, servername);            
+                       free_a_printer_driver(driver, 3);
+               }       
+
+               *returned+=ndrivers;
+               safe_free(list);
        }
-       
-       safe_free(list);
-       
+
        /* check the required size. */
        for (i=0; i<*returned; i++) {
                DEBUGADD(6,("adding driver [%d]'s size\n",i));
-               *needed += spoolss_size_printer_driver_info_3(&(driver_info_3[i]));
+               *needed += spoolss_size_printer_driver_info_3(&driver_info_3[i]);
        }
 
        if (!alloc_buffer_size(buffer, *needed)) {
@@ -3522,10 +4389,10 @@ static uint32 enumprinterdrivers_level3(fstring *list, fstring servername, fstri
                return ERROR_INSUFFICIENT_BUFFER;
        }
        
-       /* fill the buffer with the form structures */
+       /* fill the buffer with the driver structures */
        for (i=0; i<*returned; i++) {
-               DEBUGADD(6,("adding form [%d] to buffer\n",i));
-               new_smb_io_printer_driver_info_3("", buffer, &(driver_info_3[i]), 0);
+               DEBUGADD(6,("adding driver [%d] to buffer\n",i));
+               new_smb_io_printer_driver_info_3("", buffer, &driver_info_3[i], 0);
        }
 
        for (i=0; i<*returned; i++)
@@ -3548,7 +4415,6 @@ uint32 _spoolss_enumprinterdrivers( UNISTR2 *name, UNISTR2 *environment, uint32
                                    NEW_BUFFER *buffer, uint32 offered,
                                    uint32 *needed, uint32 *returned)
 {
-       int i;
        fstring *list = NULL;
        fstring servername;
        fstring architecture;
@@ -3559,37 +4425,27 @@ uint32 _spoolss_enumprinterdrivers( UNISTR2 *name, UNISTR2 *environment, uint32
        *returned=0;
 
        unistr2_to_ascii(architecture, environment, sizeof(architecture)-1);
-       *returned=get_ntdrivers(&list, architecture);
 
-       DEBUGADD(4,("we have: [%d] drivers in environment [%s]\n", *returned, architecture));
-       if(*returned == -1)
-               return ERROR_NOT_ENOUGH_MEMORY;
-
-       for (i=0; i<*returned; i++)
-               DEBUGADD(5,("driver: [%s]\n", list[i]));
-       
        switch (level) {
        case 1:
-               return enumprinterdrivers_level1(list, servername, architecture, buffer, offered, needed, returned);
-               break;
+               return enumprinterdrivers_level1(servername, architecture, buffer, offered, needed, returned);
        case 2:
-               return enumprinterdrivers_level2(list, servername, architecture, buffer, offered, needed, returned);
-               break;
+               return enumprinterdrivers_level2(servername, architecture, buffer, offered, needed, returned);
        case 3:
-               return enumprinterdrivers_level3(list, servername, architecture, buffer, offered, needed, returned);
-               break;
+               return enumprinterdrivers_level3(servername, architecture, buffer, offered, needed, returned);
        default:
+               *returned=0;
+               safe_free(list);
                return ERROR_INVALID_LEVEL;
-               break;
        }
 }
 
 /****************************************************************************
 ****************************************************************************/
-static void fill_form_1(FORM_1 *form, nt_forms_struct *list, int position)
+static void fill_form_1(FORM_1 *form, nt_forms_struct *list)
 {
        form->flag=list->flag;
-       init_unistr(&(form->name), list->name);
+       init_unistr(&form->name, list->name);
        form->width=list->width;
        form->length=list->length;
        form->left=list->left;
@@ -3600,7 +4456,7 @@ static void fill_form_1(FORM_1 *form, nt_forms_struct *list, int position)
        
 /****************************************************************************
 ****************************************************************************/
-uint32 _new_spoolss_enumforms( const POLICY_HND *handle, uint32 level, 
+uint32 _new_spoolss_enumforms( POLICY_HND *handle, uint32 level, 
                               NEW_BUFFER *buffer, uint32 offered, 
                               uint32 *needed, uint32 *numofforms)
 {
@@ -3616,72 +4472,135 @@ uint32 _new_spoolss_enumforms( const POLICY_HND *handle, uint32 level,
        *numofforms = get_ntforms(&list);
        DEBUGADD(5,("Number of forms [%d]\n",     *numofforms));
 
-       if (*numofforms == 0) return ERROR_NO_MORE_ITEMS;
+       if (*numofforms == 0) return ERROR_NO_MORE_ITEMS;
+
+       switch (level) {
+       case 1:
+               if ((forms_1=(FORM_1 *)malloc(*numofforms * sizeof(FORM_1))) == NULL) {
+                       *numofforms=0;
+                       return ERROR_NOT_ENOUGH_MEMORY;
+               }
+
+               /* construct the list of form structures */
+               for (i=0; i<*numofforms; i++) {
+                       DEBUGADD(6,("Filling form number [%d]\n",i));
+                       fill_form_1(&forms_1[i], &list[i]);
+               }
+               
+               safe_free(list);
+
+               /* check the required size. */
+               for (i=0; i<*numofforms; i++) {
+                       DEBUGADD(6,("adding form [%d]'s size\n",i));
+                       buffer_size += spoolss_size_form_1(&forms_1[i]);
+               }
+
+               *needed=buffer_size;            
+               
+               if (!alloc_buffer_size(buffer, buffer_size)){
+                       safe_free(forms_1);
+                       return ERROR_INSUFFICIENT_BUFFER;
+               }
+
+               /* fill the buffer with the form structures */
+               for (i=0; i<*numofforms; i++) {
+                       DEBUGADD(6,("adding form [%d] to buffer\n",i));
+                       new_smb_io_form_1("", buffer, &forms_1[i], 0);
+               }
+
+               safe_free(forms_1);
+
+               if (*needed > offered) {
+                       *numofforms=0;
+                       return ERROR_INSUFFICIENT_BUFFER;
+               }
+               else
+                       return NT_STATUS_NO_PROBLEMO;
+                       
+       default:
+               safe_free(list);
+               return ERROR_INVALID_LEVEL;
+       }
+
+}
+
+/****************************************************************************
+****************************************************************************/
+uint32 _spoolss_getform( POLICY_HND *handle, uint32 level, UNISTR2 *uni_formname, NEW_BUFFER *buffer, uint32 offered, uint32 *needed)
+{
+       nt_forms_struct *list=NULL;
+       FORM_1 form_1;
+       fstring form_name;
+       int buffer_size=0;
+       int numofforms, i;
+
+       unistr2_to_ascii(form_name, uni_formname, sizeof(form_name)-1);
+
+       DEBUG(4,("_spoolss_getform\n"));
+       DEBUGADD(5,("Offered buffer size [%d]\n", offered));
+       DEBUGADD(5,("Info level [%d]\n",          level));
+
+       numofforms = get_ntforms(&list);
+       DEBUGADD(5,("Number of forms [%d]\n",     numofforms));
+
+       if (numofforms == 0)
+               return ERROR_NO_MORE_ITEMS;
 
        switch (level) {
        case 1:
-               if ((forms_1=(FORM_1 *)malloc(*numofforms * sizeof(FORM_1))) == NULL) {
-                       *numofforms=0;
-                       return ERROR_NOT_ENOUGH_MEMORY;
-               }
 
-               /* construct the list of form structures */
-               for (i=0; i<*numofforms; i++) {
-                       DEBUGADD(6,("Filling form number [%d]\n",i));
-                       fill_form_1(&(forms_1[i]), &(list[i]), i);
+               /* Check if the requested name is in the list of form structures */
+               for (i=0; i<numofforms; i++) {
+
+                       DEBUG(4,("_spoolss_getform: checking form %s (want %s)\n", list[i].name, form_name));
+
+                       if (strequal(form_name, list[i].name)) {
+                               DEBUGADD(6,("Found form %s number [%d]\n", form_name, i));
+                               fill_form_1(&form_1, &list[i]);
+                               break;
+                       }
                }
                
                safe_free(list);
 
                /* check the required size. */
-               for (i=0; i<*numofforms; i++) {
-                       DEBUGADD(6,("adding form [%d]'s size\n",i));
-                       buffer_size += spoolss_size_form_1(&(forms_1[i]));
-               }
 
-               *needed=buffer_size;            
+               *needed=spoolss_size_form_1(&form_1);
                
                if (!alloc_buffer_size(buffer, buffer_size)){
-                       safe_free(forms_1);
                        return ERROR_INSUFFICIENT_BUFFER;
                }
 
-               /* fill the buffer with the form structures */
-               for (i=0; i<*numofforms; i++) {
-                       DEBUGADD(6,("adding form [%d] to buffer\n",i));
-                       new_smb_io_form_1("", buffer, &(forms_1[i]), 0);
-               }
-
-               safe_free(forms_1);
-
                if (*needed > offered) {
-                       *numofforms=0;
                        return ERROR_INSUFFICIENT_BUFFER;
                }
-               else
-                       return NT_STATUS_NO_PROBLEMO;
+
+               /* fill the buffer with the form structures */
+               DEBUGADD(6,("adding form %s [%d] to buffer\n", form_name, i));
+               new_smb_io_form_1("", buffer, &form_1, 0);
+
+               return NT_STATUS_NO_PROBLEMO;
                        
        default:
                safe_free(list);
                return ERROR_INVALID_LEVEL;
        }
-
 }
 
 /****************************************************************************
 ****************************************************************************/
 static void fill_port_1(PORT_INFO_1 *port, char *name)
 {
-       init_unistr(&(port->port_name), name);
+       init_unistr(&port->port_name, name);
 }
 
 /****************************************************************************
 ****************************************************************************/
 static void fill_port_2(PORT_INFO_2 *port, char *name)
 {
-       init_unistr(&(port->port_name), name);
-       init_unistr(&(port->monitor_name), "Moniteur Local");
-       init_unistr(&(port->description), "Local Port");
+       init_unistr(&port->port_name, name);
+       init_unistr(&port->monitor_name, "Local Monitor");
+       init_unistr(&port->description, "Local Port");
 #define PORT_TYPE_WRITE 1
        port->port_type=PORT_TYPE_WRITE;
        port->reserved=0x0;     
@@ -3692,31 +4611,75 @@ static void fill_port_2(PORT_INFO_2 *port, char *name)
 ****************************************************************************/
 static uint32 enumports_level_1(NEW_BUFFER *buffer, uint32 offered, uint32 *needed, uint32 *returned)
 {
-       int n_services=lp_numservices();
-       int snum;
-       int i=0;
-       
        PORT_INFO_1 *ports=NULL;
+       int i=0;
+
+       if (*lp_enumports_cmd()) {
+               pid_t local_pid = sys_getpid();
+               char *cmd = lp_enumports_cmd();
+               char *path;
+               char **qlines;
+               pstring tmp_file;
+               pstring command;
+               int numlines;
+               int ret;
+
+               if (*lp_pathname(lp_servicenumber(PRINTERS_NAME)))
+                       path = lp_pathname(lp_servicenumber(PRINTERS_NAME));
+               else
+                       path = tmpdir();
+
+               slprintf(tmp_file, sizeof(tmp_file), "%s/smbcmd.%d", path, local_pid);
+               slprintf(command, sizeof(command), "%s \"%d\"", cmd, 1);
+
+               unlink(tmp_file);
+               DEBUG(10,("Running [%s > %s]\n", command,tmp_file));
+               ret = smbrun(command, tmp_file, False);
+               DEBUG(10,("Returned [%d]\n", ret));
+               if (ret != 0) {
+                       unlink(tmp_file);
+                       /* Is this the best error to return here? */
+                       return ERROR_ACCESS_DENIED;
+               }
 
-       for (snum=0; snum<n_services; snum++)
-               if ( lp_browseable(snum) && lp_snum_ok(snum) && lp_print_ok(snum) )
-                       (*returned)++;
+               numlines = 0;
+               qlines = file_lines_load(tmp_file, &numlines);
+               DEBUGADD(10,("Lines returned = [%d]\n", numlines));
+               DEBUGADD(10,("Unlinking port file [%s]\n", tmp_file));
+               unlink(tmp_file);
 
-       if((ports=(PORT_INFO_1 *)malloc( (*returned+1) * sizeof(PORT_INFO_1) )) == NULL)
-               return ERROR_NOT_ENOUGH_MEMORY;
-       
-       for (snum=0; snum<n_services; snum++) {
-               if ( lp_browseable(snum) && lp_snum_ok(snum) && lp_print_ok(snum) ) {
-                       DEBUGADD(6,("Filling port number [%d]\n", i));
-                       fill_port_1(&(ports[i]), lp_servicename(snum));
-                       i++;
+               if(numlines) {
+                       if((ports=(PORT_INFO_1 *)malloc( numlines * sizeof(PORT_INFO_1) )) == NULL) {
+                               DEBUG(10,("Returning ERROR_NOT_ENOUGH_MEMORY [%x]\n", ERROR_NOT_ENOUGH_MEMORY));
+                               file_lines_free(qlines);
+                               return ERROR_NOT_ENOUGH_MEMORY;
+                       }
+
+                       for (i=0; i<numlines; i++) {
+                               DEBUG(6,("Filling port number [%d] with port [%s]\n", i, qlines[i]));
+                               fill_port_1(&ports[i], qlines[i]);
+                       }
+
+                       file_lines_free(qlines);
                }
+
+               *returned = numlines;
+
+       } else {
+               *returned = 1; /* Sole Samba port returned. */
+
+               if((ports=(PORT_INFO_1 *)malloc( sizeof(PORT_INFO_1) )) == NULL)
+                       return ERROR_NOT_ENOUGH_MEMORY;
+       
+               DEBUG(10,("enumports_level_1: port name %s\n", SAMBA_PRINTER_PORT_NAME));
+
+               fill_port_1(&ports[0], SAMBA_PRINTER_PORT_NAME);
        }
 
        /* check the required size. */
        for (i=0; i<*returned; i++) {
                DEBUGADD(6,("adding port [%d]'s size\n", i));
-               *needed += spoolss_size_port_info_1(&(ports[i]));
+               *needed += spoolss_size_port_info_1(&ports[i]);
        }
                
        if (!alloc_buffer_size(buffer, *needed)) {
@@ -3727,7 +4690,7 @@ static uint32 enumports_level_1(NEW_BUFFER *buffer, uint32 offered, uint32 *need
        /* fill the buffer with the ports structures */
        for (i=0; i<*returned; i++) {
                DEBUGADD(6,("adding port [%d] to buffer\n", i));
-               new_smb_io_port_1("", buffer, &(ports[i]), 0);
+               new_smb_io_port_1("", buffer, &ports[i], 0);
        }
 
        safe_free(ports);
@@ -3740,37 +4703,82 @@ static uint32 enumports_level_1(NEW_BUFFER *buffer, uint32 offered, uint32 *need
                return NT_STATUS_NO_PROBLEMO;
 }
 
-
 /****************************************************************************
  enumports level 2.
 ****************************************************************************/
+
 static uint32 enumports_level_2(NEW_BUFFER *buffer, uint32 offered, uint32 *needed, uint32 *returned)
 {
-       int n_services=lp_numservices();
-       int snum;
-       int i=0;
-       
        PORT_INFO_2 *ports=NULL;
+       int i=0;
+
+       if (*lp_enumports_cmd()) {
+               pid_t local_pid = sys_getpid();
+               char *cmd = lp_enumports_cmd();
+               char *path;
+               char **qlines;
+               pstring tmp_file;
+               pstring command;
+               int numlines;
+               int ret;
+
+               if (*lp_pathname(lp_servicenumber(PRINTERS_NAME)))
+                       path = lp_pathname(lp_servicenumber(PRINTERS_NAME));
+               else
+                       path = tmpdir();
+
+               slprintf(tmp_file, sizeof(tmp_file), "%s/smbcmd.%d", path, local_pid);
+               slprintf(command, sizeof(command), "%s \"%d\"", cmd, 2);
+
+               unlink(tmp_file);
+               DEBUG(10,("Running [%s > %s]\n", command,tmp_file));
+               ret = smbrun(command, tmp_file, False);
+               DEBUGADD(10,("returned [%d]\n", ret));
+               if (ret != 0) {
+                       unlink(tmp_file);
+                       /* Is this the best error to return here? */
+                       return ERROR_ACCESS_DENIED;
+               }
 
-       for (snum=0; snum<n_services; snum++)
-               if ( lp_browseable(snum) && lp_snum_ok(snum) && lp_print_ok(snum) )
-                       (*returned)++;
+               numlines = 0;
+               qlines = file_lines_load(tmp_file, &numlines);
+               DEBUGADD(10,("Lines returned = [%d]\n", numlines));
+               DEBUGADD(10,("Unlinking port file [%s]\n", tmp_file));
+               unlink(tmp_file);
 
-       if((ports=(PORT_INFO_2 *)malloc( (*returned+1) * sizeof(PORT_INFO_2) )) == NULL)
-               return ERROR_NOT_ENOUGH_MEMORY;
-       
-       for (snum=0; snum<n_services; snum++) {
-               if ( lp_browseable(snum) && lp_snum_ok(snum) && lp_print_ok(snum) ) {
-                       DEBUGADD(6,("Filling port number [%d]\n", i));
-                       fill_port_2(&(ports[i]), lp_servicename(snum));
-                       i++;
+               if(numlines) {
+                       if((ports=(PORT_INFO_2 *)malloc( numlines * sizeof(PORT_INFO_2) )) == NULL) {
+                               DEBUG(10,("Returning ERROR_NOT_ENOUGH_MEMORY [%x]\n", ERROR_NOT_ENOUGH_MEMORY));
+                               file_lines_free(qlines);
+                               return ERROR_NOT_ENOUGH_MEMORY;
+                       }
+
+                       for (i=0; i<numlines; i++) {
+                               DEBUG(6,("Filling port number [%d] with port [%s]\n", i, qlines[i]));
+                               fill_port_2(&(ports[i]), qlines[i]);
+                       }
+
+                       file_lines_free(qlines);
                }
+
+               *returned = numlines;
+
+       } else {
+
+               *returned = 1;
+
+               if((ports=(PORT_INFO_2 *)malloc( sizeof(PORT_INFO_2) )) == NULL)
+                       return ERROR_NOT_ENOUGH_MEMORY;
+       
+               DEBUG(10,("enumports_level_2: port name %s\n", SAMBA_PRINTER_PORT_NAME));
+
+               fill_port_2(&ports[0], SAMBA_PRINTER_PORT_NAME);
        }
 
        /* check the required size. */
        for (i=0; i<*returned; i++) {
                DEBUGADD(6,("adding port [%d]'s size\n", i));
-               *needed += spoolss_size_port_info_2(&(ports[i]));
+               *needed += spoolss_size_port_info_2(&ports[i]);
        }
                
        if (!alloc_buffer_size(buffer, *needed)) {
@@ -3781,7 +4789,7 @@ static uint32 enumports_level_2(NEW_BUFFER *buffer, uint32 offered, uint32 *need
        /* fill the buffer with the ports structures */
        for (i=0; i<*returned; i++) {
                DEBUGADD(6,("adding port [%d] to buffer\n", i));
-               new_smb_io_port_2("", buffer, &(ports[i]), 0);
+               new_smb_io_port_2("", buffer, &ports[i], 0);
        }
 
        safe_free(ports);
@@ -3809,13 +4817,10 @@ uint32 _spoolss_enumports( UNISTR2 *name, uint32 level,
        switch (level) {
        case 1:
                return enumports_level_1(buffer, offered, needed, returned);
-               break;
        case 2:
                return enumports_level_2(buffer, offered, needed, returned);
-               break;
        default:
                return ERROR_INVALID_LEVEL;
-               break;
        }
 }
 
@@ -3829,7 +4834,7 @@ static uint32 spoolss_addprinterex_level_2( const UNISTR2 *uni_srv_name,
 {
        NT_PRINTER_INFO_LEVEL *printer = NULL;
        fstring name;
-       fstring share_name;
+       int snum;
 
        if ((printer = (NT_PRINTER_INFO_LEVEL *)malloc(sizeof(NT_PRINTER_INFO_LEVEL))) == NULL) {
                DEBUG(0,("spoolss_addprinterex_level_2: malloc fail.\n"));
@@ -3838,38 +4843,55 @@ static uint32 spoolss_addprinterex_level_2( const UNISTR2 *uni_srv_name,
 
        ZERO_STRUCTP(printer);
 
-       clear_handle(handle);
-       
        /* convert from UNICODE to ASCII - this allocates the info_2 struct inside *printer.*/
        convert_printer_info(info, printer, 2);
 
-       unistr2_to_ascii(share_name, &info->info_2->printername, sizeof(share_name)-1);
-       
-       slprintf(name, sizeof(name)-1, "\\\\%s\\%s", global_myname, share_name);
+       if (*lp_addprinter_cmd() )
+               if ( !add_printer_hook(printer) ) {
+                       free_a_printer(&printer,2);
+                       return ERROR_ACCESS_DENIED;
+       }
 
-       /* write the ASCII on disk */
-       if (add_a_printer(*printer, 2) != 0) {
+       slprintf(name, sizeof(name)-1, "\\\\%s\\%s", global_myname,
+             printer->info_2->sharename);
+
+       if ((snum = print_queue_snum(printer->info_2->sharename)) == -1) {
                free_a_printer(&printer,2);
                return ERROR_ACCESS_DENIED;
        }
 
-       create_printer_hnd(handle);
+       /* you must be a printer admin to add a new printer */
+       if (!print_access_check(NULL, snum, PRINTER_ACCESS_ADMINISTER)) {
+               free_a_printer(&printer,2);
+               return ERROR_ACCESS_DENIED;             
+       }
+       
+       /*
+        * Do sanity check on the requested changes for Samba.
+        */
 
-       open_printer_hnd(handle);
+       if (!check_printer_ok(printer->info_2, snum)) {
+               free_a_printer(&printer,2);
+               return ERROR_INVALID_PARAMETER;
+       }
 
-       if (!set_printer_hnd_printertype(handle, name)) {
+       /* write the ASCII on disk */
+       if (add_a_printer(*printer, 2) != 0) {
                free_a_printer(&printer,2);
-               close_printer_handle(handle);
                return ERROR_ACCESS_DENIED;
        }
 
-       if (!set_printer_hnd_printername(handle, name)) {
+       if (!open_printer_hnd(handle, name)) {
+               /* Handle open failed - remove addition. */
+               del_a_printer(printer->info_2->sharename);
                free_a_printer(&printer,2);
-               close_printer_handle(handle);
                return ERROR_ACCESS_DENIED;
        }
 
        free_a_printer(&printer,2);
+
+       srv_spoolss_sendnotify(handle);
+
        return NT_STATUS_NO_PROBLEMO;
 }
 
@@ -3886,92 +4908,48 @@ uint32 _spoolss_addprinterex( const UNISTR2 *uni_srv_name, uint32 level,
                        /* we don't handle yet */
                        /* but I know what to do ... */
                        return ERROR_INVALID_LEVEL;
-                       break;
                case 2:
                        return spoolss_addprinterex_level_2(uni_srv_name, info, 
                                                            unk0, unk1, unk2, unk3,
                                                            user_switch, user, handle);
-                       break;
-               default:
-                       return ERROR_INVALID_LEVEL;
-                       break;
-       }
-}
-
-/****************************************************************************
- Modify internal driver heirarchy.
-****************************************************************************/
-
-#if MANGLE_DRIVER_PATH
-static uint32 modify_driver_heirarchy(NT_PRINTER_DRIVER_INFO_LEVEL *driver, uint32 level)
-{
-       pstring path_old;
-       pstring path_new;
-       pstring short_archi;
-       pstring model_name;
-
-       /* find_service is an smbd-specific function call */
-       int snum = find_service("print$");
-       char *model = NULL;
-
-       *short_archi = '\0';
-       switch (level) {
-               case 3:
-                       get_short_archi(short_archi, driver->info_3->environment);
-                       model = driver->info_3->name;
-                       break;
-               case 6:
-                       get_short_archi(short_archi, driver->info_6->environment);
-                       model = driver->info_6->name;
-                       break;
                default:
-                       DEBUG(0,("modify_driver_heirarchy: unknown info level (%d)\n", level));
                        return ERROR_INVALID_LEVEL;
-                       break;
-       }
-
-       slprintf(path_old, sizeof(path_old)-1, "%s/%s/TMP_%s", lp_pathname(snum), short_archi,
-               client_addr());
-
-       /* Clean up any '/' and other characters in the model name. */
-       alpha_strcpy(model_name, model, sizeof(pstring));
-
-       slprintf(path_new, sizeof(path_new)-1, "%s/%s/%s", lp_pathname(snum), short_archi, model_name);
-
-       DEBUG(10,("modify_driver_heirarchy: old_path=%s, new_path=%s\n",
-                       path_old, path_new ));
-       if (dos_rename(path_old, path_new) == -1) {
-               DEBUG(0,("modify_driver_heirarchy: rename from %s to %s failed (%s)\n", 
-                       path_old, path_new, strerror(errno) ));
-               /* We need to clean up here.... - how ? */
-               return ERROR_ACCESS_DENIED; /* We need a generic mapping from NT errors here... */
        }
-
-       return NT_STATUS_NO_PROBLEMO;
 }
-#endif
 
 /****************************************************************************
 ****************************************************************************/
-uint32 _spoolss_addprinterdriver( const UNISTR2 *server_name,
-                               uint32 level,
-                               const SPOOL_PRINTER_DRIVER_INFO_LEVEL *info)
+uint32 _spoolss_addprinterdriver(pipes_struct *p, const UNISTR2 *server_name,
+                                uint32 level, const SPOOL_PRINTER_DRIVER_INFO_LEVEL *info)
 {
        uint32 err = NT_STATUS_NO_PROBLEMO;
        NT_PRINTER_DRIVER_INFO_LEVEL driver;
+       struct current_user user;
+       
        ZERO_STRUCT(driver);
+
+       get_current_user(&user, p);     
        
        convert_printer_driver_info(info, &driver, level);
 
-       if (add_a_printer_driver(driver, level)!=0)
-               return ERROR_ACCESS_DENIED;
+       DEBUG(5,("Cleaning driver's information\n"));
+       if ((err = clean_up_driver_struct(driver, level)) != NT_STATUS_NO_PROBLEMO )
+               goto done;
 
-#if MANGLE_DRIVER_PATH
-       err = modify_driver_heirarchy(&driver, level);
-#endif
+       DEBUG(5,("Moving driver to final destination\n"));
+       if(!move_driver_to_download_area(driver, level, &user, &err)) {
+               if (err == 0)
+                       err = ERROR_ACCESS_DENIED;
+               goto done;
+       }
 
-       free_a_printer_driver(driver, level);
+       if (add_a_printer_driver(driver, level)!=0) {
+               err = ERROR_ACCESS_DENIED;
+               goto done;
+       }
 
+ done:
+       free_a_printer_driver(driver, level);
        return err;
 }
 
@@ -3979,7 +4957,7 @@ uint32 _spoolss_addprinterdriver( const UNISTR2 *server_name,
 ****************************************************************************/
 static void fill_driverdir_1(DRIVER_DIRECTORY_1 *info, char *name)
 {
-       init_unistr(&(info->name), name);
+       init_unistr(&info->name, name);
 }
 
 /****************************************************************************
@@ -3990,20 +4968,17 @@ static uint32 getprinterdriverdir_level_1(UNISTR2 *name, UNISTR2 *uni_environmen
        pstring long_archi;
        pstring short_archi;
        DRIVER_DIRECTORY_1 *info=NULL;
-       
+
+       unistr2_to_ascii(long_archi, uni_environment, sizeof(long_archi)-1);
+
+       if (get_short_archi(short_archi, long_archi)==FALSE)
+               return ERROR_INVALID_ENVIRONMENT;
+
        if((info=(DRIVER_DIRECTORY_1 *)malloc(sizeof(DRIVER_DIRECTORY_1))) == NULL)
                return ERROR_NOT_ENOUGH_MEMORY;
-       
-       unistr2_to_ascii(long_archi, uni_environment, sizeof(long_archi)-1);
-       get_short_archi(short_archi, long_archi);
-               
-#if MANGLE_DRIVER_PATH
-       slprintf(path, sizeof(path)-1, "\\\\%s\\print$\\%s\\TMP_%s", global_myname, short_archi,
-               client_addr());
-#else
-       slprintf(path, sizeof(path)-1, "\\\\%s\\print$\\%s",
-                       global_myname, short_archi);
-#endif
+
+       slprintf(path, sizeof(path)-1, "\\\\%s\\print$\\%s", global_myname, short_archi);
+
        DEBUG(4,("printer driver directory: [%s]\n", path));
 
        fill_driverdir_1(info, path);
@@ -4038,16 +5013,14 @@ uint32 _spoolss_getprinterdriverdirectory(UNISTR2 *name, UNISTR2 *uni_environmen
        switch(level) {
        case 1:
                return getprinterdriverdir_level_1(name, uni_environment, buffer, offered, needed);
-               break;
        default:
                return ERROR_INVALID_LEVEL;
-               break;
        }
 }
        
 /****************************************************************************
 ****************************************************************************/
-uint32 _spoolss_enumprinterdata(const POLICY_HND *handle, uint32 idx,
+uint32 _spoolss_enumprinterdata(POLICY_HND *handle, uint32 idx,
                                uint32 in_value_len, uint32 in_data_len,
                                uint32 *out_max_value_len, uint16 **out_value, uint32 *out_value_len,
                                uint32 *out_type,
@@ -4080,13 +5053,15 @@ uint32 _spoolss_enumprinterdata(const POLICY_HND *handle, uint32 idx,
 
        DEBUG(5,("spoolss_enumprinterdata\n"));
 
-       if (!OPEN_HANDLE(Printer))
+       if (!OPEN_HANDLE(Printer)) {
+               DEBUG(0,("_spoolss_enumprinterdata: Invalid handle (%s).\n", OUR_HANDLE(handle)));
                return ERROR_INVALID_HANDLE;
+       }
 
        if (!get_printer_snum(handle, &snum))
                return ERROR_INVALID_HANDLE;
        
-       if (get_a_printer(&printer, 2, lp_servicename(snum)) != 0x0)
+       if (get_a_printer(&printer, 2, lp_servicename(snum)) != 0)
                return ERROR_INVALID_HANDLE;
 
        /* 
@@ -4096,7 +5071,27 @@ uint32 _spoolss_enumprinterdata(const POLICY_HND *handle, uint32 idx,
         */
        if ( (in_value_len==0) && (in_data_len==0) ) {
                DEBUGADD(6,("Activating NT mega-hack to find sizes\n"));
-               
+
+#if 0
+               /*
+                * NT can ask for a specific parameter size - we need to return NO_MORE_ITEMS
+                * if this parameter size doesn't exist.
+                * Ok - my opinion here is that the client is not asking for the greatest
+                * possible size of all the parameters, but is asking specifically for the size needed
+                * for this specific parameter. In that case we can remove the loop below and
+                * simplify this lookup code considerably. JF - comments welcome. JRA.
+                */
+
+               if (!get_specific_param_by_index(*printer, 2, idx, value, &data, &type, &data_len)) {
+                       safe_free(data);
+                       free_a_printer(&printer, 2);
+                       return ERROR_NO_MORE_ITEMS;
+               }
+#endif
+
+               safe_free(data);
+               data = NULL;
+
                param_index=0;
                biggest_valuesize=0;
                biggest_datasize=0;
@@ -4108,9 +5103,22 @@ uint32 _spoolss_enumprinterdata(const POLICY_HND *handle, uint32 idx,
                        DEBUG(6,("current values: [%d], [%d]\n", biggest_valuesize, biggest_datasize));
 
                        safe_free(data);
+                       data = NULL;
                        param_index++;
                }
 
+               /*
+                * I think this is correct, it doesn't break APW and
+                * allows Gerald's Win32 test programs to work correctly,
+                * but may need altering.... JRA.
+                */
+
+               if (param_index == 0) {
+                       /* No parameters found. */
+                       free_a_printer(&printer, 2);
+                       return ERROR_NO_MORE_ITEMS;
+               }
+
                /* the value is an UNICODE string but realvaluesize is the length in bytes including the leading 0 */
                *out_value_len=2*(1+biggest_valuesize);
                *out_data_len=biggest_datasize;
@@ -4143,13 +5151,14 @@ uint32 _spoolss_enumprinterdata(const POLICY_HND *handle, uint32 idx,
         * take a pause *before* coding not *during* coding
         */
         
-       *out_max_value_len=in_value_len;
+       *out_max_value_len=(in_value_len/sizeof(uint16));
        if((*out_value=(uint16 *)malloc(in_value_len*sizeof(uint8))) == NULL) {
                safe_free(data);
                return ERROR_NOT_ENOUGH_MEMORY;
        }
-       ascii_to_unistr((char *)*out_value, value, *out_max_value_len);
-       *out_value_len=2*(1+strlen(value));
+       
+       ZERO_STRUCTP(*out_value);
+       *out_value_len = (uint32)dos_PutUniCode((char *)*out_value, value, in_value_len, True);
 
        *out_type=type;
 
@@ -4159,7 +5168,9 @@ uint32 _spoolss_enumprinterdata(const POLICY_HND *handle, uint32 idx,
                safe_free(data);
                return ERROR_NOT_ENOUGH_MEMORY;
        }
-       memcpy(*data_out, data, data_len);
+       
+       memset(*data_out,'\0',in_data_len);
+       memcpy(*data_out, data, (size_t)data_len);
        *out_data_len=data_len;
 
        safe_free(data);
@@ -4169,7 +5180,7 @@ uint32 _spoolss_enumprinterdata(const POLICY_HND *handle, uint32 idx,
 
 /****************************************************************************
 ****************************************************************************/
-uint32 _spoolss_setprinterdata( const POLICY_HND *handle,
+uint32 _spoolss_setprinterdata( POLICY_HND *handle,
                                const UNISTR2 *value,
                                uint32 type,
                                uint32 max_len,
@@ -4178,17 +5189,17 @@ uint32 _spoolss_setprinterdata( const POLICY_HND *handle,
                                uint32 numeric_data)
 {
        NT_PRINTER_INFO_LEVEL *printer = NULL;
-       NT_PRINTER_PARAM *param = NULL;
-               
+       NT_PRINTER_PARAM *param = NULL, old_param;
        int snum=0;
        uint32 status = 0x0;
        Printer_entry *Printer=find_printer_index_by_hnd(handle);
        
        DEBUG(5,("spoolss_setprinterdata\n"));
 
-       
-       if (!OPEN_HANDLE(Printer))
+       if (!OPEN_HANDLE(Printer)) {
+               DEBUG(0,("_spoolss_setprinterdata: Invalid handle (%s).\n", OUR_HANDLE(handle)));
                return ERROR_INVALID_HANDLE;
+       }
 
        if (!get_printer_snum(handle, &snum))
                return ERROR_INVALID_HANDLE;
@@ -4198,12 +5209,86 @@ uint32 _spoolss_setprinterdata( const POLICY_HND *handle,
                return ERROR_INVALID_NAME;
 
        convert_specific_param(&param, value , type, data, real_len);
+
+       /* Check if we are making any changes or not.  Return true if 
+          nothing is actually changing. */
+
+       ZERO_STRUCT(old_param);
+
+       if (get_specific_param(*printer, 2, param->value, &old_param.data,
+                              &old_param.type, &old_param.data_len)) {
+
+               if (param->type == old_param.type &&
+                   param->data_len == old_param.data_len &&
+                   memcmp(param->data, old_param.data,
+                          old_param.data_len) == 0) {
+
+                       DEBUG(3, ("setprinterdata hasn't changed\n"));
+                       status = NT_STATUS_NO_PROBLEMO;
+                       goto done;
+               }
+       }
+
+       /* Access check */
+
+       if (!print_access_check(NULL, snum, PRINTER_ACCESS_ADMINISTER)) {
+               DEBUG(3, ("security descriptor change denied by existing "
+                         "security descriptor\n"));
+               status = ERROR_ACCESS_DENIED;
+               goto done;
+       }
+
        unlink_specific_param_if_exist(printer->info_2, param);
        
        if (!add_a_specific_param(printer->info_2, param))
                status = ERROR_INVALID_PARAMETER;
        else
-               status = add_a_printer(*printer, 2);
+               status = mod_a_printer(*printer, 2);
+
+ done:
+       free_a_printer(&printer, 2);
+       safe_free(old_param.data);
+
+       return status;
+}
+
+/****************************************************************************
+****************************************************************************/
+uint32 _spoolss_deleteprinterdata( POLICY_HND *handle, const UNISTR2 *value)
+{
+       NT_PRINTER_INFO_LEVEL *printer = NULL;
+       NT_PRINTER_PARAM param;
+       int snum=0;
+       uint32 status = 0x0;
+       Printer_entry *Printer=find_printer_index_by_hnd(handle);
+       
+       DEBUG(5,("spoolss_deleteprinterdata\n"));
+       
+       if (!OPEN_HANDLE(Printer)) {
+               DEBUG(0,("_spoolss_deleteprinterdata: Invalid handle (%s).\n", OUR_HANDLE(handle)));
+               return ERROR_INVALID_HANDLE;
+       }
+
+       if (!get_printer_snum(handle, &snum))
+               return ERROR_INVALID_HANDLE;
+
+       if (!print_access_check(NULL, snum, PRINTER_ACCESS_ADMINISTER)) {
+               DEBUG(3, ("_spoolss_deleteprinterdata: security descriptor change denied by existing "
+                         "security descriptor\n"));
+               return ERROR_ACCESS_DENIED;
+       }
+
+       status = get_a_printer(&printer, 2, lp_servicename(snum));
+       if (status != 0x0)
+               return ERROR_INVALID_NAME;
+
+       ZERO_STRUCTP(&param);
+       unistr2_to_ascii(param.value, value, sizeof(param.value)-1);
+
+       if(!unlink_specific_param_if_exist(printer->info_2, &param))
+               status = ERROR_INVALID_PARAMETER;
+       else
+               status = mod_a_printer(*printer, 2);
 
        free_a_printer(&printer, 2);
        return status;
@@ -4211,7 +5296,7 @@ uint32 _spoolss_setprinterdata( const POLICY_HND *handle,
 
 /****************************************************************************
 ****************************************************************************/
-uint32 _spoolss_addform( const POLICY_HND *handle,
+uint32 _spoolss_addform( POLICY_HND *handle,
                                uint32 level,
                                const FORM *form)
 {
@@ -4221,8 +5306,10 @@ uint32 _spoolss_addform( const POLICY_HND *handle,
 
        DEBUG(5,("spoolss_addform\n"));
 
-       if (!OPEN_HANDLE(Printer))
+       if (!OPEN_HANDLE(Printer)) {
+               DEBUG(0,("_spoolss_addform: Invalid handle (%s).\n", OUR_HANDLE(handle)));
                return ERROR_INVALID_HANDLE;
+       }
 
        count=get_ntforms(&list);
        if(!add_a_form(&list, form, &count))
@@ -4236,7 +5323,32 @@ uint32 _spoolss_addform( const POLICY_HND *handle,
 
 /****************************************************************************
 ****************************************************************************/
-uint32 _spoolss_setform( const POLICY_HND *handle,
+uint32 _spoolss_deleteform( POLICY_HND *handle, UNISTR2 *form_name)
+{
+       int count=0;
+       uint32 ret = 0;
+       nt_forms_struct *list=NULL;
+       Printer_entry *Printer = find_printer_index_by_hnd(handle);
+
+       DEBUG(5,("spoolss_deleteform\n"));
+
+       if (!OPEN_HANDLE(Printer)) {
+               DEBUG(0,("_spoolss_deleteform: Invalid handle (%s).\n", OUR_HANDLE(handle)));
+               return ERROR_INVALID_HANDLE;
+       }
+
+       count = get_ntforms(&list);
+       if(!delete_a_form(&list, form_name, &count, &ret))
+               return ERROR_INVALID_PARAMETER;
+
+       safe_free(list);
+
+       return ret;
+}
+
+/****************************************************************************
+****************************************************************************/
+uint32 _spoolss_setform( POLICY_HND *handle,
                                const UNISTR2 *uni_name,
                                uint32 level,
                                const FORM *form)
@@ -4247,8 +5359,8 @@ uint32 _spoolss_setform( const POLICY_HND *handle,
 
        DEBUG(5,("spoolss_setform\n"));
 
-       if (!OPEN_HANDLE(Printer))
-       {
+       if (!OPEN_HANDLE(Printer)) {
+               DEBUG(0,("_spoolss_setform: Invalid handle (%s).\n", OUR_HANDLE(handle)));
                return ERROR_INVALID_HANDLE;
        }
        count=get_ntforms(&list);
@@ -4272,7 +5384,7 @@ static uint32 enumprintprocessors_level_1(NEW_BUFFER *buffer, uint32 offered, ui
 
        (*returned) = 0x1;
        
-       init_unistr(&(info_1->name), "winprint");
+       init_unistr(&info_1->name, "winprint");
 
        *needed += spoolss_size_printprocessor_info_1(info_1);
 
@@ -4312,10 +5424,8 @@ uint32 _spoolss_enumprintprocessors(UNISTR2 *name, UNISTR2 *environment, uint32
        switch (level) {
        case 1:
                return enumprintprocessors_level_1(buffer, offered, needed, returned);
-               break;
        default:
                return ERROR_INVALID_LEVEL;
-               break;
        }
 }
 
@@ -4331,7 +5441,7 @@ static uint32 enumprintprocdatatypes_level_1(NEW_BUFFER *buffer, uint32 offered,
 
        (*returned) = 0x1;
        
-       init_unistr(&(info_1->name), "RAW");
+       init_unistr(&info_1->name, "RAW");
 
        *needed += spoolss_size_printprocdatatype_info_1(info_1);
 
@@ -4364,10 +5474,8 @@ uint32 _spoolss_enumprintprocdatatypes(UNISTR2 *name, UNISTR2 *processor, uint32
        switch (level) {
        case 1:
                return enumprintprocdatatypes_level_1(buffer, offered, needed, returned);
-               break;
        default:
                return ERROR_INVALID_LEVEL;
-               break;
        }
 }
 
@@ -4383,7 +5491,7 @@ static uint32 enumprintmonitors_level_1(NEW_BUFFER *buffer, uint32 offered, uint
 
        (*returned) = 0x1;
        
-       init_unistr(&(info_1->name), "Local Port");
+       init_unistr(&info_1->name, "Local Port");
 
        *needed += spoolss_size_printmonitor_info_1(info_1);
 
@@ -4414,9 +5522,9 @@ static uint32 enumprintmonitors_level_2(NEW_BUFFER *buffer, uint32 offered, uint
 
        (*returned) = 0x1;
        
-       init_unistr(&(info_2->name), "Local Port");
-       init_unistr(&(info_2->environment), "Windows NT X86");
-       init_unistr(&(info_2->dll_name), "localmon.dll");
+       init_unistr(&info_2->name, "Local Port");
+       init_unistr(&info_2->environment, "Windows NT X86");
+       init_unistr(&info_2->dll_name, "localmon.dll");
 
        *needed += spoolss_size_printmonitor_info_2(info_2);
 
@@ -4456,13 +5564,10 @@ uint32 _spoolss_enumprintmonitors(UNISTR2 *name,uint32 level,
        switch (level) {
        case 1:
                return enumprintmonitors_level_1(buffer, offered, needed, returned);
-               break;          
        case 2:
                return enumprintmonitors_level_2(buffer, offered, needed, returned);
-               break;
        default:
                return ERROR_INVALID_LEVEL;
-               break;
        }
 }
 
@@ -4521,9 +5626,11 @@ static uint32 getjob_level_2(print_queue_struct *queue, int count, int snum, uin
 {
        int i=0;
        BOOL found=False;
-       JOB_INFO_2 *info_2=NULL;
+       JOB_INFO_2 *info_2;
        info_2=(JOB_INFO_2 *)malloc(sizeof(JOB_INFO_2));
 
+       ZERO_STRUCTP(info_2);
+
        if (info_2 == NULL) {
                safe_free(queue);
                return ERROR_NOT_ENOUGH_MEMORY;
@@ -4554,6 +5661,7 @@ static uint32 getjob_level_2(print_queue_struct *queue, int count, int snum, uin
 
        new_smb_io_job_info_2("", buffer, info_2, 0);
 
+       free_dev_mode(info_2->devmode);
        safe_free(info_2);
 
        if (*needed > offered)
@@ -4590,14 +5698,11 @@ uint32 _spoolss_getjob( POLICY_HND *handle, uint32 jobid, uint32 level,
        switch (level) {
        case 1:
                return getjob_level_1(queue, count, snum, jobid, buffer, offered, needed);
-               break;
        case 2:
                return getjob_level_2(queue, count, snum, jobid, buffer, offered, needed);
-               break;
        default:
                safe_free(queue);
                return ERROR_INVALID_LEVEL;
-               break;
        }
 }
 #undef OLD_NTDOMAIN