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