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