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