コード例 #1
0
ファイル: um.c プロジェクト: challinan/webserver-demo
int	umAddUser(char_t *user, char_t *pass, char_t *group, 
			  bool_t prot, bool_t disabled)
{
	int		row;
	char_t	*password;

	a_assert(user && *user);
	a_assert(pass && *pass);
	a_assert(group && *group);

	trace(3, T("UM: Adding User <%s>\n"), user);

/*
 *	Do not allow duplicates
 */
	if (umUserExists(user)) {
		return UM_ERR_DUPLICATE;
	}

/*
 *	Make sure user name and password contain valid characters
 */
	if (!umCheckName(user)) {
		return UM_ERR_BAD_NAME;
	}

	if (!umCheckName(pass)) {
		return UM_ERR_BAD_PASSWORD;
	}

/*
 *	Make sure group exists
 */
	if (!umGroupExists(group)) {
		return UM_ERR_NOT_FOUND;
	}

/*
 *	Now create the user record
 */
	row = dbAddRow(didUM, UM_USER_TABLENAME);

	if (row < 0) {
		return UM_ERR_GENERAL;
	}

	if (dbWriteStr(didUM, UM_USER_TABLENAME, UM_NAME, row, user) != 0) {
		return UM_ERR_GENERAL;
	}

	password = bstrdup(B_L, pass);
	umEncryptString(password);
	dbWriteStr(didUM, UM_USER_TABLENAME, UM_PASS, row, password);
	bfree(B_L, password);
	dbWriteStr(didUM, UM_USER_TABLENAME, UM_GROUP, row, group);
	dbWriteInt(didUM, UM_USER_TABLENAME, UM_PROT, row, prot); 
	dbWriteInt(didUM, UM_USER_TABLENAME, UM_DISABLE, row, disabled);

	return 0;
}
コード例 #2
0
ファイル: um.c プロジェクト: challinan/webserver-demo
int	umAddGroup(char_t *group, short priv, accessMeth_t am, 
			   bool_t prot, bool_t disabled)
{
	int row;

	a_assert(group && *group);
	trace(3, T("UM: Adding group <%s>\n"), group);
	
/*
 *	Do not allow duplicates
 */
	if (umGroupExists(group)) {
		return UM_ERR_DUPLICATE;
	}

/*
 *	Only allow valid characters in key field
 */
	if (!umCheckName(group)) {
		return UM_ERR_BAD_NAME;
	}

/*
 *	Add a new row to the table
 */
	if ((row = dbAddRow(didUM, UM_GROUP_TABLENAME)) < 0) {
		return UM_ERR_GENERAL;
	}

/*
 *	Write the key field
 */
	if (dbWriteStr(didUM, UM_GROUP_TABLENAME, UM_NAME, row, group) != 0) {
		return UM_ERR_GENERAL;
	}

/*
 *	Write the remaining fields
 */
	dbWriteInt(didUM, UM_GROUP_TABLENAME, UM_PRIVILEGE, row, priv);
	dbWriteInt(didUM, UM_GROUP_TABLENAME, UM_METHOD, row, (int) am);
	dbWriteInt(didUM, UM_GROUP_TABLENAME, UM_PROT, row, prot);
	dbWriteInt(didUM, UM_GROUP_TABLENAME, UM_DISABLE, row, disabled);

	return 0;
}
コード例 #3
0
ファイル: um.c プロジェクト: challinan/webserver-demo
int	umSetGroupAccessMethod(char_t *group, accessMeth_t am)
{
	int row;

	a_assert(group && *group);
	row = dbSearchStr(didUM, UM_GROUP_TABLENAME, UM_NAME, group, 0);

	if (row >= 0) {
		return dbWriteInt(didUM, UM_GROUP_TABLENAME, UM_METHOD, row, (int) am);
	} else {
		return UM_ERR_NOT_FOUND;
	}
}
コード例 #4
0
ファイル: um.c プロジェクト: challinan/webserver-demo
int	umSetAccessLimitMethod(char_t *url, accessMeth_t am)
{
	int row;

	a_assert(url && *url);
	row = dbSearchStr(didUM, UM_ACCESS_TABLENAME, UM_NAME, url, 0);

	if (row >= 0) {
		return dbWriteInt(didUM, UM_ACCESS_TABLENAME, UM_METHOD, row, (int) am);
	} else {
		return UM_ERR_NOT_FOUND;
	}
}
コード例 #5
0
ファイル: um.c プロジェクト: challinan/webserver-demo
int	umSetGroupProtected(char_t *group, bool_t protect)
{
	int row;

	a_assert(group && *group);
	row = dbSearchStr(didUM, UM_GROUP_TABLENAME, UM_NAME, group, 0);

	if (row >= 0) {
		return dbWriteInt(didUM, UM_GROUP_TABLENAME, UM_PROT, row, 
			(int) protect);
	} else {
		return UM_ERR_NOT_FOUND;
	}
}
コード例 #6
0
ファイル: um.c プロジェクト: challinan/webserver-demo
int umSetGroupEnabled(char_t *group, bool_t enabled)
{
	int row;

	a_assert(group && *group);
	row = dbSearchStr(didUM, UM_GROUP_TABLENAME, UM_NAME, group, 0);

	if (row >= 0) {
		return dbWriteInt(didUM, UM_GROUP_TABLENAME, UM_DISABLE, row, 
			(int) !enabled);
	} else {
		return UM_ERR_NOT_FOUND;
	}
}
コード例 #7
0
ファイル: um.c プロジェクト: challinan/webserver-demo
int	umSetGroupPrivilege(char_t *group, short privilege)
{
	int row;

	a_assert(group && *group);
	row = dbSearchStr(didUM, UM_GROUP_TABLENAME, UM_NAME, group, 0);

	if (row >= 0) {
		return dbWriteInt(didUM, UM_GROUP_TABLENAME, UM_PRIVILEGE, row, 
			(int)privilege);
	} else {
		return UM_ERR_NOT_FOUND;
	}
}
コード例 #8
0
ファイル: um.c プロジェクト: challinan/webserver-demo
int	umSetAccessLimitSecure(char_t *url, short secure)
{
	int row;

	a_assert(url && *url);
	row = dbSearchStr(didUM, UM_ACCESS_TABLENAME, UM_NAME, url, 0);

	if (row >= 0) {
		return dbWriteInt(didUM, UM_ACCESS_TABLENAME, UM_SECURE, row, 
			(int)secure);
	} else {
		return UM_ERR_NOT_FOUND;
	}
}
コード例 #9
0
ファイル: um.c プロジェクト: challinan/webserver-demo
int	umAddAccessLimit(char_t *url, accessMeth_t am, short secure, char_t *group)
{
	int row;

	a_assert(url && *url);
	trace(3, T("UM: Adding Access Limit for <%s>\n"), url);

/*
 *	Do not allow duplicates
 */
	if (umAccessLimitExists(url)) {
		return UM_ERR_DUPLICATE;
	}

/*
 *	Add a new row to the table
 */
	if ((row = dbAddRow(didUM, UM_ACCESS_TABLENAME)) < 0) {
		return UM_ERR_GENERAL;
	}

/*
 *	Write the key field
 */
	if(dbWriteStr(didUM, UM_ACCESS_TABLENAME, UM_NAME, row, url) < 0) {
		return UM_ERR_GENERAL;
	}

/*
 *	Write the remaining fields
 */
	dbWriteInt(didUM, UM_ACCESS_TABLENAME, UM_METHOD, row, (int)am);
	dbWriteInt(didUM, UM_ACCESS_TABLENAME, UM_SECURE, row, (int)secure);
	dbWriteStr(didUM, UM_ACCESS_TABLENAME, UM_GROUP, row, group);

	return 0;
}
コード例 #10
0
ファイル: um.c プロジェクト: challinan/webserver-demo
/*
 *	umSetUserEnabled() Enables/disables the user
 */
