Back to index

lightning-sunbird  0.9+nobinonly
secpwd.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is the Netscape security libraries.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 1994-2000
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either the GNU General Public License Version 2 or later (the "GPL"), or
00025  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 #include "secutil.h"
00037 
00038 /*
00039  * NOTE:  The contents of this file are NOT used by the client.
00040  * (They are part of the security library as a whole, but they are
00041  * NOT USED BY THE CLIENT.)  Do not change things on behalf of the
00042  * client (like localizing strings), or add things that are only
00043  * for the client (put them elsewhere).  
00044  */
00045 
00046 
00047 #ifdef XP_UNIX
00048 #include <termios.h>
00049 #endif
00050 
00051 #if defined(XP_UNIX) || defined(XP_BEOS)
00052 #include <unistd.h>  /* for isatty() */
00053 #endif
00054 
00055 #if( defined(_WINDOWS) && !defined(_WIN32_WCE)) || defined(XP_OS2_VACPP)
00056 #include <conio.h>
00057 #include <io.h>
00058 #define QUIET_FGETS quiet_fgets
00059 static char * quiet_fgets (char *buf, int length, FILE *input);
00060 #else
00061 #define QUIET_FGETS fgets
00062 #endif
00063 
00064 static void echoOff(int fd)
00065 {
00066 #if defined(XP_UNIX) && !defined(VMS)
00067     if (isatty(fd)) {
00068        struct termios tio;
00069        tcgetattr(fd, &tio);
00070        tio.c_lflag &= ~ECHO;
00071        tcsetattr(fd, TCSAFLUSH, &tio);
00072     }
00073 #endif
00074 }
00075 
00076 static void echoOn(int fd)
00077 {
00078 #if defined(XP_UNIX) && !defined(VMS)
00079     if (isatty(fd)) {
00080        struct termios tio;
00081        tcgetattr(fd, &tio);
00082        tio.c_lflag |= ECHO;
00083        tcsetattr(fd, TCSAFLUSH, &tio);
00084     }
00085 #endif
00086 }
00087 
00088 char *SEC_GetPassword(FILE *input, FILE *output, char *prompt,
00089                             PRBool (*ok)(char *))
00090 {
00091 #if defined(_WINDOWS)
00092     int isTTY = (input == stdin);
00093 #define echoOn(x)
00094 #define echoOff(x)
00095 #else
00096     int infd  = fileno(input);
00097     int isTTY = isatty(infd);
00098 #endif
00099     char phrase[200] = {'\0'};      /* ensure EOF doesn't return junk */
00100 
00101     for (;;) {
00102        /* Prompt for password */
00103        if (isTTY) {
00104            fprintf(output, "%s", prompt);
00105             fflush (output);
00106            echoOff(infd);
00107        }
00108 
00109        QUIET_FGETS ( phrase, sizeof(phrase), input);
00110 
00111        if (isTTY) {
00112            fprintf(output, "\n");
00113            echoOn(infd);
00114        }
00115 
00116        /* stomp on newline */
00117        phrase[PORT_Strlen(phrase)-1] = 0;
00118 
00119        /* Validate password */
00120        if (!(*ok)(phrase)) {
00121            /* Not weird enough */
00122            if (!isTTY) return 0;
00123            fprintf(output, "Password must be at least 8 characters long with one or more\n");
00124            fprintf(output, "non-alphabetic characters\n");
00125            continue;
00126        }
00127        return (char*) PORT_Strdup(phrase);
00128     }
00129 }
00130 
00131 
00132 
00133 PRBool SEC_CheckPassword(char *cp)
00134 {
00135     int len;
00136     char *end;
00137 
00138     len = PORT_Strlen(cp);
00139     if (len < 8) {
00140        return PR_FALSE;
00141     }
00142     end = cp + len;
00143     while (cp < end) {
00144        unsigned char ch = *cp++;
00145        if (!((ch >= 'A') && (ch <= 'Z')) &&
00146            !((ch >= 'a') && (ch <= 'z'))) {
00147            /* pass phrase has at least one non alphabetic in it */
00148            return PR_TRUE;
00149        }
00150     }
00151     return PR_FALSE;
00152 }
00153 
00154 PRBool SEC_BlindCheckPassword(char *cp)
00155 {
00156     if (cp != NULL) {
00157        return PR_TRUE;
00158     }
00159     return PR_FALSE;
00160 }
00161 
00162 /* Get a password from the input terminal, without echoing */
00163 
00164 #if defined(_WINDOWS) || defined(XP_OS2_VACPP)
00165 static char * quiet_fgets (char *buf, int length, FILE *input)
00166   {
00167   int c;
00168   char *end = buf;
00169 
00170   /* fflush (input); */
00171   memset (buf, 0, length);
00172 
00173 #ifndef XP_OS2_VACPP
00174   if (input != stdin) {
00175      return fgets(buf,length,input);
00176   }
00177 #else
00178   if (!isatty(fileno(input))) {
00179      return fgets(buf,length,input);
00180   }
00181 #endif
00182 
00183   while (1)
00184     {
00185 #if defined (_WIN32_WCE)
00186     c = getchar();   /* gets a character from stdin */
00187 #else
00188     c = getch();     /* getch gets a character from the console */
00189 #endif
00190     if (c == '\b')
00191       {
00192       if (end > buf)
00193         end--;
00194       }
00195 
00196     else if (--length > 0)
00197       *end++ = c;
00198 
00199     if (!c || c == '\n' || c == '\r')
00200       break;
00201     }
00202 
00203   return buf;
00204   }
00205 #endif