table_getpwnam.c revision 1.9
1/* $OpenBSD: table_getpwnam.c,v 1.9 2018/12/27 08:08:06 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(void *, enum table_service, const char *, 45 char **); 46static void table_getpwnam_close(void *); 47 48struct table_backend table_backend_getpwnam = { 49 "getpwnam", 50 K_USERINFO, 51 table_getpwnam_config, 52 table_getpwnam_open, 53 table_getpwnam_update, 54 table_getpwnam_close, 55 table_getpwnam_lookup, 56}; 57 58 59static int 60table_getpwnam_config(struct table *table) 61{ 62 if (table->t_config[0]) 63 return 0; 64 return 1; 65} 66 67static int 68table_getpwnam_update(struct table *table) 69{ 70 return 1; 71} 72 73static int 74table_getpwnam_open(struct table *table) 75{ 76 return 1; 77} 78 79static void 80table_getpwnam_close(void *hdl) 81{ 82 return; 83} 84 85static int 86table_getpwnam_lookup(void *hdl, enum table_service kind, const char *key, 87 char **dst) 88{ 89 struct passwd *pw; 90 91 if (kind != K_USERINFO) 92 return -1; 93 94 errno = 0; 95 do { 96 pw = getpwnam(key); 97 } while (pw == NULL && errno == EINTR); 98 99 if (pw == NULL) { 100 if (errno) 101 return -1; 102 return 0; 103 } 104 if (dst == NULL) 105 return 1; 106 107 if (asprintf(dst, "%d:%d:%s", 108 pw->pw_uid, 109 pw->pw_gid, 110 pw->pw_dir) == -1) { 111 *dst = NULL; 112 return -1; 113 } 114 115 return (1); 116} 117