table_getpwnam.c revision 1.12
1/* $OpenBSD: table_getpwnam.c,v 1.12 2018/12/27 14:23:41 eric 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 <sys/types.h> 20#include <sys/queue.h> 21#include <sys/tree.h> 22#include <sys/socket.h> 23 24#include <ctype.h> 25#include <err.h> 26#include <errno.h> 27#include <event.h> 28#include <fcntl.h> 29#include <imsg.h> 30#include <pwd.h> 31#include <stdio.h> 32#include <stdlib.h> 33#include <limits.h> 34#include <string.h> 35 36#include "smtpd.h" 37#include "log.h" 38 39 40/* getpwnam(3) backend */ 41static int table_getpwnam_config(struct table *); 42static int table_getpwnam_update(struct table *); 43static int table_getpwnam_open(struct table *); 44static int table_getpwnam_lookup(struct table *, enum table_service, const char *, 45 char **); 46static void table_getpwnam_close(struct table *); 47 48struct table_backend table_backend_getpwnam = { 49 "getpwnam", 50 K_USERINFO, 51 table_getpwnam_config, 52 NULL, 53 NULL, 54 table_getpwnam_open, 55 table_getpwnam_update, 56 table_getpwnam_close, 57 table_getpwnam_lookup, 58}; 59 60 61static int 62table_getpwnam_config(struct table *table) 63{ 64 if (table->t_config[0]) 65 return 0; 66 return 1; 67} 68 69static int 70table_getpwnam_update(struct table *table) 71{ 72 return 1; 73} 74 75static int 76table_getpwnam_open(struct table *table) 77{ 78 return 1; 79} 80 81static void 82table_getpwnam_close(struct table *table) 83{ 84 return; 85} 86 87static int 88table_getpwnam_lookup(struct table *table, enum table_service kind, const char *key, 89 char **dst) 90{ 91 struct passwd *pw; 92 93 if (kind != K_USERINFO) 94 return -1; 95 96 errno = 0; 97 do { 98 pw = getpwnam(key); 99 } while (pw == NULL && errno == EINTR); 100 101 if (pw == NULL) { 102 if (errno) 103 return -1; 104 return 0; 105 } 106 if (dst == NULL) 107 return 1; 108 109 if (asprintf(dst, "%d:%d:%s", 110 pw->pw_uid, 111 pw->pw_gid, 112 pw->pw_dir) == -1) { 113 *dst = NULL; 114 return -1; 115 } 116 117 return (1); 118} 119