Cleanup - Multiple changes, see description
[tpg/opendispense2.git] / src / cokebank_basic / main.c
index f7d32f0..21f4e06 100644 (file)
 #include <stdint.h>
 #include <stdio.h>
 #include <string.h>
+#include <string.h>
+#include <limits.h>
+#include <pwd.h>
+#include <grp.h>
 #include <openssl/sha.h>
 #include "common.h"
 #if USE_LDAP
  * 
  */
 
-// === HACKS ===
+#define USE_UNIX_GROUPS        1
 #define HACK_TPG_NOAUTH        1
 #define HACK_ROOT_NOAUTH       1
 
 // === PROTOTYPES ===
 void   Init_Cokebank(const char *Argument);
- int   Transfer(int SourceUser, int DestUser, int Ammount, const char *Reason);
- int   GetBalance(int User);
-char   *GetUserName(int User);
- int   GetUserID(const char *Username);
- int   GetMaxID(void);
- int   GetUserAuth(const char *Salt, const char *Username, const char *PasswordString);
+ int   Bank_Transfer(int SourceUser, int DestUser, int Ammount, const char *Reason);
+ int   Bank_CreateUser(const char *Username);
+ int   Bank_GetMaxID(void);
+static int Bank_int_WriteEntry(int ID);
+ int   Bank_GetUserID(const char *Username);
+ int   Bank_GetBalance(int User);
+ int   Bank_GetFlags(int User);
+ int   Bank_SetFlags(int User, int Mask, int Value);
+ int   Bank_int_AlterUserBalance(int ID, int Delta);
+ int   Bank_int_GetMinAllowedBalance(int ID);
+ int   Bank_int_AddUser(const char *Username);
+char   *Bank_GetUserName(int User);
+ int   Bank_int_GetUnixID(const char *Username);
+ int   Bank_GetUserAuth(const char *Salt, const char *Username, const char *PasswordString);
 #if USE_LDAP
 char   *ReadLDAPValue(const char *Filter, char *Value);
 #endif
@@ -48,6 +60,9 @@ FILE  *gBank_LogFile;
 char   *gsLDAPPath = "ldapi:///";
 LDAP   *gpLDAP;
 #endif
+tUser  *gaBank_Users;
+ int   giBank_NumUsers;
+FILE   *gBank_File;
 
 // === CODE ===
 /**
@@ -123,77 +138,272 @@ void Init_Cokebank(const char *Argument)
  * \param Reason       Reason for the transfer (essentially a comment)
  * \return Boolean failure
  */
-int Transfer(int SourceUser, int DestUser, int Ammount, const char *Reason)
+int Bank_Transfer(int SourceUser, int DestUser, int Ammount, const char *Reason)
 {
-        int    srcBal = Bank_GetUserBalance(SourceUser);
-        int    dstBal = Bank_GetUserBalance(DestUser);
+        int    srcBal = Bank_GetBalance(SourceUser);
+        int    dstBal = Bank_GetBalance(DestUser);
        
-       if( srcBal - Ammount < Bank_GetMinAllowedBalance(SourceUser) )
+       if( srcBal - Ammount < Bank_int_GetMinAllowedBalance(SourceUser) )
                return 1;
-       if( dstBal + Ammount < Bank_GetMinAllowedBalance(DestUser) )
+       if( dstBal + Ammount < Bank_int_GetMinAllowedBalance(DestUser) )
                return 1;
-       Bank_AlterUserBalance(DestUser, Ammount);
-       Bank_AlterUserBalance(SourceUser, -Ammount);
+       Bank_int_AlterUserBalance(DestUser, Ammount);
+       Bank_int_AlterUserBalance(SourceUser, -Ammount);
        fprintf(gBank_LogFile, "Transfer %ic #%i{%i} > #%i{%i} [%i, %i] (%s)\n",
                Ammount, SourceUser, srcBal, DestUser, dstBal,
                srcBal - Ammount, dstBal + Ammount, Reason);
        return 0;
 }
 
-int GetFlags(int User)
+int Bank_CreateUser(const char *Username)
 {
-       return Bank_GetUserFlags(User);
+        int    ret;
+       
+       ret = Bank_GetUserID(Username);
+       if( ret != -1 ) return -1;
+       
+       return Bank_int_AddUser(Username);
 }
 
-int SetFlags(int User, int Mask, int Flags)
+int Bank_GetMaxID(void)
 {
-       return Bank_SetUserFlags(User, Mask, Flags);
+       return giBank_NumUsers;
 }
 
-/**
- * \brief Get the balance of the passed user
- */
-int GetBalance(int User)
+static int Bank_int_WriteEntry(int ID)
 {
-       return Bank_GetUserBalance(User);
+       if( ID < 0 || ID >= giBank_NumUsers ) {
+               return -1;
+       }
+       
+       // Commit to file
+       fseek(gBank_File, ID*sizeof(gaBank_Users[0]), SEEK_SET);
+       fwrite(&gaBank_Users[ID], sizeof(gaBank_Users[0]), 1, gBank_File);
+       
+       return 0;
 }
 
 /**
- * \brief Return the name the passed user
+ * \brief Get the User ID of the named user
  */
