Advertisement
Guest User

Untitled

a guest
Dec 15th, 2016
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.78 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <netdb.h>
  3. #include <netinet/in.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <time.h>
  7.  
  8. int doprocessing (int sock);
  9. int authtest(char *username, char *password);
  10. int runcode();
  11. void controlgate(int action);
  12. int countlines(char *filename);
  13.  
  14. int runcode() {
  15. /*
  16. * Dead Code. Just a stub for now in case this is useful later.
  17. */
  18. system("/opt/local/bin/secret");
  19. }
  20.  
  21. #define RESPLINES 56
  22. int authtest(char *username, char *password) {
  23. FILE *fppass, *fpresp;
  24. int randline;
  25. char grampass[16] = "grampass.txt";
  26. char gramresp[16] = "gramresp.txt";
  27. char userpass[16];
  28. char validpass[16];
  29. char *response;
  30.  
  31. /* The only username allowed is "admin" */
  32. if (strcmp(username, "admin") != 0) return 0;
  33.  
  34. strcpy(userpass, password);
  35.  
  36. /* Get the password for authentication from the file */
  37. fppass = fopen(grampass, "r");
  38. if (fppass == NULL) {
  39. perror("Cannot open grampass file");
  40. return 0;
  41. }
  42. if (fgets(validpass, sizeof(validpass), fppass)==NULL) {
  43. perror("Cannot read from grampass file");
  44. return 0;
  45. }
  46. fclose(fppass);
  47.  
  48. validpass[strlen(validpass)-1] = 0; /* Remove newline */
  49. if (strcmp(userpass, validpass) == 0) {
  50. /* Success */
  51. return 1;
  52. } else {
  53. /* Return custom message, selected at random from gramresp file */
  54. srand(time(NULL));
  55. randline=rand()%countlines(gramresp);
  56.  
  57. fpresp = fopen(gramresp, "r");
  58. if (fpresp == NULL) {
  59. perror("Cannot open gramresp file");
  60. return 0;
  61. }
  62.  
  63. /*
  64. * Allocate memory for response text, then skip to that line in the
  65. * response file.
  66. */
  67. response=malloc(128);
  68. for (int i=0; i < randline; i++) {
  69. fgets(response, 128, fpresp);
  70. }
  71.  
  72. fclose(fpresp);
  73. response[strlen(response)-1] = 0; /* Remove newline */
  74. printf("ERR \"%s\"\n",response);
  75. free(response);
  76. return 0;
  77. }
  78. return 0;
  79. }
  80.  
  81. int countlines(char *filename) {
  82. FILE *fp;
  83. unsigned int lines=0;
  84.  
  85. fp = fopen(filename, "r");
  86. if (fp == NULL) {
  87. return 0;
  88. }
  89. while (EOF != (fscanf(fp, "%*[^\n]"), fscanf(fp, "%*c"))) {
  90. ++lines;
  91. }
  92. fclose(fp);
  93. return lines;
  94. }
  95.  
  96. #define OPENGATE 1
  97. #define CLOSEGATE 1
  98. void controlgate(int action) {
  99. FILE *gpio, *export, *direction;
  100.  
  101. export = fopen("/sys/class/gpio/export", "wb");
  102. if (export != NULL) {
  103. fwrite("11\n", 1, 3, export);
  104. fclose(export);
  105. }
  106.  
  107. direction = fopen("/sys/class/gpio/gpio11", "wb");
  108. if (direction != NULL) {
  109. fwrite("out\n", 1, 4, direction);
  110. fclose(direction);
  111. }
  112.  
  113. gpio = fopen("/sys/class/gpio/gpio11", "wb");
  114. if (gpio != NULL) {
  115. if (action == OPENGATE) {
  116. fwrite("1\n", 1, 2, gpio);
  117. } else {
  118. fwrite("0\n", 1, 2, gpio);
  119. fclose(gpio);
  120. }
  121. }
  122. }
  123.  
  124. int doprocessing (int sock) {
  125. int n, gotuser=0, gotpass=0;
  126. char buffer[1024], strchr[2] = "\n\x00", *token;
  127. char username[256], password[256];
  128.  
  129. memset(buffer, 0, 256);
  130. chdir("/opt/local/sbin");
  131.  
  132. /* Send stdout and stderr to socket client */
  133. dup2(sock, STDOUT_FILENO);
  134. dup2(sock, STDERR_FILENO);
  135.  
  136. printf("OK GRAMGATE ready. Send USER command.\n"); fflush(stdout);
  137.  
  138. while(1) {
  139. n = read(sock, buffer, 1024);
  140.  
  141. if (n < 0) {
  142. perror("ERROR reading from socket");
  143. return 0;
  144. }
  145.  
  146. /* Tolkeinize the received string, parsing combined commands delineated with \n */
  147. token = strtok(buffer, strchr);
  148. while (token != NULL) {
  149.  
  150. /* Got everything we need, don't parse the rest of the buffer */
  151. if (gotuser == 1 && gotpass == 1) {
  152. break;
  153. }
  154.  
  155. if (strncmp(token, "USER ", 5) == 0) {
  156. strncpy(username, token+5, sizeof(username));
  157. gotuser=1;
  158. if (gotpass == 0) {
  159. printf("OK Send PASS command.\n"); fflush(stdout);
  160. }
  161. } else if (strncmp(token, "PASS ", 5) == 0) {
  162. strncpy(password, token+5, sizeof(password));
  163. gotpass=1;
  164. if (gotuser == 0) {
  165. printf("OK Send USER command.\n"); fflush(stdout);
  166. }
  167. }
  168. token = strtok(NULL, strchr);
  169. }
  170.  
  171. if (gotuser==1 && gotpass==1) {
  172. break;
  173. }
  174. }
  175.  
  176. if (authtest(username, password)) {
  177. printf("OK Authentication Success. Send OPEN or CLOSE command.\n"); fflush(stdout);
  178. n = read(sock, buffer, 1024);
  179.  
  180. if (n < 0) {
  181. perror("ERROR reading from socket");
  182. return 0;
  183. }
  184.  
  185. if (strncmp(buffer, "OPEN", 4) == 0) {
  186. controlgate(OPENGATE);
  187. } else if (strncmp(buffer, "CLOSE", 5) == 0) {
  188. controlgate(CLOSEGATE);
  189. } else {
  190. printf("ERR Command Unrecognized.\n"); fflush(stdout);
  191. return 1;
  192. }
  193. } else {
  194. printf("ERR Authentication Failed.\n"); fflush(stdout);
  195. return 0;
  196. }
  197.  
  198. return 0;
  199. }
  200.  
  201. int main( int argc, char *argv[] ) {
  202. int sockfd, newsockfd, portno, clilen;
  203. char buffer[256];
  204. struct sockaddr_in serv_addr, cli_addr;
  205. int n, pid, sockyes=1;
  206.  
  207. /* First call to socket() function */
  208. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  209.  
  210. if (sockfd < 0) {
  211. perror("ERROR opening socket");
  212. exit(1);
  213. }
  214. if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &sockyes, sizeof(int)) == -1) {
  215. perror("ERROR setting socket options");
  216. exit(1);
  217. }
  218.  
  219. /* Initialize socket structure */
  220. memset((char *) &serv_addr, 0, sizeof(serv_addr));
  221. portno = 5001;
  222.  
  223. serv_addr.sin_family = AF_INET;
  224. serv_addr.sin_addr.s_addr = INADDR_ANY;
  225. serv_addr.sin_port = htons(portno);
  226.  
  227. /* Now bind the host address using bind() call.*/
  228. if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
  229. perror("ERROR on binding");
  230. exit(1);
  231. }
  232.  
  233. /* Now start listening for the clients, here
  234. * process will go in sleep mode and will wait
  235. * for the incoming connection
  236. */
  237.  
  238. listen(sockfd,5);
  239. clilen = sizeof(cli_addr);
  240.  
  241. while (1) {
  242. newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
  243. if (newsockfd < 0) {
  244. perror("ERROR on accept");
  245. exit(1);
  246. }
  247.  
  248. /* Create child process */
  249. pid = fork();
  250. if (pid < 0) {
  251. perror("ERROR on fork");
  252. exit(1);
  253. }
  254.  
  255. if (pid == 0) {
  256. /* This is the client process */
  257. close(sockfd);
  258. exit(doprocessing(newsockfd));
  259. } else {
  260. close(newsockfd);
  261. }
  262. } /* end of while */
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement