1*2264Sjacobs /* 2*2264Sjacobs * CDDL HEADER START 3*2264Sjacobs * 4*2264Sjacobs * The contents of this file are subject to the terms of the 5*2264Sjacobs * Common Development and Distribution License (the "License"). 6*2264Sjacobs * You may not use this file except in compliance with the License. 7*2264Sjacobs * 8*2264Sjacobs * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9*2264Sjacobs * or http://www.opensolaris.org/os/licensing. 10*2264Sjacobs * See the License for the specific language governing permissions 11*2264Sjacobs * and limitations under the License. 12*2264Sjacobs * 13*2264Sjacobs * When distributing Covered Code, include this CDDL HEADER in each 14*2264Sjacobs * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15*2264Sjacobs * If applicable, add the following below this CDDL HEADER, with the 16*2264Sjacobs * fields enclosed by brackets "[]" replaced with your own identifying 17*2264Sjacobs * information: Portions Copyright [yyyy] [name of copyright owner] 18*2264Sjacobs * 19*2264Sjacobs * CDDL HEADER END 20*2264Sjacobs */ 21*2264Sjacobs 22*2264Sjacobs /* 23*2264Sjacobs * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 24*2264Sjacobs * Use is subject to license terms. 25*2264Sjacobs * 26*2264Sjacobs */ 27*2264Sjacobs 28*2264Sjacobs /* $Id: service.c 163 2006-05-09 15:07:45Z njacobs $ */ 29*2264Sjacobs 30*2264Sjacobs #pragma ident "%Z%%M% %I% %E% SMI" 31*2264Sjacobs 32*2264Sjacobs #include <stdlib.h> 33*2264Sjacobs #include <stdio.h> 34*2264Sjacobs #include <string.h> 35*2264Sjacobs #include <stdarg.h> 36*2264Sjacobs #include <alloca.h> 37*2264Sjacobs #include <uri.h> 38*2264Sjacobs #include <papi_impl.h> 39*2264Sjacobs 40*2264Sjacobs papi_status_t 41*2264Sjacobs service_fill_in(service_t *svc, char *name) 42*2264Sjacobs { 43*2264Sjacobs papi_status_t status = PAPI_OK; 44*2264Sjacobs uri_t *uri = NULL; 45*2264Sjacobs 46*2264Sjacobs if (svc == NULL) 47*2264Sjacobs return (PAPI_BAD_ARGUMENT); 48*2264Sjacobs 49*2264Sjacobs if (name == NULL) 50*2264Sjacobs return (PAPI_OK); 51*2264Sjacobs 52*2264Sjacobs /* 53*2264Sjacobs * valid URIs are in the form: 54*2264Sjacobs * lpd://server[:port]/.../queue[#extensions] 55*2264Sjacobs * rfc-1179://server[:port]/.../queue[#extensions] 56*2264Sjacobs * any authentication information supplied the URI is ignored. 57*2264Sjacobs */ 58*2264Sjacobs if (uri_from_string((char *)name, &uri) != -1) { 59*2264Sjacobs if ((strcasecmp(uri->scheme, "lpd") == 0) || 60*2264Sjacobs (strcasecmp(uri->scheme, "rfc-1179") == 0)) { 61*2264Sjacobs if (svc->uri != NULL) 62*2264Sjacobs uri_free(svc->uri); 63*2264Sjacobs svc->uri = uri; 64*2264Sjacobs } else { 65*2264Sjacobs uri_free(uri); 66*2264Sjacobs status = PAPI_URI_SCHEME; 67*2264Sjacobs } 68*2264Sjacobs } 69*2264Sjacobs 70*2264Sjacobs return (status); 71*2264Sjacobs } 72*2264Sjacobs 73*2264Sjacobs papi_status_t 74*2264Sjacobs papiServiceCreate(papi_service_t *handle, char *service_name, 75*2264Sjacobs char *user_name, char *password, 76*2264Sjacobs int (*authCB)(papi_service_t svc, void *app_data), 77*2264Sjacobs papi_encryption_t encryption, void *app_data) 78*2264Sjacobs { 79*2264Sjacobs papi_status_t status; 80*2264Sjacobs service_t *svc = NULL; 81*2264Sjacobs 82*2264Sjacobs if (handle == NULL) 83*2264Sjacobs return (PAPI_BAD_ARGUMENT); 84*2264Sjacobs 85*2264Sjacobs if ((*handle = svc = (service_t *)calloc(1, sizeof (*svc))) == NULL) 86*2264Sjacobs return (PAPI_TEMPORARY_ERROR); 87*2264Sjacobs 88*2264Sjacobs if (service_name != NULL) 89*2264Sjacobs papiAttributeListAddString(&svc->attributes, PAPI_ATTR_EXCL, 90*2264Sjacobs "service-name", service_name); 91*2264Sjacobs 92*2264Sjacobs (void) papiServiceSetUserName(svc, user_name); 93*2264Sjacobs (void) papiServiceSetPassword(svc, password); 94*2264Sjacobs (void) papiServiceSetAuthCB(svc, authCB); 95*2264Sjacobs (void) papiServiceSetAppData(svc, app_data); 96*2264Sjacobs (void) papiServiceSetEncryption(svc, encryption); 97*2264Sjacobs 98*2264Sjacobs status = service_fill_in(svc, service_name); 99*2264Sjacobs 100*2264Sjacobs return (status); 101*2264Sjacobs } 102*2264Sjacobs 103*2264Sjacobs void 104*2264Sjacobs papiServiceDestroy(papi_service_t handle) 105*2264Sjacobs { 106*2264Sjacobs if (handle != NULL) { 107*2264Sjacobs service_t *svc = handle; 108*2264Sjacobs 109*2264Sjacobs #ifdef DEADBEEF 110*2264Sjacobs if (svc->cache != NULL) 111*2264Sjacobs cache_free(svc->cache); 112*2264Sjacobs #endif 113*2264Sjacobs if (svc->uri != NULL) 114*2264Sjacobs uri_free(svc->uri); 115*2264Sjacobs if (svc->attributes != NULL) 116*2264Sjacobs papiAttributeListFree(svc->attributes); 117*2264Sjacobs free(svc); 118*2264Sjacobs } 119*2264Sjacobs } 120*2264Sjacobs 121*2264Sjacobs papi_status_t 122*2264Sjacobs papiServiceSetUserName(papi_service_t handle, char *user_name) 123*2264Sjacobs { 124*2264Sjacobs service_t *svc = handle; 125*2264Sjacobs 126*2264Sjacobs if (svc == NULL) 127*2264Sjacobs return (PAPI_BAD_ARGUMENT); 128*2264Sjacobs 129*2264Sjacobs return (papiAttributeListAddString(&svc->attributes, PAPI_ATTR_REPLACE, 130*2264Sjacobs "user-name", user_name)); 131*2264Sjacobs } 132*2264Sjacobs 133*2264Sjacobs papi_status_t 134*2264Sjacobs papiServiceSetPassword(papi_service_t handle, char *password) 135*2264Sjacobs { 136*2264Sjacobs service_t *svc = handle; 137*2264Sjacobs 138*2264Sjacobs if (svc == NULL) 139*2264Sjacobs return (PAPI_BAD_ARGUMENT); 140*2264Sjacobs 141*2264Sjacobs return (papiAttributeListAddString(&svc->attributes, 142*2264Sjacobs PAPI_ATTR_REPLACE, "password", password)); 143*2264Sjacobs } 144*2264Sjacobs 145*2264Sjacobs papi_status_t 146*2264Sjacobs papiServiceSetEncryption(papi_service_t handle, 147*2264Sjacobs papi_encryption_t encryption) 148*2264Sjacobs { 149*2264Sjacobs service_t *svc = handle; 150*2264Sjacobs 151*2264Sjacobs if (svc == NULL) 152*2264Sjacobs return (PAPI_BAD_ARGUMENT); 153*2264Sjacobs 154*2264Sjacobs return (papiAttributeListAddInteger(&svc->attributes, PAPI_ATTR_REPLACE, 155*2264Sjacobs "encryption", (int)encryption)); 156*2264Sjacobs } 157*2264Sjacobs 158*2264Sjacobs papi_status_t 159*2264Sjacobs papiServiceSetAuthCB(papi_service_t handle, 160*2264Sjacobs int (*authCB)(papi_service_t svc, void *app_data)) 161*2264Sjacobs { 162*2264Sjacobs service_t *svc = handle; 163*2264Sjacobs 164*2264Sjacobs if (svc == NULL) 165*2264Sjacobs return (PAPI_BAD_ARGUMENT); 166*2264Sjacobs 167*2264Sjacobs svc->authCB = (int (*)(papi_service_t svc, void *))authCB; 168*2264Sjacobs 169*2264Sjacobs return (PAPI_OK); 170*2264Sjacobs } 171*2264Sjacobs 172*2264Sjacobs papi_status_t 173*2264Sjacobs papiServiceSetAppData(papi_service_t handle, void *app_data) 174*2264Sjacobs { 175*2264Sjacobs service_t *svc = handle; 176*2264Sjacobs 177*2264Sjacobs if (svc == NULL) 178*2264Sjacobs return (PAPI_BAD_ARGUMENT); 179*2264Sjacobs 180*2264Sjacobs svc->app_data = (void *)app_data; 181*2264Sjacobs 182*2264Sjacobs return (PAPI_OK); 183*2264Sjacobs } 184*2264Sjacobs 185*2264Sjacobs char * 186*2264Sjacobs papiServiceGetServiceName(papi_service_t handle) 187*2264Sjacobs { 188*2264Sjacobs service_t *svc = handle; 189*2264Sjacobs char *result = NULL; 190*2264Sjacobs 191*2264Sjacobs if (svc != NULL) 192*2264Sjacobs papiAttributeListGetString(svc->attributes, NULL, 193*2264Sjacobs "service-name", &result); 194*2264Sjacobs 195*2264Sjacobs return (result); 196*2264Sjacobs } 197*2264Sjacobs 198*2264Sjacobs char * 199*2264Sjacobs papiServiceGetUserName(papi_service_t handle) 200*2264Sjacobs { 201*2264Sjacobs service_t *svc = handle; 202*2264Sjacobs char *result = NULL; 203*2264Sjacobs 204*2264Sjacobs if (svc != NULL) 205*2264Sjacobs papiAttributeListGetString(svc->attributes, NULL, 206*2264Sjacobs "user-name", &result); 207*2264Sjacobs 208*2264Sjacobs return (result); 209*2264Sjacobs 210*2264Sjacobs } 211*2264Sjacobs 212*2264Sjacobs char * 213*2264Sjacobs papiServiceGetPassword(papi_service_t handle) 214*2264Sjacobs { 215*2264Sjacobs service_t *svc = handle; 216*2264Sjacobs char *result = NULL; 217*2264Sjacobs 218*2264Sjacobs if (svc != NULL) 219*2264Sjacobs papiAttributeListGetString(svc->attributes, NULL, 220*2264Sjacobs "password", &result); 221*2264Sjacobs 222*2264Sjacobs return (result); 223*2264Sjacobs } 224*2264Sjacobs 225*2264Sjacobs papi_encryption_t 226*2264Sjacobs papiServiceGetEncryption(papi_service_t handle) 227*2264Sjacobs { 228*2264Sjacobs service_t *svc = handle; 229*2264Sjacobs papi_encryption_t result = PAPI_ENCRYPT_NEVER; 230*2264Sjacobs 231*2264Sjacobs if (svc != NULL) 232*2264Sjacobs papiAttributeListGetInteger(svc->attributes, NULL, 233*2264Sjacobs "encryption", (int *)&result); 234*2264Sjacobs 235*2264Sjacobs return (result); 236*2264Sjacobs } 237*2264Sjacobs 238*2264Sjacobs void * 239*2264Sjacobs papiServiceGetAppData(papi_service_t handle) 240*2264Sjacobs { 241*2264Sjacobs service_t *svc = handle; 242*2264Sjacobs void *result = NULL; 243*2264Sjacobs 244*2264Sjacobs if (svc != NULL) { 245*2264Sjacobs result = svc->app_data; 246*2264Sjacobs } 247*2264Sjacobs 248*2264Sjacobs return (result); 249*2264Sjacobs 250*2264Sjacobs } 251*2264Sjacobs 252*2264Sjacobs papi_attribute_t ** 253*2264Sjacobs papiServiceGetAttributeList(papi_service_t handle) 254*2264Sjacobs { 255*2264Sjacobs service_t *svc = handle; 256*2264Sjacobs papi_attribute_t **result = NULL; 257*2264Sjacobs 258*2264Sjacobs if (svc != NULL) 259*2264Sjacobs result = svc->attributes; 260*2264Sjacobs 261*2264Sjacobs return (result); 262*2264Sjacobs } 263*2264Sjacobs 264*2264Sjacobs char * 265*2264Sjacobs papiServiceGetStatusMessage(papi_service_t handle) 266*2264Sjacobs { 267*2264Sjacobs service_t *svc = handle; 268*2264Sjacobs char *result = NULL; 269*2264Sjacobs 270*2264Sjacobs if (svc != NULL) { 271*2264Sjacobs papiAttributeListGetString(svc->attributes, NULL, 272*2264Sjacobs "detailed-status-message", &result); 273*2264Sjacobs } 274*2264Sjacobs 275*2264Sjacobs return (result); 276*2264Sjacobs } 277*2264Sjacobs 278*2264Sjacobs void 279*2264Sjacobs detailed_error(service_t *svc, char *fmt, ...) 280*2264Sjacobs { 281*2264Sjacobs if ((svc != NULL) && (fmt != NULL)) { 282*2264Sjacobs va_list ap; 283*2264Sjacobs size_t size; 284*2264Sjacobs char *message = alloca(BUFSIZ); 285*2264Sjacobs 286*2264Sjacobs va_start(ap, fmt); 287*2264Sjacobs /* 288*2264Sjacobs * fill in the message. If the buffer is too small, allocate 289*2264Sjacobs * one that is large enough and fill it in. 290*2264Sjacobs */ 291*2264Sjacobs if ((size = vsnprintf(message, BUFSIZ, fmt, ap)) >= BUFSIZ) 292*2264Sjacobs if ((message = alloca(size)) != NULL) 293*2264Sjacobs vsnprintf(message, size, fmt, ap); 294*2264Sjacobs va_end(ap); 295*2264Sjacobs 296*2264Sjacobs papiAttributeListAddString(&svc->attributes, PAPI_ATTR_APPEND, 297*2264Sjacobs "detailed-status-message", message); 298*2264Sjacobs } 299*2264Sjacobs } 300