Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <errno.h>
- #include <ctype.h>
- #include <string.h>
- #define help_text "usage: cipher [-devhm] [-p PASSWD] <infile> <outfile>\n "\
- "\n "\
- "Command descriptions:\n "\
- " p Pre-supply password for encryption/decrytion to use\n "\
- " e Encrypt your infile to your outfile with a supplied password\n "\
- " d Decrypt your infile to your outfile with a supplied password\n "\
- " v Display the current running version of the program\n "\
- " m Map files using mmap system calls\n"
- #define __NR_xcrypt 349 /* our private syscall number */
- struct xcryptParam {
- int encrypt;
- char *infile;
- char *outfile;
- char *password;
- int passlen;
- };
- int main(int argc, char *argv[])
- {
- int rc;
- struct xcryptParam *passParam = malloc(sizeof(struct xcryptParam));
- int c;
- int j = 0;
- int index;
- char temp_buf[16];
- char *pass = NULL;
- int dflag = 0;
- int eflag = 0;
- char *inFile = NULL;
- char *outFile = NULL;
- /* Initially set opterr flag to 0 for operations processing */
- opterr = 0;
- while ((c = getopt(argc, argv, "deph:")) != -1) {
- switch(c) {
- case 'd':
- // Check if user has supplied to encrypt and decrypt the file. Throw error
- if(eflag == 1) {
- fprintf(stderr, "Error: You must either supply an option to encrypt OR decrypt.\n");
- exit(EXIT_FAILURE);
- }
- passParam->encrypt = 0;
- dflag = 1;
- break;
- case 'e':
- // Check if user has supplied to encrypt and decrypt the file. Throw error
- if(dflag == 1) {
- fprintf(stderr, "Error: You must either supply an option to encrypt OR decrypt.\n");
- exit(EXIT_FAILURE);
- }
- passParam->encrypt = 1;
- eflag = 1;
- break;
- case 'h':
- printf("%s\n", help_text);
- return 1;
- case 'p':
- //pass = optarg;
- //printf("%s\n", optarg);
- break;
- case '?':
- if(optopt == 'p')
- fprintf(stderr, "Option -%c requires an argument.\n", optopt);
- else if(isprint(optopt))
- fprintf(stderr, "Unknown option '-%c.\n", optopt);
- else
- fprintf(stderr, "Unknown option character '\\x%x'.\n", optopt);
- return 1;
- default:
- abort();
- }
- }
- if((dflag == 0) && (eflag == 0)) {
- printf("Error: Encrypt/Decrypt has not been selected...\nThis is an encrypting program :-)\n");
- exit(EXIT_FAILURE);
- }
- for(index = optind; index < argc; index++) {
- if (j == 0) {
- pass = argv[index];
- }
- else if(j == 1) {
- inFile = argv[index];
- printf("1st argument: %s\n", argv[index]);
- }
- else if(j == 2) {
- outFile = argv[index];
- printf("2nd argument: %s\n", argv[index]);
- }
- else
- printf("Non-option argument %s\n", argv[index]);
- j++;
- }
- printf("Encrypt: %i\n", passParam->encrypt);
- if((inFile == NULL)) {
- printf("Error: No [in]file supplied\n");
- exit(EXIT_FAILURE);
- }
- else {
- passParam->infile = inFile;
- }
- if((outFile == NULL)) {
- printf("Error: No [out]file supplied\n");
- exit(EXIT_FAILURE);
- }
- else {
- passParam->outfile = outFile;
- }
- strcpy(temp_buf, pass); // Move the streams contents to a buff (for checking and proccessing purposes)
- passParam->password = pass;
- passParam->passlen = strlen(pass);
- rc = syscall(__NR_xcrypt, (void *)passParam);
- if (rc == 0)
- printf("syscall returned %d\n", rc);
- else
- printf("syscall returned %d (errno=%d)\n", rc, errno);
- exit(rc);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement