3 * UCC (University [of WA] Computer Club) Electronic Accounting System
5 * server.c - Client Server Code
7 * This file is licenced under the 3-clause BSD Licence. See the file
8 * COPYING for full details.
13 #include <sys/socket.h>
14 #include <netinet/in.h>
15 #include <arpa/inet.h>
21 #define DEBUG_TRACE_CLIENT 0
24 #define MAX_CONNECTION_QUEUE 5
25 #define INPUT_BUFFER_SIZE 256
26 #define CLIENT_TIMEOUT 10 // Seconds
28 #define HASH_TYPE SHA1
29 #define HASH_LENGTH 20
31 #define MSG_STR_TOO_LONG "499 Command too long (limit "EXPSTR(INPUT_BUFFER_SIZE)")\n"
34 typedef struct sClient
36 int Socket; // Client socket ID
39 int bIsTrusted; // Is the connection from a trusted host/port
50 void Server_Start(void);
51 void Server_Cleanup(void);
52 void Server_HandleClient(int Socket, int bTrusted);
53 void Server_ParseClientCommand(tClient *Client, char *CommandString);
55 void Server_Cmd_USER(tClient *Client, char *Args);
56 void Server_Cmd_PASS(tClient *Client, char *Args);
57 void Server_Cmd_AUTOAUTH(tClient *Client, char *Args);
58 void Server_Cmd_SETEUSER(tClient *Client, char *Args);
59 void Server_Cmd_ENUMITEMS(tClient *Client, char *Args);
60 void Server_Cmd_ITEMINFO(tClient *Client, char *Args);
61 void Server_Cmd_DISPENSE(tClient *Client, char *Args);
62 void Server_Cmd_GIVE(tClient *Client, char *Args);
63 void Server_Cmd_DONATE(tClient *Client, char *Args);
64 void Server_Cmd_ADD(tClient *Client, char *Args);
65 void Server_Cmd_SET(tClient *Client, char *Args);
66 void Server_Cmd_ENUMUSERS(tClient *Client, char *Args);
67 void Server_Cmd_USERINFO(tClient *Client, char *Args);
68 void _SendUserInfo(tClient *Client, int UserID);
69 void Server_Cmd_USERADD(tClient *Client, char *Args);
70 void Server_Cmd_USERFLAGS(tClient *Client, char *Args);
72 void Debug(tClient *Client, const char *Format, ...);
73 int Server_int_ParseFlags(tClient *Client, const char *Str, int *Mask, int *Value);
74 int sendf(int Socket, const char *Format, ...);
78 const struct sClientCommand {
80 void (*Function)(tClient *Client, char *Arguments);
81 } gaServer_Commands[] = {
82 {"USER", Server_Cmd_USER},
83 {"PASS", Server_Cmd_PASS},
84 {"AUTOAUTH", Server_Cmd_AUTOAUTH},
85 {"SETEUSER", Server_Cmd_SETEUSER},
86 {"ENUM_ITEMS", Server_Cmd_ENUMITEMS},
87 {"ITEM_INFO", Server_Cmd_ITEMINFO},
88 {"DISPENSE", Server_Cmd_DISPENSE},
89 {"GIVE", Server_Cmd_GIVE},
90 {"DONATE", Server_Cmd_DONATE},
91 {"ADD", Server_Cmd_ADD},
92 {"SET", Server_Cmd_SET},
93 {"ENUM_USERS", Server_Cmd_ENUMUSERS},
94 {"USER_INFO", Server_Cmd_USERINFO},
95 {"USER_ADD", Server_Cmd_USERADD},
96 {"USER_FLAGS", Server_Cmd_USERFLAGS}
98 #define NUM_COMMANDS ((int)(sizeof(gaServer_Commands)/sizeof(gaServer_Commands[0])))
101 int giServer_Port = 1020;
102 int giServer_NextClientID = 1;
107 * \brief Open listenting socket and serve connections
109 void Server_Start(void)
112 struct sockaddr_in server_addr, client_addr;
114 atexit(Server_Cleanup);
117 giServer_Socket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
118 if( giServer_Socket < 0 ) {
119 fprintf(stderr, "ERROR: Unable to create server socket\n");
123 // Make listen address
124 memset(&server_addr, 0, sizeof(server_addr));
125 server_addr.sin_family = AF_INET; // Internet Socket
126 server_addr.sin_addr.s_addr = htonl(INADDR_ANY); // Listen on all interfaces
127 server_addr.sin_port = htons(giServer_Port); // Port
130 if( bind(giServer_Socket, (struct sockaddr *) &server_addr, sizeof(server_addr)) < 0 ) {
131 fprintf(stderr, "ERROR: Unable to bind to 0.0.0.0:%i\n", giServer_Port);
137 if( listen(giServer_Socket, MAX_CONNECTION_QUEUE) < 0 ) {
138 fprintf(stderr, "ERROR: Unable to listen to socket\n");
143 printf("Listening on 0.0.0.0:%i\n", giServer_Port);
147 uint len = sizeof(client_addr);
150 // Accept a connection
151 client_socket = accept(giServer_Socket, (struct sockaddr *) &client_addr, &len);
152 if(client_socket < 0) {
153 fprintf(stderr, "ERROR: Unable to accept client connection\n");
157 // Set a timeout on the user conneciton
160 tv.tv_sec = CLIENT_TIMEOUT;
162 if( setsockopt(client_socket, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) )
164 perror("setsockopt");
169 // Debug: Print the connection string
170 if(giDebugLevel >= 2) {
171 char ipstr[INET_ADDRSTRLEN];
172 inet_ntop(AF_INET, &client_addr.sin_addr, ipstr, INET_ADDRSTRLEN);
173 printf("Client connection from %s:%i\n",
174 ipstr, ntohs(client_addr.sin_port));
177 // Doesn't matter what, localhost is trusted
178 if( ntohl( client_addr.sin_addr.s_addr ) == 0x7F000001 )
181 // Trusted Connections
182 if( ntohs(client_addr.sin_port) < 1024 )
184 // TODO: Make this runtime configurable
185 switch( ntohl( client_addr.sin_addr.s_addr ) )
187 case 0x7F000001: // 127.0.0.1 localhost
188 //case 0x825E0D00: // 130.95.13.0
189 case 0x825E0D07: // 130.95.13.7 motsugo
190 case 0x825E0D12: // 130.95.13.18 mussel
191 case 0x825E0D17: // 130.95.13.23 martello
199 // TODO: Multithread this?
200 Server_HandleClient(client_socket, bTrusted);
202 close(client_socket);
206 void Server_Cleanup(void)
208 printf("Close(%i)\n", giServer_Socket);
209 close(giServer_Socket);
213 * \brief Reads from a client socket and parses the command strings
214 * \param Socket Client socket number/handle
215 * \param bTrusted Is the client trusted?
217 void Server_HandleClient(int Socket, int bTrusted)
219 char inbuf[INPUT_BUFFER_SIZE];
221 int remspace = INPUT_BUFFER_SIZE-1;
225 memset(&clientInfo, 0, sizeof(clientInfo));
227 // Initialise Client info
228 clientInfo.Socket = Socket;
229 clientInfo.ID = giServer_NextClientID ++;
230 clientInfo.bIsTrusted = bTrusted;
231 clientInfo.EffectiveUID = -1;
236 * - The `buf` and `remspace` variables allow a line to span several
237 * calls to recv(), if a line is not completed in one recv() call
238 * it is saved to the beginning of `inbuf` and `buf` is updated to
241 // TODO: Use select() instead (to give a timeout)
242 while( (bytes = recv(Socket, buf, remspace, 0)) > 0 )
245 buf[bytes] = '\0'; // Allow us to use stdlib string functions on it
249 while( (eol = strchr(start, '\n')) )
253 Server_ParseClientCommand(&clientInfo, start);
258 // Check if there was an incomplete line
259 if( *start != '\0' ) {
260 int tailBytes = bytes - (start-buf);
261 // Roll back in buffer
262 memcpy(inbuf, start, tailBytes);
263 remspace -= tailBytes;
265 send(Socket, MSG_STR_TOO_LONG, sizeof(MSG_STR_TOO_LONG), 0);
267 remspace = INPUT_BUFFER_SIZE - 1;
272 remspace = INPUT_BUFFER_SIZE - 1;
278 fprintf(stderr, "ERROR: Unable to recieve from client on socket %i\n", Socket);
282 if(giDebugLevel >= 2) {
283 printf("Client %i: Disconnected\n", clientInfo.ID);
288 * \brief Parses a client command and calls the required helper function
289 * \param Client Pointer to client state structure
290 * \param CommandString Command from client (single line of the command)
291 * \return Heap String to return to the client
293 void Server_ParseClientCommand(tClient *Client, char *CommandString)
302 // Split at first space
303 space = strchr(CommandString, ' ');
310 while( *space == ' ' ) space ++;
315 for( i = 0; args[i]; )
317 while( CommandString[i] != ' ' ) {
318 if( CommandString[i] == '"' ) {
319 while( !(CommandString[i] != '\\' CommandString[i+1] == '"' ) )
326 while( CommandString[i] == ' ' ) i ++;
333 for( i = 0; i < NUM_COMMANDS; i++ )
335 if(strcmp(CommandString, gaServer_Commands[i].Name) == 0) {
336 gaServer_Commands[i].Function(Client, args);
341 sendf(Client->Socket, "400 Unknown Command\n");
348 * \brief Set client username
350 * Usage: USER <username>
352 void Server_Cmd_USER(tClient *Client, char *Args)
354 char *space = strchr(Args, ' ');
355 if(space) *space = '\0'; // Remove characters after the ' '
359 Debug(Client, "Authenticating as '%s'", Args);
363 free(Client->Username);
364 Client->Username = strdup(Args);
367 // Create a salt (that changes if the username is changed)
368 // Yes, I know, I'm a little paranoid, but who isn't?
369 Client->Salt[0] = 0x21 + (rand()&0x3F);
370 Client->Salt[1] = 0x21 + (rand()&0x3F);
371 Client->Salt[2] = 0x21 + (rand()&0x3F);
372 Client->Salt[3] = 0x21 + (rand()&0x3F);
373 Client->Salt[4] = 0x21 + (rand()&0x3F);
374 Client->Salt[5] = 0x21 + (rand()&0x3F);
375 Client->Salt[6] = 0x21 + (rand()&0x3F);
376 Client->Salt[7] = 0x21 + (rand()&0x3F);
378 // TODO: Also send hash type to use, (SHA1 or crypt according to [DAA])
379 sendf(Client->Socket, "100 SALT %s\n", Client->Salt);
381 sendf(Client->Socket, "100 User Set\n");
386 * \brief Authenticate as a user
390 void Server_Cmd_PASS(tClient *Client, char *Args)
392 char *space = strchr(Args, ' ');
393 if(space) *space = '\0'; // Remove characters after the ' '
395 // Pass on to cokebank
396 Client->UID = Bank_GetUserAuth(Client->Salt, Client->Username, Args);
398 if( Client->UID != -1 ) {
399 Client->bIsAuthed = 1;
400 sendf(Client->Socket, "200 Auth OK\n");
404 sendf(Client->Socket, "401 Auth Failure\n");
408 * \brief Authenticate as a user without a password
410 * Usage: AUTOAUTH <user>
412 void Server_Cmd_AUTOAUTH(tClient *Client, char *Args)
414 char *space = strchr(Args, ' ');
415 if(space) *space = '\0'; // Remove characters after the ' '
418 if( !Client->bIsTrusted ) {
420 Debug(Client, "Untrusted client attempting to AUTOAUTH");
421 sendf(Client->Socket, "401 Untrusted\n");
426 Client->UID = Bank_GetAcctByName( Args );
427 if( Client->UID < 0 ) {
429 Debug(Client, "Unknown user '%s'", Args);
430 sendf(Client->Socket, "401 Auth Failure\n");
434 // You can't be an internal account
435 if( Bank_GetFlags(Client->UID) & USER_FLAG_INTERNAL ) {
437 sendf(Client->Socket, "401 Auth Failure\n");
441 Client->bIsAuthed = 1;
444 Debug(Client, "Auto authenticated as '%s' (%i)", Args, Client->UID);
446 sendf(Client->Socket, "200 Auth OK\n");
450 * \brief Set effective user
452 void Server_Cmd_SETEUSER(tClient *Client, char *Args)
456 space = strchr(Args, ' ');
458 if(space) *space = '\0';
460 if( !strlen(Args) ) {
461 sendf(Client->Socket, "407 SETEUSER expects an argument\n");
465 // Check user permissions
466 if( !(Bank_GetFlags(Client->UID) & (USER_FLAG_COKE|USER_FLAG_ADMIN)) ) {
467 sendf(Client->Socket, "403 Not in coke\n");
472 Client->EffectiveUID = Bank_GetAcctByName(Args);
473 if( Client->EffectiveUID == -1 ) {
474 sendf(Client->Socket, "404 User not found\n");
478 // You can't be an internal account
479 if( Bank_GetFlags(Client->EffectiveUID) & USER_FLAG_INTERNAL ) {
480 Client->EffectiveUID = -1;
481 sendf(Client->Socket, "404 User not found\n");
485 sendf(Client->Socket, "200 User set\n");
489 * \brief Enumerate the items that the server knows about
491 void Server_Cmd_ENUMITEMS(tClient *Client, char *Args)
495 if( Args != NULL && strlen(Args) ) {
496 sendf(Client->Socket, "407 ENUM_ITEMS takes no arguments\n");
502 for( i = 0; i < giNumItems; i ++ ) {
503 if( gaItems[i].bHidden ) continue;
507 sendf(Client->Socket, "201 Items %i\n", count);
509 for( i = 0; i < giNumItems; i ++ ) {
510 if( gaItems[i].bHidden ) continue;
511 sendf(Client->Socket,
512 "202 Item %s:%i %i %s\n",
513 gaItems[i].Handler->Name, gaItems[i].ID, gaItems[i].Price, gaItems[i].Name
517 sendf(Client->Socket, "200 List end\n");
520 tItem *_GetItemFromString(char *String)
524 char *colon = strchr(String, ':');
536 for( i = 0; i < giNumHandlers; i ++ )
538 if( strcmp(gaHandlers[i]->Name, type) == 0) {
539 handler = gaHandlers[i];
548 for( i = 0; i < giNumItems; i ++ )
550 if( gaItems[i].Handler != handler ) continue;
551 if( gaItems[i].ID != num ) continue;
558 * \brief Fetch information on a specific item
560 void Server_Cmd_ITEMINFO(tClient *Client, char *Args)
562 tItem *item = _GetItemFromString(Args);
565 sendf(Client->Socket, "406 Bad Item ID\n");
569 sendf(Client->Socket,
570 "202 Item %s:%i %i %s\n",
571 item->Handler->Name, item->ID, item->Price, item->Name
575 void Server_Cmd_DISPENSE(tClient *Client, char *Args)
581 if( !Client->bIsAuthed ) {
582 sendf(Client->Socket, "401 Not Authenticated\n");
586 item = _GetItemFromString(Args);
588 sendf(Client->Socket, "406 Bad Item ID\n");
592 if( Client->EffectiveUID != -1 ) {
593 uid = Client->EffectiveUID;
599 switch( ret = DispenseItem( Client->UID, uid, item ) )
601 case 0: sendf(Client->Socket, "200 Dispense OK\n"); return ;
602 case 1: sendf(Client->Socket, "501 Unable to dispense\n"); return ;
603 case 2: sendf(Client->Socket, "402 Poor You\n"); return ;
605 sendf(Client->Socket, "500 Dispense Error\n");
610 void Server_Cmd_GIVE(tClient *Client, char *Args)
612 char *recipient, *ammount, *reason;
616 if( !Client->bIsAuthed ) {
617 sendf(Client->Socket, "401 Not Authenticated\n");
623 ammount = strchr(Args, ' ');
625 sendf(Client->Socket, "407 Invalid Argument, expected 3 parameters, 1 encountered\n");
631 reason = strchr(ammount, ' ');
633 sendf(Client->Socket, "407 Invalid Argument, expected 3 parameters, 2 encountered\n");
640 uid = Bank_GetAcctByName(recipient);
642 sendf(Client->Socket, "404 Invalid target user\n");
646 // You can't alter an internal account
647 if( Bank_GetFlags(uid) & USER_FLAG_INTERNAL ) {
648 sendf(Client->Socket, "404 Invalid target user\n");
653 iAmmount = atoi(ammount);
654 if( iAmmount <= 0 ) {
655 sendf(Client->Socket, "407 Invalid Argument, ammount must be > zero\n");
659 if( Client->EffectiveUID != -1 ) {
660 thisUid = Client->EffectiveUID;
663 thisUid = Client->UID;
667 switch( DispenseGive(Client->UID, thisUid, uid, iAmmount, reason) )
670 sendf(Client->Socket, "200 Give OK\n");
673 sendf(Client->Socket, "402 Poor You\n");
676 sendf(Client->Socket, "500 Unknown error\n");
681 void Server_Cmd_DONATE(tClient *Client, char *Args)
683 char *ammount, *reason;
687 if( !Client->bIsAuthed ) {
688 sendf(Client->Socket, "401 Not Authenticated\n");
694 // Get the start of the reason
695 reason = strchr(Args, ' ');
697 sendf(Client->Socket, "407 Invalid Argument, expected 2 parameters, 1 encountered\n");
703 // Check the end of the reason
704 if( strchr(reason, ' ') ) {
705 sendf(Client->Socket, "407 Invalid Argument, expected 2 parameters, more encountered\n");
710 iAmmount = atoi(ammount);
711 if( iAmmount <= 0 ) {
712 sendf(Client->Socket, "407 Invalid Argument, ammount must be > zero\n");
716 // Handle effective users
717 if( Client->EffectiveUID != -1 ) {
718 thisUid = Client->EffectiveUID;
721 thisUid = Client->UID;
725 switch( DispenseDonate(Client->UID, thisUid, iAmmount, reason) )
728 sendf(Client->Socket, "200 Give OK\n");
731 sendf(Client->Socket, "402 Poor You\n");
734 sendf(Client->Socket, "500 Unknown error\n");
739 void Server_Cmd_ADD(tClient *Client, char *Args)
741 char *user, *ammount, *reason;
744 if( !Client->bIsAuthed ) {
745 sendf(Client->Socket, "401 Not Authenticated\n");
751 ammount = strchr(Args, ' ');
753 sendf(Client->Socket, "407 Invalid Argument, expected 3 parameters, 1 encountered\n");
759 reason = strchr(ammount, ' ');
761 sendf(Client->Socket, "407 Invalid Argument, expected 3 parameters, 2 encountered\n");
767 // Check user permissions
768 if( !(Bank_GetFlags(Client->UID) & (USER_FLAG_COKE|USER_FLAG_ADMIN)) ) {
769 sendf(Client->Socket, "403 Not in coke\n");
774 uid = Bank_GetAcctByName(user);
776 sendf(Client->Socket, "404 Invalid user\n");
780 // You can't alter an internal account
781 if( Bank_GetFlags(uid) & USER_FLAG_INTERNAL ) {
782 sendf(Client->Socket, "404 Invalid user\n");
787 iAmmount = atoi(ammount);
788 if( iAmmount == 0 && ammount[0] != '0' ) {
789 sendf(Client->Socket, "407 Invalid Argument\n");
794 switch( DispenseAdd(Client->UID, uid, iAmmount, reason) )
797 sendf(Client->Socket, "200 Add OK\n");
800 sendf(Client->Socket, "402 Poor Guy\n");
803 sendf(Client->Socket, "500 Unknown error\n");
808 void Server_Cmd_SET(tClient *Client, char *Args)
810 char *user, *ammount, *reason;
813 if( !Client->bIsAuthed ) {
814 sendf(Client->Socket, "401 Not Authenticated\n");
820 ammount = strchr(Args, ' ');
822 sendf(Client->Socket, "407 Invalid Argument, expected 3 parameters, 1 encountered\n");
828 reason = strchr(ammount, ' ');
830 sendf(Client->Socket, "407 Invalid Argument, expected 3 parameters, 2 encountered\n");
836 // Check user permissions
837 if( !(Bank_GetFlags(Client->UID) & USER_FLAG_ADMIN) ) {
838 sendf(Client->Socket, "403 Not an admin\n");
843 uid = Bank_GetAcctByName(user);
845 sendf(Client->Socket, "404 Invalid user\n");
849 // You can't alter an internal account
850 if( Bank_GetFlags(uid) & USER_FLAG_INTERNAL ) {
851 sendf(Client->Socket, "404 Invalid user\n");
856 iAmmount = atoi(ammount);
857 if( iAmmount == 0 && ammount[0] != '0' ) {
858 sendf(Client->Socket, "407 Invalid Argument\n");
863 switch( DispenseSet(Client->UID, uid, iAmmount, reason) )
866 sendf(Client->Socket, "200 Add OK\n");
869 sendf(Client->Socket, "402 Poor Guy\n");
872 sendf(Client->Socket, "500 Unknown error\n");
877 void Server_Cmd_ENUMUSERS(tClient *Client, char *Args)
881 int maxBal = INT_MAX, minBal = INT_MIN;
882 int flagMask = 0, flagVal = 0;
883 int sort = BANK_ITFLAG_SORT_NAME;
884 time_t lastSeenAfter=0, lastSeenBefore=0;
886 int flags; // Iterator flags
887 int balValue; // Balance value for iterator
888 time_t timeValue; // Time value for iterator
891 if( Args && strlen(Args) )
893 char *space = Args, *type, *val;
898 space = strchr(space, ' ');
899 if(space) *space = '\0';
902 val = strchr(type, ':');
909 if( strcmp(type, "min_balance") == 0 ) {
913 else if( strcmp(type, "max_balance") == 0 ) {
917 else if( strcmp(type, "flags") == 0 ) {
918 if( Server_int_ParseFlags(Client, val, &flagMask, &flagVal) )
921 // - Last seen before timestamp
922 else if( strcmp(type, "last_seen_before") == 0 ) {
923 lastSeenAfter = atoll(val);
925 // - Last seen after timestamp
926 else if( strcmp(type, "last_seen_after") == 0 ) {
927 lastSeenAfter = atoll(val);
930 else if( strcmp(type, "sort") == 0 ) {
931 char *dash = strchr(val, '-');
936 if( strcmp(val, "name") == 0 ) {
937 sort = BANK_ITFLAG_SORT_NAME;
939 else if( strcmp(val, "balance") == 0 ) {
940 sort = BANK_ITFLAG_SORT_BAL;
942 else if( strcmp(val, "lastseen") == 0 ) {
943 sort = BANK_ITFLAG_SORT_LASTSEEN;
946 sendf(Client->Socket, "407 Unknown sort field ('%s')\n", val);
949 // Handle sort direction
951 if( strcmp(dash, "desc") == 0 ) {
952 sort |= BANK_ITFLAG_REVSORT;
955 sendf(Client->Socket, "407 Unknown sort direction '%s'\n", dash);
962 sendf(Client->Socket, "407 Unknown argument to ENUM_USERS '%s:%s'\n", type, val);
969 sendf(Client->Socket, "407 Unknown argument to ENUM_USERS '%s'\n", type);
975 *space = ' '; // Repair (to be nice)
977 while(*space == ' ') space ++;
983 if( maxBal != INT_MAX ) {
984 flags = sort|BANK_ITFLAG_MAXBALANCE;
987 else if( minBal != INT_MIN ) {
988 flags = sort|BANK_ITFLAG_MINBALANCE;
995 if( lastSeenBefore ) {
996 timeValue = lastSeenBefore;
997 flags |= BANK_ITFLAG_SEENBEFORE;
999 else if( lastSeenAfter ) {
1000 timeValue = lastSeenAfter;
1001 flags |= BANK_ITFLAG_SEENAFTER;
1006 it = Bank_Iterator(flagMask, flagVal, flags, balValue, timeValue);
1008 // Get return number
1009 while( (i = Bank_IteratorNext(it)) != -1 )
1011 int bal = Bank_GetBalance(i);
1013 if( bal == INT_MIN ) continue;
1015 if( bal < minBal ) continue;
1016 if( bal > maxBal ) continue;
1021 Bank_DelIterator(it);
1024 sendf(Client->Socket, "201 Users %i\n", numRet);
1028 it = Bank_Iterator(flagMask, flagVal, flags, balValue, timeValue);
1030 while( (i = Bank_IteratorNext(it)) != -1 )
1032 int bal = Bank_GetBalance(i);
1034 if( bal == INT_MIN ) continue;
1036 if( bal < minBal ) continue;
1037 if( bal > maxBal ) continue;
1039 _SendUserInfo(Client, i);
1042 Bank_DelIterator(it);
1044 sendf(Client->Socket, "200 List End\n");
1047 void Server_Cmd_USERINFO(tClient *Client, char *Args)
1053 space = strchr(user, ' ');
1054 if(space) *space = '\0';
1056 if( giDebugLevel ) Debug(Client, "User Info '%s'", user);
1059 uid = Bank_GetAcctByName(user);
1061 if( giDebugLevel >= 2 ) Debug(Client, "uid = %i", uid);
1063 sendf(Client->Socket, "404 Invalid user\n");
1067 _SendUserInfo(Client, uid);
1070 void _SendUserInfo(tClient *Client, int UserID)
1072 char *type, *disabled="", *door="";
1073 int flags = Bank_GetFlags(UserID);
1075 if( flags & USER_FLAG_INTERNAL ) {
1078 else if( flags & USER_FLAG_COKE ) {
1079 if( flags & USER_FLAG_ADMIN )
1080 type = "coke,admin";
1084 else if( flags & USER_FLAG_ADMIN ) {
1091 if( flags & USER_FLAG_DISABLED )
1092 disabled = ",disabled";
1093 if( flags & USER_FLAG_DOORGROUP )
1096 // TODO: User flags/type
1098 Client->Socket, "202 User %s %i %s%s%s\n",
1099 Bank_GetAcctName(UserID), Bank_GetBalance(UserID),
1100 type, disabled, door
1104 void Server_Cmd_USERADD(tClient *Client, char *Args)
1106 char *username, *space;
1108 // Check permissions
1109 if( !(Bank_GetFlags(Client->UID) & USER_FLAG_ADMIN) ) {
1110 sendf(Client->Socket, "403 Not a coke admin\n");
1116 while( *username == ' ' ) username ++;
1117 space = strchr(username, ' ');
1118 if(space) *space = '\0';
1120 // Try to create user
1121 if( Bank_CreateAcct(username) == -1 ) {
1122 sendf(Client->Socket, "404 User exists\n");
1127 char *thisName = Bank_GetAcctName(Client->UID);
1128 Log_Info("Account '%s' created by '%s'", username, thisName);
1132 sendf(Client->Socket, "200 User Added\n");
1135 void Server_Cmd_USERFLAGS(tClient *Client, char *Args)
1137 char *username, *flags;
1139 int mask=0, value=0;
1142 // Check permissions
1143 if( !(Bank_GetFlags(Client->UID) & USER_FLAG_ADMIN) ) {
1144 sendf(Client->Socket, "403 Not a coke admin\n");
1151 while( *username == ' ' ) username ++;
1152 space = strchr(username, ' ');
1154 sendf(Client->Socket, "407 USER_FLAGS requires 2 arguments, 1 given\n");
1160 while( *flags == ' ' ) flags ++;
1161 space = strchr(flags, ' ');
1162 if(space) *space = '\0';
1165 uid = Bank_GetAcctByName(username);
1167 sendf(Client->Socket, "404 User '%s' not found\n", username);
1172 if( Server_int_ParseFlags(Client, flags, &mask, &value) )
1176 Debug(Client, "Set %i(%s) flags to %x (masked %x)\n",
1177 uid, username, mask, value);
1180 Bank_SetFlags(uid, mask, value);
1183 sendf(Client->Socket, "200 User Updated\n");
1186 // --- INTERNAL HELPERS ---
1187 void Debug(tClient *Client, const char *Format, ...)
1190 //printf("%010i [%i] ", (int)time(NULL), Client->ID);
1191 printf("[%i] ", Client->ID);
1192 va_start(args, Format);
1193 vprintf(Format, args);
1198 int sendf(int Socket, const char *Format, ...)
1203 va_start(args, Format);
1204 len = vsnprintf(NULL, 0, Format, args);
1209 va_start(args, Format);
1210 vsnprintf(buf, len+1, Format, args);
1213 #if DEBUG_TRACE_CLIENT
1214 printf("sendf: %s", buf);
1217 return send(Socket, buf, len, 0);
1221 int Server_int_ParseFlags(tClient *Client, const char *Str, int *Mask, int *Value)
1228 {"disabled", USER_FLAG_DISABLED, USER_FLAG_DISABLED}
1229 ,{"door", USER_FLAG_DOORGROUP, USER_FLAG_DOORGROUP}
1230 ,{"coke", USER_FLAG_COKE, USER_FLAG_COKE}
1231 ,{"admin", USER_FLAG_ADMIN, USER_FLAG_ADMIN}
1232 ,{"internal", USER_FLAG_INTERNAL, USER_FLAG_INTERNAL}
1234 const int ciNumFlags = sizeof(cFLAGS)/sizeof(cFLAGS[0]);
1246 while( *Str == ' ' ) Str ++; // Eat whitespace
1247 space = strchr(Str, ','); // Find the end of the flag
1253 // Check for inversion/removal
1254 if( *Str == '!' || *Str == '-' ) {
1258 else if( *Str == '+' ) {
1262 // Check flag values
1263 for( i = 0; i < ciNumFlags; i ++ )
1265 if( strncmp(Str, cFLAGS[i].Name, len) == 0 ) {
1266 *Mask |= cFLAGS[i].Mask;
1267 *Value &= ~cFLAGS[i].Mask;
1269 *Value |= cFLAGS[i].Value;
1275 if( i == ciNumFlags ) {
1277 strncpy(val, Str, len+1);
1278 sendf(Client->Socket, "407 Unknown flag value '%s'\n", val);