int nasl_load_or_parse(naslctxt* ctx, const char* name1, const char * basename, const char * cache_dir) { #ifdef ENABLE_PLUGIN_SERVER char name2[MAXPATHLEN]; struct stat st1, st2; if ( cache_dir != NULL ) { snprintf(name2, sizeof(name2), "%s/%s", cache_dir, basename); if (stat(name1, &st1) >= 0 && stat(name2, &st2) >= 0) { if (st2.st_mtime > st1.st_mtime) { memset(ctx, 0, sizeof(*ctx)); if ((ctx->tree = nasl_load_parsed_tree(name2)) != NULL) return 0; } } } #endif if (init_nasl_ctx(ctx, name1) < 0) return -1; if (naslparse(ctx)) { fprintf(stderr, "\nParse error at or near line %d\n", ctx->line_nb); nasl_clean_ctx(ctx); return -1; } #ifdef ENABLE_PLUGIN_SERVER if ( cache_dir != NULL ) { if (nasl_saved_parsed_tree(name2, ctx->tree) < 0) { fprintf(stderr, "Could not dump tree to %s\n", name2); if (unlink(name2) < 0) perror(name2); } } #endif return 0; }
int nasl_parse_and_dump(const char* name1, const char * basename, const char * cache_dir) { char name2[MAXPATHLEN]; struct stat st1, st2; naslctxt ctx; if ( cache_dir == NULL ) return -1; snprintf(name2, sizeof(name2), "%s/%s", cache_dir, basename); if (stat(name1, &st1) >= 0 && stat(name2, &st2) >= 0) { if (st2.st_mtime > st1.st_mtime) return 0; } if (init_nasl_ctx(&ctx, name1) < 0) return -1; if (naslparse(&ctx)) { fprintf(stderr, "\nParse error at or near line %d\n", ctx.line_nb); nasl_clean_ctx(&ctx); return -1; } if (nasl_saved_parsed_tree(name2, ctx.tree) < 0) { fprintf(stderr, "Could not dump tree to %s\n", name2); if (unlink(name2) < 0) perror(name2); } nasl_clean_ctx(&ctx); return 0; }
int execute_nasl_script(struct arglist * script_infos, const char* name, int mode) { naslctxt ctx; nasl_func *pf; int err = 0; tree_cell *ret; lex_ctxt *lexic; char old_dir[MAXPATHLEN+1]; char *newdir; char *old; tree_cell description; struct arglist* prefs = arg_get_value(script_infos, "preferences"); char *str; int to; srand48(getpid() + getppid() + (long)time(NULL)); old_dir[sizeof(old_dir) - 1] = '\0'; getcwd(old_dir, sizeof(old_dir) - 1); #if NASL_DEBUG > 2 nasl_trace_fp = stderr; #endif if((old = arg_get_value(script_infos, "script_name")) == NULL) arg_add_value(script_infos, "script_name", ARG_STRING, strlen(name), estrdup(name)); else { efree(&old); arg_set_value(script_infos, "script_name", strlen(name), estrdup(name)); } newdir = strrchr(name, '/'); if(newdir != NULL) { char dir[MAXPATHLEN+1]; char *s; dir[sizeof(dir) - 1] = '\0'; strncpy(dir, name, sizeof(dir) - 1); s = strrchr(dir, '/'); s[0] = '\0'; chdir(dir); } if (init_nasl_ctx(&ctx, name) < 0) { chdir(old_dir); return -1; } if (naslparse(&ctx)) { nasl_perror(NULL, "\nParse error at or near line %d\n", ctx.line_nb); nasl_clean_ctx(&ctx); chdir(old_dir); return -1; } #if NASL_DEBUG > 4 nasl_dump_tree(ctx.tree); #endif lexic = init_empty_lex_ctxt(); lexic->script_infos = script_infos; str = arg_get_value(prefs, "checks_read_timeout"); if( str != NULL ) to = atoi(str); else to = 5; if(to <= 0)to = 5; lexic->recv_timeout = to; init_nasl_library(lexic); if (! (mode & NASL_EXEC_PARSE_ONLY)) { bzero(&description, sizeof(description)); description.type = CONST_INT; description.x.i_val = (mode & NASL_EXEC_DESCR) != 0; add_named_var_to_ctxt(lexic, "description", &description); truc = (lex_ctxt*)ctx.tree; if ((ret = nasl_exec(lexic, ctx.tree)) == NULL) err = -1; else deref_cell(ret); if ((pf = get_func_ref_by_name(lexic, "on_exit")) != NULL) nasl_func_call(lexic, pf, NULL); } #if NASL_DEBUG > 2 { struct rusage ru; if (getrusage(RUSAGE_SELF, &ru) < 0) perror("getrusage"); else { nasl_perror(lexic, "rusage: utime=%d.%02d stime=%d.%02d minflt=%d majflt=%d\n", ru.ru_utime.tv_sec, ru.ru_utime.tv_usec / 10000, ru.ru_stime.tv_sec, ru.ru_stime.tv_usec / 10000, ru.ru_minflt, ru.ru_majflt); } } #endif #if NASL_DEBUG > 3 nasl_dump_tree(ctx.tree); #endif nasl_clean_ctx(&ctx); free_lex_ctxt(lexic); chdir(old_dir); return err; }