1 /* Copyright (C) 1996-2002, 2003, 2006 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1996.
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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
25 #include <bits/libc-lock.h>
26 #include <rpcsvc/yp.h>
27 #include <rpcsvc/ypclnt.h>
31 __libc_lock_define_initialized (static, lock
)
33 static bool_t new_start
= 1;
38 _nss_nis_parse_aliasent (const char *key
, char *alias
, struct aliasent
*result
,
39 char *buffer
, size_t buflen
, int *errnop
)
41 char *first_unused
= buffer
+ strlen (alias
) + 1;
43 buflen
- (buflen
% __alignof__ (char *)) - strlen (alias
) - 2;
47 result
->alias_members_len
= 0;
50 strcpy (first_unused
, key
);
52 if (first_unused
[room_left
- 1] != '\0')
54 /* The line is too long for our buffer. */
60 result
->alias_name
= first_unused
;
62 /* Terminate the line for any case. */
63 cp
= strpbrk (alias
, "#\n");
67 first_unused
+= strlen (result
->alias_name
) + 1;
68 /* Adjust the pointer so it is aligned for
70 first_unused
+= __alignof__ (char *) - 1;
71 first_unused
-= ((first_unused
- (char *) 0) % __alignof__ (char *));
72 result
->alias_members
= (char **) first_unused
;
78 /* Skip leading blanks. */
79 while (isspace (*line
))
85 if (room_left
< sizeof (char *))
87 room_left
-= sizeof (char *);
88 result
->alias_members
[result
->alias_members_len
] = line
;
90 while (*line
!= '\0' && *line
!= ',')
93 if (line
!= result
->alias_members
[result
->alias_members_len
])
97 result
->alias_members_len
++;
100 return result
->alias_members_len
== 0 ? 0 : 1;
104 _nss_nis_setaliasent (void)
106 __libc_lock_lock (lock
);
116 __libc_lock_unlock (lock
);
118 return NSS_STATUS_SUCCESS
;
120 /* The 'endaliasent' function is identical. */
121 strong_alias (_nss_nis_setaliasent
, _nss_nis_endaliasent
)
123 static enum nss_status
124 internal_nis_getaliasent_r (struct aliasent
*alias
, char *buffer
,
125 size_t buflen
, int *errnop
)
129 if (__builtin_expect (yp_get_default_domain (&domain
), 0))
130 return NSS_STATUS_UNAVAIL
;
132 alias
->alias_local
= 0;
134 /* Get the next entry until we found a correct one. */
145 yperr
= yp_first (domain
, "mail.aliases", &outkey
, &keylen
, &result
,
148 yperr
= yp_next (domain
, "mail.aliases", oldkey
, oldkeylen
, &outkey
,
149 &keylen
, &result
, &len
);
151 if (__builtin_expect (yperr
!= YPERR_SUCCESS
, 0))
153 enum nss_status retval
= yperr2nss (yperr
);
155 if (retval
== NSS_STATUS_TRYAGAIN
)
160 if (__builtin_expect ((size_t) (len
+ 1) > buflen
, 0))
164 return NSS_STATUS_TRYAGAIN
;
166 char *p
= strncpy (buffer
, result
, len
);
172 parse_res
= _nss_nis_parse_aliasent (outkey
, p
, alias
, buffer
,
174 if (__builtin_expect (parse_res
== -1, 0))
178 return NSS_STATUS_TRYAGAIN
;
188 return NSS_STATUS_SUCCESS
;
192 _nss_nis_getaliasent_r (struct aliasent
*alias
, char *buffer
, size_t buflen
,
195 enum nss_status status
;
197 __libc_lock_lock (lock
);
199 status
= internal_nis_getaliasent_r (alias
, buffer
, buflen
, errnop
);
201 __libc_lock_unlock (lock
);
207 _nss_nis_getaliasbyname_r (const char *name
, struct aliasent
*alias
,
208 char *buffer
, size_t buflen
, int *errnop
)
213 return NSS_STATUS_UNAVAIL
;
216 size_t namlen
= strlen (name
);
217 char name2
[namlen
+ 1];
220 if (__builtin_expect (yp_get_default_domain (&domain
), 0))
221 return NSS_STATUS_UNAVAIL
;
223 /* Convert name to lowercase. */
225 for (i
= 0; i
< namlen
; ++i
)
226 name2
[i
] = _tolower (name
[i
]);
231 int yperr
= yp_match (domain
, "mail.aliases", name2
, namlen
, &result
, &len
);
233 if (__builtin_expect (yperr
!= YPERR_SUCCESS
, 0))
235 enum nss_status retval
= yperr2nss (yperr
);
237 if (retval
== NSS_STATUS_TRYAGAIN
)
242 if (__builtin_expect ((size_t) (len
+ 1) > buflen
, 0))
246 return NSS_STATUS_TRYAGAIN
;
249 char *p
= strncpy (buffer
, result
, len
);
255 alias
->alias_local
= 0;
256 int parse_res
= _nss_nis_parse_aliasent (name
, p
, alias
, buffer
, buflen
,
258 if (__builtin_expect (parse_res
< 1, 0))
261 return NSS_STATUS_TRYAGAIN
;
263 return NSS_STATUS_NOTFOUND
;
266 return NSS_STATUS_SUCCESS
;