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