xref: /freebsd/lib/librpcsvc/secretkey.c (revision e0e0f30f42a026cacc7c79ebf176043fc8b17c3f)
147593e96SBill Paul /*
247593e96SBill Paul  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
347593e96SBill Paul  * unrestricted use provided that this legend is included on all tape
447593e96SBill Paul  * media and as a part of the software program in whole or part.  Users
547593e96SBill Paul  * may copy or modify Sun RPC without charge, but are not authorized
647593e96SBill Paul  * to license or distribute it to anyone else except as part of a product or
747593e96SBill Paul  * program developed by the user or with the express written consent of
847593e96SBill Paul  * Sun Microsystems, Inc.
947593e96SBill Paul  *
1047593e96SBill Paul  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
1147593e96SBill Paul  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
1247593e96SBill Paul  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
1347593e96SBill Paul  *
1447593e96SBill Paul  * Sun RPC is provided with no support and without any obligation on the
1547593e96SBill Paul  * part of Sun Microsystems, Inc. to assist in its use, correction,
1647593e96SBill Paul  * modification or enhancement.
1747593e96SBill Paul  *
1847593e96SBill Paul  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
1947593e96SBill Paul  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
2047593e96SBill Paul  * OR ANY PART THEREOF.
2147593e96SBill Paul  *
2247593e96SBill Paul  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
2347593e96SBill Paul  * or profits or other special, indirect and consequential damages, even if
2447593e96SBill Paul  * Sun has been advised of the possibility of such damages.
2547593e96SBill Paul  *
2647593e96SBill Paul  * Sun Microsystems, Inc.
2747593e96SBill Paul  * 2550 Garcia Avenue
2847593e96SBill Paul  * Mountain View, California  94043
2947593e96SBill Paul  */
30542d87feSMatthew Dillon 
31542d87feSMatthew Dillon #include <sys/cdefs.h>
32542d87feSMatthew Dillon __FBSDID("$FreeBSD$");
33542d87feSMatthew Dillon 
3447593e96SBill Paul #if !defined(lint) && defined(SCCSIDS)
3547593e96SBill Paul static char sccsid[] = "@(#)secretkey.c 1.8 91/03/11 Copyr 1986 Sun Micro";
3647593e96SBill Paul #endif
3747593e96SBill Paul 
3847593e96SBill Paul /*
3947593e96SBill Paul  * secretkey.c
4047593e96SBill Paul  * Copyright (C) 1986, Sun Microsystems, Inc.
4147593e96SBill Paul  */
4247593e96SBill Paul 
4347593e96SBill Paul /*
4447593e96SBill Paul  * Secret key lookup routines
4547593e96SBill Paul  */
4647593e96SBill Paul #include <stdio.h>
4747593e96SBill Paul #include <pwd.h>
4847593e96SBill Paul #include <rpc/rpc.h>
4947593e96SBill Paul #include <rpc/key_prot.h>
5047593e96SBill Paul #include <rpcsvc/yp_prot.h>
5147593e96SBill Paul #include <rpcsvc/ypclnt.h>
5247593e96SBill Paul #include <string.h>
5347593e96SBill Paul 
5469160b1eSDavid E. O'Brien extern int xdecrypt( char *, char * );
5547593e96SBill Paul 
5647593e96SBill Paul /*
5747593e96SBill Paul  * Get somebody's encrypted secret key from the database, using the given
5847593e96SBill Paul  * passwd to decrypt it.
5947593e96SBill Paul  */
6047593e96SBill Paul int
61e0e0f30fSEd Schouten getsecretkey(char *netname, char *secretkey, char *passwd)
6247593e96SBill Paul {
6347593e96SBill Paul 	char lookup[3 * HEXKEYBYTES];
6447593e96SBill Paul 	char *p;
6547593e96SBill Paul 
6647593e96SBill Paul 	if (secretkey == NULL)
6747593e96SBill Paul 		return (0);
6847593e96SBill Paul 	if (!getpublicandprivatekey(netname, lookup))
6947593e96SBill Paul 		return (0);
7047593e96SBill Paul 	p = strchr(lookup, ':');
7147593e96SBill Paul 	if (p == NULL) {
7247593e96SBill Paul 		return (0);
7347593e96SBill Paul 	}
7447593e96SBill Paul 	p++;
7547593e96SBill Paul 	if (!xdecrypt(p, passwd)) {
7647593e96SBill Paul 		return (0);
7747593e96SBill Paul 	}
7847593e96SBill Paul 	if (memcmp(p, p + HEXKEYBYTES, KEYCHECKSUMSIZE) != 0) {
7947593e96SBill Paul 		secretkey[0] = '\0';
8047593e96SBill Paul 		return (1);
8147593e96SBill Paul 	}
8247593e96SBill Paul 	p[HEXKEYBYTES] = '\0';
8347593e96SBill Paul 	(void) strncpy(secretkey, p, HEXKEYBYTES);
8447593e96SBill Paul 	secretkey[HEXKEYBYTES] = '\0';
8547593e96SBill Paul 	return (1);
8647593e96SBill Paul }
87