]> 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 5233 2006-03-06 03:39:28Z 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 /*
82 * Parse command-line arguments...
83 */
84
85 export_all = 0;
86 http = NULL;
87 SAMBAUser = cupsUser();
88 SAMBAPassword = NULL;
89 SAMBAServer = NULL;
90
91 for (i = 1; i < argc; i ++)
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 }
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 }
133 else if (!strcmp(argv[i], "-a"))
134 export_all = 1;
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 {
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
161 if (SAMBAServer == NULL)
162 SAMBAServer = cupsServer();
163
164 if ((status = export_dest(http, argv[i])) != 0)
165 return (status);
166 }
167 else
168 usage();
169
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
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
194 num_dests = cupsGetDests2(http, &dests);
195
196 for (j = 0, status = 0; j < num_dests; j ++)
197 if (!dests[j].instance)
198 {
199 if ((status = export_dest(http, dests[j].name)) != 0)
200 break;
201 }
202
203 cupsFreeDests(num_dests, dests);
204
205 if (status)
206 return (status);
207 }
208
209 return (0);
210 }
211
212
213 /*
214 * 'export_dest()' - Export a destination to SAMBA.
215 */
216
217 int /* O - 0 on success, non-zero on error */
218 export_dest(http_t *http, /* I - Connection to server */
219 const char *dest) /* I - Destination to export */
220 {
221 int status; /* Status of export */
222 char ppdfile[1024], /* PPD file for printer drivers */
223 prompt[1024]; /* Password prompt */
224
225
226 /*
227 * Get the Windows PPD file for the printer...
228 */
229
230 if (!cupsAdminCreateWindowsPPD(http, dest, ppdfile, sizeof(ppdfile)))
231 {
232 _cupsLangPrintf(stderr,
233 _("cupsaddsmb: No PPD file for printer \"%s\" - "
234 "%s\n"),
235 dest, cupsLastErrorString());
236 return (1);
237 }
238
239 /*
240 * Try to export it...
241 */
242
243 for (status = 0; !status;)
244 {
245 /*
246 * Get the password, as needed...
247 */
248
249 if (!SAMBAPassword)
250 {
251 snprintf(prompt, sizeof(prompt),
252 _("Password for %s required to access %s via SAMBA: "),
253 SAMBAUser, SAMBAServer);
254
255 if ((SAMBAPassword = cupsGetPassword(prompt)) == NULL)
256 break;
257 }
258
259 status = cupsAdminExportSamba(dest, ppdfile, SAMBAServer,
260 SAMBAUser, SAMBAPassword,
261 Verbosity ? stderr : NULL);
262 }
263
264 unlink(ppdfile);
265
266 return (!status);
267 }
268
269
270 /*
271 * 'usage()' - Show program usage and exit...
272 */
273
274 void
275 usage(void)
276 {
277 _cupsLangPuts(stdout,
278 _("Usage: cupsaddsmb [options] printer1 ... printerN\n"
279 " cupsaddsmb [options] -a\n"
280 "\n"
281 "Options:\n"
282 " -E Encrypt the connection to the server\n"
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 /*
293 * End of "$Id: cupsaddsmb.c 5233 2006-03-06 03:39:28Z mike $".
294 */