Started on the client
[tpg/opendispense2.git] / src / server / server.c
1 /*
2  * OpenDispense 2 
3  * UCC (University [of WA] Computer Club) Electronic Accounting System
4  *
5  * server.c - Client Server Code
6  *
7  * This file is licenced under the 3-clause BSD Licence. See the file
8  * COPYING for full details.
9  */
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include "common.h"
13 #include <sys/socket.h>
14 #include <netinet/in.h>
15 #include <arpa/inet.h>
16 #include <unistd.h>
17 #include <string.h>
18
19 #define MAX_CONNECTION_QUEUE    5
20 #define INPUT_BUFFER_SIZE       256
21
22 #define HASH_TYPE       SHA512
23 #define HASH_LENGTH     64
24
25 #define MSG_STR_TOO_LONG        "499 Command too long (limit "EXPSTR(INPUT_BUFFER_SIZE)")\n"
26
27 // === TYPES ===
28 typedef struct sClient
29 {
30          int    ID;     // Client ID
31          
32          int    bIsTrusted;     // Is the connection from a trusted host/port
33         
34         char    *Username;
35         char    Salt[9];
36         
37          int    UID;
38          int    bIsAuthed;
39 }       tClient;
40
41 // === PROTOTYPES ===
42 void    Server_Start(void);
43 void    Server_HandleClient(int Socket, int bTrusted);
44 char    *Server_ParseClientCommand(tClient *Client, char *CommandString);
45 // --- Commands ---
46 char    *Server_Cmd_USER(tClient *Client, char *Args);
47 char    *Server_Cmd_PASS(tClient *Client, char *Args);
48 char    *Server_Cmd_AUTOAUTH(tClient *Client, char *Args);
49 char    *Server_Cmd_ENUMITEMS(tClient *Client, char *Args);
50 char    *Server_Cmd_ITEMINFO(tClient *Client, char *Args);
51 char    *Server_Cmd_DISPENSE(tClient *Client, char *Args);
52 // --- Helpers ---
53 void    HexBin(uint8_t *Dest, char *Src, int BufSize);
54
55 // === GLOBALS ===
56  int    giServer_Port = 1020;
57  int    giServer_NextClientID = 1;
58 // - Commands
59 struct sClientCommand {
60         char    *Name;
61         char    *(*Function)(tClient *Client, char *Arguments);
62 }       gaServer_Commands[] = {
63         {"USER", Server_Cmd_USER},
64         {"PASS", Server_Cmd_PASS},
65         {"AUTOAUTH", Server_Cmd_AUTOAUTH},
66         {"ENUM_ITEMS", Server_Cmd_ENUMITEMS},
67         {"ITEM_INFO", Server_Cmd_ITEMINFO},
68         {"DISPENSE", Server_Cmd_DISPENSE}
69 };
70 #define NUM_COMMANDS    (sizeof(gaServer_Commands)/sizeof(gaServer_Commands[0]))
71
72 // === CODE ===
73 /**
74  * \brief Open listenting socket and serve connections
75  */
76 void Server_Start(void)
77 {
78          int    server_socket, client_socket;
79         struct sockaddr_in      server_addr, client_addr;
80
81         // Create Server
82         server_socket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
83         if( server_socket < 0 ) {
84                 fprintf(stderr, "ERROR: Unable to create server socket\n");
85                 return ;
86         }
87         
88         // Make listen address
89         memset(&server_addr, 0, sizeof(server_addr));
90         server_addr.sin_family = AF_INET;       // Internet Socket
91         server_addr.sin_addr.s_addr = htonl(INADDR_ANY);        // Listen on all interfaces
92         server_addr.sin_port = htons(giServer_Port);    // Port
93
94         // Bind
95         if( bind(server_socket, (struct sockaddr *) &server_addr, sizeof(server_addr)) < 0 ) {
96                 fprintf(stderr, "ERROR: Unable to bind to 0.0.0.0:%i\n", giServer_Port);
97                 return ;
98         }
99         
100         // Listen
101         if( listen(server_socket, MAX_CONNECTION_QUEUE) < 0 ) {
102                 fprintf(stderr, "ERROR: Unable to listen to socket\n");
103                 return ;
104         }
105         
106         printf("Listening on 0.0.0.0:%i\n", giServer_Port);
107         
108         for(;;)
109         {
110                 uint    len = sizeof(client_addr);
111                  int    bTrusted = 0;
112                 
113                 client_socket = accept(server_socket, (struct sockaddr *) &client_addr, &len);
114                 if(client_socket < 0) {
115                         fprintf(stderr, "ERROR: Unable to accept client connection\n");
116                         return ;
117                 }
118                 
119                 if(giDebugLevel >= 2) {
120                         char    ipstr[INET_ADDRSTRLEN];
121                         inet_ntop(AF_INET, &client_addr.sin_addr, ipstr, INET_ADDRSTRLEN);
122                         printf("Client connection from %s:%i\n",
123                                 ipstr, ntohs(client_addr.sin_port));
124                 }
125                 
126                 // Trusted Connections
127                 if( ntohs(client_addr.sin_port) < 1024 )
128                 {
129                         // TODO: Make this runtime configurable
130                         switch( ntohl( client_addr.sin_addr.s_addr ) )
131                         {
132                         case 0x7F000001:        // 127.0.0.1    localhost
133                         //case 0x825E0D00:      // 130.95.13.0
134                         case 0x825E0D12:        // 130.95.13.18 mussel
135                         case 0x825E0D17:        // 130.95.13.23 martello
136                                 bTrusted = 1;
137                                 break;
138                         default:
139                                 break;
140                         }
141                 }
142                 
143                 // TODO: Multithread this?
144                 Server_HandleClient(client_socket, bTrusted);
145                 
146                 close(client_socket);
147         }
148 }
149
150 /**
151  * \brief Reads from a client socket and parses the command strings
152  * \param Socket        Client socket number/handle
153  * \param bTrusted      Is the client trusted?
154  */
155 void Server_HandleClient(int Socket, int bTrusted)
156 {
157         char    inbuf[INPUT_BUFFER_SIZE];
158         char    *buf = inbuf;
159          int    remspace = INPUT_BUFFER_SIZE-1;
160          int    bytes = -1;
161         tClient clientInfo = {0};
162         
163         // Initialise Client info
164         clientInfo.ID = giServer_NextClientID ++;
165         clientInfo.bIsTrusted = bTrusted;
166         
167         // Read from client
168         /*
169          * Notes:
170          * - The `buf` and `remspace` variables allow a line to span several
171          *   calls to recv(), if a line is not completed in one recv() call
172          *   it is saved to the beginning of `inbuf` and `buf` is updated to
173          *   the end of it.
174          */
175         while( (bytes = recv(Socket, buf, remspace, 0)) > 0 )
176         {
177                 char    *eol, *start;
178                 buf[bytes] = '\0';      // Allow us to use stdlib string functions on it
179                 
180                 // Split by lines
181                 start = inbuf;
182                 while( (eol = strchr(start, '\n')) )
183                 {
184                         char    *ret;
185                         *eol = '\0';
186                         ret = Server_ParseClientCommand(&clientInfo, start);
187                         // `ret` is a string on the heap
188                         send(Socket, ret, strlen(ret), 0);
189                         free(ret);
190                         start = eol + 1;
191                 }
192                 
193                 // Check if there was an incomplete line
194                 if( *start != '\0' ) {
195                          int    tailBytes = bytes - (start-buf);
196                         // Roll back in buffer
197                         memcpy(inbuf, start, tailBytes);
198                         remspace -= tailBytes;
199                         if(remspace == 0) {
200                                 send(Socket, MSG_STR_TOO_LONG, sizeof(MSG_STR_TOO_LONG), 0);
201                                 buf = inbuf;
202                                 remspace = INPUT_BUFFER_SIZE - 1;
203                         }
204                 }
205                 else {
206                         buf = inbuf;
207                         remspace = INPUT_BUFFER_SIZE - 1;
208                 }
209         }
210         
211         // Check for errors
212         if( bytes < 0 ) {
213                 fprintf(stderr, "ERROR: Unable to recieve from client on socket %i\n", Socket);
214                 return ;
215         }
216         
217         if(giDebugLevel >= 2) {
218                 printf("Client %i: Disconnected\n", clientInfo.ID);
219         }
220 }
221
222 /**
223  * \brief Parses a client command and calls the required helper function
224  * \param Client        Pointer to client state structure
225  * \param CommandString Command from client (single line of the command)
226  * \return Heap String to return to the client
227  */
228 char *Server_ParseClientCommand(tClient *Client, char *CommandString)
229 {
230         char    *space, *args;
231          int    i;
232         
233         // Split at first space
234         space = strchr(CommandString, ' ');
235         if(space == NULL) {
236                 args = NULL;
237         }
238         else {
239                 *space = '\0';
240                 args = space + 1;
241         }
242         
243         // Find command
244         for( i = 0; i < NUM_COMMANDS; i++ )
245         {
246                 if(strcmp(CommandString, gaServer_Commands[i].Name) == 0)
247                         return gaServer_Commands[i].Function(Client, args);
248         }
249         
250         return strdup("400 Unknown Command\n");
251 }
252
253 // ---
254 // Commands
255 // ---
256 /**
257  * \brief Set client username
258  * 
259  * Usage: USER <username>
260  */
261 char *Server_Cmd_USER(tClient *Client, char *Args)
262 {
263         char    *ret;
264         
265         // Debug!
266         if( giDebugLevel )
267                 printf("Client %i authenticating as '%s'\n", Client->ID, Args);
268         
269         // Save username
270         if(Client->Username)
271                 free(Client->Username);
272         Client->Username = strdup(Args);
273         
274         #if USE_SALT
275         // Create a salt (that changes if the username is changed)
276         // Yes, I know, I'm a little paranoid, but who isn't?
277         Client->Salt[0] = 0x21 + (rand()&0x3F);
278         Client->Salt[1] = 0x21 + (rand()&0x3F);
279         Client->Salt[2] = 0x21 + (rand()&0x3F);
280         Client->Salt[3] = 0x21 + (rand()&0x3F);
281         Client->Salt[4] = 0x21 + (rand()&0x3F);
282         Client->Salt[5] = 0x21 + (rand()&0x3F);
283         Client->Salt[6] = 0x21 + (rand()&0x3F);
284         Client->Salt[7] = 0x21 + (rand()&0x3F);
285         
286         // "100 Salt xxxxXXXX\n"
287         ret = strdup("100 SALT xxxxXXXX\n");
288         sprintf(ret, "100 SALT %s\n", Client->Salt);
289         #else
290         ret = strdup("100 User Set\n");
291         #endif
292         return ret;
293 }
294
295 /**
296  * \brief Authenticate as a user
297  * 
298  * Usage: PASS <hash>
299  */
300 char *Server_Cmd_PASS(tClient *Client, char *Args)
301 {
302         uint8_t clienthash[HASH_LENGTH] = {0};
303         
304         // Read user's hash
305         HexBin(clienthash, Args, HASH_LENGTH);
306         
307         // TODO: Decrypt password passed
308         
309         Client->UID = GetUserAuth(Client->Username, "");
310
311         if( Client->UID != -1 ) {
312                 Client->bIsAuthed = 1;
313                 return strdup("200 Auth OK\n");
314         }
315
316         if( giDebugLevel ) {
317                  int    i;
318                 printf("Client %i: Password hash ", Client->ID);
319                 for(i=0;i<HASH_LENGTH;i++)
320                         printf("%02x", clienthash[i]&0xFF);
321                 printf("\n");
322         }
323         
324         return strdup("401 Auth Failure\n");
325 }
326
327 /**
328  * \brief Authenticate as a user without a password
329  * 
330  * Usage: AUTOAUTH <user>
331  */
332 char *Server_Cmd_AUTOAUTH(tClient *Client, char *Args)
333 {
334         char    *spos = strchr(Args, ' ');
335         if(spos)        *spos = '\0';   // Remove characters after the ' '
336         
337         // Check if trusted
338         if( !Client->bIsTrusted ) {
339                 if(giDebugLevel)
340                         printf("Client %i: Untrusted client attempting to AUTOAUTH\n", Client->ID);
341                 return strdup("401 Untrusted\n");
342         }
343         
344         // Get UID
345         Client->UID = GetUserID( Args );
346         if( Client->UID < 0 ) {
347                 if(giDebugLevel)
348                         printf("Client %i: Unknown user '%s'\n", Client->ID, Args);
349                 return strdup("401 Auth Failure\n");
350         }
351         
352         if(giDebugLevel)
353                 printf("Client %i: Authenticated as '%s' (%i)\n", Client->ID, Args, Client->UID);
354         
355         return strdup("200 Auth OK\n");
356 }
357
358 /**
359  * \brief Enumerate the items that the server knows about
360  */
361 char *Server_Cmd_ENUMITEMS(tClient *Client, char *Args)
362 {
363 //       int    nItems = giNumItems;
364          int    retLen;
365          int    i;
366         char    *ret;
367
368         retLen = snprintf(NULL, 0, "201 Items %i", giNumItems);
369
370         for( i = 0; i < giNumItems; i ++ )
371         {
372                 retLen += snprintf(NULL, 0, " %s:%i", gaItems[i].Handler->Name, gaItems[i].ID);
373         }
374
375         ret = malloc(retLen+1);
376         retLen = 0;
377         retLen += sprintf(ret+retLen, "201 Items %i", giNumItems);
378
379         for( i = 0; i < giNumItems; i ++ ) {
380                 retLen += sprintf(ret+retLen, " %s:%i", gaItems[i].Handler->Name, gaItems[i].ID);
381         }
382
383         strcat(ret, "\n");
384
385         return ret;
386 }
387
388 tItem *_GetItemFromString(char *String)
389 {
390         tHandler        *handler;
391         char    *type = String;
392         char    *colon = strchr(String, ':');
393          int    num, i;
394         
395         if( !colon ) {
396                 return NULL;
397         }
398
399         num = atoi(colon+1);
400         *colon = '\0';
401
402         // Find handler
403         handler = NULL;
404         for( i = 0; i < giNumHandlers; i ++ )
405         {
406                 if( strcmp(gaHandlers[i]->Name, type) == 0) {
407                         handler = gaHandlers[i];
408                         break;
409                 }
410         }
411         if( !handler ) {
412                 return NULL;
413         }
414
415         // Find item
416         for( i = 0; i < giNumItems; i ++ )
417         {
418                 if( gaItems[i].Handler != handler )     continue;
419                 if( gaItems[i].ID != num )      continue;
420                 return &gaItems[i];
421         }
422         return NULL;
423 }
424
425 /**
426  * \brief Fetch information on a specific item
427  */
428 char *Server_Cmd_ITEMINFO(tClient *Client, char *Args)
429 {
430          int    retLen = 0;
431         char    *ret;
432         tItem   *item = _GetItemFromString(Args);
433         
434         if( !item ) {
435                 return strdup("406 Bad Item ID\n");
436         }
437
438         // Create return
439         retLen = snprintf(NULL, 0, "202 Item %s:%i %i %s\n",
440                 item->Handler->Name, item->ID, item->Price, item->Name);
441         ret = malloc(retLen+1);
442         sprintf(ret, "202 Item %s:%i %i %s\n",
443                 item->Handler->Name, item->ID, item->Price, item->Name);
444
445         return ret;
446 }
447
448 char *Server_Cmd_DISPENSE(tClient *Client, char *Args)
449 {
450         tItem   *item;
451         if( !Client->bIsAuthed )        return strdup("401 Not Authenticated\n");
452
453         item = _GetItemFromString(Args);
454         if( !item ) {
455                 return strdup("406 Bad Item ID\n");
456         }
457
458         switch( DispenseItem( Client->UID, item ) )
459         {
460         case 0: return strdup("200 Dispense OK\n");
461         case 1: return strdup("501 Unable to dispense\n");
462         case 2: return strdup("402 Poor You\n");
463         default:
464                 return strdup("500 Dispense Error\n");
465         }
466 }
467
468 char *Server_Cmd_GIVE(tClient *Client, char *Args)
469 {
470         char    *recipient, *ammount, *reason;
471          int    uid, iAmmount;
472         
473         if( !Client->bIsAuthed )        return strdup("401 Not Authenticated\n");
474
475         recipient = Args;
476
477         ammount = strchr(Args, ' ');
478         if( !ammount )  return strdup("407 Invalid Argument, expected 3 parameters, 1 encountered\n");
479         *ammount = '\0';
480         ammount ++;
481
482         reason = strchr(ammount, ' ');
483         if( !reason )   return strdup("407 Invalid Argument, expected 3 parameters, 2 encountered\n");
484         *reason = '\0';
485         reason ++;
486
487         // Get recipient
488         uid = GetUserID(recipient);
489         if( uid == -1 ) return strdup("404 Invalid target user");
490
491         // Parse ammount
492         iAmmount = atoi(ammount);
493         if( iAmmount <= 0 )     return strdup("407 Invalid Argument, ammount must be > zero\n");
494
495         // Do give
496         switch( Transfer(Client->UID, uid, iAmmount, reason) )
497         {
498         case 0:
499                 return strdup("200 Give OK\n");
500         default:
501                 return strdup("402 Poor You\n");
502         }
503 }
504
505 // --- INTERNAL HELPERS ---
506 // TODO: Move to another file
507 void HexBin(uint8_t *Dest, char *Src, int BufSize)
508 {
509          int    i;
510         for( i = 0; i < BufSize; i ++ )
511         {
512                 uint8_t val = 0;
513                 
514                 if('0' <= *Src && *Src <= '9')
515                         val |= (*Src-'0') << 4;
516                 else if('A' <= *Src && *Src <= 'F')
517                         val |= (*Src-'A'+10) << 4;
518                 else if('a' <= *Src && *Src <= 'f')
519                         val |= (*Src-'a'+10) << 4;
520                 else
521                         break;
522                 Src ++;
523                 
524                 if('0' <= *Src && *Src <= '9')
525                         val |= (*Src-'0');
526                 else if('A' <= *Src && *Src <= 'F')
527                         val |= (*Src-'A'+10);
528                 else if('a' <= *Src && *Src <= 'f')
529                         val |= (*Src-'a'+10);
530                 else
531                         break;
532                 Src ++;
533                 
534                 Dest[i] = val;
535         }
536         for( ; i < BufSize; i++ )
537                 Dest[i] = 0;
538 }
539
540 /**
541  * \brief Decode a Base64 value
542  */
543 int UnBase64(uint8_t *Dest, char *Src, int BufSize)
544 {
545         uint32_t        val;
546          int    i, j;
547         char    *start_src = Src;
548         
549         for( i = 0; i+2 < BufSize; i += 3 )
550         {
551                 val = 0;
552                 for( j = 0; j < 4; j++, Src ++ ) {
553                         if('A' <= *Src && *Src <= 'Z')
554                                 val |= (*Src - 'A') << ((3-j)*6);
555                         else if('a' <= *Src && *Src <= 'z')
556                                 val |= (*Src - 'a' + 26) << ((3-j)*6);
557                         else if('0' <= *Src && *Src <= '9')
558                                 val |= (*Src - '0' + 52) << ((3-j)*6);
559                         else if(*Src == '+')
560                                 val |= 62 << ((3-j)*6);
561                         else if(*Src == '/')
562                                 val |= 63 << ((3-j)*6);
563                         else if(!*Src)
564                                 break;
565                         else if(*Src != '=')
566                                 j --;   // Ignore invalid characters
567                 }
568                 Dest[i  ] = (val >> 16) & 0xFF;
569                 Dest[i+1] = (val >> 8) & 0xFF;
570                 Dest[i+2] = val & 0xFF;
571                 if(j != 4)      break;
572         }
573         
574         // Finish things off
575         if(i   < BufSize)
576                 Dest[i] = (val >> 16) & 0xFF;
577         if(i+1 < BufSize)
578                 Dest[i+1] = (val >> 8) & 0xFF;
579         
580         return Src - start_src;
581 }

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