summaryrefslogtreecommitdiffstats
path: root/ldap/clients/dsgw/edit.c
blob: a869501c9f2f285e4e18ca84377a1d0e21bb73ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/** --- BEGIN COPYRIGHT BLOCK ---
 * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
 * Copyright (C) 2005 Red Hat, Inc.
 * All rights reserved.
  --- END COPYRIGHT BLOCK ---  */
/*
 * edit.c -- CGI editable entry display -- HTTP gateway
 */

#include "dsgw.h"
#include "dbtdsgw.h"

static void get_request(char *dn, char *tmplname, 
			char *parent, unsigned long options);


int main( argc, argv, env )
    int		argc;
    char	*argv[];
#ifdef DSGW_DEBUG
    char	*env[];
#endif
{


    char		*dn, *tmplname, *p, *parent;
    unsigned long	options;

    /*
     * If the QUERY_STRING is non-NULL, it looks like this:
     *
     *     template [&CONTEXT=context] [ &INFO=infostring ] [ &ADD ] [ &DN=dn ] \
     *              [&DNATTR=attrname&DNDESC=description]
     *
     * where:
     *   "template" is the name of the edit template to use for display,
     *   "dn" is escaped dn,
     *   "infostring" is a message used to replace DS_LAST_OP_INFO directives
     *   "attrname" is the name of a DN-valued attribute
     *   "dndesc" is the destriptive name of the above DN-valued attribute
     *
     * If "&ADD" is present, we check to make sure the entry
     * does not exist, then we check that the parent entry exists, and then
     * we present an "add entry" form.
     *
     * Note: original form http://host/edit/dn[/...]?template[&...] is
     *       supported for keeping backward compatibility.
     *       But passing DN as PATH_INFO is NOT recommended.
     *       Since PATH_INFO is passed to CGI as is (non-escaped),
     *       the content has a risk to get broken especially when
     *       it contains 8-bit UTF-8 data.  (This is a known problem
     *       on localized Windows machines.)
     */

    options = DSGW_DISPLAY_OPT_EDITABLE;
    dn = NULL;
#ifndef  __LP64__
#ifdef HPUX
	/* call the static constructors in libnls */
	_main();
#endif
#endif

    if (( tmplname = getenv( "QUERY_STRING" )) != NULL && *tmplname != '\0' ) {
	tmplname = dsgw_ch_strdup( tmplname );
	while ( tmplname != NULL && ((( p = strrchr( tmplname, '&' )) != NULL ) || (p=tmplname) != NULL )) {
	    if (p == tmplname) {
		tmplname = NULL;
	    } else {
		*p++ = '\0'; 
	    }

	    if ( strcasecmp( p, "add" ) == 0 ) {
		options |= DSGW_DISPLAY_OPT_ADDING;
		if (( p = strrchr( tmplname, '&' )) != NULL ) {
		    *p++ = '\0';
		}
	    }

	    if ( p != NULL && strncasecmp( p, "info=", 5 ) == 0 ) {
		dsgw_last_op_info = dsgw_ch_strdup( p + 5 );
		dsgw_form_unescape( dsgw_last_op_info );
		continue;
	    } 
	    if ( p != NULL && strncasecmp( p, "dn=", 3 ) == 0 ) {
		dn = dsgw_ch_strdup( p + 3 );
		dsgw_form_unescape( dn );
		continue;
	    } 
	    if ( p != NULL && strncasecmp( p, "dnattr=", 7 ) == 0 ) {
		dsgw_dnattr = dsgw_ch_strdup( p + 7 );
		dsgw_form_unescape( dsgw_dnattr );
		continue;
	    } 
	    if ( p != NULL && strncasecmp( p, "dndesc=", 7 ) == 0 ) {
		dsgw_dndesc = dsgw_ch_strdup( p + 7 );
		dsgw_form_unescape( dsgw_dndesc );
		continue;
	    } 
	    if ( p != NULL && strncasecmp( p, "context=", 8 ) == 0) {
		context = dsgw_ch_strdup( p + 8 );
		dsgw_form_unescape( context );
		continue;
	    }
	    
	    /* 
	     * If none of the if-statements above matched,
	     * then it's the template name
	     */
	    tmplname = p;
	    break;
	}
	
    } else {
	tmplname = NULL;
    }

    (void)dsgw_init( argc, argv,  DSGW_METHOD_GET );
    dsgw_send_header();

#ifdef DSGW_DEBUG
   dsgw_logstringarray( "env", env ); 
#endif

    get_request(dn, tmplname, parent, options);

    exit( 0 );
}


