[PATCH] Bug 9076 - Whois using a non working host for queries by default

Bernhard Reutner-Fischer rep.dot.nop at gmail.com
Tue Jul 5 22:44:15 UTC 2016


On 5 July 2016 at 22:13, Vito Mulè <mule.vito at gmail.com> wrote:
> On 5 July 2016 at 12:36, Bernhard Reutner-Fischer <rep.dot.nop at gmail.com>
> wrote:
> On Tue, Jul 05, 2016 at 12:48:36PM +0200, Bernhard Reutner-Fischer wrote:
>> On July 5, 2016 12:26:07 PM GMT+02:00, "Vito Mulè" <mule.vito at gmail.com>
>> wrote:
>>
>>
>> Care to send an updated patch, including bloat-o-meter output?
>
> Hello I managed to get the whois server hostname as you suggested, please
> let me know what you think.
> Patch also attached to the bug
> Thanks
>
> vmule at agent4:~/busybox.old$ ./busybox_unstripped whois  nic.ikano
> Domain Name: nic.ikano
> Domain ID: D0000000007-IKANO
> WHOIS Server: whois.nic.ikano
> Referral URL: http://www.nic.ikano/
>
> vmule at agent4:~/busybox.old$ ./busybox_unstripped whois amazon.it
>
> Domain:             amazon.it
> Status:             ok
> Created:            2000-02-10 00:00:00
> Last Update:        2016-01-28 00:47:21
> Expire Date:        2017-01-12
>
>
>
> Signed-off-by: Vito Mule' <mulevito at gmail.com>
>
> function                                             old     new   delta
> whois_host                                             -     277    +277
> whois_main                                           133     277    +144
> WHOIS_HOST                                             -      15     +15
> ------------------------------------------------------------------------------
> (add/remove: 2/0 grow/shrink: 1/0 up/down: 436/0)             Total: 436
> bytes

Sill way too big ;)
>
> ---
>  networking/whois.c | 78
> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------
>  1 file changed, 68 insertions(+), 10 deletions(-)
>
> diff --git a/networking/whois.c b/networking/whois.c
> index bf33033..052f079 100644
> --- a/networking/whois.c
> +++ b/networking/whois.c
> @@ -3,6 +3,7 @@
>   * whois - tiny client for the whois directory service
>   *
>   * Copyright (c) 2011 Pere Orga <gotrunks at gmail.com>
> + * modified by Vito Mule' <mulevito at gmail.com>
>   * Licensed under GPLv2 or later, see file LICENSE in this source tree.
>   */
>  /* TODO
> @@ -28,6 +29,11 @@
>
>  #include "libbb.h"
>
> +static const char WHOIS_HOST[] = "whois.iana.org";
> +#define WHOIS_HOST_LEN  128
> +#define WHOIS_DOMAIN_LEN  32

A domain label can be at most 63 characters long.
meinedomainistschwerzulesenjedochgenaudreiundsechzigzeichenlang.at
looooooooooooooooooooooooooooooooooooooooooooooooooooooooooongc.at

> +#define WHOIS_PORT 43
> +
>  static void pipe_out(int fd)
>  {
>   FILE *fp;
> @@ -36,30 +42,82 @@ static void pipe_out(int fd)
>   fp = xfdopen_for_read(fd);
>   while (fgets(buf, sizeof(buf), fp)) {
>   char *p = strpbrk(buf, "\r\n");
> - if (p)
> + if (p) {
>   *p = '\0';
> + }
>   puts(buf);
>   }
> -
>   fclose(fp); /* closes fd too */
>  }
>
> +/* Gets tld from NAME to find right whois sever. */
> +/* Called only from main for each NAME*/
> +void whois_host(char* host, char *argv_host)

should thus be named whois_server() or, better yet, fold it into whois_main?

> +{
> + FILE *fp;
> + char buf[1024];
> + char domain[WHOIS_DOMAIN_LEN];
> + char *str_token = strdup(argv_host);
> +
> + if (strlen(host) >= 1) {
> + memset(&host[0], 0, WHOIS_HOST_LEN);
> + }
> + char *domain_token = strtok(str_token, ".");
> + while (domain_token != NULL) {
> + strncpy(domain, domain_token, WHOIS_DOMAIN_LEN);
> + domain_token = strtok(NULL, ".");
> + }

char * tld = strrchr(argv_host, '.');
/* don't support querying TLD objects for now */
if (tld == NULL || ++tld == NULL)
  exit(1);

> + int fd = create_and_connect_stream_or_die(WHOIS_HOST, WHOIS_PORT);
> + fdprintf(fd, "%s\r\n", domain);

s/domain/tld/;

> +
> + fp = xfdopen_for_read(fd);
> + while (fgets(buf, sizeof(buf), fp)) {
> + char *p = strpbrk(buf, "\r\n");
> + if (p) {
> + *p = '\0';
> + }
> + if (strstr(buf,"whois:") != NULL) {
> + char *whois_token = strtok(buf, " ");
> + whois_token = strtok(NULL, " ");
> + strncpy(host, whois_token, WHOIS_HOST_LEN);
> + }
> + }
> + fclose(fp);
> + free(str_token);
> +}
> +
>  int whois_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
>  int whois_main(int argc UNUSED_PARAM, char **argv)
>  {
> +
> + char *host = malloc(WHOIS_HOST_LEN);
>   int port = 43;
> - const char *host = "whois-servers.net";
>
>   opt_complementary = "-1:p+";
>   getopt32(argv, "h:p:", &host, &port);
> -
>   argv += optind;
> - do {
> - int fd = create_and_connect_stream_or_die(host, port);
> - fdprintf(fd, "%s\r\n", *argv);
> - pipe_out(fd);
> +
> + if (strlen(host) < 1) {
> + do {
> + if (strlen(*argv) >= 67) {
> + dprintf(1, "Invalid request: %s\n", *argv);
> + return EXIT_FAILURE;
> + }
> + whois_host(host, *argv);
> + int fd = create_and_connect_stream_or_die(host, port);
> + fdprintf(fd, "%s\r\n", *argv);
> + pipe_out(fd);
> + }
> + while (*++argv);
> + free(host);
> + } else {
> + do {
> + int fd = create_and_connect_stream_or_die(host, port);
> + fdprintf(fd, "%s\r\n", *argv);
> + pipe_out(fd);
> + }

It is generally expensive to have the same code twice in there. Just
setup the server if -h was not given. The
create_and_connect_stream_or_die etc is a common thing to do
afterwards.

Remove odd code until size whois.o is about 300 bytes in total, i'd say.
cheers,


More information about the busybox mailing list