Merge branch 'sharedm4' of /home/jelmer/samba4
[ira/wip.git] / source3 / printing / print_generic.c
index 45c9d445e8b961dfebd37b416965512bcf42962e..b789354999d2ae7391acabe8f6471f1b8bef98d4 100644 (file)
@@ -1,12 +1,11 @@
 /* 
-   Unix SMB/Netbios implementation.
-   Version 3.0
+   Unix SMB/CIFS implementation.
    printing command routines
    Copyright (C) Andrew Tridgell 1992-2000
    
    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,
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
+#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 int DEBUGLEVEL;
+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
+ Run a given print command
+ a null terminated list of value/substitute pairs is provided
+ for local substitution strings
 ****************************************************************************/
-
-#ifdef HAVE_STDARG_H
-static int print_run_command(int snum,char *command, int *outfd, char *outfile, ...)
-{
-#else /* HAVE_STDARG_H */
-static int print_run_command(va_alist)
-va_dcl
+static int print_run_command(int snum, const char* printername, bool do_sub,
+                            const char *command, int *outfd, ...)
 {
-       int snum;
-       int *outfd;
-       char *command, *outfile;
-#endif /* HAVE_STDARG_H */
-
-       pstring syscmd;
-       char *p, *arg;
+       char *syscmd;
+       char *arg;
        int ret;
+       TALLOC_CTX *ctx = talloc_tos();
        va_list ap;
+       va_start(ap, outfd);
 
-#ifdef HAVE_STDARG_H
-       va_start(ap, outfile);
-#else /* HAVE_STDARG_H */
-       va_start(ap);
-       snum = va_arg(ap,int);
-       fd = va_arg(ap, int *);
-       command = va_arg(ap,char *);
-       outfile = va_arg(ap,char *);
-#endif /* HAVE_STDARG_H */
+       /* check for a valid system printername and valid command to run */
 
-       if (!command || !*command) return -1;
+       if ( !printername || !*printername ) {
+               va_end(ap);
+               return -1;
+       }
 
-       if (!VALID_SNUM(snum)) {
-               DEBUG(0,("Invalid snum %d for command %s\n", snum, command));
+       if (!command || !*command) {
+               va_end(ap);
                return -1;
        }
 
-       pstrcpy(syscmd, command);
+       syscmd = talloc_strdup(ctx, command);
+       if (!syscmd) {
+               va_end(ap);
+               return -1;
+       }
 
        while ((arg = va_arg(ap, char *))) {
                char *value = va_arg(ap,char *);
-               pstring_sub(syscmd, arg, value);
+               syscmd = talloc_string_sub(ctx, syscmd, arg, value);
+               if (!syscmd) {
+                       va_end(ap);
+                       return -1;
+               }
        }
        va_end(ap);
-  
-       p = PRINTERNAME(snum);
-  
-       pstring_sub(syscmd, "%p", p);
-       standard_sub_snum(snum,syscmd);
 
-       /* Convert script args to unix-codepage */
-       dos_to_unix(syscmd, True);
-       ret = smbrun(syscmd,outfd,outfile);
+       syscmd = talloc_string_sub(ctx, syscmd, "%p", printername);
+       if (!syscmd) {
+               return -1;
+       }
+
+       if (do_sub && snum != -1) {
+               syscmd = talloc_sub_advanced(ctx,
+                               lp_servicename(snum),
+                               current_user_info.unix_name,
+                               "",
+                               current_user.ut.gid,
+                               get_current_username(),
+                               current_user_info.domain,
+                               syscmd);
+               if (!syscmd) {
+                       return -1;
+               }
+       }
+
+       ret = smbrun_no_sanitize(syscmd,outfd);
 
        DEBUG(3,("Running the command `%s' gave %d\n",syscmd,ret));
 
@@ -115,17 +96,15 @@ va_dcl
 /****************************************************************************
 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, NULL,
+       return print_run_command( -1, sharename, False, lprm_command, NULL,
                   "%j", jobstr,
-                  "%T", http_timestring(pjob->starttime),
+                  "%T", http_timestring(talloc_tos(), pjob->starttime),
                   NULL);
 }
 
@@ -138,8 +117,8 @@ 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, 
-                                lp_lppausecommand(snum), NULL, NULL,
+       return print_run_command(snum, PRINTERNAME(snum), True,
+                                lp_lppausecommand(snum), NULL,
                                 "%j", jobstr,
                                 NULL);
 }
@@ -150,11 +129,11 @@ resume a job
 static int generic_job_resume(int snum, struct printjob *pjob)
 {
        fstring jobstr;
-       
+
        /* need to pause the spooled entry */
        slprintf(jobstr, sizeof(jobstr)-1, "%d", pjob->sysjob);
-       return print_run_command(snum, 
-                                lp_lpresumecommand(snum), NULL, NULL,
+       return print_run_command(snum, PRINTERNAME(snum), True,
+                                lp_lpresumecommand(snum), NULL,
                                 "%j", jobstr,
                                 NULL);
 }
@@ -165,40 +144,71 @@ static int generic_job_resume(int snum, struct printjob *pjob)
 
 static int generic_job_submit(int snum, struct printjob *pjob)
 {
-       int ret;
-       pstring current_directory;
-       pstring print_directory;
-       char *wd, *p;
-       pstring jobname;
+       int ret = -1;
+       char *current_directory = NULL;
+       char *print_directory = NULL;
+       char *wd = NULL;
+       char *p = NULL;
+       char *jobname = NULL;
+       TALLOC_CTX *ctx = talloc_tos();
+       fstring job_page_count, job_size;
 
        /* we print from the directory path to give the best chance of
            parsing the lpq output */
+       current_directory = TALLOC_ARRAY(ctx,
+                                       char,
+                                       PATH_MAX+1);
+       if (!current_directory) {
+               return -1;
+       }
        wd = sys_getwd(current_directory);
-       if (!wd)
-               return 0;
+       if (!wd) {
+               return -1;
+       }
 
-       pstrcpy(print_directory, pjob->filename);
-       p = strrchr(print_directory,'/');
-       if (!p)
-               return 0;
+       print_directory = talloc_strdup(ctx, pjob->filename);
+       if (!print_directory) {
+               return -1;
+       }
+       p = strrchr_m(print_directory,'/');
+       if (!p) {
+               return -1;
+       }
        *p++ = 0;
 
-       if (chdir(print_directory) != 0)
-               return 0;
+       if (chdir(print_directory) != 0) {
+               return -1;
+       }
 
-       pstrcpy(jobname, pjob->jobname);
-       pstring_sub(jobname, "'", "_");
+       jobname = talloc_strdup(ctx, pjob->jobname);
+       if (!jobname) {
+               ret = -1;
+               goto out;
+       }
+       jobname = talloc_string_sub(ctx, jobname, "'", "_");
+       if (!jobname) {
+               ret = -1;
+               goto out;
+       }
+       slprintf(job_page_count, sizeof(job_page_count)-1, "%d", pjob->page_count);
+       slprintf(job_size, sizeof(job_size)-1, "%lu", (unsigned long)pjob->size);
 
        /* send it to the system spooler */
-       ret = print_run_command(snum, 
-                         lp_printcommand(snum), NULL, NULL,
-                         "%s", p,
-                         "%J", jobname,
-                         "%f", p,
-                         NULL);
-
-       chdir(wd);
-
+       ret = print_run_command(snum, PRINTERNAME(snum), True,
+                       lp_printcommand(snum), NULL,
+                       "%s", p,
+                       "%J", jobname,
+                       "%f", p,
+                       "%z", job_size,
+                       "%c", job_page_count,
+                       NULL);
+
+ out:
+
+       if (chdir(wd) == -1) {
+               smb_panic("chdir failed in generic_job_submit");
+       }
+       TALLOC_FREE(current_directory);
         return ret;
 }
 
@@ -206,25 +216,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 *path = lp_pathname(snum);
-       char *cmd = lp_lpqcommand(snum);
        char **qlines;
        int fd;
-       pstring tmp_file;
        int numlines, i, qcount;
-       print_queue_struct *queue;
-       fstring printer_name;
-              
-       /* Convert printer name (i.e. share name) to unix-codepage */
-       fstrcpy(printer_name, lp_servicename(snum));
-       dos_to_unix(printer_name, True);
+       print_queue_struct *queue = NULL;
        
-       slprintf(tmp_file, sizeof(tmp_file)-1, "%s/smblpq.%d", path, sys_getpid());
+       /* 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. */
 
-       unlink(tmp_file);
-       print_run_command(snum, cmd, &fd, tmp_file, NULL);
+       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",
@@ -233,26 +240,31 @@ static int generic_queue_get(int snum, print_queue_struct **q, print_status_stru
        }
        
        numlines = 0;
-       qlines = fd_lines_load(fd, &numlines, True);
+       qlines = fd_lines_load(fd, &numlines,0,NULL);
        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 (numlines && qlines) {
+               queue = SMB_MALLOC_ARRAY(print_queue_struct, numlines+1);
+               if (!queue) {
+                       TALLOC_FREE(qlines);
+                       *q = NULL;
+                       return 0;
+               }
+               memset(queue, '\0', sizeof(print_queue_struct)*(numlines+1));
 
-       if (queue) {
                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);
 
+       TALLOC_FREE(qlines);
         *q = queue;
        return qcount;
 }
@@ -262,8 +274,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,
-                                NULL);
+       return print_run_command(snum, PRINTERNAME(snum), True, lp_queuepausecommand(snum), NULL, NULL);
 }
 
 /****************************************************************************
@@ -271,6 +282,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,
-                                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,
+};
+