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