kerberos #2
5
.gitignore
vendored
5
.gitignore
vendored
@ -173,3 +173,8 @@ cython_debug/
|
||||
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||
#.idea/
|
||||
|
||||
a.out
|
||||
*.out
|
||||
*.o
|
||||
server
|
||||
client
|
||||
|
28
sample/Makefile
Normal file
28
sample/Makefile
Normal file
@ -0,0 +1,28 @@
|
||||
|
||||
CC=gcc
|
||||
|
||||
PROGRAM=client server
|
||||
|
||||
LIBSSL_LIBS=-lsasl2
|
||||
|
||||
FINAL_LIBS+=$(LIBSSL_LIBS)
|
||||
|
||||
FINAL_FLAG+=-Wimplicit-function-declaration $(FINAL_LIBS)
|
||||
|
||||
all: $(PROGRAM)
|
||||
@echo ""
|
||||
@echo "build seccess!"
|
||||
@echo ""
|
||||
|
||||
client:sample-client.c
|
||||
$(CC) -c sample-client.c $(FINAL_FLAG)
|
||||
$(CC) -o client sample-client.o $(FINAL_FLAG)
|
||||
|
||||
server:sample-server.c
|
||||
$(CC) -c sample-server.c $(FINAL_FLAG)
|
||||
$(CC) -o server sample-server.o $(FINAL_FLAG)
|
||||
|
||||
clean:
|
||||
rm *.o server client
|
||||
|
||||
|
106
sample/sample-client.c
Normal file
106
sample/sample-client.c
Normal file
@ -0,0 +1,106 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <sasl/sasl.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <linux/in.h>
|
||||
#include <string.h>
|
||||
|
||||
#define SAMPLE_SEC_BUF_SIZE (2048)
|
||||
|
||||
char buf[SAMPLE_SEC_BUF_SIZE];
|
||||
|
||||
static sasl_conn_t *conn = NULL;
|
||||
char *realm = "DOMAIN.COM";
|
||||
char *mech = "gssapi";
|
||||
char *iplocal = "127.0.0.1";
|
||||
char *ipremote = "127.0.0.1";
|
||||
char *searchpath = NULL;
|
||||
char *service = "zeekling";
|
||||
int cfd;
|
||||
|
||||
int init_sasl() {
|
||||
int result = sasl_client_init(NULL);
|
||||
if (result != SASL_OK) {
|
||||
printf("Initializing libsasl");
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int connect_server() {
|
||||
struct sockaddr_in s_add,c_add;
|
||||
unsigned short portnum=2345;
|
||||
printf("Hello,welcome to client !\r\n");
|
||||
cfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if(-1 == cfd) {
|
||||
printf("socket fail ! \r\n");
|
||||
return 1;
|
||||
}
|
||||
printf("socket ok !\r\n");
|
||||
bzero(&s_add,sizeof(struct sockaddr_in));
|
||||
s_add.sin_family=AF_INET;
|
||||
s_add.sin_addr.s_addr= inet_addr("127.0.0.1");
|
||||
s_add.sin_port=htons(portnum);
|
||||
|
||||
if(-1 == connect(cfd,(struct sockaddr *)(&s_add), sizeof(struct sockaddr))) {
|
||||
printf("connect fail !\r\n");
|
||||
return 1;
|
||||
}
|
||||
printf("connect ok !\r\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int auth_sever() {
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int result;
|
||||
const char *data;
|
||||
const char *chosenmech;
|
||||
int serverlast = 0;
|
||||
unsigned len;
|
||||
char *fqdn = "";
|
||||
char *userid = NULL;
|
||||
char *authid = NULL;
|
||||
|
||||
int recbytes;
|
||||
int sin_size;
|
||||
char buffer[1024]={0};
|
||||
|
||||
if (connect_server() != 0)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (init_sasl() != 0) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if(-1 == (recbytes = read(cfd, buffer, 1024))) {
|
||||
printf("read data fail !\r\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (strcmp(buffer, "need_auth") == 0 && auth_sever() != 0) {
|
||||
printf("auth failed");
|
||||
return -1;
|
||||
}
|
||||
printf("auth ok\r\nREC:\r\n");
|
||||
|
||||
buffer[recbytes]='\0';
|
||||
printf("%s\r\n",buffer);
|
||||
|
||||
getchar();
|
||||
close(cfd);
|
||||
|
||||
if (init_sasl() != 0) {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
124
sample/sample-server.c
Normal file
124
sample/sample-server.c
Normal file
@ -0,0 +1,124 @@
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/socket.h>
|
||||
#include <linux/in.h>
|
||||
#include <string.h>
|
||||
#include <sasl/sasl.h>
|
||||
|
||||
int sfp; /* 定义两个描述符 */
|
||||
struct sockaddr_in s_add,c_add;
|
||||
int sin_size;
|
||||
|
||||
typedef struct kClient {
|
||||
int fp;
|
||||
int auth_complete;
|
||||
int begin_auth;
|
||||
} kClient;
|
||||
|
||||
int listen_port() {
|
||||
unsigned short portnum=2345; /* 服务端使用端口 */
|
||||
|
||||
sfp = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if(-1 == sfp) {
|
||||
printf("socket fail ! \n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* 填充服务器端口地址信息,以便下面使用此地址和端口监听 */
|
||||
bzero(&s_add,sizeof(struct sockaddr_in));
|
||||
s_add.sin_family=AF_INET;
|
||||
s_add.sin_addr.s_addr=htonl(INADDR_ANY); /* 这里地址使用全0,即所有 */
|
||||
s_add.sin_port=htons(portnum);
|
||||
/* 使用bind进行绑定端口 */
|
||||
if(-1 == bind(sfp,(struct sockaddr *)(&s_add), sizeof(struct sockaddr)))
|
||||
{
|
||||
printf("bind fail !\n");
|
||||
return -1;
|
||||
}
|
||||
/* 开始监听相应的端口 */
|
||||
if(-1 == listen(sfp,5))
|
||||
{
|
||||
printf("listen fail !\n");
|
||||
return -1;
|
||||
}
|
||||
printf("Hello,welcome to my server !\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
kClient *createClient(int fp) {
|
||||
kClient *c = malloc(sizeof(kClient));
|
||||
c->fp = fp;
|
||||
c->auth_complete = -1;
|
||||
c->begin_auth = -1;
|
||||
return c;
|
||||
}
|
||||
|
||||
int auth_client(kClient *c) {
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void readQuery(kClient *c) {
|
||||
char buffer[1024]={0};
|
||||
int recbytes;
|
||||
/* 这里使用write向客户端发送信息,也可以尝试使用其他函数实现 */
|
||||
if(-1 == write(c->fp,"need_auth",9))
|
||||
{
|
||||
printf("write fail!\n");
|
||||
close(c->fp);
|
||||
return;
|
||||
}
|
||||
printf("write ok!\n");
|
||||
while (1) {
|
||||
if(-1 == (recbytes = read(c->fp,buffer,1024))) {
|
||||
printf("read data fail !\n");
|
||||
close(c->fp);
|
||||
}
|
||||
if (c->auth_complete != 1 && auth_client(c) != 0) {
|
||||
printf("auth failed!\n");
|
||||
break;
|
||||
}
|
||||
if(-1 == write(c->fp,"need_auth",9)) {
|
||||
printf("write fail!\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
close(c->fp);
|
||||
}
|
||||
|
||||
|
||||
int init_sasl() {
|
||||
int result = sasl_server_init(NULL, "sample");
|
||||
if (result != SASL_OK) {
|
||||
printf("Initializing libsasl");
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
if (listen_port() != 0 && init_sasl() != 0)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
while(1)
|
||||
{
|
||||
sin_size = sizeof(struct sockaddr_in);
|
||||
int nfp = accept(sfp, (struct sockaddr *)(&c_add), &sin_size);
|
||||
if(-1 == nfp)
|
||||
{
|
||||
printf("accept fail !\r\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("accept ok!\r\nServer start get connect from %#x : %#x\r\n",ntohl(c_add.sin_addr.s_addr),ntohs(c_add.sin_port));
|
||||
kClient *c = createClient(nfp);
|
||||
readQuery(c);
|
||||
}
|
||||
close(sfp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user