2021-04-27 18:33:31 +02:00
|
|
|
/*
|
|
|
|
WHOIS port for KolibriOS (Adapted by turbocat2001).
|
|
|
|
The main code is taken from the site:
|
|
|
|
https://www.binarytides.com/whois-client-code-in-c-with-linux-sockets/
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "sys/ksys.h"
|
|
|
|
#include <clayer/network.h>
|
2021-06-09 05:17:46 +02:00
|
|
|
#include <conio.h>
|
2022-04-15 11:11:49 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/socket.h>
|
2021-04-27 18:33:31 +02:00
|
|
|
|
2022-04-15 11:11:49 +02:00
|
|
|
FILE* out = stdout;
|
2021-04-27 18:33:31 +02:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2022-04-15 11:11:49 +02:00
|
|
|
FILE* out = stderr;
|
2021-04-27 18:33:31 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#define EXIT_SUCCESS 0
|
|
|
|
#define EXIT_FAILURE 1
|
|
|
|
|
|
|
|
#define fprintf fprintf
|
|
|
|
|
|
|
|
void show_help()
|
|
|
|
{
|
2022-04-15 11:11:49 +02:00
|
|
|
puts("Usage: whois <host> [-f <file>]\n");
|
|
|
|
puts(" host Connect to server host");
|
|
|
|
puts(" -f file Redirecting output to file\n");
|
|
|
|
puts("Example: whois google.com -f my.txt");
|
2021-04-27 18:33:31 +02:00
|
|
|
}
|
|
|
|
|
2022-04-15 11:11:49 +02:00
|
|
|
int get_whois_data(char*, char**);
|
|
|
|
int hostname_to_ip(char*, char*);
|
|
|
|
int whois_query(char*, char*, char**);
|
|
|
|
char* str_replace(char* search, char* replace, char* subject);
|
2021-04-27 18:33:31 +02:00
|
|
|
char* str_copy(char*);
|
|
|
|
|
2022-04-15 11:11:49 +02:00
|
|
|
int main(int argc, char* argv[])
|
2022-01-12 19:09:37 +01:00
|
|
|
{
|
2022-04-15 11:11:49 +02:00
|
|
|
char *domain, *data = NULL;
|
|
|
|
int f_flag = 0;
|
|
|
|
|
|
|
|
if (argc == 2) {
|
|
|
|
domain = strdup(argv[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (!strcmp(argv[2], "-f") && argc == 4) {
|
|
|
|
domain = strdup(argv[1]);
|
|
|
|
if ((out = fopen(argv[3], "w")) == NULL) {
|
|
|
|
printf("Error writing to file: '%s' !\n", argv[3]);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
show_help();
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
if (out == stdout) {
|
|
|
|
con_init();
|
|
|
|
(*con_set_title)("Whois");
|
|
|
|
}
|
|
|
|
get_whois_data(domain, &data);
|
|
|
|
exit(0);
|
2021-04-27 18:33:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Get the whois data of a domain
|
|
|
|
*/
|
|
|
|
|
2022-04-15 11:11:49 +02:00
|
|
|
int get_whois_data(char* domain, char** data)
|
2021-04-27 18:33:31 +02:00
|
|
|
{
|
2022-04-15 11:11:49 +02:00
|
|
|
char ext[1024], *pch, *response = NULL, *response_2 = NULL, *wch, *dt;
|
|
|
|
|
|
|
|
// remove "http://" and "www."
|
|
|
|
domain = str_replace("http://", "", domain);
|
|
|
|
domain = str_replace("www.", "", domain);
|
|
|
|
|
|
|
|
// get the extension , com , org , edu
|
|
|
|
dt = strdup(domain);
|
|
|
|
|
|
|
|
if (dt == NULL) {
|
|
|
|
fprintf(out, "strdup failed");
|
|
|
|
}
|
|
|
|
pch = (char*)strtok(dt, ".");
|
|
|
|
while (pch != NULL) {
|
|
|
|
strcpy(ext, pch);
|
|
|
|
pch = strtok(NULL, ".");
|
|
|
|
}
|
|
|
|
// This will tell the whois server for the particular TLD like com , org
|
|
|
|
if (whois_query("whois.iana.org", ext, &response) == EXIT_FAILURE) {
|
|
|
|
fprintf(out, "Whois query failed");
|
2021-04-27 18:33:31 +02:00
|
|
|
return 1;
|
2022-04-15 11:11:49 +02:00
|
|
|
}
|
|
|
|
fprintf(out, "\n\nResponse is:\n\n");
|
|
|
|
fprintf(out, "%s", response);
|
|
|
|
|
|
|
|
// Now analysze the response
|
|
|
|
pch = strtok(response, "\n");
|
|
|
|
while (pch != NULL) {
|
|
|
|
// Check if whois line
|
|
|
|
wch = strstr(pch, "whois.");
|
|
|
|
if (wch != NULL) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next line please
|
|
|
|
pch = strtok(NULL, "\n");
|
|
|
|
}
|
|
|
|
// Now we have the TLD whois server in wch , query again
|
|
|
|
// This will provide minimal whois information along with the parent whois server of the specific domain :)
|
|
|
|
wch = strdup(wch);
|
|
|
|
free(response);
|
|
|
|
// This should not be necessary , but segmentation fault without this , why ?
|
|
|
|
response = NULL;
|
|
|
|
if (wch != NULL) {
|
|
|
|
fprintf(out, "\nTLD Whois server is : %s", wch);
|
|
|
|
if (whois_query(wch, domain, &response) == EXIT_FAILURE) {
|
|
|
|
fprintf(out, "Whois query failed\n");
|
2021-04-27 18:33:31 +02:00
|
|
|
return EXIT_FAILURE;
|
2022-04-15 11:11:49 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fprintf(out, "\nTLD whois server for %s not found\n", ext);
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
response_2 = strdup(response);
|
|
|
|
|
2021-04-27 18:33:31 +02:00
|
|
|
// Again search for a whois server in this response. :)
|
2022-04-15 11:11:49 +02:00
|
|
|
pch = strtok(response, "\n");
|
|
|
|
while (pch != NULL) {
|
|
|
|
// Check if whois line
|
|
|
|
wch = strstr(pch, "whois.");
|
|
|
|
if (wch != NULL) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Next line please
|
|
|
|
pch = strtok(NULL, "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
If a registrar whois server is found then query it
|
|
|
|
*/
|
|
|
|
if (wch) {
|
|
|
|
// Now we have the registrar whois server , this has the direct full information of the particular domain
|
|
|
|
// so lets query again
|
|
|
|
|
|
|
|
fprintf(out, "\nRegistrar Whois server is : %s", wch);
|
|
|
|
|
|
|
|
if (whois_query(wch, domain, &response) == EXIT_FAILURE) {
|
|
|
|
fprintf(out, "Whois query failed");
|
|
|
|
} else {
|
|
|
|
fprintf(out, "\n%s", response);
|
2021-04-27 18:33:31 +02:00
|
|
|
}
|
2022-04-15 11:11:49 +02:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
otherwise echo the output from the previous whois result
|
|
|
|
*/
|
|
|
|
else {
|
|
|
|
fprintf(out, "%s", response_2);
|
|
|
|
}
|
|
|
|
return 0;
|
2021-04-27 18:33:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Perform a whois query to a server and record the response
|
|
|
|
*/
|
2022-04-15 11:11:49 +02:00
|
|
|
int whois_query(char* server, char* query, char** response)
|
2021-04-27 18:33:31 +02:00
|
|
|
{
|
2022-04-15 11:11:49 +02:00
|
|
|
char ip[32], message[100], buffer[1500];
|
|
|
|
int sock, read_size, total_size = 0;
|
|
|
|
int WHOIS_PORT = 43;
|
2021-04-27 18:33:31 +02:00
|
|
|
struct sockaddr dest;
|
2022-04-15 11:11:49 +02:00
|
|
|
|
|
|
|
sock = socket(AF_INET4, SOCK_STREAM, IPPROTO_TCP);
|
|
|
|
|
|
|
|
// Prepare connection structures :)
|
|
|
|
memset(&dest, 0, sizeof(dest));
|
|
|
|
dest.sin_family = AF_INET;
|
2021-04-27 18:33:31 +02:00
|
|
|
server = str_copy(server);
|
2022-04-15 11:11:49 +02:00
|
|
|
|
2021-04-27 18:33:31 +02:00
|
|
|
server[strcspn(server, "\r\n")] = '\0';
|
2022-04-15 11:11:49 +02:00
|
|
|
fprintf(out, "\nResolving: %s ...\n", server);
|
|
|
|
if (hostname_to_ip(server, ip) == EXIT_FAILURE) {
|
2021-04-27 18:33:31 +02:00
|
|
|
fprintf(out, "Failed\n");
|
|
|
|
return EXIT_FAILURE;
|
2022-04-15 11:11:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(out, "Found ip: %s \n", ip);
|
2021-04-27 18:33:31 +02:00
|
|
|
dest.sin_addr = inet_addr(ip);
|
2022-04-15 11:11:49 +02:00
|
|
|
dest.sin_port = PORT(WHOIS_PORT);
|
2021-04-27 18:33:31 +02:00
|
|
|
|
2022-04-15 11:11:49 +02:00
|
|
|
; // Now connect to remote server
|
|
|
|
if (connect(sock, (const struct sockaddr*)&dest, sizeof(dest)) < 0) {
|
|
|
|
perror("connect failed");
|
|
|
|
perror(strerror(errno));
|
2021-04-27 18:33:31 +02:00
|
|
|
return EXIT_FAILURE;
|
2022-04-15 11:11:49 +02:00
|
|
|
}
|
2021-04-27 18:33:31 +02:00
|
|
|
|
2022-04-15 11:11:49 +02:00
|
|
|
// Now send some data or message
|
|
|
|
fprintf(out, "\nQuerying for: %s ...\n", query);
|
|
|
|
sprintf(message, "%s\r\n", query);
|
|
|
|
if (send(sock, message, strlen(message), 0) < 0) {
|
|
|
|
perror("send failed");
|
2021-04-27 18:33:31 +02:00
|
|
|
return EXIT_FAILURE;
|
2022-04-15 11:11:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now receive the response
|
|
|
|
while ((read_size = recv(sock, buffer, sizeof(buffer), 0))) {
|
|
|
|
*response = realloc(*response, read_size + total_size);
|
|
|
|
if (*response == NULL) {
|
|
|
|
fprintf(out, "realloc failed");
|
2021-04-27 18:33:31 +02:00
|
|
|
return EXIT_FAILURE;
|
2022-04-15 11:11:49 +02:00
|
|
|
}
|
|
|
|
memcpy(*response + total_size, buffer, read_size);
|
|
|
|
total_size += read_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(out, "Done\n");
|
|
|
|
|
|
|
|
*response = realloc(*response, total_size + 1);
|
|
|
|
*(*response + total_size) = '\0';
|
|
|
|
close(sock);
|
2021-04-27 18:33:31 +02:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
Get the ip address of a given hostname
|
|
|
|
*/
|
2022-04-15 11:11:49 +02:00
|
|
|
int hostname_to_ip(char* hostname, char* ip)
|
2021-04-27 18:33:31 +02:00
|
|
|
{
|
2022-04-15 11:11:49 +02:00
|
|
|
struct addrinfo* addr_info;
|
|
|
|
char port_str[16];
|
|
|
|
sprintf(port_str, "%d", 80);
|
2021-04-27 18:33:31 +02:00
|
|
|
struct addrinfo hints;
|
|
|
|
memset(&hints, 0, sizeof(hints));
|
2022-04-15 11:11:49 +02:00
|
|
|
hints.ai_family = AF_UNSPEC; // IPv4 or IPv6 doesnt matter
|
2021-04-27 18:33:31 +02:00
|
|
|
hints.ai_socktype = SOCK_STREAM; // TCP stream sockets
|
|
|
|
if (getaddrinfo(hostname, port_str, 0, &addr_info) != 0) {
|
|
|
|
freeaddrinfo(addr_info);
|
2022-04-15 11:11:49 +02:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
} else {
|
|
|
|
strcpy(ip, inet_ntoa(addr_info->ai_addr->sin_addr));
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
2021-04-27 18:33:31 +02:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
Search and replace a string with another string , in a string
|
|
|
|
*/
|
2022-04-15 11:11:49 +02:00
|
|
|
char* str_replace(char* search, char* replace, char* subject)
|
2021-04-27 18:33:31 +02:00
|
|
|
{
|
2022-04-15 11:11:49 +02:00
|
|
|
char *p = NULL, *old = NULL, *new_subject = NULL;
|
|
|
|
int c = 0, search_size;
|
|
|
|
|
|
|
|
search_size = strlen(search);
|
|
|
|
|
|
|
|
// Count how many occurences
|
|
|
|
for (p = strstr(subject, search); p != NULL; p = strstr(p + search_size, search)) {
|
|
|
|
c++;
|
|
|
|
}
|
|
|
|
// Final size
|
|
|
|
c = (strlen(replace) - search_size) * c + strlen(subject);
|
|
|
|
|
|
|
|
// New subject with new size
|
|
|
|
new_subject = malloc(c);
|
|
|
|
|
|
|
|
// Set it to blank
|
|
|
|
strcpy(new_subject, "");
|
|
|
|
|
|
|
|
// The start position
|
|
|
|
old = subject;
|
|
|
|
|
|
|
|
for (p = strstr(subject, search); p != NULL; p = strstr(p + search_size, search)) {
|
|
|
|
// move ahead and copy some text from original subject , from a certain position
|
|
|
|
strncpy(new_subject + strlen(new_subject), old, p - old);
|
|
|
|
|
|
|
|
// move ahead and copy the replacement text
|
|
|
|
strcpy(new_subject + strlen(new_subject), replace);
|
|
|
|
|
|
|
|
// The new start position after this search match
|
|
|
|
old = p + search_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy the part after the last search match
|
|
|
|
strcpy(new_subject + strlen(new_subject), old);
|
|
|
|
|
|
|
|
return new_subject;
|
2021-04-27 18:33:31 +02:00
|
|
|
}
|
|
|
|
|
2022-04-15 11:11:49 +02:00
|
|
|
char* str_copy(char* source)
|
2021-04-27 18:33:31 +02:00
|
|
|
{
|
2022-04-15 11:11:49 +02:00
|
|
|
char* copy = malloc(strlen(source) + 1);
|
|
|
|
|
|
|
|
if (copy) {
|
2021-04-27 18:33:31 +02:00
|
|
|
strcpy(copy, source);
|
|
|
|
}
|
|
|
|
return copy;
|
2022-04-15 11:11:49 +02:00
|
|
|
}
|