comparison cli-runopts.c @ 46:3bea78e1b175

Filled out a bit, with commandline support etc
author Matt Johnston <matt@ucc.asn.au>
date Fri, 06 Aug 2004 02:51:17 +0000
parents b4874d772210
children 4b53a43f0082
comparison
equal deleted inserted replaced
45:9ee8996a375f 46:3bea78e1b175
44 ,DROPBEAR_VERSION, progname); 44 ,DROPBEAR_VERSION, progname);
45 } 45 }
46 46
47 void cli_getopts(int argc, char ** argv) { 47 void cli_getopts(int argc, char ** argv) {
48 48
49 unsigned int i; 49 unsigned int i, j;
50 char ** next = 0; 50 char ** next = 0;
51 unsigned int cmdlen;
52 int nextiskey = 0; /* A flag if the next argument is a keyfile */
51 53
52 uid_t uid; 54 uid_t uid;
53 struct passwd *pw; 55 struct passwd *pw = NULL;
54 56
55 char* userhostarg = NULL; 57 char* userhostarg = NULL;
56 58
57 /* see printhelp() for options */ 59 /* see printhelp() for options */
58 cli_opts.progname = argv[0]; 60 cli_opts.progname = argv[0];
71 if (argc != 2) { 73 if (argc != 2) {
72 printhelp(argv[0]); 74 printhelp(argv[0]);
73 exit(EXIT_FAILURE); 75 exit(EXIT_FAILURE);
74 } 76 }
75 77
76 /* We'll be editing it, should probably make a copy */ 78 for (i = 1; i < (unsigned int)argc; i++) {
77 userhostarg = m_strdup(argv[1]); 79 if (nextiskey) {
78 80 /* XXX do stuff */
79 cli_opts.remotehost = strchr(userhostarg, '@'); 81 break;
80 if (cli_opts.remotehost == NULL) {
81 /* no username portion, the cli-auth.c code can figure the local
82 * user's name */
83 cli_opts.remotehost = userhostarg;
84 } else {
85 cli_opts.remotehost[0] = '\0'; /* Split the user/host */
86 cli_opts.remotehost++;
87 cli_opts.username = userhostarg;
88 }
89
90 if (cli_opts.username == NULL) {
91 uid = getuid();
92
93 pw = getpwuid(uid);
94 if (pw == NULL || pw->pw_name == NULL) {
95 dropbear_exit("Couldn't find username for current user");
96 } 82 }
97
98 cli_opts.username = m_strdup(pw->pw_name);
99 }
100
101 if (cli_opts.remotehost[0] == '\0') {
102 dropbear_exit("Bad hostname argument");
103 }
104
105 cli_opts.remoteport = strchr(cli_opts.remotehost, ':');
106 if (cli_opts.remoteport == NULL) {
107 cli_opts.remoteport = "22";
108 } else {
109 cli_opts.remoteport[0] = '\0';
110 cli_opts.remoteport++;
111 }
112
113 #if 0
114 for (i = 1; i < (unsigned int)argc; i++) {
115 if (next) { 83 if (next) {
116 *next = argv[i]; 84 *next = argv[i];
117 if (*next == NULL) { 85 if (*next == NULL) {
118 dropbear_exit("Invalid null argument"); 86 dropbear_exit("Invalid null argument");
119 } 87 }
120 next = 0x00; 88 next = NULL;
121 continue; 89 continue;
122 } 90 }
123 91
124 if (argv[i][0] == '-') { 92 if (argv[i][0] == '-') {
93
94 /* A flag *waves* */
125 switch (argv[i][1]) { 95 switch (argv[i][1]) {
126 case 'b': 96 case 'p':
127 next = &svr_opts.bannerfile; 97 next = &cli_opts.remoteport;
128 break; 98 break;
129 #ifdef DROPBEAR_DSS 99 #ifdef DROPBEAR_PUBKEY_AUTH
130 case 'd': 100 case 'i':
131 next = &svr_opts.dsskeyfile; 101 nextiskey = 1;
132 break; 102 break;
133 #endif 103 #endif
134 #ifdef DROPBEAR_RSA
135 case 'r':
136 next = &svr_opts.rsakeyfile;
137 break;
138 #endif
139 case 'F':
140 svr_opts.forkbg = 0;
141 break;
142 #ifndef DISABLE_SYSLOG
143 case 'E':
144 svr_opts.usingsyslog = 0;
145 break;
146 #endif
147 #ifndef DISABLE_LOCALTCPFWD
148 case 'j':
149 opts.nolocaltcp = 1;
150 break;
151 #endif
152 #ifndef DISABLE_REMOTETCPFWD
153 case 'k':
154 opts.noremotetcp = 1;
155 break;
156 #endif
157 case 'p':
158 if (portnum < DROPBEAR_MAX_PORTS) {
159 portstring[portnum] = NULL;
160 next = &portstring[portnum];
161 portnum++;
162 }
163 break;
164 #ifdef DO_MOTD
165 /* motd is displayed by default, -m turns it off */
166 case 'm':
167 svr_opts.domotd = 0;
168 break;
169 #endif
170 case 'w':
171 svr_opts.norootlogin = 1;
172 break;
173 #ifdef DROPBEAR_PASSWORD_AUTH
174 case 's':
175 svr_opts.noauthpass = 1;
176 break;
177 case 'g':
178 svr_opts.norootpass = 1;
179 break;
180 #endif
181 case 'h':
182 printhelp(argv[0]);
183 exit(EXIT_FAILURE);
184 break;
185 /*
186 case '4':
187 svr_opts.ipv4 = 0;
188 break;
189 case '6':
190 svr_opts.ipv6 = 0;
191 break;
192 */
193 default: 104 default:
194 fprintf(stderr, "Unknown argument %s\n", argv[i]); 105 fprintf(stderr, "Unknown argument %s\n", argv[i]);
195 printhelp(argv[0]); 106 printhelp(argv[0]);
196 exit(EXIT_FAILURE); 107 exit(EXIT_FAILURE);
197 break; 108 break;
109 } /* Switch */
110
111 } else {
112
113 /* Either the hostname or commands */
114 /* Hostname is first up, must be set before we get the cmds */
115
116 if (cli_opts.remotehost == NULL) {
117 /* We'll be editing it, should probably make a copy */
118 userhostarg = m_strdup(argv[1]);
119
120 cli_opts.remotehost = strchr(userhostarg, '@');
121 if (cli_opts.remotehost == NULL) {
122 /* no username portion, the cli-auth.c code can figure the
123 * local user's name */
124 cli_opts.remotehost = userhostarg;
125 } else {
126 cli_opts.remotehost[0] = '\0'; /* Split the user/host */
127 cli_opts.remotehost++;
128 cli_opts.username = userhostarg;
129 }
130
131 if (cli_opts.username == NULL) {
132 uid = getuid();
133
134 pw = getpwuid(uid);
135 if (pw == NULL || pw->pw_name == NULL) {
136 dropbear_exit("I don't know my own [user]name");
137 }
138
139 cli_opts.username = m_strdup(pw->pw_name);
140 }
141
142 if (cli_opts.remotehost[0] == '\0') {
143 dropbear_exit("Bad hostname");
144 }
145 } else {
146 /* this is part of the commands to send - after this we
147 * don't parse any more options, and flags are sent as the
148 * command */
149 cmdlen = 0;
150 for (j = i; j < (unsigned int)argc; j++) {
151 cmdlen += strlen(argv[j]) + 1; /* +1 for spaces */
152 }
153 /* Allocate the space */
154 cli_opts.cmd = (char*)m_malloc(cmdlen);
155 cli_opts.cmd[0] = '\0';
156
157 /* Append all the bits */
158 for (j = i; j < (unsigned int)argc; j++) {
159 strlcat(cli_opts.cmd, argv[j], cmdlen);
160 strlcat(cli_opts.cmd, " ", cmdlen);
161 }
162 /* It'll be null-terminated here */
163
164 /* We've eaten all the options and flags */
165 break;
198 } 166 }
199 } 167 }
200 } 168 }
201 #endif
202
203 } 169 }