1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
| #include<stdlib.h> #include<stdio.h> #include<unistd.h> #include<string.h> #include<unistd.h> #include <sys/prctl.h> #include <linux/filter.h> #include <linux/seccomp.h> #include "user.pb-c.h"
char buffer[1024]; char *chunk_list[5]; int add_count = 4; int show_count = 2; void *ptr;
void welcome() { puts("welcome to user management system"); puts("Here, you can manage some users"); } void setbox() { struct sock_filter filter[] = { BPF_STMT(BPF_LD+BPF_W+BPF_ABS,4), BPF_JUMP(BPF_JMP+BPF_JEQ,0xc000003e,0,2), BPF_STMT(BPF_LD+BPF_W+BPF_ABS,0), BPF_JUMP(BPF_JMP+BPF_JEQ,59,0,1), BPF_STMT(BPF_RET+BPF_K,SECCOMP_RET_KILL), BPF_STMT(BPF_RET+BPF_K,SECCOMP_RET_ALLOW), }; struct sock_fprog prog = { .len = (unsigned short)(sizeof(filter)/sizeof(filter[0])), .filter = filter, }; prctl(PR_SET_NO_NEW_PRIVS,1,0,0,0); prctl(PR_SET_SECCOMP,SECCOMP_MODE_FILTER,&prog); }
void menu() { puts("1. add a user"); puts("2. edit a user"); puts("3. show a user"); puts("4. delete a user"); puts("your choice: "); }
void add(const char *username,size_t username_len,const char *description,size_t description_len) { char name[0x40]; char desc[0x80]; if(add_count > 0) { int index; for(index = 0;index < 5;index++) { if(!chunk_list[index]) break; } if(index < 5) { chunk_list[index] = malloc(0xc0); if(username_len > 0x40 || description_len > 0x80) puts("too long!"); else { memcpy(name, username,username_len); memcpy(chunk_list[index], name, username_len + 8); memcpy(desc, description,description_len); memcpy(chunk_list[index] + 64, desc, description_len + 8); } } else { puts("what?"); } } else { puts("you have no choice!"); } add_count--; }
void edit(uint64_t msg_id,const char *username,size_t username_len,const char *description,size_t description_len) { if(chunk_list[msg_id]) { if(username_len > 0x40 || description_len > 0x80) puts("too long!"); else { memcpy(chunk_list[msg_id], username, username_len); memcpy(chunk_list[msg_id] + 64, description, description_len); } } else { puts("what?"); } }
void delete(uint64_t msg_id) { ptr = chunk_list[msg_id]; if(ptr) { free(ptr); ptr = 0LL; puts("delete success!"); } else { puts("what?"); } }
void show(uint64_t msg_id) { if(show_count > 0) { if(chunk_list[msg_id]) { printf("the content: %s\n",chunk_list[msg_id]); printf("the content: %s\n",chunk_list[msg_id] + 64); } else { puts("nothing happen"); } } else { puts("you have no choice!"); } show_count--; }
void real_main(uint64_t action_id, uint64_t msg_id, const char *username_bytes, size_t username_len, const char *description_bytes, size_t description_len) { if(action_id == 1) add(username_bytes,username_len,description_bytes,description_len); else if(action_id == 2) edit(msg_id,username_bytes,username_len,description_bytes,description_len); else if(action_id == 3) show(msg_id); else if(action_id == 4) delete(msg_id); else puts("invalid choice.");
}
int main() { setvbuf(stdin, 0LL, 2, 0LL); setvbuf(stdout, 0LL, 2, 0LL); setvbuf(stderr, 0LL, 2, 0LL); setbox(); welcome(); User user_msg = USER__INIT; while (1) { menu(); memset(buffer, 0, sizeof(buffer)); ssize_t len = read(0, buffer, sizeof(buffer)); void *msg = user__unpack(NULL, len, buffer); if (!msg) { break; } memcpy(&user_msg, msg, sizeof(User)); real_main(user_msg.action_id, user_msg.msg_id, user_msg.username.data, user_msg.username.len, user_msg.description.data, user_msg.description.len); }
return 0; }
|