]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - scheduler/process.c
Merge changes from CUPS 1.5svn-r9717.
[thirdparty/cups.git] / scheduler / process.c
index b0481296c78962e9fe7c8fae6b4404015d44b686..8b3e3f23b07e97266a37e89d15ab89b357cdff79 100644 (file)
@@ -1,9 +1,9 @@
 /*
  * "$Id: process.c 7256 2008-01-25 00:48:54Z mike $"
  *
- *   Process management routines for the Common UNIX Printing System (CUPS).
+ *   Process management routines for the CUPS scheduler.
  *
- *   Copyright 2007 by Apple Inc.
+ *   Copyright 2007-2011 by Apple Inc.
  *   Copyright 1997-2007 by Easy Software Products, all rights reserved.
  *
  *   These coded instructions, statements, and computer programs are the
 #include <grp.h>
 #ifdef __APPLE__
 #  include <libgen.h>
-#endif /* __APPLE__ */ 
-#ifdef HAVE_SANDBOX_H
-#  define __APPLE_API_PRIVATE
-#  include <sandbox.h>
-#endif /* HAVE_SANDBOX_H */
+#endif /* __APPLE__ */
 
 
 /*
@@ -44,7 +40,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,45 +79,132 @@ cupsdCreateProfile(int job_id)             /* I - Job ID or 0 for none */
                temp[1024];             /* Quoted TempDir */
 
 
+  if (!UseProfiles)
+  {
+   /*
+    * 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);
   }
 
+  fchown(cupsFileNumber(fp), RunUser, Group);
+  fchmod(cupsFileNumber(fp), 0640);
+
   cupsd_requote(cache, CacheDir, sizeof(cache));
   cupsd_requote(request, RequestRoot, sizeof(request));
   cupsd_requote(root, ServerRoot, sizeof(root));
   cupsd_requote(temp, TempDir, sizeof(temp));
 
   cupsFilePuts(fp, "(version 1)\n");
-  cupsFilePuts(fp, "(debug deny)\n");
+  if (LogLevel >= CUPSD_LOG_DEBUG)
+    cupsFilePuts(fp, "(debug deny)\n");
   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/... */
+                "))\n",
+                request, request);
+  if (!RunUser)
+    cupsFilePuts(fp,
+                "(deny file-write* file-read-data file-read-metadata\n"
+                "  (regex"
+                " #\"^/Users$\""
+                " #\"^/Users/\""
+                "))\n");
   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);
+  /* Specifically allow applications to stat RequestRoot */
+  cupsFilePrintf(fp,
+                 "(allow file-read-metadata\n"
+                 "  (regex"
+                " #\"^%s$\""           /* RequestRoot */
+                "))\n",
+                request);
   cupsFilePrintf(fp,
                  "(allow file-write* file-read-data file-read-metadata\n"
-                 "  (regex #\"^%s$\" #\"^%s/\" #\"^%s$\" #\"^%s/\" "
-                "#\"^/Library/Caches/\"))\n",
-                temp, temp, cache, cache);
+                 "  (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/.*/\""
+                " #\"^/Users/Shared/\""
+                "))\n",
+                temp, temp, cache, cache, request, request);
+  cupsFilePuts(fp,
+              "(deny file-write*\n"
+              "  (regex"
+              " #\"^/Library/Printers/PPDs$\""
+              " #\"^/Library/Printers/PPDs/\""
+              " #\"^/Library/Printers/PPD Plugins$\""
+              " #\"^/Library/Printers/PPD Plugins/\""
+              "))\n");
   if (job_id)
+  {
+   /*
+    * Allow job filters to read the spool file(s)...
+    */
+
     cupsFilePrintf(fp,
                    "(allow file-read-data file-read-metadata\n"
                    "  (regex #\"^%s/([ac]%05d|d%05d-[0-9][0-9][0-9])$\"))\n",
                   request, job_id, job_id);
+  }
+  else
+  {
+   /*
+    * Allow email notifications from notifiers...
+    */
+
+    cupsFilePuts(fp,
+                "(allow process-exec\n"
+                "  (literal \"/usr/sbin/sendmail\")\n"
+                "  (with no-sandbox)\n"
+                ")\n");
+  }
 
   cupsFileClose(fp);
 
   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 */
@@ -134,11 +218,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);
   }
@@ -154,7 +239,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));
@@ -168,7 +258,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 */
@@ -178,14 +269,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);
 }
 
 
