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