Working on password-based user auth
[tpg/opendispense2.git] / src / cokebank_basic / main.c
1 /*
2  * OpenDispense 2 
3  * UCC (University [of WA] Computer Club) Electronic Accounting System
4  *
5  * cokebank.c - Coke-Bank management
6  *
7  * This file is licenced under the 3-clause BSD Licence. See the file COPYING
8  * for full details.
9  */
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <pwd.h>
13 #include <string.h>
14 #include <openssl/sha.h>
15 #include "common.h"
16
17 #define HACK_TPG_NOAUTH 1
18
19 // === IMPORTS ===
20 extern int      Bank_GetMinAllowedBalance(int ID);
21 extern int      Bank_GetUserBalance(int ID);
22 extern int      Bank_AlterUserBalance(int ID, int Delta);
23 extern int      Bank_GetUserByUnixID(int UnixID);
24 extern int      Bank_GetUserUnixID(int ID);
25 extern int      Bank_AddUser(int UnixID);
26 extern FILE     *gBank_File;
27 extern tUser    *gaBank_Users;
28 extern int      giBank_NumUsers;
29
30 // === PROTOTYPES ===
31 void    Init_Cokebank(const char *Argument);
32  int    Transfer(int SourceUser, int DestUser, int Ammount, const char *Reason);
33  int    GetBalance(int User);
34 char    *GetUserName(int User);
35  int    GetUserID(const char *Username); 
36  int    GetUserAuth(const char *Username, const char *Password);
37
38 // === CODE ===
39 /**
40  * \brief Load the cokebank database
41  */
42 void Init_Cokebank(const char *Argument)
43 {
44         gBank_File = fopen(Argument, "rb+");
45         if( !gBank_File ) {
46                 gBank_File = fopen(Argument, "wb+");
47         }
48         if( !gBank_File ) {
49                 perror("Opening coke bank");
50         }
51
52         fseek(gBank_File, 0, SEEK_END);
53         giBank_NumUsers = ftell(gBank_File) / sizeof(gaBank_Users[0]);
54         fseek(gBank_File, 0, SEEK_SET);
55         gaBank_Users = malloc( giBank_NumUsers * sizeof(gaBank_Users[0]) );
56         fread(gaBank_Users, sizeof(gaBank_Users[0]), giBank_NumUsers, gBank_File);
57 }
58
59 /**
60  * \brief Transfers money from one user to another
61  * \param SourceUser    Source user
62  * \param DestUser      Destination user
63  * \param Ammount       Ammount of cents to move from \a SourceUser to \a DestUser
64  * \param Reason        Reason for the transfer (essentially a comment)
65  * \return Boolean failure
66  */
67 int Transfer(int SourceUser, int DestUser, int Ammount, const char *Reason)
68 {
69         if( Bank_GetUserBalance(SourceUser) - Ammount < Bank_GetMinAllowedBalance(SourceUser) )
70                 return 1;
71         if( Bank_GetUserBalance(DestUser) + Ammount < Bank_GetMinAllowedBalance(DestUser) )
72                 return 1;
73         Bank_AlterUserBalance(DestUser, Ammount);
74         Bank_AlterUserBalance(SourceUser, -Ammount);
75         return 0;
76 }
77
78 /**
79  * \brief Get the balance of the passed user
80  */
81 int GetBalance(int User)
82 {
83         return Bank_GetUserBalance(User);;
84 }
85
86 /**
87  * \brief Return the name the passed user
88  */
89 char *GetUserName(int User)
90 {
91         struct passwd   *pwd;
92          int    unixid = Bank_GetUserUnixID(User);
93         
94         if( unixid == -1 )
95                 return strdup(">sales");
96
97         if( unixid == -2 )
98                 return strdup(">liability");
99
100         pwd = getpwuid(unixid);
101         if( !pwd )      return NULL;
102
103         return strdup(pwd->pw_name);
104 }
105
106 /**
107  * \brief Get the User ID of the named user
108  */
109 int GetUserID(const char *Username)
110 {
111          int    ret, uid;
112
113         if( strcmp(Username, ">sales") == 0 ) { // Pseudo account that sales are made into
114                 uid = -1;
115         }
116         else if( strcmp(Username, ">liability") == 0 ) {        // Pseudo acount that money is added from
117                 uid = -2;
118         }
119         else {
120                 struct passwd   *pwd;
121                 // Get user ID
122                 pwd = getpwnam(Username);
123                 if( !pwd )      return -1;
124                 uid = pwd->pw_uid;
125         }
126
127         // Get internal ID (or create new user)
128         ret = Bank_GetUserByUnixID(uid);
129         if( ret == -1 ) {
130                 ret = Bank_AddUser(uid);
131         }
132
133         return ret;
134 }
135

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