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/db/\""
174 " #\"^/private/var/folders/\""
175 " #\"^/Library/Application Support/\""
176 " #\"^/Library/Caches/\""
177 " #\"^/Library/Preferences/\""
178 " #\"^/Users/Shared/\""
180 temp
, temp
, cache
, cache
);
181 /* Read common folders */
183 "(allow file-read-data file-read-metadata\n"
184 " (literal \"/private/etc/services\")\n"
186 " #\"^/AppleInternal$\""
187 " #\"^/AppleInternal/\""
188 " #\"^/bin$\"" /* /bin */
189 " #\"^/bin/\"" /* /bin/... */
190 " #\"^/usr/bin$\"" /* /usr/bin */
191 " #\"^/usr/bin/\"" /* /usr/bin/... */
192 " #\"^/usr/libexec/cups$\"" /* /usr/libexec/cups */
193 " #\"^/usr/libexec/cups/\"" /* /usr/libexec/cups/... */
194 " #\"^/usr/sbin$\"" /* /usr/sbin */
195 " #\"^/usr/sbin/\"" /* /usr/sbin/... */
196 " #\"^/Library/Caches$\""
197 " #\"^/Library/Fonts$\""
198 " #\"^/Library/Fonts/\""
199 " #\"^/Library/Printers$\""
200 " #\"^/Library/Printers/.*$\""
201 " #\"^%s/Library$\"" /* RequestRoot/Library */
202 " #\"^%s/Library/\"" /* RequestRoot/Library/... */
203 " #\"^%s$\"" /* ServerBin */
204 " #\"^%s/\"" /* ServerBin/... */
205 " #\"^%s$\"" /* ServerRoot */
206 " #\"^%s/\"" /* ServerRoot/... */
208 request
, request
, bin
, bin
, root
, root
);
209 if (Sandboxing
== CUPSD_SANDBOXING_RELAXED
)
211 /* Limited write access to /Library/Printers/... */
213 "(allow file-write*\n"
215 " #\"^/Library/Printers/.*/\""
218 "(deny file-write*\n"
220 " #\"^/Library/Printers/PPDs$\""
221 " #\"^/Library/Printers/PPDs/\""
222 " #\"^/Library/Printers/PPD Plugins$\""
223 " #\"^/Library/Printers/PPD Plugins/\""
226 /* Allow execution of child processes */
227 cupsFilePuts(fp
, "(allow process-fork)\n");
229 "(allow process-exec\n"
231 " #\"^/bin/\"" /* /bin/... */
232 " #\"^/usr/bin/\"" /* /usr/bin/... */
233 " #\"^/usr/libexec/cups/\"" /* /usr/libexec/cups/... */
234 " #\"^/usr/sbin/\"" /* /usr/sbin/... */
235 " #\"^%s/\"" /* ServerBin/... */
236 " #\"^/Library/Printers/.*/\""
239 if (RunUser
&& getenv("CUPS_TESTROOT"))
241 /* Allow source directory access in "make test" environment */
242 char testroot
[1024]; /* Root directory of test files */
244 cupsd_requote(testroot
, getenv("CUPS_TESTROOT"), sizeof(testroot
));
247 "(allow file-write* file-read-data file-read-metadata\n"
249 " #\"^%s$\"" /* CUPS_TESTROOT */
250 " #\"^%s/\"" /* CUPS_TESTROOT/... */
254 "(allow process-exec\n"
256 " #\"^%s/\"" /* CUPS_TESTROOT/... */
262 /* Allow job filters to read the current job files... */
264 "(allow file-read-data file-read-metadata\n"
265 " (regex #\"^%s/([ac]%05d|d%05d-[0-9][0-9][0-9])$\"))\n",
266 request
, job_id
, job_id
);
270 /* Allow email notifications from notifiers... */
272 "(allow process-exec\n"
273 " (literal \"/usr/sbin/sendmail\")\n"
274 " (with no-sandbox))\n");
276 /* Allow outbound networking to local mDNSResponder and cupsd */
277 cupsFilePuts(fp
, "(allow network-outbound"
278 "\n (literal \"/private/var/run/mDNSResponder\")");
279 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
281 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
283 if (httpAddrFamily(&(lis
->address
)) == AF_LOCAL
)
285 httpAddrString(&(lis
->address
), domain
, sizeof(domain
));
286 cupsFilePrintf(fp
, "\n (literal \"%s\")", domain
);
289 if (allow_networking
)
291 /* Allow TCP and UDP networking off the machine... */
292 cupsFilePuts(fp
, "\n (remote tcp))\n");
293 cupsFilePuts(fp
, "(allow network*\n"
294 " (local udp \"*:*\")\n"
295 " (remote udp \"*:*\"))\n");
297 /* Also allow access to Bluetooth, USB, and SMB */
298 cupsFilePuts(fp
, "(allow iokit-open)\n");
299 cupsFilePuts(fp
, "(allow file-write* file-read-data file-read-metadata file-ioctl\n"
300 " (regex #\"^/dev/nsmb[0-9]+$\"))\n");
304 /* Only allow SNMP (UDP) off the machine... */
305 cupsFilePuts(fp
, ")\n");
306 cupsFilePuts(fp
, "(allow network-outbound\n"
307 " (remote udp \"*:161\"))\n");
308 cupsFilePuts(fp
, "(allow network-inbound\n"
309 " (local udp \"localhost:*\"))\n");
313 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdCreateProfile(job_id=%d,allow_networking=%d) = \"%s\"", job_id
, allow_networking
, profile
);
314 return ((void *)strdup(profile
));
317 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdCreateProfile(job_id=%d, allow_networking=%d) = NULL", job_id
, allow_networking
);
320 #endif /* HAVE_SANDBOX_H */
325 * 'cupsdDestroyProfile()' - Delete an execution profile.
329 cupsdDestroyProfile(void *profile
) /* I - Profile */
331 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdDeleteProfile(profile=\"%s\")",
332 profile
? (char *)profile
: "(null)");
334 #ifdef HAVE_SANDBOX_H
337 unlink((char *)profile
);
340 #endif /* HAVE_SANDBOX_H */
345 * 'cupsdEndProcess()' - End a process.
348 int /* O - 0 on success, -1 on failure */
349 cupsdEndProcess(int pid
, /* I - Process ID */
350 int force
) /* I - Force child to die */
352 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdEndProcess(pid=%d, force=%d)", pid
,
361 * When running as root, cupsd puts child processes in their own process
362 * group. Using "-pid" sends a signal to all processes in the group.
369 return (kill(pid
, SIGKILL
));
371 return (kill(pid
, SIGTERM
));
376 * 'cupsdFinishProcess()' - Finish a process and get its name.
379 const char * /* O - Process name */
380 cupsdFinishProcess(int pid
, /* I - Process ID */
381 char *name
, /* I - Name buffer */
382 size_t namelen
, /* I - Size of name buffer */
383 int *job_id
) /* O - Job ID pointer or NULL */
385 cupsd_proc_t key
, /* Search key */
386 *proc
; /* Matching process */
391 if ((proc
= (cupsd_proc_t
*)cupsArrayFind(process_array
, &key
)) != NULL
)
394 *job_id
= proc
->job_id
;
396 strlcpy(name
, proc
->name
, namelen
);
397 cupsArrayRemove(process_array
, proc
);
405 strlcpy(name
, "unknown", namelen
);
408 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
);
415 * 'cupsdStartProcess()' - Start a process.
418 int /* O - Process ID or 0 */
420 const char *command
, /* I - Full path to command */
421 char *argv
[], /* I - Command-line arguments */
422 char *envp
[], /* I - Environment */
423 int infd
, /* I - Standard input file descriptor */
424 int outfd
, /* I - Standard output file descriptor */
425 int errfd
, /* I - Standard error file descriptor */
426 int backfd
, /* I - Backchannel file descriptor */
427 int sidefd
, /* I - Sidechannel file descriptor */
428 int root
, /* I - Run as root? */
429 void *profile
, /* I - Security profile to use */
430 cupsd_job_t
*job
, /* I - Job associated with process */
431 int *pid
) /* O - Process ID */
433 int i
; /* Looping var */
434 const char *exec_path
= command
; /* Command to be exec'd */
435 char *real_argv
[110], /* Real command-line arguments */
436 cups_exec
[1024]; /* Path to "cups-exec" program */
437 uid_t user
; /* Command UID */
438 cupsd_proc_t
*proc
; /* New process record */
439 #ifdef HAVE_POSIX_SPAWN
440 posix_spawn_file_actions_t actions
; /* Spawn file actions */
441 posix_spawnattr_t attrs
; /* Spawn attributes */
442 char user_str
[16], /* User string */
443 group_str
[16], /* Group string */
444 nice_str
[16]; /* FilterNice string */
445 #elif defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
446 struct sigaction action
; /* POSIX signal handler */
447 #endif /* HAVE_POSIX_SPAWN */
448 #if defined(__APPLE__)
449 char processPath
[1024], /* CFProcessPath environment variable */
450 linkpath
[1024]; /* Link path for symlinks... */
451 int linkbytes
; /* Bytes for link path */
452 #endif /* __APPLE__ */
458 * Figure out the UID for the child process...
469 * Check the permissions of the command we are running...
472 if (_cupsFileCheck(command
, _CUPS_FILE_CHECK_PROGRAM
, !RunUser
,
473 cupsdLogFCMessage
, job
? job
->printer
: NULL
))
476 #if defined(__APPLE__)
480 * Add special voodoo magic for OS X - this allows OS X programs to access
481 * their bundle resources properly...
484 if ((linkbytes
= readlink(command
, linkpath
, sizeof(linkpath
) - 1)) > 0)
487 * Yes, this is a symlink to the actual program, nul-terminate and
491 linkpath
[linkbytes
] = '\0';
493 if (linkpath
[0] == '/')
494 snprintf(processPath
, sizeof(processPath
), "CFProcessPath=%s",
497 snprintf(processPath
, sizeof(processPath
), "CFProcessPath=%s/%s",
498 dirname((char *)command
), linkpath
);
501 snprintf(processPath
, sizeof(processPath
), "CFProcessPath=%s", command
);
503 envp
[0] = processPath
; /* Replace <CFProcessPath> string */
505 #endif /* __APPLE__ */
508 * Use helper program when we have a sandbox profile...
511 #ifndef HAVE_POSIX_SPAWN
513 #endif /* !HAVE_POSIX_SPAWN */
515 snprintf(cups_exec
, sizeof(cups_exec
), "%s/daemon/cups-exec", ServerBin
);
516 snprintf(user_str
, sizeof(user_str
), "%d", user
);
517 snprintf(group_str
, sizeof(group_str
), "%d", Group
);
518 snprintf(nice_str
, sizeof(nice_str
), "%d", FilterNice
);
520 real_argv
[0] = cups_exec
;
521 real_argv
[1] = (char *)"-g";
522 real_argv
[2] = group_str
;
523 real_argv
[3] = (char *)"-n";
524 real_argv
[4] = nice_str
;
525 real_argv
[5] = (char *)"-u";
526 real_argv
[6] = user_str
;
527 real_argv
[7] = profile
;
528 real_argv
[8] = (char *)command
;
531 i
< (int)(sizeof(real_argv
) / sizeof(real_argv
[0]) - 10) && argv
[i
];
533 real_argv
[i
+ 9] = argv
[i
];
535 real_argv
[i
+ 9] = NULL
;
538 exec_path
= cups_exec
;
541 if (LogLevel
== CUPSD_LOG_DEBUG2
)
543 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdStartProcess: Preparing to start \"%s\", arguments:", command
);
545 for (i
= 0; argv
[i
]; i
++)
546 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdStartProcess: argv[%d] = \"%s\"", i
, argv
[i
]);
549 #ifdef HAVE_POSIX_SPAWN
551 * Setup attributes and file actions for the spawn...
554 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdStartProcess: Setting spawn attributes.");
555 posix_spawnattr_init(&attrs
);
556 posix_spawnattr_setflags(&attrs
, POSIX_SPAWN_SETPGROUP
| POSIX_SPAWN_SETSIGDEF
);
558 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdStartProcess: Setting file actions.");
559 posix_spawn_file_actions_init(&actions
);
563 posix_spawn_file_actions_addopen(&actions
, 0, "/dev/null", O_WRONLY
, 0);
565 posix_spawn_file_actions_adddup2(&actions
, infd
, 0);
571 posix_spawn_file_actions_addopen(&actions
, 1, "/dev/null", O_WRONLY
, 0);
573 posix_spawn_file_actions_adddup2(&actions
, outfd
, 1);
579 posix_spawn_file_actions_addopen(&actions
, 2, "/dev/null", O_WRONLY
, 0);
581 posix_spawn_file_actions_adddup2(&actions
, errfd
, 2);
584 if (backfd
!= 3 && backfd
>= 0)
585 posix_spawn_file_actions_adddup2(&actions
, backfd
, 3);
587 if (sidefd
!= 4 && sidefd
>= 0)
588 posix_spawn_file_actions_adddup2(&actions
, sidefd
, 4);
590 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdStartProcess: Calling posix_spawn.");
592 if (posix_spawn(pid
, exec_path
, &actions
, &attrs
, argv
, envp
? envp
: environ
))
594 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to fork %s - %s.", command
, strerror(errno
));
599 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdStartProcess: pid=%d", (int)*pid
);
601 posix_spawn_file_actions_destroy(&actions
);
602 posix_spawnattr_destroy(&attrs
);
606 * Block signals before forking...
611 if ((*pid
= fork()) == 0)
614 * Child process goes here; update stderr as needed...
620 errfd
= open("/dev/null", O_WRONLY
);
630 * Put this process in its own process group so that we can kill any child
631 * processes it creates.
635 if (!RunUser
&& setpgid(0, 0))
638 if (!RunUser
&& setpgrp())
640 # endif /* HAVE_SETPGID */
643 * Update the remaining file descriptors as needed...
649 infd
= open("/dev/null", O_RDONLY
);
661 outfd
= open("/dev/null", O_WRONLY
);
670 if (backfd
!= 3 && backfd
>= 0)
674 fcntl(3, F_SETFL
, O_NDELAY
);
677 if (sidefd
!= 4 && sidefd
>= 0)
681 fcntl(4, F_SETFL
, O_NDELAY
);
685 * Change the priority of the process based on the FilterNice setting.
686 * (this is not done for root processes...)
693 * Reset group membership to just the main one we belong to.
696 if (!RunUser
&& setgid(Group
))
699 if (!RunUser
&& setgroups(1, &Group
))
703 * Change user to something "safe"...
706 if (!RunUser
&& user
&& setuid(user
))
710 * Change umask to restrict permissions on created files...
716 * Unblock signals before doing the exec...
720 sigset(SIGTERM
, SIG_DFL
);
721 sigset(SIGCHLD
, SIG_DFL
);
722 sigset(SIGPIPE
, SIG_DFL
);
723 # elif defined(HAVE_SIGACTION)
724 memset(&action
, 0, sizeof(action
));
726 sigemptyset(&action
.sa_mask
);
727 action
.sa_handler
= SIG_DFL
;
729 sigaction(SIGTERM
, &action
, NULL
);
730 sigaction(SIGCHLD
, &action
, NULL
);
731 sigaction(SIGPIPE
, &action
, NULL
);
733 signal(SIGTERM
, SIG_DFL
);
734 signal(SIGCHLD
, SIG_DFL
);
735 signal(SIGPIPE
, SIG_DFL
);
736 # endif /* HAVE_SIGSET */
738 cupsdReleaseSignals();
741 * Execute the command; if for some reason this doesn't work, log an error
742 * exit with a non-zero value...
746 execve(exec_path
, argv
, envp
);
748 execv(exec_path
, argv
);
755 * Error - couldn't fork a new process!
758 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to fork %s - %s.", command
,
764 cupsdReleaseSignals();
765 #endif /* HAVE_POSIX_SPAWN */
770 process_array
= cupsArrayNew((cups_array_func_t
)compare_procs
, NULL
);
774 if ((proc
= calloc(1, sizeof(cupsd_proc_t
) + strlen(command
))) != NULL
)
777 proc
->job_id
= job
? job
->id
: 0;
778 _cups_strcpy(proc
->name
, command
);
780 cupsArrayAdd(process_array
, proc
);
785 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
786 "cupsdStartProcess(command=\"%s\", argv=%p, envp=%p, "
787 "infd=%d, outfd=%d, errfd=%d, backfd=%d, sidefd=%d, root=%d, "
788 "profile=%p, job=%p(%d), pid=%p) = %d",
789 command
, argv
, envp
, infd
, outfd
, errfd
, backfd
, sidefd
,
790 root
, profile
, job
, job
? job
->id
: 0, pid
, *pid
);
797 * 'compare_procs()' - Compare two processes.
800 static int /* O - Result of comparison */
801 compare_procs(cupsd_proc_t
*a
, /* I - First process */
802 cupsd_proc_t
*b
) /* I - Second process */
804 return (a
->pid
- b
->pid
);
808 #ifdef HAVE_SANDBOX_H
810 * 'cupsd_requote()' - Make a regular-expression version of a string.
813 static char * /* O - Quoted string */
814 cupsd_requote(char *dst
, /* I - Destination buffer */
815 const char *src
, /* I - Source string */
816 size_t dstsize
) /* I - Size of destination buffer */
818 int ch
; /* Current character */
819 char *dstptr
, /* Current position in buffer */
820 *dstend
; /* End of destination buffer */
824 dstend
= dst
+ dstsize
- 2;
826 while (*src
&& dstptr
< dstend
)
830 if (ch
== '/' && !*src
)
831 break; /* Don't add trailing slash */
833 if (strchr(".?*()[]^$\\", ch
))
836 *dstptr
++ = (char)ch
;
843 #endif /* HAVE_SANDBOX_H */