Exemplo n.º 1
0
char *richacl_mask_to_text(unsigned int mask, int fmt)
{
	struct string_buffer *buffer;
	char *str = NULL;

	buffer = alloc_string_buffer(16);
	if (!buffer)
		return NULL;
	write_mask(buffer, mask, fmt);

	if (string_buffer_okay(buffer)) {
		str = realloc(buffer->buffer, buffer->offset + 1);
		if (str)
			buffer->buffer = NULL;
	} else
		errno = ENOMEM;
	free_string_buffer(buffer);
	return str;
}
Exemplo n.º 2
0
/*
 * get the next match from the current position,
 *		throught the dictionary.
 *	this will return all the matchs.
 *
 * @return friso_array_t that contains all the matchs.
 */
__STATIC_API__ friso_array_t get_next_match( friso_t friso, friso_task_t task, uint_t idx ) {

	register uint_t t;
	string_buffer_t sb = new_string_buffer_with_string( task->buffer );

	//create a match dynamic array.
	friso_array_t match = new_array_list_with_opacity( friso->max_len );
	array_list_add( match, friso_dic_get( friso->dic, __LEX_CJK_WORDS__, task->buffer ) );

	for ( t = 1; t < friso->max_len
			&& ( task->bytes = read_next_word( task, &idx, task->buffer ) ) != 0; t++ ) {

		task->unicode = get_utf8_unicode( task->buffer );
		if ( utf8_whitespace( task->unicode ) ) 	break;
		if ( ! utf8_cjk_string( task->unicode ) ) break;

		//append the task->buffer to the buffer.
		string_buffer_append( sb, task->buffer );

		//check the CJK dictionary.
		if ( friso_dic_match( friso->dic, __LEX_CJK_WORDS__, sb->buffer ) ) {
			/*
			 * add the lex_entry_t insite.
			 * here is a key point:
			 *		we use friso_dic_get function to get the address of the lex_entry_cdt
			 *		that store in the dictionary, not create a new lex_entry_cdt.
			 * so :
			 *		1.we will not bother to the allocations of the newly created lex_entry_cdt.
			 *		2.more efficient of course.
			 */
			array_list_add( match, friso_dic_get( friso->dic, __LEX_CJK_WORDS__, sb->buffer ) );
		}
	}

	/*buffer allocations clear*/
	free_string_buffer( sb );
	//array_list_trim( match );
	
	return match;
}
Exemplo n.º 3
0
//get the next cjk word from the current position, with simple mode.
__STATIC_API__ friso_hits_t next_simple_cjk( friso_t friso, friso_task_t task ) {

	uint_t t, idx = task->idx, __length__;
	string_buffer_t sb = new_string_buffer_with_string( task->buffer );
	lex_entry_t e = friso_dic_get( friso->dic, __LEX_CJK_WORDS__, sb->buffer );
	/*
	 * here bak the e->length in the task->hits->type.
	 *		we will use it to count the task->idx.
	 * for the sake of use less variable.
	 */
	__length__ = e->length;

	for ( t = 1; 
		t < friso->max_len 
		&& ( task->bytes = read_next_word( task, &idx, task->buffer ) ) != 0; t++ ) {
		
		task->unicode = get_utf8_unicode( task->buffer );

		if ( utf8_whitespace( task->unicode ) ) break;
		if ( ! utf8_cjk_string( task->unicode ) ) break;

		string_buffer_append( sb, task->buffer );

		//check the existence of the word by search the dictionary.
		if ( friso_dic_match( friso->dic, __LEX_CJK_WORDS__, sb->buffer ) ) {
			e = friso_dic_get( friso->dic, __LEX_CJK_WORDS__, sb->buffer );
		}
	}

	//correct the offset of the segment.
	task->idx += ( e->length - __length__ );
	free_string_buffer( sb );							//free the buffer

	//reset the hits.
	task->hits->word = e->word;
	task->hits->type = __FRISO_SYS_WORDS__;

	return task->hits;
}
Exemplo n.º 4
0
/*
 * load the lexicon configuration file.
 *        and load all the valid lexicon from the configuration file.
 *
 * @param friso        friso instance
 * @param    config    friso_config instance
 * @param _path        dictionary directory
 * @param _limitts    words length limit    
 */