@@ -195,18 +299,24 @@ 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          i;                      /* Looping var */
+  const char   *exec_path = command;   /* Command to be exec'd */
+  char         *real_argv[103],        /* Real command-line arguments */
+               cups_exec[1024];        /* Path to "cups-exec" program */
+  int          user;                   /* Command UID */
   cupsd_proc_t *proc;                  /* New process record */
 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
   struct sigaction action;             /* POSIX signal handler */
@@ -218,23 +328,32 @@ cupsdStartProcess(
 #endif /* __APPLE__ */
 
 
-  cupsdLogMessage(CUPSD_LOG_DEBUG2,
-                  "cupsdStartProcess(\"%s\", %p, %p, %d, %d, %d)",
-                  command, argv, envp, infd, outfd, errfd);
+  *pid = 0;
 
-  if (access(command, X_OK))
-  {
-    cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to execute %s: %s", command,
-                    strerror(errno));
-    *pid = 0;
+ /*
+  * Figure out the UID for the child process...
+  */
+
+  if (RunUser)
+    user = RunUser;
+  else if (root)
+    user = 0;
+  else
+    user = User;
+
+ /*
+  * Check the permissions of the command we are running...
+  */
+
+  if (_cupsFileCheck(command, _CUPS_FILE_CHECK_PROGRAM, !RunUser,
+                     cupsdLogFCMessage, job ? job->printer : NULL))
     return (0);
-  }
 
 #if defined(__APPLE__)
   if (envp)
   {
    /*
-    * Add special voodoo magic for MacOS X - this allows MacOS X 
+    * Add special voodoo magic for Mac OS X - this allows Mac OS X
     * programs to access their bundle resources properly...
     */
 
@@ -261,6 +380,29 @@ cupsdStartProcess(
   }
 #endif /* __APPLE__ */
 
+ /*
+  * Use helper program when we have a sandbox profile...
+  */
+
+  if (profile)
+  {
+    snprintf(cups_exec, sizeof(cups_exec), "%s/daemon/cups-exec", ServerBin);
+
+    real_argv[0] = cups_exec;
+    real_argv[1] = profile;
+    real_argv[2] = (char *)command;
+
+    for (i = 0;
+         i < (int)(sizeof(real_argv) / sizeof(real_argv[0]) - 4) && argv[i];
+        i ++)
+      real_argv[i + 3] = argv[i];
+
+    real_argv[i + 3] = NULL;
+
+    argv      = real_argv;
+    exec_path = cups_exec;
+  }
+
  /*
   * Block signals before forking...
   */
@@ -277,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);
     }
 
@@ -323,24 +475,6 @@ cupsdStartProcess(
     if (!root)
       nice(FilterNice);
 
-#ifdef HAVE_SANDBOX_H
-   /*
-    * Run in a separate security profile...
-    */
-
-    if (profile)
-    {
-      char *error = NULL;              /* Sandbox error, if any */
-
-      if (sandbox_init((char *)profile, SANDBOX_NAMED_EXTERNAL, &error))
-      {
-        fprintf(stderr, "ERROR: sandbox_init failed: %s (%s)\n", error,
-               strerror(errno));
-       sandbox_free_error(error);
-      }
-    }
-#endif /* HAVE_SANDBOX_H */
-
    /*
     * Change user to something "safe"...
     */
@@ -366,8 +500,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);
     }
 
    /*
@@ -383,6 +520,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));
 
@@ -391,26 +529,28 @@ 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();
 
    /*
-    * Execute the command; if for some reason this doesn't work,
-    * return the error code...
+    * Execute the command; if for some reason this doesn't work, log an error
+    * exit with a non-zero value...
     */
 
     if (envp)
-      execve(command, argv, envp);
+      execve(exec_path, argv, envp);
     else
-      execv(command, argv);
+      execv(exec_path, argv);
 
     perror(command);
 
-    exit(errno);
+    exit(1);
   }
   else if (*pid < 0)
   {
@@ -432,8 +572,9 @@ cupsdStartProcess(
     {
       if ((proc = calloc(1, sizeof(cupsd_proc_t) + strlen(command))) != NULL)
       {
-        proc->pid = *pid;
-       strcpy(proc->name, command);
+        proc->pid    = *pid;
+       proc->job_id = job ? job->id : 0;
+       _cups_strcpy(proc->name, command);
 
        cupsArrayAdd(process_array, proc);
       }
@@ -442,6 +583,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);
 }