#include "cupsd.h"
#include <grp.h>
#ifdef __APPLE__
# include <libgen.h>
#endif
#ifdef HAVE_SANDBOX_H
# define __APPLE_API_PRIVATE
# include <sandbox.h>
#endif
typedef struct
{
int pid,
job_id;
char name[1];
} cupsd_proc_t;
static cups_array_t *process_array = NULL;
static int compare_procs(cupsd_proc_t *a, cupsd_proc_t *b);
#ifdef HAVE_SANDBOX_H
static char *cupsd_requote(char *dst, const char *src, size_t dstsize);
#endif
void *
cupsdCreateProfile(int job_id)
{
#ifdef HAVE_SANDBOX_H
cups_file_t *fp;
char profile[1024],
cache[1024],
request[1024],
root[1024],
temp[1024];
if (!UseProfiles || RunUser)
{
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);
}
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");
cupsFilePuts(fp, "(allow default)\n");
cupsFilePrintf(fp,
"(deny file-write* file-read-data file-read-metadata\n"
" (regex #\"^%s/\"))\n", request);
cupsFilePrintf(fp,
"(deny file-write*\n"
" (regex #\"^%s\" #\"^/private/etc\" #\"^/usr/local/etc\" "
"#\"^/Library\" #\"^/System\" #\"^/Users\"))\n", root);
cupsFilePrintf(fp,
"(allow file-write* file-read-data file-read-metadata\n"
" (regex #\"^%s$\" #\"^%s/\" #\"^%s$\" #\"^%s/\""
" #\"^/Library/Application Support/\""
" #\"^/Library/Caches/\""
" #\"^/Library/Preferences/\""
" #\"^/Library/Printers/\""
"))\n",
temp, temp, cache, cache);
cupsFilePuts(fp,
"(deny file-write*\n"
" (regex #\"^/Library/Printers/PPDs/\""
" #\"^/Library/Printers/PPD Plugins/\""
"))\n");
if (job_id)
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);
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
}
void
cupsdDestroyProfile(void *profile)
{
cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdDeleteProfile(profile=\"%s\")",
profile ? (char *)profile : "(null)");
#ifdef HAVE_SANDBOX_H
if (profile)
{
unlink((char *)profile);
free(profile);
}
#endif
}
int
cupsdEndProcess(int pid,
int force)
{
cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdEndProcess(pid=%d, force=%d)", pid,
force);
if (force)
return (kill(pid, SIGKILL));
else
return (kill(pid, SIGTERM));
}
const char *
cupsdFinishProcess(int pid,
char *name,
int namelen,
int *job_id)
{
cupsd_proc_t key,
*proc;
key.pid = pid;
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);
}
else
{
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);
}
int
cupsdStartProcess(
const char *command,
char *argv[],
char *envp[],
int infd,
int outfd,
int errfd,
int backfd,
int sidefd,
int root,
void *profile,
cupsd_job_t *job,
int *pid)
{
int user;
struct stat commandinfo;
cupsd_proc_t *proc;
#if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
struct sigaction action;
#endif
#if defined(__APPLE__)
char processPath[1024],
linkpath[1024];
int linkbytes;
#endif
if (RunUser)
user = RunUser;
else if (root)
user = 0;
else
user = User;
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));
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_ISUID | S_IWOTH)) ||
(!RunUser && 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);
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);
errno = EPERM;
return (0);
}
#if defined(__APPLE__)
if (envp)
{
if ((linkbytes = readlink(command, linkpath, sizeof(linkpath) - 1)) > 0)
{
linkpath[linkbytes] = '\0';
if (linkpath[0] == '/')
snprintf(processPath, sizeof(processPath), "CFProcessPath=%s",
linkpath);
else
snprintf(processPath, sizeof(processPath), "CFProcessPath=%s/%s",
dirname((char *)command), linkpath);
}
else
snprintf(processPath, sizeof(processPath), "CFProcessPath=%s", command);
envp[0] = processPath;
}
#endif
cupsdHoldSignals();
if ((*pid = fork()) == 0)
{
if (infd != 0)
{
if (infd < 0)
infd = open("/dev/null", O_RDONLY);
if (infd != 0)
{
dup2(infd, 0);
close(infd);
}
}
if (outfd != 1)
{
if (outfd < 0)
outfd = open("/dev/null", O_WRONLY);
if (outfd != 1)
{
dup2(outfd, 1);
close(outfd);
}
}
if (errfd != 2)
{
if (errfd < 0)
errfd = open("/dev/null", O_WRONLY);
if (errfd != 2)
{
dup2(errfd, 2);
close(errfd);
}
}
if (backfd != 3 && backfd >= 0)
{
dup2(backfd, 3);
close(backfd);
fcntl(3, F_SETFL, O_NDELAY);
}
if (sidefd != 4 && sidefd >= 0)
{
dup2(sidefd, 4);
close(sidefd);
fcntl(4, F_SETFL, O_NDELAY);
}
if (!root)
nice(FilterNice);
#ifdef HAVE_SANDBOX_H
if (profile)
{
char *error = NULL;
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
if (!root && !RunUser)
{
if (setgid(Group))
exit(errno);
if (setgroups(1, &Group))
exit(errno);
if (setuid(User))
exit(errno);
}
else
{
setgid(Group);
setgroups(1, &Group);
}
umask(077);
#ifdef HAVE_SIGSET
sigset(SIGTERM, SIG_DFL);
sigset(SIGCHLD, SIG_DFL);
#elif defined(HAVE_SIGACTION)
memset(&action, 0, sizeof(action));
sigemptyset(&action.sa_mask);
action.sa_handler = SIG_DFL;
sigaction(SIGTERM, &action, NULL);
sigaction(SIGCHLD, &action, NULL);
#else
signal(SIGTERM, SIG_DFL);
signal(SIGCHLD, SIG_DFL);
#endif
cupsdReleaseSignals();
if (envp)
execve(command, argv, envp);
else
execv(command, argv);
perror(command);
exit(errno);
}
else if (*pid < 0)
{
cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to fork %s - %s.", command,
strerror(errno));
*pid = 0;
}
else
{
if (!process_array)
process_array = cupsArrayNew((cups_array_func_t)compare_procs, NULL);
if (process_array)
{
if ((proc = calloc(1, sizeof(cupsd_proc_t) + strlen(command))) != NULL)
{
proc->pid = *pid;
proc->job_id = job ? job->id : 0;
strcpy(proc->name, command);
cupsArrayAdd(process_array, proc);
}
}
}
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);
}
static int
compare_procs(cupsd_proc_t *a,
cupsd_proc_t *b)
{
return (a->pid - b->pid);
}
#ifdef HAVE_SANDBOX_H
static char *
cupsd_requote(char *dst,
const char *src,
size_t dstsize)
{
int ch;
char *dstptr,
*dstend;
dstptr = dst;
dstend = dst + dstsize - 2;
while (*src && dstptr < dstend)
{
ch = *src++;
if (strchr(".?*()[]^$\\", ch))
*dstptr++ = '\\';
*dstptr++ = ch;
}
*dstptr = '\0';
return (dst);
}
#endif