[pve-devel] r5446 - vncterm/pve2
svn-commits at proxmox.com
svn-commits at proxmox.com
Thu Jan 27 12:54:49 CET 2011
Author: dietmar
Date: 2011-01-27 12:54:49 +0100 (Thu, 27 Jan 2011)
New Revision: 5446
Modified:
vncterm/pve2/vncterm.c
Log:
connect to pvedaemon
Modified: vncterm/pve2/vncterm.c
===================================================================
--- vncterm/pve2/vncterm.c 2011-01-27 10:10:49 UTC (rev 5445)
+++ vncterm/pve2/vncterm.c 2011-01-27 11:54:49 UTC (rev 5446)
@@ -24,6 +24,9 @@
#include <stdio.h>
#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netdb.h>
#include <rfb/rfb.h>
#include <rfb/keysym.h>
#include <pty.h> /* for openpty and forkpty */
@@ -48,64 +51,113 @@
int use_x509 = 1;
-/* launch expernal script to verify credential */
-int
+static char *
+urlencode(char *buf, const char *value)
+{
+ static const char *hexchar = "0123456789abcdef";
+ char *p = buf;
+ int i;
+ int l = strlen(value);
+ for (i = 0; i < l; i++) {
+ char c = value[i];
+ if (('a' <= c && c <= 'z') ||
+ ('A' <= c && c <= 'Z') ||
+ ('0' <= c && c <= '9')) {
+ *p++ = c;
+ } else if (c == 32) {
+ *p++ = '+';
+ } else {
+ *p++ = '%';
+ *p++ = hexchar[c >> 4];
+ *p++ = hexchar[c & 15];
+ }
+ }
+ *p = 0;
+
+ return p;
+}
+
+int
pve_auth_verify(const char *username, const char *passwd)
{
- sigset_t oldmask, mask;
- int pid, status;
- char *args[20];
- char **parg;
- char *env[2];
- char **penv;
+ struct sockaddr_in server;
- char pwenv[1024];
+ int sfd = socket(AF_INET, SOCK_STREAM, 0);
+ if (sfd == -1) {
+ perror("pve_auth_verify: socket failed");
+ return -1;
+ }
- sprintf(pwenv, "PVE_PW_TICKET=%s", passwd);
+ struct hostent *he;
+ if ((he = gethostbyname("localhost")) == NULL) {
+ perror("pve_auth_verify: error resolving hostname");
+ goto err;
+ }
- const char *script = "/usr/sbin/pveum";
+ memcpy(&server.sin_addr, he->h_addr_list[0], he->h_length);
+ server.sin_family = AF_INET;
+ server.sin_port = htons(85);
- sigemptyset(&mask);
- sigaddset(&mask, SIGCHLD);
- sigprocmask(SIG_BLOCK, &mask, &oldmask);
+ if (connect(sfd, (struct sockaddr *)&server, sizeof(server))) {
+ perror("pve_auth_verify: error connecting to server");
+ goto err;
+ }
- /* try to launch pve authentification helper */
- pid = fork();
- if (pid == 0) {
- int open_max = sysconf(_SC_OPEN_MAX), i;
+ char buf[8192];
+ char form[8192];
- for (i = 0; i < open_max; i++) {
- if (i != STDIN_FILENO &&
- i != STDOUT_FILENO &&
- i != STDERR_FILENO) {
- close(i);
- }
- }
- parg = args;
- *parg++ = (char *)script;
- *parg++ = "auth";
- *parg++ = (char *)auth_path;
- *parg++ = (char *)username;
- *parg++ = (char *)auth_perm;
- *parg = NULL;
- penv = env;
- *penv++ = (char *)pwenv;
- *penv = NULL;
- execve(script, args, env);
- _exit(1);
- } else if (pid > 0) {
- while (waitpid(pid, &status, 0) != pid) {
- /* loop */
- }
- sigprocmask(SIG_SETMASK, &oldmask, NULL);
+ char *p = form;
+ p = urlencode(p, "username");
+ *p++ = '=';
+ p = urlencode(p, username);
- if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
- return 0;
- }
+ *p++ = '&';
+ p = urlencode(p, "password");
+ *p++ = '=';
+ p = urlencode(p, passwd);
+
+ *p++ = '&';
+ p = urlencode(p, "path");
+ *p++ = '=';
+ p = urlencode(p, auth_path);
+
+ *p++ = '&';
+ p = urlencode(p, "permissions");
+ *p++ = '=';
+ p = urlencode(p, auth_perm);
+
+ sprintf(buf, "POST /api2/json/ticket HTTP/1.1\n"
+ "Host: localhost:85\n"
+ "Connection: close\n"
+ "Content-Type: application/x-www-form-urlencoded\n"
+ "Content-Length: %zd\n\n%s\n", strlen(form), form);
+ ssize_t len = strlen(buf);
+ ssize_t sb = send(sfd, buf, len, 0);
+ if (sb < 0) {
+ perror("pve_auth_verify: send failed");
+ goto err;
}
+ if (sb != len) {
+ fprintf(stderr, "pve_auth_verify: partial send error");
+ goto err;
+ }
- rfbLog("could not launch auth script %s\n", script);
+ len = recv(sfd, buf, sizeof(buf) - 1, 0);
+ if (len < 0) {
+ perror("pve_auth_verify: recv failed");
+ goto err;
+ }
+ buf[len] = 0;
+
+ //printf("DATA:%s\n", buf);
+
+ shutdown(sfd, SHUT_RDWR);
+
+ return strncmp(buf, "HTTP/1.1 200 OK", 15);
+
+err:
+ shutdown(sfd, SHUT_RDWR);
return -1;
}
More information about the pve-devel
mailing list