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