2 * "$Id: usersys.c,v 1.17 2002/05/16 13:44:54 mike Exp $"
4 * User, system, and password routines for the Common UNIX Printing
7 * Copyright 1997-2002 by Easy Software Products.
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
16 * Attn: CUPS Licensing Information
17 * Easy Software Products
18 * 44141 Airport View Drive, Suite 204
19 * Hollywood, Maryland 20636-3111 USA
21 * Voice: (301) 373-9603
22 * EMail: cups-info@cups.org
23 * WWW: http://www.cups.org
25 * This file is subject to the Apple OS-Developed Software exception.
29 * cupsEncryption() - Get the default encryption settings...
30 * cupsGetPassword() - Get a password from the user...
31 * cupsServer() - Return the hostname of the default server...
32 * cupsSetEncryption() - Set the encryption preference.
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.
37 * cups_get_password() - Get a password from the user...
38 * cups_get_line() - Get a line from a file...
42 * Include necessary headers...
55 static const char *cups_get_password(const char *prompt
);
56 static char *cups_get_line(char *buf
, int buflen
, FILE *fp
);
63 static http_encryption_t cups_encryption
= (http_encryption_t
)-1;
64 static char cups_user
[65] = "",
65 cups_server
[256] = "";
66 static const char *(*cups_pwdcb
)(const char *) = cups_get_password
;
70 * 'cupsEncryption()' - Get the default encryption settings...
76 FILE *fp
; /* client.conf file */
77 char *encryption
; /* CUPS_ENCRYPTION variable */
78 const char *home
; /* Home directory of user */
79 static char line
[1024]; /* Line from file */
83 * First see if we have already set the encryption stuff...
86 if (cups_encryption
== (http_encryption_t
)-1)
89 * Then see if the CUPS_ENCRYPTION environment variable is set...
92 if ((encryption
= getenv("CUPS_ENCRYPTION")) == NULL
)
95 * Next check to see if we have a $HOME/.cupsrc or client.conf file...
98 if ((home
= getenv("HOME")) != NULL
)
100 snprintf(line
, sizeof(line
), "%s/.cupsrc", home
);
101 fp
= fopen(line
, "r");
108 if ((home
= getenv("CUPS_SERVERROOT")) != NULL
)
110 snprintf(line
, sizeof(line
), "%s/client.conf", home
);
111 fp
= fopen(line
, "r");
114 fp
= fopen(CUPS_SERVERROOT
"/client.conf", "r");
117 encryption
= "IfRequested";
122 * Read the config file and look for a ServerName line...
125 while (cups_get_line(line
, sizeof(line
), fp
) != NULL
)
126 if (strncmp(line
, "Encryption ", 11) == 0)
129 * Got it! Drop any trailing newline and find the name...
132 encryption
= line
+ strlen(line
) - 1;
133 if (*encryption
== '\n')
136 for (encryption
= line
+ 11; isspace(*encryption
); encryption
++);
145 * Set the encryption preference...
148 if (strcasecmp(encryption
, "never") == 0)
149 cups_encryption
= HTTP_ENCRYPT_NEVER
;
150 else if (strcasecmp(encryption
, "always") == 0)
151 cups_encryption
= HTTP_ENCRYPT_ALWAYS
;
152 else if (strcasecmp(encryption
, "required") == 0)
153 cups_encryption
= HTTP_ENCRYPT_REQUIRED
;
155 cups_encryption
= HTTP_ENCRYPT_IF_REQUESTED
;
158 return (cups_encryption
);
163 * 'cupsGetPassword()' - Get a password from the user...
166 const char * /* O - Password */
167 cupsGetPassword(const char *prompt
) /* I - Prompt string */
169 return ((*cups_pwdcb
)(prompt
));
174 * 'cupsSetEncryption()' - Set the encryption preference.
178 cupsSetEncryption(http_encryption_t e
) /* I - New encryption preference */
185 * 'cupsServer()' - Return the hostname of the default server...
188 const char * /* O - Server name */
191 FILE *fp
; /* client.conf file */
192 char *server
; /* Pointer to server name */
193 const char *home
; /* Home directory of user */
194 static char line
[1024]; /* Line from file */
198 * First see if we have already set the server name...
204 * Then see if the CUPS_SERVER environment variable is set...
207 if ((server
= getenv("CUPS_SERVER")) == NULL
)
210 * Next check to see if we have a $HOME/.cupsrc or client.conf file...
213 if ((home
= getenv("HOME")) != NULL
)
215 snprintf(line
, sizeof(line
), "%s/.cupsrc", home
);
216 fp
= fopen(line
, "r");
223 if ((home
= getenv("CUPS_SERVERROOT")) != NULL
)
225 snprintf(line
, sizeof(line
), "%s/client.conf", home
);
226 fp
= fopen(line
, "r");
229 fp
= fopen(CUPS_SERVERROOT
"/client.conf", "r");
232 server
= "localhost";
237 * Read the config file and look for a ServerName line...
240 while (cups_get_line(line
, sizeof(line
), fp
) != NULL
)
241 if (strncmp(line
, "ServerName ", 11) == 0)
244 * Got it! Drop any trailing newline and find the name...
247 server
= line
+ strlen(line
) - 1;
251 for (server
= line
+ 11; isspace(*server
); server
++);
260 * Copy the server name over...
263 strlcpy(cups_server
, server
, sizeof(cups_server
));
266 return (cups_server
);
271 * 'cupsSetPasswordCB()' - Set the password callback for CUPS.
275 cupsSetPasswordCB(const char *(*cb
)(const char *)) /* I - Callback function */
277 if (cb
== (const char *(*)(const char *))0)
278 cups_pwdcb
= cups_get_password
;
285 * 'cupsSetServer()' - Set the default server name...
289 cupsSetServer(const char *server
) /* I - Server name */
292 strlcpy(cups_server
, server
, sizeof(cups_server
));
294 cups_server
[0] = '\0';
299 * 'cupsSetUser()' - Set the default user name...
303 cupsSetUser(const char *user
) /* I - User name */
306 strlcpy(cups_user
, user
, sizeof(cups_user
));
312 #if defined(WIN32) || defined(__EMX__)
314 * WIN32 and OS/2 username and password stuff...
318 * 'cupsUser()' - Return the current user's name.
321 const char * /* O - User name */
325 strcpy(cups_user
, "WindowsUser");
332 * 'cups_get_password()' - Get a password from the user...
335 static const char * /* O - Password */
336 cups_get_password(const char *prompt
) /* I - Prompt string */
342 * UNIX username and password stuff...
348 * 'cupsUser()' - Return the current user's name.
351 const char * /* O - User name */
354 struct passwd
*pwd
; /* User/password entry */
360 * Rewind the password file...
366 * Lookup the password entry for the current user.
369 if ((pwd
= getpwuid(getuid())) == NULL
)
370 strcpy(cups_user
, "unknown"); /* Unknown user! */
374 * Copy the username...
379 strlcpy(cups_user
, pwd
->pw_name
, sizeof(cups_user
));
383 * Rewind the password file again...
394 * 'cups_get_password()' - Get a password from the user...
397 static const char * /* O - Password */
398 cups_get_password(const char *prompt
) /* I - Prompt string */
400 return (getpass(prompt
));
402 #endif /* WIN32 || __EMX__ */
406 * 'cups_get_line()' - Get a line from a file.
409 static char * /* O - Line from file */
410 cups_get_line(char *buf
, /* I - Line buffer */
411 int buflen
, /* I - Size of line buffer */
412 FILE *fp
) /* I - File to read from */
414 char *bufptr
; /* Pointer to end of buffer */
418 * Get the line from a file...
421 if (fgets(buf
, buflen
, fp
) == NULL
)
425 * Remove all trailing whitespace...
428 bufptr
= buf
+ strlen(buf
) - 1;
432 while (isspace(*bufptr
) && bufptr
>= buf
)
440 * End of "$Id: usersys.c,v 1.17 2002/05/16 13:44:54 mike Exp $".