Update.
[glibc.git] / nis / nis_local_names.c
blob0d23778ba9954e4f738cc03dcf0fb3d2273902ee
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 <libintl.h>
24 #include <rpcsvc/nis.h>
26 nis_name
27 nis_local_group (void)
29 static char __nisgroup[NIS_MAXNAMELEN + 1];
31 if (__nisgroup[0] == '\0')
33 char *cptr;
34 char *cp;
36 if ((cptr = getenv ("NIS_GROUP")) == NULL)
37 return __nisgroup;
39 if (strlen (cptr) >= NIS_MAXNAMELEN)
40 return __nisgroup;
42 cp = stpcpy (__nisgroup, cptr);
44 if (cp[-1] != '.')
46 cptr = nis_local_directory ();
47 if ((cp - __nisgroup) + strlen (cptr) + 1 < NIS_MAXNAMELEN)
49 *cp++ = '.';
50 strcpy (cp, cptr);
52 else
53 __nisgroup[0] = '\0';
57 return __nisgroup;
61 nis_name
62 nis_local_directory (void)
64 static char __nisdomainname[NIS_MAXNAMELEN + 1];
66 if (__nisdomainname[0] == '\0')
68 if (getdomainname (__nisdomainname, NIS_MAXNAMELEN) < 0)
69 __nisdomainname[0] = '\0';
70 else
72 char *cp = strchr (__nisdomainname, '\0');
74 /* Missing trailing dot? */
75 if (cp[-1] != '.')
77 *cp++ = '.';
78 *cp = '\0';
83 return __nisdomainname;
86 nis_name
87 nis_local_principal (void)
89 static char __principal[NIS_MAXNAMELEN + 1];
91 if (__principal[0] == '\0')
93 char buf[NIS_MAXNAMELEN + 1];
94 nis_result *res;
95 uid_t uid = geteuid ();
97 if (uid != 0)
99 int len = snprintf (buf, NIS_MAXNAMELEN - 1,
100 "[auth_name=%d,auth_type=LOCAL],cred.org_dir.%s",
101 uid, nis_local_directory ());
103 if (len >= NIS_MAXNAMELEN - 1)
104 /* XXX The buffer is too small. Can this happen??? */
105 return strcpy (__principal, "nobody");
107 if (buf[len - 1] != '.')
109 buf[len++] = '.';
110 buf[len] = '\0';
113 res = nis_list (buf, USE_DGRAM + NO_AUTHINFO + FOLLOW_LINKS +
114 FOLLOW_PATH, NULL, NULL);
116 if (res == NULL)
117 return strcpy (__principal, "nobody");
119 if (NIS_RES_STATUS (res) == NIS_SUCCESS)
121 if (res->objects.objects_len > 1)
123 /* More than one principal with same uid? something
124 wrong with cred table. Should be unique. Warn user
125 and continue. */
126 printf (_("\
127 LOCAL entry for UID %d in directory %s not unique\n"),
128 uid, nis_local_directory ());
130 strcpy (__principal, ENTRY_VAL (res->objects.objects_val, 0));
131 nis_freeresult (res);
132 return __principal;
134 else
136 nis_freeresult (res);
137 return strcpy (__principal, "nobody");
140 else
141 return strcpy (__principal, nis_local_host ());
143 /* Should be never reached */
144 return strcpy (__principal, "nobody");
146 return __principal;
149 nis_name
150 nis_local_host (void)
152 static char __nishostname[NIS_MAXNAMELEN + 1];
154 if (__nishostname[0] == '\0')
156 if (gethostname (__nishostname, NIS_MAXNAMELEN) < 0)
157 __nishostname[0] = '\0';
158 else
160 char *cp = strchr (__nishostname, '\0');
161 int len = cp - __nishostname;
163 /* Hostname already fully qualified? */
164 if (cp[-1] == '.')
165 return __nishostname;
167 if (len + strlen (nis_local_directory ()) + 1 > NIS_MAXNAMELEN)
169 __nishostname[0] = '\0';
170 return __nishostname;
173 *cp++ = '.';
174 strncpy (cp, nis_local_directory (), NIS_MAXNAMELEN - len -1);
175 __nishostname[NIS_MAXNAMELEN] = '\0';
179 return __nishostname;