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 void Debug(tClient *Client, const char *Format, ...);
72 int Server_int_ParseFlags(tClient *Client, const char *Str, int *Mask, int *Value);
73 int sendf(int Socket, const char *Format, ...);
77 const struct sClientCommand {
79 void (*Function)(tClient *Client, char *Arguments);
80 } gaServer_Commands[] = {
81 {"USER", Server_Cmd_USER},
82 {"PASS", Server_Cmd_PASS},
83 {"AUTOAUTH", Server_Cmd_AUTOAUTH},
84 {"SETEUSER", Server_Cmd_SETEUSER},
85 {"ENUM_ITEMS", Server_Cmd_ENUMITEMS},
86 {"ITEM_INFO", Server_Cmd_ITEMINFO},
87 {"DISPENSE", Server_Cmd_DISPENSE},
88 {"GIVE", Server_Cmd_GIVE},
89 {"DONATE", Server_Cmd_DONATE},
90 {"ADD", Server_Cmd_ADD},
91 {"SET", Server_Cmd_SET},
92 {"ENUM_USERS", Server_Cmd_ENUMUSERS},
93 {"USER_INFO", Server_Cmd_USERINFO},
94 {"USER_ADD", Server_Cmd_USERADD},
95 {"USER_FLAGS", Server_Cmd_USERFLAGS}
97 #define NUM_COMMANDS ((int)(sizeof(gaServer_Commands)/sizeof(gaServer_Commands[0])))
100 int giServer_Port = 1020;
101 int giServer_NextClientID = 1;
106 * \brief Open listenting socket and serve connections
108 void Server_Start(void)
111 struct sockaddr_in server_addr, client_addr;
113 atexit(Server_Cleanup);
116 giServer_Socket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
117 if( giServer_Socket < 0 ) {
118 fprintf(stderr, "ERROR: Unable to create server socket\n");
122 // Make listen address
123 memset(&server_addr, 0, sizeof(server_addr));
124 server_addr.sin_family = AF_INET; // Internet Socket
125 server_addr.sin_addr.s_addr = htonl(INADDR_ANY); // Listen on all interfaces
126 server_addr.sin_port = htons(giServer_Port); // Port
129 if( bind(giServer_Socket, (struct sockaddr *) &server_addr, sizeof(server_addr)) < 0 ) {
130 fprintf(stderr, "ERROR: Unable to bind to 0.0.0.0:%i\n", giServer_Port);
136 if( listen(giServer_Socket, MAX_CONNECTION_QUEUE) < 0 ) {
137 fprintf(stderr, "ERROR: Unable to listen to socket\n");
142 printf("Listening on 0.0.0.0:%i\n", giServer_Port);
146 uint len = sizeof(client_addr);
149 // Accept a connection
150 client_socket = accept(giServer_Socket, (struct sockaddr *) &client_addr, &len);
151 if(client_socket < 0) {
152 fprintf(stderr, "ERROR: Unable to accept client connection\n");
156 // Debug: Print the connection string
157 if(giDebugLevel >= 2) {
158 char ipstr[INET_ADDRSTRLEN];
159 inet_ntop(AF_INET, &client_addr.sin_addr, ipstr, INET_ADDRSTRLEN);
160 printf("Client connection from %s:%i\n",
161 ipstr, ntohs(client_addr.sin_port));
164 // Doesn't matter what, localhost is trusted
165 if( ntohl( client_addr.sin_addr.s_addr ) == 0x7F000001 )
168 // Trusted Connections
169 if( ntohs(client_addr.sin_port) < 1024 )
171 // TODO: Make this runtime configurable
172 switch( ntohl( client_addr.sin_addr.s_addr ) )
174 case 0x7F000001: // 127.0.0.1 localhost
175 //case 0x825E0D00: // 130.95.13.0
176 case 0x825E0D07: // 130.95.13.7 motsugo
177 case 0x825E0D12: // 130.95.13.18 mussel
178 case 0x825E0D17: // 130.95.13.23 martello
186 // TODO: Multithread this?
187 Server_HandleClient(client_socket, bTrusted);
189 close(client_socket);
193 void Server_Cleanup(void)
195 printf("Close(%i)\n", giServer_Socket);
196 close(giServer_Socket);
200 * \brief Reads from a client socket and parses the command strings
201 * \param Socket Client socket number/handle
202 * \param bTrusted Is the client trusted?
204 void Server_HandleClient(int Socket, int bTrusted)
206 char inbuf[INPUT_BUFFER_SIZE];
208 int remspace = INPUT_BUFFER_SIZE-1;
212 memset(&clientInfo, 0, sizeof(clientInfo));
214 // Initialise Client info
215 clientInfo.Socket = Socket;
216 clientInfo.ID = giServer_NextClientID ++;
217 clientInfo.bIsTrusted = bTrusted;
218 clientInfo.EffectiveUID = -1;
223 * - The `buf` and `remspace` variables allow a line to span several
224 * calls to recv(), if a line is not completed in one recv() call
225 * it is saved to the beginning of `inbuf` and `buf` is updated to
228 while( (bytes = recv(Socket, buf, remspace, 0)) > 0 )
231 buf[bytes] = '\0'; // Allow us to use stdlib string functions on it
235 while( (eol = strchr(start, '\n')) )
239 Server_ParseClientCommand(&clientInfo, start);
244 // Check if there was an incomplete line
245 if( *start != '\0' ) {
246 int tailBytes = bytes - (start-buf);
247 // Roll back in buffer
248 memcpy(inbuf, start, tailBytes);
249 remspace -= tailBytes;
251 send(Socket, MSG_STR_TOO_LONG, sizeof(MSG_STR_TOO_LONG), 0);
253 remspace = INPUT_BUFFER_SIZE - 1;
258 remspace = INPUT_BUFFER_SIZE - 1;
264 fprintf(stderr, "ERROR: Unable to recieve from client on socket %i\n", Socket);
268 if(giDebugLevel >= 2) {
269 printf("Client %i: Disconnected\n", clientInfo.ID);
274 * \brief Parses a client command and calls the required helper function
275 * \param Client Pointer to client state structure
276 * \param CommandString Command from client (single line of the command)
277 * \return Heap String to return to the client
279 void Server_ParseClientCommand(tClient *Client, char *CommandString)
288 // Split at first space
289 space = strchr(CommandString, ' ');
296 while( *space == ' ' ) space ++;
301 for( i = 0; args[i]; )
303 while( CommandString[i] != ' ' ) {
304 if( CommandString[i] == '"' ) {
305 while( !(CommandString[i] != '\\' CommandString[i+1] == '"' ) )
312 while( CommandString[i] == ' ' ) i ++;
319 for( i = 0; i < NUM_COMMANDS; i++ )
321 if(strcmp(CommandString, gaServer_Commands[i].Name) == 0) {
322 gaServer_Commands[i].Function(Client, args);
327 sendf(Client->Socket, "400 Unknown Command\n");
334 * \brief Set client username
336 * Usage: USER <username>
338 void Server_Cmd_USER(tClient *Client, char *Args)
340 char *space = strchr(Args, ' ');
341 if(space) *space = '\0'; // Remove characters after the ' '
345 Debug(Client, "Authenticating as '%s'", Args);
349 free(Client->Username);
350 Client->Username = strdup(Args);
353 // Create a salt (that changes if the username is changed)
354 // Yes, I know, I'm a little paranoid, but who isn't?
355 Client->Salt[0] = 0x21 + (rand()&0x3F);
356 Client->Salt[1] = 0x21 + (rand()&0x3F);
357 Client->Salt[2] = 0x21 + (rand()&0x3F);
358 Client->Salt[3] = 0x21 + (rand()&0x3F);
359 Client->Salt[4] = 0x21 + (rand()&0x3F);
360 Client->Salt[5] = 0x21 + (rand()&0x3F);
361 Client->Salt[6] = 0x21 + (rand()&0x3F);
362 Client->Salt[7] = 0x21 + (rand()&0x3F);
364 // TODO: Also send hash type to use, (SHA1 or crypt according to [DAA])
365 sendf(Client->Socket, "100 SALT %s\n", Client->Salt);
367 sendf(Client->Socket, "100 User Set\n");
372 * \brief Authenticate as a user
376 void Server_Cmd_PASS(tClient *Client, char *Args)
378 char *space = strchr(Args, ' ');
379 if(space) *space = '\0'; // Remove characters after the ' '
381 // Pass on to cokebank
382 Client->UID = Bank_GetUserAuth(Client->Salt, Client->Username, Args);
384 if( Client->UID != -1 ) {
385 Client->bIsAuthed = 1;
386 sendf(Client->Socket, "200 Auth OK\n");
390 sendf(Client->Socket, "401 Auth Failure\n");
394 * \brief Authenticate as a user without a password
396 * Usage: AUTOAUTH <user>
398 void Server_Cmd_AUTOAUTH(tClient *Client, char *Args)
400 char *space = strchr(Args, ' ');
401 if(space) *space = '\0'; // Remove characters after the ' '
404 if( !Client->bIsTrusted ) {
406 Debug(Client, "Untrusted client attempting to AUTOAUTH");
407 sendf(Client->Socket, "401 Untrusted\n");
412 Client->UID = Bank_GetAcctByName( Args );
413 if( Client->UID < 0 ) {
415 Debug(Client, "Unknown user '%s'", Args);
416 sendf(Client->Socket, "401 Auth Failure\n");
420 // You can't be an internal account
421 if( Bank_GetFlags(Client->UID) & USER_FLAG_INTERNAL ) {
423 sendf(Client->Socket, "401 Auth Failure\n");
427 Client->bIsAuthed = 1;
430 Debug(Client, "Auto authenticated as '%s' (%i)", Args, Client->UID);
432 sendf(Client->Socket, "200 Auth OK\n");
436 * \brief Set effective user
438 void Server_Cmd_SETEUSER(tClient *Client, char *Args)
442 space = strchr(Args, ' ');
444 if(space) *space = '\0';
446 if( !strlen(Args) ) {
447 sendf(Client->Socket, "407 SETEUSER expects an argument\n");
451 // Check user permissions
452 if( !(Bank_GetFlags(Client->UID) & (USER_FLAG_COKE|USER_FLAG_ADMIN)) ) {
453 sendf(Client->Socket, "403 Not in coke\n");
458 Client->EffectiveUID = Bank_GetAcctByName(Args);
459 if( Client->EffectiveUID == -1 ) {
460 sendf(Client->Socket, "404 User not found\n");
464 // You can't be an internal account
465 if( Bank_GetFlags(Client->EffectiveUID) & USER_FLAG_INTERNAL ) {
466 Client->EffectiveUID = -1;
467 sendf(Client->Socket, "404 User not found\n");
471 sendf(Client->Socket, "200 User set\n");
475 * \brief Enumerate the items that the server knows about
477 void Server_Cmd_ENUMITEMS(tClient *Client, char *Args)
481 if( Args != NULL && strlen(Args) ) {
482 sendf(Client->Socket, "407 ENUM_ITEMS takes no arguments\n");
486 sendf(Client->Socket, "201 Items %i\n", giNumItems);
488 for( i = 0; i < giNumItems; i ++ ) {
489 sendf(Client->Socket,
490 "202 Item %s:%i %i %s\n",
491 gaItems[i].Handler->Name, gaItems[i].ID, gaItems[i].Price, gaItems[i].Name
495 sendf(Client->Socket, "200 List end\n");
498 tItem *_GetItemFromString(char *String)
502 char *colon = strchr(String, ':');
514 for( i = 0; i < giNumHandlers; i ++ )
516 if( strcmp(gaHandlers[i]->Name, type) == 0) {
517 handler = gaHandlers[i];
526 for( i = 0; i < giNumItems; i ++ )
528 if( gaItems[i].Handler != handler ) continue;
529 if( gaItems[i].ID != num ) continue;
536 * \brief Fetch information on a specific item
538 void Server_Cmd_ITEMINFO(tClient *Client, char *Args)
540 tItem *item = _GetItemFromString(Args);
543 sendf(Client->Socket, "406 Bad Item ID\n");
547 sendf(Client->Socket,
548 "202 Item %s:%i %i %s\n",
549 item->Handler->Name, item->ID, item->Price, item->Name
553 void Server_Cmd_DISPENSE(tClient *Client, char *Args)
559 if( !Client->bIsAuthed ) {
560 sendf(Client->Socket, "401 Not Authenticated\n");
564 item = _GetItemFromString(Args);
566 sendf(Client->Socket, "406 Bad Item ID\n");
570 if( Client->EffectiveUID != -1 ) {
571 uid = Client->EffectiveUID;
577 switch( ret = DispenseItem( Client->UID, uid, item ) )
579 case 0: sendf(Client->Socket, "200 Dispense OK\n"); return ;
580 case 1: sendf(Client->Socket, "501 Unable to dispense\n"); return ;
581 case 2: sendf(Client->Socket, "402 Poor You\n"); return ;
583 sendf(Client->Socket, "500 Dispense Error\n");
588 void Server_Cmd_GIVE(tClient *Client, char *Args)
590 char *recipient, *ammount, *reason;
594 if( !Client->bIsAuthed ) {
595 sendf(Client->Socket, "401 Not Authenticated\n");
601 ammount = strchr(Args, ' ');
603 sendf(Client->Socket, "407 Invalid Argument, expected 3 parameters, 1 encountered\n");
609 reason = strchr(ammount, ' ');
611 sendf(Client->Socket, "407 Invalid Argument, expected 3 parameters, 2 encountered\n");
618 uid = Bank_GetAcctByName(recipient);
620 sendf(Client->Socket, "404 Invalid target user\n");
624 // You can't alter an internal account
625 if( Bank_GetFlags(uid) & USER_FLAG_INTERNAL ) {
626 sendf(Client->Socket, "404 Invalid target user\n");
631 iAmmount = atoi(ammount);
632 if( iAmmount <= 0 ) {
633 sendf(Client->Socket, "407 Invalid Argument, ammount must be > zero\n");
637 if( Client->EffectiveUID != -1 ) {
638 thisUid = Client->EffectiveUID;
641 thisUid = Client->UID;
645 switch( DispenseGive(Client->UID, thisUid, uid, iAmmount, reason) )
648 sendf(Client->Socket, "200 Give OK\n");
651 sendf(Client->Socket, "402 Poor You\n");
654 sendf(Client->Socket, "500 Unknown error\n");
659 void Server_Cmd_DONATE(tClient *Client, char *Args)
661 char *ammount, *reason;
665 if( !Client->bIsAuthed ) {
666 sendf(Client->Socket, "401 Not Authenticated\n");
672 // Get the start of the reason
673 reason = strchr(Args, ' ');
675 sendf(Client->Socket, "407 Invalid Argument, expected 2 parameters, 1 encountered\n");
681 // Check the end of the reason
682 if( strchr(reason, ' ') ) {
683 sendf(Client->Socket, "407 Invalid Argument, expected 2 parameters, more encountered\n");
688 iAmmount = atoi(ammount);
689 if( iAmmount <= 0 ) {
690 sendf(Client->Socket, "407 Invalid Argument, ammount must be > zero\n");
694 // Handle effective users
695 if( Client->EffectiveUID != -1 ) {
696 thisUid = Client->EffectiveUID;
699 thisUid = Client->UID;
703 switch( DispenseDonate(Client->UID, thisUid, iAmmount, reason) )
706 sendf(Client->Socket, "200 Give OK\n");
709 sendf(Client->Socket, "402 Poor You\n");
712 sendf(Client->Socket, "500 Unknown error\n");
717 void Server_Cmd_ADD(tClient *Client, char *Args)
719 char *user, *ammount, *reason;
722 if( !Client->bIsAuthed ) {
723 sendf(Client->Socket, "401 Not Authenticated\n");
729 ammount = strchr(Args, ' ');
731 sendf(Client->Socket, "407 Invalid Argument, expected 3 parameters, 1 encountered\n");
737 reason = strchr(ammount, ' ');
739 sendf(Client->Socket, "407 Invalid Argument, expected 3 parameters, 2 encountered\n");
745 // Check user permissions
746 if( !(Bank_GetFlags(Client->UID) & (USER_FLAG_COKE|USER_FLAG_ADMIN)) ) {
747 sendf(Client->Socket, "403 Not in coke\n");
752 uid = Bank_GetAcctByName(user);
754 sendf(Client->Socket, "404 Invalid user\n");
758 // You can't alter an internal account
759 if( Bank_GetFlags(uid) & USER_FLAG_INTERNAL ) {
760 sendf(Client->Socket, "404 Invalid user\n");
765 iAmmount = atoi(ammount);
766 if( iAmmount == 0 && ammount[0] != '0' ) {
767 sendf(Client->Socket, "407 Invalid Argument\n");
772 switch( DispenseAdd(Client->UID, uid, iAmmount, reason) )
775 sendf(Client->Socket, "200 Add OK\n");
778 sendf(Client->Socket, "402 Poor Guy\n");
781 sendf(Client->Socket, "500 Unknown error\n");
786 void Server_Cmd_SET(tClient *Client, char *Args)
788 char *user, *ammount, *reason;
791 if( !Client->bIsAuthed ) {
792 sendf(Client->Socket, "401 Not Authenticated\n");
798 ammount = strchr(Args, ' ');
800 sendf(Client->Socket, "407 Invalid Argument, expected 3 parameters, 1 encountered\n");
806 reason = strchr(ammount, ' ');
808 sendf(Client->Socket, "407 Invalid Argument, expected 3 parameters, 2 encountered\n");
814 // Check user permissions
815 if( !(Bank_GetFlags(Client->UID) & USER_FLAG_ADMIN) ) {
816 sendf(Client->Socket, "403 Not an admin\n");
821 uid = Bank_GetAcctByName(user);
823 sendf(Client->Socket, "404 Invalid user\n");
827 // You can't alter an internal account
828 if( Bank_GetFlags(uid) & USER_FLAG_INTERNAL ) {
829 sendf(Client->Socket, "404 Invalid user\n");
834 iAmmount = atoi(ammount);
835 if( iAmmount == 0 && ammount[0] != '0' ) {
836 sendf(Client->Socket, "407 Invalid Argument\n");
841 switch( DispenseSet(Client->UID, uid, iAmmount, reason) )
844 sendf(Client->Socket, "200 Add OK\n");
847 sendf(Client->Socket, "402 Poor Guy\n");
850 sendf(Client->Socket, "500 Unknown error\n");
855 void Server_Cmd_ENUMUSERS(tClient *Client, char *Args)
859 int maxBal = INT_MAX, minBal = INT_MIN;
860 int flagMask = 0, flagVal = 0;
861 int sort = BANK_ITFLAG_SORT_NAME;
862 time_t lastSeenAfter=0, lastSeenBefore=0;
864 int flags; // Iterator flags
865 int balValue; // Balance value for iterator
866 time_t timeValue; // Time value for iterator
869 if( Args && strlen(Args) )
871 char *space = Args, *type, *val;
876 space = strchr(space, ' ');
877 if(space) *space = '\0';
880 val = strchr(type, ':');
887 if( strcmp(type, "min_balance") == 0 ) {
891 else if( strcmp(type, "max_balance") == 0 ) {
895 else if( strcmp(type, "flags") == 0 ) {
896 if( Server_int_ParseFlags(Client, val, &flagMask, &flagVal) )
899 // - Last seen before timestamp
900 else if( strcmp(type, "last_seen_before") == 0 ) {
901 lastSeenAfter = atoll(val);
903 // - Last seen after timestamp
904 else if( strcmp(type, "last_seen_after") == 0 ) {
905 lastSeenAfter = atoll(val);
908 else if( strcmp(type, "sort") == 0 ) {
909 char *dash = strchr(val, '-');
914 if( strcmp(val, "name") == 0 ) {
915 sort = BANK_ITFLAG_SORT_NAME;
917 else if( strcmp(val, "balance") == 0 ) {
918 sort = BANK_ITFLAG_SORT_BAL;
920 else if( strcmp(val, "lastseen") == 0 ) {
921 sort = BANK_ITFLAG_SORT_LASTSEEN;
924 sendf(Client->Socket, "407 Unknown sort field ('%s')\n", val);
927 // Handle sort direction
929 if( strcmp(dash, "desc") == 0 ) {
930 sort |= BANK_ITFLAG_REVSORT;
933 sendf(Client->Socket, "407 Unknown sort direction '%s'\n", dash);
940 sendf(Client->Socket, "407 Unknown argument to ENUM_USERS '%s:%s'\n", type, val);
947 sendf(Client->Socket, "407 Unknown argument to ENUM_USERS '%s'\n", type);
953 *space = ' '; // Repair (to be nice)
955 while(*space == ' ') space ++;
961 if( maxBal != INT_MAX ) {
962 flags = sort|BANK_ITFLAG_MAXBALANCE;
965 else if( minBal != INT_MIN ) {
966 flags = sort|BANK_ITFLAG_MINBALANCE;
973 if( lastSeenBefore ) {
974 timeValue = lastSeenBefore;
975 flags |= BANK_ITFLAG_SEENBEFORE;
977 else if( lastSeenAfter ) {
978 timeValue = lastSeenAfter;
979 flags |= BANK_ITFLAG_SEENAFTER;
984 it = Bank_Iterator(flagMask, flagVal, flags, balValue, timeValue);
987 while( (i = Bank_IteratorNext(it)) != -1 )
989 int bal = Bank_GetBalance(i);
991 if( bal == INT_MIN ) continue;
993 if( bal < minBal ) continue;
994 if( bal > maxBal ) continue;
999 Bank_DelIterator(it);
1002 sendf(Client->Socket, "201 Users %i\n", numRet);
1006 it = Bank_Iterator(flagMask, flagVal, flags, balValue, timeValue);
1008 while( (i = Bank_IteratorNext(it)) != -1 )
1010 int bal = Bank_GetBalance(i);
1012 if( bal == INT_MIN ) continue;
1014 if( bal < minBal ) continue;
1015 if( bal > maxBal ) continue;
1017 _SendUserInfo(Client, i);
1020 Bank_DelIterator(it);
1022 sendf(Client->Socket, "200 List End\n");
1025 void Server_Cmd_USERINFO(tClient *Client, char *Args)
1031 space = strchr(user, ' ');
1032 if(space) *space = '\0';
1035 Debug(Client, "User Info '%s'", user);
1038 uid = Bank_GetAcctByName(user);
1040 sendf(Client->Socket, "404 Invalid user");
1044 _SendUserInfo(Client, uid);
1047 void _SendUserInfo(tClient *Client, int UserID)
1049 char *type, *disabled="", *door="";
1050 int flags = Bank_GetFlags(UserID);
1052 if( flags & USER_FLAG_INTERNAL ) {
1055 else if( flags & USER_FLAG_COKE ) {
1056 if( flags & USER_FLAG_ADMIN )
1057 type = "coke,admin";
1061 else if( flags & USER_FLAG_ADMIN ) {
1068 if( flags & USER_FLAG_DISABLED )
1069 disabled = ",disabled";
1070 if( flags & USER_FLAG_DOORGROUP )
1073 // TODO: User flags/type
1075 Client->Socket, "202 User %s %i %s%s\n",
1076 Bank_GetAcctName(UserID), Bank_GetBalance(UserID),
1081 void Server_Cmd_USERADD(tClient *Client, char *Args)
1083 char *username, *space;
1085 // Check permissions
1086 if( !(Bank_GetFlags(Client->UID) & USER_FLAG_ADMIN) ) {
1087 sendf(Client->Socket, "403 Not a coke admin\n");
1093 while( *username == ' ' ) username ++;
1094 space = strchr(username, ' ');
1095 if(space) *space = '\0';
1097 // Try to create user
1098 if( Bank_CreateAcct(username) == -1 ) {
1099 sendf(Client->Socket, "404 User exists\n");
1104 char *thisName = Bank_GetAcctName(Client->UID);
1105 Log_Info("Account '%s' created by '%s'", username, thisName);
1109 sendf(Client->Socket, "200 User Added\n");
1112 void Server_Cmd_USERFLAGS(tClient *Client, char *Args)
1114 char *username, *flags;
1116 int mask=0, value=0;
1119 // Check permissions
1120 if( !(Bank_GetFlags(Client->UID) & USER_FLAG_ADMIN) ) {
1121 sendf(Client->Socket, "403 Not a coke admin\n");
1128 while( *username == ' ' ) username ++;
1129 space = strchr(username, ' ');
1131 sendf(Client->Socket, "407 USER_FLAGS requires 2 arguments, 1 given\n");
1137 while( *flags == ' ' ) flags ++;
1138 space = strchr(flags, ' ');
1139 if(space) *space = '\0';
1142 uid = Bank_GetAcctByName(username);
1144 sendf(Client->Socket, "404 User '%s' not found\n", username);
1149 if( Server_int_ParseFlags(Client, flags, &mask, &value) )
1153 Bank_SetFlags(uid, mask, value);
1156 sendf(Client->Socket, "200 User Updated\n");
1159 // --- INTERNAL HELPERS ---
1160 void Debug(tClient *Client, const char *Format, ...)
1163 printf("%010lli [%i] ", time(NULL), Client->ID);
1164 va_start(args, Format);
1165 vprintf(NULL, 0, Format, args);
1170 int sendf(int Socket, const char *Format, ...)
1175 va_start(args, Format);
1176 len = vsnprintf(NULL, 0, Format, args);
1181 va_start(args, Format);
1182 vsnprintf(buf, len+1, Format, args);
1185 #if DEBUG_TRACE_CLIENT
1186 printf("sendf: %s", buf);
1189 return send(Socket, buf, len, 0);
1193 int Server_int_ParseFlags(tClient *Client, const char *Str, int *Mask, int *Value)
1200 {"disabled", USER_FLAG_DISABLED, USER_FLAG_DISABLED}
1201 ,{"door", USER_FLAG_DOORGROUP, USER_FLAG_DOORGROUP}
1202 ,{"coke", USER_FLAG_COKE, USER_FLAG_COKE}
1203 ,{"admin", USER_FLAG_ADMIN, USER_FLAG_ADMIN}
1204 ,{"internal", USER_FLAG_INTERNAL, USER_FLAG_INTERNAL}
1206 const int ciNumFlags = sizeof(cFLAGS)/sizeof(cFLAGS[0]);
1218 while( *Str == ' ' ) Str ++; // Eat whitespace
1219 space = strchr(Str, ','); // Find the end of the flag
1225 // Check for inversion/removal
1226 if( *Str == '!' || *Str == '-' ) {
1230 else if( *Str == '+' ) {
1234 // Check flag values
1235 for( i = 0; i < ciNumFlags; i ++ )
1237 if( strncmp(Str, cFLAGS[i].Name, len) == 0 ) {
1238 *Mask |= cFLAGS[i].Mask;
1239 *Value &= ~cFLAGS[i].Mask;
1241 *Value |= cFLAGS[i].Value;
1247 if( i == ciNumFlags ) {
1249 strncpy(val, Str, len+1);
1250 sendf(Client->Socket, "407 Unknown flag value '%s'\n", val);