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