Пример #1
0
int set_and_get_single_mysql_users(char *username, char *hostname, char *password) {
        struct sockaddr_in serv_addr;
        MYSQL_USER_HOST key;
	USERS *mysql_users;
	char ret_ip[200]="";
	char *fetch_data;
	char *db="";
	
	mysql_users = mysql_users_alloc();

        /* prepare the user@host data struct */
	memset(&serv_addr, 0, sizeof(serv_addr));
	memset(&key, 0, sizeof(key));

	
	if (hostname)	
		if(!setipaddress(&serv_addr.sin_addr, hostname)) {
			fprintf(stderr, "setipaddress failed for host [%s]\n", hostname);
			users_free(mysql_users);
			return 1;
		}
	if (username)
		key.user = username;

	memcpy(&key.ipv4, &serv_addr, sizeof(serv_addr));
	key.resource = db;

	inet_ntop(AF_INET, &(serv_addr).sin_addr, ret_ip, INET_ADDRSTRLEN);

	fprintf(stderr, "set/get [%s@%s]: IPV4 %lu is [%u].[%u].[%u].[%u]\n", username, hostname, (unsigned long) serv_addr.sin_addr.s_addr, serv_addr.sin_addr.s_addr&0xFF, (serv_addr.sin_addr.s_addr&0xFF00), (serv_addr.sin_addr.s_addr&0xFF0000), ((serv_addr.sin_addr.s_addr & 0xFF000000) / (256*256*256)));

	/* add user@host as key and passwd as value in the MySQL users hash table */
	if (!mysql_users_add(mysql_users, &key, password)) {
		fprintf(stderr, "mysql_users_add() failed for %s@%s\n", username, hostname);
		users_free(mysql_users);
		return 1;
	}

	memset(&serv_addr, 0, sizeof(serv_addr));

	if (hostname)
		if(!setipaddress(&serv_addr.sin_addr, hostname)) {
			fprintf(stderr, "setipaddress failed for host [%s]\n", hostname);
			users_free(mysql_users);
			return 1;
		}
	key.user = username;
	memcpy(&key.ipv4, &serv_addr, sizeof(serv_addr));
	key.resource = db;

	fetch_data = mysql_users_fetch(mysql_users, &key);

	users_free(mysql_users);

	if (!fetch_data)
		return 1;
	
	return 0;
}
Пример #2
0
int gw_find_mysql_user_password_sha1(char *username, uint8_t *gateway_password, DCB *dcb) {
        SERVICE *service = NULL;
	struct sockaddr_in *client;
        char *user_password = NULL;
	MYSQL_USER_HOST key;

	service = (SERVICE *) dcb->service;
	client = (struct sockaddr_in *) &dcb->ipv4;

	key.user = username;
	memcpy(&key.ipv4, client, sizeof(struct sockaddr_in));

	LOGIF(LD,
		(skygw_log_write_flush(
			LOGFILE_DEBUG,
			"%lu [MySQL Client Auth], checking user [%s@%s]",
			pthread_self(),
			key.user,
			dcb->remote)));

	/* look for user@current_host now */
        user_password = mysql_users_fetch(service->users, &key);

        if (!user_password) {
		/* The user is not authenticated @ current host */

		/* 1) Check for localhost first.
		 * The check for localhost is 127.0.0.1 (IPv4 only)
 		 */

		if (key.ipv4.sin_addr.s_addr == 0x0100007F) {
 		 	/* Skip the wildcard check and return 1 */
			LOGIF(LD,
				(skygw_log_write_flush(
					LOGFILE_DEBUG,
					"%lu [MySQL Client Auth], user [%s@%s] not existent",
					pthread_self(),
					key.user,
					dcb->remote)));

			return 1;
		}
	
		/* 2) Continue and check for wildcard host, user@%
		 * Return 1 if no match
		 */

		memset(&key.ipv4, 0, sizeof(struct sockaddr_in));

		LOGIF(LD,
			(skygw_log_write_flush(
				LOGFILE_DEBUG,
				"%lu [MySQL Client Auth], checking user [%s@%s] with wildcard host [%%]",
				pthread_self(),
				key.user,
				dcb->remote)));

		user_password = mysql_users_fetch(service->users, &key);
     
		if (!user_password) {
			/* the user@% was not found.
 			 * Return 1
 			 */
			LOGIF(LD,
				(skygw_log_write_flush(
					LOGFILE_DEBUG,
					"%lu [MySQL Client Auth], user [%s@%s] not existent",
					pthread_self(),
					key.user,
					dcb->remote)));
			return 1;
		}
	}

	/* user@host found: now check the password
 	 *
	 * Convert the hex data (40 bytes) to binary (20 bytes).
         * The gateway_password represents the SHA1(SHA1(real_password)).
         * Please note: the real_password is unknown and SHA1(real_password) is unknown as well
	 */

        if (strlen(user_password))
                gw_hex2bin(gateway_password, user_password, SHA_DIGEST_LENGTH * 2);

        return 0;
}
Пример #3
0
int set_and_get_single_mysql_users_ipv4(char *username, unsigned long ipv4, char *password) {
        struct sockaddr_in serv_addr;
        MYSQL_USER_HOST key;
        MYSQL_USER_HOST find_key;
	USERS *mysql_users;
	char ret_ip[200]="";
	char *fetch_data;
	char *db="";
	DCB *dcb;
	SERVICE *service;

	unsigned long fix_ipv4;

        dcb = dcb_alloc(DCB_ROLE_INTERNAL);

        if (dcb == NULL) {
                fprintf(stderr, "dcb_alloc() failed\n");
                return 1;
        }
        if ((service = (SERVICE *)calloc(1, sizeof(SERVICE))) == NULL) {
                fprintf(stderr, "service_alloc() failed\n");
                dcb_free(dcb);
                return 1;
        }

	if (ipv4 > UINT_MAX) {
		fix_ipv4 = UINT_MAX;
	} else {
		fix_ipv4 = ipv4;
	}	
	
	mysql_users = mysql_users_alloc();
        /* prepare the user@host data struct */
	memset(&key, 0, sizeof(key));
	memset(&serv_addr, 0, sizeof(serv_addr));
	serv_addr.sin_family = AF_INET;
	memcpy(&(serv_addr).sin_addr.s_addr, &fix_ipv4, sizeof(ipv4));

	key.user = username;
	memcpy(&key.ipv4, &serv_addr, sizeof(serv_addr));
	key.resource = db;

	inet_ntop(AF_INET, &(serv_addr).sin_addr, ret_ip, INET_ADDRSTRLEN);

	fprintf(stderr, "IPv4 passed/fixed [%lu/%lu] is [%s]\n", ipv4,fix_ipv4, ret_ip);

	/* add user@host as key and passwd as value in the MySQL users hash table */
	if (!mysql_users_add(mysql_users, &key, password)) {
		fprintf(stderr, "Failed adding %s@%s(%lu)\n", username, ret_ip, fix_ipv4);
		users_free(mysql_users);
		free(service);
		dcb_free(dcb);
		return 1;
	}

	memset(&serv_addr, 0, sizeof(serv_addr));
	memset(&find_key, 0, sizeof(find_key));

	find_key.user = username;
	memcpy(&(serv_addr).sin_addr.s_addr, &ipv4, sizeof(ipv4));
	find_key.resource = db;

	memcpy(&find_key.ipv4, &serv_addr, sizeof(serv_addr));

	fetch_data = mysql_users_fetch(mysql_users, &find_key);

	users_free(mysql_users);
	free(service);
	dcb_free(dcb);

	if (!fetch_data)
		return 1;
	
	return 0;
}