bool Client::go() { if (!isConnected()) { if (!connect()) { if (m_transport_to && m_transport_to->isConnected()) { char msg[0x100]; _snprintf(msg,sizeof(msg),"* Cannot connect to %s:%d\n",m_ip,m_port); m_transport_to->write(msg,strlen(msg)); m_transport_to->closeconn(); } return false; } } if (isConnected()) { if (!Transport::transport()) return false; } if (m_stop) { closeconn(); return false; } return true; }
Client::~Client() { if (isConnected()) closeconn(); delete m_ip; };
/* * Do the commands in cmds (initialized by yyparse). */ void docmds(struct namelist *hostlist, int argc, char **argv) { struct cmd *c; char *cp; int i; (void) signal(SIGHUP, sighandler); (void) signal(SIGINT, sighandler); (void) signal(SIGQUIT, sighandler); (void) signal(SIGTERM, sighandler); if (!nflag) mysetlinebuf(stdout); /* Make output (mostly) clean */ #if defined(USE_STATDB) if (!nflag && (dostatdb || juststatdb)) { extern long reccount; message(MT_INFO, "Making stat database [%s] ... \n", gettimestr()); if (mkstatdb() < 0) error("Warning: Make stat database failed."); message(MT_INFO, "Stat database created: %d files stored [%s].\n", reccount, gettimestr()); if (juststatdb) return; } #endif /* USE_STATDB */ /* * Print errors for any command line targets we didn't find. * If any errors are found, return to main() which will then exit. */ for (i = 0; i < argc; i++) { int found; for (found = FALSE, c = cmds; c != NULL; c = c->c_next) { if (c->c_label && argv[i] && strcmp(c->c_label, argv[i]) == 0) { found = TRUE; break; } } if (!found) error("Label \"%s\" is not defined in the distfile.", argv[i]); } if (nerrs) return; /* * Main command loop. Loop through all the commands. */ for (c = cmds; c != NULL; c = c->c_next) { checkcmd(c); if (do_fork) { /* * Let the children take care of their assigned host */ if (amchild) { if (strcmp(c->c_name, currenthost) != 0) continue; } else if (c->c_flags & CMD_ASSIGNED) { /* This cmd has been previously assigned */ debugmsg(DM_MISC, "prev assigned: %s\n", c->c_name); continue; } } if (hostlist) { /* Do specific hosts as specified on command line */ struct namelist *nlptr; for (nlptr = hostlist; nlptr; nlptr = nlptr->n_next) /* * Try an exact match and then a match * without '@' (if present). */ if ((strcmp(c->c_name, nlptr->n_name) == 0) || ((cp = strchr(c->c_name, '@')) && strcmp(++cp, nlptr->n_name) == 0)) docmd(c, argc, argv); continue; } else /* Do all of the command */ docmd(c, argc, argv); } if (do_fork) { /* * We're multi-threaded, so do appropriate shutdown * actions based on whether we're the parent or a child. */ if (amchild) { if (!IS_ON(options, DO_QUIET)) message(MT_VERBOSE, "updating of %s finished", currenthost); closeconn(); cleanup(0); exit(nerrs); } /* * Wait for all remaining active children to finish */ while (activechildren > 0) { debugmsg(DM_MISC, "Waiting for %d children to finish.\n", activechildren); waitup(); } } else if (!nflag) { /* * We're single-threaded so close down current connection */ closeconn(); cleanup(0); } }
/* * Create a connection to the rdist server on the machine rhost. * Return 0 if the connection fails or 1 if it succeeds. */ static int makeconn(char *rhost) { char *ruser, *cp; static char *cur_host = NULL; extern char *locuser; extern int64_t min_freefiles, min_freespace; extern char *remotemsglist; char tuser[BUFSIZ], buf[BUFSIZ]; u_char respbuff[BUFSIZ]; int n; debugmsg(DM_CALL, "makeconn(%s)", rhost); /* * See if we're already connected to this host */ if (cur_host != NULL && rem_w >= 0) { if (strcmp(cur_host, rhost) == 0) return(1); closeconn(); } /* * Determine remote user and current host names */ cur_host = rhost; cp = strchr(rhost, '@'); if (cp != NULL) { char c = *cp; *cp = CNULL; (void) strlcpy((char *)tuser, rhost, sizeof(tuser)); *cp = c; rhost = cp + 1; ruser = tuser; if (*ruser == CNULL) ruser = locuser; else if (!okname(ruser)) return(0); } else ruser = locuser; if (!IS_ON(options, DO_QUIET)) message(MT_VERBOSE, "updating host %s", rhost); (void) snprintf(buf, sizeof(buf), "%.*s -S", (int)(sizeof(buf)-5), path_rdistd); if ((rem_r = rem_w = remotecmd(rhost, locuser, ruser, buf)) < 0) return(0); /* * First thing received should be S_VERSION */ respbuff[0] = '\0'; n = remline(respbuff, sizeof(respbuff), TRUE); if (n <= 0 || respbuff[0] != S_VERSION) { if (n > 0) error("Unexpected input from server: \"%s\".", respbuff); else error("No input from server."); closeconn(); return(0); } /* * For future compatibility we check to see if the server * sent it's version number to us. If it did, we use it, * otherwise, we send our version number to the server and let * it decide if it can handle our protocol version. */ if (respbuff[1] == CNULL) { /* * The server wants us to send it our version number */ (void) sendcmd(S_VERSION, "%d", VERSION); if (response() < 0) return(0); } else { /* * The server sent it's version number to us */ proto_version = atoi(&respbuff[1]); if (proto_version != VERSION) { fatalerr( "Server version (%d) is not the same as local version (%d).", proto_version, VERSION); return(0); } } /* * Send config commands */ if (host[0]) { (void) sendcmd(C_SETCONFIG, "%c%s", SC_HOSTNAME, host); if (response() < 0) return(0); } if (min_freespace) { (void) sendcmd(C_SETCONFIG, "%c%lld", SC_FREESPACE, min_freespace); if (response() < 0) return(0); } if (min_freefiles) { (void) sendcmd(C_SETCONFIG, "%c%lld", SC_FREEFILES, min_freefiles); if (response() < 0) return(0); } if (remotemsglist) { (void) sendcmd(C_SETCONFIG, "%c%s", SC_LOGGING, remotemsglist); if (response() < 0) return(0); } if (strcmp(defowner, "bin") != 0) { (void) sendcmd(C_SETCONFIG, "%c%s", SC_DEFOWNER, defowner); if (response() < 0) return(0); } if (strcmp(defgroup, "bin") != 0) { (void) sendcmd(C_SETCONFIG, "%c%s", SC_DEFGROUP, defgroup); if (response() < 0) return(0); } return(1); }
void sighandler (int signum) { closeconn (sockfd); err_quit (_("Interrupted by signal %d..."), signum); }
void do_query (const int sock, const char *query) { char buf[200], *p; FILE *fi; int i = 0, hide = hide_discl; fi = fdopen (sock, "r"); if (write (sock, query, strlen (query)) < 0) err_sys ("write"); if (shutdown (sock, 1) < 0) err_sys ("shutdown"); while (fgets (buf, 200, fi)) { /* XXX errors? */ if (hide == 1) { if (strncmp (buf, hide_strings[i + 1], strlen (hide_strings[i + 1])) == 0) hide = 2; /* stop hiding */ continue; /* hide this line */ } if (hide == 0) { for (i = 0; hide_strings[i] != NULL; i += 2) { if (strncmp (buf, hide_strings[i], strlen (hide_strings[i])) == 0) { hide = 1; /* start hiding */ break; } } if (hide == 1) continue; /* hide the first line */ } #ifdef EXT_6BONE /* % referto: whois -h whois.arin.net -p 43 as 1 */ if (strncmp (buf, "% referto:", 10) == 0) { char nh[256], np[16], nq[1024]; if (sscanf (buf, REFERTO_FORMAT, nh, np, nq) == 3) { int fd; if (verb) printf (_("Detected referral to %s on %s.\n"), nq, nh); strcat (nq, "\r\n"); fd = openconn (nh, np); do_query (fd, nq); closeconn (fd); continue; } } #endif for (p = buf; *p && *p != '\r' && *p != '\n'; p++); *p = '\0'; fprintf (stdout, "%s\n", buf); } if (ferror (fi)) err_sys ("fgets"); if (hide == 1) err_quit (_("Catastrophic error: disclaimer text has been changed.\n" "Please upgrade this program.\n")); }
int main (int argc, char *argv[]) { int index; char *fstring; char *qstring; char *p; set_program_name (argv[0]); #ifdef ENABLE_NLS setlocale (LC_MESSAGES, ""); bindtextdomain (NLS_CAT_NAME, LOCALEDIR); textdomain (NLS_CAT_NAME); #endif obstack_init (&query_stk); iu_argp_init ("whois", program_authors); argp_parse (&gwhois_argp, argc, argv, ARGP_IN_ORDER, &index, NULL); obstack_1grow (&query_stk, 0); fstring = obstack_finish (&query_stk); argc -= index; argv += index; if (argc == 0 && !nopar) /* there is no parameter */ error (EXIT_FAILURE, 0, "not enough arguments"); /* parse other parameters, if any */ if (!nopar) { while (argc--) { const char *arg = *argv++; obstack_grow (&query_stk, arg, strlen (arg)); if (argc) obstack_1grow (&query_stk, ' '); } } obstack_1grow (&query_stk, 0); qstring = obstack_finish (&query_stk); if (!server && domfind (qstring, gtlds)) { if (verb) puts (_("Connecting to whois.internic.net.")); sockfd = openconn ("whois.internic.net", NULL); server = query_crsnic (sockfd, qstring); closeconn (sockfd); if (!server) exit (EXIT_SUCCESS); printf (_("\nFound InterNIC referral to %s.\n\n"), server); } if (!server) { server = whichwhois (qstring); switch (server[0]) { case 0: if (!(server = getenv ("WHOIS_SERVER"))) server = DEFAULTSERVER; if (verb) printf (_("Using default server %s.\n"), server); break; case 1: puts (_("This TLD has no whois server, but you can access the " "whois database at")); case 2: puts (server + 1); exit (EXIT_SUCCESS); case 3: puts (_("This TLD has no whois server.")); exit (EXIT_SUCCESS); default: if (verb) printf (_("Using server %s.\n"), server); } } if (getenv ("WHOIS_HIDE")) hide_discl = 0; p = queryformat (server, fstring, qstring); if (verb) printf (_("Query string: \"%s\"\n\n"), p); strcat (p, "\r\n"); signal (SIGTERM, sighandler); signal (SIGINT, sighandler); sockfd = openconn (server, port); do_query (sockfd, p); closeconn (sockfd); exit (EXIT_SUCCESS); }