]> git.ipfire.org Git - thirdparty/cups.git/blame - systemv/cupsctl.c
Sandboxed applications were not able to get the default printer (Issue #5676)
[thirdparty/cups.git] / systemv / cupsctl.c
CommitLineData
bc44d920 1/*
503b54c9 2 * Scheduler control program for CUPS.
bc44d920 3 *
20c9da81 4 * Copyright © 2007-2019 by Apple Inc.
8e52928c 5 * Copyright © 2006-2007 by Easy Software Products.
bc44d920 6 *
8e52928c
MS
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more
8 * information.
bc44d920 9 */
10
11/*
12 * Include necessary headers...
13 */
14
71e16022 15#include <cups/cups-private.h>
bc44d920 16#include <cups/adminutil.h>
bc44d920 17
18
19/*
20 * Local functions...
21 */
22
a32af27c 23static void usage(const char *opt) _CUPS_NORETURN;
bc44d920 24
25
26/*
27 * 'main()' - Get/set server settings.
28 */
29
30int /* O - Exit status */
31main(int argc, /* I - Number of command-line args */
32 char *argv[]) /* I - Command-line arguments */
33{
20c9da81 34 int i, j, /* Looping vars */
bc44d920 35 num_settings; /* Number of settings */
20c9da81
MS
36 cups_option_t *settings, /* Settings */
37 *setting; /* Current setting */
bc44d920 38 const char *opt; /* Current option character */
39 http_t *http; /* Connection to server */
20c9da81
MS
40 static const char * const disallowed[] =
41 { /* List of disallowed directives for cupsd.conf */
42 "AccessLog",
43 "CacheDir",
44 "ConfigFilePerm",
45 "DataDir",
46 "DocumentRoot",
47 "ErrorLog",
48 "FatalErrors",
49 "FileDevice",
50 "FontPath",
51 "Group",
52 "Listen",
53 "LogFilePerm",
54 "LPDConfigFile",
55 "PageLog",
56 "PassEnv",
57 "Port",
58 "Printcap",
59 "PrintcapFormat",
60 "RemoteRoot",
61 "RequestRoot",
62 "ServerBin",
63 "ServerCertificate",
64 "ServerKey",
65 "ServerKeychain",
66 "ServerRoot",
67 "SetEnv",
68 "SMBConfigFile",
69 "StateDir",
70 "SystemGroup",
71 "SystemGroupAuthKey",
72 "TempDir",
73 "User"
74 };
bc44d920 75
76
77 /*
78 * Process the command-line...
79 */
80
81 _cupsSetLocale(argv);
82
83 num_settings = 0;
84 settings = NULL;
85
86 for (i = 1; i < argc; i ++)
87 {
8e52928c
MS
88 if (!strcmp(argv[i], "--help"))
89 usage(NULL);
90 else if (argv[i][0] == '-')
bc44d920 91 {
92 if (argv[i][1] == '-')
93 {
355e94dc 94 if (!strcmp(argv[i], "--debug-logging"))
bc44d920 95 num_settings = cupsAddOption(CUPS_SERVER_DEBUG_LOGGING, "1",
96 num_settings, &settings);
97 else if (!strcmp(argv[i], "--no-debug-logging"))
98 num_settings = cupsAddOption(CUPS_SERVER_DEBUG_LOGGING, "0",
99 num_settings, &settings);
100 else if (!strcmp(argv[i], "--remote-admin"))
101 num_settings = cupsAddOption(CUPS_SERVER_REMOTE_ADMIN, "1",
102 num_settings, &settings);
103 else if (!strcmp(argv[i], "--no-remote-admin"))
104 num_settings = cupsAddOption(CUPS_SERVER_REMOTE_ADMIN, "0",
105 num_settings, &settings);
106 else if (!strcmp(argv[i], "--remote-any"))
107 num_settings = cupsAddOption(CUPS_SERVER_REMOTE_ANY, "1",
108 num_settings, &settings);
109 else if (!strcmp(argv[i], "--no-remote-any"))
110 num_settings = cupsAddOption(CUPS_SERVER_REMOTE_ANY, "0",
111 num_settings, &settings);
bc44d920 112 else if (!strcmp(argv[i], "--share-printers"))
113 num_settings = cupsAddOption(CUPS_SERVER_SHARE_PRINTERS, "1",
114 num_settings, &settings);
115 else if (!strcmp(argv[i], "--no-share-printers"))
116 num_settings = cupsAddOption(CUPS_SERVER_SHARE_PRINTERS, "0",
117 num_settings, &settings);
118 else if (!strcmp(argv[i], "--user-cancel-any"))
119 num_settings = cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY, "1",
120 num_settings, &settings);
121 else if (!strcmp(argv[i], "--no-user-cancel-any"))
122 num_settings = cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY, "0",
123 num_settings, &settings);
124 else
125 usage(argv[i]);
126 }
127 else
128 {
129 for (opt = argv[i] + 1; *opt; opt ++)
130 switch (*opt)
131 {
132 case 'E' :
133 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED);
134 break;
135
136 case 'U' :
137 i ++;
138 if (i >= argc)
139 usage(NULL);
140
141 cupsSetUser(argv[i]);
142 break;
143
144 case 'h' :
145 i ++;
146 if (i >= argc)
147 usage(NULL);
148
149 cupsSetServer(argv[i]);
150 break;
151
152 default :
153 usage(opt);
154 break;
155 }
156 }
157 }
158 else if (strchr(argv[i], '='))
159 num_settings = cupsParseOptions(argv[i], num_settings, &settings);
160 else
161 usage(argv[i]);
162 }
163
20c9da81 164 for (i = num_settings, setting = settings; i > 0; i --, setting ++)
0268488e 165 {
20c9da81
MS
166 for (j = 0; j < (int)(sizeof(disallowed) / sizeof(disallowed[0])); j ++)
167 {
168 if (!_cups_strcasecmp(setting->name, disallowed[j]))
169 {
170 _cupsLangPrintf(stderr, _("cupsctl: Cannot set %s directly."), disallowed[j]);
171 return (1);
172 }
173 }
0268488e
MS
174 }
175
bc44d920 176 /*
177 * Connect to the server using the defaults...
178 */
179
db0bd74a
MS
180 if ((http = httpConnectEncrypt(cupsServer(), ippPort(),
181 cupsEncryption())) == NULL)
182 {
0837b7e8 183 _cupsLangPrintf(stderr, _("cupsctl: Unable to connect to server: %s"),
db0bd74a
MS
184 strerror(errno));
185 return (1);
186 }
bc44d920 187
188 /*
189 * Set the current configuration if we have anything on the command-line...
190 */
191
192 if (num_settings > 0)
193 {
194 if (!cupsAdminSetServerSettings(http, num_settings, settings))
195 {
0837b7e8 196 _cupsLangPrintf(stderr, "cupsctl: %s", cupsLastErrorString());
bc44d920 197 return (1);
198 }
199 }
200 else if (!cupsAdminGetServerSettings(http, &num_settings, &settings))
201 {
0837b7e8 202 _cupsLangPrintf(stderr, "cupsctl: %s", cupsLastErrorString());
bc44d920 203 return (1);
204 }
205 else
206 {
207 for (i = 0; i < num_settings; i ++)
0837b7e8 208 _cupsLangPrintf(stdout, "%s=%s", settings[i].name, settings[i].value);
bc44d920 209 }
210
211 cupsFreeOptions(num_settings, settings);
212 return (0);
213}
214
215
216/*
217 * 'usage()' - Show program usage.
218 */
219
220static void
221usage(const char *opt) /* I - Option character/string */
222{
223 if (opt)
224 {
225 if (*opt == '-')
0837b7e8 226 _cupsLangPrintf(stderr, _("cupsctl: Unknown option \"%s\""), opt);
bc44d920 227 else
0837b7e8 228 _cupsLangPrintf(stderr, _("cupsctl: Unknown option \"-%c\""), *opt);
bc44d920 229 }
230
8e52928c 231 _cupsLangPuts(stdout, _("Usage: cupsctl [options] [param=value ... paramN=valueN]"));
0837b7e8 232 _cupsLangPuts(stdout, _("Options:"));
8e52928c
MS
233 _cupsLangPuts(stdout, _("-E Encrypt the connection to the server"));
234 _cupsLangPuts(stdout, _("-h server[:port] Connect to the named server and port"));
235 _cupsLangPuts(stdout, _("-U username Specify username to use for authentication"));
236 _cupsLangPuts(stdout, _("--[no-]debug-logging Turn debug logging on/off"));
237 _cupsLangPuts(stdout, _("--[no-]remote-admin Turn remote administration on/off"));
238 _cupsLangPuts(stdout, _("--[no-]remote-any Allow/prevent access from the Internet"));
239 _cupsLangPuts(stdout, _("--[no-]share-printers Turn printer sharing on/off"));
240 _cupsLangPuts(stdout, _("--[no-]user-cancel-any Allow/prevent users to cancel any job"));
bc44d920 241
242 exit(1);
243}