コード例 #1
0
ファイル: test_mysql_users.c プロジェクト: AAAAAK/MaxScale
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
ファイル: dbusers.c プロジェクト: rasmushoj/MaxScale
/**
 * Replace the user/passwd form mysql.user table into the service users' hashtable
 * environment.
 * The replacement is succesful only if the users' table checksums differ
 *
 * @param service   The current service
 * @return      -1 on any error or the number of users inserted (0 means no users at all)
 */
int 
replace_mysql_users(SERVICE *service)
{
int		i;
struct users	*newusers, *oldusers;

	if ((newusers = mysql_users_alloc()) == NULL)
		return -1;

	i = getUsers(service, newusers);

	if (i <= 0)
		return i;

	spinlock_acquire(&service->spin);
	oldusers = service->users;

	/* digest compare */
	if (memcmp(oldusers->cksum, newusers->cksum, SHA_DIGEST_LENGTH) == 0) {
		/* same data, nothing to do */
		LOGIF(LD, (skygw_log_write_flush(
			LOGFILE_DEBUG,
			"%lu [replace_mysql_users] users' tables not switched, checksum is the same",
			pthread_self())));
		/* free the new table */
		users_free(newusers);
		i = 0;
	} else {
		/* replace the service with effective new data */
		LOGIF(LD, (skygw_log_write_flush(
			LOGFILE_DEBUG,
			"%lu [replace_mysql_users] users' tables replaced, checksum differs",
			pthread_self())));
		service->users = newusers;
	}

	spinlock_release(&service->spin);

	if (i)
		users_free(oldusers);

	return i;
}
コード例 #3
0
ファイル: dbusers.c プロジェクト: rasmushoj/MaxScale
/**
 * Reload the user/passwd form mysql.user table into the service users' hashtable
 * environment.
 *
 * @param service   The current service
 * @return      -1 on any error or the number of users inserted (0 means no users at all)
 */
int 
reload_mysql_users(SERVICE *service)
{
int		i;
struct users	*newusers, *oldusers;

	if ((newusers = mysql_users_alloc()) == NULL)
		return 0;
	i = getUsers(service, newusers);
	spinlock_acquire(&service->spin);
	oldusers = service->users;
	service->users = newusers;
	spinlock_release(&service->spin);
	users_free(oldusers);

	return i;
}
コード例 #4
0
ファイル: maxinfo.c プロジェクト: bigshuai/MaxScale
/**
 * Add the service user to the service->users
 * via mysql_users_alloc and add_mysql_users_with_host_ipv4
 * User is added for '%' and 'localhost' hosts
 *
 * @param service The service for this router
 * @return	0 on success, 1 on failure
 */
static int
maxinfo_add_mysql_user(SERVICE *service) {
	char	*dpwd = NULL;
	char	*newpasswd = NULL;
	char	*service_user = NULL;
        char	*service_passwd = NULL;

	if (serviceGetUser(service, &service_user, &service_passwd) == 0) {
                MXS_ERROR("maxinfo: failed to get service user details");

		return 1;
	}

	dpwd = decryptPassword(service->credentials.authdata);

	if (!dpwd) {
                MXS_ERROR("maxinfo: decrypt password failed for service user %s",
                          service_user);

		return 1;
	}

	service->users = (void *)mysql_users_alloc();

	newpasswd = create_hex_sha1_sha1_passwd(dpwd);

	if (!newpasswd) {
                MXS_ERROR("maxinfo: create hex_sha1_sha1_password failed for service user %s",
                          service_user);
		users_free(service->users);
        service->users = NULL;
		return 1;
	}

	/* add service user for % and localhost */
	(void)add_mysql_users_with_host_ipv4(service->users, service->credentials.name, "%", newpasswd, "Y", "");
	(void)add_mysql_users_with_host_ipv4(service->users, service->credentials.name, "localhost", newpasswd, "Y", "");

	free(newpasswd);
	free(dpwd);

	return 0;
}
コード例 #5
0
ファイル: test_mysql_users.c プロジェクト: AAAAAK/MaxScale
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;
}
コード例 #6
0
ファイル: test_mysql_users.c プロジェクト: AAAAAK/MaxScale
int set_and_get_mysql_users_wildcards(char *username, char *hostname, char *password, char *from, char *anydb, char *db, char *db_from) {
	USERS *mysql_users;
	int ret = -1;
	struct sockaddr_in client_addr;
	DCB	*dcb;
	SERVICE *service;
	MYSQL_session *data;

	dcb = dcb_alloc(DCB_ROLE_INTERNAL);

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

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

        if (hostname) {
		if(!setipaddress(&client_addr.sin_addr, from)) {
			fprintf(stderr, "setipaddress failed for host [%s]\n", from);
			free(service);
			dcb_free(dcb);
			return ret;
		}
	}

	if ((data = (MYSQL_session *) calloc(1, sizeof(MYSQL_session))) == NULL) {
		fprintf(stderr, "MYSQL_session alloc failed\n");
		free(service);
		dcb_free(dcb);
		return ret;
	}

	
	/* client IPv4 in raw data*/
	memcpy(&dcb->ipv4, (struct sockaddr_in *)&client_addr, sizeof(struct sockaddr_in));

	dcb->service = service;

	mysql_users = mysql_users_alloc();

	service->users = mysql_users;

	if (db_from != NULL)
		strncpy(data->db, db_from,MYSQL_DATABASE_MAXLEN);
	else
		strncpy(data->db, "",MYSQL_DATABASE_MAXLEN);

	/* freed by dcb_free(dcb) */
	dcb->data = data;

	// the routine returns 1 on success
	if (anydb != NULL) {
		if (strcmp(anydb, "N") == 0) {
			ret = add_mysql_users_with_host_ipv4(mysql_users, username, hostname, password, anydb, db);
		} else if (strcmp(anydb, "Y") == 0) {
			ret = add_mysql_users_with_host_ipv4(mysql_users, username, hostname, password, "Y", "");
		} else {
			ret = add_mysql_users_with_host_ipv4(mysql_users, username, hostname, password, "N", NULL);
		}
	} else {
		ret = add_mysql_users_with_host_ipv4(mysql_users, username, hostname, password, "N", NULL);
	}
	
	if (ret == 0) {
		fprintf(stderr, "add_mysql_users_with_host_ipv4 (%s@%s, %s) FAILED\n", username, hostname, password);
	} else {
		unsigned char db_passwd[100]="";

		dcb->remote=strdup(from);

		// returns 0 on success
		ret = gw_find_mysql_user_password_sha1(username, db_passwd, dcb);
	}

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

	return ret;
}