Advertisement
Guest User

user_level.c

a guest
Dec 10th, 2012
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.76 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <errno.h>
  5. #include <ctype.h>
  6. #include <string.h>
  7. #define help_text "usage: cipher [-devhm] [-p PASSWD] <infile> <outfile>\n "\
  8.                   "\n "\
  9.                   "Command descriptions:\n "\
  10.                   "  p   Pre-supply password for encryption/decrytion to use\n "\
  11.                   "  e   Encrypt your infile to your outfile with a supplied password\n "\
  12.                   "  d   Decrypt your infile to your outfile with a supplied password\n "\
  13.                   "  v   Display the current running version of the program\n "\
  14.                   "  m   Map files using mmap system calls\n"
  15.  
  16.  
  17. #define __NR_xcrypt 349 /* our private syscall number */
  18.  
  19. struct xcryptParam {
  20.     int encrypt;
  21.     char *infile;
  22.     char *outfile;
  23.     char *password;
  24.     int passlen;
  25. };
  26.  
  27. int main(int argc, char *argv[])
  28. {
  29.     int rc;
  30.     struct xcryptParam *passParam = malloc(sizeof(struct xcryptParam));
  31.     int c;
  32.     int j = 0;
  33.     int index;
  34.     char temp_buf[16];
  35.     char *pass = NULL;
  36.     int dflag = 0;
  37.     int eflag = 0;
  38.     char *inFile = NULL;
  39.     char *outFile = NULL;
  40.  
  41.     /* Initially set opterr flag to 0 for operations processing */
  42.     opterr = 0;
  43.  
  44.     while ((c = getopt(argc, argv, "deph:")) != -1) {
  45.         switch(c) {
  46.             case 'd':
  47.                  // Check if user has supplied to encrypt and decrypt the file. Throw error
  48.                 if(eflag == 1) {
  49.                     fprintf(stderr, "Error: You must either supply an option to encrypt OR decrypt.\n");
  50.                     exit(EXIT_FAILURE);
  51.                 }
  52.                 passParam->encrypt = 0;
  53.                 dflag = 1;
  54.                 break;
  55.             case 'e':
  56.                  // Check if user has supplied to encrypt and decrypt the file. Throw error
  57.                 if(dflag == 1) {
  58.                     fprintf(stderr, "Error: You must either supply an option to encrypt OR decrypt.\n");
  59.                     exit(EXIT_FAILURE);
  60.                 }
  61.                 passParam->encrypt = 1;
  62.                 eflag = 1;
  63.                 break;
  64.             case 'h':
  65.                 printf("%s\n", help_text);
  66.                 return 1;
  67.             case 'p':
  68.                 //pass = optarg;
  69.                 //printf("%s\n", optarg);
  70.                 break;
  71.             case '?':
  72.                 if(optopt == 'p')
  73.                     fprintf(stderr, "Option -%c requires an argument.\n", optopt);
  74.                 else if(isprint(optopt))
  75.                     fprintf(stderr, "Unknown option '-%c.\n", optopt);
  76.                 else
  77.                     fprintf(stderr, "Unknown option character '\\x%x'.\n", optopt);
  78.                     return 1;
  79.             default:
  80.                 abort();
  81.         }
  82.     }
  83.  
  84.   if((dflag == 0) && (eflag == 0)) {
  85.        printf("Error: Encrypt/Decrypt has not been selected...\nThis is an encrypting program :-)\n");
  86.        exit(EXIT_FAILURE);
  87.   }
  88.  
  89.   for(index = optind; index < argc; index++) {
  90.       if (j == 0) {
  91.         pass = argv[index];
  92.       }
  93.       else if(j == 1) {
  94.                 inFile = argv[index];
  95.                 printf("1st argument: %s\n", argv[index]);
  96.            }
  97.       else if(j == 2) {
  98.                 outFile = argv[index];
  99.                 printf("2nd argument: %s\n", argv[index]);
  100.            }
  101.       else
  102.           printf("Non-option argument %s\n", argv[index]);
  103.       j++;
  104.  
  105.   }
  106. printf("Encrypt: %i\n", passParam->encrypt);
  107.   if((inFile == NULL)) {
  108.        printf("Error: No [in]file supplied\n");
  109.        exit(EXIT_FAILURE);
  110.   }
  111.   else {
  112.       passParam->infile = inFile;
  113.   }
  114.  
  115.   if((outFile == NULL)) {
  116.        printf("Error: No [out]file supplied\n");
  117.        exit(EXIT_FAILURE);
  118.   }
  119.   else {
  120.       passParam->outfile = outFile;
  121.   }
  122.  
  123.   strcpy(temp_buf, pass); // Move the streams contents to a buff (for checking and proccessing purposes)
  124.   passParam->password = pass;
  125.   passParam->passlen = strlen(pass);
  126.  
  127.     rc = syscall(__NR_xcrypt, (void *)passParam);
  128.     if (rc == 0)
  129.         printf("syscall returned %d\n", rc);
  130.     else
  131.         printf("syscall returned %d (errno=%d)\n", rc, errno);
  132.  
  133.     exit(rc);
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement