25a6ec2206fcab75d3edbd5e43d960591596fd19
[debian/amanda] / client-src / sendbackup-krb4.c
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 1991,1993 University of Maryland
4  * All Rights Reserved.
5  *
6  * Permission to use, copy, modify, distribute, and sell this software and its
7  * documentation for any purpose is hereby granted without fee, provided that
8  * the above copyright notice appear in all copies and that both that
9  * copyright notice and this permission notice appear in supporting
10  * documentation, and that the name of U.M. not be used in advertising or
11  * publicity pertaining to distribution of the software without specific,
12  * written prior permission.  U.M. makes no representations about the
13  * suitability of this software for any purpose.  It is provided "as is"
14  * without express or implied warranty.
15  *
16  * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
18  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22  *
23  * Author: James da Silva, Systems Design and Analysis Group
24  *                         Computer Science Department
25  *                         University of Maryland at College Park
26  */
27 /*
28  * sendbackup-krb4.c - those bits of sendbackup code that deal with encrypting
29  *                     data streams over the network.  Even though these just
30  *                     call the underlying DES routines, the U.S. government
31  *                     considers this a munition.  Go figure.
32  */
33 #include "krb4-security.h"
34 #include "sendbackup-krb4.h"
35
36 /*
37  * NOTE:  This symbol must be the same as DATABUF_SIZE in
38  * server-src/dumper-krb4.c
39  * so that the encrypt/decrypt routines are working on the same sized buffers.
40  * Really, this should be moved out of dumper.c so that both programs can use
41  * the same symbol.  Hopefully that can be done later.  This is good enough
42  * to get encryption working for now...
43  *
44  *                  - Chris Ross (cross@uu.net)  4-Jun-1998
45  */
46 #define DATABUF_SIZE    DISK_BLOCK_BYTES
47
48 void kencrypt_stream()
49 {
50     char *bp, buffer[DATABUF_SIZE];
51     int rdsize, wrsize, left;
52     des_key_schedule sched;
53     int l, n;
54
55     des_key_sched(session_key, sched);
56
57     while(1) {
58         /* read a block, taking into account short reads */
59         left = DATABUF_SIZE;
60         bp = buffer;
61         while(left) {
62             if((rdsize = read(0, bp, left)) == -1)
63                 error("kencrypt: read error: %s", strerror(errno));
64             if(rdsize == 0) break;
65             left -= rdsize;
66             bp += rdsize;
67         }
68         if(bp == buffer) break; /* end of file */
69
70         if(bp < buffer+DATABUF_SIZE)
71             memset(bp,0,left);
72
73         des_pcbc_encrypt(buffer, buffer, DATABUF_SIZE, sched, session_key,
74                          DES_ENCRYPT);
75
76         for(l = 0, n = DATABUF_SIZE; l < n; l += wrsize) {
77             if((wrsize = write(1, buffer + l, n - l)) < 0) {
78                 error("kencrypt: write error: %s", strerror(errno));
79             }
80         }
81     }
82 }