re-enable ffpcn on print server handle
[kai/samba.git] / source / rpc_server / srv_spoolss_nt.c
index d04aff8b150330b73fd6b4006a218fa8b1e2aab5..5ebefc5ea063e0d1047f5a71029f9f268030c85d 100644 (file)
@@ -4,7 +4,7 @@
  *  Copyright (C) Andrew Tridgell              1992-2000,
  *  Copyright (C) Luke Kenneth Casson Leighton 1996-2000,
  *  Copyright (C) Jean François Micouleau      1998-2000,
- *  Copyright (C) Jeremy Allison                   2001,
+ *  Copyright (C) Jeremy Allison               2001-2002,
  *  Copyright (C) Gerald Carter                       2000-2002,
  *  Copyright (C) Tim Potter                   2001-2002.
  *
@@ -87,6 +87,10 @@ typedef struct _Printer{
                fstring machine;
                fstring user;
        } client;
+       
+       /* devmode sent in the OpenPrinter() call */
+       NT_DEVICEMODE   *nt_devmode;
+       
 } Printer_entry;
 
 static Printer_entry *printers_list;
@@ -174,10 +178,18 @@ static void free_spool_notify_option(SPOOL_NOTIFY_OPTION **pp)
  Disconnect from the client
 ****************************************************************************/
 
-static void srv_spoolss_replycloseprinter(POLICY_HND *handle)
+static void srv_spoolss_replycloseprinter(int snum, POLICY_HND *handle)
 {
        WERROR result;
 
+       /* 
+        * Tell the specific printing tdb we no longer want messages for this printer
+        * by deregistering our PID.
+        */
+
+       if (!print_notify_deregister_pid(snum))
+               DEBUG(0,("print_notify_register_pid: Failed to register our pid for printer %s\n", lp_const_servicename(snum) ));
+
        /* weird if the test succeds !!! */
        if (smb_connections==0) {
                DEBUG(0,("srv_spoolss_replycloseprinter:Trying to close non-existant notify backchannel !\n"));
@@ -196,6 +208,11 @@ static void srv_spoolss_replycloseprinter(POLICY_HND *handle)
                cli_ulogoff(&notify_cli);
                cli_shutdown(&notify_cli);
                message_deregister(MSG_PRINTER_NOTIFY2);
+
+               /* Tell the connections db we're no longer interested in
+                * printer notify messages. */
+
+               register_message_flags( False, FLAG_MSG_PRINTING );
        }
 
        smb_connections--;
@@ -210,7 +227,8 @@ static void free_printer_entry(void *ptr)
        Printer_entry *Printer = (Printer_entry *)ptr;
 
        if (Printer->notify.client_connected==True)
-               srv_spoolss_replycloseprinter(&Printer->notify.client_hnd);
+               srv_spoolss_replycloseprinter(print_queue_snum(Printer->dev.handlename),
+                               &Printer->notify.client_hnd);
 
        Printer->notify.flags=0;
        Printer->notify.options=0;
@@ -219,6 +237,8 @@ static void free_printer_entry(void *ptr)
        free_spool_notify_option(&Printer->notify.option);
        Printer->notify.option=NULL;
        Printer->notify.client_connected=False;
+       
+       free_nt_devicemode( &Printer->nt_devmode );
 
        /* Remove from the internal list. */
        DLIST_REMOVE(printers_list, Printer);
@@ -302,11 +322,19 @@ static WERROR delete_printer_handle(pipes_struct *p, POLICY_HND *hnd)
                return WERR_BADFID;
        }
 
-       if (del_a_printer(Printer->dev.handlename) != 0) {
-               DEBUG(3,("Error deleting printer %s\n", Printer->dev.handlename));
-               return WERR_BADFID;
+       /* 
+        * It turns out that Windows allows delete printer on a handle
+        * opened by an admin user, then used on a pipe handle created
+        * by an anonymous user..... but they're working on security.... riiight !
+        * JRA.
+        */
+
+       if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
+               DEBUG(3, ("delete_printer_handle: denied by handle\n"));
+               return WERR_ACCESS_DENIED;
        }
 
+#if 0
        /* Check calling user has permission to delete printer.  Note that
           since we set the snum parameter to -1 only administrators can
           delete the printer.  This stops people with the Full Control
@@ -316,6 +344,12 @@ static WERROR delete_printer_handle(pipes_struct *p, POLICY_HND *hnd)
                DEBUG(3, ("printer delete denied by security descriptor\n"));
                return WERR_ACCESS_DENIED;
        }
+#endif
+
+       if (del_a_printer(Printer->dev.handlename) != 0) {
+               DEBUG(3,("Error deleting printer %s\n", Printer->dev.handlename));
+               return WERR_BADFID;
+       }
 
        if (*lp_deleteprinter_cmd()) {
 
@@ -338,10 +372,10 @@ static WERROR delete_printer_handle(pipes_struct *p, POLICY_HND *hnd)
                /* Send SIGHUP to process group... is there a better way? */
                kill(0, SIGHUP);
 
-               if ( ( i = lp_servicenumber( Printer->dev.handlename ) ) >= 0 ) {
-                       lp_killservice( i );
-                       return WERR_OK;
-               } else
+               /* go ahead and re-read the services immediately */
+               reload_services( False );
+
+               if ( ( i = lp_servicenumber( Printer->dev.handlename ) ) < 0 )
                        return WERR_ACCESS_DENIED;
        }
 
