Back to index

im-sdk  12.3.91
iiimpAuth.c
Go to the documentation of this file.
00001 /*
00002 Copyright 1990-2001 Sun Microsystems, Inc. All Rights Reserved.
00003 
00004 Permission is hereby granted, free of charge, to any person obtaining a
00005 copy of this software and associated documentation files (the
00006 "Software"), to deal in the Software without restriction, including
00007 without limitation the rights to use, copy, modify, merge, publish,
00008 distribute, sublicense, and/or sell copies of the Software, and to
00009 permit persons to whom the Software is furnished to do so, subject to
00010 the following conditions: The above copyright notice and this
00011 permission notice shall be included in all copies or substantial
00012 portions of the Software.
00013 
00014 
00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00016 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00017 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00018 IN NO EVENT SHALL THE OPEN GROUP OR SUN MICROSYSTEMS, INC. BE LIABLE
00019 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
00020 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
00021 THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE EVEN IF
00022 ADVISED IN ADVANCE OF THE POSSIBILITY OF SUCH DAMAGES.
00023 
00024 
00025 Except as contained in this notice, the names of The Open Group and/or
00026 Sun Microsystems, Inc. shall not be used in advertising or otherwise to
00027 promote the sale, use or other dealings in this Software without prior
00028 written authorization from The Open Group and/or Sun Microsystems,
00029 Inc., as applicable.
00030 
00031 
00032 X Window System is a trademark of The Open Group
00033 
00034 OSF/1, OSF/Motif and Motif are registered trademarks, and OSF, the OSF
00035 logo, LBX, X Window System, and Xinerama are trademarks of the Open
00036 Group. All other trademarks and registered trademarks mentioned herein
00037 are the property of their respective owners. No right, title or
00038 interest in or to any trademark, service mark, logo or trade name of
00039 Sun Microsystems, Inc. or its licensors is granted.
00040 
00041 */
00042 
00043 /*
00044  * $Id: iiimpAuth.c,v 1.1.1.1.2.1 2001/03/15 07:52:16 himi Exp $
00045  */
00046 
00047 
00048 #include <stdlib.h>
00049 #include <stddef.h>
00050 #include <string.h>
00051 #include <sys/types.h>
00052 #include <sys/stat.h>
00053 #include <sys/param.h>
00054 #include <fcntl.h>
00055 #include <errno.h>
00056 #include <ctype.h>
00057 #include <time.h>
00058 #include <unistd.h>
00059 #if defined(AUTH_STANDALONE)
00060 #include <stdio.h>
00061 #include <locale.h>
00062 #endif /* AUTH_STANDALONE */
00063 
00064 #include "iiimpAuth.h"
00065 
00066 
00067 #define PASSWORD_DIR_0             ".iiim"
00068 #define PASSWORD_DIR_1             ".iiim/auth"
00069 #define PASSWORD_FILE              ".iiim/auth/passwd"
00070 #define PASSWORD_FILE_LEN   (17)
00071 
00072 #define HOME_ENV     "HOME"
00073 
00074 char *
00075 auth_password_get(char * home_dir)
00076 {
00077        char *        password;
00078        char *        password_file;
00079        char *        home_env;
00080        int           home_dir_len;
00081        int           fd;
00082        int           len;
00083        struct stat   st;
00084        int           i;
00085        char          pwd_buf[AUTH_PASSWORD_LEN + 1];
00086        unsigned int  seed;
00087        int           c;
00088 
00089        password = NULL;
00090        password_file = NULL;
00091        fd = -1;
00092 
00093        do {
00094               home_env = getenv("HOME");
00095               if (NULL != home_env) {
00096                      home_dir = home_env;
00097               }
00098               if (NULL == home_dir) {
00099                      break;
00100               }
00101               home_dir_len = strlen(home_dir);
00102               len = (home_dir_len + 1 + PASSWORD_FILE_LEN);
00103 
00104               password_file = malloc(len + 1);
00105               if (NULL == password_file) {
00106                      break;
00107               }
00108 
00109               strcpy(password_file, home_dir);
00110               *(password_file + home_dir_len) = '/';
00111               home_dir_len += 1;
00112               strcpy(password_file + home_dir_len, PASSWORD_FILE);
00113 
00114               if (0 != stat(password_file, &st)) {
00115                      if ((ENOENT != errno) && (EACCES != errno)) {
00116                             break;
00117                      }
00118 
00119                      /* create "${HOME}/.iiim" */
00120                      strcpy(password_file + home_dir_len, PASSWORD_DIR_0);
00121                      if (0 != stat(password_file, &st)) {
00122                             if ((ENOENT != errno) && (EACCES != errno)) {
00123                                    break;
00124                             }
00125                             if (0 != mkdir(password_file, 0777)) break;
00126                      }
00127 
00128                      /* create "${HOME}/.iiim/auth" */
00129                      strcpy(password_file + home_dir_len, PASSWORD_DIR_1);
00130                      if (0 != stat(password_file, &st)) {
00131                             if ((ENOENT != errno) && (EACCES != errno)) {
00132                                    break;
00133                             }
00134                             if (0 != mkdir(password_file, 0700)) break;
00135                      }
00136 
00137                      /* create "${HOME}/.iiim/auth/password" */
00138                      strcpy(password_file + home_dir_len, PASSWORD_FILE);
00139                      fd = open(password_file, O_CREAT | O_WRONLY, 0600);
00140                      if (-1 == fd) break;
00141 
00142                      seed = (time(NULL) + getpid());
00143                      srand(seed);
00144                      for (i = 0; i < AUTH_PASSWORD_LEN; i++) {
00145                             c = rand();
00146                             c %= 62;
00147                             if (c < 10) {
00148                                    c = ('0' + c);
00149                             } else if (c < 36) {
00150                                    c = ('A' + (c - 10));
00151                             } else {
00152                                    c = ('a' + (c - 36));
00153                             }
00154                             pwd_buf[i] = c;
00155                      }
00156                      pwd_buf[AUTH_PASSWORD_LEN] = '\n';
00157 
00158                      len = write(fd, pwd_buf, AUTH_PASSWORD_LEN + 1);
00159                      if ((AUTH_PASSWORD_LEN + 1) != len) {
00160                             break;
00161                      }
00162 
00163               } else {
00164                      fd = open(password_file, O_RDONLY, 0);
00165                      if (-1 == fd) break;
00166                      len = read(fd, pwd_buf, AUTH_PASSWORD_LEN);
00167                      if (AUTH_PASSWORD_LEN != len) break;
00168                      for (i = 0; i < AUTH_PASSWORD_LEN; i++) {
00169                             if (0 == isalnum(pwd_buf[i])) {
00170                                    break;
00171                             }
00172                      }
00173                      if (AUTH_PASSWORD_LEN != i) {
00174                             break;
00175                      }
00176               }
00177 
00178               password = malloc(AUTH_PASSWORD_LEN);
00179               if (NULL == password) break;
00180               memcpy(password, pwd_buf, AUTH_PASSWORD_LEN);
00181 
00182        } while(0);
00183 
00184        if (-1 != fd) {
00185               close(fd);
00186        }
00187        free(password_file);
00188 
00189        return password;
00190 }
00191 
00192 #if defined(AUTH_STANDALONE)
00193 main(int argc, char ** argv)
00194 {
00195        char *        password;
00196        int           i;
00197        int           c;
00198 
00199        extern char * optarg;
00200        extern int    optind;
00201        extern int    opterr;
00202        extern int    optopt;
00203 
00204        setlocale(LC_MESSAGES, "");
00205 
00206        while (EOF != (c = getopt(argc, argv, "p"))) {
00207               switch (c) {
00208               case 'p':
00209                      password = auth_password_get(NULL);
00210                      printf("%s\n",
00211                             (NULL == password) ? "(NULL)" : password);
00212                      free(password);
00213                      break;
00214               default:
00215                      break;
00216               }
00217        }
00218 
00219        return 0;
00220 }
00221 #endif /* AUTH_STANDALONE */