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-2010 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"
118 " #\"^%s$\"" /* RequestRoot */
119 " #\"^%s/\"" /* RequestRoot/... */
125 "(deny file-write*\n"
127 " #\"^%s$\"" /* ServerRoot */
128 " #\"^%s/\"" /* ServerRoot/... */
129 " #\"^/private/etc$\""
130 " #\"^/private/etc/\""
131 " #\"^/usr/local/etc$\""
132 " #\"^/usr/local/etc/\""
140 "(allow file-write* file-read-data file-read-metadata\n"
142 " #\"^%s$\"" /* TempDir */
143 " #\"^%s/\"" /* TempDir/... */
144 " #\"^%s$\"" /* CacheDir */
145 " #\"^%s/\"" /* CacheDir/... */
146 " #\"^%s/Library$\"" /* RequestRoot/Library */
147 " #\"^%s/Library/\"" /* RequestRoot/Library/... */
148 " #\"^/Library/Application Support/\""
149 " #\"^/Library/Caches/\""
150 " #\"^/Library/Preferences/\""
151 " #\"^/Library/Printers/.*/\""
152 " #\"^/Users/Shared/\""
154 temp
, temp
, cache
, cache
, request
, request
);
156 "(deny file-write*\n"
158 " #\"^/Library/Printers/PPDs$\""
159 " #\"^/Library/Printers/PPDs/\""
160 " #\"^/Library/Printers/PPD Plugins$\""
161 " #\"^/Library/Printers/PPD Plugins/\""
165 "(allow file-read-data file-read-metadata\n"
166 " (regex #\"^%s/([ac]%05d|d%05d-[0-9][0-9][0-9])$\"))\n",
167 request
, job_id
, job_id
);
171 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdCreateProfile(job_id=%d) = \"%s\"",
173 return ((void *)strdup(profile
));
176 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdCreateProfile(job_id=%d) = NULL",
180 #endif /* HAVE_SANDBOX_H */
185 * 'cupsdDestroyProfile()' - Delete an execution profile.
189 cupsdDestroyProfile(void *profile
) /* I - Profile */
191 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdDeleteProfile(profile=\"%s\")",
192 profile
? (char *)profile
: "(null)");
194 #ifdef HAVE_SANDBOX_H
197 unlink((char *)profile
);
200 #endif /* HAVE_SANDBOX_H */
205 * 'cupsdEndProcess()' - End a process.
208 int /* O - 0 on success, -1 on failure */
209 cupsdEndProcess(int pid
, /* I - Process ID */
210 int force
) /* I - Force child to die */
212 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdEndProcess(pid=%d, force=%d)", pid
,
218 return (kill(pid
, SIGKILL
));
220 return (kill(pid
, SIGTERM
));
225 * 'cupsdFinishProcess()' - Finish a process and get its name.
228 const char * /* O - Process name */
229 cupsdFinishProcess(int pid
, /* I - Process ID */
230 char *name
, /* I - Name buffer */
231 int namelen
, /* I - Size of name buffer */
232 int *job_id
) /* O - Job ID pointer or NULL */
234 cupsd_proc_t key
, /* Search key */
235 *proc
; /* Matching process */
240 if ((proc
= (cupsd_proc_t
*)cupsArrayFind(process_array
, &key
)) != NULL
)
243 *job_id
= proc
->job_id
;
245 strlcpy(name
, proc
->name
, namelen
);
246 cupsArrayRemove(process_array
, proc
);
254 strlcpy(name
, "unknown", namelen
);
257 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
258 "cupsdFinishProcess(pid=%d, name=%p, namelen=%d, "
259 "job_id=%p(%d)) = \"%s\"", pid
, name
, namelen
, job_id
,
260 job_id
? *job_id
: 0, name
);
267 * 'cupsdStartProcess()' - Start a process.
270 int /* O - Process ID or 0 */
272 const char *command
, /* I - Full path to command */
273 char *argv
[], /* I - Command-line arguments */
274 char *envp
[], /* I - Environment */
275 int infd
, /* I - Standard input file descriptor */
276 int outfd
, /* I - Standard output file descriptor */
277 int errfd
, /* I - Standard error file descriptor */
278 int backfd
, /* I - Backchannel file descriptor */
279 int sidefd
, /* I - Sidechannel file descriptor */
280 int root
, /* I - Run as root? */
281 void *profile
, /* I - Security profile to use */
282 cupsd_job_t
*job
, /* I - Job associated with process */
283 int *pid
) /* O - Process ID */
285 int user
; /* Command UID */
286 struct stat commandinfo
; /* Command file information */
287 cupsd_proc_t
*proc
; /* New process record */
288 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
289 struct sigaction action
; /* POSIX signal handler */
290 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
291 #if defined(__APPLE__)
292 char processPath
[1024], /* CFProcessPath environment variable */
293 linkpath
[1024]; /* Link path for symlinks... */
294 int linkbytes
; /* Bytes for link path */
295 #endif /* __APPLE__ */
305 if (stat(command
, &commandinfo
))
309 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
310 "cupsdStartProcess(command=\"%s\", argv=%p, envp=%p, "
311 "infd=%d, outfd=%d, errfd=%d, backfd=%d, sidefd=%d, root=%d, "
312 "profile=%p, job=%p(%d), pid=%p) = %d",
313 command
, argv
, envp
, infd
, outfd
, errfd
, backfd
, sidefd
,
314 root
, profile
, job
, job
? job
->id
: 0, pid
, *pid
);
315 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to execute %s: %s", command
,
318 if (job
&& job
->printer
)
320 if (cupsdSetPrinterReasons(job
->printer
, "+cups-missing-filter-warning"))
321 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, job
->printer
, NULL
,
322 "Printer driver %s is missing.", command
);
328 ((commandinfo
.st_mode
& (S_ISUID
| S_IWGRP
| S_IWOTH
)) ||
333 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
334 "cupsdStartProcess(command=\"%s\", argv=%p, envp=%p, "
335 "infd=%d, outfd=%d, errfd=%d, backfd=%d, sidefd=%d, root=%d, "
336 "profile=%p, job=%p(%d), pid=%p) = %d",
337 command
, argv
, envp
, infd
, outfd
, errfd
, backfd
, sidefd
,
338 root
, profile
, job
, job
? job
->id
: 0, pid
, *pid
);
339 cupsdLogMessage(CUPSD_LOG_ERROR
,
340 "Unable to execute %s: insecure file permissions (0%o)",
341 command
, commandinfo
.st_mode
);
343 if (job
&& job
->printer
)
345 if (cupsdSetPrinterReasons(job
->printer
, "+cups-insecure-filter-warning"))
346 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, job
->printer
, NULL
,
347 "Printer driver %s has insecure file permissions (0%o).",
348 command
, commandinfo
.st_mode
);
355 else if ((commandinfo
.st_uid
!= user
|| !(commandinfo
.st_mode
& S_IXUSR
)) &&
356 (commandinfo
.st_gid
!= Group
|| !(commandinfo
.st_mode
& S_IXGRP
)) &&
357 !(commandinfo
.st_mode
& S_IXOTH
))
361 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
362 "cupsdStartProcess(command=\"%s\", argv=%p, envp=%p, "
363 "infd=%d, outfd=%d, errfd=%d, backfd=%d, sidefd=%d, root=%d, "
364 "profile=%p, job=%p(%d), pid=%p) = %d",
365 command
, argv
, envp
, infd
, outfd
, errfd
, backfd
, sidefd
,
366 root
, profile
, job
, job
? job
->id
: 0, pid
, *pid
);
367 cupsdLogMessage(CUPSD_LOG_ERROR
,
368 "Unable to execute %s: no execute permissions (0%o)",
369 command
, commandinfo
.st_mode
);
375 #if defined(__APPLE__)
379 * Add special voodoo magic for MacOS X - this allows MacOS X
380 * programs to access their bundle resources properly...
383 if ((linkbytes
= readlink(command
, linkpath
, sizeof(linkpath
) - 1)) > 0)
386 * Yes, this is a symlink to the actual program, nul-terminate and
390 linkpath
[linkbytes
] = '\0';
392 if (linkpath
[0] == '/')
393 snprintf(processPath
, sizeof(processPath
), "CFProcessPath=%s",
396 snprintf(processPath
, sizeof(processPath
), "CFProcessPath=%s/%s",
397 dirname((char *)command
), linkpath
);
400 snprintf(processPath
, sizeof(processPath
), "CFProcessPath=%s", command
);
402 envp
[0] = processPath
; /* Replace <CFProcessPath> string */
404 #endif /* __APPLE__ */
407 * Block signals before forking...
412 if ((*pid
= fork()) == 0)
415 * Child process goes here...
417 * Update stdin/stdout/stderr as needed...
423 infd
= open("/dev/null", O_RDONLY
);
435 outfd
= open("/dev/null", O_WRONLY
);
447 errfd
= open("/dev/null", O_WRONLY
);
456 if (backfd
!= 3 && backfd
>= 0)
460 fcntl(3, F_SETFL
, O_NDELAY
);
463 if (sidefd
!= 4 && sidefd
>= 0)
467 fcntl(4, F_SETFL
, O_NDELAY
);
471 * Change the priority of the process based on the FilterNice setting.
472 * (this is not done for root processes...)
478 #ifdef HAVE_SANDBOX_H
480 * Run in a separate security profile...
485 char *error
= NULL
; /* Sandbox error, if any */
487 if (sandbox_init((char *)profile
, SANDBOX_NAMED_EXTERNAL
, &error
))
489 fprintf(stderr
, "ERROR: sandbox_init failed: %s (%s)\n", error
,
491 sandbox_free_error(error
);
494 #endif /* HAVE_SANDBOX_H */
497 * Change user to something "safe"...
500 if (!root
&& !RunUser
)
503 * Running as root, so change to non-priviledged user...
509 if (setgroups(1, &Group
))
518 * Reset group membership to just the main one we belong to.
521 if (setgid(Group
) && !RunUser
)
524 if (setgroups(1, &Group
) && !RunUser
)
529 * Change umask to restrict permissions on created files...
535 * Unblock signals before doing the exec...
539 sigset(SIGTERM
, SIG_DFL
);
540 sigset(SIGCHLD
, SIG_DFL
);
541 sigset(SIGPIPE
, SIG_DFL
);
542 #elif defined(HAVE_SIGACTION)
543 memset(&action
, 0, sizeof(action
));
545 sigemptyset(&action
.sa_mask
);
546 action
.sa_handler
= SIG_DFL
;
548 sigaction(SIGTERM
, &action
, NULL
);
549 sigaction(SIGCHLD
, &action
, NULL
);
550 sigaction(SIGPIPE
, &action
, NULL
);
552 signal(SIGTERM
, SIG_DFL
);
553 signal(SIGCHLD
, SIG_DFL
);
554 signal(SIGPIPE
, SIG_DFL
);
555 #endif /* HAVE_SIGSET */
557 cupsdReleaseSignals();
560 * Execute the command; if for some reason this doesn't work,
561 * return the error code...
565 execve(command
, argv
, envp
);
567 execv(command
, argv
);
576 * Error - couldn't fork a new process!
579 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to fork %s - %s.", command
,
587 process_array
= cupsArrayNew((cups_array_func_t
)compare_procs
, NULL
);
591 if ((proc
= calloc(1, sizeof(cupsd_proc_t
) + strlen(command
))) != NULL
)
594 proc
->job_id
= job
? job
->id
: 0;
595 strcpy(proc
->name
, command
);
597 cupsArrayAdd(process_array
, proc
);
602 cupsdReleaseSignals();
604 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
605 "cupsdStartProcess(command=\"%s\", argv=%p, envp=%p, "
606 "infd=%d, outfd=%d, errfd=%d, backfd=%d, sidefd=%d, root=%d, "
607 "profile=%p, job=%p(%d), pid=%p) = %d",
608 command
, argv
, envp
, infd
, outfd
, errfd
, backfd
, sidefd
,
609 root
, profile
, job
, job
? job
->id
: 0, pid
, *pid
);
616 * 'compare_procs()' - Compare two processes.
619 static int /* O - Result of comparison */
620 compare_procs(cupsd_proc_t
*a
, /* I - First process */
621 cupsd_proc_t
*b
) /* I - Second process */
623 return (a
->pid
- b
->pid
);
627 #ifdef HAVE_SANDBOX_H
629 * 'cupsd_requote()' - Make a regular-expression version of a string.
632 static char * /* O - Quoted string */
633 cupsd_requote(char *dst
, /* I - Destination buffer */
634 const char *src
, /* I - Source string */
635 size_t dstsize
) /* I - Size of destination buffer */
637 int ch
; /* Current character */
638 char *dstptr
, /* Current position in buffer */
639 *dstend
; /* End of destination buffer */
643 dstend
= dst
+ dstsize
- 2;
645 while (*src
&& dstptr
< dstend
)
649 if (strchr(".?*()[]^$\\", ch
))
659 #endif /* HAVE_SANDBOX_H */
663 * End of "$Id: process.c 7256 2008-01-25 00:48:54Z mike $".