]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - scheduler/process.c
Merge changes from CUPS 1.5svn-r9022.
[thirdparty/cups.git] / scheduler / process.c
index d4cedbe31c4204b324e547bb5a7a6239463b6790..2bc6d164a18014c00751fc429cc08d88282116d2 100644 (file)
@@ -3,7 +3,7 @@
  *
  *   Process management routines for the Common UNIX Printing System (CUPS).
  *
- *   Copyright 2007-2009 by Apple Inc.
+ *   Copyright 2007-2010 by Apple Inc.
  *   Copyright 1997-2007 by Easy Software Products, all rights reserved.
  *
  *   These coded instructions, statements, and computer programs are the
@@ -44,7 +44,8 @@
 
 typedef struct
 {
-  int  pid;                            /* Process ID */
+  int  pid,                            /* Process ID */
+       job_id;                         /* Job associated with process */
   char name[1];                        /* Name of process */
 } cupsd_proc_t;
 
@@ -82,8 +83,22 @@ cupsdCreateProfile(int job_id)               /* I - Job ID or 0 for none */
                temp[1024];             /* Quoted TempDir */
 
 
+  if (!UseProfiles || RunUser)
+  {
+   /*
+    * Only use sandbox profiles as root...
+    */
+
+    cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdCreateProfile(job_id=%d) = NULL",
+                    job_id);
+
+    return (NULL);
+  }
+
   if ((fp = cupsTempFile2(profile, sizeof(profile))) == NULL)
   {
+    cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdCreateProfile(job_id=%d) = NULL",
+                    job_id);
     cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to create security profile: %s",
                     strerror(errno));
     return (NULL);
@@ -99,23 +114,50 @@ cupsdCreateProfile(int job_id)             /* I - Job ID or 0 for none */
   cupsFilePuts(fp, "(allow default)\n");
   cupsFilePrintf(fp,
                  "(deny file-write* file-read-data file-read-metadata\n"
-                 "  (regex #\"^%s/\"))\n", request);
+                 "  (regex"
+                " #\"^%s$\""           /* RequestRoot */
+                " #\"^%s/\""           /* RequestRoot/... */
+                " #\"^/Users$\""
+                " #\"^/Users/\""
+                "))\n",
+                request, request);
   cupsFilePrintf(fp,
                  "(deny file-write*\n"
-                 "  (regex #\"^%s\" #\"^/private/etc\" #\"^/usr/local/etc\" "
-                "#\"^/Library\" #\"^/System\" #\"^/Users\"))\n", root);
+                 "  (regex"
+                " #\"^%s$\""           /* ServerRoot */
+                " #\"^%s/\""           /* ServerRoot/... */
+                " #\"^/private/etc$\""
+                " #\"^/private/etc/\""
+                " #\"^/usr/local/etc$\""
+                " #\"^/usr/local/etc/\""
+                " #\"^/Library$\""
+                " #\"^/Library/\""
+                " #\"^/System$\""
+                " #\"^/System/\""
+                "))\n",
+                root, root);
   cupsFilePrintf(fp,
                  "(allow file-write* file-read-data file-read-metadata\n"
-                 "  (regex #\"^%s$\" #\"^%s/\" #\"^%s$\" #\"^%s/\""
+                 "  (regex"
+                " #\"^%s$\""           /* TempDir */
+                " #\"^%s/\""           /* TempDir/... */
+                " #\"^%s$\""           /* CacheDir */
+                " #\"^%s/\""           /* CacheDir/... */
+                " #\"^%s/Library$\""   /* RequestRoot/Library */
+                " #\"^%s/Library/\""   /* RequestRoot/Library/... */
                 " #\"^/Library/Application Support/\""
                 " #\"^/Library/Caches/\""
                 " #\"^/Library/Preferences/\""
-                " #\"^/Library/Printers/\""
+                " #\"^/Library/Printers/.*/\""
+                " #\"^/Users/Shared/\""
                 "))\n",
-                temp, temp, cache, cache);
+                temp, temp, cache, cache, request, request);
   cupsFilePuts(fp,
               "(deny file-write*\n"
-              "  (regex #\"^/Library/Printers/PPDs/\""
+              "  (regex"
+              " #\"^/Library/Printers/PPDs$\""
+              " #\"^/Library/Printers/PPDs/\""
+              " #\"^/Library/Printers/PPD Plugins$\""
               " #\"^/Library/Printers/PPD Plugins/\""
               "))\n");
   if (job_id)
