src/mn-masterd/auth.c
static int
upgrade_conn(struct conn *conn, const char *user)
{
    int           sz;
    nolp_t       *no;
    char          buf[96];
    slave_conn_t *sl;
    int  sock = conn->sock;
    MYSQL_RES *r;
    MYSQL_ROW row;

    switch (conn->auth) {
        case NOL_AUTH_TYPE_CLIENT:
            /* give this client to a slave */
            if (!srv.num_slaves) {
                /* add to pending list */
                return -1;
            } else {
                unsigned min = (unsigned)-1;
                unsigned min_o;
                unsigned x;
                /* find the slave with the least num clients connected, 
                 * and connect this client with that slave, also verify 
                 * that the slave isn't currently generating a token for 
                 * another client */
                for (x=0; x<srv.num_slaves; x++) {
                    slave_conn_t *sl = &srv.slaves[x];
                    if (sl->ready && sl->num_clients < min
                            && !sl->client_conn) {
                        min = sl->num_clients;
                        min_o = x;
                    }
                }
                /* the slave with the least amount of clients should now be at
                 * srv.slaves[min_o] */
                if (min == (unsigned)-1)
                    /* no slave found, or they were all busy */
                    return -1;

                sz = sprintf(buf, "CLIENT %s %.64s\n", inet_ntoa(conn->addr.sin_addr), user);
                srv.slaves[min_o].client_conn = conn; 
                nolp_expect_line((nolp_t *)(srv.slaves[min_o].conn->fd_ev.data),
                        &nol_m_token_reply);
                send(srv.slaves[min_o].conn->sock, buf, sz, 0);
            }
            break;

        case NOL_AUTH_TYPE_SLAVE:
            if (!(sl = nol_m_create_slave_conn(user)))
                return -1;
            if (!(no = nolp_create(slave_commands, sock)))
                return -1;

            conn->slave_n = srv.num_slaves-1;
            sl->conn      = conn;
            no->private = conn;
            conn->fd_ev.data = no;

            send_config(sock);
            send_hooks(sock);
            nol_m_create_slave_list_xml();

            /* from now on, the data received from this
             * socket will be parsed by the nolp
             * struct, in slave-conn.c */
            ev_set_cb(&conn->fd_ev, &slave_read);
            break;

        case NOL_AUTH_TYPE_USER:
            /* find out and store the permissions for this user */
            sz = sprintf(buf, "SELECT level FROM `nol_user` WHERE id = %d", conn->user_id);
            if (mysql_real_query(srv.mysql, buf, sz) != 0)
                return -1;
            if (!(r = mysql_store_result(srv.mysql)))
                return -1;
            if (!(row = mysql_fetch_row(r))) {
                mysql_free_result(r);
                return -1;
            }
            conn->level = atoi(row[0]);
            syslog(LOG_DEBUG, "#%d set permission level to %d", sock, conn->level);
            mysql_free_result(r);

            if (!(no = nolp_create(user_commands, sock)))
                return -1;

            ev_set_cb(&conn->fd_ev, &user_read);
            no->private = conn;
            conn->fd_ev.data = no;
            break;

        default:
            return 1;
    }

    return 0;
}
/** 
 * upgrade the connection depending on what value
 * conn->auth is set to. Change the event loop
 * callback function and create a slave structure
 * if the conn is a slave
 *
 * return 0 unless an error occurs
 **/
upgrade the connection depending on what value
conn->auth is set to. Change the event loop
callback function and create a slave structure
if the conn is a slave

