Update.
[glibc.git] / nss / nss_db / db-netgrp.c
blob8707d85f0437ae8cb8cfbf6d08a548e5158b0fb2
1 /* Netgroup file parser in nss_db modules.
2 Copyright (C) 1996, 1997 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 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 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 #include <db.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <string.h>
25 #include <bits/libc-lock.h>
26 #include <paths.h>
27 #include "nsswitch.h"
28 #include "netgroup.h"
31 #define DBFILE _PATH_VARDB "netgroup.db"
34 /* Locks the static variables in this file. */
35 __libc_lock_define_initialized (static, lock)
37 /* Maintenance of the shared handle open on the database. */
38 static DB *db;
39 static char *entry;
40 static char *cursor;
42 enum nss_status
43 _nss_db_setnetgrent (const char *group)
45 enum nss_status status = NSS_STATUS_SUCCESS;
46 int err;
48 __libc_lock_lock (lock);
50 /* Make sure the data base file is open. */
51 if (db == NULL)
53 err = __nss_db_open (DBFILE, DB_BTREE, O_RDONLY, 0, NULL, NULL, &db);
55 if (err != 0)
57 __set_errno (err);
58 status = err == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
60 else
62 /* We have to make sure the file is `closed on exec'. */
63 int fd;
64 int result, flags;
66 err = db->fd (db, &fd);
67 if (err != 0)
69 __set_errno (err);
70 result = -1;
72 else
73 result = flags = fcntl (fd, F_GETFD, 0);
74 if (result >= 0)
76 flags |= FD_CLOEXEC;
77 result = fcntl (fd, F_SETFD, flags);
79 if (result < 0)
81 /* Something went wrong. Close the stream and return a
82 failure. */
83 db->close (db, 0);
84 db = NULL;
85 status = NSS_STATUS_UNAVAIL;
90 if (status == NSS_STATUS_SUCCESS)
92 DBT key = { data: (void *) group, size: strlen (group), flags: 0 };
93 DBT value;
95 value.flags = 0;
96 if (db->get (db, NULL, &key, &value, 0) != 0)
97 status = NSS_STATUS_NOTFOUND;
98 else
99 cursor = entry = value.data;
102 __libc_lock_unlock (lock);
104 return status;
109 enum nss_status
110 _nss_db_endnetgrent (void)
112 __libc_lock_lock (lock);
114 if (db != NULL)
116 db->close (db, 0);
117 db = NULL;
120 __libc_lock_unlock (lock);
122 return NSS_STATUS_SUCCESS;
126 extern enum nss_status _nss_netgroup_parseline (char **cursor,
127 struct __netgrent *result,
128 char *buffer, size_t buflen,
129 int *errnop);
131 enum nss_status
132 _nss_db_getnetgrent_r (struct __netgrent *result, char *buffer, size_t buflen,
133 int *errnop)
135 int status;
137 __libc_lock_lock (lock);
139 status = _nss_netgroup_parseline (&cursor, result, buffer, buflen, errnop);
141 __libc_lock_unlock (lock);
143 return status;