comparison cli-kex.c @ 318:9916350d7d8b

don't fail fatally if the client can't get homedir from getpwuid(), fallback to $HOME.
author Matt Johnston <matt@ucc.asn.au>
date Wed, 12 Apr 2006 05:51:32 +0000
parents a62cb364f615
children 84aa4e60bd3c
comparison
equal deleted inserted replaced
315:3eea61bd9993 318:9916350d7d8b
143 143
144 char * filename = NULL; 144 char * filename = NULL;
145 FILE *hostsfile = NULL; 145 FILE *hostsfile = NULL;
146 int readonly = 0; 146 int readonly = 0;
147 struct passwd *pw = NULL; 147 struct passwd *pw = NULL;
148 char * homedir = NULL;
148 unsigned int hostlen, algolen; 149 unsigned int hostlen, algolen;
149 unsigned long len; 150 unsigned long len;
150 const char *algoname = NULL; 151 const char *algoname = NULL;
151 buffer * line = NULL; 152 buffer * line = NULL;
152 int ret; 153 int ret;
153 154
154 pw = getpwuid(getuid()); 155 pw = getpwuid(getuid());
155 156
156 if (pw == NULL) { 157 if (pw)
157 dropbear_exit("Failed to get homedir"); 158 homedir = pw->pw_dir;
158 } 159 }
159 160 pw = NULL;
160 len = strlen(pw->pw_dir); 161
161 filename = m_malloc(len + 18); /* "/.ssh/known_hosts" and null-terminator*/ 162 if (!homedir)
162 163 homedir = getenv("HOME");
163 snprintf(filename, len+18, "%s/.ssh", pw->pw_dir); 164 }
164 /* Check that ~/.ssh exists - easiest way is just to mkdir */ 165
165 if (mkdir(filename, S_IRWXU) != 0) { 166 if (homedir) {
166 if (errno != EEXIST) { 167
167 dropbear_log(LOG_INFO, "Warning: failed creating ~/.ssh: %s", 168 len = strlen(homedir);
168 strerror(errno)); 169 filename = m_malloc(len + 18); /* "/.ssh/known_hosts" and null-terminator*/
169 TRACE(("mkdir didn't work: %s", strerror(errno))) 170
170 ask_to_confirm(keyblob, keybloblen); 171 snprintf(filename, len+18, "%s/.ssh", homedir);
171 goto out; /* only get here on success */ 172 /* Check that ~/.ssh exists - easiest way is just to mkdir */
172 } 173 if (mkdir(filename, S_IRWXU) != 0) {
173 } 174 if (errno != EEXIST) {
174 175 dropbear_log(LOG_INFO, "Warning: failed creating ~/.ssh: %s",
175 snprintf(filename, len+18, "%s/.ssh/known_hosts", pw->pw_dir); 176 strerror(errno));
176 hostsfile = fopen(filename, "a+"); 177 TRACE(("mkdir didn't work: %s", strerror(errno)))
177 178 ask_to_confirm(keyblob, keybloblen);
178 if (hostsfile != NULL) { 179 goto out; /* only get here on success */
179 fseek(hostsfile, 0, SEEK_SET); 180 }
180 } else { 181 }
181 /* We mightn't have been able to open it if it was read-only */ 182
182 if (errno == EACCES || errno == EROFS) { 183 snprintf(filename, len+18, "%s/.ssh/known_hosts", homedir);
183 TRACE(("trying readonly: %s", strerror(errno))) 184 hostsfile = fopen(filename, "a+");
184 readonly = 1; 185
185 hostsfile = fopen(filename, "r"); 186 if (hostsfile != NULL) {
187 fseek(hostsfile, 0, SEEK_SET);
188 } else {
189 /* We mightn't have been able to open it if it was read-only */
190 if (errno == EACCES || errno == EROFS) {
191 TRACE(("trying readonly: %s", strerror(errno)))
192 readonly = 1;
193 hostsfile = fopen(filename, "r");
194 }
186 } 195 }
187 } 196 }
188 197
189 if (hostsfile == NULL) { 198 if (hostsfile == NULL) {
190 TRACE(("hostsfile didn't open: %s", strerror(errno))) 199 TRACE(("hostsfile didn't open: %s", strerror(errno)))
200 dropbear_log(LOG_WARNING, "Failed to open ~/.ssh/known_hosts");
191 ask_to_confirm(keyblob, keybloblen); 201 ask_to_confirm(keyblob, keybloblen);
192 goto out; /* We only get here on success */ 202 goto out; /* We only get here on success */
193 } 203 }
194 204
195 line = buf_new(MAX_KNOWNHOSTS_LINE); 205 line = buf_new(MAX_KNOWNHOSTS_LINE);