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