Remove pinentry.conf support.
[libpwmd.git] / src / misc.c
blobf8d08a0a62673e22d18704540a44a181592c8cb0
1 /*
2 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015
3 Ben Kibbey <bjk@luxsci.net>
5 This file is part of libpwmd.
7 Libpwmd is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 2 of the License, or
10 (at your option) any later version.
12 Libpwmd is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Libpwmd. If not, see <http://www.gnu.org/licenses/>.
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
24 #ifdef HAVE_LIMITS_H
25 #include <limits.h>
26 #endif
28 #ifdef HAVE_STDLIB_H
29 #include <stdlib.h>
30 #endif
32 #include <stdio.h>
33 #include <unistd.h>
34 #include <string.h>
35 #include <errno.h>
36 #include <ctype.h>
38 #ifndef LINE_MAX
39 #define LINE_MAX 2048
40 #endif
42 #include <libpwmd.h>
43 #include "misc.h"
45 #ifdef HAVE_GETPWUID_R
46 char *
47 _expand_homedir (char *str, struct passwd *pw)
49 char *p = str;
50 char *pwbuf = NULL;
51 char *result;
52 struct passwd t;
54 if (!p)
55 return NULL;
57 if (*p != '~' || *(p + 1) != '/')
58 return pwmd_strdup (p);
60 if (!pw)
62 pwbuf = _getpwuid (&t);
63 if (!pwbuf)
64 return NULL;
66 pw = &t;
69 p += 2;
70 result = pwmd_strdup_printf ("%s/%s", pw->pw_dir, p);
71 pwmd_free (pwbuf);
72 return result;
75 char *
76 _getpwuid (struct passwd *pwd)
78 size_t size = sysconf (_SC_GETPW_R_SIZE_MAX);
79 struct passwd *result;
80 char *buf;
81 int n;
83 if (size == -1)
84 size = 16384;
86 buf = pwmd_malloc (size);
87 if (!buf)
88 return NULL;
90 n = getpwuid_r (getuid (), pwd, buf, size, &result);
91 if (n)
93 pwmd_free (buf);
94 errno = n;
95 return NULL;
98 if (!result)
100 pwmd_free (buf);
101 return NULL;
104 errno = n;
105 return buf;
107 #else
108 char *
109 _expand_homedir (char *str, struct passwd *pw)
111 char *p = str;
113 if (!p)
114 return NULL;
116 if (*p != '~' || *(p + 1) != '/')
117 return pwmd_strdup (p);
119 if (!pw)
121 pw = getpwuid (getuid ());
122 if (!pw)
123 return NULL;
126 p += 2;
127 return pwmd_strdup_printf ("%s/%s", pw->pw_dir, p);
130 char *
131 _getpwuid (struct passwd *pwd)
133 struct passwd *pw = getpwuid (getuid ());
135 if (!pw)
136 return NULL;
138 *pwd = *pw;
139 return pwmd_strdup ("");
141 #endif
144 * Borrowed from libassuan.
146 char *
147 _percent_escape (const char *atext)
149 const unsigned char *s;
150 int len;
151 char *buf, *p;
153 if (!atext)
154 return NULL;
156 len = strlen (atext) * 3 + 1;
157 buf = (char *) pwmd_malloc (len);
159 if (!buf)
160 return NULL;
162 p = buf;
164 for (s = (const unsigned char *) atext; *s; s++)
166 if (*s < ' ')
168 sprintf (p, "%%%02X", *s);
169 p += 3;
171 else
172 *p++ = *s;
175 *p = 0;
176 return buf;
179 /* Common hostname parsing for urls. Handles both IPv4 and IPv6 hostname and
180 * port specification.
182 gpg_error_t
183 parse_hostname_common (const char *str, char **host, int *port)
185 const char *p = str;
186 char *t = NULL;
188 /* IPv6 with optional port. */
189 if (*p == '[')
191 p++;
192 t = strchr (p, ']');
194 else
196 int n = 0;
197 const char *x;
199 /* Handle IPv6 without proper braces around the IP. */
200 for (x = p; *x; x++)
202 if (*x == ':')
203 n++;
206 if (n <= 1)
207 t = strchr (p, ':');
210 if (t)
212 size_t len = strlen (p) - strlen (t) + 1;
214 *host = pwmd_malloc (len);
215 if (!*host)
216 return gpg_error_from_errno (ENOMEM);
218 snprintf (*host, len, "%s", p);
219 t++;
221 if (*t == ':')
222 t++;
224 if (*t)
225 *port = strtol (t, NULL, 10);
227 if (*t == '-')
228 t++;
230 while (*t && isdigit (*t))
231 t++;
233 p = t;
235 else
237 *host = pwmd_strdup (str);
238 if (!*host)
239 return gpg_error_from_errno (ENOMEM);
242 return 0;
245 char *
246 bin2hex (const unsigned char *data, size_t len)
248 size_t size = len * 2 + 1;
249 char buf[size]; // C99
250 size_t n;
252 buf[0] = 0;
254 for (n = 0; n < len; n++)
256 char c[3];
258 sprintf (c, "%02X", data[n]);
259 strcat (buf, c);
262 return pwmd_strdup (buf);