@@ -566,7 +600,14 @@ static BOOL is_monitoring_event(Printer_entry *p, uint16 notify_type,
 {
        SPOOL_NOTIFY_OPTION *option = p->notify.option;
        uint32 i, j;
-       
+
+       /* 
+        * Flags should always be zero when the change notify
+        * is registered by the cliebnt's spooler.  A user Win32 app
+        * might use the flags though instead of the NOTIFY_OPTION_INFO 
+        * --jerry
+        */
+        
        if (p->notify.flags)
                return is_monitoring_event_flags(
                        p->notify.flags, notify_type, notify_field);
@@ -644,7 +685,7 @@ static void notify_system_time(struct spoolss_notify_msg *msg,
                return;
        }
 
-       if (!make_systemtime(&systime, localtime((time_t *)msg->notify.data))) {
+       if (!make_systemtime(&systime, gmtime((time_t *)msg->notify.data))) {
                DEBUG(5, ("notify_system_time: unable to make systemtime\n"));
                return;
        }
@@ -716,26 +757,177 @@ static struct notify2_message_table job_notify_table[] = {
        /* 0x17 */ { "JOB_NOTIFY_BYTES_PRINTED", NULL },
 };
 
+
+/***********************************************************************
+ Allocate talloc context for container object
+ **********************************************************************/
+static void notify_msg_ctr_init( SPOOLSS_NOTIFY_MSG_CTR *ctr )
+{
+       if ( !ctr )
+               return;
+
+       ctr->ctx = talloc_init();
+               
+       return;
+}
+
+/***********************************************************************
+ release all allocated memory and zero out structure
+ **********************************************************************/
+static void notify_msg_ctr_destroy( SPOOLSS_NOTIFY_MSG_CTR *ctr )
+{
+       if ( !ctr )
+               return;
+
+       if ( ctr->ctx )
+               talloc_destroy(ctr->ctx);
+               
+       ZERO_STRUCTP(ctr);
+               
+       return;
+}
+
+/***********************************************************************
+ **********************************************************************/
+static TALLOC_CTX* notify_ctr_getctx( SPOOLSS_NOTIFY_MSG_CTR *ctr )
+{
+       if ( !ctr )
+               return NULL;
+               
+       return ctr->ctx;
+}
+
+/***********************************************************************
+ **********************************************************************/
+static SPOOLSS_NOTIFY_MSG_GROUP* notify_ctr_getgroup( SPOOLSS_NOTIFY_MSG_CTR *ctr, uint32 idx )
+{
+       if ( !ctr || !ctr->msg_groups )
+               return NULL;
+       
+       if ( idx >= ctr->num_groups )
+               return NULL;
+               
+       return &ctr->msg_groups[idx];
+
+}
+
+/***********************************************************************
+ How many groups of change messages do we have ?
+ **********************************************************************/
+static int notify_msg_ctr_numgroups( SPOOLSS_NOTIFY_MSG_CTR *ctr )
+{
+       if ( !ctr )
+               return 0;
+               
+       return ctr->num_groups;
+}
+
+/***********************************************************************
+ Add a SPOOLSS_NOTIFY_MSG_CTR to the correct group
+ **********************************************************************/
+static int notify_msg_ctr_addmsg( SPOOLSS_NOTIFY_MSG_CTR *ctr, SPOOLSS_NOTIFY_MSG *msg )
+{
+       SPOOLSS_NOTIFY_MSG_GROUP        *groups = NULL;
+       SPOOLSS_NOTIFY_MSG_GROUP        *msg_grp = NULL;
+       SPOOLSS_NOTIFY_MSG              *msg_list = NULL;
+       int                             i, new_slot;
+       
+       if ( !ctr || !msg )
+               return 0;
+       
+       /* loop over all groups looking for a matching printer name */
+       
+       for ( i=0; i<ctr->num_groups; i++ ) {
+               if ( strcmp(ctr->msg_groups[i].printername, msg->printer) == 0 )
+                       break;
+       }
+       
+       /* add a new group? */
+       
+       if ( i == ctr->num_groups )
+       {
+               ctr->num_groups++;
+
+               if ( !(groups = talloc_realloc( ctr->ctx, ctr->msg_groups, sizeof(SPOOLSS_NOTIFY_MSG_GROUP)*ctr->num_groups)) ) {
+                       DEBUG(0,("notify_msg_ctr_addmsg: talloc_realloc() failed!\n"));
+                       return 0;
+               }
+               ctr->msg_groups = groups;
+
+               /* clear the new entry and set the printer name */
+               
+               ZERO_STRUCT( ctr->msg_groups[ctr->num_groups-1] );
+               fstrcpy( ctr->msg_groups[ctr->num_groups-1].printername, msg->printer );
+       }
+       
+       /* add the change messages; 'i' is the correct index now regardless */
+       
+       msg_grp = &ctr->msg_groups[i];
+       
+       msg_grp->num_msgs++;
+       
+       if ( !(msg_list =  talloc_realloc( ctr->ctx, msg_grp->msgs, sizeof(SPOOLSS_NOTIFY_MSG)*msg_grp->num_msgs )) ) {
+               DEBUG(0,("notify_msg_ctr_addmsg: talloc_realloc() failed for new message [%d]!\n", msg_grp->num_msgs));
+               return 0;
+       }
+       msg_grp->msgs = msg_list;
+       
+       new_slot = msg_grp->num_msgs-1;
+       memcpy( &msg_grp->msgs[new_slot], msg, sizeof(SPOOLSS_NOTIFY_MSG) );
+       
+       /* need to allocate own copy of data */
+       
+       if ( msg->len != 0 ) 
+               msg_grp->msgs[new_slot].notify.data = talloc_memdup( ctr->ctx, msg->notify.data, msg->len );
+       
+       return ctr->num_groups;
+}
+
 /***********************************************************************
  Send a change notication message on all handles which have a call 
  back registered
  **********************************************************************/
 
-static void process_notify2_message(struct spoolss_notify_msg *msg, 
-                                   TALLOC_CTX *mem_ctx)
+static void send_notify2_changes( SPOOLSS_NOTIFY_MSG_CTR *ctr, uint32 idx )
 {
-       Printer_entry *p;
-
-       DEBUG(8,("process_notify2_message: Enter...[%s]\n", msg->printer));
+       Printer_entry            *p;
+       TALLOC_CTX               *mem_ctx = notify_ctr_getctx( ctr );
+       SPOOLSS_NOTIFY_MSG_GROUP *msg_group = notify_ctr_getgroup( ctr, idx );
+       SPOOLSS_NOTIFY_MSG       *messages;
+       
+       
+       if ( !msg_group ) {
+               DEBUG(5,("send_notify2_changes() called with no msg group!\n"));
+               return;
+       }
+       
+       messages = msg_group->msgs;
+       
+       if ( !messages ) {
+               DEBUG(5,("send_notify2_changes() called with no messages!\n"));
+               return;
+       }
+       
+       DEBUG(8,("send_notify2_changes: Enter...[%s]\n", msg_group->printername));
        
-       for (p = printers_list; p; p = p->next) {
+       /* loop over all printers */
+       
+       for (p = printers_list; p; p = p->next) 
+       {
                SPOOL_NOTIFY_INFO_DATA *data;
-               uint32 data_len = 1;
-               uint32 id;
+               uint32  data_len = 0;
+               uint32  id;
+               int     i;
 
                /* Is there notification on this handle? */
 
-               if (!p->notify.client_connected)
+               if ( !p->notify.client_connected )
                        continue;
 
                DEBUG(10,("Client connected! [%s]\n", p->dev.handlename));
@@ -744,25 +936,31 @@ static void process_notify2_message(struct spoolss_notify_msg *msg,
                    notifications. */
 
                if ( ( p->printer_type == PRINTER_HANDLE_IS_PRINTER )  &&
-                   ( !strequal(msg->printer, p->dev.handlename) ) )
+                   ( !strequal(msg_group->printername, p->dev.handlename) ) )
                        continue;
 
                DEBUG(10,("Our printer\n"));
                
+               /* allocate the max entries possible */
+               
+               data = talloc( mem_ctx, msg_group->num_msgs*sizeof(SPOOL_NOTIFY_INFO_DATA) );
+               ZERO_STRUCTP(data);
+               
+               /* build the array of change notifications */
+               
+               for ( i=0; i<msg_group->num_msgs; i++ )
+               {
+                       SPOOLSS_NOTIFY_MSG      *msg = &messages[i];
+                       
                /* Are we monitoring this event? */
 
                if (!is_monitoring_event(p, msg->type, msg->field))
                        continue;
 
+                       
                DEBUG(10,("process_notify2_message: Sending message type [%x] field [%x] for printer [%s]\n",
                        msg->type, msg->field, p->dev.handlename));
 
-               /* OK - send the event to the client */
-
-               data = talloc(mem_ctx, sizeof(SPOOL_NOTIFY_INFO_DATA));
-
-               ZERO_STRUCTP(data);
-
                /* 
                 * if the is a printer notification handle and not a job notification 
                 * type, then set the id to 0.  Other wise just use what was specified
@@ -784,8 +982,8 @@ static void process_notify2_message(struct spoolss_notify_msg *msg,
 
                /* Convert unix jobid to smb jobid */
 
-               if (msg->flags & SPOOLSS_NOTIFY_MSG_UNIX_JOBID) {
-
+                       if (msg->flags & SPOOLSS_NOTIFY_MSG_UNIX_JOBID) 
+                       {
                        id = sysjob_to_jobid(msg->id);
 
                        if (id == -1) {
@@ -794,22 +992,20 @@ static void process_notify2_message(struct spoolss_notify_msg *msg,
                        }
                }
 
-               construct_info_data(data, msg->type, msg->field, id);
+                       construct_info_data( &data[data_len], msg->type, msg->field, id );
 
                switch(msg->type) {
                case PRINTER_NOTIFY_TYPE:
                                if ( !printer_notify_table[msg->field].fn )
                                goto done;
-                                       
-                               printer_notify_table[msg->field].fn(msg, data, mem_ctx);
+                                       printer_notify_table[msg->field].fn(msg, &data[data_len], mem_ctx);
                                
                        break;
                        
                case JOB_NOTIFY_TYPE:
                                if ( !job_notify_table[msg->field].fn )
                                goto done;
-
-                               job_notify_table[msg->field].fn(msg, data, mem_ctx);
+                                       job_notify_table[msg->field].fn(msg, &data[data_len], mem_ctx);
 
                                break;
 
@@ -818,59 +1014,139 @@ static void process_notify2_message(struct spoolss_notify_msg *msg,
                                goto done;
                        }
 
+                       data_len++;
+               }
+
                cli_spoolss_rrpcn( &notify_cli, mem_ctx, &p->notify.client_hnd, 
                                data_len, data, p->notify.change, 0 );
        }
+       
 done:
-       DEBUG(8,("process_notify2_message: Exit...\n"));
+       DEBUG(8,("send_notify2_changes: Exit...\n"));
        return;
 }
 
-/* Receive a notify2 message */
+/***********************************************************************
+ **********************************************************************/
 
-static void receive_notify2_message(int msg_type, pid_t src, void *buf, 
-                                   size_t len)
+static BOOL notify2_unpack_msg( SPOOLSS_NOTIFY_MSG *msg, void *buf, size_t len )
 {
-       struct spoolss_notify_msg msg;
+
        int offset = 0;
-       TALLOC_CTX *mem_ctx = talloc_init();
 
        /* Unpack message */
 
-       ZERO_STRUCT(msg);
-
        offset += tdb_unpack((char *)buf + offset, len - offset, "f",
-                            msg.printer);
+                            msg->printer);
        
        offset += tdb_unpack((char *)buf + offset, len - offset, "ddddd",
-                            &msg.type, &msg.field, &msg.id, &msg.len, &msg.flags);
+                            &msg->type, &msg->field, &msg->id, &msg->len, &msg->flags);
 
-       if (msg.len == 0)
+       if (msg->len == 0)
                tdb_unpack((char *)buf + offset, len - offset, "dd",
-                          &msg.notify.value[0], &msg.notify.value[1]);
+                          &msg->notify.value[0], &msg->notify.value[1]);
        else
                tdb_unpack((char *)buf + offset, len - offset, "B", 
-                          &msg.len, &msg.notify.data);
+                          &msg->len, &msg->notify.data);
 
-       DEBUG(3, ("got NOTIFY2 message, type %d, field 0x%02x, flags 0x%04x\n",
-                 msg.type, msg.field, msg.flags));
+       DEBUG(3, ("notify2_unpack_msg: got NOTIFY2 message, type %d, field 0x%02x, flags 0x%04x\n",
+                 msg->type, msg->field, msg->flags));
 
-       if (msg.len == 0)
-               DEBUG(3, ("value1 = %d, value2 = %d\n", msg.notify.value[0],
-                         msg.notify.value[1]));
+       if (msg->len == 0)
+               DEBUG(3, ("notify2_unpack_msg: value1 = %d, value2 = %d\n", msg->notify.value[0],
+                         msg->notify.value[1]));
        else
-               dump_data(3, msg.notify.data, msg.len);
+               dump_data(3, msg->notify.data, msg->len);
+
+       return True;
+}
+
+/********************************************************************
+ Receive a notify2 message list
+ ********************************************************************/
 
-       /* Process message */
+static void receive_notify2_message_list(int msg_type, pid_t src, void *msg, size_t len)
+{
+       size_t                  msg_count, i;
+       char                    *buf = (char *)msg;
+       char                    *msg_ptr;
+       size_t                  msg_len;
+       SPOOLSS_NOTIFY_MSG      notify;
+       SPOOLSS_NOTIFY_MSG_CTR  messages;
+       int                     num_groups;
+
+       if (len < 4) {
+               DEBUG(0,("receive_notify2_message_list: bad message format (len < 4)!\n"));
+               return;
+       }
+       
+       msg_count = IVAL(buf, 0);
+       msg_ptr = buf + 4;
+
+       DEBUG(5, ("receive_notify2_message_list: got %d messages in list\n", msg_count));
 
-       process_notify2_message(&msg, mem_ctx);
+       if (msg_count == 0) {
+               DEBUG(0,("receive_notify2_message_list: bad message format (msg_count == 0) !\n"));
+               return;
+       }
 
-       /* Free message */
+       /* initialize the container */
+       
+       ZERO_STRUCT( messages );
+       notify_msg_ctr_init( &messages );
+       
+       /* 
+        * build message groups for each printer identified
+        * in a change_notify msg.  Remember that a PCN message
+        * includes the handle returned for the srv_spoolss_replyopenprinter()
+        * call.  Therefore messages are grouped according to printer handle.
+        */
+        
+       for ( i=0; i<msg_count; i++ ) 
+       {
+               if (msg_ptr + 4 - buf > len) {
+                       DEBUG(0,("receive_notify2_message_list: bad message format (len > buf_size) !\n"));
+                       return;
+               }
 
-       if (msg.len > 0)
-               free(msg.notify.data);
+               msg_len = IVAL(msg_ptr,0);
+               msg_ptr += 4;
 
-       talloc_destroy(mem_ctx);
+               if (msg_ptr + msg_len - buf > len) {
+                       DEBUG(0,("receive_notify2_message_list: bad message format (bad len) !\n"));
+                       return;
+               }
+               
+               /* unpack messages */
+               
+               ZERO_STRUCT( notify );
+               notify2_unpack_msg( &notify, msg_ptr, msg_len );
+               msg_ptr += msg_len;
+               
+               /* add to correct list in container */
+               
+               notify_msg_ctr_addmsg( &messages, &notify );
+               
+               /* free memory that might have been allocated by notify2_unpack_msg() */
+               
+               if ( notify.len != 0 )
+                       SAFE_FREE( notify.notify.data );
+       }
+       
+       /* process each group of messages */
+       
+       num_groups = notify_msg_ctr_numgroups( &messages );
+       for ( i=0; i<num_groups; i++ )
+               send_notify2_changes( &messages, i );
+       
+       
+       /* cleanup */
+               
+       DEBUG(10,("receive_notify2_message_list: processed %u messages\n", (uint32)msg_count ));
+               
+       notify_msg_ctr_destroy( &messages );
+       
+       return;
 }
 
 /********************************************************************
@@ -1006,6 +1282,12 @@ void reset_all_printerdata(int msg_type, pid_t src, void *buf, size_t len)
                                        DEBUG(5,("reset_all_printerdata: Error resetting printer data for printer [%s], driver [%s]!\n",
                                                printer->info_2->printername, printer->info_2->drivername));
                                }       
+                               
+                               result = mod_a_printer( *printer, 2 );
+                               if ( !W_ERROR_IS_OK(result) ) {
+                                       DEBUG(3,("reset_all_printerdata: mod_a_printer() failed!  (%s)\n", 
+                                               get_dos_error_msg(result)));
+                               }
                        }
                        
                        free_a_printer( &printer, 2 );
@@ -1143,14 +1425,65 @@ WERROR _spoolss_open_printer(pipes_struct *p, SPOOL_Q_OPEN_PRINTER *q_u, SPOOL_R
 /********************************************************************
  * spoolss_open_printer
  *
- * called from the spoolss dispatcher
+ * If the openprinterex rpc call contains a devmode,
+ * it's a per-user one. This per-user devmode is derivated
+ * from the global devmode. Openprinterex() contains a per-user 
+ * devmode for when you do EMF printing and spooling.
+ * In the EMF case, the NT workstation is only doing half the job
+ * of rendering the page. The other half is done by running the printer
+ * driver on the server.
+ * The EMF file doesn't contain the page description (paper size, orientation, ...).
+ * The EMF file only contains what is to be printed on the page.
+ * So in order for the server to know how to print, the NT client sends
+ * a devicemode attached to the openprinterex call.
+ * But this devicemode is short lived, it's only valid for the current print job.
+ *
+ * If Samba would have supported EMF spooling, this devicemode would
+ * have been attached to the handle, to sent it to the driver to correctly
+ * rasterize the EMF file.
+ *
+ * As Samba only supports RAW spooling, we only receive a ready-to-print file,
+ * we just act as a pass-thru between windows and the printer.
+ *
+ * In order to know that Samba supports only RAW spooling, NT has to call
+ * getprinter() at level 2 (attribute field) or NT has to call startdoc()
+ * and until NT sends a RAW job, we refuse it.
+ *
+ * But to call getprinter() or startdoc(), you first need a valid handle,
+ * and to get an handle you have to call openprintex(). Hence why you have
+ * a devicemode in the openprinterex() call.
+ *
+ *
+ * Differences between NT4 and NT 2000.
+ * NT4:
+ * ---
+ * On NT4, you only have a global devicemode. This global devicemode can be changed
+ * by the administrator (or by a user with enough privs). Everytime a user
+ * wants to print, the devicemode is resetted to the default. In Word, everytime
+ * you print, the printer's characteristics are always reset to the global devicemode.
+ *
+ * NT 2000:
+ * -------
+ * In W2K, there is the notion of per-user devicemode. The first time you use
+ * a printer, a per-user devicemode is build from the global devicemode.
+ * If you change your per-user devicemode, it is saved in the registry, under the
+ * H_KEY_CURRENT_KEY sub_tree. So that everytime you print, you have your default
+ * printer preferences available.
+ *
+ * To change the per-user devicemode: it's the "Printing Preferences ..." button
+ * on the General Tab of the printer properties windows.
+ *
+ * To change the global devicemode: it's the "Printing Defaults..." button
+ * on the Advanced Tab of the printer properties window.
+ *
+ * JFM.
  ********************************************************************/
 
 WERROR _spoolss_open_printer_ex( pipes_struct *p, SPOOL_Q_OPEN_PRINTER_EX *q_u, SPOOL_R_OPEN_PRINTER_EX *r_u)
 {
-       UNISTR2 *printername = NULL;
-       PRINTER_DEFAULT *printer_default = &q_u->printer_default;
-       POLICY_HND *handle = &r_u->handle;
+       UNISTR2                 *printername = NULL;
+       PRINTER_DEFAULT         *printer_default = &q_u->printer_default;
+       POLICY_HND              *handle = &r_u->handle;
 
        fstring name;
        int snum;
@@ -1180,39 +1513,36 @@ Can't find printer handle we created for printer %s\n", name ));
                return WERR_INVALID_PRINTER_NAME;
        }
 
-       /*
-          First case: the user is opening the print server:
-
-          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 allow only if the user is root (uid=0)
-          or if the user is listed in the smb.conf printer admin parameter.
-
-          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?
-          
-          
-          Second case: the user is opening a printer:
-          NT doesn't let us connect to a printer if the connecting user
-          doesn't have print permission.
-
-       */
-
        get_current_user(&user, p);
 
-       if (Printer->printer_type == PRINTER_HANDLE_IS_PRINTSERVER) {
+       /*
+        * First case: the user is opening the print server:
+        *
+        * 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 allow only if the user is root (uid=0)
+        * or if the user is listed in the smb.conf printer admin parameter.
+        *
+        * 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?
+        * 
+        * Second case: the user is opening a printer:
+        * NT doesn't let us connect to a printer if the connecting user
+        * doesn't have print permission.
+        */
 
+       if (Printer->printer_type == PRINTER_HANDLE_IS_PRINTSERVER) 
+       {
                /* Printserver handles use global struct... */
 
                snum = -1;
 
-               /* Map standard access rights to object specific access
-                  rights */
+               /* Map standard access rights to object specific access rights */
                
                se_map_standard(&printer_default->access_required, 
                                &printserver_std_mapping);
@@ -1231,23 +1561,32 @@ Can't find printer handle we created for printer %s\n", name ));
 
                /* Allow admin access */
 
-               if (printer_default->access_required & 
-                   SERVER_ACCESS_ADMINISTER) {
-
+               if ( printer_default->access_required & SERVER_ACCESS_ADMINISTER ) 
+               {
                        if (!lp_ms_add_printer_wizard()) {
                                close_printer_handle(p, handle);
                                return WERR_ACCESS_DENIED;
                        }
 
-                       if (user.uid == 0 || 
-                           user_in_list(uidtoname(user.uid),
-                                        lp_printer_admin(snum)))
-                               return WERR_OK;
+                       /* if the user is not root and not a printer admin, then fail */
                        
-                       close_printer_handle(p, handle);
-                       return WERR_ACCESS_DENIED;
+                       if ( user.uid != 0
+                            && !user_in_list(uidtoname(user.uid), lp_printer_admin(snum)) )
+                       {
+                               close_printer_handle(p, handle);
+                               return WERR_ACCESS_DENIED;
+                       }
+                       
+                       printer_default->access_required = SERVER_ACCESS_ADMINISTER;
+               }
+               else
+               {
+                       printer_default->access_required = SERVER_ACCESS_ENUMERATE;
                }
 
+               DEBUG(4,("Setting print server access = %s\n", (printer_default->access_required == SERVER_ACCESS_ADMINISTER) 
+                       ? "SERVER_ACCESS_ADMINISTER" : "SERVER_ACCESS_ENUMERATE" ));
+                       
                /* We fall through to return WERR_OK */
                
        }
@@ -1296,84 +1635,24 @@ Can't find printer handle we created for printer %s\n", name ));
                else
                        printer_default->access_required = PRINTER_ACCESS_USE;
 
-               DEBUG(4,("Setting printer access=%x\n", printer_default->access_required));
-               Printer->access_granted = printer_default->access_required;
-
-               /*
-                * If we have a default device pointer in the
-                * printer_default struct, then we need to get
-                * the printer info from the tdb and if there is
-                * no default devicemode there then we do a *SET*
-                * here ! This is insanity.... JRA.
-                */
-
-               /*
-                * If the openprinterex rpc call contains a devmode,
-                * it's a per-user one. This per-user devmode is derivated
-                * from the global devmode. Openprinterex() contains a per-user 
-                * devmode for when you do EMF printing and spooling.
-                * In the EMF case, the NT workstation is only doing half the job
-                * of rendering the page. The other half is done by running the printer
-                * driver on the server.
-                * The EMF file doesn't contain the page description (paper size, orientation, ...).
-                * The EMF file only contains what is to be printed on the page.
-                * So in order for the server to know how to print, the NT client sends
-                * a devicemode attached to the openprinterex call.
-                * But this devicemode is short lived, it's only valid for the current print job.
-                *
-                * If Samba would have supported EMF spooling, this devicemode would
-                * have been attached to the handle, to sent it to the driver to correctly
-                * rasterize the EMF file.
-                *
-                * As Samba only supports RAW spooling, we only receive a ready-to-print file,
-                * we just act as a pass-thru between windows and the printer.
-                *
-                * In order to know that Samba supports only RAW spooling, NT has to call
-                * getprinter() at level 2 (attribute field) or NT has to call startdoc()
-                * and until NT sends a RAW job, we refuse it.
-                *
-                * But to call getprinter() or startdoc(), you first need a valid handle,
-                * and to get an handle you have to call openprintex(). Hence why you have
-                * a devicemode in the openprinterex() call.
-                *
-                *
-                * Differences between NT4 and NT 2000.
-                * NT4:
-                * ---
-                * On NT4, you only have a global devicemode. This global devicemode can be changed
-                * by the administrator (or by a user with enough privs). Everytime a user
-                * wants to print, the devicemode is resetted to the default. In Word, everytime
-                * you print, the printer's characteristics are always reset to the global devicemode.
-                *
-                * NT 2000:
-                * -------
-                * In W2K, there is the notion of per-user devicemode. The first time you use
-                * a printer, a per-user devicemode is build from the global devicemode.
-                * If you change your per-user devicemode, it is saved in the registry, under the
-                * H_KEY_CURRENT_KEY sub_tree. So that everytime you print, you have your default
-                * printer preferences available.
-                *
-                * To change the per-user devicemode: it's the "Printing Preferences ..." button
-                * on the General Tab of the printer properties windows.
-                *
-                * To change the global devicemode: it's the "Printing Defaults..." button
-                * on the Advanced Tab of the printer properties window.
-                *
-                * JFM.
-                */
-
-
+               DEBUG(4,("Setting printer access = %s\n", (printer_default->access_required == PRINTER_ACCESS_ADMINISTER) 
+                       ? "PRINTER_ACCESS_ADMINISTER" : "PRINTER_ACCESS_USE" ));
 
-#if 0
-               if (printer_default->devmode_cont.devmode != NULL) {
-                       result = printer_write_default_dev( snum, printer_default);
-                       if (result != 0) {
-                               close_printer_handle(p, handle);
-                               return result;
-                       }
-               }
-#endif
        }
+       
+       Printer->access_granted = printer_default->access_required;
+       
+       /* 
+        * If the client sent a devmode in the OpenPrinter() call, then
+        * save it here in case we get a job submission on this handle
+        */
+       
+        if ( (Printer->printer_type != PRINTER_HANDLE_IS_PRINTSERVER)
+               && q_u->printer_default.devmode_cont.devmode_ptr )
+        { 
+               convert_devicemode( Printer->dev.handlename, q_u->printer_default.devmode_cont.devmode,
+                       &Printer->nt_devmode );
+        }
 
        return WERR_OK;
 }
@@ -1599,8 +1878,11 @@ WERROR _spoolss_deleteprinterdriver(pipes_struct *p, SPOOL_Q_DELETEPRINTERDRIVER
        fstring                         driver;
        fstring                         arch;
        NT_PRINTER_DRIVER_INFO_LEVEL    info;
+       NT_PRINTER_DRIVER_INFO_LEVEL    info_win2k;
        int                             version;
        struct current_user             user;
+       WERROR                          status;
+       WERROR                          status_win2k = WERR_ACCESS_DENIED;
        
        get_current_user(&user, p);
         
@@ -1608,25 +1890,58 @@ WERROR _spoolss_deleteprinterdriver(pipes_struct *p, SPOOL_Q_DELETEPRINTERDRIVER
        unistr2_to_ascii(arch,   &q_u->arch,   sizeof(arch)-1   );
        
        /* check that we have a valid driver name first */
-       if ((version=get_version_id(arch)) == -1) {
-               /* this is what NT returns */
+       
+       if ((version=get_version_id(arch)) == -1) 
                return WERR_INVALID_ENVIRONMENT;
+                               
+       ZERO_STRUCT(info);
+       ZERO_STRUCT(info_win2k);
+       
+       if (!W_ERROR_IS_OK(get_a_printer_driver(&info, 3, driver, arch, version))) 
+       {
+               /* try for Win2k driver if "Windows NT x86" */
+               
+               if ( version == 2 ) {
+                       version = 3;
+                       if (!W_ERROR_IS_OK(get_a_printer_driver(&info, 3, driver, arch, version))) {
+                               status = WERR_UNKNOWN_PRINTER_DRIVER;
+                               goto done;
+                       }
+               }
        }
        
-       /* if they said "Windows NT x86", then try for version 2 & 3 */
+       if (printer_driver_in_use(info.info_3)) {
+               status = WERR_PRINTER_DRIVER_IN_USE;
+               goto done;
+       }
        
        if ( version == 2 )
-               version = DRIVER_ANY_VERSION;
+       {               
+               if (W_ERROR_IS_OK(get_a_printer_driver(&info_win2k, 3, driver, arch, 3)))
+               {
+                       /* if we get to here, we now have 2 driver info structures to remove */
+                       /* remove the Win2k driver first*/
                
-       ZERO_STRUCT(info);
+                       status_win2k = delete_printer_driver(info_win2k.info_3, &user, 3, False );
+                       free_a_printer_driver( info_win2k, 3 );
+               
+                       /* this should not have failed---if it did, report to client */
+                       if ( !W_ERROR_IS_OK(status_win2k) )
+                               goto done;
+               }
+       }
        
-       if (!W_ERROR_IS_OK(get_a_printer_driver(&info, 3, driver, arch, version)))
-               return WERR_UNKNOWN_PRINTER_DRIVER;
+       status = delete_printer_driver(info.info_3, &user, version, False);
+       
+       /* if at least one of the deletes succeeded return OK */
        
-       if (printer_driver_in_use(info.info_3))
-               return WERR_PRINTER_DRIVER_IN_USE;
+       if ( W_ERROR_IS_OK(status) || W_ERROR_IS_OK(status_win2k) )
+               status = WERR_OK;
+       
+done:
+       free_a_printer_driver( info, 3 );
 
-       return delete_printer_driver(info.info_3, &user, DRIVER_ANY_VERSION, False);
+       return status;
 }
 
 /********************************************************************
@@ -1638,10 +1953,13 @@ WERROR _spoolss_deleteprinterdriverex(pipes_struct *p, SPOOL_Q_DELETEPRINTERDRIV
        fstring                         driver;
        fstring                         arch;
        NT_PRINTER_DRIVER_INFO_LEVEL    info;
+       NT_PRINTER_DRIVER_INFO_LEVEL    info_win2k;
        int                             version;
        uint32                          flags = q_u->delete_flags;
        BOOL                            delete_files;
        struct current_user             user;
+       WERROR                          status;
+       WERROR                          status_win2k = WERR_ACCESS_DENIED;
        
        get_current_user(&user, p);
        
@@ -1656,17 +1974,36 @@ WERROR _spoolss_deleteprinterdriverex(pipes_struct *p, SPOOL_Q_DELETEPRINTERDRIV
        
        if ( flags & DPD_DELETE_SPECIFIC_VERSION )
                version = q_u->version;
-       else if ( version == 2 )
-               /* if they said "Windows NT x86", then try for version 2 & 3 */
-               version = DRIVER_ANY_VERSION;
                
        ZERO_STRUCT(info);
+       ZERO_STRUCT(info_win2k);
+               
+       status = get_a_printer_driver(&info, 3, driver, arch, version);
        
-       if (!W_ERROR_IS_OK(get_a_printer_driver(&info, 3, driver, arch, version))) 
-               return WERR_UNKNOWN_PRINTER_DRIVER;
+       if ( !W_ERROR_IS_OK(status) ) 
+       {
+               /* 
+                * if the client asked for a specific version, 
+                * or this is something other than Windows NT x86,
+                * then we've failed 
+                */
+               
+               if ( (flags&DPD_DELETE_SPECIFIC_VERSION) || (version !=2) )
+                       goto done;
+                       
+               /* try for Win2k driver if "Windows NT x86" */
+               
+               version = 3;
+               if (!W_ERROR_IS_OK(get_a_printer_driver(&info, 3, driver, arch, version))) {
+                       status = WERR_UNKNOWN_PRINTER_DRIVER;
+                       goto done;
+               }
+       }
                
-       if ( printer_driver_in_use(info.info_3) )
-               return WERR_PRINTER_DRIVER_IN_USE;
+       if ( printer_driver_in_use(info.info_3) ) {
+               status = WERR_PRINTER_DRIVER_IN_USE;
+               goto done;
+       }
        
        /* 
         * we have a couple of cases to consider. 
@@ -1682,24 +2019,119 @@ WERROR _spoolss_deleteprinterdriverex(pipes_struct *p, SPOOL_Q_DELETEPRINTERDRIV
        
        delete_files = flags & (DPD_DELETE_ALL_FILES|DPD_DELETE_UNUSED_FILES);
        
-       if ( delete_files ) 
-       {
-               /* fail if any files are in use and DPD_DELETE_ALL_FILES is set */
+       /* fail if any files are in use and DPD_DELETE_ALL_FILES is set */
+               
+       if ( delete_files && printer_driver_files_in_use(info.info_3) & (flags&DPD_DELETE_ALL_FILES) ) {
+               /* no idea of the correct error here */
+               status = WERR_ACCESS_DENIED;    
+               goto done;
+       }
+
+                       
+       /* also check for W32X86/3 if necessary; maybe we already have? */
+               
+       if ( (version == 2) && ((flags&DPD_DELETE_SPECIFIC_VERSION) != DPD_DELETE_SPECIFIC_VERSION)  ) {
+               if (W_ERROR_IS_OK(get_a_printer_driver(&info_win2k, 3, driver, arch, 3))) 
+               {
+                       
+                       if ( delete_files && printer_driver_files_in_use(info_win2k.info_3) & (flags&DPD_DELETE_ALL_FILES) ) {
+                               /* no idea of the correct error here */
+                               free_a_printer_driver( info_win2k, 3 );
+                               status = WERR_ACCESS_DENIED;    
+                               goto done;
+                       }
+               
+                       /* if we get to here, we now have 2 driver info structures to remove */
+                       /* remove the Win2k driver first*/
                
-               if ( printer_driver_files_in_use(info.info_3) & (flags&DPD_DELETE_ALL_FILES) )
-                       /* no idea of the correct error here */
-                       return WERR_ACCESS_DENIED;      
+                       status_win2k = delete_printer_driver(info_win2k.info_3, &user, 3, delete_files);
+                       free_a_printer_driver( info_win2k, 3 );
+                               
+                       /* this should not have failed---if it did, report to client */
+                               
+                       if ( !W_ERROR_IS_OK(status_win2k) )
+                               goto done;
+               }
+       }
+
+       status = delete_printer_driver(info.info_3, &user, version, delete_files);
+
+       if ( W_ERROR_IS_OK(status) || W_ERROR_IS_OK(status_win2k) )
+               status = WERR_OK;
+done:
+       free_a_printer_driver( info, 3 );
+       
+       return status;
+}
+
+
+/****************************************************************************
+ Internal routine for retreiving printerdata
+ ***************************************************************************/
+
+static WERROR get_printer_dataex( TALLOC_CTX *ctx, NT_PRINTER_INFO_LEVEL *printer, 
+                                  char *key, char *value, uint32 *type, uint8 **data, 
+                                 uint32 *needed, uint32 in_size  )
+{
+       REGISTRY_VALUE          *val;
+       int                     size, data_len;
+       
+       if ( !(val = get_printer_data( printer->info_2, key, value)) )
+               return WERR_BADFILE;
+       
+       *type = regval_type( val );
+
+       DEBUG(5,("get_printer_dataex: allocating %d\n", in_size));
+
+       size = regval_size( val );
+       
+       /* copy the min(in_size, len) */
+       
+       if ( in_size ) {
+               data_len = (size > in_size) ? in_size : size*sizeof(uint8);
+               if ( (*data  = (uint8 *)talloc_memdup(ctx, regval_data_p(val), data_len)) == NULL )
+                       return WERR_NOMEM;
        }
+       else
+               *data = NULL;
+
+       *needed = size;
+       
+       DEBUG(5,("get_printer_dataex: copy done\n"));
+
+       return WERR_OK;
+}
+
+/****************************************************************************
+ Internal routine for removing printerdata
+ ***************************************************************************/
 
-       return delete_printer_driver(info.info_3, &user, version, delete_files);
+static WERROR delete_printer_dataex( NT_PRINTER_INFO_LEVEL *printer, char *key, char *value )
+{
+       delete_printer_data( printer->info_2, key, value );
+       
+       return mod_a_printer(*printer, 2);
 }
 
+/****************************************************************************
+ Internal routine for storing printerdata
+ ***************************************************************************/
+
+static WERROR set_printer_dataex( NT_PRINTER_INFO_LEVEL *printer, char *key, char *value, 
+                                  uint32 type, uint8 *data, int real_len  )
+{
+       delete_printer_data( printer->info_2, key, value );
+       
+       add_printer_data( printer->info_2, key, value, type, data, real_len );
+       
+       return mod_a_printer(*printer, 2);
+}
 
 /********************************************************************
  GetPrinterData on a printer server Handle.
 ********************************************************************/
 
-static BOOL getprinterdata_printer_server(TALLOC_CTX *ctx, fstring value, uint32 *type, uint8 **data, uint32 *needed, uint32 in_size)
+static WERROR getprinterdata_printer_server(TALLOC_CTX *ctx, fstring value, uint32 *type, uint8 **data, uint32 *needed, uint32 in_size)
 {              
        int i;
        
@@ -1708,50 +2140,50 @@ static BOOL getprinterdata_printer_server(TALLOC_CTX *ctx, fstring value, uint32
        if (!strcmp(value, "W3SvcInstalled")) {
                *type = 0x4;
                if((*data = (uint8 *)talloc_zero(ctx, 4*sizeof(uint8) )) == NULL)
-                       return False;
-               *needed = 0x4;                  
-               return True;
+                       return WERR_NOMEM;
+               *needed = 0x4;
+               return WERR_OK;
        }
 
        if (!strcmp(value, "BeepEnabled")) {
                *type = 0x4;
                if((*data = (uint8 *)talloc(ctx, 4*sizeof(uint8) )) == NULL)
-                       return False;
+                       return WERR_NOMEM;
                SIVAL(*data, 0, 0x00);
                *needed = 0x4;                  
-               return True;
+               return WERR_OK;
        }
 
        if (!strcmp(value, "EventLog")) {
                *type = 0x4;
                if((*data = (uint8 *)talloc(ctx, 4*sizeof(uint8) )) == NULL)
-                       return False;
+                       return WERR_NOMEM;
                /* formally was 0x1b */
                SIVAL(*data, 0, 0x0);
                *needed = 0x4;                  
-               return True;
+               return WERR_OK;
        }
 
        if (!strcmp(value, "NetPopup")) {
                *type = 0x4;
                if((*data = (uint8 *)talloc(ctx, 4*sizeof(uint8) )) == NULL)
-                       return False;
+                       return WERR_NOMEM;
                SIVAL(*data, 0, 0x00);
                *needed = 0x4;
-               return True;
+               return WERR_OK;
        }
 
        if (!strcmp(value, "MajorVersion")) {
                *type = 0x4;
                if((*data = (uint8 *)talloc(ctx, 4*sizeof(uint8) )) == NULL)
-                       return False;
+                       return WERR_NOMEM;
 #ifndef EMULATE_WIN2K_HACK /* JERRY */
                SIVAL(*data, 0, 2);
 #else
                SIVAL(*data, 0, 3);
 #endif
                *needed = 0x4;
-               return True;
+               return WERR_OK;
        }
 
        if (!strcmp(value, "DefaultSpoolDirectory")) {
@@ -1761,7 +2193,7 @@ static BOOL getprinterdata_printer_server(TALLOC_CTX *ctx, fstring value, uint32
                *type = 0x1;                    
                *needed = 2*(strlen(string)+1);         
                if((*data  = (uint8 *)talloc(ctx, ((*needed > in_size) ? *needed:in_size) *sizeof(uint8))) == NULL)
-                       return False;
+                       return WERR_NOMEM;
                memset(*data, 0, (*needed > in_size) ? *needed:in_size);
                
                /* it's done by hand ready to go on the wire */
@@ -1769,7 +2201,7 @@ static BOOL getprinterdata_printer_server(TALLOC_CTX *ctx, fstring value, uint32
                        (*data)[2*i]=string[i];
                        (*data)[2*i+1]='\0';
                }                       
-               return True;
+               return WERR_OK;
        }
 
        if (!strcmp(value, "Architecture")) {                   
@@ -1777,92 +2209,36 @@ static BOOL getprinterdata_printer_server(TALLOC_CTX *ctx, fstring value, uint32
                *type = 0x1;                    
                *needed = 2*(strlen(string)+1); 
                if((*data  = (uint8 *)talloc(ctx, ((*needed > in_size) ? *needed:in_size) *sizeof(uint8))) == NULL)
-                       return False;
+                       return WERR_NOMEM;
                memset(*data, 0, (*needed > in_size) ? *needed:in_size);
                for (i=0; i<strlen(string); i++) {
                        (*data)[2*i]=string[i];
                        (*data)[2*i+1]='\0';
                }                       
-               return True;
+               return WERR_OK;
        }
        
-       return False;
+       return WERR_INVALID_PARAM;
 }
 
-/********************************************************************
- GetPrinterData on a printer Handle.
-********************************************************************/
-
-static BOOL getprinterdata_printer(pipes_struct *p, TALLOC_CTX *ctx, POLICY_HND *handle,
-                               fstring value, uint32 *type,
-                               uint8 **data, uint32 *needed, uint32 in_size )
-{
-       NT_PRINTER_INFO_LEVEL *printer = NULL;
-       int snum=0;
-       uint8 *idata=NULL;
-       uint32 len;
-       Printer_entry *Printer = find_printer_index_by_hnd(p, handle);
-       
-       DEBUG(5,("getprinterdata_printer\n"));
-
-       if (!Printer) {
-               DEBUG(2,("getprinterdata_printer: Invalid handle (%s:%u:%u).\n", OUR_HANDLE(handle)));
-               return False;
-       }
-
-       if(!get_printer_snum(p, handle, &snum))
-               return False;
-
-       if (!W_ERROR_IS_OK(get_a_printer(&printer, 2, lp_servicename(snum))))
-               return False;
-
-       if (!get_specific_param(*printer, 2, value, &idata, type, &len)) {
-               free_a_printer(&printer, 2);
-               return False;
-       }
-
-       free_a_printer(&printer, 2);
-
-       DEBUG(5,("getprinterdata_printer:allocating %d\n", in_size));
-
-       if (in_size) {
-               if((*data  = (uint8 *)talloc(ctx, in_size *sizeof(uint8) )) == NULL) {
-                       return False;
-               }
-
-               memset(*data, 0, in_size *sizeof(uint8));
-               /* copy the min(in_size, len) */
-               memcpy(*data, idata, (len>in_size)?in_size:len *sizeof(uint8));
-       } else {
-               *data = NULL;
-       }
-
-       *needed = len;
-       
-       DEBUG(5,("getprinterdata_printer:copy done\n"));
-                       
-       SAFE_FREE(idata);
-       
-       return True;
-}      
-
 /********************************************************************
  * spoolss_getprinterdata
  ********************************************************************/
 
 WERROR _spoolss_getprinterdata(pipes_struct *p, SPOOL_Q_GETPRINTERDATA *q_u, SPOOL_R_GETPRINTERDATA *r_u)
 {
-       POLICY_HND *handle = &q_u->handle;
-       UNISTR2 *valuename = &q_u->valuename;
-       uint32 in_size = q_u->size;
-       uint32 *type = &r_u->type;
-       uint32 *out_size = &r_u->size;
-       uint8 **data = &r_u->data;
-       uint32 *needed = &r_u->needed;
-
-       fstring value;
-       BOOL found=False;
-       Printer_entry *Printer = find_printer_index_by_hnd(p, handle);
+       POLICY_HND      *handle = &q_u->handle;
+       UNISTR2         *valuename = &q_u->valuename;
+       uint32          in_size = q_u->size;
+       uint32          *type = &r_u->type;
+       uint32          *out_size = &r_u->size;
+       uint8           **data = &r_u->data;
+       uint32          *needed = &r_u->needed;
+       WERROR          status;
+       fstring         value;
+       Printer_entry   *Printer = find_printer_index_by_hnd(p, handle);
+       NT_PRINTER_INFO_LEVEL   *printer = NULL;
+       int             snum = 0;
        
        /*
         * Reminder: when it's a string, the length is in BYTES
@@ -1871,57 +2247,74 @@ WERROR _spoolss_getprinterdata(pipes_struct *p, SPOOL_Q_GETPRINTERDATA *q_u, SPO
         * JFM, 4/19/1999
         */
 
-       *out_size=in_size;
+       *out_size = in_size;
 
        /* in case of problem, return some default values */
-       *needed=0;
-       *type=0;
+       
+       *needed = 0;
+       *type   = 0;
        
        DEBUG(4,("_spoolss_getprinterdata\n"));
        
-       if (!Printer) {
-               if((*data=(uint8 *)talloc_zero(p->mem_ctx, 4*sizeof(uint8))) == NULL)
-                       return WERR_NOMEM;
+       if ( !Printer ) {
                DEBUG(2,("_spoolss_getprinterdata: Invalid handle (%s:%u:%u).\n", OUR_HANDLE(handle)));
-               return WERR_BADFID;
+               status = WERR_BADFID;
+               goto done;
        }
        
        unistr2_to_ascii(value, valuename, sizeof(value)-1);
        
-       if (Printer->printer_type == PRINTER_HANDLE_IS_PRINTSERVER)
-               found=getprinterdata_printer_server(p->mem_ctx, value, type, data, needed, *out_size);
+       if ( Printer->printer_type == PRINTER_HANDLE_IS_PRINTSERVER )
+               status = getprinterdata_printer_server( p->mem_ctx, value, type, data, needed, *out_size );
        else
-               found= getprinterdata_printer(p, p->mem_ctx, handle, value, type, data, needed, *out_size);
+       {
+               if ( !get_printer_snum(p,handle, &snum) ) {
+                       status = WERR_BADFID;
+                       goto done;
+               }
 
-       if (found==False) {
-               DEBUG(5, ("value not found, allocating %d\n", *out_size));
+               status = get_a_printer(&printer, 2, lp_servicename(snum));
+               if ( !W_ERROR_IS_OK(status) )
+                       goto done;
+                       
+               status = get_printer_dataex( p->mem_ctx, printer, SPOOL_PRINTERDATA_KEY, value, type, data, needed, *out_size );
+       }
+
+       if (*needed > *out_size)
+               status = WERR_MORE_DATA;
+       
+done:
+       if ( !W_ERROR_IS_OK(status) ) 
+       {
+               DEBUG(5, ("error: allocating %d\n", *out_size));
+               
                /* reply this param doesn't exist */
-               if (*out_size) {
-                       if((*data=(uint8 *)talloc_zero(p->mem_ctx, *out_size*sizeof(uint8))) == NULL)
+               
+               if ( *out_size ) {
+                       if((*data=(uint8 *)talloc_zero(p->mem_ctx, *out_size*sizeof(uint8))) == NULL) {
+                               if ( printer ) 
+                                       free_a_printer( &printer, 2 );
                                return WERR_NOMEM;
-               } else {
+               } 
+               } 
+               else {
                        *data = NULL;
                }
-
-               /* error depends on handle type */
-
-               if (Printer->printer_type == PRINTER_HANDLE_IS_PRINTSERVER)
-                       return WERR_INVALID_PARAM;
-               else 
-                       return WERR_BADFILE;
        }
        
-       if (*needed > *out_size)
-               return WERR_MORE_DATA;
-       else 
-               return WERR_OK;
+       /* cleanup & exit */
+
+       if ( printer )
+               free_a_printer( &printer, 2 );
+       
+       return status;
 }
 
 /*********************************************************
  Connect to the client machine.
 **********************************************************/
 
-static BOOL spoolss_connect_to_client(struct cli_state *the_cli, char *remote_machine)
+static BOOL spoolss_connect_to_client(struct cli_state *the_cli, const char *remote_machine)
 {
        extern pstring global_myname;
 
@@ -1952,6 +2345,7 @@ static BOOL spoolss_connect_to_client(struct cli_state *the_cli, char *remote_ma
        if (!attempt_netbios_session_request(the_cli, global_myname, remote_machine, &the_cli->dest_ip)) {
                DEBUG(0,("connect_to_client: machine %s rejected the NetBIOS session request.\n", 
                        remote_machine));
+               cli_shutdown(the_cli);
                return False;
        }
 
@@ -1996,7 +2390,7 @@ static BOOL spoolss_connect_to_client(struct cli_state *the_cli, char *remote_ma
         * Now start the NT Domain stuff :-).
         */
 
-       if(cli_nt_session_open(the_cli, PIPE_SPOOLSS) == False) {
+       if(cli_nt_session_open(the_cli, PI_SPOOLSS) == False) {
                DEBUG(0,("connect_to_client: unable to open the domain client session to machine %s. Error was : %s.\n", remote_machine, cli_errstr(the_cli)));
                cli_nt_session_close(the_cli);
                cli_ulogoff(the_cli);
@@ -2011,7 +2405,7 @@ static BOOL spoolss_connect_to_client(struct cli_state *the_cli, char *remote_ma
  Connect to the client.
 ****************************************************************************/
 
-static BOOL srv_spoolss_replyopenprinter(char *printer, uint32 localprinter, uint32 type, POLICY_HND *handle)
+static BOOL srv_spoolss_replyopenprinter(int snum, const char *printer, uint32 localprinter, uint32 type, POLICY_HND *handle)
 {
        WERROR result;
 
@@ -2027,9 +2421,20 @@ static BOOL srv_spoolss_replyopenprinter(char *printer, uint32 localprinter, uin
                if(!spoolss_connect_to_client(&notify_cli, unix_printer))
                        return False;
                        
-               message_register(MSG_PRINTER_NOTIFY2, receive_notify2_message);
+               message_register(MSG_PRINTER_NOTIFY2, receive_notify2_message_list);
+               /* Tell the connections db we're now interested in printer
+                * notify messages. */
+               register_message_flags( True, FLAG_MSG_PRINTING );
        }
 
+       /* 
+        * Tell the specific printing tdb we want messages for this printer
+        * by registering our PID.
+        */
+
+       if (!print_notify_register_pid(snum))
+               DEBUG(0,("print_notify_register_pid: Failed to register our pid for printer %s\n", printer ));
+
        smb_connections++;
 
        result = cli_spoolss_reply_open_printer(&notify_cli, notify_cli.mem_ctx, printer, localprinter, 
@@ -2060,6 +2465,7 @@ WERROR _spoolss_rffpcnex(pipes_struct *p, SPOOL_Q_RFFPCNEX *q_u, SPOOL_R_RFFPCNE
        uint32 options = q_u->options;
        UNISTR2 *localmachine = &q_u->localmachine;
        uint32 printerlocal = q_u->printerlocal;
+       int snum;
        SPOOL_NOTIFY_OPTION *option = q_u->option;
 
        /* store the notify value in the printer struct */
@@ -2071,6 +2477,9 @@ WERROR _spoolss_rffpcnex(pipes_struct *p, SPOOL_Q_RFFPCNEX *q_u, SPOOL_R_RFFPCNE
                return WERR_BADFID;
        }
 
+       if ( (Printer->printer_type == PRINTER_HANDLE_IS_PRINTER) && !get_printer_snum(p, handle, &snum) )
+               return WERR_BADFID;
+
        Printer->notify.flags=flags;
        Printer->notify.options=options;
        Printer->notify.printerlocal=printerlocal;
@@ -2085,7 +2494,7 @@ WERROR _spoolss_rffpcnex(pipes_struct *p, SPOOL_Q_RFFPCNEX *q_u, SPOOL_R_RFFPCNE
 
        /* Connect to the client machine and send a ReplyOpenPrinter */
 
-       if(!srv_spoolss_replyopenprinter(Printer->notify.localmachine,
+       if(!srv_spoolss_replyopenprinter(snum, Printer->notify.localmachine,
                                        Printer->notify.printerlocal, 1,
                                        &Printer->notify.client_hnd))
                return WERR_SERVER_UNAVAILABLE;
@@ -2427,8 +2836,8 @@ static void spoolss_notify_security_desc(int snum,
                                         NT_PRINTER_INFO_LEVEL *printer,
                                         TALLOC_CTX *mem_ctx)
 {
-       data->notify_data.data.length=0;
-       data->notify_data.data.string = NULL;
+       data->notify_data.sd.size = printer->info_2->secdesc_buf->len;
+       data->notify_data.sd.desc = dup_sec_desc( mem_ctx, printer->info_2->secdesc_buf->sec ) ;
 }
 
 /*******************************************************************
@@ -3434,47 +3843,20 @@ static void free_dev_mode(DEVICEMODE *dev)
        SAFE_FREE(dev); 
 }
 
+
 /****************************************************************************
- Create a DEVMODE struct. Returns malloced memory.
+ Convert an NT_DEVICEMODE to a DEVICEMODE structure.  Both pointers 
+ should be valid upon entry
 ****************************************************************************/
 
-DEVICEMODE *construct_dev_mode(int snum)
+static BOOL convert_nt_devicemode( DEVICEMODE *devmode, NT_DEVICEMODE *ntdevmode )
 {
-       char adevice[32];
-       char aform[32];
-       NT_PRINTER_INFO_LEVEL *printer = NULL;
-       NT_DEVICEMODE *ntdevmode = NULL;
-       DEVICEMODE *devmode = NULL;
-
-       DEBUG(7,("construct_dev_mode\n"));
-       
-       DEBUGADD(8,("getting printer characteristics\n"));
-
-       if ((devmode = (DEVICEMODE *)malloc(sizeof(DEVICEMODE))) == NULL) {
-               DEBUG(2,("construct_dev_mode: malloc fail.\n"));
-               return NULL;
-       }
-
-       ZERO_STRUCTP(devmode);  
-
-       if (!W_ERROR_IS_OK(get_a_printer(&printer, 2, lp_servicename(snum))))
-               goto fail;
-
-       if (printer->info_2->devmode)
-               ntdevmode = dup_nt_devicemode(printer->info_2->devmode);
-
-       if (ntdevmode == NULL) {
-               DEBUG(5, ("BONG! There was no device mode!\n"));
-               goto fail;
-       }
-
-       DEBUGADD(8,("loading DEVICEMODE\n"));
-
-       slprintf(adevice, sizeof(adevice)-1, printer->info_2->printername);
-       init_unistr(&devmode->devicename, adevice);
+       if ( !devmode || !ntdevmode )
+               return False;
+               
+       init_unistr(&devmode->devicename, ntdevmode->devicename);
 
-       slprintf(aform, sizeof(aform)-1, ntdevmode->formname);
-       init_unistr(&devmode->formname, aform);
+       init_unistr(&devmode->formname, ntdevmode->formname);
 
        devmode->specversion      = ntdevmode->specversion;
        devmode->driverversion    = ntdevmode->driverversion;
@@ -3502,23 +3884,51 @@ DEVICEMODE *construct_dev_mode(int snum)
 
        if (ntdevmode->private != NULL) {
                if ((devmode->private=(uint8 *)memdup(ntdevmode->private, ntdevmode->driverextra)) == NULL)
-                       goto fail;
+                       return False;
        }
+       
+       return True;
+}
 
-       free_nt_devicemode(&ntdevmode);
-       free_a_printer(&printer,2);
+/****************************************************************************
+ Create a DEVMODE struct. Returns malloced memory.
+****************************************************************************/
 
-       return devmode;
+DEVICEMODE *construct_dev_mode(int snum)
+{
+       NT_PRINTER_INFO_LEVEL   *printer = NULL;
+       DEVICEMODE              *devmode = NULL;
+       
+       DEBUG(7,("construct_dev_mode\n"));
+       
+       DEBUGADD(8,("getting printer characteristics\n"));
 
-  fail:
+       if (!W_ERROR_IS_OK(get_a_printer(&printer, 2, lp_servicename(snum)))) 
+               return NULL;
 
-       if (ntdevmode)
-               free_nt_devicemode(&ntdevmode);
-       if (printer)
-               free_a_printer(&printer,2);
-       free_dev_mode(devmode);
+       if ( !printer->info_2->devmode ) {
+               DEBUG(5, ("BONG! There was no device mode!\n"));
+               goto done;
+       }
+
+       if ((devmode = (DEVICEMODE *)malloc(sizeof(DEVICEMODE))) == NULL) {
+               DEBUG(2,("construct_dev_mode: malloc fail.\n"));
+               goto done;
+       }
+
+       ZERO_STRUCTP(devmode);  
+       
+       DEBUGADD(8,("loading DEVICEMODE\n"));
+
+       if ( !convert_nt_devicemode( devmode, printer->info_2->devmode ) ) {
+               free_dev_mode( devmode );
+               devmode = NULL;
+       }
+
+done:
+       free_a_printer(&printer,2);
 
-       return NULL;
+       return devmode;
 }
 
 /********************************************************************
@@ -4375,7 +4785,7 @@ static WERROR construct_printer_driver_info_2(DRIVER_INFO_2 *info, int snum, fst
  * convert an array of ascii string to a UNICODE string
  ********************************************************************/
 
-static void init_unistr_array(uint16 **uni_array, fstring *char_array, char *servername)
+static uint32 init_unistr_array(uint16 **uni_array, fstring *char_array, char *servername)
 {
        int i=0;
        int j=0;
@@ -4386,22 +4796,37 @@ static void init_unistr_array(uint16 **uni_array, fstring *char_array, char *ser
        DEBUG(6,("init_unistr_array\n"));
        *uni_array=NULL;
 
-       while (1) {
-               if (char_array == NULL)
+       while (True) 
+       {
+               if ( !char_array )
                        v = "";
-               else {
+               else 
+               {
                        v = char_array[i];
-                       if (!v) v = ""; /* hack to handle null lists */
+                       if (!v) 
+                               v = ""; /* hack to handle null lists */
                }
-               if (strlen(v) == 0) break;
-               slprintf(line, sizeof(line)-1, "\\\\%s%s", servername, v);
+               
+               /* hack to allow this to be used in places other than when generating 
+                  the list of dependent files */
+                  
+               if ( servername )
+                       slprintf( line, sizeof(line)-1, "\\\\%s%s", servername, v );
+               else
+                       pstrcpy( line, v );
+                       
                DEBUGADD(6,("%d:%s:%d\n", i, line, strlen(line)));
-               if((tuary=Realloc(*uni_array, (j+strlen(line)+2)*sizeof(uint16))) == NULL) {
+               
+               if ( (tuary=Realloc(*uni_array, (j+strlen(line)+2)*sizeof(uint16))) == NULL ) {
                        DEBUG(2,("init_unistr_array: Realloc error\n" ));
-                       return;
+                       return 0;
                } else
                        *uni_array = tuary;
-               j += (rpcstr_push((*uni_array+j), line, sizeof(uint16)*strlen(line)+2, 0)/ sizeof(uint16));
+                       
+               if ( !strlen(v) ) 
+                       break;
+               
+               j += (rpcstr_push((*uni_array+j), line, sizeof(uint16)*strlen(line)+2, STR_TERMINATE) / sizeof(uint16));
                i++;
        }
        
@@ -4410,6 +4835,10 @@ static void init_unistr_array(uint16 **uni_array, fstring *char_array, char *ser
        }
        
        DEBUGADD(6,("last one:done\n"));
+
+       /* return size of array in uint16's */
+               
+       return j+1;
 }
 
 /********************************************************************
@@ -4428,29 +4857,29 @@ static void fill_printer_driver_info_3(DRIVER_INFO_3 *info, NT_PRINTER_DRIVER_IN
        init_unistr( &info->name, driver.info_3->name );        
        init_unistr( &info->architecture, driver.info_3->environment );
 
-    if (strlen(driver.info_3->driverpath)) {
-        slprintf(temp, sizeof(temp)-1, "\\\\%s%s", servername, driver.info_3->driverpath);             
-        init_unistr( &info->driverpath, temp );
-    } else
-        init_unistr( &info->driverpath, "" );
+       if (strlen(driver.info_3->driverpath)) {
+               slprintf(temp, sizeof(temp)-1, "\\\\%s%s", servername, driver.info_3->driverpath);              
+               init_unistr( &info->driverpath, temp );
+       } else
+               init_unistr( &info->driverpath, "" );
     
-    if (strlen(driver.info_3->datafile)) {
-        slprintf(temp, sizeof(temp)-1, "\\\\%s%s", servername, driver.info_3->datafile);
-        init_unistr( &info->datafile, temp );
-    } else
-        init_unistr( &info->datafile, "" );
+       if (strlen(driver.info_3->datafile)) {
+               slprintf(temp, sizeof(temp)-1, "\\\\%s%s", servername, driver.info_3->datafile);
+               init_unistr( &info->datafile, temp );
+       } else
+               init_unistr( &info->datafile, "" );
 
-    if (strlen(driver.info_3->configfile)) {
-        slprintf(temp, sizeof(temp)-1, "\\\\%s%s", servername, driver.info_3->configfile);
-        init_unistr( &info->configfile, temp );        
-    } else
-        init_unistr( &info->configfile, "" );
+       if (strlen(driver.info_3->configfile)) {
+               slprintf(temp, sizeof(temp)-1, "\\\\%s%s", servername, driver.info_3->configfile);
+               init_unistr( &info->configfile, temp ); 
+       } else
+               init_unistr( &info->configfile, "" );
 
-    if (strlen(driver.info_3->helpfile)) {
-        slprintf(temp, sizeof(temp)-1, "\\\\%s%s", servername, driver.info_3->helpfile);
-        init_unistr( &info->helpfile, temp );
-    } else
-        init_unistr( &info->helpfile, "" );
+       if (strlen(driver.info_3->helpfile)) {
+               slprintf(temp, sizeof(temp)-1, "\\\\%s%s", servername, driver.info_3->helpfile);
+               init_unistr( &info->helpfile, temp );
+       } else
+               init_unistr( &info->helpfile, "" );
 
        init_unistr( &info->monitorname, driver.info_3->monitorname );
        init_unistr( &info->defaultdatatype, driver.info_3->defaultdatatype );
@@ -4563,8 +4992,8 @@ static void fill_printer_driver_info_6(DRIVER_INFO_6 *info, NT_PRINTER_DRIVER_IN
        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->dependentfiles = NULL;
+       init_unistr_array( &info->dependentfiles, driver.info_3->dependentfiles, servername );
 
        info->previousdrivernames=NULL;
        init_unistr_array(&info->previousdrivernames, &nullstr, servername);
@@ -4587,21 +5016,28 @@ static void fill_printer_driver_info_6(DRIVER_INFO_6 *info, NT_PRINTER_DRIVER_IN
  * fill a printer_info_6 struct
  ********************************************************************/
 
-static WERROR construct_printer_driver_info_6(DRIVER_INFO_6 *info, int snum, fstring servername, fstring architecture, uint32 version)
+static WERROR 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;
-       WERROR status;
+       NT_PRINTER_INFO_LEVEL           *printer = NULL;
+       NT_PRINTER_DRIVER_INFO_LEVEL    driver;
+       WERROR                          status;
+       
        ZERO_STRUCT(driver);
 
        status=get_a_printer(&printer, 2, lp_servicename(snum) );
+       
        DEBUG(8,("construct_printer_driver_info_6: status: %s\n", dos_errstr(status)));
+       
        if (!W_ERROR_IS_OK(status))
                return WERR_INVALID_PRINTER_NAME;
 
-       status=get_a_printer_driver(&driver, 3, printer->info_2->drivername, architecture, version);    
+       status = get_a_printer_driver(&driver, 3, printer->info_2->drivername, architecture, version);
+               
        DEBUG(8,("construct_printer_driver_info_6: status: %s\n", dos_errstr(status)));
-       if (!W_ERROR_IS_OK(status)) {
+       
+       if (!W_ERROR_IS_OK(status)) 
+       {
                /*
                 * Is this a W2k client ?
                 */
@@ -4812,9 +5248,9 @@ WERROR _spoolss_getprinterdriver2(pipes_struct *p, SPOOL_Q_GETPRINTERDRIVER2 *q_
 
        DEBUG(4,("_spoolss_getprinterdriver2\n"));
 
-       *needed=0;
-       *servermajorversion=0;
-       *serverminorversion=0;
+       *needed = 0;
+       *servermajorversion = 0;
+       *serverminorversion = 0;
 
        pstrcpy(servername, get_called_name());
        unistr2_to_ascii(architecture, uni_arch, sizeof(architecture)-1);
@@ -4910,10 +5346,6 @@ WERROR _spoolss_startdocprinter(pipes_struct *p, SPOOL_Q_STARTDOCPRINTER *q_u, S
         * in EMF format.
         *
         * So I add checks like in NT Server ...
-        *
-        * lkclXXXX jean-francois, i love this kind of thing.  oh, well,
-        * there's a bug in NT client-side code, so we'll fix it in the
-        * server-side code. *nnnnnggggh!*
         */
        
        if (info_1->p_datatype != 0) {
@@ -4931,7 +5363,7 @@ WERROR _spoolss_startdocprinter(pipes_struct *p, SPOOL_Q_STARTDOCPRINTER *q_u, S
 
        unistr2_to_ascii(jobname, &info_1->docname, sizeof(jobname));
        
-       Printer->jobid = print_job_start(&user, snum, jobname);
+       Printer->jobid = print_job_start(&user, snum, jobname, Printer->nt_devmode);
 
        /* An error occured in print_job_start() so return an appropriate
           NT error code. */
@@ -4981,6 +5413,13 @@ WERROR _spoolss_writeprinter(pipes_struct *p, SPOOL_Q_WRITEPRINTER *q_u, SPOOL_R
                return WERR_BADFID;
 
        (*buffer_written) = print_job_write(snum, Printer->jobid, (char *)buffer, buffer_size);
+       if (*buffer_written == -1) {
+               r_u->buffer_written = 0;
+               if (errno == ENOSPC)
+                       return WERR_NO_SPOOL_SPACE;
+               else
+                       return WERR_ACCESS_DENIED;
+       }
 
        r_u->buffer_written = q_u->buffer_size2;
 
@@ -5037,13 +5476,31 @@ static WERROR control_printer(POLICY_HND *handle, uint32 command,
 
 /********************************************************************
  * api_spoolss_abortprinter
+ * From MSDN: "Deletes printer's spool file if printer is configured
+ * for spooling"
  ********************************************************************/
 
 WERROR _spoolss_abortprinter(pipes_struct *p, SPOOL_Q_ABORTPRINTER *q_u, SPOOL_R_ABORTPRINTER *r_u)
 {
-       POLICY_HND *handle = &q_u->handle;
-
-       return control_printer(handle, PRINTER_CONTROL_PURGE, p);
+       POLICY_HND      *handle = &q_u->handle;
+       Printer_entry   *Printer = find_printer_index_by_hnd(p, handle);
+       int             snum;
+       struct          current_user user;
+       WERROR          errcode = WERR_OK;
+       
+       if (!Printer) {
+               DEBUG(2,("_spoolss_abortprinter: Invalid handle (%s:%u:%u)\n",OUR_HANDLE(handle)));
+               return WERR_BADFID;
+       }
+       
+       if (!get_printer_snum(p, handle, &snum))
+               return WERR_BADFID;
+       
+       get_current_user( &user, p );   
+       
+       print_job_delete( &user, snum, Printer->jobid, &errcode );      
+       
+       return errcode;
 }
 
 /********************************************************************
@@ -5171,23 +5628,17 @@ static BOOL add_printer_hook(NT_PRINTER_INFO_LEVEL *printer)
        char *cmd = lp_addprinter_cmd();
        char **qlines;
        pstring command;
-       pstring driverlocation;
        int numlines;
        int ret;
        int fd;
        fstring remote_machine = "%m";
 
-       /* build driver path... only 9X architecture is needed for legacy reasons */
-       slprintf(driverlocation, sizeof(driverlocation)-1, "\\\\%s\\print$\\WIN40\\0",
-                       get_called_name());
-       /* change \ to \\ for the shell */
-       all_string_sub(driverlocation,"\\","\\\\",sizeof(pstring));
        standard_sub_basic(current_user_info.smb_name, remote_machine,sizeof(remote_machine));
        
        slprintf(command, sizeof(command)-1, "%s \"%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, remote_machine);
+                       printer->info_2->location, printer->info_2->comment, remote_machine);
 
        DEBUG(10,("Running [%s]\n", command));
        ret = smbrun(command, &fd);
@@ -5212,261 +5663,15 @@ static BOOL add_printer_hook(NT_PRINTER_INFO_LEVEL *printer)
 
                /* Send SIGHUP to process group... is there a better way? */
                kill(0, SIGHUP);
-               add_all_printers();
+               
+               /* reload our services immediately */
+               reload_services( False );
        }
 
        file_lines_free(qlines);
        return True;
 }
 
-#if 0  /* JERRY */
-
-/* Return true if two devicemodes are equal */
-
-#define DEVMODE_CHECK_INT(field) \
-    if (d1->field != d2->field) { \
-        DEBUG(10, ("nt_devicemode_equal(): " #field " not equal (%d != %d)\n", \
-            d1->field, d2->field)); \
-        return False; \
-    }
-
-/************************************************************************
- Handy, but currently unused functions
- ***********************************************************************/
-
-static BOOL nt_devicemode_equal(NT_DEVICEMODE *d1, NT_DEVICEMODE *d2)
-{
-       if (!d1 && !d2) goto equal;  /* if both are NULL they are equal */
-
-       if (!d1 ^ !d2) {
-               DEBUG(10, ("nt_devicemode_equal(): pointers not equal\n"));
-               return False; /* if either is exclusively NULL are not equal */
-       }
-
-       if (!strequal(d1->devicename, d2->devicename)) {
-               DEBUG(10, ("nt_devicemode_equal(): device not equal (%s != %s)\n", d1->devicename, d2->devicename));
-               return False;
-       }
-
-       if (!strequal(d1->formname, d2->formname)) {
-               DEBUG(10, ("nt_devicemode_equal(): formname not equal (%s != %s)\n", d1->formname, d2->formname));
-               return False;
-       }
-
-       DEVMODE_CHECK_INT(specversion);
-       DEVMODE_CHECK_INT(driverversion);
-       DEVMODE_CHECK_INT(driverextra);
-       DEVMODE_CHECK_INT(orientation);
-       DEVMODE_CHECK_INT(papersize);
-       DEVMODE_CHECK_INT(paperlength);
-       DEVMODE_CHECK_INT(paperwidth);
-       DEVMODE_CHECK_INT(scale);
-       DEVMODE_CHECK_INT(copies);
-       DEVMODE_CHECK_INT(defaultsource);
-       DEVMODE_CHECK_INT(printquality);
-       DEVMODE_CHECK_INT(color);
-       DEVMODE_CHECK_INT(duplex);
-       DEVMODE_CHECK_INT(yresolution);
-       DEVMODE_CHECK_INT(ttoption);
-       DEVMODE_CHECK_INT(collate);
-       DEVMODE_CHECK_INT(logpixels);
-
-       DEVMODE_CHECK_INT(fields);
-       DEVMODE_CHECK_INT(bitsperpel);
-       DEVMODE_CHECK_INT(pelswidth);
-       DEVMODE_CHECK_INT(pelsheight);
-       DEVMODE_CHECK_INT(displayflags);
-       DEVMODE_CHECK_INT(displayfrequency);
-       DEVMODE_CHECK_INT(icmmethod);
-       DEVMODE_CHECK_INT(icmintent);
-       DEVMODE_CHECK_INT(mediatype);
-       DEVMODE_CHECK_INT(dithertype);
-       DEVMODE_CHECK_INT(reserved1);
-       DEVMODE_CHECK_INT(reserved2);
-       DEVMODE_CHECK_INT(panningwidth);
-       DEVMODE_CHECK_INT(panningheight);
-
-       /* compare the private data if it exists */
-       if (!d1->driverextra && !d2->driverextra) goto equal;
-
-
-       DEVMODE_CHECK_INT(driverextra);
-
-       if (memcmp(d1->private, d2->private, d1->driverextra)) {
-               DEBUG(10, ("nt_devicemode_equal(): private data not equal\n"));
-               return False;
-       }
-
- equal:
-       DEBUG(10, ("nt_devicemode_equal(): devicemodes identical\n"));
-       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) goto equal;
-
-       if ((!p1 && p2) || (p1 && !p2)) {
-               DEBUG(10, ("nt_printer_param_equal(): pointers differ\n"));
-               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)) {
-
-                               if (p1->type != q->type) {
-                                       DEBUG(10, ("nt_printer_param_equal():"
-                                                  "types for %s differ (%d != %d)\n",
-                                                  p1->value, p1->type,
-                                                  q->type));
-                                       break;
-                               }
-
-                               if (p1->data_len != q->data_len) {
-                                       DEBUG(10, ("nt_printer_param_equal():"
-                                                  "len for %s differs (%d != %d)\n",
-                                                  p1->value, p1->data_len,
-                                                  q->data_len));
-                                       break;
-                               }
-
-                               if (memcmp(p1->data, q->data, p1->data_len) == 0) {
-                                       found = True;
-                               } else {
-                                       DEBUG(10, ("nt_printer_param_equal():"
-                                                  "data for %s differs\n", p1->value));
-                               }
-
-                               break;
-                       }
-
-                       q = q->next;
-               }
-
-               if (!found) {
-                       DEBUG(10, ("nt_printer_param_equal(): param %s "
-                                  "does not exist\n", p1->value));
-                       return False;
-               }
-
-               p1 = p1->next;
-       }
-
-       equal:
-
-       DEBUG(10, ("nt_printer_param_equal(): printer params identical\n"));
-       return True;
-}
-
-/********************************************************************
- * Called by update_printer when trying to work out whether to
- * actually update printer info.
- ********************************************************************/
-
-#define PI_CHECK_INT(field) \
-    if (pi1->field != pi2->field) { \
-        DEBUG(10, ("nt_printer_info_level_equal(): " #field " not equal (%d != %d)\n", \
-            pi1->field, pi2->field)); \
-        return False; \
-    }
-
-#define PI_CHECK_STR(field) \
-    if (!strequal(pi1->field, pi2->field)) { \
-        DEBUG(10, ("nt_printer_info_level_equal(): " #field " not equal (%s != %s)\n", \
-            pi1->field, pi2->field)); \
-        return False; \
-    }
-
-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)) {
-               goto equal;
-       }
-
-       if ((!p1 && p2) || (p1 && !p2) ||
-           (!p1->info_2 && p2->info_2) ||
-           (p1->info_2 && !p2->info_2)) {
-               DEBUG(10, ("nt_printer_info_level_equal(): info levels "
-                          "differ\n"));
-               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;
-
-       /* Don't check the attributes as we stomp on the value in
-          check_printer_ok() anyway. */
-
-#if 0
-       PI_CHECK_INT(attributes);
-#endif
-
-       PI_CHECK_INT(priority);
-       PI_CHECK_INT(default_priority);
-       PI_CHECK_INT(starttime);
-       PI_CHECK_INT(untiltime);
-       PI_CHECK_INT(averageppm);
-
-       /* Yuck - don't check the printername or servername as the
-          mod_a_printer() code plays games with them.  You can't
-          change the printername or the sharename through this interface
-          in Samba. */
-
-       PI_CHECK_STR(sharename);
-       PI_CHECK_STR(portname);
-       PI_CHECK_STR(drivername);
-       PI_CHECK_STR(comment);
-       PI_CHECK_STR(location);
-
-       if (!nt_devicemode_equal(pi1->devmode, pi2->devmode)) {
-               return False;
-       }
-
-       PI_CHECK_STR(sepfile);
-       PI_CHECK_STR(printprocessor);
-       PI_CHECK_STR(datatype);
-       PI_CHECK_STR(parameters);
-
-       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;
-       }
-
-       PI_CHECK_INT(changeid);
-       PI_CHECK_INT(c_setprinter);
-       PI_CHECK_INT(setuptime);
-
- equal:
-       DEBUG(10, ("nt_printer_info_level_equal(): infos are identical\n"));
-       return True;
-}
-
-#endif
-
 /********************************************************************
  * Called by spoolss_api_setprinter
  * when updating a printer description.
@@ -5531,6 +5736,13 @@ static WERROR update_printer(pipes_struct *p, POLICY_HND *handle, uint32 level,
                        result =  WERR_NOMEM;
                        goto done;
                }
+
+               /* 
+                * make sure we actually reload the services after 
+                * this as smb.conf could have a new section in it 
+                * .... shouldn't .... but could
+                */
+               reload_services(False); 
        }
 
        /* Do sanity check on the requested changes for Samba */
@@ -5540,22 +5752,6 @@ static WERROR update_printer(pipes_struct *p, POLICY_HND *handle, uint32 level,
                goto done;
        }
 
-#if 0  /* JERRY */
-       
-       /*
-        * Another one of those historical misunderstandings...
-        * This is reminisent of a similar call we had in _spoolss_setprinterdata()
-        * I'm leaving it here as a reminder.  --jerry
-        */
-
-       if (nt_printer_info_level_equal(printer, old_printer)) {
-               DEBUG(3, ("update_printer: printer info has not changed\n"));
-               result = WERR_OK;
-               goto done;
-       }
-
-#endif
-
        /* Check calling user has permission to update printer description */
 
        if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
@@ -5574,43 +5770,22 @@ static WERROR update_printer(pipes_struct *p, POLICY_HND *handle, uint32 level,
        }
        
        /*
-        * Set the DRIVER_INIT info in the tdb; trigger on magic value for the
-        * DEVMODE.displayfrequency, which is not used for printer drivers. This
-        * requires Win32 client code (see other notes elsewhere in the code).
+        * When a *new* driver is bound to a printer, the drivername is used to
+        * lookup previously saved driver initialization info, which is then
+        * bound to the printer, simulating what happens in the Windows arch.
         */
-       if (printer->info_2->devmode &&
-               printer->info_2->devmode->displayfrequency == MAGIC_DISPLAY_FREQUENCY) {
-               DEBUG(10,("update_printer: Save printer driver init data\n"));
-               printer->info_2->devmode->displayfrequency = 0;
-               if (update_driver_init(*printer, 2)!=0) {
-                       DEBUG(10,("update_printer: error updating printer driver init DEVMODE\n"));
-                       result = WERR_ACCESS_DENIED;
-                       goto done;
+       if (!strequal(printer->info_2->drivername, old_printer->info_2->drivername))
+       {
+               if (!set_driver_init(printer, 2)) 
+               {
+                       DEBUG(5,("update_printer: Error restoring driver initialization data for driver [%s]!\n",
+                               printer->info_2->drivername));
                }
                
-               /* we need to reset all driver init data for all printers 
-                  bound to this driver */
-               
-               srv_spoolss_reset_printerdata( printer->info_2->drivername );
-               
-       } else {
-               /*
-                * When a *new* driver is bound to a printer, the drivername is used to
-                * lookup previously saved driver initialization info, which is then
-                * bound to the printer, simulating what happens in the Windows arch.
-                */
-               if (!strequal(printer->info_2->drivername, old_printer->info_2->drivername)){
-                       if (!set_driver_init(printer, 2)) {
-                               DEBUG(5,("update_printer: Error restoring driver initialization data for driver [%s]!\n",
-                                       printer->info_2->drivername));
-                       }
+               DEBUG(10,("update_printer: changing driver [%s]!  Sending event!\n",
+                       printer->info_2->drivername));
                        
-                       DEBUG(10,("update_printer: changing driver [%s]!  Sending event!\n",
-                               printer->info_2->drivername));
-                       notify_printer_driver(snum, printer->info_2->drivername);
-               }
+               notify_printer_driver(snum, printer->info_2->drivername);
        }
 
        /* Update printer info */
@@ -5678,7 +5853,7 @@ WERROR _spoolss_setprinter(pipes_struct *p, SPOOL_Q_SETPRINTER *q_u, SPOOL_R_SET
 WERROR _spoolss_fcpn(pipes_struct *p, SPOOL_Q_FCPN *q_u, SPOOL_R_FCPN *r_u)
 {
        POLICY_HND *handle = &q_u->handle;
-
+       int snum;
        Printer_entry *Printer= find_printer_index_by_hnd(p, handle);
        
        if (!Printer) {
@@ -5686,8 +5861,11 @@ WERROR _spoolss_fcpn(pipes_struct *p, SPOOL_Q_FCPN *q_u, SPOOL_R_FCPN *r_u)
                return WERR_BADFID;
        }
 
+       if (!get_printer_snum(p, handle, &snum))
+               return WERR_BADFID;
+
        if (Printer->notify.client_connected==True)
-               srv_spoolss_replycloseprinter(&Printer->notify.client_hnd);
+               srv_spoolss_replycloseprinter(snum, &Printer->notify.client_hnd);
 
        Printer->notify.flags=0;
        Printer->notify.options=0;
@@ -5867,11 +6045,9 @@ static WERROR enumjobs_level2(print_queue_struct *queue, int snum,
                goto done;
        }
                
-       if (!(devmode = construct_dev_mode(snum))) {
-               *returned = 0;
-               result = WERR_NOMEM;
-               goto done;
-       }
+       /* this should not be a failure condition if the devmode is NULL */
+       
+       devmode = construct_dev_mode(snum);
 
        for (i=0; i<*returned; i++)
                fill_job_info_2(&(info[i]), &queue[i], i, snum, ntprinter,
@@ -6777,15 +6953,17 @@ static WERROR spoolss_addprinterex_level_2( pipes_struct *p, const UNISTR2 *uni_
                return WERR_PRINTER_ALREADY_EXISTS;
        }
 
-       if (*lp_addprinter_cmd() )
+       if (*lp_addprinter_cmd() ) {
                if ( !add_printer_hook(printer) ) {
                        free_a_printer(&printer,2);
                        return WERR_ACCESS_DENIED;
        }
+       }
 
        slprintf(name, sizeof(name)-1, "\\\\%s\\%s", get_called_name(),
              printer->info_2->sharename);
 
+       
        if ((snum = print_queue_snum(printer->info_2->sharename)) == -1) {
                free_a_printer(&printer,2);
                return WERR_ACCESS_DENIED;
@@ -6813,8 +6991,11 @@ static WERROR spoolss_addprinterex_level_2( pipes_struct *p, const UNISTR2 *uni_
         */
 
        if (!devmode)
+       {
                set_driver_init(printer, 2);
-       else {
+       }
+       else 
+       {
                /* A valid devmode was included, convert and link it
                */
                DEBUGADD(10, ("spoolss_addprinterex_level_2: devmode included, converting\n"));
@@ -6824,8 +7005,6 @@ static WERROR spoolss_addprinterex_level_2( pipes_struct *p, const UNISTR2 *uni_
                        return  WERR_NOMEM;
        }
 
-       set_driver_init(printer, 2);
-       
        /* write the ASCII on disk */
        err = mod_a_printer(*printer, 2);
        if (!W_ERROR_IS_OK(err)) {
@@ -7121,38 +7300,38 @@ WERROR _spoolss_getprinterdriverdirectory(pipes_struct *p, SPOOL_Q_GETPRINTERDRI
 WERROR _spoolss_enumprinterdata(pipes_struct *p, SPOOL_Q_ENUMPRINTERDATA *q_u, SPOOL_R_ENUMPRINTERDATA *r_u)
 {
        POLICY_HND *handle = &q_u->handle;
-       uint32 idx = q_u->index;
-       uint32 in_value_len = q_u->valuesize;
-       uint32 in_data_len = q_u->datasize;
+       uint32 idx               = q_u->index;
+       uint32 in_value_len      = q_u->valuesize;
+       uint32 in_data_len       = q_u->datasize;
        uint32 *out_max_value_len = &r_u->valuesize;
-       uint16 **out_value = &r_u->value;
-       uint32 *out_value_len = &r_u->realvaluesize;
-       uint32 *out_type = &r_u->type;
+       uint16 **out_value       = &r_u->value;
+       uint32 *out_value_len    = &r_u->realvaluesize;
+       uint32 *out_type         = &r_u->type;
        uint32 *out_max_data_len = &r_u->datasize;
-       uint8  **data_out = &r_u->data;
-       uint32 *out_data_len = &r_u->realdatasize;
+       uint8  **data_out        = &r_u->data;
+       uint32 *out_data_len     = &r_u->realdatasize;
 
        NT_PRINTER_INFO_LEVEL *printer = NULL;
        
-       fstring value;
+       uint32          param_index;
+       uint32          biggest_valuesize;
+       uint32          biggest_datasize;
+       uint32          data_len;
+       Printer_entry   *Printer = find_printer_index_by_hnd(p, handle);
+       int             snum;
+       WERROR          result;
+       REGISTRY_VALUE  *val;
+       NT_PRINTER_DATA *p_data;
+       int             i, key_index, num_values;
+       int             name_length;
        
-       uint32 param_index;
-       uint32 biggest_valuesize;
-       uint32 biggest_datasize;
-       uint32 data_len;
-       Printer_entry *Printer = find_printer_index_by_hnd(p, handle);
-       int snum;
-       uint8 *data=NULL;
-       uint32 type;
-       WERROR result;
-
-       ZERO_STRUCT(printer);
+       ZERO_STRUCT( printer );
        
-       *out_type=0;
+       *out_type = 0;
 
-       *out_max_data_len=0;
-       *data_out=NULL;
-       *out_data_len=0;
+       *out_max_data_len = 0;
+       *data_out         = NULL;
+       *out_data_len     = 0;
 
        DEBUG(5,("spoolss_enumprinterdata\n"));
 
@@ -7167,103 +7346,133 @@ WERROR _spoolss_enumprinterdata(pipes_struct *p, SPOOL_Q_ENUMPRINTERDATA *q_u, S
        result = get_a_printer(&printer, 2, lp_servicename(snum));
        if (!W_ERROR_IS_OK(result))
                return result;
+               
+       p_data = &printer->info_2->data;        
+       key_index = lookup_printerkey( p_data, SPOOL_PRINTERDATA_KEY );
+
+       result = WERR_OK;
 
        /*
         * The NT machine wants to know the biggest size of value and data
         *
         * cf: MSDN EnumPrinterData remark section
         */
-       if ( (in_value_len==0) && (in_data_len==0) ) {
+        
+       if ( !in_value_len && !in_data_len ) 
+       {
                DEBUGADD(6,("Activating NT mega-hack to find sizes\n"));
 
-               SAFE_FREE(data);
-
-               param_index=0;
-               biggest_valuesize=0;
-               biggest_datasize=0;
+               param_index       = 0;
+               biggest_valuesize = 0;
+               biggest_datasize  = 0;
+                               
+               num_values = regval_ctr_numvals( &p_data->keys[key_index].values );
                
-               while (get_specific_param_by_index(*printer, 2, param_index, value, &data, &type, &data_len)) {
-                       if (strlen(value) > biggest_valuesize) biggest_valuesize=strlen(value);
-                       if (data_len > biggest_datasize) biggest_datasize=data_len;
-
-                       DEBUG(6,("current values: [%d], [%d]\n", biggest_valuesize, biggest_datasize));
-
-                       SAFE_FREE(data);
-                       param_index++;
+               for ( i=0; i<num_values; i++ )
+               {
+                       val = regval_ctr_specific_value( &p_data->keys[key_index].values, i );
+                       
+                       name_length = strlen(val->valuename);
+                       if ( strlen(val->valuename) > biggest_valuesize ) 
+                               biggest_valuesize = name_length;
+                               
+                       if ( val->size > biggest_datasize )
+                               biggest_datasize = val->size;
+                               
+                       DEBUG(6,("current values: [%d], [%d]\n", biggest_valuesize, 
+                               biggest_datasize));
                }
 
-               /* 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;
+               /* the value is an UNICODE string but real_value_size is the length 
+                  in bytes including the trailing 0 */
+                  
+               *out_value_len = 2 * (1+biggest_valuesize);
+               *out_data_len  = biggest_datasize;
 
                DEBUG(6,("final values: [%d], [%d]\n", *out_value_len, *out_data_len));
 
-               free_a_printer(&printer, 2);
-               return WERR_OK;
+               goto done;
        }
        
        /*
         * the value len is wrong in NT sp3
         * that's the number of bytes not the number of unicode chars
         */
+        
+       val = regval_ctr_specific_value( &p_data->keys[key_index].values, idx );
 
-       if (!get_specific_param_by_index(*printer, 2, idx, value, &data, &type, &data_len)) {
-
-               SAFE_FREE(data);
-               free_a_printer(&printer, 2);
+       if ( !val ) 
+       {
 
                /* out_value should default to "" or else NT4 has
                   problems unmarshalling the response */
 
-               *out_max_value_len=(in_value_len/sizeof(uint16));
-               if((*out_value=(uint16 *)talloc_zero(p->mem_ctx, in_value_len*sizeof(uint8))) == NULL)
-                       return WERR_NOMEM;
+               *out_max_value_len = (in_value_len/sizeof(uint16));
+               
+               if ( (*out_value=(uint16 *)talloc_zero(p->mem_ctx, in_value_len*sizeof(uint8))) == NULL ) 
+               {
+                       result = WERR_NOMEM;
+                       goto done;
+               }
 
                *out_value_len = (uint32)rpcstr_push((char *)*out_value, "", in_value_len, 0);
 
                /* the data is counted in bytes */
+               
                *out_max_data_len = in_data_len;
-               *out_data_len = in_data_len;
-               if((*data_out=(uint8 *)talloc_zero(p->mem_ctx, in_data_len*sizeof(uint8))) == NULL)
-                       return WERR_NOMEM;
+               *out_data_len     = in_data_len;
+               
+               /* only allocate when given a non-zero data_len */
+               
+               if ( in_data_len && ((*data_out=(uint8 *)talloc_zero(p->mem_ctx, in_data_len*sizeof(uint8))) == NULL) )
+               {
+                       result = WERR_NOMEM;
+                       goto done;
+               }
 
-               return WERR_NO_MORE_ITEMS;
+               result = WERR_NO_MORE_ITEMS;
        }
-
-       free_a_printer(&printer, 2);
-
-       /*
-        * the value is:
-        * - counted in bytes in the request
-        * - counted in UNICODE chars in the max reply
-        * - counted in bytes in the real size
-        *
-        * take a pause *before* coding not *during* coding
-        */
+       else 
+       {
+               /*
+                * the value is:
+                * - counted in bytes in the request
+                * - counted in UNICODE chars in the max reply
+                * - counted in bytes in the real size
+                *
+                * take a pause *before* coding not *during* coding
+                */
        
-       *out_max_value_len=(in_value_len/sizeof(uint16));
-       if((*out_value=(uint16 *)talloc_zero(p->mem_ctx,in_value_len*sizeof(uint8))) == NULL) {
-               SAFE_FREE(data);
-               return WERR_NOMEM;
-       }
+               /* name */
+               *out_max_value_len = ( in_value_len / sizeof(uint16) );
+               if ( (*out_value = (uint16 *)talloc_zero(p->mem_ctx, in_value_len*sizeof(uint8))) == NULL ) 
+               {
+                       result = WERR_NOMEM;
+                       goto done;
+               }
        
-       *out_value_len = (uint32)rpcstr_push((char *)*out_value,value, in_value_len, 0);
+               *out_value_len = (uint32)rpcstr_push((char *)*out_value, regval_name(val), in_value_len, 0);
 
-       *out_type=type;
+               /* type */
+               
+               *out_type = regval_type( val );
 
-       /* the data is counted in bytes */
-       *out_max_data_len=in_data_len;
-       if((*data_out=(uint8 *)talloc_zero(p->mem_ctx, in_data_len*sizeof(uint8))) == NULL) {
-               SAFE_FREE(data);
-               return WERR_NOMEM;
+               /* data - counted in bytes */
+
+               *out_max_data_len = in_data_len;
+               if ( (*data_out = (uint8 *)talloc_zero(p->mem_ctx, in_data_len*sizeof(uint8))) == NULL) 
+               {
+                       result = WERR_NOMEM;
+                       goto done;
+               }
+               data_len = (size_t)regval_size(val);
+               memcpy( *data_out, regval_data_p(val), data_len );
+               *out_data_len = data_len;
        }
-       
-       memcpy(*data_out, data, (size_t)data_len);
-       *out_data_len=data_len;
 
-       SAFE_FREE(data);
-       
-       return WERR_OK;
+done:
+       free_a_printer(&printer, 2);
+       return result;
 }
 
 /****************************************************************************
@@ -7271,17 +7480,17 @@ WERROR _spoolss_enumprinterdata(pipes_struct *p, SPOOL_Q_ENUMPRINTERDATA *q_u, S
 
 WERROR _spoolss_setprinterdata( pipes_struct *p, SPOOL_Q_SETPRINTERDATA *q_u, SPOOL_R_SETPRINTERDATA *r_u)
 {
-       POLICY_HND *handle = &q_u->handle;
-       UNISTR2 *value = &q_u->value;
-       uint32 type = q_u->type;
-       uint8 *data = q_u->data;
-       uint32 real_len = q_u->real_len;
+       POLICY_HND              *handle = &q_u->handle;
+       UNISTR2                 *value = &q_u->value;
+       uint32                  type = q_u->type;
+       uint8                   *data = q_u->data;
+       uint32                  real_len = q_u->real_len;
 
-       NT_PRINTER_INFO_LEVEL *printer = NULL;
-       NT_PRINTER_PARAM *param = NULL, old_param;
-       int snum=0;
-       WERROR status = WERR_OK;
-       Printer_entry *Printer=find_printer_index_by_hnd(p, handle);
+       NT_PRINTER_INFO_LEVEL   *printer = NULL;
+       int                     snum=0;
+       WERROR                  status = WERR_OK;
+       Printer_entry           *Printer=find_printer_index_by_hnd(p, handle);
+       fstring                 valuename;
        
        DEBUG(5,("spoolss_setprinterdata\n"));
 
@@ -7293,8 +7502,6 @@ WERROR _spoolss_setprinterdata( pipes_struct *p, SPOOL_Q_SETPRINTERDATA *q_u, SP
        if (!get_printer_snum(p,handle, &snum))
                return WERR_BADFID;
 
-       ZERO_STRUCT(old_param);
-
        /* 
         * Access check : NT returns "access denied" if you make a 
         * SetPrinterData call without the necessary privildge.
@@ -7303,46 +7510,41 @@ WERROR _spoolss_setprinterdata( pipes_struct *p, SPOOL_Q_SETPRINTERDATA *q_u, SP
         * when connecting to a printer  --jerry
         */
 
-       if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
+       if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) 
+       {
                DEBUG(3, ("_spoolss_setprinterdata: change denied by handle access permissions\n"));
                status = WERR_ACCESS_DENIED;
                goto done;
        }
 
-       /* Check if we are making any changes or not.  Return true if
-          nothing is actually changing.  This is not needed anymore but
-          has been left in as an optimization to keep from from
-          writing to disk as often  --jerry  */
-
        status = get_a_printer(&printer, 2, lp_servicename(snum));
        if (!W_ERROR_IS_OK(status))
                return status;
 
-       convert_specific_param(&param, value , type, data, real_len);
-
-       unlink_specific_param_if_exist(printer->info_2, param);
+       unistr2_to_ascii( valuename, value, sizeof(valuename)-1 );
        
        /*
         * When client side code sets a magic printer data key, detect it and save
         * the current printer data and the magic key's data (its the DEVMODE) for
         * future printer/driver initializations.
         */
-       if (param->type==3 && !strcmp( param->value, PHANTOM_DEVMODE_KEY)) {
-               /*
-                * Set devmode and printer initialization info
-                */
-               status = save_driver_init(printer, 2, param);
+       if ( (type == REG_BINARY) && strequal( valuename, PHANTOM_DEVMODE_KEY)) 
+       {
+               /* Set devmode and printer initialization info */
+               status = save_driver_init( printer, 2, data, real_len );
+       
+               srv_spoolss_reset_printerdata( printer->info_2->drivername );
        }
-       else {
-               add_a_specific_param(printer->info_2, &param);
-               status = mod_a_printer(*printer, 2);
+       else 
+       {
+       status = set_printer_dataex( printer, SPOOL_PRINTERDATA_KEY, valuename, 
+                                       type, data, real_len );
+               if ( W_ERROR_IS_OK(status) )
+                       status = mod_a_printer(*printer, 2);
        }
 
- done:
+done:
        free_a_printer(&printer, 2);
-       if (param)
-               free_nt_printer_param(&param);
-       SAFE_FREE(old_param.data);
 
        return status;
 }
@@ -7352,9 +7554,9 @@ WERROR _spoolss_setprinterdata( pipes_struct *p, SPOOL_Q_SETPRINTERDATA *q_u, SP
 
 WERROR _spoolss_resetprinter(pipes_struct *p, SPOOL_Q_RESETPRINTER *q_u, SPOOL_R_RESETPRINTER *r_u)
 {
-       POLICY_HND *handle = &q_u->handle;
-       Printer_entry *Printer=find_printer_index_by_hnd(p, handle);
-       int snum;
+       POLICY_HND      *handle = &q_u->handle;
+       Printer_entry   *Printer=find_printer_index_by_hnd(p, handle);
+       int             snum;
        
        DEBUG(5,("_spoolss_resetprinter\n"));
 
@@ -7378,16 +7580,19 @@ WERROR _spoolss_resetprinter(pipes_struct *p, SPOOL_Q_RESETPRINTER *q_u, SPOOL_R
 }
 
 
+/****************************************************************************
+****************************************************************************/
+
 WERROR _spoolss_deleteprinterdata(pipes_struct *p, SPOOL_Q_DELETEPRINTERDATA *q_u, SPOOL_R_DELETEPRINTERDATA *r_u)
 {
-       POLICY_HND *handle = &q_u->handle;
-       UNISTR2 *value = &q_u->valuename;
+       POLICY_HND      *handle = &q_u->handle;
+       UNISTR2         *value = &q_u->valuename;
 
-       NT_PRINTER_INFO_LEVEL *printer = NULL;
-       NT_PRINTER_PARAM param;
-       int snum=0;
-       WERROR status = WERR_OK;
-       Printer_entry *Printer=find_printer_index_by_hnd(p, handle);
+       NT_PRINTER_INFO_LEVEL   *printer = NULL;
+       int             snum=0;
+       WERROR          status = WERR_OK;
+       Printer_entry   *Printer=find_printer_index_by_hnd(p, handle);
+       pstring         valuename;
        
        DEBUG(5,("spoolss_deleteprinterdata\n"));
        
@@ -7408,15 +7613,12 @@ WERROR _spoolss_deleteprinterdata(pipes_struct *p, SPOOL_Q_DELETEPRINTERDATA *q_
        if (!W_ERROR_IS_OK(status))
                return status;
 
-       ZERO_STRUCTP(&param);
-       unistr2_to_ascii(param.value, value, sizeof(param.value)-1);
+       unistr2_to_ascii( valuename, value, sizeof(valuename)-1 );
 
-       if(!unlink_specific_param_if_exist(printer->info_2, &param))
-               status = WERR_INVALID_PARAM;
-       else
-               status = mod_a_printer(*printer, 2);
+       status = delete_printer_dataex( printer, SPOOL_PRINTERDATA_KEY, valuename );
 
        free_a_printer(&printer, 2);
+
        return status;
 }
 
@@ -7426,7 +7628,6 @@ WERROR _spoolss_deleteprinterdata(pipes_struct *p, SPOOL_Q_DELETEPRINTERDATA *q_
 WERROR _spoolss_addform( pipes_struct *p, SPOOL_Q_ADDFORM *q_u, SPOOL_R_ADDFORM *r_u)
 {
        POLICY_HND *handle = &q_u->handle;
-/*     uint32 level = q_u->level; - notused. */
        FORM *form = &q_u->form;
        nt_forms_struct tmpForm;
        int snum;
@@ -7443,40 +7644,52 @@ WERROR _spoolss_addform( pipes_struct *p, SPOOL_Q_ADDFORM *q_u, SPOOL_R_ADDFORM
                DEBUG(2,("_spoolss_addform: Invalid handle (%s:%u:%u).\n", OUR_HANDLE(handle)));
                return WERR_BADFID;
        }
+       
+       
+       /* forms can be added on printer of on the print server handle */
+       
+       if ( Printer->printer_type == PRINTER_HANDLE_IS_PRINTER )
+       {
+               if (!get_printer_snum(p,handle, &snum))
+                       return WERR_BADFID;
+        
+               status = get_a_printer(&printer, 2, lp_servicename(snum));
+               if (!W_ERROR_IS_OK(status))
+                       goto done;
+       }
 
-       if (!get_printer_snum(p,handle, &snum))
-                return WERR_BADFID;
-
-       if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
+       if ( !(Printer->access_granted & (PRINTER_ACCESS_ADMINISTER|SERVER_ACCESS_ADMINISTER)) ) {
                DEBUG(2,("_spoolss_addform: denied by handle permissions.\n"));
                status = WERR_ACCESS_DENIED;
                goto done;
        }
-               
+       
        /* can't add if builtin */
+       
        if (get_a_builtin_ntform(&form->name,&tmpForm)) {
-               return WERR_ALREADY_EXISTS;
+               status = WERR_ALREADY_EXISTS;
+               goto done;
        }
 
-       count=get_ntforms(&list);
-       if(!add_a_form(&list, form, &count))
-               return WERR_NOMEM;
+       count = get_ntforms(&list);
+       
+       if(!add_a_form(&list, form, &count)) {
+               status =  WERR_NOMEM;
+               goto done;
+       }
+       
        write_ntforms(&list, count);
        
        /*
-        * ChangeID must always be set
+        * ChangeID must always be set if this is a printer
         */
         
-       status = get_a_printer(&printer, 2, lp_servicename(snum));
-        if (!W_ERROR_IS_OK(status))
-               goto done;
-       
-       status = mod_a_printer(*printer, 2);
-        if (!W_ERROR_IS_OK(status))
-               goto done;
+       if ( Printer->printer_type == PRINTER_HANDLE_IS_PRINTER )
+               status = mod_a_printer(*printer, 2);
        
 done:
-       free_a_printer(&printer, 2);
+       if ( printer )
+               free_a_printer(&printer, 2);
        SAFE_FREE(list);
 
        return status;
@@ -7491,7 +7704,6 @@ WERROR _spoolss_deleteform( pipes_struct *p, SPOOL_Q_DELETEFORM *q_u, SPOOL_R_DE
        UNISTR2 *form_name = &q_u->name;
        nt_forms_struct tmpForm;
        int count=0;
-       WERROR ret = WERR_OK;
        nt_forms_struct *list=NULL;
        Printer_entry *Printer = find_printer_index_by_hnd(p, handle);
        int snum;
@@ -7505,40 +7717,49 @@ WERROR _spoolss_deleteform( pipes_struct *p, SPOOL_Q_DELETEFORM *q_u, SPOOL_R_DE
                return WERR_BADFID;
        }
 
-       if (!get_printer_snum(p, handle, &snum))
-               return WERR_BADFID;
+       /* forms can be deleted on printer of on the print server handle */
+       
+       if ( Printer->printer_type == PRINTER_HANDLE_IS_PRINTER )
+       {
+               if (!get_printer_snum(p,handle, &snum))
+                       return WERR_BADFID;
+        
+               status = get_a_printer(&printer, 2, lp_servicename(snum));
+               if (!W_ERROR_IS_OK(status))
+                       goto done;
+       }
 
-       if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
-               DEBUG(2,("_spoolss_deleteform: denied by handle permissions\n"));
-               return WERR_ACCESS_DENIED;
+       if ( !(Printer->access_granted & (PRINTER_ACCESS_ADMINISTER|SERVER_ACCESS_ADMINISTER)) ) {
+               DEBUG(2,("_spoolss_deleteform: denied by handle permissions.\n"));
+               status = WERR_ACCESS_DENIED;
+               goto done;
        }
 
        /* can't delete if builtin */
+       
        if (get_a_builtin_ntform(form_name,&tmpForm)) {
-               return WERR_INVALID_PARAM;
+               status = WERR_INVALID_PARAM;
+               goto done;
        }
 
        count = get_ntforms(&list);
-       if(!delete_a_form(&list, form_name, &count, &ret))
-               return WERR_INVALID_PARAM;
+       
+       if ( !delete_a_form(&list, form_name, &count, &status ))
+               goto done;
 
        /*
-        * ChangeID must always be set
+        * ChangeID must always be set if this is a printer
         */
         
-       status = get_a_printer(&printer, 2, lp_servicename(snum));
-        if (!W_ERROR_IS_OK(status))
-               goto done;
-       
-       status = mod_a_printer(*printer, 2);
-        if (!W_ERROR_IS_OK(status))
-               goto done;
+       if ( Printer->printer_type == PRINTER_HANDLE_IS_PRINTER )
+               status = mod_a_printer(*printer, 2);
        
 done:
-       free_a_printer(&printer, 2);
+       if ( printer )
+               free_a_printer(&printer, 2);
        SAFE_FREE(list);
 
-       return ret;
+       return status;
 }
 
 /****************************************************************************
@@ -7564,40 +7785,48 @@ WERROR _spoolss_setform(pipes_struct *p, SPOOL_Q_SETFORM *q_u, SPOOL_R_SETFORM *
                return WERR_BADFID;
        }
 
-       if (!get_printer_snum(p, handle, &snum))
-               return WERR_BADFID;
+       /* forms can be modified on printer of on the print server handle */
+       
+       if ( Printer->printer_type == PRINTER_HANDLE_IS_PRINTER )
+       {
+               if (!get_printer_snum(p,handle, &snum))
+                       return WERR_BADFID;
+        
+               status = get_a_printer(&printer, 2, lp_servicename(snum));
+               if (!W_ERROR_IS_OK(status))
+                       goto done;
+       }
 
-       if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
+       if ( !(Printer->access_granted & (PRINTER_ACCESS_ADMINISTER|SERVER_ACCESS_ADMINISTER)) ) {
                DEBUG(2,("_spoolss_setform: denied by handle permissions\n"));
-               return WERR_ACCESS_DENIED;
+               status = WERR_ACCESS_DENIED;
+               goto done;
        }
 
        /* can't set if builtin */
        if (get_a_builtin_ntform(&form->name,&tmpForm)) {
-               return WERR_INVALID_PARAM;
+               status = WERR_INVALID_PARAM;
+               goto done;
        }
 
-       count=get_ntforms(&list);
+       count = get_ntforms(&list);
        update_a_form(&list, form, count);
        write_ntforms(&list, count);
 
        /*
-        * ChangeID must always be set
+        * ChangeID must always be set if this is a printer
         */
         
-       status = get_a_printer(&printer, 2, lp_servicename(snum));
-        if (!W_ERROR_IS_OK(status))
-               goto done;
+       if ( Printer->printer_type == PRINTER_HANDLE_IS_PRINTER )
+               status = mod_a_printer(*printer, 2);
        
-       status = mod_a_printer(*printer, 2);
-        if (!W_ERROR_IS_OK(status))
-               goto done;
        
 done:
-       free_a_printer(&printer, 2);
+       if ( printer )
+               free_a_printer(&printer, 2);
        SAFE_FREE(list);
 
-       return WERR_OK;
+       return status;
 }
 
 /****************************************************************************
@@ -7846,7 +8075,7 @@ static WERROR getjob_level_1(print_queue_struct *queue, int count, int snum, uin
                return WERR_NOMEM;
        }
                
-       for (i=0; i<count && found==False; i++) {
+       for (i=0; i<count && found==False; i++) { 
                if (queue[i].job==(int)jobid)
                        found=True;
        }
@@ -7860,8 +8089,6 @@ static WERROR getjob_level_1(print_queue_struct *queue, int count, int snum, uin
        
        fill_job_info_1(info_1, &(queue[i-1]), i, snum);
        
-       SAFE_FREE(queue);
-       
        *needed += spoolss_size_job_info_1(info_1);
 
        if (!alloc_buffer_size(buffer, *needed)) {
@@ -7884,12 +8111,13 @@ static WERROR getjob_level_1(print_queue_struct *queue, int count, int snum, uin
 
 static WERROR getjob_level_2(print_queue_struct *queue, int count, int snum, uint32 jobid, NEW_BUFFER *buffer, uint32 offered, uint32 *needed)
 {
-       int i=0;
-       BOOL found=False;
-       JOB_INFO_2 *info_2;
+       int             i = 0;
+       BOOL            found = False;
+       JOB_INFO_2      *info_2;
        NT_PRINTER_INFO_LEVEL *ntprinter = NULL;
-       WERROR ret;
-       DEVICEMODE *devmode = NULL;
+       WERROR          ret;
+       DEVICEMODE      *devmode = NULL;
+       NT_DEVICEMODE   *nt_devmode = NULL;
 
        info_2=(JOB_INFO_2 *)malloc(sizeof(JOB_INFO_2));
 
@@ -7900,12 +8128,14 @@ static WERROR getjob_level_2(print_queue_struct *queue, int count, int snum, uin
                goto done;
        }
 
-       for (i=0; i<count && found==False; i++) {
-               if (queue[i].job==(int)jobid)
-                       found=True;
+       for ( i=0; i<count && found==False; i++ ) 
+       {
+               if (queue[i].job == (int)jobid)
+                       found = True;
        }
        
-       if (found==False) {
+       if ( !found ) 
+       {
                /* NT treats not found as bad param... yet another bad
                   choice */
                ret = WERR_INVALID_PARAM;
@@ -7915,11 +8145,22 @@ static WERROR getjob_level_2(print_queue_struct *queue, int count, int snum, uin
        ret = get_a_printer(&ntprinter, 2, lp_servicename(snum));
        if (!W_ERROR_IS_OK(ret))
                goto done;
-       if (construct_dev_mode(snum) == NULL) {
-               ret = WERR_NOMEM;
-               goto done;
+       
+       /* 
+        * if the print job does not have a DEVMODE associated with it, 
+        * just use the one for the printer. A NULL devicemode is not
+        *  a failure condition
+        */
+        
+       if ( !(nt_devmode=print_job_devmode( snum, jobid )) )
+               devmode = construct_dev_mode(snum);
+       else {
+               if ((devmode = (DEVICEMODE *)malloc(sizeof(DEVICEMODE))) != NULL) {
+                       ZERO_STRUCTP( devmode );
+                       convert_nt_devicemode( devmode, nt_devmode );
+               }
        }
-
+       
        fill_job_info_2(info_2, &(queue[i-1]), i, snum, ntprinter, devmode);
        
        *needed += spoolss_size_job_info_2(info_2);
@@ -7941,7 +8182,6 @@ static WERROR getjob_level_2(print_queue_struct *queue, int count, int snum, uin
  done:
        /* Cleanup allocated memory */
 
-       SAFE_FREE(queue);
        free_job_info_2(info_2);        /* Also frees devmode */
        SAFE_FREE(info_2);
        free_a_printer(&ntprinter, 2);
@@ -7960,10 +8200,11 @@ WERROR _spoolss_getjob( pipes_struct *p, SPOOL_Q_GETJOB *q_u, SPOOL_R_GETJOB *r_
        NEW_BUFFER *buffer = NULL;
        uint32 offered = q_u->offered;
        uint32 *needed = &r_u->needed;
+       WERROR          wstatus = WERR_OK;
 
        int snum;
        int count;
-       print_queue_struct *queue=NULL;
+       print_queue_struct      *queue = NULL;
        print_status_struct prt_status;
 
        /* that's an [in out] buffer */
@@ -7972,7 +8213,7 @@ WERROR _spoolss_getjob( pipes_struct *p, SPOOL_Q_GETJOB *q_u, SPOOL_R_GETJOB *r_
 
        DEBUG(5,("spoolss_getjob\n"));
        
-       *needed=0;
+       *needed = 0;
        
        if (!get_printer_snum(p, handle, &snum))
                return WERR_BADFID;
@@ -7982,19 +8223,29 @@ WERROR _spoolss_getjob( pipes_struct *p, SPOOL_Q_GETJOB *q_u, SPOOL_R_GETJOB *r_
        DEBUGADD(4,("count:[%d], prt_status:[%d], [%s]\n",
                     count, prt_status.status, prt_status.message));
                
-       switch (level) {
+       switch ( level ) {
        case 1:
-               return getjob_level_1(queue, count, snum, jobid, buffer, offered, needed);
+                       wstatus = getjob_level_1(queue, count, snum, jobid, 
+                               buffer, offered, needed);
+                       break;
        case 2:
-               return getjob_level_2(queue, count, snum, jobid, buffer, offered, needed);
+                       wstatus = getjob_level_2(queue, count, snum, jobid, 
+                               buffer, offered, needed);
+                       break;
        default:
-               SAFE_FREE(queue);
-               return WERR_UNKNOWN_LEVEL;
+                       wstatus = WERR_UNKNOWN_LEVEL;
+                       break;
        }
+       
+       SAFE_FREE(queue);
+       return wstatus;
 }
 
 /********************************************************************
- * spoolss_getprinterdataex
+ spoolss_getprinterdataex
+ From MSDN documentation of GetPrinterDataEx: pass request
+ to GetPrinterData if key is "PrinterDriverData".
  ********************************************************************/
 
 WERROR _spoolss_getprinterdataex(pipes_struct *p, SPOOL_Q_GETPRINTERDATAEX *q_u, SPOOL_R_GETPRINTERDATAEX *r_u)
@@ -8005,111 +8256,181 @@ WERROR _spoolss_getprinterdataex(pipes_struct *p, SPOOL_Q_GETPRINTERDATAEX *q_u,
        uint32          *out_size = &r_u->size;
        uint8           **data = &r_u->data;
        uint32          *needed = &r_u->needed;
-
-       fstring         key, value;
+       fstring         keyname, valuename;
+       
        Printer_entry   *Printer = find_printer_index_by_hnd(p, handle);
-       BOOL            found = False;
+       
+       NT_PRINTER_INFO_LEVEL   *printer = NULL;
+       int                     snum = 0;
+       WERROR                  status = WERR_OK;
 
        DEBUG(4,("_spoolss_getprinterdataex\n"));
 
-        unistr2_to_ascii(key, &q_u->keyname, sizeof(key) - 1);
-        unistr2_to_ascii(value, &q_u->valuename, sizeof(value) - 1);
+        unistr2_to_ascii(keyname, &q_u->keyname, sizeof(keyname) - 1);
+        unistr2_to_ascii(valuename, &q_u->valuename, sizeof(valuename) - 1);
+       
+       DEBUG(10, ("_spoolss_getprinterdataex: key => [%s], value => [%s]\n", 
+               keyname, valuename));
 
        /* in case of problem, return some default values */
-       *needed=0;
-       *type=0;
-       *out_size=0;
+       
+       *needed   = 0;
+       *type     = 0;
+       *out_size = in_size;
 
-               
        if (!Printer) {
-               if((*data=(uint8 *)talloc_zero(p->mem_ctx, 4*sizeof(uint8))) == NULL)
-                       return WERR_NOMEM;
-               DEBUG(2,("_spoolss_getprinterdata: Invalid handle (%s:%u:%u).\n", OUR_HANDLE(handle)));
-               return WERR_BADFID;
+               DEBUG(2,("_spoolss_getprinterdataex: Invalid handle (%s:%u:%u).\n", OUR_HANDLE(handle)));
+               status = WERR_BADFID;
+               goto done;
        }
 
-               
        /* Is the handle to a printer or to the server? */
 
-       if (Printer->printer_type == PRINTER_HANDLE_IS_PRINTSERVER)
-       {
+       if (Printer->printer_type == PRINTER_HANDLE_IS_PRINTSERVER) {
                DEBUG(10,("_spoolss_getprinterdatex: Not implemented for server handles yet\n"));
-               return WERR_INVALID_PARAM;
+               status = WERR_INVALID_PARAM;
+               goto done;
        }
-       else
-       {
-               /* 
-                * From MSDN documentation of GetPrinterDataEx: pass request
-                * to GetPrinterData if key is "PrinterDriverData". This is 
-                * the only key we really support. Other keys to implement:
-                * (a) DsDriver
-                * (b) DsSpooler
-                * (c) PnPData
-                */
-          
-               if (strcmp(key, "PrinterDriverData") != 0)
-                       return WERR_BADFILE;
+       
+       if ( !get_printer_snum(p,handle, &snum) )
+               return WERR_BADFID;
 
-               DEBUG(10, ("_spoolss_getprinterdataex: pass me to getprinterdata\n"));
-               found = getprinterdata_printer(p, p->mem_ctx, handle, value, 
-                       type, data, needed, in_size);
-               
+       status = get_a_printer(&printer, 2, lp_servicename(snum));
+       if ( !W_ERROR_IS_OK(status) )
+               goto done;
+
+       /* check to see if the keyname is valid */
+       if ( !strlen(keyname) ) {
+               status = WERR_INVALID_PARAM;
+               goto done;
        }
-        
-       if (!found) {
-               DEBUG(5, ("value not found, allocating %d\n", *out_size));
+       
+       if ( lookup_printerkey( &printer->info_2->data, keyname ) == -1 ) {
+               DEBUG(4,("_spoolss_getprinterdataex: Invalid keyname [%s]\n", keyname ));
+               free_a_printer( &printer, 2 );
+               status = WERR_BADFILE;
+               goto done;
+       }
+       
+       /* When given a new keyname, we should just create it */
+
+       status = get_printer_dataex( p->mem_ctx, printer, keyname, valuename, type, data, needed, in_size );
+       
+       if (*needed > *out_size)
+               status = WERR_MORE_DATA;
+
+done:
+       if ( !W_ERROR_IS_OK(status) ) 
+       {
+               DEBUG(5, ("error: allocating %d\n", *out_size));
                
                /* reply this param doesn't exist */
-               if (*out_size) {
-                       if((*data=(uint8 *)talloc_zero(p->mem_ctx, *out_size*sizeof(uint8))) == NULL)
-                               return WERR_NOMEM;
-               } else {
+               
+               if ( *out_size ) 
+               {
+                       if( (*data=(uint8 *)talloc_zero(p->mem_ctx, *out_size*sizeof(uint8))) == NULL ) {
+                               status = WERR_NOMEM;
+                               goto done;
+                       }
+               } 
+               else {
                        *data = NULL;
-               }
-
-               return WERR_INVALID_PARAM;
+       }
        }
        
-       if (*needed > *out_size)
-               return WERR_MORE_DATA;
-       else
-               return WERR_OK;
+       if ( printer )
+       free_a_printer( &printer, 2 );
+       
+       return status;
 }
 
 /********************************************************************
- * spoolss_setprinterdata
+ * spoolss_setprinterdataex
  ********************************************************************/
 
 WERROR _spoolss_setprinterdataex(pipes_struct *p, SPOOL_Q_SETPRINTERDATAEX *q_u, SPOOL_R_SETPRINTERDATAEX *r_u)
 {
-       SPOOL_Q_SETPRINTERDATA q_u_local;
-       SPOOL_R_SETPRINTERDATA r_u_local;
-        fstring key;
+       POLICY_HND              *handle = &q_u->handle; 
+       uint32                  type = q_u->type;
+       uint8                   *data = q_u->data;
+       uint32                  real_len = q_u->real_len;
 
+       NT_PRINTER_INFO_LEVEL   *printer = NULL;
+       int                     snum = 0;
+       WERROR                  status = WERR_OK;
+       Printer_entry           *Printer = find_printer_index_by_hnd(p, handle);
+       fstring                 valuename;
+       fstring                 keyname;
+       char                    *oid_string;
+       
        DEBUG(4,("_spoolss_setprinterdataex\n"));
 
         /* From MSDN documentation of SetPrinterDataEx: pass request to
            SetPrinterData if key is "PrinterDriverData" */
 
-        unistr2_to_ascii(key, &q_u->key, sizeof(key) - 1);
+       if (!Printer) {
+               DEBUG(2,("_spoolss_setprinterdata: Invalid handle (%s:%u:%u).\n", OUR_HANDLE(handle)));
+               return WERR_BADFID;
+       }
 
-        if (strcmp(key, "PrinterDriverData") != 0)
-               return WERR_INVALID_PARAM;
-               
-       ZERO_STRUCT(q_u_local); 
-       ZERO_STRUCT(r_u_local); 
-       
-       /* make a copy to call _spoolss_setprinterdata() */
-
-       memcpy(&q_u_local.handle, &q_u->handle, sizeof(POLICY_HND));
-       copy_unistr2(&q_u_local.value, &q_u->value);
-       q_u_local.type = q_u->type;
-       q_u_local.max_len = q_u->max_len;
-       q_u_local.data = q_u->data;
-       q_u_local.real_len = q_u->real_len;
-       q_u_local.numeric_data = q_u->numeric_data;
+       if ( !get_printer_snum(p,handle, &snum) )
+               return WERR_BADFID;
+
+       /* 
+        * Access check : NT returns "access denied" if you make a 
+        * SetPrinterData call without the necessary privildge.
+        * we were originally returning OK if nothing changed
+        * which made Win2k issue **a lot** of SetPrinterData
+        * when connecting to a printer  --jerry
+        */
+
+       if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) 
+       {
+               DEBUG(3, ("_spoolss_setprinterdataex: change denied by handle access permissions\n"));
+               return WERR_ACCESS_DENIED;
+       }
+
+       status = get_a_printer(&printer, 2, lp_servicename(snum));
+       if (!W_ERROR_IS_OK(status))
+               return status;
+
+        unistr2_to_ascii( valuename, &q_u->value, sizeof(valuename) - 1);
+        unistr2_to_ascii( keyname, &q_u->key, sizeof(keyname) - 1);
+       
+       /* check for OID in valuename */
+       
+       if ( (oid_string = strchr( valuename, ',' )) != NULL )
+       {
+               *oid_string = '\0';
+               oid_string++;
+       }
+
+       /* save the registry data */
+       
+       status = set_printer_dataex( printer, keyname, valuename, type, data, real_len ); 
+       
+       /* save the OID if one was specified and the previous set call succeeded */
+       
+       if ( W_ERROR_IS_OK(status) && oid_string )
+       {
+
+               fstrcat( keyname, "\\" );
+               fstrcat( keyname, SPOOL_OID_KEY );
                
-       return _spoolss_setprinterdata(p, &q_u_local, &r_u_local);
+               /* 
+                * I'm not checking the status here on purpose.  Don't know 
+                * if this is right, but I'm returning the status from the 
+                * previous set_printer_dataex() call.  I have no idea if 
+                * this is right.    --jerry
+                */
+                
+               set_printer_dataex( printer, keyname, valuename, 
+                                   REG_SZ, (void*)oid_string, strlen(oid_string)+1 );          
+       }
+       
+       free_a_printer(&printer, 2);
+
+       return status;
 }
 
 
@@ -8119,91 +8440,115 @@ WERROR _spoolss_setprinterdataex(pipes_struct *p, SPOOL_Q_SETPRINTERDATAEX *q_u,
 
 WERROR _spoolss_deleteprinterdataex(pipes_struct *p, SPOOL_Q_DELETEPRINTERDATAEX *q_u, SPOOL_R_DELETEPRINTERDATAEX *r_u)
 {
-       SPOOL_Q_DELETEPRINTERDATA q_u_local;
-       SPOOL_R_DELETEPRINTERDATA r_u_local;
-       fstring key;
-       
-        /* From MSDN documentation of SetPrinterDataEx: pass request to
-           SetPrinterData if key is "PrinterDriverData" */
+       POLICY_HND      *handle = &q_u->handle;
+       UNISTR2         *value = &q_u->valuename;
+       UNISTR2         *key = &q_u->keyname;
 
-        unistr2_to_ascii(key, &q_u->keyname, sizeof(key) - 1);
-
-        if (strcmp(key, "PrinterDriverData") != 0)
-               return WERR_INVALID_PARAM;
+       NT_PRINTER_INFO_LEVEL   *printer = NULL;
+       int             snum=0;
+       WERROR          status = WERR_OK;
+       Printer_entry   *Printer=find_printer_index_by_hnd(p, handle);
+       pstring         valuename, keyname;
        
-       memcpy(&q_u_local.handle, &q_u->handle, sizeof(POLICY_HND));
-       copy_unistr2(&q_u_local.valuename, &q_u->valuename);
+       DEBUG(5,("spoolss_deleteprinterdataex\n"));
        
-       return _spoolss_deleteprinterdata( p, &q_u_local, &r_u_local );
-}
+       if (!Printer) {
+               DEBUG(2,("_spoolss_deleteprinterdata: Invalid handle (%s:%u:%u).\n", OUR_HANDLE(handle)));
+               return WERR_BADFID;
+       }
 
+       if (!get_printer_snum(p, handle, &snum))
+               return WERR_BADFID;
 
+       if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
+               DEBUG(3, ("_spoolss_deleteprinterdataex: printer properties change denied by handle\n"));
+               return WERR_ACCESS_DENIED;
+       }
+
+       status = get_a_printer(&printer, 2, lp_servicename(snum));
+       if (!W_ERROR_IS_OK(status))
+               return status;
+
+       unistr2_to_ascii( valuename, value, sizeof(valuename)-1 );
+       unistr2_to_ascii( keyname, key, sizeof(keyname)-1 );
 
+       status = delete_printer_dataex( printer, keyname, valuename );
+
+       free_a_printer(&printer, 2);
+
+       return status;
+}
 
 /********************************************************************
  * spoolss_enumprinterkey
  ********************************************************************/
 
-/* constants for EnumPrinterKey() */
-#define ENUMERATED_KEY_SIZE    19
 
 WERROR _spoolss_enumprinterkey(pipes_struct *p, SPOOL_Q_ENUMPRINTERKEY *q_u, SPOOL_R_ENUMPRINTERKEY *r_u)
 {
-       fstring key;
-       uint16  enumkeys[ENUMERATED_KEY_SIZE+1];
-       char*   ptr = NULL;
-       int     i;
-       char    *PrinterKey = "PrinterDriverData";
-
+       fstring         key;
+       fstring         *keynames = NULL;
+       uint16          *enumkeys = NULL;
+       int             num_keys;
+       int             printerkey_len;
+       POLICY_HND      *handle = &q_u->handle;
+       Printer_entry   *Printer = find_printer_index_by_hnd(p, handle);
+       NT_PRINTER_DATA *data;
+       NT_PRINTER_INFO_LEVEL   *printer = NULL;
+       int             snum = 0;
+       WERROR          status = WERR_BADFILE;
+       
+       
        DEBUG(4,("_spoolss_enumprinterkey\n"));
 
-       unistr2_to_ascii(key, &q_u->key, sizeof(key) - 1);
+       if (!Printer) {
+               DEBUG(2,("_spoolss_enumprinterkey: Invalid handle (%s:%u:%u).\n", OUR_HANDLE(handle)));
+               return WERR_BADFID;
+       }
 
-       /* 
-        * we only support enumating all keys (key == "")
-        * Of course, the only key we support is the "PrinterDriverData" 
-        * key
-        */     
-       if (strlen(key) == 0)
-       {
-               r_u->needed = ENUMERATED_KEY_SIZE *2;
-               if (q_u->size < r_u->needed)
-                       return WERR_MORE_DATA;
-       
-               ptr = PrinterKey;
-               for (i=0; i<ENUMERATED_KEY_SIZE-2; i++)
-               {
-                       enumkeys[i] = (uint16)(*ptr);
-                       ptr++;
-               }
+       if ( !get_printer_snum(p,handle, &snum) )
+               return WERR_BADFID;
 
-               /* tag of with 2 '\0's */
-               enumkeys[i++] = '\0';
-               enumkeys[i] = '\0';
+       status = get_a_printer(&printer, 2, lp_servicename(snum));
+       if (!W_ERROR_IS_OK(status))
+               return status;
+               
+       /* get the list of subkey names */
        
-               if (!make_spoolss_buffer5(p->mem_ctx, &r_u->keys, ENUMERATED_KEY_SIZE, enumkeys))
-                       return WERR_BADFILE;
-                       
-               return WERR_OK;
+       unistr2_to_ascii( key, &q_u->key, sizeof(key)-1 );
+       data = &printer->info_2->data;
+
+       num_keys = get_printer_subkeys( data, key, &keynames );
+
+       if ( num_keys == -1 ) {
+               status = WERR_BADFILE;
+               goto done;
        }
-       
-       /* The "PrinterDriverData" key should have no subkeys */
-       if (strcmp(key, PrinterKey) == 0)
-       {
-               r_u-> needed = 2;
-               if (q_u->size < r_u->needed)
-                       return WERR_MORE_DATA;
-               enumkeys[0] = 0x0;
-               if (!make_spoolss_buffer5(p->mem_ctx, &r_u->keys, 1, enumkeys))
-                       return WERR_BADFILE;
-                       
-               return WERR_OK;
+
+       printerkey_len = init_unistr_array( &enumkeys,  keynames, NULL );
+
+       r_u->needed = printerkey_len*2;
+
+       if ( q_u->size < r_u->needed ) {
+               status = WERR_MORE_DATA;
+               goto done;
        }
-       
 
-       /* The return value for an unknown key is documented in MSDN
-          EnumPrinterKey description */
-        return WERR_BADFILE;
+       if (!make_spoolss_buffer5(p->mem_ctx, &r_u->keys, printerkey_len, enumkeys)) {
+               status = WERR_NOMEM;
+               goto done;
+       }
+                       
+       status = WERR_OK;
+
+       if ( q_u->size < r_u->needed ) 
+               status = WERR_MORE_DATA;
+
+done:
+       free_a_printer( &printer, 2 );
+       SAFE_FREE( keynames );
+       
+        return status;
 }
 
 /********************************************************************
@@ -8212,25 +8557,49 @@ WERROR _spoolss_enumprinterkey(pipes_struct *p, SPOOL_Q_ENUMPRINTERKEY *q_u, SPO
 
 WERROR _spoolss_deleteprinterkey(pipes_struct *p, SPOOL_Q_DELETEPRINTERKEY *q_u, SPOOL_R_DELETEPRINTERKEY *r_u)
 {
-       Printer_entry   *Printer = find_printer_index_by_hnd(p, &q_u->handle);
-       fstring key;
+       POLICY_HND              *handle = &q_u->handle;
+       Printer_entry           *Printer = find_printer_index_by_hnd(p, &q_u->handle);
+       fstring                 key;
+       NT_PRINTER_INFO_LEVEL   *printer = NULL;
+       int                     snum=0;
+       WERROR                  status;
+       
+       DEBUG(5,("spoolss_deleteprinterkey\n"));
        
        if (!Printer) {
-               DEBUG(2,("_spoolss_deleteprinterkey: Invalid handle (%s:%u:%u).\n", OUR_HANDLE(&q_u->handle)));
+               DEBUG(2,("_spoolss_deleteprinterkey: Invalid handle (%s:%u:%u).\n", OUR_HANDLE(handle)));
                return WERR_BADFID;
        }
+
+       /* if keyname == NULL, return error */
+       
+       if ( !q_u->keyname.buffer )
+               return WERR_INVALID_PARAM;
+               
+       if (!get_printer_snum(p, handle, &snum))
+               return WERR_BADFID;
+
+       if (Printer->access_granted != PRINTER_ACCESS_ADMINISTER) {
+               DEBUG(3, ("_spoolss_deleteprinterkey: printer properties change denied by handle\n"));
+               return WERR_ACCESS_DENIED;
+       }
+
+       status = get_a_printer(&printer, 2, lp_servicename(snum));
+       if (!W_ERROR_IS_OK(status))
+               return status;
+       
+       /* delete the key and all subneys */
        
         unistr2_to_ascii(key, &q_u->keyname, sizeof(key) - 1);
+       status = delete_all_printer_data( printer->info_2, key );       
 
-        if (strcmp(key, "PrinterDriverData") != 0)
-               return WERR_INVALID_PARAM;
-               
-       /* 
-        * this is what 2k returns when you try to delete the "PrinterDriverData"
-        * key
-        */
-        
-       return WERR_ACCESS_DENIED;      
+       if ( W_ERROR_IS_OK(status) )
+               status = mod_a_printer(*printer, 2);
+       
+       free_a_printer( &printer, 2 );
+       
+       return status;
 }
 
 
@@ -8246,14 +8615,16 @@ WERROR _spoolss_enumprinterdataex(pipes_struct *p, SPOOL_Q_ENUMPRINTERDATAEX *q_
                        needed;
        NT_PRINTER_INFO_LEVEL   *printer = NULL;
        PRINTER_ENUM_VALUES     *enum_values = NULL;
-       fstring         key, value;
+       NT_PRINTER_DATA         *p_data;
+       fstring         key;
        Printer_entry   *Printer = find_printer_index_by_hnd(p, handle);
        int             snum;
-       uint32          param_index, 
-                       data_len,
-                       type;
        WERROR          result;
-       uint8           *data=NULL;
+       int             key_index;
+       int             i;
+       REGISTRY_VALUE  *val;
+       char            *value_name;
+       int             data_len;
        
 
        DEBUG(4,("_spoolss_enumprinterdataex\n"));
@@ -8263,21 +8634,8 @@ WERROR _spoolss_enumprinterdataex(pipes_struct *p, SPOOL_Q_ENUMPRINTERDATAEX *q_
                return WERR_BADFID;
        }
 
-               
-        /* 
-        * The only key we support is "PrinterDriverData". This should return 
-        > an array of all the key/value pairs returned by EnumPrinterDataSee 
-        * _spoolss_getprinterdataex() for details    --jerry
-        */
-   
-       unistr2_to_ascii(key, &q_u->key, sizeof(key) - 1);
-       if (strcmp(key, "PrinterDriverData") != 0)
-       {
-               DEBUG(10,("_spoolss_enumprinterdataex: Unknown keyname [%s]\n", key));
-               return WERR_INVALID_PARAM;
-       }
-
-
+       /* first get the printer off of disk */
+       
        if (!get_printer_snum(p,handle, &snum))
                return WERR_BADFID;
        
@@ -8285,61 +8643,78 @@ WERROR _spoolss_enumprinterdataex(pipes_struct *p, SPOOL_Q_ENUMPRINTERDATAEX *q_
        result = get_a_printer(&printer, 2, lp_servicename(snum));
        if (!W_ERROR_IS_OK(result))
                return result;
-
        
-       /* 
-        * loop through all params and build the array to pass 
-        * back to the  client 
-        */
+       /* now look for a match on the key name */
+       
+       p_data = &printer->info_2->data;
+       
+       unistr2_to_ascii(key, &q_u->key, sizeof(key) - 1);
+       if ( (key_index = lookup_printerkey( p_data, key)) == -1  )
+       {
+               DEBUG(10,("_spoolss_enumprinterdataex: Unknown keyname [%s]\n", key));
+               result = WERR_INVALID_PARAM;
+               goto done;
+       }
+       
        result = WERR_OK;
-       param_index             = 0;
-       needed                  = 0;
-       num_entries             = 0;
+       needed = 0;
        
-       while (get_specific_param_by_index(*printer, 2, param_index, value, &data, &type, &data_len)) 
+       /* allocate the memory for the array of pointers -- if necessary */
+       
+       num_entries = regval_ctr_numvals( &p_data->keys[key_index].values );
+       if ( num_entries )
        {
-               PRINTER_ENUM_VALUES     *ptr;
-
-               DEBUG(10,("retrieved value number [%d] [%s]\n", num_entries, value));
-
-               if ((ptr=talloc_realloc(p->mem_ctx, enum_values, (num_entries+1) * sizeof(PRINTER_ENUM_VALUES))) == NULL)
+               if ( (enum_values=talloc(p->mem_ctx, num_entries*sizeof(PRINTER_ENUM_VALUES))) == NULL )
                {
-                       DEBUG(0,("talloc_realloc failed to allocate more memory!\n"));
+                       DEBUG(0,("_spoolss_enumprinterdataex: talloc() failed to allocate memory for [%d] bytes!\n",
+                               num_entries*sizeof(PRINTER_ENUM_VALUES)));
                        result = WERR_NOMEM;
                        goto done;
                }
-               enum_values = ptr;
+
+               memset( enum_values, 0x0, num_entries*sizeof(PRINTER_ENUM_VALUES) );
+       }
                
-               ZERO_STRUCTP( &enum_values[num_entries] );
+       /* 
+        * loop through all params and build the array to pass 
+        * back to the  client 
+        */
+        
+       for ( i=0; i<num_entries; i++ )
+       {
+               /* lookup the registry value */
+               
+               val = regval_ctr_specific_value( &p_data->keys[key_index].values, i );
+               DEBUG(10,("retrieved value number [%d] [%s]\n", i, regval_name(val) ));
 
                /* copy the data */
                
-               init_unistr(&enum_values[num_entries].valuename, value);
-               enum_values[num_entries].value_len = (strlen(value)+1) * 2;
-               enum_values[num_entries].type      = type;
+               value_name = regval_name( val );
+               init_unistr( &enum_values[i].valuename, value_name );
+               enum_values[i].value_len = (strlen(value_name)+1) * 2;
+               enum_values[i].type      = regval_type( val );
                
-               if ( data_len )
-               {
-                       if ( !(enum_values[num_entries].data = talloc_zero(p->mem_ctx, data_len)) ) {
-                               DEBUG(0,("talloc_realloc failed to allocate more memory [data_len=%d] for data!\n", data_len ));
+               data_len = regval_size( val );
+               if ( data_len ) {
+                       if ( !(enum_values[i].data = talloc_memdup(p->mem_ctx, regval_data_p(val), data_len)) ) 
+                       {
+                               DEBUG(0,("talloc_memdup failed to allocate memory [data_len=%d] for data!\n", 
+                                       data_len ));
                                result = WERR_NOMEM;
                                goto done;
                        }
-                       memcpy(enum_values[num_entries].data, data, data_len);
                }
-
-               enum_values[num_entries].data_len = data_len;
+               enum_values[i].data_len = data_len;
 
                /* keep track of the size of the array in bytes */
                
-               needed += spoolss_size_printer_enum_values(&enum_values[num_entries]);
-               
-               num_entries++;
-               param_index++;
+               needed += spoolss_size_printer_enum_values(&enum_values[i]);
        }
        
-       r_u->needed             = needed;
-       r_u->returned           = num_entries;
+       /* housekeeping information in the reply */
+       
+       r_u->needed     = needed;
+       r_u->returned   = num_entries;
 
        if (needed > in_size) {
                result = WERR_MORE_DATA;
@@ -8428,6 +8803,7 @@ WERROR _spoolss_getprintprocessordirectory(pipes_struct *p, SPOOL_Q_GETPRINTPROC
        case 1:
                result = getprintprocessordirectory_level_1
                  (&q_u->name, &q_u->environment, buffer, offered, needed);
+               break;
        default:
                result = WERR_UNKNOWN_LEVEL;
        }