2 Copyright (C) 2006-2023 Ben Kibbey <bjk@luxsci.net>
4 This file is part of libpwmd.
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License version 2.1 as published by the Free Software Foundation.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
49 #ifdef HAVE_GETPWUID_R
51 _expand_homedir (char *str
, struct passwd
*pw
)
61 if (*p
!= '~' || *(p
+ 1) != '/')
62 return pwmd_strdup (p
);
66 pwbuf
= _getpwuid (&t
);
74 result
= pwmd_strdup_printf ("%s/%s", pw
->pw_dir
, p
);
80 _getpwuid (struct passwd
*pwd
)
82 size_t size
= sysconf (_SC_GETPW_R_SIZE_MAX
);
83 struct passwd
*result
;
90 buf
= pwmd_malloc (size
);
94 n
= getpwuid_r (getuid (), pwd
, buf
, size
, &result
);
113 _expand_homedir (char *str
, struct passwd
*pw
)
120 if (*p
!= '~' || *(p
+ 1) != '/')
121 return pwmd_strdup (p
);
125 pw
= getpwuid (getuid ());
131 return pwmd_strdup_printf ("%s/%s", pw
->pw_dir
, p
);
135 _getpwuid (struct passwd
*pwd
)
137 struct passwd
*pw
= getpwuid (getuid ());
143 return pwmd_strdup ("");
149 * Borrowed from libassuan.
152 _percent_escape (const char *atext
)
154 const unsigned char *s
;
161 len
= strlen (atext
) * 3 + 1;
162 buf
= (char *) pwmd_malloc (len
);
169 for (s
= (const unsigned char *) atext
; *s
; s
++)
173 sprintf (p
, "%%%02X", *s
);
184 /* Common hostname parsing for urls. Handles both IPv4 and IPv6 hostname and
185 * port specification.
188 parse_hostname_common (const char *str
, char **host
, int *port
)
193 /* IPv6 with optional port. */
204 /* Handle IPv6 without proper braces around the IP. */
217 size_t len
= strlen (p
) - strlen (t
) + 1;
219 *host
= pwmd_malloc (len
);
221 return gpg_error_from_errno (ENOMEM
);
223 snprintf (*host
, len
, "%s", p
);
230 *port
= strtol (t
, NULL
, 10);
235 while (*t
&& isdigit (*t
))
240 *host
= pwmd_strdup (str
);
242 return gpg_error_from_errno (ENOMEM
);
249 bin2hex (const unsigned char *data
, size_t len
)
251 size_t size
= len
* 2 + 1;
252 char buf
[size
]; // C99
257 for (n
= 0; n
< len
; n
++)
261 sprintf (c
, "%02X", data
[n
]);
265 return pwmd_strdup (buf
);
269 set_non_blocking (assuan_fd_t fd
, int n
)
272 unsigned long opt
= !!n
;
274 if (ioctlsocket (HANDLE2SOCKET (fd
), FIONBIO
, &opt
) != NO_ERROR
)
275 return gpg_error_from_syserror ();
277 int flags
= fcntl (fd
, F_GETFL
);
280 flags
&= ~O_NONBLOCK
;
284 if (fcntl (fd
, F_SETFL
, flags
) == -1)
285 return gpg_error_from_syserror ();