2 Copyright (C) 2006-2016 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
46 #ifdef HAVE_GETPWUID_R
48 _expand_homedir (char *str
, struct passwd
*pw
)
58 if (*p
!= '~' || *(p
+ 1) != '/')
59 return pwmd_strdup (p
);
63 pwbuf
= _getpwuid (&t
);
71 result
= pwmd_strdup_printf ("%s/%s", pw
->pw_dir
, p
);
77 _getpwuid (struct passwd
*pwd
)
79 size_t size
= sysconf (_SC_GETPW_R_SIZE_MAX
);
80 struct passwd
*result
;
87 buf
= pwmd_malloc (size
);
91 n
= getpwuid_r (getuid (), pwd
, buf
, size
, &result
);
110 _expand_homedir (char *str
, struct passwd
*pw
)
117 if (*p
!= '~' || *(p
+ 1) != '/')
118 return pwmd_strdup (p
);
122 pw
= getpwuid (getuid ());
128 return pwmd_strdup_printf ("%s/%s", pw
->pw_dir
, p
);
132 _getpwuid (struct passwd
*pwd
)
134 struct passwd
*pw
= getpwuid (getuid ());
140 return pwmd_strdup ("");
145 * Borrowed from libassuan.
148 _percent_escape (const char *atext
)
150 const unsigned char *s
;
157 len
= strlen (atext
) * 3 + 1;
158 buf
= (char *) pwmd_malloc (len
);
165 for (s
= (const unsigned char *) atext
; *s
; s
++)
169 sprintf (p
, "%%%02X", *s
);
180 /* Common hostname parsing for urls. Handles both IPv4 and IPv6 hostname and
181 * port specification.
184 parse_hostname_common (const char *str
, char **host
, int *port
)
189 /* IPv6 with optional port. */
200 /* Handle IPv6 without proper braces around the IP. */
213 size_t len
= strlen (p
) - strlen (t
) + 1;
215 *host
= pwmd_malloc (len
);
217 return gpg_error_from_errno (ENOMEM
);
219 snprintf (*host
, len
, "%s", p
);
226 *port
= strtol (t
, NULL
, 10);
231 while (*t
&& isdigit (*t
))
238 *host
= pwmd_strdup (str
);
240 return gpg_error_from_errno (ENOMEM
);
247 bin2hex (const unsigned char *data
, size_t len
)
249 size_t size
= len
* 2 + 1;
250 char buf
[size
]; // C99
255 for (n
= 0; n
< len
; n
++)
259 sprintf (c
, "%02X", data
[n
]);
263 return pwmd_strdup (buf
);