OpenVAS Scanner 23.32.3
nasl_http.c
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Greenbone AG
2 * SPDX-FileCopyrightText: 2002-2004 Tenable Network Security
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 */
6
7#include "nasl_http.h"
8
9#include "../misc/plugutils.h" /* plug_get_host_fqdn */
10#include "../misc/user_agent.h" /* for user_agent_get */
11#include "exec.h"
12#include "nasl_debug.h"
13#include "nasl_func.h"
14#include "nasl_global_ctxt.h"
15#include "nasl_lex_ctxt.h"
16#include "nasl_socket.h"
17#include "nasl_tree.h"
18#include "nasl_var.h"
19
20#include <ctype.h> /* for isspace */
21#include <glib.h>
22#include <gvm/base/prefs.h> /* for prefs_get */
23#include <gvm/util/kb.h> /* for kb_item_get_str */
24#include <string.h> /* for strlen */
25
26#undef G_LOG_DOMAIN
30#define G_LOG_DOMAIN "lib nasl"
31
32/*-----------------[ http_* functions ]-------------------------------*/
33
36{
37 return nasl_open_sock_tcp_bufsz (lexic, 65536);
38}
39
42{
43 return nasl_close_socket (lexic);
44}
45
46static char *
47build_encode_URL (char *method, char *path, char *name, char *httpver)
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}
61
62static tree_cell *
63_http_req (lex_ctxt *lexic, char *keyword)
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}
173
174/*
175 * Syntax :
176 *
177 * http_get(port:<port>, item:<item>);
178 *
179 */
180tree_cell *
182{
183 return _http_req (lexic, "GET");
184}
185
186/*
187 * Syntax :
188 *
189 * http_head(port:<port>, item:<item>);
190 *
191 */
192tree_cell *
194{
195 return _http_req (lexic, "HEAD");
196}
197
198/*
199 * Syntax :
200 * http_post(port:<port>, item:<item>)
201 */
202tree_cell *
204{
205 return _http_req (lexic, "POST");
206}
207
208/*
209 * http_delete(port:<port>, item:<item>)
210 */
211tree_cell *
213{
214 return _http_req (lexic, "DELETE");
215}
216
217/*
218 * http_put(port:<port>, item:<item>, data:<data>)
219 */
220tree_cell *
222{
223 return _http_req (lexic, "PUT");
224}
225
226/*-------------------[ cgibin() ]--------------------------------*/
227
228tree_cell *
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}
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition nasl_debug.c:105
tree_cell * http_close_socket(lex_ctxt *lexic)
Definition nasl_http.c:41
tree_cell * http_head(lex_ctxt *lexic)
Definition nasl_http.c:193
static tree_cell * _http_req(lex_ctxt *lexic, char *keyword)
Definition nasl_http.c:63
tree_cell * http_put(lex_ctxt *lexic)
Definition nasl_http.c:221
tree_cell * http_open_socket(lex_ctxt *lexic)
Definition nasl_http.c:35
tree_cell * cgibin(lex_ctxt *lexic)
Definition nasl_http.c:229
tree_cell * http_get(lex_ctxt *lexic)
Definition nasl_http.c:181
tree_cell * http_delete(lex_ctxt *lexic)
Definition nasl_http.c:212
static char * build_encode_URL(char *method, char *path, char *name, char *httpver)
Definition nasl_http.c:47
tree_cell * http_post(lex_ctxt *lexic)
Definition nasl_http.c:203
const char * name
Definition nasl_init.c:439
struct struct_lex_ctxt lex_ctxt
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 * nasl_open_sock_tcp_bufsz(lex_ctxt *lexic, int bufsz)
tree_cell * nasl_close_socket(lex_ctxt *lexic)
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
Header file for module plugutils.
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
Header file: user agent functions prototypes.