r23779: Change from v2 or later to v3 or later.
[jra/samba/.git] / source3 / printing / print_generic.c
index 0b062ebdd98d3340ccb50fac367bf0b1357699e4..051bb967feec32c8618c483d0d9484c304cd0e34 100644 (file)
@@ -5,7 +5,7 @@
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
 
+#include "includes.h"
 #include "printing.h"
 
-
-/*
- * Generic printing interface definitions...
- */
-
-static int generic_job_delete(int snum, struct printjob *pjob);
-static int generic_job_pause(int snum, struct printjob *pjob);
-static int generic_job_resume(int snum, struct printjob *pjob);
-static int generic_job_submit(int snum, struct printjob *pjob);
-static int generic_queue_get(int snum, print_queue_struct **q,
-                             print_status_struct *status);
-static int generic_queue_pause(int snum);
-static int generic_queue_resume(int snum);
-
-
-struct printif generic_printif =
-               {
-                 generic_queue_get,
-                 generic_queue_pause,
-                 generic_queue_resume,
-                 generic_job_delete,
-                 generic_job_pause,
-                 generic_job_resume,
-                 generic_job_submit,
-               };
+extern struct current_user current_user;
+extern userdom_struct current_user_info;
 
 /****************************************************************************
 run a given print command 
 a null terminated list of value/substitute pairs is provided
 for local substitution strings
 ****************************************************************************/
-static int print_run_command(int snum,char *command, int *outfd, ...)
+static int print_run_command(int snum, const char* printername, BOOL do_sub,
+                            const char *command, int *outfd, ...)
 {
-
        pstring syscmd;
        char *arg;
        int ret;
        va_list ap;
        va_start(ap, outfd);
 
-       if (!command || !*command) return -1;
+       /* check for a valid system printername and valid command to run */
 
-       if (!VALID_SNUM(snum)) {
-               DEBUG(0,("Invalid snum %d for command %s\n", snum, command));
+       if ( !printername || !*printername ) 
+               return -1;
+
+       if (!command || !*command) 
                return -1;
-       }
 
        pstrcpy(syscmd, command);
 
@@ -75,10 +54,17 @@ static int print_run_command(int snum,char *command, int *outfd, ...)
        }
        va_end(ap);
   
-       pstring_sub(syscmd, "%p", PRINTERNAME(snum));
-       standard_sub_snum(snum,syscmd,sizeof(syscmd));
-
-       ret = smbrun(syscmd,outfd);
+       pstring_sub( syscmd, "%p", printername );
+
+       if ( do_sub && snum != -1 )
+               standard_sub_advanced(lp_servicename(snum),
+                                     current_user_info.unix_name, "",
+                                     current_user.ut.gid,
+                                     get_current_username(),
+                                     current_user_info.domain,
+                                     syscmd, sizeof(syscmd));
+               
+       ret = smbrun_no_sanitize(syscmd,outfd);
 
        DEBUG(3,("Running the command `%s' gave %d\n",syscmd,ret));
 
@@ -89,15 +75,13 @@ static int print_run_command(int snum,char *command, int *outfd, ...)
 /****************************************************************************
 delete a print job
 ****************************************************************************/
