Beispiel #1
0
static
rc_t GetNewPassword(const struct KFile* pwd_in, struct KFile* pwd_out, char* buf)
{
    rc_t rc = KFileWrite ( pwd_out, 0, KR_PWD_PROMPT_1, string_measure(KR_PWD_PROMPT_1, NULL), NULL);
    if (rc == 0)
    {
        char buf1[MaxPwdSize];
        size_t last_pos = 0;
        rc = ReadPassword(pwd_in, & last_pos, buf1, MaxPwdSize);
        if (rc == 0)
        {
            rc = KFileWrite ( pwd_out, 
                              string_measure(KR_PWD_PROMPT_1, NULL), 
                              KR_PWD_PROMPT_2, string_measure(KR_PWD_PROMPT_2, NULL), NULL );
            if (rc == 0)
            {
                char buf2[MaxPwdSize];
                rc = ReadPassword(pwd_in, & last_pos, buf2, sizeof(buf2));
                if (rc == 0)
                {
                    size_t pwd_size = string_measure(buf1, NULL);
                    if (string_cmp(buf1, pwd_size, buf2, string_measure(buf2, NULL), MaxPwdSize) != 0)
                        rc = RC(rcApp, rcEncryptionKey, rcCreating, rcParam, rcInconsistent);
                    else
                        string_copy(buf, MaxPwdSize, buf1, pwd_size + 1);
                }
            }
        }
    }
    return rc;
}
Beispiel #2
0
static
rc_t GetPassword(const struct KFile* pwd_in, struct KFile* pwd_out, char* buf)
{
    rc_t rc = KFileWrite ( pwd_out, 0, KR_PWD_PROMPT_1, string_measure(KR_PWD_PROMPT_1, NULL), NULL);
    if (rc == 0)
    {
        char buf1[MaxPwdSize];
        size_t last_pos = 0;
        rc = ReadPassword(pwd_in, & last_pos, buf1, MaxPwdSize);
        if (rc == 0)
            string_copy(buf, MaxPwdSize, buf1, string_measure(buf1, NULL) + 1);
    }
    return rc;
}
Beispiel #3
0
static char * matcher_get_col_cast( const p_mcol col, const char *s_type )
{
    char * res;
    size_t idx;
    uint32_t len = string_measure ( col->name, NULL ) + 4;
    len += string_measure ( s_type, NULL );
    res = malloc( len );
    if ( res == NULL ) return res;
    res[ 0 ] = '(';
    idx = string_copy_measure ( &(res[ 1 ]), len-1, s_type );
    res[ idx + 1 ] = ')';
    string_copy_measure ( &(res[ idx + 2 ]), len-(idx+2), col->name );
    return res;
}
Beispiel #4
0
static void store_date( vdb_info_date * d, split_date_ctx * ctx )
{
    uint32_t l, value;

    ctx->tmp[ ctx->dst ] = 0;
    l = string_measure ( ctx->tmp, NULL );
    value = atoi( ctx->tmp );
    if ( l == 4 )
        d->year = value;
    else if ( ( l == 8 ) && ( ctx->tmp[ 2 ] == ':' ) && ( ctx->tmp[ 5 ] == ':' ) )
    {
        ctx->tmp[ 2 ] = 0;
        d->hour = atoi( ctx->tmp );
        ctx->tmp[ 5 ] = 0;
        d->minute = atoi( &( ctx->tmp[ 3 ] ) );
    }
    else if ( l == 3 )
    {
        d->month = str_to_month_num( ctx->tmp );
    }
    else
    {
        d->day = value;
    }
    ctx->dst = 0;
}
Beispiel #5
0
static rc_t make_subfilename( const sorter_params * params, uint32_t id, char * buffer, size_t buflen )
{
    rc_t rc;
    size_t num_writ;
    if ( params->temp_path != NULL )
    {
        uint32_t l = string_measure( params->temp_path, NULL );
        if ( l == 0 )
        {
            rc = RC( rcVDB, rcNoTarg, rcConstructing, rcParam, rcInvalid );
            ErrMsg( "make_subfilename.string_measure() = 0 -> %R", rc );
        }
        else
        {
            if ( params->temp_path[ l-1 ] == '/' )
                rc = string_printf( buffer, buflen, &num_writ, "%ssub_%d_%d.dat",
                        params->temp_path, params->prefix, id );
            else
                rc = string_printf( buffer, buflen, &num_writ, "%s/sub_%d_%d.dat",
                        params->temp_path, params->prefix, id );
        }
    }
    else
        rc = string_printf( buffer, buflen, &num_writ, "sub_%d_%d.dat",
                params->prefix, id );

    if ( rc != 0 )
        ErrMsg( "make_subfilename.string_printf() -> %R", rc );
    return rc;
}
Beispiel #6
0
static
VRemoteProtocols parseProtocol ( const char * protocol, rc_t * prc )
{
    VRemoteProtocols protocols = 0;

    int n = 0;
    int j = 0;

    uint32_t sz = 0;

    assert ( protocol && prc );

    sz = string_measure ( protocol, NULL );

    while ( sz > 0 ) {
        bool found = false;
        int l = sz;

        char * c = string_chr ( protocol, sz, ',' );
        if ( c != NULL )
            l = c - protocol;

        for ( j = 0;
                j < sizeof PROTOCOLS / sizeof PROTOCOLS [ 0 ]; ++ j )
        {
            const TProtocol * p = & PROTOCOLS [ j ];
            uint32_t max_chars = p -> s > l ? p -> s : l;
            if (string_cmp ( protocol, l, p -> n, p -> s, max_chars ) == 0) {
                VRemoteProtocols next = p -> p << ( 3 * n );
                protocols = next + protocols;
                ++n;

                assert ( l >= p -> s );

                protocol += p -> s;
                sz -= p -> s;

                found = true;
                break;
            }
        }

        if ( ! found ) {
            * prc = RC ( rcExe, rcArgv, rcParsing, rcParam, rcInvalid );
            PLOGERR ( klogErr, ( klogErr, * prc, "Bad protocol '$(p)'",
                "p=%.*s", l, protocol ) );
            break;
        }

        if ( c != NULL ) {
            ++ protocol;
            -- sz;
        }
    }

    return protocols;
}
Beispiel #7
0
void CC print_into_screen( tui_screen * screen, int x, int y, const tui_ac * v,
                           const char * s, uint32_t l )
{
    if ( y < LINES_PER_SCREEN && x < CHR_PER_LINE )
    {
        tui_line * line = &screen->lines[ y ];
        uint32_t column, idx, sl = string_measure ( s, NULL );
        bool any_chars_dirty = false;
        if ( l > CHR_PER_LINE ) l =CHR_PER_LINE;
        if ( x + l > CHR_PER_LINE ) l = ( CHR_PER_LINE - x );
        if ( sl > l ) sl = l;
        if ( x + sl > CHR_PER_LINE ) sl = ( CHR_PER_LINE - x );

        for ( column = x, idx = 0; idx < sl; ++column, ++idx )
        {
            tui_ac * ac = &line->ac[ column ];
            if ( ac->attr != v->attr ||
                 ac->fg != v->fg ||
                 ac->bg != v->bg ||
                 line->chars[ column ] != s[ idx ] )
            {
                ac->attr = v->attr;
                ac->fg = v->fg;
                ac->bg = v->bg;
                line->chars[ column ] = s[ idx ];
                line->dirty[ column ] = true;
                any_chars_dirty = true;
            }
        }

        if ( sl < l )
        {
            for ( column = x + sl, idx = sl; idx < l; ++column, ++idx )
            {
                tui_ac * ac = &line->ac[ column ];
                if ( ac->attr != v->attr ||
                     ac->fg != v->fg ||
                     ac->bg != v->bg ||
                     line->chars[ column ] != ' ' )
                {
                    ac->attr = v->attr;
                    ac->fg = v->fg;
                    ac->bg = v->bg;
                    line->chars[ column ] = ' ';
                    line->dirty[ column ] = true;
                    any_chars_dirty = true;
                }
            }
        }

        if ( any_chars_dirty )
        {
            line->line_dirty = true;
            screen->dirty = true;
        }
    }
}
Beispiel #8
0
static void string_ctx_add( p_string_ctx ctx, char *s )
{
    uint32_t len = string_measure ( s, NULL );
    if ( len > 0 )
    {
        if ( ctx->len == 0 )
            ctx->s = malloc( len + 1 );
        else
            ctx->s = realloc( ctx->s, ctx->len + len );
        memcpy( &(ctx->s[ctx->len]), s, len );
        ctx->len += len;
    }
}
Beispiel #9
0
rc_t ArchiveAndEncrypt(KDirectory* wd, const char* inpath, const char* outpath, const char* passwd)
{
    const KDirectory* d;
    rc_t rc = KDirectoryOpenDirRead (wd, &d, false, "%s", inpath);
    if (rc == 0)
    {
        const KFile* infile;
        rc_t rc2;
    
        rc = KDirectoryOpenTocFileRead (d, &infile, 4, NULL, NULL, NULL);
        if (rc == 0)
        {
            KFile* outfile;
                
            /* if the file exists, add write access */
            KDirectorySetAccess( wd, false, 0600, 0777, "%s", outpath );
            rc = KDirectoryCreateFile(wd, &outfile, false, 0600, kcmCreate|kcmInit, "%s", outpath);
            if ( rc == 0 )
            {
                KFile* enc_outfile;
                KKey key;
                rc = KKeyInitRead(&key, kkeyAES256, passwd, string_measure(passwd, NULL));
                if ( rc == 0 )
                    rc = KEncFileMakeWrite(&enc_outfile, outfile, &key);
                    
                if (rc == 0)
                    rc = copy_file(infile, enc_outfile);
        
                rc2 = KFileRelease(outfile);
                if (rc == 0)
                    rc = rc2;
                /* remove write access */
                rc2 = KDirectorySetAccess( wd, false, 0400, 0777, "%s", outpath );
                if (rc == 0)
                    rc = rc2;
                rc2 = KFileRelease(enc_outfile);
                if (rc == 0)
                    rc = rc2;
            }
            rc2 = KFileRelease(infile);
            if (rc == 0)
                rc = rc2;
        }
        rc2 = KDirectoryRelease(d);
        if (rc == 0)
            rc = rc2;
    }
    return rc;
}
Beispiel #10
0
static void split_date( vdb_info_date * d )
{
    uint32_t i, l = string_measure ( d->date, NULL );
    split_date_ctx ctx;
    memset( &ctx, 0, sizeof ctx );
    for ( i = 0; i < l; ++i )
    {
        char c = d->date[ i ];
        if ( c == ' ' )
            store_date( d, &ctx );
        else
            ctx.tmp[ ctx.dst++ ] = c;
    }
    if ( ctx.dst > 0 )
        store_date( d, &ctx );
}
Beispiel #11
0
static void split_vers( vdb_info_vers * vers )
{
    uint32_t i, l = string_measure ( vers->s_vers, NULL );
    split_vers_ctx ctx;
    memset( &ctx, 0, sizeof ctx );
    for ( i = 0; i < l; ++i )
    {
        char c = vers->s_vers[ i ];
        if ( c >= '0' && c <= '9' )
            ctx.tmp[ ctx.dst++ ] = c;
        else if ( c == '.' )
            store_vers( vers, &ctx );
    }
    if ( ctx.dst > 0 )
        store_vers( vers, &ctx );
}
Beispiel #12
0
/* parse the given string and insert the found ranges 
   into the number-generator, fixes eventual overlaps */
rc_t num_gen_parse( num_gen* self, const char* src )
{
    size_t i, n;
    num_gen_parse_ctx ctx;
    rc_t rc = 0;

    if ( self == NULL )
        return RC( rcVDB, rcNoTarg, rcParsing, rcSelf, rcNull );
    if ( self == NULL )
        return RC( rcVDB, rcNoTarg, rcParsing, rcParam, rcNull );

    n = string_measure ( src, NULL );
    if ( n == 0 )
        return RC( rcVDB, rcNoTarg, rcParsing, rcParam, rcEmpty );

    ctx.num_str_idx = 0;
    ctx.this_is_the_first_number = true;
    for ( i = 0; i < n && rc == 0; ++i )
    {
        switch ( src[ i ] )
        {
        /* a dash switches from N1-mode into N2-mode */
        case '-' :
            num_gen_convert_ctx( &ctx );
            break;

        /* a comma ends a single number or a range */
        case ',' :
            rc = num_gen_convert_and_add_ctx( self, &ctx );
            break;

        /* in both mode add the char to the temp string */
        default:
            if ( ( src[i]>='0' )&&( src[i]<='9' )&&( ctx.num_str_idx < MAX_NUM_STR ) )
                ctx.num_str[ ctx.num_str_idx++ ] = src[ i ];
            break;
        }
    }
    /* dont forget to add what is left in ctx.num_str ... */
    if ( ctx.num_str_idx > 0 )
        rc = num_gen_convert_and_add_ctx( self, &ctx );
    if ( rc == 0 )
        rc = num_gen_fix_overlaps( self, NULL );
    return rc;
}
Beispiel #13
0
rc_t OpenDatabase(KKeyRing* self)
{
    rc_t rc;
    const KFile* enc_infile;
    
    assert(self);
    rc = KDirectoryOpenFileRead(self->wd, &enc_infile, "%s", self->path);
    if ( rc == 0)
    {
        rc_t rc2;
        const KFile* infile;
        KKey key;
        rc = KKeyInitRead(&key, kkeyAES256, self->passwd, string_measure(self->passwd, NULL));
        if ( rc == 0 )
        {
            rc = KEncFileMakeRead (&infile, enc_infile, &key);
            if (rc == 0)
            {   
                const KDirectory* arc;
                rc = KDirectoryOpenArcDirRead_silent_preopened(self->wd, &arc, true, "/keyring", tocKFile, (void*)infile, KArcParseSRA, NULL, NULL);
                if (rc == 0)
                {
                    /*  Hack: we violate the KDirectory object interface in order for VDBManagerMakeUpdate to succeed,
                        since it would refuse to open a read-only dir (i.e. archive);
                        We will only read from the object, though.
                    */
                    ((KDirectory*)arc)->read_only = false;
                    rc = KeyRingDatabaseLoad(self->data, arc, "/keyring");
                    rc2 = KDirectoryRelease(arc);
                    if (rc == 0)
                        rc = rc2;
                }
                rc2 = KFileRelease(infile);
                if (rc == 0)
                    rc = rc2;
            }
        }
            
        rc2 = KFileRelease(enc_infile);
        if (rc == 0)
            rc = rc2;
    }
    return rc;
}
Beispiel #14
0
rc_t col_defs_as_string( col_defs* defs, char ** dst, bool only_requested )
{
    uint32_t idx, count, total = 0, dst_idx = 0;

    if ( defs == NULL )
        return RC( rcExe, rcNoTarg, rcResolving, rcSelf, rcNull );
    if ( dst == NULL )
        return RC( rcExe, rcNoTarg, rcResolving, rcParam, rcNull );
    *dst = NULL;
    count = VectorLength( &(defs->cols) );
    for ( idx = 0;  idx < count; ++idx )
    {
        p_col_def col = (p_col_def) VectorGet ( &(defs->cols), idx );
        if ( col != NULL )
        {
            bool use_this = ( only_requested ) ? col->requested : true;
            if ( use_this )
                total += ( string_measure ( col->name, NULL ) + 1 );
        }
    }
    *dst = malloc( total + 1 );
    if ( *dst == NULL )
        return RC( rcVDB, rcNoTarg, rcResolving, rcMemory, rcExhausted );
    *dst[0] = 0;
    for ( idx = 0;  idx < count; ++idx )
    {
        p_col_def col = (p_col_def) VectorGet ( &(defs->cols), idx );
        if ( col != NULL )
        {
            bool use_this = ( only_requested ) ? col->requested : true;
            if ( use_this )
            {
                dst_idx += string_copy_measure ( &((*dst)[dst_idx]), total-dst_idx, col->name );
                (*dst)[dst_idx++] = ',';
            }
        }
    }
    if ( dst_idx > 0 )
        (*dst)[dst_idx-1] = 0;
    return 0;
}
Beispiel #15
0
rc_t KeyRingDatabaseSave ( struct KeyRingData* self, struct KDirectory* wd, const char* path )
{
    rc_t rc;
    VDBManager* vdbMgr;
    rc = VDBManagerMakeUpdate( &vdbMgr, wd );
    if (rc == 0)
    {
        VSchema* schema;
        rc = VDBManagerMakeSchema(vdbMgr, &schema);
        if (rc == 0)
        {
            rc = VSchemaParseText ( schema, "keyring_schema", schema_text, string_measure(schema_text, NULL) );
            if (rc == 0)
            {   /* create a database */
                VDatabase* db;
                rc = VDBManagerCreateDB(vdbMgr, & db, schema, "keyring:KEYRING", kcmCreate | kcmMD5, path);
                if (rc == 0)
                {   
                    rc_t rc2;
                    rc = SaveProjects(&self->projects, db);
                    if (rc == 0)
                        rc = SaveObjects(&self->objects, db);
                    /*TODO: SaveKeys */
                    rc2 = VDatabaseRelease(db);
                    if (rc == 0)
                        rc = rc2;
                }

            }
            VSchemaRelease(schema);
        }
        VDBManagerRelease(vdbMgr);
    }

    return rc;
}