summaryrefslogtreecommitdiff
path: root/nslcd/common.c
diff options
context:
space:
mode:
authorArthur de Jong <arthur@arthurdejong.org>2011-01-29 20:15:56 +0000
committerArthur de Jong <arthur@arthurdejong.org>2011-01-29 20:15:56 +0000
commit16444349cb9bef2d189607ddfc560f636a77acbb (patch)
treeb22ffe30bb9e3798b221584e74ee3719e3bf5ba5 /nslcd/common.c
parentd492b570f93108c65088462fea7a6a87d091f953 (diff)
implement a fqdn variable that can be used inside pam_authz_search filters
git-svn-id: http://arthurdejong.org/svn/nss-pam-ldapd/nss-pam-ldapd@1367 ef36b2f9-881f-0410-afb5-c4e39611909c
Diffstat (limited to 'nslcd/common.c')
-rw-r--r--nslcd/common.c72
1 files changed, 72 insertions, 0 deletions
diff --git a/nslcd/common.c b/nslcd/common.c
index dc25bed..3c865fd 100644
--- a/nslcd/common.c
+++ b/nslcd/common.c
@@ -31,6 +31,8 @@
#include <arpa/inet.h>
#include <strings.h>
#include <limits.h>
+#include <netdb.h>
+#include <string.h>
#include "nslcd.h"
#include "common.h"
@@ -52,6 +54,76 @@ int mysnprintf(char *buffer,size_t buflen,const char *format, ...)
return ((res<0)||(((size_t)res)>=buflen));
}
+#ifndef HOST_NAME_MAX
+#define HOST_NAME_MAX 255
+#endif /* not HOST_NAME_MAX */
+
+/* return the fully qualified domain name of the current host */
+const char *getfqdn(void)
+{
+ static char *fqdn=NULL;
+ char hostname[HOST_NAME_MAX+1];
+ int hostnamelen;
+ int i;
+ struct hostent *host=NULL;
+ /* if we already have a fqdn return that */
+ if (fqdn!=NULL)
+ return fqdn;
+ /* get system hostname */
+ if (gethostname(hostname,sizeof(hostname))<0)
+ {
+ log_log(LOG_ERR,"gethostname() failed: %s",strerror(errno));
+ return NULL;
+ }
+ hostnamelen=strlen(hostname);
+ /* lookup hostent */
+ host=gethostbyname(hostname);
+ if (host==NULL)
+ {
+ log_log(LOG_ERR,"gethostbyname(%s): %s",hostname,hstrerror(h_errno));
+ /* fall back to hostname */
+ fqdn=strdup(hostname);
+ return fqdn;
+ }
+ /* check h_name for fqdn starting with our hostname */
+ if ((strncasecmp(hostname,host->h_name,hostnamelen)==0)&&
+ (host->h_name[hostnamelen]=='.')&&
+ (host->h_name[hostnamelen+1]!='\0'))
+ {
+ fqdn=strdup(host->h_name);
+ return fqdn;
+ }
+ /* also check h_aliases */
+ for (i=0;host->h_aliases[i]!=NULL;i++)
+ {
+ if ((strncasecmp(hostname,host->h_aliases[i],hostnamelen)==0)&&
+ (host->h_aliases[i][hostnamelen]=='.')&&
+ (host->h_aliases[i][hostnamelen+1]!='\0'))
+ {
+ fqdn=host->h_aliases[i];
+ return fqdn;
+ }
+ }
+ /* fall back to h_name if it has a dot in it */
+ if (strchr(host->h_name,'.')!=NULL)
+ {
+ fqdn=strdup(host->h_name);
+ return fqdn;
+ }
+ /* also check h_aliases */
+ for (i=0;host->h_aliases[i]!=NULL;i++)
+ {
+ if (strchr(host->h_aliases[i],'.')!=NULL)
+ {
+ fqdn=strdup(host->h_aliases[i]);
+ return fqdn;
+ }
+ }
+ /* nothing found, fall back to hostname */
+ fqdn=strdup(hostname);
+ return fqdn;
+}
+
const char *get_userpassword(MYLDAP_ENTRY *entry,const char *attr,char *buffer,size_t buflen)
{
const char *tmpvalue;