]> git.ipfire.org Git - thirdparty/glibc.git/blob - nis/nis_local_names.c
Update.
[thirdparty/glibc.git] / nis / nis_local_names.c
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.
4
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.
9
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.
14
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. */
19
20 #include <errno.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <rpcsvc/nis.h>
24 #include <rpcsvc/nislib.h>
25
26 nis_name
27 nis_local_group (void)
28 {
29 static char __nisgroup[NIS_MAXNAMELEN + 1];
30
31 if (__nisgroup[0] == '\0')
32 {
33 char *cptr;
34
35 if ((cptr = getenv ("NIS_GROUP")) == NULL)
36 return __nisgroup;
37
38 if (strlen (cptr) >= NIS_MAXNAMELEN)
39 return __nisgroup;
40
41 strcpy (__nisgroup, cptr);
42
43 if (__nisgroup[strlen (__nisgroup) - 1] != '.')
44 {
45 cptr = nis_local_directory ();
46 if (strlen (__nisgroup) + strlen (cptr) + 1 < NIS_MAXNAMELEN)
47 {
48 strcat (__nisgroup, ".");
49 strcat (__nisgroup, cptr);
50 }
51 else
52 {
53 __nisgroup[0] = '\0';
54 return __nisgroup;
55 }
56 }
57 }
58
59 return __nisgroup;
60 }
61
62
63 nis_name
64 nis_local_directory (void)
65 {
66 static char __nisdomainname[NIS_MAXNAMELEN + 1];
67 int len;
68
69 if (__nisdomainname[0] == '\0')
70 {
71 if (getdomainname (__nisdomainname, NIS_MAXNAMELEN) < 0)
72 strcpy (__nisdomainname, "\0");
73 else
74 {
75 len = strlen (__nisdomainname);
76
77 /* Missing trailing dot? */
78 if (__nisdomainname[len - 1] != '.')
79 {
80 __nisdomainname[len] = '.';
81 __nisdomainname[len + 1] = '\0';
82 }
83 }
84 }
85
86 return __nisdomainname;
87 }
88
89 nis_name
90 nis_local_principal (void)
91 {
92 static char __principal[NIS_MAXNAMELEN + 1];
93
94 if (__principal[0] == '\0')
95 {
96 char buf[NIS_MAXNAMELEN + 1];
97 nis_result *res;
98 uid_t uid = geteuid ();
99
100 if (uid != 0)
101 {
102 snprintf (buf, NIS_MAXNAMELEN - 1,
103 "[auth_name=%d,auth_type=LOCAL],cred.org_dir.%s",
104 uid, nis_local_directory ());
105
106 if (buf[strlen (buf) - 1] != '.')
107 strcat (buf, ".");
108
109 res = nis_list (buf, USE_DGRAM + NO_AUTHINFO + FOLLOW_LINKS +
110 FOLLOW_PATH, NULL, NULL);
111
112 if (res == NULL)
113 {
114 strcpy (__principal, "nobody");
115 return __principal;
116 }
117
118 if (res->status == NIS_SUCCESS)
119 {
120 if (res->objects.objects_len > 1)
121 {
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 ());
128 }
129 strcpy (__principal, ENTRY_VAL (res->objects.objects_val, 0));
130 nis_freeresult (res);
131 return __principal;
132 }
133 else
134 {
135 nis_freeresult (res);
136 strcpy (__principal, "nobody");
137 return __principal;
138 }
139 }
140 else
141 {
142 strcpy (__principal, nis_local_host ());
143 return __principal;
144 }
145
146 /* Should be never reached */
147 strcpy (__principal, "nobody");
148 return __principal;
149 }
150 return __principal;
151 }
152
153 nis_name
154 nis_local_host (void)
155 {
156 static char __nishostname[NIS_MAXNAMELEN + 1];
157 int len;
158
159 if (__nishostname[0] == '\0')
160 {
161 char *cp = __nishostname;
162
163 if (gethostname (__nishostname, NIS_MAXNAMELEN) < 0)
164 cp = stpcpy (cp, "\0");
165
166 len = cp - __nishostname;
167
168 /* Hostname already fully qualified? */
169 if (__nishostname[len - 1] == '.')
170 return __nishostname;
171
172 if (strlen (__nishostname + strlen (nis_local_directory ()) + 1) >
173 NIS_MAXNAMELEN)
174 {
175 __nishostname[0] = '\0';
176 return __nishostname;
177 }
178
179 *cp++ = '.';
180 stpcpy (cp, nis_local_directory ());
181 }
182
183 return __nishostname;
184 }