#include "cups.h"
#include "string.h"
#include "http-private.h"
#include <stdlib.h>
#include <ctype.h>
#ifdef WIN32
# include <windows.h>
#endif
static const char *cups_get_password(const char *prompt);
static char *cups_get_line(char *buf, int buflen, FILE *fp);
static http_encryption_t cups_encryption = (http_encryption_t)-1;
static char cups_user[65] = "",
cups_server[256] = "";
static const char *(*cups_pwdcb)(const char *) = cups_get_password;
#ifdef HAVE_DOMAINSOCKETS
char cups_server_domainsocket[104] = "";
#endif
http_encryption_t
cupsEncryption(void)
{
FILE *fp;
char *encryption;
const char *home;
char line[1024];
if (cups_encryption == (http_encryption_t)-1)
{
if ((encryption = getenv("CUPS_ENCRYPTION")) == NULL)
{
if ((home = getenv("HOME")) != NULL)
{
snprintf(line, sizeof(line), "%s/.cupsrc", home);
fp = fopen(line, "r");
}
else
fp = NULL;
if (fp == NULL)
{
if ((home = getenv("CUPS_SERVERROOT")) != NULL)
{
snprintf(line, sizeof(line), "%s/client.conf", home);
fp = fopen(line, "r");
}
else
fp = fopen(CUPS_SERVERROOT "/client.conf", "r");
}
encryption = "IfRequested";
if (fp != NULL)
{
while (cups_get_line(line, sizeof(line), fp) != NULL)
if (strncmp(line, "Encryption ", 11) == 0 ||
strncmp(line, "Encryption\t", 11) == 0)
{
encryption = line + strlen(line) - 1;
if (*encryption == '\n')
*encryption = '\0';
for (encryption = line + 11; isspace(*encryption & 255); encryption ++);
break;
}
fclose(fp);
}
}
if (strcasecmp(encryption, "never") == 0)
cups_encryption = HTTP_ENCRYPT_NEVER;
else if (strcasecmp(encryption, "always") == 0)
cups_encryption = HTTP_ENCRYPT_ALWAYS;
else if (strcasecmp(encryption, "required") == 0)
cups_encryption = HTTP_ENCRYPT_REQUIRED;
else
cups_encryption = HTTP_ENCRYPT_IF_REQUESTED;
}
return (cups_encryption);
}
const char *
cupsGetPassword(const char *prompt)
{
return ((*cups_pwdcb)(prompt));
}
void
cupsSetEncryption(http_encryption_t e)
{
cups_encryption = e;
}
const char *
cupsServer(void)
{
FILE *fp;
char *server;
const char *home;
char line[1024];
if (!cups_server[0])
{
if ((server = getenv("CUPS_SERVER")) == NULL)
{
if ((home = getenv("HOME")) != NULL)
{
snprintf(line, sizeof(line), "%s/.cupsrc", home);
fp = fopen(line, "r");
}
else
fp = NULL;
if (fp == NULL)
{
if ((home = getenv("CUPS_SERVERROOT")) != NULL)
{
snprintf(line, sizeof(line), "%s/client.conf", home);
fp = fopen(line, "r");
}
else
fp = fopen(CUPS_SERVERROOT "/client.conf", "r");
}
server = "localhost";
if (fp != NULL)
{
while (cups_get_line(line, sizeof(line), fp) != NULL)
if (strncmp(line, "ServerName ", 11) == 0 ||
strncmp(line, "ServerName\t", 11) == 0)
{
server = line + strlen(line) - 1;
if (*server == '\n')
*server = '\0';
for (server = line + 11; isspace(*server & 255); server ++);
break;
}
fclose(fp);
}
}
strlcpy(cups_server, server, sizeof(cups_server));
#ifdef HAVE_DOMAINSOCKETS
if (cups_server[0] != '/')
strlcpy(cups_server_domainsocket, CUPS_DEFAULT_DOMAINSOCKET, sizeof(cups_server_domainsocket));
else
{
strlcpy(cups_server_domainsocket, cups_server, sizeof(cups_server));
strlcpy(cups_server, "localhost", sizeof(cups_server));
}
#endif
}
return (cups_server);
}
void
cupsSetPasswordCB(const char *(*cb)(const char *))
{
if (cb == (const char *(*)(const char *))0)
cups_pwdcb = cups_get_password;
else
cups_pwdcb = cb;
}
void
cupsSetServer(const char *server)
{
if (server)
strlcpy(cups_server, server, sizeof(cups_server));
else
cups_server[0] = '\0';
}
void
cupsSetUser(const char *user)
{
if (user)
strlcpy(cups_user, user, sizeof(cups_user));
else
cups_user[0] = '\0';
}
#if defined(WIN32)
const char *
cupsUser(void)
{
if (!cups_user[0])
{
DWORD size;
size = sizeof(cups_user);
if (!GetUserName(cups_user, &size))
{
strcpy(cups_user, "unknown");
}
}
return (cups_user);
}
static const char *
cups_get_password(const char *prompt)
{
return (NULL);
}
#else
# include <pwd.h>
const char *
cupsUser(void)
{
struct passwd *pwd;
if (!cups_user[0])
{
setpwent();
if ((pwd = getpwuid(getuid())) == NULL)
strcpy(cups_user, "unknown");
else
{
setpwent();
strlcpy(cups_user, pwd->pw_name, sizeof(cups_user));
}
setpwent();
}
return (cups_user);
}
static const char *
cups_get_password(const char *prompt)
{
return (getpass(prompt));
}
#endif
static char *
cups_get_line(char *buf,
int buflen,
FILE *fp)
{
char *bufptr;
if (fgets(buf, buflen, fp) == NULL)
return (NULL);
bufptr = buf + strlen(buf) - 1;
if (bufptr < buf)
return (NULL);
while (isspace(*bufptr & 255) && bufptr >= buf)
*bufptr-- = '\0';
return (buf);
}