xref: /titanic_44/usr/src/cmd/krb5/kadmin/dbutil/util.c (revision 749f21d359d8fbd020c974a1a5227316221bfc9c)
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.  M.I.T. makes no representations about the suitability of
40  * this software for any purpose.  It is provided "as is" without express
41  * or implied warranty.
42  *
43  * Utilities for kdb5_edit.
44  *
45  * Some routines derived from code contributed by the Sandia National
46  * Laboratories.  Sandia National Laboratories also makes no
47  * representations about the suitability of the modifications, or
48  * additions to this software for any purpose.  It is provided "as is"
49  * without express or implied warranty.
50  *
51  */
52 
53 #define KDB5_DISPATCH
54 #define KRB5_KDB5_DBM__
55 #include <k5-int.h>
56 /* #define these to avoid an indirection function; for future implementations,
57    these may be redirected from a dispatch table/routine */
58 #define krb5_dbm_db_set_name krb5_db_set_name
59 #define krb5_dbm_db_set_nonblocking krb5_db_set_nonblocking
60 #define krb5_dbm_db_init krb5_db_init
61 #define krb5_dbm_db_get_age krb5_db_get_age
62 #define krb5_dbm_db_create krb5_db_create
63 #define krb5_dbm_db_rename krb5_db_rename
64 #define krb5_dbm_db_get_principal krb5_db_get_principal
65 #define krb5_dbm_db_free_principal krb5_db_free_principal
66 #define krb5_dbm_db_put_principal krb5_db_put_principal
67 #define krb5_dbm_db_delete_principal krb5_db_delete_principal
68 #define krb5_dbm_db_lock krb5_db_lock
69 #define krb5_dbm_db_unlock krb5_db_unlock
70 #define krb5_dbm_db_set_lockmode krb5_db_set_lockmode
71 #define krb5_dbm_db_close_database krb5_db_close_database
72 #define krb5_dbm_db_open_database krb5_db_open_database
73 
74 #include <kadm5/admin.h>
75 #include "./kdb5_edit.h"
76 
77 #ifndef HAVE_STRSTR
78 char *
79 strstr(s1, s2)
80 char *s1;
81 char *s2;
82 {
83 	int s2len;
84 	int i;
85 	char *temp_ptr;
86 
87 	temp_ptr = s1;
88 	for (i = 0; i < strlen(s1); i++) {
89 		if (memcmp(temp_ptr, s2, strlen(s2)) == 0)
90 			return (temp_ptr);
91 		temp_ptr += 1;
92 	}
93 	return ((char *) 0);
94 }
95 
96 #endif			   /* HAVE_STRSTR */
97 
98 void
99 parse_token(token_in, must_be_first_char, num_tokens, tokens_out)
100 char *token_in;
101 int *must_be_first_char;
102 int *num_tokens;
103 char *tokens_out;
104 {
105 	int i, j;
106 	int token_count = 0;
107 
108 	i = 0;
109 	j = 0;
110 
111 	/* Eliminate Up Front Asterisks */
112 	*must_be_first_char = 1;
113 	for (i = 0; token_in[i] == '*'; i++) {
114 		*must_be_first_char = 0;
115 	}
116 
117 	if (i == strlen(token_in)) {
118 		*num_tokens = 0;
119 		return;
120 	}
121 	/* Fill first token_out */
122 	token_count++;
123 	while ((token_in[i] != '*') && (token_in[i] != '\0')) {
124 		tokens_out[j] = token_in[i];
125 		j++;
126 		i++;
127 	}
128 
129 	if (i == strlen(token_in)) {
130 		tokens_out[j] = '\0';
131 		*num_tokens = token_count;
132 		return;
133 	}
134 	/* Then All Subsequent Tokens */
135 	while (i < strlen(token_in)) {
136 		if (token_in[i] == '*') {
137 			token_count++;
138 			tokens_out[j] = '\t';
139 		} else {
140 			tokens_out[j] = token_in[i];
141 		}
142 		i++;
143 		j++;
144 	}
145 	tokens_out[j] = '\0';
146 
147 	if (tokens_out[j - 1] == '\t') {
148 		token_count--;
149 		tokens_out[j - 1] = '\0';
150 	}
151 	*num_tokens = token_count;
152 }
153 
154 int
155 check_for_match(search_field, must_be_first_character, chk_entry,
156     num_tokens, type)
157 int must_be_first_character;
158 char *search_field;
159 krb5_db_entry *chk_entry;
160 int num_tokens;
161 int type;
162 {
163 	char token1[256];
164 	char *found1;
165 	char token2[256];
166 	char *found2;
167 	char token3[256];
168 	char *found3;
169 	char *local_entry;
170 
171 	local_entry = chk_entry->princ->data[type].data;
172 
173 	token1[0] = token2[0] = token3[0] = '\0';
174 
175 	(void) sscanf(search_field, "%s\t%s\t%s", token1, token2, token3);
176 
177 	found1 = strstr(local_entry, token1);
178 
179 	if (must_be_first_character && (found1 != local_entry))
180 		return (0);
181 
182 	if (found1 && (num_tokens == 1))
183 		return (1);
184 
185 	if (found1 && (num_tokens > 1)) {
186 		found2 = strstr(local_entry, token2);
187 		if (found2 && (found2 > found1) && (num_tokens == 2))
188 			return (1);
189 	}
190 	if ((found2 > found1) && (num_tokens == 3)) {
191 		found3 = strstr(local_entry, token3);
192 		if (found3 && (found3 > found2) && (found2 > found1))
193 			return (1);
194 	}
195 	return (0);
196 }
197