#include "cups.h"
#include "string.h"
#include "http-private.h"
#include "globals.h"
#include <stdlib.h>
#include <ctype.h>
#ifdef WIN32
# include <windows.h>
#endif
static char *cups_get_line(char *buf, int buflen, FILE *fp);
http_encryption_t
cupsEncryption(void)
{
FILE *fp;
char *encryption;
const char *home;
char line[1024];
cups_globals_t *cg = _cups_globals();
if (cg->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)
cg->cups_encryption = HTTP_ENCRYPT_NEVER;
else if (strcasecmp(encryption, "always") == 0)
cg->cups_encryption = HTTP_ENCRYPT_ALWAYS;
else if (strcasecmp(encryption, "required") == 0)
cg->cups_encryption = HTTP_ENCRYPT_REQUIRED;
else
cg->cups_encryption = HTTP_ENCRYPT_IF_REQUESTED;
}
return (cg->cups_encryption);
}
const char *
cupsGetPassword(const char *prompt)
{
return ((*_cups_globals()->cups_pwdcb)(prompt));
}
void
cupsSetEncryption(http_encryption_t e)
{
_cups_globals()->cups_encryption = e;
}
const char *
cupsServer(void)
{
FILE *fp;
char *server;
const char *home;
char line[1024];
cups_globals_t *cg = _cups_globals();
if (!cg->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(cg->cups_server, server, sizeof(cg->cups_server));
#ifdef HAVE_DOMAINSOCKETS
if (cg->cups_server[0] == '/')
{
strlcpy(cg->cups_server_domainsocket, cg->cups_server, sizeof(cg->cups_server));
strlcpy(cg->cups_server, "localhost", sizeof(cg->cups_server));
}
#endif
}
return (cg->cups_server);
}
void
cupsSetPasswordCB(const char *(*cb)(const char *))
{
cups_globals_t *cg = _cups_globals();
if (cb == (const char *(*)(const char *))0)
cg->cups_pwdcb = cups_get_password;
else
cg->cups_pwdcb = cb;
}
void
cupsSetServer(const char *server)
{
cups_globals_t *cg = _cups_globals();
if (server)
strlcpy(cg->cups_server, server, sizeof(cg->cups_server));
else
cg->cups_server[0] = '\0';
}
void
cupsSetUser(const char *user)
{
cups_globals_t *cg = _cups_globals();
if (user)
strlcpy(cg->cups_user, user, sizeof(cg->cups_user));
else
cg->cups_user[0] = '\0';
}
#if defined(WIN32)
const char *
cupsUser(void)
{
cups_globals_t *cg = _cups_globals();
if (!cg->cups_user[0])
{
DWORD size;
size = sizeof(cg->cups_user);
if (!GetUserName(cg->cups_user, &size))
{
strcpy(cg->cups_user, "unknown");
}
}
return (cg->cups_user);
}
const char *
cups_get_password(const char *prompt)
{
return (NULL);
}
#else
# include <pwd.h>
const char *
cupsUser(void)
{
struct passwd *pwd;
cups_globals_t *cg = _cups_globals();
if (!cg->cups_user[0])
{
setpwent();
if ((pwd = getpwuid(getuid())) == NULL)
strcpy(cg->cups_user, "unknown");
else
{
setpwent();
strlcpy(cg->cups_user, pwd->pw_name, sizeof(cg->cups_user));
}
setpwent();
}
return (cg->cups_user);
}
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 (bufptr >= buf && isspace(*bufptr & 255))
*bufptr-- = '\0';
return (buf);
}