xref: /openbsd/usr.sbin/smtpd/table_getpwnam.c (revision 4bdff4be)
1 /*	$OpenBSD: table_getpwnam.c,v 1.14 2021/06/14 17:58:16 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 <errno.h>
20 #include <pwd.h>
21 
22 #include "smtpd.h"
23 
24 /* getpwnam(3) backend */
25 static int table_getpwnam_config(struct table *);
26 static int table_getpwnam_update(struct table *);
27 static int table_getpwnam_open(struct table *);
28 static int table_getpwnam_lookup(struct table *, enum table_service, const char *,
29     char **);
30 static void table_getpwnam_close(struct table *);
31 
32 struct table_backend table_backend_getpwnam = {
33 	"getpwnam",
34 	K_USERINFO,
35 	table_getpwnam_config,
36 	NULL,
37 	NULL,
38 	table_getpwnam_open,
39 	table_getpwnam_update,
40 	table_getpwnam_close,
41 	table_getpwnam_lookup,
42 };
43 
44 
45 static int
46 table_getpwnam_config(struct table *table)
47 {
48 	if (table->t_config[0])
49 		return 0;
50 	return 1;
51 }
52 
53 static int
54 table_getpwnam_update(struct table *table)
55 {
56 	return 1;
57 }
58 
59 static int
60 table_getpwnam_open(struct table *table)
61 {
62 	return 1;
63 }
64 
65 static void
66 table_getpwnam_close(struct table *table)
67 {
68 	return;
69 }
70 
71 static int
72 table_getpwnam_lookup(struct table *table, enum table_service kind, const char *key,
73     char **dst)
74 {
75 	struct passwd	       *pw;
76 
77 	if (kind != K_USERINFO)
78 		return -1;
79 
80 	errno = 0;
81 	do {
82 		pw = getpwnam(key);
83 	} while (pw == NULL && errno == EINTR);
84 
85 	if (pw == NULL) {
86 		if (errno)
87 			return -1;
88 		return 0;
89 	}
90 	if (dst == NULL)
91 		return 1;
92 
93 	if (asprintf(dst, "%d:%d:%s",
94 	    pw->pw_uid,
95 	    pw->pw_gid,
96 	    pw->pw_dir) == -1) {
97 		*dst = NULL;
98 		return -1;
99 	}
100 
101 	return (1);
102 }
103