]> 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 5925 2006-09-05 19:43:11Z mike $"
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 *
26 * main() - Export printers on the command-line.
27 * export_dest() - Export a destination to SAMBA.
28 * usage() - Show program usage and exit...
29 */
30
31 /*
32 * Include necessary headers...
33 */
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <cups/string.h>
38 #include <cups/adminutil.h>
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
61 int export_dest(http_t *http, const char *dest);
62 void usage(void);
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? */
76 http_t *http; /* Connection to server */
77 int num_dests; /* Number of printers */
78 cups_dest_t *dests; /* Printers */
79
80
81 _cupsSetLocale(argv);
82
83 /*
84 * Parse command-line arguments...
85 */
86
87 export_all = 0;
88 http = NULL;
89 SAMBAUser = cupsUser();
90 SAMBAPassword = NULL;
91 SAMBAServer = NULL;
92
93 for (i = 1; i < argc; i ++)
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 }
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 }
135 else if (!strcmp(argv[i], "-a"))
136 export_all = 1;
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 {
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
163 if (SAMBAServer == NULL)
164 SAMBAServer = cupsServer();
165
166 if ((status = export_dest(http, argv[i])) != 0)
167 return (status);
168 }
169 else
170 usage();
171
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
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
196 num_dests = cupsGetDests2(http, &dests);
197
198 for (j = 0, status = 0; j < num_dests; j ++)
199 if (!dests[j].instance)
200 {
201 if ((status = export_dest(http, dests[j].name)) != 0)
202 break;
203 }
204
205 cupsFreeDests(num_dests, dests);
206
207 if (status)
208 return (status);
209 }
210
211 return (0);
212 }
213
214
215 /*
216 * 'export_dest()' - Export a destination to SAMBA.
217 */
218
219 int /* O - 0 on success, non-zero on error */
220 export_dest(http_t *http, /* I - Connection to server */
221 const char *dest) /* I - Destination to export */
222 {
223 int status; /* Status of export */
224 char ppdfile[1024], /* PPD file for printer drivers */
225 prompt[1024]; /* Password prompt */
226
227
228 /*
229 * Get the Windows PPD file for the printer...
230 */
231
232 if (!cupsAdminCreateWindowsPPD(http, dest, ppdfile, sizeof(ppdfile)))
233 {
234 _cupsLangPrintf(stderr,
235 _("cupsaddsmb: No PPD file for printer \"%s\" - "
236 "%s\n"),
237 dest, cupsLastErrorString());
238 return (1);
239 }
240
241 /*
242 * Try to export it...
243 */
244
245 for (status = 0; !status;)
246 {
247 /*
248 * Get the password, as needed...
249 */
250
251 if (!SAMBAPassword)
252 {
253 snprintf(prompt, sizeof(prompt),
254 _cupsLangString(cupsLangDefault(),
255 _("Password for %s required to access %s via "
256 "SAMBA: ")),
257 SAMBAUser, SAMBAServer);
258
259 if ((SAMBAPassword = cupsGetPassword(prompt)) == NULL)
260 break;
261 }
262
263 status = cupsAdminExportSamba(dest, ppdfile, SAMBAServer,
264 SAMBAUser, SAMBAPassword,
265 Verbosity ? stderr : NULL);
266 }
267
268 unlink(ppdfile);
269
270 return (!status);
271 }
272
273
274 /*
275 * 'usage()' - Show program usage and exit...
276 */
277
278 void
279 usage(void)
280 {
281 _cupsLangPuts(stdout,
282 _("Usage: cupsaddsmb [options] printer1 ... printerN\n"
283 " cupsaddsmb [options] -a\n"
284 "\n"
285 "Options:\n"
286 " -E Encrypt the connection to the server\n"
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 /*
297 * End of "$Id: cupsaddsmb.c 5925 2006-09-05 19:43:11Z mike $".
298 */