]> git.ipfire.org Git - thirdparty/cups.git/blob - systemv/cupsaddsmb.c
Load cups into easysw/current.
[thirdparty/cups.git] / systemv / cupsaddsmb.c
1 /*
2 * "$Id: cupsaddsmb.c 6649 2007-07-11 21:46:42Z 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 SAMBAServer = cupsServer();
156
157 if ((status = export_dest(http, argv[i])) != 0)
158 return (status);
159 }
160 else
161 usage();
162
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
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)
185 SAMBAServer = cupsServer();
186
187 num_dests = cupsGetDests2(http, &dests);
188
189 for (j = 0, status = 0; j < num_dests; j ++)
190 if (!dests[j].instance)
191 {
192 if ((status = export_dest(http, dests[j].name)) != 0)
193 break;
194 }
195
196 cupsFreeDests(num_dests, dests);
197
198 if (status)
199 return (status);
200 }
201
202 return (0);
203 }
204
205
206 /*
207 * 'export_dest()' - Export a destination to SAMBA.
208 */
209
210 int /* O - 0 on success, non-zero on error */
211 export_dest(http_t *http, /* I - Connection to server */
212 const char *dest) /* I - Destination to export */
213 {
214 int status; /* Status of export */
215 char ppdfile[1024], /* PPD file for printer drivers */
216 prompt[1024]; /* Password prompt */
217
218
219 /*
220 * Get the Windows PPD file for the printer...
221 */
222
223 if (!cupsAdminCreateWindowsPPD(http, dest, ppdfile, sizeof(ppdfile)))
224 {
225 _cupsLangPrintf(stderr,
226 _("cupsaddsmb: No PPD file for printer \"%s\" - "
227 "%s\n"),
228 dest, cupsLastErrorString());
229 return (1);
230 }
231
232 /*
233 * Try to export it...
234 */
235
236 for (status = 0; !status;)
237 {
238 /*
239 * Get the password, as needed...
240 */
241
242 if (!SAMBAPassword)
243 {
244 snprintf(prompt, sizeof(prompt),
245 _cupsLangString(cupsLangDefault(),
246 _("Password for %s required to access %s via "
247 "SAMBA: ")),
248 SAMBAUser, SAMBAServer);
249
250 if ((SAMBAPassword = cupsGetPassword(prompt)) == NULL)
251 break;
252 }
253
254 status = cupsAdminExportSamba(dest, ppdfile, SAMBAServer,
255 SAMBAUser, SAMBAPassword,
256 Verbosity ? stderr : NULL);
257
258 if (!status && cupsLastError() == IPP_NOT_FOUND)
259 break;
260 }
261
262 unlink(ppdfile);
263
264 return (!status);
265 }
266
267
268 /*
269 * 'usage()' - Show program usage and exit...
270 */
271
272 void
273 usage(void)
274 {
275 _cupsLangPuts(stdout,
276 _("Usage: cupsaddsmb [options] printer1 ... printerN\n"
277 " cupsaddsmb [options] -a\n"
278 "\n"
279 "Options:\n"
280 " -E Encrypt the connection to the server\n"
281 " -H samba-server Use the named SAMBA server\n"
282 " -U samba-user Authenticate using the named SAMBA user\n"
283 " -a Export all printers\n"
284 " -h cups-server Use the named CUPS server\n"
285 " -v Be verbose (show commands)\n"));
286 exit(1);
287 }
288
289
290 /*
291 * End of "$Id: cupsaddsmb.c 6649 2007-07-11 21:46:42Z mike $".
292 */