*** empty log message ***
[shishi.git] / lib / realm.c
blob71c2170149afa028c6f4b646dbe178066df38606
1 /* realm.c realm related functions
2 * Copyright (C) 2002 Simon Josefsson
4 * This file is part of Shishi.
6 * Shishi is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Shishi is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Shishi; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "internal.h"
24 /**
25 * shishi_realm_default_guess:
27 * Guesses a realm based on getdomainname() (which really is NIS/YP
28 * domain, but if it is set it might be a good guess), or if it fails,
29 * based on gethostname(), or if it fails, the string
30 * "could-not-guess-default-realm". Note that the hostname is not
31 * trimmed off of the data returned by gethostname() to get the domain
32 * name and use that as the realm.
34 * Return value: Returns guessed realm for host as a string that has
35 * to be deallocated with free() by the caller.
36 **/
37 char *
38 shishi_realm_default_guess (void)
40 char buf[HOST_NAME_MAX];
41 int ret;
43 ret = getdomainname (buf, sizeof (buf));
44 buf[sizeof (buf) - 1] = '\0';
45 if (ret != 0 || strlen (buf) == 0 || strcmp (buf, "(none)") == 0)
47 ret = gethostname (buf, sizeof (buf));
48 buf[sizeof (buf) - 1] = '\0';
50 if (ret != 0)
51 strcpy (buf, "could-not-guess-default-realm");
54 return strdup (buf);
57 /**
58 * shishi_realm_default:
59 * @handle: Shishi library handle create by shishi_init().
61 * Return value: Returns the default realm used in the library. (Not
62 * a copy of it, so don't modify or deallocate it.)
63 **/
64 const char *
65 shishi_realm_default (Shishi * handle)
67 if (!handle->default_realm)
69 char *p;
70 p = shishi_realm_default_guess ();
71 shishi_realm_default_set (handle, p);
72 free (p);
75 return handle->default_realm;
78 /**
79 * shishi_realm_default_set:
80 * @handle: Shishi library handle create by shishi_init().
81 * @realm: string with new default realm name, or NULL to reset to default.
83 * Set the default realm used in the library. The string is copied
84 * into the library, so you can dispose of the variable immediately
85 * after calling this function.
86 **/
87 void
88 shishi_realm_default_set (Shishi * handle, const char *realm)
90 if (handle->default_realm)
91 free (handle->default_realm);
92 if (realm)
93 handle->default_realm = strdup (realm);
94 else
95 handle->default_realm = NULL;
98 const char *
99 shishi_realm_for_server_file (Shishi * handle, char *server)
101 return NULL;
104 const char *
105 shishi_realm_for_server_dns (Shishi * handle, char *server)
107 const char *p = "JOSEFSSON.ORG";
109 fprintf (stderr,
110 "warning: Assuming server `%s' is in realm `%s'\n"
111 "warning: based on insecure DNS information.\n"
112 "warning: Abort if this appear fruadulent.\n", server, p);
114 return p;
117 const char *
118 shishi_realm_for_server (Shishi * handle, char *server)
120 const char *p;
122 p = shishi_realm_for_server_file (handle, server);
123 if (!p)
124 p = shishi_realm_for_server_dns (handle, server);
126 return p;