Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:15

0001 /*
0002  * Copyright (c) 2014 embedded brains GmbH & Co. KG
0003  *
0004  * Redistribution and use in source and binary forms, with or without
0005  * modification, are permitted provided that the following conditions
0006  * are met:
0007  * 1. Redistributions of source code must retain the above copyright
0008  *    notice, this list of conditions and the following disclaimer.
0009  * 2. Redistributions in binary form must reproduce the above copyright
0010  *    notice, this list of conditions and the following disclaimer in the
0011  *    documentation and/or other materials provided with the distribution.
0012  *
0013  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
0014  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
0015  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
0016  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
0017  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
0018  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0019  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0020  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0021  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0022  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
0023  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0024  */
0025 
0026 #include <crypt.h>
0027 #include <string.h>
0028 
0029 static char *
0030 cf_default_func(const char *pw, const char *salt, struct crypt_data *data)
0031 {
0032     (void)salt;
0033 
0034     strncpy(&data->buffer[0], pw, sizeof(data->buffer) - 1);
0035     data->buffer[sizeof(data->buffer) - 1] = '\0';
0036 
0037     return (&data->buffer[0]);
0038 }
0039 
0040 static struct crypt_format cf_default =
0041   CRYPT_FORMAT_INITIALIZER(cf_default_func, NULL);
0042 
0043 static SLIST_HEAD(, crypt_format) cf_head = {
0044     &cf_default
0045 };
0046 
0047 void crypt_add_format(struct crypt_format *cf)
0048 {
0049     if (cf->link.sle_next == NULL)
0050         SLIST_INSERT_HEAD(&cf_head, cf, link);
0051 }
0052 
0053 char *
0054 crypt_r(const char *passwd, const char *salt, struct crypt_data *data)
0055 {
0056     const struct crypt_format *cf;
0057 
0058     SLIST_FOREACH(cf, &cf_head, link)
0059         if (cf->magic != NULL && strstr(salt, cf->magic) == salt)
0060             return (cf->func(passwd, salt, data));
0061 
0062     cf = SLIST_FIRST(&cf_head);
0063 
0064     return (cf->func(passwd, salt, data));
0065 }