Back to index

im-sdk  12.3.91
Defines | Functions
IMUtil.cpp File Reference
#include <config.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <fcntl.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/resource.h>
#include "IMUtil.hh"
#include "IMLog.hh"

Go to the source code of this file.

Defines

#define DIRECTORY_SEP   "/"
#define IS_DIRECTORY_SEP(c)   ((c) == '/')
#define MODEXT   ".so"
#define S_IAMB   0x1FF
#define ALTERNATE_DIR_RETRY_MAX   (10)

Functions

CARD8BIT * IM_read_file (const char *filename, size_t size)
int IM_file_size (const char *filename)
static int dir_valid (const char *path_name, uid_t euid)
void alternate_unix_domain_socket_file_dir (string &user_name, uid_t euid, string &alternate_dir)
void IM_unix_domain_socket_file_dir (string user_name, string &dir_ret)

Define Documentation

#define ALTERNATE_DIR_RETRY_MAX   (10)
#define DIRECTORY_SEP   "/"

Definition at line 10 of file IMUtil.cpp.

#define IS_DIRECTORY_SEP (   c)    ((c) == '/')

Definition at line 11 of file IMUtil.cpp.

#define MODEXT   ".so"
#define S_IAMB   0x1FF

Function Documentation

void alternate_unix_domain_socket_file_dir ( string &  user_name,
uid_t  euid,
string &  alternate_dir 
)

Definition at line 311 of file IMUtil.cpp.

{
    DIR *            dirp;
    struct dirent *  dirent;
    string           base_name;
    string           dir_name;
    char *           dir_str;
    int                     length;
    int                     found;
    int                     fd;
    int                     i;
    int                     key;

#define ALTERNATE_DIR_RETRY_MAX    (10)

    dirp = opendir("/tmp");
    if (NULL == dirp) return;

    base_name = (string(".iiimp-unix-") + user_name + string("-"));

    found = 0;
    while (NULL != (dirent = readdir(dirp))) {
       if (0 != strncmp(dirent->d_name, base_name.c_str(), base_name.size())) {
           continue;
       }

       dir_name = (string("/tmp/") + string(dirent->d_name));
       found = dir_valid(dir_name.c_str(), euid);

       if (1 == found) break;
    }

    closedir(dirp);

    if (1 == found) {
       alternate_dir = dir_name;
       return;
    }

    length = strlen("/tmp/.iiimp-unix-") + user_name.size() + 1 + 8 + 1;
    dir_str = (char *)malloc(length);
    if (NULL == dir_str) return;

    fd = open("/dev/urandom", O_RDONLY, 0);
    if (fd <= 0) srandom(time(NULL));

    for (i = 0; i < ALTERNATE_DIR_RETRY_MAX; i++) {
       if (0 <= fd) {
           read(fd, &key, sizeof (key));
       } else {
           key = random();
       }

       snprintf(dir_str, length,
               "/tmp/.iiimp-unix-%s-%08x", user_name.c_str(), key);

       if (mkdir(dir_str, S_IRWXU) < 0) {
           if (EEXIST != errno) {
              continue;
           }
       }
       if (1 == dir_valid(dir_str, euid)) {
           break;
       }
    }

    if (0 <= fd) close(fd);

    if (i < ALTERNATE_DIR_RETRY_MAX) {
       alternate_dir = string(dir_str);
    }
    free(dir_str);

    return;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int dir_valid ( const char *  path_name,
uid_t  euid 
) [static]

Definition at line 293 of file IMUtil.cpp.

                                              {
    struct stat      st;

#if !defined(S_IAMB)
#define       S_IAMB 0x1FF
#endif /* !S_IAMB */
    if ((0 == lstat(path_name, &st)) &&
       (0 != S_ISDIR(st.st_mode)) &&
        (euid == st.st_uid) &&
        ((S_IAMB & st.st_mode) == S_IRWXU)) {
       return 1;
    } else {
       return 0;
    }
}

Here is the caller graph for this function:

int IM_file_size ( const char *  filename)

Definition at line 283 of file IMUtil.cpp.

{
    struct stat s;
    if (stat(filename, &s) != 0) return -1;
    return s.st_size;
}

Here is the caller graph for this function:

CARD8BIT* IM_read_file ( const char *  filename,
size_t  size 
)

Definition at line 254 of file IMUtil.cpp.

{
    size_t rsize;
    FILE *fd;
    CARD8BIT *pr;

    fd = fopen(filename, "rb");
    if (!fd) {
       return NULL;
    }
    pr = (CARD8BIT*) malloc(sizeof(CARD8BIT) * size);
    if (!pr) {
       fclose(fd);
       return NULL;
    }
    rsize = fread(pr, sizeof(CARD8BIT), size, fd);
    fclose(fd);
    if (rsize != size) {
       free(pr);
       return NULL;
    }

    return pr;
}

Here is the caller graph for this function:

void IM_unix_domain_socket_file_dir ( string  user_name,
string &  dir_ret 
)

Definition at line 392 of file IMUtil.cpp.

{
    string    dir;
    uid_t     euid;
    struct stat      st;

    euid = geteuid();

    dir = (string("/tmp/.iiimp-unix-") + string(user_name));
    if (0 == dir_valid(dir.c_str(), euid)) {
       alternate_unix_domain_socket_file_dir(user_name, euid, dir_ret);
    } else {
       dir_ret = dir;
    }
}

Here is the call graph for this function:

Here is the caller graph for this function: