Back to index

openldap  2.4.31
passwd-shell.c
Go to the documentation of this file.
00001 /* passwd-shell.c - passwd(5) shell-based backend for slapd(8) */
00002 /* $OpenLDAP$ */
00003 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00004  *
00005  * Copyright 1998-2012 The OpenLDAP Foundation.
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted only as authorized by the OpenLDAP
00010  * Public License.
00011  *
00012  * A copy of this license is available in the file LICENSE in the
00013  * top-level directory of the distribution or, alternatively, at
00014  * <http://www.OpenLDAP.org/license.html>.
00015  */
00016 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
00017  * All rights reserved.
00018  *
00019  * Redistribution and use in source and binary forms are permitted
00020  * provided that this notice is preserved and that due credit is given
00021  * to the University of Michigan at Ann Arbor. The name of the University
00022  * may not be used to endorse or promote products derived from this
00023  * software without specific prior written permission. This software
00024  * is provided ``as is'' without express or implied warranty.
00025  */
00026 /* ACKNOWLEDGEMENTS:
00027  * This work was originally developed by the University of Michigan
00028  * (as part of U-MICH LDAP).
00029  */
00030 
00031 
00032 #include "portable.h"
00033 
00034 #include <stdio.h>
00035 
00036 #include <ac/stdlib.h>
00037 
00038 #include <ac/string.h>
00039 #include <ac/unistd.h>
00040 
00041 #include <pwd.h>
00042 
00043 #include <lber.h>
00044 #include <ldap.h>
00045 
00046 #include "shellutil.h"
00047 
00048 static void pwdfile_search LDAP_P(( struct ldop *op, FILE *ofp ));
00049 static struct ldentry *pw2entry LDAP_P(( struct ldop *op, struct passwd *pw ));
00050 
00051 static char   tmpbuf[ MAXLINELEN * 2 ];
00052 
00053 
00054 int
00055 main( int argc, char **argv )
00056 {
00057     int                     c, errflg;
00058     struct ldop             op;
00059 
00060     if (( progname = strrchr( argv[ 0 ], '/' )) == NULL ) {
00061        progname = estrdup( argv[ 0 ] );
00062     } else {
00063        progname = estrdup( progname + 1 );
00064     }
00065 
00066     errflg = debugflg = 0;
00067 
00068     while (( c = getopt( argc, argv, "d" )) != EOF ) {
00069        switch( c ) {
00070        case 'd':
00071 #ifdef LDAP_DEBUG
00072            ++debugflg;
00073 #else /* LDAP_DEBUG */
00074            fprintf( stderr, "%s: compile with -DLDAP_DEBUG for debugging\n",
00075                   progname );
00076 #endif /* LDAP_DEBUG */
00077            break;
00078        default:
00079            ++errflg;
00080        }
00081     }
00082 
00083     if ( errflg || optind < argc ) {
00084        fprintf( stderr, "usage: %s [-d]\n", progname );
00085        exit( EXIT_FAILURE );
00086     }
00087 
00088     debug_printf( "started\n" );
00089 
00090     (void) memset( (char *)&op, '\0', sizeof( op ));
00091 
00092     if ( parse_input( stdin, stdout, &op ) < 0 ) {
00093        exit( EXIT_SUCCESS );
00094     }
00095 
00096     if ( op.ldop_op != LDOP_SEARCH ) {
00097        write_result( stdout, LDAP_UNWILLING_TO_PERFORM, NULL,
00098               "Command Not Implemented" );
00099        exit( EXIT_SUCCESS );
00100     }
00101 
00102 #ifdef LDAP_DEBUG
00103     dump_ldop( &op );
00104 #endif /* LDAP_DEBUG */
00105 
00106     pwdfile_search( &op, stdout );
00107 
00108     exit( EXIT_SUCCESS );
00109 }
00110 
00111 
00112 static void
00113 pwdfile_search( struct ldop *op, FILE *ofp )
00114 {
00115     struct passwd    *pw;
00116     struct ldentry   *entry;
00117     int                     oneentry;
00118 
00119     oneentry = ( strchr( op->ldop_dn, '@' ) != NULL );
00120 
00121     for ( pw = getpwent(); pw != NULL; pw = getpwent()) {
00122        if (( entry = pw2entry( op, pw )) != NULL ) {
00123            if ( oneentry ) {
00124               if ( strcasecmp( op->ldop_dn, entry->lde_dn ) == 0 ) {
00125                   write_entry( op, entry, ofp );
00126                   break;
00127               }
00128            } else if ( test_filter( op, entry ) == LDAP_COMPARE_TRUE ) {
00129                      write_entry( op, entry, ofp );
00130            }
00131            free_entry( entry );
00132        }
00133     }
00134     endpwent();
00135 
00136     write_result( ofp, LDAP_SUCCESS, NULL, NULL );
00137 }
00138 
00139 
00140 static struct ldentry *
00141 pw2entry( struct ldop *op, struct passwd *pw )
00142 {
00143     struct ldentry   *entry;
00144     struct ldattr    *attr;
00145     int                     i;
00146 
00147     /* 
00148      * construct the DN from pw_name
00149      */
00150     if ( strchr( op->ldop_suffixes[ 0 ], '=' ) != NULL ) {
00151        /*
00152         * X.500 style DN
00153         */
00154        i = snprintf( tmpbuf, sizeof( tmpbuf ), "cn=%s, %s", pw->pw_name, op->ldop_suffixes[ 0 ] );
00155     } else {
00156        /*
00157         * RFC-822 style DN
00158         */
00159        i = snprintf( tmpbuf, sizeof( tmpbuf ), "%s@%s", pw->pw_name, op->ldop_suffixes[ 0 ] );
00160     }
00161 
00162     if ( i < 0 || i >= sizeof( tmpbuf ) ) {
00163         return NULL;
00164     }
00165 
00166     entry = (struct ldentry *) ecalloc( 1, sizeof( struct ldentry ));
00167     entry->lde_dn = estrdup( tmpbuf );
00168 
00169     /*
00170      * for now, we simply derive the LDAP attribute values as follows:
00171      *  objectClass = person
00172      *  uid = pw_name
00173      *  sn = pw_name
00174      *  cn = pw_name
00175      *  cn = pw_gecos       (second common name)
00176      */
00177     entry->lde_attrs = (struct ldattr **)ecalloc( 5, sizeof( struct ldattr * ));
00178     i = 0;
00179     attr = (struct ldattr *)ecalloc( 1, sizeof( struct ldattr ));
00180     attr->lda_name = estrdup( "objectClass" );
00181     attr->lda_values = (char **)ecalloc( 2, sizeof( char * ));
00182     attr->lda_values[ 0 ] = estrdup( "person" );
00183     entry->lde_attrs[ i++ ] = attr;
00184 
00185     attr = (struct ldattr *)ecalloc( 1, sizeof( struct ldattr ));
00186     attr->lda_name = estrdup( "uid" );
00187     attr->lda_values = (char **)ecalloc( 2, sizeof( char * ));
00188     attr->lda_values[ 0 ] = estrdup( pw->pw_name );
00189     entry->lde_attrs[ i++ ] = attr;
00190 
00191     attr = (struct ldattr *)ecalloc( 1, sizeof( struct ldattr ));
00192     attr->lda_name = estrdup( "sn" );
00193     attr->lda_values = (char **)ecalloc( 2, sizeof( char * ));
00194     attr->lda_values[ 0 ] = estrdup( pw->pw_name );
00195     entry->lde_attrs[ i++ ] = attr;
00196 
00197     attr = (struct ldattr *)ecalloc( 1, sizeof( struct ldattr ));
00198     attr->lda_name = estrdup( "cn" );
00199     attr->lda_values = (char **)ecalloc( 3, sizeof( char * ));
00200     attr->lda_values[ 0 ] = estrdup( pw->pw_name );
00201     if ( pw->pw_gecos != NULL && *pw->pw_gecos != '\0' ) {
00202        attr->lda_values[ 1 ] = estrdup( pw->pw_gecos );
00203     }
00204     entry->lde_attrs[ i++ ] = attr;
00205 
00206     return( entry );
00207 }