tree_cell* nasl_localtime(lex_ctxt* lexic) { tree_cell *retc; struct tm *ptm; time_t tictac; int utc; nasl_array *a; anon_nasl_var v; tictac = get_int_var_by_num(lexic, 0, 0); if (tictac == 0) tictac = time(NULL); utc = get_int_local_var_by_name(lexic, "utc", 0); if (utc) ptm = gmtime(&tictac); else ptm = localtime(&tictac); if (ptm == NULL) { nasl_perror(lexic, "localtime(%d,utc=%d): %s\n", tictac, utc, strerror(errno)); return NULL; } retc = alloc_typed_cell(DYN_ARRAY); retc->x.ref_val = a = emalloc(sizeof(nasl_array)); memset(&v, 0, sizeof(v)); v.var_type = VAR2_INT; v.v.v_int = ptm->tm_sec; add_var_to_array(a, "sec", &v); /* seconds */ v.v.v_int = ptm->tm_min; add_var_to_array(a, "min", &v); /* minutes */ v.v.v_int = ptm->tm_hour; add_var_to_array(a, "hour", &v); /* hours */ v.v.v_int = ptm->tm_mday; add_var_to_array(a, "mday", &v); /* day of the month */ v.v.v_int = ptm->tm_mon+1; add_var_to_array(a, "mon", &v); /* month */ v.v.v_int = ptm->tm_year+1900; add_var_to_array(a, "year", &v); /* year */ v.v.v_int = ptm->tm_wday; add_var_to_array(a, "wday", &v); /* day of the week */ v.v.v_int = ptm->tm_yday+1; add_var_to_array(a, "yday", &v); /* day in the year */ v.v.v_int = ptm->tm_isdst; add_var_to_array(a, "isdst", &v); /* daylight saving time */ return retc; }
tree_cell* nasl_make_array(lex_ctxt* lexic) { tree_cell *retc = NULL; int i, vi; anon_nasl_var *v, *v2; nasl_array *a; retc = alloc_tree_cell(0, NULL); retc->type = DYN_ARRAY; retc->x.ref_val = a = emalloc(sizeof(nasl_array)); i = vi = 0; while ((v = nasl_get_var_by_num(&lexic->ctx_vars, vi ++, 0)) != NULL) { v2 = nasl_get_var_by_num(&lexic->ctx_vars, vi ++, 0); if (v2 == NULL) { nasl_perror(lexic, "make_array: odd number (%d) of argument?\n", vi); break; } switch (v2->var_type) { case VAR2_INT: case VAR2_STRING: case VAR2_DATA: switch (v->var_type) { case VAR2_INT: add_var_to_list(a, v->v.v_int, v2); break; case VAR2_STRING: case VAR2_DATA: add_var_to_array(a, (char*)var2str(v) , v2); break; } break; case VAR2_UNDEF: default: nasl_perror(lexic, "make_array: bad value type %d for arg #%d\n", v2->var_type, vi); break; } } return retc; }
tree_cell * get_kb_list(lex_ctxt * lexic) { struct arglist * script_infos = lexic->script_infos; struct kb_item ** kb = plug_get_kb(script_infos); char * kb_mask = get_str_var_by_num(lexic, 0); tree_cell * retc; int num_elems = 0; nasl_array * a; struct kb_item * res, * top; if(kb_mask == NULL) { nasl_perror(lexic, "get_kb_list() usage : get_kb_list(<NameOfItem>)\n"); return NULL; } if(kb == NULL) { return NULL; } retc = alloc_tree_cell(0,NULL); retc->type = DYN_ARRAY; retc->x.ref_val = a = emalloc(sizeof(nasl_array)); top = res = kb_item_get_pattern(kb, kb_mask); while(res != NULL) { anon_nasl_var v; bzero(&v, sizeof(v)); if (lexic->authenticated || strncmp(res->name, SECRET_KB_PREFIX, sizeof(SECRET_KB_PREFIX) - 1) != 0) { if(res->type == KB_TYPE_INT) { v.var_type = VAR2_INT; v.v.v_int = res->v.v_int; add_var_to_array(a, res->name, &v); num_elems ++; } else if(res->type == KB_TYPE_STR) { v.var_type = VAR2_DATA; v.v.v_str.s_val = res->v.v_str; v.v.v_str.s_siz = strlen(res->v.v_str); add_var_to_array(a, res->name, &v); num_elems ++; } } #if NASL_DEBUG > 0 else nasl_perror(lexic, "get_kb_list: skipping protected KN entry %s\n", res->name); #endif res = res->next; } kb_item_get_all_free(top); if(num_elems == 0) { deref_cell(retc); return FAKE_CELL; } return retc; }
tree_cell * get_kb_list(lex_ctxt * lexic) { struct arglist * script_infos = lexic->script_infos; struct arglist * kb = arg_get_value(script_infos, "key"); /* gruik gruik */ char * kb_mask = get_str_var_by_num(lexic, 0); tree_cell * retc; int num_elems = 0; nasl_array * a; if(kb_mask == NULL) { nasl_perror(lexic, "get_kb_list() usage : get_kb_list(<NameOfItem>)\n"); return NULL; } if(kb == NULL) { return NULL; } retc = alloc_tree_cell(0,NULL); retc->type = DYN_ARRAY; retc->x.ref_val = a = emalloc(sizeof(nasl_array)); while(kb->next != NULL) { anon_nasl_var v; bzero(&v, sizeof(v)); if(str_match(kb->name, kb_mask, 1) != 0) { if(kb->type == ARG_INT) { v.var_type = VAR2_INT; v.v.v_int = (int)kb->value; add_var_to_array(a, kb->name, &v); num_elems ++; } else if(kb->type == ARG_STRING) { v.var_type = VAR2_DATA; v.v.v_str.s_val = kb->value; v.v.v_str.s_siz = strlen(kb->value); add_var_to_array(a, kb->name, &v); num_elems ++; } else if(kb->type == ARG_ARGLIST) { struct arglist * tmp = kb->value; if( tmp != NULL ) while (tmp->next != NULL ) { v.var_type = VAR2_DATA; v.v.v_str.s_val = tmp->value; v.v.v_str.s_siz = strlen(tmp->value); add_var_to_array(a, kb->name, &v); num_elems ++; tmp = tmp->next; } } } kb = kb->next; } if(num_elems == 0) { deref_cell(retc); return NULL; } return retc; }