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