summaryrefslogtreecommitdiff
path: root/nss
diff options
context:
space:
mode:
authorArthur de Jong <arthur@arthurdejong.org>2009-05-29 21:22:58 +0000
committerArthur de Jong <arthur@arthurdejong.org>2009-05-29 21:22:58 +0000
commitf91b33e7b87b0447029e9b7593b796c3b9d0d0b0 (patch)
treecb66705231e6c4480c62c3b690867c426cf2763b /nss
parent7c32d0121c95bbb05ab064b29bf7ec2ba5defc00 (diff)
refactor protocol reading and writing macros to the common directory, use more logical names and in the PAM module no longer use NSS status codes (import of r887 from nss-pam-ldapd)
git-svn-id: http://arthurdejong.org/svn/nss-pam-ldapd/nss-ldapd@904 ef36b2f9-881f-0410-afb5-c4e39611909c
Diffstat (limited to 'nss')
-rw-r--r--nss/Makefile.am8
-rw-r--r--nss/aliases.c11
-rw-r--r--nss/common.c91
-rw-r--r--nss/common.h75
-rw-r--r--nss/ethers.c2
-rw-r--r--nss/group.c6
-rw-r--r--nss/hosts.c4
-rw-r--r--nss/netgroup.c17
-rw-r--r--nss/networks.c4
-rw-r--r--nss/passwd.c10
-rw-r--r--nss/protocols.c4
-rw-r--r--nss/rpc.c4
-rw-r--r--nss/services.c6
-rw-r--r--nss/shadow.c4
14 files changed, 50 insertions, 196 deletions
diff --git a/nss/Makefile.am b/nss/Makefile.am
index 0167d20..c283b41 100644
--- a/nss/Makefile.am
+++ b/nss/Makefile.am
@@ -2,7 +2,7 @@
#
# Copyright (C) 2006 Luke Howard
# Copyright (C) 2006 West Consulting
-# Copyright (C) 2006, 2007 Arthur de Jong
+# Copyright (C) 2006, 2007, 2009 Arthur de Jong
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
@@ -28,15 +28,15 @@ AM_CFLAGS = -fPIC
NSS_VERS = 2
NSS_LDAP_NSS_VERSIONED = libnss_ldap.so.$(NSS_VERS)
-nss_ldap_so_SOURCES = common.c common.h prototypes.h \
- ../nslcd.h ../nslcd-common.h \
+nss_ldap_so_SOURCES = common.h prototypes.h \
+ ../nslcd.h ../common/nslcd-prot.h \
../compat/attrs.h \
aliases.c ethers.c group.c hosts.c netgroup.c \
networks.c passwd.c protocols.c rpc.c services.c \
shadow.c
nss_ldap_so_LDFLAGS = -shared -Wl,-soname,$(NSS_LDAP_NSS_VERSIONED) \
-Wl,--version-script,\$(srcdir)/exports.linux
-nss_ldap_so_LDADD = @nss_ldap_so_LIBS@ ../common/libtio.a
+nss_ldap_so_LDADD = @nss_ldap_so_LIBS@ ../common/libtio.a ../common/libprot.a
EXTRA_DIST = exports.linux
diff --git a/nss/aliases.c b/nss/aliases.c
index 10d5576..dd27ede 100644
--- a/nss/aliases.c
+++ b/nss/aliases.c
@@ -33,11 +33,14 @@ static enum nss_status read_aliasent(
TFILE *fp,struct aliasent *result,
char *buffer,size_t buflen,int *errnop)
{
- int32_t tmpint32,tmp2int32;
+ int32_t tmpint32,tmp2int32,tmp3int32;
size_t bufptr=0;
- /* auto-genereted read code */
- READ_STRING_BUF(fp,result->alias_name);
- READ_STRINGLIST_NUM(fp,result->alias_members,result->alias_members_len);
+ /* read the name of the alias */
+ READ_BUF_STRING(fp,result->alias_name);
+ /* read the members */
+ READ_BUF_STRINGLIST(fp,result->alias_members);
+ /* tmp3int32 holds the number of entries read */
+ result->alias_members_len=tmp3int32;
/* fill in remaining gaps in struct */
result->alias_local=0;
/* we're done */
diff --git a/nss/common.c b/nss/common.c
deleted file mode 100644
index 6428bc2..0000000
--- a/nss/common.c
+++ /dev/null
@@ -1,91 +0,0 @@
-/*
- common.c - common functions for NSS lookups
-
- Copyright (C) 2006 West Consulting
- Copyright (C) 2006, 2007, 2008 Arthur de Jong
-
- This library is free software; you can redistribute it and/or
- modify it under the terms of the GNU Lesser General Public
- License as published by the Free Software Foundation; either
- version 2.1 of the License, or (at your option) any later version.
-
- This library is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- Lesser General Public License for more details.
-
- You should have received a copy of the GNU Lesser General Public
- License along with this library; if not, write to the Free Software
- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
- 02110-1301 USA
-*/
-
-#include "config.h"
-
-#ifdef HAVE_STDINT_H
-#include <stdint.h>
-#endif /* HAVE_STDINT_H */
-#include <unistd.h>
-#include <stdio.h>
-#include <sys/socket.h>
-#include <sys/un.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <errno.h>
-#include <nss.h>
-#include <string.h>
-
-#include "nslcd.h"
-#include "common.h"
-#include "common/tio.h"
-
-/* buffer sizes for I/O */
-#define READBUFFER_MINSIZE 1024
-#define READBUFFER_MAXSIZE 2*1024*1024
-#define WRITEBUFFER_MINSIZE 32
-#define WRITEBUFFER_MAXSIZE 32
-
-/* Note that the READBUFFER_MAXSIZE should be large enough to hold any single
- result entity as defined in nslcd.h because the get*ent() functions expect
- to be able to tio_reset() the stream to re-read the current entity.
- Since group entities can grow arbitrarily large, this setting limits the
- number of users that can be put in a group. */
-
-/* returns a socket to the server or NULL on error (see errno),
- socket should be closed with fclose() */
-TFILE *nslcd_client_open()
-{
- int sock;
- struct sockaddr_un addr;
- struct timeval readtimeout,writetimeout;
- TFILE *fp;
- /* create a socket */
- if ( (sock=socket(PF_UNIX,SOCK_STREAM,0))<0 )
- return NULL;
- /* create socket address structure */
- memset(&addr,0,sizeof(struct sockaddr_un));
- addr.sun_family=AF_UNIX;
- strncpy(addr.sun_path,NSLCD_SOCKET,sizeof(addr.sun_path));
- addr.sun_path[sizeof(addr.sun_path)-1]='\0';
- /* connect to the socket */
- if (connect(sock,(struct sockaddr *)&addr,(socklen_t)sizeof(struct sockaddr_un))<0)
- {
- (void)close(sock);
- return NULL;
- }
- /* set the timeouts */
- readtimeout.tv_sec=60; /* looking up stuff may take some time */
- readtimeout.tv_usec=0;
- writetimeout.tv_sec=10; /* nslcd could be loaded with requests */
- writetimeout.tv_usec=0;
- /* create a stream object */
- if ((fp=tio_fdopen(sock,&readtimeout,&writetimeout,
- READBUFFER_MINSIZE,READBUFFER_MAXSIZE,
- WRITEBUFFER_MINSIZE,WRITEBUFFER_MAXSIZE))==NULL)
- {
- (void)close(sock);
- return NULL;
- }
- /* return the stream */
- return fp;
-}
diff --git a/nss/common.h b/nss/common.h
index 01fce97..b0f2983 100644
--- a/nss/common.h
+++ b/nss/common.h
@@ -27,45 +27,8 @@
#include <nss.h>
#include "nslcd.h"
-#include "nslcd-common.h"
+#include "common/nslcd-prot.h"
#include "compat/attrs.h"
-#include "common/tio.h"
-
-/* returns a socket to the server or NULL on error (see errno),
- socket should be closed with tio_close() */
-TFILE *nslcd_client_open(void)
- MUST_USE;
-
-/* These are macors for performing common operations in the nslcd
- request/response protocol, they are an extension for client
- applications to the macros defined in nslcd-common.h. */
-
-/* Open a client socket. */
-#define OPEN_SOCK(fp) \
- if ((fp=nslcd_client_open())==NULL) \
- { ERROR_OUT_OPENERROR }
-
-/* Write a request header with a request code. */
-#define WRITE_REQUEST(fp,req) \
- WRITE_INT32(fp,(int32_t)NSLCD_VERSION) \
- WRITE_INT32(fp,(int32_t)req)
-
-/* Read a response header and check that the returned request
- code equals the expected code. */
-#define READ_RESPONSEHEADER(fp,req) \
- READ_TYPE(fp,tmpint32,int32_t); \
- if (tmpint32!=(int32_t)NSLCD_VERSION) \
- { ERROR_OUT_READERROR(fp) } \
- READ_TYPE(fp,tmpint32,int32_t); \
- if (tmpint32!=(int32_t)(req)) \
- { ERROR_OUT_READERROR(fp) }
-
-/* Read the response code (the result code of the query) from
- the stream. */
-#define READ_RESPONSE_CODE(fp) \
- READ_TYPE(fp,tmpint32,int32_t); \
- if (tmpint32!=(int32_t)NSLCD_RESULT_BEGIN) \
- { ERROR_OUT_NOSUCCESS(fp,tmpint32) }
/* These are macros for handling read and write problems, they are
NSS specific due to the return code so are defined here. They
@@ -98,7 +61,7 @@ TFILE *nslcd_client_open(void)
/* This macro is called if the read status code is not
NSLCD_RESULT_BEGIN. */
-#define ERROR_OUT_NOSUCCESS(fp,retv) \
+#define ERROR_OUT_NOSUCCESS(fp) \
(void)tio_close(fp); \
fp=NULL; \
return NSS_STATUS_NOTFOUND;
@@ -108,20 +71,9 @@ TFILE *nslcd_client_open(void)
bodies. These functions have very common code so this can
easily be reused. */
-#ifndef SKIP_BUFCHECK
-#define NSS_BUFCHECK \
- if ((buffer==NULL)||(buflen<=0)) \
- { \
- *errnop=EINVAL; \
- return NSS_STATUS_UNAVAIL; \
- }
-#else /* SKIP_BUFCHECK */
-#define NSS_BUFCHECK /* empty */
-#endif /* SKIP_BUFCHECK */
-
/* This is a generic get..by..() generation macro. The action
- parameter is the NSLCD_ACTION_.. action, the param is the
- operation for writing the parameter and readfn is the function
+ parameter is the NSLCD_ACTION_.. action, the writefn is the
+ operation for writing the parameters and readfn is the function
name for reading a single result entry. The function is assumed
to have result, buffer, buflen and errnop parameters that define
the result structure, the user buffer with length and the
@@ -132,14 +84,13 @@ TFILE *nslcd_client_open(void)
int32_t tmpint32; \
enum nss_status retv; \
/* check that we have a valid buffer */ \
- NSS_BUFCHECK \
+ if ((buffer==NULL)||(buflen<=0)) \
+ { \
+ *errnop=EINVAL; \
+ return NSS_STATUS_UNAVAIL; \
+ } \
/* open socket and write request */ \
- OPEN_SOCK(fp); \
- WRITE_REQUEST(fp,action); \
- writefn; \
- WRITE_FLUSH(fp); \
- /* read response header */ \
- READ_RESPONSEHEADER(fp,action); \
+ NSLCD_REQUEST(fp,action,writefn); \
/* read response */ \
READ_RESPONSE_CODE(fp); \
retv=readfn; \
@@ -196,11 +147,7 @@ TFILE *nslcd_client_open(void)
if (fp==NULL) \
{ \
/* open a new stream and write the request */ \
- OPEN_SOCK(fp); \
- WRITE_REQUEST(fp,action); \
- WRITE_FLUSH(fp); \
- /* read response header */ \
- READ_RESPONSEHEADER(fp,action); \
+ NSLCD_REQUEST(fp,action,/* no writefn */); \
} \
/* prepare for buffer errors */ \
tio_mark(fp); \
diff --git a/nss/ethers.c b/nss/ethers.c
index bf9ebb8..d059e31 100644
--- a/nss/ethers.c
+++ b/nss/ethers.c
@@ -36,7 +36,7 @@ static enum nss_status read_etherent(
{
int32_t tmpint32;
size_t bufptr=0;
- READ_STRING_BUF(fp,result->e_name);
+ READ_BUF_STRING(fp,result->e_name);
READ_TYPE(fp,result->e_addr,uint8_t[6]);
return NSS_STATUS_SUCCESS;
}
diff --git a/nss/group.c b/nss/group.c
index 4fe0123..ea5e819 100644
--- a/nss/group.c
+++ b/nss/group.c
@@ -37,10 +37,10 @@ static enum nss_status read_group(
{
int32_t tmpint32,tmp2int32,tmp3int32;
size_t bufptr=0;
- READ_STRING_BUF(fp,result->gr_name);
- READ_STRING_BUF(fp,result->gr_passwd);
+ READ_BUF_STRING(fp,result->gr_name);
+ READ_BUF_STRING(fp,result->gr_passwd);
READ_TYPE(fp,result->gr_gid,gid_t);
- READ_STRINGLIST_NULLTERM(fp,result->gr_mem);
+ READ_BUF_STRINGLIST(fp,result->gr_mem);
return NSS_STATUS_SUCCESS;
}
diff --git a/nss/hosts.c b/nss/hosts.c
index a73b83b..166792d 100644
--- a/nss/hosts.c
+++ b/nss/hosts.c
@@ -72,8 +72,8 @@ static enum nss_status read_hostent(
int readaf;
size_t bufptr=0;
/* read the host entry */
- READ_STRING_BUF(fp,result->h_name);
- READ_STRINGLIST_NULLTERM(fp,result->h_aliases);
+ READ_BUF_STRING(fp,result->h_name);
+ READ_BUF_STRINGLIST(fp,result->h_aliases);
result->h_addrtype=af;
result->h_length=0;
/* read number of addresses to follow */
diff --git a/nss/netgroup.c b/nss/netgroup.c
index 57299ea..63e0b97 100644
--- a/nss/netgroup.c
+++ b/nss/netgroup.c
@@ -34,7 +34,7 @@
/* we redefine this here because we need to return NSS_STATUS_RETURN
instead of NSS_STATUS_NOTFOUND */
#undef ERROR_OUT_NOSUCCESS
-#define ERROR_OUT_NOSUCCESS(fp,retv) \
+#define ERROR_OUT_NOSUCCESS(fp) \
(void)tio_close(fp); \
fp=NULL; \
return NSS_STATUS_RETURN;
@@ -53,28 +53,28 @@ static enum nss_status read_netgrent(
{
/* the response is a reference to another netgroup */
result->type=group_val;
- READ_STRING_BUF(fp,result->val.group);
+ READ_BUF_STRING(fp,result->val.group);
}
else if (type==NSLCD_NETGROUP_TYPE_TRIPLE)
{
/* the response is a host/user/domain triple */
result->type=triple_val;
/* read host and revert to NULL on empty string */
- READ_STRING_BUF(fp,result->val.triple.host);
+ READ_BUF_STRING(fp,result->val.triple.host);
if (result->val.triple.host[0]=='\0')
{
result->val.triple.host=NULL;
bufptr--; /* free unused space */
}
/* read user and revert to NULL on empty string */
- READ_STRING_BUF(fp,result->val.triple.user);
+ READ_BUF_STRING(fp,result->val.triple.user);
if (result->val.triple.user[0]=='\0')
{
result->val.triple.user=NULL;
bufptr--; /* free unused space */
}
/* read domain and revert to NULL on empty string */
- READ_STRING_BUF(fp,result->val.triple.domain);
+ READ_BUF_STRING(fp,result->val.triple.domain);
if (result->val.triple.domain[0]=='\0')
{
result->val.triple.domain=NULL;
@@ -102,12 +102,7 @@ enum nss_status _nss_ldap_setnetgrent(const char *group,struct __netgrent UNUSED
if ((group==NULL)||(group[0]=='\0'))
return NSS_STATUS_UNAVAIL;
/* open a new stream and write the request */
- OPEN_SOCK(netgrentfp);
- WRITE_REQUEST(netgrentfp,NSLCD_ACTION_NETGROUP_BYNAME);
- WRITE_STRING(netgrentfp,group);
- WRITE_FLUSH(netgrentfp);
- /* read response header */
- READ_RESPONSEHEADER(netgrentfp,NSLCD_ACTION_NETGROUP_BYNAME);
+ NSLCD_REQUEST(netgrentfp,NSLCD_ACTION_NETGROUP_BYNAME,WRITE_STRING(netgrentfp,group));
return NSS_STATUS_SUCCESS;
}
diff --git a/nss/networks.c b/nss/networks.c
index 6b3dcd1..7e1cf11 100644
--- a/nss/networks.c
+++ b/nss/networks.c
@@ -70,8 +70,8 @@ static enum nss_status read_netent(
size_t bufptr=0;
enum nss_status retv=NSS_STATUS_NOTFOUND;
/* read the network entry */
- READ_STRING_BUF(fp,result->n_name);
- READ_STRINGLIST_NULLTERM(fp,result->n_aliases);
+ READ_BUF_STRING(fp,result->n_name);
+ READ_BUF_STRINGLIST(fp,result->n_aliases);
result->n_addrtype=AF_INET;
/* read number of addresses to follow */
READ_TYPE(fp,numaddr,int32_t);
diff --git a/nss/passwd.c b/nss/passwd.c
index 29d70fc..eb5e71b 100644
--- a/nss/passwd.c
+++ b/nss/passwd.c
@@ -36,13 +36,13 @@ static enum nss_status read_passwd(
{
int32_t tmpint32;
size_t bufptr=0;
- READ_STRING_BUF(fp,result->pw_name);
- READ_STRING_BUF(fp,result->pw_passwd);
+ READ_BUF_STRING(fp,result->pw_name);
+ READ_BUF_STRING(fp,result->pw_passwd);
READ_TYPE(fp,result->pw_uid,uid_t);
READ_TYPE(fp,result->pw_gid,gid_t);
- READ_STRING_BUF(fp,result->pw_gecos);
- READ_STRING_BUF(fp,result->pw_dir);
- READ_STRING_BUF(fp,result->pw_shell);
+ READ_BUF_STRING(fp,result->pw_gecos);
+ READ_BUF_STRING(fp,result->pw_dir);
+ READ_BUF_STRING(fp,result->pw_shell);
return NSS_STATUS_SUCCESS;
}
diff --git a/nss/protocols.c b/nss/protocols.c
index 855c4ad..9a36b15 100644
--- a/nss/protocols.c
+++ b/nss/protocols.c
@@ -36,8 +36,8 @@ static enum nss_status read_protoent(
{
int32_t tmpint32,tmp2int32,tmp3int32;
size_t bufptr=0;
- READ_STRING_BUF(fp,result->p_name);
- READ_STRINGLIST_NULLTERM(fp,result->p_aliases);
+ READ_BUF_STRING(fp,result->p_name);
+ READ_BUF_STRINGLIST(fp,result->p_aliases);
READ_INT32(fp,result->p_proto);
return NSS_STATUS_SUCCESS;
}
diff --git a/nss/rpc.c b/nss/rpc.c
index 8d73ee7..71e0be9 100644
--- a/nss/rpc.c
+++ b/nss/rpc.c
@@ -36,8 +36,8 @@ static enum nss_status read_rpcent(
{
int32_t tmpint32,tmp2int32,tmp3int32;
size_t bufptr=0;
- READ_STRING_BUF(fp,result->r_name);
- READ_STRINGLIST_NULLTERM(fp,result->r_aliases);
+ READ_BUF_STRING(fp,result->r_name);
+ READ_BUF_STRINGLIST(fp,result->r_aliases);
READ_INT32(fp,result->r_number);
return NSS_STATUS_SUCCESS;
}
diff --git a/nss/services.c b/nss/services.c
index 27625e6..6399ac6 100644
--- a/nss/services.c
+++ b/nss/services.c
@@ -36,12 +36,12 @@ static enum nss_status read_servent(
{
int32_t tmpint32,tmp2int32,tmp3int32;
size_t bufptr=0;
- READ_STRING_BUF(fp,result->s_name);
- READ_STRINGLIST_NULLTERM(fp,result->s_aliases);
+ READ_BUF_STRING(fp,result->s_name);
+ READ_BUF_STRINGLIST(fp,result->s_aliases);
/* store port number in network byte order */
READ_TYPE(fp,tmpint32,int32_t);
result->s_port=ntohs((uint16_t)tmpint32);
- READ_STRING_BUF(fp,result->s_proto);
+ READ_BUF_STRING(fp,result->s_proto);
/* we're done */
return NSS_STATUS_SUCCESS;
}
diff --git a/nss/shadow.c b/nss/shadow.c
index 195489a..3d6d82c 100644
--- a/nss/shadow.c
+++ b/nss/shadow.c
@@ -36,8 +36,8 @@ static enum nss_status read_spwd(
{
int32_t tmpint32;
size_t bufptr=0;
- READ_STRING_BUF(fp,result->sp_namp);
- READ_STRING_BUF(fp,result->sp_pwdp);
+ READ_BUF_STRING(fp,result->sp_namp);
+ READ_BUF_STRING(fp,result->sp_pwdp);
READ_INT32(fp,result->sp_lstchg);
READ_INT32(fp,result->sp_min);
READ_INT32(fp,result->sp_max);