]> git.ipfire.org Git - thirdparty/cups.git/blame - scheduler/process.c
Merge changes from CUPS 1.4svn-r8492.
[thirdparty/cups.git] / scheduler / process.c
CommitLineData
ef416fc2 1/*
75bd9771 2 * "$Id: process.c 7256 2008-01-25 00:48:54Z mike $"
ef416fc2 3 *
4 * Process management routines for the Common UNIX Printing System (CUPS).
5 *
bf3816c7 6 * Copyright 2007-2009 by Apple Inc.
f7deaa1a 7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
ef416fc2 8 *
9 * These coded instructions, statements, and computer programs are the
bc44d920 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 * Contents:
16 *
a4924f6c
MS
17 * cupsdCreateProfile() - Create an execution profile for a subprocess.
18 * cupsdDestroyProfile() - Delete an execution profile.
19 * cupsdEndProcess() - End a process.
20 * cupsdFinishProcess() - Finish a process and get its name.
21 * cupsdStartProcess() - Start a process.
22 * compare_procs() - Compare two processes.
23 * cupsd_requote() - Make a regular-expression version of a string.
ef416fc2 24 */
25
26/*
27 * Include necessary headers...
28 */
29
30#include "cupsd.h"
31#include <grp.h>
a4924f6c 32#ifdef __APPLE__
4400e98d 33# include <libgen.h>
e53920b9 34#endif /* __APPLE__ */
a4924f6c
MS
35#ifdef HAVE_SANDBOX_H
36# define __APPLE_API_PRIVATE
37# include <sandbox.h>
38#endif /* HAVE_SANDBOX_H */
ef416fc2 39
40
e00b005a 41/*
42 * Process structure...
43 */
44
45typedef struct
46{
b9faaae1
MS
47 int pid, /* Process ID */
48 job_id; /* Job associated with process */
e00b005a 49 char name[1]; /* Name of process */
50} cupsd_proc_t;
51
52
53/*
54 * Local globals...
55 */
56
57static cups_array_t *process_array = NULL;
58
59
60/*
61 * Local functions...
62 */
63
64static int compare_procs(cupsd_proc_t *a, cupsd_proc_t *b);
a4924f6c
MS
65#ifdef HAVE_SANDBOX_H
66static char *cupsd_requote(char *dst, const char *src, size_t dstsize);
67#endif /* HAVE_SANDBOX_H */
68
69
70/*
71 * 'cupsdCreateProfile()' - Create an execution profile for a subprocess.
72 */
73
74void * /* O - Profile or NULL on error */
75cupsdCreateProfile(int job_id) /* I - Job ID or 0 for none */
76{
77#ifdef HAVE_SANDBOX_H
78 cups_file_t *fp; /* File pointer */
79 char profile[1024], /* File containing the profile */
80 cache[1024], /* Quoted CacheDir */
81 request[1024], /* Quoted RequestRoot */
82 root[1024], /* Quoted ServerRoot */
83 temp[1024]; /* Quoted TempDir */
84
85
1340db2d 86 if (!UseProfiles || RunUser)
b9faaae1
MS
87 {
88 /*
89 * Only use sandbox profiles as root...
90 */
91
92 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdCreateProfile(job_id=%d) = NULL",
93 job_id);
94
95 return (NULL);
96 }
97
a4924f6c
MS
98 if ((fp = cupsTempFile2(profile, sizeof(profile))) == NULL)
99 {
b9faaae1
MS
100 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdCreateProfile(job_id=%d) = NULL",
101 job_id);
a4924f6c
MS
102 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to create security profile: %s",
103 strerror(errno));
104 return (NULL);
105 }
106
107 cupsd_requote(cache, CacheDir, sizeof(cache));
108 cupsd_requote(request, RequestRoot, sizeof(request));
109 cupsd_requote(root, ServerRoot, sizeof(root));
110 cupsd_requote(temp, TempDir, sizeof(temp));
111
112 cupsFilePuts(fp, "(version 1)\n");
113 cupsFilePuts(fp, "(debug deny)\n");
114 cupsFilePuts(fp, "(allow default)\n");
115 cupsFilePrintf(fp,
116 "(deny file-write* file-read-data file-read-metadata\n"
5bd77a73 117 " (regex #\"^%s/\"))\n", request);
a4924f6c
MS
118 cupsFilePrintf(fp,
119 "(deny file-write*\n"
120 " (regex #\"^%s\" #\"^/private/etc\" #\"^/usr/local/etc\" "
5bd77a73 121 "#\"^/Library\" #\"^/System\" #\"^/Users\"))\n", root);
a4924f6c
MS
122 cupsFilePrintf(fp,
123 "(allow file-write* file-read-data file-read-metadata\n"
ed6e7faf
MS
124 " (regex #\"^%s$\" #\"^%s/\" #\"^%s$\" #\"^%s/\""
125 " #\"^/Library/Application Support/\""
126 " #\"^/Library/Caches/\""
127 " #\"^/Library/Preferences/\""
128 " #\"^/Library/Printers/\""
129 "))\n",
a4924f6c 130 temp, temp, cache, cache);
ed6e7faf
MS
131 cupsFilePuts(fp,
132 "(deny file-write*\n"
133 " (regex #\"^/Library/Printers/PPDs/\""
134 " #\"^/Library/Printers/PPD Plugins/\""
135 "))\n");
a4924f6c
MS
136 if (job_id)
137 cupsFilePrintf(fp,
138 "(allow file-read-data file-read-metadata\n"
139 " (regex #\"^%s/([ac]%05d|d%05d-[0-9][0-9][0-9])$\"))\n",
5bd77a73 140 request, job_id, job_id);
a4924f6c
MS
141
142 cupsFileClose(fp);
143
5bd77a73
MS
144 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdCreateProfile(job_id=%d) = \"%s\"",
145 job_id, profile);
a4924f6c 146 return ((void *)strdup(profile));
b9faaae1 147
a4924f6c 148#else
b9faaae1
MS
149 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdCreateProfile(job_id=%d) = NULL",
150 job_id);
a4924f6c
MS
151
152 return (NULL);
153#endif /* HAVE_SANDBOX_H */
154}
155
156
157/*
158 * 'cupsdDestroyProfile()' - Delete an execution profile.
159 */
160
161void
162cupsdDestroyProfile(void *profile) /* I - Profile */
163{
b9faaae1
MS
164 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdDeleteProfile(profile=\"%s\")",
165 profile ? (char *)profile : "(null)");
166
a4924f6c
MS
167#ifdef HAVE_SANDBOX_H
168 if (profile)
169 {
170 unlink((char *)profile);
171 free(profile);
172 }
173#endif /* HAVE_SANDBOX_H */
174}
e00b005a 175
176
ef416fc2 177/*
178 * 'cupsdEndProcess()' - End a process.
179 */
180
181int /* O - 0 on success, -1 on failure */
182cupsdEndProcess(int pid, /* I - Process ID */
183 int force) /* I - Force child to die */
184{
b9faaae1
MS
185 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdEndProcess(pid=%d, force=%d)", pid,
186 force);
187
ef416fc2 188 if (force)
189 return (kill(pid, SIGKILL));
190 else
191 return (kill(pid, SIGTERM));
192}
193
194
e00b005a 195/*
196 * 'cupsdFinishProcess()' - Finish a process and get its name.
197 */
198
199const char * /* O - Process name */
200cupsdFinishProcess(int pid, /* I - Process ID */
201 char *name, /* I - Name buffer */
b9faaae1
MS
202 int namelen, /* I - Size of name buffer */
203 int *job_id) /* O - Job ID pointer or NULL */
e00b005a 204{
205 cupsd_proc_t key, /* Search key */
206 *proc; /* Matching process */
207
208
209 key.pid = pid;
210
211 if ((proc = (cupsd_proc_t *)cupsArrayFind(process_array, &key)) != NULL)
212 {
b9faaae1
MS
213 if (job_id)
214 *job_id = proc->job_id;
215
e00b005a 216 strlcpy(name, proc->name, namelen);
217 cupsArrayRemove(process_array, proc);
218 free(proc);
e00b005a 219 }
220 else
b9faaae1
MS
221 {
222 if (job_id)
223 *job_id = 0;
224
225 strlcpy(name, "unknown", namelen);
226 }
227
228 cupsdLogMessage(CUPSD_LOG_DEBUG2,
229 "cupsdFinishProcess(pid=%d, name=%p, namelen=%d, "
230 "job_id=%p(%d)) = \"%s\"", pid, name, namelen, job_id,
231 job_id ? *job_id : 0, name);
232
233 return (name);
e00b005a 234}
235
236
ef416fc2 237/*
238 * 'cupsdStartProcess()' - Start a process.
239 */
240
241int /* O - Process ID or 0 */
242cupsdStartProcess(
b9faaae1
MS
243 const char *command, /* I - Full path to command */
244 char *argv[], /* I - Command-line arguments */
245 char *envp[], /* I - Environment */
246 int infd, /* I - Standard input file descriptor */
247 int outfd, /* I - Standard output file descriptor */
248 int errfd, /* I - Standard error file descriptor */
249 int backfd, /* I - Backchannel file descriptor */
250 int sidefd, /* I - Sidechannel file descriptor */
251 int root, /* I - Run as root? */
252 void *profile, /* I - Security profile to use */
38e73f87 253 cupsd_job_t *job, /* I - Job associated with process */
b9faaae1 254 int *pid) /* O - Process ID */
ef416fc2 255{
bf3816c7
MS
256 int user; /* Command UID */
257 struct stat commandinfo; /* Command file information */
e00b005a 258 cupsd_proc_t *proc; /* New process record */
ef416fc2 259#if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
e00b005a 260 struct sigaction action; /* POSIX signal handler */
ef416fc2 261#endif /* HAVE_SIGACTION && !HAVE_SIGSET */
e53920b9 262#if defined(__APPLE__)
e00b005a 263 char processPath[1024], /* CFProcessPath environment variable */
264 linkpath[1024]; /* Link path for symlinks... */
265 int linkbytes; /* Bytes for link path */
e53920b9 266#endif /* __APPLE__ */
ef416fc2 267
268
bf3816c7
MS
269 if (RunUser)
270 user = RunUser;
271 else if (root)
272 user = 0;
273 else
274 user = User;
275
276 if (stat(command, &commandinfo))
76cd9e37 277 {
b9faaae1
MS
278 *pid = 0;
279
280 cupsdLogMessage(CUPSD_LOG_DEBUG2,
281 "cupsdStartProcess(command=\"%s\", argv=%p, envp=%p, "
282 "infd=%d, outfd=%d, errfd=%d, backfd=%d, sidefd=%d, root=%d, "
38e73f87 283 "profile=%p, job=%p(%d), pid=%p) = %d",
b9faaae1 284 command, argv, envp, infd, outfd, errfd, backfd, sidefd,
38e73f87 285 root, profile, job, job ? job->id : 0, pid, *pid);
76cd9e37
MS
286 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to execute %s: %s", command,
287 strerror(errno));
38e73f87
MS
288
289 if (job && job->printer)
290 cupsdSetPrinterReasons(job->printer, "+cups-missing-filter-warning");
291
76cd9e37
MS
292 return (0);
293 }
38e73f87 294 else if (commandinfo.st_mode & (S_ISUID | S_IWOTH))
bf3816c7 295 {
b9faaae1
MS
296 *pid = 0;
297
298 cupsdLogMessage(CUPSD_LOG_DEBUG2,
299 "cupsdStartProcess(command=\"%s\", argv=%p, envp=%p, "
300 "infd=%d, outfd=%d, errfd=%d, backfd=%d, sidefd=%d, root=%d, "
38e73f87 301 "profile=%p, job=%p(%d), pid=%p) = %d",
b9faaae1 302 command, argv, envp, infd, outfd, errfd, backfd, sidefd,
38e73f87 303 root, profile, job, job ? job->id : 0, pid, *pid);
bf3816c7
MS
304 cupsdLogMessage(CUPSD_LOG_ERROR,
305 "Unable to execute %s: insecure file permissions (0%o)",
306 command, commandinfo.st_mode);
b9faaae1 307
38e73f87
MS
308 if (job && job->printer)
309 cupsdSetPrinterReasons(job->printer, "+cups-insecure-filter-warning");
310
bf3816c7 311 errno = EPERM;
38e73f87 312
bf3816c7
MS
313 return (0);
314 }
315 else if ((commandinfo.st_uid != user || !(commandinfo.st_mode & S_IXUSR)) &&
316 (commandinfo.st_gid != Group || !(commandinfo.st_mode & S_IXGRP)) &&
317 !(commandinfo.st_mode & S_IXOTH))
318 {
b9faaae1
MS
319 *pid = 0;
320
321 cupsdLogMessage(CUPSD_LOG_DEBUG2,
322 "cupsdStartProcess(command=\"%s\", argv=%p, envp=%p, "
323 "infd=%d, outfd=%d, errfd=%d, backfd=%d, sidefd=%d, root=%d, "
38e73f87 324 "profile=%p, job=%p(%d), pid=%p) = %d",
b9faaae1 325 command, argv, envp, infd, outfd, errfd, backfd, sidefd,
38e73f87 326 root, profile, job, job ? job->id : 0, pid, *pid);
bf3816c7
MS
327 cupsdLogMessage(CUPSD_LOG_ERROR,
328 "Unable to execute %s: no execute permissions (0%o)",
329 command, commandinfo.st_mode);
b9faaae1 330
bf3816c7
MS
331 errno = EPERM;
332 return (0);
333 }
76cd9e37 334
e53920b9 335#if defined(__APPLE__)
336 if (envp)
e00b005a 337 {
338 /*
e53920b9 339 * Add special voodoo magic for MacOS X - this allows MacOS X
340 * programs to access their bundle resources properly...
e00b005a 341 */
342
e53920b9 343 if ((linkbytes = readlink(command, linkpath, sizeof(linkpath) - 1)) > 0)
344 {
345 /*
346 * Yes, this is a symlink to the actual program, nul-terminate and
347 * use it...
348 */
349
350 linkpath[linkbytes] = '\0';
e00b005a 351
e53920b9 352 if (linkpath[0] == '/')
353 snprintf(processPath, sizeof(processPath), "CFProcessPath=%s",
354 linkpath);
355 else
356 snprintf(processPath, sizeof(processPath), "CFProcessPath=%s/%s",
f7deaa1a 357 dirname((char *)command), linkpath);
e53920b9 358 }
e00b005a 359 else
e53920b9 360 snprintf(processPath, sizeof(processPath), "CFProcessPath=%s", command);
bd7854cb 361
e53920b9 362 envp[0] = processPath; /* Replace <CFProcessPath> string */
363 }
364#endif /* __APPLE__ */
e00b005a 365
ef416fc2 366 /*
367 * Block signals before forking...
368 */
369
370 cupsdHoldSignals();
371
372 if ((*pid = fork()) == 0)
373 {
374 /*
375 * Child process goes here...
376 *
377 * Update stdin/stdout/stderr as needed...
378 */
379
380 if (infd != 0)
381 {
382 close(0);
383 if (infd > 0)
384 dup(infd);
385 else
386 open("/dev/null", O_RDONLY);
387 }
388 if (outfd != 1)
389 {
390 close(1);
391 if (outfd > 0)
392 dup(outfd);
393 else
394 open("/dev/null", O_WRONLY);
395 }
396 if (errfd != 2)
397 {
398 close(2);
399 if (errfd > 0)
400 dup(errfd);
401 else
402 open("/dev/null", O_WRONLY);
403 }
404 if (backfd != 3)
405 {
406 close(3);
407 if (backfd > 0)
408 dup(backfd);
409 else
410 open("/dev/null", O_RDWR);
411 fcntl(3, F_SETFL, O_NDELAY);
412 }
f7deaa1a 413 if (sidefd != 4 && sidefd > 0)
414 {
415 close(4);
416 dup(sidefd);
417 fcntl(4, F_SETFL, O_NDELAY);
418 }
ef416fc2 419
420 /*
421 * Change the priority of the process based on the FilterNice setting.
5bd77a73 422 * (this is not done for root processes...)
ef416fc2 423 */
424
425 if (!root)
426 nice(FilterNice);
427
5bd77a73
MS
428#ifdef HAVE_SANDBOX_H
429 /*
430 * Run in a separate security profile...
431 */
432
433 if (profile)
434 {
435 char *error = NULL; /* Sandbox error, if any */
436
437 if (sandbox_init((char *)profile, SANDBOX_NAMED_EXTERNAL, &error))
438 {
439 fprintf(stderr, "ERROR: sandbox_init failed: %s (%s)\n", error,
440 strerror(errno));
441 sandbox_free_error(error);
442 }
443 }
444#endif /* HAVE_SANDBOX_H */
445
ef416fc2 446 /*
447 * Change user to something "safe"...
448 */
449
450 if (!root && !RunUser)
451 {
452 /*
453 * Running as root, so change to non-priviledged user...
454 */
455
456 if (setgid(Group))
e00b005a 457 exit(errno);
ef416fc2 458
459 if (setgroups(1, &Group))
e00b005a 460 exit(errno);
ef416fc2 461
462 if (setuid(User))
463 exit(errno);
464 }
465 else
466 {
467 /*
468 * Reset group membership to just the main one we belong to.
469 */
470
e00b005a 471 setgid(Group);
ef416fc2 472 setgroups(1, &Group);
473 }
474
475 /*
476 * Change umask to restrict permissions on created files...
477 */
478
479 umask(077);
480
481 /*
482 * Unblock signals before doing the exec...
483 */
484
485#ifdef HAVE_SIGSET
486 sigset(SIGTERM, SIG_DFL);
487 sigset(SIGCHLD, SIG_DFL);
488#elif defined(HAVE_SIGACTION)
489 memset(&action, 0, sizeof(action));
490
491 sigemptyset(&action.sa_mask);
492 action.sa_handler = SIG_DFL;
493
494 sigaction(SIGTERM, &action, NULL);
495 sigaction(SIGCHLD, &action, NULL);
496#else
497 signal(SIGTERM, SIG_DFL);
498 signal(SIGCHLD, SIG_DFL);
499#endif /* HAVE_SIGSET */
500
501 cupsdReleaseSignals();
502
503 /*
504 * Execute the command; if for some reason this doesn't work,
505 * return the error code...
506 */
507
508 if (envp)
509 execve(command, argv, envp);
510 else
511 execv(command, argv);
512
513 perror(command);
514
515 exit(errno);
516 }
517 else if (*pid < 0)
518 {
519 /*
520 * Error - couldn't fork a new process!
521 */
522
523 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to fork %s - %s.", command,
524 strerror(errno));
525
526 *pid = 0;
527 }
e00b005a 528 else
529 {
530 if (!process_array)
531 process_array = cupsArrayNew((cups_array_func_t)compare_procs, NULL);
532
533 if (process_array)
534 {
535 if ((proc = calloc(1, sizeof(cupsd_proc_t) + strlen(command))) != NULL)
536 {
b9faaae1 537 proc->pid = *pid;
38e73f87 538 proc->job_id = job ? job->id : 0;
e00b005a 539 strcpy(proc->name, command);
540
541 cupsArrayAdd(process_array, proc);
542 }
543 }
544 }
ef416fc2 545
546 cupsdReleaseSignals();
547
b9faaae1
MS
548 cupsdLogMessage(CUPSD_LOG_DEBUG2,
549 "cupsdStartProcess(command=\"%s\", argv=%p, envp=%p, "
550 "infd=%d, outfd=%d, errfd=%d, backfd=%d, sidefd=%d, root=%d, "
38e73f87 551 "profile=%p, job=%p(%d), pid=%p) = %d",
b9faaae1 552 command, argv, envp, infd, outfd, errfd, backfd, sidefd,
38e73f87 553 root, profile, job, job ? job->id : 0, pid, *pid);
b9faaae1 554
ef416fc2 555 return (*pid);
556}
557
558
559/*
e00b005a 560 * 'compare_procs()' - Compare two processes.
561 */
562
563static int /* O - Result of comparison */
564compare_procs(cupsd_proc_t *a, /* I - First process */
565 cupsd_proc_t *b) /* I - Second process */
566{
567 return (a->pid - b->pid);
568}
569
570
a4924f6c
MS
571#ifdef HAVE_SANDBOX_H
572/*
573 * 'cupsd_requote()' - Make a regular-expression version of a string.
574 */
575
576static char * /* O - Quoted string */
577cupsd_requote(char *dst, /* I - Destination buffer */
578 const char *src, /* I - Source string */
579 size_t dstsize) /* I - Size of destination buffer */
580{
581 int ch; /* Current character */
582 char *dstptr, /* Current position in buffer */
583 *dstend; /* End of destination buffer */
584
585
586 dstptr = dst;
587 dstend = dst + dstsize - 2;
588
589 while (*src && dstptr < dstend)
590 {
591 ch = *src++;
592
593 if (strchr(".?*()[]^$\\", ch))
594 *dstptr++ = '\\';
595
596 *dstptr++ = ch;
597 }
598
599 *dstptr = '\0';
600
601 return (dst);
602}
603#endif /* HAVE_SANDBOX_H */
604
605
e00b005a 606/*
75bd9771 607 * End of "$Id: process.c 7256 2008-01-25 00:48:54Z mike $".
ef416fc2 608 */