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. */
25 #include <bits/libc-lock.h>
26 #include <rpcsvc/nis.h>
28 #include "nss-nisplus.h"
30 __libc_lock_define_initialized (static, lock
)
32 static nis_result
*result
= NULL
;
33 static u_long next_entry
= 0;
34 static nis_name tablename_val
= NULL
;
35 static u_long tablename_len
= 0;
37 #define NISENTRYVAL(idx,col,res) \
38 ((res)->objects.objects_val[(idx)].EN_data.en_cols.en_cols_val[(col)].ec_value.ec_value_val)
40 #define NISENTRYLEN(idx,col,res) \
41 ((res)->objects.objects_val[(idx)].EN_data.en_cols.en_cols_val[(col)].ec_value.ec_value_len)
43 static enum nss_status
44 _nss_create_tablename (void)
46 if (tablename_val
== NULL
)
48 char buf
[40 + strlen (nis_local_directory ())];
51 p
= __stpcpy (buf
, "mail_aliases.org_dir.");
52 p
= __stpcpy (p
, nis_local_directory ());
53 tablename_val
= __strdup (buf
);
54 if (tablename_val
== NULL
)
55 return NSS_STATUS_TRYAGAIN
;
56 tablename_len
= strlen (tablename_val
);
58 return NSS_STATUS_SUCCESS
;
62 _nss_nisplus_parse_aliasent (nis_result
*result
, unsigned long entry
,
63 struct aliasent
*alias
, char *buffer
,
69 if ((result
->status
!= NIS_SUCCESS
&& result
->status
!= NIS_S_SUCCESS
) ||
70 __type_of (&result
->objects
.objects_val
[entry
]) != NIS_ENTRY_OBJ
||
71 strcmp(result
->objects
.objects_val
[entry
].EN_data
.en_type
,
72 "mail_aliases") != 0 ||
73 result
->objects
.objects_val
[entry
].EN_data
.en_cols
.en_cols_len
< 2)
77 char *first_unused
= buffer
+ NISENTRYLEN(0, 1, result
) + 1;
79 buflen
- (buflen
% __alignof__ (char *)) -
80 NISENTRYLEN(0, 1, result
) - 2;
84 if (NISENTRYLEN(entry
, 1, result
) >= buflen
)
86 /* The line is too long for our buffer. */
93 strncpy (buffer
, NISENTRYVAL(entry
, 1, result
),
94 NISENTRYLEN(entry
, 1, result
));
95 buffer
[NISENTRYLEN(entry
, 1, result
)] = '\0';
98 if (NISENTRYLEN(entry
, 0, result
) >= room_left
)
101 alias
->alias_local
= 0;
102 alias
->alias_members_len
= 0;
103 *first_unused
= '\0';
105 strcpy (first_unused
, NISENTRYVAL(entry
, 0, result
));
106 first_unused
[NISENTRYLEN(entry
, 0, result
)] = '\0';
107 alias
->alias_name
= first_unused
;
109 /* Terminate the line for any case. */
110 cp
= strpbrk (alias
->alias_name
, "#\n");
114 first_unused
+= strlen (alias
->alias_name
) +1;
115 /* Adjust the pointer so it is aligned for
117 first_unused
+= __alignof__ (char *) - 1;
118 first_unused
-= ((first_unused
- (char *) 0) % __alignof__ (char *));
119 alias
->alias_members
= (char **) first_unused
;
123 while (*line
!= '\0')
125 /* Skip leading blanks. */
126 while (isspace (*line
))
132 if (room_left
< sizeof (char *))
134 room_left
-= sizeof (char *);
135 alias
->alias_members
[alias
->alias_members_len
] = line
;
137 while (*line
!= '\0' && *line
!= ',')
140 if (line
!= alias
->alias_members
[alias
->alias_members_len
])
144 alias
->alias_members_len
++;
148 return alias
->alias_members_len
== 0 ? 0 : 1;
152 static enum nss_status
153 internal_setaliasent (void)
155 enum nss_status status
;
158 nis_freeresult (result
);
161 if (_nss_create_tablename () != NSS_STATUS_SUCCESS
)
162 return NSS_STATUS_UNAVAIL
;
165 result
= nis_list(tablename_val
, FOLLOW_PATH
| FOLLOW_LINKS
, NULL
, NULL
);
166 status
= niserr2nss (result
->status
);
167 if (status
!= NSS_STATUS_SUCCESS
)
169 nis_freeresult (result
);
176 _nss_nisplus_setaliasent (void)
178 enum nss_status status
;
180 __libc_lock_lock (lock
);
182 status
= internal_setaliasent ();
184 __libc_lock_unlock (lock
);
190 _nss_nisplus_endaliasent (void)
192 __libc_lock_lock (lock
);
195 nis_freeresult (result
);
199 __libc_lock_unlock (lock
);
201 return NSS_STATUS_SUCCESS
;
204 static enum nss_status
205 internal_nisplus_getaliasent_r (struct aliasent
*alias
,
206 char *buffer
, size_t buflen
)
212 enum nss_status status
;
214 status
= internal_setaliasent ();
215 if (result
== NULL
|| status
!= NSS_STATUS_SUCCESS
)
219 /* Get the next entry until we found a correct one. */
222 if (next_entry
>= result
->objects
.objects_len
)
223 return NSS_STATUS_NOTFOUND
;
225 if ((parse_res
= _nss_nisplus_parse_aliasent (result
, next_entry
, alias
,
226 buffer
, buflen
)) == -1)
227 return NSS_STATUS_TRYAGAIN
;
230 } while (!parse_res
);
232 return NSS_STATUS_SUCCESS
;
236 _nss_nisplus_getaliasent_r (struct aliasent
*result
, char *buffer
,
241 __libc_lock_lock (lock
);
243 status
= internal_nisplus_getaliasent_r (result
, buffer
, buflen
);
245 __libc_lock_unlock (lock
);
251 _nss_nisplus_getaliasbyname_r (const char *name
, struct aliasent
*alias
,
252 char *buffer
, size_t buflen
)
256 if (tablename_val
== NULL
)
257 if (_nss_create_tablename() != NSS_STATUS_SUCCESS
)
258 return NSS_STATUS_UNAVAIL
;
260 if (name
!= NULL
|| strlen(name
) <= 8)
263 char buf
[strlen (name
) + 30 + tablename_len
];
265 sprintf(buf
, "[name=%s],%s", name
, tablename_val
);
267 result
= nis_list(buf
, FOLLOW_PATH
| FOLLOW_LINKS
, NULL
, NULL
);
269 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
270 return niserr2nss (result
->status
);
272 if ((parse_res
= _nss_nisplus_parse_aliasent (result
, 0, alias
,
273 buffer
, buflen
)) == -1)
274 return NSS_STATUS_TRYAGAIN
;
277 return NSS_STATUS_SUCCESS
;
279 return NSS_STATUS_NOTFOUND
;