4 * Process management routines for the CUPS scheduler.
6 * Copyright 2007-2014 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
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/".
17 * Include necessary headers...
24 #endif /* __APPLE__ */
25 #ifdef HAVE_POSIX_SPAWN
27 extern char **environ
;
28 #endif /* HAVE_POSIX_SPAWN */
32 * Process structure...
37 int pid
, /* Process ID */
38 job_id
; /* Job associated with process */
39 char name
[1]; /* Name of process */
47 static cups_array_t
*process_array
= NULL
;
54 static int compare_procs(cupsd_proc_t
*a
, cupsd_proc_t
*b
);
56 static char *cupsd_requote(char *dst
, const char *src
, size_t dstsize
);
57 #endif /* HAVE_SANDBOX_H */
61 * 'cupsdCreateProfile()' - Create an execution profile for a subprocess.
64 void * /* O - Profile or NULL on error */
65 cupsdCreateProfile(int job_id
, /* I - Job ID or 0 for none */
66 int allow_networking
)/* I - Allow networking off machine? */
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 */
81 if (!UseSandboxing
|| Sandboxing
== CUPSD_SANDBOXING_OFF
)
84 * Only use sandbox profiles as root...
87 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdCreateProfile(job_id=%d, allow_networking=%d) = NULL", job_id
, allow_networking
);
92 if ((fp
= cupsTempFile2(profile
, sizeof(profile
))) == NULL
)
94 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdCreateProfile(job_id=%d, allow_networking=%d) = NULL", job_id
, allow_networking
);
95 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create security profile: %s",
100 fchown(cupsFileNumber(fp
), RunUser
, Group
);
101 fchmod(cupsFileNumber(fp
), 0640);
103 cupsd_requote(bin
, ServerBin
, sizeof(bin
));
104 cupsd_requote(cache
, CacheDir
, sizeof(cache
));
105 cupsd_requote(request
, RequestRoot
, sizeof(request
));
106 cupsd_requote(root
, ServerRoot
, sizeof(root
));
107 cupsd_requote(temp
, TempDir
, sizeof(temp
));
109 nodebug
= LogLevel
< CUPSD_LOG_DEBUG
? " (with no-log)" : "";
111 cupsFilePuts(fp
, "(version 1)\n");
112 if (Sandboxing
== CUPSD_SANDBOXING_STRICT
)
113 cupsFilePuts(fp
, "(deny default)\n");
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");
126 "(deny file-write* file-read-data file-read-metadata\n"
128 " #\"^%s$\"" /* RequestRoot */
129 " #\"^%s/\"" /* RequestRoot/... */
131 request
, request
, nodebug
);
134 "(deny file-write* file-read-data file-read-metadata\n"
140 "(deny file-write*\n"
142 " #\"^%s$\"" /* ServerRoot */
143 " #\"^%s/\"" /* ServerRoot/... */
144 " #\"^/private/etc$\""
145 " #\"^/private/etc/\""
146 " #\"^/usr/local/etc$\""
147 " #\"^/usr/local/etc/\""
153 root
, root
, nodebug
);
154 /* Specifically allow applications to stat RequestRoot and some other system folders */
156 "(allow file-read-metadata\n"
159 " #\"^/usr$\"" /* /usr */
160 " #\"^/Library$\"" /* /Library */
161 " #\"^/Library/Printers$\"" /* /Library/Printers */
162 " #\"^%s$\"" /* RequestRoot */
165 /* Read and write TempDir, CacheDir, and other common folders */
167 "(allow file-write* file-read-data file-read-metadata\n"
169 " #\"^%s$\"" /* TempDir */
170 " #\"^%s/\"" /* TempDir/... */
171 " #\"^%s$\"" /* CacheDir */
172 " #\"^%s/\"" /* CacheDir/... */
173 " #\"^/private/var/folders/\""
174 " #\"^/Library/Application Support/\""
175 " #\"^/Library/Caches/\""
176 " #\"^/Library/Preferences/\""
177 " #\"^/Users/Shared/\""
179 temp
, temp
, cache
, cache
);
180 /* Read common folders */
182 "(allow file-read-data file-read-metadata\n"
183 " (literal \"/private/etc/services\")\n"
185 " #\"^/bin$\"" /* /bin */
186 " #\"^/bin/\"" /* /bin/... */
187 " #\"^/usr/bin$\"" /* /usr/bin */
188 " #\"^/usr/bin/\"" /* /usr/bin/... */
189 " #\"^/usr/libexec/cups$\"" /* /usr/libexec/cups */
190 " #\"^/usr/libexec/cups/\"" /* /usr/libexec/cups/... */
191 " #\"^/usr/sbin$\"" /* /usr/sbin */
192 " #\"^/usr/sbin/\"" /* /usr/sbin/... */
193 " #\"^/Library/Caches$\""
194 " #\"^/Library/Fonts$\""
195 " #\"^/Library/Fonts/\""
196 " #\"^/Library/Printers$\""
197 " #\"^/Library/Printers/.*$\""
198 " #\"^%s/Library$\"" /* RequestRoot/Library */
199 " #\"^%s/Library/\"" /* RequestRoot/Library/... */
200 " #\"^%s$\"" /* ServerBin */
201 " #\"^%s/\"" /* ServerBin/... */
202 " #\"^%s$\"" /* ServerRoot */
203 " #\"^%s/\"" /* ServerRoot/... */
205 request
, request
, bin
, bin
, root
, root
);
206 if (Sandboxing
== CUPSD_SANDBOXING_RELAXED
)
208 /* Limited write access to /Library/Printers/... */
210 "(allow file-write*\n"
212 " #\"^/Library/Printers/.*/\""
215 "(deny file-write*\n"
217 " #\"^/Library/Printers/PPDs$\""
218 " #\"^/Library/Printers/PPDs/\""
219 " #\"^/Library/Printers/PPD Plugins$\""
220 " #\"^/Library/Printers/PPD Plugins/\""
223 /* Allow execution of child processes */
224 cupsFilePuts(fp
, "(allow process-fork)\n");
226 "(allow process-exec\n"
228 " #\"^/bin/\"" /* /bin/... */
229 " #\"^/usr/bin/\"" /* /usr/bin/... */
230 " #\"^/usr/libexec/cups/\"" /* /usr/libexec/cups/... */
231 " #\"^/usr/sbin/\"" /* /usr/sbin/... */
232 " #\"^%s/\"" /* ServerBin/... */
233 " #\"^/Library/Printers/.*/\""
236 if (RunUser
&& getenv("CUPS_TESTROOT"))
238 /* Allow source directory access in "make test" environment */
239 char testroot
[1024]; /* Root directory of test files */
241 cupsd_requote(testroot
, getenv("CUPS_TESTROOT"), sizeof(testroot
));
244 "(allow file-write* file-read-data file-read-metadata\n"
246 " #\"^%s$\"" /* CUPS_TESTROOT */
247 " #\"^%s/\"" /* CUPS_TESTROOT/... */
251 "(allow process-exec\n"
253 " #\"^%s/\"" /* CUPS_TESTROOT/... */
259 /* Allow job filters to read the current job files... */
261 "(allow file-read-data file-read-metadata\n"
262 " (regex #\"^%s/([ac]%05d|d%05d-[0-9][0-9][0-9])$\"))\n",
263 request
, job_id
, job_id
);
267 /* Allow email notifications from notifiers... */
269 "(allow process-exec\n"
270 " (literal \"/usr/sbin/sendmail\")\n"
271 " (with no-sandbox))\n");
273 /* Allow outbound networking to local mDNSResponder and cupsd */
274 cupsFilePuts(fp
, "(allow network-outbound"
275 "\n (literal \"/private/var/run/mDNSResponder\")");
276 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
278 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
280 if (httpAddrFamily(&(lis
->address
)) == AF_LOCAL
)
282 httpAddrString(&(lis
->address
), domain
, sizeof(domain
));
283 cupsFilePrintf(fp
, "\n (literal \"%s\")", domain
);
286 if (allow_networking
)
288 /* Allow TCP and UDP networking off the machine... */
289 cupsFilePuts(fp
, "\n (remote tcp))\n");
290 cupsFilePuts(fp
, "(allow network*\n"
291 " (local udp \"*:*\")\n"
292 " (remote udp \"*:*\"))\n");
296 /* Only allow SNMP (UDP) off the machine... */
297 cupsFilePuts(fp
, ")\n");
298 cupsFilePuts(fp
, "(allow network-outbound\n"
299 " (remote udp \"*:161\"))\n");
300 cupsFilePuts(fp
, "(allow network-inbound\n"
301 " (local udp \"localhost:*\"))\n");
305 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdCreateProfile(job_id=%d,allow_networking=%d) = \"%s\"", job_id
, allow_networking
, profile
);
306 return ((void *)strdup(profile
));
309 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdCreateProfile(job_id=%d, allow_networking=%d) = NULL", job_id
, allow_networking
);
312 #endif /* HAVE_SANDBOX_H */
317 * 'cupsdDestroyProfile()' - Delete an execution profile.
321 cupsdDestroyProfile(void *profile
) /* I - Profile */
323 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdDeleteProfile(profile=\"%s\")",
324 profile
? (char *)profile
: "(null)");
326 #ifdef HAVE_SANDBOX_H
329 unlink((char *)profile
);
332 #endif /* HAVE_SANDBOX_H */
337 * 'cupsdEndProcess()' - End a process.
340 int /* O - 0 on success, -1 on failure */
341 cupsdEndProcess(int pid
, /* I - Process ID */
342 int force
) /* I - Force child to die */
344 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdEndProcess(pid=%d, force=%d)", pid
,
353 * When running as root, cupsd puts child processes in their own process
354 * group. Using "-pid" sends a signal to all processes in the group.
361 return (kill(pid
, SIGKILL
));
363 return (kill(pid
, SIGTERM
));
368 * 'cupsdFinishProcess()' - Finish a process and get its name.
371 const char * /* O - Process name */
372 cupsdFinishProcess(int pid
, /* I - Process ID */
373 char *name
, /* I - Name buffer */
374 size_t namelen
, /* I - Size of name buffer */
375 int *job_id
) /* O - Job ID pointer or NULL */
377 cupsd_proc_t key
, /* Search key */
378 *proc
; /* Matching process */
383 if ((proc
= (cupsd_proc_t
*)cupsArrayFind(process_array
, &key
)) != NULL
)
386 *job_id
= proc
->job_id
;
388 strlcpy(name
, proc
->name
, namelen
);
389 cupsArrayRemove(process_array
, proc
);
397 strlcpy(name
, "unknown", namelen
);
400 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
);
407 * 'cupsdStartProcess()' - Start a process.
410 int /* O - Process ID or 0 */
412 const char *command
, /* I - Full path to command */
413 char *argv
[], /* I - Command-line arguments */
414 char *envp
[], /* I - Environment */
415 int infd
, /* I - Standard input file descriptor */
416 int outfd
, /* I - Standard output file descriptor */
417 int errfd
, /* I - Standard error file descriptor */
418 int backfd
, /* I - Backchannel file descriptor */
419 int sidefd
, /* I - Sidechannel file descriptor */
420 int root
, /* I - Run as root? */
421 void *profile
, /* I - Security profile to use */
422 cupsd_job_t
*job
, /* I - Job associated with process */
423 int *pid
) /* O - Process ID */
425 int i
; /* Looping var */
426 const char *exec_path
= command
; /* Command to be exec'd */
427 char *real_argv
[110], /* Real command-line arguments */
428 cups_exec
[1024]; /* Path to "cups-exec" program */
429 uid_t user
; /* Command UID */
430 cupsd_proc_t
*proc
; /* New process record */
431 #ifdef HAVE_POSIX_SPAWN
432 posix_spawn_file_actions_t actions
; /* Spawn file actions */
433 posix_spawnattr_t attrs
; /* Spawn attributes */
434 char user_str
[16], /* User string */
435 group_str
[16], /* Group string */
436 nice_str
[16]; /* FilterNice string */
437 #elif defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
438 struct sigaction action
; /* POSIX signal handler */
439 #endif /* HAVE_POSIX_SPAWN */
440 #if defined(__APPLE__)
441 char processPath
[1024], /* CFProcessPath environment variable */
442 linkpath
[1024]; /* Link path for symlinks... */
443 int linkbytes
; /* Bytes for link path */
444 #endif /* __APPLE__ */
450 * Figure out the UID for the child process...
461 * Check the permissions of the command we are running...
464 if (_cupsFileCheck(command
, _CUPS_FILE_CHECK_PROGRAM
, !RunUser
,
465 cupsdLogFCMessage
, job
? job
->printer
: NULL
))
468 #if defined(__APPLE__)
472 * Add special voodoo magic for OS X - this allows OS X programs to access
473 * their bundle resources properly...
476 if ((linkbytes
= readlink(command
, linkpath
, sizeof(linkpath
) - 1)) > 0)
479 * Yes, this is a symlink to the actual program, nul-terminate and
483 linkpath
[linkbytes
] = '\0';
485 if (linkpath
[0] == '/')
486 snprintf(processPath
, sizeof(processPath
), "CFProcessPath=%s",
489 snprintf(processPath
, sizeof(processPath
), "CFProcessPath=%s/%s",
490 dirname((char *)command
), linkpath
);
493 snprintf(processPath
, sizeof(processPath
), "CFProcessPath=%s", command
);
495 envp
[0] = processPath
; /* Replace <CFProcessPath> string */
497 #endif /* __APPLE__ */
500 * Use helper program when we have a sandbox profile...
503 #ifndef HAVE_POSIX_SPAWN
505 #endif /* !HAVE_POSIX_SPAWN */
507 snprintf(cups_exec
, sizeof(cups_exec
), "%s/daemon/cups-exec", ServerBin
);
508 snprintf(user_str
, sizeof(user_str
), "%d", user
);
509 snprintf(group_str
, sizeof(group_str
), "%d", Group
);
510 snprintf(nice_str
, sizeof(nice_str
), "%d", FilterNice
);
512 real_argv
[0] = cups_exec
;
513 real_argv
[1] = (char *)"-g";
514 real_argv
[2] = group_str
;
515 real_argv
[3] = (char *)"-n";
516 real_argv
[4] = nice_str
;
517 real_argv
[5] = (char *)"-u";
518 real_argv
[6] = user_str
;
519 real_argv
[7] = profile
;
520 real_argv
[8] = (char *)command
;
523 i
< (int)(sizeof(real_argv
) / sizeof(real_argv
[0]) - 10) && argv
[i
];
525 real_argv
[i
+ 9] = argv
[i
];
527 real_argv
[i
+ 9] = NULL
;
530 exec_path
= cups_exec
;
533 if (LogLevel
== CUPSD_LOG_DEBUG2
)
535 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdStartProcess: Preparing to start \"%s\", arguments:", command
);
537 for (i
= 0; argv
[i
]; i
++)
538 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdStartProcess: argv[%d] = \"%s\"", i
, argv
[i
]);
541 #ifdef HAVE_POSIX_SPAWN
543 * Setup attributes and file actions for the spawn...
546 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdStartProcess: Setting spawn attributes.");
547 posix_spawnattr_init(&attrs
);
548 posix_spawnattr_setflags(&attrs
, POSIX_SPAWN_SETPGROUP
| POSIX_SPAWN_SETSIGDEF
);
550 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdStartProcess: Setting file actions.");
551 posix_spawn_file_actions_init(&actions
);
555 posix_spawn_file_actions_addopen(&actions
, 0, "/dev/null", O_WRONLY
, 0);
557 posix_spawn_file_actions_adddup2(&actions
, infd
, 0);
563 posix_spawn_file_actions_addopen(&actions
, 1, "/dev/null", O_WRONLY
, 0);
565 posix_spawn_file_actions_adddup2(&actions
, outfd
, 1);
571 posix_spawn_file_actions_addopen(&actions
, 2, "/dev/null", O_WRONLY
, 0);
573 posix_spawn_file_actions_adddup2(&actions
, errfd
, 2);
576 if (backfd
!= 3 && backfd
>= 0)
577 posix_spawn_file_actions_adddup2(&actions
, backfd
, 3);
579 if (sidefd
!= 4 && sidefd
>= 0)
580 posix_spawn_file_actions_adddup2(&actions
, sidefd
, 4);
582 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdStartProcess: Calling posix_spawn.");
584 if (posix_spawn(pid
, exec_path
, &actions
, &attrs
, argv
, envp
? envp
: environ
))
586 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to fork %s - %s.", command
, strerror(errno
));
591 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdStartProcess: pid=%d", (int)*pid
);
593 posix_spawn_file_actions_destroy(&actions
);
594 posix_spawnattr_destroy(&attrs
);
598 * Block signals before forking...
603 if ((*pid
= fork()) == 0)
606 * Child process goes here; update stderr as needed...
612 errfd
= open("/dev/null", O_WRONLY
);
622 * Put this process in its own process group so that we can kill any child
623 * processes it creates.
627 if (!RunUser
&& setpgid(0, 0))
630 if (!RunUser
&& setpgrp())
632 # endif /* HAVE_SETPGID */
635 * Update the remaining file descriptors as needed...
641 infd
= open("/dev/null", O_RDONLY
);
653 outfd
= open("/dev/null", O_WRONLY
);
662 if (backfd
!= 3 && backfd
>= 0)
666 fcntl(3, F_SETFL
, O_NDELAY
);
669 if (sidefd
!= 4 && sidefd
>= 0)
673 fcntl(4, F_SETFL
, O_NDELAY
);
677 * Change the priority of the process based on the FilterNice setting.
678 * (this is not done for root processes...)
685 * Reset group membership to just the main one we belong to.
688 if (!RunUser
&& setgid(Group
))
691 if (!RunUser
&& setgroups(1, &Group
))
695 * Change user to something "safe"...
698 if (!RunUser
&& user
&& setuid(user
))
702 * Change umask to restrict permissions on created files...
708 * Unblock signals before doing the exec...
712 sigset(SIGTERM
, SIG_DFL
);
713 sigset(SIGCHLD
, SIG_DFL
);
714 sigset(SIGPIPE
, SIG_DFL
);
715 # elif defined(HAVE_SIGACTION)
716 memset(&action
, 0, sizeof(action
));
718 sigemptyset(&action
.sa_mask
);
719 action
.sa_handler
= SIG_DFL
;
721 sigaction(SIGTERM
, &action
, NULL
);
722 sigaction(SIGCHLD
, &action
, NULL
);
723 sigaction(SIGPIPE
, &action
, NULL
);
725 signal(SIGTERM
, SIG_DFL
);
726 signal(SIGCHLD
, SIG_DFL
);
727 signal(SIGPIPE
, SIG_DFL
);
728 # endif /* HAVE_SIGSET */
730 cupsdReleaseSignals();
733 * Execute the command; if for some reason this doesn't work, log an error
734 * exit with a non-zero value...
738 execve(exec_path
, argv
, envp
);
740 execv(exec_path
, argv
);
747 * Error - couldn't fork a new process!
750 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to fork %s - %s.", command
,
756 cupsdReleaseSignals();
757 #endif /* HAVE_POSIX_SPAWN */
762 process_array
= cupsArrayNew((cups_array_func_t
)compare_procs
, NULL
);
766 if ((proc
= calloc(1, sizeof(cupsd_proc_t
) + strlen(command
))) != NULL
)
769 proc
->job_id
= job
? job
->id
: 0;
770 _cups_strcpy(proc
->name
, command
);
772 cupsArrayAdd(process_array
, proc
);
777 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
778 "cupsdStartProcess(command=\"%s\", argv=%p, envp=%p, "
779 "infd=%d, outfd=%d, errfd=%d, backfd=%d, sidefd=%d, root=%d, "
780 "profile=%p, job=%p(%d), pid=%p) = %d",
781 command
, argv
, envp
, infd
, outfd
, errfd
, backfd
, sidefd
,
782 root
, profile
, job
, job
? job
->id
: 0, pid
, *pid
);
789 * 'compare_procs()' - Compare two processes.
792 static int /* O - Result of comparison */
793 compare_procs(cupsd_proc_t
*a
, /* I - First process */
794 cupsd_proc_t
*b
) /* I - Second process */
796 return (a
->pid
- b
->pid
);
800 #ifdef HAVE_SANDBOX_H
802 * 'cupsd_requote()' - Make a regular-expression version of a string.
805 static char * /* O - Quoted string */
806 cupsd_requote(char *dst
, /* I - Destination buffer */
807 const char *src
, /* I - Source string */
808 size_t dstsize
) /* I - Size of destination buffer */
810 int ch
; /* Current character */
811 char *dstptr
, /* Current position in buffer */
812 *dstend
; /* End of destination buffer */
816 dstend
= dst
+ dstsize
- 2;
818 while (*src
&& dstptr
< dstend
)
822 if (ch
== '/' && !*src
)
823 break; /* Don't add trailing slash */
825 if (strchr(".?*()[]^$\\", ch
))
828 *dstptr
++ = (char)ch
;
835 #endif /* HAVE_SANDBOX_H */