/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
*/
#include "lint.h"
#include "mtlib.h"
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <limits.h>
#include <pthread.h>
#include <thread.h>
#include <string.h>
#include <dirent.h>
#include <stdio.h>
#include <dlfcn.h>
#include <atomic.h>
#include <md5.h>
#include "pos4obj.h"
static char *__pos4obj_name(const char *, const char *);
static void __pos4obj_md5toa(unsigned char *, unsigned char *);
static void __pos4obj_clean(char *);
static long int name_max = 0;
int
{
int cancel_state;
int val;
/*
* Ensure path is not a symlink to somewhere else. This provides
* a modest amount of protection against easy security attacks.
* Also, set the close-on-exec flag atomically on open().
*/
return (val);
}
int
{
int cancel_state;
int val;
return (val);
}
/*
* This is to avoid loading libmd.so.1 unless we absolutely have to.
*/
typedef void (*md5_calc_t)(unsigned char *, unsigned char *, unsigned int);
static void
load_md5_calc(void)
{
if (real_md5_calc == NULL) {
else {
}
}
if (md5_handle)
(void) dlclose(md5_handle);
}
static char *
{
int olderrno;
char *dfile;
/*
* If the path is path_max - strlen(type) characters or less,
* the name of the file to use will be the path prefixed by
* the type.
*
* In the special case where the path is longer than
* path_max - strlen(type) characters, we create a string based on the
* MD5 hash of the path. We prefix that string with a '.' to
* make it obscure, and create a directory in objroot with
* that name. In that directory, we create a directory named
* after the type of object requested. Inside the type
* directory, the filename will be the path of the object. This
* prevents collisions in all namespaces.
*
* Example:
* Let objroot = "/tmp/", path = "/<longpath>", and type = ".MQD"
* Let the MD5 hash of "<longpath>" = "<hash>"
*
* The desired file is /tmp/.<hash>/.MQD/<longpath>
*/
/*
* Do not include the leading '/' in the path length.
* Assumes __pos4obj_check(path) has already been called.
*/
shortpath = 0;
if (shortpath) {
/*
* strlen(path) includes leading slash as space for NUL.
*/
} else {
/*
* Long path name. Add 3 for extra '/', '.' and '\0'
*/
}
return (NULL);
if (shortpath) {
return (dfile);
}
/*
* If we can successfully load it, call md5_calc().
* Otherwise, (this "can't happen") return NULL.
*/
if (real_md5_calc == NULL)
return (NULL);
}
/*
* Errno must be preserved across the following calls to
* mkdir. This needs to be done to prevent incorrect error
* reporting in certain cases. When we attempt to open a
* non-existent object without the O_CREAT flag, it will
* always create a lock file first. The lock file is created
* and then the open is attempted, but fails with ENOENT. The
* lock file is then destroyed. In the following code path, we
* are finding the absolute path to the lock file after
* already having attempted the open (which set errno to
* ENOENT). The following calls to mkdir will return -1 and
* set errno to EEXIST, since the hash and type directories
* were created when the lock file was created. The correct
* errno is the ENOENT from the attempted open of the desired
* object.
*/
/*
* Create hash directory. Use 777 permissions so everyone can use it.
*/
return (NULL);
}
} else {
return (NULL);
}
}
/*
* Create directory for requested type. Use 777 perms so everyone
* can use it.
*/
return (NULL);
}
} else {
return (NULL);
}
}
return (dfile);
}
/*
* Takes a 128-bit MD5 digest and transforms to a sequence of 32 ASCII
* characters. Output is the hexadecimal representation of the digest.
*
* The output buffer must be at least HASHSTRLEN + 1 characters
* long. HASHSTRLEN is the size of the MD5 digest (128 bits)
* divided by the number of bits used per char of output (4). The
* extra character at the end is for the NUL terminating character.
*/
static void
{
int i;
uint32_t *p;
/* LINTED pointer cast may result in improper alignment */
for (i = 0; i < (MD5_DIGEST_LENGTH / 4); i++)
}
/*
* This open function assume that there is no simultaneous
* to ensure that both open in O_CREAT mode happen atomically.
* It returns the crflag as 1 if file is created else 0.
*/
int
{
int fd;
char *dfile;
errno = 0;
*crflag = 0;
return (-1);
}
return (fd);
}
/*
* We need to make sure that crflag is set iff we actually create
* the file. We do this by or'ing in O_EXCL, and attempting an
* open. If that fails with an EEXIST, and O_EXCL wasn't specified
* by the caller, then the file seems to exist; we'll try an
* open with O_CREAT cleared. If that succeeds, then the file
* did indeed exist. If that fails with an ENOENT, however, the
* file was removed between the opens; we need to take another
* lap.
*/
for (;;) {
continue;
break;
}
} else {
*crflag = 1;
}
break;
}
return (fd);
}
int
{
int err;
char *dfile;
return (-1);
}
return (err);
}
/*
* This function opens the lock file for each named object
* the presence of this file in the file system is the lock
*/
int
{
char *dfile;
int fd;
return (-1);
}
while (limit-- > 0) {
< 0) {
break;
(void) sleep(1);
continue;
}
(void) __close_nc(fd);
return (1);
}
return (-1);
}
/*
* Unlocks the file by unlinking it from the filesystem
*/
int
{
}
/*
* Removes unused hash and type directories that may exist in specified path.
*/
static void
{
char *p;
int olderrno;
/*
* path is either
* 1) /<objroot>/<type><path> or
* 2) /<objroot>/.<hash>/<type>/<path>
*
* In case 1, there is nothing to clean.
*
* Detect case 2 by looking for a '/' after /objroot/ and
* remove the two trailing directories, if empty.
*/
return;
/*
* Preserve errno across calls to rmdir. See block comment in
* __pos4obj_name() for explanation.
*/
return;
*p = '\0';
return;
*p = '\0';
}
/*
* Check that path starts with a /, does not contain a / within it
* and is not longer than PATH_MAX or NAME_MAX
*/
int
{
long int i;
/*
* This assumes that __pos4obj_check() is called before
* any of the other functions in this file
*/
if (name_max == -1)
return (-1);
}
if (*path++ != '/') {
return (-1);
}
for (i = 0; *path != '\0'; i++) {
if (*path++ == '/') {
return (-1);
}
}
return (-1);
}
return (0);
}