xref: /titanic_44/usr/src/cmd/krb5/kadmin/dbutil/util.c (revision 18c2aff776a775d34a4c9893a4c72e0434d68e36)
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 #define KDB5_DISPATCH
57 #define KRB5_KDB5_DBM__
58 #include <k5-int.h>
59 /* #define these to avoid an indirection function; for future implementations,
60    these may be redirected from a dispatch table/routine */
61 #define krb5_dbm_db_set_name krb5_db_set_name
62 #define krb5_dbm_db_set_nonblocking krb5_db_set_nonblocking
63 #define krb5_dbm_db_init krb5_db_init
64 #define krb5_dbm_db_get_age krb5_db_get_age
65 #define krb5_dbm_db_create krb5_db_create
66 #define krb5_dbm_db_rename krb5_db_rename
67 #define krb5_dbm_db_get_principal krb5_db_get_principal
68 #define krb5_dbm_db_free_principal krb5_db_free_principal
69 #define krb5_dbm_db_put_principal krb5_db_put_principal
70 #define krb5_dbm_db_delete_principal krb5_db_delete_principal
71 #define krb5_dbm_db_lock krb5_db_lock
72 #define krb5_dbm_db_unlock krb5_db_unlock
73 #define krb5_dbm_db_set_lockmode krb5_db_set_lockmode
74 #define krb5_dbm_db_close_database krb5_db_close_database
75 #define krb5_dbm_db_open_database krb5_db_open_database
76 
77 #include "./kdb5_edit.h"
78 
79 #ifndef HAVE_STRSTR
80 char *
81 strstr(s1, s2)
82 char *s1;
83 char *s2;
84 {
85    int s2len;
86    int i;
87    char *temp_ptr;
88 
89    temp_ptr = s1;
90    for ( i = 0; i < strlen(s1); i++) {
91         if (memcmp(temp_ptr, s2, strlen(s2)) == 0) return(temp_ptr);
92         temp_ptr += 1;
93    }
94    return ((char *) 0);
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 
122 	/* Fill first token_out */
123     token_count++;
124     while ((token_in[i] != '*') && (token_in[i] != '\0')) {
125 	tokens_out[j] = token_in[i];
126         j++;
127 	i++;
128     }
129 
130     if (i == strlen(token_in)) {
131 	tokens_out[j] = '\0';
132 	*num_tokens = token_count;
133 	return;
134     }
135 
136 	/* Then All Subsequent Tokens */
137     while (i < strlen(token_in)) {
138 	if (token_in[i] == '*') {
139 	   token_count++;
140 	   tokens_out[j] = '\t';
141 	} else {
142 	   tokens_out[j] = token_in[i];
143 	}
144 	i++;
145 	j++;
146     }
147     tokens_out[j] = '\0';
148 
149     if (tokens_out[j - 1] == '\t') {
150 	token_count--;
151 	tokens_out[j - 1] = '\0';
152     }
153 
154     *num_tokens = token_count;
155     return;
156 }
157 
158 int
159 check_for_match(search_field, must_be_first_character, chk_entry,
160 		num_tokens, type)
161 int must_be_first_character;
162 char *search_field;
163 krb5_db_entry *chk_entry;
164 int num_tokens;
165 int type;
166 {
167     char token1[256];
168     char *found1;
169     char token2[256];
170     char *found2;
171     char token3[256];
172     char *found3;
173     char *local_entry;
174 
175     local_entry = chk_entry->princ->data[type].data;
176 
177     token1[0] = token2[0] = token3[0] = '\0';
178 
179     (void) sscanf(search_field, "%s\t%s\t%s", token1, token2, token3);
180 
181     found1 = strstr(local_entry, token1);
182 
183     if (must_be_first_character && (found1 != local_entry)) return(0);
184 
185     if (found1 && (num_tokens == 1)) return(1);
186 
187     if (found1 && (num_tokens > 1)) {
188 	found2 = strstr(local_entry, token2);
189 	if (found2 && (found2 > found1) && (num_tokens == 2)) return(1);
190     }
191 
192     if ((found2 > found1) && (num_tokens == 3)) {
193 	found3 = strstr(local_entry, token3);
194        	if (found3 && (found3 > found2) && (found2 > found1)) return(1);
195     }
196     return(0);
197 }
198 
199