Update install.texi, and regenerate INSTALL.
[glibc.git] / nis / nis_local_names.c
blob218764f32e99ffc1064ff305c1dab8fc5b907a38
1 /* Copyright (c) 1997-2021 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 Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <errno.h>
20 #include <string.h>
21 #include <unistd.h>
22 #include <libintl.h>
23 #include <rpcsvc/nis.h>
24 #include <shlib-compat.h>
26 nis_name
27 nis_local_group (void)
29 static char __nisgroup[NIS_MAXNAMELEN + 1];
31 char *cptr;
32 if (__nisgroup[0] == '\0'
33 && (cptr = getenv ("NIS_GROUP")) != NULL
34 && cptr[0] != '\0'
35 && strlen (cptr) < NIS_MAXNAMELEN)
37 char *cp = stpcpy (__nisgroup, cptr);
39 if (cp[-1] != '.')
41 cptr = nis_local_directory ();
42 if ((cp - __nisgroup) + strlen (cptr) + 1 < NIS_MAXNAMELEN)
44 *cp++ = '.';
45 strcpy (cp, cptr);
47 else
48 __nisgroup[0] = '\0';
52 return __nisgroup;
54 libnsl_hidden_nolink_def (nis_local_group, GLIBC_2_1)
56 nis_name
57 nis_local_directory (void)
59 static char __nisdomainname[NIS_MAXNAMELEN + 1];
61 if (__nisdomainname[0] == '\0')
63 if (getdomainname (__nisdomainname, NIS_MAXNAMELEN) < 0)
64 __nisdomainname[0] = '\0';
65 else
67 char *cp = rawmemchr (__nisdomainname, '\0');
69 /* Missing trailing dot? */
70 if (cp[-1] != '.')
72 *cp++ = '.';
73 *cp = '\0';
78 return __nisdomainname;
80 libnsl_hidden_nolink_def (nis_local_directory, GLIBC_2_1)
82 nis_name
83 nis_local_principal (void)
85 static char __principal[NIS_MAXNAMELEN + 1];
87 if (__principal[0] == '\0')
89 char buf[NIS_MAXNAMELEN + 1];
90 nis_result *res;
91 uid_t uid = geteuid ();
93 if (uid != 0)
95 int len = snprintf (buf, NIS_MAXNAMELEN - 1,
96 "[auth_name=%d,auth_type=LOCAL],cred.org_dir.%s",
97 uid, nis_local_directory ());
99 if (len >= NIS_MAXNAMELEN - 1)
100 nobody:
101 /* XXX The buffer is too small. Can this happen??? */
102 return strcpy (__principal, "nobody");
104 if (buf[len - 1] != '.')
106 buf[len++] = '.';
107 buf[len] = '\0';
110 res = nis_list (buf, USE_DGRAM + NO_AUTHINFO + FOLLOW_LINKS
111 + FOLLOW_PATH, NULL, NULL);
113 if (res == NULL)
114 goto nobody;
116 if (NIS_RES_STATUS (res) == NIS_SUCCESS)
118 if (res->objects.objects_len > 1)
120 /* More than one principal with same uid? something
121 wrong with cred table. Should be unique. Warn user
122 and continue. */
123 printf (_("\
124 LOCAL entry for UID %d in directory %s not unique\n"),
125 uid, nis_local_directory ());
127 strcpy (__principal, ENTRY_VAL (res->objects.objects_val, 0));
128 nis_freeresult (res);
129 return __principal;
131 else
133 nis_freeresult (res);
134 goto nobody;
137 else
138 return strcpy (__principal, nis_local_host ());
140 /* Should be never reached */
141 goto nobody;
143 return __principal;
145 libnsl_hidden_nolink_def (nis_local_principal, GLIBC_2_1)
147 nis_name
148 nis_local_host (void)
150 static char __nishostname[NIS_MAXNAMELEN + 1];
152 if (__nishostname[0] == '\0')
154 if (gethostname (__nishostname, NIS_MAXNAMELEN) < 0)
155 __nishostname[0] = '\0';
156 else
158 char *cp = rawmemchr (__nishostname, '\0');
159 int len = cp - __nishostname;
161 /* Hostname already fully qualified? */
162 if (cp[-1] == '.')
163 return __nishostname;
165 if (len + strlen (nis_local_directory ()) + 1 > NIS_MAXNAMELEN)
167 __nishostname[0] = '\0';
168 return __nishostname;
171 *cp++ = '.';
172 strncpy (cp, nis_local_directory (), NIS_MAXNAMELEN - len -1);
173 __nishostname[NIS_MAXNAMELEN] = '\0';
177 return __nishostname;
179 libnsl_hidden_nolink_def (nis_local_host, GLIBC_2_1)