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
27 #define HASH_TYPE SHA1
28 #define HASH_LENGTH 20
30 #define MSG_STR_TOO_LONG "499 Command too long (limit "EXPSTR(INPUT_BUFFER_SIZE)")\n"
33 typedef struct sClient
35 int Socket; // Client socket ID
38 int bIsTrusted; // Is the connection from a trusted host/port
49 void Server_Start(void);
50 void Server_Cleanup(void);
51 void Server_HandleClient(int Socket, int bTrusted);
52 void Server_ParseClientCommand(tClient *Client, char *CommandString);
54 void Server_Cmd_USER(tClient *Client, char *Args);
55 void Server_Cmd_PASS(tClient *Client, char *Args);
56 void Server_Cmd_AUTOAUTH(tClient *Client, char *Args);
57 void Server_Cmd_SETEUSER(tClient *Client, char *Args);
58 void Server_Cmd_ENUMITEMS(tClient *Client, char *Args);
59 void Server_Cmd_ITEMINFO(tClient *Client, char *Args);
60 void Server_Cmd_DISPENSE(tClient *Client, char *Args);
61 void Server_Cmd_GIVE(tClient *Client, char *Args);
62 void Server_Cmd_DONATE(tClient *Client, char *Args);
63 void Server_Cmd_ADD(tClient *Client, char *Args);
64 void Server_Cmd_SET(tClient *Client, char *Args);
65 void Server_Cmd_ENUMUSERS(tClient *Client, char *Args);
66 void Server_Cmd_USERINFO(tClient *Client, char *Args);
67 void _SendUserInfo(tClient *Client, int UserID);
68 void Server_Cmd_USERADD(tClient *Client, char *Args);
69 void Server_Cmd_USERFLAGS(tClient *Client, char *Args);
71 int Server_int_ParseFlags(tClient *Client, const char *Str, int *Mask, int *Value);
72 int sendf(int Socket, const char *Format, ...);
76 const struct sClientCommand {
78 void (*Function)(tClient *Client, char *Arguments);
79 } gaServer_Commands[] = {
80 {"USER", Server_Cmd_USER},
81 {"PASS", Server_Cmd_PASS},
82 {"AUTOAUTH", Server_Cmd_AUTOAUTH},
83 {"SETEUSER", Server_Cmd_SETEUSER},
84 {"ENUM_ITEMS", Server_Cmd_ENUMITEMS},
85 {"ITEM_INFO", Server_Cmd_ITEMINFO},
86 {"DISPENSE", Server_Cmd_DISPENSE},
87 {"GIVE", Server_Cmd_GIVE},
88 {"DONATE", Server_Cmd_DONATE},
89 {"ADD", Server_Cmd_ADD},
90 {"SET", Server_Cmd_SET},
91 {"ENUM_USERS", Server_Cmd_ENUMUSERS},
92 {"USER_INFO", Server_Cmd_USERINFO},
93 {"USER_ADD", Server_Cmd_USERADD},
94 {"USER_FLAGS", Server_Cmd_USERFLAGS}
96 #define NUM_COMMANDS ((int)(sizeof(gaServer_Commands)/sizeof(gaServer_Commands[0])))
99 int giServer_Port = 1020;
100 int giServer_NextClientID = 1;
105 * \brief Open listenting socket and serve connections
107 void Server_Start(void)
110 struct sockaddr_in server_addr, client_addr;
112 atexit(Server_Cleanup);
115 giServer_Socket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
116 if( giServer_Socket < 0 ) {
117 fprintf(stderr, "ERROR: Unable to create server socket\n");
121 // Make listen address
122 memset(&server_addr, 0, sizeof(server_addr));
123 server_addr.sin_family = AF_INET; // Internet Socket
124 server_addr.sin_addr.s_addr = htonl(INADDR_ANY); // Listen on all interfaces
125 server_addr.sin_port = htons(giServer_Port); // Port
128 if( bind(giServer_Socket, (struct sockaddr *) &server_addr, sizeof(server_addr)) < 0 ) {
129 fprintf(stderr, "ERROR: Unable to bind to 0.0.0.0:%i\n", giServer_Port);
135 if( listen(giServer_Socket, MAX_CONNECTION_QUEUE) < 0 ) {
136 fprintf(stderr, "ERROR: Unable to listen to socket\n");
141 printf("Listening on 0.0.0.0:%i\n", giServer_Port);
145 uint len = sizeof(client_addr);
148 // Accept a connection
149 client_socket = accept(giServer_Socket, (struct sockaddr *) &client_addr, &len);
150 if(client_socket < 0) {
151 fprintf(stderr, "ERROR: Unable to accept client connection\n");
155 // Debug: Print the connection string
156 if(giDebugLevel >= 2) {
157 char ipstr[INET_ADDRSTRLEN];
158 inet_ntop(AF_INET, &client_addr.sin_addr, ipstr, INET_ADDRSTRLEN);
159 printf("Client connection from %s:%i\n",
160 ipstr, ntohs(client_addr.sin_port));
163 // Doesn't matter what, localhost is trusted
164 if( ntohl( client_addr.sin_addr.s_addr ) == 0x7F000001 )
167 // Trusted Connections
168 if( ntohs(client_addr.sin_port) < 1024 )
170 // TODO: Make this runtime configurable
171 switch( ntohl( client_addr.sin_addr.s_addr ) )
173 case 0x7F000001: // 127.0.0.1 localhost
174 //case 0x825E0D00: // 130.95.13.0
175 case 0x825E0D07: // 130.95.13.7 motsugo
176 case 0x825E0D12: // 130.95.13.18 mussel
177 case 0x825E0D17: // 130.95.13.23 martello
185 // TODO: Multithread this?
186 Server_HandleClient(client_socket, bTrusted);
188 close(client_socket);
192 void Server_Cleanup(void)
194 printf("Close(%i)\n", giServer_Socket);
195 close(giServer_Socket);
199 * \brief Reads from a client socket and parses the command strings
200 * \param Socket Client socket number/handle
201 * \param bTrusted Is the client trusted?
203 void Server_HandleClient(int Socket, int bTrusted)
205 char inbuf[INPUT_BUFFER_SIZE];
207 int remspace = INPUT_BUFFER_SIZE-1;
211 memset(&clientInfo, 0, sizeof(clientInfo));
213 // Initialise Client info
214 clientInfo.Socket = Socket;
215 clientInfo.ID = giServer_NextClientID ++;
216 clientInfo.bIsTrusted = bTrusted;
217 clientInfo.EffectiveUID = -1;
222 * - The `buf` and `remspace` variables allow a line to span several
223 * calls to recv(), if a line is not completed in one recv() call
224 * it is saved to the beginning of `inbuf` and `buf` is updated to
227 while( (bytes = recv(Socket, buf, remspace, 0)) > 0 )
230 buf[bytes] = '\0'; // Allow us to use stdlib string functions on it
234 while( (eol = strchr(start, '\n')) )
238 Server_ParseClientCommand(&clientInfo, start);
243 // Check if there was an incomplete line
244 if( *start != '\0' ) {
245 int tailBytes = bytes - (start-buf);
246 // Roll back in buffer
247 memcpy(inbuf, start, tailBytes);
248 remspace -= tailBytes;
250 send(Socket, MSG_STR_TOO_LONG, sizeof(MSG_STR_TOO_LONG), 0);
252 remspace = INPUT_BUFFER_SIZE - 1;
257 remspace = INPUT_BUFFER_SIZE - 1;
263 fprintf(stderr, "ERROR: Unable to recieve from client on socket %i\n", Socket);
267 if(giDebugLevel >= 2) {
268 printf("Client %i: Disconnected\n", clientInfo.ID);
273 * \brief Parses a client command and calls the required helper function
274 * \param Client Pointer to client state structure
275 * \param CommandString Command from client (single line of the command)
276 * \return Heap String to return to the client
278 void Server_ParseClientCommand(tClient *Client, char *CommandString)
287 // Split at first space
288 space = strchr(CommandString, ' ');
295 while( *space == ' ' ) space ++;
300 for( i = 0; args[i]; )
302 while( CommandString[i] != ' ' ) {
303 if( CommandString[i] == '"' ) {
304 while( !(CommandString[i] != '\\' CommandString[i+1] == '"' ) )
311 while( CommandString[i] == ' ' ) i ++;
318 for( i = 0; i < NUM_COMMANDS; i++ )
320 if(strcmp(CommandString, gaServer_Commands[i].Name) == 0) {
321 gaServer_Commands[i].Function(Client, args);
326 sendf(Client->Socket, "400 Unknown Command\n");
333 * \brief Set client username
335 * Usage: USER <username>
337 void Server_Cmd_USER(tClient *Client, char *Args)
339 char *space = strchr(Args, ' ');
340 if(space) *space = '\0'; // Remove characters after the ' '
344 printf("Client %i authenticating as '%s'\n", Client->ID, Args);
348 free(Client->Username);
349 Client->Username = strdup(Args);
352 // Create a salt (that changes if the username is changed)
353 // Yes, I know, I'm a little paranoid, but who isn't?
354 Client->Salt[0] = 0x21 + (rand()&0x3F);
355 Client->Salt[1] = 0x21 + (rand()&0x3F);
356 Client->Salt[2] = 0x21 + (rand()&0x3F);
357 Client->Salt[3] = 0x21 + (rand()&0x3F);
358 Client->Salt[4] = 0x21 + (rand()&0x3F);
359 Client->Salt[5] = 0x21 + (rand()&0x3F);
360 Client->Salt[6] = 0x21 + (rand()&0x3F);
361 Client->Salt[7] = 0x21 + (rand()&0x3F);
363 // TODO: Also send hash type to use, (SHA1 or crypt according to [DAA])
364 sendf(Client->Socket, "100 SALT %s\n", Client->Salt);
366 sendf(Client->Socket, "100 User Set\n");
371 * \brief Authenticate as a user
375 void Server_Cmd_PASS(tClient *Client, char *Args)
377 char *space = strchr(Args, ' ');
378 if(space) *space = '\0'; // Remove characters after the ' '
380 // Pass on to cokebank
381 Client->UID = Bank_GetUserAuth(Client->Salt, Client->Username, Args);
383 if( Client->UID != -1 ) {
384 Client->bIsAuthed = 1;
385 sendf(Client->Socket, "200 Auth OK\n");
389 sendf(Client->Socket, "401 Auth Failure\n");
393 * \brief Authenticate as a user without a password
395 * Usage: AUTOAUTH <user>
397 void Server_Cmd_AUTOAUTH(tClient *Client, char *Args)
399 char *space = strchr(Args, ' ');
400 if(space) *space = '\0'; // Remove characters after the ' '
403 if( !Client->bIsTrusted ) {
405 printf("Client %i: Untrusted client attempting to AUTOAUTH\n", Client->ID);
406 sendf(Client->Socket, "401 Untrusted\n");
411 Client->UID = Bank_GetAcctByName( Args );
412 if( Client->UID < 0 ) {
414 printf("Client %i: Unknown user '%s'\n", Client->ID, Args);
415 sendf(Client->Socket, "401 Auth Failure\n");
419 // You can't be an internal account
420 if( Bank_GetFlags(Client->UID) & USER_FLAG_INTERNAL ) {
422 sendf(Client->Socket, "401 Auth Failure\n");
426 Client->bIsAuthed = 1;
429 printf("Client %i: Auto authenticated as '%s' (%i)\n", Client->ID, Args, Client->UID);
431 sendf(Client->Socket, "200 Auth OK\n");
435 * \brief Set effective user
437 void Server_Cmd_SETEUSER(tClient *Client, char *Args)
441 space = strchr(Args, ' ');
443 if(space) *space = '\0';
445 if( !strlen(Args) ) {
446 sendf(Client->Socket, "407 SETEUSER expects an argument\n");
450 // Check user permissions
451 if( !(Bank_GetFlags(Client->UID) & (USER_FLAG_COKE|USER_FLAG_ADMIN)) ) {
452 sendf(Client->Socket, "403 Not in coke\n");
457 Client->EffectiveUID = Bank_GetAcctByName(Args);
458 if( Client->EffectiveUID == -1 ) {
459 sendf(Client->Socket, "404 User not found\n");
463 // You can't be an internal account
464 if( Bank_GetFlags(Client->EffectiveUID) & USER_FLAG_INTERNAL ) {
465 Client->EffectiveUID = -1;
466 sendf(Client->Socket, "404 User not found\n");
470 sendf(Client->Socket, "200 User set\n");
474 * \brief Enumerate the items that the server knows about
476 void Server_Cmd_ENUMITEMS(tClient *Client, char *Args)
480 if( Args != NULL && strlen(Args) ) {
481 sendf(Client->Socket, "407 ENUM_ITEMS takes no arguments\n");
485 sendf(Client->Socket, "201 Items %i\n", giNumItems);
487 for( i = 0; i < giNumItems; i ++ ) {
488 sendf(Client->Socket,
489 "202 Item %s:%i %i %s\n",
490 gaItems[i].Handler->Name, gaItems[i].ID, gaItems[i].Price, gaItems[i].Name
494 sendf(Client->Socket, "200 List end\n");
497 tItem *_GetItemFromString(char *String)
501 char *colon = strchr(String, ':');
513 for( i = 0; i < giNumHandlers; i ++ )
515 if( strcmp(gaHandlers[i]->Name, type) == 0) {
516 handler = gaHandlers[i];
525 for( i = 0; i < giNumItems; i ++ )
527 if( gaItems[i].Handler != handler ) continue;
528 if( gaItems[i].ID != num ) continue;
535 * \brief Fetch information on a specific item
537 void Server_Cmd_ITEMINFO(tClient *Client, char *Args)
539 tItem *item = _GetItemFromString(Args);
542 sendf(Client->Socket, "406 Bad Item ID\n");
546 sendf(Client->Socket,
547 "202 Item %s:%i %i %s\n",
548 item->Handler->Name, item->ID, item->Price, item->Name
552 void Server_Cmd_DISPENSE(tClient *Client, char *Args)
558 if( !Client->bIsAuthed ) {
559 sendf(Client->Socket, "401 Not Authenticated\n");
563 item = _GetItemFromString(Args);
565 sendf(Client->Socket, "406 Bad Item ID\n");
569 if( Client->EffectiveUID != -1 ) {
570 uid = Client->EffectiveUID;
576 switch( ret = DispenseItem( Client->UID, uid, item ) )
578 case 0: sendf(Client->Socket, "200 Dispense OK\n"); return ;
579 case 1: sendf(Client->Socket, "501 Unable to dispense\n"); return ;
580 case 2: sendf(Client->Socket, "402 Poor You\n"); return ;
582 sendf(Client->Socket, "500 Dispense Error\n");
587 void Server_Cmd_GIVE(tClient *Client, char *Args)
589 char *recipient, *ammount, *reason;
593 if( !Client->bIsAuthed ) {
594 sendf(Client->Socket, "401 Not Authenticated\n");
600 ammount = strchr(Args, ' ');
602 sendf(Client->Socket, "407 Invalid Argument, expected 3 parameters, 1 encountered\n");
608 reason = strchr(ammount, ' ');
610 sendf(Client->Socket, "407 Invalid Argument, expected 3 parameters, 2 encountered\n");
617 uid = Bank_GetAcctByName(recipient);
619 sendf(Client->Socket, "404 Invalid target user\n");
623 // You can't alter an internal account
624 if( Bank_GetFlags(uid) & USER_FLAG_INTERNAL ) {
625 sendf(Client->Socket, "404 Invalid target user\n");
630 iAmmount = atoi(ammount);
631 if( iAmmount <= 0 ) {
632 sendf(Client->Socket, "407 Invalid Argument, ammount must be > zero\n");
636 if( Client->EffectiveUID != -1 ) {
637 thisUid = Client->EffectiveUID;
640 thisUid = Client->UID;
644 switch( DispenseGive(Client->UID, thisUid, uid, iAmmount, reason) )
647 sendf(Client->Socket, "200 Give OK\n");
650 sendf(Client->Socket, "402 Poor You\n");
653 sendf(Client->Socket, "500 Unknown error\n");
658 void Server_Cmd_DONATE(tClient *Client, char *Args)
660 char *ammount, *reason;
664 if( !Client->bIsAuthed ) {
665 sendf(Client->Socket, "401 Not Authenticated\n");
671 // Get the start of the reason
672 reason = strchr(Args, ' ');
674 sendf(Client->Socket, "407 Invalid Argument, expected 2 parameters, 1 encountered\n");
680 // Check the end of the reason
681 if( strchr(reason, ' ') ) {
682 sendf(Client->Socket, "407 Invalid Argument, expected 2 parameters, more encountered\n");
687 iAmmount = atoi(ammount);
688 if( iAmmount <= 0 ) {
689 sendf(Client->Socket, "407 Invalid Argument, ammount must be > zero\n");
693 // Handle effective users
694 if( Client->EffectiveUID != -1 ) {
695 thisUid = Client->EffectiveUID;
698 thisUid = Client->UID;
702 switch( DispenseDonate(Client->UID, thisUid, iAmmount, reason) )
705 sendf(Client->Socket, "200 Give OK\n");
708 sendf(Client->Socket, "402 Poor You\n");
711 sendf(Client->Socket, "500 Unknown error\n");
716 void Server_Cmd_ADD(tClient *Client, char *Args)
718 char *user, *ammount, *reason;
721 if( !Client->bIsAuthed ) {
722 sendf(Client->Socket, "401 Not Authenticated\n");
728 ammount = strchr(Args, ' ');
730 sendf(Client->Socket, "407 Invalid Argument, expected 3 parameters, 1 encountered\n");
736 reason = strchr(ammount, ' ');
738 sendf(Client->Socket, "407 Invalid Argument, expected 3 parameters, 2 encountered\n");
744 // Check user permissions
745 if( !(Bank_GetFlags(Client->UID) & (USER_FLAG_COKE|USER_FLAG_ADMIN)) ) {
746 sendf(Client->Socket, "403 Not in coke\n");
751 uid = Bank_GetAcctByName(user);
753 sendf(Client->Socket, "404 Invalid user\n");
757 // You can't alter an internal account
758 if( Bank_GetFlags(uid) & USER_FLAG_INTERNAL ) {
759 sendf(Client->Socket, "404 Invalid user\n");
764 iAmmount = atoi(ammount);
765 if( iAmmount == 0 && ammount[0] != '0' ) {
766 sendf(Client->Socket, "407 Invalid Argument\n");
771 switch( DispenseAdd(Client->UID, uid, iAmmount, reason) )
774 sendf(Client->Socket, "200 Add OK\n");
777 sendf(Client->Socket, "402 Poor Guy\n");
780 sendf(Client->Socket, "500 Unknown error\n");
785 void Server_Cmd_SET(tClient *Client, char *Args)
787 char *user, *ammount, *reason;
790 if( !Client->bIsAuthed ) {
791 sendf(Client->Socket, "401 Not Authenticated\n");
797 ammount = strchr(Args, ' ');
799 sendf(Client->Socket, "407 Invalid Argument, expected 3 parameters, 1 encountered\n");
805 reason = strchr(ammount, ' ');
807 sendf(Client->Socket, "407 Invalid Argument, expected 3 parameters, 2 encountered\n");
813 // Check user permissions
814 if( !(Bank_GetFlags(Client->UID) & USER_FLAG_ADMIN) ) {
815 sendf(Client->Socket, "403 Not an admin\n");
820 uid = Bank_GetAcctByName(user);
822 sendf(Client->Socket, "404 Invalid user\n");
826 // You can't alter an internal account
827 if( Bank_GetFlags(uid) & USER_FLAG_INTERNAL ) {
828 sendf(Client->Socket, "404 Invalid user\n");
833 iAmmount = atoi(ammount);
834 if( iAmmount == 0 && ammount[0] != '0' ) {
835 sendf(Client->Socket, "407 Invalid Argument\n");
840 switch( DispenseSet(Client->UID, uid, iAmmount, reason) )
843 sendf(Client->Socket, "200 Add OK\n");
846 sendf(Client->Socket, "402 Poor Guy\n");
849 sendf(Client->Socket, "500 Unknown error\n");
854 void Server_Cmd_ENUMUSERS(tClient *Client, char *Args)
858 int maxBal = INT_MAX, minBal = INT_MIN;
859 int flagMask = 0, flagVal = 0;
860 int sort = BANK_ITFLAG_SORT_NAME;
861 time_t lastSeenAfter=0, lastSeenBefore=0;
863 int flags; // Iterator flags
864 int balValue; // Balance value for iterator
865 time_t timeValue; // Time value for iterator
868 if( Args && strlen(Args) )
870 char *space = Args, *type, *val;
875 space = strchr(space, ' ');
876 if(space) *space = '\0';
879 val = strchr(type, ':');
886 if( strcmp(type, "min_balance") == 0 ) {
890 else if( strcmp(type, "max_balance") == 0 ) {
894 else if( strcmp(type, "flags") == 0 ) {
895 if( Server_int_ParseFlags(Client, val, &flagMask, &flagVal) )
898 // - Last seen before timestamp
899 else if( strcmp(type, "last_seen_before") == 0 ) {
900 lastSeenAfter = atoll(val);
902 // - Last seen after timestamp
903 else if( strcmp(type, "last_seen_after") == 0 ) {
904 lastSeenAfter = atoll(val);
907 else if( strcmp(type, "sort") == 0 ) {
908 char *dash = strchr(val, '-');
913 if( strcmp(val, "name") == 0 ) {
914 sort = BANK_ITFLAG_SORT_NAME;
916 else if( strcmp(val, "balance") == 0 ) {
917 sort = BANK_ITFLAG_SORT_BAL;
919 else if( strcmp(val, "lastseen") == 0 ) {
920 sort = BANK_ITFLAG_SORT_LASTSEEN;
923 sendf(Client->Socket, "407 Unknown sort field ('%s')\n", val);
926 // Handle sort direction
928 if( strcmp(dash, "desc") == 0 ) {
929 sort |= BANK_ITFLAG_REVSORT;
932 sendf(Client->Socket, "407 Unknown sort direction '%s'\n", dash);
939 sendf(Client->Socket, "407 Unknown argument to ENUM_USERS '%s:%s'\n", type, val);
946 sendf(Client->Socket, "407 Unknown argument to ENUM_USERS '%s'\n", type);
952 *space = ' '; // Repair (to be nice)
954 while(*space == ' ') space ++;
960 if( maxBal != INT_MAX ) {
961 flags = sort|BANK_ITFLAG_MAXBALANCE;
964 else if( minBal != INT_MIN ) {
965 flags = sort|BANK_ITFLAG_MINBALANCE;
972 if( lastSeenBefore ) {
973 timeValue = lastSeenBefore;
974 flags |= BANK_ITFLAG_SEENBEFORE;
976 else if( lastSeenAfter ) {
977 timeValue = lastSeenAfter;
978 flags |= BANK_ITFLAG_SEENAFTER;
983 it = Bank_Iterator(flagMask, flagVal, flags, balValue, timeValue);
986 while( (i = Bank_IteratorNext(it)) != -1 )
988 int bal = Bank_GetBalance(i);
990 if( bal == INT_MIN ) continue;
992 if( bal < minBal ) continue;
993 if( bal > maxBal ) continue;
998 Bank_DelIterator(it);
1001 sendf(Client->Socket, "201 Users %i\n", numRet);
1005 it = Bank_Iterator(flagMask, flagVal, flags, balValue, timeValue);
1007 while( (i = Bank_IteratorNext(it)) != -1 )
1009 int bal = Bank_GetBalance(i);
1011 if( bal == INT_MIN ) continue;
1013 if( bal < minBal ) continue;
1014 if( bal > maxBal ) continue;
1016 _SendUserInfo(Client, i);
1019 Bank_DelIterator(it);
1021 sendf(Client->Socket, "200 List End\n");
1024 void Server_Cmd_USERINFO(tClient *Client, char *Args)
1030 space = strchr(user, ' ');
1031 if(space) *space = '\0';
1034 uid = Bank_GetAcctByName(user);
1036 sendf(Client->Socket, "404 Invalid user");
1040 _SendUserInfo(Client, uid);
1043 void _SendUserInfo(tClient *Client, int UserID)
1045 char *type, *disabled="", *door="";
1046 int flags = Bank_GetFlags(UserID);
1048 if( flags & USER_FLAG_INTERNAL ) {
1051 else if( flags & USER_FLAG_COKE ) {
1052 if( flags & USER_FLAG_ADMIN )
1053 type = "coke,admin";
1057 else if( flags & USER_FLAG_ADMIN ) {
1064 if( flags & USER_FLAG_DISABLED )
1065 disabled = ",disabled";
1066 if( flags & USER_FLAG_DOORGROUP )
1069 // TODO: User flags/type
1071 Client->Socket, "202 User %s %i %s%s\n",
1072 Bank_GetAcctName(UserID), Bank_GetBalance(UserID),
1077 void Server_Cmd_USERADD(tClient *Client, char *Args)
1079 char *username, *space;
1081 // Check permissions
1082 if( !(Bank_GetFlags(Client->UID) & USER_FLAG_ADMIN) ) {
1083 sendf(Client->Socket, "403 Not a coke admin\n");
1089 while( *username == ' ' ) username ++;
1090 space = strchr(username, ' ');
1091 if(space) *space = '\0';
1093 // Try to create user
1094 if( Bank_CreateAcct(username) == -1 ) {
1095 sendf(Client->Socket, "404 User exists\n");
1100 char *thisName = Bank_GetAcctName(Client->UID);
1101 Log_Info("Account '%s' created by '%s'", username, thisName);
1105 sendf(Client->Socket, "200 User Added\n");
1108 void Server_Cmd_USERFLAGS(tClient *Client, char *Args)
1110 char *username, *flags;
1112 int mask=0, value=0;
1115 // Check permissions
1116 if( !(Bank_GetFlags(Client->UID) & USER_FLAG_ADMIN) ) {
1117 sendf(Client->Socket, "403 Not a coke admin\n");
1124 while( *username == ' ' ) username ++;
1125 space = strchr(username, ' ');
1127 sendf(Client->Socket, "407 USER_FLAGS requires 2 arguments, 1 given\n");
1133 while( *flags == ' ' ) flags ++;
1134 space = strchr(flags, ' ');
1135 if(space) *space = '\0';
1138 uid = Bank_GetAcctByName(username);
1140 sendf(Client->Socket, "404 User '%s' not found\n", username);
1145 if( Server_int_ParseFlags(Client, flags, &mask, &value) )
1149 Bank_SetFlags(uid, mask, value);
1152 sendf(Client->Socket, "200 User Updated\n");
1155 // --- INTERNAL HELPERS ---
1156 int sendf(int Socket, const char *Format, ...)
1161 va_start(args, Format);
1162 len = vsnprintf(NULL, 0, Format, args);
1167 va_start(args, Format);
1168 vsnprintf(buf, len+1, Format, args);
1171 #if DEBUG_TRACE_CLIENT
1172 printf("sendf: %s", buf);
1175 return send(Socket, buf, len, 0);
1179 int Server_int_ParseFlags(tClient *Client, const char *Str, int *Mask, int *Value)
1186 {"disabled", USER_FLAG_DISABLED, USER_FLAG_DISABLED}
1187 ,{"door", USER_FLAG_DOORGROUP, USER_FLAG_DOORGROUP}
1188 ,{"coke", USER_FLAG_COKE, USER_FLAG_COKE}
1189 ,{"admin", USER_FLAG_ADMIN, USER_FLAG_ADMIN}
1190 ,{"internal", USER_FLAG_INTERNAL, USER_FLAG_INTERNAL}
1192 const int ciNumFlags = sizeof(cFLAGS)/sizeof(cFLAGS[0]);
1204 while( *Str == ' ' ) Str ++; // Eat whitespace
1205 space = strchr(Str, ','); // Find the end of the flag
1211 // Check for inversion/removal
1212 if( *Str == '!' || *Str == '-' ) {
1216 else if( *Str == '+' ) {
1220 // Check flag values
1221 for( i = 0; i < ciNumFlags; i ++ )
1223 if( strncmp(Str, cFLAGS[i].Name, len) == 0 ) {
1224 *Mask |= cFLAGS[i].Mask;
1225 *Value &= ~cFLAGS[i].Mask;
1227 *Value |= cFLAGS[i].Value;
1233 if( i == ciNumFlags ) {
1235 strncpy(val, Str, len+1);
1236 sendf(Client->Socket, "407 Unknown flag value '%s'\n", val);