1 1.1 christos /* $NetBSD: homedir.c,v 1.3 2025/09/05 21:16:32 christos Exp $ */ 2 1.1 christos 3 1.1 christos /* homedir.c - create/remove user home directories */ 4 1.1 christos /* $OpenLDAP$ */ 5 1.1 christos /* This work is part of OpenLDAP Software <http://www.openldap.org/>. 6 1.1 christos * 7 1.3 christos * Copyright 2009-2024 The OpenLDAP Foundation. 8 1.1 christos * Portions copyright 2009-2010 Symas Corporation. 9 1.1 christos * All rights reserved. 10 1.1 christos * 11 1.1 christos * Redistribution and use in source and binary forms, with or without 12 1.1 christos * modification, are permitted only as authorized by the OpenLDAP 13 1.1 christos * Public License. 14 1.1 christos * 15 1.1 christos * A copy of this license is available in the file LICENSE in the 16 1.1 christos * top-level directory of the distribution or, alternatively, at 17 1.1 christos * <http://www.OpenLDAP.org/license.html>. 18 1.1 christos */ 19 1.1 christos /* ACKNOWLEDGEMENTS: 20 1.1 christos * This work was initially developed by Emily Backes at Symas 21 1.1 christos * Corp. for inclusion in OpenLDAP Software. 22 1.1 christos */ 23 1.1 christos 24 1.1 christos #include <sys/cdefs.h> 25 1.1 christos __RCSID("$NetBSD: homedir.c,v 1.3 2025/09/05 21:16:32 christos Exp $"); 26 1.1 christos 27 1.1 christos #include "portable.h" 28 1.1 christos 29 1.1 christos #ifdef SLAPD_OVER_HOMEDIR 30 1.1 christos 31 1.1 christos #define _FILE_OFFSET_BITS 64 32 1.1 christos 33 1.1 christos #include <stdio.h> 34 1.1 christos #include <fcntl.h> 35 1.1 christos 36 1.1 christos #include <ac/string.h> 37 1.1 christos #include <ac/ctype.h> 38 1.1 christos #include <ac/errno.h> 39 1.1 christos #include <sys/stat.h> 40 1.1 christos #include <ac/unistd.h> 41 1.1 christos #include <ac/dirent.h> 42 1.1 christos #include <ac/time.h> 43 1.1 christos 44 1.1 christos #include "slap.h" 45 1.1 christos #include "slap-config.h" 46 1.1 christos 47 1.1 christos #define DEFAULT_MIN_UID ( 100 ) 48 1.1 christos #define DEFAULT_SKEL ( LDAP_DIRSEP "etc" LDAP_DIRSEP "skel" ) 49 1.1 christos 50 1.1 christos typedef struct homedir_regexp { 51 1.1 christos char *match; 52 1.1 christos char *replace; 53 1.1 christos regex_t compiled; 54 1.1 christos struct homedir_regexp *next; 55 1.1 christos } homedir_regexp; 56 1.1 christos 57 1.1 christos typedef enum { 58 1.1 christos DEL_IGNORE, 59 1.1 christos DEL_DELETE, 60 1.1 christos DEL_ARCHIVE 61 1.1 christos } delete_style; 62 1.1 christos 63 1.1 christos typedef struct homedir_data { 64 1.1 christos char *skeleton_path; 65 1.1 christos unsigned min_uid; 66 1.1 christos AttributeDescription *home_ad; 67 1.1 christos AttributeDescription *uidn_ad; 68 1.1 christos AttributeDescription *gidn_ad; 69 1.1 christos homedir_regexp *regexps; 70 1.1 christos delete_style style; 71 1.1 christos char *archive_path; 72 1.1 christos } homedir_data; 73 1.1 christos 74 1.1 christos typedef struct homedir_cb_data { 75 1.1 christos slap_overinst *on; 76 1.1 christos Entry *entry; 77 1.1 christos } homedir_cb_data; 78 1.1 christos 79 1.1 christos typedef struct name_list { 80 1.1 christos char *name; 81 1.1 christos struct stat st; 82 1.1 christos struct name_list *next; 83 1.1 christos } name_list; 84 1.1 christos 85 1.1 christos typedef struct name_list_list { 86 1.1 christos name_list *list; 87 1.1 christos struct name_list_list *next; 88 1.1 christos } name_list_list; 89 1.1 christos 90 1.1 christos typedef enum { 91 1.1 christos TRAVERSE_CB_CONTINUE, 92 1.1 christos TRAVERSE_CB_DONE, 93 1.1 christos TRAVERSE_CB_FAIL 94 1.1 christos } traverse_cb_ret; 95 1.1 christos 96 1.1 christos /* private, file info, context */ 97 1.1 christos typedef traverse_cb_ret (*traverse_cb_func)( 98 1.1 christos void *, 99 1.1 christos const char *, 100 1.1 christos const struct stat *, 101 1.1 christos void * ); 102 1.1 christos typedef struct traverse_cb { 103 1.1 christos traverse_cb_func pre_func; 104 1.1 christos traverse_cb_func post_func; 105 1.1 christos void *pre_private; 106 1.1 christos void *post_private; 107 1.1 christos } traverse_cb; 108 1.1 christos 109 1.1 christos typedef struct copy_private { 110 1.1 christos int source_prefix_len; 111 1.1 christos const char *dest_prefix; 112 1.1 christos int dest_prefix_len; 113 1.1 christos uid_t uidn; 114 1.1 christos gid_t gidn; 115 1.1 christos } copy_private; 116 1.1 christos 117 1.1 christos typedef struct chown_private { 118 1.1 christos uid_t old_uidn; 119 1.1 christos uid_t new_uidn; 120 1.1 christos gid_t old_gidn; 121 1.1 christos gid_t new_gidn; 122 1.1 christos } chown_private; 123 1.1 christos 124 1.1 christos typedef struct ustar_header { 125 1.1 christos char name[100]; 126 1.1 christos char mode[8]; 127 1.1 christos char uid[8]; 128 1.1 christos char gid[8]; 129 1.1 christos char size[12]; 130 1.1 christos char mtime[12]; 131 1.1 christos char checksum[8]; 132 1.1 christos char typeflag[1]; 133 1.1 christos char linkname[100]; 134 1.1 christos char magic[6]; 135 1.1 christos char version[2]; 136 1.1 christos char uname[32]; 137 1.1 christos char gname[32]; 138 1.1 christos char devmajor[8]; 139 1.1 christos char devminor[8]; 140 1.1 christos char prefix[155]; 141 1.1 christos char pad[12]; 142 1.1 christos } ustar_header; 143 1.1 christos 144 1.1 christos typedef struct tar_private { 145 1.1 christos FILE *file; 146 1.1 christos const char *name; 147 1.1 christos } tar_private; 148 1.1 christos 149 1.1 christos /* FIXME: This mutex really needs to be executable-global, but this 150 1.1 christos * will have to do for now. 151 1.1 christos */ 152 1.1 christos static ldap_pvt_thread_mutex_t readdir_mutex; 153 1.1 christos static ConfigDriver homedir_regexp_cfg; 154 1.1 christos static ConfigDriver homedir_style_cfg; 155 1.1 christos static slap_overinst homedir; 156 1.1 christos 157 1.1 christos static ConfigTable homedircfg[] = { 158 1.1 christos { "homedir-skeleton-path", "pathname", 2, 2, 0, 159 1.1 christos ARG_STRING|ARG_OFFSET, 160 1.1 christos (void *)offsetof(homedir_data, skeleton_path), 161 1.1 christos "( OLcfgCtAt:8.1 " 162 1.1 christos "NAME 'olcSkeletonPath' " 163 1.1 christos "DESC 'Pathname for home directory skeleton template' " 164 1.1 christos "SYNTAX OMsDirectoryString " 165 1.1 christos "SINGLE-VALUE )", 166 1.1 christos NULL, { .v_string = DEFAULT_SKEL } 167 1.1 christos }, 168 1.1 christos 169 1.1 christos { "homedir-min-uidnumber", "uid number", 2, 2, 0, 170 1.1 christos ARG_UINT|ARG_OFFSET, 171 1.1 christos (void *)offsetof(homedir_data, min_uid), 172 1.1 christos "( OLcfgCtAt:8.2 " 173 1.1 christos "NAME 'olcMinimumUidNumber' " 174 1.1 christos "DESC 'Minimum uidNumber attribute to consider' " 175 1.1 christos "SYNTAX OMsInteger " 176 1.1 christos "SINGLE-VALUE )", 177 1.1 christos NULL, { .v_uint = DEFAULT_MIN_UID } 178 1.1 christos }, 179 1.1 christos 180 1.1 christos { "homedir-regexp", "regexp> <path", 3, 3, 0, 181 1.1 christos ARG_MAGIC, 182 1.1 christos homedir_regexp_cfg, 183 1.1 christos "( OLcfgCtAt:8.3 " 184 1.1 christos "NAME 'olcHomedirRegexp' " 185 1.1 christos "DESC 'Regular expression for matching and transforming paths' " 186 1.1 christos "SYNTAX OMsDirectoryString " 187 1.1 christos "X-ORDERED 'VALUES' )", 188 1.1 christos NULL, NULL 189 1.1 christos }, 190 1.1 christos 191 1.1 christos { "homedir-delete-style", "style", 2, 2, 0, 192 1.1 christos ARG_MAGIC, 193 1.1 christos homedir_style_cfg, 194 1.1 christos "( OLcfgCtAt:8.4 " 195 1.1 christos "NAME 'olcHomedirDeleteStyle' " 196 1.1 christos "DESC 'Action to perform when removing a home directory' " 197 1.1 christos "SYNTAX OMsDirectoryString " 198 1.1 christos "SINGLE-VALUE )", 199 1.1 christos NULL, NULL 200 1.1 christos }, 201 1.1 christos 202 1.1 christos { "homedir-archive-path", "pathname", 2, 2, 0, 203 1.1 christos ARG_STRING|ARG_OFFSET, 204 1.1 christos (void *)offsetof(homedir_data, archive_path), 205 1.1 christos "( OLcfgCtAt:8.5 " 206 1.1 christos "NAME 'olcHomedirArchivePath' " 207 1.1 christos "DESC 'Pathname for home directory archival' " 208 1.1 christos "SYNTAX OMsDirectoryString " 209 1.1 christos "SINGLE-VALUE )", 210 1.1 christos NULL, NULL 211 1.1 christos }, 212 1.1 christos 213 1.1 christos { NULL, NULL, 0, 0, 0, ARG_IGNORED } 214 1.1 christos }; 215 1.1 christos 216 1.1 christos static ConfigOCs homedirocs[] = { 217 1.1 christos { "( OLcfgCtOc:8.1 " 218 1.1 christos "NAME 'olcHomedirConfig' " 219 1.1 christos "DESC 'Homedir configuration' " 220 1.1 christos "SUP olcOverlayConfig " 221 1.1 christos "MAY ( olcSkeletonPath $ olcMinimumUidNumber " 222 1.1 christos "$ olcHomedirRegexp $ olcHomedirDeleteStyle " 223 1.1 christos "$ olcHomedirArchivePath ) )", 224 1.1 christos Cft_Overlay, homedircfg }, 225 1.1 christos 226 1.1 christos { NULL, 0, NULL } 227 1.1 christos }; 228 1.1 christos 229 1.1 christos static int 230 1.1 christos homedir_regexp_cfg( ConfigArgs *c ) 231 1.1 christos { 232 1.1 christos slap_overinst *on = (slap_overinst *)c->bi; 233 1.1 christos homedir_data *data = (homedir_data *)on->on_bi.bi_private; 234 1.1 christos int rc = ARG_BAD_CONF; 235 1.1 christos 236 1.1 christos assert( data != NULL ); 237 1.1 christos 238 1.1 christos switch ( c->op ) { 239 1.1 christos case SLAP_CONFIG_EMIT: { 240 1.1 christos int i; 241 1.1 christos homedir_regexp *r; 242 1.1 christos struct berval bv; 243 1.1 christos char buf[4096]; 244 1.1 christos 245 1.1 christos bv.bv_val = buf; 246 1.1 christos for ( i = 0, r = data->regexps; r != NULL; ++i, r = r->next ) { 247 1.1 christos bv.bv_len = snprintf( buf, sizeof(buf), "{%d}%s %s", i, 248 1.1 christos r->match, r->replace ); 249 1.1 christos if ( bv.bv_len >= sizeof(buf) ) { 250 1.1 christos Debug( LDAP_DEBUG_ANY, "homedir_regexp_cfg: " 251 1.1 christos "emit serialization failed: size %lu\n", 252 1.1 christos (unsigned long)bv.bv_len ); 253 1.1 christos return ARG_BAD_CONF; 254 1.1 christos } 255 1.1 christos value_add_one( &c->rvalue_vals, &bv ); 256 1.1 christos } 257 1.1 christos rc = 0; 258 1.1 christos } break; 259 1.1 christos 260 1.1 christos case LDAP_MOD_DELETE: 261 1.1 christos if ( c->valx < 0 ) { /* delete all values */ 262 1.1 christos homedir_regexp *r, *rnext; 263 1.1 christos 264 1.1 christos for ( r = data->regexps; r != NULL; r = rnext ) { 265 1.1 christos rnext = r->next; 266 1.1 christos ch_free( r->match ); 267 1.1 christos ch_free( r->replace ); 268 1.1 christos regfree( &r->compiled ); 269 1.1 christos ch_free( r ); 270 1.1 christos } 271 1.1 christos data->regexps = NULL; 272 1.1 christos rc = 0; 273 1.1 christos 274 1.1 christos } else { /* delete value by index*/ 275 1.1 christos homedir_regexp **rp, *r; 276 1.1 christos int i; 277 1.1 christos 278 1.1 christos for ( i = 0, rp = &data->regexps; i < c->valx; 279 1.1 christos ++i, rp = &(*rp)->next ) 280 1.1 christos ; 281 1.1 christos 282 1.1 christos r = *rp; 283 1.1 christos *rp = r->next; 284 1.1 christos ch_free( r->match ); 285 1.1 christos ch_free( r->replace ); 286 1.1 christos regfree( &r->compiled ); 287 1.1 christos ch_free( r ); 288 1.1 christos 289 1.1 christos rc = 0; 290 1.1 christos } 291 1.1 christos break; 292 1.1 christos 293 1.1 christos case LDAP_MOD_ADD: /* fallthrough */ 294 1.1 christos case SLAP_CONFIG_ADD: { /* add values */ 295 1.1 christos char *match = c->argv[1]; 296 1.1 christos char *replace = c->argv[2]; 297 1.1 christos regex_t compiled; 298 1.1 christos homedir_regexp **rp, *r; 299 1.1 christos 300 1.1 christos memset( &compiled, 0, sizeof(compiled) ); 301 1.1 christos rc = regcomp( &compiled, match, REG_EXTENDED ); 302 1.1 christos if ( rc ) { 303 1.1 christos regerror( rc, &compiled, c->cr_msg, sizeof(c->cr_msg) ); 304 1.1 christos regfree( &compiled ); 305 1.1 christos return ARG_BAD_CONF; 306 1.1 christos } 307 1.1 christos 308 1.1 christos r = ch_calloc( 1, sizeof(homedir_regexp) ); 309 1.1 christos r->match = strdup( match ); 310 1.1 christos r->replace = strdup( replace ); 311 1.1 christos r->compiled = compiled; 312 1.1 christos 313 1.1 christos if ( c->valx == -1 ) { /* append */ 314 1.1 christos for ( rp = &data->regexps; ( *rp ) != NULL; 315 1.1 christos rp = &(*rp)->next ) 316 1.1 christos ; 317 1.1 christos *rp = r; 318 1.1 christos 319 1.1 christos } else { /* insert at valx */ 320 1.1 christos int i; 321 1.1 christos for ( i = 0, rp = &data->regexps; i < c->valx; 322 1.1 christos rp = &(*rp)->next, ++i ) 323 1.1 christos ; 324 1.1 christos r->next = *rp; 325 1.1 christos *rp = r; 326 1.1 christos } 327 1.1 christos rc = 0; 328 1.1 christos break; 329 1.1 christos } 330 1.1 christos default: 331 1.1 christos abort(); 332 1.1 christos } 333 1.1 christos 334 1.1 christos return rc; 335 1.1 christos } 336 1.1 christos 337 1.1 christos static int 338 1.1 christos homedir_style_cfg( ConfigArgs *c ) 339 1.1 christos { 340 1.1 christos slap_overinst *on = (slap_overinst *)c->bi; 341 1.1 christos homedir_data *data = (homedir_data *)on->on_bi.bi_private; 342 1.1 christos int rc = ARG_BAD_CONF; 343 1.1 christos struct berval bv; 344 1.1 christos 345 1.1 christos assert( data != NULL ); 346 1.1 christos 347 1.1 christos switch ( c->op ) { 348 1.1 christos case SLAP_CONFIG_EMIT: 349 1.1 christos bv.bv_val = data->style == DEL_IGNORE ? "IGNORE" : 350 1.1 christos data->style == DEL_DELETE ? "DELETE" : 351 1.1 christos "ARCHIVE"; 352 1.1 christos bv.bv_len = strlen( bv.bv_val ); 353 1.1 christos rc = value_add_one( &c->rvalue_vals, &bv ); 354 1.1 christos if ( rc != 0 ) return ARG_BAD_CONF; 355 1.1 christos break; 356 1.1 christos 357 1.1 christos case LDAP_MOD_DELETE: 358 1.1 christos data->style = DEL_IGNORE; 359 1.1 christos rc = 0; 360 1.1 christos break; 361 1.1 christos 362 1.1 christos case LDAP_MOD_ADD: /* fallthrough */ 363 1.1 christos case SLAP_CONFIG_ADD: /* add values */ 364 1.1 christos if ( strcasecmp( c->argv[1], "IGNORE" ) == 0 ) 365 1.1 christos data->style = DEL_IGNORE; 366 1.1 christos else if ( strcasecmp( c->argv[1], "DELETE" ) == 0 ) 367 1.1 christos data->style = DEL_DELETE; 368 1.1 christos else if ( strcasecmp( c->argv[1], "ARCHIVE" ) == 0 ) 369 1.1 christos data->style = DEL_ARCHIVE; 370 1.1 christos else { 371 1.1 christos Debug( LDAP_DEBUG_ANY, "homedir_style_cfg: " 372 1.1 christos "unrecognized style keyword\n" ); 373 1.1 christos return ARG_BAD_CONF; 374 1.1 christos } 375 1.1 christos rc = 0; 376 1.1 christos break; 377 1.1 christos 378 1.1 christos default: 379 1.1 christos abort(); 380 1.1 christos } 381 1.1 christos 382 1.1 christos return rc; 383 1.1 christos } 384 1.1 christos 385 1.1 christos #define HOMEDIR_NULLWRAP(x) ( ( x ) == NULL ? "unknown" : (x) ) 386 1.1 christos static void 387 1.1 christos report_errno( const char *parent_func, const char *func, const char *filename ) 388 1.1 christos { 389 1.1 christos int save_errno = errno; 390 1.1 christos char ebuf[1024]; 391 1.1 christos 392 1.1 christos Debug( LDAP_DEBUG_ANY, "homedir: " 393 1.1 christos "%s: %s: \"%s\": %d (%s)\n", 394 1.1 christos HOMEDIR_NULLWRAP(parent_func), HOMEDIR_NULLWRAP(func), 395 1.1 christos HOMEDIR_NULLWRAP(filename), save_errno, 396 1.1 christos AC_STRERROR_R( save_errno, ebuf, sizeof(ebuf) ) ); 397 1.1 christos } 398 1.1 christos 399 1.1 christos static int 400 1.1 christos copy_link( 401 1.1 christos const char *dest_file, 402 1.1 christos const char *source_file, 403 1.1 christos const struct stat *st, 404 1.1 christos uid_t uidn, 405 1.1 christos gid_t gidn, 406 1.1 christos void *ctx ) 407 1.1 christos { 408 1.1 christos char *buf = NULL; 409 1.1 christos int rc; 410 1.1 christos 411 1.1 christos assert( dest_file != NULL ); 412 1.1 christos assert( source_file != NULL ); 413 1.1 christos assert( st != NULL ); 414 1.1 christos assert( (st->st_mode & S_IFMT) == S_IFLNK ); 415 1.1 christos 416 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 417 1.1 christos "copy_link: %s to %s\n", 418 1.1 christos source_file, dest_file ); 419 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 420 1.1 christos "copy_link: %s uid %ld gid %ld\n", 421 1.1 christos dest_file, (long)uidn, (long)gidn ); 422 1.1 christos 423 1.1 christos /* calloc +1 for terminator */ 424 1.1 christos buf = ber_memcalloc_x( 1, st->st_size + 1, ctx ); 425 1.1 christos if ( buf == NULL ) { 426 1.1 christos Debug( LDAP_DEBUG_ANY, "homedir: " 427 1.1 christos "copy_link: alloc failed\n" ); 428 1.1 christos return 1; 429 1.1 christos } 430 1.1 christos rc = readlink( source_file, buf, st->st_size ); 431 1.1 christos if ( rc == -1 ) { 432 1.1 christos report_errno( "copy_link", "readlink", source_file ); 433 1.1 christos goto fail; 434 1.1 christos } 435 1.1 christos rc = symlink( buf, dest_file ); 436 1.1 christos if ( rc ) { 437 1.1 christos report_errno( "copy_link", "symlink", dest_file ); 438 1.1 christos goto fail; 439 1.1 christos } 440 1.1 christos rc = lchown( dest_file, uidn, gidn ); 441 1.1 christos if ( rc ) { 442 1.1 christos report_errno( "copy_link", "lchown", dest_file ); 443 1.1 christos goto fail; 444 1.1 christos } 445 1.1 christos goto out; 446 1.1 christos 447 1.1 christos fail: 448 1.1 christos rc = 1; 449 1.1 christos 450 1.1 christos out: 451 1.1 christos if ( buf != NULL ) ber_memfree_x( buf, ctx ); 452 1.1 christos return rc; 453 1.1 christos } 454 1.1 christos 455 1.1 christos static int 456 1.1 christos copy_blocks( 457 1.1 christos FILE *source, 458 1.1 christos FILE *dest, 459 1.1 christos const char *source_file, 460 1.1 christos const char *dest_file ) 461 1.1 christos { 462 1.1 christos char buf[4096]; 463 1.1 christos size_t nread = 0; 464 1.1 christos int done = 0; 465 1.1 christos 466 1.1 christos while ( !done ) { 467 1.1 christos nread = fread( buf, 1, sizeof(buf), source ); 468 1.1 christos if ( nread == 0 ) { 469 1.1 christos if ( feof( source ) ) { 470 1.1 christos done = 1; 471 1.1 christos } else if ( ferror( source ) ) { 472 1.1 christos if ( source_file != NULL ) 473 1.1 christos Debug( LDAP_DEBUG_ANY, "homedir: " 474 1.1 christos "read error on %s\n", 475 1.1 christos source_file ); 476 1.1 christos goto fail; 477 1.1 christos } 478 1.1 christos } else { 479 1.1 christos size_t nwritten = 0; 480 1.1 christos nwritten = fwrite( buf, 1, nread, dest ); 481 1.1 christos if ( nwritten < nread ) { 482 1.1 christos if ( dest_file != NULL ) 483 1.1 christos Debug( LDAP_DEBUG_ANY, "homedir: " 484 1.1 christos "write error on %s\n", 485 1.1 christos dest_file ); 486 1.1 christos goto fail; 487 1.1 christos } 488 1.1 christos } 489 1.1 christos } 490 1.1 christos return 0; 491 1.1 christos fail: 492 1.1 christos return 1; 493 1.1 christos } 494 1.1 christos 495 1.1 christos static int 496 1.1 christos copy_file( 497 1.1 christos const char *dest_file, 498 1.1 christos const char *source_file, 499 1.1 christos uid_t uid, 500 1.1 christos gid_t gid, 501 1.1 christos int mode ) 502 1.1 christos { 503 1.1 christos FILE *source = NULL; 504 1.1 christos FILE *dest = NULL; 505 1.1 christos int rc; 506 1.1 christos 507 1.1 christos assert( dest_file != NULL ); 508 1.1 christos assert( source_file != NULL ); 509 1.1 christos 510 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 511 1.1 christos "copy_file: %s to %s mode 0%o\n", 512 1.1 christos source_file, dest_file, mode ); 513 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 514 1.1 christos "copy_file: %s uid %ld gid %ld\n", 515 1.1 christos dest_file, (long)uid, (long)gid ); 516 1.1 christos 517 1.1 christos source = fopen( source_file, "rb" ); 518 1.1 christos if ( source == NULL ) { 519 1.1 christos report_errno( "copy_file", "fopen", source_file ); 520 1.1 christos goto fail; 521 1.1 christos } 522 1.1 christos dest = fopen( dest_file, "wb" ); 523 1.1 christos if ( dest == NULL ) { 524 1.1 christos report_errno( "copy_file", "fopen", dest_file ); 525 1.1 christos goto fail; 526 1.1 christos } 527 1.1 christos 528 1.1 christos rc = copy_blocks( source, dest, source_file, dest_file ); 529 1.1 christos if ( rc != 0 ) goto fail; 530 1.1 christos 531 1.1 christos fclose( source ); 532 1.1 christos source = NULL; 533 1.1 christos rc = fclose( dest ); 534 1.1 christos dest = NULL; 535 1.1 christos if ( rc != 0 ) { 536 1.1 christos report_errno( "copy_file", "fclose", dest_file ); 537 1.1 christos goto fail; 538 1.1 christos } 539 1.1 christos 540 1.1 christos /* set owner/permission */ 541 1.1 christos rc = lchown( dest_file, uid, gid ); 542 1.1 christos if ( rc != 0 ) { 543 1.1 christos report_errno( "copy_file", "lchown", dest_file ); 544 1.1 christos goto fail; 545 1.1 christos } 546 1.1 christos rc = chmod( dest_file, mode ); 547 1.1 christos if ( rc != 0 ) { 548 1.1 christos report_errno( "copy_file", "chmod", dest_file ); 549 1.1 christos goto fail; 550 1.1 christos } 551 1.1 christos 552 1.1 christos rc = 0; 553 1.1 christos goto out; 554 1.1 christos fail: 555 1.1 christos rc = 1; 556 1.1 christos out: 557 1.1 christos if ( source != NULL ) fclose( source ); 558 1.1 christos if ( dest != NULL ) fclose( dest ); 559 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 560 1.1 christos "copy_file: %s to %s exit %d\n", 561 1.1 christos source_file, dest_file, rc ); 562 1.1 christos return rc; 563 1.1 christos } 564 1.1 christos 565 1.1 christos static void 566 1.1 christos free_name_list( name_list *names, void *ctx ) 567 1.1 christos { 568 1.1 christos name_list *next; 569 1.1 christos 570 1.1 christos while ( names != NULL ) { 571 1.1 christos next = names->next; 572 1.1 christos if ( names->name != NULL ) ber_memfree_x( names->name, ctx ); 573 1.1 christos ber_memfree_x( names, ctx ); 574 1.1 christos names = next; 575 1.1 christos } 576 1.1 christos } 577 1.1 christos 578 1.1 christos static int 579 1.1 christos grab_names( const char *dir_path, name_list **names, void *ctx ) 580 1.1 christos { 581 1.1 christos int locked = 0; 582 1.1 christos DIR *dir = NULL; 583 1.1 christos struct dirent *entry = NULL; 584 1.1 christos name_list **tail = NULL; 585 1.1 christos int dir_path_len = 0; 586 1.1 christos int rc = 0; 587 1.1 christos 588 1.1 christos assert( dir_path != NULL ); 589 1.1 christos assert( names != NULL ); 590 1.1 christos assert( *names == NULL ); 591 1.1 christos 592 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 593 1.1 christos "grab_names: %s\n", dir_path ); 594 1.1 christos 595 1.1 christos tail = names; 596 1.1 christos dir_path_len = strlen( dir_path ); 597 1.1 christos ldap_pvt_thread_mutex_lock( &readdir_mutex ); 598 1.1 christos locked = 1; 599 1.1 christos 600 1.1 christos dir = opendir( dir_path ); 601 1.1 christos if ( dir == NULL ) { 602 1.1 christos report_errno( "grab_names", "opendir", dir_path ); 603 1.1 christos goto fail; 604 1.1 christos } 605 1.1 christos 606 1.1 christos while ( ( entry = readdir( dir ) ) != NULL ) { 607 1.1 christos /* no d_namelen in ac/dirent.h */ 608 1.1 christos int d_namelen = strlen( entry->d_name ); 609 1.1 christos int full_len; 610 1.1 christos 611 1.1 christos /* Skip . and .. */ 612 1.1 christos if ( ( d_namelen == 1 && entry->d_name[0] == '.' ) || 613 1.1 christos ( d_namelen == 2 && entry->d_name[0] == '.' && 614 1.1 christos entry->d_name[1] == '.' ) ) { 615 1.1 christos continue; 616 1.1 christos } 617 1.1 christos 618 1.1 christos *tail = ber_memcalloc_x( 1, sizeof(**tail), ctx ); 619 1.1 christos if ( *tail == NULL ) { 620 1.1 christos Debug( LDAP_DEBUG_ANY, "homedir: " 621 1.1 christos "grab_names: list alloc failed\n" ); 622 1.1 christos goto fail; 623 1.1 christos } 624 1.1 christos (*tail)->next = NULL; 625 1.1 christos 626 1.1 christos /* +1 for dirsep, +1 for term */ 627 1.1 christos full_len = dir_path_len + 1 + d_namelen + 1; 628 1.1 christos (*tail)->name = ber_memalloc_x( full_len, ctx ); 629 1.1 christos if ( (*tail)->name == NULL ) { 630 1.1 christos Debug( LDAP_DEBUG_ANY, "homedir: " 631 1.1 christos "grab_names: name alloc failed\n" ); 632 1.1 christos goto fail; 633 1.1 christos } 634 1.1 christos snprintf( (*tail)->name, full_len, "%s" LDAP_DIRSEP "%s", 635 1.1 christos dir_path, entry->d_name ); 636 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 637 1.1 christos "grab_names: found \"%s\"\n", 638 1.1 christos (*tail)->name ); 639 1.1 christos 640 1.1 christos rc = lstat( (*tail)->name, &(*tail)->st ); 641 1.1 christos if ( rc ) { 642 1.1 christos report_errno( "grab_names", "lstat", (*tail)->name ); 643 1.1 christos goto fail; 644 1.1 christos } 645 1.1 christos 646 1.1 christos tail = &(*tail)->next; 647 1.1 christos } 648 1.1 christos closedir( dir ); 649 1.1 christos ldap_pvt_thread_mutex_unlock( &readdir_mutex ); 650 1.1 christos locked = 0; 651 1.1 christos 652 1.1 christos dir = NULL; 653 1.1 christos goto success; 654 1.1 christos 655 1.1 christos success: 656 1.1 christos rc = 0; 657 1.1 christos goto out; 658 1.1 christos fail: 659 1.1 christos rc = 1; 660 1.1 christos goto out; 661 1.1 christos out: 662 1.1 christos if ( dir != NULL ) closedir( dir ); 663 1.1 christos if ( locked ) ldap_pvt_thread_mutex_unlock( &readdir_mutex ); 664 1.1 christos if ( rc != 0 && *names != NULL ) { 665 1.1 christos free_name_list( *names, ctx ); 666 1.1 christos *names = NULL; 667 1.1 christos } 668 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 669 1.1 christos "grab_names: %s exit %d\n", 670 1.1 christos dir_path, rc ); 671 1.1 christos return rc; 672 1.1 christos } 673 1.1 christos 674 1.1 christos static int 675 1.1 christos traverse( const char *path, const traverse_cb *cb, void *ctx ) 676 1.1 christos { 677 1.1 christos name_list *next_name = NULL; 678 1.1 christos name_list_list *dir_stack = NULL; 679 1.1 christos name_list_list *next_dir; 680 1.1 christos int rc = 0; 681 1.1 christos 682 1.1 christos assert( path != NULL ); 683 1.1 christos assert( cb != NULL ); 684 1.1 christos assert( cb->pre_func || cb->post_func ); 685 1.1 christos 686 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 687 1.1 christos "traverse: %s\n", path ); 688 1.1 christos 689 1.1 christos dir_stack = ber_memcalloc_x( 1, sizeof(*dir_stack), ctx ); 690 1.1 christos if ( dir_stack == NULL ) goto alloc_fail; 691 1.1 christos dir_stack->next = NULL; 692 1.1 christos dir_stack->list = ber_memcalloc_x( 1, sizeof(name_list), ctx ); 693 1.1 christos if ( dir_stack->list == NULL ) goto alloc_fail; 694 1.1 christos rc = lstat( path, &dir_stack->list->st ); 695 1.1 christos if ( rc != 0 ) { 696 1.1 christos report_errno( "traverse", "lstat", path ); 697 1.1 christos goto fail; 698 1.1 christos } 699 1.1 christos dir_stack->list->next = NULL; 700 1.1 christos dir_stack->list->name = ber_strdup_x( path, ctx ); 701 1.1 christos if ( dir_stack->list->name == NULL ) goto alloc_fail; 702 1.1 christos 703 1.1 christos while ( dir_stack != NULL ) { 704 1.1 christos while ( dir_stack->list != NULL ) { 705 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 706 1.1 christos "traverse: top of loop with \"%s\"\n", 707 1.1 christos dir_stack->list->name ); 708 1.1 christos 709 1.1 christos if ( cb->pre_func != NULL ) { 710 1.1 christos traverse_cb_ret cb_rc; 711 1.1 christos cb_rc = cb->pre_func( cb->pre_private, dir_stack->list->name, 712 1.1 christos &dir_stack->list->st, ctx ); 713 1.1 christos 714 1.1 christos if ( cb_rc == TRAVERSE_CB_DONE ) goto cb_done; 715 1.1 christos if ( cb_rc == TRAVERSE_CB_FAIL ) goto cb_fail; 716 1.1 christos } 717 1.1 christos if ( (dir_stack->list->st.st_mode & S_IFMT) == S_IFDIR ) { 718 1.1 christos /* push dir onto stack */ 719 1.1 christos next_dir = dir_stack; 720 1.1 christos dir_stack = ber_memalloc_x( sizeof(*dir_stack), ctx ); 721 1.1 christos if ( dir_stack == NULL ) { 722 1.1 christos dir_stack = next_dir; 723 1.1 christos goto alloc_fail; 724 1.1 christos } 725 1.1 christos dir_stack->list = NULL; 726 1.1 christos dir_stack->next = next_dir; 727 1.1 christos rc = grab_names( 728 1.1 christos dir_stack->next->list->name, &dir_stack->list, ctx ); 729 1.1 christos if ( rc != 0 ) { 730 1.1 christos Debug( LDAP_DEBUG_ANY, "homedir: " 731 1.1 christos "traverse: grab_names %s failed\n", 732 1.1 christos dir_stack->next->list->name ); 733 1.1 christos goto fail; 734 1.1 christos } 735 1.1 christos } else { 736 1.1 christos /* just a file */ 737 1.1 christos if ( cb->post_func != NULL ) { 738 1.1 christos traverse_cb_ret cb_rc; 739 1.1 christos cb_rc = cb->post_func( cb->post_private, 740 1.1 christos dir_stack->list->name, &dir_stack->list->st, ctx ); 741 1.1 christos 742 1.1 christos if ( cb_rc == TRAVERSE_CB_DONE ) goto cb_done; 743 1.1 christos if ( cb_rc == TRAVERSE_CB_FAIL ) goto cb_fail; 744 1.1 christos } 745 1.1 christos next_name = dir_stack->list->next; 746 1.1 christos ber_memfree_x( dir_stack->list->name, ctx ); 747 1.1 christos ber_memfree_x( dir_stack->list, ctx ); 748 1.1 christos dir_stack->list = next_name; 749 1.1 christos } 750 1.1 christos } 751 1.1 christos /* Time to pop a directory off the stack */ 752 1.1 christos next_dir = dir_stack->next; 753 1.1 christos ber_memfree_x( dir_stack, ctx ); 754 1.1 christos dir_stack = next_dir; 755 1.1 christos if ( dir_stack != NULL ) { 756 1.1 christos if ( cb->post_func != NULL ) { 757 1.1 christos traverse_cb_ret cb_rc; 758 1.1 christos cb_rc = cb->post_func( cb->post_private, dir_stack->list->name, 759 1.1 christos &dir_stack->list->st, ctx ); 760 1.1 christos 761 1.1 christos if ( cb_rc == TRAVERSE_CB_DONE ) goto cb_done; 762 1.1 christos if ( cb_rc == TRAVERSE_CB_FAIL ) goto cb_fail; 763 1.1 christos } 764 1.1 christos next_name = dir_stack->list->next; 765 1.1 christos ber_memfree_x( dir_stack->list->name, ctx ); 766 1.1 christos ber_memfree_x( dir_stack->list, ctx ); 767 1.1 christos dir_stack->list = next_name; 768 1.1 christos } 769 1.1 christos } 770 1.1 christos 771 1.1 christos goto success; 772 1.1 christos 773 1.1 christos cb_done: 774 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 775 1.1 christos "traverse: cb signaled completion\n" ); 776 1.1 christos success: 777 1.1 christos rc = 0; 778 1.1 christos goto out; 779 1.1 christos 780 1.1 christos cb_fail: 781 1.1 christos Debug( LDAP_DEBUG_ANY, "homedir: " 782 1.1 christos "traverse: cb signaled failure\n" ); 783 1.1 christos goto fail; 784 1.1 christos alloc_fail: 785 1.1 christos Debug( LDAP_DEBUG_ANY, "homedir: " 786 1.1 christos "traverse: allocation failed\n" ); 787 1.1 christos fail: 788 1.1 christos rc = 1; 789 1.1 christos goto out; 790 1.1 christos 791 1.1 christos out: 792 1.1 christos while ( dir_stack != NULL ) { 793 1.1 christos free_name_list( dir_stack->list, ctx ); 794 1.1 christos next_dir = dir_stack->next; 795 1.1 christos ber_memfree_x( dir_stack, ctx ); 796 1.1 christos dir_stack = next_dir; 797 1.1 christos } 798 1.1 christos return rc; 799 1.1 christos } 800 1.1 christos 801 1.1 christos static traverse_cb_ret 802 1.1 christos traverse_copy_pre( 803 1.1 christos void *private, 804 1.1 christos const char *name, 805 1.1 christos const struct stat *st, 806 1.1 christos void *ctx ) 807 1.1 christos { 808 1.1 christos copy_private *cp = private; 809 1.1 christos char *dest_name = NULL; 810 1.1 christos int source_name_len; 811 1.1 christos int dest_name_len; 812 1.1 christos int rc; 813 1.1 christos 814 1.1 christos assert( private != NULL ); 815 1.1 christos assert( name != NULL ); 816 1.1 christos assert( st != NULL ); 817 1.1 christos 818 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 819 1.1 christos "traverse_copy_pre: %s entering\n", 820 1.1 christos name ); 821 1.1 christos 822 1.1 christos assert( cp->source_prefix_len >= 0 ); 823 1.1 christos assert( cp->dest_prefix != NULL ); 824 1.1 christos assert( cp->dest_prefix_len > 1 ); 825 1.1 christos 826 1.1 christos source_name_len = strlen( name ); 827 1.1 christos assert( source_name_len >= cp->source_prefix_len ); 828 1.1 christos /* +1 for terminator */ 829 1.1 christos dest_name_len = 830 1.1 christos source_name_len + cp->dest_prefix_len - cp->source_prefix_len + 1; 831 1.1 christos dest_name = ber_memalloc_x( dest_name_len, ctx ); 832 1.1 christos if ( dest_name == NULL ) goto alloc_fail; 833 1.1 christos 834 1.1 christos snprintf( dest_name, dest_name_len, "%s%s", cp->dest_prefix, 835 1.1 christos name + cp->source_prefix_len ); 836 1.1 christos 837 1.1 christos switch ( st->st_mode & S_IFMT ) { 838 1.1 christos case S_IFDIR: 839 1.1 christos rc = mkdir( dest_name, st->st_mode & 06775 ); 840 1.1 christos if ( rc ) { 841 1.1 christos int save_errno = errno; 842 1.1 christos switch ( save_errno ) { 843 1.1 christos case EEXIST: 844 1.1 christos /* directory already present; nothing to do */ 845 1.1 christos goto exists; 846 1.1 christos break; 847 1.1 christos case ENOENT: 848 1.1 christos /* FIXME: should mkdir -p here */ 849 1.1 christos /* fallthrough for now */ 850 1.1 christos default: 851 1.1 christos report_errno( "traverse_copy_pre", "mkdir", dest_name ); 852 1.1 christos goto fail; 853 1.1 christos } 854 1.1 christos } 855 1.1 christos rc = lchown( dest_name, cp->uidn, cp->gidn ); 856 1.1 christos if ( rc ) { 857 1.1 christos report_errno( "traverse_copy_pre", "lchown", dest_name ); 858 1.1 christos goto fail; 859 1.1 christos } 860 1.1 christos rc = chmod( dest_name, st->st_mode & 07777 ); 861 1.1 christos if ( rc ) { 862 1.1 christos report_errno( "traverse_copy_pre", "chmod", dest_name ); 863 1.1 christos goto fail; 864 1.1 christos } 865 1.1 christos break; 866 1.1 christos case S_IFREG: 867 1.1 christos rc = copy_file( 868 1.1 christos dest_name, name, cp->uidn, cp->gidn, st->st_mode & 07777 ); 869 1.1 christos if ( rc ) goto fail; 870 1.1 christos break; 871 1.1 christos case S_IFIFO: 872 1.1 christos rc = mkfifo( dest_name, 0700 ); 873 1.1 christos if ( rc ) { 874 1.1 christos report_errno( "traverse_copy_pre", "mkfifo", dest_name ); 875 1.1 christos goto fail; 876 1.1 christos } 877 1.1 christos rc = lchown( dest_name, cp->uidn, cp->gidn ); 878 1.1 christos if ( rc ) { 879 1.1 christos report_errno( "traverse_copy_pre", "lchown", dest_name ); 880 1.1 christos goto fail; 881 1.1 christos } 882 1.1 christos rc = chmod( dest_name, st->st_mode & 07777 ); 883 1.1 christos if ( rc ) { 884 1.1 christos report_errno( "traverse_copy_pre", "chmod", dest_name ); 885 1.1 christos goto fail; 886 1.1 christos } 887 1.1 christos break; 888 1.1 christos case S_IFLNK: 889 1.1 christos rc = copy_link( dest_name, name, st, cp->uidn, cp->gidn, ctx ); 890 1.1 christos if ( rc ) goto fail; 891 1.1 christos break; 892 1.1 christos default: 893 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 894 1.1 christos "traverse_copy_pre: skipping special: %s\n", 895 1.1 christos name ); 896 1.1 christos } 897 1.1 christos 898 1.1 christos goto success; 899 1.1 christos 900 1.1 christos alloc_fail: 901 1.1 christos Debug( LDAP_DEBUG_ANY, "homedir: " 902 1.1 christos "traverse_copy_pre: allocation failed\n" ); 903 1.1 christos fail: 904 1.1 christos rc = TRAVERSE_CB_FAIL; 905 1.1 christos goto out; 906 1.1 christos 907 1.1 christos exists: 908 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 909 1.1 christos "traverse_copy_pre: \"%s\" already exists," 910 1.1 christos " skipping the rest\n", 911 1.1 christos dest_name ); 912 1.1 christos rc = TRAVERSE_CB_DONE; 913 1.1 christos goto out; 914 1.1 christos 915 1.1 christos success: 916 1.1 christos rc = TRAVERSE_CB_CONTINUE; 917 1.1 christos out: 918 1.1 christos if ( dest_name != NULL ) ber_memfree_x( dest_name, ctx ); 919 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 920 1.1 christos "traverse_copy_pre: exit %d\n", rc ); 921 1.1 christos return rc; 922 1.1 christos } 923 1.1 christos 924 1.1 christos static int 925 1.1 christos copy_tree( 926 1.1 christos const char *dest_path, 927 1.1 christos const char *source_path, 928 1.1 christos uid_t uidn, 929 1.1 christos gid_t gidn, 930 1.1 christos void *ctx ) 931 1.1 christos { 932 1.1 christos traverse_cb cb; 933 1.1 christos copy_private cp; 934 1.1 christos int rc; 935 1.1 christos 936 1.1 christos assert( dest_path != NULL ); 937 1.1 christos assert( source_path != NULL ); 938 1.1 christos 939 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 940 1.1 christos "copy_tree: %s to %s entering\n", 941 1.1 christos source_path, dest_path ); 942 1.1 christos 943 1.1 christos cb.pre_func = traverse_copy_pre; 944 1.1 christos cb.post_func = NULL; 945 1.1 christos cb.pre_private = &cp; 946 1.1 christos cb.post_private = NULL; 947 1.1 christos 948 1.1 christos cp.source_prefix_len = strlen( source_path ); 949 1.1 christos cp.dest_prefix = dest_path; 950 1.1 christos cp.dest_prefix_len = strlen( dest_path ); 951 1.1 christos cp.uidn = uidn; 952 1.1 christos cp.gidn = gidn; 953 1.1 christos 954 1.1 christos if ( cp.source_prefix_len <= cp.dest_prefix_len && 955 1.1 christos strncmp( source_path, dest_path, cp.source_prefix_len ) == 0 && 956 1.1 christos ( cp.source_prefix_len == cp.dest_prefix_len || 957 1.1 christos dest_path[cp.source_prefix_len] == LDAP_DIRSEP[0] ) ) { 958 1.1 christos Debug( LDAP_DEBUG_ANY, "homedir: " 959 1.1 christos "copy_tree: aborting: %s contains %s\n", 960 1.1 christos source_path, dest_path ); 961 1.1 christos return 1; 962 1.1 christos } 963 1.1 christos 964 1.1 christos rc = traverse( source_path, &cb, ctx ); 965 1.1 christos 966 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 967 1.1 christos "copy_tree: %s exit %d\n", source_path, 968 1.1 christos rc ); 969 1.1 christos 970 1.1 christos return rc; 971 1.1 christos } 972 1.1 christos 973 1.1 christos static int 974 1.1 christos homedir_provision( 975 1.1 christos const char *dest_path, 976 1.1 christos const char *skel_path, 977 1.1 christos uid_t uidn, 978 1.1 christos gid_t gidn, 979 1.1 christos void *ctx ) 980 1.1 christos { 981 1.1 christos int rc; 982 1.1 christos 983 1.1 christos assert( dest_path != NULL ); 984 1.1 christos 985 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 986 1.1 christos "homedir_provision: %s from skeleton %s\n", 987 1.1 christos dest_path, skel_path == NULL ? "(none)" : skel_path ); 988 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 989 1.1 christos "homedir_provision: %s uidn %ld gidn %ld\n", 990 1.1 christos dest_path, (long)uidn, (long)gidn ); 991 1.1 christos 992 1.1 christos if ( skel_path == NULL ) { 993 1.1 christos rc = mkdir( dest_path, 0700 ); 994 1.1 christos if ( rc ) { 995 1.1 christos int save_errno = errno; 996 1.1 christos switch ( save_errno ) { 997 1.1 christos case EEXIST: 998 1.1 christos /* directory already present; nothing to do */ 999 1.1 christos /* but down chown either */ 1000 1.1 christos rc = 0; 1001 1.1 christos goto out; 1002 1.1 christos break; 1003 1.1 christos default: 1004 1.1 christos report_errno( "provision_homedir", "mkdir", dest_path ); 1005 1.1 christos goto fail; 1006 1.1 christos } 1007 1.1 christos } 1008 1.1 christos rc = lchown( dest_path, uidn, gidn ); 1009 1.1 christos if ( rc ) { 1010 1.1 christos report_errno( "provision_homedir", "lchown", dest_path ); 1011 1.1 christos goto fail; 1012 1.1 christos } 1013 1.1 christos 1014 1.1 christos } else { 1015 1.1 christos rc = copy_tree( dest_path, skel_path, uidn, gidn, ctx ); 1016 1.1 christos } 1017 1.1 christos 1018 1.1 christos goto out; 1019 1.1 christos 1020 1.1 christos fail: 1021 1.1 christos rc = 1; 1022 1.1 christos goto out; 1023 1.1 christos out: 1024 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 1025 1.1 christos "homedir_provision: %s to %s exit %d\n", 1026 1.1 christos skel_path, dest_path, rc ); 1027 1.1 christos return rc; 1028 1.1 christos } 1029 1.1 christos 1030 1.1 christos /* traverse func for rm -rf */ 1031 1.1 christos static traverse_cb_ret 1032 1.1 christos traverse_remove_post( 1033 1.1 christos void *private, 1034 1.1 christos const char *name, 1035 1.1 christos const struct stat *st, 1036 1.1 christos void *ctx ) 1037 1.1 christos { 1038 1.1 christos int rc; 1039 1.1 christos 1040 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 1041 1.1 christos "traverse_remove_post: %s entering\n", 1042 1.1 christos name ); 1043 1.1 christos 1044 1.1 christos if ( (st->st_mode & S_IFMT) == S_IFDIR ) { 1045 1.1 christos rc = rmdir( name ); 1046 1.1 christos if ( rc != 0 ) { 1047 1.1 christos report_errno( "traverse_remove_post", "rmdir", name ); 1048 1.1 christos goto fail; 1049 1.1 christos } 1050 1.1 christos } else { 1051 1.1 christos rc = unlink( name ); 1052 1.1 christos if ( rc != 0 ) { 1053 1.1 christos report_errno( "traverse_remove_post", "unlink", name ); 1054 1.1 christos goto fail; 1055 1.1 christos } 1056 1.1 christos } 1057 1.1 christos 1058 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 1059 1.1 christos "traverse_remove_post: %s exit continue\n", 1060 1.1 christos name ); 1061 1.1 christos return TRAVERSE_CB_CONTINUE; 1062 1.1 christos 1063 1.1 christos fail: 1064 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 1065 1.1 christos "traverse_remove_post: %s exit failure\n", 1066 1.1 christos name ); 1067 1.1 christos return TRAVERSE_CB_FAIL; 1068 1.1 christos } 1069 1.1 christos 1070 1.1 christos static int 1071 1.1 christos delete_tree( const char *path, void *ctx ) 1072 1.1 christos { 1073 1.1 christos const static traverse_cb cb = { NULL, traverse_remove_post, NULL, NULL }; 1074 1.1 christos int rc; 1075 1.1 christos 1076 1.1 christos assert( path != NULL ); 1077 1.1 christos 1078 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 1079 1.1 christos "delete_tree: %s entering\n", path ); 1080 1.1 christos 1081 1.1 christos rc = traverse( path, &cb, ctx ); 1082 1.1 christos 1083 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 1084 1.1 christos "delete_tree: %s exit %d\n", path, rc ); 1085 1.1 christos 1086 1.1 christos return rc; 1087 1.1 christos } 1088 1.1 christos 1089 1.1 christos static int 1090 1.1 christos get_tar_name( 1091 1.1 christos const char *path, 1092 1.1 christos const char *tar_path, 1093 1.1 christos char *tar_name, 1094 1.1 christos int name_size ) 1095 1.1 christos { 1096 1.1 christos int rc = 0; 1097 1.1 christos const char *ch; 1098 1.1 christos int fd = -1; 1099 1.1 christos int counter = 0; 1100 1.1 christos time_t now; 1101 1.1 christos 1102 1.1 christos assert( path != NULL ); 1103 1.1 christos assert( tar_path != NULL ); 1104 1.1 christos assert( tar_name != NULL ); 1105 1.1 christos 1106 1.1 christos for ( ch = path + strlen( path ); 1107 1.1 christos *ch != LDAP_DIRSEP[0] && ch > path; 1108 1.1 christos --ch ) 1109 1.1 christos ; 1110 1.1 christos if ( ch <= path || strlen( ch ) < 2 ) { 1111 1.1 christos Debug( LDAP_DEBUG_ANY, "homedir: " 1112 1.1 christos "get_tar_name: unable to construct a tar name from input " 1113 1.1 christos "path \"%s\"\n", 1114 1.1 christos path ); 1115 1.1 christos goto fail; 1116 1.1 christos } 1117 1.1 christos ++ch; /* skip past sep */ 1118 1.1 christos time( &now ); 1119 1.1 christos 1120 1.1 christos while ( fd < 0 ) { 1121 1.1 christos snprintf( tar_name, name_size, "%s" LDAP_DIRSEP "%s-%ld-%d.tar", 1122 1.1 christos tar_path, ch, (long)now, counter ); 1123 1.1 christos fd = open( tar_name, O_WRONLY|O_CREAT|O_EXCL, 0600 ); 1124 1.1 christos if ( fd < 0 ) { 1125 1.1 christos int save_errno = errno; 1126 1.1 christos if ( save_errno != EEXIST ) { 1127 1.1 christos report_errno( "get_tar_name", "open", tar_name ); 1128 1.1 christos goto fail; 1129 1.1 christos } 1130 1.1 christos ++counter; 1131 1.1 christos } 1132 1.1 christos } 1133 1.1 christos 1134 1.1 christos rc = 0; 1135 1.1 christos goto out; 1136 1.1 christos 1137 1.1 christos fail: 1138 1.1 christos rc = 1; 1139 1.1 christos *tar_name = '\0'; 1140 1.1 christos out: 1141 1.1 christos if ( fd >= 0 ) close( fd ); 1142 1.1 christos return rc; 1143 1.1 christos } 1144 1.1 christos 1145 1.1 christos /* traverse func for rechown */ 1146 1.1 christos static traverse_cb_ret 1147 1.1 christos traverse_chown_pre( 1148 1.1 christos void *private, 1149 1.1 christos const char *name, 1150 1.1 christos const struct stat *st, 1151 1.1 christos void *ctx ) 1152 1.1 christos { 1153 1.1 christos int rc; 1154 1.1 christos chown_private *cp = private; 1155 1.1 christos uid_t set_uidn = -1; 1156 1.1 christos gid_t set_gidn = -1; 1157 1.1 christos 1158 1.1 christos assert( private != NULL ); 1159 1.1 christos assert( name != NULL ); 1160 1.1 christos assert( st != NULL ); 1161 1.1 christos 1162 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 1163 1.1 christos "traverse_chown_pre: %s entering\n", 1164 1.1 christos name ); 1165 1.1 christos 1166 1.1 christos if ( st->st_uid == cp->old_uidn ) set_uidn = cp->new_uidn; 1167 1.1 christos if ( st->st_gid == cp->old_gidn ) set_gidn = cp->new_gidn; 1168 1.1 christos 1169 1.1 christos if ( set_uidn != (uid_t)-1 || set_gidn != (gid_t)-1 ) { 1170 1.1 christos rc = lchown( name, set_uidn, set_gidn ); 1171 1.1 christos if ( rc ) { 1172 1.1 christos report_errno( "traverse_chown_pre", "lchown", name ); 1173 1.1 christos goto fail; 1174 1.1 christos } 1175 1.1 christos } 1176 1.1 christos 1177 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 1178 1.1 christos "traverse_chown_pre: %s exit continue\n", 1179 1.1 christos name ); 1180 1.1 christos return TRAVERSE_CB_CONTINUE; 1181 1.1 christos 1182 1.1 christos fail: 1183 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 1184 1.1 christos "traverse_chown_pre: %s exit failure\n", 1185 1.1 christos name ); 1186 1.1 christos return TRAVERSE_CB_FAIL; 1187 1.1 christos } 1188 1.1 christos 1189 1.1 christos static int 1190 1.1 christos chown_tree( 1191 1.1 christos const char *path, 1192 1.1 christos uid_t old_uidn, 1193 1.1 christos uid_t new_uidn, 1194 1.1 christos gid_t old_gidn, 1195 1.1 christos gid_t new_gidn, 1196 1.1 christos void *ctx ) 1197 1.1 christos { 1198 1.1 christos traverse_cb cb; 1199 1.1 christos chown_private cp; 1200 1.1 christos int rc; 1201 1.1 christos 1202 1.1 christos assert( path != NULL ); 1203 1.1 christos 1204 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 1205 1.1 christos "chown_tree: %s entering\n", path ); 1206 1.1 christos 1207 1.1 christos cb.pre_func = traverse_chown_pre; 1208 1.1 christos cb.post_func = NULL; 1209 1.1 christos cb.pre_private = &cp; 1210 1.1 christos cb.post_private = NULL; 1211 1.1 christos 1212 1.1 christos cp.old_uidn = old_uidn; 1213 1.1 christos cp.new_uidn = new_uidn; 1214 1.1 christos cp.old_gidn = old_gidn; 1215 1.1 christos cp.new_gidn = new_gidn; 1216 1.1 christos 1217 1.1 christos rc = traverse( path, &cb, ctx ); 1218 1.1 christos 1219 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 1220 1.1 christos "chown_tree: %s exit %d\n", path, rc ); 1221 1.1 christos 1222 1.1 christos return rc; 1223 1.1 christos } 1224 1.1 christos 1225 1.1 christos static int 1226 1.1 christos homedir_rename( const char *source_path, const char *dest_path ) 1227 1.1 christos { 1228 1.1 christos int rc = 0; 1229 1.1 christos 1230 1.1 christos assert( source_path != NULL ); 1231 1.1 christos assert( dest_path != NULL ); 1232 1.1 christos 1233 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 1234 1.1 christos "homedir_rename: %s to %s\n", 1235 1.1 christos source_path, dest_path ); 1236 1.1 christos rc = rename( source_path, dest_path ); 1237 1.1 christos if ( rc != 0 ) { 1238 1.1 christos char ebuf[1024]; 1239 1.1 christos int save_errno = errno; 1240 1.1 christos 1241 1.1 christos Debug( LDAP_DEBUG_ANY, "homedir: " 1242 1.1 christos "homedir_rename: rename(\"%s\", \"%s\"): (%s)\n", 1243 1.1 christos source_path, dest_path, 1244 1.1 christos AC_STRERROR_R( save_errno, ebuf, sizeof(ebuf) ) ); 1245 1.1 christos } 1246 1.1 christos 1247 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 1248 1.1 christos "homedir_rename: %s to %s exit %d\n", 1249 1.1 christos source_path, dest_path, rc ); 1250 1.1 christos return rc; 1251 1.1 christos } 1252 1.1 christos 1253 1.1 christos /* FIXME: This assumes ASCII; needs fixing for z/OS */ 1254 1.1 christos static int 1255 1.1 christos tar_set_header( ustar_header *tar, const struct stat *st, const char *name ) 1256 1.1 christos { 1257 1.1 christos int name_len; 1258 1.1 christos int rc; 1259 1.1 christos const char *ch, *end; 1260 1.1 christos 1261 1.1 christos assert( tar != NULL ); 1262 1.1 christos assert( st != NULL ); 1263 1.1 christos assert( name != NULL ); 1264 1.1 christos assert( sizeof(*tar) == 512 ); 1265 1.1 christos assert( sizeof(tar->name) == 100 ); 1266 1.1 christos assert( sizeof(tar->prefix) == 155 ); 1267 1.1 christos assert( sizeof(tar->checksum) == 8 ); 1268 1.1 christos 1269 1.1 christos memset( tar, 0, sizeof(*tar) ); 1270 1.1 christos 1271 1.1 christos assert( name[0] == LDAP_DIRSEP[0] ); 1272 1.1 christos name += 1; /* skip leading / */ 1273 1.1 christos 1274 1.1 christos name_len = strlen( name ); 1275 1.1 christos 1276 1.1 christos /* fits in tar->name? */ 1277 1.1 christos /* Yes, name and prefix do not need a trailing nul. */ 1278 1.1 christos if ( name_len <= 100 ) { 1279 1.1 christos strncpy( tar->name, name, 100 ); 1280 1.1 christos 1281 1.1 christos /* try fit in tar->name + tar->prefix */ 1282 1.1 christos } else { 1283 1.1 christos /* try to find something to stick into tar->name */ 1284 1.1 christos for ( ch = name + name_len - 100, end = name + name_len; 1285 1.1 christos ch < end && *ch != LDAP_DIRSEP[0]; 1286 1.1 christos ++ch ) 1287 1.1 christos ; 1288 1.1 christos if ( end - ch > 0 ) /* +1 skip past sep */ 1289 1.1 christos ch++; 1290 1.1 christos else { 1291 1.1 christos /* reset; name too long for UStar */ 1292 1.1 christos Debug( LDAP_DEBUG_ANY, "homedir: " 1293 1.1 christos "tar_set_header: name too long: \"%s\"\n", 1294 1.1 christos name ); 1295 1.1 christos ch = name + name_len - 100; 1296 1.1 christos } 1297 1.1 christos strncpy( tar->name, ch + 1, 100 ); 1298 1.1 christos { 1299 1.1 christos int prefix_len = ( ch - 1 ) - name; 1300 1.1 christos if ( prefix_len > 155 ) prefix_len = 155; 1301 1.1 christos strncpy( tar->prefix, name, prefix_len ); 1302 1.1 christos } 1303 1.1 christos } 1304 1.1 christos 1305 1.1 christos snprintf( tar->mode, 8, "%06lo ", (long)st->st_mode & 07777 ); 1306 1.1 christos snprintf( tar->uid, 8, "%06lo ", (long)st->st_uid ); 1307 1.1 christos snprintf( tar->gid, 8, "%06lo ", (long)st->st_gid ); 1308 1.1 christos snprintf( tar->mtime, 12, "%010lo ", (long)st->st_mtime ); 1309 1.1 christos snprintf( tar->size, 12, "%010lo ", (long)0 ); 1310 1.1 christos switch ( st->st_mode & S_IFMT ) { 1311 1.1 christos case S_IFREG: 1312 1.1 christos tar->typeflag[0] = '0'; 1313 1.1 christos snprintf( tar->size, 12, "%010lo ", (long)st->st_size ); 1314 1.1 christos break; 1315 1.1 christos case S_IFLNK: 1316 1.1 christos tar->typeflag[0] = '2'; 1317 1.1 christos rc = readlink( name - 1, tar->linkname, 99 ); 1318 1.1 christos if ( rc == -1 ) { 1319 1.1 christos report_errno( "tar_set_header", "readlink", name ); 1320 1.1 christos goto fail; 1321 1.1 christos } 1322 1.1 christos break; 1323 1.1 christos case S_IFCHR: 1324 1.1 christos tar->typeflag[0] = '3'; 1325 1.1 christos /* FIXME: this is probably wrong but shouldn't likely be an issue */ 1326 1.1 christos snprintf( tar->devmajor, 8, "%06lo ", (long)st->st_rdev >> 16 ); 1327 1.1 christos snprintf( tar->devminor, 8, "%06lo ", (long)st->st_rdev & 0xffff ); 1328 1.1 christos break; 1329 1.1 christos case S_IFBLK: 1330 1.1 christos tar->typeflag[0] = '4'; 1331 1.1 christos /* FIXME: this is probably wrong but shouldn't likely be an issue */ 1332 1.1 christos snprintf( tar->devmajor, 8, "%06lo ", (long)st->st_rdev >> 16 ); 1333 1.1 christos snprintf( tar->devminor, 8, "%06lo ", (long)st->st_rdev & 0xffff ); 1334 1.1 christos break; 1335 1.1 christos case S_IFDIR: 1336 1.1 christos tar->typeflag[0] = '5'; 1337 1.1 christos break; 1338 1.1 christos case S_IFIFO: 1339 1.1 christos tar->typeflag[0] = '6'; 1340 1.1 christos break; 1341 1.1 christos default: 1342 1.1 christos goto fail; 1343 1.1 christos } 1344 1.1 christos snprintf( tar->magic, 6, "ustar" ); 1345 1.1 christos tar->version[0] = '0'; 1346 1.1 christos tar->version[1] = '0'; 1347 1.1 christos 1348 1.1 christos { 1349 1.1 christos unsigned char *uch = (unsigned char *)tar; 1350 1.1 christos unsigned char *uend = uch + 512; 1351 1.1 christos unsigned long sum = 0; 1352 1.1 christos 1353 1.1 christos memset( &tar->checksum, ' ', sizeof(tar->checksum) ); 1354 1.1 christos 1355 1.1 christos for ( ; uch < uend; ++uch ) 1356 1.1 christos sum += *uch; 1357 1.1 christos 1358 1.1 christos /* zero-padded, six octal digits, followed by NUL then space (!) */ 1359 1.1 christos /* Yes, that's terminated exactly reverse of the others. */ 1360 1.1 christos snprintf( tar->checksum, sizeof(tar->checksum) - 1, "%06lo", sum ); 1361 1.1 christos } 1362 1.1 christos 1363 1.1 christos return 0; 1364 1.1 christos fail: 1365 1.1 christos return 1; 1366 1.1 christos } 1367 1.1 christos 1368 1.1 christos static traverse_cb_ret 1369 1.1 christos traverse_tar_pre( 1370 1.1 christos void *private, 1371 1.1 christos const char *name, 1372 1.1 christos const struct stat *st, 1373 1.1 christos void *ctx ) 1374 1.1 christos { 1375 1.1 christos int rc; 1376 1.1 christos traverse_cb_ret cbrc; 1377 1.1 christos tar_private *tp = private; 1378 1.1 christos ustar_header tar; 1379 1.1 christos FILE *source = NULL; 1380 1.1 christos 1381 1.1 christos assert( private != NULL ); 1382 1.1 christos assert( name != NULL ); 1383 1.1 christos assert( st != NULL ); 1384 1.1 christos 1385 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 1386 1.1 christos "traverse_tar_pre: %s entering\n", name ); 1387 1.1 christos 1388 1.1 christos switch ( st->st_mode & S_IFMT ) { 1389 1.1 christos case S_IFREG: 1390 1.1 christos if ( sizeof(st->st_size) > 4 && ( st->st_size >> 33 ) >= 1 ) { 1391 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 1392 1.1 christos "traverse_tar_pre: %s is larger than 8GiB POSIX UStar " 1393 1.1 christos "file size limit\n", 1394 1.1 christos name ); 1395 1.1 christos goto fail; 1396 1.1 christos } 1397 1.1 christos /* fallthrough */ 1398 1.1 christos case S_IFDIR: 1399 1.1 christos case S_IFLNK: 1400 1.1 christos case S_IFIFO: 1401 1.1 christos case S_IFCHR: 1402 1.1 christos case S_IFBLK: 1403 1.1 christos rc = tar_set_header( &tar, st, name ); 1404 1.1 christos if ( rc ) goto fail; 1405 1.1 christos break; 1406 1.1 christos default: 1407 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 1408 1.1 christos "traverse_tar_pre: skipping \"%s\" mode %o\n", 1409 1.1 christos name, st->st_mode ); 1410 1.1 christos goto done; 1411 1.1 christos } 1412 1.1 christos 1413 1.1 christos rc = fwrite( &tar, 1, 512, tp->file ); 1414 1.1 christos if ( rc != 512 ) { 1415 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 1416 1.1 christos "traverse_tar_pre: write error in tar header\n" ); 1417 1.1 christos goto fail; 1418 1.1 christos } 1419 1.1 christos 1420 1.1 christos if ( (st->st_mode & S_IFMT) == S_IFREG ) { 1421 1.1 christos source = fopen( name, "rb" ); 1422 1.1 christos if ( source == NULL ) { 1423 1.1 christos report_errno( "traverse_tar_pre", "fopen", name ); 1424 1.1 christos goto fail; 1425 1.1 christos } 1426 1.1 christos rc = copy_blocks( source, tp->file, name, tp->name ); 1427 1.1 christos if ( rc != 0 ) goto fail; 1428 1.1 christos fclose( source ); 1429 1.1 christos source = NULL; 1430 1.1 christos } 1431 1.1 christos 1432 1.1 christos { /* advance to end of record */ 1433 1.1 christos off_t pos = ftello( tp->file ); 1434 1.1 christos if ( pos == -1 ) { 1435 1.1 christos report_errno( "traverse_tar_pre", "ftello", tp->name ); 1436 1.1 christos goto fail; 1437 1.1 christos } 1438 1.1 christos pos += ( 512 - ( pos % 512 ) ) % 512; 1439 1.1 christos rc = fseeko( tp->file, pos, SEEK_SET ); 1440 1.1 christos if ( rc != 0 ) { 1441 1.1 christos report_errno( "traverse_tar_pre", "fseeko", tp->name ); 1442 1.1 christos goto fail; 1443 1.1 christos } 1444 1.1 christos } 1445 1.1 christos 1446 1.1 christos done: 1447 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 1448 1.1 christos "traverse_tar_pre: %s exit continue\n", 1449 1.1 christos name ); 1450 1.1 christos cbrc = TRAVERSE_CB_CONTINUE; 1451 1.1 christos goto out; 1452 1.1 christos fail: 1453 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 1454 1.1 christos "traverse_tar_pre: %s exit failure\n", 1455 1.1 christos name ); 1456 1.1 christos cbrc = TRAVERSE_CB_FAIL; 1457 1.1 christos 1458 1.1 christos out: 1459 1.1 christos if ( source != NULL ) fclose( source ); 1460 1.1 christos return cbrc; 1461 1.1 christos } 1462 1.1 christos 1463 1.1 christos static int 1464 1.1 christos tar_tree( const char *path, const char *tar_name, void *ctx ) 1465 1.1 christos { 1466 1.1 christos traverse_cb cb; 1467 1.1 christos tar_private tp; 1468 1.1 christos int rc; 1469 1.1 christos 1470 1.1 christos assert( path != NULL ); 1471 1.1 christos assert( tar_name != NULL ); 1472 1.1 christos 1473 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 1474 1.1 christos "tar_tree: %s into %s entering\n", path, 1475 1.1 christos tar_name ); 1476 1.1 christos 1477 1.1 christos cb.pre_func = traverse_tar_pre; 1478 1.1 christos cb.post_func = NULL; 1479 1.1 christos cb.pre_private = &tp; 1480 1.1 christos cb.post_private = NULL; 1481 1.1 christos 1482 1.1 christos tp.name = tar_name; 1483 1.1 christos tp.file = fopen( tar_name, "wb" ); 1484 1.1 christos if ( tp.file == NULL ) { 1485 1.1 christos report_errno( "tar_tree", "fopen", tar_name ); 1486 1.1 christos goto fail; 1487 1.1 christos } 1488 1.1 christos 1489 1.1 christos rc = traverse( path, &cb, ctx ); 1490 1.1 christos if ( rc != 0 ) goto fail; 1491 1.1 christos 1492 1.1 christos { 1493 1.1 christos off_t pos = ftello( tp.file ); 1494 1.1 christos if ( pos == -1 ) { 1495 1.1 christos report_errno( "tar_tree", "ftello", tp.name ); 1496 1.1 christos goto fail; 1497 1.1 christos } 1498 1.1 christos pos += 1024; /* two zero records */ 1499 1.1 christos pos += ( 10240 - ( pos % 10240 ) ) % 10240; 1500 1.1 christos rc = ftruncate( fileno( tp.file ), pos ); 1501 1.1 christos if ( rc != 0 ) { 1502 1.1 christos report_errno( "tar_tree", "ftrunctate", tp.name ); 1503 1.1 christos goto fail; 1504 1.1 christos } 1505 1.1 christos } 1506 1.1 christos 1507 1.1 christos rc = fclose( tp.file ); 1508 1.1 christos tp.file = NULL; 1509 1.1 christos if ( rc != 0 ) { 1510 1.1 christos report_errno( "tar_tree", "fclose", tp.name ); 1511 1.1 christos goto fail; 1512 1.1 christos } 1513 1.1 christos goto out; 1514 1.1 christos 1515 1.1 christos fail: 1516 1.1 christos rc = 1; 1517 1.1 christos out: 1518 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 1519 1.1 christos "tar_tree: %s exit %d\n", path, rc ); 1520 1.1 christos if ( tp.file != NULL ) fclose( tp.file ); 1521 1.1 christos return rc; 1522 1.1 christos } 1523 1.1 christos 1524 1.1 christos static int 1525 1.1 christos homedir_deprovision( const homedir_data *data, const char *path, void *ctx ) 1526 1.1 christos { 1527 1.1 christos int rc = 0; 1528 1.1 christos char tar_name[1024]; 1529 1.1 christos 1530 1.1 christos assert( data != NULL ); 1531 1.1 christos assert( path != NULL ); 1532 1.1 christos 1533 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 1534 1.1 christos "homedir_deprovision: %s entering\n", 1535 1.1 christos path ); 1536 1.1 christos 1537 1.1 christos switch ( data->style ) { 1538 1.1 christos case DEL_IGNORE: 1539 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 1540 1.1 christos "homedir_deprovision: style is ignore\n" ); 1541 1.1 christos break; 1542 1.1 christos case DEL_ARCHIVE: 1543 1.1 christos if ( data->archive_path == NULL ) { 1544 1.1 christos Debug( LDAP_DEBUG_ANY, "homedir: " 1545 1.1 christos "homedir_deprovision: archive path not set\n" ); 1546 1.1 christos goto fail; 1547 1.1 christos } 1548 1.1 christos rc = get_tar_name( path, data->archive_path, tar_name, 1024 ); 1549 1.1 christos if ( rc != 0 ) goto fail; 1550 1.1 christos rc = tar_tree( path, tar_name, ctx ); 1551 1.1 christos if ( rc != 0 ) { 1552 1.1 christos Debug( LDAP_DEBUG_ANY, "homedir: " 1553 1.1 christos "homedir_deprovision: archive failed, not deleting\n" ); 1554 1.1 christos goto fail; 1555 1.1 christos } 1556 1.1 christos /* fall-through */ 1557 1.1 christos case DEL_DELETE: 1558 1.1 christos rc = delete_tree( path, ctx ); 1559 1.1 christos break; 1560 1.1 christos default: 1561 1.1 christos abort(); 1562 1.1 christos } 1563 1.1 christos 1564 1.1 christos rc = 0; 1565 1.1 christos goto out; 1566 1.1 christos 1567 1.1 christos fail: 1568 1.1 christos rc = 1; 1569 1.1 christos out: 1570 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 1571 1.1 christos "homedir_deprovision: %s leaving\n", 1572 1.1 christos path ); 1573 1.1 christos 1574 1.1 christos return rc; 1575 1.1 christos } 1576 1.1 christos 1577 1.1 christos /* FIXME: This assumes ASCII; needs fixing for z/OS */ 1578 1.1 christos /* FIXME: This should also be in a slapd library function somewhere */ 1579 1.1 christos #define MAX_MATCHES ( 10 ) 1580 1.1 christos static int 1581 1.1 christos homedir_match( 1582 1.1 christos const homedir_regexp *r, 1583 1.1 christos const char *homedir, 1584 1.1 christos char *result, 1585 1.1 christos size_t result_size ) 1586 1.1 christos { 1587 1.1 christos int rc; 1588 1.1 christos int n; 1589 1.1 christos regmatch_t matches[MAX_MATCHES]; 1590 1.1 christos char *resc, *repc; 1591 1.1 christos 1592 1.1 christos assert( r != NULL ); 1593 1.1 christos assert( homedir != NULL ); 1594 1.1 christos assert( result_size > 1 ); 1595 1.1 christos 1596 1.1 christos memset( matches, 0, sizeof(matches) ); 1597 1.1 christos rc = regexec( &r->compiled, homedir, MAX_MATCHES, matches, 0 ); 1598 1.1 christos if ( rc ) { 1599 1.1 christos if ( rc != REG_NOMATCH ) { 1600 1.1 christos char msg[256]; 1601 1.1 christos regerror( rc, &r->compiled, msg, sizeof(msg) ); 1602 1.1 christos Debug( LDAP_DEBUG_ANY, "homedir_match: " 1603 1.1 christos "%s\n", msg ); 1604 1.1 christos } 1605 1.1 christos return rc; 1606 1.1 christos } 1607 1.1 christos 1608 1.1 christos for ( resc = result, repc = r->replace; 1609 1.1 christos result_size > 1 && *repc != '\0'; 1610 1.1 christos ++repc, ++resc, --result_size ) { 1611 1.1 christos switch ( *repc ) { 1612 1.1 christos case '$': 1613 1.1 christos ++repc; 1614 1.1 christos n = ( *repc ) - '0'; 1615 1.1 christos if ( n < 0 || n > ( MAX_MATCHES - 1 ) || 1616 1.1 christos matches[n].rm_so < 0 ) { 1617 1.1 christos Debug( LDAP_DEBUG_ANY, "homedir: " 1618 1.1 christos "invalid regex term expansion in \"%s\" " 1619 1.1 christos "at char %ld, n is %d\n", 1620 1.1 christos r->replace, (long)( repc - r->replace ), n ); 1621 1.1 christos return 1; 1622 1.1 christos } 1623 1.1 christos { 1624 1.1 christos size_t match_len = matches[n].rm_eo - matches[n].rm_so; 1625 1.1 christos const char *match_start = homedir + matches[n].rm_so; 1626 1.1 christos if ( match_len >= result_size ) goto too_long; 1627 1.1 christos 1628 1.1 christos memcpy( resc, match_start, match_len ); 1629 1.1 christos result_size -= match_len; 1630 1.1 christos resc += match_len - 1; 1631 1.1 christos } 1632 1.1 christos break; 1633 1.1 christos 1634 1.1 christos case '\\': 1635 1.1 christos ++repc; 1636 1.1 christos /* fallthrough */ 1637 1.1 christos 1638 1.1 christos default: 1639 1.1 christos *resc = *repc; 1640 1.1 christos } 1641 1.1 christos } 1642 1.1 christos *resc = '\0'; 1643 1.1 christos if ( *repc != '\0' ) goto too_long; 1644 1.1 christos 1645 1.1 christos return 0; 1646 1.1 christos 1647 1.1 christos too_long: 1648 1.1 christos Debug( LDAP_DEBUG_ANY, "homedir: " 1649 1.1 christos "regex expansion of %s too long\n", 1650 1.1 christos r->replace ); 1651 1.1 christos *result = '\0'; 1652 1.1 christos return 1; 1653 1.1 christos } 1654 1.1 christos 1655 1.1 christos /* Sift through an entry for interesting values 1656 1.1 christos * return 0 on success and set vars 1657 1.1 christos * return 1 if homedir is not present or not valid 1658 1.1 christos * sets presence if any homedir attributes are noticed 1659 1.1 christos */ 1660 1.1 christos static int 1661 1.1 christos harvest_values( 1662 1.1 christos const homedir_data *data, 1663 1.1 christos const Entry *e, 1664 1.1 christos char *home_buf, 1665 1.1 christos int home_buf_size, 1666 1.1 christos uid_t *uidn, 1667 1.1 christos gid_t *gidn, 1668 1.1 christos int *presence ) 1669 1.1 christos { 1670 1.1 christos Attribute *a; 1671 1.1 christos char *homedir = NULL; 1672 1.1 christos 1673 1.1 christos assert( data != NULL ); 1674 1.1 christos assert( e != NULL ); 1675 1.1 christos assert( home_buf != NULL ); 1676 1.1 christos assert( home_buf_size > 1 ); 1677 1.1 christos assert( uidn != NULL ); 1678 1.1 christos assert( gidn != NULL ); 1679 1.1 christos assert( presence != NULL ); 1680 1.1 christos 1681 1.1 christos *presence = 0; 1682 1.1 christos if ( e == NULL ) return 1; 1683 1.1 christos *uidn = 0; 1684 1.1 christos *gidn = 0; 1685 1.1 christos 1686 1.1 christos for ( a = e->e_attrs; a->a_next != NULL; a = a->a_next ) { 1687 1.1 christos if ( a->a_desc == data->home_ad ) { 1688 1.1 christos homedir = a->a_vals[0].bv_val; 1689 1.1 christos *presence = 1; 1690 1.1 christos } else if ( a->a_desc == data->uidn_ad ) { 1691 1.1 christos *uidn = (uid_t)strtol( a->a_vals[0].bv_val, NULL, 10 ); 1692 1.1 christos *presence = 1; 1693 1.1 christos } else if ( a->a_desc == data->gidn_ad ) { 1694 1.1 christos *gidn = (gid_t)strtol( a->a_vals[0].bv_val, NULL, 10 ); 1695 1.1 christos *presence = 1; 1696 1.1 christos } 1697 1.1 christos } 1698 1.1 christos if ( homedir != NULL ) { 1699 1.1 christos homedir_regexp *r; 1700 1.1 christos 1701 1.1 christos for ( r = data->regexps; r != NULL; r = r->next ) { 1702 1.1 christos int rc = homedir_match( r, homedir, home_buf, home_buf_size ); 1703 1.1 christos if ( rc == 0 ) return 0; 1704 1.1 christos } 1705 1.1 christos } 1706 1.1 christos 1707 1.1 christos return 1; 1708 1.1 christos } 1709 1.1 christos 1710 1.1 christos static int 1711 1.1 christos homedir_mod_cleanup( Operation *op, SlapReply *rs ) 1712 1.1 christos { 1713 1.1 christos slap_callback *cb = NULL; 1714 1.1 christos slap_callback **cbp = NULL; 1715 1.1 christos homedir_cb_data *cb_data = NULL; 1716 1.1 christos Entry *e = NULL; 1717 1.1 christos 1718 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 1719 1.1 christos "homedir_mod_cleanup: entering\n" ); 1720 1.1 christos 1721 1.1 christos for ( cbp = &op->o_callback; 1722 1.1 christos *cbp != NULL && (*cbp)->sc_cleanup != homedir_mod_cleanup; 1723 1.1 christos cbp = &(*cbp)->sc_next ) 1724 1.1 christos ; 1725 1.1 christos 1726 1.1 christos if ( *cbp == NULL ) goto out; 1727 1.1 christos cb = *cbp; 1728 1.1 christos 1729 1.1 christos cb_data = (homedir_cb_data *)cb->sc_private; 1730 1.1 christos e = cb_data->entry; 1731 1.1 christos 1732 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 1733 1.1 christos "homedir_mod_cleanup: found <%s>\n", 1734 1.1 christos e->e_nname.bv_val ); 1735 1.1 christos entry_free( e ); 1736 1.1 christos op->o_tmpfree( cb_data, op->o_tmpmemctx ); 1737 1.1 christos *cbp = cb->sc_next; 1738 1.1 christos op->o_tmpfree( cb, op->o_tmpmemctx ); 1739 1.1 christos 1740 1.1 christos out: 1741 1.1 christos 1742 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 1743 1.1 christos "homedir_mod_cleanup: leaving\n" ); 1744 1.1 christos return SLAP_CB_CONTINUE; 1745 1.1 christos } 1746 1.1 christos 1747 1.1 christos static int 1748 1.1 christos homedir_mod_response( Operation *op, SlapReply *rs ) 1749 1.1 christos { 1750 1.1 christos slap_overinst *on = NULL; 1751 1.1 christos homedir_data *data = NULL; 1752 1.1 christos slap_callback *cb = NULL; 1753 1.1 christos homedir_cb_data *cb_data = NULL; 1754 1.1 christos Entry *e = NULL; 1755 1.1 christos int rc = SLAP_CB_CONTINUE; 1756 1.1 christos 1757 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 1758 1.1 christos "homedir_mod_response: entering\n" ); 1759 1.1 christos 1760 1.1 christos if ( rs->sr_err != LDAP_SUCCESS ) { 1761 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 1762 1.1 christos "homedir_mod_response: op was not successful\n" ); 1763 1.1 christos goto out; 1764 1.1 christos } 1765 1.1 christos 1766 1.1 christos /* Retrieve stashed entry */ 1767 1.1 christos for ( cb = op->o_callback; 1768 1.1 christos cb != NULL && cb->sc_cleanup != homedir_mod_cleanup; 1769 1.1 christos cb = cb->sc_next ) 1770 1.1 christos ; 1771 1.1 christos if ( cb == NULL ) goto out; 1772 1.1 christos cb_data = (homedir_cb_data *)cb->sc_private; 1773 1.1 christos e = cb_data->entry; 1774 1.1 christos on = cb_data->on; 1775 1.1 christos data = on->on_bi.bi_private; 1776 1.1 christos assert( e != NULL ); 1777 1.1 christos assert( data != NULL ); 1778 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 1779 1.1 christos "homedir_mod_response: found <%s>\n", 1780 1.1 christos e->e_nname.bv_val ); 1781 1.1 christos 1782 1.1 christos switch ( op->o_tag ) { 1783 1.1 christos case LDAP_REQ_DELETE: { 1784 1.1 christos char home_buf[1024]; 1785 1.1 christos uid_t uidn = 0; 1786 1.1 christos gid_t gidn = 0; 1787 1.1 christos int presence; 1788 1.1 christos 1789 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 1790 1.1 christos "homedir_mod_response: successful delete found\n" ); 1791 1.1 christos rc = harvest_values( data, e, home_buf, sizeof(home_buf), &uidn, 1792 1.1 christos &gidn, &presence ); 1793 1.1 christos if ( rc == 0 && uidn >= data->min_uid ) { 1794 1.1 christos homedir_deprovision( data, home_buf, op->o_tmpmemctx ); 1795 1.1 christos } else { 1796 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 1797 1.1 christos "homedir_mod_response: skipping\n" ); 1798 1.1 christos } 1799 1.1 christos rc = SLAP_CB_CONTINUE; 1800 1.1 christos break; 1801 1.1 christos } 1802 1.1 christos 1803 1.1 christos case LDAP_REQ_MODIFY: 1804 1.1 christos case LDAP_REQ_MODRDN: { 1805 1.1 christos Operation nop = *op; 1806 1.1 christos Entry *old_entry = e; 1807 1.1 christos Entry *new_entry = NULL; 1808 1.1 christos Entry *etmp; 1809 1.1 christos char old_home[1024]; 1810 1.1 christos char new_home[1024]; 1811 1.1 christos uid_t old_uidn, new_uidn; 1812 1.1 christos uid_t old_gidn, new_gidn; 1813 1.1 christos int old_valid = 0; 1814 1.1 christos int new_valid = 0; 1815 1.1 christos int old_presence, new_presence; 1816 1.1 christos 1817 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 1818 1.1 christos "homedir_mod_response: successful modify/modrdn found\n" ); 1819 1.1 christos 1820 1.1 christos /* retrieve the revised entry */ 1821 1.1 christos nop.o_bd = on->on_info->oi_origdb; 1822 1.1 christos rc = overlay_entry_get_ov( 1823 1.1 christos &nop, &op->o_req_ndn, NULL, NULL, 0, &etmp, on ); 1824 1.1 christos if ( etmp != NULL ) { 1825 1.1 christos new_entry = entry_dup( etmp ); 1826 1.1 christos overlay_entry_release_ov( &nop, etmp, 0, on ); 1827 1.1 christos } 1828 1.1 christos if ( rc || new_entry == NULL ) { 1829 1.1 christos Debug( LDAP_DEBUG_ANY, "homedir: " 1830 1.1 christos "homedir_mod_response: unable to get revised <%s>\n", 1831 1.1 christos op->o_req_ndn.bv_val ); 1832 1.1 christos if ( new_entry != NULL ) { 1833 1.1 christos entry_free( new_entry ); 1834 1.1 christos new_entry = NULL; 1835 1.1 christos } 1836 1.1 christos } 1837 1.1 christos 1838 1.1 christos /* analyze old and new */ 1839 1.1 christos rc = harvest_values( data, old_entry, old_home, 1024, &old_uidn, 1840 1.1 christos &old_gidn, &old_presence ); 1841 1.1 christos if ( rc == 0 && old_uidn >= data->min_uid ) old_valid = 1; 1842 1.1 christos if ( new_entry != NULL ) { 1843 1.1 christos rc = harvest_values( data, new_entry, new_home, 1024, &new_uidn, 1844 1.1 christos &new_gidn, &new_presence ); 1845 1.1 christos if ( rc == 0 && new_uidn >= data->min_uid ) new_valid = 1; 1846 1.1 christos entry_free( new_entry ); 1847 1.1 christos new_entry = NULL; 1848 1.1 christos } 1849 1.1 christos 1850 1.1 christos if ( new_valid && !old_valid ) { /* like an add */ 1851 1.1 christos if ( old_presence ) 1852 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 1853 1.1 christos "homedir_mod_response: old entry is now valid\n" ); 1854 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 1855 1.1 christos "homedir_mod_response: treating like an add\n" ); 1856 1.1 christos homedir_provision( new_home, data->skeleton_path, new_uidn, 1857 1.1 christos new_gidn, op->o_tmpmemctx ); 1858 1.1 christos 1859 1.1 christos } else if ( old_valid && !new_valid && 1860 1.1 christos !new_presence ) { /* like a del */ 1861 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 1862 1.1 christos "homedir_mod_response: treating like a del\n" ); 1863 1.1 christos homedir_deprovision( data, old_home, op->o_tmpmemctx ); 1864 1.1 christos 1865 1.1 christos } else if ( new_valid && old_valid ) { /* change */ 1866 1.1 christos int did_something = 0; 1867 1.1 christos 1868 1.1 christos if ( strcmp( old_home, new_home ) != 0 ) { 1869 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 1870 1.1 christos "homedir_mod_response: treating like a rename\n" ); 1871 1.1 christos homedir_rename( old_home, new_home ); 1872 1.1 christos did_something = 1; 1873 1.1 christos } 1874 1.1 christos if ( old_uidn != new_uidn || old_gidn != new_gidn ) { 1875 1.1 christos Debug( LDAP_DEBUG_ANY, "homedir: " 1876 1.1 christos "homedir_mod_response: rechowning\n" ); 1877 1.1 christos chown_tree( new_home, old_uidn, new_uidn, old_gidn, 1878 1.1 christos new_gidn, op->o_tmpmemctx ); 1879 1.1 christos did_something = 1; 1880 1.1 christos } 1881 1.1 christos if ( !did_something ) { 1882 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 1883 1.1 christos "homedir_mod_response: nothing to do\n" ); 1884 1.1 christos } 1885 1.1 christos } else if ( old_presence || new_presence ) { 1886 1.1 christos Debug( LDAP_DEBUG_ANY, "homedir: " 1887 1.1 christos "homedir_mod_response: <%s> values present " 1888 1.1 christos "but invalid; ignoring\n", 1889 1.1 christos op->o_req_ndn.bv_val ); 1890 1.1 christos } 1891 1.1 christos rc = SLAP_CB_CONTINUE; 1892 1.1 christos break; 1893 1.1 christos } 1894 1.1 christos 1895 1.1 christos default: 1896 1.1 christos rc = SLAP_CB_CONTINUE; 1897 1.1 christos } 1898 1.1 christos 1899 1.1 christos out: 1900 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 1901 1.1 christos "homedir_mod_response: leaving\n" ); 1902 1.1 christos return rc; 1903 1.1 christos } 1904 1.1 christos 1905 1.1 christos static int 1906 1.1 christos homedir_op_mod( Operation *op, SlapReply *rs ) 1907 1.1 christos { 1908 1.1 christos slap_overinst *on = (slap_overinst *)op->o_bd->bd_info; 1909 1.1 christos slap_callback *cb = NULL; 1910 1.1 christos homedir_cb_data *cb_data = NULL; 1911 1.1 christos Entry *e = NULL; 1912 1.1 christos Entry *se = NULL; 1913 1.1 christos Operation nop = *op; 1914 1.1 christos int rc; 1915 1.1 christos 1916 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 1917 1.1 christos "homedir_op_mod: entering\n" ); 1918 1.1 christos 1919 1.1 christos /* retrieve the entry */ 1920 1.1 christos nop.o_bd = on->on_info->oi_origdb; 1921 1.1 christos rc = overlay_entry_get_ov( &nop, &op->o_req_ndn, NULL, NULL, 0, &e, on ); 1922 1.1 christos if ( e != NULL ) { 1923 1.1 christos se = entry_dup( e ); 1924 1.1 christos overlay_entry_release_ov( &nop, e, 0, on ); 1925 1.1 christos e = se; 1926 1.1 christos } 1927 1.1 christos if ( rc || e == NULL ) { 1928 1.1 christos Debug( LDAP_DEBUG_ANY, "homedir: " 1929 1.1 christos "homedir_op_mod: unable to get <%s>\n", 1930 1.1 christos op->o_req_ndn.bv_val ); 1931 1.1 christos goto out; 1932 1.1 christos } 1933 1.1 christos 1934 1.1 christos /* Allocate the callback to hold the entry */ 1935 1.1 christos cb = op->o_tmpalloc( sizeof(slap_callback), op->o_tmpmemctx ); 1936 1.1 christos cb_data = op->o_tmpalloc( sizeof(homedir_cb_data), op->o_tmpmemctx ); 1937 1.1 christos cb->sc_cleanup = homedir_mod_cleanup; 1938 1.1 christos cb->sc_response = homedir_mod_response; 1939 1.1 christos cb->sc_private = cb_data; 1940 1.1 christos cb_data->entry = e; 1941 1.1 christos e = NULL; 1942 1.1 christos cb_data->on = on; 1943 1.1 christos cb->sc_next = op->o_callback; 1944 1.1 christos op->o_callback = cb; 1945 1.1 christos 1946 1.1 christos out: 1947 1.1 christos if ( e != NULL ) entry_free( e ); 1948 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 1949 1.1 christos "homedir_op_mod: leaving\n" ); 1950 1.1 christos return SLAP_CB_CONTINUE; 1951 1.1 christos } 1952 1.1 christos 1953 1.1 christos static int 1954 1.1 christos homedir_response( Operation *op, SlapReply *rs ) 1955 1.1 christos { 1956 1.1 christos slap_overinst *on = (slap_overinst *)op->o_bd->bd_info; 1957 1.1 christos homedir_data *data = on->on_bi.bi_private; 1958 1.1 christos 1959 1.1 christos Debug( LDAP_DEBUG_TRACE, "homedir: " 1960 1.1 christos "homedir_response: entering\n" ); 1961 1.1 christos if ( rs->sr_err != LDAP_SUCCESS || data == NULL ) return SLAP_CB_CONTINUE; 1962 1.1 christos 1963 1.1 christos switch ( op->o_tag ) { 1964 1.1 christos case LDAP_REQ_ADD: { /* Check for new homedir */ 1965 1.1 christos char home_buf[1024]; 1966 1.1 christos uid_t uidn = 0; 1967 1.1 christos gid_t gidn = 0; 1968 1.1 christos int rc, presence; 1969 1.1 christos 1970 1.1 christos rc = harvest_values( data, op->ora_e, home_buf, sizeof(home_buf), 1971 1.1 christos &uidn, &gidn, &presence ); 1972 1.1 christos if ( rc == 0 && uidn >= data->min_uid ) { 1973 1.1 christos homedir_provision( home_buf, data->skeleton_path, uidn, gidn, 1974 1.1 christos op->o_tmpmemctx ); 1975 1.1 christos } 1976 1.1 christos return SLAP_CB_CONTINUE; 1977 1.1 christos } 1978 1.1 christos 1979 1.1 christos default: 1980 1.1 christos return SLAP_CB_CONTINUE; 1981 1.1 christos } 1982 1.1 christos 1983 1.1 christos return SLAP_CB_CONTINUE; 1984 1.1 christos } 1985 1.1 christos 1986 1.1 christos static int 1987 1.1 christos homedir_db_init( BackendDB *be, ConfigReply *cr ) 1988 1.1 christos { 1989 1.1 christos slap_overinst *on = (slap_overinst *)be->bd_info; 1990 1.1 christos homedir_data *data = ch_calloc( 1, sizeof(homedir_data) ); 1991 1.1 christos const char *text; 1992 1.1 christos 1993 1.1 christos if ( slap_str2ad( "homeDirectory", &data->home_ad, &text ) || 1994 1.1 christos slap_str2ad( "uidNumber", &data->uidn_ad, &text ) || 1995 1.1 christos slap_str2ad( "gidNumber", &data->gidn_ad, &text ) ) { 1996 1.1 christos Debug( LDAP_DEBUG_ANY, "homedir: " 1997 1.1 christos "nis schema not available\n" ); 1998 1.1 christos return 1; 1999 1.1 christos } 2000 1.1 christos 2001 1.1 christos data->skeleton_path = strdup( DEFAULT_SKEL ); 2002 1.1 christos data->min_uid = DEFAULT_MIN_UID; 2003 1.1 christos data->archive_path = NULL; 2004 1.1 christos 2005 1.1 christos on->on_bi.bi_private = data; 2006 1.1 christos return 0; 2007 1.1 christos } 2008 1.1 christos 2009 1.1 christos static int 2010 1.1 christos homedir_db_destroy( BackendDB *be, ConfigReply *cr ) 2011 1.1 christos { 2012 1.1 christos slap_overinst *on = (slap_overinst *)be->bd_info; 2013 1.1 christos homedir_data *data = on->on_bi.bi_private; 2014 1.1 christos homedir_regexp *r, *rnext; 2015 1.1 christos 2016 1.1 christos if ( data != NULL ) { 2017 1.1 christos for ( r = data->regexps; r != NULL; r = rnext ) { 2018 1.1 christos rnext = r->next; 2019 1.1 christos ch_free( r->match ); 2020 1.1 christos ch_free( r->replace ); 2021 1.1 christos regfree( &r->compiled ); 2022 1.1 christos ch_free( r ); 2023 1.1 christos } 2024 1.1 christos data->regexps = NULL; 2025 1.1 christos if ( data->skeleton_path != NULL ) ch_free( data->skeleton_path ); 2026 1.1 christos if ( data->archive_path != NULL ) ch_free( data->archive_path ); 2027 1.1 christos ch_free( data ); 2028 1.1 christos } 2029 1.1 christos 2030 1.1 christos return 0; 2031 1.1 christos } 2032 1.1 christos 2033 1.1 christos int 2034 1.1 christos homedir_initialize() 2035 1.1 christos { 2036 1.1 christos int rc; 2037 1.1 christos 2038 1.1 christos assert( ' ' == 32 ); /* Lots of ASCII requirements for now */ 2039 1.1 christos 2040 1.1 christos memset( &homedir, 0, sizeof(homedir) ); 2041 1.1 christos 2042 1.1 christos homedir.on_bi.bi_type = "homedir"; 2043 1.1 christos homedir.on_bi.bi_db_init = homedir_db_init; 2044 1.1 christos homedir.on_bi.bi_db_destroy = homedir_db_destroy; 2045 1.1 christos homedir.on_bi.bi_op_delete = homedir_op_mod; 2046 1.1 christos homedir.on_bi.bi_op_modify = homedir_op_mod; 2047 1.1 christos homedir.on_response = homedir_response; 2048 1.1 christos 2049 1.1 christos homedir.on_bi.bi_cf_ocs = homedirocs; 2050 1.1 christos rc = config_register_schema( homedircfg, homedirocs ); 2051 1.1 christos if ( rc ) return rc; 2052 1.1 christos 2053 1.1 christos ldap_pvt_thread_mutex_init( &readdir_mutex ); 2054 1.1 christos 2055 1.1 christos return overlay_register( &homedir ); 2056 1.1 christos } 2057 1.1 christos 2058 1.1 christos int 2059 1.1 christos homedir_terminate() 2060 1.1 christos { 2061 1.1 christos ldap_pvt_thread_mutex_destroy( &readdir_mutex ); 2062 1.1 christos return 0; 2063 1.1 christos } 2064 1.1 christos 2065 1.1 christos #if SLAPD_OVER_HOMEDIR == SLAPD_MOD_DYNAMIC && defined(PIC) 2066 1.1 christos int 2067 1.1 christos init_module( int argc, char *argv[] ) 2068 1.1 christos { 2069 1.1 christos return homedir_initialize(); 2070 1.1 christos } 2071 1.1 christos 2072 1.1 christos int 2073 1.1 christos term_module() 2074 1.1 christos { 2075 1.1 christos return homedir_terminate(); 2076 1.1 christos } 2077 1.1 christos #endif 2078 1.1 christos 2079 1.1 christos #endif /* SLAPD_OVER_HOMEDIR */ 2080