OpenVAS Scanner 23.32.3
nasl_http.c File Reference
#include "nasl_http.h"
#include "../misc/plugutils.h"
#include "../misc/user_agent.h"
#include "exec.h"
#include "nasl_debug.h"
#include "nasl_func.h"
#include "nasl_global_ctxt.h"
#include "nasl_lex_ctxt.h"
#include "nasl_socket.h"
#include "nasl_tree.h"
#include "nasl_var.h"
#include <ctype.h>
#include <glib.h>
#include <gvm/base/prefs.h>
#include <gvm/util/kb.h>
#include <string.h>
Include dependency graph for nasl_http.c:

Go to the source code of this file.

Macros

#define G_LOG_DOMAIN   "lib nasl"
 GLib log domain.

Functions

tree_cellhttp_open_socket (lex_ctxt *lexic)
tree_cellhttp_close_socket (lex_ctxt *lexic)
static char * build_encode_URL (char *method, char *path, char *name, char *httpver)
static tree_cell_http_req (lex_ctxt *lexic, char *keyword)
tree_cellhttp_get (lex_ctxt *lexic)
tree_cellhttp_head (lex_ctxt *lexic)
tree_cellhttp_post (lex_ctxt *lexic)
tree_cellhttp_delete (lex_ctxt *lexic)
tree_cellhttp_put (lex_ctxt *lexic)
tree_cellcgibin (lex_ctxt *lexic)

Macro Definition Documentation

◆ G_LOG_DOMAIN

#define G_LOG_DOMAIN   "lib nasl"

GLib log domain.

Definition at line 30 of file nasl_http.c.

Function Documentation

◆ _http_req()

tree_cell * _http_req ( lex_ctxt * lexic,
char * keyword )
static

Definition at line 63 of file nasl_http.c.

