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