summaryrefslogtreecommitdiff
path: root/nslcd/common.c
diff options
context:
space:
mode:
authorArthur de Jong <arthur@arthurdejong.org>2012-12-22 21:38:26 +0000
committerArthur de Jong <arthur@arthurdejong.org>2012-12-22 21:38:26 +0000
commitc7bb19c55c7a902e25bdd33b0d49a2ddcf62e07a (patch)
treeb3a75ef2719bc2f334041460fd21fbdf86a23a82 /nslcd/common.c
parentd336cd6b429f8ce40c58ea287f79bbc7c23d0966 (diff)
update C coding style to a more commonly used style
git-svn-id: http://arthurdejong.org/svn/nss-pam-ldapd/nss-pam-ldapd@1873 ef36b2f9-881f-0410-afb5-c4e39611909c
Diffstat (limited to 'nslcd/common.c')
-rw-r--r--nslcd/common.c187
1 files changed, 96 insertions, 91 deletions
diff --git a/nslcd/common.c b/nslcd/common.c
index 37542b9..2b67e6c 100644
--- a/nslcd/common.c
+++ b/nslcd/common.c
@@ -42,177 +42,178 @@
#include "attmap.h"
#include "cfg.h"
-/* simple wrapper around snptintf() to return non-0 in case
+/* simple wrapper around snptintf() to return non-zero in case
of any failure (but always keep string 0-terminated) */
-int mysnprintf(char *buffer,size_t buflen,const char *format, ...)
+int mysnprintf(char *buffer, size_t buflen, const char *format, ...)
{
int res;
va_list ap;
/* do snprintf */
- va_start(ap,format);
- res=vsnprintf(buffer,buflen,format,ap);
+ va_start(ap, format);
+ res = vsnprintf(buffer, buflen, format, ap);
va_end(ap);
/* NULL-terminate the string just to be on the safe side */
- buffer[buflen-1]='\0';
+ buffer[buflen - 1] = '\0';
/* check if the string was completely written */
- return ((res<0)||(((size_t)res)>=buflen));
+ return ((res < 0) || (((size_t)res) >= buflen));
}
/* return the fully qualified domain name of the current host */
const char *getfqdn(void)
{
- static char *fqdn=NULL;
- char hostname[HOST_NAME_MAX+1];
+ static char *fqdn = NULL;
+ char hostname[HOST_NAME_MAX + 1];
int hostnamelen;
int i;
- struct hostent *host=NULL;
+ struct hostent *host = NULL;
/* if we already have a fqdn return that */
- if (fqdn!=NULL)
+ if (fqdn != NULL)
return fqdn;
/* get system hostname */
- if (gethostname(hostname,sizeof(hostname))<0)
+ if (gethostname(hostname, sizeof(hostname)) < 0)
{
- log_log(LOG_ERR,"gethostname() failed: %s",strerror(errno));
+ log_log(LOG_ERR, "gethostname() failed: %s", strerror(errno));
return NULL;
}
- hostnamelen=strlen(hostname);
+ hostnamelen = strlen(hostname);
/* lookup hostent */
- host=gethostbyname(hostname);
- if (host==NULL)
+ host = gethostbyname(hostname);
+ if (host == NULL)
{
- log_log(LOG_ERR,"gethostbyname(%s): %s",hostname,hstrerror(h_errno));
+ log_log(LOG_ERR, "gethostbyname(%s): %s", hostname, hstrerror(h_errno));
/* fall back to hostname */
- fqdn=strdup(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'))
+ if ((strncasecmp(hostname, host->h_name, hostnamelen) == 0) &&
+ (host->h_name[hostnamelen] == '.') &&
+ (host->h_name[hostnamelen + 1] != '\0'))
{
- fqdn=strdup(host->h_name);
+ fqdn = strdup(host->h_name);
return fqdn;
}
/* also check h_aliases */
- for (i=0;host->h_aliases[i]!=NULL;i++)
+ 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'))
+ if ((strncasecmp(hostname, host->h_aliases[i], hostnamelen) == 0) &&
+ (host->h_aliases[i][hostnamelen] == '.') &&
+ (host->h_aliases[i][hostnamelen + 1] != '\0'))
{
- fqdn=strdup(host->h_aliases[i]);
+ fqdn = strdup(host->h_aliases[i]);
return fqdn;
}
}
/* fall back to h_name if it has a dot in it */
- if (strchr(host->h_name,'.')!=NULL)
+ if (strchr(host->h_name, '.') != NULL)
{
- fqdn=strdup(host->h_name);
+ fqdn = strdup(host->h_name);
return fqdn;
}
/* also check h_aliases */
- for (i=0;host->h_aliases[i]!=NULL;i++)
+ for (i = 0; host->h_aliases[i] != NULL; i++)
{
- if (strchr(host->h_aliases[i],'.')!=NULL)
+ if (strchr(host->h_aliases[i], '.') != NULL)
{
- fqdn=strdup(host->h_aliases[i]);
+ fqdn = strdup(host->h_aliases[i]);
return fqdn;
}
}
/* nothing found, fall back to hostname */
- fqdn=strdup(hostname);
+ fqdn = strdup(hostname);
return fqdn;
}
-const char *get_userpassword(MYLDAP_ENTRY *entry,const char *attr,char *buffer,size_t buflen)
+const char *get_userpassword(MYLDAP_ENTRY *entry, const char *attr,
+ char *buffer, size_t buflen)
{
const char *tmpvalue;
/* get the value */
- tmpvalue=attmap_get_value(entry,attr,buffer,buflen);
- if (tmpvalue==NULL)
+ tmpvalue = attmap_get_value(entry, attr, buffer, buflen);
+ if (tmpvalue == NULL)
return NULL;
/* go over the entries and return the remainder of the value if it
starts with {crypt} or crypt$ */
- if (strncasecmp(tmpvalue,"{crypt}",7)==0)
- return tmpvalue+7;
- if (strncasecmp(tmpvalue,"crypt$",6)==0)
- return tmpvalue+6;
+ if (strncasecmp(tmpvalue, "{crypt}", 7) == 0)
+ return tmpvalue + 7;
+ if (strncasecmp(tmpvalue, "crypt$", 6) == 0)
+ return tmpvalue + 6;
/* just return the first value completely */
return tmpvalue;
/* TODO: support more password formats e.g. SMD5
- (which is $1$ but in a different format)
- (any code for this is more than welcome) */
+ (which is $1$ but in a different format)
+ (any code for this is more than welcome) */
}
/* Checks if the specified name seems to be a valid user or group name. */
int isvalidname(const char *name)
{
- return regexec(&nslcd_cfg->validnames,name,0,NULL,0)==0;
+ return regexec(&nslcd_cfg->validnames, name, 0, NULL, 0) == 0;
}
/* this writes a single address to the stream */
-int write_address(TFILE *fp,MYLDAP_ENTRY *entry,const char *attr,
+int write_address(TFILE *fp, MYLDAP_ENTRY *entry, const char *attr,
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)
+ if (inet_pton(AF_INET, addr, &ipv4addr) > 0)
{
/* write address type */
- WRITE_INT32(fp,AF_INET);
+ WRITE_INT32(fp, AF_INET);
/* write the address length */
- WRITE_INT32(fp,sizeof(struct in_addr));
+ WRITE_INT32(fp, sizeof(struct in_addr));
/* write the address itself (in network byte order) */
- WRITE(fp,&ipv4addr,sizeof(struct in_addr));
+ WRITE(fp, &ipv4addr, sizeof(struct in_addr));
}
- else if (inet_pton(AF_INET6,addr,&ipv6addr)>0)
+ else if (inet_pton(AF_INET6, addr, &ipv6addr) > 0)
{
/* write address type */
- WRITE_INT32(fp,AF_INET6);
+ WRITE_INT32(fp, AF_INET6);
/* write the address length */
- WRITE_INT32(fp,sizeof(struct in6_addr));
+ WRITE_INT32(fp, sizeof(struct in6_addr));
/* write the address itself (in network byte order) */
- WRITE(fp,&ipv6addr,sizeof(struct in6_addr));
+ WRITE(fp, &ipv6addr, sizeof(struct in6_addr));
}
else
{
/* failure, log but write simple invalid address
(otherwise the address list is messed up) */
/* TODO: have error message in correct format */
- log_log(LOG_WARNING,"%s: %s: \"%s\" unparseble",
- myldap_get_dn(entry),attmap_ether_cn,addr);
+ log_log(LOG_WARNING, "%s: %s: \"%s\" unparseble",
+ myldap_get_dn(entry), attmap_ether_cn, addr);
/* write an illegal address type */
- WRITE_INT32(fp,-1);
+ WRITE_INT32(fp, -1);
/* write an emtpy address */
- WRITE_INT32(fp,0);
+ WRITE_INT32(fp, 0);
}
/* we're done */
return 0;
}
-int read_address(TFILE *fp,char *addr,int *addrlen,int *af)
+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))
+ READ_INT32(fp, *af);
+ if ((*af != AF_INET) && (*af != AF_INET6))
{
- log_log(LOG_WARNING,"incorrect address family specified: %d",*af);
+ log_log(LOG_WARNING, "incorrect address family specified: %d", *af);
return -1;
}
/* read address length */
- READ_INT32(fp,len);
- if ((len>*addrlen)||(len<=0))
+ READ_INT32(fp, len);
+ if ((len > *addrlen) || (len <= 0))
{
- log_log(LOG_WARNING,"address length incorrect: %d",len);
+ log_log(LOG_WARNING, "address length incorrect: %d", len);
return -1;
}
- *addrlen=len;
+ *addrlen = len;
/* read address */
- READ(fp,addr,len);
+ READ(fp, addr, len);
/* we're done */
return 0;
}
@@ -222,43 +223,45 @@ int read_address(TFILE *fp,char *addr,int *addrlen,int *af)
to a format that can be used to search the objectSid property with */
char *sid2search(const char *sid)
{
- const char *tmpsid=sid;
- char *res,*tmp;
- int i=0;
+ const char *tmpsid = sid;
+ char *res, *tmp;
+ int i = 0;
long int l;
/* check the beginning of the string */
- if (strncasecmp(sid,"S-",2)!=0)
+ if (strncasecmp(sid, "S-", 2) != 0)
{
- log_log(LOG_ERR,"error in SID %s",sid);
+ log_log(LOG_ERR, "error in SID %s", sid);
exit(EXIT_FAILURE);
}
/* count the number of dashes in the sid */
- while (tmpsid!=NULL)
+ while (tmpsid != NULL)
{
i++;
- tmpsid=strchr(tmpsid+1,'-');
+ tmpsid = strchr(tmpsid + 1, '-');
}
- i-=2; /* number of security ids plus one because we add the uid later */
+ i -= 2; /* number of security ids plus one because we add the uid later */
/* allocate memory */
- res=malloc(3+3+6*3+i*4*3+1);
- if (res==NULL)
+ res = malloc(3 + 3 + 6 * 3 + i * 4 * 3 + 1);
+ if (res == NULL)
{
- log_log(LOG_CRIT,"malloc() failed to allocate memory");
+ log_log(LOG_CRIT, "malloc() failed to allocate memory");
exit(1);
}
/* build the first part */
- l=strtol(sid+2,&tmp,10);
- sprintf(res,"\\%02x\\%02x",(int)l&0xff,(int)i);
+ l = strtol(sid + 2, &tmp, 10);
+ sprintf(res, "\\%02x\\%02x", (int)l & 0xff, (int)i);
/* build authority part (we only handle 32 of the 48 bits) */
- l=strtol(tmp+1,&tmp,10);
- sprintf(res+strlen(res),"\\00\\00\\%02x\\%02x\\%02x\\%02x",
- (int)((l>>24)&0xff),(int)((l>>16)&0xff),(int)((l>>8)&0xff),(int)(l&0xff));
+ l = strtol(tmp + 1, &tmp, 10);
+ sprintf(res + strlen(res), "\\00\\00\\%02x\\%02x\\%02x\\%02x",
+ (int)((l >> 24) & 0xff), (int)((l >> 16) & 0xff),
+ (int)((l >> 8) & 0xff), (int)(l & 0xff));
/* go over the rest of the bits */
- while (*tmp!='\0')
+ while (*tmp != '\0')
{
- l=strtol(tmp+1,&tmp,10);
- sprintf(res+strlen(res),"\\%02x\\%02x\\%02x\\%02x",
- (int)(l&0xff),(int)((l>>8)&0xff),(int)((l>>16)&0xff),(int)((l>>24)&0xff));
+ l = strtol(tmp + 1, &tmp, 10);
+ sprintf(res + strlen(res), "\\%02x\\%02x\\%02x\\%02x",
+ (int)(l & 0xff), (int)((l >> 8) & 0xff), (int)((l >> 16) & 0xff),
+ (int)((l >> 24) & 0xff));
}
return res;
}
@@ -268,21 +271,23 @@ long int binsid2id(const char *binsid)
{
int i;
/* find the position of the last security id */
- i=2+6+((((int)binsid[1])&0xff)-1)*4;
- return (((long int)binsid[i])&0xff)|((((long int)binsid[i+1])&0xff)<<8)|
- ((((long int)binsid[i+2])&0xff)<<16)|((((long int)binsid[i+3])&0xff)<<24);
+ i = 2 + 6 + ((((int)binsid[1]) & 0xff) - 1) * 4;
+ return (((long int)binsid[i]) & 0xff) |
+ ((((long int)binsid[i + 1]) & 0xff) << 8) |
+ ((((long int)binsid[i + 2]) & 0xff) << 16) |
+ ((((long int)binsid[i + 3]) & 0xff) << 24);
}
#ifdef WANT_STRTOUI
/* provide a strtoui() implementation, similar to strtoul() but returning
an range-checked unsigned int instead */
-unsigned int strtoui(const char *nptr,char **endptr,int base)
+unsigned int strtoui(const char *nptr, char **endptr, int base)
{
unsigned long val;
- val=strtoul(nptr,endptr,base);
- if (val>UINT_MAX)
+ val = strtoul(nptr, endptr, base);
+ if (val > UINT_MAX)
{
- errno=ERANGE;
+ errno = ERANGE;
return UINT_MAX;
}
/* If errno was set by strtoul, we'll pass it back as-is */