64{
65 tree_cell *retc;
66 char *request, *auth, tmp[32];
67 char *item = get_str_var_by_name (lexic, "item");
68 char *data = get_str_var_by_name (lexic, "data");
69 int port = get_int_var_by_name (lexic, "port", -1);
70 struct script_infos *script_infos = lexic->script_infos;
71 int ver;
72 kb_t kb;
73
74 if (item == NULL || port < 0)
75 {
76 nasl_perror (lexic,
77 "Error : http_* functions have the following syntax :\n");
78 nasl_perror (lexic, "http_*(port:<port>, item:<item> [, data:<data>]\n");
79 return NULL;
80 }
81
82 if (port <= 0 || port > 65535)
83 {
84 nasl_perror (lexic, "http_req: invalid value %d for port parameter\n",
85 port);
86 return NULL;
87 }
88
90
91 g_snprintf (tmp, sizeof (tmp), "http/%d", port);
92 ver = kb_item_get_int (kb, tmp);
93
94 if ((ver <= 0) || (ver == 11))
95 {
96 char *hostname, *ua, *hostheader, *url;
97
99 if (hostname == NULL)
100 return NULL;
101
102 if ((user_agent_get (lexic->script_infos->ipc_context, &ua) == -2)
104 {
105 g_message ("Not possible to send the User Agent to the host process. "
106 "Invalid IPC context");
107 }
108 /* Servers should not have a problem with port 80 or 443 appended.
109 * RFC2616 allows to omit the port in which case the default port for
110 * that service is assumed.
111 * However, some servers like IIS/OWA wrongly respond with a "404"
112 * instead of a "200" in case the port is appended. Because of this,
113 * ports 80 and 443 are not appended.
114 */
115 if (port == 80 || port == 443)
116 hostheader = g_strdup (hostname);
117 else
118 hostheader = g_strdup_printf ("%s:%d", hostname, port);
119
120 url = build_encode_URL (keyword, NULL, item, "HTTP/1.1");
121 request = g_strdup_printf ("%s\r\n\
122Connection: Close\r\n\
123Host: %s\r\n\
124Pragma: no-cache\r\n\
125Cache-Control: no-cache\r\n\
126User-Agent: %s\r\n\
127Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, image/png, */*\r\n\
128Accept-Language: en\r\n\
129Accept-Charset: iso-8859-1,*,utf-8\r\n",
130 url, hostheader, ua);
131 g_free (hostname);
132 g_free (hostheader);
133 g_free (ua);
134 g_free (url);
135 }
136 else
137 request = build_encode_URL (keyword, NULL, item, "HTTP/1.0\r\n");
138
139 g_snprintf (tmp, sizeof (tmp), "/tmp/http/auth/%d", port);
140 auth = kb_item_get_str (kb, tmp);
141 if (!auth)
142 auth = kb_item_get_str (kb, "http/auth");
143
144 if (auth)
145 {
146 char *authntmp = g_strconcat (request, auth, "\r\n", NULL);
147 g_free (request);
148 g_free (auth);
149 request = authntmp;
150 }
151 if (data)
152 {
153 char content_length[128], *data_tmp;
154
155 g_snprintf (content_length, sizeof (content_length),
156 "Content-Length: %zu\r\n\r\n", strlen (data));
157 data_tmp = g_strconcat (request, content_length, data, NULL);
158 g_free (request);
159 request = data_tmp;
160 }
161 else
162 {
163 char *no_data_tmp = g_strconcat (request, "\r\n", NULL);
164 g_free (request);
165 request = no_data_tmp;
166 }
167
169 retc->size = strlen (request);
170 retc->x.str_val = request;
171 return retc;
172}
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition nasl_debug.c:105
static char * build_encode_URL(char *method, char *path, char *name, char *httpver)
Definition nasl_http.c:47
char * get_str_var_by_name(lex_ctxt *, const char *)
Definition nasl_var.c:1118
long int get_int_var_by_name(lex_ctxt *, const char *, int)
Definition nasl_var.c:1101
tree_cell * alloc_typed_cell(int typ)
Definition nasl_tree.c:25
@ CONST_DATA
Definition nasl_tree.h:82
struct TC tree_cell
const char * hostname
char * plug_get_host_fqdn(struct script_infos *args)
Definition plugutils.c:291
kb_t plug_get_kb(struct script_infos *args)
Definition plugutils.c:1152
int size
Definition nasl_tree.h:99
union TC::@332262321161220155002104006201360276211317150140 x
char * str_val
Definition nasl_tree.h:103
struct ipc_context * ipc_context
Definition scanneraux.h:31
struct script_infos * script_infos
int user_agent_get(struct ipc_context *ipc_context, char **useragent)
Get user-agent.
Definition user_agent.c:114

References alloc_typed_cell(), build_encode_URL(), CONST_DATA, get_int_var_by_name(), get_str_var_by_name(), hostname, script_infos::ipc_context, nasl_perror(), plug_get_host_fqdn(), plug_get_kb(), struct_lex_ctxt::script_infos, TC::size, script_infos::standalone, TC::str_val, user_agent_get(), and TC::x.

Referenced by http_delete(), http_get(), http_head(), http_post(), and http_put().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ build_encode_URL()

char * build_encode_URL ( char * method,
char * path,
char * name,
char * httpver )
static

Definition at line 47 of file nasl_http.c.

48{
49 char *ret, *ret2;
50
51 if (path == NULL)
52 ret = g_strdup (name);
53 else
54 ret = g_strdup_printf ("%s/%s", path, name);
55
56 g_debug ("Request => %s", ret);
57 ret2 = g_strdup_printf ("%s %s %s", method, ret, httpver);
58 g_free (ret);
59 return ret2;
60}
const char * name
Definition nasl_init.c:439

References name.

Referenced by _http_req().

Here is the caller graph for this function:

◆ cgibin()

tree_cell * cgibin ( lex_ctxt * lexic)

Definition at line 229 of file nasl_http.c.

230{
231 const char *path = prefs_get ("cgi_path");
232 tree_cell *retc;
233
234 (void) lexic;
235 if (path == NULL)
236 path = "/cgi-bin:/scripts";
238 retc->x.str_val = g_strdup (path);
239 retc->size = strlen (path);
240
241 return retc;
242}

References alloc_typed_cell(), CONST_DATA, TC::size, TC::str_val, and TC::x.

Here is the call graph for this function:

◆ http_close_socket()

tree_cell * http_close_socket ( lex_ctxt * lexic)

Definition at line 41 of file nasl_http.c.

42{
43 return nasl_close_socket (lexic);
44}
tree_cell * nasl_close_socket(lex_ctxt *lexic)

References nasl_close_socket().

Here is the call graph for this function:

◆ http_delete()

tree_cell * http_delete ( lex_ctxt * lexic)

Definition at line 212 of file nasl_http.c.

213{
214 return _http_req (lexic, "DELETE");
215}
static tree_cell * _http_req(lex_ctxt *lexic, char *keyword)
Definition nasl_http.c:63

References _http_req().

Here is the call graph for this function:

◆ http_get()

tree_cell * http_get ( lex_ctxt * lexic)

Definition at line 181 of file nasl_http.c.

182{
183 return _http_req (lexic, "GET");
184}

References _http_req().

Referenced by plugin_do_run().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ http_head()

tree_cell * http_head ( lex_ctxt * lexic)

Definition at line 193 of file nasl_http.c.

194{
195 return _http_req (lexic, "HEAD");
196}

References _http_req().

Here is the call graph for this function:

◆ http_open_socket()

tree_cell * http_open_socket ( lex_ctxt * lexic)

Definition at line 35 of file nasl_http.c.

36{
37 return nasl_open_sock_tcp_bufsz (lexic, 65536);
38}
tree_cell * nasl_open_sock_tcp_bufsz(lex_ctxt *lexic, int bufsz)

References nasl_open_sock_tcp_bufsz().

Here is the call graph for this function:

◆ http_post()

tree_cell * http_post ( lex_ctxt * lexic)

Definition at line 203 of file nasl_http.c.

204{
205 return _http_req (lexic, "POST");
206}

References _http_req().

Here is the call graph for this function:

◆ http_put()

tree_cell * http_put ( lex_ctxt * lexic)

Definition at line 221 of file nasl_http.c.

222{
223 return _http_req (lexic, "PUT");
224}

References _http_req().

Here is the call graph for this function: