r8066: * had to modify the printer data storage slightly in ntprinters.tdb
[gd/samba/.git] / source / rpc_server / srv_spoolss_nt.c
index 3c611be9ace60860e916f7fe1cabcec7d6e7e94a..19ef3700e6dbc3ef938ba2858185916d959f7d71 100644 (file)
@@ -28,6 +28,8 @@
 
 #include "includes.h"
 
+extern userdom_struct current_user_info;
+
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_RPC_SRV
 
@@ -581,7 +583,7 @@ static BOOL set_printer_hnd_name(Printer_entry *Printer, char *handlename)
                
                DEBUGADD(10, ("printername: %s\n", printername));
                
-                       free_a_printer( &printer, 2);
+               free_a_printer( &printer, 2);
        }
 
        if ( !found ) {
@@ -1443,7 +1445,7 @@ static DEVICEMODE* dup_devicemode(TALLOC_CTX *ctx, DEVICEMODE *devmode)
                        return NULL;
        }
 
-       d->private = TALLOC_MEMDUP(ctx, devmode->private, devmode->driverextra);
+       d->dev_private = TALLOC_MEMDUP(ctx, devmode->dev_private, devmode->driverextra);
        
        return d;
 }
@@ -1491,10 +1493,10 @@ static void convert_to_openprinterex(TALLOC_CTX *ctx, SPOOL_Q_OPEN_PRINTER_EX *q
 
        DEBUG(8,("convert_to_openprinterex\n"));
                                
-       q_u_ex->printername_ptr = q_u->printername_ptr;
-       
-       if (q_u->printername_ptr)
-               copy_unistr2(&q_u_ex->printername, &q_u->printername);
+       if ( q_u->printername ) {
+               q_u_ex->printername = TALLOC_P( ctx, UNISTR2 );
+               copy_unistr2(q_u_ex->printername, q_u->printername);
+       }
        
        copy_printer_default(ctx, &q_u_ex->printer_default, &q_u->printer_default);
 }
