]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
757d2cad | 2 | * "$Id: cupsaddsmb.c 5233 2006-03-06 03:39:28Z 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 | ||
81 | /* | |
82 | * Parse command-line arguments... | |
83 | */ | |
84 | ||
757d2cad | 85 | export_all = 0; |
86 | http = NULL; | |
ef416fc2 | 87 | SAMBAUser = cupsUser(); |
88 | SAMBAPassword = NULL; | |
89 | SAMBAServer = NULL; | |
90 | ||
91 | for (i = 1; i < argc; i ++) | |
757d2cad | 92 | if (!strcmp(argv[i], "-E")) |
93 | { | |
94 | #ifdef HAVE_SSL | |
95 | cupsSetEncryption(HTTP_ENCRYPT_REQUIRED); | |
96 | #else | |
97 | _cupsLangPrintf(stderr, | |
98 | _("%s: Sorry, no encryption support compiled in!\n"), | |
99 | argv[0]); | |
100 | #endif /* HAVE_SSL */ | |
101 | } | |
102 | else if (!strcmp(argv[i], "-H")) | |
103 | { | |
104 | i ++; | |
105 | if (i >= argc) | |
106 | usage(); | |
107 | ||
108 | SAMBAServer = argv[i]; | |
109 | } | |
ef416fc2 | 110 | else if (!strcmp(argv[i], "-U")) |
111 | { | |
112 | char *sep; /* Separator for password */ | |
113 | ||
114 | ||
115 | i ++; | |
116 | if (i >= argc) | |
117 | usage(); | |
118 | ||
119 | SAMBAUser = argv[i]; | |
120 | ||
121 | if ((sep = strchr(argv[i], '%')) != NULL) | |
122 | { | |
123 | /* | |
124 | * Nul-terminate the username at the first % and point the | |
125 | * password at the rest... | |
126 | */ | |
127 | ||
128 | *sep++ = '\0'; | |
129 | ||
130 | SAMBAPassword = sep; | |
131 | } | |
132 | } | |
757d2cad | 133 | else if (!strcmp(argv[i], "-a")) |
134 | export_all = 1; | |
ef416fc2 | 135 | else if (!strcmp(argv[i], "-h")) |
136 | { | |
137 | i ++; | |
138 | if (i >= argc) | |
139 | usage(); | |
140 | ||
141 | cupsSetServer(argv[i]); | |
142 | } | |
143 | else if (!strcmp(argv[i], "-v")) | |
144 | Verbosity = 1; | |
145 | else if (argv[i][0] != '-') | |
146 | { | |
757d2cad | 147 | if (!http) |
148 | { | |
149 | /* | |
150 | * Connect to the server... | |
151 | */ | |
152 | ||
153 | if ((http = httpConnectEncrypt(cupsServer(), ippPort(), | |
154 | cupsEncryption())) == NULL) | |
155 | { | |
156 | _cupsLangPrintf(stderr, _("%s: Unable to connect to server\n"), argv[0]); | |
157 | exit(1); | |
158 | } | |
159 | } | |
160 | ||
ef416fc2 | 161 | if (SAMBAServer == NULL) |
162 | SAMBAServer = cupsServer(); | |
163 | ||
757d2cad | 164 | if ((status = export_dest(http, argv[i])) != 0) |
ef416fc2 | 165 | return (status); |
166 | } | |
167 | else | |
168 | usage(); | |
169 | ||
757d2cad | 170 | /* |
171 | * Connect to the server... | |
172 | */ | |
173 | ||
174 | if ((http = httpConnectEncrypt(cupsServer(), ippPort(), | |
175 | cupsEncryption())) == NULL) | |
176 | { | |
177 | _cupsLangPrintf(stderr, _("%s: Unable to connect to server\n"), argv[0]); | |
178 | exit(1); | |
179 | } | |
180 | ||
ef416fc2 | 181 | /* |
182 | * See if the user specified "-a"... | |
183 | */ | |
184 | ||
185 | if (export_all) | |
186 | { | |
187 | /* | |
188 | * Export all printers... | |
189 | */ | |
190 | ||
191 | if (SAMBAServer == NULL) | |
192 | SAMBAServer = cupsServer(); | |
193 | ||
757d2cad | 194 | num_dests = cupsGetDests2(http, &dests); |
ef416fc2 | 195 | |
196 | for (j = 0, status = 0; j < num_dests; j ++) | |
197 | if (!dests[j].instance) | |
198 | { | |
757d2cad | 199 | if ((status = export_dest(http, dests[j].name)) != 0) |
ef416fc2 | 200 | break; |
201 | } | |
202 | ||
203 | cupsFreeDests(num_dests, dests); | |
204 | ||
205 | if (status) | |
206 | return (status); | |
207 | } | |
208 | ||
209 | return (0); | |
210 | } | |
211 | ||
212 | ||
ef416fc2 | 213 | /* |
214 | * 'export_dest()' - Export a destination to SAMBA. | |
215 | */ | |
216 | ||
217 | int /* O - 0 on success, non-zero on error */ | |
757d2cad | 218 | export_dest(http_t *http, /* I - Connection to server */ |
219 | const char *dest) /* I - Destination to export */ | |
ef416fc2 | 220 | { |
757d2cad | 221 | int status; /* Status of export */ |
222 | char ppdfile[1024], /* PPD file for printer drivers */ | |
223 | prompt[1024]; /* Password prompt */ | |
ef416fc2 | 224 | |
225 | ||
226 | /* | |
757d2cad | 227 | * Get the Windows PPD file for the printer... |
ef416fc2 | 228 | */ |
229 | ||
757d2cad | 230 | if (!cupsAdminCreateWindowsPPD(http, dest, ppdfile, sizeof(ppdfile))) |
ef416fc2 | 231 | { |
fa73b229 | 232 | _cupsLangPrintf(stderr, |
ef416fc2 | 233 | _("cupsaddsmb: No PPD file for printer \"%s\" - " |
b423cd4c | 234 | "%s\n"), |
235 | dest, cupsLastErrorString()); | |
757d2cad | 236 | return (1); |
ef416fc2 | 237 | } |
238 | ||
239 | /* | |
757d2cad | 240 | * Try to export it... |
ef416fc2 | 241 | */ |
242 | ||
757d2cad | 243 | for (status = 0; !status;) |
ef416fc2 | 244 | { |
ef416fc2 | 245 | /* |
757d2cad | 246 | * Get the password, as needed... |
ef416fc2 | 247 | */ |
248 | ||
757d2cad | 249 | if (!SAMBAPassword) |
ef416fc2 | 250 | { |
757d2cad | 251 | snprintf(prompt, sizeof(prompt), |
252 | _("Password for %s required to access %s via SAMBA: "), | |
253 | SAMBAUser, SAMBAServer); | |
ef416fc2 | 254 | |
757d2cad | 255 | if ((SAMBAPassword = cupsGetPassword(prompt)) == NULL) |
256 | break; | |
ef416fc2 | 257 | } |
258 | ||
757d2cad | 259 | status = cupsAdminExportSamba(dest, ppdfile, SAMBAServer, |
260 | SAMBAUser, SAMBAPassword, | |
261 | Verbosity ? stderr : NULL); | |
ef416fc2 | 262 | } |
263 | ||
264 | unlink(ppdfile); | |
265 | ||
757d2cad | 266 | return (!status); |
ef416fc2 | 267 | } |
268 | ||
269 | ||
270 | /* | |
271 | * 'usage()' - Show program usage and exit... | |
272 | */ | |
273 | ||
274 | void | |
275 | usage(void) | |
276 | { | |
fa73b229 | 277 | _cupsLangPuts(stdout, |
ef416fc2 | 278 | _("Usage: cupsaddsmb [options] printer1 ... printerN\n" |
279 | " cupsaddsmb [options] -a\n" | |
280 | "\n" | |
281 | "Options:\n" | |
757d2cad | 282 | " -E Encrypt the connection to the server\n" |
ef416fc2 | 283 | " -H samba-server Use the named SAMBA server\n" |
284 | " -U samba-user Authenticate using the named SAMBA user\n" | |
285 | " -a Export all printers\n" | |
286 | " -h cups-server Use the named CUPS server\n" | |
287 | " -v Be verbose (show commands)\n")); | |
288 | exit(1); | |
289 | } | |
290 | ||
291 | ||
292 | /* | |
757d2cad | 293 | * End of "$Id: cupsaddsmb.c 5233 2006-03-06 03:39:28Z mike $". |
ef416fc2 | 294 | */ |