1 /*
2  * Scheduler control program for CUPS.
3  *
4  * Copyright © 2007-2019 by Apple Inc.
5  * Copyright © 2006-2007 by Easy Software Products.
6  *
7  * Licensed under Apache License v2.0.  See the file "LICENSE" for more
8  * information.
9  */
10 
11 /*
12  * Include necessary headers...
13  */
14 
15 #include <cups/cups-private.h>
16 #include <cups/adminutil.h>
17 
18 
19 /*
20  * Local functions...
21  */
22 
23 static void	usage(const char *opt) _CUPS_NORETURN;
24 
25 
26 /*
27  * 'main()' - Get/set server settings.
28  */
29 
30 int					/* O - Exit status */
main(int argc,char * argv[])31 main(int  argc,				/* I - Number of command-line args */
32      char *argv[])			/* I - Command-line arguments */
33 {
34   int		i, j,			/* Looping vars */
35 		num_settings;		/* Number of settings */
36   cups_option_t	*settings,		/* Settings */
37 		*setting;		/* Current setting */
38   const char	*opt;			/* Current option character */
39   http_t	*http;			/* Connection to server */
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   };
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   {
88     if (!strcmp(argv[i], "--help"))
89       usage(NULL);
90     else if (argv[i][0] == '-')
91     {
92       if (argv[i][1] == '-')
93       {
94         if (!strcmp(argv[i], "--debug-logging"))
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);
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 
164   for (i = num_settings, setting = settings; i > 0; i --, setting ++)
165   {
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     }
174   }
175 
176  /*
177   * Connect to the server using the defaults...
178   */
179 
180   if ((http = httpConnectEncrypt(cupsServer(), ippPort(),
181                                  cupsEncryption())) == NULL)
182   {
183     _cupsLangPrintf(stderr, _("cupsctl: Unable to connect to server: %s"),
184                     strerror(errno));
185     return (1);
186   }
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     {
196       _cupsLangPrintf(stderr, "cupsctl: %s", cupsLastErrorString());
197       return (1);
198     }
199   }
200   else if (!cupsAdminGetServerSettings(http, &num_settings, &settings))
201   {
202     _cupsLangPrintf(stderr, "cupsctl: %s", cupsLastErrorString());
203     return (1);
204   }
205   else
206   {
207     for (i = 0; i < num_settings; i ++)
208       _cupsLangPrintf(stdout, "%s=%s", settings[i].name, settings[i].value);
209   }
210 
211   cupsFreeOptions(num_settings, settings);
212   return (0);
213 }
214 
215 
216 /*
217  * 'usage()' - Show program usage.
218  */
219 
220 static void
usage(const char * opt)221 usage(const char *opt)			/* I - Option character/string */
222 {
223   if (opt)
224   {
225     if (*opt == '-')
226       _cupsLangPrintf(stderr, _("cupsctl: Unknown option \"%s\""), opt);
227     else
228       _cupsLangPrintf(stderr, _("cupsctl: Unknown option \"-%c\""), *opt);
229   }
230 
231   _cupsLangPuts(stdout, _("Usage: cupsctl [options] [param=value ... paramN=valueN]"));
232   _cupsLangPuts(stdout, _("Options:"));
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"));
241 
242   exit(1);
243 }
244