void initIPC(){ for (int i = 0; i < MAX_PIPES; ++i) { pipes[i]=(pipe_t )0; } pipeMutex = getMutex(PIPES_MUTEX); }
void Security::generateSessionKey() throw (SecurityException) { int seedSize; unsigned char seed[SEED_SIZE]; #ifndef _WINDOWS // Reads 256 bytes from file /dev/urandom and adds them to // PRNG to use as seed of the random number generator int fd = open("/dev/urandom", O_RDONLY); if (fd < 0) #ifdef MXHPUXPA // HP-UX PA-RISC // /dev/urandom file does not exist on HP-UX PA-RISK. If customers // do not choose to install the HP-UX Strong Random Number Generator // package which can be obtained from this link, provide the following // weak Random Number Generator { seedSize = WEAK_SEED_SIZE; if (seedGenerator((char*)seed) != seedSize) throw SecurityException(UNABLE_TO_SEED_RNG, NULL); } #else // other UNIX platforms throw SecurityException(UNABLE_TO_SEED_RNG, NULL); seedSize = SEED_SIZE; int num = 0; // This can infinitely loops if /dev/urandom does not return 256 bytes // However, it's very very unlikely to happen. while (num != seedSize) { int count = 0; count = read(fd, &seed[num], seedSize - num); if (count > 0) num += count; else if (count == 0) { close(fd); throw SecurityException(FAILED_GENERATE_RANDOM_NUM, NULL); } else //count < 0 { switch (errno) { #ifdef EINTR case EINTR: // Interrupted system call #endif #ifdef EAGAIN case EAGAIN: // Resource temporarily unavailable #endif /* No error, try again */ break; default: close(fd); throw SecurityException(FAILED_GENERATE_RANDOM_NUM, NULL); } } } close(fd); #endif //MXHPUXPA #else // _WINDOWS seedSize=SEED_SIZE; // Get a handle to the DLL module. HINSTANCE hLib = LoadLibrary(TEXT("advapi32.dll")); // Do the following to avoid the overhead of loading the entire CryptoAPI if (hLib) { BOOLEAN (APIENTRY *pfn)(void*, ULONG) = (BOOLEAN (APIENTRY *)(void*,ULONG))GetProcAddress(hLib, "SystemFunction036"); if (pfn) { if(!pfn(seed, seedSize)) { throw SecurityException(UNABLE_TO_SEED_RNG, NULL); } } } else { throw SecurityException(FAILED_LOADING_LIB, NULL); } #endif // _WINDOWS getMutex(); RAND_seed((const void *)seed, seedSize); // Check if the PRNG has been seeded with enough randomness if (!RAND_status ()) { releaseMutex(); throw SecurityException(UNABLE_TO_SEED_RNG, NULL); } // Generate 64 bytes of random number using the seed generated above int errcode = RAND_bytes((unsigned char*) &m_pwdKey.data.session_key[0], SESSION_KEYLEN + NONCE_SIZE); releaseMutex(); if (errcode != 1) throw SecurityException(SESSION_KEY_GENERATION_FAILED, NULL); // Get nonce sequence memcpy((char*) &m_nonceSeq, &m_pwdKey.data.nonce[NONCE_RANDOM], NONCE_SEQNUM); if (!m_littleEndian) { m_nonceSeq = swapByteOrderOfLL(m_nonceSeq); // Store the swap bytes back in nonce //intLLong.llVal = m_nonceSeq; memcpy(&m_pwdKey.data.nonce[NONCE_RANDOM], (char*) &m_nonceSeq, NONCE_SEQNUM); } // Set time when session key is generated m_keyTime = get_msts(); }
int lockManagerWriteLockHashed(int xid, byte * dat, int datLen) { if(xid == -1) { return 0; } pthread_mutex_lock(&xid_table_mutex); pblHashTable_t * xidLocks = pblHtLookup_r(xidLockTable, &xid, sizeof(int)); if(!xidLocks) { xidLocks = lockManagerBeginTransactionUnlocked(xid); } long currentLockLevel = (long)pblHtLookup_r(xidLocks, dat, datLen); // printf("xid %d write lock (%d)\n", xid, currentLockLevel); int me = 0; pthread_mutex_unlock(&xid_table_mutex); if(currentLockLevel >= LM_WRITELOCK) { return 0; } else if(currentLockLevel == LM_READLOCK) { me = 1; } pthread_mutex_t * mut = getMutex(dat, datLen); pthread_mutex_lock(mut); lock * ridLock = pblHtLookup_r(ridLockTable, dat, datLen); if(!ridLock) { ridLock = createLock(dat, datLen); } ridLock->active++; ridLock->waiting++; if(ridLock->writers || (ridLock->readers - me)) { struct timeval tv; int tod_ret = gettimeofday(&tv, NULL); tv.tv_sec++; struct timespec ts; ts.tv_sec = tv.tv_sec; ts.tv_nsec = tv.tv_usec * 1000; if(tod_ret != 0) { perror("Could not get time of day"); return LLADD_INTERNAL_ERROR; } while(ridLock->writers || (ridLock->readers - me)) { int lockret = pthread_cond_timedwait(&ridLock->writeOK, mut, &ts); if(lockret == ETIMEDOUT) { ridLock->waiting--; ridLock->active--; pthread_mutex_unlock(mut); return LLADD_DEADLOCK; } } } ridLock->waiting--; if(currentLockLevel == 0) { ridLock->readers++; ridLock->writers++; } else if (currentLockLevel == LM_READLOCK) { ridLock->writers++; pblHtRemove_r(xidLocks, dat, datLen); } if(currentLockLevel != LM_WRITELOCK) { pblHtInsert_r(xidLocks, dat, datLen, (void*)LM_WRITELOCK); } ridLock->active--; pthread_mutex_unlock(mut); return 0; }