static void
get_request(char *dn, char *tmplname, char *parent, unsigned long options)
{
    LDAP		*ld;

    if ( dn == NULL ) { /* not found in QUERY_STRING */
	dsgw_error( DSGW_ERR_MISSINGINPUT, NULL, DSGW_ERROPT_EXIT, 0, NULL );
    }

#ifdef DSGW_DEBUG
    dsgw_log( "get_request: dn: \"%s\", tmplname: \"%s\" "
	      "dnattr: \"%s\", dndesc: \"%s\"\n", dn,
	    ( tmplname == NULL ) ? "(null)" : tmplname, 
	    ( dsgw_dnattr == NULL ) ? "(null)" : dsgw_dnattr, 
	    ( dsgw_dndesc == NULL ) ? "(null)" : dsgw_dndesc );
#endif

    (void)dsgw_init_ldap( &ld, NULL, 0, 0);

    if (( options & DSGW_DISPLAY_OPT_ADDING ) == 0 ) {
	/*
	 * editing an existing entry -- if no DN is provided and we are running
	 * under the admin server, try to get DN from admin. server
	 */
	if ( *dn == '\0' ) {
	    (void)dsgw_get_adm_identity( ld, NULL, &dn, NULL,
		    DSGW_ERROPT_EXIT );
	}

	dsgw_read_entry( ld, dn, NULL, tmplname, NULL, options );

    } else {
	dsgwtmplinfo        *tip;
    	char		    *matched;

	/*
	 * new entry -- check to make sure it doesn't exist
	 */
	if ( dsgw_ldap_entry_exists( ld, dn, &matched, DSGW_ERROPT_EXIT )) {
	    char	**rdns;

	    dsgw_html_begin( XP_GetClientStr(DBT_entryAlreadyExists_), 1 );
	    dsgw_emits( XP_GetClientStr(DBT_anEntryNamed_) );
	    rdns = ldap_explode_dn( dn, 1 );
	    dsgw_html_href(
		    dsgw_build_urlprefix(),
		    dn, ( rdns == NULL || rdns[ 0 ] == NULL ) ? dn : rdns[ 0 ],
		    NULL, XP_GetClientStr(DBT_onmouseoverWindowStatusClickHere_) );
	    if ( rdns != NULL ) {
		ldap_value_free( rdns );
	    }
	    dsgw_emits( XP_GetClientStr(DBT_alreadyExistsPPleaseChooseAnothe_) );

	    dsgw_form_begin( NULL, NULL );
	    dsgw_emits( "\n<CENTER><TABLE border=2 width=\"100%\"><TR>\n" );
	    dsgw_emits( "<TD WIDTH=\"50%\" ALIGN=\"center\">\n" );
	    dsgw_emitf( "<INPUT TYPE=\"button\" VALUE=\"%s\" "
		"onClick=\"parent.close()\">", XP_GetClientStr(DBT_closeWindow_1) );
	    dsgw_emits( "<TD WIDTH=\"50%\" ALIGN=\"center\">\n" );
	    dsgw_emit_helpbutton( "ENTRYEXISTS" );
	    dsgw_emits( "\n</TABLE></CENTER></FORM>\n" );
	    dsgw_html_end();
	} else if ( !dsgw_is_dnparent( matched, dn ) &&
		!dsgw_dn_cmp( dn, gc->gc_ldapsearchbase )) {
	    /*
	     * The parent entry does not exist, and the dn being added is not
	     * the same as the suffix for which the gateway is configured.
	     */
	    dsgw_html_begin( XP_GetClientStr(DBT_parentEntryDoesNotExist_), 1 );
	    dsgw_emitf( XP_GetClientStr(DBT_youCannotAddAnEntryByTheNamePBSB_),
		    dn );
	    parent = dsgw_dn_parent( dn );
	    if ( parent == NULL || strlen( parent ) == 0 ) {
		dsgw_emits( XP_GetClientStr(DBT_itsParentN_) );
	    } else {
		dsgw_emitf( XP_GetClientStr(DBT_anEntryNamedPBSBN_), parent );
		free( parent );
	    }
	    dsgw_form_begin( NULL, NULL );
	    dsgw_emits( "\n<CENTER><TABLE border=2 width=\"100%\"><TR>\n" );
	    dsgw_emits( "<TD WIDTH=\"50%\" ALIGN=\"center\">\n" );
	    dsgw_emitf( "<INPUT TYPE=\"button\" VALUE=\"%s\" "
		"onClick=\"parent.close()\">", XP_GetClientStr(DBT_closeWindow_2) );
	    dsgw_emits( "<TD WIDTH=\"50%\" ALIGN=\"center\">\n" );
	    dsgw_emit_helpbutton( "ADD_NOPARENT" );
	    dsgw_emits( "\n</TABLE></CENTER></FORM>\n" );
	    dsgw_html_end();
	} else {
	    /*
	     * The parent exists, or the user is adding the entry whose DN
	     * is the same as the suffix for which the gateway is configured.
	     * Display the "add entry" form.
	     */

	    if ( tmplname == NULL ) {
#ifdef DSGW_DEBUG
                dsgw_log( "NULL tmplname\n" );
#endif
		dsgw_error( DSGW_ERR_MISSINGINPUT,
			XP_GetClientStr(DBT_missingTemplate_),
			DSGW_ERROPT_EXIT, 0, NULL );
	    }

	    tip = dsgw_display_init( DSGW_TMPLTYPE_DISPLAY, tmplname, options );
     
	    dsgw_display_entry( tip, ld, NULL, NULL, dn );
	    dsgw_display_done( tip );
	}
    }

    ldap_unbind( ld );
}