@@ -1588,7 +1590,6 @@ WERROR _spoolss_open_printer(pipes_struct *p, SPOOL_Q_OPEN_PRINTER *q_u, SPOOL_R
 
 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;
 
@@ -1597,15 +1598,13 @@ WERROR _spoolss_open_printer_ex( pipes_struct *p, SPOOL_Q_OPEN_PRINTER_EX *q_u,
        struct current_user user;
        Printer_entry *Printer=NULL;
 
-       if (q_u->printername_ptr != 0)
-               printername = &q_u->printername;
-
-       if (printername == NULL)
+       if ( !q_u->printername )
                return WERR_INVALID_PRINTER_NAME;
 
        /* some sanity check because you can open a printer or a print server */
        /* aka: \\server\printer or \\server */
-       unistr2_to_ascii(name, printername, sizeof(name)-1);
+
+       unistr2_to_ascii(name, q_u->printername, sizeof(name)-1);
 
        DEBUGADD(3,("checking name: %s\n",name));
 
@@ -1731,6 +1730,11 @@ WERROR _spoolss_open_printer_ex( pipes_struct *p, SPOOL_Q_OPEN_PRINTER_EX *q_u,
 
                /* check smb.conf parameters and the the sec_desc */
                
+               if ( !check_access(smbd_server_fd(), lp_hostsallow(snum), lp_hostsdeny(snum)) ) {    
+                       DEBUG(3, ("access DENIED (hosts allow/deny) for printer open\n"));
+                       return WERR_ACCESS_DENIED;
+               }
+
                if (!user_ok(uidtoname(user.uid), snum, user.groups, user.ngroups) || !print_access_check(&user, snum, printer_default->access_required)) {
                        DEBUG(3, ("access DENIED for printer open\n"));
                        close_printer_handle(p, handle);
@@ -1881,12 +1885,12 @@ BOOL convert_devicemode(const char *printername, const DEVICEMODE *devmode,
         * has a new one. JRA.
         */
 
-       if ((devmode->driverextra != 0) && (devmode->private != NULL)) {
-               SAFE_FREE(nt_devmode->private);
+       if ((devmode->driverextra != 0) && (devmode->dev_private != NULL)) {
+               SAFE_FREE(nt_devmode->nt_dev_private);
                nt_devmode->driverextra=devmode->driverextra;
-               if((nt_devmode->private=SMB_MALLOC_ARRAY(uint8, nt_devmode->driverextra)) == NULL)
+               if((nt_devmode->nt_dev_private=SMB_MALLOC_ARRAY(uint8, nt_devmode->driverextra)) == NULL)
                        return False;
-               memcpy(nt_devmode->private, devmode->private, nt_devmode->driverextra);
+               memcpy(nt_devmode->nt_dev_private, devmode->dev_private, nt_devmode->driverextra);
        }
 
        *pp_nt_devmode = nt_devmode;
@@ -2207,7 +2211,8 @@ static WERROR get_printer_dataex( TALLOC_CTX *ctx, NT_PRINTER_INFO_LEVEL *printe
                                  uint32 *needed, uint32 in_size  )
 {
        REGISTRY_VALUE          *val;
-       int                     size, data_len;
+       uint32                  size;
+       int                     data_len;
        
        if ( !(val = get_printer_data( printer->info_2, key, value)) )
                return WERR_BADFILE;
@@ -2349,10 +2354,9 @@ static WERROR getprinterdata_printer_server(TALLOC_CTX *ctx, fstring value, uint
                *type = REG_BINARY;
                *needed = 0x114;
 
-               if((*data = (uint8 *)TALLOC(ctx, *needed)) == NULL)
+               if ( !(*data = TALLOC_ZERO_ARRAY(ctx, uint8, *needed)) )
                        return WERR_NOMEM;
-               ZERO_STRUCTP( *data );
-               
+
                SIVAL(*data, 0, *needed);       /* size */
                SIVAL(*data, 4, 5);             /* Windows 2000 == 5.0 */
                SIVAL(*data, 8, 0);
@@ -4086,7 +4090,7 @@ static void free_dev_mode(DEVICEMODE *dev)
        if (dev == NULL)
                return;
 
-       SAFE_FREE(dev->private);
+       SAFE_FREE(dev->dev_private);
        SAFE_FREE(dev); 
 }
 
@@ -4129,8 +4133,8 @@ static BOOL convert_nt_devicemode( DEVICEMODE *devmode, NT_DEVICEMODE *ntdevmode
        devmode->mediatype        = ntdevmode->mediatype;
        devmode->dithertype       = ntdevmode->dithertype;
 
-       if (ntdevmode->private != NULL) {
-               if ((devmode->private=(uint8 *)memdup(ntdevmode->private, ntdevmode->driverextra)) == NULL)
+       if (ntdevmode->nt_dev_private != NULL) {
+               if ((devmode->dev_private=(uint8 *)memdup(ntdevmode->nt_dev_private, ntdevmode->driverextra)) == NULL)
                        return False;
        }
        
@@ -5794,8 +5798,9 @@ WERROR _spoolss_writeprinter(pipes_struct *p, SPOOL_Q_WRITEPRINTER *q_u, SPOOL_R
        if (!get_printer_snum(p, handle, &snum))
                return WERR_BADFID;
 
-       (*buffer_written) = print_job_write(snum, Printer->jobid, (char *)buffer, buffer_size);
-       if (*buffer_written == -1) {
+       (*buffer_written) = (uint32)print_job_write(snum, Printer->jobid, (const char *)buffer,
+                                       (SMB_OFF_T)-1, (size_t)buffer_size);
+       if (*buffer_written == (uint32)-1) {
                r_u->buffer_written = 0;
                if (errno == ENOSPC)
                        return WERR_NO_SPOOL_SPACE;
@@ -6030,7 +6035,6 @@ static BOOL check_printer_ok(NT_PRINTER_INFO_LEVEL_2 *info, int snum)
 
 static BOOL add_printer_hook(NT_USER_TOKEN *token, NT_PRINTER_INFO_LEVEL *printer)
 {
-       extern userdom_struct current_user_info;
        char *cmd = lp_addprinter_cmd();
        char **qlines;
        pstring command;
@@ -7244,24 +7248,32 @@ static void fill_port_2(PORT_INFO_2 *port, const char *name)
        port->reserved=0x0;     
 }
 
+
 /****************************************************************************
- enumports level 1.
+ wrapper around the enumer ports command
 ****************************************************************************/
 
-static WERROR enumports_level_1(RPC_BUFFER *buffer, uint32 offered, uint32 *needed, uint32 *returned)
+WERROR enumports_hook( int *count, char ***lines )
 {
-       PORT_INFO_1 *ports=NULL;
-       int i=0;
-       WERROR result = WERR_OK;
+       char *cmd = lp_enumports_cmd();
+       char **qlines;
+       pstring command;
+       int numlines;
+       int ret;
+       int fd;
 
-       if (*lp_enumports_cmd()) {
-               char *cmd = lp_enumports_cmd();
-               char **qlines;
-               pstring command;
-               int numlines;
-               int ret;
-               int fd;
 
+       /* if no hook then just fill in the default port */
+       
+       if ( !*cmd ) {
+               qlines = SMB_MALLOC_ARRAY( char*, 2 );
+               qlines[0] = SMB_STRDUP( SAMBA_PRINTER_PORT_NAME );
+               qlines[1] = NULL;
+               numlines = 1;
+       }
+       else {
+               /* we have a valid enumport command */
+               
                slprintf(command, sizeof(command)-1, "%s \"%d\"", cmd, 1);
 
                DEBUG(10,("Running [%s]\n", command));
@@ -7270,7 +7282,7 @@ static WERROR enumports_level_1(RPC_BUFFER *buffer, uint32 offered, uint32 *need
                if (ret != 0) {
                        if (fd != -1)
                                close(fd);
-                       /* Is this the best error to return here? */
+                       
                        return WERR_ACCESS_DENIED;
                }
 
@@ -7278,36 +7290,47 @@ static WERROR enumports_level_1(RPC_BUFFER *buffer, uint32 offered, uint32 *need
                qlines = fd_lines_load(fd, &numlines);
                DEBUGADD(10,("Lines returned = [%d]\n", numlines));
                close(fd);
+       }
+       
+       *count = numlines;
+       *lines = qlines;
 
-               if(numlines) {
-                       if((ports=SMB_MALLOC_ARRAY( PORT_INFO_1, numlines )) == NULL) {
-                               DEBUG(10,("Returning WERR_NOMEM [%s]\n", 
-                                         dos_errstr(WERR_NOMEM)));
-                               file_lines_free(qlines);
-                               return WERR_NOMEM;
-                       }
+       return WERR_OK;
+}
 
-                       for (i=0; i<numlines; i++) {
-                               DEBUG(6,("Filling port number [%d] with port [%s]\n", i, qlines[i]));
-                               fill_port_1(&ports[i], qlines[i]);
-                       }
+/****************************************************************************
+ enumports level 1.
+****************************************************************************/
+
+static WERROR enumports_level_1(RPC_BUFFER *buffer, uint32 offered, uint32 *needed, uint32 *returned)
+{
+       PORT_INFO_1 *ports=NULL;
+       int i=0;
+       WERROR result = WERR_OK;
+       char **qlines;
+       int numlines;
 
+       if ( !W_ERROR_IS_OK(result = enumports_hook( &numlines, &qlines )) ) 
+               return result;
+       
+       if(numlines) {
+               if((ports=SMB_MALLOC_ARRAY( PORT_INFO_1, numlines )) == NULL) {
+                       DEBUG(10,("Returning WERR_NOMEM [%s]\n", 
+                                 dos_errstr(WERR_NOMEM)));
                        file_lines_free(qlines);
+                       return WERR_NOMEM;
                }
 
-               *returned = numlines;
-
-       } else {
-               *returned = 1; /* Sole Samba port returned. */
-
-               if((ports=SMB_MALLOC_P(PORT_INFO_1)) == NULL)
-                       return WERR_NOMEM;
-       
-               DEBUG(10,("enumports_level_1: port name %s\n", SAMBA_PRINTER_PORT_NAME));
+               for (i=0; i<numlines; i++) {
+                       DEBUG(6,("Filling port number [%d] with port [%s]\n", i, qlines[i]));
+                       fill_port_1(&ports[i], qlines[i]);
+               }
 
-               fill_port_1(&ports[0], SAMBA_PRINTER_PORT_NAME);
+               file_lines_free(qlines);
        }
 
+       *returned = numlines;
+
        /* check the required size. */
        for (i=0; i<*returned; i++) {
                DEBUGADD(6,("adding port [%d]'s size\n", i));
@@ -7348,69 +7371,29 @@ static WERROR enumports_level_2(RPC_BUFFER *buffer, uint32 offered, uint32 *need
        PORT_INFO_2 *ports=NULL;
        int i=0;
        WERROR result = WERR_OK;
+       char **qlines;
+       int numlines;
 
-       if (*lp_enumports_cmd()) {
-               char *cmd = lp_enumports_cmd();
-               char *path;
-               char **qlines;
-               pstring tmp_file;
-               pstring command;
-               int numlines;
-               int ret;
-               int fd;
-
-               if (*lp_pathname(lp_servicenumber(PRINTERS_NAME)))
-                       path = lp_pathname(lp_servicenumber(PRINTERS_NAME));
-               else
-                       path = lp_lockdir();
-
-               slprintf(tmp_file, sizeof(tmp_file)-1, "%s/smbcmd.%u.", path, (unsigned int)sys_getpid());
-               slprintf(command, sizeof(command)-1, "%s \"%d\"", cmd, 2);
-
-               unlink(tmp_file);
-               DEBUG(10,("Running [%s > %s]\n", command,tmp_file));
-               ret = smbrun(command, &fd);
-               DEBUGADD(10,("returned [%d]\n", ret));
-               if (ret != 0) {
-                       if (fd != -1)
-                               close(fd);
-                       /* Is this the best error to return here? */
-                       return WERR_ACCESS_DENIED;
-               }
-
-               numlines = 0;
-               qlines = fd_lines_load(fd, &numlines);
-               DEBUGADD(10,("Lines returned = [%d]\n", numlines));
-               close(fd);
-
-               if(numlines) {
-                       if((ports=SMB_MALLOC_ARRAY( PORT_INFO_2, numlines)) == NULL) {
-                               file_lines_free(qlines);
-                               return WERR_NOMEM;
-                       }
-
-                       for (i=0; i<numlines; i++) {
-                               DEBUG(6,("Filling port number [%d] with port [%s]\n", i, qlines[i]));
-                               fill_port_2(&(ports[i]), qlines[i]);
-                       }
-
+       if ( !W_ERROR_IS_OK(result = enumports_hook( &numlines, &qlines )) ) 
+               return result;
+       
+       
+       if(numlines) {
+               if((ports=SMB_MALLOC_ARRAY( PORT_INFO_2, numlines)) == NULL) {
                        file_lines_free(qlines);
+                       return WERR_NOMEM;
                }
 
-               *returned = numlines;
-
-       } else {
-
-               *returned = 1;
-
-               if((ports=SMB_MALLOC_P(PORT_INFO_2)) == NULL)
-                       return WERR_NOMEM;
-       
-               DEBUG(10,("enumports_level_2: port name %s\n", SAMBA_PRINTER_PORT_NAME));
+               for (i=0; i<numlines; i++) {
+                       DEBUG(6,("Filling port number [%d] with port [%s]\n", i, qlines[i]));
+                       fill_port_2(&(ports[i]), qlines[i]);
+               }
 
-               fill_port_2(&ports[0], SAMBA_PRINTER_PORT_NAME);
+               file_lines_free(qlines);
        }
 
+       *returned = numlines;
+
        /* check the required size. */
        for (i=0; i<*returned; i++) {
                DEBUGADD(6,("adding port [%d]'s size\n", i));
@@ -7595,7 +7578,7 @@ static WERROR spoolss_addprinterex_level_2( pipes_struct *p, const UNISTR2 *uni_
 
 WERROR _spoolss_addprinterex( pipes_struct *p, SPOOL_Q_ADDPRINTEREX *q_u, SPOOL_R_ADDPRINTEREX *r_u)
 {
-       UNISTR2 *uni_srv_name = &q_u->server_name;
+       UNISTR2 *uni_srv_name = q_u->server_name;
        uint32 level = q_u->level;
        SPOOL_PRINTER_INFO_LEVEL *info = &q_u->info;
        DEVICEMODE *devmode = q_u->devmode_ctr.devmode;
@@ -7646,9 +7629,7 @@ WERROR _spoolss_addprinterdriver(pipes_struct *p, SPOOL_Q_ADDPRINTERDRIVER *q_u,
                goto done;
 
        DEBUG(5,("Moving driver to final destination\n"));
-       if(!move_driver_to_download_area(driver, level, &user, &err)) {
-               if (W_ERROR_IS_OK(err))
-                       err = WERR_ACCESS_DENIED;
+       if( !W_ERROR_IS_OK(err = move_driver_to_download_area(driver, level, &user, &err)) ) {
                goto done;
        }
 
@@ -8050,7 +8031,7 @@ WERROR _spoolss_enumprinterdata(pipes_struct *p, SPOOL_Q_ENUMPRINTERDATA *q_u, S
                        result = WERR_NOMEM;
                        goto done;
                }
-               data_len = (size_t)regval_size(val);
+               data_len = regval_size(val);
                memcpy( *data_out, regval_data_p(val), data_len );
                *out_data_len = data_len;
        }
@@ -9270,7 +9251,7 @@ WERROR _spoolss_enumprinterdataex(pipes_struct *p, SPOOL_Q_ENUMPRINTERDATAEX *q_
        int             i;
        REGISTRY_VALUE  *val;
        char            *value_name;
-       int             data_len;
+       uint32          data_len;
        
 
        DEBUG(4,("_spoolss_enumprinterdataex\n"));