]>
git.ipfire.org Git - thirdparty/cups.git/blob - systemv/cupsaddsmb.c
2 * "cupsaddsmb" command for CUPS.
4 * Copyright 2007-2012 by Apple Inc.
5 * Copyright 2001-2006 by Easy Software Products.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
11 * Include necessary headers...
14 #include <cups/cups-private.h>
15 #include <cups/adminutil.h>
26 const char *SAMBAUser
,
35 int export_dest(http_t
*http
, const char *dest
);
36 void usage(void) __attribute__((noreturn
));
40 * 'main()' - Export printers on the command-line.
43 int /* O - Exit status */
44 main(int argc
, /* I - Number of command-line arguments */
45 char *argv
[]) /* I - Command-line arguments */
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 */
58 * Parse command-line arguments...
63 SAMBAUser
= cupsUser();
67 for (i
= 1; i
< argc
; i
++)
68 if (!strcmp(argv
[i
], "-E"))
71 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
73 _cupsLangPrintf(stderr
,
74 _("%s: Sorry, no encryption support."),
78 else if (!strcmp(argv
[i
], "-H"))
84 SAMBAServer
= argv
[i
];
86 else if (!strcmp(argv
[i
], "-U"))
88 char *sep
; /* Separator for password */
97 if ((sep
= strchr(argv
[i
], '%')) != NULL
)
100 * Nul-terminate the username at the first % and point the
101 * password at the rest...
109 else if (!strcmp(argv
[i
], "-a"))
111 else if (!strcmp(argv
[i
], "-h"))
117 cupsSetServer(argv
[i
]);
119 else if (!strcmp(argv
[i
], "-v"))
121 else if (argv
[i
][0] != '-')
126 * Connect to the server...
129 if ((http
= httpConnectEncrypt(cupsServer(), ippPort(),
130 cupsEncryption())) == NULL
)
132 _cupsLangPrintf(stderr
, _("%s: Unable to connect to server."),
138 if (SAMBAServer
== NULL
)
140 SAMBAServer
= cupsServer();
142 if (SAMBAServer
[0] == '/') /* Use localhost instead of domain socket */
143 SAMBAServer
= "localhost";
146 if ((status
= export_dest(http
, argv
[i
])) != 0)
153 * Connect to the server...
156 if ((http
= httpConnectEncrypt(cupsServer(), ippPort(),
157 cupsEncryption())) == NULL
)
159 _cupsLangPrintf(stderr
, _("%s: Unable to connect to server."), argv
[0]);
164 * See if the user specified "-a"...
170 * Export all printers...
173 if (SAMBAServer
== NULL
)
175 SAMBAServer
= cupsServer();
177 if (SAMBAServer
[0] == '/') /* Use localhost instead of domain socket */
178 SAMBAServer
= "localhost";
181 num_dests
= cupsGetDests2(http
, &dests
);
183 for (j
= 0, status
= 0; j
< num_dests
; j
++)
184 if (!dests
[j
].instance
)
186 if ((status
= export_dest(http
, dests
[j
].name
)) != 0)
190 cupsFreeDests(num_dests
, dests
);
201 * 'export_dest()' - Export a destination to SAMBA.
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 */
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 */
215 * Get the Windows PPD file for the printer...
218 if (!cupsAdminCreateWindowsPPD(http
, dest
, ppdfile
, sizeof(ppdfile
)))
220 _cupsLangPrintf(stderr
,
221 _("cupsaddsmb: No PPD file for printer \"%s\" - %s"),
222 dest
, cupsLastErrorString());
227 * Try to export it...
230 for (status
= 0, tries
= 0; !status
&& tries
< 3; tries
++)
233 * Get the password, as needed...
238 snprintf(prompt
, sizeof(prompt
),
239 _cupsLangString(cupsLangDefault(),
240 _("Password for %s required to access %s via "
242 SAMBAUser
, SAMBAServer
);
244 if ((SAMBAPassword
= cupsGetPassword(prompt
)) == NULL
)
248 status
= cupsAdminExportSamba(dest
, ppdfile
, SAMBAServer
,
249 SAMBAUser
, SAMBAPassword
,
250 Verbosity
? stderr
: NULL
);
252 if (!status
&& cupsLastError() == IPP_NOT_FOUND
)
263 * 'usage()' - Show program usage and exit...
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 "
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."));