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");
427 printf("Client %i: Authenticated as '%s' (%i)\n", Client->ID, Args, Client->UID);
429 sendf(Client->Socket, "200 Auth OK\n");
433 * \brief Set effective user
435 void Server_Cmd_SETEUSER(tClient *Client, char *Args)
439 space = strchr(Args, ' ');
441 if(space) *space = '\0';
443 if( !strlen(Args) ) {
444 sendf(Client->Socket, "407 SETEUSER expects an argument\n");
448 // Check user permissions
449 if( !(Bank_GetFlags(Client->UID) & (USER_FLAG_COKE|USER_FLAG_ADMIN)) ) {
450 sendf(Client->Socket, "403 Not in coke\n");
455 Client->EffectiveUID = Bank_GetAcctByName(Args);
456 if( Client->EffectiveUID == -1 ) {
457 sendf(Client->Socket, "404 User not found\n");
461 // You can't be an internal account
462 if( Bank_GetFlags(Client->EffectiveUID) & USER_FLAG_INTERNAL ) {
463 Client->EffectiveUID = -1;
464 sendf(Client->Socket, "404 User not found\n");
468 sendf(Client->Socket, "200 User set\n");
472 * \brief Enumerate the items that the server knows about
474 void Server_Cmd_ENUMITEMS(tClient *Client, char *Args)
478 if( Args != NULL && strlen(Args) ) {
479 sendf(Client->Socket, "407 ENUM_ITEMS takes no arguments\n");
483 sendf(Client->Socket, "201 Items %i\n", giNumItems);
485 for( i = 0; i < giNumItems; i ++ ) {
486 sendf(Client->Socket,
487 "202 Item %s:%i %i %s\n",
488 gaItems[i].Handler->Name, gaItems[i].ID, gaItems[i].Price, gaItems[i].Name
492 sendf(Client->Socket, "200 List end\n");
495 tItem *_GetItemFromString(char *String)
499 char *colon = strchr(String, ':');
511 for( i = 0; i < giNumHandlers; i ++ )
513 if( strcmp(gaHandlers[i]->Name, type) == 0) {
514 handler = gaHandlers[i];
523 for( i = 0; i < giNumItems; i ++ )
525 if( gaItems[i].Handler != handler ) continue;
526 if( gaItems[i].ID != num ) continue;
533 * \brief Fetch information on a specific item
535 void Server_Cmd_ITEMINFO(tClient *Client, char *Args)
537 tItem *item = _GetItemFromString(Args);
540 sendf(Client->Socket, "406 Bad Item ID\n");
544 sendf(Client->Socket,
545 "202 Item %s:%i %i %s\n",
546 item->Handler->Name, item->ID, item->Price, item->Name
550 void Server_Cmd_DISPENSE(tClient *Client, char *Args)
556 if( !Client->bIsAuthed ) {
557 sendf(Client->Socket, "401 Not Authenticated\n");
561 item = _GetItemFromString(Args);
563 sendf(Client->Socket, "406 Bad Item ID\n");
567 if( Client->EffectiveUID != -1 ) {
568 uid = Client->EffectiveUID;
574 switch( ret = DispenseItem( Client->UID, uid, item ) )
576 case 0: sendf(Client->Socket, "200 Dispense OK\n"); return ;
577 case 1: sendf(Client->Socket, "501 Unable to dispense\n"); return ;
578 case 2: sendf(Client->Socket, "402 Poor You\n"); return ;
580 sendf(Client->Socket, "500 Dispense Error\n");
585 void Server_Cmd_GIVE(tClient *Client, char *Args)
587 char *recipient, *ammount, *reason;
591 if( !Client->bIsAuthed ) {
592 sendf(Client->Socket, "401 Not Authenticated\n");
598 ammount = strchr(Args, ' ');
600 sendf(Client->Socket, "407 Invalid Argument, expected 3 parameters, 1 encountered\n");
606 reason = strchr(ammount, ' ');
608 sendf(Client->Socket, "407 Invalid Argument, expected 3 parameters, 2 encountered\n");
615 uid = Bank_GetAcctByName(recipient);
617 sendf(Client->Socket, "404 Invalid target user\n");
621 // You can't alter an internal account
622 if( Bank_GetFlags(uid) & USER_FLAG_INTERNAL ) {
623 sendf(Client->Socket, "404 Invalid target user\n");
628 iAmmount = atoi(ammount);
629 if( iAmmount <= 0 ) {
630 sendf(Client->Socket, "407 Invalid Argument, ammount must be > zero\n");
634 if( Client->EffectiveUID != -1 ) {
635 thisUid = Client->EffectiveUID;
638 thisUid = Client->UID;
642 switch( DispenseGive(Client->UID, thisUid, uid, iAmmount, reason) )
645 sendf(Client->Socket, "200 Give OK\n");
648 sendf(Client->Socket, "402 Poor You\n");
651 sendf(Client->Socket, "500 Unknown error\n");
656 void Server_Cmd_DONATE(tClient *Client, char *Args)
658 char *ammount, *reason;
662 if( !Client->bIsAuthed ) {
663 sendf(Client->Socket, "401 Not Authenticated\n");
669 // Get the start of the reason
670 reason = strchr(Args, ' ');
672 sendf(Client->Socket, "407 Invalid Argument, expected 2 parameters, 1 encountered\n");
678 // Check the end of the reason
679 if( strchr(reason, ' ') ) {
680 sendf(Client->Socket, "407 Invalid Argument, expected 2 parameters, more encountered\n");
685 iAmmount = atoi(ammount);
686 if( iAmmount <= 0 ) {
687 sendf(Client->Socket, "407 Invalid Argument, ammount must be > zero\n");
691 // Handle effective users
692 if( Client->EffectiveUID != -1 ) {
693 thisUid = Client->EffectiveUID;
696 thisUid = Client->UID;
700 switch( DispenseDonate(Client->UID, thisUid, iAmmount, reason) )
703 sendf(Client->Socket, "200 Give OK\n");
706 sendf(Client->Socket, "402 Poor You\n");
709 sendf(Client->Socket, "500 Unknown error\n");
714 void Server_Cmd_ADD(tClient *Client, char *Args)
716 char *user, *ammount, *reason;
719 if( !Client->bIsAuthed ) {
720 sendf(Client->Socket, "401 Not Authenticated\n");
726 ammount = strchr(Args, ' ');
728 sendf(Client->Socket, "407 Invalid Argument, expected 3 parameters, 1 encountered\n");
734 reason = strchr(ammount, ' ');
736 sendf(Client->Socket, "407 Invalid Argument, expected 3 parameters, 2 encountered\n");
742 // Check user permissions
743 if( !(Bank_GetFlags(Client->UID) & (USER_FLAG_COKE|USER_FLAG_ADMIN)) ) {
744 sendf(Client->Socket, "403 Not in coke\n");
749 uid = Bank_GetAcctByName(user);
751 sendf(Client->Socket, "404 Invalid user\n");
755 // You can't alter an internal account
756 if( Bank_GetFlags(uid) & USER_FLAG_INTERNAL ) {
757 sendf(Client->Socket, "404 Invalid user\n");
762 iAmmount = atoi(ammount);
763 if( iAmmount == 0 && ammount[0] != '0' ) {
764 sendf(Client->Socket, "407 Invalid Argument\n");
769 switch( DispenseAdd(Client->UID, uid, iAmmount, reason) )
772 sendf(Client->Socket, "200 Add OK\n");
775 sendf(Client->Socket, "402 Poor Guy\n");
778 sendf(Client->Socket, "500 Unknown error\n");
783 void Server_Cmd_SET(tClient *Client, char *Args)
785 char *user, *ammount, *reason;
788 if( !Client->bIsAuthed ) {
789 sendf(Client->Socket, "401 Not Authenticated\n");
795 ammount = strchr(Args, ' ');
797 sendf(Client->Socket, "407 Invalid Argument, expected 3 parameters, 1 encountered\n");
803 reason = strchr(ammount, ' ');
805 sendf(Client->Socket, "407 Invalid Argument, expected 3 parameters, 2 encountered\n");
811 // Check user permissions
812 if( !(Bank_GetFlags(Client->UID) & USER_FLAG_ADMIN) ) {
813 sendf(Client->Socket, "403 Not an admin\n");
818 uid = Bank_GetAcctByName(user);
820 sendf(Client->Socket, "404 Invalid user\n");
824 // You can't alter an internal account
825 if( Bank_GetFlags(uid) & USER_FLAG_INTERNAL ) {
826 sendf(Client->Socket, "404 Invalid user\n");
831 iAmmount = atoi(ammount);
832 if( iAmmount == 0 && ammount[0] != '0' ) {
833 sendf(Client->Socket, "407 Invalid Argument\n");
838 switch( DispenseSet(Client->UID, uid, iAmmount, reason) )
841 sendf(Client->Socket, "200 Add OK\n");
844 sendf(Client->Socket, "402 Poor Guy\n");
847 sendf(Client->Socket, "500 Unknown error\n");
852 void Server_Cmd_ENUMUSERS(tClient *Client, char *Args)
856 int maxBal = INT_MAX, minBal = INT_MIN;
857 int flagMask = 0, flagVal = 0;
858 int sort = BANK_ITFLAG_SORT_NAME;
859 time_t lastSeenAfter=0, lastSeenBefore=0;
861 int flags; // Iterator flags
862 int balValue; // Balance value for iterator
863 time_t timeValue; // Time value for iterator
866 if( Args && strlen(Args) )
868 char *space = Args, *type, *val;
873 space = strchr(space, ' ');
874 if(space) *space = '\0';
877 val = strchr(type, ':');
884 if( strcmp(type, "min_balance") == 0 ) {
888 else if( strcmp(type, "max_balance") == 0 ) {
892 else if( strcmp(type, "flags") == 0 ) {
893 if( Server_int_ParseFlags(Client, val, &flagMask, &flagVal) )
896 // - Last seen before timestamp
897 else if( strcmp(type, "last_seen_before") == 0 ) {
898 lastSeenAfter = atoll(val);
900 // - Last seen after timestamp
901 else if( strcmp(type, "last_seen_after") == 0 ) {
902 lastSeenAfter = atoll(val);
905 else if( strcmp(type, "sort") == 0 ) {
906 char *dash = strchr(val, '-');
911 if( strcmp(val, "name") == 0 ) {
912 sort = BANK_ITFLAG_SORT_NAME;
914 else if( strcmp(val, "balance") == 0 ) {
915 sort = BANK_ITFLAG_SORT_BAL;
917 else if( strcmp(val, "lastseen") == 0 ) {
918 sort = BANK_ITFLAG_SORT_LASTSEEN;
921 sendf(Client->Socket, "407 Unknown sort field ('%s')\n", val);
924 // Handle sort direction
926 if( strcmp(dash, "desc") == 0 ) {
927 sort |= BANK_ITFLAG_REVSORT;
930 sendf(Client->Socket, "407 Unknown sort direction '%s'\n", dash);
937 sendf(Client->Socket, "407 Unknown argument to ENUM_USERS '%s:%s'\n", type, val);
944 sendf(Client->Socket, "407 Unknown argument to ENUM_USERS '%s'\n", type);
950 *space = ' '; // Repair (to be nice)
952 while(*space == ' ') space ++;
958 if( maxBal != INT_MAX ) {
959 flags = sort|BANK_ITFLAG_MAXBALANCE;
962 else if( minBal != INT_MIN ) {
963 flags = sort|BANK_ITFLAG_MINBALANCE;
970 if( lastSeenBefore ) {
971 timeValue = lastSeenBefore;
972 flags |= BANK_ITFLAG_SEENBEFORE;
974 else if( lastSeenAfter ) {
975 timeValue = lastSeenAfter;
976 flags |= BANK_ITFLAG_SEENAFTER;
981 it = Bank_Iterator(flagMask, flagVal, flags, balValue, timeValue);
984 while( (i = Bank_IteratorNext(it)) != -1 )
986 int bal = Bank_GetBalance(i);
988 if( bal == INT_MIN ) continue;
990 if( bal < minBal ) continue;
991 if( bal > maxBal ) continue;
996 Bank_DelIterator(it);
999 sendf(Client->Socket, "201 Users %i\n", numRet);
1003 it = Bank_Iterator(flagMask, flagVal, flags, balValue, timeValue);
1005 while( (i = Bank_IteratorNext(it)) != -1 )
1007 int bal = Bank_GetBalance(i);
1009 if( bal == INT_MIN ) continue;
1011 if( bal < minBal ) continue;
1012 if( bal > maxBal ) continue;
1014 _SendUserInfo(Client, i);
1017 Bank_DelIterator(it);
1019 sendf(Client->Socket, "200 List End\n");
1022 void Server_Cmd_USERINFO(tClient *Client, char *Args)
1028 space = strchr(user, ' ');
1029 if(space) *space = '\0';
1032 uid = Bank_GetAcctByName(user);
1034 sendf(Client->Socket, "404 Invalid user");
1038 _SendUserInfo(Client, uid);
1041 void _SendUserInfo(tClient *Client, int UserID)
1043 char *type, *disabled="", *door="";
1044 int flags = Bank_GetFlags(UserID);
1046 if( flags & USER_FLAG_INTERNAL ) {
1049 else if( flags & USER_FLAG_COKE ) {
1050 if( flags & USER_FLAG_ADMIN )
1051 type = "coke,admin";
1055 else if( flags & USER_FLAG_ADMIN ) {
1062 if( flags & USER_FLAG_DISABLED )
1063 disabled = ",disabled";
1064 if( flags & USER_FLAG_DOORGROUP )
1067 // TODO: User flags/type
1069 Client->Socket, "202 User %s %i %s%s\n",
1070 Bank_GetAcctName(UserID), Bank_GetBalance(UserID),
1075 void Server_Cmd_USERADD(tClient *Client, char *Args)
1077 char *username, *space;
1079 // Check permissions
1080 if( !(Bank_GetFlags(Client->UID) & USER_FLAG_ADMIN) ) {
1081 sendf(Client->Socket, "403 Not a coke admin\n");
1087 while( *username == ' ' ) username ++;
1088 space = strchr(username, ' ');
1089 if(space) *space = '\0';
1091 // Try to create user
1092 if( Bank_CreateAcct(username) == -1 ) {
1093 sendf(Client->Socket, "404 User exists\n");
1098 char *thisName = Bank_GetAcctName(Client->UID);
1099 Log_Info("Account '%s' created by '%s'", username, thisName);
1103 sendf(Client->Socket, "200 User Added\n");
1106 void Server_Cmd_USERFLAGS(tClient *Client, char *Args)
1108 char *username, *flags;
1110 int mask=0, value=0;
1113 // Check permissions
1114 if( !(Bank_GetFlags(Client->UID) & USER_FLAG_ADMIN) ) {
1115 sendf(Client->Socket, "403 Not a coke admin\n");
1122 while( *username == ' ' ) username ++;
1123 space = strchr(username, ' ');
1125 sendf(Client->Socket, "407 USER_FLAGS requires 2 arguments, 1 given\n");
1131 while( *flags == ' ' ) flags ++;
1132 space = strchr(flags, ' ');
1133 if(space) *space = '\0';
1136 uid = Bank_GetAcctByName(username);
1138 sendf(Client->Socket, "404 User '%s' not found\n", username);
1143 if( Server_int_ParseFlags(Client, flags, &mask, &value) )
1147 Bank_SetFlags(uid, mask, value);
1150 sendf(Client->Socket, "200 User Updated\n");
1153 // --- INTERNAL HELPERS ---
1154 int sendf(int Socket, const char *Format, ...)
1159 va_start(args, Format);
1160 len = vsnprintf(NULL, 0, Format, args);
1165 va_start(args, Format);
1166 vsnprintf(buf, len+1, Format, args);
1169 #if DEBUG_TRACE_CLIENT
1170 printf("sendf: %s", buf);
1173 return send(Socket, buf, len, 0);
1177 int Server_int_ParseFlags(tClient *Client, const char *Str, int *Mask, int *Value)
1184 {"disabled", USER_FLAG_DISABLED, USER_FLAG_DISABLED}
1185 ,{"door", USER_FLAG_DOORGROUP, USER_FLAG_DOORGROUP}
1186 ,{"coke", USER_FLAG_COKE, USER_FLAG_COKE}
1187 ,{"admin", USER_FLAG_ADMIN, USER_FLAG_ADMIN}
1188 ,{"internal", USER_FLAG_INTERNAL, USER_FLAG_INTERNAL}
1190 const int ciNumFlags = sizeof(cFLAGS)/sizeof(cFLAGS[0]);
1202 while( *Str == ' ' ) Str ++; // Eat whitespace
1203 space = strchr(Str, ','); // Find the end of the flag
1209 // Check for inversion/removal
1210 if( *Str == '!' || *Str == '-' ) {
1214 else if( *Str == '+' ) {
1218 // Check flag values
1219 for( i = 0; i < ciNumFlags; i ++ )
1221 if( strncmp(Str, cFLAGS[i].Name, len) == 0 ) {
1222 *Mask |= cFLAGS[i].Mask;
1223 *Value &= ~cFLAGS[i].Mask;
1225 *Value |= cFLAGS[i].Value;
1231 if( i == ciNumFlags ) {
1233 strncpy(val, Str, len+1);
1234 sendf(Client->Socket, "407 Unknown flag value '%s'\n", val);