-static int generic_job_delete(int snum, struct printjob *pjob)
+static int generic_job_delete( const char *sharename, const char *lprm_command, struct printjob *pjob)
 {
        fstring jobstr;
 
        /* need to delete the spooled entry */
        slprintf(jobstr, sizeof(jobstr)-1, "%d", pjob->sysjob);
-       return print_run_command(
-                  snum, 
-                  lp_lprmcommand(snum), NULL,
+       return print_run_command( -1, sharename, False, lprm_command, NULL,
                   "%j", jobstr,
                   "%T", http_timestring(pjob->starttime),
                   NULL);
@@ -112,7 +96,7 @@ static int generic_job_pause(int snum, struct printjob *pjob)
        
        /* need to pause the spooled entry */
        slprintf(jobstr, sizeof(jobstr)-1, "%d", pjob->sysjob);
-       return print_run_command(snum, 
+       return print_run_command(snum, PRINTERNAME(snum), True,
                                 lp_lppausecommand(snum), NULL,
                                 "%j", jobstr,
                                 NULL);
@@ -127,7 +111,7 @@ static int generic_job_resume(int snum, struct printjob *pjob)
        
        /* need to pause the spooled entry */
        slprintf(jobstr, sizeof(jobstr)-1, "%d", pjob->sysjob);
-       return print_run_command(snum, 
+       return print_run_command(snum, PRINTERNAME(snum), True,
                                 lp_lpresumecommand(snum), NULL,
                                 "%j", jobstr,
                                 NULL);
@@ -164,10 +148,10 @@ static int generic_job_submit(int snum, struct printjob *pjob)
        pstrcpy(jobname, pjob->jobname);
        pstring_sub(jobname, "'", "_");
        slprintf(job_page_count, sizeof(job_page_count)-1, "%d", pjob->page_count);
-       slprintf(job_size, sizeof(job_size)-1, "%d", pjob->size);
+       slprintf(job_size, sizeof(job_size)-1, "%lu", (unsigned long)pjob->size);
 
        /* send it to the system spooler */
-       ret = print_run_command(snum, 
+       ret = print_run_command(snum, PRINTERNAME(snum), True,
                        lp_printcommand(snum), NULL,
                        "%s", p,
                        "%J", jobname,
@@ -185,17 +169,22 @@ static int generic_job_submit(int snum, struct printjob *pjob)
 /****************************************************************************
 get the current list of queued jobs
 ****************************************************************************/
-static int generic_queue_get(int snum, print_queue_struct **q, print_status_struct *status)
+static int generic_queue_get(const char *printer_name, 
+                             enum printing_types printing_type,
+                             char *lpq_command,
+                             print_queue_struct **q, 
+                             print_status_struct *status)
 {
        char **qlines;
        int fd;
        int numlines, i, qcount;
        print_queue_struct *queue = NULL;
-       fstring printer_name;
-              
-       fstrcpy(printer_name, lp_servicename(snum));
        
-       print_run_command(snum, lp_lpqcommand(snum), &fd, NULL);
+       /* never do substitution when running the 'lpq command' since we can't
+          get it rigt when using the background update daemon.  Make the caller 
+          do it before passing off the command string to us here. */
+
+       print_run_command(-1, printer_name, False, lpq_command, &fd, NULL);
 
        if (fd == -1) {
                DEBUG(5,("generic_queue_get: Can't read print queue status for printer %s\n",
@@ -204,27 +193,31 @@ static int generic_queue_get(int snum, print_queue_struct **q, print_status_stru
        }
        
        numlines = 0;
-       qlines = fd_lines_load(fd, &numlines);
+       qlines = fd_lines_load(fd, &numlines,0);
        close(fd);
 
        /* turn the lpq output into a series of job structures */
        qcount = 0;
        ZERO_STRUCTP(status);
-       if (numlines)
-               queue = (print_queue_struct *)malloc(sizeof(print_queue_struct)*(numlines+1));
-
-       if (queue) {
+       if (numlines && qlines) {
+               queue = SMB_MALLOC_ARRAY(print_queue_struct, numlines+1);
+               if (!queue) {
+                       file_lines_free(qlines);
+                       *q = NULL;
+                       return 0;
+               }
                memset(queue, '\0', sizeof(print_queue_struct)*(numlines+1));
+
                for (i=0; i<numlines; i++) {
                        /* parse the line */
-                       if (parse_lpq_entry(snum,qlines[i],
+                       if (parse_lpq_entry(printing_type,qlines[i],
                                            &queue[qcount],status,qcount==0)) {
                                qcount++;
                        }
                }               
        }
-       file_lines_free(qlines);
 
+       file_lines_free(qlines);
         *q = queue;
        return qcount;
 }
@@ -234,7 +227,7 @@ static int generic_queue_get(int snum, print_queue_struct **q, print_status_stru
 ****************************************************************************/
 static int generic_queue_pause(int snum)
 {
-       return print_run_command(snum, lp_queuepausecommand(snum), NULL, NULL);
+       return print_run_command(snum, PRINTERNAME(snum), True, lp_queuepausecommand(snum), NULL, NULL);
 }
 
 /****************************************************************************
@@ -242,5 +235,22 @@ static int generic_queue_pause(int snum)
 ****************************************************************************/
 static int generic_queue_resume(int snum)
 {
-       return print_run_command(snum, lp_queueresumecommand(snum), NULL, NULL);
+       return print_run_command(snum, PRINTERNAME(snum), True, lp_queueresumecommand(snum), NULL, NULL);
 }
+
+/****************************************************************************
+ * Generic printing interface definitions...
+ ***************************************************************************/
+
+struct printif generic_printif =
+{
+       DEFAULT_PRINTING,
+       generic_queue_get,
+       generic_queue_pause,
+       generic_queue_resume,
+       generic_job_delete,
+       generic_job_pause,
+       generic_job_resume,
+       generic_job_submit,
+};
+