kerberosTest/sample/sample-client.c
2022-07-17 20:42:28 +08:00

167 lines
3.9 KiB
C

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sasl/sasl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <linux/in.h>
#define SAMPLE_SEC_BUF_SIZE (2048)
char buf[SAMPLE_SEC_BUF_SIZE];
static sasl_conn_t *conn = NULL;
const char *realm = "TEST.COM";
const char *mech = "GSSAPI";
const char *iplocal = "127.0.0.1";
const char *ipremote = "127.0.0.1";
char *searchpath = NULL;
const char *service = "server";
const char *fqdn = "hadoop.test.com";
int cfd;
int init_sasl() {
int result = sasl_client_init(NULL);
if (result != SASL_OK) {
printf("Initializing libsasl\n");
return -1;
}
printf("sasl_client_init ok\n");
return 0;
}
int connect_server() {
struct sockaddr_in s_add, c_add;
unsigned short portnum = 2345;
printf("Hello,welcome to client !\n");
cfd = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == cfd) {
printf("socket fail ! \n");
return 1;
}
printf("socket ok !\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 !\n");
return 1;
}
printf("connect ok !\n");
return 0;
}
int readTicket(char *ticket) {
char len_str[10] = {0};
int recbytes;
if (-1 == (recbytes = read(cfd, len_str, 10))) {
printf("read len fail !\n");
close(cfd);
return 0;
}
char buffer[1024] = {0};
int len = atoi(len_str);
printf("read len=%s\n", len_str);
if (-1 == (recbytes = read(cfd, ticket, len))) {
printf("read data fail !\n");
close(cfd);
return 0;
}
return len;
}
void writeTicket(char *data, unsigned len) {
char len_str[10] = {0};
sprintf(len_str, "%d", len);
if (-1 == write(cfd, len_str, 10)) {
printf("write failed\n");
return 1;
}
if (-1 == write(cfd, data, len)) {
printf("write failed\n");
return 1;
}
}
int auth_sever() {
printf("begin auth\n");
int result = sasl_client_new("server", fqdn, NULL, NULL, NULL, 0, &conn);
if (result != SASL_OK) {
printf("client new client failed\n");
return 1;
}
char data[1024] = {0};
unsigned len = 0;
printf("begin sasl_client_start\n");
result = sasl_client_start(conn, mech, NULL, &data, &len, &mech);
if (result != SASL_OK && result != SASL_CONTINUE) {
printf("client auth start failed error:%s\n", sasl_errdetail(conn));
return 1;
}
//data[len] = '\0';
printf("sasl_client_start ok: ticket=%s, len=%d\n", data, len);
while (1) {
char len_str[10] = {0};
writeTicket(data, len);
char *buffer;
int size = readTicket(buffer);
result = sasl_client_step(conn, buffer, size, NULL, &data, &len);
if (result != SASL_OK && result != SASL_CONTINUE) {
printf("auth failed, %s\n", sasl_errdetail(conn));
return 1;
}
}
return 0;
}
int main() {
int result;
const char *data;
const char *chosenmech;
int serverlast = 0;
unsigned len;
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 !\n");
return -1;
}
if (strcmp(buffer, "need_auth") == 0 && auth_sever() != 0) {
printf("auth failed\n");
return -1;
}
printf("auth ok\nREC:\n");
buffer[recbytes] = '\0';
printf("%s\n", buffer);
auth_sever();
getchar();
close(cfd);
if (init_sasl() != 0) {
return 1;
}
return 0;
}