18#include <gvm/base/logging.h>
28#define G_LOG_DOMAIN "lib nasl"
33#define SNMP_VERSION_1 0
38#define SNMP_VERSION_2c 1
40#define FD_STDERR_FLAG 1
41#define FD_STDOUT_FLAG 0
87 g_free (result->
name);
102 if (strcmp (proto,
"tcp") && strcmp (proto,
"udp") && strcmp (proto,
"tcp6")
103 && strcmp (proto,
"udp6"))
122 assert (result->
name);
126 memset (&v, 0,
sizeof (v));
131 memset (&v, 0,
sizeof v);
139 memset (&v, 0,
sizeof v);
158 memset (&v, 0,
sizeof (v));
163 memset (&v, 0,
sizeof v);
174#include <net-snmp/net-snmp-config.h>
175#include <net-snmp/net-snmp-includes.h>
177#define NASL_SNMP_GET SNMP_MSG_GET
178#define NASL_SNMP_GETNEXT SNMP_MSG_GETNEXT
191snmp_get (
struct snmp_session *session,
const char *
oid_str,
194 struct snmp_session *ss;
195 struct snmp_pdu *query, *response;
196 oid oid_buf[MAX_OID_LEN];
197 size_t oid_size = MAX_OID_LEN;
200 ss = snmp_open (session);
203 snmp_error (session, &status, &status, &(result->
name));
207 query = snmp_pdu_create (action);
208 read_objid (oid_str, oid_buf, &oid_size);
209 snmp_add_null_var (query, oid_buf, oid_size);
210 status = snmp_synch_response (ss, query, &response);
211 if (status != STAT_SUCCESS)
213 snmp_error (ss, &status, &status, &(result->
name));
219 if (response->errstat == SNMP_ERR_NOERROR)
221 struct variable_list *vars = response->variables;
222 size_t res_len = 0, buf_len = 0, res_len1 = 0, buf_len1 = 0;
224 netsnmp_ds_set_boolean (NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT,
226 sprint_realloc_value ((u_char **) &(result->
name), &buf_len, &res_len, 1,
227 vars->name, vars->name_length, vars);
228 sprint_realloc_objid ((u_char **) &(result->
oid_str), &buf_len1,
229 &res_len1, 1, vars->name, vars->name_length);
231 snmp_free_pdu (response);
235 result->
name = g_strdup (snmp_errstring (response->errstat));
236 snmp_free_pdu (response);
251 struct snmp_session session;
261 setenv (
"MIBS",
"", 1);
262 init_snmp (
"openvas");
263 snmp_sess_init (&session);
264 session.version = SNMP_VERSION_3;
265 session.peername = (
char *) request->
peername;
266 session.securityName = (
char *) request->username;
267 session.securityNameLen = strlen (session.securityName);
269 if (request->privpass)
270 session.securityLevel = SNMP_SEC_LEVEL_AUTHPRIV;
272 session.securityLevel = SNMP_SEC_LEVEL_AUTHNOPRIV;
273 if (request->authproto == 0)
275 session.securityAuthProto = usmHMACMD5AuthProtocol;
276 session.securityAuthProtoLen = USM_AUTH_PROTO_MD5_LEN;
280 session.securityAuthProto = usmHMACSHA1AuthProtocol;
281 session.securityAuthProtoLen = USM_AUTH_PROTO_SHA_LEN;
283 session.securityAuthKeyLen = USM_AUTH_KU_LEN;
284 if (generate_Ku (session.securityAuthProto, session.securityAuthProtoLen,
285 (u_char *) request->authpass, strlen (request->authpass),
286 session.securityAuthKey, &session.securityAuthKeyLen)
289 result->
name = g_strdup (
"generate_Ku: Error");
292 if (request->privpass)
294 if (request->privproto)
296 session.securityPrivProto = usmAESPrivProtocol;
297 session.securityPrivProtoLen = USM_PRIV_PROTO_AES_LEN;
301#ifdef NETSNMP_DISABLE_DES
303 g_strdup (
"DES not supported in this net-snmp version.");
306 session.securityPrivProto = usmDESPrivProtocol;
307 session.securityPrivProtoLen = USM_PRIV_PROTO_DES_LEN;
310 session.securityPrivKeyLen = USM_PRIV_KU_LEN;
311 if (generate_Ku (session.securityAuthProto, session.securityAuthProtoLen,
312 (
unsigned char *) request->privpass,
313 strlen (request->privpass), session.securityPrivKey,
314 &session.securityPrivKeyLen)
317 result->
name = g_strdup (
"generate_Ku: Error");
322 return snmp_get (&session, request->oid_str, request->action, result);
337 struct snmp_session session;
346 setenv (
"MIBS",
"", 1);
347 snmp_sess_init (&session);
348 session.version = request->
version;
349 session.peername = (
char *) request->
peername;
350 session.community = (u_char *) request->community;
351 session.community_len = strlen (request->community);
353 return snmp_get (&session, request->oid_str, request->action, result);
358#define NASL_SNMP_GET 0
359#define NASL_SNMP_GETNEXT 1
369 gchar **res_split, **res_aux;
371 res_aux = res_split = g_strsplit (result->
name,
"\n", 0);
381 if (*res_aux == NULL)
385 if ((pos = strchr (result->
name,
'\n')) != NULL)
391 *res_aux = g_strrstr (*res_aux,
"Reason: ");
394 g_free (result->
name);
395 result->
name = g_strdup (*res_aux + 8);
401 g_strfreev (res_split);
416 GString *
string = NULL;
418 string = g_string_new (
"");
424 bytes = read (fd, buf,
sizeof (buf));
428 g_string_append_len (
string, buf, bytes);
431 g_warning (
"snmpget: %s", strerror (errno));
432 g_string_free (
string, TRUE);
441 gchar **oid_and_name;
443 oid_and_name = g_strsplit (
string->str,
" ", 2);
444 result->
oid_str = g_strdup (oid_and_name[0]);
445 result->
name = g_strdup (oid_and_name[1]);
446 g_strfreev (oid_and_name);
451 g_string_free (
string, TRUE);
468 char *argv[8], *pos = NULL;
470 int sout = 0, serr = 0, ret;
481 setenv (
"MIBS",
"", 1);
488 argv[5] = g_strdup (request->
peername);
489 argv[6] = g_strdup (request->
oid_str);
491 ret = g_spawn_async_with_pipes (NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL,
492 NULL, NULL, NULL, &sout, &serr, &err);
499 g_warning (
"snmpget: %s", err ? err->message :
"Error");
511 if (result->
name && result->
name[0] !=
'\0')
519 g_free (result->
name);
525 if ((pos = strchr (result->
name,
'\0')) != NULL)
546 char *argv[18], *pos = NULL;
548 int sout = 0, serr = 0, ret;
558 setenv (
"MIBS",
"", 1);
564 argv[4] = g_strdup (request->
username);
566 argv[6] = g_strdup (request->
authpass);
568 argv[8] = request->
privpass ?
"authPriv" :
"authNoPriv";
570 argv[10] = request->
authproto ?
"SHA" :
"MD5";
573 argv[11] = g_strdup (request->
peername);
574 argv[12] = g_strdup (request->
oid_str);
576 argv[14] = request->
privproto ?
"AES" :
"DES";
578 argv[16] = g_strdup (request->
privpass);
583 argv[11] = g_strdup (request->
peername);
584 argv[12] = g_strdup (request->
oid_str);
588 ret = g_spawn_async_with_pipes (NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL,
589 NULL, NULL, NULL, &sout, &serr, &err);
599 g_warning (
"%s: %s", argv[0], err ? err->message :
"Error");
606 if (result->
name && result->
name[0] !=
'\0')
614 g_free (result->
name);
620 if ((pos = strchr (result->
name,
'\0')) != NULL)
642 static char *next_oid_str;
654 request->
oid_str = next_oid_str;
663 if (port < 0 || port > 65535)
674 g_snprintf (peername,
sizeof (peername),
"%s:%s:%d", proto,
684 if (result->
oid_str != NULL && g_strstr_len (result->
oid_str, 3,
"iso"))
686 next_oid_str = result->
oid_str + 2;
687 next_oid_str[0] =
'1';
688 result->
oid_str = g_strdup (next_oid_str);
690 else if (result->
oid_str != NULL)
691 next_oid_str = result->
oid_str;
730 const char *proto, *authproto, *privproto;
736 static char *next_oid_str;
749 request->
oid_str = next_oid_str;
763 if (port < 0 || port > 65535)
774 if (!privproto || !request->
privpass)
780 if (!strcasecmp (authproto,
"md5"))
782 else if (!strcasecmp (authproto,
"sha1"))
792 if (!strcasecmp (privproto,
"des"))
794 else if (!strcasecmp (privproto,
"aes"))
803 g_snprintf (peername,
sizeof (peername),
"%s:%s:%d", proto,
813 if (result->
oid_str != NULL && g_strstr_len (result->
oid_str, 3,
"iso"))
815 next_oid_str = result->
oid_str + 2;
816 next_oid_str[0] =
'1';
817 result->
oid_str = g_strdup (next_oid_str);
819 else if (result->
oid_str != NULL)
820 next_oid_str = result->
oid_str;
struct struct_lex_ctxt lex_ctxt
char * get_str_var_by_name(lex_ctxt *, const char *)
long int get_int_var_by_name(lex_ctxt *, const char *, int)
struct snmpv3_request * snmpv3_request_t
static int snmpv1v2c_get(const snmpv1v2_request_t request, snmp_result_t result)
SNMP v1 or v2c Get query value. snmpget cmd wrapper.
#define SNMP_VERSION_1
SNMP V1.
tree_cell * nasl_snmpv3_getnext(lex_ctxt *lexic)
static void destroy_snmp_result(snmp_result_t result)
#define SNMP_VERSION_2c
SNMP V2c.
tree_cell * nasl_snmpv1_get(lex_ctxt *lexic)
struct snmpv1v2_request * snmpv1v2_request_t
static tree_cell * array_from_snmp_result(int ret, const snmp_result_t result)
static tree_cell * nasl_snmpv3_get_action(lex_ctxt *lexic, u_char action)
static int snmpv3_get(const snmpv3_request_t request, snmp_result_t result)
SNMPv3 Get query value. snmpget cmd wrapper.
tree_cell * nasl_snmpv2c_get(lex_ctxt *lexic)
tree_cell * nasl_snmpv3_get(lex_ctxt *lexic)
static int proto_is_valid(const char *proto)
struct snmp_result * snmp_result_t
tree_cell * nasl_snmpv1_getnext(lex_ctxt *lexic)
tree_cell * nasl_snmpv2c_getnext(lex_ctxt *lexic)
static void parse_snmp_error(snmp_result_t result)
Parse the snmp error.
static int check_spwan_output(int fd, snmp_result_t result, int fd_flag)
Read data from a file descriptor.
static tree_cell * nasl_snmpv1v2c_get(lex_ctxt *lexic, int version, u_char action)
#define NASL_SNMP_GETNEXT
static tree_cell * array_from_snmp_error(int ret, const char *err)
Headers of an API for SNMP used by NASL scripts.
tree_cell * alloc_typed_cell(int typ)
int add_var_to_list(nasl_array *a, int i, const anon_nasl_var *v)
struct st_a_nasl_var anon_nasl_var
struct st_nasl_array nasl_array
char * plug_get_host_ip_str(struct script_infos *desc)
Header file for module plugutils.
union TC::@332262321161220155002104006201360276211317150140 x
SNMP Request struct for snmp v1 and v2c.
SNMP Request struct for snmp v3.
union st_a_nasl_var::@154137074032032170165360023270032033276061363156 v
Define a string struct for storing the response.
struct script_infos * script_infos