]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
75bd9771 | 2 | * "$Id: process.c 7256 2008-01-25 00:48:54Z mike $" |
ef416fc2 | 3 | * |
4 | * Process management routines for the Common UNIX Printing System (CUPS). | |
5 | * | |
bc44d920 | 6 | * Copyright 2007 by Apple Inc. |
f7deaa1a | 7 | * Copyright 1997-2007 by Easy Software Products, all rights reserved. |
ef416fc2 | 8 | * |
9 | * These coded instructions, statements, and computer programs are the | |
bc44d920 | 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 | * Contents: | |
16 | * | |
a4924f6c MS |
17 | * cupsdCreateProfile() - Create an execution profile for a subprocess. |
18 | * cupsdDestroyProfile() - Delete an execution profile. | |
19 | * cupsdEndProcess() - End a process. | |
20 | * cupsdFinishProcess() - Finish a process and get its name. | |
21 | * cupsdStartProcess() - Start a process. | |
22 | * compare_procs() - Compare two processes. | |
23 | * cupsd_requote() - Make a regular-expression version of a string. | |
ef416fc2 | 24 | */ |
25 | ||
26 | /* | |
27 | * Include necessary headers... | |
28 | */ | |
29 | ||
30 | #include "cupsd.h" | |
31 | #include <grp.h> | |
a4924f6c | 32 | #ifdef __APPLE__ |
4400e98d | 33 | # include <libgen.h> |
e53920b9 | 34 | #endif /* __APPLE__ */ |
a4924f6c MS |
35 | #ifdef HAVE_SANDBOX_H |
36 | # define __APPLE_API_PRIVATE | |
37 | # include <sandbox.h> | |
38 | #endif /* HAVE_SANDBOX_H */ | |
ef416fc2 | 39 | |
40 | ||
e00b005a | 41 | /* |
42 | * Process structure... | |
43 | */ | |
44 | ||
45 | typedef struct | |
46 | { | |
47 | int pid; /* Process ID */ | |
48 | char name[1]; /* Name of process */ | |
49 | } cupsd_proc_t; | |
50 | ||
51 | ||
52 | /* | |
53 | * Local globals... | |
54 | */ | |
55 | ||
56 | static cups_array_t *process_array = NULL; | |
57 | ||
58 | ||
59 | /* | |
60 | * Local functions... | |
61 | */ | |
62 | ||
63 | static int compare_procs(cupsd_proc_t *a, cupsd_proc_t *b); | |
a4924f6c MS |
64 | #ifdef HAVE_SANDBOX_H |
65 | static char *cupsd_requote(char *dst, const char *src, size_t dstsize); | |
66 | #endif /* HAVE_SANDBOX_H */ | |
67 | ||
68 | ||
69 | /* | |
70 | * 'cupsdCreateProfile()' - Create an execution profile for a subprocess. | |
71 | */ | |
72 | ||
73 | void * /* O - Profile or NULL on error */ | |
74 | cupsdCreateProfile(int job_id) /* I - Job ID or 0 for none */ | |
75 | { | |
76 | #ifdef HAVE_SANDBOX_H | |
77 | cups_file_t *fp; /* File pointer */ | |
78 | char profile[1024], /* File containing the profile */ | |
79 | cache[1024], /* Quoted CacheDir */ | |
80 | request[1024], /* Quoted RequestRoot */ | |
81 | root[1024], /* Quoted ServerRoot */ | |
82 | temp[1024]; /* Quoted TempDir */ | |
83 | ||
84 | ||
85 | if ((fp = cupsTempFile2(profile, sizeof(profile))) == NULL) | |
86 | { | |
87 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to create security profile: %s", | |
88 | strerror(errno)); | |
89 | return (NULL); | |
90 | } | |
91 | ||
92 | cupsd_requote(cache, CacheDir, sizeof(cache)); | |
93 | cupsd_requote(request, RequestRoot, sizeof(request)); | |
94 | cupsd_requote(root, ServerRoot, sizeof(root)); | |
95 | cupsd_requote(temp, TempDir, sizeof(temp)); | |
96 | ||
97 | cupsFilePuts(fp, "(version 1)\n"); | |
98 | cupsFilePuts(fp, "(debug deny)\n"); | |
99 | cupsFilePuts(fp, "(allow default)\n"); | |
100 | cupsFilePrintf(fp, | |
101 | "(deny file-write* file-read-data file-read-metadata\n" | |
5bd77a73 | 102 | " (regex #\"^%s/\"))\n", request); |
a4924f6c MS |
103 | cupsFilePrintf(fp, |
104 | "(deny file-write*\n" | |
105 | " (regex #\"^%s\" #\"^/private/etc\" #\"^/usr/local/etc\" " | |
5bd77a73 | 106 | "#\"^/Library\" #\"^/System\" #\"^/Users\"))\n", root); |
a4924f6c MS |
107 | cupsFilePrintf(fp, |
108 | "(allow file-write* file-read-data file-read-metadata\n" | |
5a738aea MS |
109 | " (regex #\"^%s$\" #\"^%s/\" #\"^%s$\" #\"^%s/\" " |
110 | "#\"^/Library/Caches/\"))\n", | |
a4924f6c MS |
111 | temp, temp, cache, cache); |
112 | if (job_id) | |
113 | cupsFilePrintf(fp, | |
114 | "(allow file-read-data file-read-metadata\n" | |
115 | " (regex #\"^%s/([ac]%05d|d%05d-[0-9][0-9][0-9])$\"))\n", | |
5bd77a73 | 116 | request, job_id, job_id); |
a4924f6c MS |
117 | |
118 | cupsFileClose(fp); | |
119 | ||
5bd77a73 MS |
120 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdCreateProfile(job_id=%d) = \"%s\"", |
121 | job_id, profile); | |
a4924f6c MS |
122 | return ((void *)strdup(profile)); |
123 | #else | |
124 | ||
125 | return (NULL); | |
126 | #endif /* HAVE_SANDBOX_H */ | |
127 | } | |
128 | ||
129 | ||
130 | /* | |
131 | * 'cupsdDestroyProfile()' - Delete an execution profile. | |
132 | */ | |
133 | ||
134 | void | |
135 | cupsdDestroyProfile(void *profile) /* I - Profile */ | |
136 | { | |
137 | #ifdef HAVE_SANDBOX_H | |
138 | if (profile) | |
139 | { | |
5bd77a73 MS |
140 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdDeleteProfile(profile=\"%s\")", |
141 | (char *)profile); | |
a4924f6c MS |
142 | unlink((char *)profile); |
143 | free(profile); | |
144 | } | |
145 | #endif /* HAVE_SANDBOX_H */ | |
146 | } | |
e00b005a | 147 | |
148 | ||
ef416fc2 | 149 | /* |
150 | * 'cupsdEndProcess()' - End a process. | |
151 | */ | |
152 | ||
153 | int /* O - 0 on success, -1 on failure */ | |
154 | cupsdEndProcess(int pid, /* I - Process ID */ | |
155 | int force) /* I - Force child to die */ | |
156 | { | |
157 | if (force) | |
158 | return (kill(pid, SIGKILL)); | |
159 | else | |
160 | return (kill(pid, SIGTERM)); | |
161 | } | |
162 | ||
163 | ||
e00b005a | 164 | /* |
165 | * 'cupsdFinishProcess()' - Finish a process and get its name. | |
166 | */ | |
167 | ||
168 | const char * /* O - Process name */ | |
169 | cupsdFinishProcess(int pid, /* I - Process ID */ | |
170 | char *name, /* I - Name buffer */ | |
171 | int namelen) /* I - Size of name buffer */ | |
172 | { | |
173 | cupsd_proc_t key, /* Search key */ | |
174 | *proc; /* Matching process */ | |
175 | ||
176 | ||
177 | key.pid = pid; | |
178 | ||
179 | if ((proc = (cupsd_proc_t *)cupsArrayFind(process_array, &key)) != NULL) | |
180 | { | |
181 | strlcpy(name, proc->name, namelen); | |
182 | cupsArrayRemove(process_array, proc); | |
183 | free(proc); | |
184 | ||
185 | return (name); | |
186 | } | |
187 | else | |
188 | return ("unknown"); | |
189 | } | |
190 | ||
191 | ||
ef416fc2 | 192 | /* |
193 | * 'cupsdStartProcess()' - Start a process. | |
194 | */ | |
195 | ||
196 | int /* O - Process ID or 0 */ | |
197 | cupsdStartProcess( | |
198 | const char *command, /* I - Full path to command */ | |
199 | char *argv[], /* I - Command-line arguments */ | |
200 | char *envp[], /* I - Environment */ | |
201 | int infd, /* I - Standard input file descriptor */ | |
202 | int outfd, /* I - Standard output file descriptor */ | |
203 | int errfd, /* I - Standard error file descriptor */ | |
204 | int backfd, /* I - Backchannel file descriptor */ | |
f7deaa1a | 205 | int sidefd, /* I - Sidechannel file descriptor */ |
ef416fc2 | 206 | int root, /* I - Run as root? */ |
a4924f6c | 207 | void *profile, /* I - Security profile to use */ |
ef416fc2 | 208 | int *pid) /* O - Process ID */ |
209 | { | |
e00b005a | 210 | cupsd_proc_t *proc; /* New process record */ |
ef416fc2 | 211 | #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET) |
e00b005a | 212 | struct sigaction action; /* POSIX signal handler */ |
ef416fc2 | 213 | #endif /* HAVE_SIGACTION && !HAVE_SIGSET */ |
e53920b9 | 214 | #if defined(__APPLE__) |
e00b005a | 215 | char processPath[1024], /* CFProcessPath environment variable */ |
216 | linkpath[1024]; /* Link path for symlinks... */ | |
217 | int linkbytes; /* Bytes for link path */ | |
e53920b9 | 218 | #endif /* __APPLE__ */ |
ef416fc2 | 219 | |
220 | ||
221 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
222 | "cupsdStartProcess(\"%s\", %p, %p, %d, %d, %d)", | |
223 | command, argv, envp, infd, outfd, errfd); | |
224 | ||
76cd9e37 MS |
225 | if (access(command, X_OK)) |
226 | { | |
227 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to execute %s: %s", command, | |
228 | strerror(errno)); | |
7dfedb92 | 229 | *pid = 0; |
76cd9e37 MS |
230 | return (0); |
231 | } | |
232 | ||
e53920b9 | 233 | #if defined(__APPLE__) |
234 | if (envp) | |
e00b005a | 235 | { |
236 | /* | |
e53920b9 | 237 | * Add special voodoo magic for MacOS X - this allows MacOS X |
238 | * programs to access their bundle resources properly... | |
e00b005a | 239 | */ |
240 | ||
e53920b9 | 241 | if ((linkbytes = readlink(command, linkpath, sizeof(linkpath) - 1)) > 0) |
242 | { | |
243 | /* | |
244 | * Yes, this is a symlink to the actual program, nul-terminate and | |
245 | * use it... | |
246 | */ | |
247 | ||
248 | linkpath[linkbytes] = '\0'; | |
e00b005a | 249 | |
e53920b9 | 250 | if (linkpath[0] == '/') |
251 | snprintf(processPath, sizeof(processPath), "CFProcessPath=%s", | |
252 | linkpath); | |
253 | else | |
254 | snprintf(processPath, sizeof(processPath), "CFProcessPath=%s/%s", | |
f7deaa1a | 255 | dirname((char *)command), linkpath); |
e53920b9 | 256 | } |
e00b005a | 257 | else |
e53920b9 | 258 | snprintf(processPath, sizeof(processPath), "CFProcessPath=%s", command); |
bd7854cb | 259 | |
e53920b9 | 260 | envp[0] = processPath; /* Replace <CFProcessPath> string */ |
261 | } | |
262 | #endif /* __APPLE__ */ | |
e00b005a | 263 | |
ef416fc2 | 264 | /* |
265 | * Block signals before forking... | |
266 | */ | |
267 | ||
268 | cupsdHoldSignals(); | |
269 | ||
270 | if ((*pid = fork()) == 0) | |
271 | { | |
272 | /* | |
273 | * Child process goes here... | |
274 | * | |
275 | * Update stdin/stdout/stderr as needed... | |
276 | */ | |
277 | ||
278 | if (infd != 0) | |
279 | { | |
280 | close(0); | |
281 | if (infd > 0) | |
282 | dup(infd); | |
283 | else | |
284 | open("/dev/null", O_RDONLY); | |
285 | } | |
286 | if (outfd != 1) | |
287 | { | |
288 | close(1); | |
289 | if (outfd > 0) | |
290 | dup(outfd); | |
291 | else | |
292 | open("/dev/null", O_WRONLY); | |
293 | } | |
294 | if (errfd != 2) | |
295 | { | |
296 | close(2); | |
297 | if (errfd > 0) | |
298 | dup(errfd); | |
299 | else | |
300 | open("/dev/null", O_WRONLY); | |
301 | } | |
302 | if (backfd != 3) | |
303 | { | |
304 | close(3); | |
305 | if (backfd > 0) | |
306 | dup(backfd); | |
307 | else | |
308 | open("/dev/null", O_RDWR); | |
309 | fcntl(3, F_SETFL, O_NDELAY); | |
310 | } | |
f7deaa1a | 311 | if (sidefd != 4 && sidefd > 0) |
312 | { | |
313 | close(4); | |
314 | dup(sidefd); | |
315 | fcntl(4, F_SETFL, O_NDELAY); | |
316 | } | |
ef416fc2 | 317 | |
318 | /* | |
319 | * Change the priority of the process based on the FilterNice setting. | |
5bd77a73 | 320 | * (this is not done for root processes...) |
ef416fc2 | 321 | */ |
322 | ||
323 | if (!root) | |
324 | nice(FilterNice); | |
325 | ||
5bd77a73 MS |
326 | #ifdef HAVE_SANDBOX_H |
327 | /* | |
328 | * Run in a separate security profile... | |
329 | */ | |
330 | ||
331 | if (profile) | |
332 | { | |
333 | char *error = NULL; /* Sandbox error, if any */ | |
334 | ||
335 | if (sandbox_init((char *)profile, SANDBOX_NAMED_EXTERNAL, &error)) | |
336 | { | |
337 | fprintf(stderr, "ERROR: sandbox_init failed: %s (%s)\n", error, | |
338 | strerror(errno)); | |
339 | sandbox_free_error(error); | |
340 | } | |
341 | } | |
342 | #endif /* HAVE_SANDBOX_H */ | |
343 | ||
ef416fc2 | 344 | /* |
345 | * Change user to something "safe"... | |
346 | */ | |
347 | ||
348 | if (!root && !RunUser) | |
349 | { | |
350 | /* | |
351 | * Running as root, so change to non-priviledged user... | |
352 | */ | |
353 | ||
354 | if (setgid(Group)) | |
e00b005a | 355 | exit(errno); |
ef416fc2 | 356 | |
357 | if (setgroups(1, &Group)) | |
e00b005a | 358 | exit(errno); |
ef416fc2 | 359 | |
360 | if (setuid(User)) | |
361 | exit(errno); | |
362 | } | |
363 | else | |
364 | { | |
365 | /* | |
366 | * Reset group membership to just the main one we belong to. | |
367 | */ | |
368 | ||
e00b005a | 369 | setgid(Group); |
ef416fc2 | 370 | setgroups(1, &Group); |
371 | } | |
372 | ||
373 | /* | |
374 | * Change umask to restrict permissions on created files... | |
375 | */ | |
376 | ||
377 | umask(077); | |
378 | ||
379 | /* | |
380 | * Unblock signals before doing the exec... | |
381 | */ | |
382 | ||
383 | #ifdef HAVE_SIGSET | |
384 | sigset(SIGTERM, SIG_DFL); | |
385 | sigset(SIGCHLD, SIG_DFL); | |
386 | #elif defined(HAVE_SIGACTION) | |
387 | memset(&action, 0, sizeof(action)); | |
388 | ||
389 | sigemptyset(&action.sa_mask); | |
390 | action.sa_handler = SIG_DFL; | |
391 | ||
392 | sigaction(SIGTERM, &action, NULL); | |
393 | sigaction(SIGCHLD, &action, NULL); | |
394 | #else | |
395 | signal(SIGTERM, SIG_DFL); | |
396 | signal(SIGCHLD, SIG_DFL); | |
397 | #endif /* HAVE_SIGSET */ | |
398 | ||
399 | cupsdReleaseSignals(); | |
400 | ||
401 | /* | |
402 | * Execute the command; if for some reason this doesn't work, | |
403 | * return the error code... | |
404 | */ | |
405 | ||
406 | if (envp) | |
407 | execve(command, argv, envp); | |
408 | else | |
409 | execv(command, argv); | |
410 | ||
411 | perror(command); | |
412 | ||
413 | exit(errno); | |
414 | } | |
415 | else if (*pid < 0) | |
416 | { | |
417 | /* | |
418 | * Error - couldn't fork a new process! | |
419 | */ | |
420 | ||
421 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to fork %s - %s.", command, | |
422 | strerror(errno)); | |
423 | ||
424 | *pid = 0; | |
425 | } | |
e00b005a | 426 | else |
427 | { | |
428 | if (!process_array) | |
429 | process_array = cupsArrayNew((cups_array_func_t)compare_procs, NULL); | |
430 | ||
431 | if (process_array) | |
432 | { | |
433 | if ((proc = calloc(1, sizeof(cupsd_proc_t) + strlen(command))) != NULL) | |
434 | { | |
435 | proc->pid = *pid; | |
436 | strcpy(proc->name, command); | |
437 | ||
438 | cupsArrayAdd(process_array, proc); | |
439 | } | |
440 | } | |
441 | } | |
ef416fc2 | 442 | |
443 | cupsdReleaseSignals(); | |
444 | ||
445 | return (*pid); | |
446 | } | |
447 | ||
448 | ||
449 | /* | |
e00b005a | 450 | * 'compare_procs()' - Compare two processes. |
451 | */ | |
452 | ||
453 | static int /* O - Result of comparison */ | |
454 | compare_procs(cupsd_proc_t *a, /* I - First process */ | |
455 | cupsd_proc_t *b) /* I - Second process */ | |
456 | { | |
457 | return (a->pid - b->pid); | |
458 | } | |
459 | ||
460 | ||
a4924f6c MS |
461 | #ifdef HAVE_SANDBOX_H |
462 | /* | |
463 | * 'cupsd_requote()' - Make a regular-expression version of a string. | |
464 | */ | |
465 | ||
466 | static char * /* O - Quoted string */ | |
467 | cupsd_requote(char *dst, /* I - Destination buffer */ | |
468 | const char *src, /* I - Source string */ | |
469 | size_t dstsize) /* I - Size of destination buffer */ | |
470 | { | |
471 | int ch; /* Current character */ | |
472 | char *dstptr, /* Current position in buffer */ | |
473 | *dstend; /* End of destination buffer */ | |
474 | ||
475 | ||
476 | dstptr = dst; | |
477 | dstend = dst + dstsize - 2; | |
478 | ||
479 | while (*src && dstptr < dstend) | |
480 | { | |
481 | ch = *src++; | |
482 | ||
483 | if (strchr(".?*()[]^$\\", ch)) | |
484 | *dstptr++ = '\\'; | |
485 | ||
486 | *dstptr++ = ch; | |
487 | } | |
488 | ||
489 | *dstptr = '\0'; | |
490 | ||
491 | return (dst); | |
492 | } | |
493 | #endif /* HAVE_SANDBOX_H */ | |
494 | ||
495 | ||
e00b005a | 496 | /* |
75bd9771 | 497 | * End of "$Id: process.c 7256 2008-01-25 00:48:54Z mike $". |
ef416fc2 | 498 | */ |