#include <cups/cups-private.h>
#include <cups/adminutil.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/wait.h>
int Verbosity = 0;
const char *SAMBAUser,
*SAMBAPassword,
*SAMBAServer;
int export_dest(http_t *http, const char *dest);
void usage(void) __attribute__((noreturn));
int
main(int argc,
char *argv[])
{
int i, j;
int status;
int export_all;
http_t *http;
int num_dests;
cups_dest_t *dests;
_cupsSetLocale(argv);
export_all = 0;
http = NULL;
SAMBAUser = cupsUser();
SAMBAPassword = NULL;
SAMBAServer = NULL;
for (i = 1; i < argc; i ++)
if (!strcmp(argv[i], "-E"))
{
#ifdef HAVE_SSL
cupsSetEncryption(HTTP_ENCRYPT_REQUIRED);
#else
_cupsLangPrintf(stderr,
_("%s: Sorry, no encryption support."),
argv[0]);
#endif
}
else if (!strcmp(argv[i], "-H"))
{
i ++;
if (i >= argc)
usage();
SAMBAServer = argv[i];
}
else if (!strcmp(argv[i], "-U"))
{
char *sep;
i ++;
if (i >= argc)
usage();
SAMBAUser = argv[i];
if ((sep = strchr(argv[i], '%')) != NULL)
{
*sep++ = '\0';
SAMBAPassword = sep;
}
}
else if (!strcmp(argv[i], "-a"))
export_all = 1;
else if (!strcmp(argv[i], "-h"))
{
i ++;
if (i >= argc)
usage();
cupsSetServer(argv[i]);
}
else if (!strcmp(argv[i], "-v"))
Verbosity = 1;
else if (argv[i][0] != '-')
{
if (!http)
{
if ((http = httpConnectEncrypt(cupsServer(), ippPort(),
cupsEncryption())) == NULL)
{
_cupsLangPrintf(stderr, _("%s: Unable to connect to server."),
argv[0]);
exit(1);
}
}
if (SAMBAServer == NULL)
{
SAMBAServer = cupsServer();
if (SAMBAServer[0] == '/')
SAMBAServer = "localhost";
}
if ((status = export_dest(http, argv[i])) != 0)
return (status);
}
else
usage();
if ((http = httpConnectEncrypt(cupsServer(), ippPort(),
cupsEncryption())) == NULL)
{
_cupsLangPrintf(stderr, _("%s: Unable to connect to server."), argv[0]);
exit(1);
}
if (export_all)
{
if (SAMBAServer == NULL)
{
SAMBAServer = cupsServer();
if (SAMBAServer[0] == '/')
SAMBAServer = "localhost";
}
num_dests = cupsGetDests2(http, &dests);
for (j = 0, status = 0; j < num_dests; j ++)
if (!dests[j].instance)
{
if ((status = export_dest(http, dests[j].name)) != 0)
break;
}
cupsFreeDests(num_dests, dests);
if (status)
return (status);
}
return (0);
}
int
export_dest(http_t *http,
const char *dest)
{
int status;
char ppdfile[1024],
prompt[1024];
int tries;
if (!cupsAdminCreateWindowsPPD(http, dest, ppdfile, sizeof(ppdfile)))
{
_cupsLangPrintf(stderr,
_("cupsaddsmb: No PPD file for printer \"%s\" - %s"),
dest, cupsLastErrorString());
return (1);
}
for (status = 0, tries = 0; !status && tries < 3; tries ++)
{
if (!SAMBAPassword)
{
snprintf(prompt, sizeof(prompt),
_cupsLangString(cupsLangDefault(),
_("Password for %s required to access %s via "
"SAMBA: ")),
SAMBAUser, SAMBAServer);
if ((SAMBAPassword = cupsGetPassword(prompt)) == NULL)
break;
}
status = cupsAdminExportSamba(dest, ppdfile, SAMBAServer,
SAMBAUser, SAMBAPassword,
Verbosity ? stderr : NULL);
if (!status && cupsLastError() == IPP_NOT_FOUND)
break;
}
unlink(ppdfile);
return (!status);
}
void
usage(void)
{
_cupsLangPuts(stdout, _("Usage: cupsaddsmb [options] printer1 ... printerN"));
_cupsLangPuts(stdout, _(" cupsaddsmb [options] -a"));
_cupsLangPuts(stdout, "");
_cupsLangPuts(stdout, _("Options:"));
_cupsLangPuts(stdout, _(" -E Encrypt the connection."));
_cupsLangPuts(stdout, _(" -H samba-server Use the named SAMBA "
"server."));
_cupsLangPuts(stdout, _(" -U username Specify username."));
_cupsLangPuts(stdout, _(" -a Export all printers."));
_cupsLangPuts(stdout, _(" -h server[:port] Specify server address."));
_cupsLangPuts(stdout, _(" -v Be verbose."));
exit(1);
}