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