Back to index

im-sdk  12.3.91
Defines | Functions
iiimpAuth.c File Reference
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <fcntl.h>
#include <errno.h>
#include <ctype.h>
#include <time.h>
#include <unistd.h>
#include "iiimpAuth.h"

Go to the source code of this file.

Defines

#define PASSWORD_DIR_0   ".iiim"
#define PASSWORD_DIR_1   ".iiim/auth"
#define PASSWORD_FILE   ".iiim/auth/passwd"
#define PASSWORD_FILE_LEN   (17)
#define HOME_ENV   "HOME"

Functions

char * auth_password_get (char *home_dir)

Define Documentation

#define HOME_ENV   "HOME"

Definition at line 72 of file iiimpAuth.c.

#define PASSWORD_DIR_0   ".iiim"

Definition at line 67 of file iiimpAuth.c.

#define PASSWORD_DIR_1   ".iiim/auth"

Definition at line 68 of file iiimpAuth.c.

#define PASSWORD_FILE   ".iiim/auth/passwd"

Definition at line 69 of file iiimpAuth.c.

#define PASSWORD_FILE_LEN   (17)

Definition at line 70 of file iiimpAuth.c.


Function Documentation

char* auth_password_get ( char *  home_dir)

Definition at line 75 of file iiimpAuth.c.

{
       char *        password;
       char *        password_file;
       char *        home_env;
       int           home_dir_len;
       int           fd;
       int           len;
       struct stat   st;
       int           i;
       char          pwd_buf[AUTH_PASSWORD_LEN + 1];
       unsigned int  seed;
       int           c;

       password = NULL;
       password_file = NULL;
       fd = -1;

       do {
              home_env = getenv("HOME");
              if (NULL != home_env) {
                     home_dir = home_env;
              }
              if (NULL == home_dir) {
                     break;
              }
              home_dir_len = strlen(home_dir);
              len = (home_dir_len + 1 + PASSWORD_FILE_LEN);

              password_file = malloc(len + 1);
              if (NULL == password_file) {
                     break;
              }

              strcpy(password_file, home_dir);
              *(password_file + home_dir_len) = '/';
              home_dir_len += 1;
              strcpy(password_file + home_dir_len, PASSWORD_FILE);

              if (0 != stat(password_file, &st)) {
                     if ((ENOENT != errno) && (EACCES != errno)) {
                            break;
                     }

                     /* create "${HOME}/.iiim" */
                     strcpy(password_file + home_dir_len, PASSWORD_DIR_0);
                     if (0 != stat(password_file, &st)) {
                            if ((ENOENT != errno) && (EACCES != errno)) {
                                   break;
                            }
                            if (0 != mkdir(password_file, 0777)) break;
                     }

                     /* create "${HOME}/.iiim/auth" */
                     strcpy(password_file + home_dir_len, PASSWORD_DIR_1);
                     if (0 != stat(password_file, &st)) {
                            if ((ENOENT != errno) && (EACCES != errno)) {
                                   break;
                            }
                            if (0 != mkdir(password_file, 0700)) break;
                     }

                     /* create "${HOME}/.iiim/auth/password" */
                     strcpy(password_file + home_dir_len, PASSWORD_FILE);
                     fd = open(password_file, O_CREAT | O_WRONLY, 0600);
                     if (-1 == fd) break;

                     seed = (time(NULL) + getpid());
                     srand(seed);
                     for (i = 0; i < AUTH_PASSWORD_LEN; i++) {
                            c = rand();
                            c %= 62;
                            if (c < 10) {
                                   c = ('0' + c);
                            } else if (c < 36) {
                                   c = ('A' + (c - 10));
                            } else {
                                   c = ('a' + (c - 36));
                            }
                            pwd_buf[i] = c;
                     }
                     pwd_buf[AUTH_PASSWORD_LEN] = '\n';

                     len = write(fd, pwd_buf, AUTH_PASSWORD_LEN + 1);
                     if ((AUTH_PASSWORD_LEN + 1) != len) {
                            break;
                     }

              } else {
                     fd = open(password_file, O_RDONLY, 0);
                     if (-1 == fd) break;
                     len = read(fd, pwd_buf, AUTH_PASSWORD_LEN);
                     if (AUTH_PASSWORD_LEN != len) break;
                     for (i = 0; i < AUTH_PASSWORD_LEN; i++) {
                            if (0 == isalnum(pwd_buf[i])) {
                                   break;
                            }
                     }
                     if (AUTH_PASSWORD_LEN != i) {
                            break;
                     }
              }

              password = malloc(AUTH_PASSWORD_LEN);
              if (NULL == password) break;
              memcpy(password, pwd_buf, AUTH_PASSWORD_LEN);

       } while(0);

       if (-1 != fd) {
              close(fd);
       }
       free(password_file);

       return password;
}

Here is the call graph for this function: