]>
git.proxmox.com Git - spiceterm.git/blob - src/auth-pve.c
5 #include <sys/socket.h>
10 static char *auth_path
= "/";
11 static char *auth_perm
= "Sys.Console";
14 pve_auth_set_path(char *path
)
20 pve_auth_set_permissions(char *perm
)
26 urlencode(char *buf
, const char *value
)
28 static const char *hexchar
= "0123456789abcdef";
31 int l
= strlen(value
);
32 for (i
= 0; i
< l
; i
++) {
34 if (('a' <= c
&& c
<= 'z') ||
35 ('A' <= c
&& c
<= 'Z') ||
36 ('0' <= c
&& c
<= '9')) {
42 *p
++ = hexchar
[c
>> 4];
43 *p
++ = hexchar
[c
& 15];
52 pve_auth_verify(const char *clientip
, const char *username
, const char *passwd
)
54 struct sockaddr_in server
;
56 int sfd
= socket(AF_INET
, SOCK_STREAM
, 0);
58 perror("pve_auth_verify: socket failed");
63 if ((he
= gethostbyname("localhost")) == NULL
) {
64 fprintf(stderr
, "pve_auth_verify: error resolving hostname\n");
68 memcpy(&server
.sin_addr
, he
->h_addr_list
[0], he
->h_length
);
69 server
.sin_family
= AF_INET
;
70 server
.sin_port
= htons(85);
72 if (connect(sfd
, (struct sockaddr
*)&server
, sizeof(server
))) {
73 perror("pve_auth_verify: error connecting to server");
81 p
= urlencode(p
, "username");
83 p
= urlencode(p
, username
);
86 p
= urlencode(p
, "password");
88 p
= urlencode(p
, passwd
);
91 p
= urlencode(p
, "path");
93 p
= urlencode(p
, auth_path
);
96 p
= urlencode(p
, "privs");
98 p
= urlencode(p
, auth_perm
);
100 sprintf(buf
, "POST /api2/json/access/ticket HTTP/1.1\n"
101 "Host: localhost:85\n"
102 "Connection: close\n"
104 "Content-Type: application/x-www-form-urlencoded\n"
105 "Content-Length: %zd\n\n%s\n", clientip
, strlen(form
), form
);
106 ssize_t len
= strlen(buf
);
107 ssize_t sb
= send(sfd
, buf
, len
, 0);
109 perror("pve_auth_verify: send failed");
113 fprintf(stderr
, "pve_auth_verify: partial send error\n");
117 len
= recv(sfd
, buf
, sizeof(buf
) - 1, 0);
119 perror("pve_auth_verify: recv failed");
125 //printf("DATA:%s\n", buf);
127 shutdown(sfd
, SHUT_RDWR
);
129 if (!strncmp(buf
, "HTTP/1.1 200 OK", 15)) {
133 char *firstline
= strtok(buf
, "\n");
135 fprintf(stderr
, "auth failed: %s\n", firstline
);
140 shutdown(sfd
, SHUT_RDWR
);