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