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