Beispiel #1
0
irc_user_t *irc_user_by_name( irc_t *irc, const char *nick )
{
	char key[strlen(nick)+1];
	
	strcpy( key, nick );
	if( nick_lc( key ) )
		return g_hash_table_lookup( irc->nick_user_hash, key );
	else
		return NULL;
}
Beispiel #2
0
static storage_status_t xml_save( irc_t *irc, int overwrite )
{
	storage_status_t ret = STORAGE_OK;
	char path[512], *path2 = NULL, *xml = NULL;
	struct xt_node *tree = NULL;
	size_t len;
	int fd;
	
	path2 = g_strdup( irc->user->nick );
	nick_lc( NULL, path2 );
	g_snprintf( path, sizeof( path ) - 20, "%s%s%s", global.conf->configdir, path2, ".xml" );
	g_free( path2 );
	
	if( !overwrite && g_access( path, F_OK ) == 0 )
		return STORAGE_ALREADY_EXISTS;
	
	strcat( path, ".XXXXXX" );
	if( ( fd = mkstemp( path ) ) < 0 )
	{
		irc_rootmsg( irc, "Error while opening configuration file." );
		return STORAGE_OTHER_ERROR;
	}
	
	tree = xml_generate( irc );
	xml = xt_to_string_i( tree );
	len = strlen( xml );
	if( write( fd, xml, len ) != len ||
	    fsync( fd ) != 0 || /* #559 */
	    close( fd ) != 0 )
		goto error;
	
	path2 = g_strndup( path, strlen( path ) - 7 );
	if( rename( path, path2 ) != 0 )
	{
		g_free( path2 );
		goto error;
	}
	g_free( path2 );
	
	goto finish;

error:
	irc_rootmsg( irc, "Write error. Disk full?" );
	ret = STORAGE_OTHER_ERROR;

finish:	
	close( fd );
	unlink( path );
	g_free( xml );
	xt_free_node( tree );
	
	return ret;
}
Beispiel #3
0
static storage_status_t xml_remove(const char *nick)
{
	char s[512], *lc;

	lc = g_strdup(nick);
	nick_lc(NULL, lc);
	g_snprintf(s, 511, "%s%s%s", global.conf->configdir, lc, ".xml");
	g_free(lc);

	if (unlink(s) == -1) {
		return STORAGE_OTHER_ERROR;
	}

	return STORAGE_OK;
}
Beispiel #4
0
static void irc_cmd_watch( irc_t *irc, char **cmd )
{
	int i;
	
	/* Obviously we could also mark a user structure as being
	   watched, but what if the WATCH command is sent right
	   after connecting? The user won't exist yet then... */
	for( i = 1; cmd[i]; i ++ )
	{
		char *nick;
		irc_user_t *iu;
		
		if( !cmd[i][0] || !cmd[i][1] )
			break;
		
		nick = g_strdup( cmd[i] + 1 );
		nick_lc( nick );
		
		iu = irc_user_by_name( irc, nick );
		
		if( cmd[i][0] == '+' )
		{
			if( !g_hash_table_lookup( irc->watches, nick ) )
				g_hash_table_insert( irc->watches, nick, nick );
			
			if( iu && iu->bu && iu->bu->flags & BEE_USER_ONLINE )
				irc_send_num( irc, 604, "%s %s %s %d :%s", iu->nick, iu->user,
				              iu->host, (int) time( NULL ), "is online" );
			else
				irc_send_num( irc, 605, "%s %s %s %d :%s", nick, "*", "*",
				              (int) time( NULL ), "is offline" );
		}
		else if( cmd[i][0] == '-' )
		{
			gpointer okey, ovalue;
			
			if( g_hash_table_lookup_extended( irc->watches, nick, &okey, &ovalue ) )
			{
				g_hash_table_remove( irc->watches, okey );
				g_free( okey );
				
				irc_send_num( irc, 602, "%s %s %s %d :%s", nick, "*", "*", 0, "Stopped watching" );
			}
		}
	}
}
Beispiel #5
0
irc_user_t *irc_user_new( irc_t *irc, const char *nick )
{
	irc_user_t *iu = g_new0( irc_user_t, 1 );
	
	iu->irc = irc;
	iu->nick = g_strdup( nick );
	iu->user = iu->host = iu->fullname = iu->nick;
	
	iu->key = g_strdup( nick );
	nick_lc( iu->key );
	/* Using the hash table for speed and irc->users for easy iteration
	   through the list (since the GLib API doesn't have anything sane
	   for that.) */
	g_hash_table_insert( irc->nick_user_hash, iu->key, iu );
	irc->users = g_slist_insert_sorted( irc->users, iu, irc_user_cmp );
	
	return iu;
}
Beispiel #6
0
static storage_status_t xml_remove( const char *nick, const char *password )
{
	char s[512], *lc;
	storage_status_t status;

	status = xml_check_pass( nick, password );
	if( status != STORAGE_OK )
		return status;

	lc = g_strdup( nick );
	nick_lc( lc );
	g_snprintf( s, 511, "%s%s%s", global.conf->configdir, lc, ".xml" );
	g_free( lc );
	
	if( unlink( s ) == -1 )
		return STORAGE_OTHER_ERROR;
	
	return STORAGE_OK;
}
Beispiel #7
0
char *nick_get(bee_user_t *bu)
{
	static char nick[MAX_NICK_LENGTH + 1];
	char *store_handle, *found_nick;
	irc_t *irc = (irc_t *) bu->bee->ui_data;

	memset(nick, 0, MAX_NICK_LENGTH + 1);

	store_handle = clean_handle(bu->handle);
	/* Find out if we stored a nick for this person already. If not, try
	   to generate a sane nick automatically. */
	if ((found_nick = g_hash_table_lookup(bu->ic->acc->nicks, store_handle))) {
		strncpy(nick, found_nick, MAX_NICK_LENGTH);
	} else if ((found_nick = nick_gen(bu))) {
		strncpy(nick, found_nick, MAX_NICK_LENGTH);
		g_free(found_nick);
	} else {
		/* Keep this fallback since nick_gen() can return NULL in some cases. */
		char *s;

		g_snprintf(nick, MAX_NICK_LENGTH, "%s", bu->handle);
		if ((s = strchr(nick, '@'))) {
			while (*s) {
				*(s++) = 0;
			}
		}

		nick_strip(irc, nick);
		if (set_getbool(&bu->bee->set, "lcnicks")) {
			nick_lc(irc, nick);
		}
	}
	g_free(store_handle);

	/* Make sure the nick doesn't collide with an existing one by adding
	   underscores and that kind of stuff, if necessary. */
	nick_dedupe(bu, nick);

	return nick;
}
Beispiel #8
0
static storage_status_t xml_save( irc_t *irc, int overwrite )
{
	char path[512], *path2, *pass_buf = NULL;
	set_t *set;
	account_t *acc;
	int fd;
	md5_byte_t pass_md5[21];
	md5_state_t md5_state;
	GSList *l;
	
	path2 = g_strdup( irc->user->nick );
	nick_lc( path2 );
	g_snprintf( path, sizeof( path ) - 2, "%s%s%s", global.conf->configdir, path2, ".xml" );
	g_free( path2 );
	
	if( !overwrite && g_access( path, F_OK ) == 0 )
		return STORAGE_ALREADY_EXISTS;
	
	strcat( path, ".XXXXXX" );
	if( ( fd = mkstemp( path ) ) < 0 )
	{
		irc_rootmsg( irc, "Error while opening configuration file." );
		return STORAGE_OTHER_ERROR;
	}
	
	/* Generate a salted md5sum of the password. Use 5 bytes for the salt
	   (to prevent dictionary lookups of passwords) to end up with a 21-
	   byte password hash, more convenient for base64 encoding. */
	random_bytes( pass_md5 + 16, 5 );
	md5_init( &md5_state );
	md5_append( &md5_state, (md5_byte_t*) irc->password, strlen( irc->password ) );
	md5_append( &md5_state, pass_md5 + 16, 5 ); /* Add the salt. */
	md5_finish( &md5_state, pass_md5 );
	/* Save the hash in base64-encoded form. */
	pass_buf = base64_encode( pass_md5, 21 );
	
	if( !xml_printf( fd, 0, "<user nick=\"%s\" password=\"%s\" version=\"%d\">\n", irc->user->nick, pass_buf, XML_FORMAT_VERSION ) )
		goto write_error;
	
	g_free( pass_buf );
	
	for( set = irc->b->set; set; set = set->next )
		if( set->value && !( set->flags & SET_NOSAVE ) )
			if( !xml_printf( fd, 1, "<setting name=\"%s\">%s</setting>\n", set->key, set->value ) )
				goto write_error;
	
	for( acc = irc->b->accounts; acc; acc = acc->next )
	{
		unsigned char *pass_cr;
		char *pass_b64;
		int pass_len;
		
		pass_len = arc_encode( acc->pass, strlen( acc->pass ), (unsigned char**) &pass_cr, irc->password, 12 );
		pass_b64 = base64_encode( pass_cr, pass_len );
		g_free( pass_cr );
		
		if( !xml_printf( fd, 1, "<account protocol=\"%s\" handle=\"%s\" password=\"%s\" "
		                        "autoconnect=\"%d\" tag=\"%s\"", acc->prpl->name, acc->user,
		                        pass_b64, acc->auto_connect, acc->tag ) )
		{
			g_free( pass_b64 );
			goto write_error;
		}
		g_free( pass_b64 );
		
		if( acc->server && acc->server[0] && !xml_printf( fd, 0, " server=\"%s\"", acc->server ) )
			goto write_error;
		if( !xml_printf( fd, 0, ">\n" ) )
			goto write_error;
		
		for( set = acc->set; set; set = set->next )
			if( set->value && !( set->flags & ACC_SET_NOSAVE ) )
				if( !xml_printf( fd, 2, "<setting name=\"%s\">%s</setting>\n", set->key, set->value ) )
					goto write_error;
		
		/* This probably looks pretty strange. g_hash_table_foreach
		   is quite a PITA already (but it can't get much better in
		   C without using #define, I'm afraid), and since it
		   doesn't seem to be possible to abort the foreach on write
		   errors, so instead let's use the _find function and
		   return TRUE on write errors. Which means, if we found
		   something, there was an error. :-) */
		if( g_hash_table_find( acc->nicks, xml_save_nick, & fd ) )
			goto write_error;
		
		if( !xml_printf( fd, 1, "</account>\n" ) )
			goto write_error;
	}
	
	for( l = irc->channels; l; l = l->next )
	{
		irc_channel_t *ic = l->data;
		
		if( ic->flags & IRC_CHANNEL_TEMP )
			continue;
		
		if( !xml_printf( fd, 1, "<channel name=\"%s\" type=\"%s\">\n",
		                 ic->name, set_getstr( &ic->set, "type" ) ) )
			goto write_error;
		
		for( set = ic->set; set; set = set->next )
			if( set->value && strcmp( set->key, "type" ) != 0 )
				if( !xml_printf( fd, 2, "<setting name=\"%s\">%s</setting>\n", set->key, set->value ) )
					goto write_error;
		
		if( !xml_printf( fd, 1, "</channel>\n" ) )
			goto write_error;
	}
	
	if( !xml_printf( fd, 0, "</user>\n" ) )
		goto write_error;
	
	fsync( fd );
	close( fd );
	
	path2 = g_strndup( path, strlen( path ) - 7 );
	if( rename( path, path2 ) != 0 )
	{
		irc_rootmsg( irc, "Error while renaming temporary configuration file." );
		
		g_free( path2 );
		unlink( path );
		
		return STORAGE_OTHER_ERROR;
	}
	
	g_free( path2 );
	
	return STORAGE_OK;

write_error:
	g_free( pass_buf );
	
	irc_rootmsg( irc, "Write error. Disk full?" );
	close( fd );
	
	return STORAGE_OTHER_ERROR;
}
Beispiel #9
0
static storage_status_t xml_load_real( irc_t *irc, const char *my_nick, const char *password, xml_pass_st action )
{
	GMarkupParseContext *ctx;
	struct xml_parsedata *xd;
	char *fn, buf[512];
	GError *gerr = NULL;
	int fd, st;
	
	xd = g_new0( struct xml_parsedata, 1 );
	xd->irc = irc;
	xd->given_nick = g_strdup( my_nick );
	xd->given_pass = g_strdup( password );
	xd->pass_st = action;
	nick_lc( xd->given_nick );
	
	fn = g_strdup_printf( "%s%s%s", global.conf->configdir, xd->given_nick, ".xml" );
	if( ( fd = open( fn, O_RDONLY ) ) < 0 )
	{
		xml_destroy_xd( xd );
		g_free( fn );
		return STORAGE_NO_SUCH_USER;
	}
	g_free( fn );
	
	ctx = g_markup_parse_context_new( &xml_parser, 0, xd, xml_destroy_xd );
	
	while( ( st = read( fd, buf, sizeof( buf ) ) ) > 0 )
	{
		if( !g_markup_parse_context_parse( ctx, buf, st, &gerr ) || gerr )
		{
			xml_pass_st pass_st = xd->pass_st;
			
			g_markup_parse_context_free( ctx );
			close( fd );
			
			if( pass_st == XML_PASS_WRONG )
			{
				g_clear_error( &gerr );
				return STORAGE_INVALID_PASSWORD;
			}
			else
			{
				if( gerr && irc )
					irc_rootmsg( irc, "Error from XML-parser: %s", gerr->message );
				
				g_clear_error( &gerr );
				return STORAGE_OTHER_ERROR;
			}
		}
	}
	/* Just to be sure... */
	g_clear_error( &gerr );
	
	g_markup_parse_context_free( ctx );
	close( fd );
	
	if( action == XML_PASS_CHECK_ONLY )
		return STORAGE_OK;
	
	return STORAGE_OK;
}
Beispiel #10
0
static storage_status_t xml_load_real( irc_t *irc, const char *my_nick, const char *password, xml_pass_st action )
{
	struct xml_parsedata xd[1];
	char *fn, buf[2048];
	int fd, st;
	struct xt_parser *xp = NULL;
	struct xt_node *node;
	storage_status_t ret = STORAGE_OTHER_ERROR;
	
	xd->irc = irc;
	strncpy( xd->given_nick, my_nick, MAX_NICK_LENGTH );
	xd->given_nick[MAX_NICK_LENGTH] = '\0';
	nick_lc( NULL, xd->given_nick );
	xd->given_pass = (char*) password;
	
	fn = g_strconcat( global.conf->configdir, xd->given_nick, ".xml", NULL );
	if( ( fd = open( fn, O_RDONLY ) ) < 0 )
	{
		ret = STORAGE_NO_SUCH_USER;
		goto error;
	}
	
	xp = xt_new( handlers, xd );
	while( ( st = read( fd, buf, sizeof( buf ) ) ) > 0 )
	{
		st = xt_feed( xp, buf, st );
		if( st != 1 )
			break;
	}
	close( fd );
	if( st != 0 )
		goto error;
	
	node = xp->root;
	if( node == NULL || node->next != NULL || strcmp( node->name, "user" ) != 0 )
		goto error;
	
	{
		char *nick = xt_find_attr( node, "nick" );
		char *pass = xt_find_attr( node, "password" );
		
		if( !nick || !pass )
		{
			goto error;
		}
		else if( ( st = md5_verify_password( xd->given_pass, pass ) ) != 0 )
		{
			ret = STORAGE_INVALID_PASSWORD;
			goto error;
		}
	}
	
	if( action == XML_PASS_CHECK_ONLY )
	{
		ret = STORAGE_OK;
		goto error;
	}
	
	/* DO NOT call xt_handle() before verifying the password! */
	if( xt_handle( xp, NULL, 1 ) == XT_HANDLED )
		ret = STORAGE_OK;
	
	handle_settings( node, &xd->irc->b->set );
	
error:
	xt_free( xp );
	g_free( fn );
	return ret;
}
Beispiel #11
0
static storage_status_t xml_load_real(irc_t *irc, const char *my_nick, const char *password, xml_action action)
{
	struct xml_parsedata xd[1];
	char *fn, buf[2048];
	int fd, st;
	struct xt_parser *xp = NULL;
	struct xt_node *node;
	storage_status_t ret = STORAGE_OTHER_ERROR;

	xd->irc = irc;
	strncpy(xd->given_nick, my_nick, MAX_NICK_LENGTH);
	xd->given_nick[MAX_NICK_LENGTH] = '\0';
	nick_lc(NULL, xd->given_nick);
	xd->given_pass = (char *) password;

	fn = g_strconcat(global.conf->configdir, xd->given_nick, ".xml", NULL);
	if ((fd = open(fn, O_RDONLY)) < 0) {
		if (errno == ENOENT) {
			ret = STORAGE_NO_SUCH_USER;
		} else {
			irc_rootmsg(irc, "Error loading user config: %s", g_strerror(errno));
		}
		goto error;
	}

	xp = xt_new(handlers, xd);
	while ((st = read(fd, buf, sizeof(buf))) > 0) {
		st = xt_feed(xp, buf, st);
		if (st != 1) {
			break;
		}
	}
	close(fd);
	if (st != 0) {
		goto error;
	}

	node = xp->root;
	if (node == NULL || node->next != NULL || strcmp(node->name, "user") != 0) {
		goto error;
	}

	if (action == XML_PASS_CHECK) {
		char *nick = xt_find_attr(node, "nick");
		char *pass = xt_find_attr(node, "password");
		char *backend = xt_find_attr(node, "auth_backend");

		if (!nick || !(pass || backend)) {
			goto error;
		}

		if (backend) {
			g_free(xd->irc->auth_backend);
			xd->irc->auth_backend = g_strdup(backend);
			ret = STORAGE_CHECK_BACKEND;
		} else if ((st = md5_verify_password(xd->given_pass, pass)) != 0) {
			ret = STORAGE_INVALID_PASSWORD;
		} else {
			ret = STORAGE_OK;
		}
		goto error;
	}

	if (xt_handle(xp, NULL, 1) == XT_HANDLED) {
		ret = STORAGE_OK;
	}

	handle_settings(node, &xd->irc->b->set);

error:
	xt_free(xp);
	g_free(fn);
	return ret;
}