xref: /illumos-gate/usr/src/cmd/krb5/kadmin/dbutil/util.c (revision b210e77709da8e42dfe621e10ccf4be504206058)
1 /*
2  * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
3  *
4  *	Openvision retains the copyright to derivative works of
5  *	this source code.  Do *NOT* create a derivative of this
6  *	source code before consulting with your legal department.
7  *	Do *NOT* integrate *ANY* of this source code into another
8  *	product before consulting with your legal department.
9  *
10  *	For further information, read the top-level Openvision
11  *	copyright which is contained in the top-level MIT Kerberos
12  *	copyright.
13  *
14  * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
15  *
16  */
17 
18 
19 /*
20  * admin/edit/util.c
21  *
22  * Copyright 1992 by the Massachusetts Institute of Technology.
23  * All Rights Reserved.
24  *
25  * Export of this software from the United States of America may
26  *   require a specific license from the United States Government.
27  *   It is the responsibility of any person or organization contemplating
28  *   export to obtain such a license before exporting.
29  *
30  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
31  * distribute this software and its documentation for any purpose and
32  * without fee is hereby granted, provided that the above copyright
33  * notice appear in all copies and that both that copyright notice and
34  * this permission notice appear in supporting documentation, and that
35  * the name of M.I.T. not be used in advertising or publicity pertaining
36  * to distribution of the software without specific, written prior
37  * permission.  Furthermore if you modify this software you must label
38  * your software as modified software and not distribute it in such a
39  * fashion that it might be confused with the original M.I.T. software.
40  * M.I.T. makes no representations about the suitability of
41  * this software for any purpose.  It is provided "as is" without express
42  * or implied warranty.
43  *
44  * Utilities for kdb5_edit.
45  *
46  * Some routines derived from code contributed by the Sandia National
47  * Laboratories.  Sandia National Laboratories also makes no
48  * representations about the suitability of the modifications, or
49  * additions to this software for any purpose.  It is provided "as is"
50  * without express or implied warranty.
51  *
52  */
53 
54 #include <k5-int.h>
55 #include "./kdb5_edit.h"
56 
57 #ifndef HAVE_STRSTR
58 char *
59 strstr(s1, s2)
60 char *s1;
61 char *s2;
62 {
63    int s2len;
64    int i;
65    char *temp_ptr;
66 
67    temp_ptr = s1;
68    for ( i = 0; i < strlen(s1); i++) {
69         if (memcmp(temp_ptr, s2, strlen(s2)) == 0) return(temp_ptr);
70         temp_ptr += 1;
71    }
72    return ((char *) 0);
73 }
74 #endif	/* HAVE_STRSTR */
75 
76 void
77 parse_token(token_in, must_be_first_char, num_tokens, tokens_out)
78 char *token_in;
79 int  *must_be_first_char;
80 int  *num_tokens;
81 char *tokens_out;
82 {
83     int i, j;
84     int token_count = 0;
85 
86     i = 0;
87     j = 0;
88 
89 	/* Eliminate Up Front Asterisks */
90     *must_be_first_char = 1;
91     for (i = 0; token_in[i] == '*'; i++) {
92 	*must_be_first_char = 0;
93     }
94 
95     if (i == strlen(token_in)) {
96 	*num_tokens = 0;
97 	return;
98     }
99 
100 	/* Fill first token_out */
101     token_count++;
102     while ((token_in[i] != '*') && (token_in[i] != '\0')) {
103 	tokens_out[j] = token_in[i];
104         j++;
105 	i++;
106     }
107 
108     if (i == strlen(token_in)) {
109 	tokens_out[j] = '\0';
110 	*num_tokens = token_count;
111 	return;
112     }
113 
114 	/* Then All Subsequent Tokens */
115     while (i < strlen(token_in)) {
116 	if (token_in[i] == '*') {
117 	   token_count++;
118 	   tokens_out[j] = '\t';
119 	} else {
120 	   tokens_out[j] = token_in[i];
121 	}
122 	i++;
123 	j++;
124     }
125     tokens_out[j] = '\0';
126 
127     if (tokens_out[j - 1] == '\t') {
128 	token_count--;
129 	tokens_out[j - 1] = '\0';
130     }
131 
132     *num_tokens = token_count;
133     return;
134 }
135 
136 int
137 check_for_match(search_field, must_be_first_character, chk_entry,
138 		num_tokens, type)
139 int must_be_first_character;
140 char *search_field;
141 krb5_db_entry *chk_entry;
142 int num_tokens;
143 int type;
144 {
145     char token1[256];
146     char *found1;
147     char token2[256];
148     char *found2;
149     char token3[256];
150     char *found3;
151     char *local_entry;
152 
153     local_entry = chk_entry->princ->data[type].data;
154 
155     token1[0] = token2[0] = token3[0] = '\0';
156 
157     (void) sscanf(search_field, "%s\t%s\t%s", token1, token2, token3);
158 
159     found1 = strstr(local_entry, token1);
160 
161     if (must_be_first_character && (found1 != local_entry)) return(0);
162 
163     if (found1 && (num_tokens == 1)) return(1);
164 
165     if (found1 && (num_tokens > 1)) {
166 	found2 = strstr(local_entry, token2);
167 	if (found2 && (found2 > found1) && (num_tokens == 2)) return(1);
168     }
169 
170     if ((found2 > found1) && (num_tokens == 3)) {
171 	found3 = strstr(local_entry, token3);
172        	if (found3 && (found3 > found2) && (found2 > found1)) return(1);
173     }
174     return(0);
175 }
176 
177