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 client_socket = accept(giServer_Socket, (struct sockaddr *) &client_addr, &len);
149 if(client_socket < 0) {
150 fprintf(stderr, "ERROR: Unable to accept client connection\n");
154 if(giDebugLevel >= 2) {
155 char ipstr[INET_ADDRSTRLEN];
156 inet_ntop(AF_INET, &client_addr.sin_addr, ipstr, INET_ADDRSTRLEN);
157 printf("Client connection from %s:%i\n",
158 ipstr, ntohs(client_addr.sin_port));
161 // Trusted Connections
162 if( ntohs(client_addr.sin_port) < 1024 )
164 // TODO: Make this runtime configurable
165 switch( ntohl( client_addr.sin_addr.s_addr ) )
167 case 0x7F000001: // 127.0.0.1 localhost
168 //case 0x825E0D00: // 130.95.13.0
169 case 0x825E0D12: // 130.95.13.18 mussel
170 case 0x825E0D17: // 130.95.13.23 martello
178 // TODO: Multithread this?
179 Server_HandleClient(client_socket, bTrusted);
181 close(client_socket);
185 void Server_Cleanup(void)
187 printf("Close(%i)\n", giServer_Socket);
188 close(giServer_Socket);
192 * \brief Reads from a client socket and parses the command strings
193 * \param Socket Client socket number/handle
194 * \param bTrusted Is the client trusted?
196 void Server_HandleClient(int Socket, int bTrusted)
198 char inbuf[INPUT_BUFFER_SIZE];
200 int remspace = INPUT_BUFFER_SIZE-1;
204 memset(&clientInfo, 0, sizeof(clientInfo));
206 // Initialise Client info
207 clientInfo.Socket = Socket;
208 clientInfo.ID = giServer_NextClientID ++;
209 clientInfo.bIsTrusted = bTrusted;
210 clientInfo.EffectiveUID = -1;
215 * - The `buf` and `remspace` variables allow a line to span several
216 * calls to recv(), if a line is not completed in one recv() call
217 * it is saved to the beginning of `inbuf` and `buf` is updated to
220 while( (bytes = recv(Socket, buf, remspace, 0)) > 0 )
223 buf[bytes] = '\0'; // Allow us to use stdlib string functions on it
227 while( (eol = strchr(start, '\n')) )
231 Server_ParseClientCommand(&clientInfo, start);
236 // Check if there was an incomplete line
237 if( *start != '\0' ) {
238 int tailBytes = bytes - (start-buf);
239 // Roll back in buffer
240 memcpy(inbuf, start, tailBytes);
241 remspace -= tailBytes;
243 send(Socket, MSG_STR_TOO_LONG, sizeof(MSG_STR_TOO_LONG), 0);
245 remspace = INPUT_BUFFER_SIZE - 1;
250 remspace = INPUT_BUFFER_SIZE - 1;
256 fprintf(stderr, "ERROR: Unable to recieve from client on socket %i\n", Socket);
260 if(giDebugLevel >= 2) {
261 printf("Client %i: Disconnected\n", clientInfo.ID);
266 * \brief Parses a client command and calls the required helper function
267 * \param Client Pointer to client state structure
268 * \param CommandString Command from client (single line of the command)
269 * \return Heap String to return to the client
271 void Server_ParseClientCommand(tClient *Client, char *CommandString)
280 // Split at first space
281 space = strchr(CommandString, ' ');
288 while( *space == ' ' ) space ++;
293 for( i = 0; args[i]; )
295 while( CommandString[i] != ' ' ) {
296 if( CommandString[i] == '"' ) {
297 while( !(CommandString[i] != '\\' CommandString[i+1] == '"' ) )
304 while( CommandString[i] == ' ' ) i ++;
311 for( i = 0; i < NUM_COMMANDS; i++ )
313 if(strcmp(CommandString, gaServer_Commands[i].Name) == 0) {
314 gaServer_Commands[i].Function(Client, args);
319 sendf(Client->Socket, "400 Unknown Command\n");
326 * \brief Set client username
328 * Usage: USER <username>
330 void Server_Cmd_USER(tClient *Client, char *Args)
332 char *space = strchr(Args, ' ');
333 if(space) *space = '\0'; // Remove characters after the ' '
337 printf("Client %i authenticating as '%s'\n", Client->ID, Args);
341 free(Client->Username);
342 Client->Username = strdup(Args);
345 // Create a salt (that changes if the username is changed)
346 // Yes, I know, I'm a little paranoid, but who isn't?
347 Client->Salt[0] = 0x21 + (rand()&0x3F);
348 Client->Salt[1] = 0x21 + (rand()&0x3F);
349 Client->Salt[2] = 0x21 + (rand()&0x3F);
350 Client->Salt[3] = 0x21 + (rand()&0x3F);
351 Client->Salt[4] = 0x21 + (rand()&0x3F);
352 Client->Salt[5] = 0x21 + (rand()&0x3F);
353 Client->Salt[6] = 0x21 + (rand()&0x3F);
354 Client->Salt[7] = 0x21 + (rand()&0x3F);
356 // TODO: Also send hash type to use, (SHA1 or crypt according to [DAA])
357 sendf(Client->Socket, "100 SALT %s\n", Client->Salt);
359 sendf(Client->Socket, "100 User Set\n");
364 * \brief Authenticate as a user
368 void Server_Cmd_PASS(tClient *Client, char *Args)
370 char *space = strchr(Args, ' ');
371 if(space) *space = '\0'; // Remove characters after the ' '
373 // Pass on to cokebank
374 Client->UID = Bank_GetUserAuth(Client->Salt, Client->Username, Args);
376 if( Client->UID != -1 ) {
377 Client->bIsAuthed = 1;
378 sendf(Client->Socket, "200 Auth OK\n");
382 sendf(Client->Socket, "401 Auth Failure\n");
386 * \brief Authenticate as a user without a password
388 * Usage: AUTOAUTH <user>
390 void Server_Cmd_AUTOAUTH(tClient *Client, char *Args)
392 char *space = strchr(Args, ' ');
393 if(space) *space = '\0'; // Remove characters after the ' '
396 if( !Client->bIsTrusted ) {
398 printf("Client %i: Untrusted client attempting to AUTOAUTH\n", Client->ID);
399 sendf(Client->Socket, "401 Untrusted\n");
404 Client->UID = Bank_GetAcctByName( Args );
405 if( Client->UID < 0 ) {
407 printf("Client %i: Unknown user '%s'\n", Client->ID, Args);
408 sendf(Client->Socket, "401 Auth Failure\n");
412 // You can't be an internal account
413 if( Bank_GetFlags(Client->UID) & USER_FLAG_INTERNAL ) {
415 sendf(Client->Socket, "401 Auth Failure\n");
420 printf("Client %i: Authenticated as '%s' (%i)\n", Client->ID, Args, Client->UID);
422 sendf(Client->Socket, "200 Auth OK\n");
426 * \brief Set effective user
428 void Server_Cmd_SETEUSER(tClient *Client, char *Args)
432 space = strchr(Args, ' ');
434 if(space) *space = '\0';
436 if( !strlen(Args) ) {
437 sendf(Client->Socket, "407 SETEUSER expects an argument\n");
441 // Check user permissions
442 if( !(Bank_GetFlags(Client->UID) & USER_FLAG_COKE) ) {
443 sendf(Client->Socket, "403 Not in coke\n");
448 Client->EffectiveUID = Bank_GetAcctByName(Args);
449 if( Client->EffectiveUID == -1 ) {
450 sendf(Client->Socket, "404 User not found\n");
454 // You can't be an internal account
455 if( Bank_GetFlags(Client->EffectiveUID) & USER_FLAG_INTERNAL ) {
456 Client->EffectiveUID = -1;
457 sendf(Client->Socket, "404 User not found\n");
461 sendf(Client->Socket, "200 User set\n");
465 * \brief Enumerate the items that the server knows about
467 void Server_Cmd_ENUMITEMS(tClient *Client, char *Args)
471 if( Args != NULL && strlen(Args) ) {
472 sendf(Client->Socket, "407 ENUM_ITEMS takes no arguments\n");
476 sendf(Client->Socket, "201 Items %i\n", giNumItems);
478 for( i = 0; i < giNumItems; i ++ ) {
479 sendf(Client->Socket,
480 "202 Item %s:%i %i %s\n",
481 gaItems[i].Handler->Name, gaItems[i].ID, gaItems[i].Price, gaItems[i].Name
485 sendf(Client->Socket, "200 List end\n");
488 tItem *_GetItemFromString(char *String)
492 char *colon = strchr(String, ':');
504 for( i = 0; i < giNumHandlers; i ++ )
506 if( strcmp(gaHandlers[i]->Name, type) == 0) {
507 handler = gaHandlers[i];
516 for( i = 0; i < giNumItems; i ++ )
518 if( gaItems[i].Handler != handler ) continue;
519 if( gaItems[i].ID != num ) continue;
526 * \brief Fetch information on a specific item
528 void Server_Cmd_ITEMINFO(tClient *Client, char *Args)
530 tItem *item = _GetItemFromString(Args);
533 sendf(Client->Socket, "406 Bad Item ID\n");
537 sendf(Client->Socket,
538 "202 Item %s:%i %i %s\n",
539 item->Handler->Name, item->ID, item->Price, item->Name
543 void Server_Cmd_DISPENSE(tClient *Client, char *Args)
549 if( !Client->bIsAuthed ) {
550 sendf(Client->Socket, "401 Not Authenticated\n");
554 item = _GetItemFromString(Args);
556 sendf(Client->Socket, "406 Bad Item ID\n");
560 if( Client->EffectiveUID != -1 ) {
561 uid = Client->EffectiveUID;
567 switch( ret = DispenseItem( Client->UID, uid, item ) )
569 case 0: sendf(Client->Socket, "200 Dispense OK\n"); return ;
570 case 1: sendf(Client->Socket, "501 Unable to dispense\n"); return ;
571 case 2: sendf(Client->Socket, "402 Poor You\n"); return ;
573 sendf(Client->Socket, "500 Dispense Error\n");
578 void Server_Cmd_GIVE(tClient *Client, char *Args)
580 char *recipient, *ammount, *reason;
584 if( !Client->bIsAuthed ) {
585 sendf(Client->Socket, "401 Not Authenticated\n");
591 ammount = strchr(Args, ' ');
593 sendf(Client->Socket, "407 Invalid Argument, expected 3 parameters, 1 encountered\n");
599 reason = strchr(ammount, ' ');
601 sendf(Client->Socket, "407 Invalid Argument, expected 3 parameters, 2 encountered\n");
608 uid = Bank_GetAcctByName(recipient);
610 sendf(Client->Socket, "404 Invalid target user\n");
614 // You can't alter an internal account
615 if( Bank_GetFlags(uid) & USER_FLAG_INTERNAL ) {
616 sendf(Client->Socket, "404 Invalid target user\n");
621 iAmmount = atoi(ammount);
622 if( iAmmount <= 0 ) {
623 sendf(Client->Socket, "407 Invalid Argument, ammount must be > zero\n");
627 if( Client->EffectiveUID != -1 ) {
628 thisUid = Client->EffectiveUID;
631 thisUid = Client->UID;
635 switch( DispenseGive(Client->UID, thisUid, uid, iAmmount, reason) )
638 sendf(Client->Socket, "200 Give OK\n");
641 sendf(Client->Socket, "402 Poor You\n");
644 sendf(Client->Socket, "500 Unknown error\n");
649 void Server_Cmd_DONATE(tClient *Client, char *Args)
651 char *ammount, *reason;
655 if( !Client->bIsAuthed ) {
656 sendf(Client->Socket, "401 Not Authenticated\n");
662 // Get the start of the reason
663 reason = strchr(Args, ' ');
665 sendf(Client->Socket, "407 Invalid Argument, expected 2 parameters, 1 encountered\n");
671 // Check the end of the reason
672 if( strchr(reason, ' ') ) {
673 sendf(Client->Socket, "407 Invalid Argument, expected 2 parameters, more encountered\n");
678 iAmmount = atoi(ammount);
679 if( iAmmount <= 0 ) {
680 sendf(Client->Socket, "407 Invalid Argument, ammount must be > zero\n");
684 // Handle effective users
685 if( Client->EffectiveUID != -1 ) {
686 thisUid = Client->EffectiveUID;
689 thisUid = Client->UID;
693 switch( DispenseDonate(Client->UID, thisUid, iAmmount, reason) )
696 sendf(Client->Socket, "200 Give OK\n");
699 sendf(Client->Socket, "402 Poor You\n");
702 sendf(Client->Socket, "500 Unknown error\n");
707 void Server_Cmd_ADD(tClient *Client, char *Args)
709 char *user, *ammount, *reason;
712 if( !Client->bIsAuthed ) {
713 sendf(Client->Socket, "401 Not Authenticated\n");
719 ammount = strchr(Args, ' ');
721 sendf(Client->Socket, "407 Invalid Argument, expected 3 parameters, 1 encountered\n");
727 reason = strchr(ammount, ' ');
729 sendf(Client->Socket, "407 Invalid Argument, expected 3 parameters, 2 encountered\n");
735 // Check user permissions
736 if( !(Bank_GetFlags(Client->UID) & USER_FLAG_COKE) ) {
737 sendf(Client->Socket, "403 Not in coke\n");
742 uid = Bank_GetAcctByName(user);
744 sendf(Client->Socket, "404 Invalid user\n");
748 // You can't alter an internal account
749 if( Bank_GetFlags(uid) & USER_FLAG_INTERNAL ) {
750 sendf(Client->Socket, "404 Invalid user\n");
755 iAmmount = atoi(ammount);
756 if( iAmmount == 0 && ammount[0] != '0' ) {
757 sendf(Client->Socket, "407 Invalid Argument\n");
762 switch( DispenseAdd(Client->UID, uid, iAmmount, reason) )
765 sendf(Client->Socket, "200 Add OK\n");
768 sendf(Client->Socket, "402 Poor Guy\n");
771 sendf(Client->Socket, "500 Unknown error\n");
776 void Server_Cmd_SET(tClient *Client, char *Args)
778 char *user, *ammount, *reason;
781 if( !Client->bIsAuthed ) {
782 sendf(Client->Socket, "401 Not Authenticated\n");
788 ammount = strchr(Args, ' ');
790 sendf(Client->Socket, "407 Invalid Argument, expected 3 parameters, 1 encountered\n");
796 reason = strchr(ammount, ' ');
798 sendf(Client->Socket, "407 Invalid Argument, expected 3 parameters, 2 encountered\n");
804 // Check user permissions
805 if( !(Bank_GetFlags(Client->UID) & USER_FLAG_ADMIN) ) {
806 sendf(Client->Socket, "403 Not an admin\n");
811 uid = Bank_GetAcctByName(user);
813 sendf(Client->Socket, "404 Invalid user\n");
817 // You can't alter an internal account
818 if( Bank_GetFlags(uid) & USER_FLAG_INTERNAL ) {
819 sendf(Client->Socket, "404 Invalid user\n");
824 iAmmount = atoi(ammount);
825 if( iAmmount == 0 && ammount[0] != '0' ) {
826 sendf(Client->Socket, "407 Invalid Argument\n");
831 switch( DispenseSet(Client->UID, uid, iAmmount, reason) )
834 sendf(Client->Socket, "200 Add OK\n");
837 sendf(Client->Socket, "402 Poor Guy\n");
840 sendf(Client->Socket, "500 Unknown error\n");
845 void Server_Cmd_ENUMUSERS(tClient *Client, char *Args)
849 int maxBal = INT_MAX, minBal = INT_MIN;
850 int flagMask = 0, flagVal = 0;
851 int sort = BANK_ITFLAG_SORT_NAME;
852 time_t lastSeenAfter=0, lastSeenBefore=0;
854 int flags; // Iterator flags
855 int balValue; // Balance value for iterator
856 time_t timeValue; // Time value for iterator
859 if( Args && strlen(Args) )
861 char *space = Args, *type, *val;
866 space = strchr(space, ' ');
867 if(space) *space = '\0';
870 val = strchr(type, ':');
877 if( strcmp(type, "min_balance") == 0 ) {
881 else if( strcmp(type, "max_balance") == 0 ) {
885 else if( strcmp(type, "flags") == 0 ) {
886 if( Server_int_ParseFlags(Client, val, &flagMask, &flagVal) )
889 // - Last seen before timestamp
890 else if( strcmp(type, "last_seen_before") == 0 ) {
891 lastSeenAfter = atoll(val);
893 // - Last seen after timestamp
894 else if( strcmp(type, "last_seen_after") == 0 ) {
895 lastSeenAfter = atoll(val);
898 else if( strcmp(type, "sort") == 0 ) {
899 char *dash = strchr(val, '-');
904 if( strcmp(val, "name") == 0 ) {
905 sort = BANK_ITFLAG_SORT_NAME;
907 else if( strcmp(val, "balance") == 0 ) {
908 sort = BANK_ITFLAG_SORT_BAL;
910 else if( strcmp(val, "lastseen") == 0 ) {
911 sort = BANK_ITFLAG_SORT_LASTSEEN;
914 sendf(Client->Socket, "407 Unknown sort field ('%s')\n", val);
917 // Handle sort direction
919 if( strcmp(dash, "desc") == 0 ) {
920 sort |= BANK_ITFLAG_REVSORT;
923 sendf(Client->Socket, "407 Unknown sort direction '%s'\n", dash);
930 sendf(Client->Socket, "407 Unknown argument to ENUM_USERS '%s:%s'\n", type, val);
937 sendf(Client->Socket, "407 Unknown argument to ENUM_USERS '%s'\n", type);
943 *space = ' '; // Repair (to be nice)
945 while(*space == ' ') space ++;
951 if( maxBal != INT_MAX ) {
952 flags = sort|BANK_ITFLAG_MAXBALANCE;
955 else if( minBal != INT_MIN ) {
956 flags = sort|BANK_ITFLAG_MINBALANCE;
963 if( lastSeenBefore ) {
964 timeValue = lastSeenBefore;
965 flags |= BANK_ITFLAG_SEENBEFORE;
967 else if( lastSeenAfter ) {
968 timeValue = lastSeenAfter;
969 flags |= BANK_ITFLAG_SEENAFTER;
974 it = Bank_Iterator(flagMask, flagVal, flags, balValue, timeValue);
977 while( (i = Bank_IteratorNext(it)) != -1 )
979 int bal = Bank_GetBalance(i);
981 if( bal == INT_MIN ) continue;
983 if( bal < minBal ) continue;
984 if( bal > maxBal ) continue;
989 Bank_DelIterator(it);
992 sendf(Client->Socket, "201 Users %i\n", numRet);
996 it = Bank_Iterator(flagMask, flagVal, flags, balValue, timeValue);
998 while( (i = Bank_IteratorNext(it)) != -1 )
1000 int bal = Bank_GetBalance(i);
1002 if( bal == INT_MIN ) continue;
1004 if( bal < minBal ) continue;
1005 if( bal > maxBal ) continue;
1007 _SendUserInfo(Client, i);
1010 Bank_DelIterator(it);
1012 sendf(Client->Socket, "200 List End\n");
1015 void Server_Cmd_USERINFO(tClient *Client, char *Args)
1021 space = strchr(user, ' ');
1022 if(space) *space = '\0';
1025 uid = Bank_GetAcctByName(user);
1027 sendf(Client->Socket, "404 Invalid user");
1031 _SendUserInfo(Client, uid);
1034 void _SendUserInfo(tClient *Client, int UserID)
1036 char *type, *disabled="", *door="";
1037 int flags = Bank_GetFlags(UserID);
1039 if( flags & USER_FLAG_INTERNAL ) {
1042 else if( flags & USER_FLAG_COKE ) {
1043 if( flags & USER_FLAG_ADMIN )
1044 type = "coke,admin";
1048 else if( flags & USER_FLAG_ADMIN ) {
1055 if( flags & USER_FLAG_DISABLED )
1056 disabled = ",disabled";
1057 if( flags & USER_FLAG_DOORGROUP )
1060 // TODO: User flags/type
1062 Client->Socket, "202 User %s %i %s%s\n",
1063 Bank_GetAcctName(UserID), Bank_GetBalance(UserID),
1068 void Server_Cmd_USERADD(tClient *Client, char *Args)
1070 char *username, *space;
1072 // Check permissions
1073 if( !(Bank_GetFlags(Client->UID) & USER_FLAG_ADMIN) ) {
1074 sendf(Client->Socket, "403 Not a coke admin\n");
1080 while( *username == ' ' ) username ++;
1081 space = strchr(username, ' ');
1082 if(space) *space = '\0';
1084 // Try to create user
1085 if( Bank_CreateAcct(username) == -1 ) {
1086 sendf(Client->Socket, "404 User exists\n");
1091 char *thisName = Bank_GetAcctName(Client->UID);
1092 Log_Info("Account '%s' created by '%s'", username, thisName);
1096 sendf(Client->Socket, "200 User Added\n");
1099 void Server_Cmd_USERFLAGS(tClient *Client, char *Args)
1101 char *username, *flags;
1103 int mask=0, value=0;
1106 // Check permissions
1107 if( !(Bank_GetFlags(Client->UID) & USER_FLAG_ADMIN) ) {
1108 sendf(Client->Socket, "403 Not a coke admin\n");
1115 while( *username == ' ' ) username ++;
1116 space = strchr(username, ' ');
1118 sendf(Client->Socket, "407 USER_FLAGS requires 2 arguments, 1 given\n");
1124 while( *flags == ' ' ) flags ++;
1125 space = strchr(flags, ' ');
1126 if(space) *space = '\0';
1129 uid = Bank_GetAcctByName(username);
1131 sendf(Client->Socket, "404 User '%s' not found\n", username);
1136 if( Server_int_ParseFlags(Client, flags, &mask, &value) )
1140 Bank_SetFlags(uid, mask, value);
1143 sendf(Client->Socket, "200 User Updated\n");
1146 // --- INTERNAL HELPERS ---
1147 int sendf(int Socket, const char *Format, ...)
1152 va_start(args, Format);
1153 len = vsnprintf(NULL, 0, Format, args);
1158 va_start(args, Format);
1159 vsnprintf(buf, len+1, Format, args);
1162 #if DEBUG_TRACE_CLIENT
1163 printf("sendf: %s", buf);
1166 return send(Socket, buf, len, 0);
1170 int Server_int_ParseFlags(tClient *Client, const char *Str, int *Mask, int *Value)
1177 {"disabled", USER_FLAG_DISABLED, USER_FLAG_DISABLED}
1178 ,{"door", USER_FLAG_DOORGROUP, USER_FLAG_DOORGROUP}
1179 ,{"coke", USER_FLAG_COKE, USER_FLAG_COKE}
1180 ,{"admin", USER_FLAG_ADMIN, USER_FLAG_ADMIN}
1181 ,{"internal", USER_FLAG_INTERNAL, USER_FLAG_INTERNAL}
1183 const int ciNumFlags = sizeof(cFLAGS)/sizeof(cFLAGS[0]);
1195 while( *Str == ' ' ) Str ++; // Eat whitespace
1196 space = strchr(Str, ','); // Find the end of the flag
1202 // Check for inversion/removal
1203 if( *Str == '!' || *Str == '-' ) {
1207 else if( *Str == '+' ) {
1211 // Check flag values
1212 for( i = 0; i < ciNumFlags; i ++ )
1214 if( strncmp(Str, cFLAGS[i].Name, len) == 0 ) {
1215 *Mask |= cFLAGS[i].Mask;
1216 *Value &= ~cFLAGS[i].Mask;
1218 *Value |= cFLAGS[i].Value;
1224 if( i == ciNumFlags ) {
1226 strncpy(val, Str, len+1);
1227 sendf(Client->Socket, "407 Unknown flag value '%s'\n", val);