[Autosec-devel] sonar/src plugin.h,1.32,1.33 sonar.c,1.26,1.27 sonar.h,1.26,1.27 target.c,1.8,1.9 ta
Brought to you by:
red0x
From: <re...@us...> - 2004-01-17 03:12:35
|
Update of /cvsroot/autosec/sonar/src In directory sc8-pr-cvs1:/tmp/cvs-serv14128/src Modified Files: plugin.h sonar.c sonar.h target.c target.h util.c util.h Log Message: Removed results_t and placed it all in target_t Index: plugin.h =================================================================== RCS file: /cvsroot/autosec/sonar/src/plugin.h,v retrieving revision 1.32 retrieving revision 1.33 diff -C2 -d -r1.32 -r1.33 *** plugin.h 6 Dec 2003 00:14:41 -0000 1.32 --- plugin.h 17 Jan 2004 03:12:31 -0000 1.33 *************** *** 29,33 **** #define DEFAULT_COUNT 1 #define DEFAULT_TIMEOUT 5000 ! #define API_VER 5 /** \name Error and Success Values --- 29,33 ---- #define DEFAULT_COUNT 1 #define DEFAULT_TIMEOUT 5000 ! #define API_VER 6 /** \name Error and Success Values *************** *** 104,107 **** --- 104,116 ---- #define PORT_UNFILTERED 7 + typedef struct stats_s stats_t; + + struct stats_s + { + unsigned int packets_sent; + unsigned int packets_recvd; + unsigned short ttl; + double rtt_min, rtt_max, rtt_avg; + }; typedef struct target_s target_t; *************** *** 113,118 **** /// user specified entry char *entry; - /// canonical hostname - //char *hostname; /// list of ports to scan int ports[65537]; --- 122,125 ---- *************** *** 125,148 **** /// number of ports in this host uint32_t num_ports; ! }; ! ! typedef struct result_s result_t; ! ! struct result_s ! { ! /// next result for plugins that return all results at once ! result_t *next; ! /// hostname ! char *hostname; ! /// ports state structure ! unsigned char ports[65537]; ! /// address information ! struct addrinfo *ainfo; ! /// round trip time ! double rtt; ! /// time to live ! unsigned int ttl; ! /// extra data? ! void *user_data; }; --- 132,137 ---- /// number of ports in this host uint32_t num_ports; ! /// statistics ! stats_t stats; }; *************** *** 225,229 **** int (*plugin_output) (char *msg, va_list * ap); /// output plugins result output function ! int (*plugin_results) (result_t *result); /// prints, via sonar_t->output(), line(s) to add to sonar_usage() --- 214,218 ---- int (*plugin_output) (char *msg, va_list * ap); /// output plugins result output function ! int (*plugin_results) (target_t *result, unsigned int num); /// prints, via sonar_t->output(), line(s) to add to sonar_usage() *************** *** 292,302 **** /// Pointer to sonar's output to console function (no timestamp) int (*output) (char *msg, ...); - - /// point to sonar's result collecting function - int (*results) (result_t *results, unsigned long num_records); - /// insert results into a linked list of results - int (*insert_results) (result_t *results, result_t *insertion); - /// free a results linked list - void (*free_results) (result_t *results); /// Pointer to usage function, for error reports --- 281,284 ---- *************** *** 307,310 **** --- 289,294 ---- /// Pointer to a function to drop root priveleges int (*dropprivs) (uid_t); + /// find a target, for updating + target_t *(*find_target) (struct sonar_s *this, struct addrinfo *ai); /// Name of file we are (for permissions checking) Index: sonar.c =================================================================== RCS file: /cvsroot/autosec/sonar/src/sonar.c,v retrieving revision 1.26 retrieving revision 1.27 diff -C2 -d -r1.26 -r1.27 *** sonar.c 10 Nov 2003 10:22:34 -0000 1.26 --- sonar.c 17 Jan 2004 03:12:32 -0000 1.27 *************** *** 95,104 **** mthis->resolve = &Host_serv; mthis->output = &sonar_rawoutput; - mthis->results = &sonar_result_hook; - mthis->insert_results = &insert_results; - mthis->free_results = &free_results; mthis->usage = &sonar_usage; mthis->getprivs = &sonar_getprivs; mthis->dropprivs = &sonar_dropprivs; // no targets yet ;) mthis->vectors = NULL; --- 95,102 ---- mthis->resolve = &Host_serv; mthis->output = &sonar_rawoutput; mthis->usage = &sonar_usage; mthis->getprivs = &sonar_getprivs; mthis->dropprivs = &sonar_dropprivs; + mthis->find_target = &sonar_find_target; // no targets yet ;) mthis->vectors = NULL; *************** *** 118,122 **** { ! int cur = 0, opt = 0; int plugin_selected = 0, i; target_t *first_target; --- 116,120 ---- { ! int cur = 0, opt = 0, ret; int plugin_selected = 0, i; target_t *first_target; *************** *** 124,127 **** --- 122,126 ---- float dif = 0; static char *plugin; + int n; struct poptOption optionsTable[] = { *************** *** 436,439 **** --- 435,439 ---- if(gettimeofday(&ts, NULL) != 0) err_ret("gettimeofday failed"); + for(i = 0; i < num_net; i++) { *************** *** 451,456 **** } if(err == PLUGIN_OK) /* the meaning of this has changed slightly, */ ! { /* it just means everything went alright, no errors */ ! } cur++; --- 451,456 ---- } if(err == PLUGIN_OK) /* the meaning of this has changed slightly, */ ! { /* it just means everything went alright, no errors */ ! } cur++; *************** *** 459,462 **** --- 459,472 ---- sleep(mthis.sCon.iScanDelay); } + for(n = 0; n < num_out; n++) + { + if(output_list[n].plug->selected && output_list[n].plug->ready) + { + /* let each plugin parse its own results */ + ret = output_list[n].plug->plugin_results(mthis.vectors, mthis.num_targets); + if(ret != PLUGIN_OK) + log_write(LOG_CONSOLE | LOG_SLOG, "output plugin \'%s\' had trouble writing output\n", output_list[i].plug->shortname); + } + } if(gettimeofday(&te, NULL) != 0) err_ret("gettimeofday failed"); *************** *** 597,603 **** } if(status != PLUGIN_OK) ! return SONAR_ERROR; } ! return status; } --- 607,613 ---- } if(status != PLUGIN_OK) ! return status; } ! return SONAR_OK; } *************** *** 608,636 **** * @todo Fix up plugins to use this function to report results, instead of printing out */ ! int sonar_result_hook(result_t *results, unsigned long num_records) ! { ! int i, ret = SONAR_OK; ! unsigned long j = 0; ! assert(results != NULL); ! ! /* go through the number of records */ ! /* we use '<=' here because we want a stub for hosts that did not return any info */ ! while(j <= num_records && results != NULL) ! { ! /* go through the output plugins and send it to the selected ones */ ! for(i=0; i < num_out; i++) ! { ! if(output_list[i].plug->selected && output_list[i].plug->ready) ! { ! /* let each plugin parse its own results */ ! ret = output_list[i].plug->plugin_results(results); ! if(ret != PLUGIN_OK) ! log_write(LOG_CONSOLE | LOG_SLOG, "output plugin \'%s\' had trouble writing output\n", output_list[i].plug->shortname); ! } ! } ! j++; ! results = results->next; ! } ! ! return ret; ! } --- 618,647 ---- * @todo Fix up plugins to use this function to report results, instead of printing out */ ! // ! // int sonar_result_hook(result_t *results, unsigned long num_records) ! // { ! // int i, ret = SONAR_OK; ! // unsigned long j = 0; ! // assert(results != NULL); ! // ! // /* go through the number of records */ ! // /* we use '<=' here because we want a stub for hosts that did not return any info */ ! // while(j <= num_records && results != NULL) ! // { ! // /* go through the output plugins and send it to the selected ones */ ! // for(i=0; i < num_out; i++) ! // { ! // if(output_list[i].plug->selected && output_list[i].plug->ready) ! // { ! // /* let each plugin parse its own results */ ! // ret = output_list[i].plug->plugin_results(results); ! // if(ret != PLUGIN_OK) ! // log_write(LOG_CONSOLE | LOG_SLOG, "output plugin \'%s\' had trouble writing output\n", output_list[i].plug->shortname); ! // } ! // } ! // j++; ! // results = results->next; ! // } ! // ! // return ret; ! // } Index: sonar.h =================================================================== RCS file: /cvsroot/autosec/sonar/src/sonar.h,v retrieving revision 1.26 retrieving revision 1.27 diff -C2 -d -r1.26 -r1.27 *** sonar.h 5 Dec 2003 22:47:21 -0000 1.26 --- sonar.h 17 Jan 2004 03:12:32 -0000 1.27 *************** *** 77,81 **** void sonar_status_report(sonar_t mthis); int sonar_rawoutput(char *msg, ...); - int sonar_result_hook(result_t *results, unsigned long num_records); int sonar_sweep(target_t *targets); void default_context(void *in); --- 77,80 ---- Index: target.c =================================================================== RCS file: /cvsroot/autosec/sonar/src/target.c,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -d -r1.8 -r1.9 *** target.c 5 Dec 2003 22:47:52 -0000 1.8 --- target.c 17 Jan 2004 03:12:32 -0000 1.9 *************** *** 28,31 **** --- 28,33 ---- #include "sonar.h" + #include <string.h> + extern sonar_t mthis; *************** *** 33,37 **** "\100$ autosec/sonar: $Id$"; ! static unsigned long check_netmask(target_t *target); static int parse_target_ports(char *ports, target_t **target); static int load_default_ports(target_t **target); --- 35,39 ---- "\100$ autosec/sonar: $Id$"; ! static unsigned long check_netmask(sonar_t *this, target_t *target); static int parse_target_ports(char *ports, target_t **target); static int load_default_ports(target_t **target); *************** *** 53,62 **** len = strlen(entry); ! // copy the entry over ! hostname = (char *) malloc(len + 1); ! if(!hostname) ! err_sys("sonar_add_target: malloc error"); ! memset((char *) hostname, 0, len + 1); ! strncpy(hostname, entry, len); ports = mask = hostname; --- 55,61 ---- len = strlen(entry); ! /* copy the entry over */ ! hostname = strndup(entry, len); ! ports = mask = hostname; *************** *** 100,115 **** if(target == NULL) err_sys("sonar_add_target: malloc error"); // start filling in needed values ! target->entry = (char *) malloc(len + 1); ! if(!target->entry) ! err_sys("sonar_add_target: malloc error"); ! memset(target->entry, 0,len + 1); ! strncpy(target->entry, entry, len); target->netmask = imask; //parse list of ports. num_ports = parse_target_ports(ports, &(target)); - // See if we have an ipv4 hostname, and wrap it - // call Wrapped host_serv so we dont have to check for errors. addr = Host_serv(hostname, NULL, 0, 0); --- 99,110 ---- if(target == NULL) err_sys("sonar_add_target: malloc error"); + memset(target, 0, sizeof(target_t)); + // start filling in needed values ! target->entry = hostname; /*strndup(entry, len);*/ target->netmask = imask; //parse list of ports. num_ports = parse_target_ports(ports, &(target)); // call Wrapped host_serv so we dont have to check for errors. addr = Host_serv(hostname, NULL, 0, 0); *************** *** 118,122 **** target->num_ports = num_ports; target->next = NULL; ! num_hosts = check_netmask(target); ent = this->vectors; --- 113,117 ---- target->num_ports = num_ports; target->next = NULL; ! num_hosts = check_netmask(this, target); ent = this->vectors; *************** *** 131,141 **** this->num_targets++; - /* XXX: Fix this, put it in target_t */ - this->num_ports += num_ports; - return SONAR_OK; } ! static unsigned long check_netmask(target_t *target) { unsigned long retval = 0, longtmp; --- 126,204 ---- this->num_targets++; return SONAR_OK; } ! /* find a target in the linked list by address info */ ! target_t *sonar_find_target(sonar_t *this, struct addrinfo *ai) ! { ! target_t *res = NULL; ! uint32_t start, end, longtmp; ! struct sockaddr_in *sa; ! ! if(!ai || !this) /* meaningless answer to a retarded question */ ! return NULL; ! if(!this->vectors) ! return NULL; ! ! res = this->vectors; ! ! if(res->addrinfo) ! { ! assert(res->addrinfo->ai_addr != NULL); ! switch(res->addrinfo->ai_family) ! { ! case PF_INET: ! sa = (struct sockaddr_in *) res->addrinfo->ai_addr; ! longtmp = ntohl(sa->sin_addr.s_addr); ! start = longtmp & (unsigned long) (0 - (1 << (32 - res->netmask))); ! end = longtmp | (unsigned long) ((1<<(32 - res->netmask)) - 1); ! /* if its in range, we have a match */ ! sa = (struct sockaddr_in *) ai->ai_addr; ! assert(sa != NULL); ! longtmp = ntohl(sa->sin_addr.s_addr); ! ! if(longtmp <= end && longtmp >= start) ! return res; ! break; ! default: ! log_write(LOG_DBUG, "IPv4 supported only\n"); ! return NULL; ! } ! if(memcmp(res->addrinfo, ai, sizeof(struct addrinfo)) == 0) ! return res; ! } ! while(res->next) ! { ! res = res->next; ! if(res->addrinfo) ! { ! assert(res->addrinfo->ai_addr != NULL); ! switch(res->addrinfo->ai_family) ! { ! case PF_INET: ! sa = (struct sockaddr_in *) res->addrinfo->ai_addr; ! longtmp = ntohl(sa->sin_addr.s_addr); ! start = longtmp & (unsigned long) (0 - (1 << (32 - res->netmask))); ! end = longtmp | (unsigned long) ((1<<(32 - res->netmask)) - 1); ! /* if its in range, we have a match */ ! sa = (struct sockaddr_in *) ai->ai_addr; ! assert(sa != NULL); ! longtmp = ntohl(sa->sin_addr.s_addr); ! ! if(longtmp <= end && longtmp >= start) ! return res; ! break; ! default: ! log_write(LOG_DBUG, "IPv4 supported only\n"); ! return NULL; ! } ! if(memcmp(res->addrinfo, ai, sizeof(struct addrinfo)) == 0) ! return res; ! } ! } ! return NULL; ! } ! ! static unsigned long check_netmask(sonar_t *this, target_t *target) { unsigned long retval = 0, longtmp; *************** *** 151,158 **** start = longtmp & (unsigned long) (0 - (1 << (32 - target->netmask))); if(((start << 24) >> 24) == 0) ! mthis.pCon.bBroadcast++; end = longtmp | (unsigned long) ((1<<(32 - target->netmask)) - 1); if(((end << 24) >> 24) == 0xff) ! mthis.pCon.bBroadcast++; if(start > end) err_msg("Invalid netmask\n"); --- 214,221 ---- start = longtmp & (unsigned long) (0 - (1 << (32 - target->netmask))); if(((start << 24) >> 24) == 0) ! this->pCon.bBroadcast++; end = longtmp | (unsigned long) ((1<<(32 - target->netmask)) - 1); if(((end << 24) >> 24) == 0xff) ! this->pCon.bBroadcast++; if(start > end) err_msg("Invalid netmask\n"); *************** *** 173,177 **** unsigned int ret = 0; ! if(!*target){ *target = malloc(sizeof(target_t)); if(!*target) --- 236,241 ---- unsigned int ret = 0; ! if(!*target) ! { *target = malloc(sizeof(target_t)); if(!*target) *************** *** 181,187 **** /* get default ports from /etc/services */ if(!ports) - { return load_default_ports(target); - } len = strlen(ports); --- 245,249 ---- *************** *** 199,207 **** } - log_write(LOG_DBUG, " clearing port list\n"); - //clear the port list - for(i = 0; i < 65537; i++) - (*target)->ports[i] = 0; - dest = strtok(ports, ","); --- 261,264 ---- *************** *** 238,242 **** while((se = getservent()) != NULL) { ! (*target)->ports[se->s_port] = 1; } endservent(); --- 295,299 ---- while((se = getservent()) != NULL) { ! (*target)->ports[se->s_port] = PORT_SCAN; } endservent(); *************** *** 248,260 **** int sonar_debug_targets(target_t *target) { ! int i = 0; while(target) { ! err_msg(" target debug:"); err_msg(" entry: %s", target->entry); err_msg(" hostname: %s", target->addrinfo->ai_canonname); err_msg(" ports:"); for(i = 0; i < 65536; i++) ! if(target->ports[i] == 1) printf("%d,",i); err_msg(""); --- 305,317 ---- int sonar_debug_targets(target_t *target) { ! int i = 0,n = 0; while(target) { ! err_msg(" target debug[%d]:", n++); err_msg(" entry: %s", target->entry); err_msg(" hostname: %s", target->addrinfo->ai_canonname); err_msg(" ports:"); for(i = 0; i < 65536; i++) ! if(target->ports[i] == PORT_SCAN) printf("%d,",i); err_msg(""); Index: target.h =================================================================== RCS file: /cvsroot/autosec/sonar/src/target.h,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** target.h 4 Nov 2003 23:48:40 -0000 1.5 --- target.h 17 Jan 2004 03:12:32 -0000 1.6 *************** *** 26,29 **** --- 26,30 ---- #define ALL_PORTS 65537 + target_t *sonar_find_target(sonar_t *this, struct addrinfo *ai); int sonar_add_target(sonar_t * this, char *entry); Index: util.c =================================================================== RCS file: /cvsroot/autosec/sonar/src/util.c,v retrieving revision 1.21 retrieving revision 1.22 diff -C2 -d -r1.21 -r1.22 *** util.c 10 Nov 2003 10:24:41 -0000 1.21 --- util.c 17 Jan 2004 03:12:32 -0000 1.22 *************** *** 457,637 **** return hosts2; } - - static int copy_ainfo(struct addrinfo **dest, struct addrinfo *src, int num) - { - int len; - struct addrinfo *first = NULL; - - /* for some reason, we didn't want to copy anything */ - if(num < 1) - return PLUGIN_OK; - - if(!(*dest)) - { - (*dest) = (struct addrinfo *) malloc(sizeof(struct addrinfo)); - if(!(*dest)) - err_sys("memory exhausted\n"); - memset((*dest), 0, sizeof(struct addrinfo)); - } - - first = *dest; - while(num > 0) - { - (*dest)->ai_flags = src->ai_flags; - (*dest)->ai_family = src->ai_family; - (*dest)->ai_socktype = src->ai_socktype; - (*dest)->ai_protocol = src->ai_protocol; - (*dest)->ai_addrlen = src->ai_addrlen; - (*dest)->ai_addr = (struct sockaddr *) malloc(src->ai_addrlen); - if(!(*dest)->ai_addr) - err_sys("memory exhausted\n"); - memset((*dest)->ai_addr, 0, src->ai_addrlen); - memcpy((*dest)->ai_addr, src->ai_addr, src->ai_addrlen); - if(src->ai_canonname) - { - len = strlen(src->ai_canonname); - (*dest)->ai_canonname = (char *) malloc(len+1); - if(!(*dest)->ai_canonname) - err_sys("Memory exhausted\n"); - memset((*dest)->ai_canonname, 0, len+1); - strncpy((*dest)->ai_canonname, src->ai_canonname, len); - } - else - { - (*dest)->ai_canonname = NULL; - } - (*dest)->ai_next = NULL; - if(--num > 0) /* we need more of the list */ - { - /* this only makes sense if there is more to copy */ - assert(src->ai_next != NULL); - (*dest)->ai_next = (struct addrinfo *) malloc(sizeof(struct addrinfo)); - if(!(*dest)->ai_next) - err_sys("memory exhausted\n"); - /* advance the lists */ - (*dest) = (*dest)->ai_next; - src = src->ai_next; - } - } - *dest = first; - return PLUGIN_OK; - } - - /** - * @fn insert_results(result_t *results, result_t *insertion) - * Insert a result into the results linked list - * @param results Results linked list - * @param insertion result_t structure to insert - * @return PLUGIN_OK if everything went well. - */ - int insert_results(result_t *results, result_t *insertion) - { - int len, i, match = 0; - size_t in_len, cmp_len; - result_t *wait = NULL; - assert(results != NULL); - assert(insertion != NULL); - assert(insertion->ainfo != NULL); - assert(insertion->ainfo->ai_addr != NULL); - - /* we are comparing by address data */ - in_len = insertion->ainfo->ai_addrlen; - while(results) - { - /* no records yet */ - if(!(results->ainfo)) - { - /* copy the address info */ - copy_ainfo(&(results->ainfo), insertion->ainfo, 1); - /* straight up copy it, no need to remalloc */ - /*memcpy(results, insertion, sizeof(result_t)); XXX: doing this memcpy will - overwrite the addrinfo, causing random segfaults later*/ - results->hostname = insertion->hostname; - memcpy(results->ports, insertion->ports, sizeof(unsigned char) * 65537); - results->rtt = insertion->rtt; - results->ttl = insertion->ttl; - results->user_data = insertion->user_data; - results->next = NULL; - /* we're done, dood */ - return PLUGIN_OK; - } - /* this crap should not happen */ - assert(results->ainfo != NULL); - assert(results->ainfo->ai_addr != NULL); - - /* do we have a match? */ - cmp_len = results->ainfo->ai_addrlen; - len = (in_len < cmp_len) ? in_len: cmp_len; - if(memcmp(results->ainfo->ai_addr, insertion->ainfo->ai_addr, len) == 0) - { - match++; - /* the only thing that should be different is the ports, so update that */ - for(i=0; i < 65537; i++) - { - /* port has a state we wish to update associated with it */ - if(results->ports[i] < PORT_OPENED) - results->ports[i] = insertion->ports[i]; - } - } - - wait = results; - results = results->next; - } - /* no addresses matched, so make a new one */ - if(!match) - { - int len; - /* we need a new record */ - /* rewind the list*/ - results = wait; - /* insert a new record */ - results->next = (result_t *) malloc(sizeof(result_t)); - if(!(results->next)) - err_sys("No memory\n"); - /* fast forward */ - results = results->next; - memset(results, 0, sizeof(result_t)); - /* remalloc the addrinfo so as not to get cleared out */ - copy_ainfo(&(results->ainfo), insertion->ainfo, 1); - results->hostname = insertion->hostname; - memcpy(results->ports, insertion->ports, sizeof(unsigned char) * 65537); - results->rtt = insertion->rtt; - results->ttl = insertion->ttl; - results->user_data = insertion->user_data; - len = strlen(insertion->hostname); - /*results->ainfo->ai_canonname = (char *) malloc(len +1); - if(!results->ainfo->ai_canonname) - err_sys("Memory exhausted\n"); - memset(results->ainfo->ai_canonname, 0, len + 1); - strncpy(results->ainfo->ai_canonname, insertion->hostname, len);*/ - results->next = NULL; - } - return PLUGIN_OK; - } - - #define xfree(x) free(x); x = NULL - - /** - * @fn free_results(result_t *results) - * Free the memory associated with a results linked list - * @param results Results linked list to free. - */ - void free_results(result_t *results) - { - result_t *wait; - - while(results != NULL) - { - wait = results; - - /* XXX: sometimes this dies */ - freeaddrinfo(results->ainfo); - - xfree(results->ainfo); - xfree(results->user_data); - xfree(results->hostname); - results = results->next; - /* for some reason, this dies */ - /*xfree(wait);*/ - } - } --- 457,458 ---- Index: util.h =================================================================== RCS file: /cvsroot/autosec/sonar/src/util.h,v retrieving revision 1.21 retrieving revision 1.22 diff -C2 -d -r1.21 -r1.22 *** util.h 10 Nov 2003 10:25:23 -0000 1.21 --- util.h 17 Jan 2004 03:12:32 -0000 1.22 *************** *** 70,75 **** void escape_shell(char **myfunc); target_t *randomize(target_t * in); - int insert_results(result_t *results, result_t *insertion); - void free_results(result_t *results); #endif --- 70,73 ---- |