]> arthur.barton.de Git - netatalk.git/blob - etc/uams/uams_pgp.c
Initial revision
[netatalk.git] / etc / uams / uams_pgp.c
1 /* Copyright (c) 1990,1993 Regents of The University of Michigan.
2  * Copyright (c) 1999 Adrian Sun (asun@u.washington.edu) 
3  * All Rights Reserved.  See COPYRIGHT.
4  */
5
6 #ifdef UAM_PGP
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <unistd.h>
11 #include <pwd.h>
12 #include <syslog.h>
13
14 #include <bn.h>
15
16 #include <atalk/afp.h>
17 #include <atalk/uam.h>
18
19 #define KEYSIZE 16
20 #define PASSWDLEN 64
21 #define CRYPTBUFLEN  (KEYSIZE*2)
22 #define CRYPT2BUFLEN (KEYSIZE + PASSWDLEN)
23
24 /* hash a number to a 16-bit quantity */
25 #define pgphash(a) ((((unsigned long) (a) >> 8) ^ \
26                      (unsigned long) (a)) & 0xffff)
27
28 /* the secret key */
29 static struct passwd *pgppwd;
30
31
32 /* pgp passwd */
33 static int pgp_login(void *obj, struct passwd **uam_pwd,
34                      char *ibuf, int ibuflen,
35                      char *rbuf, int *rbuflen)
36 {
37     BIGNUM *bn, *gbn, *pbn;
38     u_int16_t sessid;
39     int len, i;
40     char *name;
41
42     *rbuflen = 0;
43
44     if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, (void *) &name, &i) < 0)
45       return AFPERR_PARAM;
46
47     len = (unsigned char) *ibuf++;
48     if ( len > i ) {
49         return( AFPERR_PARAM );
50     }
51
52     memcpy(name, ibuf, len );
53     ibuf += len;
54     name[ len ] = '\0';
55     if ((unsigned long) ibuf & 1) /* padding */
56       ++ibuf;
57
58     if (( pgppwd = uam_getname(name, i)) == NULL ) {
59       return AFPERR_PARAM;
60     }
61
62     syslog( LOG_INFO, "pgp login: %s", name);
63     if (uam_checkuser(pgppwd) < 0)
64       return AFPERR_NOTAUTH;
65
66     /* get the challenge */
67     len = (unsigned char) *ibuf++;
68     /* challenge */
69     
70     /* get the signature. it's always 16 bytes. */
71     if (uam_afpserver_option(obj, UAM_OPTION_SIGNATURE, 
72                              (void *) &name, NULL) < 0) {
73       *rbuflen = 0;
74       goto passwd_fail;
75     }
76     memcpy(rbuf + KEYSIZE, name, KEYSIZE); 
77
78 pgp_fail:
79     return AFPERR_PARAM;
80 }
81
82 static int pgp_logincont(void *obj, struct passwd **uam_pwd,
83                          char *ibuf, int ibuflen, 
84                          char *rbuf, int *rbuflen)
85 {
86     BIGNUM *bn1, *bn2, *bn3;
87     u_int16_t sessid;
88     char *p;
89
90     *rbuflen = 0;
91
92     /* check for session id */
93     memcpy(&sessid, ibuf, sizeof(sessid));
94     if (sessid != pgphash(obj))
95       return AFPERR_PARAM;
96     ibuf += sizeof(sessid);
97    
98     /* use rbuf as scratch space */
99     CAST_cbc_encrypt(ibuf, rbuf, CRYPT2BUFLEN, &castkey,
100                      iv, CAST_DECRYPT);
101     
102     /* check to make sure that the random number is the same. we
103      * get sent back an incremented random number. */
104     if (!(bn1 = BN_bin2bn(rbuf, KEYSIZE, NULL)))
105       return AFPERR_PARAM;
106
107     if (!(bn2 = BN_bin2bn(randbuf, sizeof(randbuf), NULL))) {
108       BN_free(bn1);
109       return AFPERR_PARAM;
110     }
111       
112     /* zero out the random number */
113     memset(rbuf, 0, sizeof(randbuf));
114     memset(randbuf, 0, sizeof(randbuf));
115     rbuf += KEYSIZE;
116
117     if (!(bn3 = BN_new())) {
118       BN_free(bn2);
119       BN_free(bn1);
120       return AFPERR_PARAM;
121     }
122
123     BN_sub(bn3, bn1, bn2);
124     BN_free(bn2);
125     BN_free(bn1);
126
127     /* okay. is it one more? */
128     if (!BN_is_one(bn3)) {
129       BN_free(bn3);
130       return AFPERR_PARAM;
131     }
132     BN_free(bn3);
133
134 #ifdef AFS
135     if ( kcheckuser(*uam_pwd, rbuf) == 0) {
136       *uam_pwd = pgppwd;
137       return AFP_OK;
138     }
139 #endif AFS
140
141     rbuf[PASSWDLEN] = '\0';
142     p = crypt( rbuf, pgppwd->pw_passwd );
143     memset(rbuf, 0, PASSWDLEN);
144     if ( strcmp( p, pgppwd->pw_passwd ) == 0 ) {
145       *uam_pwd = pgppwd;
146       return AFP_OK;
147     }
148
149     return AFPERR_NOTAUTH;
150 }
151
152
153 static int uam_setup(const char *path)
154 {
155   if (uam_register(UAM_SERVER_LOGIN, path, "PGPuam 1.0",
156                    pgp_login, pgp_logincont, NULL) < 0)
157     return -1;
158
159   return 0;
160 }
161
162 static void uam_cleanup(void)
163 {
164   uam_unregister(UAM_SERVER_LOGIN, "PGPuam 1.0");
165 }
166
167 UAM_MODULE_EXPORT struct uam_export uams_pgp = {
168   UAM_MODULE_SERVER,
169   UAM_MODULE_VERSION,
170   uam_setup, uam_cleanup
171 };
172 #endif