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