]> git.ipfire.org Git - thirdparty/cups.git/blame - systemv/cupsaddsmb.c
Merge changes from CUPS 1.5svn-r8849.
[thirdparty/cups.git] / systemv / cupsaddsmb.c
CommitLineData
ef416fc2 1/*
75bd9771 2 * "$Id: cupsaddsmb.c 7033 2007-10-19 02:11:28Z mike $"
ef416fc2 3 *
4 * "cupsaddsmb" command for the Common UNIX Printing System (CUPS).
5 *
4d301e69 6 * Copyright 2007-2009 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,
4d301e69 91 _("%s: Sorry, no encryption support compiled in\n"),
757d2cad 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)
2fb76298 155 {
ef416fc2 156 SAMBAServer = cupsServer();
157
2fb76298
MS
158 if (SAMBAServer[0] == '/') /* Use localhost instead of domain socket */
159 SAMBAServer = "localhost";
160 }
161
757d2cad 162 if ((status = export_dest(http, argv[i])) != 0)
ef416fc2 163 return (status);
164 }
165 else
166 usage();
167
757d2cad 168 /*
169 * Connect to the server...
170 */
171
172 if ((http = httpConnectEncrypt(cupsServer(), ippPort(),
173 cupsEncryption())) == NULL)
174 {
175 _cupsLangPrintf(stderr, _("%s: Unable to connect to server\n"), argv[0]);
176 exit(1);
177 }
178
ef416fc2 179 /*
180 * See if the user specified "-a"...
181 */
182
183 if (export_all)
184 {
185 /*
186 * Export all printers...
187 */
188
189 if (SAMBAServer == NULL)
2fb76298 190 {
ef416fc2 191 SAMBAServer = cupsServer();
192
2fb76298
MS
193 if (SAMBAServer[0] == '/') /* Use localhost instead of domain socket */
194 SAMBAServer = "localhost";
195 }
196
757d2cad 197 num_dests = cupsGetDests2(http, &dests);
ef416fc2 198
199 for (j = 0, status = 0; j < num_dests; j ++)
200 if (!dests[j].instance)
201 {
757d2cad 202 if ((status = export_dest(http, dests[j].name)) != 0)
ef416fc2 203 break;
204 }
205
206 cupsFreeDests(num_dests, dests);
207
208 if (status)
209 return (status);
210 }
211
212 return (0);
213}
214
215
ef416fc2 216/*
217 * 'export_dest()' - Export a destination to SAMBA.
218 */
219
220int /* O - 0 on success, non-zero on error */
757d2cad 221export_dest(http_t *http, /* I - Connection to server */
222 const char *dest) /* I - Destination to export */
ef416fc2 223{
757d2cad 224 int status; /* Status of export */
225 char ppdfile[1024], /* PPD file for printer drivers */
226 prompt[1024]; /* Password prompt */
ef416fc2 227
228
229 /*
757d2cad 230 * Get the Windows PPD file for the printer...
ef416fc2 231 */
232
757d2cad 233 if (!cupsAdminCreateWindowsPPD(http, dest, ppdfile, sizeof(ppdfile)))
ef416fc2 234 {
fa73b229 235 _cupsLangPrintf(stderr,
ef416fc2 236 _("cupsaddsmb: No PPD file for printer \"%s\" - "
b423cd4c 237 "%s\n"),
238 dest, cupsLastErrorString());
757d2cad 239 return (1);
ef416fc2 240 }
241
242 /*
757d2cad 243 * Try to export it...
ef416fc2 244 */
245
757d2cad 246 for (status = 0; !status;)
ef416fc2 247 {
ef416fc2 248 /*
757d2cad 249 * Get the password, as needed...
ef416fc2 250 */
251
757d2cad 252 if (!SAMBAPassword)
ef416fc2 253 {
757d2cad 254 snprintf(prompt, sizeof(prompt),
8ca02f3c 255 _cupsLangString(cupsLangDefault(),
256 _("Password for %s required to access %s via "
257 "SAMBA: ")),
757d2cad 258 SAMBAUser, SAMBAServer);
ef416fc2 259
757d2cad 260 if ((SAMBAPassword = cupsGetPassword(prompt)) == NULL)
261 break;
ef416fc2 262 }
263
757d2cad 264 status = cupsAdminExportSamba(dest, ppdfile, SAMBAServer,
265 SAMBAUser, SAMBAPassword,
266 Verbosity ? stderr : NULL);
bc44d920 267
268 if (!status && cupsLastError() == IPP_NOT_FOUND)
269 break;
ef416fc2 270 }
271
272 unlink(ppdfile);
273
757d2cad 274 return (!status);
ef416fc2 275}
276
277
278/*
279 * 'usage()' - Show program usage and exit...
280 */
281
282void
283usage(void)
284{
fa73b229 285 _cupsLangPuts(stdout,
ef416fc2 286 _("Usage: cupsaddsmb [options] printer1 ... printerN\n"
287 " cupsaddsmb [options] -a\n"
288 "\n"
289 "Options:\n"
757d2cad 290 " -E Encrypt the connection to the server\n"
ef416fc2 291 " -H samba-server Use the named SAMBA server\n"
292 " -U samba-user Authenticate using the named SAMBA user\n"
293 " -a Export all printers\n"
294 " -h cups-server Use the named CUPS server\n"
295 " -v Be verbose (show commands)\n"));
296 exit(1);
297}
298
299
300/*
75bd9771 301 * End of "$Id: cupsaddsmb.c 7033 2007-10-19 02:11:28Z mike $".
ef416fc2 302 */