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