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

📄 servutil.c

📁 安全开发库。含客户端建立ssl连接、签名、证书验证、证书发布和撤销等。编译用到nss
💻 C
📖 第 1 页 / 共 3 页
字号:
    *d = '\0';}#endif#ifdef DEBUGchar * SSM_StandaloneGetPasswdCallback(PK11SlotInfo *slot, PRBool retry,                                 void *arg){    char *result = NULL;    char *env = NULL;    SECItem theItem = {siBuffer, NULL, 0};    if (retry)    {        /* Don't spin forever, just bail */        SSM_DEBUG("Static password you provided was not accepted. "                  "Cancelling request.\n");        return NULL;    }    env = PR_GetEnv(SSM_ENV_STATIC_PASSWORD);    if (!env)        goto done;        ATOB_ConvertAsciiToItem(&theItem, env);    if (theItem.data == NULL)        goto done;    result = (char *) PR_CALLOC(theItem.len + 1);    if (!result)        goto done;    strncpy(result, (char *) theItem.data, theItem.len); done:    if (theItem.data != NULL)        SECITEM_FreeItem(&theItem, PR_FALSE);    if (result)        SSM_DEBUG("Responding to password request with a password.\n");    else        SSM_DEBUG("Responding to password callback with NULL password.\n");    return result;}PRBool SSM_StandaloneVerifyPasswdCallback(PK11SlotInfo * slot, void * arg){    return PR_TRUE; }#endifSSMStatusSSM_CloseSocketWithLinger(PRFileDesc *sock){    PRSocketOptionData opt;    SSMStatus rv = PR_SUCCESS;        /*        Set linger for 15 minutes.       ### mwelch This time should really depend on what has been written       to the socket. For example, if we've just written 5 megabytes of data       down a 14.4 wire, we should wait considerably longer than 15 minutes.       The point here is that there's no way to determine a constant value        ahead of time. So, 15 minutes. (Note that generally only client sockets       are closed in this way, so we hopefully will avoid major trouble.)     */    opt.option = PR_SockOpt_Linger;    opt.value.linger.polarity = PR_TRUE;    opt.value.linger.linger = PR_SecondsToInterval(60*15);    rv = PR_SetSocketOption(sock, &opt);    /* Now close the socket. */    if (rv == PR_SUCCESS)        rv = PR_Close(sock);    return rv;}char*SSM_GetCharFromKey(const char *key, const char *locale){    SSMTextGenContext *textGenCxt  = NULL;    char              *asciiString = NULL;    SSMStatus           rv;    rv = SSMTextGen_NewContext(NULL, NULL, NULL, NULL, &textGenCxt);    if (rv != PR_SUCCESS) {        goto loser;    }    rv = SSM_FindUTF8StringInBundles(textGenCxt, key,                                     &asciiString);    if (rv != PR_SUCCESS) {        goto loser;    }    SSMTextGen_DestroyContext(textGenCxt);    return asciiString; loser:    if (textGenCxt != NULL) {        SSMTextGen_DestroyContext(textGenCxt);    }    if (asciiString != NULL) {        PR_Free(asciiString);    }    return NULL;}SSMStatus SSM_RequestFilePathFromUser(SSMResource *res,                                      const char  *promptKey,                                      const char  *fileRegEx,                                      PRBool       getExistingFile){    SECItem           *filePathRequest = NULL;    char              *asciiPrompt     = NULL;    SSMStatus          rv;    FilePathRequest    request;    filePathRequest = SSM_ZNEW(SECItem);    if (filePathRequest == NULL) {        return SSM_FAILURE;    }    asciiPrompt = SSM_GetCharFromKey(promptKey, "ISO_8859-1");    if (asciiPrompt == NULL) {        goto loser;    }    request.resID = res->m_id;    request.prompt = asciiPrompt;    request.getExistingFile = (CMBool) getExistingFile;    request.fileRegEx = fileRegEx;    if (CMT_EncodeMessage(FilePathRequestTemplate, (CMTItem*)filePathRequest, (void*)&request) != CMTSuccess) {        goto loser;    }    filePathRequest->type = (SECItemType) (SSM_EVENT_MESSAGE | SSM_FILE_PATH_EVENT);    SSM_LockResource(res);    rv = SSM_SendQMessage(res->m_connection->m_controlOutQ,                          20,                          filePathRequest->type,                          filePathRequest->len,                          (char*)filePathRequest->data,                          PR_TRUE);    SECITEM_FreeItem(filePathRequest, PR_TRUE);    SSM_WaitResource(res, PR_INTERVAL_NO_TIMEOUT);    SSM_UnlockResource(res);    return (res->m_fileName == NULL) ? SSM_FAILURE : SSM_SUCCESS;     loser:    if (filePathRequest != NULL) {        SECITEM_FreeItem(filePathRequest, PR_TRUE);    }    return SSM_FAILURE;}void SSM_HandleFilePathReply(SSMControlConnection *ctrl,                              SECItem              *message){  SSMStatus rv;  SSMResource  *res;  FilePathReply reply;  if (CMT_DecodeMessage(FilePathReplyTemplate, &reply, (CMTItem*)message) != CMTSuccess) {      return;  }  rv = SSMControlConnection_GetResource(ctrl, reply.resID, &res);  if (rv != PR_SUCCESS || res == NULL) {      return;  }  if (reply.filePath != NULL) {    if (reply.filePath[0] == '\0') {      res->m_fileName = NULL;    } else {      res->m_fileName = PL_strdup(reply.filePath);    }    PR_Free(reply.filePath);  } else {    res->m_fileName = NULL;    res->m_buttonType = SSM_BUTTON_CANCEL;  }  SSM_LockResource(res);  SSM_NotifyResource(res);  SSM_UnlockResource(res);  SSM_FreeResource(res);}void SSM_HandleUserPromptReply(SSMControlConnection *ctrl,                               SECItem              *msg){    SSMStatus rv;    SSMResource *res;    PromptReply reply;    if (CMT_DecodeMessage(PromptReplyTemplate, &reply, (CMTItem*)msg) != CMTSuccess) {        return;    }    rv = SSMControlConnection_GetResource(ctrl, reply.resID, &res);    if (rv != PR_SUCCESS || res == NULL)  {        return;    }    /* XXX sjlee: if the password length was zero, it would have been      * translated as a null pointer through transport.  We need to restore it      * to an empty string.  We can do that by looking up the cancel value of      * the reply.     */    if ((reply.promptReply == NULL) && !reply.cancel) {        reply.promptReply = "";    }    SSM_HandlePromptReply(res, reply.promptReply);}#ifdef TIMEBOMBSECItem * SSMTimebomb_GetMessage(SSMControlConnection * control){  SECItem * message = NULL;  PRInt32 read, type, len;  char * buffer = NULL;  SSMStatus rv = PR_FAILURE;   buffer = (char *)PORT_ZAlloc(sizeof(type)+ sizeof(len));  if (!buffer)    goto loser;  SSM_DEBUG("waiting for new message from socket.\n");  read = SSM_ReadThisMany(control->m_socket,(void *)buffer,sizeof(type)+sizeof(len));  if (read != sizeof(type)+sizeof(len))    {     SSM_DEBUG("Bytes read (%ld) != bytes expected (%ld). (hdr)\n",               (long) read,               (long) (sizeof(type)+sizeof(len)));     rv = PR_FAILURE;     goto loser;    }  message = SSM_ConstructMessage(PR_ntohl(((unsigned long *)buffer)[1]));  if (!message || !message->data)    {      SSM_DEBUG("Missing %s.\n",(!message) ? "message" : "message data");      rv = PR_OUT_OF_MEMORY_ERROR;      goto loser;    }  message->type = PR_ntohl(((unsigned long *)buffer)[0]);  SSM_DEBUG("reading %ld more from socket.\n", message->len);  read = SSM_ReadThisMany(control->m_socket, (void *)message->data, message->len);  if ((unsigned int) read != message->len)    {     SSM_DEBUG("Bytes read (%ld) != bytes expected (%ld). (msg)\n",                           (long) read,                           (long) message->len);     rv = PR_GetError();     if (rv == PR_SUCCESS) rv = PR_FAILURE;     goto loser;   }  if (buffer)     PR_Free(buffer);  return message;loser:  if (buffer)    PR_Free(buffer);  return NULL;}SSMStatus SSMTimebomb_SendMessage(SSMControlConnection * control,                                  SECItem * message){  PRInt32 tmp, sent;  if (!message)    goto loser;  /* now send message.   * I want to do it here to keep timebomb code close together */  tmp = PR_htonl(message->type);  sent =  SSM_WriteThisMany(control->m_socket, &tmp, sizeof(tmp));  if (sent != sizeof(tmp))    goto loser;  tmp = PR_htonl(message->len);  sent =  SSM_WriteThisMany(control->m_socket, &tmp, sizeof(tmp));  if (sent != sizeof(tmp))    goto loser;  sent = SSM_WriteThisMany(control->m_socket, message->data, message->len);  if (sent != message->len)    goto loser;  return SSM_SUCCESS;loser:   SSM_DEBUG("timebomb: can't send error message!\n");  return SSM_FAILURE;}#endifvoid SSM_DestroyAttrValue(SSMAttributeValue *value, PRBool freeit){    if ((value->type == SSM_STRING_ATTRIBUTE) && (value->u.string.data))        PR_Free(value->u.string.data);    value->type = (SSMResourceAttrType) 0;    if (freeit)        PR_Free(value);}CMTStatus SSM_SSMStringToString(char ** string,			       int *len, 			       SSMString * ssmString) {  char * str = NULL;  int realLen;  CMTStatus rv = CMTSuccess;  if (!ssmString || !string ) {     rv = (CMTStatus) PR_INVALID_ARGUMENT_ERROR;    goto loser;  }  /* in case we fail */  *string = NULL;  if (len) *len = 0;  /* Convert from net byte order */  realLen = SSMPR_ntohl(ssmString->m_length);  str = (char *)PR_CALLOC(realLen+1); /* add 1 byte for end 0 */  if (!str) {    rv = (CMTStatus) PR_OUT_OF_MEMORY_ERROR;    goto loser;  }    memcpy(str, (char *) &(ssmString->m_data), realLen);  /* str[realLen]=0; */  if (len) *len = realLen;  *string = str;  return rv;  loser:  if (str)     PR_Free(str);  if (string && *string) {    PR_Free(*string);    *string = NULL;  }  if (rv == CMTSuccess)     rv = CMTFailure;  return rv;}CMTStatus SSM_StringToSSMString(SSMString ** ssmString, int length, 				  char * string){  SSMPRUint32 len;  SSMString *result = NULL;  CMTStatus rv = CMTSuccess;    if (!string || !ssmString) {    rv = (CMTStatus) PR_INVALID_ARGUMENT_ERROR;    goto loser;  }  *ssmString = NULL; /* in case we fail */  if (length) len = length;   else len = strlen(string);  if (len <= 0) {    rv = (CMTStatus) PR_INVALID_ARGUMENT_ERROR;    goto loser;  }  result = (SSMString *) PR_CALLOC(sizeof(PRUint32) + 				   SSMSTRING_PADDED_LENGTH(len));  if (!result) {    rv = (CMTStatus) PR_OUT_OF_MEMORY_ERROR;    goto loser;  }  result->m_length = SSMPR_htonl(len);  memcpy((char *) (&(result->m_data)), string, len);  *ssmString = result;  goto done;  loser:  if (result)    PR_Free(result);  *ssmString = NULL;  if (rv == CMTSuccess)    rv = CMTFailure;done:  return rv;}#ifdef XP_UNIXvoid SSM_ReleaseLockFile(){  if (lockfile) {    char filePath[64];        GET_LOCK_FILE_PATH(filePath);    SSM_DEBUG("Releasing and deleting the file %s\n", filePath);    PR_UnlockFile(lockfile);    PR_Close(lockfile);    lockfile = NULL;    PR_Delete(filePath);    GET_CONTROL_SOCK_PATH(filePath);    SSM_DEBUG("Deleting %s. (The control connection socket)\n", filePath);    PR_Delete(filePath);  }}#endifint SSM_strncasecmp(const char *s1, const char *s2, size_t count){    char *myS1 = NULL, *myS2 = NULL;    int rv, len, i;    myS1 = strdup(s1);    myS2 = strdup(s2);    len = strlen(myS1);    for (i=0;i<len;i++) {        myS1[i] = tolower(myS1[i]);    }    len = strlen(myS2);    for (i=0;i<len;i++) {        myS2[i] = tolower(myS2[i]);    }    rv = strncmp(myS1,myS2,count);    free(myS1);    free(myS2);    return rv;}

⌨️ 快捷键说明

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