Update.
[glibc.git] / nis / nis_local_names.c
blobe760f433b261e193541c69e48bcf8c6029d88567
1 /* Copyright (c) 1997 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>
24 #include <rpcsvc/nislib.h>
26 nis_name
27 nis_local_group (void)
29 static char __nisgroup[NIS_MAXNAMELEN + 1];
31 if (__nisgroup[0] == '\0')
33 char *cptr;
35 if ((cptr = getenv ("NIS_GROUP")) == NULL)
36 return __nisgroup;
38 if (strlen (cptr) >= NIS_MAXNAMELEN)
39 return __nisgroup;
41 strcpy (__nisgroup, cptr);
43 if (__nisgroup[strlen (__nisgroup) - 1] != '.')
45 cptr = nis_local_directory ();
46 if (strlen (__nisgroup) + strlen (cptr) + 1 < NIS_MAXNAMELEN)
48 strcat (__nisgroup, ".");
49 strcat (__nisgroup, cptr);
51 else
53 __nisgroup[0] = '\0';
54 return __nisgroup;
59 return __nisgroup;
63 nis_name
64 nis_local_directory (void)
66 static char __nisdomainname[NIS_MAXNAMELEN + 1];
67 int len;
69 if (__nisdomainname[0] == '\0')
71 if (getdomainname (__nisdomainname, NIS_MAXNAMELEN) < 0)
72 strcpy (__nisdomainname, "\0");
73 else
75 len = strlen (__nisdomainname);
77 /* Missing trailing dot? */
78 if (__nisdomainname[len - 1] != '.')
80 __nisdomainname[len] = '.';
81 __nisdomainname[len + 1] = '\0';
86 return __nisdomainname;
89 nis_name
90 nis_local_principal (void)
92 static char __principal[NIS_MAXNAMELEN + 1];
94 if (__principal[0] == '\0')
96 char buf[NIS_MAXNAMELEN + 1];
97 nis_result *res;
98 uid_t uid = geteuid ();
100 if (uid != 0)
102 snprintf (buf, NIS_MAXNAMELEN - 1,
103 "[auth_name=%d,auth_type=LOCAL],cred.org_dir.%s",
104 uid, nis_local_directory ());
106 if (buf[strlen (buf) - 1] != '.')
107 strcat (buf, ".");
109 res = nis_list (buf, USE_DGRAM + NO_AUTHINFO + FOLLOW_LINKS +
110 FOLLOW_PATH, NULL, NULL);
112 if (res == NULL)
114 strcpy (__principal, "nobody");
115 return __principal;
118 if (res->status == 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 strcpy (__principal, "nobody");
137 return __principal;
140 else
142 strcpy (__principal, nis_local_host ());
143 return __principal;
146 /* Should be never reached */
147 strcpy (__principal, "nobody");
148 return __principal;
150 return __principal;
153 nis_name
154 nis_local_host (void)
156 static char __nishostname[NIS_MAXNAMELEN + 1];
157 int len;
159 if (__nishostname[0] == '\0')
161 if (gethostname (__nishostname, NIS_MAXNAMELEN) < 0)
162 __nishostname[0] = '\0';
163 else
165 char *cp;
166 len = strlen(__nishostname);
168 /* Hostname already fully qualified? */
169 if (__nishostname[len - 1] == '.')
170 return __nishostname;
172 if ((strlen (__nishostname) + strlen (nis_local_directory ()) + 1) >
173 NIS_MAXNAMELEN)
175 __nishostname[0] = '\0';
176 return __nishostname;
179 cp = &__nishostname[len];
180 *cp++ = '.';
181 strncpy (cp, nis_local_directory (), NIS_MAXNAMELEN - len -1);
182 __nishostname[NIS_MAXNAMELEN] = '\0';
186 return __nishostname;