2 Copyright (C) 2006-2018 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 as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
48 #ifdef HAVE_GETPWUID_R
50 _expand_homedir (char *str
, struct passwd
*pw
)
60 if (*p
!= '~' || *(p
+ 1) != '/')
61 return pwmd_strdup (p
);
65 pwbuf
= _getpwuid (&t
);
73 result
= pwmd_strdup_printf ("%s/%s", pw
->pw_dir
, p
);
79 _getpwuid (struct passwd
*pwd
)
81 size_t size
= sysconf (_SC_GETPW_R_SIZE_MAX
);
82 struct passwd
*result
;
89 buf
= pwmd_malloc (size
);
93 n
= getpwuid_r (getuid (), pwd
, buf
, size
, &result
);
112 _expand_homedir (char *str
, struct passwd
*pw
)
119 if (*p
!= '~' || *(p
+ 1) != '/')
120 return pwmd_strdup (p
);
124 pw
= getpwuid (getuid ());
130 return pwmd_strdup_printf ("%s/%s", pw
->pw_dir
, p
);
134 _getpwuid (struct passwd
*pwd
)
136 struct passwd
*pw
= getpwuid (getuid ());
142 return pwmd_strdup ("");
148 * Borrowed from libassuan.
151 _percent_escape (const char *atext
)
153 const unsigned char *s
;
160 len
= strlen (atext
) * 3 + 1;
161 buf
= (char *) pwmd_malloc (len
);
168 for (s
= (const unsigned char *) atext
; *s
; s
++)
172 sprintf (p
, "%%%02X", *s
);
183 /* Common hostname parsing for urls. Handles both IPv4 and IPv6 hostname and
184 * port specification.
187 parse_hostname_common (const char *str
, char **host
, int *port
)
192 /* IPv6 with optional port. */
203 /* Handle IPv6 without proper braces around the IP. */
216 size_t len
= strlen (p
) - strlen (t
) + 1;
218 *host
= pwmd_malloc (len
);
220 return gpg_error_from_errno (ENOMEM
);
222 snprintf (*host
, len
, "%s", p
);
229 *port
= strtol (t
, NULL
, 10);
234 while (*t
&& isdigit (*t
))
239 *host
= pwmd_strdup (str
);
241 return gpg_error_from_errno (ENOMEM
);
248 bin2hex (const unsigned char *data
, size_t len
)
250 size_t size
= len
* 2 + 1;
251 char buf
[size
]; // C99
256 for (n
= 0; n
< len
; n
++)
260 sprintf (c
, "%02X", data
[n
]);
264 return pwmd_strdup (buf
);
268 set_non_blocking (assuan_fd_t fd
, int n
)
271 unsigned long opt
= !!n
;
273 if (ioctlsocket (HANDLE2SOCKET (fd
), FIONBIO
, &opt
) != NO_ERROR
)
274 return gpg_error_from_syserror ();
276 int flags
= fcntl (fd
, F_GETFL
);
279 flags
&= ~O_NONBLOCK
;
283 if (fcntl (fd
, F_SETFL
, flags
) == -1)
284 return gpg_error_from_syserror ();