]> git.sur5r.net Git - openldap/blob - contrib/whois++/describe.c
Annoying little son of a `free' BUG fixed.
[openldap] / contrib / whois++ / describe.c
1 #if !defined(lint)
2 static char copyright[] = "Copyright 1992 The University of Adelaide";
3 #endif
4
5 /*
6  *                      D E S C R I B E
7  *
8  * Author:      Mark R. Prior
9  *              Communications and Systems Branch
10  *              Information Technology Division
11  *              The University of Adelaide
12  * E-mail:      mrp@itd.adelaide.edu.au
13  * Date:        October 1992
14  * Version:     1.7
15  * Description:
16  *              A module implementing the describe whois++ command.
17  *
18  * Redistribution and use in source and binary forms are permitted
19  * provided that the above copyright notice and this paragraph are
20  * duplicated in all such forms and that any documentation,
21  * advertising materials, and other materials related to such
22  * distribution and use acknowledge that the software was developed
23  * by the University of Adelaide. The name of the University may not
24  * be used to endorse or promote products derived from this software
25  * without specific prior written permission.
26  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
27  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
28  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  */
30
31 #include "whois++.h"
32
33 int     displayDescribe( ld, organisation )
34 LDAP    *ld;
35 char    *organisation;
36 {
37
38         int             i, len1, len2;
39         LDAPMessage     *result, *entry;
40         char            *value, *ptr;
41         char            **values;
42         static char     *masterDSA[] = { "masterDSA", NULL };
43         static char     *manager[] = { "manager", NULL };
44         static char     *roleOccupant[] = { "roleOccupant", NULL };
45         static char     *attributes[] = { "postalAddress", "telephoneNumber",
46                         "facsimileTelephoneNumber", "mail", "lastModifiedBy",
47 #if defined(UOFA)
48                         "preferredName",
49 #endif
50                         NULL };
51         extern char     *index();
52
53         if ( !EQ( language, "english" ) ) 
54                 printFormatted( lineLength, TRUE, stdout,
55                         "The output of the DESCRIBE command must be in english \
56 according to the IAFA services template." );
57
58         printFormatted( lineLength, FALSE, stdout,
59                 "%-19s Whois++", "Service-Name:" );
60         if ( hostname != NULL )
61                 printFormatted( lineLength, FALSE, stdout, "%-19s %s",
62                         "Primary-Name:", hostname );
63         else
64                 printFormatted( lineLength, FALSE, stdout,
65                         "%-19s <unknown>", "Primary-Name:" );
66         printFormatted( lineLength, FALSE, stdout,
67                 "%-19s Whois++ service using LDAP to access a Quipu based",
68                 "Description:" );
69         printFormatted( lineLength, FALSE, stdout,
70                 "%-19s X.500 Directory Service.", "" );
71         printFormatted( lineLength, FALSE, stdout,
72                 "%-19s whois++ protocol on tcp port 43", "Access-Protocol:" );
73         printFormatted( lineLength, FALSE, stdout,
74                 "%-19s whois, x.500, ldap", "Keywords:" );
75         printFormatted( lineLength, FALSE, stdout,
76                 "%-19s 24 hours a day, 7 days a week", "Access-Times:" );
77         printFormatted( lineLength, FALSE, stdout,
78                 "%-19s Open Access", "Policy:" );
79         printFormatted( lineLength, FALSE, stdout,
80                 "%-19s ", "URI:" );
81         if ( contact == NULL ) {
82                 /*
83                  * The contact hasn't identified themselves in the tailor file
84                  * so lets try to work it out by finding out who manages the
85                  * DSA that masters the organisation's entry!
86                  */
87                 if ( debug > 2 )
88                         printFormatted( lineLength, TRUE, stdout,
89                                 "No contact info provided, searching ..." );
90                 ldap_search_s( ld, organisation, LDAP_SCOPE_BASE,
91                         "objectclass=*", masterDSA, 0, &result );
92                 if ( ld->ld_errno != LDAP_SUCCESS ) {
93                         printFormatted( lineLength, FALSE, stdout,
94                                 "%-19s <Unknown>", "Contact:" );
95                         return TRUE;
96                 }
97                 if ( debug > 2 )
98                         printFormatted( lineLength, TRUE, stdout,
99                                 "Looking for the master DSA ..." );
100                 if ( (values = ldap_get_values( ld, result, "masterDSA" )) == NULL
101                         || values[0] == NULL ) {
102                         printFormatted( lineLength, FALSE, stdout,
103                                 "%-19s <Unknown>", "Contact:" );
104                         return TRUE;
105                 }
106                 if ( debug > 2 )
107                         printFormatted( lineLength, TRUE, stdout,
108                                 "Searching for the DSA manager ..." );
109                 ldap_search_s( ld, values[0], LDAP_SCOPE_BASE, "objectclass=*",
110                         manager, 0, &result );
111                 if ( ld->ld_errno != LDAP_SUCCESS ) {
112                         printFormatted( lineLength, FALSE, stdout,
113                                 "%-19s <Unknown>", "Contact:" );
114                         printFormatted( lineLength, TRUE, stdout,
115                                 "Search failed, %s",
116                                 ldap_err2string( ld->ld_errno ) );
117                         return TRUE;
118                 }
119                 if ( (values = ldap_get_values( ld, result, "manager" )) == NULL ) {
120                         printFormatted( lineLength, FALSE, stdout,
121                                 "%-19s <Unknown>", "Contact:" );
122                         return TRUE;
123                 }
124                 if ( debug > 2 )
125                         printFormatted( lineLength, TRUE, stdout,
126                                 "Retrieving the DSA manager's entry ..." );
127                 /*
128                  * we have at least one manager for this DSA but which one is
129                  * the "correct" one to list?
130                  */
131                 len1 = strlen( organisation );
132                 for ( i = 0; values[i] != NULL; i++ )
133                         if ( strlen( values[i] ) >= len1 ) {
134                                 len2 = strlen( values[i] );
135                                 if ( EQ( organisation, &values[i][len2-len1] ) )
136                                         contact = strdup( values[i] );
137                         }
138                 ldap_value_free( values );
139                 if ( contact == NULL ) {
140                         printFormatted( lineLength, FALSE, stdout,
141                                 "%-19s <Unknown>", "Contact:" );
142                         if ( debug )
143                                 printFormatted( lineLength, TRUE, stdout,
144                                         "Cannot find a suitable manager" );
145                         return TRUE;
146                 }
147                 ldap_search_s( ld, contact, LDAP_SCOPE_BASE, "objectclass=*",
148                         roleOccupant, 0, &result );
149                 if ( ld->ld_errno != LDAP_SUCCESS ) {
150                         printFormatted( lineLength, FALSE, stdout,
151                                 "%-19s <Unknown>", "Contact:" );
152                         printFormatted( lineLength, TRUE, stdout,
153                                 "Search failed, %s",
154                                 ldap_err2string( ld->ld_errno ) );
155                         return TRUE;
156                 }
157                 if ( (values = ldap_get_values( ld, result, "roleOccupant" )) != NULL
158                         || values[0] == NULL ) {
159                         free( contact );
160                         /* Just pick one! */
161                         contact = strdup( values[0] );
162                         ldap_value_free( values );
163                 }
164                 if ( debug > 2 )
165                         printFormatted( lineLength, TRUE, stdout,
166                                 "The contact is %s", contact );
167         }
168         ldap_search_s( ld, contact, LDAP_SCOPE_BASE, "objectclass=*", 
169                 attributes, 0, &result );
170         if ( ld->ld_errno != LDAP_SUCCESS ) {
171                 printFormatted( lineLength, FALSE, stdout, "%-19s <Unknown>",
172                         "Contact:" );
173                 printFormatted( lineLength, TRUE, stdout,
174                         "Read for \"%s\" returned error, %s", contact,
175                         ldap_err2string( ld->ld_errno ) );
176         }
177 #if defined(UOFA)
178         if ( (values = ldap_get_values( ld, result, "preferredName" )) != NULL
179                 && values[0] != NULL ) {
180                 printFormatted( lineLength, FALSE, stdout, "%-19s %s",
181                         "Contact:", values[0] );
182                 ldap_value_free( values );
183         } else {
184 #endif
185                 value = ldap_dn2ufn( ptr = ldap_get_dn( ld, result ) );
186                 free( ptr );
187                 if ( (ptr = index( value, ',' )) != NULL )
188                         *ptr = '\0';
189                 printFormatted( lineLength, FALSE, stdout, "%-19s %s",
190                         "Contact:", value );
191                 free( value );
192 #if defined(UOFA)
193         }
194 #endif
195         if ( ( values = ldap_get_values( ld, result, "postalAddress" )) != NULL ) {
196                 for ( i = 0; values[i] != NULL; i++ ) {
197                         printFormatted( lineLength, FALSE, stdout, "%-19s %s",
198                                 "Postal-Address:",
199                                 strtok( values[i], "$" ) );
200                         while ( ( ptr = strtok( NULL, "$" ) ) != NULL )
201                                 printFormatted( lineLength, FALSE, stdout,
202                                         "%-19s%s", "", ptr );
203                 }
204                 ldap_value_free( values );
205         }
206         if ( ( values = ldap_get_values( ld, result, "telephoneNumber" )) != NULL ) {
207                 for ( i = 0; values[i] != NULL; i++ )
208                         printFormatted( lineLength, FALSE, stdout, "%-19s %s",
209                                 "Telephone:", values[i] );
210                 ldap_value_free( values );
211         }
212         if ( ( values = ldap_get_values( ld, result, "facsimileTelephoneNumber" )) != NULL ) {
213                 for ( i = 0; values[i] != NULL; i++ )
214                         printFormatted( lineLength, FALSE, stdout, "%-19s %s",
215                                 "Fax:", values[i] );
216                 ldap_value_free( values );
217         }
218         if ( ( values = ldap_get_values( ld, result, "mail" )) != NULL ) {
219                 for ( i = 0; values[i] != NULL; i++ )
220                         printFormatted( lineLength, FALSE, stdout, "%-19s %s",
221                                 "Electronic-Address:", values[i] );
222                 ldap_value_free( values );
223         }
224         if ( ( values = ldap_get_values( ld, result, "lastModifiedBy" )) != NULL ) {
225                 for ( i = 0; values[i] != NULL; i++ )
226                         printFormatted( lineLength, FALSE, stdout,
227                                 "%-19s \"%s\"", "Modified-By:", values[i] );
228                 ldap_value_free( values );
229         }
230         return FALSE;
231 }