Changed user type from an enum to a collection of flags
authorJohn Hodge <[email protected]>
Fri, 7 Jan 2011 09:43:44 +0000 (17:43 +0800)
committerJohn Hodge <[email protected]>
Fri, 7 Jan 2011 09:43:44 +0000 (17:43 +0800)
- Fixed negative cents glitch in `dispense acct`

src/client/main.c
src/cokebank.h
src/cokebank_basic/bank.c
src/cokebank_basic/main.c
src/server/server.c

index 9b136f8..711d1a9 100644 (file)
@@ -1138,7 +1138,7 @@ void _PrintUserLine(const char *Line)
                flags[flagsLen] = '\0';
                
                bal = atoi(Line + matches[4].rm_so);
-               printf("%-15s: $%4i.%02i (%s)\n", username, bal/100, bal%100, flags);
+               printf("%-15s: $%4i.%02i (%s)\n", username, bal/100, abs(bal)%100, flags);
        }
 }
 
index 9b9d2a1..25a3566 100644 (file)
 #define COKEBANK_SALES_ACCT    ">sales"        //!< Sales made into
 #define COKEBANK_DEBT_ACCT     ">liability"    //!< Credit taken out of
 
-enum eCokebank_Flags {
-       USER_FLAG_TYPEMASK = 0x03,
-       USER_TYPE_NORMAL = 0x00,
-       USER_TYPE_COKE   = 0x01,
-       USER_TYPE_WHEEL  = 0x02,
-       USER_TYPE_GOD    = 0x03,
-       
-       USER_FLAG_DOORGROUP = 0x40,
-       USER_FLAG_DISABLED  = 0x80
+enum eCokebank_Flags { 
+       USER_FLAG_COKE          = 0x01,
+       USER_FLAG_WHEEL         = 0x02,
+       USER_FLAG_DOORGROUP     = 0x04,
+       USER_FLAG_INTERNAL      = 0x40,
+       USER_FLAG_DISABLED      = 0x80
 };
 
 // --- Cokebank Functions ---
index abf5763..ce963c6 100644 (file)
@@ -55,17 +55,15 @@ int Bank_GetUserFlags(int ID)
 {
        if( ID < 0 || ID >= giBank_NumUsers )
                return -1;
-               
-       // TODO: Implement checking the PAM groups and status instead, then
-       // fall back on the database. (and update if there is a difference)
 
        // root
        if( gaBank_Users[ID].UnixID == 0 ) {
-               gaBank_Users[ID].Flags &= ~USER_FLAG_TYPEMASK;
-               gaBank_Users[ID].Flags |= USER_TYPE_WHEEL;
+               gaBank_Users[ID].Flags |= USER_FLAG_WHEEL|USER_FLAG_COKE;
        }
 
        #if USE_UNIX_GROUPS
+       // TODO: Implement checking the PAM groups and status instead, then
+       // fall back on the database. (and update if there is a difference)
        if( gaBank_Users[ID].UnixID > 0 )
        {
                struct passwd   *pwd;
@@ -80,10 +78,9 @@ int Bank_GetUserFlags(int ID)
                if( grp ) {
                        for( i = 0; grp->gr_mem[i]; i ++ )
                        {
-                               if( strcmp(grp->gr_mem[i], pwd->pw_name) == 0 )
-                               {
-                                       gaBank_Users[ID].Flags &= ~USER_FLAG_TYPEMASK;
-                                       gaBank_Users[ID].Flags |= USER_TYPE_COKE;
+                               if( strcmp(grp->gr_mem[i], pwd->pw_name) == 0 ) {
+                                       gaBank_Users[ID].Flags |= USER_FLAG_COKE;
+                                       break ;
                                }
                        }
                }
@@ -93,10 +90,9 @@ int Bank_GetUserFlags(int ID)
                if( grp ) {
                        for( i = 0; grp->gr_mem[i]; i ++ )
                        {
-                               if( strcmp(grp->gr_mem[i], pwd->pw_name) == 0 )
-                               {
-                                       gaBank_Users[ID].Flags &= ~USER_FLAG_TYPEMASK;
-                                       gaBank_Users[ID].Flags |= USER_TYPE_WHEEL;
+                               if( strcmp(grp->gr_mem[i], pwd->pw_name) == 0 ) {
+                                       gaBank_Users[ID].Flags |= USER_FLAG_WHEEL;
+                                       break ;
                                }
                        }
                }
@@ -115,7 +111,7 @@ int Bank_SetUserFlags(int ID, int Mask, int Value)
        // Silently ignore changes to root and meta accounts
        if( gaBank_Users[ID].UnixID <= 0 )      return 0;
        
-       gaBank_Users[ID].Flags &= Mask;
+       gaBank_Users[ID].Flags &= ~Mask;
        gaBank_Users[ID].Flags |= Value;
        
        return 0;
@@ -155,17 +151,26 @@ int Bank_SetUserBalance(int ID, int Value)
 
 int Bank_GetMinAllowedBalance(int ID)
 {
+        int    flags;
        if( ID < 0 || ID >= giBank_NumUsers )
                return 0;
 
-       switch( Bank_GetUserFlags(ID) & USER_FLAG_TYPEMASK )
-       {
-       case USER_TYPE_NORMAL:  return      0;
-       case USER_TYPE_COKE:    return  -2000;
-       case USER_TYPE_WHEEL:   return -10000;
-       case USER_TYPE_GOD:     return INT_MIN;
-       default:        return 0;
-       }
+       flags = Bank_GetUserFlags(ID);
+
+       // Internal accounts have no limit
+       if( (flags & USER_FLAG_INTERNAL) )
+               return INT_MIN;
+
+       // Wheel is allowed to go to -$100
+       if( (flags & USER_FLAG_WHEEL) )
+               return -10000;
+       
+       // Coke is allowed to go to -$20
+       if( (flags & USER_FLAG_COKE) )
+               return -2000;
+
+       // For everyone else, no negative
+       return 0;
 }
 
 /**
@@ -187,10 +192,13 @@ int Bank_AddUser(const char *Username)
        gaBank_Users[giBank_NumUsers].Flags = 0;
        
        if( strcmp(Username, COKEBANK_DEBT_ACCT) == 0 ) {
-               gaBank_Users[giBank_NumUsers].Flags = USER_TYPE_GOD;    // No minium
+               gaBank_Users[giBank_NumUsers].Flags = USER_FLAG_INTERNAL;
+       }
+       else if( strcmp(Username, COKEBANK_SALES_ACCT) == 0 ) {
+               gaBank_Users[giBank_NumUsers].Flags = USER_FLAG_INTERNAL;
        }
        else if( strcmp(Username, "root") == 0 ) {
-               gaBank_Users[giBank_NumUsers].Flags = USER_TYPE_GOD;    // No minium
+               gaBank_Users[giBank_NumUsers].Flags = USER_FLAG_WHEEL|USER_FLAG_COKE;
        }
        
        // Commit to file
index 61e777c..4121614 100644 (file)
 # include <ldap.h>
 #endif
 
+/*
+ * NOTES:
+ * 
+ * http://linuxdevcenter.com/pub/a/linux/2003/08/14/libldap.html
+ * - Using libldap, the LDAP Client Library 
+ * 
+ */
+
 // === HACKS ===
 #define HACK_TPG_NOAUTH        1
 #define HACK_ROOT_NOAUTH       1
index 9469b60..86c63d6 100644 (file)
@@ -401,6 +401,13 @@ void Server_Cmd_AUTOAUTH(tClient *Client, char *Args)
                return ;
        }
        
+       // You can't be an internal account
+       if( GetFlags(Client->UID) & USER_FLAG_INTERNAL ) {
+               Client->UID = -1;
+               sendf(Client->Socket, "401 Auth Failure\n");
+               return ;
+       }
+       
        if(giDebugLevel)
                printf("Client %i: Authenticated as '%s' (%i)\n", Client->ID, Args, Client->UID);
        
@@ -424,7 +431,7 @@ void Server_Cmd_SETEUSER(tClient *Client, char *Args)
        }
 
        // Check user permissions
-       if( (GetFlags(Client->UID) & USER_FLAG_TYPEMASK) < USER_TYPE_COKE ) {
+       if( !(GetFlags(Client->UID) & USER_FLAG_COKE) ) {
                sendf(Client->Socket, "403 Not in coke\n");
                return ;
        }
@@ -436,6 +443,13 @@ void Server_Cmd_SETEUSER(tClient *Client, char *Args)
                return ;
        }
        
+       // You can't be an internal account
+       if( GetFlags(Client->EffectiveUID) & USER_FLAG_INTERNAL ) {
+               Client->EffectiveUID = -1;
+               sendf(Client->Socket, "404 User not found\n");
+               return ;
+       }
+       
        sendf(Client->Socket, "200 User set\n");
 }
 
@@ -583,6 +597,12 @@ void Server_Cmd_GIVE(tClient *Client, char *Args)
                sendf(Client->Socket, "404 Invalid target user\n");
                return ;
        }
+       
+       // You can't alter an internal account
+       if( GetFlags(uid) & USER_FLAG_INTERNAL ) {
+               sendf(Client->Socket, "404 Invalid target user\n");
+               return ;
+       }
 
        // Parse ammount
        iAmmount = atoi(ammount);
@@ -642,20 +662,20 @@ void Server_Cmd_ADD(tClient *Client, char *Args)
        reason ++;
 
        // Check user permissions
-       if( (GetFlags(Client->UID) & USER_FLAG_TYPEMASK) < USER_TYPE_COKE ) {
+       if( !(GetFlags(Client->UID) & USER_FLAG_COKE)  ) {
                sendf(Client->Socket, "403 Not in coke\n");
                return ;
        }
 
        // Get recipient
        uid = GetUserID(user);
-
-       // Check user permissions
-       if( (GetFlags(Client->UID) & USER_FLAG_TYPEMASK) < USER_TYPE_COKE ) {
-               sendf(Client->Socket, "403 Not in coke\n");
+       if( uid == -1 ) {
+               sendf(Client->Socket, "404 Invalid user\n");
                return ;
        }
-       if( uid == -1 ) {
+       
+       // You can't alter an internal account
+       if( GetFlags(uid) & USER_FLAG_INTERNAL ) {
                sendf(Client->Socket, "404 Invalid user\n");
                return ;
        }
@@ -759,22 +779,29 @@ void Server_Cmd_USERINFO(tClient *Client, char *Args)
 
 void _SendUserInfo(tClient *Client, int UserID)
 {
-       char    *type, *disabled="";
+       char    *type, *disabled="", *door="";
         int    flags = GetFlags(UserID);
        
-       switch( flags & USER_FLAG_TYPEMASK )
-       {
-       default:
-       case USER_TYPE_NORMAL:  type = "user";  break;
-       case USER_TYPE_COKE:    type = "coke";  break;
-       case USER_TYPE_WHEEL:   type = "wheel"; break;
-       case USER_TYPE_GOD:     type = "meta";  break;
+       if( flags & USER_FLAG_INTERNAL ) {
+               type = "internal";
+       }
+       else if( flags & USER_FLAG_COKE ) {
+               if( flags & USER_FLAG_WHEEL )
+                       type = "coke,wheel";
+               else
+                       type = "coke";
+       }
+       else if( flags & USER_FLAG_WHEEL ) {
+               type = "wheel";
+       }
+       else {
+               type = "user";
        }
        
        if( flags & USER_FLAG_DISABLED )
                disabled = ",disabled";
        if( flags & USER_FLAG_DOORGROUP )
-               disabled = ",door";
+               door = ",door";
        
        // TODO: User flags/type
        sendf(
@@ -789,7 +816,7 @@ void Server_Cmd_USERADD(tClient *Client, char *Args)
        char    *username, *space;
        
        // Check permissions
-       if( (GetFlags(Client->UID) & USER_FLAG_TYPEMASK) < USER_TYPE_WHEEL ) {
+       if( !(GetFlags(Client->UID) & USER_FLAG_WHEEL) ) {
                sendf(Client->Socket, "403 Not Wheel\n");
                return ;
        }
@@ -817,7 +844,7 @@ void Server_Cmd_USERFLAGS(tClient *Client, char *Args)
         int    uid;
        
        // Check permissions
-       if( (GetFlags(Client->UID) & USER_FLAG_TYPEMASK) < USER_TYPE_WHEEL ) {
+       if( !(GetFlags(Client->UID) & USER_FLAG_WHEEL) ) {
                sendf(Client->Socket, "403 Not Wheel\n");
                return ;
        }
@@ -854,12 +881,11 @@ void Server_Cmd_USERFLAGS(tClient *Client, char *Args)
                         int    Mask;
                         int    Value;
                }       cFLAGS[] = {
-                       {"disabled", USER_FLAG_DISABLED, USER_FLAG_DISABLED},
-                       {"door", USER_FLAG_DOORGROUP, USER_FLAG_DOORGROUP},
-                       {"user", USER_FLAG_TYPEMASK, USER_TYPE_NORMAL},
-                       {"coke", USER_FLAG_TYPEMASK, USER_TYPE_COKE},
-                       {"wheel", USER_FLAG_TYPEMASK, USER_TYPE_WHEEL},
-                       {"meta", USER_FLAG_TYPEMASK, USER_TYPE_GOD}
+                        {"disabled", USER_FLAG_DISABLED, USER_FLAG_DISABLED}
+                       ,{"door", USER_FLAG_DOORGROUP, USER_FLAG_DOORGROUP}
+                       ,{"coke", USER_FLAG_COKE, USER_FLAG_COKE}
+                       ,{"wheel", USER_FLAG_WHEEL, USER_FLAG_WHEEL}
+               //      ,{"internal", USER_FLAG_INTERNAL, USER_FLAG_INTERNAL}
                };
                const int       ciNumFlags = sizeof(cFLAGS)/sizeof(cFLAGS[0]);
                

UCC git Repository :: git.ucc.asn.au