-char *GetUserName(int User)
+int Bank_GetUserID(const char *Username)
+{
+        int    i, uid;
+       
+       uid = Bank_int_GetUnixID(Username);
+       
+       // Expensive search :(
+       for( i = 0; i < giBank_NumUsers; i ++ )
+       {
+               if( gaBank_Users[i].UnixID == uid )
+                       return i;
+       }
+
+       return -1;
+}
+
+int Bank_GetBalance(int ID)
+{
+       if( ID < 0 || ID >= giBank_NumUsers )
+               return INT_MIN;
+
+       return gaBank_Users[ID].Balance;
+}
+
+int Bank_GetFlags(int ID)
+{
+       if( ID < 0 || ID >= giBank_NumUsers )
+               return -1;
+
+       // root
+       if( gaBank_Users[ID].UnixID == 0 ) {
+               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;
+               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_COKE;
+                                       break ;
+                               }
+                       }
+               }
+               
+               // 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_WHEEL;
+                                       break ;
+                               }
+                       }
+               }
+       }
+       #endif
+
+       return gaBank_Users[ID].Flags;
+}
+
+int Bank_SetFlags(int ID, int Mask, int Value)
+{
+       // Sanity
+       if( ID < 0 || ID >= giBank_NumUsers )
+               return -1;
+       
+       // 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 |= Value;
+
+       Bank_int_WriteEntry(ID);
+       
+       return 0;
+}
+
+int Bank_int_AlterUserBalance(int ID, int Delta)
 {
-       return Bank_GetUserName(User);
+       // Sanity
+       if( ID < 0 || ID >= giBank_NumUsers )
+               return -1;
+
+       // Update
+       gaBank_Users[ID].Balance += Delta;
+
+       Bank_int_WriteEntry(ID);
+       
+       return 0;
+}
+
+int Bank_int_GetMinAllowedBalance(int ID)
+{
+        int    flags;
+       if( ID < 0 || ID >= giBank_NumUsers )
+               return 0;
+
+       flags = Bank_GetFlags(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;
 }
 
 /**
- * \brief Get the User ID of the named user
+ * \brief Create a new user in our database
  */
-int GetUserID(const char *Username)
+int Bank_int_AddUser(const char *Username)
 {
-       return Bank_GetUserByName(Username);
+       void    *tmp;
+        int    uid = Bank_int_GetUnixID(Username);
+
+       // Can has moar space plz?
+       tmp = realloc(gaBank_Users, (giBank_NumUsers+1)*sizeof(gaBank_Users[0]));
+       if( !tmp )      return -1;
+       gaBank_Users = tmp;
+
+       // Crete new user
+       gaBank_Users[giBank_NumUsers].UnixID = uid;
+       gaBank_Users[giBank_NumUsers].Balance = 0;
+       gaBank_Users[giBank_NumUsers].Flags = 0;
+       
+       if( strcmp(Username, COKEBANK_DEBT_ACCT) == 0 ) {
+               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_FLAG_WHEEL|USER_FLAG_COKE;
+       }
+
+       // Increment count
+       giBank_NumUsers ++;
+       
+       Bank_int_WriteEntry(giBank_NumUsers - 1);
+
+       return 0;
 }
 
-int CreateUser(const char *Username)
+// ---
+// Unix user dependent code
+// TODO: Modify to keep its own list of usernames
+// ---
+/**
+ * \brief Return the name the passed user
+ */
+char *Bank_GetUserName(int ID)
 {
-        int    ret;
+       struct passwd   *pwd;
        
-       ret = Bank_GetUserByName(Username);
-       if( ret != -1 ) return -1;
+       if( ID < 0 || ID >= giBank_NumUsers )
+               return NULL;
        
-       return Bank_AddUser(Username);
+       if( gaBank_Users[ID].UnixID == -1 )
+               return strdup(COKEBANK_SALES_ACCT);
+
+       if( gaBank_Users[ID].UnixID == -2 )
+               return strdup(COKEBANK_DEBT_ACCT);
+
+       pwd = getpwuid(gaBank_Users[ID].UnixID);
+       if( !pwd )      return NULL;
+
+       return strdup(pwd->pw_name);
 }
 
-int GetMaxID(void)
+int Bank_int_GetUnixID(const char *Username)
 {
-       return giBank_NumUsers;
+        int    uid;
+
+       if( strcmp(Username, COKEBANK_SALES_ACCT) == 0 ) {      // Pseudo account that sales are made into
+               uid = -1;
+       }
+       else if( strcmp(Username, COKEBANK_DEBT_ACCT) == 0 ) {  // Pseudo acount that money is added from
+               uid = -2;
+       }
+       else {
+               struct passwd   *pwd;
+               // Get user ID
+               pwd = getpwnam(Username);
+               if( !pwd )      return -1;
+               uid = pwd->pw_uid;
+       }
+       return uid;
 }
 
+
 /**
  * \brief Authenticate a user
  * \return User ID, or -1 if authentication failed
  */
-int GetUserAuth(const char *Salt, const char *Username, const char *PasswordString)
+int Bank_GetUserAuth(const char *Salt, const char *Username, const char *PasswordString)
 {
        #if USE_LDAP
        uint8_t hash[20];
@@ -204,15 +414,23 @@ int GetUserAuth(const char *Salt, const char *Username, const char *PasswordStri
        char    *passhash;
        #endif
        
+       #if 1
+       // Only here to shut GCC up (until password auth is implemented
+       if( Salt == NULL )
+               return -1;
+       if( PasswordString == NULL )
+               return -1;
+       #endif
+       
        #if HACK_TPG_NOAUTH
        if( strcmp(Username, "tpg") == 0 )
-               return GetUserID("tpg");
+               return Bank_GetUserID("tpg");
        #endif
        #if HACK_ROOT_NOAUTH
        if( strcmp(Username, "root") == 0 ) {
-               int ret = GetUserID("root");
+               int ret = Bank_GetUserID("root");
                if( ret == -1 )
-                       return CreateUser("root");
+                       return Bank_CreateUser("root");
                return ret;
        }
        #endif

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