table_getpwnam.c revision 1.15
1/* $OpenBSD: table_getpwnam.c,v 1.15 2024/05/14 13:28:08 op Exp $ */ 2 3/* 4 * Copyright (c) 2012 Gilles Chehade <gilles@poolp.org> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19#include <errno.h> 20#include <pwd.h> 21 22#include "smtpd.h" 23 24/* getpwnam(3) backend */ 25static int table_getpwnam_config(struct table *); 26static int table_getpwnam_update(struct table *); 27static int table_getpwnam_open(struct table *); 28static int table_getpwnam_lookup(struct table *, enum table_service, const char *, 29 char **); 30static void table_getpwnam_close(struct table *); 31 32struct table_backend table_backend_getpwnam = { 33 .name = "getpwnam", 34 .services = K_USERINFO, 35 .config = table_getpwnam_config, 36 .add = NULL, 37 .dump = NULL, 38 .open = table_getpwnam_open, 39 .update = table_getpwnam_update, 40 .close = table_getpwnam_close, 41 .lookup = table_getpwnam_lookup, 42 .fetch = NULL, 43}; 44 45 46static int 47table_getpwnam_config(struct table *table) 48{ 49 if (table->t_config[0]) 50 return 0; 51 return 1; 52} 53 54static int 55table_getpwnam_update(struct table *table) 56{ 57 return 1; 58} 59 60static int 61table_getpwnam_open(struct table *table) 62{ 63 return 1; 64} 65 66static void 67table_getpwnam_close(struct table *table) 68{ 69 return; 70} 71 72static int 73table_getpwnam_lookup(struct table *table, enum table_service kind, const char *key, 74 char **dst) 75{ 76 struct passwd *pw; 77 78 if (kind != K_USERINFO) 79 return -1; 80 81 errno = 0; 82 do { 83 pw = getpwnam(key); 84 } while (pw == NULL && errno == EINTR); 85 86 if (pw == NULL) { 87 if (errno) 88 return -1; 89 return 0; 90 } 91 if (dst == NULL) 92 return 1; 93 94 if (asprintf(dst, "%d:%d:%s", 95 pw->pw_uid, 96 pw->pw_gid, 97 pw->pw_dir) == -1) { 98 *dst = NULL; 99 return -1; 100 } 101 102 return (1); 103} 104