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