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