]> git.ipfire.org Git - thirdparty/cups.git/blame - scheduler/process.c
More sandbox tweaking for IOKit access of USB/Bluetooth.
[thirdparty/cups.git] / scheduler / process.c
CommitLineData
ef416fc2 1/*
f2d18633 2 * "$Id$"
ef416fc2 3 *
7e86f2f6 4 * Process management routines for the CUPS scheduler.
ef416fc2 5 *
7e86f2f6
MS
6 * Copyright 2007-2014 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
ef416fc2 8 *
7e86f2f6
MS
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/".
ef416fc2 14 */
15
16/*
17 * Include necessary headers...
18 */
19
20#include "cupsd.h"
21#include <grp.h>
a4924f6c 22#ifdef __APPLE__
4400e98d 23# include <libgen.h>
6d2f911b 24#endif /* __APPLE__ */
c82f05ea
MS
25#ifdef HAVE_POSIX_SPAWN
26# include <spawn.h>
27extern char **environ;
28#endif /* HAVE_POSIX_SPAWN */
ef416fc2 29
30
e00b005a 31/*
32 * Process structure...
33 */
34
35typedef struct
36{
b9faaae1
MS
37 int pid, /* Process ID */
38 job_id; /* Job associated with process */
e00b005a 39 char name[1]; /* Name of process */
40} cupsd_proc_t;
41
42
43/*
44 * Local globals...
45 */
46
47static cups_array_t *process_array = NULL;
48
49
50/*
51 * Local functions...
52 */
53
54static int compare_procs(cupsd_proc_t *a, cupsd_proc_t *b);
a4924f6c
MS
55#ifdef HAVE_SANDBOX_H
56static char *cupsd_requote(char *dst, const char *src, size_t dstsize);
57#endif /* HAVE_SANDBOX_H */
58
59
60/*
61 * 'cupsdCreateProfile()' - Create an execution profile for a subprocess.
62 */
63
64void * /* O - Profile or NULL on error */
8fe0183a
MS
65cupsdCreateProfile(int job_id, /* I - Job ID or 0 for none */
66 int allow_networking)/* I - Allow networking off machine? */
a4924f6c
MS
67{
68#ifdef HAVE_SANDBOX_H
8fe0183a
MS
69 cups_file_t *fp; /* File pointer */
70 char profile[1024], /* File containing the profile */
71 bin[1024], /* Quoted ServerBin */
72 cache[1024], /* Quoted CacheDir */
73 domain[1024], /* Domain socket, if any */
74 request[1024], /* Quoted RequestRoot */
75 root[1024], /* Quoted ServerRoot */
76 temp[1024]; /* Quoted TempDir */
77 const char *nodebug; /* " (with no-log)" for no debug */
78 cupsd_listener_t *lis; /* Current listening socket */
79
80
81 if (!UseSandboxing || Sandboxing == CUPSD_SANDBOXING_OFF)
b9faaae1
MS
82 {
83 /*
84 * Only use sandbox profiles as root...
85 */
86
8fe0183a 87 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdCreateProfile(job_id=%d, allow_networking=%d) = NULL", job_id, allow_networking);
b9faaae1
MS
88
89 return (NULL);
90 }
91
a4924f6c
MS
92 if ((fp = cupsTempFile2(profile, sizeof(profile))) == NULL)
93 {
8fe0183a 94 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdCreateProfile(job_id=%d, allow_networking=%d) = NULL", job_id, allow_networking);
a4924f6c
MS
95 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to create security profile: %s",
96 strerror(errno));
97 return (NULL);
98 }
99
0268488e
MS
100 fchown(cupsFileNumber(fp), RunUser, Group);
101 fchmod(cupsFileNumber(fp), 0640);
102
8fe0183a 103 cupsd_requote(bin, ServerBin, sizeof(bin));
a4924f6c
MS
104 cupsd_requote(cache, CacheDir, sizeof(cache));
105 cupsd_requote(request, RequestRoot, sizeof(request));
106 cupsd_requote(root, ServerRoot, sizeof(root));
107 cupsd_requote(temp, TempDir, sizeof(temp));
108
88f9aafc
MS
109 nodebug = LogLevel < CUPSD_LOG_DEBUG ? " (with no-log)" : "";
110
a4924f6c 111 cupsFilePuts(fp, "(version 1)\n");
8fe0183a
MS
112 if (Sandboxing == CUPSD_SANDBOXING_STRICT)
113 cupsFilePuts(fp, "(deny default)\n");
114 else
115 cupsFilePuts(fp, "(allow default)\n");
116 if (LogLevel >= CUPSD_LOG_DEBUG)
117 cupsFilePuts(fp, "(debug deny)\n");
118 cupsFilePuts(fp, "(import \"system.sb\")\n");
119 cupsFilePuts(fp, "(system-network)\n");
120 cupsFilePuts(fp, "(allow mach-per-user-lookup)\n");
121 cupsFilePuts(fp, "(allow ipc-posix-sem)\n");
122 cupsFilePuts(fp, "(allow ipc-posix-shm)\n");
123 cupsFilePuts(fp, "(allow ipc-sysv-shm)\n");
124 cupsFilePuts(fp, "(allow mach-lookup)\n");
a4924f6c 125 cupsFilePrintf(fp,
8fe0183a
MS
126 "(deny file-write* file-read-data file-read-metadata\n"
127 " (regex"
5a6b583a
MS
128 " #\"^%s$\"" /* RequestRoot */
129 " #\"^%s/\"" /* RequestRoot/... */
88f9aafc
MS
130 ")%s)\n",
131 request, request, nodebug);
0268488e 132 if (!RunUser)
88f9aafc
MS
133 cupsFilePrintf(fp,
134 "(deny file-write* file-read-data file-read-metadata\n"
135 " (regex"
136 " #\"^/Users$\""
137 " #\"^/Users/\""
138 ")%s)\n", nodebug);
a4924f6c
MS
139 cupsFilePrintf(fp,
140 "(deny file-write*\n"
5a6b583a
MS
141 " (regex"
142 " #\"^%s$\"" /* ServerRoot */
143 " #\"^%s/\"" /* ServerRoot/... */
144 " #\"^/private/etc$\""
145 " #\"^/private/etc/\""
146 " #\"^/usr/local/etc$\""
147 " #\"^/usr/local/etc/\""
148 " #\"^/Library$\""
149 " #\"^/Library/\""
150 " #\"^/System$\""
151 " #\"^/System/\""
88f9aafc
MS
152 ")%s)\n",
153 root, root, nodebug);
8fe0183a 154 /* Specifically allow applications to stat RequestRoot and some other system folders */
7cf5915e
MS
155 cupsFilePrintf(fp,
156 "(allow file-read-metadata\n"
157 " (regex"
8fe0183a
MS
158 " #\"^/$\"" /* / */
159 " #\"^/usr$\"" /* /usr */
160 " #\"^/Library$\"" /* /Library */
161 " #\"^/Library/Printers$\"" /* /Library/Printers */
7cf5915e
MS
162 " #\"^%s$\"" /* RequestRoot */
163 "))\n",
164 request);
8fe0183a 165 /* Read and write TempDir, CacheDir, and other common folders */
a4924f6c
MS
166 cupsFilePrintf(fp,
167 "(allow file-write* file-read-data file-read-metadata\n"
5a6b583a
MS
168 " (regex"
169 " #\"^%s$\"" /* TempDir */
170 " #\"^%s/\"" /* TempDir/... */
171 " #\"^%s$\"" /* CacheDir */
172 " #\"^%s/\"" /* CacheDir/... */
bcc4b655 173 " #\"^/private/var/db/\""
8fe0183a 174 " #\"^/private/var/folders/\""
777e09f8 175 " #\"^/private/var/run/\""
ed6e7faf
MS
176 " #\"^/Library/Application Support/\""
177 " #\"^/Library/Caches/\""
178 " #\"^/Library/Preferences/\""
5a6b583a 179 " #\"^/Users/Shared/\""
ed6e7faf 180 "))\n",
8fe0183a
MS
181 temp, temp, cache, cache);
182 /* Read common folders */
88f9aafc 183 cupsFilePrintf(fp,
8fe0183a 184 "(allow file-read-data file-read-metadata\n"
8fe0183a 185 " (regex"
bcc4b655
MS
186 " #\"^/AppleInternal$\""
187 " #\"^/AppleInternal/\""
8fe0183a
MS
188 " #\"^/bin$\"" /* /bin */
189 " #\"^/bin/\"" /* /bin/... */
777e09f8 190 " #\"^/private$\""
d9183105
MS
191 " #\"^/private/etc$\""
192 " #\"^/private/etc/\""
777e09f8
MS
193 " #\"^/private/var$\""
194 " #\"^/private/var/db$\""
d9183105 195 " #\"^/private/var/folders$\""
777e09f8 196 " #\"^/private/var/spool$\""
8fe0183a
MS
197 " #\"^/usr/bin$\"" /* /usr/bin */
198 " #\"^/usr/bin/\"" /* /usr/bin/... */
199 " #\"^/usr/libexec/cups$\"" /* /usr/libexec/cups */
200 " #\"^/usr/libexec/cups/\"" /* /usr/libexec/cups/... */
3f4342fa
MS
201 " #\"^/usr/libexec/fax$\"" /* /usr/libexec/fax */
202 " #\"^/usr/libexec/fax/\"" /* /usr/libexec/fax/... */
8fe0183a
MS
203 " #\"^/usr/sbin$\"" /* /usr/sbin */
204 " #\"^/usr/sbin/\"" /* /usr/sbin/... */
0e53e7d1
MS
205 " #\"^/Library/Application Support$\""
206 " #\"^/Library/Application Support/\""
8fe0183a
MS
207 " #\"^/Library/Caches$\""
208 " #\"^/Library/Fonts$\""
209 " #\"^/Library/Fonts/\""
96964950
MS
210 " #\"^/Library/Frameworks$\""
211 " #\"^/Library/Frameworks/\""
777e09f8
MS
212 " #\"^/Library/Keychains$\""
213 " #\"^/Library/Keychains/\""
8fe0183a 214 " #\"^/Library/Printers$\""
777e09f8 215 " #\"^/Library/Printers/\""
8fe0183a
MS
216 " #\"^%s/Library$\"" /* RequestRoot/Library */
217 " #\"^%s/Library/\"" /* RequestRoot/Library/... */
218 " #\"^%s$\"" /* ServerBin */
219 " #\"^%s/\"" /* ServerBin/... */
220 " #\"^%s$\"" /* ServerRoot */
221 " #\"^%s/\"" /* ServerRoot/... */
222 "))\n",
223 request, request, bin, bin, root, root);
224 if (Sandboxing == CUPSD_SANDBOXING_RELAXED)
225 {
226 /* Limited write access to /Library/Printers/... */
227 cupsFilePuts(fp,
228 "(allow file-write*\n"
88f9aafc 229 " (regex"
8fe0183a
MS
230 " #\"^/Library/Printers/.*/\""
231 "))\n");
232 cupsFilePrintf(fp,
233 "(deny file-write*\n"
234 " (regex"
235 " #\"^/Library/Printers/PPDs$\""
236 " #\"^/Library/Printers/PPDs/\""
237 " #\"^/Library/Printers/PPD Plugins$\""
238 " #\"^/Library/Printers/PPD Plugins/\""
239 ")%s)\n", nodebug);
240 }
241 /* Allow execution of child processes */
242 cupsFilePuts(fp, "(allow process-fork)\n");
243 cupsFilePrintf(fp,
244 "(allow process-exec\n"
245 " (regex"
246 " #\"^/bin/\"" /* /bin/... */
247 " #\"^/usr/bin/\"" /* /usr/bin/... */
248 " #\"^/usr/libexec/cups/\"" /* /usr/libexec/cups/... */
8a6466eb 249 " #\"^/usr/libexec/fax/\"" /* /usr/libexec/fax/... */
8fe0183a
MS
250 " #\"^/usr/sbin/\"" /* /usr/sbin/... */
251 " #\"^%s/\"" /* ServerBin/... */
252 " #\"^/Library/Printers/.*/\""
3f4342fa 253 " #\"^/System/Library/Frameworks/Python.framework/\""
8fe0183a
MS
254 "))\n",
255 bin);
256 if (RunUser && getenv("CUPS_TESTROOT"))
e60ec91f 257 {
8fe0183a
MS
258 /* Allow source directory access in "make test" environment */
259 char testroot[1024]; /* Root directory of test files */
e60ec91f 260
8fe0183a
MS
261 cupsd_requote(testroot, getenv("CUPS_TESTROOT"), sizeof(testroot));
262
263 cupsFilePrintf(fp,
264 "(allow file-write* file-read-data file-read-metadata\n"
265 " (regex"
266 " #\"^%s$\"" /* CUPS_TESTROOT */
267 " #\"^%s/\"" /* CUPS_TESTROOT/... */
268 "))\n",
269 testroot, testroot);
270 cupsFilePrintf(fp,
271 "(allow process-exec\n"
272 " (regex"
273 " #\"^%s/\"" /* CUPS_TESTROOT/... */
274 "))\n",
275 testroot);
276 }
277 if (job_id)
278 {
279 /* Allow job filters to read the current job files... */
a4924f6c
MS
280 cupsFilePrintf(fp,
281 "(allow file-read-data file-read-metadata\n"
5a757074 282 " (regex #\"^%s/([ac]%05d|d%05d-[0-9][0-9][0-9])$\"))\n",
5bd77a73 283 request, job_id, job_id);
e60ec91f
MS
284 }
285 else
286 {
8fe0183a 287 /* Allow email notifications from notifiers... */
e60ec91f
MS
288 cupsFilePuts(fp,
289 "(allow process-exec\n"
290 " (literal \"/usr/sbin/sendmail\")\n"
8fe0183a
MS
291 " (with no-sandbox))\n");
292 }
777e09f8 293 /* Allow outbound networking to local services */
8fe0183a 294 cupsFilePuts(fp, "(allow network-outbound"
777e09f8 295 "\n (regex #\"^/private/var/run/\")");
8fe0183a
MS
296 for (lis = (cupsd_listener_t *)cupsArrayFirst(Listeners);
297 lis;
298 lis = (cupsd_listener_t *)cupsArrayNext(Listeners))
299 {
300 if (httpAddrFamily(&(lis->address)) == AF_LOCAL)
301 {
302 httpAddrString(&(lis->address), domain, sizeof(domain));
303 cupsFilePrintf(fp, "\n (literal \"%s\")", domain);
304 }
305 }
306 if (allow_networking)
307 {
308 /* Allow TCP and UDP networking off the machine... */
309 cupsFilePuts(fp, "\n (remote tcp))\n");
4c6277cc 310 cupsFilePuts(fp, "(allow network-bind)\n"); /* for LPD resvport */
8fe0183a
MS
311 cupsFilePuts(fp, "(allow network*\n"
312 " (local udp \"*:*\")\n"
313 " (remote udp \"*:*\"))\n");
bcc4b655 314
6e83de05 315 /* Also allow access to Bluetooth, USB, device files, etc. */
c12b983a 316 cupsFilePuts(fp, "(allow iokit*)\n");
e2685126 317 cupsFilePuts(fp, "(allow file-write* file-read-data file-read-metadata file-ioctl\n"
4c6277cc 318 " (regex #\"^/dev/\"))\n");
6e83de05 319 cupsFilePuts(fp, "(allow distributed-notification-post)\n");
8fe0183a
MS
320 }
321 else
322 {
323 /* Only allow SNMP (UDP) off the machine... */
324 cupsFilePuts(fp, ")\n");
325 cupsFilePuts(fp, "(allow network-outbound\n"
326 " (remote udp \"*:161\"))\n");
327 cupsFilePuts(fp, "(allow network-inbound\n"
328 " (local udp \"localhost:*\"))\n");
c12b983a 329 cupsFilePuts(fp, "(deny iokit* (with no-report))\n");
e60ec91f 330 }
a4924f6c
MS
331 cupsFileClose(fp);
332
8fe0183a 333 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdCreateProfile(job_id=%d,allow_networking=%d) = \"%s\"", job_id, allow_networking, profile);
a4924f6c 334 return ((void *)strdup(profile));
b9faaae1 335
a4924f6c 336#else
8fe0183a 337 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdCreateProfile(job_id=%d, allow_networking=%d) = NULL", job_id, allow_networking);
a4924f6c
MS
338
339 return (NULL);
340#endif /* HAVE_SANDBOX_H */
341}
342
343
344/*
345 * 'cupsdDestroyProfile()' - Delete an execution profile.
346 */
347
348void
349cupsdDestroyProfile(void *profile) /* I - Profile */
350{
b9faaae1
MS
351 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdDeleteProfile(profile=\"%s\")",
352 profile ? (char *)profile : "(null)");
353
a4924f6c
MS
354#ifdef HAVE_SANDBOX_H
355 if (profile)
356 {
357 unlink((char *)profile);
358 free(profile);
359 }
360#endif /* HAVE_SANDBOX_H */
361}
e00b005a 362
363
ef416fc2 364/*
365 * 'cupsdEndProcess()' - End a process.
366 */
367
368int /* O - 0 on success, -1 on failure */
369cupsdEndProcess(int pid, /* I - Process ID */
370 int force) /* I - Force child to die */
371{
b9faaae1
MS
372 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdEndProcess(pid=%d, force=%d)", pid,
373 force);
374
ef55b745
MS
375 if (!pid)
376 return (0);
88f9aafc
MS
377
378 if (!RunUser)
379 {
380 /*
381 * When running as root, cupsd puts child processes in their own process
382 * group. Using "-pid" sends a signal to all processes in the group.
383 */
384
385 pid = -pid;
386 }
387
388 if (force)
ef416fc2 389 return (kill(pid, SIGKILL));
390 else
391 return (kill(pid, SIGTERM));
392}
393
394
e00b005a 395/*
396 * 'cupsdFinishProcess()' - Finish a process and get its name.
397 */
398
399const char * /* O - Process name */
07623986
MS
400cupsdFinishProcess(int pid, /* I - Process ID */
401 char *name, /* I - Name buffer */
402 size_t namelen, /* I - Size of name buffer */
403 int *job_id) /* O - Job ID pointer or NULL */
e00b005a 404{
405 cupsd_proc_t key, /* Search key */
406 *proc; /* Matching process */
407
408
409 key.pid = pid;
410
411 if ((proc = (cupsd_proc_t *)cupsArrayFind(process_array, &key)) != NULL)
412 {
b9faaae1
MS
413 if (job_id)
414 *job_id = proc->job_id;
415
e00b005a 416 strlcpy(name, proc->name, namelen);
417 cupsArrayRemove(process_array, proc);
418 free(proc);
e00b005a 419 }
420 else
b9faaae1
MS
421 {
422 if (job_id)
423 *job_id = 0;
424
425 strlcpy(name, "unknown", namelen);
426 }
427
07623986 428 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdFinishProcess(pid=%d, name=%p, namelen=" CUPS_LLFMT ", job_id=%p(%d)) = \"%s\"", pid, name, CUPS_LLCAST namelen, job_id, job_id ? *job_id : 0, name);
b9faaae1
MS
429
430 return (name);
e00b005a 431}
432
433
ef416fc2 434/*
435 * 'cupsdStartProcess()' - Start a process.
436 */
437
438int /* O - Process ID or 0 */
439cupsdStartProcess(
b9faaae1
MS
440 const char *command, /* I - Full path to command */
441 char *argv[], /* I - Command-line arguments */
442 char *envp[], /* I - Environment */
443 int infd, /* I - Standard input file descriptor */
444 int outfd, /* I - Standard output file descriptor */
445 int errfd, /* I - Standard error file descriptor */
446 int backfd, /* I - Backchannel file descriptor */
447 int sidefd, /* I - Sidechannel file descriptor */
448 int root, /* I - Run as root? */
449 void *profile, /* I - Security profile to use */
38e73f87 450 cupsd_job_t *job, /* I - Job associated with process */
b9faaae1 451 int *pid) /* O - Process ID */
ef416fc2 452{
0268488e 453 int i; /* Looping var */
e60ec91f 454 const char *exec_path = command; /* Command to be exec'd */
28c194b0 455 char *real_argv[110], /* Real command-line arguments */
0268488e 456 cups_exec[1024]; /* Path to "cups-exec" program */
7e86f2f6 457 uid_t user; /* Command UID */
e00b005a 458 cupsd_proc_t *proc; /* New process record */
c82f05ea
MS
459#ifdef HAVE_POSIX_SPAWN
460 posix_spawn_file_actions_t actions; /* Spawn file actions */
461 posix_spawnattr_t attrs; /* Spawn attributes */
462 char user_str[16], /* User string */
463 group_str[16], /* Group string */
464 nice_str[16]; /* FilterNice string */
07623986 465#elif defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
e00b005a 466 struct sigaction action; /* POSIX signal handler */
07623986 467#endif /* HAVE_POSIX_SPAWN */
e53920b9 468#if defined(__APPLE__)
e00b005a 469 char processPath[1024], /* CFProcessPath environment variable */
470 linkpath[1024]; /* Link path for symlinks... */
471 int linkbytes; /* Bytes for link path */
e53920b9 472#endif /* __APPLE__ */
ef416fc2 473
474
22c9029b
MS
475 *pid = 0;
476
0268488e
MS
477 /*
478 * Figure out the UID for the child process...
479 */
480
bf3816c7
MS
481 if (RunUser)
482 user = RunUser;
483 else if (root)
484 user = 0;
485 else
486 user = User;
487
0268488e
MS
488 /*
489 * Check the permissions of the command we are running...
490 */
491
22c9029b
MS
492 if (_cupsFileCheck(command, _CUPS_FILE_CHECK_PROGRAM, !RunUser,
493 cupsdLogFCMessage, job ? job->printer : NULL))
bf3816c7 494 return (0);
76cd9e37 495
e53920b9 496#if defined(__APPLE__)
497 if (envp)
e00b005a 498 {
499 /*
f3c17241
MS
500 * Add special voodoo magic for OS X - this allows OS X programs to access
501 * their bundle resources properly...
e00b005a 502 */
503
e53920b9 504 if ((linkbytes = readlink(command, linkpath, sizeof(linkpath) - 1)) > 0)
505 {
506 /*
507 * Yes, this is a symlink to the actual program, nul-terminate and
508 * use it...
509 */
510
511 linkpath[linkbytes] = '\0';
e00b005a 512
e53920b9 513 if (linkpath[0] == '/')
514 snprintf(processPath, sizeof(processPath), "CFProcessPath=%s",
515 linkpath);
516 else
517 snprintf(processPath, sizeof(processPath), "CFProcessPath=%s/%s",
f7deaa1a 518 dirname((char *)command), linkpath);
e53920b9 519 }
e00b005a 520 else
e53920b9 521 snprintf(processPath, sizeof(processPath), "CFProcessPath=%s", command);
bd7854cb 522
e53920b9 523 envp[0] = processPath; /* Replace <CFProcessPath> string */
524 }
525#endif /* __APPLE__ */
e00b005a 526
0268488e
MS
527 /*
528 * Use helper program when we have a sandbox profile...
529 */
530
c82f05ea 531#ifndef HAVE_POSIX_SPAWN
0268488e 532 if (profile)
c82f05ea 533#endif /* !HAVE_POSIX_SPAWN */
0268488e
MS
534 {
535 snprintf(cups_exec, sizeof(cups_exec), "%s/daemon/cups-exec", ServerBin);
8fe0183a 536 snprintf(user_str, sizeof(user_str), "%d", user);
c82f05ea
MS
537 snprintf(group_str, sizeof(group_str), "%d", Group);
538 snprintf(nice_str, sizeof(nice_str), "%d", FilterNice);
0268488e
MS
539
540 real_argv[0] = cups_exec;
28c194b0
MS
541 real_argv[1] = (char *)"-g";
542 real_argv[2] = group_str;
543 real_argv[3] = (char *)"-n";
c82f05ea 544 real_argv[4] = nice_str;
28c194b0
MS
545 real_argv[5] = (char *)"-u";
546 real_argv[6] = user_str;
547 real_argv[7] = profile;
548 real_argv[8] = (char *)command;
0268488e
MS
549
550 for (i = 0;
28c194b0 551 i < (int)(sizeof(real_argv) / sizeof(real_argv[0]) - 10) && argv[i];
0268488e 552 i ++)
28c194b0 553 real_argv[i + 9] = argv[i];
0268488e 554
28c194b0 555 real_argv[i + 9] = NULL;
0268488e 556
e60ec91f
MS
557 argv = real_argv;
558 exec_path = cups_exec;
0268488e
MS
559 }
560
c82f05ea
MS
561 if (LogLevel == CUPSD_LOG_DEBUG2)
562 {
563 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdStartProcess: Preparing to start \"%s\", arguments:", command);
564
565 for (i = 0; argv[i]; i ++)
566 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdStartProcess: argv[%d] = \"%s\"", i, argv[i]);
567 }
568
569#ifdef HAVE_POSIX_SPAWN
570 /*
571 * Setup attributes and file actions for the spawn...
572 */
573
574 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdStartProcess: Setting spawn attributes.");
575 posix_spawnattr_init(&attrs);
576 posix_spawnattr_setflags(&attrs, POSIX_SPAWN_SETPGROUP | POSIX_SPAWN_SETSIGDEF);
577
578 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdStartProcess: Setting file actions.");
579 posix_spawn_file_actions_init(&actions);
580 if (infd != 0)
581 {
582 if (infd < 0)
583 posix_spawn_file_actions_addopen(&actions, 0, "/dev/null", O_WRONLY, 0);
584 else
585 posix_spawn_file_actions_adddup2(&actions, infd, 0);
586 }
587
588 if (outfd != 1)
589 {
590 if (outfd < 0)
591 posix_spawn_file_actions_addopen(&actions, 1, "/dev/null", O_WRONLY, 0);
592 else
593 posix_spawn_file_actions_adddup2(&actions, outfd, 1);
594 }
595
596 if (errfd != 2)
597 {
598 if (errfd < 0)
599 posix_spawn_file_actions_addopen(&actions, 2, "/dev/null", O_WRONLY, 0);
600 else
601 posix_spawn_file_actions_adddup2(&actions, errfd, 2);
602 }
603
604 if (backfd != 3 && backfd >= 0)
605 posix_spawn_file_actions_adddup2(&actions, backfd, 3);
606
607 if (sidefd != 4 && sidefd >= 0)
608 posix_spawn_file_actions_adddup2(&actions, sidefd, 4);
609
610 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdStartProcess: Calling posix_spawn.");
611
612 if (posix_spawn(pid, exec_path, &actions, &attrs, argv, envp ? envp : environ))
613 {
614 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to fork %s - %s.", command, strerror(errno));
615
616 *pid = 0;
617 }
618 else
619 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdStartProcess: pid=%d", (int)*pid);
620
621 posix_spawn_file_actions_destroy(&actions);
622 posix_spawnattr_destroy(&attrs);
623
624#else
ef416fc2 625 /*
626 * Block signals before forking...
627 */
628
629 cupsdHoldSignals();
630
631 if ((*pid = fork()) == 0)
632 {
633 /*
88f9aafc
MS
634 * Child process goes here; update stderr as needed...
635 */
636
637 if (errfd != 2)
638 {
639 if (errfd < 0)
640 errfd = open("/dev/null", O_WRONLY);
641
642 if (errfd != 2)
643 {
644 dup2(errfd, 2);
645 close(errfd);
646 }
647 }
648
649 /*
650 * Put this process in its own process group so that we can kill any child
651 * processes it creates.
652 */
653
c82f05ea 654# ifdef HAVE_SETPGID
88f9aafc
MS
655 if (!RunUser && setpgid(0, 0))
656 exit(errno + 100);
c82f05ea 657# else
88f9aafc
MS
658 if (!RunUser && setpgrp())
659 exit(errno + 100);
c82f05ea 660# endif /* HAVE_SETPGID */
88f9aafc
MS
661
662 /*
663 * Update the remaining file descriptors as needed...
ef416fc2 664 */
665
666 if (infd != 0)
667 {
68b10830
MS
668 if (infd < 0)
669 infd = open("/dev/null", O_RDONLY);
670
671 if (infd != 0)
672 {
673 dup2(infd, 0);
674 close(infd);
675 }
ef416fc2 676 }
68b10830 677
ef416fc2 678 if (outfd != 1)
679 {
68b10830
MS
680 if (outfd < 0)
681 outfd = open("/dev/null", O_WRONLY);
682
683 if (outfd != 1)
684 {
685 dup2(outfd, 1);
686 close(outfd);
687 }
ef416fc2 688 }
68b10830 689
68b10830 690 if (backfd != 3 && backfd >= 0)
ef416fc2 691 {
68b10830
MS
692 dup2(backfd, 3);
693 close(backfd);
ef416fc2 694 fcntl(3, F_SETFL, O_NDELAY);
695 }
68b10830
MS
696
697 if (sidefd != 4 && sidefd >= 0)
f7deaa1a 698 {
68b10830
MS
699 dup2(sidefd, 4);
700 close(sidefd);
f7deaa1a 701 fcntl(4, F_SETFL, O_NDELAY);
702 }
ef416fc2 703
704 /*
705 * Change the priority of the process based on the FilterNice setting.
5bd77a73 706 * (this is not done for root processes...)
ef416fc2 707 */
708
709 if (!root)
710 nice(FilterNice);
711
712 /*
88f9aafc 713 * Reset group membership to just the main one we belong to.
ef416fc2 714 */
715
88f9aafc
MS
716 if (!RunUser && setgid(Group))
717 exit(errno + 100);
ef416fc2 718
88f9aafc
MS
719 if (!RunUser && setgroups(1, &Group))
720 exit(errno + 100);
ef416fc2 721
88f9aafc
MS
722 /*
723 * Change user to something "safe"...
724 */
41681883 725
88f9aafc
MS
726 if (!RunUser && user && setuid(user))
727 exit(errno + 100);
ef416fc2 728
729 /*
730 * Change umask to restrict permissions on created files...
731 */
732
733 umask(077);
734
735 /*
736 * Unblock signals before doing the exec...
737 */
738
c82f05ea 739# ifdef HAVE_SIGSET
ef416fc2 740 sigset(SIGTERM, SIG_DFL);
741 sigset(SIGCHLD, SIG_DFL);
ef55b745 742 sigset(SIGPIPE, SIG_DFL);
c82f05ea 743# elif defined(HAVE_SIGACTION)
ef416fc2 744 memset(&action, 0, sizeof(action));
745
746 sigemptyset(&action.sa_mask);
747 action.sa_handler = SIG_DFL;
748
749 sigaction(SIGTERM, &action, NULL);
750 sigaction(SIGCHLD, &action, NULL);
ef55b745 751 sigaction(SIGPIPE, &action, NULL);
c82f05ea 752# else
ef416fc2 753 signal(SIGTERM, SIG_DFL);
754 signal(SIGCHLD, SIG_DFL);
ef55b745 755 signal(SIGPIPE, SIG_DFL);
c82f05ea 756# endif /* HAVE_SIGSET */
ef416fc2 757
758 cupsdReleaseSignals();
759
760 /*
0268488e
MS
761 * Execute the command; if for some reason this doesn't work, log an error
762 * exit with a non-zero value...
ef416fc2 763 */
764
765 if (envp)
e60ec91f 766 execve(exec_path, argv, envp);
ef416fc2 767 else
e60ec91f 768 execv(exec_path, argv);
ef416fc2 769
88f9aafc 770 exit(errno + 100);
ef416fc2 771 }
772 else if (*pid < 0)
773 {
774 /*
775 * Error - couldn't fork a new process!
776 */
777
778 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to fork %s - %s.", command,
779 strerror(errno));
780
781 *pid = 0;
782 }
c82f05ea
MS
783
784 cupsdReleaseSignals();
785#endif /* HAVE_POSIX_SPAWN */
786
787 if (*pid)
e00b005a 788 {
789 if (!process_array)
790 process_array = cupsArrayNew((cups_array_func_t)compare_procs, NULL);
f3c17241 791
e00b005a 792 if (process_array)
793 {
794 if ((proc = calloc(1, sizeof(cupsd_proc_t) + strlen(command))) != NULL)
795 {
b9faaae1 796 proc->pid = *pid;
38e73f87 797 proc->job_id = job ? job->id : 0;
e60ec91f 798 _cups_strcpy(proc->name, command);
e00b005a 799
800 cupsArrayAdd(process_array, proc);
801 }
802 }
803 }
ef416fc2 804
b9faaae1
MS
805 cupsdLogMessage(CUPSD_LOG_DEBUG2,
806 "cupsdStartProcess(command=\"%s\", argv=%p, envp=%p, "
807 "infd=%d, outfd=%d, errfd=%d, backfd=%d, sidefd=%d, root=%d, "
38e73f87 808 "profile=%p, job=%p(%d), pid=%p) = %d",
b9faaae1 809 command, argv, envp, infd, outfd, errfd, backfd, sidefd,
38e73f87 810 root, profile, job, job ? job->id : 0, pid, *pid);
b9faaae1 811
ef416fc2 812 return (*pid);
813}
814
815
816/*
e00b005a 817 * 'compare_procs()' - Compare two processes.
818 */
819
820static int /* O - Result of comparison */
821compare_procs(cupsd_proc_t *a, /* I - First process */
822 cupsd_proc_t *b) /* I - Second process */
823{
824 return (a->pid - b->pid);
825}
826
827
a4924f6c
MS
828#ifdef HAVE_SANDBOX_H
829/*
830 * 'cupsd_requote()' - Make a regular-expression version of a string.
831 */
832
833static char * /* O - Quoted string */
834cupsd_requote(char *dst, /* I - Destination buffer */
835 const char *src, /* I - Source string */
836 size_t dstsize) /* I - Size of destination buffer */
837{
838 int ch; /* Current character */
839 char *dstptr, /* Current position in buffer */
840 *dstend; /* End of destination buffer */
841
842
843 dstptr = dst;
844 dstend = dst + dstsize - 2;
845
846 while (*src && dstptr < dstend)
847 {
848 ch = *src++;
849
cb7f98ee
MS
850 if (ch == '/' && !*src)
851 break; /* Don't add trailing slash */
852
a4924f6c
MS
853 if (strchr(".?*()[]^$\\", ch))
854 *dstptr++ = '\\';
855
7e86f2f6 856 *dstptr++ = (char)ch;
a4924f6c
MS
857 }
858
859 *dstptr = '\0';
860
861 return (dst);
862}
863#endif /* HAVE_SANDBOX_H */
864
865
e00b005a 866/*
f2d18633 867 * End of "$Id$".
ef416fc2 868 */