48 const char *message, ...);
51#define SHM_KEY (key_t)0x0d50d5ec
52#define SEM_KEY (key_t)0x0d50d5ec
53#define SHM_PERM S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP
54#define SEM_PERM S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP
56#ifndef HAVE_UNION_SEMUN
69prompt_pass(
char *prompt)
73 struct termios oldt, newt;
75 if (prompt == NULL)
return NULL;
80 if (isatty(fileno(stdin))) {
81 if (tcgetattr(fileno(stdin), &oldt) != 0)
return NULL;
83 newt.c_lflag &= ~ECHO;
84 if (tcsetattr(fileno(stdin), TCSAFLUSH, &newt) != 0)
return NULL;
96 if (isatty(fileno(stdin))) {
97 tcsetattr(fileno(stdin), TCSAFLUSH, &oldt);
117 "Could not access the semaphore: %s", strerror(errno));
123 if (semctl(semid, 0, SETVAL, arg) == -1) {
125 "Could not set value on the semaphore: %s", strerror(errno));
132 if (semctl(semid, 0, IPC_STAT, arg) != 0) {
134 "Could not stat the semaphore: %s", strerror(errno));
140 buf.sem_perm.gid != getegid())
143 "Bad permissions on the semaphore, please read Getting Help/Troubleshooting on OpenDNSSEC Wiki about this.");
151hsm_sem_wait(
int semid)
153 struct sembuf sb = { 0, -1, 0 };
155 if (semop(semid, &sb, 1) == -1) {
157 "Could not lock the semaphore: %s", strerror(errno));
165hsm_sem_post(
int semid)
167 struct sembuf sb = { 0, 1, 0 };
169 if (semop(semid, &sb, 1) == -1) {
171 "Could not unlock the semaphore: %s", strerror(errno));
192 "Could not access the shared memory: %s", strerror(errno));
202 if (shmctl(shmid, IPC_STAT, &buf) != 0) {
204 "Could not stat the semaphore: %s", strerror(errno));
209 if ((
size_t)buf.shm_segsz != shmsize) {
211 "Bad memory size, please read Getting Help/Troubleshooting on OpenDNSSEC Wiki about this.");
217 buf.shm_perm.gid != getegid())
220 "Bad permissions on the shared memory, please read Getting Help/Troubleshooting on OpenDNSSEC Wiki about this.");
238 char *prompt_pin = NULL;
239 unsigned int size = 0;
243 if (repository == NULL)
return NULL;
247 semid = hsm_sem_open();
248 if (semid == -1)
return NULL;
251 if (hsm_sem_wait(semid) != 0)
return NULL;
254 shmid = hsm_shm_open();
261 pins = (
char *)shmat(shmid, NULL, 0);
262 if (pins == (
char *)-1) {
272 size = strlen(&pins[index]);
274 memcpy(pin, &pins[index], size);
286 snprintf(prompt, 64,
"Enter PIN for token %s: ", repository);
287 prompt_pin = prompt_pass(prompt);
288 if (prompt_pin == NULL) {
298 size = strlen(prompt_pin);
301 memcpy(pin, prompt_pin, size);
304 memset(prompt_pin,
'\0', strlen(prompt_pin));
333 unsigned int size = 0;
337 if (repository == NULL)
return NULL;
349 semid = hsm_sem_open();
350 if (semid == -1)
return NULL;
353 if (hsm_sem_wait(semid) != 0)
return NULL;
356 shmid = hsm_shm_open();
363 pins = (
char *)shmat(shmid, NULL, 0);
364 if (pins == (
char *)-1) {
374 if (pins[index] ==
'\0') {
376 "No PIN in shared memory. "
377 "Please login with \"ods-hsmutil login\"");
388 "Removed bad PIN in shared memory. "
389 "Please login again with \"ods-hsmutil login\"");
397 size = strlen(&pins[index]);
399 memcpy(pin, &pins[index], size);
423 if (errno != ENOENT) {
425 "Could not access the semaphore: %s", strerror(errno));
431 if (semctl(semid, 0, IPC_RMID, arg) != 0) {
433 "Could not delete the semaphore: %s", strerror(errno));
441 if (errno != ENOENT) {
443 "Could not access the shared memory: %s", strerror(errno));
448 if (shmctl(shmid, IPC_RMID, &buf) != 0) {
450 "Could not stat the semaphore: %s", strerror(errno));
#define HSM_MAX_PIN_LENGTH
char * hsm_prompt_pin(unsigned int id, const char *repository, unsigned int mode)
void hsm_ctx_set_error(hsm_ctx_t *ctx, int error, const char *action, const char *message,...)
char * hsm_check_pin(unsigned int id, const char *repository, unsigned int mode)