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