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