root/shared/session.c
/*DEFINITIONS
This source file includes following definitions.1 /* 2 ** Copyright (c) 2024 Amelia Campbell 3 ** 4 ** Redistribution and use in source and binary forms, with or without 5 ** modification, are permitted provided that the following conditions are 6 ** met: 7 ** 8 ** 1. Redistributions of source code must retain the above copyright 9 ** notice, this list of conditions and the following disclaimer. 10 ** 2. Redistributions in binary form must reproduce the above copyright 11 ** notice, this list of conditions and the following disclaimer in the 12 ** documentation and/or other materials provided with the 13 ** distribution. 14 ** 3. Neither the name of the copyright holder nor the names of its 15 ** contributors may be used to endorse or promote products derived 16 ** from this software without specific prior written permission. 17 ** 18 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 19 ** IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20 ** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 21 ** PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 ** HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #include <fcntl.h> 32 #include <stdio.h> 33 #include <stdlib.h> 34 #include <string.h> 35 #include <errno.h> 36 #include <unistd.h> 37 #include <sys/types.h> 38 #include <sys/mman.h> 39 #include <sys/stat.h> 40 #include <openssl/evp.h> 41 42 #include "macro.h" 43 #include "term.h" 44 #include "hash.h" 45 46 #define MAXNAME 128 47 #define SESZ HASH_TSZ 48 49 unsigned char *session = NULL; 50 char sname[MAXNAME]; 51 52 static void session_name(void) 53 { 54 char *user; 55 if (!(user = getenv("USER"))) 56 fatal("agent failed to acquire name from USER env\n"); 57 58 snprintf(sname, MAXNAME, "/dsesU%s", user); 59 } 60 61 62 void session_create(unsigned char *k, size_t kl) 63 { 64 hash_t skey; 65 int fd; 66 session_name(); 67 if ((fd = shm_open(sname, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR))<0) 68 fatal("agent shm failed to create: %s\n", strerror(errno)); 69 if (ftruncate(fd, SESZ) < 0) 70 fatal("agent shm could not be truncated to correct size\n"); 71 if (MAP_FAILED == (session = mmap(NULL, SESZ, 72 PROT_READ|PROT_WRITE, 73 MAP_FILE|MAP_SHARED, fd, 0))) 74 fatal("agent shm could not be mapped into memory\n"); 75 76 genkey(k, kl, &skey); 77 memcpy(session, skey.r, SESZ); 78 munmap(session, SESZ); 79 close(fd); 80 } 81 82 83 void session_destroy(void) 84 { 85 session_name(); 86 if (shm_unlink(sname) < 0) 87 fatal("failed to unlink agent shm (does it even exist?)\n"); 88 } 89 90 91 int session_open(void) 92 { 93 int fd; 94 session_name(); 95 if ((fd = shm_open(sname, O_RDONLY, 0)) < 0) 96 return -1; 97 if (MAP_FAILED == (session = mmap(NULL, SESZ, 98 PROT_READ, 99 MAP_FILE, fd, 0))) 100 fatal("agent shm could not be mapped into memory\n"); 101 close(fd); 102 return 0; 103 } 104 105 106 void session_close(void) 107 { 108 if (!session) return; 109 munmap(session, SESZ); 110 }
/*