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