Cleanups of bugs and segfaults
[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 "common.h"
15
16 #define HACK_TPG_NOAUTH 1
17
18 // === IMPORTS ===
19 extern int      Bank_GetMinAllowedBalance(int ID);
20 extern int      Bank_GetUserBalance(int ID);
21 extern int      Bank_AlterUserBalance(int ID, int Delta);
22 extern int      Bank_GetUserByUnixID(int UnixID);
23 extern int      Bank_GetUserUnixID(int ID);
24 extern int      Bank_AddUser(int UnixID);
25 extern FILE     *gBank_File;
26 extern tUser    *gaBank_Users;
27 extern int      giBank_NumUsers;
28
29 // === PROTOTYPES ===
30 void    Init_Cokebank(const char *Argument);
31  int    Transfer(int SourceUser, int DestUser, int Ammount, const char *Reason);
32  int    GetBalance(int User);
33 char    *GetUserName(int User);
34  int    GetUserID(const char *Username); 
35  int    GetUserAuth(const char *Username, const char *Password);
36
37 // === CODE ===
38 /**
39  * \brief Load the cokebank database
40  */
41 void Init_Cokebank(const char *Argument)
42 {
43         gBank_File = fopen(Argument, "rb+");
44         if( !gBank_File ) {
45                 gBank_File = fopen(Argument, "wb+");
46         }
47         if( !gBank_File ) {
48                 perror("Opening coke bank");
49         }
50
51         fseek(gBank_File, 0, SEEK_END);
52         giBank_NumUsers = ftell(gBank_File) / sizeof(gaBank_Users[0]);
53         fseek(gBank_File, 0, SEEK_SET);
54         gaBank_Users = malloc( giBank_NumUsers * sizeof(gaBank_Users[0]) );
55         fread(gaBank_Users, sizeof(gaBank_Users[0]), giBank_NumUsers, gBank_File);
56 }
57
58 /**
59  * \brief Transfers money from one user to another
60  * \param SourceUser    Source user
61  * \param DestUser      Destination user
62  * \param Ammount       Ammount of cents to move from \a SourceUser to \a DestUser
63  * \param Reason        Reason for the transfer (essentially a comment)
64  * \return Boolean failure
65  */
66 int Transfer(int SourceUser, int DestUser, int Ammount, const char *Reason)
67 {
68         if( Bank_GetUserBalance(SourceUser) - Ammount < Bank_GetMinAllowedBalance(SourceUser) )
69                 return 1;
70         if( Bank_GetUserBalance(DestUser) + Ammount < Bank_GetMinAllowedBalance(DestUser) )
71                 return 1;
72         Bank_AlterUserBalance(DestUser, Ammount);
73         Bank_AlterUserBalance(SourceUser, -Ammount);
74         return 0;
75 }
76
77 /**
78  * \brief Get the balance of the passed user
79  */
80 int GetBalance(int User)
81 {
82         return 0;
83 }
84
85 /**
86  * \brief Return the name the passed user
87  */
88 char *GetUserName(int User)
89 {
90         struct passwd   *pwd;
91          int    unixid = Bank_GetUserUnixID(User);
92         
93         if( unixid == -1 )
94                 return strdup(">sales");
95
96         if( unixid == -2 )
97                 return strdup(">liability");
98
99         pwd = getpwuid(unixid);
100         if( !pwd )      return NULL;
101
102         return strdup(pwd->pw_name);
103 }
104
105 /**
106  * \brief Get the User ID of the named user
107  */
108 int GetUserID(const char *Username)
109 {
110          int    ret, uid;
111
112         if( strcmp(Username, ">sales") == 0 ) { // Pseudo account that sales are made into
113                 uid = -1;
114         }
115         else if( strcmp(Username, ">liability") == 0 ) {        // Pseudo acount that money is added from
116                 uid = -2;
117         }
118         else {
119                 struct passwd   *pwd;
120                 // Get user ID
121                 pwd = getpwnam(Username);
122                 if( !pwd )      return -1;
123                 uid = pwd->pw_uid;
124         }
125
126         // Get internal ID (or create new user)
127         ret = Bank_GetUserByUnixID(uid);
128         if( ret == -1 ) {
129                 ret = Bank_AddUser(uid);
130         }
131
132         return ret;
133 }
134
135 /**
136  * \brief Authenticate a user
137  * \return User ID, or -1 if authentication failed
138  */
139 int GetUserAuth(const char *Username, const char *Password)
140 {
141         #if HACK_TPG_NOAUTH
142         if( strcmp(Username, "tpg") == 0 )
143                 return GetUserID("tpg");
144         #endif
145         return -1;
146 }
147

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