From e5a71411f3cab38fd8222c6a51d4791c330d5de7 Mon Sep 17 00:00:00 2001 From: Arthur de Jong Date: Wed, 27 Dec 2006 09:48:43 +0000 Subject: do not allocate new memory with malloc() for each request with a string parameter but use a buffer allocated on the stack instead (this simplifies free()-ing the buffer(s) in case of problems) git-svn-id: http://arthurdejong.org/svn/nss-pam-ldapd/nss-ldapd@204 ef36b2f9-881f-0410-afb5-c4e39611909c --- nslcd-common.h | 31 ++++++++++++++++--------------- nslcd/alias.c | 6 ++---- nslcd/common.h | 4 ++-- nslcd/ether.c | 6 ++---- nslcd/group.c | 15 ++++----------- nslcd/host.c | 6 ++---- nslcd/netgroup.c | 4 ++-- nslcd/network.c | 7 ++----- nslcd/passwd.c | 7 ++----- nslcd/protocol.c | 6 ++---- nslcd/rpc.c | 6 ++---- nslcd/service.c | 15 +++++---------- nslcd/shadow.c | 6 ++---- 13 files changed, 45 insertions(+), 74 deletions(-) diff --git a/nslcd-common.h b/nslcd-common.h index 25efd83..ee2e1d8 100644 --- a/nslcd-common.h +++ b/nslcd-common.h @@ -150,9 +150,10 @@ static void debug_dump(const void *ptr,size_t size) #define BUF_CHECK(fp,sz) \ if ((bufptr+(size_t)(sz))>buflen) \ { \ - DEBUG_PRINT("READ : buffer error: %d bytes missing",((sz)-(buflen))); \ + /* will not fit */ \ + DEBUG_PRINT("READ : buffer error: %d bytes too large",((sz)-(buflen))); \ ERROR_OUT_BUFERROR(fp); \ - } /* will not fit */ + } /* move the buffer pointer */ #define BUF_SKIP(sz) \ @@ -176,24 +177,24 @@ static void debug_dump(const void *ptr,size_t size) (field)=BUF_CUR; \ BUF_SKIP(tmpint32+1); -/* read a string from the stream dynamically allocating memory - for the string (don't forget to call free() later on) */ -#define READ_STRING_ALLOC(fp,field) \ +/* read a string in a fixed-size "normal" buffer */ +#define READ_STRING_BUF2(fp,buffer,buflen) \ /* read the size of the string */ \ READ_TYPE(fp,tmpint32,int32_t); \ - /* allocate memory */ \ - (field)=(char *)malloc((size_t)(tmpint32+1)); \ - if ((field)==NULL) \ + DEBUG_PRINT("READ_STRING: var="__STRING(buffer)" strlen=%d",tmpint32); \ + /* check if read would fit */ \ + if (((size_t)tmpint32)>=(buflen)) \ { \ - DEBUG_PRINT("READ_STRING: var="__STRING(field)" malloc() error: %s",strerror(errno)); \ - ERROR_OUT_ALLOCERROR(fp); \ - } /* problem allocating */ \ + /* will not fit */ \ + DEBUG_PRINT("READ : buffer error: %d bytes too large",(tmpint32-(buflen))+1); \ + ERROR_OUT_BUFERROR(fp); \ + } \ /* read string from the stream */ \ if (tmpint32>0) \ - { READ(fp,(field),(size_t)tmpint32); } \ - /* null-terminate string */ \ - (field)[tmpint32]='\0'; \ - DEBUG_PRINT("READ_STRING: var="__STRING(field)" string=\"%s\"",(field)); + { READ(fp,buffer,(size_t)tmpint32); } \ + /* null-terminate string in buffer */ \ + buffer[tmpint32]='\0'; \ + DEBUG_PRINT("READ_STRING: var="__STRING(buffer)" string=\"%s\"",buffer); /* read an array from a stram and store the length of the array in num (size for the array is allocated) */ diff --git a/nslcd/alias.c b/nslcd/alias.c index 4698f1b..2bc0936 100644 --- a/nslcd/alias.c +++ b/nslcd/alias.c @@ -80,10 +80,10 @@ static int write_alias(LDAPMessage *e,struct ldap_state *pvt,FILE *fp) int nslcd_alias_byname(FILE *fp) { int32_t tmpint32; - char *name; + char name[256]; struct ldap_args a; /* read request parameters */ - READ_STRING_ALLOC(fp,name); + READ_STRING_BUF2(fp,name,sizeof(name)); /* log call */ log_log(LOG_DEBUG,"nslcd_alias_byname(%s)",name); /* write the response header */ @@ -94,8 +94,6 @@ int nslcd_alias_byname(FILE *fp) LA_STRING(a)=name; LA_TYPE(a)=LA_TYPE_STRING; _nss_ldap_searchbyname(&a,_nss_ldap_filt_getaliasbyname,LM_ALIASES,fp,write_alias); - /* no more need for this */ - free(name); WRITE_FLUSH(fp); /* we're done */ return 0; diff --git a/nslcd/common.h b/nslcd/common.h index 42f47ea..374cf55 100644 --- a/nslcd/common.h +++ b/nslcd/common.h @@ -48,8 +48,8 @@ int nss2nslcd(enum nss_status code); log_log(LOG_WARNING,"error reading from client"); \ return -1; -#define ERROR_OUT_ALLOCERROR(fp) \ - log_log(LOG_ERR,"error allocating memory"); \ +#define ERROR_OUT_BUFERROR(fp) \ + log_log(LOG_WARNING,"client supplied argument too large"); \ return -1; diff --git a/nslcd/ether.c b/nslcd/ether.c index ab7b870..c818a6d 100644 --- a/nslcd/ether.c +++ b/nslcd/ether.c @@ -128,7 +128,7 @@ _nss_ldap_parse_ether (LDAPMessage * e, int nslcd_ether_byname(FILE *fp) { int32_t tmpint32; - char *name; + char name[256]; struct ldap_args a; /* these are here for now until we rewrite the LDAP code */ struct ether result; @@ -136,7 +136,7 @@ int nslcd_ether_byname(FILE *fp) int errnop; int retv; /* read request parameters */ - READ_STRING_ALLOC(fp,name); + READ_STRING_BUF2(fp,name,sizeof(name)); /* log call */ log_log(LOG_DEBUG,"nslcd_ether_byname(%s)",name); /* write the response header */ @@ -147,8 +147,6 @@ int nslcd_ether_byname(FILE *fp) LA_STRING(a)=name; LA_TYPE(a)=LA_TYPE_STRING; retv=nss2nslcd(_nss_ldap_getbyname(&a,&result,buffer,1024,&errnop,_nss_ldap_filt_gethostton,LM_ETHERS,_nss_ldap_parse_ether)); - /* no more need for this string */ - free(name); /* write the response */ WRITE_INT32(fp,retv); if (retv==NSLCD_RESULT_SUCCESS) diff --git a/nslcd/group.c b/nslcd/group.c index faf7007..1643d9e 100644 --- a/nslcd/group.c +++ b/nslcd/group.c @@ -1059,7 +1059,7 @@ static enum nss_status group_bymember(const char *user, long int *start, int nslcd_group_byname(FILE *fp) { int32_t tmpint32,tmp2int32,tmp3int32; - char *name; + char name[256]; struct ldap_args a; /* these are here for now until we rewrite the LDAP code */ struct group result; @@ -1067,8 +1067,7 @@ int nslcd_group_byname(FILE *fp) int errnop; int retv; /* read request parameters */ - READ_STRING_ALLOC(fp,name); - /* FIXME: free() this buffer somewhere */ + READ_STRING_BUF2(fp,name,sizeof(name)); /* log call */ log_log(LOG_DEBUG,"nslcd_group_byname(%s)",name); /* static buffer size check */ @@ -1082,8 +1081,6 @@ int nslcd_group_byname(FILE *fp) LA_STRING(a)=name; LA_TYPE(a)=LA_TYPE_STRING; retv=nss2nslcd(_nss_ldap_getbyname(&a,&result,buffer,1024,&errnop,_nss_ldap_filt_getgrnam,LM_GROUP,_nss_ldap_parse_gr)); - /* no more need for this */ - free(name); /* write the response */ WRITE_INT32(fp,NSLCD_VERSION); WRITE_INT32(fp,NSLCD_ACTION_GROUP_BYNAME); @@ -1109,7 +1106,6 @@ int nslcd_group_bygid(FILE *fp) int retv; /* read request parameters */ READ_TYPE(fp,gid,gid_t); - /* FIXME: free() this buffer somewhere */ /* log call */ log_log(LOG_DEBUG,"nslcd_group_bygid(%d)",(int)gid); /* static buffer size check */ @@ -1139,7 +1135,7 @@ int nslcd_group_bygid(FILE *fp) int nslcd_group_bymember(FILE *fp) { int32_t tmpint32; - char *name; + char name[256]; /* these are here for now until we rewrite the LDAP code */ int errnop; int retv; @@ -1147,8 +1143,7 @@ int nslcd_group_bymember(FILE *fp) long int i; gid_t groupsp[1024]; /* read request parameters */ - READ_STRING_ALLOC(fp,name); - /* FIXME: free() this buffer somewhere */ + READ_STRING_BUF2(fp,name,sizeof(name)); /* log call */ log_log(LOG_DEBUG,"nslcd_group_bymember(%s)",name); /* do the LDAP request */ @@ -1189,8 +1184,6 @@ int nslcd_group_bymember(FILE *fp) WRITE_INT32(fp,retv); } WRITE_FLUSH(fp); - /* no more need for this */ - free(name); /* we're done */ return 0; } diff --git a/nslcd/host.c b/nslcd/host.c index ec04bf2..60dda78 100644 --- a/nslcd/host.c +++ b/nslcd/host.c @@ -247,14 +247,14 @@ _nss_ldap_parse_hostv6 (LDAPMessage * e, int nslcd_host_byname(FILE *fp) { int32_t tmpint32; - char *name; + char name[256]; struct ldap_args a; int retv; struct hostent result; char buffer[1024]; int errnop; /* read request parameters */ - READ_STRING_ALLOC(fp,name); + READ_STRING_BUF2(fp,name,sizeof(name)); /* log call */ log_log(LOG_DEBUG,"nslcd_host_byname(%s)",name); /* write the response header */ @@ -270,8 +270,6 @@ int nslcd_host_byname(FILE *fp) #else _nss_ldap_parse_hostv4)); #endif - /* no more need for this string */ - free(name); /* write the response */ WRITE_INT32(fp,retv); if (retv==NSLCD_RESULT_SUCCESS) diff --git a/nslcd/netgroup.c b/nslcd/netgroup.c index e79657d..3641783 100644 --- a/nslcd/netgroup.c +++ b/nslcd/netgroup.c @@ -291,7 +291,7 @@ int nslcd_netgroup_byname(FILE *fp) int32_t tmpint32; static struct ent_context *netgroup_context=NULL; - char *name; + char name[256]; /* these are here for now until we rewrite the LDAP code */ struct __netgrent result; char buffer[1024]; @@ -299,7 +299,7 @@ int nslcd_netgroup_byname(FILE *fp) struct ldap_args a; enum nss_status stat=NSS_STATUS_SUCCESS; /* read request parameters */ - READ_STRING_ALLOC(fp,name); + READ_STRING_BUF2(fp,name,sizeof(name)); /* log call */ log_log(LOG_DEBUG,"nslcd_netgroup_byname(%s)",name); /* write the response header */ diff --git a/nslcd/network.c b/nslcd/network.c index e44a3e1..6b13ad3 100644 --- a/nslcd/network.c +++ b/nslcd/network.c @@ -111,18 +111,17 @@ _nss_ldap_parse_net (LDAPMessage * e, int nslcd_network_byname(FILE *fp) { int32_t tmpint32; - char *name; + char name[256]; struct ldap_args a; int retv; struct netent result; char buffer[1024]; int errnop; /* read request parameters */ - READ_STRING_ALLOC(fp,name); + READ_STRING_BUF2(fp,name,sizeof(name)); /* log call */ log_log(LOG_DEBUG,"nslcd_network_byname(%s)",name); /* write the response header */ - /* FIXME: free(name) when one of these writes fails */ WRITE_INT32(fp,NSLCD_VERSION); WRITE_INT32(fp,NSLCD_ACTION_NETWORK_BYNAME); /* do the LDAP request */ @@ -130,8 +129,6 @@ int nslcd_network_byname(FILE *fp) LA_STRING(a)=name; LA_TYPE(a)=LA_TYPE_STRING; retv=nss2nslcd(_nss_ldap_getbyname(&a,&result,buffer,1024,&errnop,_nss_ldap_filt_getnetbyname,LM_NETWORKS,_nss_ldap_parse_net)); - /* no more need for this string */ - free(name); /* write the response */ WRITE_INT32(fp,retv); if (retv==NSLCD_RESULT_SUCCESS) diff --git a/nslcd/passwd.c b/nslcd/passwd.c index eeca355..c393ab6 100644 --- a/nslcd/passwd.c +++ b/nslcd/passwd.c @@ -173,7 +173,7 @@ static enum nss_status _nss_ldap_parse_pw (LDAPMessage * e, int nslcd_passwd_byname(FILE *fp) { int32_t tmpint32; - char *name; + char name[256]; /* these are here for now until we rewrite the LDAP code */ struct passwd result; char buffer[1024]; @@ -181,8 +181,7 @@ int nslcd_passwd_byname(FILE *fp) int retv; struct ldap_args a; /* read request parameters */ - READ_STRING_ALLOC(fp,name); - /* FIXME: free() this buffer somewhere */ + READ_STRING_BUF2(fp,name,sizeof(name)); /* log call */ log_log(LOG_DEBUG,"nslcd_passwd_byname(%s)",name); /* do the LDAP request */ @@ -190,8 +189,6 @@ int nslcd_passwd_byname(FILE *fp) LA_STRING(a)=name; LA_TYPE(a)=LA_TYPE_STRING; retv=nss2nslcd(_nss_ldap_getbyname(&a,&result,buffer,1024,&errnop,_nss_ldap_filt_getpwnam,LM_PASSWD,_nss_ldap_parse_pw)); - /* no more need for this string */ - free(name); /* write the response */ WRITE_INT32(fp,NSLCD_VERSION); WRITE_INT32(fp,NSLCD_ACTION_PASSWD_BYNAME); diff --git a/nslcd/protocol.c b/nslcd/protocol.c index 71ce52d..857f7cd 100644 --- a/nslcd/protocol.c +++ b/nslcd/protocol.c @@ -96,7 +96,7 @@ static enum nss_status _nss_ldap_parse_proto (LDAPMessage *e, int nslcd_protocol_byname(FILE *fp) { int32_t tmpint32,tmp2int32,tmp3int32; - char *name; + char name[256]; struct ldap_args a; /* these are here for now until we rewrite the LDAP code */ struct protoent result; @@ -104,7 +104,7 @@ int nslcd_protocol_byname(FILE *fp) int errnop; int retv; /* read request parameters */ - READ_STRING_ALLOC(fp,name); + READ_STRING_BUF2(fp,name,sizeof(name)); /* log call */ log_log(LOG_DEBUG,"nslcd_protocol_byname(%s)",name); /* write the response header */ @@ -115,8 +115,6 @@ int nslcd_protocol_byname(FILE *fp) LA_STRING(a)=name; LA_TYPE(a)=LA_TYPE_STRING; retv=nss2nslcd(_nss_ldap_getbyname(&a,&result,buffer,1024,&errnop,_nss_ldap_filt_getprotobyname,LM_PROTOCOLS,_nss_ldap_parse_proto)); - /* no more need for this string */ - free(name); /* write the response */ WRITE_INT32(fp,retv); if (retv==NSLCD_RESULT_SUCCESS) diff --git a/nslcd/rpc.c b/nslcd/rpc.c index b58cca7..27b2b62 100644 --- a/nslcd/rpc.c +++ b/nslcd/rpc.c @@ -108,7 +108,7 @@ static enum nss_status _nss_ldap_parse_rpc (LDAPMessage * e, int nslcd_rpc_byname(FILE *fp) { int32_t tmpint32; - char *name; + char name[256]; struct ldap_args a; /* these are here for now until we rewrite the LDAP code */ struct rpcent result; @@ -116,7 +116,7 @@ int nslcd_rpc_byname(FILE *fp) int errnop; int retv; /* read request parameters */ - READ_STRING_ALLOC(fp,name); + READ_STRING_BUF2(fp,name,sizeof(name)); /* log call */ log_log(LOG_DEBUG,"nslcd_rpc_byname(%s)",name); /* write the response header */ @@ -127,8 +127,6 @@ int nslcd_rpc_byname(FILE *fp) LA_STRING(a)=name; LA_TYPE(a)=LA_TYPE_STRING; retv=nss2nslcd(_nss_ldap_getbyname(&a,&result,buffer,1024,&errnop,_nss_ldap_filt_getrpcbyname,LM_RPC,_nss_ldap_parse_rpc)); - /* no more need for this string */ - free(name); /* write the response */ WRITE_INT32(fp,retv); if (retv==NSLCD_RESULT_SUCCESS) diff --git a/nslcd/service.c b/nslcd/service.c index 7739b8e..59065aa 100644 --- a/nslcd/service.c +++ b/nslcd/service.c @@ -192,7 +192,7 @@ static enum nss_status _nss_ldap_parse_serv (LDAPMessage *e, int nslcd_service_byname(FILE *fp) { int32_t tmpint32; - char *name,*protocol; + char name[256],protocol[256]; struct ldap_args a; /* these are here for now until we rewrite the LDAP code */ struct servent result; @@ -200,8 +200,8 @@ int nslcd_service_byname(FILE *fp) int errnop; int retv; /* read request parameters */ - READ_STRING_ALLOC(fp,name); - READ_STRING_ALLOC(fp,protocol); + READ_STRING_BUF2(fp,name,sizeof(name)); + READ_STRING_BUF2(fp,protocol,sizeof(protocol)); /* log call */ log_log(LOG_DEBUG,"nslcd_service_byname(%s,%s)",name,protocol); /* write the response header */ @@ -215,9 +215,6 @@ int nslcd_service_byname(FILE *fp) retv=nss2nslcd(_nss_ldap_getbyname(&a,&result,buffer,1024,&errnop, ((strlen(protocol)==0)?_nss_ldap_filt_getservbyname:_nss_ldap_filt_getservbynameproto), LM_SERVICES,_nss_ldap_parse_serv)); - /* no more need for these strings */ - free(name); - free(protocol); /* write the response */ WRITE_INT32(fp,retv); if (retv==NSLCD_RESULT_SUCCESS) @@ -231,7 +228,7 @@ int nslcd_service_bynumber(FILE *fp) { int32_t tmpint32; int number; - char *protocol; + char protocol[256]; struct ldap_args a; /* these are here for now until we rewrite the LDAP code */ struct servent result; @@ -240,7 +237,7 @@ int nslcd_service_bynumber(FILE *fp) int retv; /* read request parameters */ READ_INT32(fp,number); - READ_STRING_ALLOC(fp,protocol); + READ_STRING_BUF2(fp,protocol,sizeof(protocol)); /* log call */ log_log(LOG_DEBUG,"nslcd_service_bynumber(%d,%s)",number,protocol); /* write the response header */ @@ -254,8 +251,6 @@ int nslcd_service_bynumber(FILE *fp) retv=nss2nslcd(_nss_ldap_getbyname(&a,&result,buffer,1024,&errnop, ((strlen(protocol)==0)?_nss_ldap_filt_getservbyport:_nss_ldap_filt_getservbyportproto), LM_SERVICES,_nss_ldap_parse_serv)); - /* no more need for this string */ - free(protocol); /* write the response */ WRITE_INT32(fp,retv); if (retv==NSLCD_RESULT_SUCCESS) diff --git a/nslcd/shadow.c b/nslcd/shadow.c index d1b22bd..cd3a2ee 100644 --- a/nslcd/shadow.c +++ b/nslcd/shadow.c @@ -122,14 +122,14 @@ static enum nss_status _nss_ldap_parse_sp(LDAPMessage *e, int nslcd_shadow_byname(FILE *fp) { int32_t tmpint32; - char *name; + char name[256]; struct ldap_args a; int retv; struct spwd result; char buffer[1024]; int errnop; /* read request parameters */ - READ_STRING_ALLOC(fp,name); + READ_STRING_BUF2(fp,name,sizeof(name)); /* log call */ log_log(LOG_DEBUG,"nslcd_shadow_byname(%s)",name); /* write the response header */ @@ -140,8 +140,6 @@ int nslcd_shadow_byname(FILE *fp) LA_STRING(a)=name; LA_TYPE(a)=LA_TYPE_STRING; retv=nss2nslcd(_nss_ldap_getbyname(&a,&result,buffer,1024,&errnop,_nss_ldap_filt_getspnam,LM_SHADOW,_nss_ldap_parse_sp)); - /* no more need for this string */ - free(name); /* write the response */ WRITE_INT32(fp,retv); if (retv==NSLCD_RESULT_SUCCESS) -- cgit v1.2.3