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