table_getpwnam.c revision 1.16
11541Srgrimes/*	$OpenBSD: table_getpwnam.c,v 1.16 2024/05/14 13:30:37 op Exp $	*/
21541Srgrimes
31541Srgrimes/*
41541Srgrimes * Copyright (c) 2012 Gilles Chehade <gilles@poolp.org>
51541Srgrimes *
61541Srgrimes * Permission to use, copy, modify, and distribute this software for any
71541Srgrimes * purpose with or without fee is hereby granted, provided that the above
81541Srgrimes * copyright notice and this permission notice appear in all copies.
91541Srgrimes *
101541Srgrimes * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
111541Srgrimes * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
121541Srgrimes * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
131541Srgrimes * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
141541Srgrimes * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
151541Srgrimes * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
161541Srgrimes * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
171541Srgrimes */
181541Srgrimes
191541Srgrimes#include <errno.h>
201541Srgrimes#include <pwd.h>
211541Srgrimes
221541Srgrimes#include "smtpd.h"
231541Srgrimes
241541Srgrimes/* getpwnam(3) backend */
251541Srgrimesstatic int table_getpwnam_config(struct table *);
261541Srgrimesstatic int table_getpwnam_lookup(struct table *, enum table_service, const char *,
271541Srgrimes    char **);
281541Srgrimes
291541Srgrimesstruct table_backend table_backend_getpwnam = {
301541Srgrimes	.name = "getpwnam",
311541Srgrimes	.services = K_USERINFO,
321541Srgrimes	.config = table_getpwnam_config,
331541Srgrimes	.add = NULL,
3450477Speter	.dump = NULL,
351541Srgrimes	.open = NULL,
361541Srgrimes	.update = NULL,
3737413Sjulian	.close = NULL,
3841793Sluigi	.lookup = table_getpwnam_lookup,
3930966Sjoerg	.fetch = NULL,
4034746Speter};
4155009Sshin
42101096Srwatson
4364060Sdarrenrstatic int
4477574Skristable_getpwnam_config(struct table *table)
45113384Ssilby{
4630966Sjoerg	if (table->t_config[0])
471541Srgrimes		return 0;
481549Srgrimes	return 1;
4941990Sluigi}
50101096Srwatson
511541Srgrimesstatic int
521541Srgrimestable_getpwnam_lookup(struct table *table, enum table_service kind, const char *key,
531541Srgrimes    char **dst)
541541Srgrimes{
551541Srgrimes	struct passwd	       *pw;
56112591Ssilby
571541Srgrimes	if (kind != K_USERINFO)
581541Srgrimes		return -1;
591541Srgrimes
601541Srgrimes	errno = 0;
611541Srgrimes	do {
621541Srgrimes		pw = getpwnam(key);
631541Srgrimes	} while (pw == NULL && errno == EINTR);
641541Srgrimes
651541Srgrimes	if (pw == NULL) {
661541Srgrimes		if (errno)
671541Srgrimes			return -1;
6815295Swollman		return 0;
691541Srgrimes	}
7030354Sphk	if (dst == NULL)
7130309Sphk		return 1;
7255009Sshin
7355009Sshin	if (asprintf(dst, "%d:%d:%s",
7455009Sshin	    pw->pw_uid,
7555009Sshin	    pw->pw_gid,
7655009Sshin	    pw->pw_dir) == -1) {
7755009Sshin		*dst = NULL;
7855009Sshin		return -1;
7955009Sshin	}
8055009Sshin
8155009Sshin	return (1);
82105199Ssam}
83105199Ssam