1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
/*
nslcd-client.c - request/response functions for nslcd communication
Copyright (C) 2006 West Consulting
Copyright (C) 2006 Arthur de Jong
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
MA 02110-1301 USA
*/
#include <stdint.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include "nslcd-client.h"
/* returns a socket to the server or NULL on error (see errno),
socket should be closed with fclose() */
FILE *nslcd_client_open()
{
int sock;
struct sockaddr_un addr;
FILE *fp;
/* create a socket */
if ( (sock=socket(PF_UNIX,SOCK_STREAM,0))<0 )
return NULL;
/* create socket address structure */
addr.sun_family=AF_UNIX;
strcpy(addr.sun_path,NSLCD_SOCKET);
/* connect to the socket */
if (connect(sock,(struct sockaddr *)&addr,sizeof(struct sockaddr_un))<0)
{
close(sock);
return NULL;
}
/* create a stream object */
if ((fp=fdopen(sock,"w+"))==NULL)
{
close(sock);
return NULL;
}
/* return the stream */
return fp;
}
/* helper marco for writes, bails out on any write problems */
#define WRITE(socket, buf, count) \
if (fwrite(buf, 1, count, socket) < (count)) \
{ return -1; }
/* helper macro for writing 32-bit integer values, uses tmpint32 as
temporary value (should be defined by caller) */
#define WRITE_INT32(socket, i) \
tmpint32 = (int32_t)(i); \
WRITE(socket, &tmpint32, sizeof(int32_t))
/* write a request message, returns <0 in case of errors */
int nslcd_client_writerequest(FILE *sock,int type,char *name,size_t count)
{
int32_t tmpint32;
/* see nslcd.h for protocol definition */
WRITE_INT32(sock, NSLCD_VERSION);
WRITE_INT32(sock, type);
WRITE_INT32(sock, count);
WRITE(sock, name, count);
WRITE_INT32(sock, NSLCD_MAGIC);
if (fflush(sock)<0)
return -1;
return 0; /* success */
}
/* read a response message */
int nslcd_client_readresponse(FILE *sock,void *buf,size_t bufsize)
{
/* see nslcd.h for protocol definition */
/* TODO: validate */
return -1; /* not implemented */
}
/* the main program... , for now just for testing */
int main(int argc,char *argv[])
{
FILE *sock;
char buf[1024];
/* open socket */
if ((sock=nslcd_client_open())==NULL)
{
fprintf(stderr,"test: socket unavailable: %s\n", strerror(errno));
return 1;
}
/* write request */
if (nslcd_client_writerequest(sock,NSLCD_RT_GETPWBYNAME,"aart",6)<0)
{
fprintf(stderr,"test: write failed: %s\n", strerror(errno));
return 1;
}
/* read response */
if (nslcd_client_readresponse(sock,buf,1024)<0)
{
fprintf(stderr,"test: read failed: %s\n", strerror(errno));
return 1;
}
/* print results */
/* close */
fclose(sock);
return 0;
}
|