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