Update.
[glibc.git] / nis / nis_local_names.c
blob22601a46aa8bc37fa0c684ebe7d238e51177764f
1 /* Copyright (c) 1997, 1998 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <errno.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <rpcsvc/nis.h>
25 nis_name
26 nis_local_group (void)
28 static char __nisgroup[NIS_MAXNAMELEN + 1];
30 if (__nisgroup[0] == '\0')
32 char *cptr;
33 char *cp;
35 if ((cptr = getenv ("NIS_GROUP")) == NULL)
36 return __nisgroup;
38 if (strlen (cptr) >= NIS_MAXNAMELEN)
39 return __nisgroup;
41 cp = stpcpy (__nisgroup, cptr);
43 if (cp[-1] != '.')
45 cptr = nis_local_directory ();
46 if ((cp - __nisgroup) + strlen (cptr) + 1 < NIS_MAXNAMELEN)
48 *cp++ = '.';
49 strcpy (cp, cptr);
51 else
52 __nisgroup[0] = '\0';
56 return __nisgroup;
60 nis_name
61 nis_local_directory (void)
63 static char __nisdomainname[NIS_MAXNAMELEN + 1];
65 if (__nisdomainname[0] == '\0')
67 if (getdomainname (__nisdomainname, NIS_MAXNAMELEN) < 0)
68 __nisdomainname[0] = '\0';
69 else
71 char *cp = strchr (__nisdomainname, '\0');
73 /* Missing trailing dot? */
74 if (cp[-1] != '.')
76 *cp++ = '.';
77 *cp = '\0';
82 return __nisdomainname;
85 nis_name
86 nis_local_principal (void)
88 static char __principal[NIS_MAXNAMELEN + 1];
90 if (__principal[0] == '\0')
92 char buf[NIS_MAXNAMELEN + 1];
93 nis_result *res;
94 uid_t uid = geteuid ();
96 if (uid != 0)
98 int len = snprintf (buf, NIS_MAXNAMELEN - 1,
99 "[auth_name=%d,auth_type=LOCAL],cred.org_dir.%s",
100 uid, nis_local_directory ());
102 if (len >= NIS_MAXNAMELEN - 1)
103 /* XXX The buffer is too small. Can this happen??? */
104 return strcpy (__principal, "nobody");
106 if (buf[len - 1] != '.')
108 buf[len++] = '.';
109 buf[len] = '\0';
112 res = nis_list (buf, USE_DGRAM + NO_AUTHINFO + FOLLOW_LINKS +
113 FOLLOW_PATH, NULL, NULL);
115 if (res == NULL)
116 return strcpy (__principal, "nobody");
118 if (NIS_RES_STATUS (res) == NIS_SUCCESS)
120 if (res->objects.objects_len > 1)
122 /* More than one principal with same uid? something
123 wrong with cred table. Should be unique. Warn user
124 and continue. */
125 printf (_("\
126 LOCAL entry for UID %d in directory %s not unique\n"),
127 uid, nis_local_directory ());
129 strcpy (__principal, ENTRY_VAL (res->objects.objects_val, 0));
130 nis_freeresult (res);
131 return __principal;
133 else
135 nis_freeresult (res);
136 return strcpy (__principal, "nobody");
139 else
140 return strcpy (__principal, nis_local_host ());
142 /* Should be never reached */
143 return strcpy (__principal, "nobody");
145 return __principal;
148 nis_name
149 nis_local_host (void)
151 static char __nishostname[NIS_MAXNAMELEN + 1];
153 if (__nishostname[0] == '\0')
155 if (gethostname (__nishostname, NIS_MAXNAMELEN) < 0)
156 __nishostname[0] = '\0';
157 else
159 char *cp = strchr (__nishostname, '\0');
160 int len = cp - __nishostname;
162 /* Hostname already fully qualified? */
163 if (cp[-1] == '.')
164 return __nishostname;
166 if (len + strlen (nis_local_directory ()) + 1 > NIS_MAXNAMELEN)
168 __nishostname[0] = '\0';
169 return __nishostname;
172 *cp++ = '.';
173 strncpy (cp, nis_local_directory (), NIS_MAXNAMELEN - len -1);
174 __nishostname[NIS_MAXNAMELEN] = '\0';
178 return __nishostname;