int	umSetUserEnabled(char_t *user, bool_t enabled)
{
	int row;

	a_assert(user && *user);
/*
 *	Find the row of the user
 */
	row = dbSearchStr(didUM, UM_USER_TABLENAME, UM_NAME, user, 0);
	if (row >= 0) {
		return dbWriteInt(didUM, UM_USER_TABLENAME, UM_DISABLE, row, !enabled);
	} else {
		return UM_ERR_NOT_FOUND;
	}
}
コード例 #11
0
ファイル: um.c プロジェクト: challinan/webserver-demo
/*
 *	umSetUserProtected() sets the delete protection for the user
 */
int	umSetUserProtected(char_t *user, bool_t protect)
{
	int row;

	a_assert(user && *user);
/*
 *	Find the row of the user
 */
	row = dbSearchStr(didUM, UM_USER_TABLENAME, UM_NAME, user, 0);

	if (row >= 0) {
		return dbWriteInt(didUM, UM_USER_TABLENAME, UM_PROT, row, protect);
	} else {
		return UM_ERR_NOT_FOUND;
	}
}
コード例 #12
0
int dbLoad(int did, char_t *filename, int flags)
{
    gstat_t		sbuf;
    char_t		*buf, *keyword, *value, *path, *ptr;
    char_t		*tablename;
    int			fd, tid, row;
    dbTable_t	*pTable;

    a_assert(did >= 0);

    fmtAlloc(&path, FNAMESIZE, T("%s/%s"), basicGetProductDir(), filename);
    trace(4, T("DB: About to read data file <%s>\n"), path);

    if (gstat(path, &sbuf) < 0) {
        trace(3, T("DB: Failed to stat persistent data file.\n"));
        bfree(B_L, path);
        return -1;
    }

    fd = gopen(path, O_RDONLY | O_BINARY, 0666);
    bfree(B_L, path);

    if (fd < 0) {
        trace(3, T("DB: No persistent data file present.\n"));
        return -1;
    }

    if (sbuf.st_size <= 0) {
        trace(3, T("DB: Persistent data file is empty.\n"));
        gclose(fd);
        return -1;
    }
    /*
     *	Read entire file into temporary buffer
     */
    buf = balloc(B_L, sbuf.st_size + 1);
#ifdef CE
    if (readAscToUni(fd, &buf, sbuf.st_size) != (int)sbuf.st_size) {
#else
    if (gread(fd, buf, sbuf.st_size) != (int)sbuf.st_size) {
#endif
        trace(3, T("DB: Persistent data read failed.\n"));
        bfree(B_L, buf);
        gclose(fd);
        return -1;
    }

    gclose(fd);
    *(buf + sbuf.st_size) = '\0';

    row = -1;
    tid = -1;
    pTable = NULL;
    ptr = gstrtok(buf, T("\n"));
    tablename = NULL;

    do {
        if (crack(ptr, &keyword, &value) < 0) {
            trace(5, T("DB: Failed to crack line %s\n"), ptr);
            continue;
        }

        a_assert(keyword && *keyword);

        if (gstrcmp(keyword, KEYWORD_TABLE) == 0) {
            /*
             *			Table name found, check to see if it's registered
             */
            if (tablename) {
                bfree(B_L, tablename);
            }

            tablename = bstrdup(B_L, value);
            tid = dbGetTableId(did, tablename);

            if (tid >= 0) {
                pTable = dbListTables[tid];
            } else {
                pTable = NULL;
            }

        } else if (gstrcmp(keyword, KEYWORD_ROW) == 0) {
            /*
             *			Row/Record indicator found, add a new row to table
             */
            if (tid >= 0) {
                int nRows = dbGetTableNrow(did, tablename);

                if (dbSetTableNrow(did, tablename, nRows + 1) == 0) {
                    row = nRows;
                }
            }

        } else if (row != -1) {
            /*
             *			some other data found, assume it's a COLUMN=value
             */
            int nColumn = GetColumnIndex(tid, keyword);

            if ((nColumn >= 0) && (pTable != NULL)) {
                int nColumnType = pTable->columnTypes[nColumn];
                if (nColumnType == T_STRING) {
                    dbWriteStr(did, tablename, keyword, row, value);
                } else {
                    dbWriteInt(did, tablename, keyword, row, gstrtoi(value));
                }
            }
        }
    } while ((ptr = gstrtok(NULL, T("\n"))) != NULL);

    if (tablename) {
        bfree(B_L, tablename);
    }

    bfree(B_L, buf);

    return 0;
}

/******************************************************************************/
/*
 *	Return a table id given the table name
 */

int dbGetTableId(int did, char_t *tablename)
{
    int			tid;
    dbTable_t	*pTable;

    a_assert(tablename);

    for (tid = 0; (tid < dbMaxTables); tid++) {
        if ((pTable = dbListTables[tid]) != NULL) {
            if (gstrcmp(tablename, pTable->name) == 0) {
                return tid;
            }
        }
    }

    return -1;
}