Ejemplo n.º 1
0
/*
 * netconf_create_hello
 * create capability string (once)
 */
int
netconf_create_hello(cbuf *xf,            /* msg buffer */
		     int session_id)
{
    int retval = 0;

    add_preamble(xf);
    cprintf(xf, "<hello>");
    cprintf(xf, "<capabilities>");
    cprintf(xf, "<capability>urn:ietf:params:xml:ns:netconf:base:1.0</capability>\n");
    cprintf(xf, "<capability>urn:ietf:params:xml:ns:netconf:capability:candidate:1:0</capability>\n");
    cprintf(xf, "<capability>urn:ietf:params:xml:ns:netconf:capability:validate:1.0</capability>\n");
   cprintf(xf, "<capability>urn:ietf:params:netconf:capability:xpath:1.0</capability>\n");
   cprintf(xf, "<capability>urn:ietf:params:netconf:capability:notification:1.0</capability>\n");


//    cprintf(xf, "<capability>urn:rnr:rnrapi:1:0</capability>");
    cprintf(xf, "</capabilities>");
    cprintf(xf, "<session-id>%lu</session-id>", 42+session_id);
    cprintf(xf, "</hello>");
    add_postamble(xf);
    return retval;
}
static int read_write_file(struct filename *f1name, struct filename *f2name, unsigned char *key, int flag)
{
	struct file *fp1 =NULL;	
	struct file *fp2 =NULL;
	struct file *fp_temp = NULL;
	int rbytes, wbytes;
	char *buf = NULL;
	int keylen =16;
	int rc;
	int flag_outfile, flag_delete_temp;
	mm_segment_t fs;

	flag_outfile =0;
	flag_delete_temp =0;
	fp1 = filp_open(f1name->name,O_RDONLY,0);
	putname(f1name);
	
	if(!fp1)
	{
		printk("Error opening input file \n");
		rc = -ENOENT;
		return rc;			
	}
	
	if(IS_ERR(fp1))
	{
		printk("Error opening input file \n");
		rc = -ENOENT;
		return rc;
	}
	
	rc = is_regular_file(fp1);
	if(rc){
		printk("Input file is not regular \n");
		rc = -EISDIR;
		goto out;
	}
	
	if(!fp1->f_op){
		printk("Permission Denied \n");
		rc = -EPERM;
		goto out;
	}

	if(!fp1->f_op->read){
		printk("Read operation not permitted \n");
		rc = -EPERM;
		goto out;
	}

	flag_outfile = is_file_exists(f2name->name);
	
	if(flag_outfile)
		fp2 = filp_open(f2name->name,O_WRONLY ,fp1->f_mode);
	else
		fp2 = filp_open(f2name->name,O_CREAT | O_WRONLY ,fp1->f_mode);

	fp_temp = filp_open(strcat((char *)f2name->name,".tmp"),O_CREAT | O_WRONLY,fp1->f_mode);

	putname(f2name);

	if(!fp2 || !fp_temp){
		printk("Error opening write file\n");
		rc= -ENOENT;
		return rc;
	}

	if(IS_ERR(fp2) || IS_ERR(fp_temp)){
		printk("Error opening write file\n");
		rc= -ENOENT;
		return rc;
	}

	if(!fp2->f_op || !fp2->f_op){
                printk("Permission Denied \n");
                rc = -EPERM;
                goto out;
        }

        if(!fp2->f_op->write || !fp2->f_op->write){
                printk("Write operation not permitted \n");
                rc = -EPERM;
                goto out;
        }
	
	rc = is_same_file(fp1,fp2);
        if(rc){
                printk("Input and output files are same \n");
                rc = -EINVAL;
                goto out;
        }
	
	buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
	if(IS_ERR(buf)){
		printk("Error while allocationg temporary buffer for reading and writing \n");
                rc = -PTR_ERR(key);
                goto out;
        }

	/* Add info about key in preamble during encryption */
	if(flag == 1)
	{	
		rc=add_preamble(fp_temp,key,keylen);
		if(rc< 0){
			printk("Error adding preamble to the outfile\n");
			goto out;
		}
	}

	/* Verify hashed key stored in preamble during decryption */
	if(flag == 0)
	{
		rc=verify_preamble(fp1,key,keylen);
		if(rc<0){
			printk("Error verifying preamble in the encrypted file\n");
			goto out;
		}
	}
	
	/* read and write actual data */
	while(1){
		fs=get_fs();
		set_fs(get_ds());
		
		rbytes = fp1->f_op->read(fp1,buf,PAGE_SIZE,&fp1->f_pos);
		
		if(rbytes < 0){
			printk("Failed reading input file\n");
			set_fs(fs);
			rc= -EIO;
			goto out;
		}

		if (rbytes == 0){
			printk("Reached end of file while reading \n");
			rc= 0;
			goto out_rename;
		}
		
		/* If flag is set as 1, input file is encrypted */
		if(flag == 1){
			rc=encrypt_decrypt_file(buf,key,rbytes,flag);
			if(rc < 0){
				printk("Encrypt failure\n");
				goto out;
			}
		}
		/* If flag is set as 0, input file is decrypted */
		else if(flag == 0){
			rc=encrypt_decrypt_file(buf,key,rbytes,flag);
			if(rc < 0){
				printk("Decrypt failure \n");
				goto out;
			}
		}

		wbytes = fp_temp->f_op->write(fp_temp,buf,rbytes,&fp_temp->f_pos);
		
		if(wbytes < 0){
			printk("Failed writing output file\n");
                        set_fs(fs);
                        rc= -EIO;
                        goto out;
		}
		set_fs(fs);
	}
	
out_rename:
	flag_delete_temp=1;
	rc = rename_temp_file(fp_temp,fp2);
	if(rc)
		printk("Rename operation failed \n");

out:	
	if(flag_delete_temp==0){
		if(rc<0){
			if(delete_partial_file(fp_temp))
				printk("Deleting partial temp file failed \n");
		}
	}
	printk("flag_outfile = %d \n",flag_outfile);
	if(flag_outfile==0){
		if(rc < 0){
			if(delete_partial_file(fp2))
				printk("Deleting out file failed\n");
        	}
	}
	if(fp_temp)
		filp_close(fp_temp,NULL);
	if(buf)
		kfree(buf);
	if(fp2)
		filp_close(fp2,NULL);
	if(fp1)
		filp_close(fp1,NULL);
	if(IS_ERR(f2name))
		putname(f2name);
	if(IS_ERR(f1name))
		putname(f1name);
	return rc;
}