1 #include "../burp.h"
2 #include "../alloc.h"
3 #include "../asfd.h"
4 #include "../cmd.h"
5 #include "../cntr.h"
6 #include "../handy.h"
7 #include "../iobuf.h"
8 #include "../log.h"
9 #include "auth.h"
10 
authorise_client(struct asfd * asfd,char ** server_version,const char * cname,const char * password,struct cntr * cntr)11 int authorise_client(struct asfd *asfd,
12 	char **server_version, const char *cname, const char *password,
13 	struct cntr *cntr)
14 {
15 	int ret=-1;
16 	char hello[256]="";
17 	struct iobuf *rbuf=asfd->rbuf;
18 
19 	snprintf(hello, sizeof(hello), "hello:%s", PACKAGE_VERSION);
20 	if(asfd->write_str(asfd, CMD_GEN, hello))
21 	{
22 		logp("problem with auth\n");
23 		goto end;
24 	}
25 
26 	if(asfd->read(asfd)
27 	  || rbuf->cmd!=CMD_GEN
28 	  || strncmp_w(rbuf->buf, "whoareyou"))
29 	{
30 		logp("problem with auth\n");
31 		goto end;
32 	}
33 	if(rbuf->buf)
34 	{
35 		char *cp=NULL;
36 		if((cp=strchr(rbuf->buf, ':')))
37 		{
38 			cp++;
39 			if(cp && !(*server_version=strdup_w(cp, __func__)))
40 				goto end;
41 		}
42 		iobuf_free_content(rbuf);
43 	}
44 
45 	if(asfd->write_str(asfd, CMD_GEN, cname)
46 	  || asfd_read_expect(asfd, CMD_GEN, "okpassword")
47 	  || asfd->write_str(asfd, CMD_GEN, password)
48 	  || asfd->read(asfd))
49 	{
50 		logp("problem with auth\n");
51 		goto end;
52 	}
53 
54 	if(rbuf->cmd==CMD_WARNING) // special case for the version warning
55 	{
56 		//logw(conf->p1cntr, rbuf->buf);
57 		logp("WARNING: %s\n", iobuf_to_printable(rbuf));
58 		cntr_add(cntr, rbuf->cmd, 0);
59 		iobuf_free_content(rbuf);
60 		if(asfd->read(asfd))
61 		{
62 			logp("problem with auth\n");
63 			goto end;
64 		}
65 	}
66 	if(rbuf->cmd==CMD_GEN && !strcmp(rbuf->buf, "ok"))
67 	{
68 		// It is OK.
69 		logp("auth ok\n");
70 	}
71 	else
72 	{
73 		iobuf_log_unexpected(rbuf, __func__);
74 		goto end;
75 	}
76 
77 	ret=0;
78 end:
79 	iobuf_free_content(rbuf);
80 	return ret;
81 }
82