]>
git.proxmox.com Git - spiceterm.git/blob - auth-pve.c
5 #include <sys/socket.h>
10 static char *auth_path
= "/";
11 static char *auth_perm
= "Sys.Console";
14 urlencode(char *buf
, const char *value
)
16 static const char *hexchar
= "0123456789abcdef";
19 int l
= strlen(value
);
20 for (i
= 0; i
< l
; i
++) {
22 if (('a' <= c
&& c
<= 'z') ||
23 ('A' <= c
&& c
<= 'Z') ||
24 ('0' <= c
&& c
<= '9')) {
30 *p
++ = hexchar
[c
>> 4];
31 *p
++ = hexchar
[c
& 15];
40 pve_auth_verify(const char *clientip
, const char *username
, const char *passwd
)
42 struct sockaddr_in server
;
44 int sfd
= socket(AF_INET
, SOCK_STREAM
, 0);
46 perror("pve_auth_verify: socket failed");
51 if ((he
= gethostbyname("localhost")) == NULL
) {
52 fprintf(stderr
, "pve_auth_verify: error resolving hostname\n");
56 memcpy(&server
.sin_addr
, he
->h_addr_list
[0], he
->h_length
);
57 server
.sin_family
= AF_INET
;
58 server
.sin_port
= htons(85);
60 if (connect(sfd
, (struct sockaddr
*)&server
, sizeof(server
))) {
61 perror("pve_auth_verify: error connecting to server");
69 p
= urlencode(p
, "username");
71 p
= urlencode(p
, username
);
74 p
= urlencode(p
, "password");
76 p
= urlencode(p
, passwd
);
79 p
= urlencode(p
, "path");
81 p
= urlencode(p
, auth_path
);
84 p
= urlencode(p
, "privs");
86 p
= urlencode(p
, auth_perm
);
88 sprintf(buf
, "POST /api2/json/access/ticket HTTP/1.1\n"
89 "Host: localhost:85\n"
92 "Content-Type: application/x-www-form-urlencoded\n"
93 "Content-Length: %zd\n\n%s\n", clientip
, strlen(form
), form
);
94 ssize_t len
= strlen(buf
);
95 ssize_t sb
= send(sfd
, buf
, len
, 0);
97 perror("pve_auth_verify: send failed");
101 fprintf(stderr
, "pve_auth_verify: partial send error\n");
105 len
= recv(sfd
, buf
, sizeof(buf
) - 1, 0);
107 perror("pve_auth_verify: recv failed");
113 //printf("DATA:%s\n", buf);
115 shutdown(sfd
, SHUT_RDWR
);
117 if (!strncmp(buf
, "HTTP/1.1 200 OK", 15)) {
121 char *firstline
= strtok(buf
, "\n");
123 fprintf(stderr
, "auth failed: %s\n", firstline
);
128 shutdown(sfd
, SHUT_RDWR
);