]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
503b54c9 | 2 | * "cupsaddsmb" command for CUPS. |
ef416fc2 | 3 | * |
2c48abc6 | 4 | * Copyright 2007-2018 by Apple Inc. |
503b54c9 | 5 | * Copyright 2001-2006 by Easy Software Products. |
ef416fc2 | 6 | * |
503b54c9 MS |
7 | * These coded instructions, statements, and computer programs are the |
8 | * property of Apple Inc. and are protected by Federal copyright | |
9 | * law. Distribution and use rights are outlined in the file "LICENSE.txt" | |
10 | * which should have been included with this file. If this file is | |
57b7b66b | 11 | * missing or damaged, see the license at "http://www.cups.org/". |
ef416fc2 | 12 | */ |
13 | ||
14 | /* | |
15 | * Include necessary headers... | |
16 | */ | |
17 | ||
71e16022 | 18 | #include <cups/cups-private.h> |
757d2cad | 19 | #include <cups/adminutil.h> |
ef416fc2 | 20 | #include <fcntl.h> |
21 | #include <unistd.h> | |
22 | #include <sys/wait.h> | |
23 | ||
24 | ||
25 | /* | |
26 | * Local globals... | |
27 | */ | |
28 | ||
29 | int Verbosity = 0; | |
30 | const char *SAMBAUser, | |
31 | *SAMBAPassword, | |
32 | *SAMBAServer; | |
33 | ||
34 | ||
35 | /* | |
36 | * Local functions... | |
37 | */ | |
38 | ||
757d2cad | 39 | int export_dest(http_t *http, const char *dest); |
2c48abc6 | 40 | void usage(void) _CUPS_NORETURN; |
ef416fc2 | 41 | |
42 | ||
43 | /* | |
44 | * 'main()' - Export printers on the command-line. | |
45 | */ | |
46 | ||
47 | int /* O - Exit status */ | |
48 | main(int argc, /* I - Number of command-line arguments */ | |
49 | char *argv[]) /* I - Command-line arguments */ | |
50 | { | |
51 | int i, j; /* Looping vars */ | |
52 | int status; /* Status from export_dest() */ | |
53 | int export_all; /* Export all printers? */ | |
757d2cad | 54 | http_t *http; /* Connection to server */ |
ef416fc2 | 55 | int num_dests; /* Number of printers */ |
56 | cups_dest_t *dests; /* Printers */ | |
57 | ||
58 | ||
07725fee | 59 | _cupsSetLocale(argv); |
d09495fa | 60 | |
ef416fc2 | 61 | /* |
62 | * Parse command-line arguments... | |
63 | */ | |
64 | ||
757d2cad | 65 | export_all = 0; |
66 | http = NULL; | |
ef416fc2 | 67 | SAMBAUser = cupsUser(); |
68 | SAMBAPassword = NULL; | |
69 | SAMBAServer = NULL; | |
70 | ||
71 | for (i = 1; i < argc; i ++) | |
757d2cad | 72 | if (!strcmp(argv[i], "-E")) |
73 | { | |
74 | #ifdef HAVE_SSL | |
75 | cupsSetEncryption(HTTP_ENCRYPT_REQUIRED); | |
76 | #else | |
77 | _cupsLangPrintf(stderr, | |
0837b7e8 | 78 | _("%s: Sorry, no encryption support."), |
757d2cad | 79 | argv[0]); |
80 | #endif /* HAVE_SSL */ | |
81 | } | |
82 | else if (!strcmp(argv[i], "-H")) | |
83 | { | |
84 | i ++; | |
85 | if (i >= argc) | |
86 | usage(); | |
87 | ||
88 | SAMBAServer = argv[i]; | |
89 | } | |
ef416fc2 | 90 | else if (!strcmp(argv[i], "-U")) |
91 | { | |
92 | char *sep; /* Separator for password */ | |
93 | ||
94 | ||
95 | i ++; | |
96 | if (i >= argc) | |
97 | usage(); | |
98 | ||
99 | SAMBAUser = argv[i]; | |
100 | ||
101 | if ((sep = strchr(argv[i], '%')) != NULL) | |
102 | { | |
103 | /* | |
104 | * Nul-terminate the username at the first % and point the | |
105 | * password at the rest... | |
106 | */ | |
107 | ||
108 | *sep++ = '\0'; | |
109 | ||
110 | SAMBAPassword = sep; | |
111 | } | |
112 | } | |
757d2cad | 113 | else if (!strcmp(argv[i], "-a")) |
114 | export_all = 1; | |
ef416fc2 | 115 | else if (!strcmp(argv[i], "-h")) |
116 | { | |
117 | i ++; | |
118 | if (i >= argc) | |
119 | usage(); | |
120 | ||
121 | cupsSetServer(argv[i]); | |
122 | } | |
123 | else if (!strcmp(argv[i], "-v")) | |
124 | Verbosity = 1; | |
125 | else if (argv[i][0] != '-') | |
126 | { | |
757d2cad | 127 | if (!http) |
128 | { | |
129 | /* | |
130 | * Connect to the server... | |
131 | */ | |
132 | ||
133 | if ((http = httpConnectEncrypt(cupsServer(), ippPort(), | |
134 | cupsEncryption())) == NULL) | |
135 | { | |
0837b7e8 MS |
136 | _cupsLangPrintf(stderr, _("%s: Unable to connect to server."), |
137 | argv[0]); | |
757d2cad | 138 | exit(1); |
139 | } | |
140 | } | |
141 | ||
ef416fc2 | 142 | if (SAMBAServer == NULL) |
2fb76298 | 143 | { |
ef416fc2 | 144 | SAMBAServer = cupsServer(); |
145 | ||
2fb76298 MS |
146 | if (SAMBAServer[0] == '/') /* Use localhost instead of domain socket */ |
147 | SAMBAServer = "localhost"; | |
148 | } | |
149 | ||
757d2cad | 150 | if ((status = export_dest(http, argv[i])) != 0) |
ef416fc2 | 151 | return (status); |
152 | } | |
153 | else | |
154 | usage(); | |
155 | ||
757d2cad | 156 | /* |
157 | * Connect to the server... | |
158 | */ | |
159 | ||
160 | if ((http = httpConnectEncrypt(cupsServer(), ippPort(), | |
161 | cupsEncryption())) == NULL) | |
162 | { | |
0837b7e8 | 163 | _cupsLangPrintf(stderr, _("%s: Unable to connect to server."), argv[0]); |
757d2cad | 164 | exit(1); |
165 | } | |
166 | ||
ef416fc2 | 167 | /* |
168 | * See if the user specified "-a"... | |
169 | */ | |
170 | ||
171 | if (export_all) | |
172 | { | |
173 | /* | |
174 | * Export all printers... | |
175 | */ | |
176 | ||
177 | if (SAMBAServer == NULL) | |
2fb76298 | 178 | { |
ef416fc2 | 179 | SAMBAServer = cupsServer(); |
180 | ||
2fb76298 MS |
181 | if (SAMBAServer[0] == '/') /* Use localhost instead of domain socket */ |
182 | SAMBAServer = "localhost"; | |
183 | } | |
184 | ||
757d2cad | 185 | num_dests = cupsGetDests2(http, &dests); |
ef416fc2 | 186 | |
187 | for (j = 0, status = 0; j < num_dests; j ++) | |
188 | if (!dests[j].instance) | |
189 | { | |
757d2cad | 190 | if ((status = export_dest(http, dests[j].name)) != 0) |
ef416fc2 | 191 | break; |
192 | } | |
193 | ||
194 | cupsFreeDests(num_dests, dests); | |
195 | ||
196 | if (status) | |
197 | return (status); | |
198 | } | |
199 | ||
200 | return (0); | |
201 | } | |
202 | ||
203 | ||
ef416fc2 | 204 | /* |
205 | * 'export_dest()' - Export a destination to SAMBA. | |
206 | */ | |
207 | ||
208 | int /* O - 0 on success, non-zero on error */ | |
757d2cad | 209 | export_dest(http_t *http, /* I - Connection to server */ |
210 | const char *dest) /* I - Destination to export */ | |
ef416fc2 | 211 | { |
757d2cad | 212 | int status; /* Status of export */ |
213 | char ppdfile[1024], /* PPD file for printer drivers */ | |
214 | prompt[1024]; /* Password prompt */ | |
7cf5915e | 215 | int tries; /* Number of tries */ |
ef416fc2 | 216 | |
217 | ||
218 | /* | |
757d2cad | 219 | * Get the Windows PPD file for the printer... |
ef416fc2 | 220 | */ |
221 | ||
757d2cad | 222 | if (!cupsAdminCreateWindowsPPD(http, dest, ppdfile, sizeof(ppdfile))) |
ef416fc2 | 223 | { |
fa73b229 | 224 | _cupsLangPrintf(stderr, |
0837b7e8 | 225 | _("cupsaddsmb: No PPD file for printer \"%s\" - %s"), |
b423cd4c | 226 | dest, cupsLastErrorString()); |
757d2cad | 227 | return (1); |
ef416fc2 | 228 | } |
229 | ||
230 | /* | |
757d2cad | 231 | * Try to export it... |
ef416fc2 | 232 | */ |
233 | ||
7cf5915e | 234 | for (status = 0, tries = 0; !status && tries < 3; tries ++) |
ef416fc2 | 235 | { |
ef416fc2 | 236 | /* |
757d2cad | 237 | * Get the password, as needed... |
ef416fc2 | 238 | */ |
239 | ||
757d2cad | 240 | if (!SAMBAPassword) |
ef416fc2 | 241 | { |
757d2cad | 242 | snprintf(prompt, sizeof(prompt), |
8ca02f3c | 243 | _cupsLangString(cupsLangDefault(), |
244 | _("Password for %s required to access %s via " | |
245 | "SAMBA: ")), | |
757d2cad | 246 | SAMBAUser, SAMBAServer); |
ef416fc2 | 247 | |
757d2cad | 248 | if ((SAMBAPassword = cupsGetPassword(prompt)) == NULL) |
249 | break; | |
ef416fc2 | 250 | } |
251 | ||
757d2cad | 252 | status = cupsAdminExportSamba(dest, ppdfile, SAMBAServer, |
253 | SAMBAUser, SAMBAPassword, | |
254 | Verbosity ? stderr : NULL); | |
bc44d920 | 255 | |
256 | if (!status && cupsLastError() == IPP_NOT_FOUND) | |
257 | break; | |
ef416fc2 | 258 | } |
259 | ||
260 | unlink(ppdfile); | |
261 | ||
757d2cad | 262 | return (!status); |
ef416fc2 | 263 | } |
264 | ||
265 | ||
266 | /* | |
267 | * 'usage()' - Show program usage and exit... | |
268 | */ | |
269 | ||
270 | void | |
271 | usage(void) | |
272 | { | |
0837b7e8 MS |
273 | _cupsLangPuts(stdout, _("Usage: cupsaddsmb [options] printer1 ... printerN")); |
274 | _cupsLangPuts(stdout, _(" cupsaddsmb [options] -a")); | |
275 | _cupsLangPuts(stdout, ""); | |
276 | _cupsLangPuts(stdout, _("Options:")); | |
f3c17241 | 277 | _cupsLangPuts(stdout, _(" -E Encrypt the connection.")); |
84315f46 | 278 | _cupsLangPuts(stdout, _(" -H samba-server Use the named SAMBA " |
0837b7e8 | 279 | "server.")); |
f3c17241 | 280 | _cupsLangPuts(stdout, _(" -U username Specify username.")); |
84315f46 | 281 | _cupsLangPuts(stdout, _(" -a Export all printers.")); |
f3c17241 MS |
282 | _cupsLangPuts(stdout, _(" -h server[:port] Specify server address.")); |
283 | _cupsLangPuts(stdout, _(" -v Be verbose.")); | |
0837b7e8 | 284 | |
ef416fc2 | 285 | exit(1); |
286 | } |