コード例 #1
0
ファイル: as_mysql_wckey.c プロジェクト: sblanton/slurm
static int _reset_default_wckey(mysql_conn_t *mysql_conn,
                                slurmdb_wckey_rec_t *wckey)
{
    time_t now = time(NULL);
    int rc = SLURM_SUCCESS;
    char *query = NULL;
    MYSQL_RES *result = NULL;
    MYSQL_ROW row;

    if ((wckey->is_def != 1)
            || !wckey->cluster || !wckey->user || !wckey->name)
        return SLURM_ERROR;

    xstrfmtcat(query, "update \"%s_%s\" set is_def=0, mod_time=%ld "
               "where (user='******' && wckey_name!='%s' && is_def=1);"
               "select id_wckey from \"%s_%s\" "
               "where (user='******' && wckey_name!='%s' && is_def=1);",
               wckey->cluster, wckey_table, (long)now,
               wckey->user, wckey->name,
               wckey->cluster, wckey_table,
               wckey->user, wckey->name);
    if (debug_flags & DEBUG_FLAG_DB_WCKEY)
        DB_DEBUG(mysql_conn->conn, "query\n%s", query);
    if (!(result = mysql_db_query_ret(mysql_conn, query, 1))) {
        xfree(query);
        rc = SLURM_ERROR;
        goto end_it;
    }
    xfree(query);

    while ((row = mysql_fetch_row(result))) {
        slurmdb_wckey_rec_t *mod_wckey =
            xmalloc(sizeof(slurmdb_wckey_rec_t));
        slurmdb_init_wckey_rec(mod_wckey, 0);

        mod_wckey->id = slurm_atoul(row[0]);
        mod_wckey->is_def = 0;

        if (addto_update_list(mysql_conn->update_list,
                              SLURMDB_MODIFY_WCKEY,
                              mod_wckey)
                != SLURM_SUCCESS) {
            slurmdb_destroy_wckey_rec(mod_wckey);
            error("couldn't add to the update list");
            rc = SLURM_ERROR;
            break;
        }
    }
    mysql_free_result(result);
end_it:
    return rc;
}
コード例 #2
0
ファイル: as_mysql_user.c プロジェクト: BYUHPC/slurm
extern List as_mysql_modify_users(mysql_conn_t *mysql_conn, uint32_t uid,
				  slurmdb_user_cond_t *user_cond,
				  slurmdb_user_rec_t *user)
{
	ListIterator itr = NULL;
	List ret_list = NULL;
	int rc = SLURM_SUCCESS;
	char *object = NULL;
	char *vals = NULL, *extra = NULL, *query = NULL, *name_char = NULL;
	time_t now = time(NULL);
	char *user_name = NULL;
	int set = 0;
	MYSQL_RES *result = NULL;
	MYSQL_ROW row;

	if (!user_cond || !user) {
		error("we need something to change");
		return NULL;
	}

	if (check_connection(mysql_conn) != SLURM_SUCCESS)
		return NULL;

	if (user_cond->assoc_cond && user_cond->assoc_cond->user_list
	    && list_count(user_cond->assoc_cond->user_list)) {
		set = 0;
		xstrcat(extra, " && (");
		itr = list_iterator_create(user_cond->assoc_cond->user_list);
		while ((object = list_next(itr))) {
			if (set)
				xstrcat(extra, " || ");
			xstrfmtcat(extra, "name='%s'", object);
			set = 1;
		}
		list_iterator_destroy(itr);
		xstrcat(extra, ")");
	}

	if (user_cond->admin_level != SLURMDB_ADMIN_NOTSET)
		xstrfmtcat(extra, " && admin_level=%u",
			   user_cond->admin_level);

	ret_list = _get_other_user_names_to_mod(mysql_conn, uid, user_cond);

	if (user->name)
		xstrfmtcat(vals, ", name='%s'", user->name);

	if (user->admin_level != SLURMDB_ADMIN_NOTSET)
		xstrfmtcat(vals, ", admin_level=%u", user->admin_level);

	if ((!extra && !ret_list)
	    || (!vals && !user->default_acct && !user->default_wckey)) {
		errno = SLURM_NO_CHANGE_IN_DATA;
		error("Nothing to change");
		return NULL;
	}

	if (!extra) {
		/* means we got a ret_list and don't need to look at
		   the user_table. */
		goto no_user_table;
	}

	query = xstrdup_printf(
		"select distinct name from %s where deleted=0 %s;",
		user_table, extra);
	xfree(extra);
	if (!(result = mysql_db_query_ret(
		      mysql_conn, query, 0))) {
		xfree(query);
		if (ret_list)
			list_destroy(ret_list);
		return NULL;
	}

	if (!ret_list)
		ret_list = list_create(slurm_destroy_char);
	while ((row = mysql_fetch_row(result))) {
		slurmdb_user_rec_t *user_rec = NULL;

		object = row[0];
		slurm_addto_char_list(ret_list, object);
		if (!name_char)
			xstrfmtcat(name_char, "(name='%s'", object);
		else
			xstrfmtcat(name_char, " || name='%s'", object);

		user_rec = xmalloc(sizeof(slurmdb_user_rec_t));

		if (!user->name)
			user_rec->name = xstrdup(object);
		else {
			user_rec->name = xstrdup(user->name);
			user_rec->old_name = xstrdup(object);
			if (_change_user_name(mysql_conn, user_rec)
			    != SLURM_SUCCESS)
				break;
		}

		user_rec->admin_level = user->admin_level;
		if (addto_update_list(mysql_conn->update_list,
				      SLURMDB_MODIFY_USER, user_rec)
		    != SLURM_SUCCESS)
			slurmdb_destroy_user_rec(user_rec);
	}
	mysql_free_result(result);

no_user_table:
	if (!list_count(ret_list)) {
		errno = SLURM_NO_CHANGE_IN_DATA;
		debug3("didn't effect anything\n%s", query);
		xfree(vals);
		xfree(query);
		return ret_list;
	} else if (user->name && (list_count(ret_list) != 1)) {
		errno = ESLURM_ONE_CHANGE;
		xfree(vals);
		xfree(query);
		if (ret_list)
			list_destroy(ret_list);
		return NULL;
	}

	xfree(query);

	if (name_char && vals) {
		xstrcat(name_char, ")");
		user_name = uid_to_string((uid_t) uid);
		rc = modify_common(mysql_conn, DBD_MODIFY_USERS, now,
				   user_name, user_table, name_char,
				   vals, NULL);
		xfree(user_name);
	}

	xfree(name_char);
	xfree(vals);
	if (rc == SLURM_ERROR) {
		error("Couldn't modify users");
		list_destroy(ret_list);
		ret_list = NULL;
	}

	if (user->default_acct) {
		slurmdb_association_cond_t assoc_cond;
		slurmdb_association_rec_t assoc;
		List tmp_list = NULL;

		memset(&assoc_cond, 0, sizeof(slurmdb_association_cond_t));
		slurmdb_init_association_rec(&assoc, 0);
		assoc.is_def = 1;
		assoc_cond.acct_list = list_create(NULL);
		list_append(assoc_cond.acct_list, user->default_acct);
		assoc_cond.user_list = ret_list;
		if (user_cond->assoc_cond
		    && user_cond->assoc_cond->cluster_list)
			assoc_cond.cluster_list =
				user_cond->assoc_cond->cluster_list;
		tmp_list = as_mysql_modify_assocs(mysql_conn, uid,
						  &assoc_cond, &assoc);
		list_destroy(assoc_cond.acct_list);

		if (!tmp_list) {
			list_destroy(ret_list);
			ret_list = NULL;
			goto end_it;
		}
		/* char *names = NULL; */
		/* ListIterator itr = list_iterator_create(tmp_list); */
		/* while ((names = list_next(itr))) { */
		/* 	info("%s", names); */
		/* } */
		/* list_iterator_destroy(itr); */
		list_destroy(tmp_list);
	}

	if (user->default_wckey) {
		slurmdb_wckey_cond_t wckey_cond;
		slurmdb_wckey_rec_t wckey;
		List tmp_list = NULL;

		memset(&wckey_cond, 0, sizeof(slurmdb_wckey_cond_t));
		slurmdb_init_wckey_rec(&wckey, 0);
		wckey.is_def = 1;
		wckey_cond.name_list = list_create(NULL);
		list_append(wckey_cond.name_list, user->default_wckey);
		wckey_cond.user_list = ret_list;
		if (user_cond->assoc_cond
		    && user_cond->assoc_cond->cluster_list)
			wckey_cond.cluster_list =
				user_cond->assoc_cond->cluster_list;
		tmp_list = as_mysql_modify_wckeys(mysql_conn, uid,
						  &wckey_cond, &wckey);
		list_destroy(wckey_cond.name_list);

		if (!tmp_list) {
			list_destroy(ret_list);
			ret_list = NULL;
			goto end_it;
		}
		/* char *names = NULL; */
		/* ListIterator itr = list_iterator_create(tmp_list); */
		/* while ((names = list_next(itr))) { */
		/* 	info("%s", names); */
		/* } */
		/* list_iterator_destroy(itr); */
		list_destroy(tmp_list);
	}
end_it:
	return ret_list;
}
コード例 #3
0
ファイル: as_mysql_wckey.c プロジェクト: sblanton/slurm
extern List as_mysql_modify_wckeys(mysql_conn_t *mysql_conn,
                                   uint32_t uid,
                                   slurmdb_wckey_cond_t *wckey_cond,
                                   slurmdb_wckey_rec_t *wckey)
{
    List ret_list = NULL;
    int rc = SLURM_SUCCESS;
    char *extra = NULL, *object = NULL, *vals = NULL;
    char *user_name = NULL;
    List use_cluster_list = as_mysql_cluster_list;
    ListIterator itr;

    if (!wckey_cond || !wckey) {
        error("we need something to change");
        return NULL;
    }

    if (check_connection(mysql_conn) != SLURM_SUCCESS)
        return NULL;

    if (!is_user_min_admin_level(mysql_conn, uid, SLURMDB_ADMIN_OPERATOR)) {
        if (wckey_cond->user_list
                && (list_count(wckey_cond->user_list) == 1)) {
            uid_t pw_uid;
            char *name;
            name = list_peek(wckey_cond->user_list);
            if ((uid_from_string (name, &pw_uid) >= 0)
                    && (pw_uid == uid)) {
                /* Make sure they aren't trying to
                   change something else and then set
                   this association as a default.
                */
                slurmdb_init_wckey_rec(wckey, 1);
                wckey->is_def = 1;
                goto is_same_user;
            }
        }

        error("Only admins can modify wckeys");
        errno = ESLURM_ACCESS_DENIED;
        return NULL;
    }
is_same_user:

    (void) _setup_wckey_cond_limits(wckey_cond, &extra);

    if (wckey->is_def == 1)
        xstrcat(vals, ", is_def=1");

    if (!extra || !vals) {
        error("Nothing to modify '%s' '%s'", extra, vals);
        return NULL;
    }

    if (wckey_cond->cluster_list && list_count(wckey_cond->cluster_list))
        use_cluster_list = wckey_cond->cluster_list;

    user_name = uid_to_string((uid_t) uid);

    if (use_cluster_list == as_mysql_cluster_list)
        slurm_mutex_lock(&as_mysql_cluster_list_lock);

    ret_list = list_create(slurm_destroy_char);
    itr = list_iterator_create(use_cluster_list);
    while ((object = list_next(itr))) {
        if ((rc = _cluster_modify_wckeys(
                      mysql_conn, wckey, object,
                      extra, vals, user_name, ret_list))
                != SLURM_SUCCESS)
            break;
    }
    list_iterator_destroy(itr);
    xfree(extra);
    xfree(user_name);

    if (use_cluster_list == as_mysql_cluster_list)
        slurm_mutex_unlock(&as_mysql_cluster_list_lock);

    if (rc == SLURM_ERROR) {
        list_destroy(ret_list);
        ret_list = NULL;
    }

    return ret_list;
}
コード例 #4
0
ファイル: as_mysql_wckey.c プロジェクト: sblanton/slurm
static int _cluster_modify_wckeys(mysql_conn_t *mysql_conn,
                                  slurmdb_wckey_rec_t *wckey,
                                  char *cluster_name, char *extra,
                                  char *vals, char *user_name,
                                  List ret_list)
{
    int rc = SLURM_SUCCESS;
    MYSQL_RES *result = NULL;
    MYSQL_ROW row;
    char *wckey_char = NULL;
    time_t now = time(NULL);
    char *query = NULL;

    query = xstrdup_printf("select t1.id_wckey, t1.wckey_name, t1.user "
                           "from \"%s_%s\" as t1%s;",
                           cluster_name, wckey_table, extra);
    if (!(result = mysql_db_query_ret(mysql_conn, query, 0))) {
        xfree(query);
        return SLURM_ERROR;
    }

    /* This key doesn't exist on this cluster, that is ok. */
    if (!mysql_num_rows(result))
        return SLURM_SUCCESS;

    while ((row = mysql_fetch_row(result))) {
        slurmdb_wckey_rec_t *wckey_rec = NULL;
        char *object = xstrdup_printf(
                           "C = %-10s W = %-20s U = %-9s",
                           cluster_name, row[1], row[2]);
        list_append(ret_list, object);
        if (!wckey_char)
            xstrfmtcat(wckey_char, "id_wckey='%s'", row[0]);
        else
            xstrfmtcat(wckey_char, " || id_wckey='%s'", row[0]);

        wckey_rec = xmalloc(sizeof(slurmdb_wckey_rec_t));
        /* we only need id and cluster when removing
           no real need to init */
        wckey_rec->id = slurm_atoul(row[0]);
        wckey_rec->cluster = xstrdup(cluster_name);
        wckey_rec->is_def = wckey->is_def;
        if (addto_update_list(mysql_conn->update_list,
                              SLURMDB_MODIFY_WCKEY, wckey_rec)
                != SLURM_SUCCESS)
            slurmdb_destroy_wckey_rec(wckey_rec);

        if (wckey->is_def == 1) {
            /* Use fresh one here so we don't have to
               worry about dealing with bad values.
            */
            slurmdb_wckey_rec_t tmp_wckey;
            slurmdb_init_wckey_rec(&tmp_wckey, 0);
            tmp_wckey.is_def = 1;
            tmp_wckey.cluster = cluster_name;
            tmp_wckey.name = row[1];
            tmp_wckey.user = row[2];
            if ((rc = _reset_default_wckey(mysql_conn, &tmp_wckey))
                    != SLURM_SUCCESS)
                break;
        }
    }
    mysql_free_result(result);

    if (!list_count(ret_list)) {
        errno = SLURM_NO_CHANGE_IN_DATA;
        if (debug_flags & DEBUG_FLAG_DB_WCKEY)
            DB_DEBUG(mysql_conn->conn,
                     "didn't effect anything\n%s", query);
        xfree(query);
        xfree(wckey_char);
        return SLURM_SUCCESS;
    }

    xfree(query);
    rc = modify_common(mysql_conn, DBD_MODIFY_WCKEYS, now,
                       user_name, wckey_table, wckey_char,
                       vals, cluster_name);
    xfree(wckey_char);

    return rc;
}