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