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