1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
|
/*
* "$Id: usersys.c 7721 2008-07-11 22:48:49Z mike $"
*
* User, system, and password routines for the Common UNIX Printing
* System (CUPS).
*
* Copyright 2007 by Apple Inc.
* Copyright 1997-2006 by Easy Software Products.
*
* These coded instructions, statements, and computer programs are the
* property of Apple Inc. and are protected by Federal copyright
* law. Distribution and use rights are outlined in the file "LICENSE.txt"
* which should have been included with this file. If this file is
* file is missing or damaged, see the license at "http://www.cups.org/".
*
* This file is subject to the Apple OS-Developed Software exception.
*
* Contents:
*
* cupsEncryption() - Get the default encryption settings.
* cupsGetPassword() - Get a password from the user.
* cupsServer() - Return the hostname of the default server.
* cupsSetEncryption() - Set the encryption preference.
* cupsSetPasswordCB() - Set the password callback for CUPS.
* cupsSetServer() - Set the default server name.
* cupsSetUser() - Set the default user name.
* cupsUser() - Return the current users name.
* _cupsGetPassword() - Get a password from the user.
* cups_open_client_conf() - Open the client.conf file.
*/
/*
* Include necessary headers...
*/
#include "http-private.h"
#include "globals.h"
#include <stdlib.h>
#include <sys/stat.h>
#ifdef WIN32
# include <windows.h>
#endif /* WIN32 */
#include "debug.h"
/*
* Local functions...
*/
static cups_file_t *cups_open_client_conf(void);
/*
* 'cupsEncryption()' - Get the default encryption settings.
*
* The default encryption setting comes from the CUPS_ENCRYPTION
* environment variable, then the ~/.cupsrc file, and finally the
* /etc/cups/client.conf file. If not set, the default is
* HTTP_ENCRYPT_IF_REQUESTED.
*/
http_encryption_t /* O - Encryption settings */
cupsEncryption(void)
{
cups_file_t *fp; /* client.conf file */
char *encryption; /* CUPS_ENCRYPTION variable */
char line[1024], /* Line from file */
*value; /* Value on line */
int linenum; /* Line number */
_cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
/*
* First see if we have already set the encryption stuff...
*/
if (cg->encryption == (http_encryption_t)-1)
{
/*
* Then see if the CUPS_ENCRYPTION environment variable is set...
*/
if ((encryption = getenv("CUPS_ENCRYPTION")) == NULL)
{
/*
* No, open the client.conf file...
*/
fp = cups_open_client_conf();
encryption = "IfRequested";
if (fp)
{
/*
* Read the config file and look for an Encryption line...
*/
linenum = 0;
while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum) != NULL)
if (!strcasecmp(line, "Encryption") && value)
{
/*
* Got it!
*/
encryption = value;
break;
}
cupsFileClose(fp);
}
}
/*
* Set the encryption preference...
*/
if (!strcasecmp(encryption, "never"))
cg->encryption = HTTP_ENCRYPT_NEVER;
else if (!strcasecmp(encryption, "always"))
cg->encryption = HTTP_ENCRYPT_ALWAYS;
else if (!strcasecmp(encryption, "required"))
cg->encryption = HTTP_ENCRYPT_REQUIRED;
else
cg->encryption = HTTP_ENCRYPT_IF_REQUESTED;
}
return (cg->encryption);
}
/*
* 'cupsGetPassword()' - Get a password from the user.
*
* Uses the current password callback function. Returns NULL if the
* user does not provide a password.
*/
const char * /* O - Password */
cupsGetPassword(const char *prompt) /* I - Prompt string */
{
return ((*_cupsGlobals()->password_cb)(prompt));
}
/*
* 'cupsSetEncryption()' - Set the encryption preference.
*/
void
cupsSetEncryption(http_encryption_t e) /* I - New encryption preference */
{
_cupsGlobals()->encryption = e;
}
/*
* 'cupsServer()' - Return the hostname/address of the default server.
*
* The returned value can be a fully-qualified hostname, a numeric
* IPv4 or IPv6 address, or a domain socket pathname.
*/
const char * /* O - Server name */
cupsServer(void)
{
cups_file_t *fp; /* client.conf file */
char *server; /* Pointer to server name */
char *port; /* Port number */
char line[1024], /* Line from file */
*value; /* Value on line */
int linenum; /* Line number in file */
#ifdef CUPS_DEFAULT_DOMAINSOCKET
struct stat sockinfo; /* Domain socket information */
#endif /* CUPS_DEFAULT_DOMAINSOCKET */
_cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
/*
* First see if we have already set the server name...
*/
if (!cg->server[0])
{
/*
* Then see if the CUPS_SERVER environment variable is set...
*/
if ((server = getenv("CUPS_SERVER")) == NULL)
{
/*
* No environment variable, try the client.conf file...
*/
fp = cups_open_client_conf();
#ifdef CUPS_DEFAULT_DOMAINSOCKET
/*
* If we are compiled with domain socket support, only use the
* domain socket if it exists and has the right permissions...
*/
if (!stat(CUPS_DEFAULT_DOMAINSOCKET, &sockinfo) &&
(sockinfo.st_mode & S_IRWXO) == S_IRWXO)
server = CUPS_DEFAULT_DOMAINSOCKET;
else
#endif /* CUPS_DEFAULT_DOMAINSOCKET */
server = "localhost";
if (fp)
{
/*
* Read the config file and look for a ServerName line...
*/
linenum = 0;
while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum) != NULL)
{
DEBUG_printf(("cupsServer: %d: %s %s\n", linenum, line,
value ? value : "(null)"));
if (!strcasecmp(line, "ServerName") && value)
{
/*
* Got it!
*/
DEBUG_puts("cupsServer: Got a ServerName line!");
server = value;
break;
}
}
cupsFileClose(fp);
}
}
/*
* Copy the server name over and set the port number, if any...
*/
DEBUG_printf(("cupsServer: Using server \"%s\"...\n", server));
strlcpy(cg->server, server, sizeof(cg->server));
if (cg->server[0] != '/' && (port = strrchr(cg->server, ':')) != NULL &&
!strchr(port, ']') && isdigit(port[1] & 255))
{
*port++ = '\0';
DEBUG_printf(("cupsServer: Using port %d...\n", atoi(port)));
ippSetPort(atoi(port));
}
if (cg->server[0] == '/')
strcpy(cg->servername, "localhost");
else
strlcpy(cg->servername, cg->server, sizeof(cg->servername));
}
return (cg->server);
}
/*
* 'cupsSetPasswordCB()' - Set the password callback for CUPS.
*
* Pass NULL to restore the default (console) password callback.
*/
void
cupsSetPasswordCB(cups_password_cb_t cb)/* I - Callback function */
{
_cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
if (cb == (const char *(*)(const char *))0)
cg->password_cb = _cupsGetPassword;
else
cg->password_cb = cb;
}
/*
* 'cupsSetServer()' - Set the default server name.
*
* The "server" string can be a fully-qualified hostname, a numeric
* IPv4 or IPv6 address, or a domain socket pathname. Pass NULL to
* restore the default server name.
*/
void
cupsSetServer(const char *server) /* I - Server name */
{
char *port; /* Pointer to port */
_cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
if (server)
{
strlcpy(cg->server, server, sizeof(cg->server));
if (cg->server[0] != '/' && (port = strrchr(cg->server, ':')) != NULL &&
!strchr(port, ']') && isdigit(port[1] & 255))
{
*port++ = '\0';
ippSetPort(atoi(port));
}
if (cg->server[0] == '/')
strcpy(cg->servername, "localhost");
else
strlcpy(cg->servername, cg->server, sizeof(cg->servername));
}
else
{
cg->server[0] = '\0';
cg->servername[0] = '\0';
}
}
/*
* 'cupsSetUser()' - Set the default user name.
*
* Pass NULL to restore the default user name.
*/
void
cupsSetUser(const char *user) /* I - User name */
{
_cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
if (user)
strlcpy(cg->user, user, sizeof(cg->user));
else
cg->user[0] = '\0';
}
#if defined(WIN32)
/*
* WIN32 username and password stuff.
*/
/*
* 'cupsUser()' - Return the current user's name.
*/
const char * /* O - User name */
cupsUser(void)
{
_cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
if (!cg->user[0])
{
DWORD size; /* Size of string */
size = sizeof(cg->user);
if (!GetUserName(cg->user, &size))
{
/*
* Use the default username...
*/
strcpy(cg->user, "unknown");
}
}
return (cg->user);
}
/*
* '_cupsGetPassword()' - Get a password from the user.
*/
const char * /* O - Password */
_cupsGetPassword(const char *prompt) /* I - Prompt string */
{
return (NULL);
}
#else
/*
* UNIX username and password stuff...
*/
# include <pwd.h>
/*
* 'cupsUser()' - Return the current user's name.
*/
const char * /* O - User name */
cupsUser(void)
{
struct passwd *pwd; /* User/password entry */
_cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
if (!cg->user[0])
{
/*
* Rewind the password file...
*/
setpwent();
/*
* Lookup the password entry for the current user.
*/
if ((pwd = getpwuid(getuid())) == NULL)
strcpy(cg->user, "unknown"); /* Unknown user! */
else
{
/*
* Copy the username...
*/
setpwent();
strlcpy(cg->user, pwd->pw_name, sizeof(cg->user));
}
/*
* Rewind the password file again...
*/
setpwent();
}
return (cg->user);
}
/*
* '_cupsGetPassword()' - Get a password from the user.
*/
const char * /* O - Password */
_cupsGetPassword(const char *prompt) /* I - Prompt string */
{
return (getpass(prompt));
}
#endif /* WIN32 */
/*
* 'cups_open_client_conf()' - Open the client.conf file.
*/
static cups_file_t * /* O - File or NULL */
cups_open_client_conf(void)
{
cups_file_t *fp; /* File */
const char *home; /* Home directory of user */
char filename[1024]; /* Filename */
_cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
if ((home = getenv("HOME")) != NULL)
{
/*
* Look for ~/.cups/client.conf or ~/.cupsrc...
*/
snprintf(filename, sizeof(filename), "%s/.cups/client.conf", home);
if ((fp = cupsFileOpen(filename, "r")) != NULL)
{
DEBUG_printf(("cups_open_client_conf: Using \"%s\"...\n", filename));
return (fp);
}
snprintf(filename, sizeof(filename), "%s/.cupsrc", home);
if ((fp = cupsFileOpen(filename, "r")) != NULL)
{
DEBUG_printf(("cups_open_client_conf: Using \"%s\"...\n", filename));
return (fp);
}
}
snprintf(filename, sizeof(filename), "%s/client.conf", cg->cups_serverroot);
return (cupsFileOpen(filename, "r"));
}
/*
* End of "$Id: usersys.c 7721 2008-07-11 22:48:49Z mike $".
*/
|