@@ -129,7 +171,10 @@ cupsdCreateProfile(int job_id)             /* I - Job ID or 0 for none */
   cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdCreateProfile(job_id=%d) = \"%s\"",
                   job_id, profile);
   return ((void *)strdup(profile));
+
 #else
+  cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdCreateProfile(job_id=%d) = NULL",
+                  job_id);
 
   return (NULL);
 #endif /* HAVE_SANDBOX_H */
@@ -143,11 +188,12 @@ cupsdCreateProfile(int job_id)            /* I - Job ID or 0 for none */
 void
 cupsdDestroyProfile(void *profile)     /* I - Profile */
 {
+  cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdDeleteProfile(profile=\"%s\")",
+                 profile ? (char *)profile : "(null)");
+
 #ifdef HAVE_SANDBOX_H
   if (profile)
   {
-    cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdDeleteProfile(profile=\"%s\")",
-                    (char *)profile);
     unlink((char *)profile);
     free(profile);
   }
@@ -163,7 +209,12 @@ int                                        /* O - 0 on success, -1 on failure */
 cupsdEndProcess(int pid,               /* I - Process ID */
                 int force)             /* I - Force child to die */
 {
-  if (force)
+  cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdEndProcess(pid=%d, force=%d)", pid,
+                  force);
+
+  if (!pid)
+    return (0);
+  else if (force)
     return (kill(pid, SIGKILL));
   else
     return (kill(pid, SIGTERM));
@@ -177,7 +228,8 @@ cupsdEndProcess(int pid,            /* I - Process ID */
 const char *                           /* O - Process name */
 cupsdFinishProcess(int  pid,           /* I - Process ID */
                    char *name,         /* I - Name buffer */
-                  int  namelen)        /* I - Size of name buffer */
+                  int  namelen,        /* I - Size of name buffer */
+                  int  *job_id)        /* O - Job ID pointer or NULL */
 {
   cupsd_proc_t key,                    /* Search key */
                *proc;                  /* Matching process */
@@ -187,14 +239,27 @@ cupsdFinishProcess(int  pid,              /* I - Process ID */
 
   if ((proc = (cupsd_proc_t *)cupsArrayFind(process_array, &key)) != NULL)
   {
+    if (job_id)
+      *job_id = proc->job_id;
+
     strlcpy(name, proc->name, namelen);
     cupsArrayRemove(process_array, proc);
     free(proc);
-
-    return (name);
   }
   else
-    return ("unknown");
+  {
+    if (job_id)
+      *job_id = 0;
+
+    strlcpy(name, "unknown", namelen);
+  }
+
+  cupsdLogMessage(CUPSD_LOG_DEBUG2,
+                 "cupsdFinishProcess(pid=%d, name=%p, namelen=%d, "
+                 "job_id=%p(%d)) = \"%s\"", pid, name, namelen, job_id,
+                 job_id ? *job_id : 0, name);
+
+  return (name);
 }
 
 
@@ -204,17 +269,18 @@ cupsdFinishProcess(int  pid,              /* I - Process ID */
 
 int                                    /* O - Process ID or 0 */
 cupsdStartProcess(
-    const char *command,               /* I - Full path to command */
-    char       *argv[],                        /* I - Command-line arguments */
-    char       *envp[],                        /* I - Environment */
-    int        infd,                   /* I - Standard input file descriptor */
-    int        outfd,                  /* I - Standard output file descriptor */
-    int        errfd,                  /* I - Standard error file descriptor */
-    int        backfd,                 /* I - Backchannel file descriptor */
-    int        sidefd,                 /* I - Sidechannel file descriptor */
-    int        root,                   /* I - Run as root? */
-    void       *profile,               /* I - Security profile to use */
-    int        *pid)                   /* O - Process ID */
+    const char  *command,              /* I - Full path to command */
+    char        *argv[],               /* I - Command-line arguments */
+    char        *envp[],               /* I - Environment */
+    int         infd,                  /* I - Standard input file descriptor */
+    int         outfd,                 /* I - Standard output file descriptor */
+    int         errfd,                 /* I - Standard error file descriptor */
+    int         backfd,                        /* I - Backchannel file descriptor */
+    int         sidefd,                        /* I - Sidechannel file descriptor */
+    int         root,                  /* I - Run as root? */
+    void        *profile,              /* I - Security profile to use */
+    cupsd_job_t *job,                  /* I - Job associated with process */
+    int         *pid)                  /* O - Process ID */
 {
   int          user;                   /* Command UID */
   struct stat  commandinfo;            /* Command file information */
@@ -229,10 +295,6 @@ cupsdStartProcess(
 #endif /* __APPLE__ */
 
 
-  cupsdLogMessage(CUPSD_LOG_DEBUG2,
-                  "cupsdStartProcess(\"%s\", %p, %p, %d, %d, %d)",
-                  command, argv, envp, infd, outfd, errfd);
-
   if (RunUser)
     user = RunUser;
   else if (root)
@@ -242,28 +304,70 @@ cupsdStartProcess(
 
   if (stat(command, &commandinfo))
   {
+    *pid = 0;
+
+    cupsdLogMessage(CUPSD_LOG_DEBUG2,
+                   "cupsdStartProcess(command=\"%s\", argv=%p, envp=%p, "
+                   "infd=%d, outfd=%d, errfd=%d, backfd=%d, sidefd=%d, root=%d, "
+                   "profile=%p, job=%p(%d), pid=%p) = %d",
+                   command, argv, envp, infd, outfd, errfd, backfd, sidefd,
+                   root, profile, job, job ? job->id : 0, pid, *pid);
     cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to execute %s: %s", command,
                     strerror(errno));
-    *pid = 0;
+
+    if (job && job->printer)
+    {
+      if (cupsdSetPrinterReasons(job->printer, "+cups-missing-filter-warning"))
+       cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, job->printer, NULL,
+                     "Printer driver %s is missing.", command);
+    }
+
     return (0);
   }
-  else if (commandinfo.st_mode & S_IWOTH)
+  else if (!RunUser &&
+           ((commandinfo.st_mode & (S_ISUID | S_IWGRP | S_IWOTH)) ||
+            commandinfo.st_uid))
   {
+    *pid = 0;
+
+    cupsdLogMessage(CUPSD_LOG_DEBUG2,
+                   "cupsdStartProcess(command=\"%s\", argv=%p, envp=%p, "
+                   "infd=%d, outfd=%d, errfd=%d, backfd=%d, sidefd=%d, root=%d, "
+                   "profile=%p, job=%p(%d), pid=%p) = %d",
+                   command, argv, envp, infd, outfd, errfd, backfd, sidefd,
+                   root, profile, job, job ? job->id : 0, pid, *pid);
     cupsdLogMessage(CUPSD_LOG_ERROR,
                     "Unable to execute %s: insecure file permissions (0%o)",
                    command, commandinfo.st_mode);
-    *pid  = 0;
+
+    if (job && job->printer)
+    {
+      if (cupsdSetPrinterReasons(job->printer, "+cups-insecure-filter-warning"))
+       cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, job->printer, NULL,
+                     "Printer driver %s has insecure file permissions (0%o).",
+                     command, commandinfo.st_mode);
+    }
+
     errno = EPERM;
+
     return (0);
   }
   else if ((commandinfo.st_uid != user || !(commandinfo.st_mode & S_IXUSR)) &&
            (commandinfo.st_gid != Group || !(commandinfo.st_mode & S_IXGRP)) &&
            !(commandinfo.st_mode & S_IXOTH))
   {
+    *pid = 0;
+
+    cupsdLogMessage(CUPSD_LOG_DEBUG2,
+                   "cupsdStartProcess(command=\"%s\", argv=%p, envp=%p, "
+                   "infd=%d, outfd=%d, errfd=%d, backfd=%d, sidefd=%d, root=%d, "
+                   "profile=%p, job=%p(%d), pid=%p) = %d",
+                   command, argv, envp, infd, outfd, errfd, backfd, sidefd,
+                   root, profile, job, job ? job->id : 0, pid, *pid);
     cupsdLogMessage(CUPSD_LOG_ERROR,
                     "Unable to execute %s: no execute permissions (0%o)",
                    command, commandinfo.st_mode);
-    *pid  = 0;
+
     errno = EPERM;
     return (0);
   }
@@ -315,41 +419,51 @@ cupsdStartProcess(
 
     if (infd != 0)
     {
-      close(0);
-      if (infd > 0)
-        dup(infd);
-      else
-        open("/dev/null", O_RDONLY);
+      if (infd < 0)
+        infd = open("/dev/null", O_RDONLY);
+
+      if (infd != 0)
+      {
+        dup2(infd, 0);
+       close(infd);
+      }
     }
+
     if (outfd != 1)
     {
-      close(1);
-      if (outfd > 0)
-       dup(outfd);
-      else
-        open("/dev/null", O_WRONLY);
+      if (outfd < 0)
+        outfd = open("/dev/null", O_WRONLY);
+
+      if (outfd != 1)
+      {
+        dup2(outfd, 1);
+       close(outfd);
+      }
     }
+
     if (errfd != 2)
     {
-      close(2);
-      if (errfd > 0)
-        dup(errfd);
-      else
-        open("/dev/null", O_WRONLY);
+      if (errfd < 0)
+        errfd = open("/dev/null", O_WRONLY);
+
+      if (errfd != 2)
+      {
+        dup2(errfd, 2);
+       close(errfd);
+      }
     }
-    if (backfd != 3)
+
+    if (backfd != 3 && backfd >= 0)
     {
-      close(3);
-      if (backfd > 0)
-       dup(backfd);
-      else
-        open("/dev/null", O_RDWR);
+      dup2(backfd, 3);
+      close(backfd);
       fcntl(3, F_SETFL, O_NDELAY);
     }
-    if (sidefd != 4 && sidefd > 0)
+
+    if (sidefd != 4 && sidefd >= 0)
     {
-      close(4);
-      dup(sidefd);
+      dup2(sidefd, 4);
+      close(sidefd);
       fcntl(4, F_SETFL, O_NDELAY);
     }
 
@@ -404,8 +518,11 @@ cupsdStartProcess(
       * Reset group membership to just the main one we belong to.
       */
 
-      setgid(Group);
-      setgroups(1, &Group);
+      if (setgid(Group) && !RunUser)
+        exit(errno);
+
+      if (setgroups(1, &Group) && !RunUser)
+        exit(errno);
     }
 
    /*
@@ -421,6 +538,7 @@ cupsdStartProcess(
 #ifdef HAVE_SIGSET
     sigset(SIGTERM, SIG_DFL);
     sigset(SIGCHLD, SIG_DFL);
+    sigset(SIGPIPE, SIG_DFL);
 #elif defined(HAVE_SIGACTION)
     memset(&action, 0, sizeof(action));
 
@@ -429,9 +547,11 @@ cupsdStartProcess(
 
     sigaction(SIGTERM, &action, NULL);
     sigaction(SIGCHLD, &action, NULL);
+    sigaction(SIGPIPE, &action, NULL);
 #else
     signal(SIGTERM, SIG_DFL);
     signal(SIGCHLD, SIG_DFL);
+    signal(SIGPIPE, SIG_DFL);
 #endif /* HAVE_SIGSET */
 
     cupsdReleaseSignals();
@@ -470,7 +590,8 @@ cupsdStartProcess(
     {
       if ((proc = calloc(1, sizeof(cupsd_proc_t) + strlen(command))) != NULL)
       {
-        proc->pid = *pid;
+        proc->pid    = *pid;
+       proc->job_id = job ? job->id : 0;
        strcpy(proc->name, command);
 
        cupsArrayAdd(process_array, proc);
@@ -480,6 +601,13 @@ cupsdStartProcess(
 
   cupsdReleaseSignals();
 
+  cupsdLogMessage(CUPSD_LOG_DEBUG2,
+                 "cupsdStartProcess(command=\"%s\", argv=%p, envp=%p, "
+                 "infd=%d, outfd=%d, errfd=%d, backfd=%d, sidefd=%d, root=%d, "
+                 "profile=%p, job=%p(%d), pid=%p) = %d",
+                 command, argv, envp, infd, outfd, errfd, backfd, sidefd,
+                 root, profile, job, job ? job->id : 0, pid, *pid);
+
   return (*pid);
 }