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