1 /* This file is part of pam-modules. 2 Copyright (C) 2008, 2010-2012, 2014-2015, 2018 Sergey Poznyakoff 3 4 This program is free software; you can redistribute it and/or modify it 5 under the terms of the GNU General Public License as published by the 6 Free Software Foundation; either version 3 of the License, or (at your 7 option) any later version. 8 9 This program is distributed in the hope that it will be useful, 10 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 GNU General Public License for more details. 13 14 You should have received a copy of the GNU General Public License along 15 with this program. If not, see <http://www.gnu.org/licenses/>. */ 16 17 #include <graypam.h> 18 19 jmp_buf gray_pam_jmp; gray_escape_string(gray_slist_t slist,const char * str,size_t len)20 21 void 22 gray_raise(const char *fmt, ...) 23 { 24 va_list ap; 25 va_start(ap, fmt); 26 _pam_vlog(LOG_ERR, fmt, ap); 27 va_end(ap); 28 longjmp(gray_pam_jmp, 1); 29 } 30 31 void * 32 gray_malloc(size_t size) 33 { 34 void *p = malloc(size); 35 if (!p) 36 gray_raise("Not enough memory"); 37 return p; 38 } 39 40 void * 41 gray_zalloc(size_t size) 42 { 43 void *p = malloc(size); 44 if (!p) 45 gray_raise("Not enough memory"); 46 memset(p, 0, size); 47 return p; 48 } 49 50 void * 51 gray_calloc(size_t count, size_t size) 52 { 53 return gray_zalloc(count * size); 54 } 55 56 void * 57 gray_realloc(void *ptr, size_t size) 58 { 59 ptr = realloc(ptr, size); 60 if (!ptr) 61 gray_raise("Not enough memory"); 62 return ptr; 63 } 64 65 void * 66 gray_2nrealloc(void *ptr, size_t *pcount, size_t elsiz) 67 { 68 size_t count = *pcount; 69 70 if (!ptr) { 71 if (!count) 72 count = *pcount = 16; 73 return gray_calloc(count, elsiz); 74 } 75 if ((size_t)-1 / 2 / elsiz <= count) 76 gray_raise("Not enough memory"); 77 count *= 2; 78 *pcount = count; 79 return gray_realloc(ptr, count * elsiz); 80 } 81 82 83 char * 84 gray_strdup(const char *str) 85 { 86 char *p; 87 88 if (!str) 89 return NULL; 90 p = gray_malloc(strlen(str) + 1); 91 return strcpy(p, str); 92 } 93 94 95 void 96 gray_pam_delete(char *x) 97 { 98 PAM_OVERWRITE(x); 99 free(x); 100 } 101 102 void 103 gray_cleanup_string(pam_handle_t *pamh, void *x, int error_status) 104 { 105 gray_pam_delete(x); 106 } 107 108 void 109 gray_cleanup_regex(pam_handle_t *pamh, void *x, int error_status) 110 { 111 regfree((regex_t*)x); 112 } 113 114 void 115 gray_make_str(pam_handle_t *pamh, const char *str, const char *name, 116 char **ret) 117 { 118 int retval; 119 char *newstr = XSTRDUP(str); 120 121 retval = pam_set_data(pamh, name, (void *)newstr, gray_cleanup_string); 122 if (retval != PAM_SUCCESS) { 123 _pam_log(LOG_CRIT, 124 "can't keep data [%s]: %s", 125 name, 126 pam_strerror(pamh, retval)); 127 gray_pam_delete(newstr); 128 } else { 129 *ret = newstr; 130 newstr = NULL; 131 } 132 } 133 134 135