xref: /freebsd/lib/libiscsiutil/utils.c (revision dd3603749cb7f20a628f04d595b105962b21a3d2)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2012 The FreeBSD Foundation
5  *
6  * This software was developed by Edward Tomasz Napierala under sponsorship
7  * from the FreeBSD Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <ctype.h>
32 #include <string.h>
33 
34 #include "libiscsiutil.h"
35 
36 #define	MAX_NAME_LEN			223
37 
38 char *
checked_strdup(const char * s)39 checked_strdup(const char *s)
40 {
41 	char *c;
42 
43 	c = strdup(s);
44 	if (c == NULL)
45 		log_err(1, "strdup");
46 	return (c);
47 }
48 
49 bool
valid_iscsi_name(const char * name,void (* warn_fn)(const char *,...))50 valid_iscsi_name(const char *name, void (*warn_fn)(const char *, ...))
51 {
52 	int i;
53 
54 	if (strlen(name) >= MAX_NAME_LEN) {
55 		warn_fn("overlong name for target \"%s\"; max length allowed "
56 		    "by iSCSI specification is %d characters",
57 		    name, MAX_NAME_LEN);
58 		return (false);
59 	}
60 
61 	/*
62 	 * In the cases below, we don't return an error, just in case the admin
63 	 * was right, and we're wrong.
64 	 */
65 	if (strncasecmp(name, "iqn.", strlen("iqn.")) == 0) {
66 		for (i = strlen("iqn."); name[i] != '\0'; i++) {
67 			/*
68 			 * XXX: We should verify UTF-8 normalisation, as defined
69 			 *      by 3.2.6.2: iSCSI Name Encoding.
70 			 */
71 			if (isalnum(name[i]))
72 				continue;
73 			if (name[i] == '-' || name[i] == '.' || name[i] == ':')
74 				continue;
75 			warn_fn("invalid character \"%c\" in target name "
76 			    "\"%s\"; allowed characters are letters, digits, "
77 			    "'-', '.', and ':'", name[i], name);
78 			break;
79 		}
80 		/*
81 		 * XXX: Check more stuff: valid date and a valid reversed domain.
82 		 */
83 	} else if (strncasecmp(name, "eui.", strlen("eui.")) == 0) {
84 		if (strlen(name) != strlen("eui.") + 16)
85 			warn_fn("invalid target name \"%s\"; the \"eui.\" "
86 			    "should be followed by exactly 16 hexadecimal "
87 			    "digits", name);
88 		for (i = strlen("eui."); name[i] != '\0'; i++) {
89 			if (!isxdigit(name[i])) {
90 				warn_fn("invalid character \"%c\" in target "
91 				    "name \"%s\"; allowed characters are 1-9 "
92 				    "and A-F", name[i], name);
93 				break;
94 			}
95 		}
96 	} else if (strncasecmp(name, "naa.", strlen("naa.")) == 0) {
97 		if (strlen(name) > strlen("naa.") + 32)
98 			warn_fn("invalid target name \"%s\"; the \"naa.\" "
99 			    "should be followed by at most 32 hexadecimal "
100 			    "digits", name);
101 		for (i = strlen("naa."); name[i] != '\0'; i++) {
102 			if (!isxdigit(name[i])) {
103 				warn_fn("invalid character \"%c\" in target "
104 				    "name \"%s\"; allowed characters are 1-9 "
105 				    "and A-F", name[i], name);
106 				break;
107 			}
108 		}
109 	} else {
110 		warn_fn("invalid target name \"%s\"; should start with "
111 		    "either \"iqn.\", \"eui.\", or \"naa.\"",
112 		    name);
113 	}
114 	return (true);
115 }
116