return 0 unless an error occurs
upgrade the connection depending on what value conn - > auth is set to . Change the event loop callback function and create a slave structure if the conn is a slave return 0 unless an error occurs
static int
upgrade_conn(struct conn *conn, const char *user)
{
    int           sz;
    nolp_t       *no;
    char          buf[96];
    slave_conn_t *sl;
    int  sock = conn->sock;
    MYSQL_RES *r;
    MYSQL_ROW row;
    switch (conn->auth) {
        case NOL_AUTH_TYPE_CLIENT:
            if (!srv.num_slaves) {
                return -1;
            } else {
                unsigned min = (unsigned)-1;
                unsigned min_o;
                unsigned x;
                for (x=0; x<srv.num_slaves; x++) {
                    slave_conn_t *sl = &srv.slaves[x];
                    if (sl->ready && sl->num_clients < min
                            && !sl->client_conn) {
                        min = sl->num_clients;
                        min_o = x;
                    }
                }
                if (min == (unsigned)-1)
                    return -1;
                sz = sprintf(buf, "CLIENT %s %.64s\n", inet_ntoa(conn->addr.sin_addr), user);
                srv.slaves[min_o].client_conn = conn; 
                nolp_expect_line((nolp_t *)(srv.slaves[min_o].conn->fd_ev.data),
                        &nol_m_token_reply);
                send(srv.slaves[min_o].conn->sock, buf, sz, 0);
            }
            break;
        case NOL_AUTH_TYPE_SLAVE:
            if (!(sl = nol_m_create_slave_conn(user)))
                return -1;
            if (!(no = nolp_create(slave_commands, sock)))
                return -1;
            conn->slave_n = srv.num_slaves-1;
            sl->conn      = conn;
            no->private = conn;
            conn->fd_ev.data = no;
            send_config(sock);
            send_hooks(sock);
            nol_m_create_slave_list_xml();
            ev_set_cb(&conn->fd_ev, &slave_read);
            break;
        case NOL_AUTH_TYPE_USER:
            sz = sprintf(buf, "SELECT level FROM `nol_user` WHERE id = %d", conn->user_id);
            if (mysql_real_query(srv.mysql, buf, sz) != 0)
                return -1;
            if (!(r = mysql_store_result(srv.mysql)))
                return -1;
            if (!(row = mysql_fetch_row(r))) {
                mysql_free_result(r);
                return -1;
            }
            conn->level = atoi(row[0]);
            syslog(LOG_DEBUG, "#%d set permission level to %d", sock, conn->level);
            mysql_free_result(r);
            if (!(no = nolp_create(user_commands, sock)))
                return -1;
            ev_set_cb(&conn->fd_ev, &user_read);
            no->private = conn;
            conn->fd_ev.data = no;
            break;
        default:
            return 1;
    }
    return 0;
}
static int upgrade_conn ( struct conn * conn , const char * user ) { int sz ; nolp_t * no ; char buf [ 96 ] ; slave_conn_t * sl ; int sock = conn -> sock ; MYSQL_RES * r ; MYSQL_ROW row ; switch ( conn -> auth ) { case NOL_AUTH_TYPE_CLIENT : if ( ! srv . num_slaves ) { return -1 ; } else { unsigned min = ( unsigned ) -1 ; unsigned min_o ; unsigned x ; for ( x = 0 ; x < srv . num_slaves ; x ++ ) { slave_conn_t * sl = & srv . slaves [ x ] ; if ( sl -> ready && sl -> num_clients < min && ! sl -> client_conn ) { min = sl -> num_clients ; min_o = x ; } } if ( min == ( unsigned ) -1 ) return -1 ; sz = sprintf ( buf , " \n " , inet_ntoa ( conn -> addr . sin_addr ) , user ) ; srv . slaves [ min_o ] . client_conn = conn ; nolp_expect_line ( ( nolp_t * ) ( srv . slaves [ min_o ] . conn -> fd_ev . data ) , & nol_m_token_reply ) ; send ( srv . slaves [ min_o ] . conn -> sock , buf , sz , 0 ) ; } break ; case NOL_AUTH_TYPE_SLAVE : if ( ! ( sl = nol_m_create_slave_conn ( user ) ) ) return -1 ; if ( ! ( no = nolp_create ( slave_commands , sock ) ) ) return -1 ; conn -> slave_n = srv . num_slaves - 1 ; sl -> conn = conn ; no -> private = conn ; conn -> fd_ev . data = no ; send_config ( sock ) ; send_hooks ( sock ) ; nol_m_create_slave_list_xml ( ) ; ev_set_cb ( & conn -> fd_ev , & slave_read ) ; break ; case NOL_AUTH_TYPE_USER : sz = sprintf ( buf , " " , conn -> user_id ) ; if ( mysql_real_query ( srv . mysql , buf , sz ) != 0 ) return -1 ; if ( ! ( r = mysql_store_result ( srv . mysql ) ) ) return -1 ; if ( ! ( row = mysql_fetch_row ( r ) ) ) { mysql_free_result ( r ) ; return -1 ; } conn -> level = atoi ( row [ 0 ] ) ; syslog ( LOG_DEBUG , " " , sock , conn -> level ) ; mysql_free_result ( r ) ; if ( ! ( no = nolp_create ( user_commands , sock ) ) ) return -1 ; ev_set_cb ( & conn -> fd_ev , & user_read ) ; no -> private = conn ; conn -> fd_ev . data = no ; break ; default : return 1 ; } return 0 ; }
upgrade the connection depending on what value
 conn->auth is set to.
upgrade the connection depending on what value conn - > auth is set to .
/* give this client to a slave */ /* add to pending list */ /* find the slave with the least num clients connected, 
                 * and connect this client with that slave, also verify 
                 * that the slave isn't currently generating a token for 
                 * another client */ /* the slave with the least amount of clients should now be at
                 * srv.slaves[min_o] */ /* no slave found, or they were all busy */ /* from now on, the data received from this
             * socket will be parsed by the nolp
             * struct, in slave-conn.c */ /* find out and store the permissions for this user */