1 /* passwd-shell.c - passwd(5) shell-based backend for slapd(8) */
2 /* $OpenLDAP: pkg/ldap/servers/slapd/shell-backends/passwd-shell.c,v 1.14.2.4 2008/02/11 23:26:49 kurt Exp $ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2008 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17 * All rights reserved.
19 * Redistribution and use in source and binary forms are permitted
20 * provided that this notice is preserved and that due credit is given
21 * to the University of Michigan at Ann Arbor. The name of the University
22 * may not be used to endorse or promote products derived from this
23 * software without specific prior written permission. This software
24 * is provided ``as is'' without express or implied warranty.
27 * This work was originally developed by the University of Michigan
28 * (as part of U-MICH LDAP).
36 #include <ac/stdlib.h>
38 #include <ac/string.h>
39 #include <ac/unistd.h>
46 #include "shellutil.h"
48 static void pwdfile_search
LDAP_P(( struct ldop
*op
, FILE *ofp
));
49 static struct ldentry
*pw2entry
LDAP_P(( struct ldop
*op
, struct passwd
*pw
));
51 static char tmpbuf
[ MAXLINELEN
* 2 ];
55 main( int argc
, char **argv
)
60 if (( progname
= strrchr( argv
[ 0 ], '/' )) == NULL
) {
61 progname
= estrdup( argv
[ 0 ] );
63 progname
= estrdup( progname
+ 1 );
66 errflg
= debugflg
= 0;
68 while (( c
= getopt( argc
, argv
, "d" )) != EOF
) {
73 #else /* LDAP_DEBUG */
74 fprintf( stderr
, "%s: compile with -DLDAP_DEBUG for debugging\n",
76 #endif /* LDAP_DEBUG */
83 if ( errflg
|| optind
< argc
) {
84 fprintf( stderr
, "usage: %s [-d]\n", progname
);
88 debug_printf( "started\n" );
90 (void) memset( (char *)&op
, '\0', sizeof( op
));
92 if ( parse_input( stdin
, stdout
, &op
) < 0 ) {
96 if ( op
.ldop_op
!= LDOP_SEARCH
) {
97 write_result( stdout
, LDAP_UNWILLING_TO_PERFORM
, NULL
,
98 "Command Not Implemented" );
104 #endif /* LDAP_DEBUG */
106 pwdfile_search( &op
, stdout
);
108 exit( EXIT_SUCCESS
);
113 pwdfile_search( struct ldop
*op
, FILE *ofp
)
116 struct ldentry
*entry
;
119 oneentry
= ( strchr( op
->ldop_dn
, '@' ) != NULL
);
121 for ( pw
= getpwent(); pw
!= NULL
; pw
= getpwent()) {
122 if (( entry
= pw2entry( op
, pw
)) != NULL
) {
124 if ( strcasecmp( op
->ldop_dn
, entry
->lde_dn
) == 0 ) {
125 write_entry( op
, entry
, ofp
);
128 } else if ( test_filter( op
, entry
) == LDAP_COMPARE_TRUE
) {
129 write_entry( op
, entry
, ofp
);
136 write_result( ofp
, LDAP_SUCCESS
, NULL
, NULL
);
140 static struct ldentry
*
141 pw2entry( struct ldop
*op
, struct passwd
*pw
)
143 struct ldentry
*entry
;
148 * construct the DN from pw_name
150 if ( strchr( op
->ldop_suffixes
[ 0 ], '=' ) != NULL
) {
154 i
= snprintf( tmpbuf
, sizeof( tmpbuf
), "cn=%s, %s", pw
->pw_name
, op
->ldop_suffixes
[ 0 ] );
159 i
= snprintf( tmpbuf
, sizeof( tmpbuf
), "%s@%s", pw
->pw_name
, op
->ldop_suffixes
[ 0 ] );
162 if ( i
< 0 || i
>= sizeof( tmpbuf
) ) {
166 entry
= (struct ldentry
*) ecalloc( 1, sizeof( struct ldentry
));
167 entry
->lde_dn
= estrdup( tmpbuf
);
170 * for now, we simply derive the LDAP attribute values as follows:
171 * objectClass = person
175 * cn = pw_gecos (second common name)
177 entry
->lde_attrs
= (struct ldattr
**)ecalloc( 5, sizeof( struct ldattr
* ));
179 attr
= (struct ldattr
*)ecalloc( 1, sizeof( struct ldattr
));
180 attr
->lda_name
= estrdup( "objectClass" );
181 attr
->lda_values
= (char **)ecalloc( 2, sizeof( char * ));
182 attr
->lda_values
[ 0 ] = estrdup( "person" );
183 entry
->lde_attrs
[ i
++ ] = attr
;
185 attr
= (struct ldattr
*)ecalloc( 1, sizeof( struct ldattr
));
186 attr
->lda_name
= estrdup( "uid" );
187 attr
->lda_values
= (char **)ecalloc( 2, sizeof( char * ));
188 attr
->lda_values
[ 0 ] = estrdup( pw
->pw_name
);
189 entry
->lde_attrs
[ i
++ ] = attr
;
191 attr
= (struct ldattr
*)ecalloc( 1, sizeof( struct ldattr
));
192 attr
->lda_name
= estrdup( "sn" );
193 attr
->lda_values
= (char **)ecalloc( 2, sizeof( char * ));
194 attr
->lda_values
[ 0 ] = estrdup( pw
->pw_name
);
195 entry
->lde_attrs
[ i
++ ] = attr
;
197 attr
= (struct ldattr
*)ecalloc( 1, sizeof( struct ldattr
));
198 attr
->lda_name
= estrdup( "cn" );
199 attr
->lda_values
= (char **)ecalloc( 3, sizeof( char * ));
200 attr
->lda_values
[ 0 ] = estrdup( pw
->pw_name
);
201 if ( pw
->pw_gecos
!= NULL
&& *pw
->pw_gecos
!= '\0' ) {
202 attr
->lda_values
[ 1 ] = estrdup( pw
->pw_gecos
);
204 entry
->lde_attrs
[ i
++ ] = attr
;