Пример #1
0
int create_user (char *username, char *password, int priv_level)
{

    int i = MAX_USERS;

    if (!username || !password) {
        cli_printf("Error!!! Creating Username and Password...\n");
        return -1;
    }

#if 0
    if (password_validation (password) < 0) {
        cli_printf("Error!!! Passwords should be AlphaNumberic... eg: \"PassWord123\"\n");
        return -1;
    }
#endif
    if (priv_level < 0 || priv_level > 5) {
        cli_printf("Error!!! Priority Level Range Between 0 to 5\n");
        return -1;
    }

    if(!update_user_info (username, password, priv_level))
        return 0;

    while (i--) {
        if (!userdb[i].status) {
            strncpy (userdb[i].user_name, username, MAX_USER_NAME);
            strncpy (userdb[i].password, password, MAX_USER_PASSWORD);
            if (priv_level >= 0)
                userdb[i].priv_level = priv_level;
            else
                userdb[i].priv_level = 5;
            userdb[i].status  = 1;
            return 0;
        }
    }

    cli_printf("Oops!!! Unable to Create User\n");
    return -1;
}
Пример #2
0
void handle_client(conn_t *conn) {
    conn->user = NULL;
    for(; ;) {
        request_t *rqst = parse_args(conn->input);
        if(rqst != NULL) {
            if(IS_PROTOCOL(rqst, P_LOGIN)) {
                if(login(rqst->argv[1], rqst->argv[2]) == 0) {
                    conn->user = (t_user*) malloc(sizeof(t_user));
                    memcpy(conn->user, find_user_by_id(rqst->argv[1]), sizeof(t_user));
                    simple_response(0, conn);
                } else {
                    simple_response(1, conn);
                }
            } 
            else if(IS_PROTOCOL(rqst, P_SIGNUP)) {
                if(signup(rqst->argv[1], rqst->argv[2]) == 0 ) {
                    simple_response(0, conn);
                } else {
                    simple_response(1, conn);
                }
            } 
            else if(IS_PROTOCOL(rqst, P_EXIT)) {
                simple_response(0, conn);
                return;
            }
            else  if(conn->user != NULL) { //authorized user
                if(IS_PROTOCOL(rqst, P_BUY)) {
                    if(buy(conn->user->id, rqst->argv[1]) == 0)  {
                        simple_response(0, conn);
                    } else {
                        simple_response(1, conn);
                    }
                } else if(IS_PROTOCOL(rqst, P_QUERY)) {

                    t_ticket_list *list = query(rqst->argv[1], rqst->argv[2]);

                    write(conn->dfd, "*", 1);
                    char *tmp = ltoa(list->num);
                    int tmplen = strlen(tmp);
                    write(conn->dfd, tmp, tmplen);
                    write(conn->dfd, CRLF, CLLEN);

                    free(tmp);

                    tmp = ltoa(sizeof(t_ticket));
                    tmplen = strlen(tmp);
                    int i;
                    for (i = 0; i < list->num; ++i) {
                        write(conn->dfd, "?", 1); write(conn->dfd, tmp, tmplen); write(conn->dfd, CRLF, CLLEN);
                        write(conn->dfd, &list->data[i], sizeof(t_ticket));
                    }
                    free(tmp);
                } else if(IS_PROTOCOL(rqst, P_USER_INFO)) {

                    char *tmp = ltoa(sizeof(t_user));
                    int tmplen = strlen(tmp);
                    write(conn->dfd, "?", 1); write(conn->dfd, tmp, tmplen); write(conn->dfd, CRLF, CLLEN);
                    write(conn->dfd, conn->user, sizeof(t_user));

                } else if(IS_PROTOCOL(rqst, P_USER_UPD)) {
                    if(update_user_info(conn->user->id, rqst->argv[1], rqst->argv[2], rqst->argv[3]) == 0)  {
                        memcpy(conn->user, find_user_by_id(conn->user->id), sizeof(t_user));
                        simple_response(0, conn);
                    } else {
                        simple_response(1, conn);
                    }

                } else if(IS_PROTOCOL(rqst, P_QUERY_BUY)) {
                    t_ticket_list *list = query_buy(conn->user->id);

                    write(conn->dfd, "*", 1);
                    char *tmp = ltoa(list->num);
                    int tmplen = strlen(tmp);
                    write(conn->dfd, tmp, tmplen);
                    write(conn->dfd, CRLF, CLLEN);

                    free(tmp);

                    tmp = ltoa(sizeof(t_ticket));
                    tmplen = strlen(tmp);
                    int i;
                    for (i = 0; i < list->num; ++i) {
                        write(conn->dfd, "?", 1); write(conn->dfd, tmp, tmplen); write(conn->dfd, CRLF, CLLEN);
                        write(conn->dfd, &list->data[i], sizeof(t_ticket));
                    }
                    free(tmp);

                } else if(IS_PROTOCOL(rqst, P_REFUND)) {
                    if(refund(conn->user->id, rqst->argv[1]) == 0)  {
                        simple_response(0, conn);
                    } else {
                        simple_response(1, conn);
                    }
                }
                else if(IS_PROTOCOL(rqst, P_TICKET) && conn->user->type == 1) {
                    t_ticket *tkt = load_ticket(rqst->argv[1]);
                    if(tkt == NULL) {
                        simple_response(1, conn);
                    } else {
                        char *tmp = ltoa(sizeof(t_ticket));
                        write(conn->dfd, "?", 1); write(conn->dfd, tmp, strlen(tmp));write(conn->dfd, CRLF, CLLEN);
                        write(conn->dfd, tkt, sizeof(t_ticket));
                        free(tmp);
                    }
                } else if(IS_PROTOCOL(rqst, P_TKT_UPDATE) && conn->user->type == 1) {
                    if(add_update_ticket(rqst->argv[1],
                            rqst->argv[2],
                            rqst->argv[3],
                            rqst->argv[4],
                            rqst->argv[5],
                            rqst->argv[6],
                            rqst->argv[7],
                            rqst->argv[8],
                            rqst->argv[9]
                            ) == 0) {
                        simple_response(0, conn);
                    } else {
                        simple_response(1, conn);
                    }
                } else if(IS_PROTOCOL(rqst, P_STN_ADD) && conn->user->type == 1) {
                    if(add_station(rqst->argv[1]) == 0)  {
                        simple_response(0, conn);
                    } else {
                        simple_response(1, conn);
                    }

                } else if(IS_PROTOCOL(rqst, P_STN_DEL) && conn->user->type == 1) {
                    if(del_station(rqst->argv[1]) == 0)  {
                        simple_response(0, conn);
                    } else {
                        simple_response(1, conn);
                    }

                } else if(IS_PROTOCOL(rqst, P_STN_ALL) && conn->user->type == 1) {
                    t_station_list *list = all_station();

                    write(conn->dfd, "*", 1);
                    char *tmp = ltoa(list->num);
                    int tmplen = strlen(tmp);
                    write(conn->dfd, tmp, tmplen);
                    write(conn->dfd, CRLF, CLLEN);

                    free(tmp);

                    tmp = ltoa(sizeof(t_station));
                    tmplen = strlen(tmp);
                    int i;
                    for (i = 0; i < list->num; ++i) {
                        write(conn->dfd, "?", 1); write(conn->dfd, tmp, tmplen); write(conn->dfd, CRLF, CLLEN);
                        write(conn->dfd, &list->data[i], sizeof(t_station));
                    }
                    free(tmp);

                }
            } 
            else {
                simple_response(1, conn);
            }
        }
    }
}