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