diff options
author | Arthur de Jong <arthur@arthurdejong.org> | 2007-12-09 15:49:56 +0100 |
---|---|---|
committer | Arthur de Jong <arthur@arthurdejong.org> | 2007-12-09 15:49:56 +0100 |
commit | 534c504364428682deaa2704c3f9ae4cf7f6ab39 (patch) | |
tree | ae85847944cd9ef2fc6ae9b5072661696e82bedf /nslcd/common.c | |
parent | 432cb4f71939e9675f9e29a610124a2cd687352b (diff) |
switch to new LDAP entry parsing code that is much simpler and more readable
git-svn-id: http://arthurdejong.org/svn/nss-pam-ldapd/nss-ldapd@488 ef36b2f9-881f-0410-afb5-c4e39611909c
Diffstat (limited to 'nslcd/common.c')
-rw-r--r-- | nslcd/common.c | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/nslcd/common.c b/nslcd/common.c index bf0d1ea..5472c93 100644 --- a/nslcd/common.c +++ b/nslcd/common.c @@ -25,9 +25,14 @@ #include <stdio.h> #include <stdarg.h> +#include <sys/types.h> +#include <sys/socket.h> +#include <arpa/inet.h> +#include <strings.h> #include "nslcd.h" #include "common.h" +#include "log.h" /* simple wrapper around snptintf() to return non-0 in case of any failure (but always keep string 0-terminated) */ @@ -43,3 +48,95 @@ int mysnprintf(char *buffer,size_t buflen,const char *format, ...) /* check if the string was completely written */ return ((res<0)||(((size_t)res)>=buflen)); } + +/* This tries to get the user password attribute from the entry. + It will try to return an encrypted password as it is used in /etc/passwd, + /etc/group or /etc/shadow depending upon what is in the directory. + This function will return NULL if no passwd and will return the literal + value in the directory if conversion is not possible. */ +const char *get_userpassword(MYLDAP_ENTRY *entry,const char *attr) +{ + const char **values; + int i; + /* get the entries */ + values=myldap_get_values(entry,attr); + if ((values==NULL)||(values[0]==NULL)) + return NULL; + /* go over the entries and return the remainder of the value if it + starts with {crypt} or crypt$ */ + for (i=0;values[i]!=NULL;i++) + { + if (strncasecmp(values[i],"{crypt}",7)==0) + return values[i]+7; + if (strncasecmp(values[i],"crypt$",6)==0) + return values[i]+7; + } + /* just return the first value completely */ + return values[0]; + /* TODO: support more password formats e.g. SMD5 + (which is $1$ but in a different format) + (any code for this is more than welcome) */ +} + +/* this writes a single address to the stream */ +int write_address(TFILE *fp,const char *addr) +{ + int32_t tmpint32; + struct in_addr ipv4addr; + struct in6_addr ipv6addr; + /* try to parse the address as IPv4 first, fall back to IPv6 */ + if (inet_pton(AF_INET,addr,&ipv4addr)>0) + { + /* write address type */ + WRITE_INT32(fp,AF_INET); + /* write the address length */ + WRITE_INT32(fp,sizeof(struct in_addr)); + /* write the address itself (in network byte order) */ + WRITE_TYPE(fp,ipv4addr,struct in_addr); + } + else if (inet_pton(AF_INET6,addr,&ipv6addr)>0) + { + /* write address type */ + WRITE_INT32(fp,AF_INET6); + /* write the address length */ + WRITE_INT32(fp,sizeof(struct in6_addr)); + /* write the address itself (in network byte order) */ + WRITE_TYPE(fp,ipv6addr,struct in6_addr); + } + else + { + /* failure, log but write simple invalid address */ + log_log(LOG_WARNING,"unparseble address: %s",addr); + /* write an illegal address type */ + WRITE_INT32(fp,-1); + /* write an emtpy address */ + WRITE_INT32(fp,0); + } + /* we're done */ + return 0; +} + +int read_address(TFILE *fp,char *addr,int *addrlen,int *af) +{ + int32_t tmpint32; + int len; + /* read address family */ + READ_INT32(fp,*af); + if ((*af!=AF_INET)&&(*af!=AF_INET6)) + { + log_log(LOG_WARNING,"incorrect address family specified: %d",*af); + return -1; + } + /* read address length */ + READ_INT32(fp,len); + if ((len>*addrlen)||(len<=0)) + { + log_log(LOG_WARNING,"address length incorrect: %d",len); + return -1; + } + *addrlen=len; + /* read address */ + READ(fp,addr,len); + /* we're done */ + return 0; +} |