/security/nss/cmd/modutil/instsec.c

http://github.com/zpao/v8monkey · C · 182 lines · 98 code · 21 blank · 63 comment · 19 complexity · 16e26ef5935d0b475d8e5aa457ab464d MD5 · raw file

  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3. *
  4. * The contents of this file are subject to the Mozilla Public License Version
  5. * 1.1 (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. * http://www.mozilla.org/MPL/
  8. *
  9. * Software distributed under the License is distributed on an "AS IS" basis,
  10. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. * for the specific language governing rights and limitations under the
  12. * License.
  13. *
  14. * The Original Code is the Netscape security libraries.
  15. *
  16. * The Initial Developer of the Original Code is
  17. * Netscape Communications Corporation.
  18. * Portions created by the Initial Developer are Copyright (C) 1994-2000
  19. * the Initial Developer. All Rights Reserved.
  20. *
  21. * Contributor(s):
  22. *
  23. * Alternatively, the contents of this file may be used under the terms of
  24. * either the GNU General Public License Version 2 or later (the "GPL"), or
  25. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  26. * in which case the provisions of the GPL or the LGPL are applicable instead
  27. * of those above. If you wish to allow use of your version of this file only
  28. * under the terms of either the GPL or the LGPL, and not to allow others to
  29. * use your version of this file under the terms of the MPL, indicate your
  30. * decision by deleting the provisions above and replace them with the notice
  31. * and other provisions required by the GPL or the LGPL. If you do not delete
  32. * the provisions above, a recipient may use your version of this file under
  33. * the terms of any one of the MPL, the GPL or the LGPL.
  34. *
  35. * ***** END LICENSE BLOCK ***** */
  36. #include <plarena.h>
  37. #include <prerror.h>
  38. #include <prio.h>
  39. #include <prprf.h>
  40. #include <seccomon.h>
  41. #include <secmod.h>
  42. #include <jar.h>
  43. #include <secutil.h>
  44. /* These are installation functions that make calls to the security library.
  45. * We don't want to include security include files in the C++ code too much.
  46. */
  47. static char* PR_fgets(char *buf, int size, PRFileDesc *file);
  48. /***************************************************************************
  49. *
  50. * P k 1 1 I n s t a l l _ A d d N e w M o d u l e
  51. */
  52. int
  53. Pk11Install_AddNewModule(char* moduleName, char* dllPath,
  54. unsigned long defaultMechanismFlags,
  55. unsigned long cipherEnableFlags)
  56. {
  57. return (SECMOD_AddNewModule(moduleName, dllPath,
  58. SECMOD_PubMechFlagstoInternal(defaultMechanismFlags),
  59. SECMOD_PubCipherFlagstoInternal(cipherEnableFlags))
  60. == SECSuccess) ? 0 : -1;
  61. }
  62. /*************************************************************************
  63. *
  64. * P k 1 1 I n s t a l l _ U s e r V e r i f y J a r
  65. *
  66. * Gives the user feedback on the signatures of a JAR files, asks them
  67. * whether they actually want to continue.
  68. * Assumes the jar structure has already been created and is valid.
  69. * Returns 0 if the user wants to continue the installation, nonzero
  70. * if the user wishes to abort.
  71. */
  72. short
  73. Pk11Install_UserVerifyJar(JAR *jar, PRFileDesc *out, PRBool query)
  74. {
  75. JAR_Context *ctx;
  76. JAR_Cert *fing;
  77. JAR_Item *item;
  78. char stdinbuf[80];
  79. int count=0;
  80. CERTCertificate *cert, *prev=NULL;
  81. PR_fprintf(out, "\nThis installation JAR file was signed by:\n");
  82. ctx = JAR_find(jar, NULL, jarTypeSign);
  83. while(JAR_find_next(ctx, &item) >= 0 ) {
  84. fing = (JAR_Cert*) item->data;
  85. cert = fing->cert;
  86. if(cert==prev) {
  87. continue;
  88. }
  89. count++;
  90. PR_fprintf(out, "----------------------------------------------\n");
  91. if(cert) {
  92. if(cert->nickname) {
  93. PR_fprintf(out, "**NICKNAME**\n%s\n", cert->nickname);
  94. }
  95. if(cert->subjectName) {
  96. PR_fprintf(out, "**SUBJECT NAME**\n%s\n", cert->subjectName); }
  97. if(cert->issuerName) {
  98. PR_fprintf(out, "**ISSUER NAME**\n%s\n", cert->issuerName);
  99. }
  100. } else {
  101. PR_fprintf(out, "No matching certificate could be found.\n");
  102. }
  103. PR_fprintf(out, "----------------------------------------------\n\n");
  104. prev=cert;
  105. }
  106. JAR_find_end(ctx);
  107. if(count==0) {
  108. PR_fprintf(out, "No signatures found: JAR FILE IS UNSIGNED.\n");
  109. }
  110. if(query) {
  111. PR_fprintf(out,
  112. "Do you wish to continue this installation? (y/n) ");
  113. if(PR_fgets(stdinbuf, 80, PR_STDIN) != NULL) {
  114. char *response;
  115. if( (response=strtok(stdinbuf, " \t\n\r")) ) {
  116. if( !PL_strcasecmp(response, "y") ||
  117. !PL_strcasecmp(response, "yes") ) {
  118. return 0;
  119. }
  120. }
  121. }
  122. }
  123. return 1;
  124. }
  125. /**************************************************************************
  126. *
  127. * P R _ f g e t s
  128. *
  129. * fgets implemented with NSPR.
  130. */
  131. static char*
  132. PR_fgets(char *buf, int size, PRFileDesc *file)
  133. {
  134. int i;
  135. int status;
  136. char c;
  137. i=0;
  138. while(i < size-1) {
  139. status = PR_Read(file, (void*) &c, 1);
  140. if(status==-1) {
  141. return NULL;
  142. } else if(status==0) {
  143. break;
  144. }
  145. buf[i++] = c;
  146. if(c=='\n') {
  147. break;
  148. }
  149. }
  150. buf[i]='\0';
  151. return buf;
  152. }
  153. /**************************************************************************
  154. *
  155. * m y S E C U _ E r r o r S t r i n g
  156. *
  157. */
  158. const char* mySECU_ErrorString(PRErrorCode errnum)
  159. {
  160. return SECU_Strerror(errnum);
  161. }