1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
6 * Alvaro Lopez Ortega <alvaro@alobbs.com>
7 * Stefan de Konink <stefan@konink.de>
9 * Copyright (C) 2001-2008 Alvaro Lopez Ortega
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of version 2 of the GNU General Public
13 * License as published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
26 #include "handler_virt.h"
27 #include "handler_avahi.h"
28 #include <cherokee/cherokee.h>
30 /* Plug-in initialization
32 * In this function you can use any of these:
33 * http_delete | http_get | http_post | http_put
35 * For a full list: cherokee_http_method_t
37 * It is what your handler to be implements.
40 PLUGIN_INFO_HANDLER_EASIEST_INIT (virt
, http_get
| http_post
);
43 /* Methods implementation
46 props_free (cherokee_handler_virt_props_t
*props
)
48 return cherokee_module_props_free_base (MODULE_PROPS(props
));
53 cherokee_handler_virt_configure (cherokee_config_node_t
*conf
, cherokee_server_t
*srv
, cherokee_module_props_t
**_props
)
56 cherokee_handler_virt_props_t
*props
;
58 /* Instance a new property object
61 if (*_props
== NULL
) {
62 CHEROKEE_NEW_STRUCT (n
, handler_virt_props
);
64 cherokee_handler_avahi_props_init_base (PROP_AVAHI(n
), MODULE_PROPS_FREE(props_free
));
66 /* Look at handler_virt.h
67 * This is an virt of configuration.
69 n
->authenticate
= true; /* by default we are secure! */
70 n
->read_only
= true; /* by default we are secure! */
72 *_props
= MODULE_PROPS(n
);
75 props
= PROP_VIRT(*_props
);
77 cherokee_config_node_foreach (i
, conf
) {
78 cherokee_config_node_t
*subconf
= CONFIG_NODE(i
);
80 if (equal_buf_str (&subconf
->key
, "authenticate")) {
81 props
->authenticate
= atoi(subconf
->val
.buf
);
84 if (equal_buf_str (&subconf
->key
, "read_only")) {
85 props
->read_only
= atoi(subconf
->val
.buf
);
93 return cherokee_handler_avahi_configure (conf
, srv
, _props
);
98 cherokee_handler_virt_init (cherokee_handler_virt_t
*hdl
)
104 cherokee_connection_t
*conn
= HANDLER_CONN(hdl
);
105 cherokee_buffer_init(&hdl
->user
);
106 cherokee_buffer_init(&hdl
->vm
);
108 #define XSL "default.xsl"
110 hdl
->action
= nothing
;
112 if (strncmp(next
, XSL
, sizeof(XSL
)) == 0) {
113 hdl
->action
= defaultxsl
;
117 cherokee_buffer_add (&conn
->pathinfo
,
118 conn
->request
.buf
+ conn
->web_directory
.len
,
119 conn
->request
.len
- conn
->web_directory
.len
);
121 this = conn
->pathinfo
.buf
+ 1;
122 next
= strchr(this, '/');
125 if ((!next
&& (this && (len
= strlen(this)) == 0)) || (next
&& ((len
= next
- this) == 0)) )
126 hdl
->action
= showall
;
128 cherokee_buffer_add (&hdl
->user
, this, len
);
131 if (HDL_VIRT_PROPS(hdl
)->authenticate
) {
132 if (!conn
->validator
||
134 (!cherokee_buffer_cmp_buf(&conn
->validator
->user
, &hdl
->user
) &&
135 !(isroot
= cherokee_buffer_cmp (&conn
->validator
->user
, "root", 4))))) {
136 hdl
->action
= nothing
; /* just in case */
137 conn
->error_code
= http_unauthorized
;
144 if (hdl
->action
== showall
) {
146 hdl
->action
= nothing
;
147 conn
->error_code
= http_unauthorized
;
150 return virt_build_page(hdl
);
156 hdl
->action
= showuservms
;
157 return virt_build_page(hdl
);
160 next
= strchr(this, '/');
162 if (!next
&& (this && (len
= strlen(this)) == 0) || (next
&& ((len
= next
- this) == 0)) ) {
163 hdl
->action
= showuservms
;
164 return virt_build_page(hdl
);
168 cherokee_buffer_add (&hdl
->vm
, this, len
);
171 hdl
->action
= domainGetXMLDesc
;
172 return virt_build_page(hdl
);
175 next
= strchr(this, '/');
177 if (!next
&& (this && (len
= strlen(this)) == 0) || (next
&& ((len
= next
- this) == 0)) ) {
178 hdl
->action
= domainGetXMLDesc
;
179 return virt_build_page(hdl
);
183 hdl
->action
= not_implemented
;
184 switch (conn
->header
.method
) {
186 if (strncmp(this, "virDomain", 9) == 0) {
187 if (strncmp(this+9, "Get", 3) == 0) {
188 if (strcmp(this+12, "ID") == 0) hdl
->action
= domainGetID
;
189 else if (strcmp(this+12, "Name") == 0) hdl
->action
= domainGetName
;
190 else if (strcmp(this+12, "MaxMemory") == 0) hdl
->action
= domainGetMaxMemory
;
191 else if (strcmp(this+12, "MaxVcpus") == 0) hdl
->action
= domainGetMaxVcpus
;
192 else if (strcmp(this+12, "OSType") == 0) hdl
->action
= domainGetOSType
;
193 else if (strcmp(this+12, "UUID") == 0) hdl
->action
= domainGetUUID
;
194 else if (strcmp(this+12, "UUIDString") == 0) hdl
->action
= domainGetUUIDString
;
195 else if (strcmp(this+12, "XMLDesc") == 0) hdl
->action
= domainGetXMLDesc
;
197 else if (strcmp(this+9, "Reboot") == 0) hdl
->action
= domainReboot
;
198 else if (strcmp(this+9, "Shutdown") == 0) hdl
->action
= domainShutdown
;
204 cherokee_post_get_len (&conn
->post
, &postl
);
206 if (postl
<= 0 || postl
>= (INT_MAX
-1)) {
207 conn
->error_code
= http_bad_request
;
211 if (strncmp(this, "virDomain", 9) == 0) {
212 if (strcmp(this+9, "AttachDevice") == 0) hdl
->action
= domainAttachDevice
;
213 if (strcmp(this+9, "DetachDevice") == 0) hdl
->action
= domainDetachDevice
;
222 if (hdl
->action
<= 0) {
223 conn
->error_code
= http_bad_request
;
228 return virt_build_page(hdl
);
232 cherokee_handler_virt_free (cherokee_handler_virt_t
*hdl
)
234 cherokee_buffer_mrproper (&hdl
->buffer
);
235 cherokee_buffer_mrproper (&hdl
->user
);
236 cherokee_buffer_mrproper (&hdl
->vm
);
242 cherokee_handler_virt_step (cherokee_handler_virt_t
*hdl
, cherokee_buffer_t
*buffer
)
244 cherokee_buffer_add_buffer (buffer
, &hdl
->buffer
);
245 return ret_eof_have_data
;
249 cherokee_handler_virt_add_headers (cherokee_handler_virt_t
*hdl
, cherokee_buffer_t
*buffer
)
251 cherokee_buffer_add_va (buffer
, "Content-Length: %d"CRLF
, hdl
->buffer
.len
);
253 if (hdl
->action
> xml
)
254 cherokee_buffer_add_str (buffer
, "Content-Type: application/xml"CRLF
);
256 cherokee_buffer_add_str (buffer
, "Content-Type: text/plain"CRLF
);
263 while_func_entries (cherokee_buffer_t *key, void *value, void *param) {
264 virConnectPtr conn = NULL;
265 cherokee_buffer_t uri = CHEROKEE_BUF_INIT;
266 cherokee_buffer_t *buf = (cherokee_buffer_t *)param;
268 cherokee_buffer_add_va (&uri, "xen://%s/", value);
270 if (HDL_VIRT_PROPS(hdl)->read_only == FALSE && !(conn = virConnectOpen (uri.buf))) {
271 if (!(conn = virConnectOpen (uri.buf))) {
276 if (!conn && !(conn = virConnectOpenReadOnly (uri.buf))) {
280 if (!(dom = virDomainLookupByName(conn, (const char *) key->buf))) {
283 char *xml = virDomainGetXMLDesc(dom, 0);
284 cherokee_buffer_add(buf, xml, strlen(xml));
286 virConnectClose(conn);
289 cherokee_buffer_mrproper(&uri);
295 virt_virt_function(cherokee_handler_virt_t
*hdl
, virDomainPtr dom
) {
296 cherokee_connection_t
*conn
= HANDLER_CONN(hdl
);
297 cherokee_buffer_t
*buf
= &HDL_AVAHI(hdl
)->buffer
;
299 switch (hdl
->action
) {
300 case domainAttachDevice
: {
303 cherokee_buffer_t post
= CHEROKEE_BUF_INIT
;
304 cherokee_post_get_len (&conn
->post
, &postl
);
305 cherokee_post_walk_read (&conn
->post
, &post
, (cuint_t
) postl
);
306 if ((result
= virDomainAttachDevice(dom
, (const char *) post
.buf
)) != 0)
307 conn
->error_code
= http_internal_error
;
309 cherokee_buffer_mrproper(&post
);
310 cherokee_buffer_add_long10(buf
, result
);
314 case domainDetachDevice
: {
317 cherokee_buffer_t post
= CHEROKEE_BUF_INIT
;
318 cherokee_post_get_len (&conn
->post
, &postl
);
319 cherokee_post_walk_read (&conn
->post
, &post
, (cuint_t
) postl
);
320 if ((result
= virDomainDetachDevice(dom
, (const char *) post
.buf
)) != 0) {
321 conn
->error_code
= http_internal_error
;
323 cherokee_buffer_mrproper(&post
);
324 cherokee_buffer_add_long10(buf
, result
);
329 cherokee_buffer_add_ulong10(buf
, virDomainGetID(dom
));
332 case domainGetMaxMemory
: {
333 cherokee_buffer_add_ulong10(buf
, virDomainGetMaxMemory (dom
));
336 case domainGetMaxVcpus
: {
337 cherokee_buffer_add_long10(buf
, virDomainGetMaxVcpus (dom
));
340 case domainGetName
: {
341 const char *name
= virDomainGetName (dom
);
342 cherokee_buffer_add(buf
, name
, strlen(name
));
345 case domainGetUUID
: {
346 unsigned char uuid
[VIR_UUID_BUFLEN
];
347 if (virDomainGetUUID(dom
, uuid
) == 0) {
348 cherokee_buffer_add_str(buf
, uuid
);
350 conn
->error_code
= http_internal_error
;
355 case domainGetUUIDString
: {
356 unsigned char uuid
[VIR_UUID_STRING_BUFLEN
];
357 if (virDomainGetUUIDString(dom
, uuid
) == 0) {
358 cherokee_buffer_add_str(buf
, uuid
);
360 conn
->error_code
= http_internal_error
;
367 cherokee_buffer_add_long10(buf
, virDomainReboot (dom
, 0));
371 case domainShutdown
: {
372 cherokee_buffer_add_long10(buf
, virDomainShutdown (dom
));
377 case domainGetOSType
: {
378 char *ostype
= virDomainGetOSType(dom
);
379 cherokee_buffer_add(buf
, ostype
, strlen(ostype
));
384 case domainGetXMLDesc
: {
385 char *xml
= virDomainGetXMLDesc(dom
, 0);
386 cherokee_buffer_add(buf
, xml
, strlen(xml
));
396 virt_virt_do(cherokee_handler_virt_t
*hdl
, cherokee_buffer_t
*domu
, cherokee_buffer_t
*uri
)
398 cherokee_connection_t
*conn
= HANDLER_CONN(hdl
);
400 ret_t ret
= ret_error
;
401 virConnectPtr virConn
= NULL
;
404 if (HDL_VIRT_PROPS(hdl
)->read_only
== FALSE
)
405 virConn
= virConnectOpen (uri
->buf
);
407 if (!virConn
&& !(virConn
= virConnectOpenReadOnly (uri
->buf
))) {
408 conn
->error_code
= http_service_unavailable
;
413 if (!(dom
= virDomainLookupByName(virConn
, domu
->buf
))) {
414 conn
->error_code
= http_not_found
;
416 ret
= virt_virt_function(hdl
, dom
);
418 virConnectClose(virConn
);
424 virt_while (cherokee_buffer_t
*key
, void *value
, void *param
) {
425 cherokee_handler_virt_t
* hdl
= param
;
426 // cherokee_buffer_t uri = CHEROKEE_BUF_INIT;
427 // cherokee_buffer_add_va (&uri, "xen://%s/", ((cherokee_buffer_t *) value)->buf);
428 // virt_virt_do((cherokee_handler_virt_t *) param, key, &uri);
429 // cherokee_buffer_mrproper(&uri);
431 cherokee_buffer_add_va (&hdl
->buffer
, "<domain><name>%s</name></domain>", key
->buf
);
437 virt_while_user (cherokee_buffer_t
*key
, void *value
, void *param
) {
438 cherokee_handler_virt_t
*hdl
= param
;
439 if (key
->len
> hdl
->user
.len
&& key
->buf
[hdl
->user
.len
] == '_' && strncmp(key
->buf
, hdl
->user
.buf
, hdl
->user
.len
) == 0)
440 return virt_while (key
, value
, param
);
446 virt_build_page (cherokee_handler_virt_t
*hdl
)
449 cherokee_connection_t
*conn
= HANDLER_CONN(hdl
);
451 if (hdl
->action
> xml
)
452 cherokee_buffer_add_str (&hdl
->buffer
, "<?xml version=\"1.0\"?>\n<?xml-stylesheet type=\"text/xsl\" href=\"/default.xsl\"?>\n");
455 /* First, block the event loop */
456 avahi_threaded_poll_lock(HDL_AVAHI_PROPS(hdl
)->threaded_poll
);
457 switch (hdl
->action
) {
459 cherokee_buffer_add_str (&hdl
->buffer
, "<domains>");
460 cherokee_avl_while(&HDL_AVAHI_PROPS(hdl
)->entries
, virt_while
, hdl
, NULL
, NULL
);
461 cherokee_buffer_add_str (&hdl
->buffer
, "</domains>");
466 cherokee_buffer_add_str (&hdl
->buffer
, "<domains>");
467 cherokee_avl_while(&HDL_AVAHI_PROPS(hdl
)->entries
, virt_while_user
, hdl
, NULL
, NULL
);
468 cherokee_buffer_add_str (&hdl
->buffer
, "</domains>");
473 cherokee_buffer_t domu
= CHEROKEE_BUF_INIT
;
474 cherokee_buffer_t uri
= CHEROKEE_BUF_INIT
;
475 cherokee_buffer_t
*hostname
= NULL
;
476 cherokee_buffer_add_va (&domu
, "%s_%s", hdl
->user
.buf
, hdl
->vm
.buf
);
477 if ((ret
= cherokee_avl_get_ptr(&HDL_AVAHI_PROPS(hdl
)->entries
, domu
.buf
, (void **) &hostname
)) == ret_error
) {
478 hdl
->action
= nothing
;
479 conn
->error_code
= http_not_found
;
482 cherokee_buffer_add_va (&uri
, "xen://%s/", hostname
->buf
);
483 ret
= virt_virt_do(hdl
, &domu
, &uri
);
484 cherokee_buffer_mrproper(&domu
);
485 cherokee_buffer_mrproper(&uri
);
489 /* Finally, unblock the event loop */
490 avahi_threaded_poll_unlock(HDL_AVAHI_PROPS(hdl
)->threaded_poll
);
497 cherokee_handler_virt_new (cherokee_handler_t
**hdl
, cherokee_connection_t
*cnt
, cherokee_module_props_t
*props
)
500 CHEROKEE_NEW_STRUCT (n
, handler_virt
);
502 /* Init the base class
505 cherokee_handler_init_base(HANDLER(n
), cnt
, HANDLER_PROPS(props
), PLUGIN_INFO_HANDLER_PTR(virt
));
507 MODULE(n
)->init
= (handler_func_init_t
) cherokee_handler_virt_init
;
508 MODULE(n
)->free
= (module_func_free_t
) cherokee_handler_virt_free
;
509 HANDLER(n
)->step
= (handler_func_step_t
) cherokee_handler_virt_step
;
510 HANDLER(n
)->add_headers
= (handler_func_add_headers_t
) cherokee_handler_virt_add_headers
;
512 HANDLER(n
)->support
= hsupport_length
| hsupport_range
;
514 ret
= cherokee_buffer_init (&n
->buffer
);
515 if (unlikely(ret
!= ret_ok
))
518 ret
= cherokee_buffer_ensure_size (&n
->buffer
, 4*1024);
519 if (unlikely(ret
!= ret_ok
))