diff options
author | Arthur de Jong <arthur@arthurdejong.org> | 2006-11-30 18:52:45 +0100 |
---|---|---|
committer | Arthur de Jong <arthur@arthurdejong.org> | 2006-11-30 18:52:45 +0100 |
commit | 3c38a95f26742a77e664878517da8a6a3db7847e (patch) | |
tree | d47ffaab8099f1fc42a7e48c1f07f974d5f16c02 | |
parent | 3239c5ad583d149f656d99eaa46d2aa49611b4b0 (diff) |
implement a simple threading solution and move code from nslcd-server.c to nslcd.c
git-svn-id: http://arthurdejong.org/svn/nss-pam-ldapd/libnss_ldapd@135 ef36b2f9-881f-0410-afb5-c4e39611909c
-rw-r--r-- | server/Makefile.am | 3 | ||||
-rw-r--r-- | server/alias.c | 1 | ||||
-rw-r--r-- | server/common.h | 38 | ||||
-rw-r--r-- | server/ether.c | 1 | ||||
-rw-r--r-- | server/group.c | 1 | ||||
-rw-r--r-- | server/host.c | 1 | ||||
-rw-r--r-- | server/ldap-nss.c | 1 | ||||
-rw-r--r-- | server/netgroup.c | 1 | ||||
-rw-r--r-- | server/network.c | 1 | ||||
-rw-r--r-- | server/nslcd-server.c | 191 | ||||
-rw-r--r-- | server/nslcd-server.h | 72 | ||||
-rw-r--r-- | server/nslcd.c | 213 | ||||
-rw-r--r-- | server/passwd.c | 1 | ||||
-rw-r--r-- | server/protocol.c | 1 | ||||
-rw-r--r-- | server/rpc.c | 1 | ||||
-rw-r--r-- | server/service.c | 1 | ||||
-rw-r--r-- | server/shadow.c | 1 | ||||
-rw-r--r-- | server/util.c | 1 |
18 files changed, 230 insertions, 300 deletions
diff --git a/server/Makefile.am b/server/Makefile.am index c5f5ed0..f751bc1 100644 --- a/server/Makefile.am +++ b/server/Makefile.am @@ -21,7 +21,6 @@ noinst_PROGRAMS = nslcd nslcd_SOURCES = nslcd.c ../nslcd.h ../nslcd-common.h \ - nslcd-server.c nslcd-server.h \ log.c log.h \ xmalloc.c xmalloc.h \ common.c common.h \ @@ -30,3 +29,5 @@ nslcd_SOURCES = nslcd.c ../nslcd.h ../nslcd-common.h \ dnsconfig.c dnsconfig.h ldap-nss.c ldap-nss.h \ ldap-schema.c ldap-schema.h pagectrl.c pagectrl.h \ resolve.c resolve.h util.c util.h + +nslcd_CFLAGS = -pthread diff --git a/server/alias.c b/server/alias.c index 04cf6b6..62e29f3 100644 --- a/server/alias.c +++ b/server/alias.c @@ -40,7 +40,6 @@ #include "ldap-nss.h" #include "util.h" -#include "nslcd-server.h" #include "common.h" #include "log.h" diff --git a/server/common.h b/server/common.h index 69b1732..42f47ea 100644 --- a/server/common.h +++ b/server/common.h @@ -24,12 +24,14 @@ #ifndef _SERVER_COMMON_H #define _SERVER_COMMON_H 1 -#include <nss.h> +#include "nslcd.h" #include "nslcd-common.h" + /* translates a nss code (as defined in nss.h) to a nslcd return code (as defined in nslcd.h) */ /* FIXME: this is a temporary hack, get rid of it */ +#include <nss.h> int nss2nslcd(enum nss_status code); @@ -50,4 +52,38 @@ int nss2nslcd(enum nss_status code); log_log(LOG_ERR,"error allocating memory"); \ return -1; + +/* these are the different functions that handle the database + specific actions, see nslcd.h for the action descriptions */ +int nslcd_alias_byname(FILE *fp); +int nslcd_alias_all(FILE *fp); +int nslcd_ether_byname(FILE *fp); +int nslcd_ether_byether(FILE *fp); +int nslcd_ether_all(FILE *fp); +int nslcd_group_byname(FILE *fp); +int nslcd_group_bygid(FILE *fp); +int nslcd_group_bymember(FILE *fp); +int nslcd_group_all(FILE *fp); +int nslcd_host_byname(FILE *fp); +int nslcd_host_byaddr(FILE *fp); +int nslcd_host_all(FILE *fp); +int nslcd_netgroup_byname(FILE *fp); +int nslcd_network_byname(FILE *fp); +int nslcd_network_byaddr(FILE *fp); +int nslcd_network_all(FILE *fp); +int nslcd_passwd_byname(FILE *fp); +int nslcd_passwd_byuid(FILE *fp); +int nslcd_passwd_all(FILE *fp); +int nslcd_protocol_byname(FILE *fp); +int nslcd_protocol_bynumber(FILE *fp); +int nslcd_protocol_all(FILE *fp); +int nslcd_rpc_byname(FILE *fp); +int nslcd_rpc_bynumber(FILE *fp); +int nslcd_rpc_all(FILE *fp); +int nslcd_service_byname(FILE *fp); +int nslcd_service_bynumber(FILE *fp); +int nslcd_service_all(FILE *fp); +int nslcd_shadow_byname(FILE *fp); +int nslcd_shadow_all(FILE *fp); + #endif /* not _SERVER_COMMON_H */ diff --git a/server/ether.c b/server/ether.c index 6efbf9c..3ebd4fd 100644 --- a/server/ether.c +++ b/server/ether.c @@ -57,7 +57,6 @@ #include "ldap-nss.h" #include "util.h" -#include "nslcd-server.h" #include "common.h" #include "log.h" diff --git a/server/group.c b/server/group.c index 5442a5e..c8b1b2b 100644 --- a/server/group.c +++ b/server/group.c @@ -46,7 +46,6 @@ #include "ldap-nss.h" #include "util.h" -#include "nslcd-server.h" #include "common.h" #include "log.h" diff --git a/server/host.c b/server/host.c index 3af1b65..cadfce9 100644 --- a/server/host.c +++ b/server/host.c @@ -51,7 +51,6 @@ #include "ldap-nss.h" #include "util.h" -#include "nslcd-server.h" #include "common.h" #include "log.h" diff --git a/server/ldap-nss.c b/server/ldap-nss.c index 0267aea..0266846 100644 --- a/server/ldap-nss.c +++ b/server/ldap-nss.c @@ -84,7 +84,6 @@ #include "util.h" #include "dnsconfig.h" #include "pagectrl.h" -#include "nslcd-server.h" #include "common.h" #include "log.h" diff --git a/server/netgroup.c b/server/netgroup.c index 2599d28..a9975c9 100644 --- a/server/netgroup.c +++ b/server/netgroup.c @@ -50,7 +50,6 @@ #include "ldap-nss.h" #include "util.h" -#include "nslcd-server.h" #include "common.h" #include "log.h" diff --git a/server/network.c b/server/network.c index 98f2caa..e818d4e 100644 --- a/server/network.c +++ b/server/network.c @@ -48,7 +48,6 @@ #include "ldap-nss.h" #include "util.h" -#include "nslcd-server.h" #include "common.h" #include "log.h" diff --git a/server/nslcd-server.c b/server/nslcd-server.c deleted file mode 100644 index 7f3c10d..0000000 --- a/server/nslcd-server.c +++ /dev/null @@ -1,191 +0,0 @@ -/* - nslcd-server.c - server socket routines - - Copyright (C) 2006 West Consulting - Copyright (C) 2006 Arthur de Jong - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public - License along with this library; if not, write to the Free - Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, - MA 02110-1301 USA -*/ - -#include "config.h" - -#include <stdlib.h> -#include <stdint.h> -#include <unistd.h> -#include <malloc.h> -#include <stdio.h> -#include <sys/socket.h> -#include <sys/un.h> -#include <errno.h> -#include <fcntl.h> - -#include "nslcd-server.h" -#include "log.h" - - -/* returns a socket ready to answer requests from the client, - return <0 on error */ -int nslcd_server_open(void) -{ - int sock; - /*int flag;*/ - struct sockaddr_un addr; - - /* create a socket */ - if ( (sock=socket(PF_UNIX,SOCK_STREAM,0))<0 ) - { - log_log(LOG_ERR,"cannot create socket: %s",strerror(errno)); - exit(1); - } - - /* create socket address structure */ - memset(&addr,0,sizeof(struct sockaddr_un)); - addr.sun_family=AF_UNIX; - strcpy(addr.sun_path,NSLCD_SOCKET); - - /* unlink to socket */ - if (unlink(NSLCD_SOCKET)<0) - { - log_log(LOG_DEBUG,"unlink() of "NSLCD_SOCKET" failed (ignored): %s", - strerror(errno)); - } - - /* bind to the socket */ - if (bind(sock,(struct sockaddr *)&addr,sizeof(struct sockaddr_un))<0) - { - log_log(LOG_ERR,"bind() to "NSLCD_SOCKET" failed: %s", - strerror(errno)); - if (close(sock)) - log_log(LOG_WARNING,"problem closing socket: %s",strerror(errno)); - exit(1); - } - -#ifdef NONBLOCKING - /* we are going to block for now and implement threading later on */ - /* do not block on accept() */ - if ((flag=fcntl(sock,F_GETFL,0))<0) - { - log_log(LOG_ERR,"fctnl(F_GETFL) failed: %s",strerror(errno)); - if (close(sock)) - log_log(LOG_WARNING,"problem closing socket: %s",strerror(errno)); - exit(1); - } - if (fcntl(sock,F_SETFL,flag|O_NONBLOCK)<0) - { - log_log(LOG_ERR,"fctnl(F_SETFL,O_NONBLOCK) failed: %s",strerror(errno)); - if (close(sock)) - log_log(LOG_WARNING,"problem closing socket: %s",strerror(errno)); - exit(1); - } -#endif /* NONBLOCKING */ - - /* close the file descriptor on exit */ - if (fcntl(sock,F_SETFD,FD_CLOEXEC)<0) - { - log_log(LOG_ERR,"fctnl(F_SETFL,O_NONBLOCK) failed: %s",strerror(errno)); - if (close(sock)) - log_log(LOG_WARNING,"problem closing socket: %s",strerror(errno)); - exit(1); - } - -#ifdef DONT_FOR_NOW - /* Set permissions for the socket. */ - chmod (_PATH_NSCDSOCKET, DEFFILEMODE); -#endif /* DONT_FOR_NOW */ - - /* start listening for connections */ - if (listen(sock,SOMAXCONN)<0) - { - log_log(LOG_ERR,"listen() failed: %s",strerror(errno)); - if (close(sock)) - log_log(LOG_WARNING,"problem closing socket: %s",strerror(errno)); - exit(1); - } - - /* we're done */ - return sock; -} - -/* redifine the ERROR_OUT mechanism */ -#undef ERROR_OUT_READERROR -#define ERROR_OUT_READERROR(fp) \ - fclose(fp); \ - log_log(LOG_DEBUG,"error reading from stream: %s",strerror(errno)); \ - return; - -/* read a request message, returns <0 in case of errors, - this function closes the socket */ -void nslcd_server_handlerequest(int sock) -{ - int32_t tmpint32; - FILE *fp; - /* create a stream object */ - if ((fp=fdopen(sock,"w+"))==NULL) - { - close(sock); - return; - } - /* read the protocol version */ - READ_TYPE(fp,tmpint32,int32_t); - if (tmpint32 != NSLCD_VERSION) - { - fclose(fp); - log_log(LOG_DEBUG,"wrong nslcd version id (%d)",(int)tmpint32); - return; - } - /* read the request type */ - READ_TYPE(fp,tmpint32,int32_t); - /* handle request */ - switch (tmpint32) - { - case NSLCD_ACTION_ALIAS_BYNAME: nslcd_alias_byname(fp); break; - case NSLCD_ACTION_ALIAS_ALL: nslcd_alias_all(fp); break; - case NSLCD_ACTION_ETHER_BYNAME: nslcd_ether_byname(fp); break; - case NSLCD_ACTION_ETHER_BYETHER: nslcd_ether_byether(fp); break; - case NSLCD_ACTION_ETHER_ALL: nslcd_ether_all(fp); break; - case NSLCD_ACTION_GROUP_BYNAME: nslcd_group_byname(fp); break; - case NSLCD_ACTION_GROUP_BYGID: nslcd_group_bygid(fp); break; - case NSLCD_ACTION_GROUP_BYMEMBER: nslcd_group_bymember(fp); break; - case NSLCD_ACTION_GROUP_ALL: nslcd_group_all(fp); break; - case NSLCD_ACTION_HOST_BYNAME: nslcd_host_byname(fp); break; - case NSLCD_ACTION_HOST_BYADDR: nslcd_host_byaddr(fp); break; - case NSLCD_ACTION_HOST_ALL: nslcd_host_all(fp); break; - case NSLCD_ACTION_NETGROUP_BYNAME: nslcd_netgroup_byname(fp); break; - case NSLCD_ACTION_NETWORK_BYNAME: nslcd_network_byname(fp); break; - case NSLCD_ACTION_NETWORK_BYADDR: nslcd_network_byaddr(fp); break; - case NSLCD_ACTION_NETWORK_ALL: nslcd_network_all(fp); break; - case NSLCD_ACTION_PASSWD_BYNAME: nslcd_passwd_byname(fp); break; - case NSLCD_ACTION_PASSWD_BYUID: nslcd_passwd_byuid(fp); break; - case NSLCD_ACTION_PASSWD_ALL: nslcd_passwd_all(fp); break; - case NSLCD_ACTION_PROTOCOL_BYNAME: nslcd_protocol_byname(fp); break; - case NSLCD_ACTION_PROTOCOL_BYNUMBER:nslcd_protocol_bynumber(fp); break; - case NSLCD_ACTION_PROTOCOL_ALL: nslcd_protocol_all(fp); break; - case NSLCD_ACTION_RPC_BYNAME: nslcd_rpc_byname(fp); break; - case NSLCD_ACTION_RPC_BYNUMBER: nslcd_rpc_bynumber(fp); break; - case NSLCD_ACTION_RPC_ALL: nslcd_rpc_all(fp); break; - case NSLCD_ACTION_SERVICE_BYNAME: nslcd_service_byname(fp); break; - case NSLCD_ACTION_SERVICE_BYNUMBER: nslcd_service_bynumber(fp); break; - case NSLCD_ACTION_SERVICE_ALL: nslcd_service_all(fp); break; - case NSLCD_ACTION_SHADOW_BYNAME: nslcd_shadow_byname(fp); break; - case NSLCD_ACTION_SHADOW_ALL: nslcd_shadow_all(fp); break; - default: - log_log(LOG_WARNING,"invalid request id: %d",(int)tmpint32); - break; - } - /* we're done with the request */ - fclose(fp); - return; -} diff --git a/server/nslcd-server.h b/server/nslcd-server.h deleted file mode 100644 index 2b6b80a..0000000 --- a/server/nslcd-server.h +++ /dev/null @@ -1,72 +0,0 @@ -/* - nslcd-server.h - server socket routines - - Copyright (C) 2006 West Consulting - Copyright (C) 2006 Arthur de Jong - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public - License along with this library; if not, write to the Free - Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, - MA 02110-1301 USA -*/ - -#ifndef _NSLCD_SERVER_H -#define _NSLCD_SERVER_H 1 - -#include "nslcd.h" -#include "nslcd-common.h" - -/* returns a socket ready to answer requests from the client, - return <0 on error */ -int nslcd_server_open(void); - -/* create a strem from the client socket, read a request message - and pass the stream to one of the functions below. - returns <0 in case of errors, this function closes the socket */ -void nslcd_server_handlerequest(int sock); - -/* LDAP methods */ -/* TODO: these definitions should probably be moved */ - -int nslcd_alias_byname(FILE *fp); -int nslcd_alias_all(FILE *fp); -int nslcd_ether_byname(FILE *fp); -int nslcd_ether_byether(FILE *fp); -int nslcd_ether_all(FILE *fp); -int nslcd_group_byname(FILE *fp); -int nslcd_group_bygid(FILE *fp); -int nslcd_group_bymember(FILE *fp); -int nslcd_group_all(FILE *fp); -int nslcd_host_byname(FILE *fp); -int nslcd_host_byaddr(FILE *fp); -int nslcd_host_all(FILE *fp); -int nslcd_netgroup_byname(FILE *fp); -int nslcd_network_byname(FILE *fp); -int nslcd_network_byaddr(FILE *fp); -int nslcd_network_all(FILE *fp); -int nslcd_passwd_byname(FILE *fp); -int nslcd_passwd_byuid(FILE *fp); -int nslcd_passwd_all(FILE *fp); -int nslcd_protocol_byname(FILE *fp); -int nslcd_protocol_bynumber(FILE *fp); -int nslcd_protocol_all(FILE *fp); -int nslcd_rpc_byname(FILE *fp); -int nslcd_rpc_bynumber(FILE *fp); -int nslcd_rpc_all(FILE *fp); -int nslcd_service_byname(FILE *fp); -int nslcd_service_bynumber(FILE *fp); -int nslcd_service_all(FILE *fp); -int nslcd_shadow_byname(FILE *fp); -int nslcd_shadow_all(FILE *fp); - -#endif /* not _NSLCD_SERVER_H */ diff --git a/server/nslcd.c b/server/nslcd.c index 25db64e..5fb87f8 100644 --- a/server/nslcd.c +++ b/server/nslcd.c @@ -23,11 +23,12 @@ #include "config.h" #include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <stdint.h> #include <sys/types.h> #include <sys/param.h> #include <sys/wait.h> -#include <string.h> -#include <stdio.h> #ifdef HAVE_GETOPT_H #include <getopt.h> #endif /* HAVE_GETOPT_H */ @@ -38,15 +39,16 @@ #include <fcntl.h> #include <sys/stat.h> #include <sys/socket.h> +#include <sys/un.h> #ifdef HAVE_GRP_H #include <grp.h> #endif /* HAVE_GRP_H */ #include <nss.h> +#include <pthread.h> #include "nslcd.h" -#include "nslcd-server.h" -#include "xmalloc.h" #include "log.h" +#include "common.h" /* the definition of the environment */ @@ -65,6 +67,11 @@ static volatile int nslcd_exitsignal=0; static int nslcd_serversocket=-1; +/* thread ids of all running threads */ +#define NUM_THREADS 5 +pthread_t nslcd_threads[NUM_THREADS]; + + /* display version information */ static void display_version(FILE *fp) { @@ -198,7 +205,12 @@ static const char *signame(int signum) /* signal handler for closing down */ static RETSIGTYPE sigexit_handler(int signum) { + int i; nslcd_exitsignal=signum; + /* cancel all running threads */ + for (i=0;i<NUM_THREADS;i++) + pthread_cancel(nslcd_threads[i]); + } /* do some cleaning up before terminating */ @@ -213,18 +225,101 @@ static void exithandler(void) } -/* handle a connection by doing fork() and stuff */ -static void handleconnection(int csock) +/* returns a socket ready to answer requests from the client, + return <0 on error */ +static int open_socket(void) +{ + int sock; + struct sockaddr_un addr; + + /* create a socket */ + if ( (sock=socket(PF_UNIX,SOCK_STREAM,0))<0 ) + { + log_log(LOG_ERR,"cannot create socket: %s",strerror(errno)); + exit(1); + } + + /* create socket address structure */ + memset(&addr,0,sizeof(struct sockaddr_un)); + addr.sun_family=AF_UNIX; + strcpy(addr.sun_path,NSLCD_SOCKET); + + /* unlink to socket */ + if (unlink(NSLCD_SOCKET)<0) + { + log_log(LOG_DEBUG,"unlink() of "NSLCD_SOCKET" failed (ignored): %s", + strerror(errno)); + } + + /* bind to the socket */ + if (bind(sock,(struct sockaddr *)&addr,sizeof(struct sockaddr_un))<0) + { + log_log(LOG_ERR,"bind() to "NSLCD_SOCKET" failed: %s", + strerror(errno)); + if (close(sock)) + log_log(LOG_WARNING,"problem closing socket: %s",strerror(errno)); + exit(1); + } + + /* close the file descriptor on exit */ + if (fcntl(sock,F_SETFD,FD_CLOEXEC)<0) + { + log_log(LOG_ERR,"fctnl(F_SETFL,O_NONBLOCK) failed: %s",strerror(errno)); + if (close(sock)) + log_log(LOG_WARNING,"problem closing socket: %s",strerror(errno)); + exit(1); + } + +#ifdef DONT_FOR_NOW + /* Set permissions for the socket. */ + chmod (_PATH_NSCDSOCKET, DEFFILEMODE); +#endif /* DONT_FOR_NOW */ + + /* start listening for connections */ + if (listen(sock,SOMAXCONN)<0) + { + log_log(LOG_ERR,"listen() failed: %s",strerror(errno)); + if (close(sock)) + log_log(LOG_WARNING,"problem closing socket: %s",strerror(errno)); + exit(1); + } + + /* we're done */ + return sock; +} + +/* read the version information and action from the stream + this function returns the read action in location pointer to by action */ +static int read_header(FILE *fp,int32_t *action) +{ + int32_t tmpint32; + /* read the protocol version */ + READ_TYPE(fp,tmpint32,int32_t); + if (tmpint32 != NSLCD_VERSION) + { + log_log(LOG_DEBUG,"wrong nslcd version id (%d)",(int)tmpint32); + return -1; + } + /* read the request type */ + READ(fp,action,sizeof(int32_t)); + return 0; +} + +/* read a request message, returns <0 in case of errors, + this function closes the socket */ +static void handleconnection(int sock) { + FILE *fp; socklen_t alen; struct ucred client; + int32_t action; /* look up process information from client */ alen=sizeof(struct ucred); - if (getsockopt(csock,SOL_SOCKET,SO_PEERCRED,&client,&alen) < 0) + if (getsockopt(sock,SOL_SOCKET,SO_PEERCRED,&client,&alen) < 0) { log_log(LOG_ERR,"getsockopt(SO_PEERCRED) failed: %s", strerror(errno)); - if (close(csock)) + if (close(sock)) log_log(LOG_WARNING,"problem closing socket: %s",strerror(errno)); return; } @@ -233,12 +328,62 @@ static void handleconnection(int csock) log_log(LOG_INFO,"connection from pid=%d uid=%d gid=%d", (int)client.pid,(int)client.uid,(int)client.gid); - /* FIXME: pass credentials along? */ + /* create a stream object */ + if ((fp=fdopen(sock,"w+"))==NULL) + { + close(sock); + return; + } - nslcd_server_handlerequest(csock); + /* read request */ + if (read_header(fp,&action)) + { + fclose(fp); + return; + } -} + /* handle request */ + switch (action) + { + case NSLCD_ACTION_ALIAS_BYNAME: nslcd_alias_byname(fp); break; + case NSLCD_ACTION_ALIAS_ALL: nslcd_alias_all(fp); break; + case NSLCD_ACTION_ETHER_BYNAME: nslcd_ether_byname(fp); break; + case NSLCD_ACTION_ETHER_BYETHER: nslcd_ether_byether(fp); break; + case NSLCD_ACTION_ETHER_ALL: nslcd_ether_all(fp); break; + case NSLCD_ACTION_GROUP_BYNAME: nslcd_group_byname(fp); break; + case NSLCD_ACTION_GROUP_BYGID: nslcd_group_bygid(fp); break; + case NSLCD_ACTION_GROUP_BYMEMBER: nslcd_group_bymember(fp); break; + case NSLCD_ACTION_GROUP_ALL: nslcd_group_all(fp); break; + case NSLCD_ACTION_HOST_BYNAME: nslcd_host_byname(fp); break; + case NSLCD_ACTION_HOST_BYADDR: nslcd_host_byaddr(fp); break; + case NSLCD_ACTION_HOST_ALL: nslcd_host_all(fp); break; + case NSLCD_ACTION_NETGROUP_BYNAME: nslcd_netgroup_byname(fp); break; + case NSLCD_ACTION_NETWORK_BYNAME: nslcd_network_byname(fp); break; + case NSLCD_ACTION_NETWORK_BYADDR: nslcd_network_byaddr(fp); break; + case NSLCD_ACTION_NETWORK_ALL: nslcd_network_all(fp); break; + case NSLCD_ACTION_PASSWD_BYNAME: nslcd_passwd_byname(fp); break; + case NSLCD_ACTION_PASSWD_BYUID: nslcd_passwd_byuid(fp); break; + case NSLCD_ACTION_PASSWD_ALL: nslcd_passwd_all(fp); break; + case NSLCD_ACTION_PROTOCOL_BYNAME: nslcd_protocol_byname(fp); break; + case NSLCD_ACTION_PROTOCOL_BYNUMBER:nslcd_protocol_bynumber(fp); break; + case NSLCD_ACTION_PROTOCOL_ALL: nslcd_protocol_all(fp); break; + case NSLCD_ACTION_RPC_BYNAME: nslcd_rpc_byname(fp); break; + case NSLCD_ACTION_RPC_BYNUMBER: nslcd_rpc_bynumber(fp); break; + case NSLCD_ACTION_RPC_ALL: nslcd_rpc_all(fp); break; + case NSLCD_ACTION_SERVICE_BYNAME: nslcd_service_byname(fp); break; + case NSLCD_ACTION_SERVICE_BYNUMBER: nslcd_service_bynumber(fp); break; + case NSLCD_ACTION_SERVICE_ALL: nslcd_service_all(fp); break; + case NSLCD_ACTION_SHADOW_BYNAME: nslcd_shadow_byname(fp); break; + case NSLCD_ACTION_SHADOW_ALL: nslcd_shadow_all(fp); break; + default: + log_log(LOG_WARNING,"invalid request id: %d",(int)action); + break; + } + /* we're done with the request */ + fclose(fp); + return; +} /* accept a connection on the socket */ static void acceptconnection(void) @@ -323,12 +468,24 @@ static void install_sighandler(int signum,RETSIGTYPE (*handler) (int)) } } +static void *worker(void *arg) +{ + /* start waiting for incoming connections */ + while (nslcd_exitsignal==0) + { + /* wait for a new connection */ + acceptconnection(); + } + return NULL; +} + /* the main program... */ int main(int argc,char *argv[]) { gid_t mygid=-1; uid_t myuid=-1; + int i; /* parse the command line */ parse_cmdline(argc,argv); @@ -336,14 +493,14 @@ int main(int argc,char *argv[]) /* clear the environment */ /* TODO:implement */ + /* check if we are already running */ + /* FIXME: implement (maybe pass along options or commands) */ + /* disable ldap lookups of host names to avoid lookup loop and fall back to files dns (a sensible default) */ if (__nss_configure_lookup("hosts","files dns")) log_log(LOG_ERR,"unable to override hosts lookup method: %s",strerror(errno)); - /* check if we are already running */ - /* FIXME: implement */ - /* daemonize */ if ((!nslcd_debugging)&&(daemon(0,0)<0)) { @@ -366,7 +523,7 @@ int main(int argc,char *argv[]) write_pidfile(NSLCD_PIDFILE); /* create socket */ - nslcd_serversocket=nslcd_server_open(); + nslcd_serversocket=open_socket(); #ifdef HAVE_SETGROUPS /* drop all supplemental groups */ @@ -435,7 +592,7 @@ int main(int argc,char *argv[]) cap_free(caps); #endif /* USE_CAPABILITIES */ - /* install signalhandlers for some other signals */ + /* install signalhandlers for some signals */ install_sighandler(SIGHUP, sigexit_handler); install_sighandler(SIGINT, sigexit_handler); install_sighandler(SIGQUIT,sigexit_handler); @@ -444,16 +601,28 @@ int main(int argc,char *argv[]) install_sighandler(SIGTERM,sigexit_handler); install_sighandler(SIGUSR1,sigexit_handler); install_sighandler(SIGUSR2,sigexit_handler); - /* TODO: install signal handlers for reloading configuration */ log_log(LOG_INFO,"accepting connections"); - - /* start waiting for incoming connections */ - while (nslcd_exitsignal==0) + + /* start worker threads */ + for (i=0;i<NUM_THREADS;i++) { - /* wait for a new connection */ - acceptconnection(); + if (pthread_create(&nslcd_threads[i],NULL,worker,NULL)) + { + log_log(LOG_ERR,"unable to start worker thread %d: %s",i,strerror(errno)); + exit(1); + } + } + + /* wait for all threads to die */ + for (i=0;i<NUM_THREADS;i++) + { + if (pthread_join(nslcd_threads[i],NULL)) + { + log_log(LOG_ERR,"unable to wait for worker thread %d: %s",i,strerror(errno)); + exit(1); + } } /* print something about received signals */ diff --git a/server/passwd.c b/server/passwd.c index 1ee6742..e9b5c52 100644 --- a/server/passwd.c +++ b/server/passwd.c @@ -46,7 +46,6 @@ #include "ldap-nss.h" #include "util.h" -#include "nslcd-server.h" #include "common.h" #include "log.h" diff --git a/server/protocol.c b/server/protocol.c index 9089156..f486c8f 100644 --- a/server/protocol.c +++ b/server/protocol.c @@ -50,7 +50,6 @@ #include "ldap-nss.h" #include "util.h" -#include "nslcd-server.h" #include "common.h" #include "log.h" diff --git a/server/rpc.c b/server/rpc.c index 2da1a6b..ff292a7 100644 --- a/server/rpc.c +++ b/server/rpc.c @@ -54,7 +54,6 @@ #include "ldap-nss.h" #include "util.h" -#include "nslcd-server.h" #include "common.h" #include "log.h" diff --git a/server/service.c b/server/service.c index 118f5c5..291bae3 100644 --- a/server/service.c +++ b/server/service.c @@ -54,7 +54,6 @@ #include "ldap-nss.h" #include "util.h" -#include "nslcd-server.h" #include "common.h" #include "log.h" diff --git a/server/shadow.c b/server/shadow.c index bb2141b..673695a 100644 --- a/server/shadow.c +++ b/server/shadow.c @@ -46,7 +46,6 @@ #include "ldap-nss.h" #include "util.h" -#include "nslcd-server.h" #include "common.h" #include "log.h" diff --git a/server/util.c b/server/util.c index 438d32a..0530ef3 100644 --- a/server/util.c +++ b/server/util.c @@ -52,7 +52,6 @@ #include "ldap-nss.h" #include "util.h" -#include "nslcd-server.h" #include "common.h" #include "log.h" |