louieson
March 13th, 2010, 07:50 AM
Dear all,
Along time ago a wrote a C daemon server program that communicates with clients. When a client first attaches the program it passes the users email credentials which where check against an IMAP server. When the IMAP server was retired I was only given the option to authenticate against LDAP. This worked very well for a number of years until we had to virtualize our servers. We used CENTOS, but when CENTOS patched its self it broke the VM. So we looked at Ubuntu 8.04 and it worked well under a VM environment.
My daemon programmed complied with no warning (-Wall switch). But the clients could not connect. After much experimenting I reliased that if I linked in the ldap library it would not allow socket connections.
Compile the program below first like this
cc t1.c -o t1
and run it (it will go into background) and connect to it using Telnet like so
telnet 127.0.0.1 9446
The telnet program connects and after you type a string of greater that 10 charaters an hit return. The program will echo back the first 10 characters and terminate the telnet session.
kill the daemon with
killall t1
If you repeat the process but this time compile and link in ldap like so
cc t1.c -o t1 -lldap
the program will compile correctly however when you run it and try to connect with telnet, telnet says that connect was closed by foreign host.
Any one got any ideas. All I have done is just linked in LDAP and it prevents any socket open for listening from working, and I have not even used ldap library calls in the code
Regards
Louie
sample code below
//-------------------------------------------------------
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pwd.h>
#include <sys/time.h>
#include <errno.h>
#include <netdb.h>
#include <sys/stat.h>
#include <sys/uio.h>
#include <sys/wait.h>
#include <sys/un.h>
#define FALSE 0
#define TRUE !FALSE
char sendBuff[5000];
int server_sockfd, client_sockfd;
// added for updated system
struct sockaddr_in admin_address;
struct sockaddr_in temp_address;
int main(int argc,char **argv)
{
int pid;
int server_len,client_len;
struct sockaddr_in server_address;
struct sockaddr_in client_address;
unsigned char *cptr;
FILE *fp;
int loop = 1;
struct sockaddr_in loopback_address;
if ((pid = fork()) == -1)
{
fprintf(stderr,"Unable to fork\n");
exit(0);
}
if (pid)
return 0;
server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
server_address.sin_family=AF_INET;
// server_address.sin_addr.s_addr = inet_addr("127.0.0.1"); // replaced to allow for admin client to communicate
server_address.sin_addr.s_addr = htonl(INADDR_ANY);
loopback_address.sin_addr.s_addr = inet_addr("127.0.0.1");
server_address.sin_port = htons(9446);
server_len = sizeof(server_address);
bind(server_sockfd,(struct sockaddr *)&server_address,server_len);
listen(server_sockfd,5);
while (loop)
{
int j= 20;
client_sockfd=(unsigned)accept(server_sockfd,
(struct sockaddr *)&client_address,
(unsigned *)&client_len);
j = readn(client_sockfd,sendBuff,10);
// readn(client_sockfd,sendBuff,2000);
cptr=(unsigned char *)sendBuff;
temp_address.sin_addr.s_addr = client_address.sin_addr.s_addr;
printf("%s\n",sendBuff);
readn(client_sockfd,"ABCDEFHIJKLMNOPQRSTUVWXYZ",20);
close(client_sockfd);
}
return 0;
}
int writen(int fd, void *vptr, int n)
{
int nleft;
int nwritten;
char *ptr;
ptr = vptr;
nleft = n;
while (nleft > 0)
{
if ((nwritten = write(fd,ptr,nleft)) <=0)
{
if (errno == EINTR)
nwritten = 0;
else
return -1;
}
nleft -=nwritten;
ptr+= nwritten;
}
return (n);
}
int readn(int fd, void *vptr, int n)
{
int nleft;
int nread;
char *ptr;
ptr = vptr;
nleft = n;
while (nleft > 0)
{
if ((nread = read(fd,ptr,nleft)) < 0)
{
if (errno == EINTR)
nread = 0;
else
return -1;
}
else if (nread == 0)
break;
nleft -=nread;
ptr+= nread;
}
return (n- nleft);
}
Along time ago a wrote a C daemon server program that communicates with clients. When a client first attaches the program it passes the users email credentials which where check against an IMAP server. When the IMAP server was retired I was only given the option to authenticate against LDAP. This worked very well for a number of years until we had to virtualize our servers. We used CENTOS, but when CENTOS patched its self it broke the VM. So we looked at Ubuntu 8.04 and it worked well under a VM environment.
My daemon programmed complied with no warning (-Wall switch). But the clients could not connect. After much experimenting I reliased that if I linked in the ldap library it would not allow socket connections.
Compile the program below first like this
cc t1.c -o t1
and run it (it will go into background) and connect to it using Telnet like so
telnet 127.0.0.1 9446
The telnet program connects and after you type a string of greater that 10 charaters an hit return. The program will echo back the first 10 characters and terminate the telnet session.
kill the daemon with
killall t1
If you repeat the process but this time compile and link in ldap like so
cc t1.c -o t1 -lldap
the program will compile correctly however when you run it and try to connect with telnet, telnet says that connect was closed by foreign host.
Any one got any ideas. All I have done is just linked in LDAP and it prevents any socket open for listening from working, and I have not even used ldap library calls in the code
Regards
Louie
sample code below
//-------------------------------------------------------
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pwd.h>
#include <sys/time.h>
#include <errno.h>
#include <netdb.h>
#include <sys/stat.h>
#include <sys/uio.h>
#include <sys/wait.h>
#include <sys/un.h>
#define FALSE 0
#define TRUE !FALSE
char sendBuff[5000];
int server_sockfd, client_sockfd;
// added for updated system
struct sockaddr_in admin_address;
struct sockaddr_in temp_address;
int main(int argc,char **argv)
{
int pid;
int server_len,client_len;
struct sockaddr_in server_address;
struct sockaddr_in client_address;
unsigned char *cptr;
FILE *fp;
int loop = 1;
struct sockaddr_in loopback_address;
if ((pid = fork()) == -1)
{
fprintf(stderr,"Unable to fork\n");
exit(0);
}
if (pid)
return 0;
server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
server_address.sin_family=AF_INET;
// server_address.sin_addr.s_addr = inet_addr("127.0.0.1"); // replaced to allow for admin client to communicate
server_address.sin_addr.s_addr = htonl(INADDR_ANY);
loopback_address.sin_addr.s_addr = inet_addr("127.0.0.1");
server_address.sin_port = htons(9446);
server_len = sizeof(server_address);
bind(server_sockfd,(struct sockaddr *)&server_address,server_len);
listen(server_sockfd,5);
while (loop)
{
int j= 20;
client_sockfd=(unsigned)accept(server_sockfd,
(struct sockaddr *)&client_address,
(unsigned *)&client_len);
j = readn(client_sockfd,sendBuff,10);
// readn(client_sockfd,sendBuff,2000);
cptr=(unsigned char *)sendBuff;
temp_address.sin_addr.s_addr = client_address.sin_addr.s_addr;
printf("%s\n",sendBuff);
readn(client_sockfd,"ABCDEFHIJKLMNOPQRSTUVWXYZ",20);
close(client_sockfd);
}
return 0;
}
int writen(int fd, void *vptr, int n)
{
int nleft;
int nwritten;
char *ptr;
ptr = vptr;
nleft = n;
while (nleft > 0)
{
if ((nwritten = write(fd,ptr,nleft)) <=0)
{
if (errno == EINTR)
nwritten = 0;
else
return -1;
}
nleft -=nwritten;
ptr+= nwritten;
}
return (n);
}
int readn(int fd, void *vptr, int n)
{
int nleft;
int nread;
char *ptr;
ptr = vptr;
nleft = n;
while (nleft > 0)
{
if ((nread = read(fd,ptr,nleft)) < 0)
{
if (errno == EINTR)
nread = 0;
else
return -1;
}
else if (nread == 0)
break;
nleft -=nread;
ptr+= nread;
}
return (n- nleft);
}