]> git.sur5r.net Git - openldap/blob - servers/slapd/shell-backends/passwd-shell.c
merged with autoconf branch
[openldap] / servers / slapd / shell-backends / passwd-shell.c
1 /*
2  passwd-shell.c - /etc/passwd shell-based backend for standalone ldap server
3
4  Copyright (c) 1995 Regents of the University of Michigan.
5  All rights reserved.
6
7  Redistribution and use in source and binary forms are permitted
8  provided that this notice is preserved and that due credit is given
9  to the University of Michigan at Ann Arbor. The name of the University
10  may not be used to endorse or promote products derived from this
11  software without specific prior written permission. This software
12  is provided ``as is'' without express or implied warranty.
13 */
14
15
16 #include "portable.h"
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <pwd.h>
21
22 #include <ac/string.h>
23
24 #include <lber.h>
25 #include <ldap.h>
26
27 #include "shellutil.h"
28 #include "passwd-shell.h"
29
30
31 static void pwdfile_search LDAP_P(( struct ldop *op, FILE *ofp ));
32 static struct ldentry *pw2entry LDAP_P(( struct ldop *op, struct passwd *pw ));
33
34 static char     tmpbuf[ MAXLINELEN * 2 ];
35
36
37 main( int argc, char **argv )
38 {
39     int                 c, errflg;
40     struct ldop         op;
41     extern int          optind;
42     extern char         *optarg;
43
44     if (( progname = strrchr( argv[ 0 ], '/' )) == NULL ) {
45         progname = estrdup( argv[ 0 ] );
46     } else {
47         progname = estrdup( progname + 1 );
48     }
49
50     errflg = debugflg = 0;
51
52     while (( c = getopt( argc, argv, "d" )) != EOF ) {
53         switch( c ) {
54         case 'd':
55 #ifdef LDAP_DEBUG
56             ++debugflg;
57 #else /* LDAP_DEBUG */
58             fprintf( stderr, "%s: compile with -DLDAP_DEBUG for debugging\n",
59                     progname );
60 #endif /* LDAP_DEBUG */
61             break;
62         default:
63             ++errflg;
64         }
65     }
66
67     if ( errflg || optind < argc ) {
68         fprintf( stderr, "usage: %s [-d]\n", progname );
69         exit( 1 );
70     }
71
72     debug_printf( "started\n" );
73
74     (void) memset( (char *)&op, '\0', sizeof( op ));
75
76     if ( parse_input( stdin, stdout, &op ) < 0 ) {
77         exit( 0 );
78     }
79
80     if ( op.ldop_op != LDOP_SEARCH ) {
81         write_result( stdout, LDAP_UNWILLING_TO_PERFORM, NULL,
82                 "Command Not Implemented" );
83         exit( 0 );
84     }
85
86 #ifdef LDAP_DEBUG
87     dump_ldop( &op );
88 #endif /* LDAP_DEBUG */
89
90     pwdfile_search( &op, stdout );
91
92     exit( 0 );
93 }
94
95
96 static void
97 pwdfile_search( struct ldop *op, FILE *ofp )
98 {
99     struct passwd       *pw;
100     struct ldentry      *entry;
101     int                 oneentry;
102
103     oneentry = ( strchr( op->ldop_dn, '@' ) != NULL );
104
105     for ( pw = getpwent(); pw != NULL; pw = getpwent()) {
106         if (( entry = pw2entry( op, pw )) != NULL ) {
107             if ( oneentry ) {
108                 if ( strcasecmp( op->ldop_dn, entry->lde_dn ) == 0 ) {
109                     write_entry( op, entry, ofp );
110                     break;
111                 }
112             } else if ( test_filter( op, entry )) {
113                 write_entry( op, entry, ofp );
114             }
115             free_entry( entry );
116         }
117     }
118     endpwent();
119
120     write_result( ofp, LDAP_SUCCESS, NULL, NULL );
121 }
122
123
124 static struct ldentry *
125 pw2entry( struct ldop *op, struct passwd *pw )
126 {
127     struct ldentry      *entry;
128     struct ldattr       *attr;
129     int                 i;
130
131     entry = (struct ldentry *) ecalloc( 1, sizeof( struct ldentry ));
132
133     /* 
134      * construct the DN from pw_name
135      */
136     if ( strchr( op->ldop_suffixes[ 0 ], '=' ) != NULL ) {
137         /*
138          * X.500 style DN
139          */
140         sprintf( tmpbuf, "cn=%s, %s", pw->pw_name, op->ldop_suffixes[ 0 ] );
141     } else {
142         /*
143          * RFC-822 style DN
144          */
145         sprintf( tmpbuf, "%s@%s", pw->pw_name, op->ldop_suffixes[ 0 ] );
146     }
147     entry->lde_dn = estrdup( tmpbuf );
148
149     /*
150      * for now, we simply derive the LDAP attribute values as follows:
151      *  objectClass = person
152      *  uid = pw_name
153      *  sn = pw_name
154      *  cn = pw_name
155      *  cn = pw_gecos   (second common name)
156      */
157     entry->lde_attrs = (struct ldattr **)ecalloc( 5, sizeof( struct ldattr * ));
158     i = 0;
159     attr = (struct ldattr *)ecalloc( 1, sizeof( struct ldattr ));
160     attr->lda_name = estrdup( "objectClass" );
161     attr->lda_values = (char **)ecalloc( 2, sizeof( char * ));
162     attr->lda_values[ 0 ] = estrdup( "person" );
163     entry->lde_attrs[ i++ ] = attr;
164
165     attr = (struct ldattr *)ecalloc( 1, sizeof( struct ldattr ));
166     attr->lda_name = estrdup( "uid" );
167     attr->lda_values = (char **)ecalloc( 2, sizeof( char * ));
168     attr->lda_values[ 0 ] = estrdup( pw->pw_name );
169     entry->lde_attrs[ i++ ] = attr;
170
171     attr = (struct ldattr *)ecalloc( 1, sizeof( struct ldattr ));
172     attr->lda_name = estrdup( "sn" );
173     attr->lda_values = (char **)ecalloc( 2, sizeof( char * ));
174     attr->lda_values[ 0 ] = estrdup( pw->pw_name );
175     entry->lde_attrs[ i++ ] = attr;
176
177     attr = (struct ldattr *)ecalloc( 1, sizeof( struct ldattr ));
178     attr->lda_name = estrdup( "cn" );
179     attr->lda_values = (char **)ecalloc( 3, sizeof( char * ));
180     attr->lda_values[ 0 ] = estrdup( pw->pw_name );
181     if ( pw->pw_gecos != NULL && *pw->pw_gecos != '\0' ) {
182         attr->lda_values[ 1 ] = estrdup( pw->pw_gecos );
183     }
184     entry->lde_attrs[ i++ ] = attr;
185
186     return( entry );
187 }