|
|
1.1 root 1: #include <stdio.h>
2: #include <string.h>
3:
4: #define VERSION 1
5:
6: extern char *xs();
7: extern char *getpass(), *getlogin();
8: extern char *ctime(), *malloc(), *realloc();
9: extern long time();
10: extern void exit();
11: char *slurp();
12: char name[9];
13:
14: main(ac,av)
15: char **av;
16: {
17: long tloc;
18: int n, headlen;
19: FILE *fil;
20: char *date, *doc, *sig, *key, *head;
21: char prompt[100], hexdate[10];
22:
23: if(ac>2 && strcmp(av[1], "-n")==0){
24: strncpy(name, av[2], 8);
25: av += 2;
26: ac -= 2;
27: } else
28: strcpy(name,getlogin());
29:
30: switch(ac){
31: case 2:
32: fil = fopen(av[1],"r");
33: if(fil == 0){
34: write(2,"sign: ",6);
35: perror(av[1]);
36: exit(1);
37: }
38: break;
39: case 1:
40: fil = stdin;
41: break;
42: default:
43: fprintf(stderr,"usage: sign [-n name] [file]\n");
44: exit(1);
45: break;
46: }
47:
48: (void)time(&tloc);
49: date = ctime(&tloc);
50: date[24] = 0;
51: sprintf(hexdate,"%08lx",tloc);
52: headlen = 24+8+strlen(name);
53: head = malloc(headlen+1);
54: strcpy(head,date);
55: strcat(head,hexdate);
56: strcat(head,name);
57:
58: doc = slurp(fil, &n, head);
59: sprintf(prompt, "%s's signing key: ", name);
60: key = getpass(prompt);
61: if(key == 0) {
62: fprintf(stderr, "sign: failed to get password\n");
63: exit(1);
64: }
65: sig = xs(key, doc, n);
66: n -= headlen;
67: while(*key)
68: *key++ = 0;
69:
70: printf("Signed by %s, %s\n", name, date);
71: printf("sum=%s, date=%s, count=%d, ver=%d\n",
72: sig, hexdate, n, VERSION);
73: printf("------\n");
74: fwrite(doc+headlen,1,n,stdout);
75: printf("------\n");
76: printf("sum=%s, date=%s, count=%d, ver=%d\n",
77: sig, hexdate, n, VERSION);
78: printf("End %s, %s\n", name, date);
79: exit(0);
80: }
81:
82: char *
83: slurp(fil, np, date)
84: FILE *fil;
85: int *np;
86: char *date;
87: {
88: char *s;
89: unsigned n, ntotal;
90: int nl, c;
91: int nleft;
92:
93: nleft = 0;
94: s = strdup(date);
95: ntotal = n = strlen(s);
96:
97: nl = 1;
98: while((c=getc(fil)) != EOF) {
99: if(nleft<=2) {
100: int d = 500+ntotal/2;
101: nleft += d;
102: ntotal += d;
103: s = realloc(s, ntotal);
104: if(s==0) {
105: fprintf(stderr,"sign: out of space\n");
106: exit(1);
107: }
108: }
109: if(nl) {
110: s[n++] = ',';
111: nleft--;
112: }
113: nl = 0;
114: s[n++] = c;
115: nleft--;
116: if(c=='\n')
117: nl = 1;
118: }
119: if(!nl) {
120: s[n++] = '\n';
121: fprintf(stderr,"sign: newline appended\n");
122: }
123:
124: *np = n;
125: return s;
126: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.