Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.05 KB | None | 0 0
  1. //This code is BUGGY and badly written - that is its purpose.
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <strings.h>
  6. #include <string.h>
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <fcntl.h>
  10. #include <unistd.h>
  11. #include <sys/utsname.h>
  12. #include <errno.h>
  13.  
  14. #define CHECKIN 0
  15. #define CHECKOUT 1
  16.  
  17. #define CHILD -10
  18.  
  19. #define REPOSITORY ".bcvs"
  20. #define LOGEXT ".comments"
  21. #define BLOCK_LIST_PATH ".bcvs/block.list"
  22. #define MAX_BLOCK_PRE 64
  23. #define PATH_MAX 512
  24.  
  25. void help(char*);
  26. int copyFile(char*);
  27. void writeLog(FILE*);
  28.  
  29. int opcode;
  30.  
  31. char *block_prefix[MAX_BLOCK_PRE];
  32. int max_prefix = 0;
  33.  
  34. void read_block_list() {
  35. FILE *block = fopen(BLOCK_LIST_PATH, "r");
  36. char temp_string[PATH_MAX] = {0};
  37. if (block == NULL) {
  38. printf("BCVS blocklist missing!\n");
  39. exit(-1);
  40. }
  41. while (max_prefix < MAX_BLOCK_PRE && !feof(block)) {
  42. if (fgets(temp_string, PATH_MAX, block) != NULL) {
  43. max_prefix++;
  44. block_prefix[max_prefix] = strdup(temp_string);
  45. //kill newline
  46. char *p;
  47. if (p = strchr(block_prefix[max_prefix], '\n')) {
  48. *p = '\0';
  49. }
  50. }
  51. }
  52. }
  53.  
  54. int is_blocked(char *filename) {
  55. char canonical_pathname[PATH_MAX];
  56. int i;
  57. char *path = realpath(filename, canonical_pathname);
  58. if (path == NULL) {
  59. return 0; /* filename does not exist, so not blocked!! */
  60. }
  61. for(i = 0; i <= max_prefix; i++) {
  62. if(block_prefix[i] && (strstr(path, block_prefix[i]) == path)) {
  63. return 1;
  64. }
  65. }
  66. return 0; /* path did not start with any of the blocked prefixes */
  67. }
  68.  
  69. int main(int argc, char* argv[]) {
  70. int i;
  71. char log[256] = {0};
  72. FILE *logfile;
  73.  
  74. //check arguments
  75. if (argc != 3) {
  76. help(argv[0]);
  77. return 1;
  78. }
  79.  
  80. if (strncmp("co", argv[1], 255) == 0) {
  81. opcode = CHECKOUT;
  82. }
  83. else if (strncmp("ci", argv[1], 255) == 0) {
  84. opcode = CHECKIN;
  85. } else {
  86. help(argv[0]);
  87. return 1;
  88. }
  89.  
  90. read_block_list();
  91. if (is_blocked(argv[2])) {
  92. printf("User asked to copy forbidden file.\n");
  93. return 3;
  94. }
  95.  
  96. //Now that we know the user can write to the file, let's take
  97. //a moment and have the user put in a comment to the logfile
  98. //construct log paths
  99. strcpy(log, REPOSITORY);
  100. strcat(log, "/");
  101. strcat(log, argv[2]);
  102. strcat(log, LOGEXT);
  103.  
  104. logfile = fopen(log, "a");
  105. writeLog(logfile); //does error checking for us
  106. if(logfile != NULL){
  107. fclose(logfile);
  108. }
  109.  
  110. i = copyFile(argv[2]);
  111.  
  112. if (i == CHILD) return CHILD;
  113. if (i < 0) {
  114. //let's still write the logfile, even if we failed to copy
  115. printf("Copy failed!\n");
  116. return -9;
  117. }
  118.  
  119.  
  120.  
  121. return 0;
  122. }
  123.  
  124. void help(char* name) {
  125. printf("Usage: %s [ci | co] filename\n", name);
  126. }
  127.  
  128. int copyFile(char* arg) {
  129. int c;
  130. int i;
  131. pid_t pid;
  132. int status = 0;
  133. char user[16] = {0};
  134. char tempString[72] = {0};
  135. FILE *sourcefile, *destfile;
  136. char chmodString[] = "0700";
  137. char src[72] = {0};
  138. char dst[72] = {0};
  139.  
  140. if (opcode == CHECKOUT) {
  141.  
  142. strcpy(dst, arg); //copy destination file
  143.  
  144. //construct repository path
  145. strcpy(src, REPOSITORY);
  146. strcat(src, "/");
  147. strcat(src, dst);
  148. }
  149. else if (opcode == CHECKIN) {
  150.  
  151. strcpy(src, arg); //copy source file
  152.  
  153. //construct repository path
  154. strcpy(dst, REPOSITORY);
  155. strcat(dst, "/");
  156. strcat(dst, src);
  157. }
  158. else {
  159. //huh?
  160. return -1;
  161. }
  162.  
  163. sourcefile = fopen(src, "r");
  164. destfile = fopen(dst, "w");
  165.  
  166. if (!sourcefile || !destfile) {
  167. // could not open one or both of the files
  168. printf("Error while opening files.\n");
  169. return -1;
  170. }
  171.  
  172. i = 0;
  173. c = getc(sourcefile);
  174. while (c != EOF) {
  175. fputc(c, destfile);
  176. i++;
  177. c = getc(sourcefile);
  178. }
  179.  
  180. fclose(sourcefile);
  181. fclose(destfile);
  182.  
  183. if (opcode == CHECKOUT) {
  184. //chown
  185. pid = fork();
  186. if (pid < 0) return -1;
  187. else if (pid == 0) {
  188. //child
  189. strcat(user, getenv("USER"));
  190. if (user != 0) execlp("chown", "chown", user, dst, (char *)0);
  191. printf("Warning: chown failed!\n");
  192. perror("execlp");
  193. return CHILD;
  194. } else {
  195. //parent
  196. waitpid(pid, &status, NULL);
  197. }
  198.  
  199. //chmod
  200. pid = fork();
  201. if (pid < 0) return -1;
  202. else if (pid == 0) {
  203. //child
  204. execlp("chmod", "chmod", chmodString, dst, (char *)0);
  205. //chmod(dst, S_IREAD | S_IWRITE);
  206. printf("Warning: chmod failed!\n");
  207. perror("execlp");
  208. return CHILD;
  209. } else {
  210. //parent
  211. waitpid(pid, &status, NULL);
  212. }
  213. }
  214.  
  215. return i;
  216. }
  217.  
  218. void writeLog(FILE* logfile) {
  219. int i;
  220. char c;
  221. char *tempString;
  222.  
  223. if (!logfile) {
  224. // could not open one or both of the files
  225. printf("Error while opening log file.\n");
  226. return;
  227. }
  228.  
  229. //no one needs more than 1024 chars of note space!
  230. tempString = calloc(1024, sizeof(char));
  231.  
  232. printf("Please write a SHORT explanation:\n");
  233. c = i = 0;
  234. while (c != '\n' && c != EOF) {
  235. c = getchar();
  236. tempString[i] = c;
  237. fwrite(&c, sizeof(char), 1, logfile);
  238. i++;
  239. }
  240.  
  241. printf(tempString);
  242. printf("written to log.\n");
  243. free(tempString);
  244. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement