Beispiel #1
0
static krb5_error_code KRB5_CALLCONV
scc_resolve(krb5_context context, krb5_ccache *id, const char *res)
{
    krb5_scache *s;
    int ret;

    s = scc_alloc(context, res);
    if (s == NULL) {
	krb5_set_error_message(context, KRB5_CC_NOMEM,
			       N_("malloc: out of memory", ""));
	return KRB5_CC_NOMEM;
    }

    ret = make_database(context, s);
    if (ret) {
	scc_free(s);
	return ret;
    }

    ret = sqlite3_bind_text(s->scache_name, 1, s->name, -1, NULL);
    if (ret != SQLITE_OK) {
	krb5_set_error_message(context, ENOMEM,
			       "bind name: %s", sqlite3_errmsg(s->db));
	scc_free(s);
	return ENOMEM;
    }

    if (sqlite3_step(s->scache_name) == SQLITE_ROW) {

	if (sqlite3_column_type(s->scache_name, 0) != SQLITE_INTEGER) {
	    sqlite3_reset(s->scache_name);
	    krb5_set_error_message(context, KRB5_CC_END,
				   N_("Cache name of wrong type "
				      "for scache %s", ""),
				   s->name);
	    scc_free(s);
	    return KRB5_CC_END;
	}

	s->cid = sqlite3_column_int(s->scache_name, 0);
    } else {
	s->cid = SCACHE_INVALID_CID;
    }
    sqlite3_reset(s->scache_name);

    (*id)->data.data = s;
    (*id)->data.length = sizeof(*s);

    return 0;
}
Beispiel #2
0
static krb5_error_code KRB5_CALLCONV
scc_get_principal(krb5_context context,
		  krb5_ccache id,
		  krb5_principal *principal)
{
    krb5_scache *s = SCACHE(id);
    krb5_error_code ret;
    const char *str;

    *principal = NULL;

    ret = make_database(context, s);
    if (ret)
	return ret;

    sqlite3_bind_int(s->scache, 1, s->cid);

    if (sqlite3_step(s->scache) != SQLITE_ROW) {
	sqlite3_reset(s->scache);
	krb5_set_error_message(context, KRB5_CC_END,
			       N_("No principal for cache SCC:%s:%s", ""),
			       s->name, s->file);
	return KRB5_CC_END;
    }

    if (sqlite3_column_type(s->scache, 0) != SQLITE_TEXT) {
	sqlite3_reset(s->scache);
	krb5_set_error_message(context, KRB5_CC_END,
			       N_("Principal data of wrong type "
				  "for SCC:%s:%s", ""),
			       s->name, s->file);
	return KRB5_CC_END;
    }

    str = (const char *)sqlite3_column_text(s->scache, 0);
    if (str == NULL) {
	sqlite3_reset(s->scache);
	krb5_set_error_message(context, KRB5_CC_END,
			       N_("Principal not set for SCC:%s:%s", ""),
			       s->name, s->file);
	return KRB5_CC_END;
    }

    ret = krb5_parse_name(context, str, principal);

    sqlite3_reset(s->scache);

    return ret;
}
Beispiel #3
0
static krb5_error_code KRB5_CALLCONV
scc_get_first (krb5_context context,
	       krb5_ccache id,
	       krb5_cc_cursor *cursor)
{
    krb5_scache *s = SCACHE(id);
    krb5_error_code ret;
    struct cred_ctx *ctx;
    char *str = NULL, *name = NULL;

    *cursor = NULL;

    ctx = calloc(1, sizeof(*ctx));
    if (ctx == NULL) {
	krb5_set_error_message(context, ENOMEM,
			       N_("malloc: out of memory", ""));
	return ENOMEM;
    }

    ret = make_database(context, s);
    if (ret) {
	free(ctx);
	return ret;
    }

    if (s->cid == SCACHE_INVALID_CID) {
	krb5_set_error_message(context, KRB5_CC_END,
			       N_("Iterating a invalid scache %s", ""),
			       s->name);
	free(ctx);
	return KRB5_CC_END;
    }

    ret = asprintf(&name, "credIteration%pPid%d",
                   ctx, (int)getpid());
    if (ret < 0 || name == NULL) {
	krb5_set_error_message(context, ENOMEM,
			       N_("malloc: out of memory", ""));
	free(ctx);
	return ENOMEM;
    }

    ret = asprintf(&ctx->drop, "DROP TABLE %s", name);
    if (ret < 0 || ctx->drop == NULL) {
	krb5_set_error_message(context, ENOMEM,
			       N_("malloc: out of memory", ""));
	free(name);
	free(ctx);
	return ENOMEM;
    }

    ret = asprintf(&str, "CREATE TEMPORARY TABLE %s "
	     "AS SELECT oid,created_at FROM credentials WHERE cid = %lu",
	     name, (unsigned long)s->cid);
    if (ret < 0 || str == NULL) {
	free(ctx->drop);
	free(name);
	free(ctx);
	return ENOMEM;
    }

    ret = exec_stmt(context, s->db, str, KRB5_CC_IO);
    free(str);
    str = NULL;
    if (ret) {
	free(ctx->drop);
	free(name);
	free(ctx);
	return ret;
    }

    ret = asprintf(&str, "SELECT oid FROM %s ORDER BY created_at", name);
    if (ret < 0 || str == NULL) {
	exec_stmt(context, s->db, ctx->drop, 0);
	free(ctx->drop);
	free(name);
	free(ctx);
	return ret;
    }

    ret = prepare_stmt(context, s->db, &ctx->stmt, str);
    free(str);
    str = NULL;
    free(name);
    if (ret) {
	exec_stmt(context, s->db, ctx->drop, 0);
	free(ctx->drop);
	free(ctx);
	return ret;
    }

    ret = prepare_stmt(context, s->db, &ctx->credstmt,
		       "SELECT cred FROM credentials WHERE oid = ?");
    if (ret) {
	sqlite3_finalize(ctx->stmt);
	exec_stmt(context, s->db, ctx->drop, 0);
	free(ctx->drop);
	free(ctx);
	return ret;
    }

    *cursor = ctx;

    return 0;
}
Beispiel #4
0
static krb5_error_code KRB5_CALLCONV
scc_store_cred(krb5_context context,
	       krb5_ccache id,
	       krb5_creds *creds)
{
    sqlite_uint64 credid;
    krb5_scache *s = SCACHE(id);
    krb5_error_code ret;
    krb5_data data;

    ret = make_database(context, s);
    if (ret)
	return ret;

    ret = encode_creds(context, creds, &data);
    if (ret)
	return ret;

    sqlite3_bind_int(s->icred, 1, s->cid);
    {
	krb5_enctype etype = 0;
	int kvno = 0;
	Ticket t;
	size_t len;

	ret = decode_Ticket(creds->ticket.data,
			    creds->ticket.length, &t, &len);
	if (ret == 0) {
	    if(t.enc_part.kvno)
		kvno = *t.enc_part.kvno;

	    etype = t.enc_part.etype;

	    free_Ticket(&t);
	}

	sqlite3_bind_int(s->icred, 2, kvno);
	sqlite3_bind_int(s->icred, 3, etype);

    }

    sqlite3_bind_blob(s->icred, 4, data.data, data.length, free_data);
    sqlite3_bind_int(s->icred, 5, time(NULL));

    ret = exec_stmt(context, s->db, "BEGIN IMMEDIATE TRANSACTION", KRB5_CC_IO);
    if (ret) return ret;

    do {
	ret = sqlite3_step(s->icred);
    } while (ret == SQLITE_ROW);
    sqlite3_reset(s->icred);
    if (ret != SQLITE_DONE) {
	ret = KRB5_CC_IO;
	krb5_set_error_message(context, ret,
			       N_("Failed to add credential: %s", ""),
			       sqlite3_errmsg(s->db));
	goto rollback;
    }

    credid = sqlite3_last_insert_rowid(s->db);

    {
	bind_principal(context, s->db, s->iprincipal, 1, creds->server);
	sqlite3_bind_int(s->iprincipal, 2, 1);
	sqlite3_bind_int(s->iprincipal, 3, credid);

	do {
	    ret = sqlite3_step(s->iprincipal);
	} while (ret == SQLITE_ROW);
	sqlite3_reset(s->iprincipal);
	if (ret != SQLITE_DONE) {
	    ret = KRB5_CC_IO;
	    krb5_set_error_message(context, ret,
				   N_("Failed to add principal: %s", ""),
				   sqlite3_errmsg(s->db));
	    goto rollback;
	}
    }

    {
	bind_principal(context, s->db, s->iprincipal, 1, creds->client);
	sqlite3_bind_int(s->iprincipal, 2, 0);
	sqlite3_bind_int(s->iprincipal, 3, credid);

	do {
	    ret = sqlite3_step(s->iprincipal);
	} while (ret == SQLITE_ROW);
	sqlite3_reset(s->iprincipal);
	if (ret != SQLITE_DONE) {
	    ret = KRB5_CC_IO;
	    krb5_set_error_message(context, ret,
				   N_("Failed to add principal: %s", ""),
				   sqlite3_errmsg(s->db));
	    goto rollback;
	}
    }

    ret = exec_stmt(context, s->db, "COMMIT", KRB5_CC_IO);
    if (ret) return ret;

    return 0;

rollback:
    exec_stmt(context, s->db, "ROLLBACK", 0);

    return ret;
}
Beispiel #5
0
static krb5_error_code KRB5_CALLCONV
scc_initialize(krb5_context context,
	       krb5_ccache id,
	       krb5_principal primary_principal)
{
    krb5_scache *s = SCACHE(id);
    krb5_error_code ret;

    ret = make_database(context, s);
    if (ret)
	return ret;

    ret = exec_stmt(context, s->db, "BEGIN IMMEDIATE TRANSACTION", KRB5_CC_IO);
    if (ret) return ret;

    if (s->cid == SCACHE_INVALID_CID) {
	ret = create_cache(context, s);
	if (ret)
	    goto rollback;
    } else {
	sqlite3_bind_int(s->dcred, 1, s->cid);
	do {
	    ret = sqlite3_step(s->dcred);
	} while (ret == SQLITE_ROW);
	sqlite3_reset(s->dcred);
	if (ret != SQLITE_DONE) {
	    ret = KRB5_CC_IO;
	    krb5_set_error_message(context, ret,
				   N_("Failed to delete old "
				      "credentials: %s", ""),
				   sqlite3_errmsg(s->db));
	    goto rollback;
	}
    }

    ret = bind_principal(context, s->db, s->ucachep, 1, primary_principal);
    if (ret)
	goto rollback;
    sqlite3_bind_int(s->ucachep, 2, s->cid);

    do {
	ret = sqlite3_step(s->ucachep);
    } while (ret == SQLITE_ROW);
    sqlite3_reset(s->ucachep);
    if (ret != SQLITE_DONE) {
	ret = KRB5_CC_IO;
	krb5_set_error_message(context, ret,
			       N_("Failed to bind principal to cache %s", ""),
			       sqlite3_errmsg(s->db));
	goto rollback;
    }

    ret = exec_stmt(context, s->db, "COMMIT", KRB5_CC_IO);
    if (ret) return ret;

    return 0;

rollback:
    exec_stmt(context, s->db, "ROLLBACK", 0);

    return ret;

}
Beispiel #6
0
static krb5_error_code KRB5_CALLCONV
scc_move(krb5_context context, krb5_ccache from, krb5_ccache to)
{
    krb5_scache *sfrom = SCACHE(from);
    krb5_scache *sto = SCACHE(to);
    krb5_error_code ret;

    if (strcmp(sfrom->file, sto->file) != 0) {
	krb5_set_error_message(context, KRB5_CC_BADNAME,
			       N_("Can't handle cross database "
				  "credential move: %s -> %s", ""),
			       sfrom->file, sto->file);
	return KRB5_CC_BADNAME;
    }

    ret = make_database(context, sfrom);
    if (ret)
	return ret;

    ret = exec_stmt(context, sfrom->db,
		    "BEGIN IMMEDIATE TRANSACTION", KRB5_CC_IO);
    if (ret) return ret;

    if (sto->cid != SCACHE_INVALID_CID) {
	/* drop old cache entry */

	sqlite3_bind_int(sfrom->dcache, 1, sto->cid);
	do {
	    ret = sqlite3_step(sfrom->dcache);
	} while (ret == SQLITE_ROW);
	sqlite3_reset(sfrom->dcache);
	if (ret != SQLITE_DONE) {
	    krb5_set_error_message(context, KRB5_CC_IO,
				   N_("Failed to delete old cache: %d", ""),
				   (int)ret);
	    goto rollback;
	}
    }

    sqlite3_bind_text(sfrom->ucachen, 1, sto->name, -1, NULL);
    sqlite3_bind_int(sfrom->ucachen, 2, sfrom->cid);

    do {
	ret = sqlite3_step(sfrom->ucachen);
    } while (ret == SQLITE_ROW);
    sqlite3_reset(sfrom->ucachen);
    if (ret != SQLITE_DONE) {
	krb5_set_error_message(context, KRB5_CC_IO,
			       N_("Failed to update new cache: %d", ""),
			       (int)ret);
	goto rollback;
    }

    sto->cid = sfrom->cid;

    ret = exec_stmt(context, sfrom->db, "COMMIT", KRB5_CC_IO);
    if (ret) return ret;

    scc_free(sfrom);

    return 0;

rollback:
    exec_stmt(context, sfrom->db, "ROLLBACK", 0);
    scc_free(sfrom);

    return KRB5_CC_IO;
}
Beispiel #7
0
static krb5_error_code KRB5_CALLCONV
scc_remove_cred(krb5_context context,
		 krb5_ccache id,
		 krb5_flags which,
		 krb5_creds *mcreds)
{
    krb5_scache *s = SCACHE(id);
    krb5_error_code ret;
    sqlite3_stmt *stmt;
    sqlite_uint64 credid = 0;
    const void *data = NULL;
    size_t len = 0;

    ret = make_database(context, s);
    if (ret)
	return ret;

    ret = prepare_stmt(context, s->db, &stmt,
		       "SELECT cred,oid FROM credentials "
		       "WHERE cid = ?");
    if (ret)
	return ret;

    sqlite3_bind_int(stmt, 1, s->cid);

    /* find credential... */
    while (1) {
	krb5_creds creds;

	ret = sqlite3_step(stmt);
	if (ret == SQLITE_DONE) {
	    ret = 0;
	    break;
	} else if (ret != SQLITE_ROW) {
	    ret = KRB5_CC_IO;
	    krb5_set_error_message(context, ret,
				   N_("scache Database failed: %s", ""),
				   sqlite3_errmsg(s->db));
	    break;
	}

	if (sqlite3_column_type(stmt, 0) != SQLITE_BLOB) {
	    ret = KRB5_CC_END;
	    krb5_set_error_message(context, ret,
				   N_("Credential of wrong type "
				      "for SCC:%s:%s", ""),
				   s->name, s->file);
	    break;
	}

	data = sqlite3_column_blob(stmt, 0);
	len = sqlite3_column_bytes(stmt, 0);

	ret = decode_creds(context, data, len, &creds);
	if (ret)
	    break;

	ret = krb5_compare_creds(context, which, mcreds, &creds);
	krb5_free_cred_contents(context, &creds);
	if (ret) {
	    credid = sqlite3_column_int64(stmt, 1);
	    ret = 0;
	    break;
	}
    }

    sqlite3_finalize(stmt);

    if (id) {
	ret = prepare_stmt(context, s->db, &stmt,
			   "DELETE FROM credentials WHERE oid=?");
	if (ret)
	    return ret;
	sqlite3_bind_int(stmt, 1, credid);

	do {
	    ret = sqlite3_step(stmt);
	} while (ret == SQLITE_ROW);
	sqlite3_finalize(stmt);
	if (ret != SQLITE_DONE) {
	    ret = KRB5_CC_IO;
	    krb5_set_error_message(context, ret,
				   N_("failed to delete scache credental", ""));
	} else
	    ret = 0;
    }

    return ret;
}
Beispiel #8
0
    namespace code_generation{


      enum profile_type{
        axpy = 1, aXpY = 2, dot = 3,
        gemvAv = 4, gemvTv = 5,
        gemmAA = 6, gemmTA = 7, gemmAT = 8, gemmTT = 9
      };

      typedef std::pair<profile_type, size_t> profile_id;

      typedef std::map< std::pair<cl_uint, cl_device_type>, std::map<profile_id, profile_base const * > > builtin_database_t;

      static  builtin_database_t make_database(){

        builtin_database_t res;

        //AMD GPUs
        {
          std::map<std::pair<profile_type, size_t>, profile_base const * > tmp;

          //BLAS 1
          tmp.insert(std::make_pair(std::make_pair(axpy,4), new saxpy_vector_profile(1,4,64)));
          tmp.insert(std::make_pair(std::make_pair(dot,4), new scalar_reduction_profile(8,128,128)));

          tmp.insert(std::make_pair(std::make_pair(axpy,8), new saxpy_vector_profile(2,1,64)));
          tmp.insert(std::make_pair(std::make_pair(dot,8), new scalar_reduction_profile(2,256,64)));

          //BLAS2
          tmp.insert(std::make_pair(std::make_pair(gemvAv,4), new vector_reduction_profile(1,256,1024)));
          tmp.insert(std::make_pair(std::make_pair(gemvTv,4), new vector_reduction_profile(32,8,256)));

          tmp.insert(std::make_pair(std::make_pair(gemvAv,8), new vector_reduction_profile(1,256,1024)));
          tmp.insert(std::make_pair(std::make_pair(gemvTv,8), new vector_reduction_profile(64,4,256)));

          //BLAS 3
          tmp.insert(std::make_pair(std::make_pair(gemmAA,4), new matrix_product_profile(16,64,256,4,2,4,1,0,4,1)));
          tmp.insert(std::make_pair(std::make_pair(gemmTA,4), new matrix_product_profile(32,256,256,4,4,8,0,0,4,1)));
          tmp.insert(std::make_pair(std::make_pair(gemmAT,4), new matrix_product_profile(64,32,64,4,8,4,1,0,4,1)));
          tmp.insert(std::make_pair(std::make_pair(gemmTT,4), new matrix_product_profile(128,64,32,8,4,8,0,0,4,1)));

          tmp.insert(std::make_pair(std::make_pair(gemmAA,8), new matrix_product_profile(32,64,128,4,4,4,1,0,4,1)));
          tmp.insert(std::make_pair(std::make_pair(gemmTA,8), new matrix_product_profile(32,128,128,4,2,4,0,0,2,1)));


          res.insert(std::make_pair(std::make_pair(4098,CL_DEVICE_TYPE_GPU),tmp));
        }

        //NVidia GPUs
        {
          std::map<std::pair<profile_type, size_t>, profile_base const * > tmp;

          //BLAS1
          tmp.insert(std::make_pair(std::make_pair(axpy,4), new saxpy_vector_profile(1,1,256)));
          tmp.insert(std::make_pair(std::make_pair(dot,4), new scalar_reduction_profile(4,64,512)));

          tmp.insert(std::make_pair(std::make_pair(axpy,8), new saxpy_vector_profile(2,1,64)));
          tmp.insert(std::make_pair(std::make_pair(dot,8), new scalar_reduction_profile(2,64,512)));

          //BLAS2
          tmp.insert(std::make_pair(std::make_pair(gemvAv,4), new vector_reduction_profile(1,256,1023)));
          tmp.insert(std::make_pair(std::make_pair(gemvTv,4), new vector_reduction_profile(64,4,64)));

          tmp.insert(std::make_pair(std::make_pair(gemvAv,8), new vector_reduction_profile(1,128,1024)));
          tmp.insert(std::make_pair(std::make_pair(gemvTv,8), new vector_reduction_profile(16,32,1024)));


          //BLAS3
          tmp.insert(std::make_pair(std::make_pair(gemmAA,4), new matrix_product_profile(16,128,128,8,4,2,1,0,1,32)));
          tmp.insert(std::make_pair(std::make_pair(gemmAT,4), new matrix_product_profile(32,32,128,2,4,8,1,1,2,1)));
          tmp.insert(std::make_pair(std::make_pair(gemmTA,4), new matrix_product_profile(32,128,256,8,4,2,1,0,1,1)));
          tmp.insert(std::make_pair(std::make_pair(gemmTT,4), new matrix_product_profile(32,32,128,4,4,4,1,1,4,1)));

          tmp.insert(std::make_pair(std::make_pair(gemmAA,8), new matrix_product_profile(16,64,128,2,2,8,1,0,1,32)));
          tmp.insert(std::make_pair(std::make_pair(gemmTA,8), new matrix_product_profile(256,128,32,2,2,8,0,1,2,1)));


          res.insert(std::make_pair(std::make_pair(4318,CL_DEVICE_TYPE_GPU),tmp));
        }

        //Intel CPUs
        {
          std::map<std::pair<profile_type, size_t>, profile_base const * > tmp;

          //BLAS1
          tmp.insert(std::make_pair(std::make_pair(axpy,4), new saxpy_vector_profile(8,16,256)));
          tmp.insert(std::make_pair(std::make_pair(dot,4), new scalar_reduction_profile(8,8,512)));

          tmp.insert(std::make_pair(std::make_pair(axpy,4), new saxpy_vector_profile(8,16,32)));
          tmp.insert(std::make_pair(std::make_pair(dot,4), new scalar_reduction_profile(8,8,512)));


          //BLAS2
          tmp.insert(std::make_pair(std::make_pair(gemvAv,4), new vector_reduction_profile(2,1,8)));
          tmp.insert(std::make_pair(std::make_pair(gemvTv,4), new vector_reduction_profile(16,8,8)));

          tmp.insert(std::make_pair(std::make_pair(gemvAv,8), new vector_reduction_profile(1,1,8)));
          tmp.insert(std::make_pair(std::make_pair(gemvTv,8), new vector_reduction_profile(8,16,16)));

          //BLAS3
          tmp.insert(std::make_pair(std::make_pair(gemmAA,4), new matrix_product_profile(64,64,128,4,4,128,0,0,4,1)));
          tmp.insert(std::make_pair(std::make_pair(gemmTA,4), new matrix_product_profile(128,64,32,16,4,32,0,0,1,1)));

          tmp.insert(std::make_pair(std::make_pair(gemmAA,8), new matrix_product_profile(128,64,64,8,4,64,0,0,2,1)));
          tmp.insert(std::make_pair(std::make_pair(gemmTA,8), new matrix_product_profile(128,128,32,8,4,16,0,0,1,1)));


          res.insert(std::make_pair(std::make_pair(32902,CL_DEVICE_TYPE_CPU),tmp));
          res.insert(std::make_pair(std::make_pair(4098,CL_DEVICE_TYPE_CPU),tmp));
        }
        return res;
      }

      static std::map<profile_type, profile_base const * > make_default_profiles(){
        std::map<profile_type, profile_base const * > res;
        res.insert(std::make_pair(aXpY, new saxpy_matrix_profile(1,128)));
        res.insert(std::make_pair(axpy, new saxpy_vector_profile(1,1,128)));
        res.insert(std::make_pair(dot, new scalar_reduction_profile(1,128,128)));
        res.insert(std::make_pair(gemvAv, new vector_reduction_profile(1,16,64)));
        res.insert(std::make_pair(gemvTv, new vector_reduction_profile(1,16,64)));
        res.insert(std::make_pair(gemmAA, new matrix_product_profile(32,32,32,4,4,4,true,false,1,1)));
        res.insert(std::make_pair(gemmTA, new matrix_product_profile(32,32,32,4,4,4,true,false,1,1)));
        res.insert(std::make_pair(gemmAT, new matrix_product_profile(32,32,32,4,4,4,true,false,1,1)));
        res.insert(std::make_pair(gemmTT, new matrix_product_profile(32,32,32,4,4,4,true,false,1,1)));
        return res;
      }

      static builtin_database_t builtin_database = make_database();
      static std::map<profile_type, profile_base const * > default_profiles = make_default_profiles();

    }