Fixing crappy code in coke handler, debug in others
[tpg/opendispense2.git] / src / server / server.c
index 9c36f3d..315b129 100644 (file)
@@ -61,12 +61,14 @@ void        Server_Cmd_DISPENSE(tClient *Client, char *Args);
 void   Server_Cmd_GIVE(tClient *Client, char *Args);
 void   Server_Cmd_DONATE(tClient *Client, char *Args);
 void   Server_Cmd_ADD(tClient *Client, char *Args);
+void   Server_Cmd_SET(tClient *Client, char *Args);
 void   Server_Cmd_ENUMUSERS(tClient *Client, char *Args);
 void   Server_Cmd_USERINFO(tClient *Client, char *Args);
 void   _SendUserInfo(tClient *Client, int UserID);
 void   Server_Cmd_USERADD(tClient *Client, char *Args);
 void   Server_Cmd_USERFLAGS(tClient *Client, char *Args);
 // --- Helpers ---
+void   Debug(tClient *Client, const char *Format, ...);
  int   Server_int_ParseFlags(tClient *Client, const char *Str, int *Mask, int *Value);
  int   sendf(int Socket, const char *Format, ...);
 
@@ -86,6 +88,7 @@ const struct sClientCommand {
        {"GIVE", Server_Cmd_GIVE},
        {"DONATE", Server_Cmd_DONATE},
        {"ADD", Server_Cmd_ADD},
+       {"SET", Server_Cmd_SET},
        {"ENUM_USERS", Server_Cmd_ENUMUSERS},
        {"USER_INFO", Server_Cmd_USERINFO},
        {"USER_ADD", Server_Cmd_USERADD},
@@ -143,12 +146,14 @@ void Server_Start(void)
                uint    len = sizeof(client_addr);
                 int    bTrusted = 0;
                
+               // Accept a connection
                client_socket = accept(giServer_Socket, (struct sockaddr *) &client_addr, &len);
                if(client_socket < 0) {
                        fprintf(stderr, "ERROR: Unable to accept client connection\n");
                        return ;
                }
                
+               // Debug: Print the connection string
                if(giDebugLevel >= 2) {
                        char    ipstr[INET_ADDRSTRLEN];
                        inet_ntop(AF_INET, &client_addr.sin_addr, ipstr, INET_ADDRSTRLEN);
@@ -156,6 +161,10 @@ void Server_Start(void)
                                ipstr, ntohs(client_addr.sin_port));
                }
                
+               // Doesn't matter what, localhost is trusted
+               if( ntohl( client_addr.sin_addr.s_addr ) == 0x7F000001 )
+                       bTrusted = 1;
+               
                // Trusted Connections
                if( ntohs(client_addr.sin_port) < 1024 )
                {
@@ -164,6 +173,7 @@ void Server_Start(void)
                        {
                        case 0x7F000001:        // 127.0.0.1    localhost
                        //case 0x825E0D00:      // 130.95.13.0
+                       case 0x825E0D07:        // 130.95.13.7  motsugo
                        case 0x825E0D12:        // 130.95.13.18 mussel
                        case 0x825E0D17:        // 130.95.13.23 martello
                                bTrusted = 1;
@@ -332,7 +342,7 @@ void Server_Cmd_USER(tClient *Client, char *Args)
        
        // Debug!
        if( giDebugLevel )
-               printf("Client %i authenticating as '%s'\n", Client->ID, Args);
+               Debug(Client, "Authenticating as '%s'", Args);
        
        // Save username
        if(Client->Username)
@@ -393,7 +403,7 @@ void Server_Cmd_AUTOAUTH(tClient *Client, char *Args)
        // Check if trusted
        if( !Client->bIsTrusted ) {
                if(giDebugLevel)
-                       printf("Client %i: Untrusted client attempting to AUTOAUTH\n", Client->ID);
+                       Debug(Client, "Untrusted client attempting to AUTOAUTH");
                sendf(Client->Socket, "401 Untrusted\n");
                return ;
        }
@@ -402,7 +412,7 @@ void Server_Cmd_AUTOAUTH(tClient *Client, char *Args)
        Client->UID = Bank_GetAcctByName( Args );       
        if( Client->UID < 0 ) {
                if(giDebugLevel)
-                       printf("Client %i: Unknown user '%s'\n", Client->ID, Args);
+                       Debug(Client, "Unknown user '%s'", Args);
                sendf(Client->Socket, "401 Auth Failure\n");
                return ;
        }
@@ -413,9 +423,11 @@ void Server_Cmd_AUTOAUTH(tClient *Client, char *Args)
                sendf(Client->Socket, "401 Auth Failure\n");
                return ;
        }
+
+       Client->bIsAuthed = 1;
        
        if(giDebugLevel)
-               printf("Client %i: Authenticated as '%s' (%i)\n", Client->ID, Args, Client->UID);
+               Debug(Client, "Auto authenticated as '%s' (%i)", Args, Client->UID);
        
        sendf(Client->Socket, "200 Auth OK\n");
 }