FRISO_API void friso_dic_load_from_ifile( 
        friso_t friso, 
        friso_config_t config,
        fstring _path,
        uint_t _limits  ) 
{

    //1.parse the configuration file.
    FILE *__stream;
    char __chars__[1024], __key__[30], *__line__;
    uint_t __length__, i, t;
    friso_lex_t lex_t;
    string_buffer_t sb;

    //get the lexicon configruation file path
    sb = new_string_buffer();
    string_buffer_append( sb, _path );
    string_buffer_append( sb, __FRISO_LEX_IFILE__ );
    //printf("%s\n", sb->buffer);

    if ( ( __stream = fopen( sb->buffer, "rb" ) ) != NULL ) 
    {
        while ( ( __line__ = 
                    file_get_line( __chars__, __stream ) ) != NULL ) 
        {
            //comment filter.
            if ( __line__[0] == '#' ) continue;
            if ( __line__[0] == '\0' ) continue;

            __length__ = strlen( __line__ );
            //item start
            if ( __line__[ __length__ - 1 ] == '[' ) 
            {
                //get the type key
                for ( i = 0; i < __length__
                        && ( __line__[i] == ' ' || __line__[i] == '\t' ); i++ ); 
                for ( t = 0; i < __length__; i++,t++ ) {
                    if ( __line__[i] == ' ' 
                            || __line__[i] == '\t' || __line__[i] == ':' ) break;
                    __key__[t] = __line__[i];
                }
                __key__[t] = '\0';

                //get the lexicon type
                lex_t = get_lexicon_type_with_constant(__key__);
                if ( lex_t == -1 ) continue; 

                //printf("key=%s, type=%d\n", __key__, lex_t );
                while ( ( __line__ = file_get_line( __chars__, __stream ) ) != NULL ) 
                {
                    //comments filter.
                    if ( __line__[0] == '#' ) continue;
                    if ( __line__[0] == '\0' ) continue; 

                    __length__ = strlen( __line__ );
                    if ( __line__[ __length__ - 1 ] == ']' ) break;

                    for ( i = 0; i < __length__ 
                            && ( __line__[i] == ' ' || __line__[i] == '\t' ); i++ );
                    for ( t = 0; i < __length__; i++,t++ ) {
                        if ( __line__[i] == ' ' 
                                || __line__[i] == '\t' || __line__[i] == ';' ) break;
                        __key__[t] = __line__[i]; 
                    }
                    __key__[t] = '\0';

                    //load the lexicon item from the lexicon file.
                    string_buffer_clear( sb );
                    string_buffer_append( sb, _path );
                    string_buffer_append( sb, __key__ );
                    //printf("key=%s, type=%d\n", __key__, lex_t);
                    friso_dic_load( friso, config, lex_t, sb->buffer, _limits );
                }

            } 

        } //end while

        fclose( __stream );
    } else {
        printf("Warning: Fail to open the lexicon configuration file %s\n", sb->buffer);
    }

    free_string_buffer(sb);    
}
Exemplo n.º 5
0
int ssh2_auth(const char *hostip,
              const char *username,
              const char *password, 
              char **errmsg)
{
    unsigned long hostaddr;
    int sock;
    struct sockaddr_in sin;
    LIBSSH2_SESSION *session;
    int rc;
    LIBSSH2_KNOWNHOSTS *nh;
    struct StringBuffer *sberrmsg = new_string_buffer();

#ifdef WIN32
    WSADATA wsadata;
    WSAStartup(MAKEWORD(2,0), &wsadata);
#endif

    rc = libssh2_init (0);
    if (rc != 0) {
        sbprintf (sberrmsg, "libssh2 initialization failed (%d)\n", rc);
        *errmsg = free_string_buffer(sberrmsg, 0);
        return 1;
    }

    hostaddr = inet_addr(hostip);

    /* Ultra basic "connect to port 22 on localhost"
     * Your code is responsible for creating the socket establishing the
     * connection
     */
    sock = socket(AF_INET, SOCK_STREAM, 0);

    sin.sin_family = AF_INET;
    sin.sin_port = htons(22);
    sin.sin_addr.s_addr = hostaddr;
    if (connect(sock, (struct sockaddr*)(&sin),
                sizeof(struct sockaddr_in)) != 0) {
        sbprintf(sberrmsg, "failed to connect!\n");
        *errmsg = free_string_buffer(sberrmsg, 0);
        return -1;
    }

    /* Create a session instance */
    session = libssh2_session_init();
    if (!session)
        return -1;

    /* tell libssh2 we want it all done non-blocking */
    libssh2_session_set_blocking(session, 0);

    /* ... start it up. This will trade welcome banners, exchange keys,
     * and setup crypto, compression, and MAC layers
     */
    while ((rc = libssh2_session_handshake(session, sock)) ==
           LIBSSH2_ERROR_EAGAIN);
    if (rc) {
        sbprintf(sberrmsg, "Failure establishing SSH session: %d\n", rc);
        *errmsg = free_string_buffer(sberrmsg, 0);
        return -1;
    }

    nh = libssh2_knownhost_init(session);
    if(!nh) {
        /* eeek, do cleanup here */
        return 2;
    }

    /* We could authenticate via password */
    while ((rc = libssh2_userauth_password(session, username, password)) ==
           LIBSSH2_ERROR_EAGAIN);
    if (rc) {
        sbprintf(sberrmsg, "Authentication by password failed.\n");
        *errmsg = free_string_buffer(sberrmsg, 0);
    }
    else {
        free_string_buffer(sberrmsg, 1);
    }


    libssh2_session_disconnect(session,
                               "Normal Shutdown, Thank you for playing");
    libssh2_session_free(session);

#ifdef WIN32
    closesocket(sock);
#else
    close(sock);
#endif

    libssh2_exit();

    return rc;
}
Exemplo n.º 6
0
//get the next latin word from the current position.
__STATIC_API__ friso_hits_t next_basic_latin( friso_t friso, friso_task_t task ) {

	char __convert = 0, t = 0;
	string_buffer_t sb, temp;
	lex_entry_t e = NULL;

	//full-half width and upper-lower case exchange.
	task->unicode = get_utf8_unicode( task->buffer );
	___LATAIN_FULL_UPPER_CHECK___

	//creat a new string buffer and append the task->buffer insite.
	sb = new_string_buffer_with_string( task->buffer );


	//segmentation.
	while ( ( task->bytes = read_next_word( task, &task->idx, task->buffer ) ) != 0 ) {

		task->unicode = get_utf8_unicode( task->buffer );

		if ( utf8_whitespace( task->unicode ) ) break;
		if ( utf8_en_punctuation( task->unicode ) && ! utf8_keep_punctuation( task->buffer ) ) break; 
		if ( ! ( utf8_halfwidth_letter_digit( task->unicode ) || utf8_fullwidth_letter_digit( task->unicode ) ) ) {
			task->idx -= task->bytes;
			t = 1;
			break;
		}

		//full-half width and upper-lower case convert
		___LATAIN_FULL_UPPER_CHECK___

		//append the word the buffer.
		string_buffer_append( sb, task->buffer );
	}

	/*clear the useless english punctuation from
		the end of the buffer.*/
	for ( ; sb->length > 0 
			&& sb->buffer[ sb->length - 1 ] != '%' 
			&& is_en_punctuation( sb->buffer[ sb->length - 1 ] ); ) {
		sb->buffer[ --sb->length ] = '\0';
	}

	/*
	 * find the chinese or english mixed word.
	 * 		or single chinese units.*/
	if ( t == 1 ) {
		if ( utf8_cjk_string( task->unicode ) ) {
			//temp string buffer.
			temp = new_string_buffer_with_string( sb->buffer );

			for ( t = 0; 
				t < friso->mix_len 
				&& ( task->bytes = read_next_word( task , &task->idx, task->buffer ) ) != 0; t++ ) {
				
				task->unicode = get_utf8_unicode( task->buffer );

				if ( ! utf8_cjk_string( task->unicode ) ) {
					task->idx -= task->bytes;
					break;
				}

				string_buffer_append( temp, task->buffer );

				//check the mixed word dictionary.
				if ( friso_dic_match( friso->dic, __LEX_MIX_WORDS__, temp->buffer ) ) {
					__convert = 1;
					//get the lexicon entry from the dictionary.
					e = friso_dic_get( friso->dic, __LEX_MIX_WORDS__, temp->buffer );
				}
			}

			//correct the segmentation offset.
			task->idx -= ( temp->length - ( e == NULL ? sb->length : e->length ) );
			free_string_buffer( temp );

			//no match for mix word, try to find a single chinese unit.
			if ( __convert == 0 ) {
				//check if it is string made up with numeric
				if ( utf8_numeric_string( sb->buffer ) 
						&& ( task->bytes = read_next_word( task, &task->idx, task->buffer ) ) != 0 ) {

					//check the single chinese units dictionary.
					if ( friso_dic_match( friso->dic, __LEX_CJK_UNITS__, task->buffer ) ) {
						string_buffer_append( sb, task->buffer );
					} else {
						task->idx -= task->bytes;
					}
				}
			}	//end convert condition

		}
	}


	if ( __convert == 1 ) {
		free_string_buffer( sb );
		task->hits->word = e->word;
		task->hits->type = __FRISO_SYS_WORDS__;
	} else {
		/*
		 * adjust the string buffer.
		 *		here we do not trim the buffer cause its allocations will be free
		 *	after the call of friso_next - sooner or later it will be released.
		 *	if your memory almost run out, you should call string_buffer_trim.
		 *	or we save the time to do the allocations and copy the buffer insite.
		 */
		//string_buffer_trim( sb );
		task->hits->word = string_buffer_devote( sb );
		task->hits->type = __FRISO_NEW_WORDS__;
	}

	return task->hits;
}
Exemplo n.º 7
0
int ssh2_forward_port(const char *hostip,
                      const char *username,
                      const char *password,
                      int listenport,
                      const char *desthost,
                      int destport,
                      int *initialized,
                      int *finished,
                      char **errmsg)
{
    int rc, sock = -1, listensock = -1, forwardsock = -1, i;
    struct sockaddr_in sin;
    socklen_t sinlen;
    LIBSSH2_SESSION *session;
    LIBSSH2_CHANNEL *channel = NULL;
    const char *shost;
    unsigned int sport;
    fd_set fds;
    struct timeval tv;
    ssize_t len, wr;
    char buf[16384];
    struct StringBuffer *sberrmsg = new_string_buffer();

#ifdef WIN32
    char sockopt;
    WSADATA wsadata;

    WSAStartup(MAKEWORD(2,0), &wsadata);
#else
    int sockopt;
#endif

    rc = libssh2_init (0);
    if (rc != 0) {
        sbprintf (sberrmsg, "libssh2 initialization failed (%d)\n", rc);
        return 1;
    }

    /* Connect to SSH server */
    sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
    memset(&sin, 0, sizeof(sin));
    sin.sin_family = AF_INET;
    if (INADDR_NONE == (sin.sin_addr.s_addr = inet_addr(hostip))) {
        perror("inet_addr");
        return -1;
    }
    sin.sin_port = htons(22);
    if (connect(sock, (struct sockaddr*)(&sin),
                sizeof(struct sockaddr_in)) != 0) {
        sbprintf(sberrmsg, "failed to connect!\n");
        return -1;
    }

    /* Create a session instance */
    session = libssh2_session_init();
    if(!session) {
        sbprintf(sberrmsg, "Could not initialize SSH session!\n");
        return -1;
    }

    /* ... start it up. This will trade welcome banners, exchange keys,
     * and setup crypto, compression, and MAC layers
     */
    rc = libssh2_session_handshake(session, sock);
    if(rc) {
        sbprintf(sberrmsg, "Error when starting up SSH session: %d\n", rc);
        return -1;
    }

    if (libssh2_userauth_password(session, username, password)) {
        sbprintf(sberrmsg, "Authentication by password failed.\n");
        goto shutdown;
    }

    listensock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
    sin.sin_family = AF_INET;
    sin.sin_port = htons(listenport);
    if (INADDR_NONE == (sin.sin_addr.s_addr = inet_addr("127.0.0.1"))) {
        sbprintf(sberrmsg, "inet_addr: %s.", strerror(errno));
        goto shutdown;
    }
    sockopt = 1;
    setsockopt(listensock, SOL_SOCKET, SO_REUSEADDR, &sockopt, sizeof(sockopt));
    sinlen=sizeof(sin);
    if (-1 == bind(listensock, (struct sockaddr *)&sin, sinlen)) {
        sbprintf(sberrmsg, "bind: %s.", strerror(errno));
        goto shutdown;
    }
    if (-1 == listen(listensock, 2)) {
        sbprintf(sberrmsg, "listen: %s.", strerror(errno));
        goto shutdown;
    }

    printf("Waiting for TCP connection on %s:%d...\n",
        inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
    *initialized = 1;

    forwardsock = accept(listensock, (struct sockaddr *)&sin, &sinlen);
    if (-1 == forwardsock) {
        sbprintf(sberrmsg, "accept: %s.", strerror(errno));
        goto shutdown;
    }

    shost = inet_ntoa(sin.sin_addr);
    sport = ntohs(sin.sin_port);

    printf("Forwarding connection from %s:%d here to remote %s:%d\n", shost,
        sport, desthost, destport);

    channel = libssh2_channel_direct_tcpip_ex(session, desthost,
        destport, shost, sport);
    if (!channel) {
        sbprintf(sberrmsg, "Could not open the direct-tcpip channel!\n"
                "(Note that this can be a problem at the server!"
                " Please review the server logs.)\n");
        goto shutdown;
    }

    /* Must use non-blocking IO hereafter due to the current libssh2 API */
    libssh2_session_set_blocking(session, 0);

    while (1) {
        if(*finished) libssh2_channel_send_eof(channel);
        FD_ZERO(&fds);
        FD_SET(forwardsock, &fds);
        tv.tv_sec = 0;
        tv.tv_usec = 100000;
        rc = select(forwardsock + 1, &fds, NULL, NULL, &tv);
        if (-1 == rc) {
            sbprintf(sberrmsg, "select: %s.", strerror(errno));
            goto shutdown;
        }
        if (rc && FD_ISSET(forwardsock, &fds)) {
            len = recv(forwardsock, buf, sizeof(buf), 0);
            if (len < 0) {
                perror("read");
                goto shutdown;
            } else if (0 == len) {
                printf("The client at %s:%d disconnected!\n", shost, sport);
                goto shutdown;
            }
            wr = 0;
            do {
                i = libssh2_channel_write(channel, buf, len);
                if (i < 0) {
                    sbprintf(sberrmsg, "libssh2_channel_write: %d\n", i);
                    goto shutdown;
                }
                wr += i;
            } while(i > 0 && wr < len);
        }
        while (1) {
            len = libssh2_channel_read(channel, buf, sizeof(buf));
            if (LIBSSH2_ERROR_EAGAIN == len)
                break;
            else if (len < 0) {
                sbprintf(sberrmsg, "libssh2_channel_read: %d", (int)len);
                goto shutdown;
            }
            wr = 0;
            while (wr < len) {
                i = send(forwardsock, buf + wr, len - wr, 0);
                if (i <= 0) {
                    perror("write");
                    goto shutdown;
                }
                wr += i;
            }
            if (libssh2_channel_eof(channel)) {
                printf("The server at %s:%d disconnected!\n",
                    desthost, destport);
                goto shutdown;
            }
        }
    }

shutdown:
 fprintf(stderr, "finished = %d\n", *finished);
#ifdef WIN32
    closesocket(forwardsock);
    closesocket(listensock);
#else
    close(forwardsock);
    close(listensock);
#endif
    if (channel)
        libssh2_channel_free(channel);
    libssh2_session_disconnect(session, "Client disconnecting normally");
    libssh2_session_free(session);

#ifdef WIN32
    closesocket(sock);
#else
    close(sock);
#endif

    libssh2_exit();

    if(string_buffer_is_empty(sberrmsg)) {
       free_string_buffer(sberrmsg, 1);
       return 0;
    }

    *errmsg = free_string_buffer(sberrmsg, 0);
    return 1;
}