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