]> arthur.barton.de Git - netatalk.git/blob - etc/uams/uams_dhx_pam.c
Initial patch for authenticated printing. Requires LaserWriter 8.5.1 or
[netatalk.git] / etc / uams / uams_dhx_pam.c
1 /*
2  * Copyright (c) 1990,1993 Regents of The University of Michigan.
3  * Copyright (c) 1999 Adrian Sun (asun@u.washington.edu) 
4  * All Rights Reserved.  See COPYRIGHT.
5  */
6 #if defined(USE_PAM) && defined(UAM_DHX)
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <syslog.h>
11
12 #include <security/pam_appl.h>
13
14 #include <bn.h>
15 #include <dh.h>
16 #include <cast.h>
17
18 #include <atalk/afp.h>
19 #include <atalk/uam.h>
20
21 #define KEYSIZE 16
22 #define PASSWDLEN 64
23 #define CRYPTBUFLEN  (KEYSIZE*2)
24 #define CRYPT2BUFLEN (KEYSIZE + PASSWDLEN)
25 #define CHANGEPWBUFLEN (KEYSIZE + 2*PASSWDLEN)
26
27 /* hash a number to a 16-bit quantity */
28 #define dhxhash(a) ((((unsigned long) (a) >> 8) ^ \
29                      (unsigned long) (a)) & 0xffff)
30
31 /* the secret key */
32 static CAST_KEY castkey;
33 static struct passwd *dhxpwd;
34 static u_int8_t randbuf[KEYSIZE];
35
36 /* diffie-hellman bits */
37 static unsigned char msg2_iv[] = "CJalbert";
38 static unsigned char msg3_iv[] = "LWallace";
39 static const u_int8_t p[] = {0xBA, 0x28, 0x73, 0xDF, 0xB0, 0x60, 0x57, 0xD4,
40                              0x3F, 0x20, 0x24, 0x74, 0x4C, 0xEE, 0xE7, 0x5B};
41 static const u_int8_t g = 0x07;
42
43
44 /* Static variables used to communicate between the conversation function
45  * and the server_login function
46  */
47 static pam_handle_t *pamh = NULL; 
48 static char *PAM_username;
49 static char *PAM_password;
50
51 /* PAM conversation function
52  * Here we assume (for now, at least) that echo on means login name, and
53  * echo off means password.
54  */
55 static int PAM_conv (int num_msg,
56                      const struct pam_message **msg,
57                      struct pam_response **resp,
58                      void *appdata_ptr) {
59   int count = 0;
60   struct pam_response *reply;
61   
62 #define COPY_STRING(s) (s) ? strdup(s) : NULL
63   
64   if (num_msg < 1)
65     return PAM_CONV_ERR;
66
67   reply = (struct pam_response *) 
68     calloc(num_msg, sizeof(struct pam_response));
69
70   if (!reply)
71     return PAM_CONV_ERR;
72
73   for (count = 0; count < num_msg; count++) {
74     char *string = NULL;
75
76     switch (msg[count]->msg_style) {
77     case PAM_PROMPT_ECHO_ON:
78       if (!(string = COPY_STRING(PAM_username)))
79         goto pam_fail_conv;
80       break;
81     case PAM_PROMPT_ECHO_OFF:
82       if (!(string = COPY_STRING(PAM_password)))
83         goto pam_fail_conv;
84       break;
85     case PAM_TEXT_INFO:
86 #ifdef PAM_BINARY_PROMPT
87     case PAM_BINARY_PROMPT:
88 #endif
89       /* ignore it... */
90       break;
91     case PAM_ERROR_MSG:
92     default:
93       goto pam_fail_conv;
94     }
95
96     if (string) {  
97       reply[count].resp_retcode = 0;
98       reply[count].resp = string;
99       string = NULL;
100     }
101   }
102
103   *resp = reply;
104   return PAM_SUCCESS;
105
106 pam_fail_conv:
107   for (count = 0; count < num_msg; count++) {
108     if (!reply[count].resp)
109         continue;
110     switch (msg[count]->msg_style) {
111     case PAM_PROMPT_ECHO_OFF:
112     case PAM_PROMPT_ECHO_ON:
113       free(reply[count].resp);
114       break;      
115     }
116   }
117   free(reply);
118   return PAM_CONV_ERR;
119 }
120
121 static struct pam_conv PAM_conversation = {
122   &PAM_conv,
123   NULL
124 };
125
126
127 static int dhx_setup(void *obj, char *ibuf, int ibuflen, 
128                      char *rbuf, int *rbuflen)
129 {
130     u_int16_t sessid;
131     int i;
132     BIGNUM *bn, *gbn, *pbn;
133     DH *dh;
134
135     /* get the client's public key */
136     if (!(bn = BN_bin2bn(ibuf, KEYSIZE, NULL)))
137       return AFPERR_PARAM;
138
139     /* get our primes */
140     if (!(gbn = BN_bin2bn(&g, sizeof(g), NULL))) {
141       BN_clear_free(bn);
142       return AFPERR_PARAM;
143     }
144
145     if (!(pbn = BN_bin2bn(p, sizeof(p), NULL))) {
146       BN_free(gbn);
147       BN_clear_free(bn);
148       return AFPERR_PARAM;
149     }
150
151     /* okay, we're ready */
152     if (!(dh = DH_new())) {
153       BN_free(pbn);
154       BN_free(gbn);
155       BN_clear_free(bn);
156       return AFPERR_PARAM;
157     }
158
159     /* generate key and make sure that we have enough space */
160     dh->p = pbn;
161     dh->g = gbn;
162     if (!DH_generate_key(dh) || (BN_num_bytes(dh->pub_key) > KEYSIZE)) {
163       goto pam_fail;
164     }
165
166     /* figure out the key. store the key in rbuf for now. */
167     i = DH_compute_key(rbuf, bn, dh);
168     
169     /* set the key */
170     CAST_set_key(&castkey, i, rbuf);
171     
172     /* session id. it's just a hashed version of the object pointer. */
173     sessid = dhxhash(obj);
174     memcpy(rbuf, &sessid, sizeof(sessid));
175     rbuf += sizeof(sessid);
176     *rbuflen += sizeof(sessid);
177     
178     /* public key */
179     BN_bn2bin(dh->pub_key, rbuf); 
180     rbuf += KEYSIZE;
181     *rbuflen += KEYSIZE;
182
183     /* buffer to be encrypted */
184     i = sizeof(randbuf);
185     if (uam_afpserver_option(obj, UAM_OPTION_RANDNUM, (void *) randbuf,
186                              &i) < 0) {
187       *rbuflen = 0;
188       goto pam_fail;
189     }    
190     memcpy(rbuf, &randbuf, sizeof(randbuf));
191
192     /* get the signature. it's always 16 bytes. */
193 #if 0
194     if (uam_afpserver_option(obj, UAM_OPTION_SIGNATURE, 
195                              (void *) &buf, NULL) < 0) {
196       *rbuflen = 0;
197       goto pam_fail;
198     }
199     memcpy(rbuf + KEYSIZE, buf, KEYSIZE); 
200 #else
201     memset(rbuf + KEYSIZE, 0, KEYSIZE); 
202 #endif
203
204     /* encrypt using cast */
205     CAST_cbc_encrypt(rbuf, rbuf, CRYPTBUFLEN, &castkey, msg2_iv, 
206                      CAST_ENCRYPT);
207     *rbuflen += CRYPTBUFLEN;
208     BN_free(bn);
209     DH_free(dh);
210     return AFPERR_AUTHCONT;
211
212 pam_fail:
213     BN_free(bn);
214     DH_free(dh);
215     return AFPERR_PARAM;
216 }
217
218
219 /* dhx login: things are done in a slightly bizarre order to avoid
220  * having to clean things up if there's an error. */
221 static int pam_login(void *obj, struct passwd **uam_pwd,
222                      char *ibuf, int ibuflen,
223                      char *rbuf, int *rbuflen)
224 {
225     char *buf;
226     int len, i;
227
228     *rbuflen = 0;
229
230     /* grab some of the options */
231     if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, (void *) &buf,
232                              &i) < 0)
233       return AFPERR_PARAM;
234
235     len = (unsigned char) *ibuf++;
236     if ( len > i ) {
237         return( AFPERR_PARAM );
238     }
239
240     memcpy(buf, ibuf, len );
241     ibuf += len;
242     buf[ len ] = '\0';
243     if ((unsigned long) ibuf & 1) /* pad to even boundary */
244       ++ibuf;
245
246     if (( dhxpwd = uam_getname(buf, i)) == NULL ) {
247         return AFPERR_PARAM;
248     }
249
250     PAM_username = buf;
251     syslog( LOG_INFO, "dhx login: %s", buf);
252     return dhx_setup(obj, ibuf, ibuflen, rbuf, rbuflen);
253 }
254
255
256 static int pam_logincont(void *obj, struct passwd **uam_pwd,
257                          char *ibuf, int ibuflen, 
258                          char *rbuf, int *rbuflen)
259 {
260     char *hostname;
261     BIGNUM *bn1, *bn2, *bn3;
262     u_int16_t sessid;
263     int err, PAM_error;
264
265     *rbuflen = 0;
266
267     /* check for session id */
268     memcpy(&sessid, ibuf, sizeof(sessid));
269     if (sessid != dhxhash(obj))
270       return AFPERR_PARAM;
271     ibuf += sizeof(sessid);
272     
273     if (uam_afpserver_option(obj, UAM_OPTION_HOSTNAME,
274                              (void *) &hostname, NULL) < 0)
275       return AFPERR_MISC;
276
277     CAST_cbc_encrypt(ibuf, rbuf, CRYPT2BUFLEN, &castkey,
278                      msg3_iv, CAST_DECRYPT);
279     memset(&castkey, 0, sizeof(castkey));
280
281     /* check to make sure that the random number is the same. we
282      * get sent back an incremented random number. */
283     if (!(bn1 = BN_bin2bn(rbuf, KEYSIZE, NULL)))
284       return AFPERR_PARAM;
285
286     if (!(bn2 = BN_bin2bn(randbuf, sizeof(randbuf), NULL))) {
287       BN_free(bn1);
288       return AFPERR_PARAM;
289     }
290       
291     /* zero out the random number */
292     memset(rbuf, 0, sizeof(randbuf));
293     memset(randbuf, 0, sizeof(randbuf));
294     rbuf += KEYSIZE;
295
296     if (!(bn3 = BN_new())) {
297       BN_free(bn2);
298       BN_free(bn1);
299       return AFPERR_PARAM;
300     }
301
302     BN_sub(bn3, bn1, bn2);
303     BN_free(bn2);
304     BN_free(bn1);
305
306     /* okay. is it one more? */
307     if (!BN_is_one(bn3)) {
308       BN_free(bn3);
309       return AFPERR_PARAM;
310     }
311     BN_free(bn3);
312
313     /* Set these things up for the conv function */
314     rbuf[PASSWDLEN] = '\0';
315     PAM_password = rbuf;
316
317     err = AFPERR_NOTAUTH;
318     PAM_error = pam_start("netatalk", PAM_username, &PAM_conversation,
319                           &pamh);
320     if (PAM_error != PAM_SUCCESS)
321       goto logincont_err;
322
323     /* solaris craps out if PAM_TTY and PAM_RHOST aren't set. */
324     pam_set_item(pamh, PAM_TTY, "afpd");
325     pam_set_item(pamh, PAM_RHOST, hostname);
326     PAM_error = pam_authenticate(pamh,0);
327     if (PAM_error != PAM_SUCCESS) {
328       if (PAM_error == PAM_MAXTRIES) 
329         err = AFPERR_PWDEXPR;
330       goto logincont_err;
331     }      
332
333     PAM_error = pam_acct_mgmt(pamh, 0);
334     if (PAM_error != PAM_SUCCESS) {
335       if (PAM_error == PAM_ACCT_EXPIRED)
336         err = AFPERR_PWDEXPR;
337 #ifdef PAM_AUTHTOKEN_REQD
338       else if (PAM_error == PAM_AUTHTOKEN_REQD) 
339         err = AFPERR_PWDCHNG;
340 #endif
341       goto logincont_err;
342     }
343
344 #ifndef PAM_CRED_ESTABLISH
345 #define PAM_CRED_ESTABLISH PAM_ESTABLISH_CRED
346 #endif
347     PAM_error = pam_setcred(pamh, PAM_CRED_ESTABLISH);
348     if (PAM_error != PAM_SUCCESS)
349       goto logincont_err;
350
351     PAM_error = pam_open_session(pamh, 0);
352     if (PAM_error != PAM_SUCCESS)
353       goto logincont_err;
354
355     memset(rbuf, 0, PASSWDLEN); /* zero out the password */
356     *uam_pwd = dhxpwd;
357     return AFP_OK;
358
359 logincont_err:
360     pam_end(pamh, PAM_error);
361     pamh = NULL;
362     memset(rbuf, 0, CRYPT2BUFLEN);
363     return err;
364 }
365
366 /* logout */
367 static void pam_logout() {
368     pam_close_session(pamh, 0);
369     pam_end(pamh, 0);
370     pamh = NULL;
371 }
372
373
374 /* change pw for dhx needs a couple passes to get everything all
375  * right. basically, it's like the login/logincont sequence */
376 static int pam_changepw(void *obj, char *username,
377                         struct passwd *pwd, char *ibuf, int ibuflen,
378                         char *rbuf, int *rbuflen)
379 {
380     BIGNUM *bn1, *bn2, *bn3;
381
382     char *hostname;
383     pam_handle_t *lpamh;
384     uid_t uid;
385     u_int16_t sessid;
386     int PAM_error;
387
388     /* grab the id */
389     memcpy(&sessid, ibuf, sizeof(sessid));
390     ibuf += sizeof(sessid);
391     
392     if (!sessid) {  /* no sessid -> initialization phase */
393       PAM_username = username;
394       ibuflen -= sizeof(sessid);
395       return dhx_setup(obj, ibuf, ibuflen, rbuf, rbuflen);
396     }
397
398
399     /* otherwise, it's like logincont but different. */
400
401     /* check out the session id */
402     if (sessid != dhxhash(obj))
403       return AFPERR_PARAM;
404
405     /* we need this for pam */
406     if (uam_afpserver_option(obj, UAM_OPTION_HOSTNAME,
407                              (void *) &hostname, NULL) < 0)
408       return AFPERR_MISC;
409
410     /* grab the client's nonce, old password, and new password. */
411     CAST_cbc_encrypt(ibuf, ibuf, CHANGEPWBUFLEN, &castkey,
412                      msg3_iv, CAST_DECRYPT);
413     memset(&castkey, 0, sizeof(castkey));
414
415     /* check to make sure that the random number is the same. we
416      * get sent back an incremented random number. */
417     if (!(bn1 = BN_bin2bn(ibuf, KEYSIZE, NULL)))
418       return AFPERR_PARAM;
419
420     if (!(bn2 = BN_bin2bn(randbuf, sizeof(randbuf), NULL))) {
421       BN_free(bn1);
422       return AFPERR_PARAM;
423     }
424       
425     /* zero out the random number */
426     memset(rbuf, 0, sizeof(randbuf));
427     memset(randbuf, 0, sizeof(randbuf));
428
429     if (!(bn3 = BN_new())) {
430       BN_free(bn2);
431       BN_free(bn1);
432       return AFPERR_PARAM;
433     }
434
435     BN_sub(bn3, bn1, bn2);
436     BN_free(bn2);
437     BN_free(bn1);
438
439     /* okay. is it one more? */
440 #if 0
441     if (!BN_is_one(bn3)) {
442       BN_free(bn3);
443       return AFPERR_PARAM;
444     }
445 #endif
446     BN_free(bn3);
447
448     /* Set these things up for the conv function. the old password
449      * is at the end. */
450     ibuf += KEYSIZE;
451     ibuf[PASSWDLEN + PASSWDLEN] = '\0';
452     PAM_password = ibuf + PASSWDLEN;
453
454     PAM_error = pam_start("netatalk", username, &PAM_conversation,
455                           &lpamh);
456     if (PAM_error != PAM_SUCCESS) 
457       return AFPERR_PARAM;
458     pam_set_item(lpamh, PAM_TTY, "afpd");
459     pam_set_item(lpamh, PAM_RHOST, hostname);
460
461     /* we might need to do this as root */
462     uid = geteuid();
463     seteuid(0);
464     PAM_error = pam_authenticate(lpamh, 0);
465     if (PAM_error != PAM_SUCCESS) {
466       seteuid(uid);
467       pam_end(lpamh, PAM_error);
468       return AFPERR_NOTAUTH;
469     }
470
471     /* clear out old passwd */
472     memset(ibuf + PASSWDLEN, 0, PASSWDLEN);
473
474     /* new password */
475     PAM_password = ibuf;
476     ibuf[PASSWDLEN] = '\0';
477
478     /* this really does need to be done as root */
479     PAM_error = pam_chauthtok(lpamh, 0);
480     seteuid(uid); /* un-root ourselves. */
481     memset(ibuf, 0, PASSWDLEN);
482     if (PAM_error != PAM_SUCCESS) {
483       pam_end(lpamh, PAM_error);
484       return AFPERR_ACCESS;
485     }
486
487     pam_end(lpamh, 0);
488     return AFP_OK;
489 }
490
491
492 static int uam_setup(const char *path)
493 {
494   if (uam_register(UAM_SERVER_LOGIN, path, "DHCAST128", pam_login, 
495                    pam_logincont, pam_logout) < 0)
496     return -1;
497
498   if (uam_register(UAM_SERVER_CHANGEPW, path, "DHCAST128", 
499                    pam_changepw) < 0) {
500     uam_unregister(UAM_SERVER_LOGIN, "DHCAST128");
501     return -1;
502   }
503
504   /*uam_register(UAM_SERVER_PRINTAUTH, path, "DHCAST128",
505     pam_printer);*/
506
507   return 0;
508 }
509
510 static void uam_cleanup(void)
511 {
512   uam_unregister(UAM_SERVER_LOGIN, "DHCAST128");
513   uam_unregister(UAM_SERVER_CHANGEPW, "DHCAST128");
514   /*uam_unregister(UAM_SERVER_PRINTAUTH, "DHCAST128"); */
515 }
516
517 UAM_MODULE_EXPORT struct uam_export uams_dhx = {
518   UAM_MODULE_SERVER,
519   UAM_MODULE_VERSION,
520   uam_setup, uam_cleanup
521 };
522
523 #endif /* USE_PAM && UAM_DHX */