1 /* Copyright (C) 1997, 1998, 2001, 2002, 2003, 2005, 2006
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
27 #include <bits/libc-lock.h>
28 #include <rpcsvc/nis.h>
30 #include "nss-nisplus.h"
32 __libc_lock_define_initialized (static, lock
)
34 static nis_result
*result
;
35 static u_long next_entry
;
36 static nis_name tablename_val
;
37 static size_t tablename_len
;
39 #define NISENTRYVAL(idx, col, res) \
40 (NIS_RES_OBJECT (res)[idx].EN_data.en_cols.en_cols_val[col].ec_value.ec_value_val)
42 #define NISENTRYLEN(idx, col, res) \
43 (NIS_RES_OBJECT (res)[idx].EN_data.en_cols.en_cols_val[col].ec_value.ec_value_len)
45 static enum nss_status
46 _nss_create_tablename (int *errnop
)
48 if (tablename_val
== NULL
)
50 const char *local_dir
= nis_local_directory ();
51 size_t local_dir_len
= strlen (local_dir
);
52 static const char prefix
[] = "mail_aliases.org_dir.";
54 char *p
= malloc (sizeof (prefix
) + local_dir_len
);
58 return NSS_STATUS_TRYAGAIN
;
61 memcpy (__stpcpy (p
, prefix
), local_dir
, local_dir_len
+ 1);
63 tablename_len
= sizeof (prefix
) - 1 + local_dir_len
;
65 atomic_write_barrier ();
70 return NSS_STATUS_SUCCESS
;
74 _nss_nisplus_parse_aliasent (nis_result
*result
, unsigned long entry
,
75 struct aliasent
*alias
, char *buffer
,
76 size_t buflen
, int *errnop
)
81 if ((result
->status
!= NIS_SUCCESS
&& result
->status
!= NIS_S_SUCCESS
)
82 || __type_of (&NIS_RES_OBJECT (result
)[entry
]) != NIS_ENTRY_OBJ
83 || strcmp (NIS_RES_OBJECT (result
)[entry
].EN_data
.en_type
,
85 || NIS_RES_OBJECT (result
)[entry
].EN_data
.en_cols
.en_cols_len
< 2)
88 if (NISENTRYLEN (entry
, 1, result
) >= buflen
)
90 /* The line is too long for our buffer. */
96 char *cp
= __stpncpy (buffer
, NISENTRYVAL (entry
, 1, result
),
97 NISENTRYLEN (entry
, 1, result
));
100 char *first_unused
= cp
+ 1;
101 size_t room_left
= buflen
- (first_unused
- buffer
);
103 alias
->alias_local
= 0;
104 alias
->alias_members_len
= 0;
106 if (NISENTRYLEN (entry
, 0, result
) >= room_left
)
109 cp
= __stpncpy (first_unused
, NISENTRYVAL (entry
, 0, result
),
110 NISENTRYLEN (entry
, 0, result
));
112 alias
->alias_name
= first_unused
;
114 /* Terminate the line for any case. */
115 cp
= strpbrk (alias
->alias_name
, "#\n");
119 size_t len
= strlen (alias
->alias_name
) + 1;
123 /* Adjust the pointer so it is aligned for
125 size_t adjust
= ((__alignof__ (char *)
126 - (first_unused
- (char *) 0) % __alignof__ (char *))
127 % __alignof__ (char *));
128 if (room_left
< adjust
)
130 first_unused
+= adjust
;
133 alias
->alias_members
= (char **) first_unused
;
136 while (*line
!= '\0')
138 /* Skip leading blanks. */
139 while (isspace (*line
))
145 if (room_left
< sizeof (char *))
147 room_left
-= sizeof (char *);
148 alias
->alias_members
[alias
->alias_members_len
] = line
;
150 while (*line
!= '\0' && *line
!= ',')
153 if (line
!= alias
->alias_members
[alias
->alias_members_len
])
156 ++alias
->alias_members_len
;
158 else if (*line
== ',')
162 return alias
->alias_members_len
== 0 ? 0 : 1;
165 static enum nss_status
166 internal_setaliasent (void)
168 enum nss_status status
;
173 nis_freeresult (result
);
177 if (_nss_create_tablename (&err
) != NSS_STATUS_SUCCESS
)
178 return NSS_STATUS_UNAVAIL
;
181 result
= nis_list (tablename_val
, FOLLOW_PATH
| FOLLOW_LINKS
, NULL
, NULL
);
184 status
= NSS_STATUS_TRYAGAIN
;
185 __set_errno (ENOMEM
);
189 status
= niserr2nss (result
->status
);
190 if (status
!= NSS_STATUS_SUCCESS
)
192 nis_freeresult (result
);
200 _nss_nisplus_setaliasent (void)
202 enum nss_status status
;
204 __libc_lock_lock (lock
);
206 status
= internal_setaliasent ();
208 __libc_lock_unlock (lock
);
214 _nss_nisplus_endaliasent (void)
216 __libc_lock_lock (lock
);
220 nis_freeresult (result
);
225 __libc_lock_unlock (lock
);
227 return NSS_STATUS_SUCCESS
;
230 static enum nss_status
231 internal_nisplus_getaliasent_r (struct aliasent
*alias
,
232 char *buffer
, size_t buflen
, int *errnop
)
238 enum nss_status status
;
240 status
= internal_setaliasent ();
241 if (result
== NULL
|| status
!= NSS_STATUS_SUCCESS
)
245 /* Get the next entry until we found a correct one. */
248 if (next_entry
>= result
->objects
.objects_len
)
249 return NSS_STATUS_NOTFOUND
;
251 parse_res
= _nss_nisplus_parse_aliasent (result
, next_entry
, alias
,
252 buffer
, buflen
, errnop
);
254 return NSS_STATUS_TRYAGAIN
;
260 return NSS_STATUS_SUCCESS
;
264 _nss_nisplus_getaliasent_r (struct aliasent
*result
, char *buffer
,
265 size_t buflen
, int *errnop
)
269 __libc_lock_lock (lock
);
271 status
= internal_nisplus_getaliasent_r (result
, buffer
, buflen
, errnop
);
273 __libc_lock_unlock (lock
);
279 _nss_nisplus_getaliasbyname_r (const char *name
, struct aliasent
*alias
,
280 char *buffer
, size_t buflen
, int *errnop
)
284 if (tablename_val
== NULL
)
286 __libc_lock_lock (lock
);
288 enum nss_status status
= _nss_create_tablename (errnop
);
290 __libc_lock_unlock (lock
);
292 if (status
!= NSS_STATUS_SUCCESS
)
299 return NSS_STATUS_UNAVAIL
;
302 char buf
[strlen (name
) + 9 + tablename_len
];
305 snprintf (buf
, sizeof (buf
), "[name=%s],%s", name
, tablename_val
);
307 nis_result
*result
= nis_list (buf
, FOLLOW_PATH
| FOLLOW_LINKS
, NULL
, NULL
);
312 return NSS_STATUS_TRYAGAIN
;
315 if (__builtin_expect (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
, 0))
317 enum nss_status status
= niserr2nss (result
->status
);
318 nis_freeresult (result
);
322 parse_res
= _nss_nisplus_parse_aliasent (result
, 0, alias
,
323 buffer
, buflen
, errnop
);
325 /* We do not need the lookup result anymore. */
326 nis_freeresult (result
);
328 if (__builtin_expect (parse_res
< 1, 0))
330 __set_errno (olderr
);
333 return NSS_STATUS_TRYAGAIN
;
335 return NSS_STATUS_NOTFOUND
;
338 return NSS_STATUS_SUCCESS
;