]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/usersys.c
Make CUPS API threadsafe (STR #1276), replace FILE's with
[thirdparty/cups.git] / cups / usersys.c
CommitLineData
6b67a15e 1/*
c9d3f842 2 * "$Id$"
6b67a15e 3 *
4 * User, system, and password routines for the Common UNIX Printing
5 * System (CUPS).
6 *
c9d3f842 7 * Copyright 1997-2005 by Easy Software Products.
6b67a15e 8 *
9 * These coded instructions, statements, and computer programs are the
10 * property of Easy Software Products and are protected by Federal
11 * copyright law. Distribution and use rights are outlined in the file
12 * "LICENSE.txt" which should have been included with this file. If this
13 * file is missing or damaged please contact Easy Software Products
14 * at:
15 *
16 * Attn: CUPS Licensing Information
17 * Easy Software Products
18 * 44141 Airport View Drive, Suite 204
c9d3f842 19 * Hollywood, Maryland 20636 USA
6b67a15e 20 *
c4dcf3cc 21 * Voice: (301) 373-9600
6b67a15e 22 * EMail: cups-info@cups.org
23 * WWW: http://www.cups.org
24 *
dab1a4d8 25 * This file is subject to the Apple OS-Developed Software exception.
26 *
6b67a15e 27 * Contents:
28 *
2a0ef17a 29 * cupsEncryption() - Get the default encryption settings...
c68b6ae8 30 * cupsGetPassword() - Get a password from the user...
31 * cupsServer() - Return the hostname of the default server...
2a0ef17a 32 * cupsSetEncryption() - Set the encryption preference.
c68b6ae8 33 * cupsSetPasswordCB() - Set the password callback for CUPS.
34 * cupsSetServer() - Set the default server name...
35 * cupsSetUser() - Set the default user name...
36 * cupsUser() - Return the current users name.
03f61bf3 37 * _cupsGetPassword() - Get a password from the user...
6b67a15e 38 */
39
40/*
41 * Include necessary headers...
42 */
43
c6075312 44#include "http-private.h"
03f61bf3 45#include "globals.h"
6b67a15e 46#include <stdlib.h>
e335a094 47#ifdef WIN32
48# include <windows.h>
49#endif /* WIN32 */
50
6b67a15e 51
2a0ef17a 52/*
53 * 'cupsEncryption()' - Get the default encryption settings...
54 */
55
56http_encryption_t
57cupsEncryption(void)
58{
03f61bf3 59 cups_file_t *fp; /* client.conf file */
2a0ef17a 60 char *encryption; /* CUPS_ENCRYPTION variable */
61 const char *home; /* Home directory of user */
6db7190f 62 char line[1024]; /* Line from file */
03f61bf3 63 cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
2a0ef17a 64
65
66 /*
67 * First see if we have already set the encryption stuff...
68 */
69
03f61bf3 70 if (cg->encryption == (http_encryption_t)-1)
2a0ef17a 71 {
72 /*
73 * Then see if the CUPS_ENCRYPTION environment variable is set...
74 */
75
76 if ((encryption = getenv("CUPS_ENCRYPTION")) == NULL)
77 {
78 /*
79 * Next check to see if we have a $HOME/.cupsrc or client.conf file...
80 */
81
82 if ((home = getenv("HOME")) != NULL)
83 {
84 snprintf(line, sizeof(line), "%s/.cupsrc", home);
03f61bf3 85 fp = cupsFileOpen(line, "r");
2a0ef17a 86 }
87 else
88 fp = NULL;
89
90 if (fp == NULL)
91 {
92 if ((home = getenv("CUPS_SERVERROOT")) != NULL)
93 {
94 snprintf(line, sizeof(line), "%s/client.conf", home);
03f61bf3 95 fp = cupsFileOpen(line, "r");
2a0ef17a 96 }
97 else
03f61bf3 98 fp = cupsFileOpen(CUPS_SERVERROOT "/client.conf", "r");
2a0ef17a 99 }
100
101 encryption = "IfRequested";
102
103 if (fp != NULL)
104 {
105 /*
6cf4fe45 106 * Read the config file and look for an Encryption line...
2a0ef17a 107 */
108
03f61bf3 109 while (cupsFileGets(fp, line, sizeof(line)) != NULL)
759f5023 110 if (strncmp(line, "Encryption ", 11) == 0 ||
111 strncmp(line, "Encryption\t", 11) == 0)
2a0ef17a 112 {
113 /*
114 * Got it! Drop any trailing newline and find the name...
115 */
116
117 encryption = line + strlen(line) - 1;
118 if (*encryption == '\n')
119 *encryption = '\0';
120
64bbab09 121 for (encryption = line + 11; isspace(*encryption & 255); encryption ++);
2a0ef17a 122 break;
123 }
124
03f61bf3 125 cupsFileClose(fp);
2a0ef17a 126 }
127 }
128
129 /*
130 * Set the encryption preference...
131 */
132
133 if (strcasecmp(encryption, "never") == 0)
03f61bf3 134 cg->encryption = HTTP_ENCRYPT_NEVER;
2a0ef17a 135 else if (strcasecmp(encryption, "always") == 0)
03f61bf3 136 cg->encryption = HTTP_ENCRYPT_ALWAYS;
2a0ef17a 137 else if (strcasecmp(encryption, "required") == 0)
03f61bf3 138 cg->encryption = HTTP_ENCRYPT_REQUIRED;
2a0ef17a 139 else
03f61bf3 140 cg->encryption = HTTP_ENCRYPT_IF_REQUESTED;
2a0ef17a 141 }
142
03f61bf3 143 return (cg->encryption);
2a0ef17a 144}
145
146
6b67a15e 147/*
148 * 'cupsGetPassword()' - Get a password from the user...
149 */
150
063e1ac7 151const char * /* O - Password */
6b67a15e 152cupsGetPassword(const char *prompt) /* I - Prompt string */
153{
03f61bf3 154 return ((*_cupsGlobals()->password_cb)(prompt));
6b67a15e 155}
6b67a15e 156
6b67a15e 157
2a0ef17a 158/*
159 * 'cupsSetEncryption()' - Set the encryption preference.
160 */
161
162void
163cupsSetEncryption(http_encryption_t e) /* I - New encryption preference */
164{
03f61bf3 165 _cupsGlobals()->encryption = e;
2a0ef17a 166}
167
168
6b67a15e 169/*
c68b6ae8 170 * 'cupsServer()' - Return the hostname of the default server...
6b67a15e 171 */
172
c68b6ae8 173const char * /* O - Server name */
174cupsServer(void)
6b67a15e 175{
03f61bf3 176 cups_file_t *fp; /* client.conf file */
c68b6ae8 177 char *server; /* Pointer to server name */
178 const char *home; /* Home directory of user */
1fcc120e 179 char *port; /* Port number */
03f61bf3 180 char line[1024], /* Line from file */
181 *value; /* Value on line */
182 int linenum; /* Line number in file */
183 cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
6b67a15e 184
185
186 /*
c68b6ae8 187 * First see if we have already set the server name...
6b67a15e 188 */
189
03f61bf3 190 if (!cg->server[0])
c68b6ae8 191 {
192 /*
193 * Then see if the CUPS_SERVER environment variable is set...
194 */
6b67a15e 195
c68b6ae8 196 if ((server = getenv("CUPS_SERVER")) == NULL)
197 {
198 /*
199 * Next check to see if we have a $HOME/.cupsrc or client.conf file...
200 */
201
202 if ((home = getenv("HOME")) != NULL)
203 {
04de52f8 204 snprintf(line, sizeof(line), "%s/.cupsrc", home);
03f61bf3 205 fp = cupsFileOpen(line, "r");
c68b6ae8 206 }
207 else
208 fp = NULL;
6b67a15e 209
c68b6ae8 210 if (fp == NULL)
211 {
212 if ((home = getenv("CUPS_SERVERROOT")) != NULL)
213 {
04de52f8 214 snprintf(line, sizeof(line), "%s/client.conf", home);
03f61bf3 215 fp = cupsFileOpen(line, "r");
c68b6ae8 216 }
217 else
03f61bf3 218 fp = cupsFileOpen(CUPS_SERVERROOT "/client.conf", "r");
c68b6ae8 219 }
6b67a15e 220
c68b6ae8 221 server = "localhost";
6b67a15e 222
c68b6ae8 223 if (fp != NULL)
224 {
225 /*
226 * Read the config file and look for a ServerName line...
227 */
228
03f61bf3 229 linenum = 0;
230 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum) != NULL)
231 if (!strcmp(line, "ServerName") && value)
c68b6ae8 232 {
233 /*
03f61bf3 234 * Got it!
c68b6ae8 235 */
236
03f61bf3 237 server = value;
c68b6ae8 238 break;
239 }
240
03f61bf3 241 cupsFileClose(fp);
c68b6ae8 242 }
243 }
6b67a15e 244
c68b6ae8 245 /*
1fcc120e 246 * Copy the server name over and set the port number, if any...
c68b6ae8 247 */
248
03f61bf3 249 strlcpy(cg->server, server, sizeof(cg->server));
c6075312 250
03f61bf3 251 if (cg->server[0] != '/' && (port = strrchr(cg->server, ':')) != NULL &&
1fcc120e 252 isdigit(port[1] & 255))
c6075312 253 {
1fcc120e 254 *port++ = '\0';
255
256 ippSetPort(atoi(port));
257 }
c68b6ae8 258 }
259
03f61bf3 260 return (cg->server);
6b67a15e 261}
262
263
264/*
c68b6ae8 265 * 'cupsSetPasswordCB()' - Set the password callback for CUPS.
6b67a15e 266 */
267
c68b6ae8 268void
269cupsSetPasswordCB(const char *(*cb)(const char *)) /* I - Callback function */
6b67a15e 270{
03f61bf3 271 cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
272
273
c68b6ae8 274 if (cb == (const char *(*)(const char *))0)
03f61bf3 275 cg->password_cb = _cupsGetPassword;
c68b6ae8 276 else
03f61bf3 277 cg->password_cb = cb;
6b67a15e 278}
6b67a15e 279
280
281/*
c68b6ae8 282 * 'cupsSetServer()' - Set the default server name...
6b67a15e 283 */
284
c68b6ae8 285void
286cupsSetServer(const char *server) /* I - Server name */
6b67a15e 287{
03f61bf3 288 cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
289
290
c68b6ae8 291 if (server)
03f61bf3 292 strlcpy(cg->server, server, sizeof(cg->server));
c68b6ae8 293 else
03f61bf3 294 cg->server[0] = '\0';
c68b6ae8 295}
6b67a15e 296
6b67a15e 297
c68b6ae8 298/*
299 * 'cupsSetUser()' - Set the default user name...
300 */
6b67a15e 301
c68b6ae8 302void
303cupsSetUser(const char *user) /* I - User name */
304{
03f61bf3 305 cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
306
307
c68b6ae8 308 if (user)
03f61bf3 309 strlcpy(cg->user, user, sizeof(cg->user));
caa58f49 310 else
03f61bf3 311 cg->user[0] = '\0';
c68b6ae8 312}
313
314
e335a094 315#if defined(WIN32)
c68b6ae8 316/*
e335a094 317 * WIN32 username and password stuff...
c68b6ae8 318 */
319
320/*
321 * 'cupsUser()' - Return the current user's name.
322 */
323
324const char * /* O - User name */
325cupsUser(void)
326{
03f61bf3 327 cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
328
329
330 if (!cg->user[0])
e335a094 331 {
332 DWORD size; /* Size of string */
333
334
03f61bf3 335 size = sizeof(cg->user);
336 if (!GetUserName(cg->user, &size))
e335a094 337 {
338 /*
339 * Use the default username...
340 */
341
03f61bf3 342 strcpy(cg->user, "unknown");
e335a094 343 }
344 }
c68b6ae8 345
03f61bf3 346 return (cg->user);
c68b6ae8 347}
348
349
350/*
03f61bf3 351 * '_cupsGetPassword()' - Get a password from the user...
c68b6ae8 352 */
353
03f61bf3 354const char * /* O - Password */
355_cupsGetPassword(const char *prompt) /* I - Prompt string */
c68b6ae8 356{
357 return (NULL);
358}
359#else
360/*
361 * UNIX username and password stuff...
362 */
363
364# include <pwd.h>
caa58f49 365
c68b6ae8 366/*
367 * 'cupsUser()' - Return the current user's name.
368 */
369
370const char * /* O - User name */
371cupsUser(void)
372{
373 struct passwd *pwd; /* User/password entry */
03f61bf3 374 cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
c68b6ae8 375
376
03f61bf3 377 if (!cg->user[0])
3e3712a4 378 {
c68b6ae8 379 /*
380 * Rewind the password file...
381 */
caa58f49 382
c68b6ae8 383 setpwent();
6b67a15e 384
c68b6ae8 385 /*
386 * Lookup the password entry for the current user.
387 */
6b67a15e 388
c68b6ae8 389 if ((pwd = getpwuid(getuid())) == NULL)
03f61bf3 390 strcpy(cg->user, "unknown"); /* Unknown user! */
c68b6ae8 391 else
6b67a15e 392 {
393 /*
06d5ef42 394 * Copy the username...
6b67a15e 395 */
396
c68b6ae8 397 setpwent();
6b67a15e 398
03f61bf3 399 strlcpy(cg->user, pwd->pw_name, sizeof(cg->user));
6b67a15e 400 }
06d5ef42 401
402 /*
403 * Rewind the password file again...
404 */
405
406 setpwent();
c68b6ae8 407 }
6b67a15e 408
03f61bf3 409 return (cg->user);
c68b6ae8 410}
6b67a15e 411
6b67a15e 412
c68b6ae8 413/*
03f61bf3 414 * '_cupsGetPassword()' - Get a password from the user...
c68b6ae8 415 */
416
03f61bf3 417const char * /* O - Password */
418_cupsGetPassword(const char *prompt) /* I - Prompt string */
c68b6ae8 419{
420 return (getpass(prompt));
6b67a15e 421}
e335a094 422#endif /* WIN32 */
6b67a15e 423
424
e93faa10 425/*
c9d3f842 426 * End of "$Id$".
6b67a15e 427 */