Added new user command (wheel only)
[tpg/opendispense2.git] / src / cokebank_basic / bank.c
index 0f07a17..9b9e2d5 100644 (file)
 #include <string.h>
 #include <limits.h>
 #include <pwd.h>
+#include <grp.h>
 #include "common.h"
 
-enum {
-       FLAG_TYPEMASK    = 0x03,
-       USER_TYPE_NORMAL = 0x00,
-       USER_TYPE_COKE   = 0x01,
-       USER_TYPE_WHEEL  = 0x02,
-       USER_TYPE_GOD    = 0x03
-};
+#define USE_UNIX_GROUPS        1
 
 // === PROTOTYPES ===
 static int     GetUnixID(const char *Username);
@@ -56,6 +51,61 @@ int Bank_GetUserBalance(int ID)
        return gaBank_Users[ID].Balance;
 }
 
+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;
+       }
+
+       #if USE_UNIX_GROUPS
+       if( gaBank_Users[ID].UnixID > 0 )
+       {
+               struct passwd   *pwd;
+               struct group    *grp;
+                int    i;
+               
+               // Get username
+               pwd = getpwuid( gaBank_Users[ID].UnixID );
+               
+               // Check for additions to the "coke" group
+               grp = getgrnam("coke");
+               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;
+                               }
+                       }
+               }
+               
+               // Check for additions to the "wheel" group
+               grp = getgrnam("wheel");
+               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;
+                               }
+                       }
+               }
+       }
+       #endif
+
+       return gaBank_Users[ID].Flags;
+}
+
 int Bank_AlterUserBalance(int ID, int Delta)
 {
        // Sanity
@@ -93,9 +143,9 @@ int Bank_GetMinAllowedBalance(int ID)
        if( ID < 0 || ID >= giBank_NumUsers )
                return 0;
 
-       switch( gaBank_Users[ID].Flags & FLAG_TYPEMASK )
+       switch( Bank_GetUserFlags(ID) & USER_FLAG_TYPEMASK )
        {
-       case USER_TYPE_NORMAL:  return     0;
+       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;
@@ -121,7 +171,10 @@ int Bank_AddUser(const char *Username)
        gaBank_Users[giBank_NumUsers].Balance = 0;
        gaBank_Users[giBank_NumUsers].Flags = 0;
        
-       if( strcmp(Username, ">liability") == 0 ) {
+       if( strcmp(Username, COKEBANK_DEBT_ACCT) == 0 ) {
+               gaBank_Users[giBank_NumUsers].Flags = USER_TYPE_GOD;    // No minium
+       }
+       else if( strcmp(Username, "root") == 0 ) {
                gaBank_Users[giBank_NumUsers].Flags = USER_TYPE_GOD;    // No minium
        }
        
@@ -147,10 +200,10 @@ char *Bank_GetUserName(int ID)
                return NULL;
        
        if( gaBank_Users[ID].UnixID == -1 )
-               return strdup(">sales");
+               return strdup(COKEBANK_SALES_ACCT);
 
        if( gaBank_Users[ID].UnixID == -2 )
-               return strdup(">liability");
+               return strdup(COKEBANK_DEBT_ACCT);
 
        pwd = getpwuid(gaBank_Users[ID].UnixID);
        if( !pwd )      return NULL;
@@ -162,10 +215,10 @@ static int GetUnixID(const char *Username)
 {
         int    uid;
 
-       if( strcmp(Username, ">sales") == 0 ) { // Pseudo account that sales are made into
+       if( strcmp(Username, COKEBANK_SALES_ACCT) == 0 ) {      // Pseudo account that sales are made into
                uid = -1;
        }
-       else if( strcmp(Username, ">liability") == 0 ) {        // Pseudo acount that money is added from
+       else if( strcmp(Username, COKEBANK_DEBT_ACCT) == 0 ) {  // Pseudo acount that money is added from
                uid = -2;
        }
        else {

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