Update copyright notices with scripts/update-copyrights
[glibc.git] / nis / nis_local_names.c
blob5c0e0344a76d45f74d65d80f3291569d293986b5
1 /* Copyright (c) 1997-2014 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 <http://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>
25 nis_name
26 nis_local_group (void)
28 static char __nisgroup[NIS_MAXNAMELEN + 1];
30 char *cptr;
31 if (__nisgroup[0] == '\0'
32 && (cptr = getenv ("NIS_GROUP")) != NULL
33 && strlen (cptr) < NIS_MAXNAMELEN)
35 char *cp = stpcpy (__nisgroup, cptr);
37 if (cp[-1] != '.')
39 cptr = nis_local_directory ();
40 if ((cp - __nisgroup) + strlen (cptr) + 1 < NIS_MAXNAMELEN)
42 *cp++ = '.';
43 strcpy (cp, cptr);
45 else
46 __nisgroup[0] = '\0';
50 return __nisgroup;
52 libnsl_hidden_def (nis_local_group)
54 nis_name
55 nis_local_directory (void)
57 static char __nisdomainname[NIS_MAXNAMELEN + 1];
59 if (__nisdomainname[0] == '\0')
61 if (getdomainname (__nisdomainname, NIS_MAXNAMELEN) < 0)
62 __nisdomainname[0] = '\0';
63 else
65 char *cp = rawmemchr (__nisdomainname, '\0');
67 /* Missing trailing dot? */
68 if (cp[-1] != '.')
70 *cp++ = '.';
71 *cp = '\0';
76 return __nisdomainname;
78 libnsl_hidden_def (nis_local_directory)
80 nis_name
81 nis_local_principal (void)
83 static char __principal[NIS_MAXNAMELEN + 1];
85 if (__principal[0] == '\0')
87 char buf[NIS_MAXNAMELEN + 1];
88 nis_result *res;
89 uid_t uid = geteuid ();
91 if (uid != 0)
93 int len = snprintf (buf, NIS_MAXNAMELEN - 1,
94 "[auth_name=%d,auth_type=LOCAL],cred.org_dir.%s",
95 uid, nis_local_directory ());
97 if (len >= NIS_MAXNAMELEN - 1)
98 nobody:
99 /* XXX The buffer is too small. Can this happen??? */
100 return strcpy (__principal, "nobody");
102 if (buf[len - 1] != '.')
104 buf[len++] = '.';
105 buf[len] = '\0';
108 res = nis_list (buf, USE_DGRAM + NO_AUTHINFO + FOLLOW_LINKS +
109 FOLLOW_PATH, NULL, NULL);
111 if (res == NULL)
112 goto nobody;
114 if (NIS_RES_STATUS (res) == NIS_SUCCESS)
116 if (res->objects.objects_len > 1)
118 /* More than one principal with same uid? something
119 wrong with cred table. Should be unique. Warn user
120 and continue. */
121 printf (_("\
122 LOCAL entry for UID %d in directory %s not unique\n"),
123 uid, nis_local_directory ());
125 strcpy (__principal, ENTRY_VAL (res->objects.objects_val, 0));
126 nis_freeresult (res);
127 return __principal;
129 else
131 nis_freeresult (res);
132 goto nobody;
135 else
136 return strcpy (__principal, nis_local_host ());
138 /* Should be never reached */
139 goto nobody;
141 return __principal;
143 libnsl_hidden_def (nis_local_principal)
145 nis_name
146 nis_local_host (void)
148 static char __nishostname[NIS_MAXNAMELEN + 1];
150 if (__nishostname[0] == '\0')
152 if (gethostname (__nishostname, NIS_MAXNAMELEN) < 0)
153 __nishostname[0] = '\0';
154 else
156 char *cp = rawmemchr (__nishostname, '\0');
157 int len = cp - __nishostname;
159 /* Hostname already fully qualified? */
160 if (cp[-1] == '.')
161 return __nishostname;
163 if (len + strlen (nis_local_directory ()) + 1 > NIS_MAXNAMELEN)
165 __nishostname[0] = '\0';
166 return __nishostname;
169 *cp++ = '.';
170 strncpy (cp, nis_local_directory (), NIS_MAXNAMELEN - len -1);
171 __nishostname[NIS_MAXNAMELEN] = '\0';
175 return __nishostname;
177 libnsl_hidden_def (nis_local_host)