1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate /* 30*7c478bd9Sstevel@tonic-gate * 31*7c478bd9Sstevel@tonic-gate * gsscred utility 32*7c478bd9Sstevel@tonic-gate * Manages mapping between a security principal name and unix uid 33*7c478bd9Sstevel@tonic-gate */ 34*7c478bd9Sstevel@tonic-gate 35*7c478bd9Sstevel@tonic-gate #include <stdio.h> 36*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 37*7c478bd9Sstevel@tonic-gate #include <string.h> 38*7c478bd9Sstevel@tonic-gate #include <errno.h> 39*7c478bd9Sstevel@tonic-gate #include <ctype.h> 40*7c478bd9Sstevel@tonic-gate #include "gsscred.h" 41*7c478bd9Sstevel@tonic-gate 42*7c478bd9Sstevel@tonic-gate /* From g_glue.c */ 43*7c478bd9Sstevel@tonic-gate 44*7c478bd9Sstevel@tonic-gate extern int 45*7c478bd9Sstevel@tonic-gate get_der_length(unsigned char **, unsigned int, unsigned int *); 46*7c478bd9Sstevel@tonic-gate 47*7c478bd9Sstevel@tonic-gate extern unsigned int 48*7c478bd9Sstevel@tonic-gate der_length_size(unsigned int); 49*7c478bd9Sstevel@tonic-gate 50*7c478bd9Sstevel@tonic-gate extern int 51*7c478bd9Sstevel@tonic-gate put_der_length(unsigned int, unsigned char **, unsigned int); 52*7c478bd9Sstevel@tonic-gate 53*7c478bd9Sstevel@tonic-gate 54*7c478bd9Sstevel@tonic-gate 55*7c478bd9Sstevel@tonic-gate /* 56*7c478bd9Sstevel@tonic-gate * GSS export name constants 57*7c478bd9Sstevel@tonic-gate */ 58*7c478bd9Sstevel@tonic-gate static const char *expNameTokId = "\x04\x01"; 59*7c478bd9Sstevel@tonic-gate static const int expNameTokIdLen = 2; 60*7c478bd9Sstevel@tonic-gate static const int mechOidLenLen = 2; 61*7c478bd9Sstevel@tonic-gate static const int mechOidTagLen = 1; 62*7c478bd9Sstevel@tonic-gate 63*7c478bd9Sstevel@tonic-gate 64*7c478bd9Sstevel@tonic-gate /* 65*7c478bd9Sstevel@tonic-gate * Internal utility routines. 66*7c478bd9Sstevel@tonic-gate */ 67*7c478bd9Sstevel@tonic-gate 68*7c478bd9Sstevel@tonic-gate /* 69*7c478bd9Sstevel@tonic-gate * gsscred_read_config_file 70*7c478bd9Sstevel@tonic-gate * 71*7c478bd9Sstevel@tonic-gate * function to read the optional gsscred configuration file 72*7c478bd9Sstevel@tonic-gate * which specifies which backend to use to store the gsscred 73*7c478bd9Sstevel@tonic-gate * table. 74*7c478bd9Sstevel@tonic-gate * 75*7c478bd9Sstevel@tonic-gate * we now only support flat files (btw, this file for backend is Obsoleted 76*7c478bd9Sstevel@tonic-gate * by PSARC) 77*7c478bd9Sstevel@tonic-gate */ 78*7c478bd9Sstevel@tonic-gate int 79*7c478bd9Sstevel@tonic-gate gsscred_read_config_file(void) 80*7c478bd9Sstevel@tonic-gate { 81*7c478bd9Sstevel@tonic-gate return (GSSCRED_FLAT_FILE); 82*7c478bd9Sstevel@tonic-gate } /* gsscred_read_config_file */ 83*7c478bd9Sstevel@tonic-gate 84*7c478bd9Sstevel@tonic-gate 85*7c478bd9Sstevel@tonic-gate /* 86*7c478bd9Sstevel@tonic-gate * gsscred_MakeName 87*7c478bd9Sstevel@tonic-gate * 88*7c478bd9Sstevel@tonic-gate * construct a principal name in the GSS_C_NT_EXPORT_NAME format. 89*7c478bd9Sstevel@tonic-gate */ 90*7c478bd9Sstevel@tonic-gate int gsscred_MakeName(const gss_OID mechOid, const char *name, 91*7c478bd9Sstevel@tonic-gate const char *nameOidStr, gss_buffer_t nameOut) 92*7c478bd9Sstevel@tonic-gate { 93*7c478bd9Sstevel@tonic-gate gss_OID nameOid; 94*7c478bd9Sstevel@tonic-gate gss_name_t intName; 95*7c478bd9Sstevel@tonic-gate OM_uint32 minor, major; 96*7c478bd9Sstevel@tonic-gate gss_buffer_desc aName = GSS_C_EMPTY_BUFFER, oidStr; 97*7c478bd9Sstevel@tonic-gate 98*7c478bd9Sstevel@tonic-gate nameOut->length = 0; 99*7c478bd9Sstevel@tonic-gate nameOut->value = NULL; 100*7c478bd9Sstevel@tonic-gate 101*7c478bd9Sstevel@tonic-gate /* we need to import the name, then canonicalize it, then export it */ 102*7c478bd9Sstevel@tonic-gate if (nameOidStr == NULL) 103*7c478bd9Sstevel@tonic-gate nameOid = (gss_OID)GSS_C_NT_USER_NAME; 104*7c478bd9Sstevel@tonic-gate else { 105*7c478bd9Sstevel@tonic-gate oidStr.length = strlen(nameOidStr); 106*7c478bd9Sstevel@tonic-gate oidStr.value = (void *)nameOidStr; 107*7c478bd9Sstevel@tonic-gate if (gss_str_to_oid(&minor, &oidStr, &nameOid) != 108*7c478bd9Sstevel@tonic-gate GSS_S_COMPLETE) { 109*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 110*7c478bd9Sstevel@tonic-gate gettext("\nInvalid name oid supplied [%s].\n"), 111*7c478bd9Sstevel@tonic-gate nameOidStr); 112*7c478bd9Sstevel@tonic-gate return (0); 113*7c478bd9Sstevel@tonic-gate } 114*7c478bd9Sstevel@tonic-gate } 115*7c478bd9Sstevel@tonic-gate 116*7c478bd9Sstevel@tonic-gate /* first import the name */ 117*7c478bd9Sstevel@tonic-gate aName.length = strlen(name); 118*7c478bd9Sstevel@tonic-gate aName.value = (void*)name; 119*7c478bd9Sstevel@tonic-gate major = gss_import_name(&minor, &aName, nameOid, &intName); 120*7c478bd9Sstevel@tonic-gate if (nameOidStr != NULL) { 121*7c478bd9Sstevel@tonic-gate free(nameOid->elements); 122*7c478bd9Sstevel@tonic-gate free(nameOid); 123*7c478bd9Sstevel@tonic-gate } 124*7c478bd9Sstevel@tonic-gate 125*7c478bd9Sstevel@tonic-gate if (major != GSS_S_COMPLETE) { 126*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 127*7c478bd9Sstevel@tonic-gate gettext("\nInternal error importing name [%s].\n"), 128*7c478bd9Sstevel@tonic-gate name); 129*7c478bd9Sstevel@tonic-gate return (0); 130*7c478bd9Sstevel@tonic-gate } 131*7c478bd9Sstevel@tonic-gate 132*7c478bd9Sstevel@tonic-gate /* now canonicalize the name */ 133*7c478bd9Sstevel@tonic-gate if (gss_canonicalize_name(&minor, intName, mechOid, NULL) 134*7c478bd9Sstevel@tonic-gate != GSS_S_COMPLETE) { 135*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 136*7c478bd9Sstevel@tonic-gate gettext("\nInternal error canonicalizing name" 137*7c478bd9Sstevel@tonic-gate " [%s].\n"), 138*7c478bd9Sstevel@tonic-gate name); 139*7c478bd9Sstevel@tonic-gate (void) gss_release_name(&minor, &intName); 140*7c478bd9Sstevel@tonic-gate return (0); 141*7c478bd9Sstevel@tonic-gate } 142*7c478bd9Sstevel@tonic-gate 143*7c478bd9Sstevel@tonic-gate /* now convert to export format */ 144*7c478bd9Sstevel@tonic-gate if (gss_export_name(&minor, intName, nameOut) != GSS_S_COMPLETE) { 145*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 146*7c478bd9Sstevel@tonic-gate gettext("\nInternal error exporting name [%s].\n"), 147*7c478bd9Sstevel@tonic-gate name); 148*7c478bd9Sstevel@tonic-gate (void) gss_release_name(&minor, &intName); 149*7c478bd9Sstevel@tonic-gate return (0); 150*7c478bd9Sstevel@tonic-gate } 151*7c478bd9Sstevel@tonic-gate 152*7c478bd9Sstevel@tonic-gate (void) gss_release_name(&minor, &intName); 153*7c478bd9Sstevel@tonic-gate return (1); 154*7c478bd9Sstevel@tonic-gate } /* ******* makeName ****** */ 155*7c478bd9Sstevel@tonic-gate 156*7c478bd9Sstevel@tonic-gate 157*7c478bd9Sstevel@tonic-gate /* 158*7c478bd9Sstevel@tonic-gate * Constructs a part of the GSS_NT_EXPORT_NAME 159*7c478bd9Sstevel@tonic-gate * Only the mechanism independent name part is created. 160*7c478bd9Sstevel@tonic-gate */ 161*7c478bd9Sstevel@tonic-gate int 162*7c478bd9Sstevel@tonic-gate gsscred_MakeNameHeader(const gss_OID mechOid, gss_buffer_t outNameHdr) 163*7c478bd9Sstevel@tonic-gate { 164*7c478bd9Sstevel@tonic-gate unsigned char *buf = NULL; 165*7c478bd9Sstevel@tonic-gate int mechOidDERLength, mechOidLength; 166*7c478bd9Sstevel@tonic-gate 167*7c478bd9Sstevel@tonic-gate /* determine the length of buffer needed */ 168*7c478bd9Sstevel@tonic-gate mechOidDERLength = der_length_size(mechOid->length); 169*7c478bd9Sstevel@tonic-gate outNameHdr->length = mechOidLenLen + mechOidTagLen + 170*7c478bd9Sstevel@tonic-gate mechOidDERLength + expNameTokIdLen + mechOid->length; 171*7c478bd9Sstevel@tonic-gate if ((outNameHdr->value = (void*)malloc(outNameHdr->length)) == NULL) { 172*7c478bd9Sstevel@tonic-gate outNameHdr->length = 0; 173*7c478bd9Sstevel@tonic-gate return (0); 174*7c478bd9Sstevel@tonic-gate } 175*7c478bd9Sstevel@tonic-gate 176*7c478bd9Sstevel@tonic-gate /* start by putting the token id */ 177*7c478bd9Sstevel@tonic-gate buf = (unsigned char *) outNameHdr->value; 178*7c478bd9Sstevel@tonic-gate (void) memset(outNameHdr->value, '\0', outNameHdr->length); 179*7c478bd9Sstevel@tonic-gate (void) memcpy(buf, expNameTokId, expNameTokIdLen); 180*7c478bd9Sstevel@tonic-gate buf += expNameTokIdLen; 181*7c478bd9Sstevel@tonic-gate 182*7c478bd9Sstevel@tonic-gate /* 183*7c478bd9Sstevel@tonic-gate * next 2 bytes contain the mech oid length (includes 184*7c478bd9Sstevel@tonic-gate * DER encoding) 185*7c478bd9Sstevel@tonic-gate */ 186*7c478bd9Sstevel@tonic-gate mechOidLength = mechOidTagLen + mechOidDERLength + 187*7c478bd9Sstevel@tonic-gate mechOid->length; 188*7c478bd9Sstevel@tonic-gate 189*7c478bd9Sstevel@tonic-gate *buf++ = (mechOidLength & 0xFF00) >> 8; 190*7c478bd9Sstevel@tonic-gate *buf++ = (mechOidLength & 0x00FF); 191*7c478bd9Sstevel@tonic-gate *buf++ = 0x06; 192*7c478bd9Sstevel@tonic-gate if (put_der_length(mechOid->length, &buf, 193*7c478bd9Sstevel@tonic-gate mechOidDERLength) != 0) { 194*7c478bd9Sstevel@tonic-gate /* free the buffer */ 195*7c478bd9Sstevel@tonic-gate free(outNameHdr->value); 196*7c478bd9Sstevel@tonic-gate return (0); 197*7c478bd9Sstevel@tonic-gate } 198*7c478bd9Sstevel@tonic-gate 199*7c478bd9Sstevel@tonic-gate /* now add the mechanism oid */ 200*7c478bd9Sstevel@tonic-gate (void) memcpy(buf, mechOid->elements, mechOid->length); 201*7c478bd9Sstevel@tonic-gate 202*7c478bd9Sstevel@tonic-gate /* we stop here because the rest is mechanism specific */ 203*7c478bd9Sstevel@tonic-gate return (1); 204*7c478bd9Sstevel@tonic-gate } /* gsscred_MakeNameHeader */ 205*7c478bd9Sstevel@tonic-gate 206*7c478bd9Sstevel@tonic-gate 207*7c478bd9Sstevel@tonic-gate /* 208*7c478bd9Sstevel@tonic-gate * Converts the supplied string to HEX. 209*7c478bd9Sstevel@tonic-gate * The passed in buffer must be twice as long as the input buffer. 210*7c478bd9Sstevel@tonic-gate * Long form is used (i.e. '\0' will become '00'). This is needed 211*7c478bd9Sstevel@tonic-gate * to enable proper re-parsing of names. 212*7c478bd9Sstevel@tonic-gate */ 213*7c478bd9Sstevel@tonic-gate int 214*7c478bd9Sstevel@tonic-gate gsscred_AsHex(gss_buffer_t dataIn, gss_buffer_t dataOut) 215*7c478bd9Sstevel@tonic-gate { 216*7c478bd9Sstevel@tonic-gate int i; 217*7c478bd9Sstevel@tonic-gate char *out, *in; 218*7c478bd9Sstevel@tonic-gate unsigned int tmp; 219*7c478bd9Sstevel@tonic-gate 220*7c478bd9Sstevel@tonic-gate if (dataOut->length < ((dataIn->length *2) + 1)) 221*7c478bd9Sstevel@tonic-gate return (0); 222*7c478bd9Sstevel@tonic-gate 223*7c478bd9Sstevel@tonic-gate out = (char *)dataOut->value; 224*7c478bd9Sstevel@tonic-gate in = (char *)dataIn->value; 225*7c478bd9Sstevel@tonic-gate dataOut->length = 0; 226*7c478bd9Sstevel@tonic-gate 227*7c478bd9Sstevel@tonic-gate for (i = 0; i < dataIn->length; i++) { 228*7c478bd9Sstevel@tonic-gate tmp = (unsigned int)(*in++)&0xff; 229*7c478bd9Sstevel@tonic-gate (void) sprintf(out, "%02X", tmp); 230*7c478bd9Sstevel@tonic-gate out++; 231*7c478bd9Sstevel@tonic-gate out++; 232*7c478bd9Sstevel@tonic-gate } 233*7c478bd9Sstevel@tonic-gate dataOut->length = out - (char *)dataOut->value; 234*7c478bd9Sstevel@tonic-gate *out = '\0'; 235*7c478bd9Sstevel@tonic-gate 236*7c478bd9Sstevel@tonic-gate return (1); 237*7c478bd9Sstevel@tonic-gate } /* ******* gsscred_AsHex ******* */ 238*7c478bd9Sstevel@tonic-gate 239*7c478bd9Sstevel@tonic-gate 240*7c478bd9Sstevel@tonic-gate /* 241*7c478bd9Sstevel@tonic-gate * GSS entry point for retrieving user uid mappings. 242*7c478bd9Sstevel@tonic-gate * The name buffer contains a principal name in exported format. 243*7c478bd9Sstevel@tonic-gate */ 244*7c478bd9Sstevel@tonic-gate int 245*7c478bd9Sstevel@tonic-gate gss_getGssCredEntry(const gss_buffer_t expName, uid_t *uid) 246*7c478bd9Sstevel@tonic-gate { 247*7c478bd9Sstevel@tonic-gate int tableSource; 248*7c478bd9Sstevel@tonic-gate unsigned char *buf; 249*7c478bd9Sstevel@tonic-gate gss_buffer_desc mechOidDesc = GSS_C_EMPTY_BUFFER, 250*7c478bd9Sstevel@tonic-gate mechHexOidDesc = GSS_C_EMPTY_BUFFER, 251*7c478bd9Sstevel@tonic-gate expNameHexDesc = GSS_C_EMPTY_BUFFER; 252*7c478bd9Sstevel@tonic-gate char oidHexBuf[256], expNameHexBuf[1024]; 253*7c478bd9Sstevel@tonic-gate unsigned int dummy; 254*7c478bd9Sstevel@tonic-gate int len; 255*7c478bd9Sstevel@tonic-gate 256*7c478bd9Sstevel@tonic-gate tableSource = gsscred_read_config_file(); 257*7c478bd9Sstevel@tonic-gate 258*7c478bd9Sstevel@tonic-gate /* 259*7c478bd9Sstevel@tonic-gate * for xfn (ldap?), we must first construct, a hex mechansim oid string 260*7c478bd9Sstevel@tonic-gate */ 261*7c478bd9Sstevel@tonic-gate if (expName->length < (expNameTokIdLen + mechOidLenLen + 262*7c478bd9Sstevel@tonic-gate mechOidTagLen)) 263*7c478bd9Sstevel@tonic-gate return (0); 264*7c478bd9Sstevel@tonic-gate 265*7c478bd9Sstevel@tonic-gate buf = (unsigned char *)expName->value; 266*7c478bd9Sstevel@tonic-gate buf += expNameTokIdLen; 267*7c478bd9Sstevel@tonic-gate 268*7c478bd9Sstevel@tonic-gate /* skip oid length - get to der */ 269*7c478bd9Sstevel@tonic-gate buf++; 270*7c478bd9Sstevel@tonic-gate buf++; 271*7c478bd9Sstevel@tonic-gate 272*7c478bd9Sstevel@tonic-gate /* skip oid tag */ 273*7c478bd9Sstevel@tonic-gate buf++; 274*7c478bd9Sstevel@tonic-gate 275*7c478bd9Sstevel@tonic-gate /* get oid length */ 276*7c478bd9Sstevel@tonic-gate len = get_der_length(&buf, 277*7c478bd9Sstevel@tonic-gate (expName->length - expNameTokIdLen 278*7c478bd9Sstevel@tonic-gate - mechOidLenLen - mechOidTagLen), &dummy); 279*7c478bd9Sstevel@tonic-gate if (len == -1) 280*7c478bd9Sstevel@tonic-gate return (0); 281*7c478bd9Sstevel@tonic-gate else 282*7c478bd9Sstevel@tonic-gate mechOidDesc.length = len; 283*7c478bd9Sstevel@tonic-gate 284*7c478bd9Sstevel@tonic-gate if (expName->length < 285*7c478bd9Sstevel@tonic-gate (expNameTokIdLen + mechOidLenLen + mechOidDesc.length 286*7c478bd9Sstevel@tonic-gate + dummy+ mechOidTagLen)) 287*7c478bd9Sstevel@tonic-gate return (0); 288*7c478bd9Sstevel@tonic-gate 289*7c478bd9Sstevel@tonic-gate mechOidDesc.value = (void *)buf; 290*7c478bd9Sstevel@tonic-gate 291*7c478bd9Sstevel@tonic-gate /* convert the oid buffer to hex */ 292*7c478bd9Sstevel@tonic-gate mechHexOidDesc.value = (void*) oidHexBuf; 293*7c478bd9Sstevel@tonic-gate mechHexOidDesc.length = sizeof (oidHexBuf); 294*7c478bd9Sstevel@tonic-gate if (!gsscred_AsHex(&mechOidDesc, &mechHexOidDesc)) 295*7c478bd9Sstevel@tonic-gate return (0); 296*7c478bd9Sstevel@tonic-gate 297*7c478bd9Sstevel@tonic-gate /* also need to convert the name buffer into hex */ 298*7c478bd9Sstevel@tonic-gate expNameHexDesc.value = expNameHexBuf; 299*7c478bd9Sstevel@tonic-gate expNameHexDesc.length = sizeof (expNameHexBuf); 300*7c478bd9Sstevel@tonic-gate if (!gsscred_AsHex(expName, &expNameHexDesc)) 301*7c478bd9Sstevel@tonic-gate return (0); 302*7c478bd9Sstevel@tonic-gate 303*7c478bd9Sstevel@tonic-gate if (tableSource == GSSCRED_FLAT_FILE) 304*7c478bd9Sstevel@tonic-gate return (file_getGssCredUid(&expNameHexDesc, uid)); 305*7c478bd9Sstevel@tonic-gate 306*7c478bd9Sstevel@tonic-gate return (0); /* XXX for new backends (ldap, dss), 0->1 probably */ 307*7c478bd9Sstevel@tonic-gate } /* gss_getGssCredEntry */ 308