1 /* $OpenBSD: table_getpwnam.c,v 1.4 2015/01/20 17:37:54 deraadt 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 */ 41 static int table_getpwnam_config(struct table *); 42 static int table_getpwnam_update(struct table *); 43 static void *table_getpwnam_open(struct table *); 44 static int table_getpwnam_lookup(void *, struct dict *, const char *, enum table_service, 45 union lookup *); 46 static void table_getpwnam_close(void *); 47 48 struct table_backend table_backend_getpwnam = { 49 K_USERINFO, 50 table_getpwnam_config, 51 table_getpwnam_open, 52 table_getpwnam_update, 53 table_getpwnam_close, 54 table_getpwnam_lookup, 55 }; 56 57 58 static int 59 table_getpwnam_config(struct table *table) 60 { 61 if (table->t_config[0]) 62 return 0; 63 return 1; 64 } 65 66 static int 67 table_getpwnam_update(struct table *table) 68 { 69 return 1; 70 } 71 72 static void * 73 table_getpwnam_open(struct table *table) 74 { 75 return table; 76 } 77 78 static void 79 table_getpwnam_close(void *hdl) 80 { 81 return; 82 } 83 84 static int 85 table_getpwnam_lookup(void *hdl, struct dict *params, const char *key, enum table_service kind, 86 union lookup *lk) 87 { 88 struct passwd *pw; 89 size_t s; 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 (lk == NULL) 105 return 1; 106 107 lk->userinfo.uid = pw->pw_uid; 108 lk->userinfo.gid = pw->pw_gid; 109 s = strlcpy(lk->userinfo.username, pw->pw_name, 110 sizeof(lk->userinfo.username)); 111 if (s >= sizeof(lk->userinfo.username)) 112 return (-1); 113 s = strlcpy(lk->userinfo.directory, pw->pw_dir, 114 sizeof(lk->userinfo.directory)); 115 if (s >= sizeof(lk->userinfo.directory)) 116 return (-1); 117 118 return (1); 119 } 120