~ocsinventory-dev/ocsinventory-server/stable-2.0

« back to all changes in this revision

Viewing changes to Apache/Ocsinventory/Server/Capacities/Snmp.pm

  • Committer: mortheres
  • Date: 2011-11-03 21:36:18 UTC
  • Revision ID: guillaume.protet@mortheres.info-20111103213618-askbthbod5mzeogw
Fix Bug #881406 about security issue due to snmp_com.txt file which is no longer used
Thanks to Remi Collet for his report

Show diffs side-by-side

added added

removed removed

Lines of Context:
60
60
  my $current_context = shift;
61
61
  my $resp = shift;
62
62
  my $select_ip_req;
63
 
  my $select_snmpcom_uri;
64
 
  my @DevicesToScan;
65
 
  my @SnmpCommunities;
 
63
  my $select_communities_req;
 
64
  my $select_deviceid_req;
 
65
  my @devicesToScan;
 
66
  my @communities;
66
67
 
67
68
  #Verify if SNMP is enable for this computer or in config
68
69
  my $snmpSwitch = &_get_snmp_switch($current_context);
78
79
  my $lanToDiscover = $current_context->{'PARAMS'}{'IPDISCOVER'}->{'TVALUE'};
79
80
  my $behaviour     = $current_context->{'PARAMS'}{'IPDISCOVER'}->{'IVALUE'};
80
81
  my $groupsParams  = $current_context->{'PARAMS_G'};
 
82
 
 
83
  #Only if communication is https 
 
84
  if ($current_context->{'APACHE_OBJECT'}->subprocess_env('https')) {
 
85
     
 
86
    $select_deviceid_req=$dbh->prepare('SELECT DEVICEID FROM hardware WHERE DEVICEID=?');
 
87
    $select_deviceid_req->execute($current_context->{'DEVICEID'});
 
88
 
 
89
    #Only if agent deviceid already exists in database
 
90
    if ($select_deviceid_req->fetchrow_hashref) {
81
91
 
82
 
  #If the computer is Ipdicover elected 
83
 
  if ($behaviour == 1 || $behaviour == 2) {
84
 
 
85
 
    #Getting non inventoried network devices for the agent subnet 
86
 
    $select_ip_req=$dbh->prepare('SELECT IP,MAC FROM netmap WHERE NETID=? AND mac NOT IN (SELECT DISTINCT(macaddr) FROM networks WHERE macaddr IS NOT NULL AND IPSUBNET=?)');
87
 
    $select_ip_req->execute($lanToDiscover,$lanToDiscover);
88
 
 
89
 
    while(my $row = $select_ip_req->fetchrow_hashref){
90
 
      push @DevicesToScan,$row;
91
 
    }
92
 
 
93
 
    if (@DevicesToScan) {
94
 
 
95
 
      #Adding devices informations in the XML
96
 
      foreach my $device (@DevicesToScan) {
97
 
        push @snmp,{
98
 
          'IPADDR'       => $device->{IP},
99
 
          'MACADDR'       => $device->{MAC},
100
 
          'TYPE'     => 'DEVICE',
101
 
        };
102
 
      }
103
 
 
104
 
      #Getting snmp_com.txt URI
105
 
      $select_snmpcom_uri=$dbh->prepare('SELECT TVALUE FROM config WHERE NAME="SNMP_URI"');
106
 
      $select_snmpcom_uri->execute();
107
 
 
108
 
      if (my $row = $select_snmpcom_uri->fetchrow_hashref) {
109
 
        #Adding snmp_com.txt URI in XML
110
 
        push @snmp,{
111
 
          'TYPE' => 'COMMUNITY',
112
 
          'SNMPCOM_LOC'  => $row->{'TVALUE'},
113
 
        };
114
 
      }
115
 
 
116
 
      #Final XML
117
 
      push @{ $resp->{'OPTION'} },{
118
 
        'NAME' => ['SNMP'],
119
 
        'PARAM' => \@snmp,
120
 
      };
121
 
    }
122
 
  }
 
92
      #If the computer is Ipdicover elected 
 
93
      if ($behaviour == 1 || $behaviour == 2) {
 
94
 
 
95
        #Getting non inventoried network devices for the agent subnet 
 
96
        $select_ip_req=$dbh->prepare('SELECT IP,MAC FROM netmap WHERE NETID=? AND mac NOT IN (SELECT DISTINCT(macaddr) FROM networks WHERE macaddr IS NOT NULL AND IPSUBNET=?)');
 
97
        $select_ip_req->execute($lanToDiscover,$lanToDiscover);
 
98
 
 
99
        while(my $row = $select_ip_req->fetchrow_hashref){
 
100
          push @devicesToScan,$row;
 
101
        }
 
102
 
 
103
        if (@devicesToScan) {
 
104
 
 
105
          #Adding devices informations in the XML
 
106
          foreach my $device (@devicesToScan) {
 
107
            push @snmp,{
 
108
              'IPADDR'       => $device->{IP},
 
109
              'MACADDR'       => $device->{MAC},
 
110
              'TYPE'     => 'DEVICE',
 
111
            };
 
112
          }
 
113
 
 
114
          #Getting snmp communities
 
115
          $select_communities_req = $dbh->prepare('SELECT VERSION,NAME,USERNAME,AUTHKEY,AUTHPASSWD FROM snmp_communities');
 
116
          $select_communities_req->execute();
 
117
 
 
118
          while(my $row = $select_communities_req->fetchrow_hashref){
 
119
            push @communities,$row;
 
120
          }
 
121
 
 
122
          if (@communities) {
 
123
            foreach my $community (@communities) {
 
124
              push @snmp,{
 
125
                'VERSION'       => $community->{'VERSION'}?$community->{'VERSION'}:'',
 
126
                'NAME'       => $community->{'NAME'}?$community->{'NAME'}:'',
 
127
                'USERNAME'     => $community->{'USERNAME'}?$community->{'USERNAME'}:'',
 
128
                'AUTHKEY'   => $community->{'AUTHKEY'}?$community->{'AUTHKEY'}:'',
 
129
                'AUTHPASSWD'   => $community->{'AUTHPASSWD'}?$community->{'AUTHPASSWD'}:'',
 
130
                'TYPE'   => 'COMMUNITY',
 
131
              };
 
132
            }
 
133
          }
 
134
 
 
135
          #Final XML
 
136
          push @{ $resp->{'OPTION'} },{
 
137
            'NAME' => ['SNMP'],
 
138
            'PARAM' => \@snmp,
 
139
          };
 
140
        }
 
141
      }
 
142
    } else { &_log(104,'snmp',"error: agent must have a deviceid in database !!"); }
 
143
  } else { &_log(103,'snmp',"error: agent must communicate using https to be able to get SNMP communities !!"); } 
123
144
}
124
145
 
125
146
sub snmp_handler{
126
 
 
127
147
  my $current_context = shift;
128
148
 
129
149
  #Verify if SNMP is enable for this computer or in config