Cherokee module for HandlerVirt
[handlervirt.git] / handler_virt.c
blob2eee157da76004765d4beb3e201a328ff69532b4
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 /* Cherokee
5 * Authors:
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
23 * USA
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
45 static ret_t
46 props_free (cherokee_handler_virt_props_t *props)
48 return cherokee_module_props_free_base (MODULE_PROPS(props));
52 ret_t
53 cherokee_handler_virt_configure (cherokee_config_node_t *conf, cherokee_server_t *srv, cherokee_module_props_t **_props)
55 cherokee_list_t *i;
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);
90 /* Init base class
93 return cherokee_handler_avahi_configure (conf, srv, _props);
97 ret_t
98 cherokee_handler_virt_init (cherokee_handler_virt_t *hdl)
100 int isroot = false;
101 int len;
102 char *this, *next;
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;
114 return ret_ok;
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;
127 else {
128 cherokee_buffer_add (&hdl->user, this, len);
131 if (HDL_VIRT_PROPS(hdl)->authenticate) {
132 if (!conn->validator ||
133 (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;
138 return ret_error;
140 } else {
141 isroot = true;
144 if (hdl->action == showall) {
145 if (!isroot) {
146 hdl->action = nothing;
147 conn->error_code = http_unauthorized;
148 return ret_error;
149 } else {
150 return virt_build_page(hdl);
155 if (!next) {
156 hdl->action = showuservms;
157 return virt_build_page(hdl);
158 } else {
159 this = next + 1;
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);
170 if (!next) {
171 hdl->action = domainGetXMLDesc;
172 return virt_build_page(hdl);
173 } else {
174 this = next + 1;
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) {
185 case http_get:
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;
200 break;
202 case http_post: {
203 off_t postl;
204 cherokee_post_get_len (&conn->post, &postl);
206 if (postl <= 0 || postl >= (INT_MAX-1)) {
207 conn->error_code = http_bad_request;
208 return ret_error;
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;
215 break;
218 default:
219 return ret_error;
222 if (hdl->action <= 0) {
223 conn->error_code = http_bad_request;
224 return ret_error;
228 return virt_build_page(hdl);
231 ret_t
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);
238 return ret_ok;
241 ret_t
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;
248 ret_t
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);
255 else
256 cherokee_buffer_add_str (buffer, "Content-Type: text/plain"CRLF);
258 return ret_ok;
262 static ret_t
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))) {
272 return ret_error;
276 if (!conn && !(conn = virConnectOpenReadOnly (uri.buf))) {
277 return ret_error;
278 } else {
279 virDomainPtr dom;
280 if (!(dom = virDomainLookupByName(conn, (const char *) key->buf))) {
281 return ret_error;
282 } else {
283 char *xml = virDomainGetXMLDesc(dom, 0);
284 cherokee_buffer_add(buf, xml, strlen(xml));
286 virConnectClose(conn);
289 cherokee_buffer_mrproper(&uri);
291 return ret_ok;
294 static ret_t
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: {
301 off_t postl;
302 int result;
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);
311 break;
314 case domainDetachDevice: {
315 off_t postl;
316 int result;
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);
325 break;
328 case domainGetID: {
329 cherokee_buffer_add_ulong10(buf, virDomainGetID(dom));
330 break;
332 case domainGetMaxMemory: {
333 cherokee_buffer_add_ulong10(buf, virDomainGetMaxMemory (dom));
334 break;
336 case domainGetMaxVcpus: {
337 cherokee_buffer_add_long10(buf, virDomainGetMaxVcpus (dom));
338 break;
340 case domainGetName: {
341 const char *name = virDomainGetName (dom);
342 cherokee_buffer_add(buf, name, strlen(name));
343 break;
345 case domainGetUUID: {
346 unsigned char uuid[VIR_UUID_BUFLEN];
347 if (virDomainGetUUID(dom, uuid) == 0) {
348 cherokee_buffer_add_str(buf, uuid);
349 } else {
350 conn->error_code = http_internal_error;
351 return ret_error;
353 break;
355 case domainGetUUIDString: {
356 unsigned char uuid[VIR_UUID_STRING_BUFLEN];
357 if (virDomainGetUUIDString(dom, uuid) == 0) {
358 cherokee_buffer_add_str(buf, uuid);
359 } else {
360 conn->error_code = http_internal_error;
361 return ret_error;
363 break;
366 case domainReboot: {
367 cherokee_buffer_add_long10(buf, virDomainReboot (dom, 0));
368 break;
371 case domainShutdown: {
372 cherokee_buffer_add_long10(buf, virDomainShutdown (dom));
373 break;
377 case domainGetOSType: {
378 char *ostype = virDomainGetOSType(dom);
379 cherokee_buffer_add(buf, ostype, strlen(ostype));
380 free(ostype);
381 break;
384 case domainGetXMLDesc: {
385 char *xml = virDomainGetXMLDesc(dom, 0);
386 cherokee_buffer_add(buf, xml, strlen(xml));
387 free(xml);
388 break;
392 return ret_ok;
395 static ret_t
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;
409 return ret_error;
412 virDomainPtr dom;
413 if (!(dom = virDomainLookupByName(virConn, domu->buf))) {
414 conn->error_code = http_not_found;
415 } else {
416 ret = virt_virt_function(hdl, dom);
418 virConnectClose(virConn);
420 return ret_ok;
423 static ret_t
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);
433 return ret_ok;
436 static ret_t
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);
442 return ret_ok;
445 static ret_t
446 virt_build_page (cherokee_handler_virt_t *hdl)
448 ret_t ret;
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) {
458 case showall:
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>");
462 break;
464 case showuservms: {
465 void *param[3];
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>");
469 break;
472 default: {
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;
480 return ret;
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);
492 return ret_ok;
496 ret_t
497 cherokee_handler_virt_new (cherokee_handler_t **hdl, cherokee_connection_t *cnt, cherokee_module_props_t *props)
499 ret_t ret;
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))
516 return ret;
518 ret = cherokee_buffer_ensure_size (&n->buffer, 4*1024);
519 if (unlikely(ret != ret_ok))
520 return ret;
522 *hdl = HANDLER(n);
524 return ret_ok;