xref: /openbsd/usr.sbin/smtpd/table_getpwnam.c (revision a6445c1d)
1 /*	$OpenBSD: table_getpwnam.c,v 1.3 2014/07/08 13:49:09 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 <string.h>
34 
35 #include "smtpd.h"
36 #include "log.h"
37 
38 
39 /* getpwnam(3) backend */
40 static int table_getpwnam_config(struct table *);
41 static int table_getpwnam_update(struct table *);
42 static void *table_getpwnam_open(struct table *);
43 static int table_getpwnam_lookup(void *, struct dict *, const char *, enum table_service,
44     union lookup *);
45 static void  table_getpwnam_close(void *);
46 
47 struct table_backend table_backend_getpwnam = {
48 	K_USERINFO,
49 	table_getpwnam_config,
50 	table_getpwnam_open,
51 	table_getpwnam_update,
52 	table_getpwnam_close,
53 	table_getpwnam_lookup,
54 };
55 
56 
57 static int
58 table_getpwnam_config(struct table *table)
59 {
60 	if (table->t_config[0])
61 		return 0;
62 	return 1;
63 }
64 
65 static int
66 table_getpwnam_update(struct table *table)
67 {
68 	return 1;
69 }
70 
71 static void *
72 table_getpwnam_open(struct table *table)
73 {
74 	return table;
75 }
76 
77 static void
78 table_getpwnam_close(void *hdl)
79 {
80 	return;
81 }
82 
83 static int
84 table_getpwnam_lookup(void *hdl, struct dict *params, const char *key, enum table_service kind,
85     union lookup *lk)
86 {
87 	struct passwd	       *pw;
88 	size_t			s;
89 
90 	if (kind != K_USERINFO)
91 		return -1;
92 
93 	errno = 0;
94 	do {
95 		pw = getpwnam(key);
96 	} while (pw == NULL && errno == EINTR);
97 
98 	if (pw == NULL) {
99 		if (errno)
100 			return -1;
101 		return 0;
102 	}
103 	if (lk == NULL)
104 		return 1;
105 
106 	lk->userinfo.uid = pw->pw_uid;
107 	lk->userinfo.gid = pw->pw_gid;
108 	s = strlcpy(lk->userinfo.username, pw->pw_name,
109 	    sizeof(lk->userinfo.username));
110 	if (s >= sizeof(lk->userinfo.username))
111 		return (-1);
112 	s = strlcpy(lk->userinfo.directory, pw->pw_dir,
113 	    sizeof(lk->userinfo.directory));
114 	if (s >= sizeof(lk->userinfo.directory))
115 		return (-1);
116 
117 	return (1);
118 }
119