]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
75bd9771 | 2 | * "$Id: process.c 7256 2008-01-25 00:48:54Z mike $" |
ef416fc2 | 3 | * |
cc754834 | 4 | * Process management routines for the CUPS scheduler. |
ef416fc2 | 5 | * |
5a6b583a | 6 | * Copyright 2007-2010 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> |
6d2f911b | 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 | { | |
b9faaae1 MS |
47 | int pid, /* Process ID */ |
48 | job_id; /* Job associated with process */ | |
e00b005a | 49 | char name[1]; /* Name of process */ |
50 | } cupsd_proc_t; | |
51 | ||
52 | ||
53 | /* | |
54 | * Local globals... | |
55 | */ | |
56 | ||
57 | static cups_array_t *process_array = NULL; | |
58 | ||
59 | ||
60 | /* | |
61 | * Local functions... | |
62 | */ | |
63 | ||
64 | static int compare_procs(cupsd_proc_t *a, cupsd_proc_t *b); | |
a4924f6c MS |
65 | #ifdef HAVE_SANDBOX_H |
66 | static char *cupsd_requote(char *dst, const char *src, size_t dstsize); | |
67 | #endif /* HAVE_SANDBOX_H */ | |
68 | ||
69 | ||
70 | /* | |
71 | * 'cupsdCreateProfile()' - Create an execution profile for a subprocess. | |
72 | */ | |
73 | ||
74 | void * /* O - Profile or NULL on error */ | |
75 | cupsdCreateProfile(int job_id) /* I - Job ID or 0 for none */ | |
76 | { | |
77 | #ifdef HAVE_SANDBOX_H | |
78 | cups_file_t *fp; /* File pointer */ | |
79 | char profile[1024], /* File containing the profile */ | |
80 | cache[1024], /* Quoted CacheDir */ | |
81 | request[1024], /* Quoted RequestRoot */ | |
82 | root[1024], /* Quoted ServerRoot */ | |
83 | temp[1024]; /* Quoted TempDir */ | |
84 | ||
85 | ||
1340db2d | 86 | if (!UseProfiles || RunUser) |
b9faaae1 MS |
87 | { |
88 | /* | |
89 | * Only use sandbox profiles as root... | |
90 | */ | |
91 | ||
92 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdCreateProfile(job_id=%d) = NULL", | |
93 | job_id); | |
94 | ||
95 | return (NULL); | |
96 | } | |
97 | ||
a4924f6c MS |
98 | if ((fp = cupsTempFile2(profile, sizeof(profile))) == NULL) |
99 | { | |
b9faaae1 MS |
100 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdCreateProfile(job_id=%d) = NULL", |
101 | job_id); | |
a4924f6c MS |
102 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to create security profile: %s", |
103 | strerror(errno)); | |
104 | return (NULL); | |
105 | } | |
106 | ||
107 | cupsd_requote(cache, CacheDir, sizeof(cache)); | |
108 | cupsd_requote(request, RequestRoot, sizeof(request)); | |
109 | cupsd_requote(root, ServerRoot, sizeof(root)); | |
110 | cupsd_requote(temp, TempDir, sizeof(temp)); | |
111 | ||
112 | cupsFilePuts(fp, "(version 1)\n"); | |
cc754834 MS |
113 | if (LogLevel >= CUPSD_LOG_DEBUG) |
114 | cupsFilePuts(fp, "(debug deny)\n"); | |
a4924f6c MS |
115 | cupsFilePuts(fp, "(allow default)\n"); |
116 | cupsFilePrintf(fp, | |
117 | "(deny file-write* file-read-data file-read-metadata\n" | |
5a6b583a MS |
118 | " (regex" |
119 | " #\"^%s$\"" /* RequestRoot */ | |
120 | " #\"^%s/\"" /* RequestRoot/... */ | |
121 | " #\"^/Users$\"" | |
122 | " #\"^/Users/\"" | |
123 | "))\n", | |
124 | request, request); | |
a4924f6c MS |
125 | cupsFilePrintf(fp, |
126 | "(deny file-write*\n" | |
5a6b583a MS |
127 | " (regex" |
128 | " #\"^%s$\"" /* ServerRoot */ | |
129 | " #\"^%s/\"" /* ServerRoot/... */ | |
130 | " #\"^/private/etc$\"" | |
131 | " #\"^/private/etc/\"" | |
132 | " #\"^/usr/local/etc$\"" | |
133 | " #\"^/usr/local/etc/\"" | |
134 | " #\"^/Library$\"" | |
135 | " #\"^/Library/\"" | |
136 | " #\"^/System$\"" | |
137 | " #\"^/System/\"" | |
138 | "))\n", | |
139 | root, root); | |
a4924f6c MS |
140 | cupsFilePrintf(fp, |
141 | "(allow file-write* file-read-data file-read-metadata\n" | |
5a6b583a MS |
142 | " (regex" |
143 | " #\"^%s$\"" /* TempDir */ | |
144 | " #\"^%s/\"" /* TempDir/... */ | |
145 | " #\"^%s$\"" /* CacheDir */ | |
146 | " #\"^%s/\"" /* CacheDir/... */ | |
147 | " #\"^%s/Library$\"" /* RequestRoot/Library */ | |
148 | " #\"^%s/Library/\"" /* RequestRoot/Library/... */ | |
ed6e7faf MS |
149 | " #\"^/Library/Application Support/\"" |
150 | " #\"^/Library/Caches/\"" | |
151 | " #\"^/Library/Preferences/\"" | |
5a6b583a MS |
152 | " #\"^/Library/Printers/.*/\"" |
153 | " #\"^/Users/Shared/\"" | |
ed6e7faf | 154 | "))\n", |
5a6b583a | 155 | temp, temp, cache, cache, request, request); |
ed6e7faf MS |
156 | cupsFilePuts(fp, |
157 | "(deny file-write*\n" | |
5a6b583a MS |
158 | " (regex" |
159 | " #\"^/Library/Printers/PPDs$\"" | |
160 | " #\"^/Library/Printers/PPDs/\"" | |
161 | " #\"^/Library/Printers/PPD Plugins$\"" | |
ed6e7faf MS |
162 | " #\"^/Library/Printers/PPD Plugins/\"" |
163 | "))\n"); | |
a4924f6c MS |
164 | if (job_id) |
165 | cupsFilePrintf(fp, | |
166 | "(allow file-read-data file-read-metadata\n" | |
167 | " (regex #\"^%s/([ac]%05d|d%05d-[0-9][0-9][0-9])$\"))\n", | |
5bd77a73 | 168 | request, job_id, job_id); |
a4924f6c MS |
169 | |
170 | cupsFileClose(fp); | |
171 | ||
5bd77a73 MS |
172 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdCreateProfile(job_id=%d) = \"%s\"", |
173 | job_id, profile); | |
a4924f6c | 174 | return ((void *)strdup(profile)); |
b9faaae1 | 175 | |
a4924f6c | 176 | #else |
b9faaae1 MS |
177 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdCreateProfile(job_id=%d) = NULL", |
178 | job_id); | |
a4924f6c MS |
179 | |
180 | return (NULL); | |
181 | #endif /* HAVE_SANDBOX_H */ | |
182 | } | |
183 | ||
184 | ||
185 | /* | |
186 | * 'cupsdDestroyProfile()' - Delete an execution profile. | |
187 | */ | |
188 | ||
189 | void | |
190 | cupsdDestroyProfile(void *profile) /* I - Profile */ | |
191 | { | |
b9faaae1 MS |
192 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdDeleteProfile(profile=\"%s\")", |
193 | profile ? (char *)profile : "(null)"); | |
194 | ||
a4924f6c MS |
195 | #ifdef HAVE_SANDBOX_H |
196 | if (profile) | |
197 | { | |
198 | unlink((char *)profile); | |
199 | free(profile); | |
200 | } | |
201 | #endif /* HAVE_SANDBOX_H */ | |
202 | } | |
e00b005a | 203 | |
204 | ||
ef416fc2 | 205 | /* |
206 | * 'cupsdEndProcess()' - End a process. | |
207 | */ | |
208 | ||
209 | int /* O - 0 on success, -1 on failure */ | |
210 | cupsdEndProcess(int pid, /* I - Process ID */ | |
211 | int force) /* I - Force child to die */ | |
212 | { | |
b9faaae1 MS |
213 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdEndProcess(pid=%d, force=%d)", pid, |
214 | force); | |
215 | ||
ef55b745 MS |
216 | if (!pid) |
217 | return (0); | |
218 | else if (force) | |
ef416fc2 | 219 | return (kill(pid, SIGKILL)); |
220 | else | |
221 | return (kill(pid, SIGTERM)); | |
222 | } | |
223 | ||
224 | ||
e00b005a | 225 | /* |
226 | * 'cupsdFinishProcess()' - Finish a process and get its name. | |
227 | */ | |
228 | ||
229 | const char * /* O - Process name */ | |
230 | cupsdFinishProcess(int pid, /* I - Process ID */ | |
231 | char *name, /* I - Name buffer */ | |
b9faaae1 MS |
232 | int namelen, /* I - Size of name buffer */ |
233 | int *job_id) /* O - Job ID pointer or NULL */ | |
e00b005a | 234 | { |
235 | cupsd_proc_t key, /* Search key */ | |
236 | *proc; /* Matching process */ | |
237 | ||
238 | ||
239 | key.pid = pid; | |
240 | ||
241 | if ((proc = (cupsd_proc_t *)cupsArrayFind(process_array, &key)) != NULL) | |
242 | { | |
b9faaae1 MS |
243 | if (job_id) |
244 | *job_id = proc->job_id; | |
245 | ||
e00b005a | 246 | strlcpy(name, proc->name, namelen); |
247 | cupsArrayRemove(process_array, proc); | |
248 | free(proc); | |
e00b005a | 249 | } |
250 | else | |
b9faaae1 MS |
251 | { |
252 | if (job_id) | |
253 | *job_id = 0; | |
254 | ||
255 | strlcpy(name, "unknown", namelen); | |
256 | } | |
257 | ||
258 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
259 | "cupsdFinishProcess(pid=%d, name=%p, namelen=%d, " | |
260 | "job_id=%p(%d)) = \"%s\"", pid, name, namelen, job_id, | |
261 | job_id ? *job_id : 0, name); | |
262 | ||
263 | return (name); | |
e00b005a | 264 | } |
265 | ||
266 | ||
ef416fc2 | 267 | /* |
268 | * 'cupsdStartProcess()' - Start a process. | |
269 | */ | |
270 | ||
271 | int /* O - Process ID or 0 */ | |
272 | cupsdStartProcess( | |
b9faaae1 MS |
273 | const char *command, /* I - Full path to command */ |
274 | char *argv[], /* I - Command-line arguments */ | |
275 | char *envp[], /* I - Environment */ | |
276 | int infd, /* I - Standard input file descriptor */ | |
277 | int outfd, /* I - Standard output file descriptor */ | |
278 | int errfd, /* I - Standard error file descriptor */ | |
279 | int backfd, /* I - Backchannel file descriptor */ | |
280 | int sidefd, /* I - Sidechannel file descriptor */ | |
281 | int root, /* I - Run as root? */ | |
282 | void *profile, /* I - Security profile to use */ | |
38e73f87 | 283 | cupsd_job_t *job, /* I - Job associated with process */ |
b9faaae1 | 284 | int *pid) /* O - Process ID */ |
ef416fc2 | 285 | { |
bf3816c7 MS |
286 | int user; /* Command UID */ |
287 | struct stat commandinfo; /* Command file information */ | |
e00b005a | 288 | cupsd_proc_t *proc; /* New process record */ |
ef416fc2 | 289 | #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET) |
e00b005a | 290 | struct sigaction action; /* POSIX signal handler */ |
ef416fc2 | 291 | #endif /* HAVE_SIGACTION && !HAVE_SIGSET */ |
e53920b9 | 292 | #if defined(__APPLE__) |
e00b005a | 293 | char processPath[1024], /* CFProcessPath environment variable */ |
294 | linkpath[1024]; /* Link path for symlinks... */ | |
295 | int linkbytes; /* Bytes for link path */ | |
e53920b9 | 296 | #endif /* __APPLE__ */ |
ef416fc2 | 297 | |
298 | ||
bf3816c7 MS |
299 | if (RunUser) |
300 | user = RunUser; | |
301 | else if (root) | |
302 | user = 0; | |
303 | else | |
304 | user = User; | |
305 | ||
306 | if (stat(command, &commandinfo)) | |
76cd9e37 | 307 | { |
b9faaae1 MS |
308 | *pid = 0; |
309 | ||
310 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
311 | "cupsdStartProcess(command=\"%s\", argv=%p, envp=%p, " | |
312 | "infd=%d, outfd=%d, errfd=%d, backfd=%d, sidefd=%d, root=%d, " | |
38e73f87 | 313 | "profile=%p, job=%p(%d), pid=%p) = %d", |
b9faaae1 | 314 | command, argv, envp, infd, outfd, errfd, backfd, sidefd, |
38e73f87 | 315 | root, profile, job, job ? job->id : 0, pid, *pid); |
76cd9e37 MS |
316 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to execute %s: %s", command, |
317 | strerror(errno)); | |
38e73f87 MS |
318 | |
319 | if (job && job->printer) | |
e07d4801 MS |
320 | { |
321 | if (cupsdSetPrinterReasons(job->printer, "+cups-missing-filter-warning")) | |
322 | cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, job->printer, NULL, | |
323 | "Printer driver %s is missing.", command); | |
324 | } | |
38e73f87 | 325 | |
76cd9e37 MS |
326 | return (0); |
327 | } | |
ef55b745 MS |
328 | else if (!RunUser && |
329 | ((commandinfo.st_mode & (S_ISUID | S_IWGRP | S_IWOTH)) || | |
330 | commandinfo.st_uid)) | |
bf3816c7 | 331 | { |
b9faaae1 MS |
332 | *pid = 0; |
333 | ||
334 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
335 | "cupsdStartProcess(command=\"%s\", argv=%p, envp=%p, " | |
336 | "infd=%d, outfd=%d, errfd=%d, backfd=%d, sidefd=%d, root=%d, " | |
38e73f87 | 337 | "profile=%p, job=%p(%d), pid=%p) = %d", |
b9faaae1 | 338 | command, argv, envp, infd, outfd, errfd, backfd, sidefd, |
38e73f87 | 339 | root, profile, job, job ? job->id : 0, pid, *pid); |
bf3816c7 MS |
340 | cupsdLogMessage(CUPSD_LOG_ERROR, |
341 | "Unable to execute %s: insecure file permissions (0%o)", | |
342 | command, commandinfo.st_mode); | |
b9faaae1 | 343 | |
38e73f87 | 344 | if (job && job->printer) |
e07d4801 MS |
345 | { |
346 | if (cupsdSetPrinterReasons(job->printer, "+cups-insecure-filter-warning")) | |
347 | cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, job->printer, NULL, | |
348 | "Printer driver %s has insecure file permissions (0%o).", | |
349 | command, commandinfo.st_mode); | |
350 | } | |
38e73f87 | 351 | |
bf3816c7 | 352 | errno = EPERM; |
38e73f87 | 353 | |
bf3816c7 MS |
354 | return (0); |
355 | } | |
356 | else if ((commandinfo.st_uid != user || !(commandinfo.st_mode & S_IXUSR)) && | |
357 | (commandinfo.st_gid != Group || !(commandinfo.st_mode & S_IXGRP)) && | |
358 | !(commandinfo.st_mode & S_IXOTH)) | |
359 | { | |
b9faaae1 MS |
360 | *pid = 0; |
361 | ||
362 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
363 | "cupsdStartProcess(command=\"%s\", argv=%p, envp=%p, " | |
364 | "infd=%d, outfd=%d, errfd=%d, backfd=%d, sidefd=%d, root=%d, " | |
38e73f87 | 365 | "profile=%p, job=%p(%d), pid=%p) = %d", |
b9faaae1 | 366 | command, argv, envp, infd, outfd, errfd, backfd, sidefd, |
38e73f87 | 367 | root, profile, job, job ? job->id : 0, pid, *pid); |
bf3816c7 MS |
368 | cupsdLogMessage(CUPSD_LOG_ERROR, |
369 | "Unable to execute %s: no execute permissions (0%o)", | |
370 | command, commandinfo.st_mode); | |
b9faaae1 | 371 | |
bf3816c7 MS |
372 | errno = EPERM; |
373 | return (0); | |
374 | } | |
76cd9e37 | 375 | |
e53920b9 | 376 | #if defined(__APPLE__) |
377 | if (envp) | |
e00b005a | 378 | { |
379 | /* | |
6d2f911b | 380 | * Add special voodoo magic for Mac OS X - this allows Mac OS X |
e53920b9 | 381 | * programs to access their bundle resources properly... |
e00b005a | 382 | */ |
383 | ||
e53920b9 | 384 | if ((linkbytes = readlink(command, linkpath, sizeof(linkpath) - 1)) > 0) |
385 | { | |
386 | /* | |
387 | * Yes, this is a symlink to the actual program, nul-terminate and | |
388 | * use it... | |
389 | */ | |
390 | ||
391 | linkpath[linkbytes] = '\0'; | |
e00b005a | 392 | |
e53920b9 | 393 | if (linkpath[0] == '/') |
394 | snprintf(processPath, sizeof(processPath), "CFProcessPath=%s", | |
395 | linkpath); | |
396 | else | |
397 | snprintf(processPath, sizeof(processPath), "CFProcessPath=%s/%s", | |
f7deaa1a | 398 | dirname((char *)command), linkpath); |
e53920b9 | 399 | } |
e00b005a | 400 | else |
e53920b9 | 401 | snprintf(processPath, sizeof(processPath), "CFProcessPath=%s", command); |
bd7854cb | 402 | |
e53920b9 | 403 | envp[0] = processPath; /* Replace <CFProcessPath> string */ |
404 | } | |
405 | #endif /* __APPLE__ */ | |
e00b005a | 406 | |
ef416fc2 | 407 | /* |
408 | * Block signals before forking... | |
409 | */ | |
410 | ||
411 | cupsdHoldSignals(); | |
412 | ||
413 | if ((*pid = fork()) == 0) | |
414 | { | |
415 | /* | |
416 | * Child process goes here... | |
417 | * | |
418 | * Update stdin/stdout/stderr as needed... | |
419 | */ | |
420 | ||
421 | if (infd != 0) | |
422 | { | |
68b10830 MS |
423 | if (infd < 0) |
424 | infd = open("/dev/null", O_RDONLY); | |
425 | ||
426 | if (infd != 0) | |
427 | { | |
428 | dup2(infd, 0); | |
429 | close(infd); | |
430 | } | |
ef416fc2 | 431 | } |
68b10830 | 432 | |
ef416fc2 | 433 | if (outfd != 1) |
434 | { | |
68b10830 MS |
435 | if (outfd < 0) |
436 | outfd = open("/dev/null", O_WRONLY); | |
437 | ||
438 | if (outfd != 1) | |
439 | { | |
440 | dup2(outfd, 1); | |
441 | close(outfd); | |
442 | } | |
ef416fc2 | 443 | } |
68b10830 | 444 | |
ef416fc2 | 445 | if (errfd != 2) |
446 | { | |
68b10830 MS |
447 | if (errfd < 0) |
448 | errfd = open("/dev/null", O_WRONLY); | |
449 | ||
450 | if (errfd != 2) | |
451 | { | |
452 | dup2(errfd, 2); | |
453 | close(errfd); | |
454 | } | |
ef416fc2 | 455 | } |
68b10830 MS |
456 | |
457 | if (backfd != 3 && backfd >= 0) | |
ef416fc2 | 458 | { |
68b10830 MS |
459 | dup2(backfd, 3); |
460 | close(backfd); | |
ef416fc2 | 461 | fcntl(3, F_SETFL, O_NDELAY); |
462 | } | |
68b10830 MS |
463 | |
464 | if (sidefd != 4 && sidefd >= 0) | |
f7deaa1a | 465 | { |
68b10830 MS |
466 | dup2(sidefd, 4); |
467 | close(sidefd); | |
f7deaa1a | 468 | fcntl(4, F_SETFL, O_NDELAY); |
469 | } | |
ef416fc2 | 470 | |
471 | /* | |
472 | * Change the priority of the process based on the FilterNice setting. | |
5bd77a73 | 473 | * (this is not done for root processes...) |
ef416fc2 | 474 | */ |
475 | ||
476 | if (!root) | |
477 | nice(FilterNice); | |
478 | ||
5bd77a73 MS |
479 | #ifdef HAVE_SANDBOX_H |
480 | /* | |
481 | * Run in a separate security profile... | |
482 | */ | |
483 | ||
484 | if (profile) | |
485 | { | |
486 | char *error = NULL; /* Sandbox error, if any */ | |
487 | ||
488 | if (sandbox_init((char *)profile, SANDBOX_NAMED_EXTERNAL, &error)) | |
489 | { | |
490 | fprintf(stderr, "ERROR: sandbox_init failed: %s (%s)\n", error, | |
491 | strerror(errno)); | |
492 | sandbox_free_error(error); | |
493 | } | |
494 | } | |
495 | #endif /* HAVE_SANDBOX_H */ | |
496 | ||
ef416fc2 | 497 | /* |
498 | * Change user to something "safe"... | |
499 | */ | |
500 | ||
501 | if (!root && !RunUser) | |
502 | { | |
503 | /* | |
504 | * Running as root, so change to non-priviledged user... | |
505 | */ | |
506 | ||
507 | if (setgid(Group)) | |
e00b005a | 508 | exit(errno); |
ef416fc2 | 509 | |
510 | if (setgroups(1, &Group)) | |
e00b005a | 511 | exit(errno); |
ef416fc2 | 512 | |
513 | if (setuid(User)) | |
514 | exit(errno); | |
515 | } | |
516 | else | |
517 | { | |
518 | /* | |
519 | * Reset group membership to just the main one we belong to. | |
520 | */ | |
521 | ||
41681883 MS |
522 | if (setgid(Group) && !RunUser) |
523 | exit(errno); | |
524 | ||
525 | if (setgroups(1, &Group) && !RunUser) | |
526 | exit(errno); | |
ef416fc2 | 527 | } |
528 | ||
529 | /* | |
530 | * Change umask to restrict permissions on created files... | |
531 | */ | |
532 | ||
533 | umask(077); | |
534 | ||
535 | /* | |
536 | * Unblock signals before doing the exec... | |
537 | */ | |
538 | ||
539 | #ifdef HAVE_SIGSET | |
540 | sigset(SIGTERM, SIG_DFL); | |
541 | sigset(SIGCHLD, SIG_DFL); | |
ef55b745 | 542 | sigset(SIGPIPE, SIG_DFL); |
ef416fc2 | 543 | #elif defined(HAVE_SIGACTION) |
544 | memset(&action, 0, sizeof(action)); | |
545 | ||
546 | sigemptyset(&action.sa_mask); | |
547 | action.sa_handler = SIG_DFL; | |
548 | ||
549 | sigaction(SIGTERM, &action, NULL); | |
550 | sigaction(SIGCHLD, &action, NULL); | |
ef55b745 | 551 | sigaction(SIGPIPE, &action, NULL); |
ef416fc2 | 552 | #else |
553 | signal(SIGTERM, SIG_DFL); | |
554 | signal(SIGCHLD, SIG_DFL); | |
ef55b745 | 555 | signal(SIGPIPE, SIG_DFL); |
ef416fc2 | 556 | #endif /* HAVE_SIGSET */ |
557 | ||
558 | cupsdReleaseSignals(); | |
559 | ||
560 | /* | |
561 | * Execute the command; if for some reason this doesn't work, | |
562 | * return the error code... | |
563 | */ | |
564 | ||
565 | if (envp) | |
566 | execve(command, argv, envp); | |
567 | else | |
568 | execv(command, argv); | |
569 | ||
570 | perror(command); | |
571 | ||
572 | exit(errno); | |
573 | } | |
574 | else if (*pid < 0) | |
575 | { | |
576 | /* | |
577 | * Error - couldn't fork a new process! | |
578 | */ | |
579 | ||
580 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to fork %s - %s.", command, | |
581 | strerror(errno)); | |
582 | ||
583 | *pid = 0; | |
584 | } | |
e00b005a | 585 | else |
586 | { | |
587 | if (!process_array) | |
588 | process_array = cupsArrayNew((cups_array_func_t)compare_procs, NULL); | |
589 | ||
590 | if (process_array) | |
591 | { | |
592 | if ((proc = calloc(1, sizeof(cupsd_proc_t) + strlen(command))) != NULL) | |
593 | { | |
b9faaae1 | 594 | proc->pid = *pid; |
38e73f87 | 595 | proc->job_id = job ? job->id : 0; |
e00b005a | 596 | strcpy(proc->name, command); |
597 | ||
598 | cupsArrayAdd(process_array, proc); | |
599 | } | |
600 | } | |
601 | } | |
ef416fc2 | 602 | |
603 | cupsdReleaseSignals(); | |
604 | ||
b9faaae1 MS |
605 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
606 | "cupsdStartProcess(command=\"%s\", argv=%p, envp=%p, " | |
607 | "infd=%d, outfd=%d, errfd=%d, backfd=%d, sidefd=%d, root=%d, " | |
38e73f87 | 608 | "profile=%p, job=%p(%d), pid=%p) = %d", |
b9faaae1 | 609 | command, argv, envp, infd, outfd, errfd, backfd, sidefd, |
38e73f87 | 610 | root, profile, job, job ? job->id : 0, pid, *pid); |
b9faaae1 | 611 | |
ef416fc2 | 612 | return (*pid); |
613 | } | |
614 | ||
615 | ||
616 | /* | |
e00b005a | 617 | * 'compare_procs()' - Compare two processes. |
618 | */ | |
619 | ||
620 | static int /* O - Result of comparison */ | |
621 | compare_procs(cupsd_proc_t *a, /* I - First process */ | |
622 | cupsd_proc_t *b) /* I - Second process */ | |
623 | { | |
624 | return (a->pid - b->pid); | |
625 | } | |
626 | ||
627 | ||
a4924f6c MS |
628 | #ifdef HAVE_SANDBOX_H |
629 | /* | |
630 | * 'cupsd_requote()' - Make a regular-expression version of a string. | |
631 | */ | |
632 | ||
633 | static char * /* O - Quoted string */ | |
634 | cupsd_requote(char *dst, /* I - Destination buffer */ | |
635 | const char *src, /* I - Source string */ | |
636 | size_t dstsize) /* I - Size of destination buffer */ | |
637 | { | |
638 | int ch; /* Current character */ | |
639 | char *dstptr, /* Current position in buffer */ | |
640 | *dstend; /* End of destination buffer */ | |
641 | ||
642 | ||
643 | dstptr = dst; | |
644 | dstend = dst + dstsize - 2; | |
645 | ||
646 | while (*src && dstptr < dstend) | |
647 | { | |
648 | ch = *src++; | |
649 | ||
650 | if (strchr(".?*()[]^$\\", ch)) | |
651 | *dstptr++ = '\\'; | |
652 | ||
653 | *dstptr++ = ch; | |
654 | } | |
655 | ||
656 | *dstptr = '\0'; | |
657 | ||
658 | return (dst); | |
659 | } | |
660 | #endif /* HAVE_SANDBOX_H */ | |
661 | ||
662 | ||
e00b005a | 663 | /* |
75bd9771 | 664 | * End of "$Id: process.c 7256 2008-01-25 00:48:54Z mike $". |
ef416fc2 | 665 | */ |