1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
/*
* Sandbox helper for CUPS.
*
* Copyright © 2020-2024 by OpenPrinting.
* Copyright © 2007-2014 by Apple Inc.
*
* Licensed under Apache License v2.0. See the file "LICENSE" for more
* information.
*
* Usage:
*
* cups-exec /path/to/profile [-u UID] [-g GID] [-n NICE] /path/to/program argv0 argv1 ... argvN
*/
/*
* Include necessary headers...
*/
#include <cups/string-private.h>
#include <cups/file.h>
#include <unistd.h>
#include <fcntl.h>
#include <grp.h>
#include <sys/stat.h>
#ifdef HAVE_SANDBOX_H
# include <sandbox.h>
# ifndef SANDBOX_NAMED_EXTERNAL
# define SANDBOX_NAMED_EXTERNAL 0x0003
# endif /* !SANDBOX_NAMED_EXTERNAL */
# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif /* HAVE_SANDBOX_H */
/*
* Local functions...
*/
static void usage(void) _CUPS_NORETURN;
/*
* 'main()' - Apply sandbox profile and execute program.
*/
int /* O - Exit status */
main(int argc, /* I - Number of command-line args */
char *argv[]) /* I - Command-line arguments */
{
int i; /* Looping var */
const char *opt; /* Current option character */
uid_t uid = getuid(); /* UID */
gid_t gid = getgid(); /* GID */
int niceval = 0; /* Nice value */
#ifdef HAVE_SANDBOX_H
char *sandbox_error = NULL; /* Sandbox error, if any */
#endif /* HAVE_SANDBOX_H */
/*
* Parse command-line...
*/
for (i = 1; i < argc; i ++)
{
if (argv[i][0] == '-')
{
for (opt = argv[i] + 1; *opt; opt ++)
{
switch (*opt)
{
case 'g' : /* -g gid */
i ++;
if (i >= argc)
usage();
gid = (gid_t)strtoul(argv[i], NULL, 10);
break;
case 'n' : /* -n nice-value */
i ++;
if (i >= argc)
usage();
niceval = atoi(argv[i]);
break;
case 'u' : /* -g gid */
i ++;
if (i >= argc)
usage();
uid = (uid_t)strtoul(argv[i], NULL, 10);
break;
default :
fprintf(stderr, "cups-exec: Unknown option '-%c'.\n", *opt);
usage();
}
}
}
else
break;
}
/*
* Check that we have enough arguments...
*/
if ((i + 3) > argc)
{
fputs("cups-exec: Insufficient arguments.\n", stderr);
usage();
}
/*
* Make sure side and back channel FDs are non-blocking...
*/
fcntl(3, F_SETFL, O_NDELAY);
fcntl(4, F_SETFL, O_NDELAY);
/*
* Change UID, GID, and nice value...
*/
if (uid)
nice(niceval);
if (!getuid())
{
if (setgid(gid))
exit(errno + 100);
# if CUPS_SNAP
if (setgroups(0, NULL))
# else
if (setgroups(1, &gid))
# endif /* CUPS_SNAP */
exit(errno + 100);
if (uid && setuid(uid))
exit(errno + 100);
}
umask(077);
#ifdef HAVE_SANDBOX_H
/*
* Run in a separate security profile...
*/
if (strcmp(argv[i], "none") &&
sandbox_init(argv[i], SANDBOX_NAMED_EXTERNAL, &sandbox_error))
{
cups_file_t *fp; /* File */
char line[1024]; /* Line from file */
unsigned linenum = 0; /* Line number in file */
fprintf(stderr, "DEBUG: sandbox_init failed: %s (%s)\n", sandbox_error,
strerror(errno));
sandbox_free_error(sandbox_error);
if ((fp = cupsFileOpen(argv[i], "r")) != NULL)
{
while (cupsFileGets(fp, line, sizeof(line)))
{
linenum ++;
fprintf(stderr, "DEBUG: %4u %s\n", linenum, line);
}
cupsFileClose(fp);
}
return (100 + EINVAL);
}
#endif /* HAVE_SANDBOX_H */
/*
* Execute the program...
*/
execv(argv[i + 1], argv + i + 2);
/*
* If we get here, execv() failed...
*/
fprintf(stderr, "DEBUG: execv failed: %s\n", strerror(errno));
return (errno + 100);
}
/*
* 'usage()' - Show program usage.
*/
static void
usage(void)
{
fputs("Usage: cups-exec [-g gid] [-n nice-value] [-u uid] /path/to/profile /path/to/program argv0 argv1 ... argvN\n", stderr);
exit(1);
}
|