Пример #1
0
void http_header::append_accept_key(const char* sec_key, string& out) const
{
	string tmp(sec_key);
	tmp += "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";

	sha1 sha;
	sha.input(tmp.c_str(), (unsigned int) tmp.size());
	unsigned char digest[20];
	sha.result2((unsigned*) digest);

	//little endian to big endian
	for (int i = 0; i < 20; i += 4)
	{
		unsigned char c;

		c = digest[i];
		digest[i] = digest[i + 3];
		digest[i + 3] = c;

		c = digest[i + 1];
		digest[i + 1] = digest[i + 2];
		digest[i + 2] = c;
	}

	unsigned char* s = acl_base64_encode((char*) digest, 20);
	out << "Sec-WebSocket-Accept: " << (char*) s << "\r\n";
	acl_myfree(s);
}
Пример #2
0
int smtp_auth(SMTP_CLIENT *client, const char *user, const char *pass)
{
	int   ret;
	ACL_ARGV *tokens = NULL;
	char *user_encoded = NULL, *pass_encoded = NULL;

#undef	RETURN
#define	RETURN(x) do {  \
	if (tokens)  \
		acl_argv_free(tokens);  \
	if (user_encoded)  \
		acl_myfree(user_encoded);  \
	if (pass_encoded)  \
		acl_myfree(pass_encoded);  \
	return (x);  \
} while (0)

	client->smtp_code = 0;
	client->buf[0] = 0;

	/* 发送认证命令 */

	ret = acl_vstream_fprintf(client->conn, "AUTH LOGIN\r\n");
	if (ret == ACL_VSTREAM_EOF) {
		acl_msg_error("%s(%d): send AUTH LOGIN to error(%s)",
			__FUNCTION__, __LINE__, acl_last_serror());
		RETURN (-1);
	}

	ret = acl_vstream_gets_nonl(client->conn, client->buf, client->size);
	if (ret == ACL_VSTREAM_EOF) {
		acl_msg_error("%s(%d): gets AUTH LOGIN's reply error(%s)",
			__FUNCTION__, __LINE__, acl_last_serror());
		RETURN (-1);
	}

	tokens = acl_argv_split(client->buf, "\t ");
	client->smtp_code = atoi(tokens->argv[0]);
	if (client->smtp_code != 334) {
		acl_msg_error("%s(%d): AUTH LOGIN failed, line(%s)",
			__FUNCTION__, __LINE__, client->buf);
		RETURN (-1);
	}

	/* 发送邮箱帐号 */

	user_encoded = (char*) acl_base64_encode(user, (int) strlen(user));
	ret = acl_vstream_fprintf(client->conn, "%s\r\n", user_encoded);
	if (ret == ACL_VSTREAM_EOF) {
		acl_msg_error("%s(%d): send user error(%s)",
			__FUNCTION__, __LINE__, acl_last_serror());
		RETURN (-1);
	}

	ret = acl_vstream_gets_nonl(client->conn, client->buf, client->size);
	if (ret == ACL_VSTREAM_EOF) {
		acl_msg_error("%s(%d): auth gets error(%s)",
			__FUNCTION__, __LINE__, acl_last_serror());
		RETURN (-1);
	}
	acl_argv_free(tokens);
	tokens = acl_argv_split(client->buf, "\t ");
	client->smtp_code = atoi(tokens->argv[0]);
	if (client->smtp_code != 334) {
		acl_msg_error("%s(%d): AUTH LOGIN failed, line(%s)",
			__FUNCTION__, __LINE__, client->buf);
		RETURN (-1);
	}

	/* 发送 password */

	pass_encoded = (char*) acl_base64_encode(pass, (int) strlen(pass));
	ret = acl_vstream_fprintf(client->conn, "%s\r\n", pass_encoded);
	if (ret == ACL_VSTREAM_EOF) {
		acl_msg_error("%s(%d): send pass error(%s)",
			__FUNCTION__, __LINE__, acl_last_serror());
		RETURN (-1);
	}
	ret = acl_vstream_gets_nonl(client->conn, client->buf, client->size);
	if (ret == ACL_VSTREAM_EOF) {
		acl_msg_error("%s(%d): auth gets pass's reply error(%s)",
			__FUNCTION__, __LINE__, acl_last_serror());
		RETURN (-1);
	}
	acl_argv_free(tokens);
	tokens = acl_argv_split(client->buf, "\t ");
	client->smtp_code = atoi(tokens->argv[0]);
	if (client->smtp_code != 235) {
		acl_msg_error("%s(%d): AUTH LOGIN failed, line(%s)",
			__FUNCTION__, __LINE__, client->buf);
		RETURN (-1);
	}

	RETURN (0);
}