Initial import from FreeBSD RELENG_4:
[dragonfly.git] / crypto / kerberosIV / lib / krb / parse_name.c
1 /*
2  * Copyright (c) 1995, 1996, 1997 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  * 
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 
17  * 3. Neither the name of the Institute nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  * 
21  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 #include "krb_locl.h"
35
36 RCSID("$Id: parse_name.c,v 1.7 1999/12/02 16:58:43 joda Exp $");
37
38 int
39 krb_parse_name(const char *fullname, krb_principal *principal)
40 {
41     const char *p;
42     char *ns, *np;
43     enum {n, i, r} pos = n;
44     int quote = 0;
45     ns = np = principal->name;
46
47     principal->name[0] = 0;
48     principal->instance[0] = 0;
49     principal->realm[0] = 0;
50
51     for(p = fullname; *p; p++){
52         if(np - ns == ANAME_SZ - 1) /* XXX they have the same size */
53             return KNAME_FMT;
54         if(quote){
55             *np++ = *p;
56             quote = 0;
57             continue;
58         }
59         if(*p == '\\')
60             quote = 1;
61         else if(*p == '.' && pos == n){
62             *np = 0;
63             ns = np = principal->instance;
64             pos = i;
65         }else if(*p == '@' && (pos == n || pos == i)){
66             *np = 0;
67             ns = np = principal->realm;
68             pos = r;
69         }else
70             *np++ = *p;
71     }
72     *np = 0;
73     if(quote || principal->name[0] == 0)
74         return KNAME_FMT;
75     return KSUCCESS;
76 }
77
78 int
79 kname_parse(char *np, char *ip, char *rp, char *fullname)
80 {
81     krb_principal p;
82     int ret;
83     if((ret = krb_parse_name(fullname, &p)) == 0){
84         strlcpy (np, p.name, ANAME_SZ);
85         strlcpy (ip, p.instance, INST_SZ);
86         if(p.realm[0])
87             strlcpy (rp, p.realm, REALM_SZ);
88     }
89     return ret;
90 }
91 /*
92  * k_isname() returns 1 if the given name is a syntactically legitimate
93  * Kerberos name; returns 0 if it's not.
94  */
95
96 int
97 k_isname(char *s)
98 {
99     char c;
100     int backslash = 0;
101
102     if (!*s)
103         return 0;
104     if (strlen(s) > ANAME_SZ - 1)
105         return 0;
106     while ((c = *s++)) {
107         if (backslash) {
108             backslash = 0;
109             continue;
110         }
111         switch(c) {
112         case '\\':
113             backslash = 1;
114             break;
115         case '.':
116             return 0;
117             /* break; */
118         case '@':
119             return 0;
120             /* break; */
121         }
122     }
123     return 1;
124 }
125
126
127 /*
128  * k_isinst() returns 1 if the given name is a syntactically legitimate
129  * Kerberos instance; returns 0 if it's not.
130  */
131
132 int
133 k_isinst(char *s)
134 {
135     char c;
136     int backslash = 0;
137
138     if (strlen(s) > INST_SZ - 1)
139         return 0;
140     while ((c = *s++)) {
141         if (backslash) {
142             backslash = 0;
143             continue;
144         }
145         switch(c) {
146         case '\\':
147             backslash = 1;
148             break;
149         case '.':
150 #if     INSTANCE_DOTS_OK
151             break;
152 #else   /* INSTANCE_DOTS_OK */
153             return 0; 
154 #endif  /* INSTANCE_DOTS_OK */
155             /* break; */
156         case '@':
157             return 0;
158             /* break; */
159         }
160     }
161     return 1;
162 }
163
164 /*
165  * k_isrealm() returns 1 if the given name is a syntactically legitimate
166  * Kerberos realm; returns 0 if it's not.
167  */
168
169 int
170 k_isrealm(char *s)
171 {
172     char c;
173     int backslash = 0;
174
175     if (!*s)
176         return 0;
177     if (strlen(s) > REALM_SZ - 1)
178         return 0;
179     while ((c = *s++)) {
180         if (backslash) {
181             backslash = 0;
182             continue;
183         }
184         switch(c) {
185         case '\\':
186             backslash = 1;
187             break;
188         case '@':
189             return 0;
190             /* break; */
191         }
192     }
193     return 1;
194 }