Changeset 206 for trunk/src


Ignore:
Timestamp:
Jan 8, 2009, 10:08:45 PM (16 years ago)
Author:
katerina
Message:

Fix for ticket #133 (improve port checking and its reporting to prelude).

Location:
trunk/src
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/sh_cat.c

    r192 r206  
    135135#ifdef SH_USE_PORTCHECK
    136136  { MSG_PORT_MISS,   SH_ERR_SEVERE,  EVENT, N_("msg=\"POLICY [ServiceMissing] %s\"")},
    137   { MSG_PORT_NEW,    SH_ERR_SEVERE,  EVENT, N_("msg=\"POLICY [ServiceNew] %s\" path=\"%s\" userid=\"%s\"")},
    138   { MSG_PORT_RESTART,SH_ERR_SEVERE,  EVENT, N_("msg=\"POLICY [ServiceRestarted] %s\" path=\"%s\" userid=\"%s\"")},
    139   { MSG_PORT_NEWPORT,SH_ERR_SEVERE,  EVENT, N_("msg=\"POLICY [ServicePortSwitch] %s\" path=\"%s\" userid=\"%s\"")},
     137  { MSG_PORT_NEW,    SH_ERR_SEVERE,  EVENT, N_("msg=\"POLICY [ServiceNew] %s\" path=\"%s\"  pid=\"%lu\" userid=\"%s\"")},
     138  { MSG_PORT_RESTART,SH_ERR_SEVERE,  EVENT, N_("msg=\"POLICY [ServiceRestarted] %s\" path=\"%s\" pid=\"%lu\" userid=\"%s\"")},
     139  { MSG_PORT_NEWPORT,SH_ERR_SEVERE,  EVENT, N_("msg=\"POLICY [ServicePortSwitch] %s\" path=\"%s\" pid=\"%lu\" userid=\"%s\"")},
    140140#endif
    141141
     
    464464#ifdef SH_USE_PORTCHECK
    465465  { MSG_PORT_MISS,   SH_ERR_SEVERE,  EVENT, N_("msg=<POLICY [ServiceMissing] %s>")},
    466   { MSG_PORT_NEW,    SH_ERR_SEVERE,  EVENT, N_("msg=<POLICY [ServiceNew] %s> path=<%s> userid=<%s>")},
    467   { MSG_PORT_RESTART,SH_ERR_SEVERE,  EVENT, N_("msg=<POLICY [ServiceRestarted] %s> path=<%s> userid=<%s>")},
    468   { MSG_PORT_NEWPORT,SH_ERR_SEVERE,  EVENT, N_("msg=<POLICY [ServicePortSwitch] %s> path=<%s> userid=<%s>")},
     466  { MSG_PORT_NEW,    SH_ERR_SEVERE,  EVENT, N_("msg=<POLICY [ServiceNew] %s> path=<%s> pid=<%lu> userid=<%s>")},
     467  { MSG_PORT_RESTART,SH_ERR_SEVERE,  EVENT, N_("msg=<POLICY [ServiceRestarted] %s> path=<%s> pid=<%lu> userid=<%s>")},
     468  { MSG_PORT_NEWPORT,SH_ERR_SEVERE,  EVENT, N_("msg=<POLICY [ServicePortSwitch] %s> path=<%s> pid=<%lu> userid=<%s>")},
    469469#endif
    470470
  • trunk/src/sh_port2proc.c

    r199 r206  
    311311/* returns the command and fills the 'user' array
    312312 */
    313 char * sh_port2proc_query(int proto, struct in_addr * saddr, int sport,
     313char * sh_port2proc_query(int proto, struct in_addr * saddr, int sport, unsigned long * pid,
    314314                          char * user, size_t userlen)
    315315{
     
    320320  else
    321321    fd = fopen("/proc/net/udp", "r");
     322
     323  *pid = 0;
    322324
    323325  if (fd)
     
    345347                        {
    346348                          fclose(fd);
     349                          *pid = (unsigned long) new->pid;
    347350                          if (new->path)
    348351                            {
     
    363366    }
    364367 err_out:
    365   sl_strlcpy(user, "0", userlen);
     368  sl_strlcpy(user, "-", userlen);
    366369  return sh_util_strdup("-");
    367370}
     
    750753
    751754char * sh_port2proc_query(int proto, struct in_addr * saddr, int sport,
    752                           char * user, size_t userlen)
     755                          unsigned long * pid, char * user, size_t userlen)
    753756{
    754757  int n, hash;
     
    756759  struct in_addr * haddr;
    757760  struct sock * s;
     761
     762  *pid = 0;
    758763 
    759764  for (xf = xfiles, n = 0; n < nxfiles; ++n, ++xf) {
     
    796801        struct sock_store try;
    797802       
     803        *pid = xf->xf_pid;
     804
    798805        try.pid  = xf->xf_pid;
    799806        try.path = NULL;
     
    862869
    863870char * sh_port2proc_query(int proto, struct in_addr * saddr, int sport,
    864                           char * user, size_t userlen)
     871                          unsigned long * pid, char * user, size_t userlen)
    865872{
    866873  (void) proto;
     
    868875  (void) sport;
    869876
     877  *pid = 0;
     878
    870879  sl_strlcpy(user, "-", userlen);
    871880  return sh_util_strdup("-");
  • trunk/src/sh_portcheck.c

    r180 r206  
    137137
    138138extern char * sh_port2proc_query(int proto, struct in_addr * saddr, int sport,
    139                                  char * user, size_t userlen);
     139                                 unsigned long * pid, char * user, size_t userlen);
    140140extern int sh_port2proc_prepare();
    141141
     
    515515        {
    516516          char * path;
     517          unsigned long qpid;
    517518          char   user[USER_MAX];
    518519
     
    523524                  inet_ntoa(haddr), port, SH_PROTO_STR(proto), service);
    524525#else
    525           path = sh_port2proc_query(proto, &haddr, port, user, sizeof(user));
     526          path = sh_port2proc_query(proto, &haddr, port, &qpid, user, sizeof(user));
    526527          SH_MUTEX_LOCK(mutex_thread_nolog);
    527528          sh_error_handle(sh_portchk_severity, FIL__, __LINE__, 0,
    528                           MSG_PORT_NEW, errbuf, path, user);
     529                          MSG_PORT_NEW, errbuf, path, qpid, user);
    529530          SH_MUTEX_UNLOCK(mutex_thread_nolog);
    530531          SH_FREE(path);
     
    538539        {
    539540          char * path;
     541          unsigned long qpid;
    540542          char   user[USER_MAX];
    541543
     
    545547          fprintf(stderr, _("service: %s\n"), errbuf);
    546548#else
    547           path = sh_port2proc_query(proto, &haddr, port, user, sizeof(user));
     549          path = sh_port2proc_query(proto, &haddr, port, &qpid, user, sizeof(user));
    548550          SH_MUTEX_LOCK(mutex_thread_nolog);
    549551          sh_error_handle(sh_portchk_severity, FIL__, __LINE__, 0,
    550                           MSG_PORT_RESTART, errbuf, path, user);
     552                          MSG_PORT_RESTART, errbuf, path, qpid, user);
    551553          SH_MUTEX_UNLOCK(mutex_thread_nolog);
    552554          SH_FREE(path);
     
    558560        {
    559561          char * path;
     562          unsigned long qpid;
    560563          char   user[USER_MAX];
    561564
     
    565568          fprintf(stderr, _("service: %s\n"), errbuf);
    566569#else
    567           path = sh_port2proc_query(proto, &haddr, port, user, sizeof(user));
     570          path = sh_port2proc_query(proto, &haddr, port, &qpid, user, sizeof(user));
    568571          SH_MUTEX_LOCK(mutex_thread_nolog);
    569572          sh_error_handle(sh_portchk_severity, FIL__, __LINE__, 0,
    570                           MSG_PORT_NEWPORT, errbuf, path, user);
     573                          MSG_PORT_NEWPORT, errbuf, path, qpid, user);
    571574          SH_MUTEX_UNLOCK(mutex_thread_nolog);
    572575          SH_FREE(path);
     
    585588        {
    586589          char * path;
     590          unsigned long qpid;
    587591          char   user[USER_MAX];
    588592
     
    593597                  inet_ntoa(haddr), port, SH_PROTO_STR(proto), check_services(port, proto));
    594598#else
    595           path = sh_port2proc_query(proto, &haddr, port, user, sizeof(user));
     599          path = sh_port2proc_query(proto, &haddr, port, &qpid, user, sizeof(user));
    596600          SH_MUTEX_LOCK(mutex_thread_nolog);
    597601          sh_error_handle(sh_portchk_severity, FIL__, __LINE__, 0,
    598                           MSG_PORT_NEW, errbuf, path, user);
     602                          MSG_PORT_NEW, errbuf, path, qpid, user);
    599603          SH_MUTEX_UNLOCK(mutex_thread_nolog);
    600604          SH_FREE(path);
     
    608612        {
    609613          char * path;
     614          unsigned long qpid;
    610615          char   user[USER_MAX];
    611616
     
    615620          fprintf(stderr, _("port   : %s\n"), errbuf);
    616621#else
    617           path = sh_port2proc_query(proto, &haddr, port, user, sizeof(user));
     622          path = sh_port2proc_query(proto, &haddr, port, &qpid, user, sizeof(user));
    618623          SH_MUTEX_LOCK(mutex_thread_nolog);
    619624          sh_error_handle(sh_portchk_severity, FIL__, __LINE__, 0,
    620                           MSG_PORT_RESTART, errbuf, path, user);
     625                          MSG_PORT_RESTART, errbuf, path, qpid, user);
    621626          SH_MUTEX_UNLOCK(mutex_thread_nolog);
    622627          SH_FREE(path);
  • trunk/src/sh_prelude.c

    r181 r206  
    696696        int ret;
    697697        long port;
    698         char *ptr, *new, *tmp, *ip, *srv, *end;
     698        char *ptr, *new, *tmp, *ip, *srv, *protocol, *end;
    699699        prelude_string_t *str;
    700700        idmef_address_t *address;
     
    704704        idmef_service_t *service;
    705705        idmef_source_t *source = idmef_alert_get_next_source(alert, NULL);
     706        struct passwd *pw;
     707#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
     708        struct passwd pwd;
     709        char buffer[SH_PWBUF_SIZE];
     710#endif
    706711
    707712        new = sh_util_strdup(msg);
     
    778783        if ( *ptr && *end == '\0' && port >= 0 && port < 65536) {
    779784
     785                char * tmpw;
     786
    780787                if ( ! source ) {
    781788                        ret = idmef_alert_new_source(alert, &source, IDMEF_LIST_APPEND);
    782789                        if ( ret < 0 ) {
    783                                 free(srv);
    784790                                SH_FREE( new );
    785791                                return ret;
     
    789795                ret = idmef_source_new_service(source, &service);
    790796                if ( ret < 0 ) {
    791                         free(srv);
    792797                        SH_FREE( new );
    793798                        return ret;
     
    795800
    796801                idmef_service_set_port(service, port);
     802
     803                ret = idmef_service_new_protocol(service, &str);
     804                if ( ret < 0 ) {
     805                        SH_FREE( new );
     806                        return ret;
     807                }
     808               
     809                ++tmp;
     810                if (*tmp) {
     811                        char * tmpw = tmp;
     812                        char tmpw_store;
     813                        while (*tmpw && !isblank((int) *tmpw)) ++tmpw;
     814                        tmpw_store = *tmpw; *tmpw = '\0';
     815                        protocol = strdup(tmp);
     816                        *tmpw = tmpw_store;
     817                        prelude_string_set_nodup(str, protocol);
     818                }
     819
    797820        }
    798821
     
    850873        SH_FREE( new );
    851874
    852         ptr = get_value(msg, _("user"), NULL);
     875        ptr = get_value(msg, _("userid"), NULL);
    853876
    854877        if ( ptr ) {
    855878
     879                idmef_user_id_t * user_id;
     880
    856881                ret = idmef_source_new_user(source, &user);
    857882                if ( ret < 0 ) {
     
    860885                }
    861886
    862                 ret = idmef_user_new_ident(user, &str);
    863                 if ( ret < 0 ) {
    864                         free(ptr);
    865                         return ret;
    866                 }
     887                idmef_user_set_category(user, IDMEF_USER_CATEGORY_APPLICATION);
     888               
     889                ret = idmef_user_new_user_id(user, &user_id, IDMEF_LIST_APPEND);
     890                if ( ret < 0 ) {
     891                        free(ptr);
     892                        return ret;
     893                }
     894               
     895                idmef_user_id_set_type(user_id, IDMEF_USER_ID_TYPE_CURRENT_USER);
     896
     897#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
     898                sh_getpwnam_r(ptr, &pwd, buffer, sizeof(buffer), &pw);
     899#else
     900                pw = sh_getpwnam(ptr);
     901#endif
     902                if ( pw )
     903                        idmef_user_id_set_number(user_id, pw->pw_uid);
     904
     905                ret = idmef_user_id_new_name(user_id, &str);
     906                if ( ret < 0 ) {
     907                        free(ptr);
     908                        return ret;
     909                }
    867910                prelude_string_set_nodup(str, ptr);
     911
    868912        }
    869913
    870914        ptr = get_value(msg, _("path"), NULL);
     915        tmp = get_value(msg, _("pid"), NULL);
    871916
    872917        if ( ptr ) {
     
    889934                prelude_string_set_nodup(str, ptr);
    890935
    891                 ptr = strrchr(ptr, '/');
    892                 if ( ptr ) {
     936               
     937                if ( NULL != strrchr(ptr, '/') ) {
    893938                        ret = idmef_process_new_name(process, &str);
    894939                        if ( ret == 0 ) {
     940                                ptr = strrchr(ptr, '/');
    895941                                prelude_string_set_dup(str, ptr + 1);
    896942                        }
    897                 }
    898         }
     943                } else {
     944                        ret = idmef_process_new_name(process, &str);
     945                        if ( ret == 0 ) {
     946                                prelude_string_set_dup(str, ptr);
     947                        }
     948                }
     949
     950                idmef_process_set_pid(process, strtoul(tmp, NULL, 0));
     951        }
     952
     953        if (tmp)
     954          free(tmp);
    899955
    900956        return 0;
Note: See TracChangeset for help on using the changeset viewer.