⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 slp_spi.c

📁 SLP协议在linux下的实现。此版本为1.2.1版。官方网站为www.openslp.org
💻 C
📖 第 1 页 / 共 2 页
字号:
/***************************************************************************//*                                                                         *//* Project:     OpenSLP - OpenSource implementation of Service Location    *//*              Protocol Version 2                                         *//*                                                                         *//* File:        slp_spi.h                                                  *//*                                                                         *//* Abstract:    Functions for fetching SPI information from the filesystem *//*              Current implementation uses OpenSSL. For details see       *//*              (see http://www.openssl.org                                *//*                                                                         *//*-------------------------------------------------------------------------*//*                                                                         *//*     Please submit patches to http://www.openslp.org                     *//*                                                                         *//*-------------------------------------------------------------------------*//*                                                                         *//* Copyright (C) 2000 Caldera Systems, Inc                                 *//* All rights reserved.                                                    *//*                                                                         *//* Redistribution and use in source and binary forms, with or without      *//* modification, are permitted provided that the following conditions are  *//* met:                                                                    */ /*                                                                         *//*      Redistributions of source code must retain the above copyright     *//*      notice, this list of conditions and the following disclaimer.      *//*                                                                         *//*      Redistributions in binary form must reproduce the above copyright  *//*      notice, this list of conditions and the following disclaimer in    *//*      the documentation and/or other materials provided with the         *//*      distribution.                                                      *//*                                                                         *//*      Neither the name of Caldera Systems nor the names of its           *//*      contributors may be used to endorse or promote products derived    *//*      from this software without specific prior written permission.      *//*                                                                         *//* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS     *//* `AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT      *//* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR   *//* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE CALDERA      *//* SYSTEMS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, *//* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT        *//* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;  LOSS OF USE,  *//* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON       *//* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *//* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE   *//* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.    *//*                                                                         *//***************************************************************************/#include "slp_spi.h"#include "slp_xmalloc.h"#include <string.h>#include <stdio.h>#include <openssl/pem.h>#include <openssl/bn.h>#define MAX_SPI_ENTRY_LEN   1024#define PUBLIC_TOKEN        "PUBLIC"#define PRIVATE_TOKEN       "PRIVATE"/*-------------------------------------------------------------------------*/void SLPSpiEntryFree(SLPSpiEntry* victim)/*-------------------------------------------------------------------------*/{    if(victim->keyfilename) xfree(victim->keyfilename);    if(victim->spistr) xfree(victim->spistr);    if(victim->key) SLPCryptoDSAKeyDestroy(victim->key);    if(victim) xfree(victim);}/*-------------------------------------------------------------------------*/SLPSpiEntry* SLPSpiEntryFind(SLPList* cache,                             int keytype,                             int spistrlen,                             const char* spistr)/* pass in null spistr to find the first Cached entry                      *//*-------------------------------------------------------------------------*/{    SLPSpiEntry* entry = (SLPSpiEntry*)cache->head;    while(entry)    {        if(spistr)        {            if (entry->spistrlen == spistrlen &&                memcmp(entry->spistr,spistr,spistrlen) == 0 &&                        entry->keytype == keytype)            {                return entry;            }        }        else        {            if(keytype == SLPSPI_KEY_TYPE_ANY || entry->keytype == keytype)            {                return entry;            }        }        entry = (SLPSpiEntry*)entry->listitem.next;    }    return 0;}/*-------------------------------------------------------------------------*/SLPCryptoDSAKey* SLPSpiReadKeyFile(const char* keyfile, int keytype)/*-------------------------------------------------------------------------*/{    FILE*            fp;    SLPCryptoDSAKey* result = 0;    fp = fopen(keyfile,"r");    if(fp)    {        if(keytype == SLPSPI_KEY_TYPE_PUBLIC)        {            result = PEM_read_DSA_PUBKEY(fp, &result, NULL, NULL);        }        else if (keytype == SLPSPI_KEY_TYPE_PRIVATE)        {            result =  PEM_read_DSAPrivateKey(fp, &result, NULL, NULL);        }	    fclose(fp);    }    return result;}/*-------------------------------------------------------------------------*/SLPSpiEntry* SLPSpiReadSpiFile(FILE* fp, int keytype)/* Caller needs to free returned memory SLPSpiEntryFree()                  *//*-------------------------------------------------------------------------*/{    SLPSpiEntry*    result;    char            tmp;    char*           line;    char*           slider1;    char*           slider2;    /*----------------------------*/    /* Allocate memory for result */    /*----------------------------*/    line = (char*) xmalloc(MAX_SPI_ENTRY_LEN);    result = (SLPSpiEntry*) xmalloc(sizeof(SLPSpiEntry));    if(result == 0 || line == 0)    {        return 0;    }    memset(result,0,sizeof(SLPSpiEntry));        /*---------------------------*/    /* Read the next valid entry */    /*---------------------------*/    while(fgets(line, MAX_SPI_ENTRY_LEN, fp))    {        /*----------------------*/        /* read the first token */        /*----------------------*/        slider1 = line;        /* skip leading whitespace */        while(*slider1 && *slider1 <= 0x20) slider1++;        /* skip all white lines */        if(*slider1 == 0) continue;        /* skip commented lines */        if(*slider1 == '#') continue;        /* PUBLIC|PRIVATE */        slider2 = slider1;        while(*slider2 && *slider2 > 0x20) slider2++;        if(strncasecmp(PUBLIC_TOKEN,slider1,slider2-slider1) == 0)        {            if(keytype == SLPSPI_KEY_TYPE_PRIVATE) continue;            result->keytype = SLPSPI_KEY_TYPE_PUBLIC;        }        else if(strncasecmp(PRIVATE_TOKEN,slider1,slider2-slider1) == 0)        {            if(keytype == SLPSPI_KEY_TYPE_PUBLIC) continue;            result->keytype = SLPSPI_KEY_TYPE_PRIVATE;        }        else        {            /* unknown token */            continue;        }        /*-----------------------*/        /* read the second token */        /*-----------------------*/        slider1=slider2;        /* skip leading whitespace */        while(*slider1 && *slider1 <= 0x20) slider1++;        /* SPI string */        slider2 = slider1;        while(*slider2 && *slider2 > 0x20) slider2++;        /* SPI string is at slider1 length slider2 - slider1 */            result->spistr = (char*)xmalloc(slider2-slider1);        if(result->spistr)        {            memcpy(result->spistr,slider1,slider2-slider1);            result->spistrlen = slider2-slider1;            }                           /*----------------------*/        /* read the third token */        /*----------------------*/        slider1=slider2;        /* skip leading whitespace */        while(*slider1 && *slider1 <= 0x20) slider1++;        /* SPI string */        slider2 = slider1;        while(*slider2 && *slider2 > 0x20) slider2++;        /* key file path is at slider1 length slider2 - slider1 */        tmp = *slider2;         *slider2 = 0;        result->keyfilename = xstrdup(slider1);        result->key = 0; /* read it later */         *slider2 = tmp;                /*-----------------*/        /* See what we got */        /*-----------------*/        if(result &&           result->spistr &&           result->keyfilename)        {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -