📄 ipa_adm.c
字号:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pcap.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netinet/if_ether.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <sys/msg.h>
#include <sys/socket.h>
#include <stdarg.h>
#include <sys/errno.h>
#include <sys/signal.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
#include <sys/signal.h>
#include <sys/time.h>
#include <time.h>
#include <netdb.h>
#include <ctype.h>
#include <stdarg.h>
#include <fcntl.h>
#include <stdio.h>
#include <dirent.h>
#include "ipa_fun.h"
char gd_SYS_LOG_FILE[128];
int add_ip_list(char *argv[]);
int delete_ip_list(char *argv[]);
int add_white_list(char *argv[]);
int delete_white_list(char *argv[]);
int print_ip_list(char *argv[]);
int print_white_list(char *argv[]);
int usage();
int main(int argc, char *argv[])
{
int ret;
char hostname[256];
char cmd[32];
strcpy(gd_SYS_LOG_FILE, "ipa_adm.log");
ret = gethostname(hostname, sizeof(hostname));
// printf("ret[%d] hostname[%s]\n", ret, hostname);
if (getenv("IPA_SERVICE_PORT") == NULL)
{
printf("The \"IPA_SERVICE_PORT\" env varible is not set!\n");
exit(1);
}
if (argc < 2)
{
printf("Invalid argument!\n");
usage();
exit(1);
}
strcpy(cmd, argv[1]);
if (strcmp(cmd, "0") == 0)
{
if (argc != 5)
{
printf("bad argument for add ip list!\n");
usage();
exit(1);
}
add_ip_list(argv);
}
else if (strcmp(cmd, "1") == 0)
{
if (argc != 3)
{
printf("bad argument for delete ip list!\n");
usage();
exit(1);
}
delete_ip_list(argv);
}
else if (strcmp(cmd, "2") == 0)
{
if (argc != 5)
{
printf("bad argument for add white list!\n");
usage();
exit(1);
}
add_white_list(argv);
}
else if (strcmp(cmd, "3") == 0)
{
if (argc != 3)
{
printf("bad argument for delete white list!\n");
usage();
exit(1);
}
delete_white_list(argv);
}
else if (strcmp(cmd, "4") == 0)
{
if (argc != 2)
{
printf("bad argument for print ip list to /tmp/wl.txt!\n");
usage();
exit(1);
}
print_ip_list(argv);
}
else if (strcmp(cmd, "5") == 0)
{
if (argc != 2)
{
printf("bad argument for print white list to /tmp/ipl.txt!\n");
usage();
exit(1);
}
print_white_list(argv);
}
else if (strcmp(cmd, "6") == 0)
{
if (argc != 2)
{
printf("bad argument for reset ip list!\n");
usage();
exit(1);
}
reset_ip_list(argv);
}
else if (strcmp(cmd, "7") == 0)
{
if (argc != 2)
{
printf("bad argument for reset white list!\n");
usage();
exit(1);
}
reset_white_list(argv);
}
else if (strcmp(cmd, "8") == 0)
{
if (argc != 5)
{
printf("bad argument for check if ip/mac/name in the white list!\n");
usage();
exit(1);
}
check_if_ip_mac_name_in_the_white_list(argv);
}
else if (strcmp(cmd, "9") == 0)
{
if (argc != 5)
{
printf("bad argument for check if ip/mac/name in the IP list!\n");
usage();
exit(1);
}
check_if_ip_mac_name_in_the_ip_list(argv);
}
else
{
printf("Bad command!\n");
usage();
}
return 0;
}
int usage()
{
printf("\nUsage:\n");
printf(" ./ipa_adm 0 <IP> <MAC> <NAME> :add ip list\n");
printf(" ./ipa_adm 1 <IP> :delete ip list\n");
printf(" ./ipa_adm 2 <IP> <MAC> <NAME> :add white list\n");
printf(" ./ipa_adm 3 <IP> :delete white list\n");
printf(" ./ipa_adm 4 :print ip list to /tmp/ipl.txt\n");
printf(" ./ipa_adm 5 :print white list to /tmp/wl.txt\n");
printf(" ./ipa_adm 6 :reset ip list\n");
printf(" ./ipa_adm 7 :reset white list\n");
printf(" ./ipa_adm 8 <IP> <MAC> <NAME> :check if ip/mac/name in the white list\n");
printf(" ./ipa_adm 9 <IP> <MAC> <NAME> :check if ip/mac/name in the IP list\n");
printf("\n");
}
int check_if_ip_mac_name_in_the_ip_list(char *argv[])
{
char buf[256];
char outbuf[256];
int outlen;
Header_t *pheader;
IP_MAC_NAME_t *plist;
char hostname[256];
int ret;
ret = gethostname(hostname, sizeof(hostname));
if (ret < 0)
{
printf("gethostname fail![%s]\n", strerror(errno));
return -1;
}
pheader = (Header_t *)buf;
plist = (IP_MAC_NAME_t *)(buf+sizeof(Header_t));
pheader->buflen = sizeof(Header_t) + sizeof(IP_MAC_NAME_t);
pheader->code = CHECK_IF_IP_MAC_NAME_IN_THE_IP_LIST;
pheader->buflen = htonl(pheader->buflen);
pheader->code = htonl(pheader->code );
if ( (strlen(argv[2]) > 15) || (strlen(argv[2]) < 7) )
{
printf("Bad ip[%s]\n", argv[2]);
return -1;
}
if ( strlen(argv[3]) != 17)
{
printf("Bad mac[%s]\n", argv[3]);
return -1;
}
if ( (strlen(argv[4]) > 31) || (strlen(argv[4]) < 1) )
{
printf("Bad name[%s], too long(1-31)\n", argv[4]);
return -1;
}
strcpy(plist->ip, argv[2]);
strcpy(plist->mac, argv[3]);
strcpy(plist->name, argv[4]);
ret = client_request(hostname, getenv("IPA_SERVICE_PORT"), buf, ntohl(pheader->buflen),
outbuf, &outlen);
if (ret < 0) return -1;
pheader = (Header_t *)outbuf;
pheader->buflen = ntohl(pheader->buflen);
pheader->code = ntohl(pheader->code );
if (pheader->code != 0)
{
printf("%s", outbuf+sizeof(Header_t));
}
else
{
printf("Yes, it's in the ip list!\n");
}
return 0;
}
int check_if_ip_mac_name_in_the_white_list(char *argv[])
{
char buf[256];
char outbuf[256];
int outlen;
Header_t *pheader;
IP_MAC_NAME_t *plist;
char hostname[256];
int ret;
ret = gethostname(hostname, sizeof(hostname));
if (ret < 0)
{
printf("gethostname fail![%s]\n", strerror(errno));
return -1;
}
pheader = (Header_t *)buf;
plist = (IP_MAC_NAME_t *)(buf+sizeof(Header_t));
pheader->buflen = sizeof(Header_t) + sizeof(IP_MAC_NAME_t);
pheader->code = CHECK_IF_IP_MAC_NAME_IN_THE_WHITE_LIST;
pheader->buflen = htonl(pheader->buflen);
pheader->code = htonl(pheader->code );
if ( (strlen(argv[2]) > 15) || (strlen(argv[2]) < 7) )
{
printf("Bad ip[%s]\n", argv[2]);
return -1;
}
if ( strlen(argv[3]) != 17)
{
printf("Bad mac[%s]\n", argv[3]);
return -1;
}
if ( (strlen(argv[4]) > 31) || (strlen(argv[4]) < 1) )
{
printf("Bad name[%s], too long(1-31)\n", argv[4]);
return -1;
}
strcpy(plist->ip, argv[2]);
strcpy(plist->mac, argv[3]);
strcpy(plist->name, argv[4]);
ret = client_request(hostname, getenv("IPA_SERVICE_PORT"), buf, ntohl(pheader->buflen),
outbuf, &outlen);
if (ret < 0) return -1;
pheader = (Header_t *)outbuf;
pheader->buflen = ntohl(pheader->buflen);
pheader->code = ntohl(pheader->code );
if (pheader->code != 0)
{
printf("%s", outbuf+sizeof(Header_t));
}
else
{
printf("Yes, it's in the white list!\n");
}
return 0;
}
int reset_white_list(char *argv[])
{
char buf[256];
char outbuf[256];
int outlen;
Header_t *pheader;
char hostname[256];
int ret;
ret = gethostname(hostname, sizeof(hostname));
if (ret < 0)
{
printf("gethostname fail![%s]\n", strerror(errno));
return -1;
}
pheader = (Header_t *)buf;
pheader->buflen = sizeof(Header_t);
pheader->code = RESET_WHITE_LIST;
pheader->buflen = htonl(pheader->buflen);
pheader->code = htonl(pheader->code );
ret = client_request(hostname, getenv("IPA_SERVICE_PORT"), buf, ntohl(pheader->buflen),
outbuf, &outlen);
if (ret < 0) return -1;
pheader = (Header_t *)outbuf;
pheader->buflen = ntohl(pheader->buflen);
pheader->code = ntohl(pheader->code );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -