1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
|
/*
* Copyright (c) 1998-2002, 2004 Proofpoint, Inc. and its suppliers.
* All rights reserved.
* Copyright (c) 1992 Eric P. Allman. All rights reserved.
* Copyright (c) 1992, 1993
* The Regents of the University of California. All rights reserved.
*
* By using this file, you agree to the terms and conditions set
* forth in the LICENSE file which can be found at the top level of
* the sendmail distribution.
*
*/
#include <sm/gen.h>
#ifndef lint
SM_UNUSED(static char copyright[]) =
"@(#) Copyright (c) 1998-2001 Proofpoint, Inc. and its suppliers.\n\
All rights reserved.\n\
Copyright (c) 1992 Eric P. Allman. All rights reserved.\n\
Copyright (c) 1992, 1993\n\
The Regents of the University of California. All rights reserved.\n";
#endif /* ! lint */
#ifndef lint
SM_UNUSED(static char id[]) = "@(#)$Id: editmap.c,v 1.26 2013-11-22 20:51:26 ca Exp $";
#endif
#include <sys/types.h>
#ifndef ISC_UNIX
# include <sys/file.h>
#endif
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#ifdef EX_OK
# undef EX_OK /* unistd.h may have another use for this */
#endif
#include <sysexits.h>
#include <assert.h>
#include <sm/sendmail.h>
#include <sendmail/sendmail.h>
#include <sendmail/pathnames.h>
#include <libsmdb/smdb.h>
uid_t RealUid;
gid_t RealGid;
char *RealUserName;
uid_t RunAsUid;
gid_t RunAsGid;
char *RunAsUserName;
int Verbose = 2;
bool DontInitGroups = false;
uid_t TrustedUid = 0;
BITMAP256 DontBlameSendmail;
#define BUFSIZE 1024
#define ISSEP(c) (isascii(c) && isspace(c))
static void usage __P((char *));
static void
usage(progname)
char *progname;
{
fprintf(stderr,
"Usage: %s [-C cffile] [-N] [-f] [-q|-u|-x] maptype mapname key [ \"value ...\" ]\n",
progname);
exit(EX_USAGE);
}
int
main(argc, argv)
int argc;
char **argv;
{
char *progname;
char *cfile;
bool query = false;
bool update = false;
bool remove = false;
bool inclnull = false;
bool foldcase = true;
unsigned int nops = 0;
int exitstat;
int opt;
char *typename = NULL;
char *mapname = NULL;
char *keyname = NULL;
char *value = NULL;
int mode;
int smode;
int putflags = 0;
long sff = SFF_ROOTOK|SFF_REGONLY;
struct passwd *pw;
SMDB_DATABASE *database;
SMDB_DBENT db_key, db_val;
SMDB_DBPARAMS params;
SMDB_USER_INFO user_info;
#if HASFCHOWN
FILE *cfp;
char buf[MAXLINE];
#endif
static char rnamebuf[MAXNAME]; /* holds RealUserName */
extern char *optarg;
extern int optind;
memset(¶ms, '\0', sizeof params);
params.smdbp_cache_size = 1024 * 1024;
progname = strrchr(argv[0], '/');
if (progname != NULL)
progname++;
else
progname = argv[0];
cfile = _PATH_SENDMAILCF;
clrbitmap(DontBlameSendmail);
RunAsUid = RealUid = getuid();
RunAsGid = RealGid = getgid();
pw = getpwuid(RealUid);
if (pw != NULL)
(void) sm_strlcpy(rnamebuf, pw->pw_name, sizeof rnamebuf);
else
(void) sm_snprintf(rnamebuf, sizeof rnamebuf,
"Unknown UID %d", (int) RealUid);
RunAsUserName = RealUserName = rnamebuf;
user_info.smdbu_id = RunAsUid;
user_info.smdbu_group_id = RunAsGid;
(void) sm_strlcpy(user_info.smdbu_name, RunAsUserName,
SMDB_MAX_USER_NAME_LEN);
#define OPTIONS "C:fquxN"
while ((opt = getopt(argc, argv, OPTIONS)) != -1)
{
switch (opt)
{
case 'C':
cfile = optarg;
break;
case 'f':
foldcase = false;
break;
case 'q':
query = true;
nops++;
break;
case 'u':
update = true;
nops++;
break;
case 'x':
remove = true;
nops++;
break;
case 'N':
inclnull = true;
break;
default:
usage(progname);
assert(0); /* NOTREACHED */
}
}
if (!bitnset(DBS_WRITEMAPTOSYMLINK, DontBlameSendmail))
sff |= SFF_NOSLINK;
if (!bitnset(DBS_WRITEMAPTOHARDLINK, DontBlameSendmail))
sff |= SFF_NOHLINK;
if (!bitnset(DBS_LINKEDMAPINWRITABLEDIR, DontBlameSendmail))
sff |= SFF_NOWLINK;
argc -= optind;
argv += optind;
if ((nops != 1) ||
(query && argc != 3) ||
(remove && argc != 3) ||
(update && argc <= 3))
{
usage(progname);
assert(0); /* NOTREACHED */
}
typename = argv[0];
mapname = argv[1];
keyname = argv[2];
if (update)
value = argv[3];
if (foldcase)
{
char *lower;
lower = makelower(keyname);
/* if it is different then it is a static variable */
if (keyname != lower)
keyname = lower;
}
#if HASFCHOWN
/* Find TrustedUser value in sendmail.cf */
if ((cfp = fopen(cfile, "r")) == NULL)
{
fprintf(stderr, "%s: %s: %s\n", progname,
cfile, sm_errstring(errno));
exit(EX_NOINPUT);
}
while (fgets(buf, sizeof(buf), cfp) != NULL)
{
register char *b;
if ((b = strchr(buf, '\n')) != NULL)
*b = '\0';
b = buf;
switch (*b++)
{
case 'O': /* option */
if (strncasecmp(b, " TrustedUser", 12) == 0 &&
!(isascii(b[12]) && isalnum(b[12])))
{
b = strchr(b, '=');
if (b == NULL)
continue;
while (isascii(*++b) && isspace(*b))
continue;
if (isascii(*b) && isdigit(*b))
TrustedUid = atoi(b);
else
{
TrustedUid = 0;
pw = getpwnam(b);
if (pw == NULL)
fprintf(stderr,
"TrustedUser: unknown user %s\n", b);
else
TrustedUid = pw->pw_uid;
}
# ifdef UID_MAX
if (TrustedUid > UID_MAX)
{
fprintf(stderr,
"TrustedUser: uid value (%ld) > UID_MAX (%ld)",
(long) TrustedUid,
(long) UID_MAX);
TrustedUid = 0;
}
# endif /* UID_MAX */
break;
}
default:
continue;
}
}
(void) fclose(cfp);
#endif /* HASFCHOWN */
if (query)
{
mode = O_RDONLY;
smode = S_IRUSR;
}
else
{
mode = O_RDWR | O_CREAT;
sff |= SFF_CREAT|SFF_NOTEXCL;
smode = S_IWUSR;
}
params.smdbp_num_elements = 4096;
errno = smdb_open_database(&database, mapname, mode, smode, sff,
typename, &user_info, ¶ms);
if (errno != SMDBE_OK)
{
char *hint;
if (errno == SMDBE_UNSUPPORTED_DB_TYPE &&
(hint = smdb_db_definition(typename)) != NULL)
fprintf(stderr,
"%s: Need to recompile with -D%s for %s support\n",
progname, hint, typename);
else
fprintf(stderr,
"%s: error opening type %s map %s: %s\n",
progname, typename, mapname,
sm_errstring(errno));
exit(EX_CANTCREAT);
}
(void) database->smdb_sync(database, 0);
if (geteuid() == 0 && TrustedUid != 0)
{
errno = database->smdb_set_owner(database, TrustedUid, -1);
if (errno != SMDBE_OK)
{
fprintf(stderr,
"WARNING: ownership change on %s failed %s",
mapname, sm_errstring(errno));
}
}
exitstat = EX_OK;
if (query)
{
memset(&db_key, '\0', sizeof db_key);
memset(&db_val, '\0', sizeof db_val);
db_key.data = keyname;
db_key.size = strlen(keyname);
if (inclnull)
db_key.size++;
errno = database->smdb_get(database, &db_key, &db_val, 0);
if (errno != SMDBE_OK)
{
/* XXX - Need to distinguish between not found */
fprintf(stderr,
"%s: couldn't find key %s in map %s\n",
progname, keyname, mapname);
exitstat = EX_UNAVAILABLE;
}
else
{
printf("%.*s\n", (int) db_val.size,
(char *) db_val.data);
}
}
else if (update)
{
memset(&db_key, '\0', sizeof db_key);
memset(&db_val, '\0', sizeof db_val);
db_key.data = keyname;
db_key.size = strlen(keyname);
if (inclnull)
db_key.size++;
db_val.data = value;
db_val.size = strlen(value);
if (inclnull)
db_val.size++;
errno = database->smdb_put(database, &db_key, &db_val,
putflags);
if (errno != SMDBE_OK)
{
fprintf(stderr,
"%s: error updating (%s, %s) in map %s: %s\n",
progname, keyname, value, mapname,
sm_errstring(errno));
exitstat = EX_IOERR;
}
}
else if (remove)
{
memset(&db_key, '\0', sizeof db_key);
memset(&db_val, '\0', sizeof db_val);
db_key.data = keyname;
db_key.size = strlen(keyname);
if (inclnull)
db_key.size++;
errno = database->smdb_del(database, &db_key, 0);
switch (errno)
{
case SMDBE_NOT_FOUND:
fprintf(stderr,
"%s: key %s doesn't exist in map %s\n",
progname, keyname, mapname);
/* Don't set exitstat */
break;
case SMDBE_OK:
/* All's well */
break;
default:
fprintf(stderr,
"%s: couldn't remove key %s in map %s (error)\n",
progname, keyname, mapname);
exitstat = EX_IOERR;
break;
}
}
else
{
assert(0); /* NOT REACHED */
}
/*
** Now close the database.
*/
errno = database->smdb_close(database);
if (errno != SMDBE_OK)
{
fprintf(stderr, "%s: close(%s): %s\n",
progname, mapname, sm_errstring(errno));
exitstat = EX_IOERR;
}
smdb_free_database(database);
exit(exitstat);
/* NOTREACHED */
return exitstat;
}
|