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