2 * "$Id: process.c 7256 2008-01-25 00:48:54Z mike $"
4 * Process management routines for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2009 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
17 * cupsdCreateProfile() - Create an execution profile for a subprocess.
18 * cupsdDestroyProfile() - Delete an execution profile.
19 * cupsdEndProcess() - End a process.
20 * cupsdFinishProcess() - Finish a process and get its name.
21 * cupsdStartProcess() - Start a process.
22 * compare_procs() - Compare two processes.
23 * cupsd_requote() - Make a regular-expression version of a string.
27 * Include necessary headers...
34 #endif /* __APPLE__ */
36 # define __APPLE_API_PRIVATE
38 #endif /* HAVE_SANDBOX_H */
42 * Process structure...
47 int pid
, /* Process ID */
48 job_id
; /* Job associated with process */
49 char name
[1]; /* Name of process */
57 static cups_array_t
*process_array
= NULL
;
64 static int compare_procs(cupsd_proc_t
*a
, cupsd_proc_t
*b
);
66 static char *cupsd_requote(char *dst
, const char *src
, size_t dstsize
);
67 #endif /* HAVE_SANDBOX_H */
71 * 'cupsdCreateProfile()' - Create an execution profile for a subprocess.
74 void * /* O - Profile or NULL on error */
75 cupsdCreateProfile(int job_id
) /* I - Job ID or 0 for none */
78 cups_file_t
*fp
; /* File pointer */
79 char profile
[1024], /* File containing the profile */
80 cache
[1024], /* Quoted CacheDir */
81 request
[1024], /* Quoted RequestRoot */
82 root
[1024], /* Quoted ServerRoot */
83 temp
[1024]; /* Quoted TempDir */
86 if (!UseProfiles
|| RunUser
)
89 * Only use sandbox profiles as root...
92 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdCreateProfile(job_id=%d) = NULL",
98 if ((fp
= cupsTempFile2(profile
, sizeof(profile
))) == NULL
)
100 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdCreateProfile(job_id=%d) = NULL",
102 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create security profile: %s",
107 cupsd_requote(cache
, CacheDir
, sizeof(cache
));
108 cupsd_requote(request
, RequestRoot
, sizeof(request
));
109 cupsd_requote(root
, ServerRoot
, sizeof(root
));
110 cupsd_requote(temp
, TempDir
, sizeof(temp
));
112 cupsFilePuts(fp
, "(version 1)\n");
113 cupsFilePuts(fp
, "(debug deny)\n");
114 cupsFilePuts(fp
, "(allow default)\n");
116 "(deny file-write* file-read-data file-read-metadata\n"
117 " (regex #\"^%s/\"))\n", request
);
119 "(deny file-write*\n"
120 " (regex #\"^%s\" #\"^/private/etc\" #\"^/usr/local/etc\" "
121 "#\"^/Library\" #\"^/System\" #\"^/Users\"))\n", root
);
123 "(allow file-write* file-read-data file-read-metadata\n"
124 " (regex #\"^%s$\" #\"^%s/\" #\"^%s$\" #\"^%s/\""
125 " #\"^/Library/Application Support/\""
126 " #\"^/Library/Caches/\""
127 " #\"^/Library/Preferences/\""
128 " #\"^/Library/Printers/\""
130 temp
, temp
, cache
, cache
);
132 "(deny file-write*\n"
133 " (regex #\"^/Library/Printers/PPDs/\""
134 " #\"^/Library/Printers/PPD Plugins/\""
138 "(allow file-read-data file-read-metadata\n"
139 " (regex #\"^%s/([ac]%05d|d%05d-[0-9][0-9][0-9])$\"))\n",
140 request
, job_id
, job_id
);
144 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdCreateProfile(job_id=%d) = \"%s\"",
146 return ((void *)strdup(profile
));
149 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdCreateProfile(job_id=%d) = NULL",
153 #endif /* HAVE_SANDBOX_H */
158 * 'cupsdDestroyProfile()' - Delete an execution profile.
162 cupsdDestroyProfile(void *profile
) /* I - Profile */
164 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdDeleteProfile(profile=\"%s\")",
165 profile
? (char *)profile
: "(null)");
167 #ifdef HAVE_SANDBOX_H
170 unlink((char *)profile
);
173 #endif /* HAVE_SANDBOX_H */
178 * 'cupsdEndProcess()' - End a process.
181 int /* O - 0 on success, -1 on failure */
182 cupsdEndProcess(int pid
, /* I - Process ID */
183 int force
) /* I - Force child to die */
185 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdEndProcess(pid=%d, force=%d)", pid
,
189 return (kill(pid
, SIGKILL
));
191 return (kill(pid
, SIGTERM
));
196 * 'cupsdFinishProcess()' - Finish a process and get its name.
199 const char * /* O - Process name */
200 cupsdFinishProcess(int pid
, /* I - Process ID */
201 char *name
, /* I - Name buffer */
202 int namelen
, /* I - Size of name buffer */
203 int *job_id
) /* O - Job ID pointer or NULL */
205 cupsd_proc_t key
, /* Search key */
206 *proc
; /* Matching process */
211 if ((proc
= (cupsd_proc_t
*)cupsArrayFind(process_array
, &key
)) != NULL
)
214 *job_id
= proc
->job_id
;
216 strlcpy(name
, proc
->name
, namelen
);
217 cupsArrayRemove(process_array
, proc
);
225 strlcpy(name
, "unknown", namelen
);
228 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
229 "cupsdFinishProcess(pid=%d, name=%p, namelen=%d, "
230 "job_id=%p(%d)) = \"%s\"", pid
, name
, namelen
, job_id
,
231 job_id
? *job_id
: 0, name
);
238 * 'cupsdStartProcess()' - Start a process.
241 int /* O - Process ID or 0 */
243 const char *command
, /* I - Full path to command */
244 char *argv
[], /* I - Command-line arguments */
245 char *envp
[], /* I - Environment */
246 int infd
, /* I - Standard input file descriptor */
247 int outfd
, /* I - Standard output file descriptor */
248 int errfd
, /* I - Standard error file descriptor */
249 int backfd
, /* I - Backchannel file descriptor */
250 int sidefd
, /* I - Sidechannel file descriptor */
251 int root
, /* I - Run as root? */
252 void *profile
, /* I - Security profile to use */
253 cupsd_job_t
*job
, /* I - Job associated with process */
254 int *pid
) /* O - Process ID */
256 int user
; /* Command UID */
257 struct stat commandinfo
; /* Command file information */
258 cupsd_proc_t
*proc
; /* New process record */
259 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
260 struct sigaction action
; /* POSIX signal handler */
261 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
262 #if defined(__APPLE__)
263 char processPath
[1024], /* CFProcessPath environment variable */
264 linkpath
[1024]; /* Link path for symlinks... */
265 int linkbytes
; /* Bytes for link path */
266 #endif /* __APPLE__ */
276 if (stat(command
, &commandinfo
))
280 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
281 "cupsdStartProcess(command=\"%s\", argv=%p, envp=%p, "
282 "infd=%d, outfd=%d, errfd=%d, backfd=%d, sidefd=%d, root=%d, "
283 "profile=%p, job=%p(%d), pid=%p) = %d",
284 command
, argv
, envp
, infd
, outfd
, errfd
, backfd
, sidefd
,
285 root
, profile
, job
, job
? job
->id
: 0, pid
, *pid
);
286 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to execute %s: %s", command
,
289 if (job
&& job
->printer
)
291 if (cupsdSetPrinterReasons(job
->printer
, "+cups-missing-filter-warning"))
292 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, job
->printer
, NULL
,
293 "Printer driver %s is missing.", command
);
298 else if ((commandinfo
.st_mode
& (S_ISUID
| S_IWGRP
| S_IWOTH
)) ||
299 (!RunUser
&& commandinfo
.st_uid
))
303 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
304 "cupsdStartProcess(command=\"%s\", argv=%p, envp=%p, "
305 "infd=%d, outfd=%d, errfd=%d, backfd=%d, sidefd=%d, root=%d, "
306 "profile=%p, job=%p(%d), pid=%p) = %d",
307 command
, argv
, envp
, infd
, outfd
, errfd
, backfd
, sidefd
,
308 root
, profile
, job
, job
? job
->id
: 0, pid
, *pid
);
309 cupsdLogMessage(CUPSD_LOG_ERROR
,
310 "Unable to execute %s: insecure file permissions (0%o)",
311 command
, commandinfo
.st_mode
);
313 if (job
&& job
->printer
)
315 if (cupsdSetPrinterReasons(job
->printer
, "+cups-insecure-filter-warning"))
316 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, job
->printer
, NULL
,
317 "Printer driver %s has insecure file permissions (0%o).",
318 command
, commandinfo
.st_mode
);
325 else if ((commandinfo
.st_uid
!= user
|| !(commandinfo
.st_mode
& S_IXUSR
)) &&
326 (commandinfo
.st_gid
!= Group
|| !(commandinfo
.st_mode
& S_IXGRP
)) &&
327 !(commandinfo
.st_mode
& S_IXOTH
))
331 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
332 "cupsdStartProcess(command=\"%s\", argv=%p, envp=%p, "
333 "infd=%d, outfd=%d, errfd=%d, backfd=%d, sidefd=%d, root=%d, "
334 "profile=%p, job=%p(%d), pid=%p) = %d",
335 command
, argv
, envp
, infd
, outfd
, errfd
, backfd
, sidefd
,
336 root
, profile
, job
, job
? job
->id
: 0, pid
, *pid
);
337 cupsdLogMessage(CUPSD_LOG_ERROR
,
338 "Unable to execute %s: no execute permissions (0%o)",
339 command
, commandinfo
.st_mode
);
345 #if defined(__APPLE__)
349 * Add special voodoo magic for MacOS X - this allows MacOS X
350 * programs to access their bundle resources properly...
353 if ((linkbytes
= readlink(command
, linkpath
, sizeof(linkpath
) - 1)) > 0)
356 * Yes, this is a symlink to the actual program, nul-terminate and
360 linkpath
[linkbytes
] = '\0';
362 if (linkpath
[0] == '/')
363 snprintf(processPath
, sizeof(processPath
), "CFProcessPath=%s",
366 snprintf(processPath
, sizeof(processPath
), "CFProcessPath=%s/%s",
367 dirname((char *)command
), linkpath
);
370 snprintf(processPath
, sizeof(processPath
), "CFProcessPath=%s", command
);
372 envp
[0] = processPath
; /* Replace <CFProcessPath> string */
374 #endif /* __APPLE__ */
377 * Block signals before forking...
382 if ((*pid
= fork()) == 0)
385 * Child process goes here...
387 * Update stdin/stdout/stderr as needed...
393 infd
= open("/dev/null", O_RDONLY
);
405 outfd
= open("/dev/null", O_WRONLY
);
417 errfd
= open("/dev/null", O_WRONLY
);
426 if (backfd
!= 3 && backfd
>= 0)
430 fcntl(3, F_SETFL
, O_NDELAY
);
433 if (sidefd
!= 4 && sidefd
>= 0)
437 fcntl(4, F_SETFL
, O_NDELAY
);
441 * Change the priority of the process based on the FilterNice setting.
442 * (this is not done for root processes...)
448 #ifdef HAVE_SANDBOX_H
450 * Run in a separate security profile...
455 char *error
= NULL
; /* Sandbox error, if any */
457 if (sandbox_init((char *)profile
, SANDBOX_NAMED_EXTERNAL
, &error
))
459 fprintf(stderr
, "ERROR: sandbox_init failed: %s (%s)\n", error
,
461 sandbox_free_error(error
);
464 #endif /* HAVE_SANDBOX_H */
467 * Change user to something "safe"...
470 if (!root
&& !RunUser
)
473 * Running as root, so change to non-priviledged user...
479 if (setgroups(1, &Group
))
488 * Reset group membership to just the main one we belong to.
492 setgroups(1, &Group
);
496 * Change umask to restrict permissions on created files...
502 * Unblock signals before doing the exec...
506 sigset(SIGTERM
, SIG_DFL
);
507 sigset(SIGCHLD
, SIG_DFL
);
508 #elif defined(HAVE_SIGACTION)
509 memset(&action
, 0, sizeof(action
));
511 sigemptyset(&action
.sa_mask
);
512 action
.sa_handler
= SIG_DFL
;
514 sigaction(SIGTERM
, &action
, NULL
);
515 sigaction(SIGCHLD
, &action
, NULL
);
517 signal(SIGTERM
, SIG_DFL
);
518 signal(SIGCHLD
, SIG_DFL
);
519 #endif /* HAVE_SIGSET */
521 cupsdReleaseSignals();
524 * Execute the command; if for some reason this doesn't work,
525 * return the error code...
529 execve(command
, argv
, envp
);
531 execv(command
, argv
);
540 * Error - couldn't fork a new process!
543 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to fork %s - %s.", command
,
551 process_array
= cupsArrayNew((cups_array_func_t
)compare_procs
, NULL
);
555 if ((proc
= calloc(1, sizeof(cupsd_proc_t
) + strlen(command
))) != NULL
)
558 proc
->job_id
= job
? job
->id
: 0;
559 strcpy(proc
->name
, command
);
561 cupsArrayAdd(process_array
, proc
);
566 cupsdReleaseSignals();
568 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
569 "cupsdStartProcess(command=\"%s\", argv=%p, envp=%p, "
570 "infd=%d, outfd=%d, errfd=%d, backfd=%d, sidefd=%d, root=%d, "
571 "profile=%p, job=%p(%d), pid=%p) = %d",
572 command
, argv
, envp
, infd
, outfd
, errfd
, backfd
, sidefd
,
573 root
, profile
, job
, job
? job
->id
: 0, pid
, *pid
);
580 * 'compare_procs()' - Compare two processes.
583 static int /* O - Result of comparison */
584 compare_procs(cupsd_proc_t
*a
, /* I - First process */
585 cupsd_proc_t
*b
) /* I - Second process */
587 return (a
->pid
- b
->pid
);
591 #ifdef HAVE_SANDBOX_H
593 * 'cupsd_requote()' - Make a regular-expression version of a string.
596 static char * /* O - Quoted string */
597 cupsd_requote(char *dst
, /* I - Destination buffer */
598 const char *src
, /* I - Source string */
599 size_t dstsize
) /* I - Size of destination buffer */
601 int ch
; /* Current character */
602 char *dstptr
, /* Current position in buffer */
603 *dstend
; /* End of destination buffer */
607 dstend
= dst
+ dstsize
- 2;
609 while (*src
&& dstptr
< dstend
)
613 if (strchr(".?*()[]^$\\", ch
))
623 #endif /* HAVE_SANDBOX_H */
627 * End of "$Id: process.c 7256 2008-01-25 00:48:54Z mike $".