@@ -437,7 +449,7 @@ void Server_Cmd_SETEUSER(tClient *Client, char *Args)
        }
 
        // Check user permissions
-       if( !(Bank_GetFlags(Client->UID) & USER_FLAG_COKE) ) {
+       if( !(Bank_GetFlags(Client->UID) & (USER_FLAG_COKE|USER_FLAG_ADMIN)) ) {
                sendf(Client->Socket, "403 Not in coke\n");
                return ;
        }
@@ -731,7 +743,7 @@ void Server_Cmd_ADD(tClient *Client, char *Args)
        reason ++;
 
        // Check user permissions
-       if( !(Bank_GetFlags(Client->UID) & USER_FLAG_COKE)  ) {
+       if( !(Bank_GetFlags(Client->UID) & (USER_FLAG_COKE|USER_FLAG_ADMIN))  ) {
                sendf(Client->Socket, "403 Not in coke\n");
                return ;
        }
@@ -771,6 +783,75 @@ void Server_Cmd_ADD(tClient *Client, char *Args)
        }
 }
 
+void Server_Cmd_SET(tClient *Client, char *Args)
+{
+       char    *user, *ammount, *reason;
+        int    uid, iAmmount;
+       
+       if( !Client->bIsAuthed ) {
+               sendf(Client->Socket, "401 Not Authenticated\n");
+               return ;
+       }
+
+       user = Args;
+
+       ammount = strchr(Args, ' ');
+       if( !ammount ) {
+               sendf(Client->Socket, "407 Invalid Argument, expected 3 parameters, 1 encountered\n");
+               return ;
+       }
+       *ammount = '\0';
+       ammount ++;
+
+       reason = strchr(ammount, ' ');
+       if( !reason ) {
+               sendf(Client->Socket, "407 Invalid Argument, expected 3 parameters, 2 encountered\n");
+               return ;
+       }
+       *reason = '\0';
+       reason ++;
+
+       // Check user permissions
+       if( !(Bank_GetFlags(Client->UID) & USER_FLAG_ADMIN)  ) {
+               sendf(Client->Socket, "403 Not an admin\n");
+               return ;
+       }
+
+       // Get recipient
+       uid = Bank_GetAcctByName(user);
+       if( uid == -1 ) {
+               sendf(Client->Socket, "404 Invalid user\n");
+               return ;
+       }
+       
+       // You can't alter an internal account
+       if( Bank_GetFlags(uid) & USER_FLAG_INTERNAL ) {
+               sendf(Client->Socket, "404 Invalid user\n");
+               return ;
+       }
+
+       // Parse ammount
+       iAmmount = atoi(ammount);
+       if( iAmmount == 0 && ammount[0] != '0' ) {
+               sendf(Client->Socket, "407 Invalid Argument\n");
+               return ;
+       }
+
+       // Do give
+       switch( DispenseSet(Client->UID, uid, iAmmount, reason) )
+       {
+       case 0:
+               sendf(Client->Socket, "200 Add OK\n");
+               return ;
+       case 2:
+               sendf(Client->Socket, "402 Poor Guy\n");
+               return ;
+       default:
+               sendf(Client->Socket, "500 Unknown error\n");
+               return ;
+       }
+}
+
 void Server_Cmd_ENUMUSERS(tClient *Client, char *Args)
 {
         int    i, numRet = 0;
@@ -950,10 +1031,14 @@ void Server_Cmd_USERINFO(tClient *Client, char *Args)
        space = strchr(user, ' ');
        if(space)       *space = '\0';
        
+       if( giDebugLevel )      Debug(Client, "User Info '%s'", user);
+       
        // Get recipient
        uid = Bank_GetAcctByName(user);
+       
+       if( giDebugLevel >= 2 ) Debug(Client, "uid = %i", uid);
        if( uid == -1 ) {
-               sendf(Client->Socket, "404 Invalid user");
+               sendf(Client->Socket, "404 Invalid user\n");
                return ;
        }
        
@@ -1073,6 +1158,17 @@ void Server_Cmd_USERFLAGS(tClient *Client, char *Args)
 }
 
 // --- INTERNAL HELPERS ---
+void Debug(tClient *Client, const char *Format, ...)
+{
+       va_list args;
+       //printf("%010i [%i] ", (int)time(NULL), Client->ID);
+       printf("[%i] ", Client->ID);
+       va_start(args, Format);
+       vprintf(Format, args);
+       va_end(args);
+       printf("\n");
+}
+
 int sendf(int Socket, const char *Format, ...)
 {
        va_list args;

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